realtime.patch 821 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476
  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/debug-monitors.c linux-4.1.20/arch/arm64/kernel/debug-monitors.c
  1318. --- linux-4.1.20.orig/arch/arm64/kernel/debug-monitors.c 2016-03-17 19:11:03.000000000 +0100
  1319. +++ linux-4.1.20/arch/arm64/kernel/debug-monitors.c 2016-03-21 20:18:28.000000000 +0100
  1320. @@ -184,20 +184,21 @@
  1321. /* EL1 Single Step Handler hooks */
  1322. static LIST_HEAD(step_hook);
  1323. -static DEFINE_RWLOCK(step_hook_lock);
  1324. +static DEFINE_SPINLOCK(step_hook_lock);
  1325. void register_step_hook(struct step_hook *hook)
  1326. {
  1327. - write_lock(&step_hook_lock);
  1328. - list_add(&hook->node, &step_hook);
  1329. - write_unlock(&step_hook_lock);
  1330. + spin_lock(&step_hook_lock);
  1331. + list_add_rcu(&hook->node, &step_hook);
  1332. + spin_unlock(&step_hook_lock);
  1333. }
  1334. void unregister_step_hook(struct step_hook *hook)
  1335. {
  1336. - write_lock(&step_hook_lock);
  1337. - list_del(&hook->node);
  1338. - write_unlock(&step_hook_lock);
  1339. + spin_lock(&step_hook_lock);
  1340. + list_del_rcu(&hook->node);
  1341. + spin_unlock(&step_hook_lock);
  1342. + synchronize_rcu();
  1343. }
  1344. /*
  1345. @@ -211,15 +212,15 @@
  1346. struct step_hook *hook;
  1347. int retval = DBG_HOOK_ERROR;
  1348. - read_lock(&step_hook_lock);
  1349. + rcu_read_lock();
  1350. - list_for_each_entry(hook, &step_hook, node) {
  1351. + list_for_each_entry_rcu(hook, &step_hook, node) {
  1352. retval = hook->fn(regs, esr);
  1353. if (retval == DBG_HOOK_HANDLED)
  1354. break;
  1355. }
  1356. - read_unlock(&step_hook_lock);
  1357. + rcu_read_unlock();
  1358. return retval;
  1359. }
  1360. @@ -271,20 +272,21 @@
  1361. * Use reader/writer locks instead of plain spinlock.
  1362. */
  1363. static LIST_HEAD(break_hook);
  1364. -static DEFINE_RWLOCK(break_hook_lock);
  1365. +static DEFINE_SPINLOCK(break_hook_lock);
  1366. void register_break_hook(struct break_hook *hook)
  1367. {
  1368. - write_lock(&break_hook_lock);
  1369. - list_add(&hook->node, &break_hook);
  1370. - write_unlock(&break_hook_lock);
  1371. + spin_lock(&break_hook_lock);
  1372. + list_add_rcu(&hook->node, &break_hook);
  1373. + spin_unlock(&break_hook_lock);
  1374. }
  1375. void unregister_break_hook(struct break_hook *hook)
  1376. {
  1377. - write_lock(&break_hook_lock);
  1378. - list_del(&hook->node);
  1379. - write_unlock(&break_hook_lock);
  1380. + spin_lock(&break_hook_lock);
  1381. + list_del_rcu(&hook->node);
  1382. + spin_unlock(&break_hook_lock);
  1383. + synchronize_rcu();
  1384. }
  1385. static int call_break_hook(struct pt_regs *regs, unsigned int esr)
  1386. @@ -292,11 +294,11 @@
  1387. struct break_hook *hook;
  1388. int (*fn)(struct pt_regs *regs, unsigned int esr) = NULL;
  1389. - read_lock(&break_hook_lock);
  1390. - list_for_each_entry(hook, &break_hook, node)
  1391. + rcu_read_lock();
  1392. + list_for_each_entry_rcu(hook, &break_hook, node)
  1393. if ((esr & hook->esr_mask) == hook->esr_val)
  1394. fn = hook->fn;
  1395. - read_unlock(&break_hook_lock);
  1396. + rcu_read_unlock();
  1397. return fn ? fn(regs, esr) : DBG_HOOK_ERROR;
  1398. }
  1399. diff -Nur linux-4.1.20.orig/arch/arm64/kernel/entry.S linux-4.1.20/arch/arm64/kernel/entry.S
  1400. --- linux-4.1.20.orig/arch/arm64/kernel/entry.S 2016-03-17 19:11:03.000000000 +0100
  1401. +++ linux-4.1.20/arch/arm64/kernel/entry.S 2016-03-21 20:18:28.000000000 +0100
  1402. @@ -367,11 +367,16 @@
  1403. #ifdef CONFIG_PREEMPT
  1404. get_thread_info tsk
  1405. ldr w24, [tsk, #TI_PREEMPT] // get preempt count
  1406. - cbnz w24, 1f // preempt count != 0
  1407. + cbnz w24, 2f // preempt count != 0
  1408. ldr x0, [tsk, #TI_FLAGS] // get flags
  1409. - tbz x0, #TIF_NEED_RESCHED, 1f // needs rescheduling?
  1410. - bl el1_preempt
  1411. + tbnz x0, #TIF_NEED_RESCHED, 1f // needs rescheduling?
  1412. +
  1413. + ldr w24, [tsk, #TI_PREEMPT_LAZY] // get preempt lazy count
  1414. + cbnz w24, 2f // preempt lazy count != 0
  1415. + tbz x0, #TIF_NEED_RESCHED_LAZY, 2f // needs rescheduling?
  1416. 1:
  1417. + bl el1_preempt
  1418. +2:
  1419. #endif
  1420. #ifdef CONFIG_TRACE_IRQFLAGS
  1421. bl trace_hardirqs_on
  1422. @@ -385,6 +390,7 @@
  1423. 1: bl preempt_schedule_irq // irq en/disable is done inside
  1424. ldr x0, [tsk, #TI_FLAGS] // get new tasks TI_FLAGS
  1425. tbnz x0, #TIF_NEED_RESCHED, 1b // needs rescheduling?
  1426. + tbnz x0, #TIF_NEED_RESCHED_LAZY, 1b // needs rescheduling?
  1427. ret x24
  1428. #endif
  1429. @@ -622,6 +628,7 @@
  1430. str x0, [sp, #S_X0] // returned x0
  1431. work_pending:
  1432. tbnz x1, #TIF_NEED_RESCHED, work_resched
  1433. + tbnz x1, #TIF_NEED_RESCHED_LAZY, work_resched
  1434. /* TIF_SIGPENDING, TIF_NOTIFY_RESUME or TIF_FOREIGN_FPSTATE case */
  1435. ldr x2, [sp, #S_PSTATE]
  1436. mov x0, sp // 'regs'
  1437. diff -Nur linux-4.1.20.orig/arch/arm64/kernel/insn.c linux-4.1.20/arch/arm64/kernel/insn.c
  1438. --- linux-4.1.20.orig/arch/arm64/kernel/insn.c 2016-03-17 19:11:03.000000000 +0100
  1439. +++ linux-4.1.20/arch/arm64/kernel/insn.c 2016-03-21 20:18:28.000000000 +0100
  1440. @@ -77,7 +77,7 @@
  1441. }
  1442. }
  1443. -static DEFINE_SPINLOCK(patch_lock);
  1444. +static DEFINE_RAW_SPINLOCK(patch_lock);
  1445. static void __kprobes *patch_map(void *addr, int fixmap)
  1446. {
  1447. @@ -124,13 +124,13 @@
  1448. unsigned long flags = 0;
  1449. int ret;
  1450. - spin_lock_irqsave(&patch_lock, flags);
  1451. + raw_spin_lock_irqsave(&patch_lock, flags);
  1452. waddr = patch_map(addr, FIX_TEXT_POKE0);
  1453. ret = probe_kernel_write(waddr, &insn, AARCH64_INSN_SIZE);
  1454. patch_unmap(FIX_TEXT_POKE0);
  1455. - spin_unlock_irqrestore(&patch_lock, flags);
  1456. + raw_spin_unlock_irqrestore(&patch_lock, flags);
  1457. return ret;
  1458. }
  1459. diff -Nur linux-4.1.20.orig/arch/arm64/kernel/perf_event.c linux-4.1.20/arch/arm64/kernel/perf_event.c
  1460. --- linux-4.1.20.orig/arch/arm64/kernel/perf_event.c 2016-03-17 19:11:03.000000000 +0100
  1461. +++ linux-4.1.20/arch/arm64/kernel/perf_event.c 2016-03-21 20:18:28.000000000 +0100
  1462. @@ -488,7 +488,7 @@
  1463. }
  1464. err = request_irq(irq, armpmu->handle_irq,
  1465. - IRQF_NOBALANCING,
  1466. + IRQF_NOBALANCING | IRQF_NO_THREAD,
  1467. "arm-pmu", armpmu);
  1468. if (err) {
  1469. pr_err("unable to request IRQ%d for ARM PMU counters\n",
  1470. diff -Nur linux-4.1.20.orig/arch/arm64/mm/fault.c linux-4.1.20/arch/arm64/mm/fault.c
  1471. --- linux-4.1.20.orig/arch/arm64/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  1472. +++ linux-4.1.20/arch/arm64/mm/fault.c 2016-03-21 20:18:28.000000000 +0100
  1473. @@ -211,7 +211,7 @@
  1474. * If we're in an interrupt or have no user context, we must not take
  1475. * the fault.
  1476. */
  1477. - if (in_atomic() || !mm)
  1478. + if (faulthandler_disabled() || !mm)
  1479. goto no_context;
  1480. if (user_mode(regs))
  1481. diff -Nur linux-4.1.20.orig/arch/avr32/include/asm/uaccess.h linux-4.1.20/arch/avr32/include/asm/uaccess.h
  1482. --- linux-4.1.20.orig/arch/avr32/include/asm/uaccess.h 2016-03-17 19:11:03.000000000 +0100
  1483. +++ linux-4.1.20/arch/avr32/include/asm/uaccess.h 2016-03-21 20:18:28.000000000 +0100
  1484. @@ -97,7 +97,8 @@
  1485. * @x: Value to copy to user space.
  1486. * @ptr: Destination address, in user space.
  1487. *
  1488. - * Context: User context only. This function may sleep.
  1489. + * Context: User context only. This function may sleep if pagefaults are
  1490. + * enabled.
  1491. *
  1492. * This macro copies a single simple value from kernel space to user
  1493. * space. It supports simple types like char and int, but not larger
  1494. @@ -116,7 +117,8 @@
  1495. * @x: Variable to store result.
  1496. * @ptr: Source address, in user space.
  1497. *
  1498. - * Context: User context only. This function may sleep.
  1499. + * Context: User context only. This function may sleep if pagefaults are
  1500. + * enabled.
  1501. *
  1502. * This macro copies a single simple variable from user space to kernel
  1503. * space. It supports simple types like char and int, but not larger
  1504. @@ -136,7 +138,8 @@
  1505. * @x: Value to copy to user space.
  1506. * @ptr: Destination address, in user space.
  1507. *
  1508. - * Context: User context only. This function may sleep.
  1509. + * Context: User context only. This function may sleep if pagefaults are
  1510. + * enabled.
  1511. *
  1512. * This macro copies a single simple value from kernel space to user
  1513. * space. It supports simple types like char and int, but not larger
  1514. @@ -158,7 +161,8 @@
  1515. * @x: Variable to store result.
  1516. * @ptr: Source address, in user space.
  1517. *
  1518. - * Context: User context only. This function may sleep.
  1519. + * Context: User context only. This function may sleep if pagefaults are
  1520. + * enabled.
  1521. *
  1522. * This macro copies a single simple variable from user space to kernel
  1523. * space. It supports simple types like char and int, but not larger
  1524. diff -Nur linux-4.1.20.orig/arch/avr32/mm/fault.c linux-4.1.20/arch/avr32/mm/fault.c
  1525. --- linux-4.1.20.orig/arch/avr32/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  1526. +++ linux-4.1.20/arch/avr32/mm/fault.c 2016-03-21 20:18:28.000000000 +0100
  1527. @@ -14,11 +14,11 @@
  1528. #include <linux/pagemap.h>
  1529. #include <linux/kdebug.h>
  1530. #include <linux/kprobes.h>
  1531. +#include <linux/uaccess.h>
  1532. #include <asm/mmu_context.h>
  1533. #include <asm/sysreg.h>
  1534. #include <asm/tlb.h>
  1535. -#include <asm/uaccess.h>
  1536. #ifdef CONFIG_KPROBES
  1537. static inline int notify_page_fault(struct pt_regs *regs, int trap)
  1538. @@ -81,7 +81,7 @@
  1539. * If we're in an interrupt or have no user context, we must
  1540. * not take the fault...
  1541. */
  1542. - if (in_atomic() || !mm || regs->sr & SYSREG_BIT(GM))
  1543. + if (faulthandler_disabled() || !mm || regs->sr & SYSREG_BIT(GM))
  1544. goto no_context;
  1545. local_irq_enable();
  1546. diff -Nur linux-4.1.20.orig/arch/cris/mm/fault.c linux-4.1.20/arch/cris/mm/fault.c
  1547. --- linux-4.1.20.orig/arch/cris/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  1548. +++ linux-4.1.20/arch/cris/mm/fault.c 2016-03-21 20:18:28.000000000 +0100
  1549. @@ -8,7 +8,7 @@
  1550. #include <linux/interrupt.h>
  1551. #include <linux/module.h>
  1552. #include <linux/wait.h>
  1553. -#include <asm/uaccess.h>
  1554. +#include <linux/uaccess.h>
  1555. #include <arch/system.h>
  1556. extern int find_fixup_code(struct pt_regs *);
  1557. @@ -109,11 +109,11 @@
  1558. info.si_code = SEGV_MAPERR;
  1559. /*
  1560. - * If we're in an interrupt or "atomic" operation or have no
  1561. + * If we're in an interrupt, have pagefaults disabled or have no
  1562. * user context, we must not take the fault.
  1563. */
  1564. - if (in_atomic() || !mm)
  1565. + if (faulthandler_disabled() || !mm)
  1566. goto no_context;
  1567. if (user_mode(regs))
  1568. diff -Nur linux-4.1.20.orig/arch/frv/mm/fault.c linux-4.1.20/arch/frv/mm/fault.c
  1569. --- linux-4.1.20.orig/arch/frv/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  1570. +++ linux-4.1.20/arch/frv/mm/fault.c 2016-03-21 20:18:28.000000000 +0100
  1571. @@ -19,9 +19,9 @@
  1572. #include <linux/kernel.h>
  1573. #include <linux/ptrace.h>
  1574. #include <linux/hardirq.h>
  1575. +#include <linux/uaccess.h>
  1576. #include <asm/pgtable.h>
  1577. -#include <asm/uaccess.h>
  1578. #include <asm/gdb-stub.h>
  1579. /*****************************************************************************/
  1580. @@ -78,7 +78,7 @@
  1581. * If we're in an interrupt or have no user
  1582. * context, we must not take the fault..
  1583. */
  1584. - if (in_atomic() || !mm)
  1585. + if (faulthandler_disabled() || !mm)
  1586. goto no_context;
  1587. if (user_mode(__frame))
  1588. diff -Nur linux-4.1.20.orig/arch/frv/mm/highmem.c linux-4.1.20/arch/frv/mm/highmem.c
  1589. --- linux-4.1.20.orig/arch/frv/mm/highmem.c 2016-03-17 19:11:03.000000000 +0100
  1590. +++ linux-4.1.20/arch/frv/mm/highmem.c 2016-03-21 20:18:28.000000000 +0100
  1591. @@ -42,6 +42,7 @@
  1592. unsigned long paddr;
  1593. int type;
  1594. + preempt_disable();
  1595. pagefault_disable();
  1596. type = kmap_atomic_idx_push();
  1597. paddr = page_to_phys(page);
  1598. @@ -85,5 +86,6 @@
  1599. }
  1600. kmap_atomic_idx_pop();
  1601. pagefault_enable();
  1602. + preempt_enable();
  1603. }
  1604. EXPORT_SYMBOL(__kunmap_atomic);
  1605. diff -Nur linux-4.1.20.orig/arch/hexagon/include/asm/uaccess.h linux-4.1.20/arch/hexagon/include/asm/uaccess.h
  1606. --- linux-4.1.20.orig/arch/hexagon/include/asm/uaccess.h 2016-03-17 19:11:03.000000000 +0100
  1607. +++ linux-4.1.20/arch/hexagon/include/asm/uaccess.h 2016-03-21 20:18:28.000000000 +0100
  1608. @@ -36,7 +36,8 @@
  1609. * @addr: User space pointer to start of block to check
  1610. * @size: Size of block to check
  1611. *
  1612. - * Context: User context only. This function may sleep.
  1613. + * Context: User context only. This function may sleep if pagefaults are
  1614. + * enabled.
  1615. *
  1616. * Checks if a pointer to a block of memory in user space is valid.
  1617. *
  1618. diff -Nur linux-4.1.20.orig/arch/ia64/mm/fault.c linux-4.1.20/arch/ia64/mm/fault.c
  1619. --- linux-4.1.20.orig/arch/ia64/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  1620. +++ linux-4.1.20/arch/ia64/mm/fault.c 2016-03-21 20:18:28.000000000 +0100
  1621. @@ -11,10 +11,10 @@
  1622. #include <linux/kprobes.h>
  1623. #include <linux/kdebug.h>
  1624. #include <linux/prefetch.h>
  1625. +#include <linux/uaccess.h>
  1626. #include <asm/pgtable.h>
  1627. #include <asm/processor.h>
  1628. -#include <asm/uaccess.h>
  1629. extern int die(char *, struct pt_regs *, long);
  1630. @@ -96,7 +96,7 @@
  1631. /*
  1632. * If we're in an interrupt or have no user context, we must not take the fault..
  1633. */
  1634. - if (in_atomic() || !mm)
  1635. + if (faulthandler_disabled() || !mm)
  1636. goto no_context;
  1637. #ifdef CONFIG_VIRTUAL_MEM_MAP
  1638. diff -Nur linux-4.1.20.orig/arch/m32r/include/asm/uaccess.h linux-4.1.20/arch/m32r/include/asm/uaccess.h
  1639. --- linux-4.1.20.orig/arch/m32r/include/asm/uaccess.h 2016-03-17 19:11:03.000000000 +0100
  1640. +++ linux-4.1.20/arch/m32r/include/asm/uaccess.h 2016-03-21 20:18:29.000000000 +0100
  1641. @@ -91,7 +91,8 @@
  1642. * @addr: User space pointer to start of block to check
  1643. * @size: Size of block to check
  1644. *
  1645. - * Context: User context only. This function may sleep.
  1646. + * Context: User context only. This function may sleep if pagefaults are
  1647. + * enabled.
  1648. *
  1649. * Checks if a pointer to a block of memory in user space is valid.
  1650. *
  1651. @@ -155,7 +156,8 @@
  1652. * @x: Variable to store result.
  1653. * @ptr: Source address, in user space.
  1654. *
  1655. - * Context: User context only. This function may sleep.
  1656. + * Context: User context only. This function may sleep if pagefaults are
  1657. + * enabled.
  1658. *
  1659. * This macro copies a single simple variable from user space to kernel
  1660. * space. It supports simple types like char and int, but not larger
  1661. @@ -175,7 +177,8 @@
  1662. * @x: Value to copy to user space.
  1663. * @ptr: Destination address, in user space.
  1664. *
  1665. - * Context: User context only. This function may sleep.
  1666. + * Context: User context only. This function may sleep if pagefaults are
  1667. + * enabled.
  1668. *
  1669. * This macro copies a single simple value from kernel space to user
  1670. * space. It supports simple types like char and int, but not larger
  1671. @@ -194,7 +197,8 @@
  1672. * @x: Variable to store result.
  1673. * @ptr: Source address, in user space.
  1674. *
  1675. - * Context: User context only. This function may sleep.
  1676. + * Context: User context only. This function may sleep if pagefaults are
  1677. + * enabled.
  1678. *
  1679. * This macro copies a single simple variable from user space to kernel
  1680. * space. It supports simple types like char and int, but not larger
  1681. @@ -274,7 +278,8 @@
  1682. * @x: Value to copy to user space.
  1683. * @ptr: Destination address, in user space.
  1684. *
  1685. - * Context: User context only. This function may sleep.
  1686. + * Context: User context only. This function may sleep if pagefaults are
  1687. + * enabled.
  1688. *
  1689. * This macro copies a single simple value from kernel space to user
  1690. * space. It supports simple types like char and int, but not larger
  1691. @@ -568,7 +573,8 @@
  1692. * @from: Source address, in kernel space.
  1693. * @n: Number of bytes to copy.
  1694. *
  1695. - * Context: User context only. This function may sleep.
  1696. + * Context: User context only. This function may sleep if pagefaults are
  1697. + * enabled.
  1698. *
  1699. * Copy data from kernel space to user space. Caller must check
  1700. * the specified block with access_ok() before calling this function.
  1701. @@ -588,7 +594,8 @@
  1702. * @from: Source address, in kernel space.
  1703. * @n: Number of bytes to copy.
  1704. *
  1705. - * Context: User context only. This function may sleep.
  1706. + * Context: User context only. This function may sleep if pagefaults are
  1707. + * enabled.
  1708. *
  1709. * Copy data from kernel space to user space.
  1710. *
  1711. @@ -606,7 +613,8 @@
  1712. * @from: Source address, in user space.
  1713. * @n: Number of bytes to copy.
  1714. *
  1715. - * Context: User context only. This function may sleep.
  1716. + * Context: User context only. This function may sleep if pagefaults are
  1717. + * enabled.
  1718. *
  1719. * Copy data from user space to kernel space. Caller must check
  1720. * the specified block with access_ok() before calling this function.
  1721. @@ -626,7 +634,8 @@
  1722. * @from: Source address, in user space.
  1723. * @n: Number of bytes to copy.
  1724. *
  1725. - * Context: User context only. This function may sleep.
  1726. + * Context: User context only. This function may sleep if pagefaults are
  1727. + * enabled.
  1728. *
  1729. * Copy data from user space to kernel space.
  1730. *
  1731. @@ -677,7 +686,8 @@
  1732. * strlen_user: - Get the size of a string in user space.
  1733. * @str: The string to measure.
  1734. *
  1735. - * Context: User context only. This function may sleep.
  1736. + * Context: User context only. This function may sleep if pagefaults are
  1737. + * enabled.
  1738. *
  1739. * Get the size of a NUL-terminated string in user space.
  1740. *
  1741. diff -Nur linux-4.1.20.orig/arch/m32r/mm/fault.c linux-4.1.20/arch/m32r/mm/fault.c
  1742. --- linux-4.1.20.orig/arch/m32r/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  1743. +++ linux-4.1.20/arch/m32r/mm/fault.c 2016-03-21 20:18:29.000000000 +0100
  1744. @@ -24,9 +24,9 @@
  1745. #include <linux/vt_kern.h> /* For unblank_screen() */
  1746. #include <linux/highmem.h>
  1747. #include <linux/module.h>
  1748. +#include <linux/uaccess.h>
  1749. #include <asm/m32r.h>
  1750. -#include <asm/uaccess.h>
  1751. #include <asm/hardirq.h>
  1752. #include <asm/mmu_context.h>
  1753. #include <asm/tlbflush.h>
  1754. @@ -111,10 +111,10 @@
  1755. mm = tsk->mm;
  1756. /*
  1757. - * If we're in an interrupt or have no user context or are running in an
  1758. - * atomic region then we must not take the fault..
  1759. + * If we're in an interrupt or have no user context or have pagefaults
  1760. + * disabled then we must not take the fault.
  1761. */
  1762. - if (in_atomic() || !mm)
  1763. + if (faulthandler_disabled() || !mm)
  1764. goto bad_area_nosemaphore;
  1765. if (error_code & ACE_USERMODE)
  1766. diff -Nur linux-4.1.20.orig/arch/m68k/mm/fault.c linux-4.1.20/arch/m68k/mm/fault.c
  1767. --- linux-4.1.20.orig/arch/m68k/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  1768. +++ linux-4.1.20/arch/m68k/mm/fault.c 2016-03-21 20:18:29.000000000 +0100
  1769. @@ -10,10 +10,10 @@
  1770. #include <linux/ptrace.h>
  1771. #include <linux/interrupt.h>
  1772. #include <linux/module.h>
  1773. +#include <linux/uaccess.h>
  1774. #include <asm/setup.h>
  1775. #include <asm/traps.h>
  1776. -#include <asm/uaccess.h>
  1777. #include <asm/pgalloc.h>
  1778. extern void die_if_kernel(char *, struct pt_regs *, long);
  1779. @@ -81,7 +81,7 @@
  1780. * If we're in an interrupt or have no user
  1781. * context, we must not take the fault..
  1782. */
  1783. - if (in_atomic() || !mm)
  1784. + if (faulthandler_disabled() || !mm)
  1785. goto no_context;
  1786. if (user_mode(regs))
  1787. diff -Nur linux-4.1.20.orig/arch/metag/mm/fault.c linux-4.1.20/arch/metag/mm/fault.c
  1788. --- linux-4.1.20.orig/arch/metag/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  1789. +++ linux-4.1.20/arch/metag/mm/fault.c 2016-03-21 20:18:29.000000000 +0100
  1790. @@ -105,7 +105,7 @@
  1791. mm = tsk->mm;
  1792. - if (in_atomic() || !mm)
  1793. + if (faulthandler_disabled() || !mm)
  1794. goto no_context;
  1795. if (user_mode(regs))
  1796. diff -Nur linux-4.1.20.orig/arch/metag/mm/highmem.c linux-4.1.20/arch/metag/mm/highmem.c
  1797. --- linux-4.1.20.orig/arch/metag/mm/highmem.c 2016-03-17 19:11:03.000000000 +0100
  1798. +++ linux-4.1.20/arch/metag/mm/highmem.c 2016-03-21 20:18:29.000000000 +0100
  1799. @@ -43,7 +43,7 @@
  1800. unsigned long vaddr;
  1801. int type;
  1802. - /* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */
  1803. + preempt_disable();
  1804. pagefault_disable();
  1805. if (!PageHighMem(page))
  1806. return page_address(page);
  1807. @@ -82,6 +82,7 @@
  1808. }
  1809. pagefault_enable();
  1810. + preempt_enable();
  1811. }
  1812. EXPORT_SYMBOL(__kunmap_atomic);
  1813. @@ -95,6 +96,7 @@
  1814. unsigned long vaddr;
  1815. int type;
  1816. + preempt_disable();
  1817. pagefault_disable();
  1818. type = kmap_atomic_idx_push();
  1819. diff -Nur linux-4.1.20.orig/arch/microblaze/include/asm/uaccess.h linux-4.1.20/arch/microblaze/include/asm/uaccess.h
  1820. --- linux-4.1.20.orig/arch/microblaze/include/asm/uaccess.h 2016-03-17 19:11:03.000000000 +0100
  1821. +++ linux-4.1.20/arch/microblaze/include/asm/uaccess.h 2016-03-21 20:18:29.000000000 +0100
  1822. @@ -178,7 +178,8 @@
  1823. * @x: Variable to store result.
  1824. * @ptr: Source address, in user space.
  1825. *
  1826. - * Context: User context only. This function may sleep.
  1827. + * Context: User context only. This function may sleep if pagefaults are
  1828. + * enabled.
  1829. *
  1830. * This macro copies a single simple variable from user space to kernel
  1831. * space. It supports simple types like char and int, but not larger
  1832. @@ -290,7 +291,8 @@
  1833. * @x: Value to copy to user space.
  1834. * @ptr: Destination address, in user space.
  1835. *
  1836. - * Context: User context only. This function may sleep.
  1837. + * Context: User context only. This function may sleep if pagefaults are
  1838. + * enabled.
  1839. *
  1840. * This macro copies a single simple value from kernel space to user
  1841. * space. It supports simple types like char and int, but not larger
  1842. diff -Nur linux-4.1.20.orig/arch/microblaze/mm/fault.c linux-4.1.20/arch/microblaze/mm/fault.c
  1843. --- linux-4.1.20.orig/arch/microblaze/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  1844. +++ linux-4.1.20/arch/microblaze/mm/fault.c 2016-03-21 20:18:29.000000000 +0100
  1845. @@ -107,14 +107,14 @@
  1846. if ((error_code & 0x13) == 0x13 || (error_code & 0x11) == 0x11)
  1847. is_write = 0;
  1848. - if (unlikely(in_atomic() || !mm)) {
  1849. + if (unlikely(faulthandler_disabled() || !mm)) {
  1850. if (kernel_mode(regs))
  1851. goto bad_area_nosemaphore;
  1852. - /* in_atomic() in user mode is really bad,
  1853. + /* faulthandler_disabled() in user mode is really bad,
  1854. as is current->mm == NULL. */
  1855. - pr_emerg("Page fault in user mode with in_atomic(), mm = %p\n",
  1856. - mm);
  1857. + pr_emerg("Page fault in user mode with faulthandler_disabled(), mm = %p\n",
  1858. + mm);
  1859. pr_emerg("r15 = %lx MSR = %lx\n",
  1860. regs->r15, regs->msr);
  1861. die("Weird page fault", regs, SIGSEGV);
  1862. diff -Nur linux-4.1.20.orig/arch/microblaze/mm/highmem.c linux-4.1.20/arch/microblaze/mm/highmem.c
  1863. --- linux-4.1.20.orig/arch/microblaze/mm/highmem.c 2016-03-17 19:11:03.000000000 +0100
  1864. +++ linux-4.1.20/arch/microblaze/mm/highmem.c 2016-03-21 20:18:29.000000000 +0100
  1865. @@ -37,7 +37,7 @@
  1866. unsigned long vaddr;
  1867. int idx, type;
  1868. - /* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */
  1869. + preempt_disable();
  1870. pagefault_disable();
  1871. if (!PageHighMem(page))
  1872. return page_address(page);
  1873. @@ -63,6 +63,7 @@
  1874. if (vaddr < __fix_to_virt(FIX_KMAP_END)) {
  1875. pagefault_enable();
  1876. + preempt_enable();
  1877. return;
  1878. }
  1879. @@ -84,5 +85,6 @@
  1880. #endif
  1881. kmap_atomic_idx_pop();
  1882. pagefault_enable();
  1883. + preempt_enable();
  1884. }
  1885. EXPORT_SYMBOL(__kunmap_atomic);
  1886. diff -Nur linux-4.1.20.orig/arch/mips/Kconfig linux-4.1.20/arch/mips/Kconfig
  1887. --- linux-4.1.20.orig/arch/mips/Kconfig 2016-03-17 19:11:03.000000000 +0100
  1888. +++ linux-4.1.20/arch/mips/Kconfig 2016-03-21 20:18:29.000000000 +0100
  1889. @@ -2366,7 +2366,7 @@
  1890. #
  1891. config HIGHMEM
  1892. bool "High Memory Support"
  1893. - depends on 32BIT && CPU_SUPPORTS_HIGHMEM && SYS_SUPPORTS_HIGHMEM && !CPU_MIPS32_3_5_EVA
  1894. + depends on 32BIT && CPU_SUPPORTS_HIGHMEM && SYS_SUPPORTS_HIGHMEM && !CPU_MIPS32_3_5_EVA && !PREEMPT_RT_FULL
  1895. config CPU_SUPPORTS_HIGHMEM
  1896. bool
  1897. diff -Nur linux-4.1.20.orig/arch/mips/include/asm/uaccess.h linux-4.1.20/arch/mips/include/asm/uaccess.h
  1898. --- linux-4.1.20.orig/arch/mips/include/asm/uaccess.h 2016-03-17 19:11:03.000000000 +0100
  1899. +++ linux-4.1.20/arch/mips/include/asm/uaccess.h 2016-03-21 20:18:29.000000000 +0100
  1900. @@ -103,7 +103,8 @@
  1901. * @addr: User space pointer to start of block to check
  1902. * @size: Size of block to check
  1903. *
  1904. - * Context: User context only. This function may sleep.
  1905. + * Context: User context only. This function may sleep if pagefaults are
  1906. + * enabled.
  1907. *
  1908. * Checks if a pointer to a block of memory in user space is valid.
  1909. *
  1910. @@ -138,7 +139,8 @@
  1911. * @x: Value to copy to user space.
  1912. * @ptr: Destination address, in user space.
  1913. *
  1914. - * Context: User context only. This function may sleep.
  1915. + * Context: User context only. This function may sleep if pagefaults are
  1916. + * enabled.
  1917. *
  1918. * This macro copies a single simple value from kernel space to user
  1919. * space. It supports simple types like char and int, but not larger
  1920. @@ -157,7 +159,8 @@
  1921. * @x: Variable to store result.
  1922. * @ptr: Source address, in user space.
  1923. *
  1924. - * Context: User context only. This function may sleep.
  1925. + * Context: User context only. This function may sleep if pagefaults are
  1926. + * enabled.
  1927. *
  1928. * This macro copies a single simple variable from user space to kernel
  1929. * space. It supports simple types like char and int, but not larger
  1930. @@ -177,7 +180,8 @@
  1931. * @x: Value to copy to user space.
  1932. * @ptr: Destination address, in user space.
  1933. *
  1934. - * Context: User context only. This function may sleep.
  1935. + * Context: User context only. This function may sleep if pagefaults are
  1936. + * enabled.
  1937. *
  1938. * This macro copies a single simple value from kernel space to user
  1939. * space. It supports simple types like char and int, but not larger
  1940. @@ -199,7 +203,8 @@
  1941. * @x: Variable to store result.
  1942. * @ptr: Source address, in user space.
  1943. *
  1944. - * Context: User context only. This function may sleep.
  1945. + * Context: User context only. This function may sleep if pagefaults are
  1946. + * enabled.
  1947. *
  1948. * This macro copies a single simple variable from user space to kernel
  1949. * space. It supports simple types like char and int, but not larger
  1950. @@ -498,7 +503,8 @@
  1951. * @x: Value to copy to user space.
  1952. * @ptr: Destination address, in user space.
  1953. *
  1954. - * Context: User context only. This function may sleep.
  1955. + * Context: User context only. This function may sleep if pagefaults are
  1956. + * enabled.
  1957. *
  1958. * This macro copies a single simple value from kernel space to user
  1959. * space. It supports simple types like char and int, but not larger
  1960. @@ -517,7 +523,8 @@
  1961. * @x: Variable to store result.
  1962. * @ptr: Source address, in user space.
  1963. *
  1964. - * Context: User context only. This function may sleep.
  1965. + * Context: User context only. This function may sleep if pagefaults are
  1966. + * enabled.
  1967. *
  1968. * This macro copies a single simple variable from user space to kernel
  1969. * space. It supports simple types like char and int, but not larger
  1970. @@ -537,7 +544,8 @@
  1971. * @x: Value to copy to user space.
  1972. * @ptr: Destination address, in user space.
  1973. *
  1974. - * Context: User context only. This function may sleep.
  1975. + * Context: User context only. This function may sleep if pagefaults are
  1976. + * enabled.
  1977. *
  1978. * This macro copies a single simple value from kernel space to user
  1979. * space. It supports simple types like char and int, but not larger
  1980. @@ -559,7 +567,8 @@
  1981. * @x: Variable to store result.
  1982. * @ptr: Source address, in user space.
  1983. *
  1984. - * Context: User context only. This function may sleep.
  1985. + * Context: User context only. This function may sleep if pagefaults are
  1986. + * enabled.
  1987. *
  1988. * This macro copies a single simple variable from user space to kernel
  1989. * space. It supports simple types like char and int, but not larger
  1990. @@ -815,7 +824,8 @@
  1991. * @from: Source address, in kernel space.
  1992. * @n: Number of bytes to copy.
  1993. *
  1994. - * Context: User context only. This function may sleep.
  1995. + * Context: User context only. This function may sleep if pagefaults are
  1996. + * enabled.
  1997. *
  1998. * Copy data from kernel space to user space. Caller must check
  1999. * the specified block with access_ok() before calling this function.
  2000. @@ -888,7 +898,8 @@
  2001. * @from: Source address, in kernel space.
  2002. * @n: Number of bytes to copy.
  2003. *
  2004. - * Context: User context only. This function may sleep.
  2005. + * Context: User context only. This function may sleep if pagefaults are
  2006. + * enabled.
  2007. *
  2008. * Copy data from kernel space to user space.
  2009. *
  2010. @@ -1075,7 +1086,8 @@
  2011. * @from: Source address, in user space.
  2012. * @n: Number of bytes to copy.
  2013. *
  2014. - * Context: User context only. This function may sleep.
  2015. + * Context: User context only. This function may sleep if pagefaults are
  2016. + * enabled.
  2017. *
  2018. * Copy data from user space to kernel space. Caller must check
  2019. * the specified block with access_ok() before calling this function.
  2020. @@ -1107,7 +1119,8 @@
  2021. * @from: Source address, in user space.
  2022. * @n: Number of bytes to copy.
  2023. *
  2024. - * Context: User context only. This function may sleep.
  2025. + * Context: User context only. This function may sleep if pagefaults are
  2026. + * enabled.
  2027. *
  2028. * Copy data from user space to kernel space.
  2029. *
  2030. @@ -1329,7 +1342,8 @@
  2031. * strlen_user: - Get the size of a string in user space.
  2032. * @str: The string to measure.
  2033. *
  2034. - * Context: User context only. This function may sleep.
  2035. + * Context: User context only. This function may sleep if pagefaults are
  2036. + * enabled.
  2037. *
  2038. * Get the size of a NUL-terminated string in user space.
  2039. *
  2040. @@ -1398,7 +1412,8 @@
  2041. * strnlen_user: - Get the size of a string in user space.
  2042. * @str: The string to measure.
  2043. *
  2044. - * Context: User context only. This function may sleep.
  2045. + * Context: User context only. This function may sleep if pagefaults are
  2046. + * enabled.
  2047. *
  2048. * Get the size of a NUL-terminated string in user space.
  2049. *
  2050. diff -Nur linux-4.1.20.orig/arch/mips/kernel/signal-common.h linux-4.1.20/arch/mips/kernel/signal-common.h
  2051. --- linux-4.1.20.orig/arch/mips/kernel/signal-common.h 2016-03-17 19:11:03.000000000 +0100
  2052. +++ linux-4.1.20/arch/mips/kernel/signal-common.h 2016-03-21 20:18:29.000000000 +0100
  2053. @@ -28,12 +28,7 @@
  2054. extern int fpcsr_pending(unsigned int __user *fpcsr);
  2055. /* Make sure we will not lose FPU ownership */
  2056. -#ifdef CONFIG_PREEMPT
  2057. -#define lock_fpu_owner() preempt_disable()
  2058. -#define unlock_fpu_owner() preempt_enable()
  2059. -#else
  2060. -#define lock_fpu_owner() pagefault_disable()
  2061. -#define unlock_fpu_owner() pagefault_enable()
  2062. -#endif
  2063. +#define lock_fpu_owner() ({ preempt_disable(); pagefault_disable(); })
  2064. +#define unlock_fpu_owner() ({ pagefault_enable(); preempt_enable(); })
  2065. #endif /* __SIGNAL_COMMON_H */
  2066. diff -Nur linux-4.1.20.orig/arch/mips/mm/fault.c linux-4.1.20/arch/mips/mm/fault.c
  2067. --- linux-4.1.20.orig/arch/mips/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  2068. +++ linux-4.1.20/arch/mips/mm/fault.c 2016-03-21 20:18:29.000000000 +0100
  2069. @@ -21,10 +21,10 @@
  2070. #include <linux/module.h>
  2071. #include <linux/kprobes.h>
  2072. #include <linux/perf_event.h>
  2073. +#include <linux/uaccess.h>
  2074. #include <asm/branch.h>
  2075. #include <asm/mmu_context.h>
  2076. -#include <asm/uaccess.h>
  2077. #include <asm/ptrace.h>
  2078. #include <asm/highmem.h> /* For VMALLOC_END */
  2079. #include <linux/kdebug.h>
  2080. @@ -94,7 +94,7 @@
  2081. * If we're in an interrupt or have no user
  2082. * context, we must not take the fault..
  2083. */
  2084. - if (in_atomic() || !mm)
  2085. + if (faulthandler_disabled() || !mm)
  2086. goto bad_area_nosemaphore;
  2087. if (user_mode(regs))
  2088. diff -Nur linux-4.1.20.orig/arch/mips/mm/highmem.c linux-4.1.20/arch/mips/mm/highmem.c
  2089. --- linux-4.1.20.orig/arch/mips/mm/highmem.c 2016-03-17 19:11:03.000000000 +0100
  2090. +++ linux-4.1.20/arch/mips/mm/highmem.c 2016-03-21 20:18:29.000000000 +0100
  2091. @@ -47,7 +47,7 @@
  2092. unsigned long vaddr;
  2093. int idx, type;
  2094. - /* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */
  2095. + preempt_disable();
  2096. pagefault_disable();
  2097. if (!PageHighMem(page))
  2098. return page_address(page);
  2099. @@ -72,6 +72,7 @@
  2100. if (vaddr < FIXADDR_START) { // FIXME
  2101. pagefault_enable();
  2102. + preempt_enable();
  2103. return;
  2104. }
  2105. @@ -92,6 +93,7 @@
  2106. #endif
  2107. kmap_atomic_idx_pop();
  2108. pagefault_enable();
  2109. + preempt_enable();
  2110. }
  2111. EXPORT_SYMBOL(__kunmap_atomic);
  2112. @@ -104,6 +106,7 @@
  2113. unsigned long vaddr;
  2114. int idx, type;
  2115. + preempt_disable();
  2116. pagefault_disable();
  2117. type = kmap_atomic_idx_push();
  2118. diff -Nur linux-4.1.20.orig/arch/mips/mm/init.c linux-4.1.20/arch/mips/mm/init.c
  2119. --- linux-4.1.20.orig/arch/mips/mm/init.c 2016-03-17 19:11:03.000000000 +0100
  2120. +++ linux-4.1.20/arch/mips/mm/init.c 2016-03-21 20:18:29.000000000 +0100
  2121. @@ -90,6 +90,7 @@
  2122. BUG_ON(Page_dcache_dirty(page));
  2123. + preempt_disable();
  2124. pagefault_disable();
  2125. idx = (addr >> PAGE_SHIFT) & (FIX_N_COLOURS - 1);
  2126. idx += in_interrupt() ? FIX_N_COLOURS : 0;
  2127. @@ -152,6 +153,7 @@
  2128. write_c0_entryhi(old_ctx);
  2129. local_irq_restore(flags);
  2130. pagefault_enable();
  2131. + preempt_enable();
  2132. }
  2133. void copy_user_highpage(struct page *to, struct page *from,
  2134. diff -Nur linux-4.1.20.orig/arch/mn10300/include/asm/highmem.h linux-4.1.20/arch/mn10300/include/asm/highmem.h
  2135. --- linux-4.1.20.orig/arch/mn10300/include/asm/highmem.h 2016-03-17 19:11:03.000000000 +0100
  2136. +++ linux-4.1.20/arch/mn10300/include/asm/highmem.h 2016-03-21 20:18:29.000000000 +0100
  2137. @@ -75,6 +75,7 @@
  2138. unsigned long vaddr;
  2139. int idx, type;
  2140. + preempt_disable();
  2141. pagefault_disable();
  2142. if (page < highmem_start_page)
  2143. return page_address(page);
  2144. @@ -98,6 +99,7 @@
  2145. if (vaddr < FIXADDR_START) { /* FIXME */
  2146. pagefault_enable();
  2147. + preempt_enable();
  2148. return;
  2149. }
  2150. @@ -122,6 +124,7 @@
  2151. kmap_atomic_idx_pop();
  2152. pagefault_enable();
  2153. + preempt_enable();
  2154. }
  2155. #endif /* __KERNEL__ */
  2156. diff -Nur linux-4.1.20.orig/arch/mn10300/mm/fault.c linux-4.1.20/arch/mn10300/mm/fault.c
  2157. --- linux-4.1.20.orig/arch/mn10300/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  2158. +++ linux-4.1.20/arch/mn10300/mm/fault.c 2016-03-21 20:18:29.000000000 +0100
  2159. @@ -23,8 +23,8 @@
  2160. #include <linux/interrupt.h>
  2161. #include <linux/init.h>
  2162. #include <linux/vt_kern.h> /* For unblank_screen() */
  2163. +#include <linux/uaccess.h>
  2164. -#include <asm/uaccess.h>
  2165. #include <asm/pgalloc.h>
  2166. #include <asm/hardirq.h>
  2167. #include <asm/cpu-regs.h>
  2168. @@ -168,7 +168,7 @@
  2169. * If we're in an interrupt or have no user
  2170. * context, we must not take the fault..
  2171. */
  2172. - if (in_atomic() || !mm)
  2173. + if (faulthandler_disabled() || !mm)
  2174. goto no_context;
  2175. if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_USR)
  2176. diff -Nur linux-4.1.20.orig/arch/nios2/mm/fault.c linux-4.1.20/arch/nios2/mm/fault.c
  2177. --- linux-4.1.20.orig/arch/nios2/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  2178. +++ linux-4.1.20/arch/nios2/mm/fault.c 2016-03-21 20:18:29.000000000 +0100
  2179. @@ -77,7 +77,7 @@
  2180. * If we're in an interrupt or have no user
  2181. * context, we must not take the fault..
  2182. */
  2183. - if (in_atomic() || !mm)
  2184. + if (faulthandler_disabled() || !mm)
  2185. goto bad_area_nosemaphore;
  2186. if (user_mode(regs))
  2187. diff -Nur linux-4.1.20.orig/arch/parisc/include/asm/cacheflush.h linux-4.1.20/arch/parisc/include/asm/cacheflush.h
  2188. --- linux-4.1.20.orig/arch/parisc/include/asm/cacheflush.h 2016-03-17 19:11:03.000000000 +0100
  2189. +++ linux-4.1.20/arch/parisc/include/asm/cacheflush.h 2016-03-21 20:18:29.000000000 +0100
  2190. @@ -142,6 +142,7 @@
  2191. static inline void *kmap_atomic(struct page *page)
  2192. {
  2193. + preempt_disable();
  2194. pagefault_disable();
  2195. return page_address(page);
  2196. }
  2197. @@ -150,6 +151,7 @@
  2198. {
  2199. flush_kernel_dcache_page_addr(addr);
  2200. pagefault_enable();
  2201. + preempt_enable();
  2202. }
  2203. #define kmap_atomic_prot(page, prot) kmap_atomic(page)
  2204. diff -Nur linux-4.1.20.orig/arch/parisc/kernel/traps.c linux-4.1.20/arch/parisc/kernel/traps.c
  2205. --- linux-4.1.20.orig/arch/parisc/kernel/traps.c 2016-03-17 19:11:03.000000000 +0100
  2206. +++ linux-4.1.20/arch/parisc/kernel/traps.c 2016-03-21 20:18:29.000000000 +0100
  2207. @@ -26,9 +26,9 @@
  2208. #include <linux/console.h>
  2209. #include <linux/bug.h>
  2210. #include <linux/ratelimit.h>
  2211. +#include <linux/uaccess.h>
  2212. #include <asm/assembly.h>
  2213. -#include <asm/uaccess.h>
  2214. #include <asm/io.h>
  2215. #include <asm/irq.h>
  2216. #include <asm/traps.h>
  2217. @@ -796,7 +796,7 @@
  2218. * unless pagefault_disable() was called before.
  2219. */
  2220. - if (fault_space == 0 && !in_atomic())
  2221. + if (fault_space == 0 && !faulthandler_disabled())
  2222. {
  2223. pdc_chassis_send_status(PDC_CHASSIS_DIRECT_PANIC);
  2224. parisc_terminate("Kernel Fault", regs, code, fault_address);
  2225. diff -Nur linux-4.1.20.orig/arch/parisc/mm/fault.c linux-4.1.20/arch/parisc/mm/fault.c
  2226. --- linux-4.1.20.orig/arch/parisc/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  2227. +++ linux-4.1.20/arch/parisc/mm/fault.c 2016-03-21 20:18:29.000000000 +0100
  2228. @@ -15,8 +15,8 @@
  2229. #include <linux/sched.h>
  2230. #include <linux/interrupt.h>
  2231. #include <linux/module.h>
  2232. +#include <linux/uaccess.h>
  2233. -#include <asm/uaccess.h>
  2234. #include <asm/traps.h>
  2235. /* Various important other fields */
  2236. @@ -207,7 +207,7 @@
  2237. int fault;
  2238. unsigned int flags;
  2239. - if (in_atomic())
  2240. + if (pagefault_disabled())
  2241. goto no_context;
  2242. tsk = current;
  2243. diff -Nur linux-4.1.20.orig/arch/powerpc/Kconfig linux-4.1.20/arch/powerpc/Kconfig
  2244. --- linux-4.1.20.orig/arch/powerpc/Kconfig 2016-03-17 19:11:03.000000000 +0100
  2245. +++ linux-4.1.20/arch/powerpc/Kconfig 2016-03-21 20:18:29.000000000 +0100
  2246. @@ -60,10 +60,11 @@
  2247. config RWSEM_GENERIC_SPINLOCK
  2248. bool
  2249. + default y if PREEMPT_RT_FULL
  2250. config RWSEM_XCHGADD_ALGORITHM
  2251. bool
  2252. - default y
  2253. + default y if !PREEMPT_RT_FULL
  2254. config GENERIC_LOCKBREAK
  2255. bool
  2256. @@ -138,6 +139,7 @@
  2257. select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST
  2258. select GENERIC_STRNCPY_FROM_USER
  2259. select GENERIC_STRNLEN_USER
  2260. + select HAVE_PREEMPT_LAZY
  2261. select HAVE_MOD_ARCH_SPECIFIC
  2262. select MODULES_USE_ELF_RELA
  2263. select CLONE_BACKWARDS
  2264. @@ -312,7 +314,7 @@
  2265. config HIGHMEM
  2266. bool "High memory support"
  2267. - depends on PPC32
  2268. + depends on PPC32 && !PREEMPT_RT_FULL
  2269. source kernel/Kconfig.hz
  2270. source kernel/Kconfig.preempt
  2271. 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
  2272. --- linux-4.1.20.orig/arch/powerpc/include/asm/kvm_host.h 2016-03-17 19:11:03.000000000 +0100
  2273. +++ linux-4.1.20/arch/powerpc/include/asm/kvm_host.h 2016-03-21 20:18:29.000000000 +0100
  2274. @@ -280,7 +280,7 @@
  2275. u8 in_guest;
  2276. struct list_head runnable_threads;
  2277. spinlock_t lock;
  2278. - wait_queue_head_t wq;
  2279. + struct swait_head wq;
  2280. spinlock_t stoltb_lock; /* protects stolen_tb and preempt_tb */
  2281. u64 stolen_tb;
  2282. u64 preempt_tb;
  2283. @@ -613,7 +613,7 @@
  2284. u8 prodded;
  2285. u32 last_inst;
  2286. - wait_queue_head_t *wqp;
  2287. + struct swait_head *wqp;
  2288. struct kvmppc_vcore *vcore;
  2289. int ret;
  2290. int trap;
  2291. 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
  2292. --- linux-4.1.20.orig/arch/powerpc/include/asm/thread_info.h 2016-03-17 19:11:03.000000000 +0100
  2293. +++ linux-4.1.20/arch/powerpc/include/asm/thread_info.h 2016-03-21 20:18:29.000000000 +0100
  2294. @@ -42,6 +42,8 @@
  2295. int cpu; /* cpu we're on */
  2296. int preempt_count; /* 0 => preemptable,
  2297. <0 => BUG */
  2298. + int preempt_lazy_count; /* 0 => preemptable,
  2299. + <0 => BUG */
  2300. unsigned long local_flags; /* private flags for thread */
  2301. /* low level flags - has atomic operations done on it */
  2302. @@ -82,8 +84,7 @@
  2303. #define TIF_SYSCALL_TRACE 0 /* syscall trace active */
  2304. #define TIF_SIGPENDING 1 /* signal pending */
  2305. #define TIF_NEED_RESCHED 2 /* rescheduling necessary */
  2306. -#define TIF_POLLING_NRFLAG 3 /* true if poll_idle() is polling
  2307. - TIF_NEED_RESCHED */
  2308. +#define TIF_NEED_RESCHED_LAZY 3 /* lazy rescheduling necessary */
  2309. #define TIF_32BIT 4 /* 32 bit binary */
  2310. #define TIF_RESTORE_TM 5 /* need to restore TM FP/VEC/VSX */
  2311. #define TIF_SYSCALL_AUDIT 7 /* syscall auditing active */
  2312. @@ -101,6 +102,8 @@
  2313. #if defined(CONFIG_PPC64)
  2314. #define TIF_ELF2ABI 18 /* function descriptors must die! */
  2315. #endif
  2316. +#define TIF_POLLING_NRFLAG 19 /* true if poll_idle() is polling
  2317. + TIF_NEED_RESCHED */
  2318. /* as above, but as bit values */
  2319. #define _TIF_SYSCALL_TRACE (1<<TIF_SYSCALL_TRACE)
  2320. @@ -119,14 +122,16 @@
  2321. #define _TIF_SYSCALL_TRACEPOINT (1<<TIF_SYSCALL_TRACEPOINT)
  2322. #define _TIF_EMULATE_STACK_STORE (1<<TIF_EMULATE_STACK_STORE)
  2323. #define _TIF_NOHZ (1<<TIF_NOHZ)
  2324. +#define _TIF_NEED_RESCHED_LAZY (1<<TIF_NEED_RESCHED_LAZY)
  2325. #define _TIF_SYSCALL_DOTRACE (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | \
  2326. _TIF_SECCOMP | _TIF_SYSCALL_TRACEPOINT | \
  2327. _TIF_NOHZ)
  2328. #define _TIF_USER_WORK_MASK (_TIF_SIGPENDING | _TIF_NEED_RESCHED | \
  2329. _TIF_NOTIFY_RESUME | _TIF_UPROBE | \
  2330. - _TIF_RESTORE_TM)
  2331. + _TIF_RESTORE_TM | _TIF_NEED_RESCHED_LAZY)
  2332. #define _TIF_PERSYSCALL_MASK (_TIF_RESTOREALL|_TIF_NOERROR)
  2333. +#define _TIF_NEED_RESCHED_MASK (_TIF_NEED_RESCHED | _TIF_NEED_RESCHED_LAZY)
  2334. /* Bits in local_flags */
  2335. /* Don't move TLF_NAPPING without adjusting the code in entry_32.S */
  2336. diff -Nur linux-4.1.20.orig/arch/powerpc/kernel/asm-offsets.c linux-4.1.20/arch/powerpc/kernel/asm-offsets.c
  2337. --- linux-4.1.20.orig/arch/powerpc/kernel/asm-offsets.c 2016-03-17 19:11:03.000000000 +0100
  2338. +++ linux-4.1.20/arch/powerpc/kernel/asm-offsets.c 2016-03-21 20:18:29.000000000 +0100
  2339. @@ -160,6 +160,7 @@
  2340. DEFINE(TI_FLAGS, offsetof(struct thread_info, flags));
  2341. DEFINE(TI_LOCAL_FLAGS, offsetof(struct thread_info, local_flags));
  2342. DEFINE(TI_PREEMPT, offsetof(struct thread_info, preempt_count));
  2343. + DEFINE(TI_PREEMPT_LAZY, offsetof(struct thread_info, preempt_lazy_count));
  2344. DEFINE(TI_TASK, offsetof(struct thread_info, task));
  2345. DEFINE(TI_CPU, offsetof(struct thread_info, cpu));
  2346. diff -Nur linux-4.1.20.orig/arch/powerpc/kernel/entry_32.S linux-4.1.20/arch/powerpc/kernel/entry_32.S
  2347. --- linux-4.1.20.orig/arch/powerpc/kernel/entry_32.S 2016-03-17 19:11:03.000000000 +0100
  2348. +++ linux-4.1.20/arch/powerpc/kernel/entry_32.S 2016-03-21 20:18:29.000000000 +0100
  2349. @@ -813,7 +813,14 @@
  2350. cmpwi 0,r0,0 /* if non-zero, just restore regs and return */
  2351. bne restore
  2352. andi. r8,r8,_TIF_NEED_RESCHED
  2353. + bne+ 1f
  2354. + lwz r0,TI_PREEMPT_LAZY(r9)
  2355. + cmpwi 0,r0,0 /* if non-zero, just restore regs and return */
  2356. + bne restore
  2357. + lwz r0,TI_FLAGS(r9)
  2358. + andi. r0,r0,_TIF_NEED_RESCHED_LAZY
  2359. beq+ restore
  2360. +1:
  2361. lwz r3,_MSR(r1)
  2362. andi. r0,r3,MSR_EE /* interrupts off? */
  2363. beq restore /* don't schedule if so */
  2364. @@ -824,11 +831,11 @@
  2365. */
  2366. bl trace_hardirqs_off
  2367. #endif
  2368. -1: bl preempt_schedule_irq
  2369. +2: bl preempt_schedule_irq
  2370. CURRENT_THREAD_INFO(r9, r1)
  2371. lwz r3,TI_FLAGS(r9)
  2372. - andi. r0,r3,_TIF_NEED_RESCHED
  2373. - bne- 1b
  2374. + andi. r0,r3,_TIF_NEED_RESCHED_MASK
  2375. + bne- 2b
  2376. #ifdef CONFIG_TRACE_IRQFLAGS
  2377. /* And now, to properly rebalance the above, we tell lockdep they
  2378. * are being turned back on, which will happen when we return
  2379. @@ -1149,7 +1156,7 @@
  2380. #endif /* !(CONFIG_4xx || CONFIG_BOOKE) */
  2381. do_work: /* r10 contains MSR_KERNEL here */
  2382. - andi. r0,r9,_TIF_NEED_RESCHED
  2383. + andi. r0,r9,_TIF_NEED_RESCHED_MASK
  2384. beq do_user_signal
  2385. do_resched: /* r10 contains MSR_KERNEL here */
  2386. @@ -1170,7 +1177,7 @@
  2387. MTMSRD(r10) /* disable interrupts */
  2388. CURRENT_THREAD_INFO(r9, r1)
  2389. lwz r9,TI_FLAGS(r9)
  2390. - andi. r0,r9,_TIF_NEED_RESCHED
  2391. + andi. r0,r9,_TIF_NEED_RESCHED_MASK
  2392. bne- do_resched
  2393. andi. r0,r9,_TIF_USER_WORK_MASK
  2394. beq restore_user
  2395. diff -Nur linux-4.1.20.orig/arch/powerpc/kernel/entry_64.S linux-4.1.20/arch/powerpc/kernel/entry_64.S
  2396. --- linux-4.1.20.orig/arch/powerpc/kernel/entry_64.S 2016-03-17 19:11:03.000000000 +0100
  2397. +++ linux-4.1.20/arch/powerpc/kernel/entry_64.S 2016-03-21 20:18:29.000000000 +0100
  2398. @@ -636,7 +636,7 @@
  2399. #else
  2400. beq restore
  2401. #endif
  2402. -1: andi. r0,r4,_TIF_NEED_RESCHED
  2403. +1: andi. r0,r4,_TIF_NEED_RESCHED_MASK
  2404. beq 2f
  2405. bl restore_interrupts
  2406. SCHEDULE_USER
  2407. @@ -698,10 +698,18 @@
  2408. #ifdef CONFIG_PREEMPT
  2409. /* Check if we need to preempt */
  2410. + lwz r8,TI_PREEMPT(r9)
  2411. + cmpwi 0,r8,0 /* if non-zero, just restore regs and return */
  2412. + bne restore
  2413. andi. r0,r4,_TIF_NEED_RESCHED
  2414. + bne+ check_count
  2415. +
  2416. + andi. r0,r4,_TIF_NEED_RESCHED_LAZY
  2417. beq+ restore
  2418. + lwz r8,TI_PREEMPT_LAZY(r9)
  2419. +
  2420. /* Check that preempt_count() == 0 and interrupts are enabled */
  2421. - lwz r8,TI_PREEMPT(r9)
  2422. +check_count:
  2423. cmpwi cr1,r8,0
  2424. ld r0,SOFTE(r1)
  2425. cmpdi r0,0
  2426. @@ -718,7 +726,7 @@
  2427. /* Re-test flags and eventually loop */
  2428. CURRENT_THREAD_INFO(r9, r1)
  2429. ld r4,TI_FLAGS(r9)
  2430. - andi. r0,r4,_TIF_NEED_RESCHED
  2431. + andi. r0,r4,_TIF_NEED_RESCHED_MASK
  2432. bne 1b
  2433. /*
  2434. diff -Nur linux-4.1.20.orig/arch/powerpc/kernel/irq.c linux-4.1.20/arch/powerpc/kernel/irq.c
  2435. --- linux-4.1.20.orig/arch/powerpc/kernel/irq.c 2016-03-17 19:11:03.000000000 +0100
  2436. +++ linux-4.1.20/arch/powerpc/kernel/irq.c 2016-03-21 20:18:30.000000000 +0100
  2437. @@ -614,6 +614,7 @@
  2438. }
  2439. }
  2440. +#ifndef CONFIG_PREEMPT_RT_FULL
  2441. void do_softirq_own_stack(void)
  2442. {
  2443. struct thread_info *curtp, *irqtp;
  2444. @@ -631,6 +632,7 @@
  2445. if (irqtp->flags)
  2446. set_bits(irqtp->flags, &curtp->flags);
  2447. }
  2448. +#endif
  2449. irq_hw_number_t virq_to_hw(unsigned int virq)
  2450. {
  2451. diff -Nur linux-4.1.20.orig/arch/powerpc/kernel/misc_32.S linux-4.1.20/arch/powerpc/kernel/misc_32.S
  2452. --- linux-4.1.20.orig/arch/powerpc/kernel/misc_32.S 2016-03-17 19:11:03.000000000 +0100
  2453. +++ linux-4.1.20/arch/powerpc/kernel/misc_32.S 2016-03-21 20:18:30.000000000 +0100
  2454. @@ -40,6 +40,7 @@
  2455. * We store the saved ksp_limit in the unused part
  2456. * of the STACK_FRAME_OVERHEAD
  2457. */
  2458. +#ifndef CONFIG_PREEMPT_RT_FULL
  2459. _GLOBAL(call_do_softirq)
  2460. mflr r0
  2461. stw r0,4(r1)
  2462. @@ -56,6 +57,7 @@
  2463. stw r10,THREAD+KSP_LIMIT(r2)
  2464. mtlr r0
  2465. blr
  2466. +#endif
  2467. /*
  2468. * void call_do_irq(struct pt_regs *regs, struct thread_info *irqtp);
  2469. diff -Nur linux-4.1.20.orig/arch/powerpc/kernel/misc_64.S linux-4.1.20/arch/powerpc/kernel/misc_64.S
  2470. --- linux-4.1.20.orig/arch/powerpc/kernel/misc_64.S 2016-03-17 19:11:03.000000000 +0100
  2471. +++ linux-4.1.20/arch/powerpc/kernel/misc_64.S 2016-03-21 20:18:30.000000000 +0100
  2472. @@ -29,6 +29,7 @@
  2473. .text
  2474. +#ifndef CONFIG_PREEMPT_RT_FULL
  2475. _GLOBAL(call_do_softirq)
  2476. mflr r0
  2477. std r0,16(r1)
  2478. @@ -39,6 +40,7 @@
  2479. ld r0,16(r1)
  2480. mtlr r0
  2481. blr
  2482. +#endif
  2483. _GLOBAL(call_do_irq)
  2484. mflr r0
  2485. diff -Nur linux-4.1.20.orig/arch/powerpc/kvm/Kconfig linux-4.1.20/arch/powerpc/kvm/Kconfig
  2486. --- linux-4.1.20.orig/arch/powerpc/kvm/Kconfig 2016-03-17 19:11:03.000000000 +0100
  2487. +++ linux-4.1.20/arch/powerpc/kvm/Kconfig 2016-03-21 20:18:30.000000000 +0100
  2488. @@ -172,6 +172,7 @@
  2489. config KVM_MPIC
  2490. bool "KVM in-kernel MPIC emulation"
  2491. depends on KVM && E500
  2492. + depends on !PREEMPT_RT_FULL
  2493. select HAVE_KVM_IRQCHIP
  2494. select HAVE_KVM_IRQFD
  2495. select HAVE_KVM_IRQ_ROUTING
  2496. diff -Nur linux-4.1.20.orig/arch/powerpc/kvm/book3s_hv.c linux-4.1.20/arch/powerpc/kvm/book3s_hv.c
  2497. --- linux-4.1.20.orig/arch/powerpc/kvm/book3s_hv.c 2016-03-17 19:11:03.000000000 +0100
  2498. +++ linux-4.1.20/arch/powerpc/kvm/book3s_hv.c 2016-03-21 20:18:30.000000000 +0100
  2499. @@ -115,11 +115,11 @@
  2500. static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
  2501. {
  2502. int cpu = vcpu->cpu;
  2503. - wait_queue_head_t *wqp;
  2504. + struct swait_head *wqp;
  2505. wqp = kvm_arch_vcpu_wq(vcpu);
  2506. - if (waitqueue_active(wqp)) {
  2507. - wake_up_interruptible(wqp);
  2508. + if (swaitqueue_active(wqp)) {
  2509. + swait_wake_interruptible(wqp);
  2510. ++vcpu->stat.halt_wakeup;
  2511. }
  2512. @@ -692,8 +692,8 @@
  2513. tvcpu->arch.prodded = 1;
  2514. smp_mb();
  2515. if (vcpu->arch.ceded) {
  2516. - if (waitqueue_active(&vcpu->wq)) {
  2517. - wake_up_interruptible(&vcpu->wq);
  2518. + if (swaitqueue_active(&vcpu->wq)) {
  2519. + swait_wake_interruptible(&vcpu->wq);
  2520. vcpu->stat.halt_wakeup++;
  2521. }
  2522. }
  2523. @@ -1432,7 +1432,7 @@
  2524. INIT_LIST_HEAD(&vcore->runnable_threads);
  2525. spin_lock_init(&vcore->lock);
  2526. spin_lock_init(&vcore->stoltb_lock);
  2527. - init_waitqueue_head(&vcore->wq);
  2528. + init_swait_head(&vcore->wq);
  2529. vcore->preempt_tb = TB_NIL;
  2530. vcore->lpcr = kvm->arch.lpcr;
  2531. vcore->first_vcpuid = core * threads_per_subcore;
  2532. @@ -2079,10 +2079,9 @@
  2533. {
  2534. struct kvm_vcpu *vcpu;
  2535. int do_sleep = 1;
  2536. + DEFINE_SWAITER(wait);
  2537. - DEFINE_WAIT(wait);
  2538. -
  2539. - prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
  2540. + swait_prepare(&vc->wq, &wait, TASK_INTERRUPTIBLE);
  2541. /*
  2542. * Check one last time for pending exceptions and ceded state after
  2543. @@ -2096,7 +2095,7 @@
  2544. }
  2545. if (!do_sleep) {
  2546. - finish_wait(&vc->wq, &wait);
  2547. + swait_finish(&vc->wq, &wait);
  2548. return;
  2549. }
  2550. @@ -2104,7 +2103,7 @@
  2551. trace_kvmppc_vcore_blocked(vc, 0);
  2552. spin_unlock(&vc->lock);
  2553. schedule();
  2554. - finish_wait(&vc->wq, &wait);
  2555. + swait_finish(&vc->wq, &wait);
  2556. spin_lock(&vc->lock);
  2557. vc->vcore_state = VCORE_INACTIVE;
  2558. trace_kvmppc_vcore_blocked(vc, 1);
  2559. @@ -2148,7 +2147,7 @@
  2560. kvmppc_start_thread(vcpu);
  2561. trace_kvm_guest_enter(vcpu);
  2562. } else if (vc->vcore_state == VCORE_SLEEPING) {
  2563. - wake_up(&vc->wq);
  2564. + swait_wake(&vc->wq);
  2565. }
  2566. }
  2567. diff -Nur linux-4.1.20.orig/arch/powerpc/mm/fault.c linux-4.1.20/arch/powerpc/mm/fault.c
  2568. --- linux-4.1.20.orig/arch/powerpc/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  2569. +++ linux-4.1.20/arch/powerpc/mm/fault.c 2016-03-21 20:18:30.000000000 +0100
  2570. @@ -33,13 +33,13 @@
  2571. #include <linux/ratelimit.h>
  2572. #include <linux/context_tracking.h>
  2573. #include <linux/hugetlb.h>
  2574. +#include <linux/uaccess.h>
  2575. #include <asm/firmware.h>
  2576. #include <asm/page.h>
  2577. #include <asm/pgtable.h>
  2578. #include <asm/mmu.h>
  2579. #include <asm/mmu_context.h>
  2580. -#include <asm/uaccess.h>
  2581. #include <asm/tlbflush.h>
  2582. #include <asm/siginfo.h>
  2583. #include <asm/debug.h>
  2584. @@ -272,15 +272,16 @@
  2585. if (!arch_irq_disabled_regs(regs))
  2586. local_irq_enable();
  2587. - if (in_atomic() || mm == NULL) {
  2588. + if (faulthandler_disabled() || mm == NULL) {
  2589. if (!user_mode(regs)) {
  2590. rc = SIGSEGV;
  2591. goto bail;
  2592. }
  2593. - /* in_atomic() in user mode is really bad,
  2594. + /* faulthandler_disabled() in user mode is really bad,
  2595. as is current->mm == NULL. */
  2596. printk(KERN_EMERG "Page fault in user mode with "
  2597. - "in_atomic() = %d mm = %p\n", in_atomic(), mm);
  2598. + "faulthandler_disabled() = %d mm = %p\n",
  2599. + faulthandler_disabled(), mm);
  2600. printk(KERN_EMERG "NIP = %lx MSR = %lx\n",
  2601. regs->nip, regs->msr);
  2602. die("Weird page fault", regs, SIGSEGV);
  2603. diff -Nur linux-4.1.20.orig/arch/powerpc/mm/highmem.c linux-4.1.20/arch/powerpc/mm/highmem.c
  2604. --- linux-4.1.20.orig/arch/powerpc/mm/highmem.c 2016-03-17 19:11:03.000000000 +0100
  2605. +++ linux-4.1.20/arch/powerpc/mm/highmem.c 2016-03-21 20:18:30.000000000 +0100
  2606. @@ -34,7 +34,7 @@
  2607. unsigned long vaddr;
  2608. int idx, type;
  2609. - /* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */
  2610. + preempt_disable();
  2611. pagefault_disable();
  2612. if (!PageHighMem(page))
  2613. return page_address(page);
  2614. @@ -59,6 +59,7 @@
  2615. if (vaddr < __fix_to_virt(FIX_KMAP_END)) {
  2616. pagefault_enable();
  2617. + preempt_enable();
  2618. return;
  2619. }
  2620. @@ -82,5 +83,6 @@
  2621. kmap_atomic_idx_pop();
  2622. pagefault_enable();
  2623. + preempt_enable();
  2624. }
  2625. EXPORT_SYMBOL(__kunmap_atomic);
  2626. 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
  2627. --- linux-4.1.20.orig/arch/powerpc/platforms/ps3/device-init.c 2016-03-17 19:11:03.000000000 +0100
  2628. +++ linux-4.1.20/arch/powerpc/platforms/ps3/device-init.c 2016-03-21 20:18:30.000000000 +0100
  2629. @@ -752,7 +752,7 @@
  2630. }
  2631. pr_debug("%s:%u: notification %s issued\n", __func__, __LINE__, op);
  2632. - res = wait_event_interruptible(dev->done.wait,
  2633. + res = swait_event_interruptible(dev->done.wait,
  2634. dev->done.done || kthread_should_stop());
  2635. if (kthread_should_stop())
  2636. res = -EINTR;
  2637. 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
  2638. --- linux-4.1.20.orig/arch/s390/include/asm/kvm_host.h 2016-03-17 19:11:03.000000000 +0100
  2639. +++ linux-4.1.20/arch/s390/include/asm/kvm_host.h 2016-03-21 20:18:30.000000000 +0100
  2640. @@ -419,7 +419,7 @@
  2641. struct kvm_s390_local_interrupt {
  2642. spinlock_t lock;
  2643. struct kvm_s390_float_interrupt *float_int;
  2644. - wait_queue_head_t *wq;
  2645. + struct swait_head *wq;
  2646. atomic_t *cpuflags;
  2647. DECLARE_BITMAP(sigp_emerg_pending, KVM_MAX_VCPUS);
  2648. struct kvm_s390_irq_payload irq;
  2649. diff -Nur linux-4.1.20.orig/arch/s390/include/asm/uaccess.h linux-4.1.20/arch/s390/include/asm/uaccess.h
  2650. --- linux-4.1.20.orig/arch/s390/include/asm/uaccess.h 2016-03-17 19:11:03.000000000 +0100
  2651. +++ linux-4.1.20/arch/s390/include/asm/uaccess.h 2016-03-21 20:18:30.000000000 +0100
  2652. @@ -98,7 +98,8 @@
  2653. * @from: Source address, in user space.
  2654. * @n: Number of bytes to copy.
  2655. *
  2656. - * Context: User context only. This function may sleep.
  2657. + * Context: User context only. This function may sleep if pagefaults are
  2658. + * enabled.
  2659. *
  2660. * Copy data from user space to kernel space. Caller must check
  2661. * the specified block with access_ok() before calling this function.
  2662. @@ -118,7 +119,8 @@
  2663. * @from: Source address, in kernel space.
  2664. * @n: Number of bytes to copy.
  2665. *
  2666. - * Context: User context only. This function may sleep.
  2667. + * Context: User context only. This function may sleep if pagefaults are
  2668. + * enabled.
  2669. *
  2670. * Copy data from kernel space to user space. Caller must check
  2671. * the specified block with access_ok() before calling this function.
  2672. @@ -264,7 +266,8 @@
  2673. * @from: Source address, in kernel space.
  2674. * @n: Number of bytes to copy.
  2675. *
  2676. - * Context: User context only. This function may sleep.
  2677. + * Context: User context only. This function may sleep if pagefaults are
  2678. + * enabled.
  2679. *
  2680. * Copy data from kernel space to user space.
  2681. *
  2682. @@ -290,7 +293,8 @@
  2683. * @from: Source address, in user space.
  2684. * @n: Number of bytes to copy.
  2685. *
  2686. - * Context: User context only. This function may sleep.
  2687. + * Context: User context only. This function may sleep if pagefaults are
  2688. + * enabled.
  2689. *
  2690. * Copy data from user space to kernel space.
  2691. *
  2692. @@ -348,7 +352,8 @@
  2693. * strlen_user: - Get the size of a string in user space.
  2694. * @str: The string to measure.
  2695. *
  2696. - * Context: User context only. This function may sleep.
  2697. + * Context: User context only. This function may sleep if pagefaults are
  2698. + * enabled.
  2699. *
  2700. * Get the size of a NUL-terminated string in user space.
  2701. *
  2702. diff -Nur linux-4.1.20.orig/arch/s390/kvm/interrupt.c linux-4.1.20/arch/s390/kvm/interrupt.c
  2703. --- linux-4.1.20.orig/arch/s390/kvm/interrupt.c 2016-03-17 19:11:03.000000000 +0100
  2704. +++ linux-4.1.20/arch/s390/kvm/interrupt.c 2016-03-21 20:18:30.000000000 +0100
  2705. @@ -875,13 +875,13 @@
  2706. void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu)
  2707. {
  2708. - if (waitqueue_active(&vcpu->wq)) {
  2709. + if (swaitqueue_active(&vcpu->wq)) {
  2710. /*
  2711. * The vcpu gave up the cpu voluntarily, mark it as a good
  2712. * yield-candidate.
  2713. */
  2714. vcpu->preempted = true;
  2715. - wake_up_interruptible(&vcpu->wq);
  2716. + swait_wake_interruptible(&vcpu->wq);
  2717. vcpu->stat.halt_wakeup++;
  2718. }
  2719. }
  2720. @@ -987,7 +987,7 @@
  2721. spin_lock(&li->lock);
  2722. irq.u.pgm.code = code;
  2723. __inject_prog(vcpu, &irq);
  2724. - BUG_ON(waitqueue_active(li->wq));
  2725. + BUG_ON(swaitqueue_active(li->wq));
  2726. spin_unlock(&li->lock);
  2727. return 0;
  2728. }
  2729. @@ -1006,7 +1006,7 @@
  2730. spin_lock(&li->lock);
  2731. irq.u.pgm = *pgm_info;
  2732. rc = __inject_prog(vcpu, &irq);
  2733. - BUG_ON(waitqueue_active(li->wq));
  2734. + BUG_ON(swaitqueue_active(li->wq));
  2735. spin_unlock(&li->lock);
  2736. return rc;
  2737. }
  2738. diff -Nur linux-4.1.20.orig/arch/s390/mm/fault.c linux-4.1.20/arch/s390/mm/fault.c
  2739. --- linux-4.1.20.orig/arch/s390/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  2740. +++ linux-4.1.20/arch/s390/mm/fault.c 2016-03-21 20:18:30.000000000 +0100
  2741. @@ -399,7 +399,7 @@
  2742. * user context.
  2743. */
  2744. fault = VM_FAULT_BADCONTEXT;
  2745. - if (unlikely(!user_space_fault(regs) || in_atomic() || !mm))
  2746. + if (unlikely(!user_space_fault(regs) || faulthandler_disabled() || !mm))
  2747. goto out;
  2748. address = trans_exc_code & __FAIL_ADDR_MASK;
  2749. diff -Nur linux-4.1.20.orig/arch/score/include/asm/uaccess.h linux-4.1.20/arch/score/include/asm/uaccess.h
  2750. --- linux-4.1.20.orig/arch/score/include/asm/uaccess.h 2016-03-17 19:11:03.000000000 +0100
  2751. +++ linux-4.1.20/arch/score/include/asm/uaccess.h 2016-03-21 20:18:30.000000000 +0100
  2752. @@ -36,7 +36,8 @@
  2753. * @addr: User space pointer to start of block to check
  2754. * @size: Size of block to check
  2755. *
  2756. - * Context: User context only. This function may sleep.
  2757. + * Context: User context only. This function may sleep if pagefaults are
  2758. + * enabled.
  2759. *
  2760. * Checks if a pointer to a block of memory in user space is valid.
  2761. *
  2762. @@ -61,7 +62,8 @@
  2763. * @x: Value to copy to user space.
  2764. * @ptr: Destination address, in user space.
  2765. *
  2766. - * Context: User context only. This function may sleep.
  2767. + * Context: User context only. This function may sleep if pagefaults are
  2768. + * enabled.
  2769. *
  2770. * This macro copies a single simple value from kernel space to user
  2771. * space. It supports simple types like char and int, but not larger
  2772. @@ -79,7 +81,8 @@
  2773. * @x: Variable to store result.
  2774. * @ptr: Source address, in user space.
  2775. *
  2776. - * Context: User context only. This function may sleep.
  2777. + * Context: User context only. This function may sleep if pagefaults are
  2778. + * enabled.
  2779. *
  2780. * This macro copies a single simple variable from user space to kernel
  2781. * space. It supports simple types like char and int, but not larger
  2782. @@ -98,7 +101,8 @@
  2783. * @x: Value to copy to user space.
  2784. * @ptr: Destination address, in user space.
  2785. *
  2786. - * Context: User context only. This function may sleep.
  2787. + * Context: User context only. This function may sleep if pagefaults are
  2788. + * enabled.
  2789. *
  2790. * This macro copies a single simple value from kernel space to user
  2791. * space. It supports simple types like char and int, but not larger
  2792. @@ -119,7 +123,8 @@
  2793. * @x: Variable to store result.
  2794. * @ptr: Source address, in user space.
  2795. *
  2796. - * Context: User context only. This function may sleep.
  2797. + * Context: User context only. This function may sleep if pagefaults are
  2798. + * enabled.
  2799. *
  2800. * This macro copies a single simple variable from user space to kernel
  2801. * space. It supports simple types like char and int, but not larger
  2802. diff -Nur linux-4.1.20.orig/arch/score/mm/fault.c linux-4.1.20/arch/score/mm/fault.c
  2803. --- linux-4.1.20.orig/arch/score/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  2804. +++ linux-4.1.20/arch/score/mm/fault.c 2016-03-21 20:18:30.000000000 +0100
  2805. @@ -34,6 +34,7 @@
  2806. #include <linux/string.h>
  2807. #include <linux/types.h>
  2808. #include <linux/ptrace.h>
  2809. +#include <linux/uaccess.h>
  2810. /*
  2811. * This routine handles page faults. It determines the address,
  2812. @@ -73,7 +74,7 @@
  2813. * If we're in an interrupt or have no user
  2814. * context, we must not take the fault..
  2815. */
  2816. - if (in_atomic() || !mm)
  2817. + if (pagefault_disabled() || !mm)
  2818. goto bad_area_nosemaphore;
  2819. if (user_mode(regs))
  2820. diff -Nur linux-4.1.20.orig/arch/sh/kernel/irq.c linux-4.1.20/arch/sh/kernel/irq.c
  2821. --- linux-4.1.20.orig/arch/sh/kernel/irq.c 2016-03-17 19:11:03.000000000 +0100
  2822. +++ linux-4.1.20/arch/sh/kernel/irq.c 2016-03-21 20:18:30.000000000 +0100
  2823. @@ -147,6 +147,7 @@
  2824. hardirq_ctx[cpu] = NULL;
  2825. }
  2826. +#ifndef CONFIG_PREEMPT_RT_FULL
  2827. void do_softirq_own_stack(void)
  2828. {
  2829. struct thread_info *curctx;
  2830. @@ -174,6 +175,7 @@
  2831. "r5", "r6", "r7", "r8", "r9", "r15", "t", "pr"
  2832. );
  2833. }
  2834. +#endif
  2835. #else
  2836. static inline void handle_one_irq(unsigned int irq)
  2837. {
  2838. diff -Nur linux-4.1.20.orig/arch/sh/mm/fault.c linux-4.1.20/arch/sh/mm/fault.c
  2839. --- linux-4.1.20.orig/arch/sh/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  2840. +++ linux-4.1.20/arch/sh/mm/fault.c 2016-03-21 20:18:30.000000000 +0100
  2841. @@ -17,6 +17,7 @@
  2842. #include <linux/kprobes.h>
  2843. #include <linux/perf_event.h>
  2844. #include <linux/kdebug.h>
  2845. +#include <linux/uaccess.h>
  2846. #include <asm/io_trapped.h>
  2847. #include <asm/mmu_context.h>
  2848. #include <asm/tlbflush.h>
  2849. @@ -438,9 +439,9 @@
  2850. /*
  2851. * If we're in an interrupt, have no user context or are running
  2852. - * in an atomic region then we must not take the fault:
  2853. + * with pagefaults disabled then we must not take the fault:
  2854. */
  2855. - if (unlikely(in_atomic() || !mm)) {
  2856. + if (unlikely(faulthandler_disabled() || !mm)) {
  2857. bad_area_nosemaphore(regs, error_code, address);
  2858. return;
  2859. }
  2860. diff -Nur linux-4.1.20.orig/arch/sparc/Kconfig linux-4.1.20/arch/sparc/Kconfig
  2861. --- linux-4.1.20.orig/arch/sparc/Kconfig 2016-03-17 19:11:03.000000000 +0100
  2862. +++ linux-4.1.20/arch/sparc/Kconfig 2016-03-21 20:18:30.000000000 +0100
  2863. @@ -189,12 +189,10 @@
  2864. source kernel/Kconfig.hz
  2865. config RWSEM_GENERIC_SPINLOCK
  2866. - bool
  2867. - default y if SPARC32
  2868. + def_bool PREEMPT_RT_FULL
  2869. config RWSEM_XCHGADD_ALGORITHM
  2870. - bool
  2871. - default y if SPARC64
  2872. + def_bool !RWSEM_GENERIC_SPINLOCK && !PREEMPT_RT_FULL
  2873. config GENERIC_HWEIGHT
  2874. bool
  2875. diff -Nur linux-4.1.20.orig/arch/sparc/kernel/irq_64.c linux-4.1.20/arch/sparc/kernel/irq_64.c
  2876. --- linux-4.1.20.orig/arch/sparc/kernel/irq_64.c 2016-03-17 19:11:03.000000000 +0100
  2877. +++ linux-4.1.20/arch/sparc/kernel/irq_64.c 2016-03-21 20:18:30.000000000 +0100
  2878. @@ -849,6 +849,7 @@
  2879. set_irq_regs(old_regs);
  2880. }
  2881. +#ifndef CONFIG_PREEMPT_RT_FULL
  2882. void do_softirq_own_stack(void)
  2883. {
  2884. void *orig_sp, *sp = softirq_stack[smp_processor_id()];
  2885. @@ -863,6 +864,7 @@
  2886. __asm__ __volatile__("mov %0, %%sp"
  2887. : : "r" (orig_sp));
  2888. }
  2889. +#endif
  2890. #ifdef CONFIG_HOTPLUG_CPU
  2891. void fixup_irqs(void)
  2892. diff -Nur linux-4.1.20.orig/arch/sparc/mm/fault_32.c linux-4.1.20/arch/sparc/mm/fault_32.c
  2893. --- linux-4.1.20.orig/arch/sparc/mm/fault_32.c 2016-03-17 19:11:03.000000000 +0100
  2894. +++ linux-4.1.20/arch/sparc/mm/fault_32.c 2016-03-21 20:18:30.000000000 +0100
  2895. @@ -21,6 +21,7 @@
  2896. #include <linux/perf_event.h>
  2897. #include <linux/interrupt.h>
  2898. #include <linux/kdebug.h>
  2899. +#include <linux/uaccess.h>
  2900. #include <asm/page.h>
  2901. #include <asm/pgtable.h>
  2902. @@ -29,7 +30,6 @@
  2903. #include <asm/setup.h>
  2904. #include <asm/smp.h>
  2905. #include <asm/traps.h>
  2906. -#include <asm/uaccess.h>
  2907. #include "mm_32.h"
  2908. @@ -196,7 +196,7 @@
  2909. * If we're in an interrupt or have no user
  2910. * context, we must not take the fault..
  2911. */
  2912. - if (in_atomic() || !mm)
  2913. + if (pagefault_disabled() || !mm)
  2914. goto no_context;
  2915. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  2916. diff -Nur linux-4.1.20.orig/arch/sparc/mm/fault_64.c linux-4.1.20/arch/sparc/mm/fault_64.c
  2917. --- linux-4.1.20.orig/arch/sparc/mm/fault_64.c 2016-03-17 19:11:03.000000000 +0100
  2918. +++ linux-4.1.20/arch/sparc/mm/fault_64.c 2016-03-21 20:18:30.000000000 +0100
  2919. @@ -22,12 +22,12 @@
  2920. #include <linux/kdebug.h>
  2921. #include <linux/percpu.h>
  2922. #include <linux/context_tracking.h>
  2923. +#include <linux/uaccess.h>
  2924. #include <asm/page.h>
  2925. #include <asm/pgtable.h>
  2926. #include <asm/openprom.h>
  2927. #include <asm/oplib.h>
  2928. -#include <asm/uaccess.h>
  2929. #include <asm/asi.h>
  2930. #include <asm/lsu.h>
  2931. #include <asm/sections.h>
  2932. @@ -330,7 +330,7 @@
  2933. * If we're in an interrupt or have no user
  2934. * context, we must not take the fault..
  2935. */
  2936. - if (in_atomic() || !mm)
  2937. + if (faulthandler_disabled() || !mm)
  2938. goto intr_or_no_mm;
  2939. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  2940. diff -Nur linux-4.1.20.orig/arch/sparc/mm/highmem.c linux-4.1.20/arch/sparc/mm/highmem.c
  2941. --- linux-4.1.20.orig/arch/sparc/mm/highmem.c 2016-03-17 19:11:03.000000000 +0100
  2942. +++ linux-4.1.20/arch/sparc/mm/highmem.c 2016-03-21 20:18:30.000000000 +0100
  2943. @@ -53,7 +53,7 @@
  2944. unsigned long vaddr;
  2945. long idx, type;
  2946. - /* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */
  2947. + preempt_disable();
  2948. pagefault_disable();
  2949. if (!PageHighMem(page))
  2950. return page_address(page);
  2951. @@ -91,6 +91,7 @@
  2952. if (vaddr < FIXADDR_START) { // FIXME
  2953. pagefault_enable();
  2954. + preempt_enable();
  2955. return;
  2956. }
  2957. @@ -126,5 +127,6 @@
  2958. kmap_atomic_idx_pop();
  2959. pagefault_enable();
  2960. + preempt_enable();
  2961. }
  2962. EXPORT_SYMBOL(__kunmap_atomic);
  2963. diff -Nur linux-4.1.20.orig/arch/sparc/mm/init_64.c linux-4.1.20/arch/sparc/mm/init_64.c
  2964. --- linux-4.1.20.orig/arch/sparc/mm/init_64.c 2016-03-17 19:11:03.000000000 +0100
  2965. +++ linux-4.1.20/arch/sparc/mm/init_64.c 2016-03-21 20:18:30.000000000 +0100
  2966. @@ -2738,7 +2738,7 @@
  2967. struct mm_struct *mm = current->mm;
  2968. struct tsb_config *tp;
  2969. - if (in_atomic() || !mm) {
  2970. + if (faulthandler_disabled() || !mm) {
  2971. const struct exception_table_entry *entry;
  2972. entry = search_exception_tables(regs->tpc);
  2973. diff -Nur linux-4.1.20.orig/arch/tile/include/asm/uaccess.h linux-4.1.20/arch/tile/include/asm/uaccess.h
  2974. --- linux-4.1.20.orig/arch/tile/include/asm/uaccess.h 2016-03-17 19:11:03.000000000 +0100
  2975. +++ linux-4.1.20/arch/tile/include/asm/uaccess.h 2016-03-21 20:18:30.000000000 +0100
  2976. @@ -78,7 +78,8 @@
  2977. * @addr: User space pointer to start of block to check
  2978. * @size: Size of block to check
  2979. *
  2980. - * Context: User context only. This function may sleep.
  2981. + * Context: User context only. This function may sleep if pagefaults are
  2982. + * enabled.
  2983. *
  2984. * Checks if a pointer to a block of memory in user space is valid.
  2985. *
  2986. @@ -192,7 +193,8 @@
  2987. * @x: Variable to store result.
  2988. * @ptr: Source address, in user space.
  2989. *
  2990. - * Context: User context only. This function may sleep.
  2991. + * Context: User context only. This function may sleep if pagefaults are
  2992. + * enabled.
  2993. *
  2994. * This macro copies a single simple variable from user space to kernel
  2995. * space. It supports simple types like char and int, but not larger
  2996. @@ -274,7 +276,8 @@
  2997. * @x: Value to copy to user space.
  2998. * @ptr: Destination address, in user space.
  2999. *
  3000. - * Context: User context only. This function may sleep.
  3001. + * Context: User context only. This function may sleep if pagefaults are
  3002. + * enabled.
  3003. *
  3004. * This macro copies a single simple value from kernel space to user
  3005. * space. It supports simple types like char and int, but not larger
  3006. @@ -330,7 +333,8 @@
  3007. * @from: Source address, in kernel space.
  3008. * @n: Number of bytes to copy.
  3009. *
  3010. - * Context: User context only. This function may sleep.
  3011. + * Context: User context only. This function may sleep if pagefaults are
  3012. + * enabled.
  3013. *
  3014. * Copy data from kernel space to user space. Caller must check
  3015. * the specified block with access_ok() before calling this function.
  3016. @@ -366,7 +370,8 @@
  3017. * @from: Source address, in user space.
  3018. * @n: Number of bytes to copy.
  3019. *
  3020. - * Context: User context only. This function may sleep.
  3021. + * Context: User context only. This function may sleep if pagefaults are
  3022. + * enabled.
  3023. *
  3024. * Copy data from user space to kernel space. Caller must check
  3025. * the specified block with access_ok() before calling this function.
  3026. @@ -437,7 +442,8 @@
  3027. * @from: Source address, in user space.
  3028. * @n: Number of bytes to copy.
  3029. *
  3030. - * Context: User context only. This function may sleep.
  3031. + * Context: User context only. This function may sleep if pagefaults are
  3032. + * enabled.
  3033. *
  3034. * Copy data from user space to user space. Caller must check
  3035. * the specified blocks with access_ok() before calling this function.
  3036. diff -Nur linux-4.1.20.orig/arch/tile/mm/fault.c linux-4.1.20/arch/tile/mm/fault.c
  3037. --- linux-4.1.20.orig/arch/tile/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  3038. +++ linux-4.1.20/arch/tile/mm/fault.c 2016-03-21 20:18:30.000000000 +0100
  3039. @@ -354,9 +354,9 @@
  3040. /*
  3041. * If we're in an interrupt, have no user context or are running in an
  3042. - * atomic region then we must not take the fault.
  3043. + * region with pagefaults disabled then we must not take the fault.
  3044. */
  3045. - if (in_atomic() || !mm) {
  3046. + if (pagefault_disabled() || !mm) {
  3047. vma = NULL; /* happy compiler */
  3048. goto bad_area_nosemaphore;
  3049. }
  3050. diff -Nur linux-4.1.20.orig/arch/tile/mm/highmem.c linux-4.1.20/arch/tile/mm/highmem.c
  3051. --- linux-4.1.20.orig/arch/tile/mm/highmem.c 2016-03-17 19:11:03.000000000 +0100
  3052. +++ linux-4.1.20/arch/tile/mm/highmem.c 2016-03-21 20:18:30.000000000 +0100
  3053. @@ -201,7 +201,7 @@
  3054. int idx, type;
  3055. pte_t *pte;
  3056. - /* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */
  3057. + preempt_disable();
  3058. pagefault_disable();
  3059. /* Avoid icache flushes by disallowing atomic executable mappings. */
  3060. @@ -259,6 +259,7 @@
  3061. }
  3062. pagefault_enable();
  3063. + preempt_enable();
  3064. }
  3065. EXPORT_SYMBOL(__kunmap_atomic);
  3066. diff -Nur linux-4.1.20.orig/arch/um/kernel/trap.c linux-4.1.20/arch/um/kernel/trap.c
  3067. --- linux-4.1.20.orig/arch/um/kernel/trap.c 2016-03-17 19:11:03.000000000 +0100
  3068. +++ linux-4.1.20/arch/um/kernel/trap.c 2016-03-21 20:18:30.000000000 +0100
  3069. @@ -35,10 +35,10 @@
  3070. *code_out = SEGV_MAPERR;
  3071. /*
  3072. - * If the fault was during atomic operation, don't take the fault, just
  3073. + * If the fault was with pagefaults disabled, don't take the fault, just
  3074. * fail.
  3075. */
  3076. - if (in_atomic())
  3077. + if (faulthandler_disabled())
  3078. goto out_nosemaphore;
  3079. if (is_user)
  3080. diff -Nur linux-4.1.20.orig/arch/unicore32/mm/fault.c linux-4.1.20/arch/unicore32/mm/fault.c
  3081. --- linux-4.1.20.orig/arch/unicore32/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  3082. +++ linux-4.1.20/arch/unicore32/mm/fault.c 2016-03-21 20:18:30.000000000 +0100
  3083. @@ -218,7 +218,7 @@
  3084. * If we're in an interrupt or have no user
  3085. * context, we must not take the fault..
  3086. */
  3087. - if (in_atomic() || !mm)
  3088. + if (faulthandler_disabled() || !mm)
  3089. goto no_context;
  3090. if (user_mode(regs))
  3091. diff -Nur linux-4.1.20.orig/arch/x86/Kconfig linux-4.1.20/arch/x86/Kconfig
  3092. --- linux-4.1.20.orig/arch/x86/Kconfig 2016-03-17 19:11:03.000000000 +0100
  3093. +++ linux-4.1.20/arch/x86/Kconfig 2016-03-21 20:18:30.000000000 +0100
  3094. @@ -22,6 +22,7 @@
  3095. ### Arch settings
  3096. config X86
  3097. def_bool y
  3098. + select HAVE_PREEMPT_LAZY
  3099. select ACPI_SYSTEM_POWER_STATES_SUPPORT if ACPI
  3100. select ARCH_MIGHT_HAVE_ACPI_PDC if ACPI
  3101. select ARCH_HAS_DEBUG_STRICT_USER_COPY_CHECKS
  3102. @@ -203,8 +204,11 @@
  3103. def_bool y
  3104. depends on ISA_DMA_API
  3105. +config RWSEM_GENERIC_SPINLOCK
  3106. + def_bool PREEMPT_RT_FULL
  3107. +
  3108. config RWSEM_XCHGADD_ALGORITHM
  3109. - def_bool y
  3110. + def_bool !RWSEM_GENERIC_SPINLOCK && !PREEMPT_RT_FULL
  3111. config GENERIC_CALIBRATE_DELAY
  3112. def_bool y
  3113. @@ -838,7 +842,7 @@
  3114. config MAXSMP
  3115. bool "Enable Maximum number of SMP Processors and NUMA Nodes"
  3116. depends on X86_64 && SMP && DEBUG_KERNEL
  3117. - select CPUMASK_OFFSTACK
  3118. + select CPUMASK_OFFSTACK if !PREEMPT_RT_FULL
  3119. ---help---
  3120. Enable maximum number of CPUS and NUMA Nodes for this architecture.
  3121. If unsure, say N.
  3122. 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
  3123. --- linux-4.1.20.orig/arch/x86/crypto/aesni-intel_glue.c 2016-03-17 19:11:03.000000000 +0100
  3124. +++ linux-4.1.20/arch/x86/crypto/aesni-intel_glue.c 2016-03-21 20:18:30.000000000 +0100
  3125. @@ -382,14 +382,14 @@
  3126. err = blkcipher_walk_virt(desc, &walk);
  3127. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  3128. - kernel_fpu_begin();
  3129. while ((nbytes = walk.nbytes)) {
  3130. + kernel_fpu_begin();
  3131. aesni_ecb_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  3132. - nbytes & AES_BLOCK_MASK);
  3133. + nbytes & AES_BLOCK_MASK);
  3134. + kernel_fpu_end();
  3135. nbytes &= AES_BLOCK_SIZE - 1;
  3136. err = blkcipher_walk_done(desc, &walk, nbytes);
  3137. }
  3138. - kernel_fpu_end();
  3139. return err;
  3140. }
  3141. @@ -406,14 +406,14 @@
  3142. err = blkcipher_walk_virt(desc, &walk);
  3143. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  3144. - kernel_fpu_begin();
  3145. while ((nbytes = walk.nbytes)) {
  3146. + kernel_fpu_begin();
  3147. aesni_ecb_dec(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  3148. nbytes & AES_BLOCK_MASK);
  3149. + kernel_fpu_end();
  3150. nbytes &= AES_BLOCK_SIZE - 1;
  3151. err = blkcipher_walk_done(desc, &walk, nbytes);
  3152. }
  3153. - kernel_fpu_end();
  3154. return err;
  3155. }
  3156. @@ -430,14 +430,14 @@
  3157. err = blkcipher_walk_virt(desc, &walk);
  3158. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  3159. - kernel_fpu_begin();
  3160. while ((nbytes = walk.nbytes)) {
  3161. + kernel_fpu_begin();
  3162. aesni_cbc_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  3163. nbytes & AES_BLOCK_MASK, walk.iv);
  3164. + kernel_fpu_end();
  3165. nbytes &= AES_BLOCK_SIZE - 1;
  3166. err = blkcipher_walk_done(desc, &walk, nbytes);
  3167. }
  3168. - kernel_fpu_end();
  3169. return err;
  3170. }
  3171. @@ -454,14 +454,14 @@
  3172. err = blkcipher_walk_virt(desc, &walk);
  3173. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  3174. - kernel_fpu_begin();
  3175. while ((nbytes = walk.nbytes)) {
  3176. + kernel_fpu_begin();
  3177. aesni_cbc_dec(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  3178. nbytes & AES_BLOCK_MASK, walk.iv);
  3179. + kernel_fpu_end();
  3180. nbytes &= AES_BLOCK_SIZE - 1;
  3181. err = blkcipher_walk_done(desc, &walk, nbytes);
  3182. }
  3183. - kernel_fpu_end();
  3184. return err;
  3185. }
  3186. @@ -513,18 +513,20 @@
  3187. err = blkcipher_walk_virt_block(desc, &walk, AES_BLOCK_SIZE);
  3188. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  3189. - kernel_fpu_begin();
  3190. while ((nbytes = walk.nbytes) >= AES_BLOCK_SIZE) {
  3191. + kernel_fpu_begin();
  3192. aesni_ctr_enc_tfm(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  3193. nbytes & AES_BLOCK_MASK, walk.iv);
  3194. + kernel_fpu_end();
  3195. nbytes &= AES_BLOCK_SIZE - 1;
  3196. err = blkcipher_walk_done(desc, &walk, nbytes);
  3197. }
  3198. if (walk.nbytes) {
  3199. + kernel_fpu_begin();
  3200. ctr_crypt_final(ctx, &walk);
  3201. + kernel_fpu_end();
  3202. err = blkcipher_walk_done(desc, &walk, 0);
  3203. }
  3204. - kernel_fpu_end();
  3205. return err;
  3206. }
  3207. 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
  3208. --- linux-4.1.20.orig/arch/x86/crypto/cast5_avx_glue.c 2016-03-17 19:11:03.000000000 +0100
  3209. +++ linux-4.1.20/arch/x86/crypto/cast5_avx_glue.c 2016-03-21 20:18:30.000000000 +0100
  3210. @@ -60,7 +60,7 @@
  3211. static int ecb_crypt(struct blkcipher_desc *desc, struct blkcipher_walk *walk,
  3212. bool enc)
  3213. {
  3214. - bool fpu_enabled = false;
  3215. + bool fpu_enabled;
  3216. struct cast5_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  3217. const unsigned int bsize = CAST5_BLOCK_SIZE;
  3218. unsigned int nbytes;
  3219. @@ -76,7 +76,7 @@
  3220. u8 *wsrc = walk->src.virt.addr;
  3221. u8 *wdst = walk->dst.virt.addr;
  3222. - fpu_enabled = cast5_fpu_begin(fpu_enabled, nbytes);
  3223. + fpu_enabled = cast5_fpu_begin(false, nbytes);
  3224. /* Process multi-block batch */
  3225. if (nbytes >= bsize * CAST5_PARALLEL_BLOCKS) {
  3226. @@ -104,10 +104,9 @@
  3227. } while (nbytes >= bsize);
  3228. done:
  3229. + cast5_fpu_end(fpu_enabled);
  3230. err = blkcipher_walk_done(desc, walk, nbytes);
  3231. }
  3232. -
  3233. - cast5_fpu_end(fpu_enabled);
  3234. return err;
  3235. }
  3236. @@ -228,7 +227,7 @@
  3237. static int cbc_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  3238. struct scatterlist *src, unsigned int nbytes)
  3239. {
  3240. - bool fpu_enabled = false;
  3241. + bool fpu_enabled;
  3242. struct blkcipher_walk walk;
  3243. int err;
  3244. @@ -237,12 +236,11 @@
  3245. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  3246. while ((nbytes = walk.nbytes)) {
  3247. - fpu_enabled = cast5_fpu_begin(fpu_enabled, nbytes);
  3248. + fpu_enabled = cast5_fpu_begin(false, nbytes);
  3249. nbytes = __cbc_decrypt(desc, &walk);
  3250. + cast5_fpu_end(fpu_enabled);
  3251. err = blkcipher_walk_done(desc, &walk, nbytes);
  3252. }
  3253. -
  3254. - cast5_fpu_end(fpu_enabled);
  3255. return err;
  3256. }
  3257. @@ -312,7 +310,7 @@
  3258. static int ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  3259. struct scatterlist *src, unsigned int nbytes)
  3260. {
  3261. - bool fpu_enabled = false;
  3262. + bool fpu_enabled;
  3263. struct blkcipher_walk walk;
  3264. int err;
  3265. @@ -321,13 +319,12 @@
  3266. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  3267. while ((nbytes = walk.nbytes) >= CAST5_BLOCK_SIZE) {
  3268. - fpu_enabled = cast5_fpu_begin(fpu_enabled, nbytes);
  3269. + fpu_enabled = cast5_fpu_begin(false, nbytes);
  3270. nbytes = __ctr_crypt(desc, &walk);
  3271. + cast5_fpu_end(fpu_enabled);
  3272. err = blkcipher_walk_done(desc, &walk, nbytes);
  3273. }
  3274. - cast5_fpu_end(fpu_enabled);
  3275. -
  3276. if (walk.nbytes) {
  3277. ctr_crypt_final(desc, &walk);
  3278. err = blkcipher_walk_done(desc, &walk, 0);
  3279. diff -Nur linux-4.1.20.orig/arch/x86/crypto/glue_helper.c linux-4.1.20/arch/x86/crypto/glue_helper.c
  3280. --- linux-4.1.20.orig/arch/x86/crypto/glue_helper.c 2016-03-17 19:11:03.000000000 +0100
  3281. +++ linux-4.1.20/arch/x86/crypto/glue_helper.c 2016-03-21 20:18:30.000000000 +0100
  3282. @@ -39,7 +39,7 @@
  3283. void *ctx = crypto_blkcipher_ctx(desc->tfm);
  3284. const unsigned int bsize = 128 / 8;
  3285. unsigned int nbytes, i, func_bytes;
  3286. - bool fpu_enabled = false;
  3287. + bool fpu_enabled;
  3288. int err;
  3289. err = blkcipher_walk_virt(desc, walk);
  3290. @@ -49,7 +49,7 @@
  3291. u8 *wdst = walk->dst.virt.addr;
  3292. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  3293. - desc, fpu_enabled, nbytes);
  3294. + desc, false, nbytes);
  3295. for (i = 0; i < gctx->num_funcs; i++) {
  3296. func_bytes = bsize * gctx->funcs[i].num_blocks;
  3297. @@ -71,10 +71,10 @@
  3298. }
  3299. done:
  3300. + glue_fpu_end(fpu_enabled);
  3301. err = blkcipher_walk_done(desc, walk, nbytes);
  3302. }
  3303. - glue_fpu_end(fpu_enabled);
  3304. return err;
  3305. }
  3306. @@ -194,7 +194,7 @@
  3307. struct scatterlist *src, unsigned int nbytes)
  3308. {
  3309. const unsigned int bsize = 128 / 8;
  3310. - bool fpu_enabled = false;
  3311. + bool fpu_enabled;
  3312. struct blkcipher_walk walk;
  3313. int err;
  3314. @@ -203,12 +203,12 @@
  3315. while ((nbytes = walk.nbytes)) {
  3316. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  3317. - desc, fpu_enabled, nbytes);
  3318. + desc, false, nbytes);
  3319. nbytes = __glue_cbc_decrypt_128bit(gctx, desc, &walk);
  3320. + glue_fpu_end(fpu_enabled);
  3321. err = blkcipher_walk_done(desc, &walk, nbytes);
  3322. }
  3323. - glue_fpu_end(fpu_enabled);
  3324. return err;
  3325. }
  3326. EXPORT_SYMBOL_GPL(glue_cbc_decrypt_128bit);
  3327. @@ -277,7 +277,7 @@
  3328. struct scatterlist *src, unsigned int nbytes)
  3329. {
  3330. const unsigned int bsize = 128 / 8;
  3331. - bool fpu_enabled = false;
  3332. + bool fpu_enabled;
  3333. struct blkcipher_walk walk;
  3334. int err;
  3335. @@ -286,13 +286,12 @@
  3336. while ((nbytes = walk.nbytes) >= bsize) {
  3337. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  3338. - desc, fpu_enabled, nbytes);
  3339. + desc, false, nbytes);
  3340. nbytes = __glue_ctr_crypt_128bit(gctx, desc, &walk);
  3341. + glue_fpu_end(fpu_enabled);
  3342. err = blkcipher_walk_done(desc, &walk, nbytes);
  3343. }
  3344. - glue_fpu_end(fpu_enabled);
  3345. -
  3346. if (walk.nbytes) {
  3347. glue_ctr_crypt_final_128bit(
  3348. gctx->funcs[gctx->num_funcs - 1].fn_u.ctr, desc, &walk);
  3349. @@ -347,7 +346,7 @@
  3350. void *tweak_ctx, void *crypt_ctx)
  3351. {
  3352. const unsigned int bsize = 128 / 8;
  3353. - bool fpu_enabled = false;
  3354. + bool fpu_enabled;
  3355. struct blkcipher_walk walk;
  3356. int err;
  3357. @@ -360,21 +359,21 @@
  3358. /* set minimum length to bsize, for tweak_fn */
  3359. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  3360. - desc, fpu_enabled,
  3361. + desc, false,
  3362. nbytes < bsize ? bsize : nbytes);
  3363. -
  3364. /* calculate first value of T */
  3365. tweak_fn(tweak_ctx, walk.iv, walk.iv);
  3366. + glue_fpu_end(fpu_enabled);
  3367. while (nbytes) {
  3368. + fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  3369. + desc, false, nbytes);
  3370. nbytes = __glue_xts_crypt_128bit(gctx, crypt_ctx, desc, &walk);
  3371. + glue_fpu_end(fpu_enabled);
  3372. err = blkcipher_walk_done(desc, &walk, nbytes);
  3373. nbytes = walk.nbytes;
  3374. }
  3375. -
  3376. - glue_fpu_end(fpu_enabled);
  3377. -
  3378. return err;
  3379. }
  3380. EXPORT_SYMBOL_GPL(glue_xts_crypt_128bit);
  3381. diff -Nur linux-4.1.20.orig/arch/x86/include/asm/preempt.h linux-4.1.20/arch/x86/include/asm/preempt.h
  3382. --- linux-4.1.20.orig/arch/x86/include/asm/preempt.h 2016-03-17 19:11:03.000000000 +0100
  3383. +++ linux-4.1.20/arch/x86/include/asm/preempt.h 2016-03-21 20:18:30.000000000 +0100
  3384. @@ -82,17 +82,33 @@
  3385. * a decrement which hits zero means we have no preempt_count and should
  3386. * reschedule.
  3387. */
  3388. -static __always_inline bool __preempt_count_dec_and_test(void)
  3389. +static __always_inline bool ____preempt_count_dec_and_test(void)
  3390. {
  3391. GEN_UNARY_RMWcc("decl", __preempt_count, __percpu_arg(0), "e");
  3392. }
  3393. +static __always_inline bool __preempt_count_dec_and_test(void)
  3394. +{
  3395. + if (____preempt_count_dec_and_test())
  3396. + return true;
  3397. +#ifdef CONFIG_PREEMPT_LAZY
  3398. + return test_thread_flag(TIF_NEED_RESCHED_LAZY);
  3399. +#else
  3400. + return false;
  3401. +#endif
  3402. +}
  3403. +
  3404. /*
  3405. * Returns true when we need to resched and can (barring IRQ state).
  3406. */
  3407. static __always_inline bool should_resched(int preempt_offset)
  3408. {
  3409. +#ifdef CONFIG_PREEMPT_LAZY
  3410. + return unlikely(raw_cpu_read_4(__preempt_count) == preempt_offset ||
  3411. + test_thread_flag(TIF_NEED_RESCHED_LAZY));
  3412. +#else
  3413. return unlikely(raw_cpu_read_4(__preempt_count) == preempt_offset);
  3414. +#endif
  3415. }
  3416. #ifdef CONFIG_PREEMPT
  3417. diff -Nur linux-4.1.20.orig/arch/x86/include/asm/signal.h linux-4.1.20/arch/x86/include/asm/signal.h
  3418. --- linux-4.1.20.orig/arch/x86/include/asm/signal.h 2016-03-17 19:11:03.000000000 +0100
  3419. +++ linux-4.1.20/arch/x86/include/asm/signal.h 2016-03-21 20:18:30.000000000 +0100
  3420. @@ -23,6 +23,19 @@
  3421. unsigned long sig[_NSIG_WORDS];
  3422. } sigset_t;
  3423. +/*
  3424. + * Because some traps use the IST stack, we must keep preemption
  3425. + * disabled while calling do_trap(), but do_trap() may call
  3426. + * force_sig_info() which will grab the signal spin_locks for the
  3427. + * task, which in PREEMPT_RT_FULL are mutexes. By defining
  3428. + * ARCH_RT_DELAYS_SIGNAL_SEND the force_sig_info() will set
  3429. + * TIF_NOTIFY_RESUME and set up the signal to be sent on exit of the
  3430. + * trap.
  3431. + */
  3432. +#if defined(CONFIG_PREEMPT_RT_FULL)
  3433. +#define ARCH_RT_DELAYS_SIGNAL_SEND
  3434. +#endif
  3435. +
  3436. #ifndef CONFIG_COMPAT
  3437. typedef sigset_t compat_sigset_t;
  3438. #endif
  3439. diff -Nur linux-4.1.20.orig/arch/x86/include/asm/stackprotector.h linux-4.1.20/arch/x86/include/asm/stackprotector.h
  3440. --- linux-4.1.20.orig/arch/x86/include/asm/stackprotector.h 2016-03-17 19:11:03.000000000 +0100
  3441. +++ linux-4.1.20/arch/x86/include/asm/stackprotector.h 2016-03-21 20:18:30.000000000 +0100
  3442. @@ -57,7 +57,7 @@
  3443. */
  3444. static __always_inline void boot_init_stack_canary(void)
  3445. {
  3446. - u64 canary;
  3447. + u64 uninitialized_var(canary);
  3448. u64 tsc;
  3449. #ifdef CONFIG_X86_64
  3450. @@ -68,8 +68,16 @@
  3451. * of randomness. The TSC only matters for very early init,
  3452. * there it already has some randomness on most systems. Later
  3453. * on during the bootup the random pool has true entropy too.
  3454. + *
  3455. + * For preempt-rt we need to weaken the randomness a bit, as
  3456. + * we can't call into the random generator from atomic context
  3457. + * due to locking constraints. We just leave canary
  3458. + * uninitialized and use the TSC based randomness on top of
  3459. + * it.
  3460. */
  3461. +#ifndef CONFIG_PREEMPT_RT_FULL
  3462. get_random_bytes(&canary, sizeof(canary));
  3463. +#endif
  3464. tsc = __native_read_tsc();
  3465. canary += tsc + (tsc << 32UL);
  3466. 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
  3467. --- linux-4.1.20.orig/arch/x86/include/asm/thread_info.h 2016-03-17 19:11:03.000000000 +0100
  3468. +++ linux-4.1.20/arch/x86/include/asm/thread_info.h 2016-03-21 20:18:30.000000000 +0100
  3469. @@ -55,6 +55,8 @@
  3470. __u32 status; /* thread synchronous flags */
  3471. __u32 cpu; /* current CPU */
  3472. int saved_preempt_count;
  3473. + int preempt_lazy_count; /* 0 => lazy preemptable
  3474. + <0 => BUG */
  3475. mm_segment_t addr_limit;
  3476. void __user *sysenter_return;
  3477. unsigned int sig_on_uaccess_error:1;
  3478. @@ -95,6 +97,7 @@
  3479. #define TIF_SYSCALL_EMU 6 /* syscall emulation active */
  3480. #define TIF_SYSCALL_AUDIT 7 /* syscall auditing active */
  3481. #define TIF_SECCOMP 8 /* secure computing */
  3482. +#define TIF_NEED_RESCHED_LAZY 9 /* lazy rescheduling necessary */
  3483. #define TIF_USER_RETURN_NOTIFY 11 /* notify kernel of userspace return */
  3484. #define TIF_UPROBE 12 /* breakpointed or singlestepping */
  3485. #define TIF_NOTSC 16 /* TSC is not accessible in userland */
  3486. @@ -119,6 +122,7 @@
  3487. #define _TIF_SYSCALL_EMU (1 << TIF_SYSCALL_EMU)
  3488. #define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
  3489. #define _TIF_SECCOMP (1 << TIF_SECCOMP)
  3490. +#define _TIF_NEED_RESCHED_LAZY (1 << TIF_NEED_RESCHED_LAZY)
  3491. #define _TIF_USER_RETURN_NOTIFY (1 << TIF_USER_RETURN_NOTIFY)
  3492. #define _TIF_UPROBE (1 << TIF_UPROBE)
  3493. #define _TIF_NOTSC (1 << TIF_NOTSC)
  3494. @@ -168,6 +172,8 @@
  3495. #define _TIF_WORK_CTXSW_PREV (_TIF_WORK_CTXSW|_TIF_USER_RETURN_NOTIFY)
  3496. #define _TIF_WORK_CTXSW_NEXT (_TIF_WORK_CTXSW)
  3497. +#define _TIF_NEED_RESCHED_MASK (_TIF_NEED_RESCHED | _TIF_NEED_RESCHED_LAZY)
  3498. +
  3499. #define STACK_WARN (THREAD_SIZE/8)
  3500. /*
  3501. diff -Nur linux-4.1.20.orig/arch/x86/include/asm/uaccess.h linux-4.1.20/arch/x86/include/asm/uaccess.h
  3502. --- linux-4.1.20.orig/arch/x86/include/asm/uaccess.h 2016-03-17 19:11:03.000000000 +0100
  3503. +++ linux-4.1.20/arch/x86/include/asm/uaccess.h 2016-03-21 20:18:30.000000000 +0100
  3504. @@ -74,7 +74,8 @@
  3505. * @addr: User space pointer to start of block to check
  3506. * @size: Size of block to check
  3507. *
  3508. - * Context: User context only. This function may sleep.
  3509. + * Context: User context only. This function may sleep if pagefaults are
  3510. + * enabled.
  3511. *
  3512. * Checks if a pointer to a block of memory in user space is valid.
  3513. *
  3514. @@ -145,7 +146,8 @@
  3515. * @x: Variable to store result.
  3516. * @ptr: Source address, in user space.
  3517. *
  3518. - * Context: User context only. This function may sleep.
  3519. + * Context: User context only. This function may sleep if pagefaults are
  3520. + * enabled.
  3521. *
  3522. * This macro copies a single simple variable from user space to kernel
  3523. * space. It supports simple types like char and int, but not larger
  3524. @@ -240,7 +242,8 @@
  3525. * @x: Value to copy to user space.
  3526. * @ptr: Destination address, in user space.
  3527. *
  3528. - * Context: User context only. This function may sleep.
  3529. + * Context: User context only. This function may sleep if pagefaults are
  3530. + * enabled.
  3531. *
  3532. * This macro copies a single simple value from kernel space to user
  3533. * space. It supports simple types like char and int, but not larger
  3534. @@ -455,7 +458,8 @@
  3535. * @x: Variable to store result.
  3536. * @ptr: Source address, in user space.
  3537. *
  3538. - * Context: User context only. This function may sleep.
  3539. + * Context: User context only. This function may sleep if pagefaults are
  3540. + * enabled.
  3541. *
  3542. * This macro copies a single simple variable from user space to kernel
  3543. * space. It supports simple types like char and int, but not larger
  3544. @@ -479,7 +483,8 @@
  3545. * @x: Value to copy to user space.
  3546. * @ptr: Destination address, in user space.
  3547. *
  3548. - * Context: User context only. This function may sleep.
  3549. + * Context: User context only. This function may sleep if pagefaults are
  3550. + * enabled.
  3551. *
  3552. * This macro copies a single simple value from kernel space to user
  3553. * space. It supports simple types like char and int, but not larger
  3554. 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
  3555. --- linux-4.1.20.orig/arch/x86/include/asm/uaccess_32.h 2016-03-17 19:11:03.000000000 +0100
  3556. +++ linux-4.1.20/arch/x86/include/asm/uaccess_32.h 2016-03-21 20:18:30.000000000 +0100
  3557. @@ -70,7 +70,8 @@
  3558. * @from: Source address, in kernel space.
  3559. * @n: Number of bytes to copy.
  3560. *
  3561. - * Context: User context only. This function may sleep.
  3562. + * Context: User context only. This function may sleep if pagefaults are
  3563. + * enabled.
  3564. *
  3565. * Copy data from kernel space to user space. Caller must check
  3566. * the specified block with access_ok() before calling this function.
  3567. @@ -117,7 +118,8 @@
  3568. * @from: Source address, in user space.
  3569. * @n: Number of bytes to copy.
  3570. *
  3571. - * Context: User context only. This function may sleep.
  3572. + * Context: User context only. This function may sleep if pagefaults are
  3573. + * enabled.
  3574. *
  3575. * Copy data from user space to kernel space. Caller must check
  3576. * the specified block with access_ok() before calling this function.
  3577. 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
  3578. --- linux-4.1.20.orig/arch/x86/include/asm/uv/uv_bau.h 2016-03-17 19:11:03.000000000 +0100
  3579. +++ linux-4.1.20/arch/x86/include/asm/uv/uv_bau.h 2016-03-21 20:18:30.000000000 +0100
  3580. @@ -615,9 +615,9 @@
  3581. cycles_t send_message;
  3582. cycles_t period_end;
  3583. cycles_t period_time;
  3584. - spinlock_t uvhub_lock;
  3585. - spinlock_t queue_lock;
  3586. - spinlock_t disable_lock;
  3587. + raw_spinlock_t uvhub_lock;
  3588. + raw_spinlock_t queue_lock;
  3589. + raw_spinlock_t disable_lock;
  3590. /* tunables */
  3591. int max_concurr;
  3592. int max_concurr_const;
  3593. @@ -776,15 +776,15 @@
  3594. * to be lowered below the current 'v'. atomic_add_unless can only stop
  3595. * on equal.
  3596. */
  3597. -static inline int atomic_inc_unless_ge(spinlock_t *lock, atomic_t *v, int u)
  3598. +static inline int atomic_inc_unless_ge(raw_spinlock_t *lock, atomic_t *v, int u)
  3599. {
  3600. - spin_lock(lock);
  3601. + raw_spin_lock(lock);
  3602. if (atomic_read(v) >= u) {
  3603. - spin_unlock(lock);
  3604. + raw_spin_unlock(lock);
  3605. return 0;
  3606. }
  3607. atomic_inc(v);
  3608. - spin_unlock(lock);
  3609. + raw_spin_unlock(lock);
  3610. return 1;
  3611. }
  3612. 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
  3613. --- linux-4.1.20.orig/arch/x86/include/asm/uv/uv_hub.h 2016-03-17 19:11:03.000000000 +0100
  3614. +++ linux-4.1.20/arch/x86/include/asm/uv/uv_hub.h 2016-03-21 20:18:30.000000000 +0100
  3615. @@ -492,7 +492,7 @@
  3616. unsigned short nr_online_cpus;
  3617. unsigned short pnode;
  3618. short memory_nid;
  3619. - spinlock_t nmi_lock; /* obsolete, see uv_hub_nmi */
  3620. + raw_spinlock_t nmi_lock; /* obsolete, see uv_hub_nmi */
  3621. unsigned long nmi_count; /* obsolete, see uv_hub_nmi */
  3622. };
  3623. extern struct uv_blade_info *uv_blade_info;
  3624. 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
  3625. --- linux-4.1.20.orig/arch/x86/kernel/apic/io_apic.c 2016-03-17 19:11:03.000000000 +0100
  3626. +++ linux-4.1.20/arch/x86/kernel/apic/io_apic.c 2016-03-21 20:18:30.000000000 +0100
  3627. @@ -1891,7 +1891,8 @@
  3628. static inline bool ioapic_irqd_mask(struct irq_data *data, struct irq_cfg *cfg)
  3629. {
  3630. /* If we are moving the irq we need to mask it */
  3631. - if (unlikely(irqd_is_setaffinity_pending(data))) {
  3632. + if (unlikely(irqd_is_setaffinity_pending(data) &&
  3633. + !irqd_irq_inprogress(data))) {
  3634. mask_ioapic(cfg);
  3635. return true;
  3636. }
  3637. 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
  3638. --- linux-4.1.20.orig/arch/x86/kernel/apic/x2apic_uv_x.c 2016-03-17 19:11:03.000000000 +0100
  3639. +++ linux-4.1.20/arch/x86/kernel/apic/x2apic_uv_x.c 2016-03-21 20:18:30.000000000 +0100
  3640. @@ -949,7 +949,7 @@
  3641. uv_blade_info[blade].pnode = pnode;
  3642. uv_blade_info[blade].nr_possible_cpus = 0;
  3643. uv_blade_info[blade].nr_online_cpus = 0;
  3644. - spin_lock_init(&uv_blade_info[blade].nmi_lock);
  3645. + raw_spin_lock_init(&uv_blade_info[blade].nmi_lock);
  3646. min_pnode = min(pnode, min_pnode);
  3647. max_pnode = max(pnode, max_pnode);
  3648. blade++;
  3649. diff -Nur linux-4.1.20.orig/arch/x86/kernel/asm-offsets.c linux-4.1.20/arch/x86/kernel/asm-offsets.c
  3650. --- linux-4.1.20.orig/arch/x86/kernel/asm-offsets.c 2016-03-17 19:11:03.000000000 +0100
  3651. +++ linux-4.1.20/arch/x86/kernel/asm-offsets.c 2016-03-21 20:18:30.000000000 +0100
  3652. @@ -32,6 +32,7 @@
  3653. OFFSET(TI_flags, thread_info, flags);
  3654. OFFSET(TI_status, thread_info, status);
  3655. OFFSET(TI_addr_limit, thread_info, addr_limit);
  3656. + OFFSET(TI_preempt_lazy_count, thread_info, preempt_lazy_count);
  3657. BLANK();
  3658. OFFSET(crypto_tfm_ctx_offset, crypto_tfm, __crt_ctx);
  3659. @@ -71,4 +72,5 @@
  3660. BLANK();
  3661. DEFINE(PTREGS_SIZE, sizeof(struct pt_regs));
  3662. + DEFINE(_PREEMPT_ENABLED, PREEMPT_ENABLED);
  3663. }
  3664. 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
  3665. --- linux-4.1.20.orig/arch/x86/kernel/cpu/mcheck/mce.c 2016-03-17 19:11:03.000000000 +0100
  3666. +++ linux-4.1.20/arch/x86/kernel/cpu/mcheck/mce.c 2016-03-21 20:18:30.000000000 +0100
  3667. @@ -41,6 +41,8 @@
  3668. #include <linux/debugfs.h>
  3669. #include <linux/irq_work.h>
  3670. #include <linux/export.h>
  3671. +#include <linux/jiffies.h>
  3672. +#include <linux/work-simple.h>
  3673. #include <asm/processor.h>
  3674. #include <asm/traps.h>
  3675. @@ -1267,7 +1269,7 @@
  3676. static unsigned long check_interval = INITIAL_CHECK_INTERVAL;
  3677. static DEFINE_PER_CPU(unsigned long, mce_next_interval); /* in jiffies */
  3678. -static DEFINE_PER_CPU(struct timer_list, mce_timer);
  3679. +static DEFINE_PER_CPU(struct hrtimer, mce_timer);
  3680. static unsigned long mce_adjust_timer_default(unsigned long interval)
  3681. {
  3682. @@ -1276,32 +1278,18 @@
  3683. static unsigned long (*mce_adjust_timer)(unsigned long interval) = mce_adjust_timer_default;
  3684. -static void __restart_timer(struct timer_list *t, unsigned long interval)
  3685. +static enum hrtimer_restart __restart_timer(struct hrtimer *timer, unsigned long interval)
  3686. {
  3687. - unsigned long when = jiffies + interval;
  3688. - unsigned long flags;
  3689. -
  3690. - local_irq_save(flags);
  3691. -
  3692. - if (timer_pending(t)) {
  3693. - if (time_before(when, t->expires))
  3694. - mod_timer_pinned(t, when);
  3695. - } else {
  3696. - t->expires = round_jiffies(when);
  3697. - add_timer_on(t, smp_processor_id());
  3698. - }
  3699. -
  3700. - local_irq_restore(flags);
  3701. + if (!interval)
  3702. + return HRTIMER_NORESTART;
  3703. + hrtimer_forward_now(timer, ns_to_ktime(jiffies_to_nsecs(interval)));
  3704. + return HRTIMER_RESTART;
  3705. }
  3706. -static void mce_timer_fn(unsigned long data)
  3707. +static enum hrtimer_restart mce_timer_fn(struct hrtimer *timer)
  3708. {
  3709. - struct timer_list *t = this_cpu_ptr(&mce_timer);
  3710. - int cpu = smp_processor_id();
  3711. unsigned long iv;
  3712. - WARN_ON(cpu != data);
  3713. -
  3714. iv = __this_cpu_read(mce_next_interval);
  3715. if (mce_available(this_cpu_ptr(&cpu_info))) {
  3716. @@ -1324,7 +1312,7 @@
  3717. done:
  3718. __this_cpu_write(mce_next_interval, iv);
  3719. - __restart_timer(t, iv);
  3720. + return __restart_timer(timer, iv);
  3721. }
  3722. /*
  3723. @@ -1332,7 +1320,7 @@
  3724. */
  3725. void mce_timer_kick(unsigned long interval)
  3726. {
  3727. - struct timer_list *t = this_cpu_ptr(&mce_timer);
  3728. + struct hrtimer *t = this_cpu_ptr(&mce_timer);
  3729. unsigned long iv = __this_cpu_read(mce_next_interval);
  3730. __restart_timer(t, interval);
  3731. @@ -1347,7 +1335,7 @@
  3732. int cpu;
  3733. for_each_online_cpu(cpu)
  3734. - del_timer_sync(&per_cpu(mce_timer, cpu));
  3735. + hrtimer_cancel(&per_cpu(mce_timer, cpu));
  3736. }
  3737. static void mce_do_trigger(struct work_struct *work)
  3738. @@ -1357,6 +1345,56 @@
  3739. static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
  3740. +static void __mce_notify_work(struct swork_event *event)
  3741. +{
  3742. + /* Not more than two messages every minute */
  3743. + static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
  3744. +
  3745. + /* wake processes polling /dev/mcelog */
  3746. + wake_up_interruptible(&mce_chrdev_wait);
  3747. +
  3748. + /*
  3749. + * There is no risk of missing notifications because
  3750. + * work_pending is always cleared before the function is
  3751. + * executed.
  3752. + */
  3753. + if (mce_helper[0] && !work_pending(&mce_trigger_work))
  3754. + schedule_work(&mce_trigger_work);
  3755. +
  3756. + if (__ratelimit(&ratelimit))
  3757. + pr_info(HW_ERR "Machine check events logged\n");
  3758. +}
  3759. +
  3760. +#ifdef CONFIG_PREEMPT_RT_FULL
  3761. +static bool notify_work_ready __read_mostly;
  3762. +static struct swork_event notify_work;
  3763. +
  3764. +static int mce_notify_work_init(void)
  3765. +{
  3766. + int err;
  3767. +
  3768. + err = swork_get();
  3769. + if (err)
  3770. + return err;
  3771. +
  3772. + INIT_SWORK(&notify_work, __mce_notify_work);
  3773. + notify_work_ready = true;
  3774. + return 0;
  3775. +}
  3776. +
  3777. +static void mce_notify_work(void)
  3778. +{
  3779. + if (notify_work_ready)
  3780. + swork_queue(&notify_work);
  3781. +}
  3782. +#else
  3783. +static void mce_notify_work(void)
  3784. +{
  3785. + __mce_notify_work(NULL);
  3786. +}
  3787. +static inline int mce_notify_work_init(void) { return 0; }
  3788. +#endif
  3789. +
  3790. /*
  3791. * Notify the user(s) about new machine check events.
  3792. * Can be called from interrupt context, but not from machine check/NMI
  3793. @@ -1364,19 +1402,8 @@
  3794. */
  3795. int mce_notify_irq(void)
  3796. {
  3797. - /* Not more than two messages every minute */
  3798. - static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
  3799. -
  3800. if (test_and_clear_bit(0, &mce_need_notify)) {
  3801. - /* wake processes polling /dev/mcelog */
  3802. - wake_up_interruptible(&mce_chrdev_wait);
  3803. -
  3804. - if (mce_helper[0])
  3805. - schedule_work(&mce_trigger_work);
  3806. -
  3807. - if (__ratelimit(&ratelimit))
  3808. - pr_info(HW_ERR "Machine check events logged\n");
  3809. -
  3810. + mce_notify_work();
  3811. return 1;
  3812. }
  3813. return 0;
  3814. @@ -1649,7 +1676,7 @@
  3815. }
  3816. }
  3817. -static void mce_start_timer(unsigned int cpu, struct timer_list *t)
  3818. +static void mce_start_timer(unsigned int cpu, struct hrtimer *t)
  3819. {
  3820. unsigned long iv = check_interval * HZ;
  3821. @@ -1658,16 +1685,17 @@
  3822. per_cpu(mce_next_interval, cpu) = iv;
  3823. - t->expires = round_jiffies(jiffies + iv);
  3824. - add_timer_on(t, cpu);
  3825. + hrtimer_start_range_ns(t, ns_to_ktime(jiffies_to_usecs(iv) * 1000ULL),
  3826. + 0, HRTIMER_MODE_REL_PINNED);
  3827. }
  3828. static void __mcheck_cpu_init_timer(void)
  3829. {
  3830. - struct timer_list *t = this_cpu_ptr(&mce_timer);
  3831. + struct hrtimer *t = this_cpu_ptr(&mce_timer);
  3832. unsigned int cpu = smp_processor_id();
  3833. - setup_timer(t, mce_timer_fn, cpu);
  3834. + hrtimer_init(t, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  3835. + t->function = mce_timer_fn;
  3836. mce_start_timer(cpu, t);
  3837. }
  3838. @@ -2345,6 +2373,8 @@
  3839. if (!mce_available(raw_cpu_ptr(&cpu_info)))
  3840. return;
  3841. + hrtimer_cancel(this_cpu_ptr(&mce_timer));
  3842. +
  3843. if (!(action & CPU_TASKS_FROZEN))
  3844. cmci_clear();
  3845. for (i = 0; i < mca_cfg.banks; i++) {
  3846. @@ -2371,6 +2401,7 @@
  3847. if (b->init)
  3848. wrmsrl(MSR_IA32_MCx_CTL(i), b->ctl);
  3849. }
  3850. + __mcheck_cpu_init_timer();
  3851. }
  3852. /* Get notified when a cpu comes on/off. Be hotplug friendly. */
  3853. @@ -2378,7 +2409,6 @@
  3854. mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
  3855. {
  3856. unsigned int cpu = (unsigned long)hcpu;
  3857. - struct timer_list *t = &per_cpu(mce_timer, cpu);
  3858. switch (action & ~CPU_TASKS_FROZEN) {
  3859. case CPU_ONLINE:
  3860. @@ -2398,11 +2428,9 @@
  3861. break;
  3862. case CPU_DOWN_PREPARE:
  3863. smp_call_function_single(cpu, mce_disable_cpu, &action, 1);
  3864. - del_timer_sync(t);
  3865. break;
  3866. case CPU_DOWN_FAILED:
  3867. smp_call_function_single(cpu, mce_reenable_cpu, &action, 1);
  3868. - mce_start_timer(cpu, t);
  3869. break;
  3870. }
  3871. @@ -2441,6 +2469,10 @@
  3872. goto err_out;
  3873. }
  3874. + err = mce_notify_work_init();
  3875. + if (err)
  3876. + goto err_out;
  3877. +
  3878. if (!zalloc_cpumask_var(&mce_device_initialized, GFP_KERNEL)) {
  3879. err = -ENOMEM;
  3880. goto err_out;
  3881. diff -Nur linux-4.1.20.orig/arch/x86/kernel/dumpstack_32.c linux-4.1.20/arch/x86/kernel/dumpstack_32.c
  3882. --- linux-4.1.20.orig/arch/x86/kernel/dumpstack_32.c 2016-03-17 19:11:03.000000000 +0100
  3883. +++ linux-4.1.20/arch/x86/kernel/dumpstack_32.c 2016-03-21 20:18:30.000000000 +0100
  3884. @@ -42,7 +42,7 @@
  3885. unsigned long *stack, unsigned long bp,
  3886. const struct stacktrace_ops *ops, void *data)
  3887. {
  3888. - const unsigned cpu = get_cpu();
  3889. + const unsigned cpu = get_cpu_light();
  3890. int graph = 0;
  3891. u32 *prev_esp;
  3892. @@ -86,7 +86,7 @@
  3893. break;
  3894. touch_nmi_watchdog();
  3895. }
  3896. - put_cpu();
  3897. + put_cpu_light();
  3898. }
  3899. EXPORT_SYMBOL(dump_trace);
  3900. diff -Nur linux-4.1.20.orig/arch/x86/kernel/dumpstack_64.c linux-4.1.20/arch/x86/kernel/dumpstack_64.c
  3901. --- linux-4.1.20.orig/arch/x86/kernel/dumpstack_64.c 2016-03-17 19:11:03.000000000 +0100
  3902. +++ linux-4.1.20/arch/x86/kernel/dumpstack_64.c 2016-03-21 20:18:30.000000000 +0100
  3903. @@ -152,7 +152,7 @@
  3904. unsigned long *stack, unsigned long bp,
  3905. const struct stacktrace_ops *ops, void *data)
  3906. {
  3907. - const unsigned cpu = get_cpu();
  3908. + const unsigned cpu = get_cpu_light();
  3909. struct thread_info *tinfo;
  3910. unsigned long *irq_stack = (unsigned long *)per_cpu(irq_stack_ptr, cpu);
  3911. unsigned long dummy;
  3912. @@ -241,7 +241,7 @@
  3913. * This handles the process stack:
  3914. */
  3915. bp = ops->walk_stack(tinfo, stack, bp, ops, data, NULL, &graph);
  3916. - put_cpu();
  3917. + put_cpu_light();
  3918. }
  3919. EXPORT_SYMBOL(dump_trace);
  3920. @@ -255,7 +255,7 @@
  3921. int cpu;
  3922. int i;
  3923. - preempt_disable();
  3924. + migrate_disable();
  3925. cpu = smp_processor_id();
  3926. irq_stack_end = (unsigned long *)(per_cpu(irq_stack_ptr, cpu));
  3927. @@ -291,7 +291,7 @@
  3928. pr_cont(" %016lx", *stack++);
  3929. touch_nmi_watchdog();
  3930. }
  3931. - preempt_enable();
  3932. + migrate_enable();
  3933. pr_cont("\n");
  3934. show_trace_log_lvl(task, regs, sp, bp, log_lvl);
  3935. diff -Nur linux-4.1.20.orig/arch/x86/kernel/entry_32.S linux-4.1.20/arch/x86/kernel/entry_32.S
  3936. --- linux-4.1.20.orig/arch/x86/kernel/entry_32.S 2016-03-17 19:11:03.000000000 +0100
  3937. +++ linux-4.1.20/arch/x86/kernel/entry_32.S 2016-03-21 20:18:30.000000000 +0100
  3938. @@ -359,8 +359,24 @@
  3939. ENTRY(resume_kernel)
  3940. DISABLE_INTERRUPTS(CLBR_ANY)
  3941. need_resched:
  3942. + # preempt count == 0 + NEED_RS set?
  3943. cmpl $0,PER_CPU_VAR(__preempt_count)
  3944. +#ifndef CONFIG_PREEMPT_LAZY
  3945. jnz restore_all
  3946. +#else
  3947. + jz test_int_off
  3948. +
  3949. + # atleast preempt count == 0 ?
  3950. + cmpl $_PREEMPT_ENABLED,PER_CPU_VAR(__preempt_count)
  3951. + jne restore_all
  3952. +
  3953. + cmpl $0,TI_preempt_lazy_count(%ebp) # non-zero preempt_lazy_count ?
  3954. + jnz restore_all
  3955. +
  3956. + testl $_TIF_NEED_RESCHED_LAZY, TI_flags(%ebp)
  3957. + jz restore_all
  3958. +test_int_off:
  3959. +#endif
  3960. testl $X86_EFLAGS_IF,PT_EFLAGS(%esp) # interrupts off (exception path) ?
  3961. jz restore_all
  3962. call preempt_schedule_irq
  3963. @@ -594,7 +610,7 @@
  3964. ALIGN
  3965. RING0_PTREGS_FRAME # can't unwind into user space anyway
  3966. work_pending:
  3967. - testb $_TIF_NEED_RESCHED, %cl
  3968. + testl $_TIF_NEED_RESCHED_MASK, %ecx
  3969. jz work_notifysig
  3970. work_resched:
  3971. call schedule
  3972. @@ -607,7 +623,7 @@
  3973. andl $_TIF_WORK_MASK, %ecx # is there any work to be done other
  3974. # than syscall tracing?
  3975. jz restore_all
  3976. - testb $_TIF_NEED_RESCHED, %cl
  3977. + testl $_TIF_NEED_RESCHED_MASK, %ecx
  3978. jnz work_resched
  3979. work_notifysig: # deal with pending signals and
  3980. diff -Nur linux-4.1.20.orig/arch/x86/kernel/entry_64.S linux-4.1.20/arch/x86/kernel/entry_64.S
  3981. --- linux-4.1.20.orig/arch/x86/kernel/entry_64.S 2016-03-17 19:11:03.000000000 +0100
  3982. +++ linux-4.1.20/arch/x86/kernel/entry_64.S 2016-03-21 20:18:30.000000000 +0100
  3983. @@ -370,8 +370,8 @@
  3984. /* First do a reschedule test. */
  3985. /* edx: work, edi: workmask */
  3986. int_careful:
  3987. - bt $TIF_NEED_RESCHED,%edx
  3988. - jnc int_very_careful
  3989. + testl $_TIF_NEED_RESCHED_MASK,%edx
  3990. + jz int_very_careful
  3991. TRACE_IRQS_ON
  3992. ENABLE_INTERRUPTS(CLBR_NONE)
  3993. pushq_cfi %rdi
  3994. @@ -776,7 +776,23 @@
  3995. bt $9,EFLAGS(%rsp) /* interrupts were off? */
  3996. jnc 1f
  3997. 0: cmpl $0,PER_CPU_VAR(__preempt_count)
  3998. +#ifndef CONFIG_PREEMPT_LAZY
  3999. jnz 1f
  4000. +#else
  4001. + jz do_preempt_schedule_irq
  4002. +
  4003. + # atleast preempt count == 0 ?
  4004. + cmpl $_PREEMPT_ENABLED,PER_CPU_VAR(__preempt_count)
  4005. + jnz 1f
  4006. +
  4007. + GET_THREAD_INFO(%rcx)
  4008. + cmpl $0, TI_preempt_lazy_count(%rcx)
  4009. + jnz 1f
  4010. +
  4011. + bt $TIF_NEED_RESCHED_LAZY,TI_flags(%rcx)
  4012. + jnc 1f
  4013. +do_preempt_schedule_irq:
  4014. +#endif
  4015. call preempt_schedule_irq
  4016. jmp 0b
  4017. 1:
  4018. @@ -844,8 +860,8 @@
  4019. /* edi: workmask, edx: work */
  4020. retint_careful:
  4021. CFI_RESTORE_STATE
  4022. - bt $TIF_NEED_RESCHED,%edx
  4023. - jnc retint_signal
  4024. + testl $_TIF_NEED_RESCHED_MASK,%edx
  4025. + jz retint_signal
  4026. TRACE_IRQS_ON
  4027. ENABLE_INTERRUPTS(CLBR_NONE)
  4028. pushq_cfi %rdi
  4029. @@ -1118,6 +1134,7 @@
  4030. jmp 2b
  4031. .previous
  4032. +#ifndef CONFIG_PREEMPT_RT_FULL
  4033. /* Call softirq on interrupt stack. Interrupts are off. */
  4034. ENTRY(do_softirq_own_stack)
  4035. CFI_STARTPROC
  4036. @@ -1137,6 +1154,7 @@
  4037. ret
  4038. CFI_ENDPROC
  4039. END(do_softirq_own_stack)
  4040. +#endif
  4041. #ifdef CONFIG_XEN
  4042. idtentry xen_hypervisor_callback xen_do_hypervisor_callback has_error_code=0
  4043. diff -Nur linux-4.1.20.orig/arch/x86/kernel/irq_32.c linux-4.1.20/arch/x86/kernel/irq_32.c
  4044. --- linux-4.1.20.orig/arch/x86/kernel/irq_32.c 2016-03-17 19:11:03.000000000 +0100
  4045. +++ linux-4.1.20/arch/x86/kernel/irq_32.c 2016-03-21 20:18:30.000000000 +0100
  4046. @@ -135,6 +135,7 @@
  4047. cpu, per_cpu(hardirq_stack, cpu), per_cpu(softirq_stack, cpu));
  4048. }
  4049. +#ifndef CONFIG_PREEMPT_RT_FULL
  4050. void do_softirq_own_stack(void)
  4051. {
  4052. struct thread_info *curstk;
  4053. @@ -153,6 +154,7 @@
  4054. call_on_stack(__do_softirq, isp);
  4055. }
  4056. +#endif
  4057. bool handle_irq(unsigned irq, struct pt_regs *regs)
  4058. {
  4059. diff -Nur linux-4.1.20.orig/arch/x86/kernel/process_32.c linux-4.1.20/arch/x86/kernel/process_32.c
  4060. --- linux-4.1.20.orig/arch/x86/kernel/process_32.c 2016-03-17 19:11:03.000000000 +0100
  4061. +++ linux-4.1.20/arch/x86/kernel/process_32.c 2016-03-21 20:18:30.000000000 +0100
  4062. @@ -35,6 +35,7 @@
  4063. #include <linux/uaccess.h>
  4064. #include <linux/io.h>
  4065. #include <linux/kdebug.h>
  4066. +#include <linux/highmem.h>
  4067. #include <asm/pgtable.h>
  4068. #include <asm/ldt.h>
  4069. @@ -210,6 +211,35 @@
  4070. }
  4071. EXPORT_SYMBOL_GPL(start_thread);
  4072. +#ifdef CONFIG_PREEMPT_RT_FULL
  4073. +static void switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p)
  4074. +{
  4075. + int i;
  4076. +
  4077. + /*
  4078. + * Clear @prev's kmap_atomic mappings
  4079. + */
  4080. + for (i = 0; i < prev_p->kmap_idx; i++) {
  4081. + int idx = i + KM_TYPE_NR * smp_processor_id();
  4082. + pte_t *ptep = kmap_pte - idx;
  4083. +
  4084. + kpte_clear_flush(ptep, __fix_to_virt(FIX_KMAP_BEGIN + idx));
  4085. + }
  4086. + /*
  4087. + * Restore @next_p's kmap_atomic mappings
  4088. + */
  4089. + for (i = 0; i < next_p->kmap_idx; i++) {
  4090. + int idx = i + KM_TYPE_NR * smp_processor_id();
  4091. +
  4092. + if (!pte_none(next_p->kmap_pte[i]))
  4093. + set_pte(kmap_pte - idx, next_p->kmap_pte[i]);
  4094. + }
  4095. +}
  4096. +#else
  4097. +static inline void
  4098. +switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p) { }
  4099. +#endif
  4100. +
  4101. /*
  4102. * switch_to(x,y) should switch tasks from x to y.
  4103. @@ -292,6 +322,8 @@
  4104. task_thread_info(next_p)->flags & _TIF_WORK_CTXSW_NEXT))
  4105. __switch_to_xtra(prev_p, next_p, tss);
  4106. + switch_kmaps(prev_p, next_p);
  4107. +
  4108. /*
  4109. * Leave lazy mode, flushing any hypercalls made here.
  4110. * This must be done before restoring TLS segments so
  4111. diff -Nur linux-4.1.20.orig/arch/x86/kernel/signal.c linux-4.1.20/arch/x86/kernel/signal.c
  4112. --- linux-4.1.20.orig/arch/x86/kernel/signal.c 2016-03-17 19:11:03.000000000 +0100
  4113. +++ linux-4.1.20/arch/x86/kernel/signal.c 2016-03-21 20:18:30.000000000 +0100
  4114. @@ -726,6 +726,14 @@
  4115. {
  4116. user_exit();
  4117. +#ifdef ARCH_RT_DELAYS_SIGNAL_SEND
  4118. + if (unlikely(current->forced_info.si_signo)) {
  4119. + struct task_struct *t = current;
  4120. + force_sig_info(t->forced_info.si_signo, &t->forced_info, t);
  4121. + t->forced_info.si_signo = 0;
  4122. + }
  4123. +#endif
  4124. +
  4125. if (thread_info_flags & _TIF_UPROBE)
  4126. uprobe_notify_resume(regs);
  4127. diff -Nur linux-4.1.20.orig/arch/x86/kvm/lapic.c linux-4.1.20/arch/x86/kvm/lapic.c
  4128. --- linux-4.1.20.orig/arch/x86/kvm/lapic.c 2016-03-17 19:11:03.000000000 +0100
  4129. +++ linux-4.1.20/arch/x86/kvm/lapic.c 2016-03-21 20:18:30.000000000 +0100
  4130. @@ -1106,7 +1106,7 @@
  4131. static void apic_timer_expired(struct kvm_lapic *apic)
  4132. {
  4133. struct kvm_vcpu *vcpu = apic->vcpu;
  4134. - wait_queue_head_t *q = &vcpu->wq;
  4135. + struct swait_head *q = &vcpu->wq;
  4136. struct kvm_timer *ktimer = &apic->lapic_timer;
  4137. if (atomic_read(&apic->lapic_timer.pending))
  4138. @@ -1115,8 +1115,8 @@
  4139. atomic_inc(&apic->lapic_timer.pending);
  4140. kvm_set_pending_timer(vcpu);
  4141. - if (waitqueue_active(q))
  4142. - wake_up_interruptible(q);
  4143. + if (swaitqueue_active(q))
  4144. + swait_wake_interruptible(q);
  4145. if (apic_lvtt_tscdeadline(apic))
  4146. ktimer->expired_tscdeadline = ktimer->tscdeadline;
  4147. @@ -1169,8 +1169,36 @@
  4148. __delay(tsc_deadline - guest_tsc);
  4149. }
  4150. +static enum hrtimer_restart apic_timer_fn(struct hrtimer *data);
  4151. +
  4152. +static void __apic_timer_expired(struct hrtimer *data)
  4153. +{
  4154. + int ret, i = 0;
  4155. + enum hrtimer_restart r;
  4156. + struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer);
  4157. +
  4158. + r = apic_timer_fn(data);
  4159. +
  4160. + if (r == HRTIMER_RESTART) {
  4161. + do {
  4162. + ret = hrtimer_start_expires(data, HRTIMER_MODE_ABS);
  4163. + if (ret == -ETIME)
  4164. + hrtimer_add_expires_ns(&ktimer->timer,
  4165. + ktimer->period);
  4166. + i++;
  4167. + } while (ret == -ETIME && i < 10);
  4168. +
  4169. + if (ret == -ETIME) {
  4170. + printk_once(KERN_ERR "%s: failed to reprogram timer\n",
  4171. + __func__);
  4172. + WARN_ON_ONCE(1);
  4173. + }
  4174. + }
  4175. +}
  4176. +
  4177. static void start_apic_timer(struct kvm_lapic *apic)
  4178. {
  4179. + int ret;
  4180. ktime_t now;
  4181. atomic_set(&apic->lapic_timer.pending, 0);
  4182. @@ -1201,9 +1229,11 @@
  4183. }
  4184. }
  4185. - hrtimer_start(&apic->lapic_timer.timer,
  4186. + ret = hrtimer_start(&apic->lapic_timer.timer,
  4187. ktime_add_ns(now, apic->lapic_timer.period),
  4188. HRTIMER_MODE_ABS);
  4189. + if (ret == -ETIME)
  4190. + __apic_timer_expired(&apic->lapic_timer.timer);
  4191. apic_debug("%s: bus cycle is %" PRId64 "ns, now 0x%016"
  4192. PRIx64 ", "
  4193. @@ -1235,8 +1265,10 @@
  4194. do_div(ns, this_tsc_khz);
  4195. expire = ktime_add_ns(now, ns);
  4196. expire = ktime_sub_ns(expire, lapic_timer_advance_ns);
  4197. - hrtimer_start(&apic->lapic_timer.timer,
  4198. + ret = hrtimer_start(&apic->lapic_timer.timer,
  4199. expire, HRTIMER_MODE_ABS);
  4200. + if (ret == -ETIME)
  4201. + __apic_timer_expired(&apic->lapic_timer.timer);
  4202. } else
  4203. apic_timer_expired(apic);
  4204. @@ -1709,6 +1741,7 @@
  4205. hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
  4206. HRTIMER_MODE_ABS);
  4207. apic->lapic_timer.timer.function = apic_timer_fn;
  4208. + apic->lapic_timer.timer.irqsafe = 1;
  4209. /*
  4210. * APIC is created enabled. This will prevent kvm_lapic_set_base from
  4211. @@ -1836,7 +1869,8 @@
  4212. timer = &vcpu->arch.apic->lapic_timer.timer;
  4213. if (hrtimer_cancel(timer))
  4214. - hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
  4215. + if (hrtimer_start_expires(timer, HRTIMER_MODE_ABS) == -ETIME)
  4216. + __apic_timer_expired(timer);
  4217. }
  4218. /*
  4219. diff -Nur linux-4.1.20.orig/arch/x86/kvm/x86.c linux-4.1.20/arch/x86/kvm/x86.c
  4220. --- linux-4.1.20.orig/arch/x86/kvm/x86.c 2016-03-17 19:11:03.000000000 +0100
  4221. +++ linux-4.1.20/arch/x86/kvm/x86.c 2016-03-21 20:18:30.000000000 +0100
  4222. @@ -5809,6 +5809,13 @@
  4223. goto out;
  4224. }
  4225. +#ifdef CONFIG_PREEMPT_RT_FULL
  4226. + if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
  4227. + printk(KERN_ERR "RT requires X86_FEATURE_CONSTANT_TSC\n");
  4228. + return -EOPNOTSUPP;
  4229. + }
  4230. +#endif
  4231. +
  4232. r = kvm_mmu_module_init();
  4233. if (r)
  4234. goto out_free_percpu;
  4235. diff -Nur linux-4.1.20.orig/arch/x86/lib/usercopy_32.c linux-4.1.20/arch/x86/lib/usercopy_32.c
  4236. --- linux-4.1.20.orig/arch/x86/lib/usercopy_32.c 2016-03-17 19:11:03.000000000 +0100
  4237. +++ linux-4.1.20/arch/x86/lib/usercopy_32.c 2016-03-21 20:18:30.000000000 +0100
  4238. @@ -647,7 +647,8 @@
  4239. * @from: Source address, in kernel space.
  4240. * @n: Number of bytes to copy.
  4241. *
  4242. - * Context: User context only. This function may sleep.
  4243. + * Context: User context only. This function may sleep if pagefaults are
  4244. + * enabled.
  4245. *
  4246. * Copy data from kernel space to user space.
  4247. *
  4248. @@ -668,7 +669,8 @@
  4249. * @from: Source address, in user space.
  4250. * @n: Number of bytes to copy.
  4251. *
  4252. - * Context: User context only. This function may sleep.
  4253. + * Context: User context only. This function may sleep if pagefaults are
  4254. + * enabled.
  4255. *
  4256. * Copy data from user space to kernel space.
  4257. *
  4258. diff -Nur linux-4.1.20.orig/arch/x86/mm/fault.c linux-4.1.20/arch/x86/mm/fault.c
  4259. --- linux-4.1.20.orig/arch/x86/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  4260. +++ linux-4.1.20/arch/x86/mm/fault.c 2016-03-21 20:18:30.000000000 +0100
  4261. @@ -13,6 +13,7 @@
  4262. #include <linux/hugetlb.h> /* hstate_index_to_shift */
  4263. #include <linux/prefetch.h> /* prefetchw */
  4264. #include <linux/context_tracking.h> /* exception_enter(), ... */
  4265. +#include <linux/uaccess.h> /* faulthandler_disabled() */
  4266. #include <asm/traps.h> /* dotraplinkage, ... */
  4267. #include <asm/pgalloc.h> /* pgd_*(), ... */
  4268. @@ -1133,9 +1134,9 @@
  4269. /*
  4270. * If we're in an interrupt, have no user context or are running
  4271. - * in an atomic region then we must not take the fault:
  4272. + * in a region with pagefaults disabled then we must not take the fault
  4273. */
  4274. - if (unlikely(in_atomic() || !mm)) {
  4275. + if (unlikely(faulthandler_disabled() || !mm)) {
  4276. bad_area_nosemaphore(regs, error_code, address);
  4277. return;
  4278. }
  4279. diff -Nur linux-4.1.20.orig/arch/x86/mm/highmem_32.c linux-4.1.20/arch/x86/mm/highmem_32.c
  4280. --- linux-4.1.20.orig/arch/x86/mm/highmem_32.c 2016-03-17 19:11:03.000000000 +0100
  4281. +++ linux-4.1.20/arch/x86/mm/highmem_32.c 2016-03-21 20:18:30.000000000 +0100
  4282. @@ -32,10 +32,11 @@
  4283. */
  4284. void *kmap_atomic_prot(struct page *page, pgprot_t prot)
  4285. {
  4286. + pte_t pte = mk_pte(page, prot);
  4287. unsigned long vaddr;
  4288. int idx, type;
  4289. - /* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */
  4290. + preempt_disable_nort();
  4291. pagefault_disable();
  4292. if (!PageHighMem(page))
  4293. @@ -45,7 +46,10 @@
  4294. idx = type + KM_TYPE_NR*smp_processor_id();
  4295. vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
  4296. BUG_ON(!pte_none(*(kmap_pte-idx)));
  4297. - set_pte(kmap_pte-idx, mk_pte(page, prot));
  4298. +#ifdef CONFIG_PREEMPT_RT_FULL
  4299. + current->kmap_pte[type] = pte;
  4300. +#endif
  4301. + set_pte(kmap_pte-idx, pte);
  4302. arch_flush_lazy_mmu_mode();
  4303. return (void *)vaddr;
  4304. @@ -88,6 +92,9 @@
  4305. * is a bad idea also, in case the page changes cacheability
  4306. * attributes or becomes a protected page in a hypervisor.
  4307. */
  4308. +#ifdef CONFIG_PREEMPT_RT_FULL
  4309. + current->kmap_pte[type] = __pte(0);
  4310. +#endif
  4311. kpte_clear_flush(kmap_pte-idx, vaddr);
  4312. kmap_atomic_idx_pop();
  4313. arch_flush_lazy_mmu_mode();
  4314. @@ -100,6 +107,7 @@
  4315. #endif
  4316. pagefault_enable();
  4317. + preempt_enable_nort();
  4318. }
  4319. EXPORT_SYMBOL(__kunmap_atomic);
  4320. diff -Nur linux-4.1.20.orig/arch/x86/mm/iomap_32.c linux-4.1.20/arch/x86/mm/iomap_32.c
  4321. --- linux-4.1.20.orig/arch/x86/mm/iomap_32.c 2016-03-17 19:11:03.000000000 +0100
  4322. +++ linux-4.1.20/arch/x86/mm/iomap_32.c 2016-03-21 20:18:30.000000000 +0100
  4323. @@ -56,15 +56,22 @@
  4324. void *kmap_atomic_prot_pfn(unsigned long pfn, pgprot_t prot)
  4325. {
  4326. + pte_t pte = pfn_pte(pfn, prot);
  4327. unsigned long vaddr;
  4328. int idx, type;
  4329. + preempt_disable();
  4330. pagefault_disable();
  4331. type = kmap_atomic_idx_push();
  4332. idx = type + KM_TYPE_NR * smp_processor_id();
  4333. vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
  4334. - set_pte(kmap_pte - idx, pfn_pte(pfn, prot));
  4335. + WARN_ON(!pte_none(*(kmap_pte - idx)));
  4336. +
  4337. +#ifdef CONFIG_PREEMPT_RT_FULL
  4338. + current->kmap_pte[type] = pte;
  4339. +#endif
  4340. + set_pte(kmap_pte - idx, pte);
  4341. arch_flush_lazy_mmu_mode();
  4342. return (void *)vaddr;
  4343. @@ -112,10 +119,14 @@
  4344. * is a bad idea also, in case the page changes cacheability
  4345. * attributes or becomes a protected page in a hypervisor.
  4346. */
  4347. +#ifdef CONFIG_PREEMPT_RT_FULL
  4348. + current->kmap_pte[type] = __pte(0);
  4349. +#endif
  4350. kpte_clear_flush(kmap_pte-idx, vaddr);
  4351. kmap_atomic_idx_pop();
  4352. }
  4353. pagefault_enable();
  4354. + preempt_enable();
  4355. }
  4356. EXPORT_SYMBOL_GPL(iounmap_atomic);
  4357. 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
  4358. --- linux-4.1.20.orig/arch/x86/platform/uv/tlb_uv.c 2016-03-17 19:11:03.000000000 +0100
  4359. +++ linux-4.1.20/arch/x86/platform/uv/tlb_uv.c 2016-03-21 20:18:30.000000000 +0100
  4360. @@ -714,9 +714,9 @@
  4361. quiesce_local_uvhub(hmaster);
  4362. - spin_lock(&hmaster->queue_lock);
  4363. + raw_spin_lock(&hmaster->queue_lock);
  4364. reset_with_ipi(&bau_desc->distribution, bcp);
  4365. - spin_unlock(&hmaster->queue_lock);
  4366. + raw_spin_unlock(&hmaster->queue_lock);
  4367. end_uvhub_quiesce(hmaster);
  4368. @@ -736,9 +736,9 @@
  4369. quiesce_local_uvhub(hmaster);
  4370. - spin_lock(&hmaster->queue_lock);
  4371. + raw_spin_lock(&hmaster->queue_lock);
  4372. reset_with_ipi(&bau_desc->distribution, bcp);
  4373. - spin_unlock(&hmaster->queue_lock);
  4374. + raw_spin_unlock(&hmaster->queue_lock);
  4375. end_uvhub_quiesce(hmaster);
  4376. @@ -759,7 +759,7 @@
  4377. cycles_t tm1;
  4378. hmaster = bcp->uvhub_master;
  4379. - spin_lock(&hmaster->disable_lock);
  4380. + raw_spin_lock(&hmaster->disable_lock);
  4381. if (!bcp->baudisabled) {
  4382. stat->s_bau_disabled++;
  4383. tm1 = get_cycles();
  4384. @@ -772,7 +772,7 @@
  4385. }
  4386. }
  4387. }
  4388. - spin_unlock(&hmaster->disable_lock);
  4389. + raw_spin_unlock(&hmaster->disable_lock);
  4390. }
  4391. static void count_max_concurr(int stat, struct bau_control *bcp,
  4392. @@ -835,7 +835,7 @@
  4393. */
  4394. static void uv1_throttle(struct bau_control *hmaster, struct ptc_stats *stat)
  4395. {
  4396. - spinlock_t *lock = &hmaster->uvhub_lock;
  4397. + raw_spinlock_t *lock = &hmaster->uvhub_lock;
  4398. atomic_t *v;
  4399. v = &hmaster->active_descriptor_count;
  4400. @@ -968,7 +968,7 @@
  4401. struct bau_control *hmaster;
  4402. hmaster = bcp->uvhub_master;
  4403. - spin_lock(&hmaster->disable_lock);
  4404. + raw_spin_lock(&hmaster->disable_lock);
  4405. if (bcp->baudisabled && (get_cycles() >= bcp->set_bau_on_time)) {
  4406. stat->s_bau_reenabled++;
  4407. for_each_present_cpu(tcpu) {
  4408. @@ -980,10 +980,10 @@
  4409. tbcp->period_giveups = 0;
  4410. }
  4411. }
  4412. - spin_unlock(&hmaster->disable_lock);
  4413. + raw_spin_unlock(&hmaster->disable_lock);
  4414. return 0;
  4415. }
  4416. - spin_unlock(&hmaster->disable_lock);
  4417. + raw_spin_unlock(&hmaster->disable_lock);
  4418. return -1;
  4419. }
  4420. @@ -1901,9 +1901,9 @@
  4421. bcp->cong_reps = congested_reps;
  4422. bcp->disabled_period = sec_2_cycles(disabled_period);
  4423. bcp->giveup_limit = giveup_limit;
  4424. - spin_lock_init(&bcp->queue_lock);
  4425. - spin_lock_init(&bcp->uvhub_lock);
  4426. - spin_lock_init(&bcp->disable_lock);
  4427. + raw_spin_lock_init(&bcp->queue_lock);
  4428. + raw_spin_lock_init(&bcp->uvhub_lock);
  4429. + raw_spin_lock_init(&bcp->disable_lock);
  4430. }
  4431. }
  4432. 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
  4433. --- linux-4.1.20.orig/arch/x86/platform/uv/uv_time.c 2016-03-17 19:11:03.000000000 +0100
  4434. +++ linux-4.1.20/arch/x86/platform/uv/uv_time.c 2016-03-21 20:18:30.000000000 +0100
  4435. @@ -58,7 +58,7 @@
  4436. /* There is one of these allocated per node */
  4437. struct uv_rtc_timer_head {
  4438. - spinlock_t lock;
  4439. + raw_spinlock_t lock;
  4440. /* next cpu waiting for timer, local node relative: */
  4441. int next_cpu;
  4442. /* number of cpus on this node: */
  4443. @@ -178,7 +178,7 @@
  4444. uv_rtc_deallocate_timers();
  4445. return -ENOMEM;
  4446. }
  4447. - spin_lock_init(&head->lock);
  4448. + raw_spin_lock_init(&head->lock);
  4449. head->ncpus = uv_blade_nr_possible_cpus(bid);
  4450. head->next_cpu = -1;
  4451. blade_info[bid] = head;
  4452. @@ -232,7 +232,7 @@
  4453. unsigned long flags;
  4454. int next_cpu;
  4455. - spin_lock_irqsave(&head->lock, flags);
  4456. + raw_spin_lock_irqsave(&head->lock, flags);
  4457. next_cpu = head->next_cpu;
  4458. *t = expires;
  4459. @@ -244,12 +244,12 @@
  4460. if (uv_setup_intr(cpu, expires)) {
  4461. *t = ULLONG_MAX;
  4462. uv_rtc_find_next_timer(head, pnode);
  4463. - spin_unlock_irqrestore(&head->lock, flags);
  4464. + raw_spin_unlock_irqrestore(&head->lock, flags);
  4465. return -ETIME;
  4466. }
  4467. }
  4468. - spin_unlock_irqrestore(&head->lock, flags);
  4469. + raw_spin_unlock_irqrestore(&head->lock, flags);
  4470. return 0;
  4471. }
  4472. @@ -268,7 +268,7 @@
  4473. unsigned long flags;
  4474. int rc = 0;
  4475. - spin_lock_irqsave(&head->lock, flags);
  4476. + raw_spin_lock_irqsave(&head->lock, flags);
  4477. if ((head->next_cpu == bcpu && uv_read_rtc(NULL) >= *t) || force)
  4478. rc = 1;
  4479. @@ -280,7 +280,7 @@
  4480. uv_rtc_find_next_timer(head, pnode);
  4481. }
  4482. - spin_unlock_irqrestore(&head->lock, flags);
  4483. + raw_spin_unlock_irqrestore(&head->lock, flags);
  4484. return rc;
  4485. }
  4486. @@ -300,13 +300,18 @@
  4487. static cycle_t uv_read_rtc(struct clocksource *cs)
  4488. {
  4489. unsigned long offset;
  4490. + cycle_t cycles;
  4491. + preempt_disable();
  4492. if (uv_get_min_hub_revision_id() == 1)
  4493. offset = 0;
  4494. else
  4495. offset = (uv_blade_processor_id() * L1_CACHE_BYTES) % PAGE_SIZE;
  4496. - return (cycle_t)uv_read_local_mmr(UVH_RTC | offset);
  4497. + cycles = (cycle_t)uv_read_local_mmr(UVH_RTC | offset);
  4498. + preempt_enable();
  4499. +
  4500. + return cycles;
  4501. }
  4502. /*
  4503. diff -Nur linux-4.1.20.orig/arch/xtensa/mm/fault.c linux-4.1.20/arch/xtensa/mm/fault.c
  4504. --- linux-4.1.20.orig/arch/xtensa/mm/fault.c 2016-03-17 19:11:03.000000000 +0100
  4505. +++ linux-4.1.20/arch/xtensa/mm/fault.c 2016-03-21 20:18:30.000000000 +0100
  4506. @@ -15,10 +15,10 @@
  4507. #include <linux/mm.h>
  4508. #include <linux/module.h>
  4509. #include <linux/hardirq.h>
  4510. +#include <linux/uaccess.h>
  4511. #include <asm/mmu_context.h>
  4512. #include <asm/cacheflush.h>
  4513. #include <asm/hardirq.h>
  4514. -#include <asm/uaccess.h>
  4515. #include <asm/pgalloc.h>
  4516. DEFINE_PER_CPU(unsigned long, asid_cache) = ASID_USER_FIRST;
  4517. @@ -57,7 +57,7 @@
  4518. /* If we're in an interrupt or have no user
  4519. * context, we must not take the fault..
  4520. */
  4521. - if (in_atomic() || !mm) {
  4522. + if (faulthandler_disabled() || !mm) {
  4523. bad_page_fault(regs, address, SIGSEGV);
  4524. return;
  4525. }
  4526. diff -Nur linux-4.1.20.orig/arch/xtensa/mm/highmem.c linux-4.1.20/arch/xtensa/mm/highmem.c
  4527. --- linux-4.1.20.orig/arch/xtensa/mm/highmem.c 2016-03-17 19:11:03.000000000 +0100
  4528. +++ linux-4.1.20/arch/xtensa/mm/highmem.c 2016-03-21 20:18:30.000000000 +0100
  4529. @@ -42,6 +42,7 @@
  4530. enum fixed_addresses idx;
  4531. unsigned long vaddr;
  4532. + preempt_disable();
  4533. pagefault_disable();
  4534. if (!PageHighMem(page))
  4535. return page_address(page);
  4536. @@ -79,6 +80,7 @@
  4537. }
  4538. pagefault_enable();
  4539. + preempt_enable();
  4540. }
  4541. EXPORT_SYMBOL(__kunmap_atomic);
  4542. diff -Nur linux-4.1.20.orig/block/blk-core.c linux-4.1.20/block/blk-core.c
  4543. --- linux-4.1.20.orig/block/blk-core.c 2016-03-17 19:11:03.000000000 +0100
  4544. +++ linux-4.1.20/block/blk-core.c 2016-03-21 20:18:30.000000000 +0100
  4545. @@ -100,6 +100,9 @@
  4546. INIT_LIST_HEAD(&rq->queuelist);
  4547. INIT_LIST_HEAD(&rq->timeout_list);
  4548. +#ifdef CONFIG_PREEMPT_RT_FULL
  4549. + INIT_WORK(&rq->work, __blk_mq_complete_request_remote_work);
  4550. +#endif
  4551. rq->cpu = -1;
  4552. rq->q = q;
  4553. rq->__sector = (sector_t) -1;
  4554. @@ -194,7 +197,7 @@
  4555. **/
  4556. void blk_start_queue(struct request_queue *q)
  4557. {
  4558. - WARN_ON(!irqs_disabled());
  4559. + WARN_ON_NONRT(!irqs_disabled());
  4560. queue_flag_clear(QUEUE_FLAG_STOPPED, q);
  4561. __blk_run_queue(q);
  4562. @@ -661,7 +664,7 @@
  4563. q->bypass_depth = 1;
  4564. __set_bit(QUEUE_FLAG_BYPASS, &q->queue_flags);
  4565. - init_waitqueue_head(&q->mq_freeze_wq);
  4566. + init_swait_head(&q->mq_freeze_wq);
  4567. if (blkcg_init_queue(q))
  4568. goto fail_bdi;
  4569. @@ -3077,7 +3080,7 @@
  4570. blk_run_queue_async(q);
  4571. else
  4572. __blk_run_queue(q);
  4573. - spin_unlock(q->queue_lock);
  4574. + spin_unlock_irq(q->queue_lock);
  4575. }
  4576. static void flush_plug_callbacks(struct blk_plug *plug, bool from_schedule)
  4577. @@ -3125,7 +3128,6 @@
  4578. void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
  4579. {
  4580. struct request_queue *q;
  4581. - unsigned long flags;
  4582. struct request *rq;
  4583. LIST_HEAD(list);
  4584. unsigned int depth;
  4585. @@ -3145,11 +3147,6 @@
  4586. q = NULL;
  4587. depth = 0;
  4588. - /*
  4589. - * Save and disable interrupts here, to avoid doing it for every
  4590. - * queue lock we have to take.
  4591. - */
  4592. - local_irq_save(flags);
  4593. while (!list_empty(&list)) {
  4594. rq = list_entry_rq(list.next);
  4595. list_del_init(&rq->queuelist);
  4596. @@ -3162,7 +3159,7 @@
  4597. queue_unplugged(q, depth, from_schedule);
  4598. q = rq->q;
  4599. depth = 0;
  4600. - spin_lock(q->queue_lock);
  4601. + spin_lock_irq(q->queue_lock);
  4602. }
  4603. /*
  4604. @@ -3189,8 +3186,6 @@
  4605. */
  4606. if (q)
  4607. queue_unplugged(q, depth, from_schedule);
  4608. -
  4609. - local_irq_restore(flags);
  4610. }
  4611. void blk_finish_plug(struct blk_plug *plug)
  4612. diff -Nur linux-4.1.20.orig/block/blk-ioc.c linux-4.1.20/block/blk-ioc.c
  4613. --- linux-4.1.20.orig/block/blk-ioc.c 2016-03-17 19:11:03.000000000 +0100
  4614. +++ linux-4.1.20/block/blk-ioc.c 2016-03-21 20:18:31.000000000 +0100
  4615. @@ -7,6 +7,7 @@
  4616. #include <linux/bio.h>
  4617. #include <linux/blkdev.h>
  4618. #include <linux/slab.h>
  4619. +#include <linux/delay.h>
  4620. #include "blk.h"
  4621. @@ -109,7 +110,7 @@
  4622. spin_unlock(q->queue_lock);
  4623. } else {
  4624. spin_unlock_irqrestore(&ioc->lock, flags);
  4625. - cpu_relax();
  4626. + cpu_chill();
  4627. spin_lock_irqsave_nested(&ioc->lock, flags, 1);
  4628. }
  4629. }
  4630. @@ -187,7 +188,7 @@
  4631. spin_unlock(icq->q->queue_lock);
  4632. } else {
  4633. spin_unlock_irqrestore(&ioc->lock, flags);
  4634. - cpu_relax();
  4635. + cpu_chill();
  4636. goto retry;
  4637. }
  4638. }
  4639. diff -Nur linux-4.1.20.orig/block/blk-iopoll.c linux-4.1.20/block/blk-iopoll.c
  4640. --- linux-4.1.20.orig/block/blk-iopoll.c 2016-03-17 19:11:03.000000000 +0100
  4641. +++ linux-4.1.20/block/blk-iopoll.c 2016-03-21 20:18:31.000000000 +0100
  4642. @@ -35,6 +35,7 @@
  4643. list_add_tail(&iop->list, this_cpu_ptr(&blk_cpu_iopoll));
  4644. __raise_softirq_irqoff(BLOCK_IOPOLL_SOFTIRQ);
  4645. local_irq_restore(flags);
  4646. + preempt_check_resched_rt();
  4647. }
  4648. EXPORT_SYMBOL(blk_iopoll_sched);
  4649. @@ -132,6 +133,7 @@
  4650. __raise_softirq_irqoff(BLOCK_IOPOLL_SOFTIRQ);
  4651. local_irq_enable();
  4652. + preempt_check_resched_rt();
  4653. }
  4654. /**
  4655. @@ -201,6 +203,7 @@
  4656. this_cpu_ptr(&blk_cpu_iopoll));
  4657. __raise_softirq_irqoff(BLOCK_IOPOLL_SOFTIRQ);
  4658. local_irq_enable();
  4659. + preempt_check_resched_rt();
  4660. }
  4661. return NOTIFY_OK;
  4662. diff -Nur linux-4.1.20.orig/block/blk-mq-cpu.c linux-4.1.20/block/blk-mq-cpu.c
  4663. --- linux-4.1.20.orig/block/blk-mq-cpu.c 2016-03-17 19:11:03.000000000 +0100
  4664. +++ linux-4.1.20/block/blk-mq-cpu.c 2016-03-21 20:18:31.000000000 +0100
  4665. @@ -16,7 +16,7 @@
  4666. #include "blk-mq.h"
  4667. static LIST_HEAD(blk_mq_cpu_notify_list);
  4668. -static DEFINE_RAW_SPINLOCK(blk_mq_cpu_notify_lock);
  4669. +static DEFINE_SPINLOCK(blk_mq_cpu_notify_lock);
  4670. static int blk_mq_main_cpu_notify(struct notifier_block *self,
  4671. unsigned long action, void *hcpu)
  4672. @@ -25,7 +25,10 @@
  4673. struct blk_mq_cpu_notifier *notify;
  4674. int ret = NOTIFY_OK;
  4675. - raw_spin_lock(&blk_mq_cpu_notify_lock);
  4676. + if (action != CPU_POST_DEAD)
  4677. + return NOTIFY_OK;
  4678. +
  4679. + spin_lock(&blk_mq_cpu_notify_lock);
  4680. list_for_each_entry(notify, &blk_mq_cpu_notify_list, list) {
  4681. ret = notify->notify(notify->data, action, cpu);
  4682. @@ -33,7 +36,7 @@
  4683. break;
  4684. }
  4685. - raw_spin_unlock(&blk_mq_cpu_notify_lock);
  4686. + spin_unlock(&blk_mq_cpu_notify_lock);
  4687. return ret;
  4688. }
  4689. @@ -41,16 +44,16 @@
  4690. {
  4691. BUG_ON(!notifier->notify);
  4692. - raw_spin_lock(&blk_mq_cpu_notify_lock);
  4693. + spin_lock(&blk_mq_cpu_notify_lock);
  4694. list_add_tail(&notifier->list, &blk_mq_cpu_notify_list);
  4695. - raw_spin_unlock(&blk_mq_cpu_notify_lock);
  4696. + spin_unlock(&blk_mq_cpu_notify_lock);
  4697. }
  4698. void blk_mq_unregister_cpu_notifier(struct blk_mq_cpu_notifier *notifier)
  4699. {
  4700. - raw_spin_lock(&blk_mq_cpu_notify_lock);
  4701. + spin_lock(&blk_mq_cpu_notify_lock);
  4702. list_del(&notifier->list);
  4703. - raw_spin_unlock(&blk_mq_cpu_notify_lock);
  4704. + spin_unlock(&blk_mq_cpu_notify_lock);
  4705. }
  4706. void blk_mq_init_cpu_notifier(struct blk_mq_cpu_notifier *notifier,
  4707. diff -Nur linux-4.1.20.orig/block/blk-mq.c linux-4.1.20/block/blk-mq.c
  4708. --- linux-4.1.20.orig/block/blk-mq.c 2016-03-17 19:11:03.000000000 +0100
  4709. +++ linux-4.1.20/block/blk-mq.c 2016-03-21 20:18:31.000000000 +0100
  4710. @@ -88,7 +88,7 @@
  4711. if (!(gfp & __GFP_WAIT))
  4712. return -EBUSY;
  4713. - ret = wait_event_interruptible(q->mq_freeze_wq,
  4714. + ret = swait_event_interruptible(q->mq_freeze_wq,
  4715. !q->mq_freeze_depth || blk_queue_dying(q));
  4716. if (blk_queue_dying(q))
  4717. return -ENODEV;
  4718. @@ -107,7 +107,7 @@
  4719. struct request_queue *q =
  4720. container_of(ref, struct request_queue, mq_usage_counter);
  4721. - wake_up_all(&q->mq_freeze_wq);
  4722. + swait_wake_all(&q->mq_freeze_wq);
  4723. }
  4724. void blk_mq_freeze_queue_start(struct request_queue *q)
  4725. @@ -127,7 +127,7 @@
  4726. static void blk_mq_freeze_queue_wait(struct request_queue *q)
  4727. {
  4728. - wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->mq_usage_counter));
  4729. + swait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->mq_usage_counter));
  4730. }
  4731. /*
  4732. @@ -151,7 +151,7 @@
  4733. spin_unlock_irq(q->queue_lock);
  4734. if (wake) {
  4735. percpu_ref_reinit(&q->mq_usage_counter);
  4736. - wake_up_all(&q->mq_freeze_wq);
  4737. + swait_wake_all(&q->mq_freeze_wq);
  4738. }
  4739. }
  4740. EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
  4741. @@ -170,7 +170,7 @@
  4742. * dying, we need to ensure that processes currently waiting on
  4743. * the queue are notified as well.
  4744. */
  4745. - wake_up_all(&q->mq_freeze_wq);
  4746. + swait_wake_all(&q->mq_freeze_wq);
  4747. }
  4748. bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
  4749. @@ -217,6 +217,9 @@
  4750. rq->resid_len = 0;
  4751. rq->sense = NULL;
  4752. +#ifdef CONFIG_PREEMPT_RT_FULL
  4753. + INIT_WORK(&rq->work, __blk_mq_complete_request_remote_work);
  4754. +#endif
  4755. INIT_LIST_HEAD(&rq->timeout_list);
  4756. rq->timeout = 0;
  4757. @@ -346,6 +349,17 @@
  4758. }
  4759. EXPORT_SYMBOL(blk_mq_end_request);
  4760. +#ifdef CONFIG_PREEMPT_RT_FULL
  4761. +
  4762. +void __blk_mq_complete_request_remote_work(struct work_struct *work)
  4763. +{
  4764. + struct request *rq = container_of(work, struct request, work);
  4765. +
  4766. + rq->q->softirq_done_fn(rq);
  4767. +}
  4768. +
  4769. +#else
  4770. +
  4771. static void __blk_mq_complete_request_remote(void *data)
  4772. {
  4773. struct request *rq = data;
  4774. @@ -353,6 +367,8 @@
  4775. rq->q->softirq_done_fn(rq);
  4776. }
  4777. +#endif
  4778. +
  4779. static void blk_mq_ipi_complete_request(struct request *rq)
  4780. {
  4781. struct blk_mq_ctx *ctx = rq->mq_ctx;
  4782. @@ -364,19 +380,23 @@
  4783. return;
  4784. }
  4785. - cpu = get_cpu();
  4786. + cpu = get_cpu_light();
  4787. if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
  4788. shared = cpus_share_cache(cpu, ctx->cpu);
  4789. if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
  4790. +#ifdef CONFIG_PREEMPT_RT_FULL
  4791. + schedule_work_on(ctx->cpu, &rq->work);
  4792. +#else
  4793. rq->csd.func = __blk_mq_complete_request_remote;
  4794. rq->csd.info = rq;
  4795. rq->csd.flags = 0;
  4796. smp_call_function_single_async(ctx->cpu, &rq->csd);
  4797. +#endif
  4798. } else {
  4799. rq->q->softirq_done_fn(rq);
  4800. }
  4801. - put_cpu();
  4802. + put_cpu_light();
  4803. }
  4804. void __blk_mq_complete_request(struct request *rq)
  4805. @@ -905,14 +925,14 @@
  4806. return;
  4807. if (!async) {
  4808. - int cpu = get_cpu();
  4809. + int cpu = get_cpu_light();
  4810. if (cpumask_test_cpu(cpu, hctx->cpumask)) {
  4811. __blk_mq_run_hw_queue(hctx);
  4812. - put_cpu();
  4813. + put_cpu_light();
  4814. return;
  4815. }
  4816. - put_cpu();
  4817. + put_cpu_light();
  4818. }
  4819. kblockd_schedule_delayed_work_on(blk_mq_hctx_next_cpu(hctx),
  4820. @@ -1589,7 +1609,7 @@
  4821. {
  4822. struct blk_mq_hw_ctx *hctx = data;
  4823. - if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
  4824. + if (action == CPU_POST_DEAD)
  4825. return blk_mq_hctx_cpu_offline(hctx, cpu);
  4826. /*
  4827. diff -Nur linux-4.1.20.orig/block/blk-mq.h linux-4.1.20/block/blk-mq.h
  4828. --- linux-4.1.20.orig/block/blk-mq.h 2016-03-17 19:11:03.000000000 +0100
  4829. +++ linux-4.1.20/block/blk-mq.h 2016-03-21 20:18:31.000000000 +0100
  4830. @@ -76,7 +76,10 @@
  4831. static inline struct blk_mq_ctx *__blk_mq_get_ctx(struct request_queue *q,
  4832. unsigned int cpu)
  4833. {
  4834. - return per_cpu_ptr(q->queue_ctx, cpu);
  4835. + struct blk_mq_ctx *ctx;
  4836. +
  4837. + ctx = per_cpu_ptr(q->queue_ctx, cpu);
  4838. + return ctx;
  4839. }
  4840. /*
  4841. @@ -87,12 +90,12 @@
  4842. */
  4843. static inline struct blk_mq_ctx *blk_mq_get_ctx(struct request_queue *q)
  4844. {
  4845. - return __blk_mq_get_ctx(q, get_cpu());
  4846. + return __blk_mq_get_ctx(q, get_cpu_light());
  4847. }
  4848. static inline void blk_mq_put_ctx(struct blk_mq_ctx *ctx)
  4849. {
  4850. - put_cpu();
  4851. + put_cpu_light();
  4852. }
  4853. struct blk_mq_alloc_data {
  4854. diff -Nur linux-4.1.20.orig/block/blk-softirq.c linux-4.1.20/block/blk-softirq.c
  4855. --- linux-4.1.20.orig/block/blk-softirq.c 2016-03-17 19:11:03.000000000 +0100
  4856. +++ linux-4.1.20/block/blk-softirq.c 2016-03-21 20:18:31.000000000 +0100
  4857. @@ -51,6 +51,7 @@
  4858. raise_softirq_irqoff(BLOCK_SOFTIRQ);
  4859. local_irq_restore(flags);
  4860. + preempt_check_resched_rt();
  4861. }
  4862. /*
  4863. @@ -93,6 +94,7 @@
  4864. this_cpu_ptr(&blk_cpu_done));
  4865. raise_softirq_irqoff(BLOCK_SOFTIRQ);
  4866. local_irq_enable();
  4867. + preempt_check_resched_rt();
  4868. }
  4869. return NOTIFY_OK;
  4870. @@ -150,6 +152,7 @@
  4871. goto do_local;
  4872. local_irq_restore(flags);
  4873. + preempt_check_resched_rt();
  4874. }
  4875. /**
  4876. diff -Nur linux-4.1.20.orig/block/bounce.c linux-4.1.20/block/bounce.c
  4877. --- linux-4.1.20.orig/block/bounce.c 2016-03-17 19:11:03.000000000 +0100
  4878. +++ linux-4.1.20/block/bounce.c 2016-03-21 20:18:31.000000000 +0100
  4879. @@ -54,11 +54,11 @@
  4880. unsigned long flags;
  4881. unsigned char *vto;
  4882. - local_irq_save(flags);
  4883. + local_irq_save_nort(flags);
  4884. vto = kmap_atomic(to->bv_page);
  4885. memcpy(vto + to->bv_offset, vfrom, to->bv_len);
  4886. kunmap_atomic(vto);
  4887. - local_irq_restore(flags);
  4888. + local_irq_restore_nort(flags);
  4889. }
  4890. #else /* CONFIG_HIGHMEM */
  4891. diff -Nur linux-4.1.20.orig/crypto/algapi.c linux-4.1.20/crypto/algapi.c
  4892. --- linux-4.1.20.orig/crypto/algapi.c 2016-03-17 19:11:03.000000000 +0100
  4893. +++ linux-4.1.20/crypto/algapi.c 2016-03-21 20:18:31.000000000 +0100
  4894. @@ -695,13 +695,13 @@
  4895. int crypto_register_notifier(struct notifier_block *nb)
  4896. {
  4897. - return blocking_notifier_chain_register(&crypto_chain, nb);
  4898. + return srcu_notifier_chain_register(&crypto_chain, nb);
  4899. }
  4900. EXPORT_SYMBOL_GPL(crypto_register_notifier);
  4901. int crypto_unregister_notifier(struct notifier_block *nb)
  4902. {
  4903. - return blocking_notifier_chain_unregister(&crypto_chain, nb);
  4904. + return srcu_notifier_chain_unregister(&crypto_chain, nb);
  4905. }
  4906. EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
  4907. diff -Nur linux-4.1.20.orig/crypto/api.c linux-4.1.20/crypto/api.c
  4908. --- linux-4.1.20.orig/crypto/api.c 2016-03-17 19:11:03.000000000 +0100
  4909. +++ linux-4.1.20/crypto/api.c 2016-03-21 20:18:31.000000000 +0100
  4910. @@ -31,7 +31,7 @@
  4911. DECLARE_RWSEM(crypto_alg_sem);
  4912. EXPORT_SYMBOL_GPL(crypto_alg_sem);
  4913. -BLOCKING_NOTIFIER_HEAD(crypto_chain);
  4914. +SRCU_NOTIFIER_HEAD(crypto_chain);
  4915. EXPORT_SYMBOL_GPL(crypto_chain);
  4916. static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg);
  4917. @@ -236,10 +236,10 @@
  4918. {
  4919. int ok;
  4920. - ok = blocking_notifier_call_chain(&crypto_chain, val, v);
  4921. + ok = srcu_notifier_call_chain(&crypto_chain, val, v);
  4922. if (ok == NOTIFY_DONE) {
  4923. request_module("cryptomgr");
  4924. - ok = blocking_notifier_call_chain(&crypto_chain, val, v);
  4925. + ok = srcu_notifier_call_chain(&crypto_chain, val, v);
  4926. }
  4927. return ok;
  4928. diff -Nur linux-4.1.20.orig/crypto/internal.h linux-4.1.20/crypto/internal.h
  4929. --- linux-4.1.20.orig/crypto/internal.h 2016-03-17 19:11:03.000000000 +0100
  4930. +++ linux-4.1.20/crypto/internal.h 2016-03-21 20:18:31.000000000 +0100
  4931. @@ -48,7 +48,7 @@
  4932. extern struct list_head crypto_alg_list;
  4933. extern struct rw_semaphore crypto_alg_sem;
  4934. -extern struct blocking_notifier_head crypto_chain;
  4935. +extern struct srcu_notifier_head crypto_chain;
  4936. #ifdef CONFIG_PROC_FS
  4937. void __init crypto_init_proc(void);
  4938. @@ -142,7 +142,7 @@
  4939. static inline void crypto_notify(unsigned long val, void *v)
  4940. {
  4941. - blocking_notifier_call_chain(&crypto_chain, val, v);
  4942. + srcu_notifier_call_chain(&crypto_chain, val, v);
  4943. }
  4944. #endif /* _CRYPTO_INTERNAL_H */
  4945. diff -Nur linux-4.1.20.orig/drivers/acpi/acpica/acglobal.h linux-4.1.20/drivers/acpi/acpica/acglobal.h
  4946. --- linux-4.1.20.orig/drivers/acpi/acpica/acglobal.h 2016-03-17 19:11:03.000000000 +0100
  4947. +++ linux-4.1.20/drivers/acpi/acpica/acglobal.h 2016-03-21 20:18:31.000000000 +0100
  4948. @@ -112,7 +112,7 @@
  4949. * interrupt level
  4950. */
  4951. ACPI_GLOBAL(acpi_spinlock, acpi_gbl_gpe_lock); /* For GPE data structs and registers */
  4952. -ACPI_GLOBAL(acpi_spinlock, acpi_gbl_hardware_lock); /* For ACPI H/W except GPE registers */
  4953. +ACPI_GLOBAL(acpi_raw_spinlock, acpi_gbl_hardware_lock); /* For ACPI H/W except GPE registers */
  4954. ACPI_GLOBAL(acpi_spinlock, acpi_gbl_reference_count_lock);
  4955. /* Mutex for _OSI support */
  4956. diff -Nur linux-4.1.20.orig/drivers/acpi/acpica/hwregs.c linux-4.1.20/drivers/acpi/acpica/hwregs.c
  4957. --- linux-4.1.20.orig/drivers/acpi/acpica/hwregs.c 2016-03-17 19:11:03.000000000 +0100
  4958. +++ linux-4.1.20/drivers/acpi/acpica/hwregs.c 2016-03-21 20:18:31.000000000 +0100
  4959. @@ -269,14 +269,14 @@
  4960. ACPI_BITMASK_ALL_FIXED_STATUS,
  4961. ACPI_FORMAT_UINT64(acpi_gbl_xpm1a_status.address)));
  4962. - lock_flags = acpi_os_acquire_lock(acpi_gbl_hardware_lock);
  4963. + raw_spin_lock_irqsave(acpi_gbl_hardware_lock, lock_flags);
  4964. /* Clear the fixed events in PM1 A/B */
  4965. status = acpi_hw_register_write(ACPI_REGISTER_PM1_STATUS,
  4966. ACPI_BITMASK_ALL_FIXED_STATUS);
  4967. - acpi_os_release_lock(acpi_gbl_hardware_lock, lock_flags);
  4968. + raw_spin_unlock_irqrestore(acpi_gbl_hardware_lock, lock_flags);
  4969. if (ACPI_FAILURE(status)) {
  4970. goto exit;
  4971. diff -Nur linux-4.1.20.orig/drivers/acpi/acpica/hwxface.c linux-4.1.20/drivers/acpi/acpica/hwxface.c
  4972. --- linux-4.1.20.orig/drivers/acpi/acpica/hwxface.c 2016-03-17 19:11:03.000000000 +0100
  4973. +++ linux-4.1.20/drivers/acpi/acpica/hwxface.c 2016-03-21 20:18:31.000000000 +0100
  4974. @@ -374,7 +374,7 @@
  4975. return_ACPI_STATUS(AE_BAD_PARAMETER);
  4976. }
  4977. - lock_flags = acpi_os_acquire_lock(acpi_gbl_hardware_lock);
  4978. + raw_spin_lock_irqsave(acpi_gbl_hardware_lock, lock_flags);
  4979. /*
  4980. * At this point, we know that the parent register is one of the
  4981. @@ -435,7 +435,7 @@
  4982. unlock_and_exit:
  4983. - acpi_os_release_lock(acpi_gbl_hardware_lock, lock_flags);
  4984. + raw_spin_unlock_irqrestore(acpi_gbl_hardware_lock, lock_flags);
  4985. return_ACPI_STATUS(status);
  4986. }
  4987. diff -Nur linux-4.1.20.orig/drivers/acpi/acpica/utmutex.c linux-4.1.20/drivers/acpi/acpica/utmutex.c
  4988. --- linux-4.1.20.orig/drivers/acpi/acpica/utmutex.c 2016-03-17 19:11:03.000000000 +0100
  4989. +++ linux-4.1.20/drivers/acpi/acpica/utmutex.c 2016-03-21 20:18:31.000000000 +0100
  4990. @@ -88,7 +88,7 @@
  4991. return_ACPI_STATUS (status);
  4992. }
  4993. - status = acpi_os_create_lock (&acpi_gbl_hardware_lock);
  4994. + status = acpi_os_create_raw_lock (&acpi_gbl_hardware_lock);
  4995. if (ACPI_FAILURE (status)) {
  4996. return_ACPI_STATUS (status);
  4997. }
  4998. @@ -141,7 +141,7 @@
  4999. /* Delete the spinlocks */
  5000. acpi_os_delete_lock(acpi_gbl_gpe_lock);
  5001. - acpi_os_delete_lock(acpi_gbl_hardware_lock);
  5002. + acpi_os_delete_raw_lock(acpi_gbl_hardware_lock);
  5003. acpi_os_delete_lock(acpi_gbl_reference_count_lock);
  5004. /* Delete the reader/writer lock */
  5005. diff -Nur linux-4.1.20.orig/drivers/ata/libata-sff.c linux-4.1.20/drivers/ata/libata-sff.c
  5006. --- linux-4.1.20.orig/drivers/ata/libata-sff.c 2016-03-17 19:11:03.000000000 +0100
  5007. +++ linux-4.1.20/drivers/ata/libata-sff.c 2016-03-21 20:18:31.000000000 +0100
  5008. @@ -678,9 +678,9 @@
  5009. unsigned long flags;
  5010. unsigned int consumed;
  5011. - local_irq_save(flags);
  5012. + local_irq_save_nort(flags);
  5013. consumed = ata_sff_data_xfer32(dev, buf, buflen, rw);
  5014. - local_irq_restore(flags);
  5015. + local_irq_restore_nort(flags);
  5016. return consumed;
  5017. }
  5018. @@ -719,7 +719,7 @@
  5019. unsigned long flags;
  5020. /* FIXME: use a bounce buffer */
  5021. - local_irq_save(flags);
  5022. + local_irq_save_nort(flags);
  5023. buf = kmap_atomic(page);
  5024. /* do the actual data transfer */
  5025. @@ -727,7 +727,7 @@
  5026. do_write);
  5027. kunmap_atomic(buf);
  5028. - local_irq_restore(flags);
  5029. + local_irq_restore_nort(flags);
  5030. } else {
  5031. buf = page_address(page);
  5032. ap->ops->sff_data_xfer(qc->dev, buf + offset, qc->sect_size,
  5033. @@ -864,7 +864,7 @@
  5034. unsigned long flags;
  5035. /* FIXME: use bounce buffer */
  5036. - local_irq_save(flags);
  5037. + local_irq_save_nort(flags);
  5038. buf = kmap_atomic(page);
  5039. /* do the actual data transfer */
  5040. @@ -872,7 +872,7 @@
  5041. count, rw);
  5042. kunmap_atomic(buf);
  5043. - local_irq_restore(flags);
  5044. + local_irq_restore_nort(flags);
  5045. } else {
  5046. buf = page_address(page);
  5047. consumed = ap->ops->sff_data_xfer(dev, buf + offset,
  5048. diff -Nur linux-4.1.20.orig/drivers/char/random.c linux-4.1.20/drivers/char/random.c
  5049. --- linux-4.1.20.orig/drivers/char/random.c 2016-03-17 19:11:03.000000000 +0100
  5050. +++ linux-4.1.20/drivers/char/random.c 2016-03-21 20:18:31.000000000 +0100
  5051. @@ -776,8 +776,6 @@
  5052. } sample;
  5053. long delta, delta2, delta3;
  5054. - preempt_disable();
  5055. -
  5056. sample.jiffies = jiffies;
  5057. sample.cycles = random_get_entropy();
  5058. sample.num = num;
  5059. @@ -818,7 +816,6 @@
  5060. */
  5061. credit_entropy_bits(r, min_t(int, fls(delta>>1), 11));
  5062. }
  5063. - preempt_enable();
  5064. }
  5065. void add_input_randomness(unsigned int type, unsigned int code,
  5066. @@ -871,28 +868,27 @@
  5067. return *(ptr + f->reg_idx++);
  5068. }
  5069. -void add_interrupt_randomness(int irq, int irq_flags)
  5070. +void add_interrupt_randomness(int irq, int irq_flags, __u64 ip)
  5071. {
  5072. struct entropy_store *r;
  5073. struct fast_pool *fast_pool = this_cpu_ptr(&irq_randomness);
  5074. - struct pt_regs *regs = get_irq_regs();
  5075. unsigned long now = jiffies;
  5076. cycles_t cycles = random_get_entropy();
  5077. __u32 c_high, j_high;
  5078. - __u64 ip;
  5079. unsigned long seed;
  5080. int credit = 0;
  5081. if (cycles == 0)
  5082. - cycles = get_reg(fast_pool, regs);
  5083. + cycles = get_reg(fast_pool, NULL);
  5084. c_high = (sizeof(cycles) > 4) ? cycles >> 32 : 0;
  5085. j_high = (sizeof(now) > 4) ? now >> 32 : 0;
  5086. fast_pool->pool[0] ^= cycles ^ j_high ^ irq;
  5087. fast_pool->pool[1] ^= now ^ c_high;
  5088. - ip = regs ? instruction_pointer(regs) : _RET_IP_;
  5089. + if (!ip)
  5090. + ip = _RET_IP_;
  5091. fast_pool->pool[2] ^= ip;
  5092. fast_pool->pool[3] ^= (sizeof(ip) > 4) ? ip >> 32 :
  5093. - get_reg(fast_pool, regs);
  5094. + get_reg(fast_pool, NULL);
  5095. fast_mix(fast_pool);
  5096. add_interrupt_bench(cycles);
  5097. diff -Nur linux-4.1.20.orig/drivers/clocksource/tcb_clksrc.c linux-4.1.20/drivers/clocksource/tcb_clksrc.c
  5098. --- linux-4.1.20.orig/drivers/clocksource/tcb_clksrc.c 2016-03-17 19:11:03.000000000 +0100
  5099. +++ linux-4.1.20/drivers/clocksource/tcb_clksrc.c 2016-03-21 20:18:31.000000000 +0100
  5100. @@ -23,8 +23,7 @@
  5101. * this 32 bit free-running counter. the second channel is not used.
  5102. *
  5103. * - The third channel may be used to provide a 16-bit clockevent
  5104. - * source, used in either periodic or oneshot mode. This runs
  5105. - * at 32 KiHZ, and can handle delays of up to two seconds.
  5106. + * source, used in either periodic or oneshot mode.
  5107. *
  5108. * A boot clocksource and clockevent source are also currently needed,
  5109. * unless the relevant platforms (ARM/AT91, AVR32/AT32) are changed so
  5110. @@ -74,6 +73,7 @@
  5111. struct tc_clkevt_device {
  5112. struct clock_event_device clkevt;
  5113. struct clk *clk;
  5114. + u32 freq;
  5115. void __iomem *regs;
  5116. };
  5117. @@ -82,13 +82,6 @@
  5118. return container_of(clkevt, struct tc_clkevt_device, clkevt);
  5119. }
  5120. -/* For now, we always use the 32K clock ... this optimizes for NO_HZ,
  5121. - * because using one of the divided clocks would usually mean the
  5122. - * tick rate can never be less than several dozen Hz (vs 0.5 Hz).
  5123. - *
  5124. - * A divided clock could be good for high resolution timers, since
  5125. - * 30.5 usec resolution can seem "low".
  5126. - */
  5127. static u32 timer_clock;
  5128. static void tc_mode(enum clock_event_mode m, struct clock_event_device *d)
  5129. @@ -111,11 +104,12 @@
  5130. case CLOCK_EVT_MODE_PERIODIC:
  5131. clk_enable(tcd->clk);
  5132. - /* slow clock, count up to RC, then irq and restart */
  5133. + /* count up to RC, then irq and restart */
  5134. __raw_writel(timer_clock
  5135. | ATMEL_TC_WAVE | ATMEL_TC_WAVESEL_UP_AUTO,
  5136. regs + ATMEL_TC_REG(2, CMR));
  5137. - __raw_writel((32768 + HZ/2) / HZ, tcaddr + ATMEL_TC_REG(2, RC));
  5138. + __raw_writel((tcd->freq + HZ / 2) / HZ,
  5139. + tcaddr + ATMEL_TC_REG(2, RC));
  5140. /* Enable clock and interrupts on RC compare */
  5141. __raw_writel(ATMEL_TC_CPCS, regs + ATMEL_TC_REG(2, IER));
  5142. @@ -128,7 +122,7 @@
  5143. case CLOCK_EVT_MODE_ONESHOT:
  5144. clk_enable(tcd->clk);
  5145. - /* slow clock, count up to RC, then irq and stop */
  5146. + /* count up to RC, then irq and stop */
  5147. __raw_writel(timer_clock | ATMEL_TC_CPCSTOP
  5148. | ATMEL_TC_WAVE | ATMEL_TC_WAVESEL_UP_AUTO,
  5149. regs + ATMEL_TC_REG(2, CMR));
  5150. @@ -157,8 +151,12 @@
  5151. .name = "tc_clkevt",
  5152. .features = CLOCK_EVT_FEAT_PERIODIC
  5153. | CLOCK_EVT_FEAT_ONESHOT,
  5154. +#ifdef CONFIG_ATMEL_TCB_CLKSRC_USE_SLOW_CLOCK
  5155. /* Should be lower than at91rm9200's system timer */
  5156. .rating = 125,
  5157. +#else
  5158. + .rating = 200,
  5159. +#endif
  5160. .set_next_event = tc_next_event,
  5161. .set_mode = tc_mode,
  5162. },
  5163. @@ -178,8 +176,9 @@
  5164. return IRQ_NONE;
  5165. }
  5166. -static int __init setup_clkevents(struct atmel_tc *tc, int clk32k_divisor_idx)
  5167. +static int __init setup_clkevents(struct atmel_tc *tc, int divisor_idx)
  5168. {
  5169. + unsigned divisor = atmel_tc_divisors[divisor_idx];
  5170. int ret;
  5171. struct clk *t2_clk = tc->clk[2];
  5172. int irq = tc->irq[2];
  5173. @@ -193,7 +192,11 @@
  5174. clkevt.regs = tc->regs;
  5175. clkevt.clk = t2_clk;
  5176. - timer_clock = clk32k_divisor_idx;
  5177. + timer_clock = divisor_idx;
  5178. + if (!divisor)
  5179. + clkevt.freq = 32768;
  5180. + else
  5181. + clkevt.freq = clk_get_rate(t2_clk) / divisor;
  5182. clkevt.clkevt.cpumask = cpumask_of(0);
  5183. @@ -203,7 +206,7 @@
  5184. return ret;
  5185. }
  5186. - clockevents_config_and_register(&clkevt.clkevt, 32768, 1, 0xffff);
  5187. + clockevents_config_and_register(&clkevt.clkevt, clkevt.freq, 1, 0xffff);
  5188. return ret;
  5189. }
  5190. @@ -340,7 +343,11 @@
  5191. goto err_disable_t1;
  5192. /* channel 2: periodic and oneshot timer support */
  5193. +#ifdef CONFIG_ATMEL_TCB_CLKSRC_USE_SLOW_CLOCK
  5194. ret = setup_clkevents(tc, clk32k_divisor_idx);
  5195. +#else
  5196. + ret = setup_clkevents(tc, best_divisor_idx);
  5197. +#endif
  5198. if (ret)
  5199. goto err_unregister_clksrc;
  5200. diff -Nur linux-4.1.20.orig/drivers/clocksource/timer-atmel-pit.c linux-4.1.20/drivers/clocksource/timer-atmel-pit.c
  5201. --- linux-4.1.20.orig/drivers/clocksource/timer-atmel-pit.c 2016-03-17 19:11:03.000000000 +0100
  5202. +++ linux-4.1.20/drivers/clocksource/timer-atmel-pit.c 2016-03-21 20:18:31.000000000 +0100
  5203. @@ -90,6 +90,7 @@
  5204. return elapsed;
  5205. }
  5206. +static struct irqaction at91sam926x_pit_irq;
  5207. /*
  5208. * Clockevent device: interrupts every 1/HZ (== pit_cycles * MCK/16)
  5209. */
  5210. @@ -100,6 +101,8 @@
  5211. switch (mode) {
  5212. case CLOCK_EVT_MODE_PERIODIC:
  5213. + /* Set up irq handler */
  5214. + setup_irq(at91sam926x_pit_irq.irq, &at91sam926x_pit_irq);
  5215. /* update clocksource counter */
  5216. data->cnt += data->cycle * PIT_PICNT(pit_read(data->base, AT91_PIT_PIVR));
  5217. pit_write(data->base, AT91_PIT_MR,
  5218. @@ -113,6 +116,7 @@
  5219. /* disable irq, leaving the clocksource active */
  5220. pit_write(data->base, AT91_PIT_MR,
  5221. (data->cycle - 1) | AT91_PIT_PITEN);
  5222. + remove_irq(at91sam926x_pit_irq.irq, &at91sam926x_pit_irq);
  5223. break;
  5224. case CLOCK_EVT_MODE_RESUME:
  5225. break;
  5226. diff -Nur linux-4.1.20.orig/drivers/clocksource/timer-atmel-st.c linux-4.1.20/drivers/clocksource/timer-atmel-st.c
  5227. --- linux-4.1.20.orig/drivers/clocksource/timer-atmel-st.c 2016-03-17 19:11:03.000000000 +0100
  5228. +++ linux-4.1.20/drivers/clocksource/timer-atmel-st.c 2016-03-21 20:18:31.000000000 +0100
  5229. @@ -131,6 +131,7 @@
  5230. break;
  5231. case CLOCK_EVT_MODE_SHUTDOWN:
  5232. case CLOCK_EVT_MODE_UNUSED:
  5233. + remove_irq(NR_IRQS_LEGACY + AT91_ID_SYS, &at91rm9200_timer_irq);
  5234. case CLOCK_EVT_MODE_RESUME:
  5235. irqmask = 0;
  5236. break;
  5237. diff -Nur linux-4.1.20.orig/drivers/cpufreq/Kconfig.x86 linux-4.1.20/drivers/cpufreq/Kconfig.x86
  5238. --- linux-4.1.20.orig/drivers/cpufreq/Kconfig.x86 2016-03-17 19:11:03.000000000 +0100
  5239. +++ linux-4.1.20/drivers/cpufreq/Kconfig.x86 2016-03-21 20:18:31.000000000 +0100
  5240. @@ -123,7 +123,7 @@
  5241. config X86_POWERNOW_K8
  5242. tristate "AMD Opteron/Athlon64 PowerNow!"
  5243. - depends on ACPI && ACPI_PROCESSOR && X86_ACPI_CPUFREQ
  5244. + depends on ACPI && ACPI_PROCESSOR && X86_ACPI_CPUFREQ && !PREEMPT_RT_BASE
  5245. help
  5246. This adds the CPUFreq driver for K8/early Opteron/Athlon64 processors.
  5247. Support for K10 and newer processors is now in acpi-cpufreq.
  5248. diff -Nur linux-4.1.20.orig/drivers/cpufreq/cpufreq.c linux-4.1.20/drivers/cpufreq/cpufreq.c
  5249. --- linux-4.1.20.orig/drivers/cpufreq/cpufreq.c 2016-03-17 19:11:03.000000000 +0100
  5250. +++ linux-4.1.20/drivers/cpufreq/cpufreq.c 2016-03-21 20:18:31.000000000 +0100
  5251. @@ -64,12 +64,6 @@
  5252. return cpufreq_driver->target_index || cpufreq_driver->target;
  5253. }
  5254. -/*
  5255. - * rwsem to guarantee that cpufreq driver module doesn't unload during critical
  5256. - * sections
  5257. - */
  5258. -static DECLARE_RWSEM(cpufreq_rwsem);
  5259. -
  5260. /* internal prototypes */
  5261. static int __cpufreq_governor(struct cpufreq_policy *policy,
  5262. unsigned int event);
  5263. @@ -215,9 +209,6 @@
  5264. if (cpu >= nr_cpu_ids)
  5265. return NULL;
  5266. - if (!down_read_trylock(&cpufreq_rwsem))
  5267. - return NULL;
  5268. -
  5269. /* get the cpufreq driver */
  5270. read_lock_irqsave(&cpufreq_driver_lock, flags);
  5271. @@ -230,9 +221,6 @@
  5272. read_unlock_irqrestore(&cpufreq_driver_lock, flags);
  5273. - if (!policy)
  5274. - up_read(&cpufreq_rwsem);
  5275. -
  5276. return policy;
  5277. }
  5278. EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
  5279. @@ -240,7 +228,6 @@
  5280. void cpufreq_cpu_put(struct cpufreq_policy *policy)
  5281. {
  5282. kobject_put(&policy->kobj);
  5283. - up_read(&cpufreq_rwsem);
  5284. }
  5285. EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
  5286. @@ -765,9 +752,6 @@
  5287. struct freq_attr *fattr = to_attr(attr);
  5288. ssize_t ret;
  5289. - if (!down_read_trylock(&cpufreq_rwsem))
  5290. - return -EINVAL;
  5291. -
  5292. down_read(&policy->rwsem);
  5293. if (fattr->show)
  5294. @@ -776,7 +760,6 @@
  5295. ret = -EIO;
  5296. up_read(&policy->rwsem);
  5297. - up_read(&cpufreq_rwsem);
  5298. return ret;
  5299. }
  5300. @@ -793,9 +776,6 @@
  5301. if (!cpu_online(policy->cpu))
  5302. goto unlock;
  5303. - if (!down_read_trylock(&cpufreq_rwsem))
  5304. - goto unlock;
  5305. -
  5306. down_write(&policy->rwsem);
  5307. if (fattr->store)
  5308. @@ -804,8 +784,6 @@
  5309. ret = -EIO;
  5310. up_write(&policy->rwsem);
  5311. -
  5312. - up_read(&cpufreq_rwsem);
  5313. unlock:
  5314. put_online_cpus();
  5315. @@ -1117,16 +1095,12 @@
  5316. if (unlikely(policy))
  5317. return 0;
  5318. - if (!down_read_trylock(&cpufreq_rwsem))
  5319. - return 0;
  5320. -
  5321. /* Check if this cpu was hot-unplugged earlier and has siblings */
  5322. read_lock_irqsave(&cpufreq_driver_lock, flags);
  5323. for_each_policy(policy) {
  5324. if (cpumask_test_cpu(cpu, policy->related_cpus)) {
  5325. read_unlock_irqrestore(&cpufreq_driver_lock, flags);
  5326. ret = cpufreq_add_policy_cpu(policy, cpu, dev);
  5327. - up_read(&cpufreq_rwsem);
  5328. return ret;
  5329. }
  5330. }
  5331. @@ -1269,8 +1243,6 @@
  5332. kobject_uevent(&policy->kobj, KOBJ_ADD);
  5333. - up_read(&cpufreq_rwsem);
  5334. -
  5335. /* Callback for handling stuff after policy is ready */
  5336. if (cpufreq_driver->ready)
  5337. cpufreq_driver->ready(policy);
  5338. @@ -1304,8 +1276,6 @@
  5339. cpufreq_policy_free(policy);
  5340. nomem_out:
  5341. - up_read(&cpufreq_rwsem);
  5342. -
  5343. return ret;
  5344. }
  5345. @@ -2499,19 +2469,20 @@
  5346. pr_debug("unregistering driver %s\n", driver->name);
  5347. + /* Protect against concurrent cpu hotplug */
  5348. + get_online_cpus();
  5349. subsys_interface_unregister(&cpufreq_interface);
  5350. if (cpufreq_boost_supported())
  5351. cpufreq_sysfs_remove_file(&boost.attr);
  5352. unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
  5353. - down_write(&cpufreq_rwsem);
  5354. write_lock_irqsave(&cpufreq_driver_lock, flags);
  5355. cpufreq_driver = NULL;
  5356. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  5357. - up_write(&cpufreq_rwsem);
  5358. + put_online_cpus();
  5359. return 0;
  5360. }
  5361. diff -Nur linux-4.1.20.orig/drivers/gpio/Kconfig linux-4.1.20/drivers/gpio/Kconfig
  5362. --- linux-4.1.20.orig/drivers/gpio/Kconfig 2016-03-17 19:11:03.000000000 +0100
  5363. +++ linux-4.1.20/drivers/gpio/Kconfig 2016-03-21 20:18:31.000000000 +0100
  5364. @@ -308,7 +308,7 @@
  5365. family of SOCs.
  5366. config GPIO_OMAP
  5367. - bool "TI OMAP GPIO support" if COMPILE_TEST && !ARCH_OMAP2PLUS
  5368. + tristate "TI OMAP GPIO support" if ARCH_OMAP2PLUS || COMPILE_TEST
  5369. default y if ARCH_OMAP
  5370. depends on ARM
  5371. select GENERIC_IRQ_CHIP
  5372. diff -Nur linux-4.1.20.orig/drivers/gpio/gpio-omap.c linux-4.1.20/drivers/gpio/gpio-omap.c
  5373. --- linux-4.1.20.orig/drivers/gpio/gpio-omap.c 2016-03-17 19:11:03.000000000 +0100
  5374. +++ linux-4.1.20/drivers/gpio/gpio-omap.c 2016-03-21 20:18:31.000000000 +0100
  5375. @@ -29,6 +29,7 @@
  5376. #include <linux/platform_data/gpio-omap.h>
  5377. #define OFF_MODE 1
  5378. +#define OMAP4_GPIO_DEBOUNCINGTIME_MASK 0xFF
  5379. static LIST_HEAD(omap_gpio_list);
  5380. @@ -50,14 +51,15 @@
  5381. struct gpio_bank {
  5382. struct list_head node;
  5383. void __iomem *base;
  5384. - u16 irq;
  5385. + int irq;
  5386. u32 non_wakeup_gpios;
  5387. u32 enabled_non_wakeup_gpios;
  5388. struct gpio_regs context;
  5389. u32 saved_datain;
  5390. u32 level_mask;
  5391. u32 toggle_mask;
  5392. - spinlock_t lock;
  5393. + raw_spinlock_t lock;
  5394. + raw_spinlock_t wa_lock;
  5395. struct gpio_chip chip;
  5396. struct clk *dbck;
  5397. u32 mod_usage;
  5398. @@ -67,7 +69,7 @@
  5399. struct device *dev;
  5400. bool is_mpuio;
  5401. bool dbck_flag;
  5402. - bool loses_context;
  5403. +
  5404. bool context_valid;
  5405. int stride;
  5406. u32 width;
  5407. @@ -175,7 +177,7 @@
  5408. static inline void omap_gpio_dbck_enable(struct gpio_bank *bank)
  5409. {
  5410. if (bank->dbck_enable_mask && !bank->dbck_enabled) {
  5411. - clk_prepare_enable(bank->dbck);
  5412. + clk_enable(bank->dbck);
  5413. bank->dbck_enabled = true;
  5414. writel_relaxed(bank->dbck_enable_mask,
  5415. @@ -193,7 +195,7 @@
  5416. */
  5417. writel_relaxed(0, bank->base + bank->regs->debounce_en);
  5418. - clk_disable_unprepare(bank->dbck);
  5419. + clk_disable(bank->dbck);
  5420. bank->dbck_enabled = false;
  5421. }
  5422. }
  5423. @@ -204,8 +206,9 @@
  5424. * @offset: the gpio number on this @bank
  5425. * @debounce: debounce time to use
  5426. *
  5427. - * OMAP's debounce time is in 31us steps so we need
  5428. - * to convert and round up to the closest unit.
  5429. + * OMAP's debounce time is in 31us steps
  5430. + * <debounce time> = (GPIO_DEBOUNCINGTIME[7:0].DEBOUNCETIME + 1) x 31
  5431. + * so we need to convert and round up to the closest unit.
  5432. */
  5433. static void omap2_set_gpio_debounce(struct gpio_bank *bank, unsigned offset,
  5434. unsigned debounce)
  5435. @@ -213,34 +216,33 @@
  5436. void __iomem *reg;
  5437. u32 val;
  5438. u32 l;
  5439. + bool enable = !!debounce;
  5440. if (!bank->dbck_flag)
  5441. return;
  5442. - if (debounce < 32)
  5443. - debounce = 0x01;
  5444. - else if (debounce > 7936)
  5445. - debounce = 0xff;
  5446. - else
  5447. - debounce = (debounce / 0x1f) - 1;
  5448. + if (enable) {
  5449. + debounce = DIV_ROUND_UP(debounce, 31) - 1;
  5450. + debounce &= OMAP4_GPIO_DEBOUNCINGTIME_MASK;
  5451. + }
  5452. l = BIT(offset);
  5453. - clk_prepare_enable(bank->dbck);
  5454. + clk_enable(bank->dbck);
  5455. reg = bank->base + bank->regs->debounce;
  5456. writel_relaxed(debounce, reg);
  5457. reg = bank->base + bank->regs->debounce_en;
  5458. val = readl_relaxed(reg);
  5459. - if (debounce)
  5460. + if (enable)
  5461. val |= l;
  5462. else
  5463. val &= ~l;
  5464. bank->dbck_enable_mask = val;
  5465. writel_relaxed(val, reg);
  5466. - clk_disable_unprepare(bank->dbck);
  5467. + clk_disable(bank->dbck);
  5468. /*
  5469. * Enable debounce clock per module.
  5470. * This call is mandatory because in omap_gpio_request() when
  5471. @@ -285,7 +287,7 @@
  5472. bank->context.debounce = 0;
  5473. writel_relaxed(bank->context.debounce, bank->base +
  5474. bank->regs->debounce);
  5475. - clk_disable_unprepare(bank->dbck);
  5476. + clk_disable(bank->dbck);
  5477. bank->dbck_enabled = false;
  5478. }
  5479. }
  5480. @@ -488,9 +490,6 @@
  5481. unsigned long flags;
  5482. unsigned offset = d->hwirq;
  5483. - if (!BANK_USED(bank))
  5484. - pm_runtime_get_sync(bank->dev);
  5485. -
  5486. if (type & ~IRQ_TYPE_SENSE_MASK)
  5487. return -EINVAL;
  5488. @@ -498,20 +497,28 @@
  5489. (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
  5490. return -EINVAL;
  5491. - spin_lock_irqsave(&bank->lock, flags);
  5492. + raw_spin_lock_irqsave(&bank->lock, flags);
  5493. retval = omap_set_gpio_triggering(bank, offset, type);
  5494. + if (retval) {
  5495. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5496. + goto error;
  5497. + }
  5498. omap_gpio_init_irq(bank, offset);
  5499. if (!omap_gpio_is_input(bank, offset)) {
  5500. - spin_unlock_irqrestore(&bank->lock, flags);
  5501. - return -EINVAL;
  5502. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5503. + retval = -EINVAL;
  5504. + goto error;
  5505. }
  5506. - spin_unlock_irqrestore(&bank->lock, flags);
  5507. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5508. if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
  5509. __irq_set_handler_locked(d->irq, handle_level_irq);
  5510. else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
  5511. __irq_set_handler_locked(d->irq, handle_edge_irq);
  5512. + return 0;
  5513. +
  5514. +error:
  5515. return retval;
  5516. }
  5517. @@ -626,34 +633,30 @@
  5518. return -EINVAL;
  5519. }
  5520. - spin_lock_irqsave(&bank->lock, flags);
  5521. + raw_spin_lock_irqsave(&bank->lock, flags);
  5522. if (enable)
  5523. bank->context.wake_en |= gpio_bit;
  5524. else
  5525. bank->context.wake_en &= ~gpio_bit;
  5526. writel_relaxed(bank->context.wake_en, bank->base + bank->regs->wkup_en);
  5527. - spin_unlock_irqrestore(&bank->lock, flags);
  5528. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5529. return 0;
  5530. }
  5531. -static void omap_reset_gpio(struct gpio_bank *bank, unsigned offset)
  5532. -{
  5533. - omap_set_gpio_direction(bank, offset, 1);
  5534. - omap_set_gpio_irqenable(bank, offset, 0);
  5535. - omap_clear_gpio_irqstatus(bank, offset);
  5536. - omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
  5537. - omap_clear_gpio_debounce(bank, offset);
  5538. -}
  5539. -
  5540. /* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
  5541. static int omap_gpio_wake_enable(struct irq_data *d, unsigned int enable)
  5542. {
  5543. struct gpio_bank *bank = omap_irq_data_get_bank(d);
  5544. unsigned offset = d->hwirq;
  5545. + int ret;
  5546. +
  5547. + ret = omap_set_gpio_wakeup(bank, offset, enable);
  5548. + if (!ret)
  5549. + ret = irq_set_irq_wake(bank->irq, enable);
  5550. - return omap_set_gpio_wakeup(bank, offset, enable);
  5551. + return ret;
  5552. }
  5553. static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
  5554. @@ -668,17 +671,10 @@
  5555. if (!BANK_USED(bank))
  5556. pm_runtime_get_sync(bank->dev);
  5557. - spin_lock_irqsave(&bank->lock, flags);
  5558. - /* Set trigger to none. You need to enable the desired trigger with
  5559. - * request_irq() or set_irq_type(). Only do this if the IRQ line has
  5560. - * not already been requested.
  5561. - */
  5562. - if (!LINE_USED(bank->irq_usage, offset)) {
  5563. - omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
  5564. - omap_enable_gpio_module(bank, offset);
  5565. - }
  5566. + raw_spin_lock_irqsave(&bank->lock, flags);
  5567. + omap_enable_gpio_module(bank, offset);
  5568. bank->mod_usage |= BIT(offset);
  5569. - spin_unlock_irqrestore(&bank->lock, flags);
  5570. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5571. return 0;
  5572. }
  5573. @@ -688,11 +684,14 @@
  5574. struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
  5575. unsigned long flags;
  5576. - spin_lock_irqsave(&bank->lock, flags);
  5577. + raw_spin_lock_irqsave(&bank->lock, flags);
  5578. bank->mod_usage &= ~(BIT(offset));
  5579. + if (!LINE_USED(bank->irq_usage, offset)) {
  5580. + omap_set_gpio_direction(bank, offset, 1);
  5581. + omap_clear_gpio_debounce(bank, offset);
  5582. + }
  5583. omap_disable_gpio_module(bank, offset);
  5584. - omap_reset_gpio(bank, offset);
  5585. - spin_unlock_irqrestore(&bank->lock, flags);
  5586. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5587. /*
  5588. * If this is the last gpio to be freed in the bank,
  5589. @@ -711,29 +710,27 @@
  5590. * line's interrupt handler has been run, we may miss some nested
  5591. * interrupts.
  5592. */
  5593. -static void omap_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
  5594. +static irqreturn_t omap_gpio_irq_handler(int irq, void *gpiobank)
  5595. {
  5596. void __iomem *isr_reg = NULL;
  5597. u32 isr;
  5598. unsigned int bit;
  5599. - struct gpio_bank *bank;
  5600. - int unmasked = 0;
  5601. - struct irq_chip *irqchip = irq_desc_get_chip(desc);
  5602. - struct gpio_chip *chip = irq_get_handler_data(irq);
  5603. + struct gpio_bank *bank = gpiobank;
  5604. + unsigned long wa_lock_flags;
  5605. + unsigned long lock_flags;
  5606. - chained_irq_enter(irqchip, desc);
  5607. -
  5608. - bank = container_of(chip, struct gpio_bank, chip);
  5609. isr_reg = bank->base + bank->regs->irqstatus;
  5610. - pm_runtime_get_sync(bank->dev);
  5611. -
  5612. if (WARN_ON(!isr_reg))
  5613. goto exit;
  5614. + pm_runtime_get_sync(bank->dev);
  5615. +
  5616. while (1) {
  5617. u32 isr_saved, level_mask = 0;
  5618. u32 enabled;
  5619. + raw_spin_lock_irqsave(&bank->lock, lock_flags);
  5620. +
  5621. enabled = omap_get_gpio_irqbank_mask(bank);
  5622. isr_saved = isr = readl_relaxed(isr_reg) & enabled;
  5623. @@ -747,12 +744,7 @@
  5624. omap_clear_gpio_irqbank(bank, isr_saved & ~level_mask);
  5625. omap_enable_gpio_irqbank(bank, isr_saved & ~level_mask);
  5626. - /* if there is only edge sensitive GPIO pin interrupts
  5627. - configured, we could unmask GPIO bank interrupt immediately */
  5628. - if (!level_mask && !unmasked) {
  5629. - unmasked = 1;
  5630. - chained_irq_exit(irqchip, desc);
  5631. - }
  5632. + raw_spin_unlock_irqrestore(&bank->lock, lock_flags);
  5633. if (!isr)
  5634. break;
  5635. @@ -761,6 +753,7 @@
  5636. bit = __ffs(isr);
  5637. isr &= ~(BIT(bit));
  5638. + raw_spin_lock_irqsave(&bank->lock, lock_flags);
  5639. /*
  5640. * Some chips can't respond to both rising and falling
  5641. * at the same time. If this irq was requested with
  5642. @@ -771,18 +764,20 @@
  5643. if (bank->toggle_mask & (BIT(bit)))
  5644. omap_toggle_gpio_edge_triggering(bank, bit);
  5645. + raw_spin_unlock_irqrestore(&bank->lock, lock_flags);
  5646. +
  5647. + raw_spin_lock_irqsave(&bank->wa_lock, wa_lock_flags);
  5648. +
  5649. generic_handle_irq(irq_find_mapping(bank->chip.irqdomain,
  5650. bit));
  5651. +
  5652. + raw_spin_unlock_irqrestore(&bank->wa_lock,
  5653. + wa_lock_flags);
  5654. }
  5655. }
  5656. - /* if bank has any level sensitive GPIO pin interrupt
  5657. - configured, we must unmask the bank interrupt only after
  5658. - handler(s) are executed in order to avoid spurious bank
  5659. - interrupt */
  5660. exit:
  5661. - if (!unmasked)
  5662. - chained_irq_exit(irqchip, desc);
  5663. pm_runtime_put(bank->dev);
  5664. + return IRQ_HANDLED;
  5665. }
  5666. static unsigned int omap_gpio_irq_startup(struct irq_data *d)
  5667. @@ -791,15 +786,22 @@
  5668. unsigned long flags;
  5669. unsigned offset = d->hwirq;
  5670. - if (!BANK_USED(bank))
  5671. - pm_runtime_get_sync(bank->dev);
  5672. + raw_spin_lock_irqsave(&bank->lock, flags);
  5673. - spin_lock_irqsave(&bank->lock, flags);
  5674. - omap_gpio_init_irq(bank, offset);
  5675. - spin_unlock_irqrestore(&bank->lock, flags);
  5676. + if (!LINE_USED(bank->mod_usage, offset))
  5677. + omap_set_gpio_direction(bank, offset, 1);
  5678. + else if (!omap_gpio_is_input(bank, offset))
  5679. + goto err;
  5680. + omap_enable_gpio_module(bank, offset);
  5681. + bank->irq_usage |= BIT(offset);
  5682. +
  5683. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5684. omap_gpio_unmask_irq(d);
  5685. return 0;
  5686. +err:
  5687. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5688. + return -EINVAL;
  5689. }
  5690. static void omap_gpio_irq_shutdown(struct irq_data *d)
  5691. @@ -808,11 +810,28 @@
  5692. unsigned long flags;
  5693. unsigned offset = d->hwirq;
  5694. - spin_lock_irqsave(&bank->lock, flags);
  5695. + raw_spin_lock_irqsave(&bank->lock, flags);
  5696. bank->irq_usage &= ~(BIT(offset));
  5697. + omap_set_gpio_irqenable(bank, offset, 0);
  5698. + omap_clear_gpio_irqstatus(bank, offset);
  5699. + omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
  5700. + if (!LINE_USED(bank->mod_usage, offset))
  5701. + omap_clear_gpio_debounce(bank, offset);
  5702. omap_disable_gpio_module(bank, offset);
  5703. - omap_reset_gpio(bank, offset);
  5704. - spin_unlock_irqrestore(&bank->lock, flags);
  5705. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5706. +}
  5707. +
  5708. +static void omap_gpio_irq_bus_lock(struct irq_data *data)
  5709. +{
  5710. + struct gpio_bank *bank = omap_irq_data_get_bank(data);
  5711. +
  5712. + if (!BANK_USED(bank))
  5713. + pm_runtime_get_sync(bank->dev);
  5714. +}
  5715. +
  5716. +static void gpio_irq_bus_sync_unlock(struct irq_data *data)
  5717. +{
  5718. + struct gpio_bank *bank = omap_irq_data_get_bank(data);
  5719. /*
  5720. * If this is the last IRQ to be freed in the bank,
  5721. @@ -836,10 +855,10 @@
  5722. unsigned offset = d->hwirq;
  5723. unsigned long flags;
  5724. - spin_lock_irqsave(&bank->lock, flags);
  5725. + raw_spin_lock_irqsave(&bank->lock, flags);
  5726. omap_set_gpio_irqenable(bank, offset, 0);
  5727. omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
  5728. - spin_unlock_irqrestore(&bank->lock, flags);
  5729. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5730. }
  5731. static void omap_gpio_unmask_irq(struct irq_data *d)
  5732. @@ -849,7 +868,7 @@
  5733. u32 trigger = irqd_get_trigger_type(d);
  5734. unsigned long flags;
  5735. - spin_lock_irqsave(&bank->lock, flags);
  5736. + raw_spin_lock_irqsave(&bank->lock, flags);
  5737. if (trigger)
  5738. omap_set_gpio_triggering(bank, offset, trigger);
  5739. @@ -861,7 +880,7 @@
  5740. }
  5741. omap_set_gpio_irqenable(bank, offset, 1);
  5742. - spin_unlock_irqrestore(&bank->lock, flags);
  5743. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5744. }
  5745. /*---------------------------------------------------------------------*/
  5746. @@ -874,9 +893,9 @@
  5747. OMAP_MPUIO_GPIO_MASKIT / bank->stride;
  5748. unsigned long flags;
  5749. - spin_lock_irqsave(&bank->lock, flags);
  5750. + raw_spin_lock_irqsave(&bank->lock, flags);
  5751. writel_relaxed(0xffff & ~bank->context.wake_en, mask_reg);
  5752. - spin_unlock_irqrestore(&bank->lock, flags);
  5753. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5754. return 0;
  5755. }
  5756. @@ -889,9 +908,9 @@
  5757. OMAP_MPUIO_GPIO_MASKIT / bank->stride;
  5758. unsigned long flags;
  5759. - spin_lock_irqsave(&bank->lock, flags);
  5760. + raw_spin_lock_irqsave(&bank->lock, flags);
  5761. writel_relaxed(bank->context.wake_en, mask_reg);
  5762. - spin_unlock_irqrestore(&bank->lock, flags);
  5763. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5764. return 0;
  5765. }
  5766. @@ -937,9 +956,9 @@
  5767. bank = container_of(chip, struct gpio_bank, chip);
  5768. reg = bank->base + bank->regs->direction;
  5769. - spin_lock_irqsave(&bank->lock, flags);
  5770. + raw_spin_lock_irqsave(&bank->lock, flags);
  5771. dir = !!(readl_relaxed(reg) & BIT(offset));
  5772. - spin_unlock_irqrestore(&bank->lock, flags);
  5773. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5774. return dir;
  5775. }
  5776. @@ -949,9 +968,9 @@
  5777. unsigned long flags;
  5778. bank = container_of(chip, struct gpio_bank, chip);
  5779. - spin_lock_irqsave(&bank->lock, flags);
  5780. + raw_spin_lock_irqsave(&bank->lock, flags);
  5781. omap_set_gpio_direction(bank, offset, 1);
  5782. - spin_unlock_irqrestore(&bank->lock, flags);
  5783. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5784. return 0;
  5785. }
  5786. @@ -973,10 +992,10 @@
  5787. unsigned long flags;
  5788. bank = container_of(chip, struct gpio_bank, chip);
  5789. - spin_lock_irqsave(&bank->lock, flags);
  5790. + raw_spin_lock_irqsave(&bank->lock, flags);
  5791. bank->set_dataout(bank, offset, value);
  5792. omap_set_gpio_direction(bank, offset, 0);
  5793. - spin_unlock_irqrestore(&bank->lock, flags);
  5794. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5795. return 0;
  5796. }
  5797. @@ -988,9 +1007,9 @@
  5798. bank = container_of(chip, struct gpio_bank, chip);
  5799. - spin_lock_irqsave(&bank->lock, flags);
  5800. + raw_spin_lock_irqsave(&bank->lock, flags);
  5801. omap2_set_gpio_debounce(bank, offset, debounce);
  5802. - spin_unlock_irqrestore(&bank->lock, flags);
  5803. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5804. return 0;
  5805. }
  5806. @@ -1001,9 +1020,9 @@
  5807. unsigned long flags;
  5808. bank = container_of(chip, struct gpio_bank, chip);
  5809. - spin_lock_irqsave(&bank->lock, flags);
  5810. + raw_spin_lock_irqsave(&bank->lock, flags);
  5811. bank->set_dataout(bank, offset, value);
  5812. - spin_unlock_irqrestore(&bank->lock, flags);
  5813. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5814. }
  5815. /*---------------------------------------------------------------------*/
  5816. @@ -1048,10 +1067,6 @@
  5817. /* Initialize interface clk ungated, module enabled */
  5818. if (bank->regs->ctrl)
  5819. writel_relaxed(0, base + bank->regs->ctrl);
  5820. -
  5821. - bank->dbck = clk_get(bank->dev, "dbclk");
  5822. - if (IS_ERR(bank->dbck))
  5823. - dev_err(bank->dev, "Could not get gpio dbck\n");
  5824. }
  5825. static int omap_gpio_chip_init(struct gpio_bank *bank, struct irq_chip *irqc)
  5826. @@ -1080,7 +1095,6 @@
  5827. } else {
  5828. bank->chip.label = "gpio";
  5829. bank->chip.base = gpio;
  5830. - gpio += bank->width;
  5831. }
  5832. bank->chip.ngpio = bank->width;
  5833. @@ -1090,6 +1104,9 @@
  5834. return ret;
  5835. }
  5836. + if (!bank->is_mpuio)
  5837. + gpio += bank->width;
  5838. +
  5839. #ifdef CONFIG_ARCH_OMAP1
  5840. /*
  5841. * REVISIT: Once we have OMAP1 supporting SPARSE_IRQ, we can drop
  5842. @@ -1112,7 +1129,7 @@
  5843. }
  5844. ret = gpiochip_irqchip_add(&bank->chip, irqc,
  5845. - irq_base, omap_gpio_irq_handler,
  5846. + irq_base, handle_bad_irq,
  5847. IRQ_TYPE_NONE);
  5848. if (ret) {
  5849. @@ -1121,10 +1138,14 @@
  5850. return -ENODEV;
  5851. }
  5852. - gpiochip_set_chained_irqchip(&bank->chip, irqc,
  5853. - bank->irq, omap_gpio_irq_handler);
  5854. + gpiochip_set_chained_irqchip(&bank->chip, irqc, bank->irq, NULL);
  5855. - return 0;
  5856. + ret = devm_request_irq(bank->dev, bank->irq, omap_gpio_irq_handler,
  5857. + 0, dev_name(bank->dev), bank);
  5858. + if (ret)
  5859. + gpiochip_remove(&bank->chip);
  5860. +
  5861. + return ret;
  5862. }
  5863. static const struct of_device_id omap_gpio_match[];
  5864. @@ -1163,17 +1184,23 @@
  5865. irqc->irq_unmask = omap_gpio_unmask_irq,
  5866. irqc->irq_set_type = omap_gpio_irq_type,
  5867. irqc->irq_set_wake = omap_gpio_wake_enable,
  5868. + irqc->irq_bus_lock = omap_gpio_irq_bus_lock,
  5869. + irqc->irq_bus_sync_unlock = gpio_irq_bus_sync_unlock,
  5870. irqc->name = dev_name(&pdev->dev);
  5871. - res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  5872. - if (unlikely(!res)) {
  5873. - dev_err(dev, "Invalid IRQ resource\n");
  5874. - return -ENODEV;
  5875. + bank->irq = platform_get_irq(pdev, 0);
  5876. + if (bank->irq <= 0) {
  5877. + if (!bank->irq)
  5878. + bank->irq = -ENXIO;
  5879. + if (bank->irq != -EPROBE_DEFER)
  5880. + dev_err(dev,
  5881. + "can't get irq resource ret=%d\n", bank->irq);
  5882. + return bank->irq;
  5883. }
  5884. - bank->irq = res->start;
  5885. bank->dev = dev;
  5886. bank->chip.dev = dev;
  5887. + bank->chip.owner = THIS_MODULE;
  5888. bank->dbck_flag = pdata->dbck_flag;
  5889. bank->stride = pdata->bank_stride;
  5890. bank->width = pdata->bank_width;
  5891. @@ -1183,15 +1210,9 @@
  5892. #ifdef CONFIG_OF_GPIO
  5893. bank->chip.of_node = of_node_get(node);
  5894. #endif
  5895. - if (node) {
  5896. - if (!of_property_read_bool(node, "ti,gpio-always-on"))
  5897. - bank->loses_context = true;
  5898. - } else {
  5899. - bank->loses_context = pdata->loses_context;
  5900. -
  5901. - if (bank->loses_context)
  5902. - bank->get_context_loss_count =
  5903. - pdata->get_context_loss_count;
  5904. + if (!node) {
  5905. + bank->get_context_loss_count =
  5906. + pdata->get_context_loss_count;
  5907. }
  5908. if (bank->regs->set_dataout && bank->regs->clr_dataout)
  5909. @@ -1199,16 +1220,27 @@
  5910. else
  5911. bank->set_dataout = omap_set_gpio_dataout_mask;
  5912. - spin_lock_init(&bank->lock);
  5913. + raw_spin_lock_init(&bank->lock);
  5914. + raw_spin_lock_init(&bank->wa_lock);
  5915. /* Static mapping, never released */
  5916. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  5917. bank->base = devm_ioremap_resource(dev, res);
  5918. if (IS_ERR(bank->base)) {
  5919. - irq_domain_remove(bank->chip.irqdomain);
  5920. return PTR_ERR(bank->base);
  5921. }
  5922. + if (bank->dbck_flag) {
  5923. + bank->dbck = devm_clk_get(bank->dev, "dbclk");
  5924. + if (IS_ERR(bank->dbck)) {
  5925. + dev_err(bank->dev,
  5926. + "Could not get gpio dbck. Disable debounce\n");
  5927. + bank->dbck_flag = false;
  5928. + } else {
  5929. + clk_prepare(bank->dbck);
  5930. + }
  5931. + }
  5932. +
  5933. platform_set_drvdata(pdev, bank);
  5934. pm_runtime_enable(bank->dev);
  5935. @@ -1221,8 +1253,11 @@
  5936. omap_gpio_mod_init(bank);
  5937. ret = omap_gpio_chip_init(bank, irqc);
  5938. - if (ret)
  5939. + if (ret) {
  5940. + pm_runtime_put_sync(bank->dev);
  5941. + pm_runtime_disable(bank->dev);
  5942. return ret;
  5943. + }
  5944. omap_gpio_show_rev(bank);
  5945. @@ -1233,6 +1268,19 @@
  5946. return 0;
  5947. }
  5948. +static int omap_gpio_remove(struct platform_device *pdev)
  5949. +{
  5950. + struct gpio_bank *bank = platform_get_drvdata(pdev);
  5951. +
  5952. + list_del(&bank->node);
  5953. + gpiochip_remove(&bank->chip);
  5954. + pm_runtime_disable(bank->dev);
  5955. + if (bank->dbck_flag)
  5956. + clk_unprepare(bank->dbck);
  5957. +
  5958. + return 0;
  5959. +}
  5960. +
  5961. #ifdef CONFIG_ARCH_OMAP2PLUS
  5962. #if defined(CONFIG_PM)
  5963. @@ -1246,7 +1294,7 @@
  5964. unsigned long flags;
  5965. u32 wake_low, wake_hi;
  5966. - spin_lock_irqsave(&bank->lock, flags);
  5967. + raw_spin_lock_irqsave(&bank->lock, flags);
  5968. /*
  5969. * Only edges can generate a wakeup event to the PRCM.
  5970. @@ -1299,7 +1347,7 @@
  5971. bank->get_context_loss_count(bank->dev);
  5972. omap_gpio_dbck_disable(bank);
  5973. - spin_unlock_irqrestore(&bank->lock, flags);
  5974. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  5975. return 0;
  5976. }
  5977. @@ -1314,14 +1362,14 @@
  5978. unsigned long flags;
  5979. int c;
  5980. - spin_lock_irqsave(&bank->lock, flags);
  5981. + raw_spin_lock_irqsave(&bank->lock, flags);
  5982. /*
  5983. * On the first resume during the probe, the context has not
  5984. * been initialised and so initialise it now. Also initialise
  5985. * the context loss count.
  5986. */
  5987. - if (bank->loses_context && !bank->context_valid) {
  5988. + if (!bank->context_valid) {
  5989. omap_gpio_init_context(bank);
  5990. if (bank->get_context_loss_count)
  5991. @@ -1342,22 +1390,20 @@
  5992. writel_relaxed(bank->context.risingdetect,
  5993. bank->base + bank->regs->risingdetect);
  5994. - if (bank->loses_context) {
  5995. - if (!bank->get_context_loss_count) {
  5996. + if (!bank->get_context_loss_count) {
  5997. + omap_gpio_restore_context(bank);
  5998. + } else {
  5999. + c = bank->get_context_loss_count(bank->dev);
  6000. + if (c != bank->context_loss_count) {
  6001. omap_gpio_restore_context(bank);
  6002. } else {
  6003. - c = bank->get_context_loss_count(bank->dev);
  6004. - if (c != bank->context_loss_count) {
  6005. - omap_gpio_restore_context(bank);
  6006. - } else {
  6007. - spin_unlock_irqrestore(&bank->lock, flags);
  6008. - return 0;
  6009. - }
  6010. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  6011. + return 0;
  6012. }
  6013. }
  6014. if (!bank->workaround_enabled) {
  6015. - spin_unlock_irqrestore(&bank->lock, flags);
  6016. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  6017. return 0;
  6018. }
  6019. @@ -1412,18 +1458,19 @@
  6020. }
  6021. bank->workaround_enabled = false;
  6022. - spin_unlock_irqrestore(&bank->lock, flags);
  6023. + raw_spin_unlock_irqrestore(&bank->lock, flags);
  6024. return 0;
  6025. }
  6026. #endif /* CONFIG_PM */
  6027. +#if IS_BUILTIN(CONFIG_GPIO_OMAP)
  6028. void omap2_gpio_prepare_for_idle(int pwr_mode)
  6029. {
  6030. struct gpio_bank *bank;
  6031. list_for_each_entry(bank, &omap_gpio_list, node) {
  6032. - if (!BANK_USED(bank) || !bank->loses_context)
  6033. + if (!BANK_USED(bank))
  6034. continue;
  6035. bank->power_mode = pwr_mode;
  6036. @@ -1437,12 +1484,13 @@
  6037. struct gpio_bank *bank;
  6038. list_for_each_entry(bank, &omap_gpio_list, node) {
  6039. - if (!BANK_USED(bank) || !bank->loses_context)
  6040. + if (!BANK_USED(bank))
  6041. continue;
  6042. pm_runtime_get_sync(bank->dev);
  6043. }
  6044. }
  6045. +#endif
  6046. #if defined(CONFIG_PM)
  6047. static void omap_gpio_init_context(struct gpio_bank *p)
  6048. @@ -1598,6 +1646,7 @@
  6049. static struct platform_driver omap_gpio_driver = {
  6050. .probe = omap_gpio_probe,
  6051. + .remove = omap_gpio_remove,
  6052. .driver = {
  6053. .name = "omap_gpio",
  6054. .pm = &gpio_pm_ops,
  6055. @@ -1615,3 +1664,13 @@
  6056. return platform_driver_register(&omap_gpio_driver);
  6057. }
  6058. postcore_initcall(omap_gpio_drv_reg);
  6059. +
  6060. +static void __exit omap_gpio_exit(void)
  6061. +{
  6062. + platform_driver_unregister(&omap_gpio_driver);
  6063. +}
  6064. +module_exit(omap_gpio_exit);
  6065. +
  6066. +MODULE_DESCRIPTION("omap gpio driver");
  6067. +MODULE_ALIAS("platform:gpio-omap");
  6068. +MODULE_LICENSE("GPL v2");
  6069. 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
  6070. --- linux-4.1.20.orig/drivers/gpu/drm/i915/i915_gem_execbuffer.c 2016-03-17 19:11:03.000000000 +0100
  6071. +++ linux-4.1.20/drivers/gpu/drm/i915/i915_gem_execbuffer.c 2016-03-21 20:18:31.000000000 +0100
  6072. @@ -32,6 +32,7 @@
  6073. #include "i915_trace.h"
  6074. #include "intel_drv.h"
  6075. #include <linux/dma_remapping.h>
  6076. +#include <linux/uaccess.h>
  6077. #define __EXEC_OBJECT_HAS_PIN (1<<31)
  6078. #define __EXEC_OBJECT_HAS_FENCE (1<<30)
  6079. @@ -465,7 +466,7 @@
  6080. }
  6081. /* We can't wait for rendering with pagefaults disabled */
  6082. - if (obj->active && in_atomic())
  6083. + if (obj->active && pagefault_disabled())
  6084. return -EFAULT;
  6085. if (use_cpu_reloc(obj))
  6086. @@ -1338,7 +1339,9 @@
  6087. return ret;
  6088. }
  6089. +#ifndef CONFIG_PREEMPT_RT_BASE
  6090. trace_i915_gem_ring_dispatch(intel_ring_get_request(ring), dispatch_flags);
  6091. +#endif
  6092. i915_gem_execbuffer_move_to_active(vmas, ring);
  6093. i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj);
  6094. 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
  6095. --- linux-4.1.20.orig/drivers/gpu/drm/i915/i915_gem_shrinker.c 2016-03-17 19:11:03.000000000 +0100
  6096. +++ linux-4.1.20/drivers/gpu/drm/i915/i915_gem_shrinker.c 2016-03-21 20:18:31.000000000 +0100
  6097. @@ -39,7 +39,7 @@
  6098. if (!mutex_is_locked(mutex))
  6099. return false;
  6100. -#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_MUTEXES)
  6101. +#if (defined(CONFIG_SMP) || defined(CONFIG_DEBUG_MUTEXES)) && !defined(CONFIG_PREEMPT_RT_BASE)
  6102. return mutex->owner == task;
  6103. #else
  6104. /* Since UP may be pre-empted, we cannot assume that we own the lock */
  6105. 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
  6106. --- linux-4.1.20.orig/drivers/gpu/drm/i915/i915_irq.c 2016-03-17 19:11:03.000000000 +0100
  6107. +++ linux-4.1.20/drivers/gpu/drm/i915/i915_irq.c 2016-03-21 20:18:31.000000000 +0100
  6108. @@ -676,6 +676,7 @@
  6109. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  6110. /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
  6111. + preempt_disable_rt();
  6112. /* Get optional system timestamp before query. */
  6113. if (stime)
  6114. @@ -727,6 +728,7 @@
  6115. *etime = ktime_get();
  6116. /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
  6117. + preempt_enable_rt();
  6118. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  6119. 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
  6120. --- linux-4.1.20.orig/drivers/gpu/drm/i915/intel_display.c 2016-03-17 19:11:03.000000000 +0100
  6121. +++ linux-4.1.20/drivers/gpu/drm/i915/intel_display.c 2016-03-21 20:18:31.000000000 +0100
  6122. @@ -10088,7 +10088,7 @@
  6123. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  6124. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6125. - WARN_ON(!in_interrupt());
  6126. + WARN_ON_NONRT(!in_interrupt());
  6127. if (crtc == NULL)
  6128. return;
  6129. 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
  6130. --- linux-4.1.20.orig/drivers/gpu/drm/i915/intel_sprite.c 2016-03-17 19:11:03.000000000 +0100
  6131. +++ linux-4.1.20/drivers/gpu/drm/i915/intel_sprite.c 2016-03-21 20:18:31.000000000 +0100
  6132. @@ -37,6 +37,7 @@
  6133. #include "intel_drv.h"
  6134. #include <drm/i915_drm.h>
  6135. #include "i915_drv.h"
  6136. +#include <linux/locallock.h>
  6137. static bool
  6138. format_is_yuv(uint32_t format)
  6139. @@ -61,6 +62,8 @@
  6140. return DIV_ROUND_UP(usecs * mode->crtc_clock, 1000 * mode->crtc_htotal);
  6141. }
  6142. +static DEFINE_LOCAL_IRQ_LOCK(pipe_update_lock);
  6143. +
  6144. /**
  6145. * intel_pipe_update_start() - start update of a set of display registers
  6146. * @crtc: the crtc of which the registers are going to be updated
  6147. @@ -101,7 +104,7 @@
  6148. if (WARN_ON(drm_crtc_vblank_get(&crtc->base)))
  6149. return false;
  6150. - local_irq_disable();
  6151. + local_lock_irq(pipe_update_lock);
  6152. trace_i915_pipe_update_start(crtc, min, max);
  6153. @@ -123,11 +126,11 @@
  6154. break;
  6155. }
  6156. - local_irq_enable();
  6157. + local_unlock_irq(pipe_update_lock);
  6158. timeout = schedule_timeout(timeout);
  6159. - local_irq_disable();
  6160. + local_lock_irq(pipe_update_lock);
  6161. }
  6162. finish_wait(wq, &wait);
  6163. @@ -158,7 +161,7 @@
  6164. trace_i915_pipe_update_end(crtc, end_vbl_count);
  6165. - local_irq_enable();
  6166. + local_unlock_irq(pipe_update_lock);
  6167. if (start_vbl_count != end_vbl_count)
  6168. DRM_ERROR("Atomic update failure on pipe %c (start=%u end=%u)\n",
  6169. 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
  6170. --- linux-4.1.20.orig/drivers/gpu/drm/radeon/radeon_display.c 2016-03-17 19:11:03.000000000 +0100
  6171. +++ linux-4.1.20/drivers/gpu/drm/radeon/radeon_display.c 2016-03-21 20:18:31.000000000 +0100
  6172. @@ -1798,6 +1798,7 @@
  6173. struct radeon_device *rdev = dev->dev_private;
  6174. /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
  6175. + preempt_disable_rt();
  6176. /* Get optional system timestamp before query. */
  6177. if (stime)
  6178. @@ -1890,6 +1891,7 @@
  6179. *etime = ktime_get();
  6180. /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
  6181. + preempt_enable_rt();
  6182. /* Decode into vertical and horizontal scanout position. */
  6183. *vpos = position & 0x1fff;
  6184. diff -Nur linux-4.1.20.orig/drivers/i2c/busses/i2c-omap.c linux-4.1.20/drivers/i2c/busses/i2c-omap.c
  6185. --- linux-4.1.20.orig/drivers/i2c/busses/i2c-omap.c 2016-03-17 19:11:03.000000000 +0100
  6186. +++ linux-4.1.20/drivers/i2c/busses/i2c-omap.c 2016-03-21 20:18:31.000000000 +0100
  6187. @@ -996,15 +996,12 @@
  6188. u16 mask;
  6189. u16 stat;
  6190. - spin_lock(&dev->lock);
  6191. - mask = omap_i2c_read_reg(dev, OMAP_I2C_IE_REG);
  6192. stat = omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG);
  6193. + mask = omap_i2c_read_reg(dev, OMAP_I2C_IE_REG);
  6194. if (stat & mask)
  6195. ret = IRQ_WAKE_THREAD;
  6196. - spin_unlock(&dev->lock);
  6197. -
  6198. return ret;
  6199. }
  6200. diff -Nur linux-4.1.20.orig/drivers/ide/alim15x3.c linux-4.1.20/drivers/ide/alim15x3.c
  6201. --- linux-4.1.20.orig/drivers/ide/alim15x3.c 2016-03-17 19:11:03.000000000 +0100
  6202. +++ linux-4.1.20/drivers/ide/alim15x3.c 2016-03-21 20:18:31.000000000 +0100
  6203. @@ -234,7 +234,7 @@
  6204. isa_dev = pci_get_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533, NULL);
  6205. - local_irq_save(flags);
  6206. + local_irq_save_nort(flags);
  6207. if (m5229_revision < 0xC2) {
  6208. /*
  6209. @@ -325,7 +325,7 @@
  6210. }
  6211. pci_dev_put(north);
  6212. pci_dev_put(isa_dev);
  6213. - local_irq_restore(flags);
  6214. + local_irq_restore_nort(flags);
  6215. return 0;
  6216. }
  6217. diff -Nur linux-4.1.20.orig/drivers/ide/hpt366.c linux-4.1.20/drivers/ide/hpt366.c
  6218. --- linux-4.1.20.orig/drivers/ide/hpt366.c 2016-03-17 19:11:03.000000000 +0100
  6219. +++ linux-4.1.20/drivers/ide/hpt366.c 2016-03-21 20:18:31.000000000 +0100
  6220. @@ -1241,7 +1241,7 @@
  6221. dma_old = inb(base + 2);
  6222. - local_irq_save(flags);
  6223. + local_irq_save_nort(flags);
  6224. dma_new = dma_old;
  6225. pci_read_config_byte(dev, hwif->channel ? 0x4b : 0x43, &masterdma);
  6226. @@ -1252,7 +1252,7 @@
  6227. if (dma_new != dma_old)
  6228. outb(dma_new, base + 2);
  6229. - local_irq_restore(flags);
  6230. + local_irq_restore_nort(flags);
  6231. printk(KERN_INFO " %s: BM-DMA at 0x%04lx-0x%04lx\n",
  6232. hwif->name, base, base + 7);
  6233. diff -Nur linux-4.1.20.orig/drivers/ide/ide-io-std.c linux-4.1.20/drivers/ide/ide-io-std.c
  6234. --- linux-4.1.20.orig/drivers/ide/ide-io-std.c 2016-03-17 19:11:03.000000000 +0100
  6235. +++ linux-4.1.20/drivers/ide/ide-io-std.c 2016-03-21 20:18:31.000000000 +0100
  6236. @@ -175,7 +175,7 @@
  6237. unsigned long uninitialized_var(flags);
  6238. if ((io_32bit & 2) && !mmio) {
  6239. - local_irq_save(flags);
  6240. + local_irq_save_nort(flags);
  6241. ata_vlb_sync(io_ports->nsect_addr);
  6242. }
  6243. @@ -186,7 +186,7 @@
  6244. insl(data_addr, buf, words);
  6245. if ((io_32bit & 2) && !mmio)
  6246. - local_irq_restore(flags);
  6247. + local_irq_restore_nort(flags);
  6248. if (((len + 1) & 3) < 2)
  6249. return;
  6250. @@ -219,7 +219,7 @@
  6251. unsigned long uninitialized_var(flags);
  6252. if ((io_32bit & 2) && !mmio) {
  6253. - local_irq_save(flags);
  6254. + local_irq_save_nort(flags);
  6255. ata_vlb_sync(io_ports->nsect_addr);
  6256. }
  6257. @@ -230,7 +230,7 @@
  6258. outsl(data_addr, buf, words);
  6259. if ((io_32bit & 2) && !mmio)
  6260. - local_irq_restore(flags);
  6261. + local_irq_restore_nort(flags);
  6262. if (((len + 1) & 3) < 2)
  6263. return;
  6264. diff -Nur linux-4.1.20.orig/drivers/ide/ide-io.c linux-4.1.20/drivers/ide/ide-io.c
  6265. --- linux-4.1.20.orig/drivers/ide/ide-io.c 2016-03-17 19:11:03.000000000 +0100
  6266. +++ linux-4.1.20/drivers/ide/ide-io.c 2016-03-21 20:18:31.000000000 +0100
  6267. @@ -659,7 +659,7 @@
  6268. /* disable_irq_nosync ?? */
  6269. disable_irq(hwif->irq);
  6270. /* local CPU only, as if we were handling an interrupt */
  6271. - local_irq_disable();
  6272. + local_irq_disable_nort();
  6273. if (hwif->polling) {
  6274. startstop = handler(drive);
  6275. } else if (drive_is_ready(drive)) {
  6276. diff -Nur linux-4.1.20.orig/drivers/ide/ide-iops.c linux-4.1.20/drivers/ide/ide-iops.c
  6277. --- linux-4.1.20.orig/drivers/ide/ide-iops.c 2016-03-17 19:11:03.000000000 +0100
  6278. +++ linux-4.1.20/drivers/ide/ide-iops.c 2016-03-21 20:18:31.000000000 +0100
  6279. @@ -129,12 +129,12 @@
  6280. if ((stat & ATA_BUSY) == 0)
  6281. break;
  6282. - local_irq_restore(flags);
  6283. + local_irq_restore_nort(flags);
  6284. *rstat = stat;
  6285. return -EBUSY;
  6286. }
  6287. }
  6288. - local_irq_restore(flags);
  6289. + local_irq_restore_nort(flags);
  6290. }
  6291. /*
  6292. * Allow status to settle, then read it again.
  6293. diff -Nur linux-4.1.20.orig/drivers/ide/ide-probe.c linux-4.1.20/drivers/ide/ide-probe.c
  6294. --- linux-4.1.20.orig/drivers/ide/ide-probe.c 2016-03-17 19:11:03.000000000 +0100
  6295. +++ linux-4.1.20/drivers/ide/ide-probe.c 2016-03-21 20:18:31.000000000 +0100
  6296. @@ -196,10 +196,10 @@
  6297. int bswap = 1;
  6298. /* local CPU only; some systems need this */
  6299. - local_irq_save(flags);
  6300. + local_irq_save_nort(flags);
  6301. /* read 512 bytes of id info */
  6302. hwif->tp_ops->input_data(drive, NULL, id, SECTOR_SIZE);
  6303. - local_irq_restore(flags);
  6304. + local_irq_restore_nort(flags);
  6305. drive->dev_flags |= IDE_DFLAG_ID_READ;
  6306. #ifdef DEBUG
  6307. diff -Nur linux-4.1.20.orig/drivers/ide/ide-taskfile.c linux-4.1.20/drivers/ide/ide-taskfile.c
  6308. --- linux-4.1.20.orig/drivers/ide/ide-taskfile.c 2016-03-17 19:11:03.000000000 +0100
  6309. +++ linux-4.1.20/drivers/ide/ide-taskfile.c 2016-03-21 20:18:31.000000000 +0100
  6310. @@ -250,7 +250,7 @@
  6311. page_is_high = PageHighMem(page);
  6312. if (page_is_high)
  6313. - local_irq_save(flags);
  6314. + local_irq_save_nort(flags);
  6315. buf = kmap_atomic(page) + offset;
  6316. @@ -271,7 +271,7 @@
  6317. kunmap_atomic(buf);
  6318. if (page_is_high)
  6319. - local_irq_restore(flags);
  6320. + local_irq_restore_nort(flags);
  6321. len -= nr_bytes;
  6322. }
  6323. @@ -414,7 +414,7 @@
  6324. }
  6325. if ((drive->dev_flags & IDE_DFLAG_UNMASK) == 0)
  6326. - local_irq_disable();
  6327. + local_irq_disable_nort();
  6328. ide_set_handler(drive, &task_pio_intr, WAIT_WORSTCASE);
  6329. 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
  6330. --- linux-4.1.20.orig/drivers/infiniband/ulp/ipoib/ipoib_multicast.c 2016-03-17 19:11:03.000000000 +0100
  6331. +++ linux-4.1.20/drivers/infiniband/ulp/ipoib/ipoib_multicast.c 2016-03-21 20:18:31.000000000 +0100
  6332. @@ -821,7 +821,7 @@
  6333. ipoib_dbg_mcast(priv, "restarting multicast task\n");
  6334. - local_irq_save(flags);
  6335. + local_irq_save_nort(flags);
  6336. netif_addr_lock(dev);
  6337. spin_lock(&priv->lock);
  6338. @@ -903,7 +903,7 @@
  6339. spin_unlock(&priv->lock);
  6340. netif_addr_unlock(dev);
  6341. - local_irq_restore(flags);
  6342. + local_irq_restore_nort(flags);
  6343. /*
  6344. * make sure the in-flight joins have finished before we attempt
  6345. diff -Nur linux-4.1.20.orig/drivers/input/gameport/gameport.c linux-4.1.20/drivers/input/gameport/gameport.c
  6346. --- linux-4.1.20.orig/drivers/input/gameport/gameport.c 2016-03-17 19:11:03.000000000 +0100
  6347. +++ linux-4.1.20/drivers/input/gameport/gameport.c 2016-03-21 20:18:31.000000000 +0100
  6348. @@ -124,12 +124,12 @@
  6349. tx = 1 << 30;
  6350. for(i = 0; i < 50; i++) {
  6351. - local_irq_save(flags);
  6352. + local_irq_save_nort(flags);
  6353. GET_TIME(t1);
  6354. for (t = 0; t < 50; t++) gameport_read(gameport);
  6355. GET_TIME(t2);
  6356. GET_TIME(t3);
  6357. - local_irq_restore(flags);
  6358. + local_irq_restore_nort(flags);
  6359. udelay(i * 10);
  6360. if ((t = DELTA(t2,t1) - DELTA(t3,t2)) < tx) tx = t;
  6361. }
  6362. @@ -148,11 +148,11 @@
  6363. tx = 1 << 30;
  6364. for(i = 0; i < 50; i++) {
  6365. - local_irq_save(flags);
  6366. + local_irq_save_nort(flags);
  6367. rdtscl(t1);
  6368. for (t = 0; t < 50; t++) gameport_read(gameport);
  6369. rdtscl(t2);
  6370. - local_irq_restore(flags);
  6371. + local_irq_restore_nort(flags);
  6372. udelay(i * 10);
  6373. if (t2 - t1 < tx) tx = t2 - t1;
  6374. }
  6375. diff -Nur linux-4.1.20.orig/drivers/leds/trigger/Kconfig linux-4.1.20/drivers/leds/trigger/Kconfig
  6376. --- linux-4.1.20.orig/drivers/leds/trigger/Kconfig 2016-03-17 19:11:03.000000000 +0100
  6377. +++ linux-4.1.20/drivers/leds/trigger/Kconfig 2016-03-21 20:18:31.000000000 +0100
  6378. @@ -61,7 +61,7 @@
  6379. config LEDS_TRIGGER_CPU
  6380. bool "LED CPU Trigger"
  6381. - depends on LEDS_TRIGGERS
  6382. + depends on LEDS_TRIGGERS && !PREEMPT_RT_BASE
  6383. help
  6384. This allows LEDs to be controlled by active CPUs. This shows
  6385. the active CPUs across an array of LEDs so you can see which
  6386. diff -Nur linux-4.1.20.orig/drivers/md/bcache/Kconfig linux-4.1.20/drivers/md/bcache/Kconfig
  6387. --- linux-4.1.20.orig/drivers/md/bcache/Kconfig 2016-03-17 19:11:03.000000000 +0100
  6388. +++ linux-4.1.20/drivers/md/bcache/Kconfig 2016-03-21 20:18:31.000000000 +0100
  6389. @@ -1,6 +1,7 @@
  6390. config BCACHE
  6391. tristate "Block device as cache"
  6392. + depends on !PREEMPT_RT_FULL
  6393. ---help---
  6394. Allows a block device to be used as cache for other devices; uses
  6395. a btree for indexing and the layout is optimized for SSDs.
  6396. diff -Nur linux-4.1.20.orig/drivers/md/dm.c linux-4.1.20/drivers/md/dm.c
  6397. --- linux-4.1.20.orig/drivers/md/dm.c 2016-03-17 19:11:03.000000000 +0100
  6398. +++ linux-4.1.20/drivers/md/dm.c 2016-03-21 20:18:31.000000000 +0100
  6399. @@ -2134,7 +2134,7 @@
  6400. /* Establish tio->ti before queuing work (map_tio_request) */
  6401. tio->ti = ti;
  6402. queue_kthread_work(&md->kworker, &tio->work);
  6403. - BUG_ON(!irqs_disabled());
  6404. + BUG_ON_NONRT(!irqs_disabled());
  6405. }
  6406. goto out;
  6407. diff -Nur linux-4.1.20.orig/drivers/md/raid5.c linux-4.1.20/drivers/md/raid5.c
  6408. --- linux-4.1.20.orig/drivers/md/raid5.c 2016-03-17 19:11:03.000000000 +0100
  6409. +++ linux-4.1.20/drivers/md/raid5.c 2016-03-21 20:18:31.000000000 +0100
  6410. @@ -1918,8 +1918,9 @@
  6411. struct raid5_percpu *percpu;
  6412. unsigned long cpu;
  6413. - cpu = get_cpu();
  6414. + cpu = get_cpu_light();
  6415. percpu = per_cpu_ptr(conf->percpu, cpu);
  6416. + spin_lock(&percpu->lock);
  6417. if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
  6418. ops_run_biofill(sh);
  6419. overlap_clear++;
  6420. @@ -1975,7 +1976,8 @@
  6421. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  6422. wake_up(&sh->raid_conf->wait_for_overlap);
  6423. }
  6424. - put_cpu();
  6425. + spin_unlock(&percpu->lock);
  6426. + put_cpu_light();
  6427. }
  6428. static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp)
  6429. @@ -6363,6 +6365,7 @@
  6430. __func__, cpu);
  6431. break;
  6432. }
  6433. + spin_lock_init(&per_cpu_ptr(conf->percpu, cpu)->lock);
  6434. }
  6435. put_online_cpus();
  6436. diff -Nur linux-4.1.20.orig/drivers/md/raid5.h linux-4.1.20/drivers/md/raid5.h
  6437. --- linux-4.1.20.orig/drivers/md/raid5.h 2016-03-17 19:11:03.000000000 +0100
  6438. +++ linux-4.1.20/drivers/md/raid5.h 2016-03-21 20:18:31.000000000 +0100
  6439. @@ -495,6 +495,7 @@
  6440. int recovery_disabled;
  6441. /* per cpu variables */
  6442. struct raid5_percpu {
  6443. + spinlock_t lock; /* Protection for -RT */
  6444. struct page *spare_page; /* Used when checking P/Q in raid6 */
  6445. struct flex_array *scribble; /* space for constructing buffer
  6446. * lists and performing address
  6447. diff -Nur linux-4.1.20.orig/drivers/misc/Kconfig linux-4.1.20/drivers/misc/Kconfig
  6448. --- linux-4.1.20.orig/drivers/misc/Kconfig 2016-03-17 19:11:03.000000000 +0100
  6449. +++ linux-4.1.20/drivers/misc/Kconfig 2016-03-21 20:18:31.000000000 +0100
  6450. @@ -54,6 +54,7 @@
  6451. config ATMEL_TCLIB
  6452. bool "Atmel AT32/AT91 Timer/Counter Library"
  6453. depends on (AVR32 || ARCH_AT91)
  6454. + default y if PREEMPT_RT_FULL
  6455. help
  6456. Select this if you want a library to allocate the Timer/Counter
  6457. blocks found on many Atmel processors. This facilitates using
  6458. @@ -69,8 +70,7 @@
  6459. are combined to make a single 32-bit timer.
  6460. When GENERIC_CLOCKEVENTS is defined, the third timer channel
  6461. - may be used as a clock event device supporting oneshot mode
  6462. - (delays of up to two seconds) based on the 32 KiHz clock.
  6463. + may be used as a clock event device supporting oneshot mode.
  6464. config ATMEL_TCB_CLKSRC_BLOCK
  6465. int
  6466. @@ -84,6 +84,15 @@
  6467. TC can be used for other purposes, such as PWM generation and
  6468. interval timing.
  6469. +config ATMEL_TCB_CLKSRC_USE_SLOW_CLOCK
  6470. + bool "TC Block use 32 KiHz clock"
  6471. + depends on ATMEL_TCB_CLKSRC
  6472. + default y if !PREEMPT_RT_FULL
  6473. + help
  6474. + Select this to use 32 KiHz base clock rate as TC block clock
  6475. + source for clock events.
  6476. +
  6477. +
  6478. config DUMMY_IRQ
  6479. tristate "Dummy IRQ handler"
  6480. default n
  6481. @@ -113,6 +122,35 @@
  6482. for information on the specific driver level and support statement
  6483. for your IBM server.
  6484. +config HWLAT_DETECTOR
  6485. + tristate "Testing module to detect hardware-induced latencies"
  6486. + depends on DEBUG_FS
  6487. + depends on RING_BUFFER
  6488. + default m
  6489. + ---help---
  6490. + A simple hardware latency detector. Use this module to detect
  6491. + large latencies introduced by the behavior of the underlying
  6492. + system firmware external to Linux. We do this using periodic
  6493. + use of stop_machine to grab all available CPUs and measure
  6494. + for unexplainable gaps in the CPU timestamp counter(s). By
  6495. + default, the module is not enabled until the "enable" file
  6496. + within the "hwlat_detector" debugfs directory is toggled.
  6497. +
  6498. + This module is often used to detect SMI (System Management
  6499. + Interrupts) on x86 systems, though is not x86 specific. To
  6500. + this end, we default to using a sample window of 1 second,
  6501. + during which we will sample for 0.5 seconds. If an SMI or
  6502. + similar event occurs during that time, it is recorded
  6503. + into an 8K samples global ring buffer until retreived.
  6504. +
  6505. + WARNING: This software should never be enabled (it can be built
  6506. + but should not be turned on after it is loaded) in a production
  6507. + environment where high latencies are a concern since the
  6508. + sampling mechanism actually introduces latencies for
  6509. + regular tasks while the CPU(s) are being held.
  6510. +
  6511. + If unsure, say N
  6512. +
  6513. config PHANTOM
  6514. tristate "Sensable PHANToM (PCI)"
  6515. depends on PCI
  6516. diff -Nur linux-4.1.20.orig/drivers/misc/Makefile linux-4.1.20/drivers/misc/Makefile
  6517. --- linux-4.1.20.orig/drivers/misc/Makefile 2016-03-17 19:11:03.000000000 +0100
  6518. +++ linux-4.1.20/drivers/misc/Makefile 2016-03-21 20:18:31.000000000 +0100
  6519. @@ -38,6 +38,7 @@
  6520. obj-$(CONFIG_HMC6352) += hmc6352.o
  6521. obj-y += eeprom/
  6522. obj-y += cb710/
  6523. +obj-$(CONFIG_HWLAT_DETECTOR) += hwlat_detector.o
  6524. obj-$(CONFIG_SPEAR13XX_PCIE_GADGET) += spear13xx_pcie_gadget.o
  6525. obj-$(CONFIG_VMWARE_BALLOON) += vmw_balloon.o
  6526. obj-$(CONFIG_ARM_CHARLCD) += arm-charlcd.o
  6527. diff -Nur linux-4.1.20.orig/drivers/misc/hwlat_detector.c linux-4.1.20/drivers/misc/hwlat_detector.c
  6528. --- linux-4.1.20.orig/drivers/misc/hwlat_detector.c 1970-01-01 01:00:00.000000000 +0100
  6529. +++ linux-4.1.20/drivers/misc/hwlat_detector.c 2016-03-21 20:18:31.000000000 +0100
  6530. @@ -0,0 +1,1240 @@
  6531. +/*
  6532. + * hwlat_detector.c - A simple Hardware Latency detector.
  6533. + *
  6534. + * Use this module to detect large system latencies induced by the behavior of
  6535. + * certain underlying system hardware or firmware, independent of Linux itself.
  6536. + * The code was developed originally to detect the presence of SMIs on Intel
  6537. + * and AMD systems, although there is no dependency upon x86 herein.
  6538. + *
  6539. + * The classical example usage of this module is in detecting the presence of
  6540. + * SMIs or System Management Interrupts on Intel and AMD systems. An SMI is a
  6541. + * somewhat special form of hardware interrupt spawned from earlier CPU debug
  6542. + * modes in which the (BIOS/EFI/etc.) firmware arranges for the South Bridge
  6543. + * LPC (or other device) to generate a special interrupt under certain
  6544. + * circumstances, for example, upon expiration of a special SMI timer device,
  6545. + * due to certain external thermal readings, on certain I/O address accesses,
  6546. + * and other situations. An SMI hits a special CPU pin, triggers a special
  6547. + * SMI mode (complete with special memory map), and the OS is unaware.
  6548. + *
  6549. + * Although certain hardware-inducing latencies are necessary (for example,
  6550. + * a modern system often requires an SMI handler for correct thermal control
  6551. + * and remote management) they can wreak havoc upon any OS-level performance
  6552. + * guarantees toward low-latency, especially when the OS is not even made
  6553. + * aware of the presence of these interrupts. For this reason, we need a
  6554. + * somewhat brute force mechanism to detect these interrupts. In this case,
  6555. + * we do it by hogging all of the CPU(s) for configurable timer intervals,
  6556. + * sampling the built-in CPU timer, looking for discontiguous readings.
  6557. + *
  6558. + * WARNING: This implementation necessarily introduces latencies. Therefore,
  6559. + * you should NEVER use this module in a production environment
  6560. + * requiring any kind of low-latency performance guarantee(s).
  6561. + *
  6562. + * Copyright (C) 2008-2009 Jon Masters, Red Hat, Inc. <jcm@redhat.com>
  6563. + *
  6564. + * Includes useful feedback from Clark Williams <clark@redhat.com>
  6565. + *
  6566. + * This file is licensed under the terms of the GNU General Public
  6567. + * License version 2. This program is licensed "as is" without any
  6568. + * warranty of any kind, whether express or implied.
  6569. + */
  6570. +
  6571. +#include <linux/module.h>
  6572. +#include <linux/init.h>
  6573. +#include <linux/ring_buffer.h>
  6574. +#include <linux/time.h>
  6575. +#include <linux/hrtimer.h>
  6576. +#include <linux/kthread.h>
  6577. +#include <linux/debugfs.h>
  6578. +#include <linux/seq_file.h>
  6579. +#include <linux/uaccess.h>
  6580. +#include <linux/version.h>
  6581. +#include <linux/delay.h>
  6582. +#include <linux/slab.h>
  6583. +#include <linux/trace_clock.h>
  6584. +
  6585. +#define BUF_SIZE_DEFAULT 262144UL /* 8K*(sizeof(entry)) */
  6586. +#define BUF_FLAGS (RB_FL_OVERWRITE) /* no block on full */
  6587. +#define U64STR_SIZE 22 /* 20 digits max */
  6588. +
  6589. +#define VERSION "1.0.0"
  6590. +#define BANNER "hwlat_detector: "
  6591. +#define DRVNAME "hwlat_detector"
  6592. +#define DEFAULT_SAMPLE_WINDOW 1000000 /* 1s */
  6593. +#define DEFAULT_SAMPLE_WIDTH 500000 /* 0.5s */
  6594. +#define DEFAULT_LAT_THRESHOLD 10 /* 10us */
  6595. +
  6596. +/* Module metadata */
  6597. +
  6598. +MODULE_LICENSE("GPL");
  6599. +MODULE_AUTHOR("Jon Masters <jcm@redhat.com>");
  6600. +MODULE_DESCRIPTION("A simple hardware latency detector");
  6601. +MODULE_VERSION(VERSION);
  6602. +
  6603. +/* Module parameters */
  6604. +
  6605. +static int debug;
  6606. +static int enabled;
  6607. +static int threshold;
  6608. +
  6609. +module_param(debug, int, 0); /* enable debug */
  6610. +module_param(enabled, int, 0); /* enable detector */
  6611. +module_param(threshold, int, 0); /* latency threshold */
  6612. +
  6613. +/* Buffering and sampling */
  6614. +
  6615. +static struct ring_buffer *ring_buffer; /* sample buffer */
  6616. +static DEFINE_MUTEX(ring_buffer_mutex); /* lock changes */
  6617. +static unsigned long buf_size = BUF_SIZE_DEFAULT;
  6618. +static struct task_struct *kthread; /* sampling thread */
  6619. +
  6620. +/* DebugFS filesystem entries */
  6621. +
  6622. +static struct dentry *debug_dir; /* debugfs directory */
  6623. +static struct dentry *debug_max; /* maximum TSC delta */
  6624. +static struct dentry *debug_count; /* total detect count */
  6625. +static struct dentry *debug_sample_width; /* sample width us */
  6626. +static struct dentry *debug_sample_window; /* sample window us */
  6627. +static struct dentry *debug_sample; /* raw samples us */
  6628. +static struct dentry *debug_threshold; /* threshold us */
  6629. +static struct dentry *debug_enable; /* enable/disable */
  6630. +
  6631. +/* Individual samples and global state */
  6632. +
  6633. +struct sample; /* latency sample */
  6634. +struct data; /* Global state */
  6635. +
  6636. +/* Sampling functions */
  6637. +static int __buffer_add_sample(struct sample *sample);
  6638. +static struct sample *buffer_get_sample(struct sample *sample);
  6639. +
  6640. +/* Threading and state */
  6641. +static int kthread_fn(void *unused);
  6642. +static int start_kthread(void);
  6643. +static int stop_kthread(void);
  6644. +static void __reset_stats(void);
  6645. +static int init_stats(void);
  6646. +
  6647. +/* Debugfs interface */
  6648. +static ssize_t simple_data_read(struct file *filp, char __user *ubuf,
  6649. + size_t cnt, loff_t *ppos, const u64 *entry);
  6650. +static ssize_t simple_data_write(struct file *filp, const char __user *ubuf,
  6651. + size_t cnt, loff_t *ppos, u64 *entry);
  6652. +static int debug_sample_fopen(struct inode *inode, struct file *filp);
  6653. +static ssize_t debug_sample_fread(struct file *filp, char __user *ubuf,
  6654. + size_t cnt, loff_t *ppos);
  6655. +static int debug_sample_release(struct inode *inode, struct file *filp);
  6656. +static int debug_enable_fopen(struct inode *inode, struct file *filp);
  6657. +static ssize_t debug_enable_fread(struct file *filp, char __user *ubuf,
  6658. + size_t cnt, loff_t *ppos);
  6659. +static ssize_t debug_enable_fwrite(struct file *file,
  6660. + const char __user *user_buffer,
  6661. + size_t user_size, loff_t *offset);
  6662. +
  6663. +/* Initialization functions */
  6664. +static int init_debugfs(void);
  6665. +static void free_debugfs(void);
  6666. +static int detector_init(void);
  6667. +static void detector_exit(void);
  6668. +
  6669. +/* Individual latency samples are stored here when detected and packed into
  6670. + * the ring_buffer circular buffer, where they are overwritten when
  6671. + * more than buf_size/sizeof(sample) samples are received. */
  6672. +struct sample {
  6673. + u64 seqnum; /* unique sequence */
  6674. + u64 duration; /* ktime delta */
  6675. + u64 outer_duration; /* ktime delta (outer loop) */
  6676. + struct timespec timestamp; /* wall time */
  6677. + unsigned long lost;
  6678. +};
  6679. +
  6680. +/* keep the global state somewhere. */
  6681. +static struct data {
  6682. +
  6683. + struct mutex lock; /* protect changes */
  6684. +
  6685. + u64 count; /* total since reset */
  6686. + u64 max_sample; /* max hardware latency */
  6687. + u64 threshold; /* sample threshold level */
  6688. +
  6689. + u64 sample_window; /* total sampling window (on+off) */
  6690. + u64 sample_width; /* active sampling portion of window */
  6691. +
  6692. + atomic_t sample_open; /* whether the sample file is open */
  6693. +
  6694. + wait_queue_head_t wq; /* waitqeue for new sample values */
  6695. +
  6696. +} data;
  6697. +
  6698. +/**
  6699. + * __buffer_add_sample - add a new latency sample recording to the ring buffer
  6700. + * @sample: The new latency sample value
  6701. + *
  6702. + * This receives a new latency sample and records it in a global ring buffer.
  6703. + * No additional locking is used in this case.
  6704. + */
  6705. +static int __buffer_add_sample(struct sample *sample)
  6706. +{
  6707. + return ring_buffer_write(ring_buffer,
  6708. + sizeof(struct sample), sample);
  6709. +}
  6710. +
  6711. +/**
  6712. + * buffer_get_sample - remove a hardware latency sample from the ring buffer
  6713. + * @sample: Pre-allocated storage for the sample
  6714. + *
  6715. + * This retrieves a hardware latency sample from the global circular buffer
  6716. + */
  6717. +static struct sample *buffer_get_sample(struct sample *sample)
  6718. +{
  6719. + struct ring_buffer_event *e = NULL;
  6720. + struct sample *s = NULL;
  6721. + unsigned int cpu = 0;
  6722. +
  6723. + if (!sample)
  6724. + return NULL;
  6725. +
  6726. + mutex_lock(&ring_buffer_mutex);
  6727. + for_each_online_cpu(cpu) {
  6728. + e = ring_buffer_consume(ring_buffer, cpu, NULL, &sample->lost);
  6729. + if (e)
  6730. + break;
  6731. + }
  6732. +
  6733. + if (e) {
  6734. + s = ring_buffer_event_data(e);
  6735. + memcpy(sample, s, sizeof(struct sample));
  6736. + } else
  6737. + sample = NULL;
  6738. + mutex_unlock(&ring_buffer_mutex);
  6739. +
  6740. + return sample;
  6741. +}
  6742. +
  6743. +#ifndef CONFIG_TRACING
  6744. +#define time_type ktime_t
  6745. +#define time_get() ktime_get()
  6746. +#define time_to_us(x) ktime_to_us(x)
  6747. +#define time_sub(a, b) ktime_sub(a, b)
  6748. +#define init_time(a, b) (a).tv64 = b
  6749. +#define time_u64(a) ((a).tv64)
  6750. +#else
  6751. +#define time_type u64
  6752. +#define time_get() trace_clock_local()
  6753. +#define time_to_us(x) div_u64(x, 1000)
  6754. +#define time_sub(a, b) ((a) - (b))
  6755. +#define init_time(a, b) (a = b)
  6756. +#define time_u64(a) a
  6757. +#endif
  6758. +/**
  6759. + * get_sample - sample the CPU TSC and look for likely hardware latencies
  6760. + *
  6761. + * Used to repeatedly capture the CPU TSC (or similar), looking for potential
  6762. + * hardware-induced latency. Called with interrupts disabled and with
  6763. + * data.lock held.
  6764. + */
  6765. +static int get_sample(void)
  6766. +{
  6767. + time_type start, t1, t2, last_t2;
  6768. + s64 diff, total = 0;
  6769. + u64 sample = 0;
  6770. + u64 outer_sample = 0;
  6771. + int ret = -1;
  6772. +
  6773. + init_time(last_t2, 0);
  6774. + start = time_get(); /* start timestamp */
  6775. +
  6776. + do {
  6777. +
  6778. + t1 = time_get(); /* we'll look for a discontinuity */
  6779. + t2 = time_get();
  6780. +
  6781. + if (time_u64(last_t2)) {
  6782. + /* Check the delta from outer loop (t2 to next t1) */
  6783. + diff = time_to_us(time_sub(t1, last_t2));
  6784. + /* This shouldn't happen */
  6785. + if (diff < 0) {
  6786. + pr_err(BANNER "time running backwards\n");
  6787. + goto out;
  6788. + }
  6789. + if (diff > outer_sample)
  6790. + outer_sample = diff;
  6791. + }
  6792. + last_t2 = t2;
  6793. +
  6794. + total = time_to_us(time_sub(t2, start)); /* sample width */
  6795. +
  6796. + /* This checks the inner loop (t1 to t2) */
  6797. + diff = time_to_us(time_sub(t2, t1)); /* current diff */
  6798. +
  6799. + /* This shouldn't happen */
  6800. + if (diff < 0) {
  6801. + pr_err(BANNER "time running backwards\n");
  6802. + goto out;
  6803. + }
  6804. +
  6805. + if (diff > sample)
  6806. + sample = diff; /* only want highest value */
  6807. +
  6808. + } while (total <= data.sample_width);
  6809. +
  6810. + ret = 0;
  6811. +
  6812. + /* If we exceed the threshold value, we have found a hardware latency */
  6813. + if (sample > data.threshold || outer_sample > data.threshold) {
  6814. + struct sample s;
  6815. +
  6816. + ret = 1;
  6817. +
  6818. + data.count++;
  6819. + s.seqnum = data.count;
  6820. + s.duration = sample;
  6821. + s.outer_duration = outer_sample;
  6822. + s.timestamp = CURRENT_TIME;
  6823. + __buffer_add_sample(&s);
  6824. +
  6825. + /* Keep a running maximum ever recorded hardware latency */
  6826. + if (sample > data.max_sample)
  6827. + data.max_sample = sample;
  6828. + }
  6829. +
  6830. +out:
  6831. + return ret;
  6832. +}
  6833. +
  6834. +/*
  6835. + * kthread_fn - The CPU time sampling/hardware latency detection kernel thread
  6836. + * @unused: A required part of the kthread API.
  6837. + *
  6838. + * Used to periodically sample the CPU TSC via a call to get_sample. We
  6839. + * disable interrupts, which does (intentionally) introduce latency since we
  6840. + * need to ensure nothing else might be running (and thus pre-empting).
  6841. + * Obviously this should never be used in production environments.
  6842. + *
  6843. + * Currently this runs on which ever CPU it was scheduled on, but most
  6844. + * real-worald hardware latency situations occur across several CPUs,
  6845. + * but we might later generalize this if we find there are any actualy
  6846. + * systems with alternate SMI delivery or other hardware latencies.
  6847. + */
  6848. +static int kthread_fn(void *unused)
  6849. +{
  6850. + int ret;
  6851. + u64 interval;
  6852. +
  6853. + while (!kthread_should_stop()) {
  6854. +
  6855. + mutex_lock(&data.lock);
  6856. +
  6857. + local_irq_disable();
  6858. + ret = get_sample();
  6859. + local_irq_enable();
  6860. +
  6861. + if (ret > 0)
  6862. + wake_up(&data.wq); /* wake up reader(s) */
  6863. +
  6864. + interval = data.sample_window - data.sample_width;
  6865. + do_div(interval, USEC_PER_MSEC); /* modifies interval value */
  6866. +
  6867. + mutex_unlock(&data.lock);
  6868. +
  6869. + if (msleep_interruptible(interval))
  6870. + break;
  6871. + }
  6872. +
  6873. + return 0;
  6874. +}
  6875. +
  6876. +/**
  6877. + * start_kthread - Kick off the hardware latency sampling/detector kthread
  6878. + *
  6879. + * This starts a kernel thread that will sit and sample the CPU timestamp
  6880. + * counter (TSC or similar) and look for potential hardware latencies.
  6881. + */
  6882. +static int start_kthread(void)
  6883. +{
  6884. + kthread = kthread_run(kthread_fn, NULL,
  6885. + DRVNAME);
  6886. + if (IS_ERR(kthread)) {
  6887. + pr_err(BANNER "could not start sampling thread\n");
  6888. + enabled = 0;
  6889. + return -ENOMEM;
  6890. + }
  6891. +
  6892. + return 0;
  6893. +}
  6894. +
  6895. +/**
  6896. + * stop_kthread - Inform the hardware latency samping/detector kthread to stop
  6897. + *
  6898. + * This kicks the running hardware latency sampling/detector kernel thread and
  6899. + * tells it to stop sampling now. Use this on unload and at system shutdown.
  6900. + */
  6901. +static int stop_kthread(void)
  6902. +{
  6903. + int ret;
  6904. +
  6905. + ret = kthread_stop(kthread);
  6906. +
  6907. + return ret;
  6908. +}
  6909. +
  6910. +/**
  6911. + * __reset_stats - Reset statistics for the hardware latency detector
  6912. + *
  6913. + * We use data to store various statistics and global state. We call this
  6914. + * function in order to reset those when "enable" is toggled on or off, and
  6915. + * also at initialization. Should be called with data.lock held.
  6916. + */
  6917. +static void __reset_stats(void)
  6918. +{
  6919. + data.count = 0;
  6920. + data.max_sample = 0;
  6921. + ring_buffer_reset(ring_buffer); /* flush out old sample entries */
  6922. +}
  6923. +
  6924. +/**
  6925. + * init_stats - Setup global state statistics for the hardware latency detector
  6926. + *
  6927. + * We use data to store various statistics and global state. We also use
  6928. + * a global ring buffer (ring_buffer) to keep raw samples of detected hardware
  6929. + * induced system latencies. This function initializes these structures and
  6930. + * allocates the global ring buffer also.
  6931. + */
  6932. +static int init_stats(void)
  6933. +{
  6934. + int ret = -ENOMEM;
  6935. +
  6936. + mutex_init(&data.lock);
  6937. + init_waitqueue_head(&data.wq);
  6938. + atomic_set(&data.sample_open, 0);
  6939. +
  6940. + ring_buffer = ring_buffer_alloc(buf_size, BUF_FLAGS);
  6941. +
  6942. + if (WARN(!ring_buffer, KERN_ERR BANNER
  6943. + "failed to allocate ring buffer!\n"))
  6944. + goto out;
  6945. +
  6946. + __reset_stats();
  6947. + data.threshold = threshold ?: DEFAULT_LAT_THRESHOLD; /* threshold us */
  6948. + data.sample_window = DEFAULT_SAMPLE_WINDOW; /* window us */
  6949. + data.sample_width = DEFAULT_SAMPLE_WIDTH; /* width us */
  6950. +
  6951. + ret = 0;
  6952. +
  6953. +out:
  6954. + return ret;
  6955. +
  6956. +}
  6957. +
  6958. +/*
  6959. + * simple_data_read - Wrapper read function for global state debugfs entries
  6960. + * @filp: The active open file structure for the debugfs "file"
  6961. + * @ubuf: The userspace provided buffer to read value into
  6962. + * @cnt: The maximum number of bytes to read
  6963. + * @ppos: The current "file" position
  6964. + * @entry: The entry to read from
  6965. + *
  6966. + * This function provides a generic read implementation for the global state
  6967. + * "data" structure debugfs filesystem entries. It would be nice to use
  6968. + * simple_attr_read directly, but we need to make sure that the data.lock
  6969. + * is held during the actual read.
  6970. + */
  6971. +static ssize_t simple_data_read(struct file *filp, char __user *ubuf,
  6972. + size_t cnt, loff_t *ppos, const u64 *entry)
  6973. +{
  6974. + char buf[U64STR_SIZE];
  6975. + u64 val = 0;
  6976. + int len = 0;
  6977. +
  6978. + memset(buf, 0, sizeof(buf));
  6979. +
  6980. + if (!entry)
  6981. + return -EFAULT;
  6982. +
  6983. + mutex_lock(&data.lock);
  6984. + val = *entry;
  6985. + mutex_unlock(&data.lock);
  6986. +
  6987. + len = snprintf(buf, sizeof(buf), "%llu\n", (unsigned long long)val);
  6988. +
  6989. + return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
  6990. +
  6991. +}
  6992. +
  6993. +/*
  6994. + * simple_data_write - Wrapper write function for global state debugfs entries
  6995. + * @filp: The active open file structure for the debugfs "file"
  6996. + * @ubuf: The userspace provided buffer to write value from
  6997. + * @cnt: The maximum number of bytes to write
  6998. + * @ppos: The current "file" position
  6999. + * @entry: The entry to write to
  7000. + *
  7001. + * This function provides a generic write implementation for the global state
  7002. + * "data" structure debugfs filesystem entries. It would be nice to use
  7003. + * simple_attr_write directly, but we need to make sure that the data.lock
  7004. + * is held during the actual write.
  7005. + */
  7006. +static ssize_t simple_data_write(struct file *filp, const char __user *ubuf,
  7007. + size_t cnt, loff_t *ppos, u64 *entry)
  7008. +{
  7009. + char buf[U64STR_SIZE];
  7010. + int csize = min(cnt, sizeof(buf));
  7011. + u64 val = 0;
  7012. + int err = 0;
  7013. +
  7014. + memset(buf, '\0', sizeof(buf));
  7015. + if (copy_from_user(buf, ubuf, csize))
  7016. + return -EFAULT;
  7017. +
  7018. + buf[U64STR_SIZE-1] = '\0'; /* just in case */
  7019. + err = kstrtoull(buf, 10, &val);
  7020. + if (err)
  7021. + return -EINVAL;
  7022. +
  7023. + mutex_lock(&data.lock);
  7024. + *entry = val;
  7025. + mutex_unlock(&data.lock);
  7026. +
  7027. + return csize;
  7028. +}
  7029. +
  7030. +/**
  7031. + * debug_count_fopen - Open function for "count" debugfs entry
  7032. + * @inode: The in-kernel inode representation of the debugfs "file"
  7033. + * @filp: The active open file structure for the debugfs "file"
  7034. + *
  7035. + * This function provides an open implementation for the "count" debugfs
  7036. + * interface to the hardware latency detector.
  7037. + */
  7038. +static int debug_count_fopen(struct inode *inode, struct file *filp)
  7039. +{
  7040. + return 0;
  7041. +}
  7042. +
  7043. +/**
  7044. + * debug_count_fread - Read function for "count" debugfs entry
  7045. + * @filp: The active open file structure for the debugfs "file"
  7046. + * @ubuf: The userspace provided buffer to read value into
  7047. + * @cnt: The maximum number of bytes to read
  7048. + * @ppos: The current "file" position
  7049. + *
  7050. + * This function provides a read implementation for the "count" debugfs
  7051. + * interface to the hardware latency detector. Can be used to read the
  7052. + * number of latency readings exceeding the configured threshold since
  7053. + * the detector was last reset (e.g. by writing a zero into "count").
  7054. + */
  7055. +static ssize_t debug_count_fread(struct file *filp, char __user *ubuf,
  7056. + size_t cnt, loff_t *ppos)
  7057. +{
  7058. + return simple_data_read(filp, ubuf, cnt, ppos, &data.count);
  7059. +}
  7060. +
  7061. +/**
  7062. + * debug_count_fwrite - Write function for "count" debugfs entry
  7063. + * @filp: The active open file structure for the debugfs "file"
  7064. + * @ubuf: The user buffer that contains the value to write
  7065. + * @cnt: The maximum number of bytes to write to "file"
  7066. + * @ppos: The current position in the debugfs "file"
  7067. + *
  7068. + * This function provides a write implementation for the "count" debugfs
  7069. + * interface to the hardware latency detector. Can be used to write a
  7070. + * desired value, especially to zero the total count.
  7071. + */
  7072. +static ssize_t debug_count_fwrite(struct file *filp,
  7073. + const char __user *ubuf,
  7074. + size_t cnt,
  7075. + loff_t *ppos)
  7076. +{
  7077. + return simple_data_write(filp, ubuf, cnt, ppos, &data.count);
  7078. +}
  7079. +
  7080. +/**
  7081. + * debug_enable_fopen - Dummy open function for "enable" debugfs interface
  7082. + * @inode: The in-kernel inode representation of the debugfs "file"
  7083. + * @filp: The active open file structure for the debugfs "file"
  7084. + *
  7085. + * This function provides an open implementation for the "enable" debugfs
  7086. + * interface to the hardware latency detector.
  7087. + */
  7088. +static int debug_enable_fopen(struct inode *inode, struct file *filp)
  7089. +{
  7090. + return 0;
  7091. +}
  7092. +
  7093. +/**
  7094. + * debug_enable_fread - Read function for "enable" debugfs interface
  7095. + * @filp: The active open file structure for the debugfs "file"
  7096. + * @ubuf: The userspace provided buffer to read value into
  7097. + * @cnt: The maximum number of bytes to read
  7098. + * @ppos: The current "file" position
  7099. + *
  7100. + * This function provides a read implementation for the "enable" debugfs
  7101. + * interface to the hardware latency detector. Can be used to determine
  7102. + * whether the detector is currently enabled ("0\n" or "1\n" returned).
  7103. + */
  7104. +static ssize_t debug_enable_fread(struct file *filp, char __user *ubuf,
  7105. + size_t cnt, loff_t *ppos)
  7106. +{
  7107. + char buf[4];
  7108. +
  7109. + if ((cnt < sizeof(buf)) || (*ppos))
  7110. + return 0;
  7111. +
  7112. + buf[0] = enabled ? '1' : '0';
  7113. + buf[1] = '\n';
  7114. + buf[2] = '\0';
  7115. + if (copy_to_user(ubuf, buf, strlen(buf)))
  7116. + return -EFAULT;
  7117. + return *ppos = strlen(buf);
  7118. +}
  7119. +
  7120. +/**
  7121. + * debug_enable_fwrite - Write function for "enable" debugfs interface
  7122. + * @filp: The active open file structure for the debugfs "file"
  7123. + * @ubuf: The user buffer that contains the value to write
  7124. + * @cnt: The maximum number of bytes to write to "file"
  7125. + * @ppos: The current position in the debugfs "file"
  7126. + *
  7127. + * This function provides a write implementation for the "enable" debugfs
  7128. + * interface to the hardware latency detector. Can be used to enable or
  7129. + * disable the detector, which will have the side-effect of possibly
  7130. + * also resetting the global stats and kicking off the measuring
  7131. + * kthread (on an enable) or the converse (upon a disable).
  7132. + */
  7133. +static ssize_t debug_enable_fwrite(struct file *filp,
  7134. + const char __user *ubuf,
  7135. + size_t cnt,
  7136. + loff_t *ppos)
  7137. +{
  7138. + char buf[4];
  7139. + int csize = min(cnt, sizeof(buf));
  7140. + long val = 0;
  7141. + int err = 0;
  7142. +
  7143. + memset(buf, '\0', sizeof(buf));
  7144. + if (copy_from_user(buf, ubuf, csize))
  7145. + return -EFAULT;
  7146. +
  7147. + buf[sizeof(buf)-1] = '\0'; /* just in case */
  7148. + err = kstrtoul(buf, 10, &val);
  7149. + if (0 != err)
  7150. + return -EINVAL;
  7151. +
  7152. + if (val) {
  7153. + if (enabled)
  7154. + goto unlock;
  7155. + enabled = 1;
  7156. + __reset_stats();
  7157. + if (start_kthread())
  7158. + return -EFAULT;
  7159. + } else {
  7160. + if (!enabled)
  7161. + goto unlock;
  7162. + enabled = 0;
  7163. + err = stop_kthread();
  7164. + if (err) {
  7165. + pr_err(BANNER "cannot stop kthread\n");
  7166. + return -EFAULT;
  7167. + }
  7168. + wake_up(&data.wq); /* reader(s) should return */
  7169. + }
  7170. +unlock:
  7171. + return csize;
  7172. +}
  7173. +
  7174. +/**
  7175. + * debug_max_fopen - Open function for "max" debugfs entry
  7176. + * @inode: The in-kernel inode representation of the debugfs "file"
  7177. + * @filp: The active open file structure for the debugfs "file"
  7178. + *
  7179. + * This function provides an open implementation for the "max" debugfs
  7180. + * interface to the hardware latency detector.
  7181. + */
  7182. +static int debug_max_fopen(struct inode *inode, struct file *filp)
  7183. +{
  7184. + return 0;
  7185. +}
  7186. +
  7187. +/**
  7188. + * debug_max_fread - Read function for "max" debugfs entry
  7189. + * @filp: The active open file structure for the debugfs "file"
  7190. + * @ubuf: The userspace provided buffer to read value into
  7191. + * @cnt: The maximum number of bytes to read
  7192. + * @ppos: The current "file" position
  7193. + *
  7194. + * This function provides a read implementation for the "max" debugfs
  7195. + * interface to the hardware latency detector. Can be used to determine
  7196. + * the maximum latency value observed since it was last reset.
  7197. + */
  7198. +static ssize_t debug_max_fread(struct file *filp, char __user *ubuf,
  7199. + size_t cnt, loff_t *ppos)
  7200. +{
  7201. + return simple_data_read(filp, ubuf, cnt, ppos, &data.max_sample);
  7202. +}
  7203. +
  7204. +/**
  7205. + * debug_max_fwrite - Write function for "max" debugfs entry
  7206. + * @filp: The active open file structure for the debugfs "file"
  7207. + * @ubuf: The user buffer that contains the value to write
  7208. + * @cnt: The maximum number of bytes to write to "file"
  7209. + * @ppos: The current position in the debugfs "file"
  7210. + *
  7211. + * This function provides a write implementation for the "max" debugfs
  7212. + * interface to the hardware latency detector. Can be used to reset the
  7213. + * maximum or set it to some other desired value - if, then, subsequent
  7214. + * measurements exceed this value, the maximum will be updated.
  7215. + */
  7216. +static ssize_t debug_max_fwrite(struct file *filp,
  7217. + const char __user *ubuf,
  7218. + size_t cnt,
  7219. + loff_t *ppos)
  7220. +{
  7221. + return simple_data_write(filp, ubuf, cnt, ppos, &data.max_sample);
  7222. +}
  7223. +
  7224. +
  7225. +/**
  7226. + * debug_sample_fopen - An open function for "sample" debugfs interface
  7227. + * @inode: The in-kernel inode representation of this debugfs "file"
  7228. + * @filp: The active open file structure for the debugfs "file"
  7229. + *
  7230. + * This function handles opening the "sample" file within the hardware
  7231. + * latency detector debugfs directory interface. This file is used to read
  7232. + * raw samples from the global ring_buffer and allows the user to see a
  7233. + * running latency history. Can be opened blocking or non-blocking,
  7234. + * affecting whether it behaves as a buffer read pipe, or does not.
  7235. + * Implements simple locking to prevent multiple simultaneous use.
  7236. + */
  7237. +static int debug_sample_fopen(struct inode *inode, struct file *filp)
  7238. +{
  7239. + if (!atomic_add_unless(&data.sample_open, 1, 1))
  7240. + return -EBUSY;
  7241. + else
  7242. + return 0;
  7243. +}
  7244. +
  7245. +/**
  7246. + * debug_sample_fread - A read function for "sample" debugfs interface
  7247. + * @filp: The active open file structure for the debugfs "file"
  7248. + * @ubuf: The user buffer that will contain the samples read
  7249. + * @cnt: The maximum bytes to read from the debugfs "file"
  7250. + * @ppos: The current position in the debugfs "file"
  7251. + *
  7252. + * This function handles reading from the "sample" file within the hardware
  7253. + * latency detector debugfs directory interface. This file is used to read
  7254. + * raw samples from the global ring_buffer and allows the user to see a
  7255. + * running latency history. By default this will block pending a new
  7256. + * value written into the sample buffer, unless there are already a
  7257. + * number of value(s) waiting in the buffer, or the sample file was
  7258. + * previously opened in a non-blocking mode of operation.
  7259. + */
  7260. +static ssize_t debug_sample_fread(struct file *filp, char __user *ubuf,
  7261. + size_t cnt, loff_t *ppos)
  7262. +{
  7263. + int len = 0;
  7264. + char buf[64];
  7265. + struct sample *sample = NULL;
  7266. +
  7267. + if (!enabled)
  7268. + return 0;
  7269. +
  7270. + sample = kzalloc(sizeof(struct sample), GFP_KERNEL);
  7271. + if (!sample)
  7272. + return -ENOMEM;
  7273. +
  7274. + while (!buffer_get_sample(sample)) {
  7275. +
  7276. + DEFINE_WAIT(wait);
  7277. +
  7278. + if (filp->f_flags & O_NONBLOCK) {
  7279. + len = -EAGAIN;
  7280. + goto out;
  7281. + }
  7282. +
  7283. + prepare_to_wait(&data.wq, &wait, TASK_INTERRUPTIBLE);
  7284. + schedule();
  7285. + finish_wait(&data.wq, &wait);
  7286. +
  7287. + if (signal_pending(current)) {
  7288. + len = -EINTR;
  7289. + goto out;
  7290. + }
  7291. +
  7292. + if (!enabled) { /* enable was toggled */
  7293. + len = 0;
  7294. + goto out;
  7295. + }
  7296. + }
  7297. +
  7298. + len = snprintf(buf, sizeof(buf), "%010lu.%010lu\t%llu\t%llu\n",
  7299. + sample->timestamp.tv_sec,
  7300. + sample->timestamp.tv_nsec,
  7301. + sample->duration,
  7302. + sample->outer_duration);
  7303. +
  7304. +
  7305. + /* handling partial reads is more trouble than it's worth */
  7306. + if (len > cnt)
  7307. + goto out;
  7308. +
  7309. + if (copy_to_user(ubuf, buf, len))
  7310. + len = -EFAULT;
  7311. +
  7312. +out:
  7313. + kfree(sample);
  7314. + return len;
  7315. +}
  7316. +
  7317. +/**
  7318. + * debug_sample_release - Release function for "sample" debugfs interface
  7319. + * @inode: The in-kernel inode represenation of the debugfs "file"
  7320. + * @filp: The active open file structure for the debugfs "file"
  7321. + *
  7322. + * This function completes the close of the debugfs interface "sample" file.
  7323. + * Frees the sample_open "lock" so that other users may open the interface.
  7324. + */
  7325. +static int debug_sample_release(struct inode *inode, struct file *filp)
  7326. +{
  7327. + atomic_dec(&data.sample_open);
  7328. +
  7329. + return 0;
  7330. +}
  7331. +
  7332. +/**
  7333. + * debug_threshold_fopen - Open function for "threshold" debugfs entry
  7334. + * @inode: The in-kernel inode representation of the debugfs "file"
  7335. + * @filp: The active open file structure for the debugfs "file"
  7336. + *
  7337. + * This function provides an open implementation for the "threshold" debugfs
  7338. + * interface to the hardware latency detector.
  7339. + */
  7340. +static int debug_threshold_fopen(struct inode *inode, struct file *filp)
  7341. +{
  7342. + return 0;
  7343. +}
  7344. +
  7345. +/**
  7346. + * debug_threshold_fread - Read function for "threshold" debugfs entry
  7347. + * @filp: The active open file structure for the debugfs "file"
  7348. + * @ubuf: The userspace provided buffer to read value into
  7349. + * @cnt: The maximum number of bytes to read
  7350. + * @ppos: The current "file" position
  7351. + *
  7352. + * This function provides a read implementation for the "threshold" debugfs
  7353. + * interface to the hardware latency detector. It can be used to determine
  7354. + * the current threshold level at which a latency will be recorded in the
  7355. + * global ring buffer, typically on the order of 10us.
  7356. + */
  7357. +static ssize_t debug_threshold_fread(struct file *filp, char __user *ubuf,
  7358. + size_t cnt, loff_t *ppos)
  7359. +{
  7360. + return simple_data_read(filp, ubuf, cnt, ppos, &data.threshold);
  7361. +}
  7362. +
  7363. +/**
  7364. + * debug_threshold_fwrite - Write function for "threshold" debugfs entry
  7365. + * @filp: The active open file structure for the debugfs "file"
  7366. + * @ubuf: The user buffer that contains the value to write
  7367. + * @cnt: The maximum number of bytes to write to "file"
  7368. + * @ppos: The current position in the debugfs "file"
  7369. + *
  7370. + * This function provides a write implementation for the "threshold" debugfs
  7371. + * interface to the hardware latency detector. It can be used to configure
  7372. + * the threshold level at which any subsequently detected latencies will
  7373. + * be recorded into the global ring buffer.
  7374. + */
  7375. +static ssize_t debug_threshold_fwrite(struct file *filp,
  7376. + const char __user *ubuf,
  7377. + size_t cnt,
  7378. + loff_t *ppos)
  7379. +{
  7380. + int ret;
  7381. +
  7382. + ret = simple_data_write(filp, ubuf, cnt, ppos, &data.threshold);
  7383. +
  7384. + if (enabled)
  7385. + wake_up_process(kthread);
  7386. +
  7387. + return ret;
  7388. +}
  7389. +
  7390. +/**
  7391. + * debug_width_fopen - Open function for "width" 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 "width" debugfs
  7396. + * interface to the hardware latency detector.
  7397. + */
  7398. +static int debug_width_fopen(struct inode *inode, struct file *filp)
  7399. +{
  7400. + return 0;
  7401. +}
  7402. +
  7403. +/**
  7404. + * debug_width_fread - Read function for "width" debugfs entry
  7405. + * @filp: The active open file structure for the debugfs "file"
  7406. + * @ubuf: The userspace provided buffer to read value into
  7407. + * @cnt: The maximum number of bytes to read
  7408. + * @ppos: The current "file" position
  7409. + *
  7410. + * This function provides a read implementation for the "width" debugfs
  7411. + * interface to the hardware latency detector. It can be used to determine
  7412. + * for how many us of the total window us we will actively sample for any
  7413. + * hardware-induced latecy periods. Obviously, it is not possible to
  7414. + * sample constantly and have the system respond to a sample reader, or,
  7415. + * worse, without having the system appear to have gone out to lunch.
  7416. + */
  7417. +static ssize_t debug_width_fread(struct file *filp, char __user *ubuf,
  7418. + size_t cnt, loff_t *ppos)
  7419. +{
  7420. + return simple_data_read(filp, ubuf, cnt, ppos, &data.sample_width);
  7421. +}
  7422. +
  7423. +/**
  7424. + * debug_width_fwrite - Write function for "width" debugfs entry
  7425. + * @filp: The active open file structure for the debugfs "file"
  7426. + * @ubuf: The user buffer that contains the value to write
  7427. + * @cnt: The maximum number of bytes to write to "file"
  7428. + * @ppos: The current position in the debugfs "file"
  7429. + *
  7430. + * This function provides a write implementation for the "width" debugfs
  7431. + * interface to the hardware latency detector. It can be used to configure
  7432. + * for how many us of the total window us we will actively sample for any
  7433. + * hardware-induced latency periods. Obviously, it is not possible to
  7434. + * sample constantly and have the system respond to a sample reader, or,
  7435. + * worse, without having the system appear to have gone out to lunch. It
  7436. + * is enforced that width is less that the total window size.
  7437. + */
  7438. +static ssize_t debug_width_fwrite(struct file *filp,
  7439. + const char __user *ubuf,
  7440. + size_t cnt,
  7441. + loff_t *ppos)
  7442. +{
  7443. + char buf[U64STR_SIZE];
  7444. + int csize = min(cnt, sizeof(buf));
  7445. + u64 val = 0;
  7446. + int err = 0;
  7447. +
  7448. + memset(buf, '\0', sizeof(buf));
  7449. + if (copy_from_user(buf, ubuf, csize))
  7450. + return -EFAULT;
  7451. +
  7452. + buf[U64STR_SIZE-1] = '\0'; /* just in case */
  7453. + err = kstrtoull(buf, 10, &val);
  7454. + if (0 != err)
  7455. + return -EINVAL;
  7456. +
  7457. + mutex_lock(&data.lock);
  7458. + if (val < data.sample_window)
  7459. + data.sample_width = val;
  7460. + else {
  7461. + mutex_unlock(&data.lock);
  7462. + return -EINVAL;
  7463. + }
  7464. + mutex_unlock(&data.lock);
  7465. +
  7466. + if (enabled)
  7467. + wake_up_process(kthread);
  7468. +
  7469. + return csize;
  7470. +}
  7471. +
  7472. +/**
  7473. + * debug_window_fopen - Open function for "window" debugfs entry
  7474. + * @inode: The in-kernel inode representation of the debugfs "file"
  7475. + * @filp: The active open file structure for the debugfs "file"
  7476. + *
  7477. + * This function provides an open implementation for the "window" debugfs
  7478. + * interface to the hardware latency detector. The window is the total time
  7479. + * in us that will be considered one sample period. Conceptually, windows
  7480. + * occur back-to-back and contain a sample width period during which
  7481. + * actual sampling occurs.
  7482. + */
  7483. +static int debug_window_fopen(struct inode *inode, struct file *filp)
  7484. +{
  7485. + return 0;
  7486. +}
  7487. +
  7488. +/**
  7489. + * debug_window_fread - Read function for "window" debugfs entry
  7490. + * @filp: The active open file structure for the debugfs "file"
  7491. + * @ubuf: The userspace provided buffer to read value into
  7492. + * @cnt: The maximum number of bytes to read
  7493. + * @ppos: The current "file" position
  7494. + *
  7495. + * This function provides a read implementation for the "window" debugfs
  7496. + * interface to the hardware latency detector. The window is the total time
  7497. + * in us that will be considered one sample period. Conceptually, windows
  7498. + * occur back-to-back and contain a sample width period during which
  7499. + * actual sampling occurs. Can be used to read the total window size.
  7500. + */
  7501. +static ssize_t debug_window_fread(struct file *filp, char __user *ubuf,
  7502. + size_t cnt, loff_t *ppos)
  7503. +{
  7504. + return simple_data_read(filp, ubuf, cnt, ppos, &data.sample_window);
  7505. +}
  7506. +
  7507. +/**
  7508. + * debug_window_fwrite - Write function for "window" debugfs entry
  7509. + * @filp: The active open file structure for the debugfs "file"
  7510. + * @ubuf: The user buffer that contains the value to write
  7511. + * @cnt: The maximum number of bytes to write to "file"
  7512. + * @ppos: The current position in the debugfs "file"
  7513. + *
  7514. + * This function provides a write implementation for the "window" debufds
  7515. + * interface to the hardware latency detetector. The window is the total time
  7516. + * in us that will be considered one sample period. Conceptually, windows
  7517. + * occur back-to-back and contain a sample width period during which
  7518. + * actual sampling occurs. Can be used to write a new total window size. It
  7519. + * is enfoced that any value written must be greater than the sample width
  7520. + * size, or an error results.
  7521. + */
  7522. +static ssize_t debug_window_fwrite(struct file *filp,
  7523. + const char __user *ubuf,
  7524. + size_t cnt,
  7525. + loff_t *ppos)
  7526. +{
  7527. + char buf[U64STR_SIZE];
  7528. + int csize = min(cnt, sizeof(buf));
  7529. + u64 val = 0;
  7530. + int err = 0;
  7531. +
  7532. + memset(buf, '\0', sizeof(buf));
  7533. + if (copy_from_user(buf, ubuf, csize))
  7534. + return -EFAULT;
  7535. +
  7536. + buf[U64STR_SIZE-1] = '\0'; /* just in case */
  7537. + err = kstrtoull(buf, 10, &val);
  7538. + if (0 != err)
  7539. + return -EINVAL;
  7540. +
  7541. + mutex_lock(&data.lock);
  7542. + if (data.sample_width < val)
  7543. + data.sample_window = val;
  7544. + else {
  7545. + mutex_unlock(&data.lock);
  7546. + return -EINVAL;
  7547. + }
  7548. + mutex_unlock(&data.lock);
  7549. +
  7550. + return csize;
  7551. +}
  7552. +
  7553. +/*
  7554. + * Function pointers for the "count" debugfs file operations
  7555. + */
  7556. +static const struct file_operations count_fops = {
  7557. + .open = debug_count_fopen,
  7558. + .read = debug_count_fread,
  7559. + .write = debug_count_fwrite,
  7560. + .owner = THIS_MODULE,
  7561. +};
  7562. +
  7563. +/*
  7564. + * Function pointers for the "enable" debugfs file operations
  7565. + */
  7566. +static const struct file_operations enable_fops = {
  7567. + .open = debug_enable_fopen,
  7568. + .read = debug_enable_fread,
  7569. + .write = debug_enable_fwrite,
  7570. + .owner = THIS_MODULE,
  7571. +};
  7572. +
  7573. +/*
  7574. + * Function pointers for the "max" debugfs file operations
  7575. + */
  7576. +static const struct file_operations max_fops = {
  7577. + .open = debug_max_fopen,
  7578. + .read = debug_max_fread,
  7579. + .write = debug_max_fwrite,
  7580. + .owner = THIS_MODULE,
  7581. +};
  7582. +
  7583. +/*
  7584. + * Function pointers for the "sample" debugfs file operations
  7585. + */
  7586. +static const struct file_operations sample_fops = {
  7587. + .open = debug_sample_fopen,
  7588. + .read = debug_sample_fread,
  7589. + .release = debug_sample_release,
  7590. + .owner = THIS_MODULE,
  7591. +};
  7592. +
  7593. +/*
  7594. + * Function pointers for the "threshold" debugfs file operations
  7595. + */
  7596. +static const struct file_operations threshold_fops = {
  7597. + .open = debug_threshold_fopen,
  7598. + .read = debug_threshold_fread,
  7599. + .write = debug_threshold_fwrite,
  7600. + .owner = THIS_MODULE,
  7601. +};
  7602. +
  7603. +/*
  7604. + * Function pointers for the "width" debugfs file operations
  7605. + */
  7606. +static const struct file_operations width_fops = {
  7607. + .open = debug_width_fopen,
  7608. + .read = debug_width_fread,
  7609. + .write = debug_width_fwrite,
  7610. + .owner = THIS_MODULE,
  7611. +};
  7612. +
  7613. +/*
  7614. + * Function pointers for the "window" debugfs file operations
  7615. + */
  7616. +static const struct file_operations window_fops = {
  7617. + .open = debug_window_fopen,
  7618. + .read = debug_window_fread,
  7619. + .write = debug_window_fwrite,
  7620. + .owner = THIS_MODULE,
  7621. +};
  7622. +
  7623. +/**
  7624. + * init_debugfs - A function to initialize the debugfs interface files
  7625. + *
  7626. + * This function creates entries in debugfs for "hwlat_detector", including
  7627. + * files to read values from the detector, current samples, and the
  7628. + * maximum sample that has been captured since the hardware latency
  7629. + * dectector was started.
  7630. + */
  7631. +static int init_debugfs(void)
  7632. +{
  7633. + int ret = -ENOMEM;
  7634. +
  7635. + debug_dir = debugfs_create_dir(DRVNAME, NULL);
  7636. + if (!debug_dir)
  7637. + goto err_debug_dir;
  7638. +
  7639. + debug_sample = debugfs_create_file("sample", 0444,
  7640. + debug_dir, NULL,
  7641. + &sample_fops);
  7642. + if (!debug_sample)
  7643. + goto err_sample;
  7644. +
  7645. + debug_count = debugfs_create_file("count", 0444,
  7646. + debug_dir, NULL,
  7647. + &count_fops);
  7648. + if (!debug_count)
  7649. + goto err_count;
  7650. +
  7651. + debug_max = debugfs_create_file("max", 0444,
  7652. + debug_dir, NULL,
  7653. + &max_fops);
  7654. + if (!debug_max)
  7655. + goto err_max;
  7656. +
  7657. + debug_sample_window = debugfs_create_file("window", 0644,
  7658. + debug_dir, NULL,
  7659. + &window_fops);
  7660. + if (!debug_sample_window)
  7661. + goto err_window;
  7662. +
  7663. + debug_sample_width = debugfs_create_file("width", 0644,
  7664. + debug_dir, NULL,
  7665. + &width_fops);
  7666. + if (!debug_sample_width)
  7667. + goto err_width;
  7668. +
  7669. + debug_threshold = debugfs_create_file("threshold", 0644,
  7670. + debug_dir, NULL,
  7671. + &threshold_fops);
  7672. + if (!debug_threshold)
  7673. + goto err_threshold;
  7674. +
  7675. + debug_enable = debugfs_create_file("enable", 0644,
  7676. + debug_dir, &enabled,
  7677. + &enable_fops);
  7678. + if (!debug_enable)
  7679. + goto err_enable;
  7680. +
  7681. + else {
  7682. + ret = 0;
  7683. + goto out;
  7684. + }
  7685. +
  7686. +err_enable:
  7687. + debugfs_remove(debug_threshold);
  7688. +err_threshold:
  7689. + debugfs_remove(debug_sample_width);
  7690. +err_width:
  7691. + debugfs_remove(debug_sample_window);
  7692. +err_window:
  7693. + debugfs_remove(debug_max);
  7694. +err_max:
  7695. + debugfs_remove(debug_count);
  7696. +err_count:
  7697. + debugfs_remove(debug_sample);
  7698. +err_sample:
  7699. + debugfs_remove(debug_dir);
  7700. +err_debug_dir:
  7701. +out:
  7702. + return ret;
  7703. +}
  7704. +
  7705. +/**
  7706. + * free_debugfs - A function to cleanup the debugfs file interface
  7707. + */
  7708. +static void free_debugfs(void)
  7709. +{
  7710. + /* could also use a debugfs_remove_recursive */
  7711. + debugfs_remove(debug_enable);
  7712. + debugfs_remove(debug_threshold);
  7713. + debugfs_remove(debug_sample_width);
  7714. + debugfs_remove(debug_sample_window);
  7715. + debugfs_remove(debug_max);
  7716. + debugfs_remove(debug_count);
  7717. + debugfs_remove(debug_sample);
  7718. + debugfs_remove(debug_dir);
  7719. +}
  7720. +
  7721. +/**
  7722. + * detector_init - Standard module initialization code
  7723. + */
  7724. +static int detector_init(void)
  7725. +{
  7726. + int ret = -ENOMEM;
  7727. +
  7728. + pr_info(BANNER "version %s\n", VERSION);
  7729. +
  7730. + ret = init_stats();
  7731. + if (0 != ret)
  7732. + goto out;
  7733. +
  7734. + ret = init_debugfs();
  7735. + if (0 != ret)
  7736. + goto err_stats;
  7737. +
  7738. + if (enabled)
  7739. + ret = start_kthread();
  7740. +
  7741. + goto out;
  7742. +
  7743. +err_stats:
  7744. + ring_buffer_free(ring_buffer);
  7745. +out:
  7746. + return ret;
  7747. +
  7748. +}
  7749. +
  7750. +/**
  7751. + * detector_exit - Standard module cleanup code
  7752. + */
  7753. +static void detector_exit(void)
  7754. +{
  7755. + int err;
  7756. +
  7757. + if (enabled) {
  7758. + enabled = 0;
  7759. + err = stop_kthread();
  7760. + if (err)
  7761. + pr_err(BANNER "cannot stop kthread\n");
  7762. + }
  7763. +
  7764. + free_debugfs();
  7765. + ring_buffer_free(ring_buffer); /* free up the ring buffer */
  7766. +
  7767. +}
  7768. +
  7769. +module_init(detector_init);
  7770. +module_exit(detector_exit);
  7771. diff -Nur linux-4.1.20.orig/drivers/mmc/host/mmci.c linux-4.1.20/drivers/mmc/host/mmci.c
  7772. --- linux-4.1.20.orig/drivers/mmc/host/mmci.c 2016-03-17 19:11:03.000000000 +0100
  7773. +++ linux-4.1.20/drivers/mmc/host/mmci.c 2016-03-21 20:18:31.000000000 +0100
  7774. @@ -1155,15 +1155,12 @@
  7775. struct sg_mapping_iter *sg_miter = &host->sg_miter;
  7776. struct variant_data *variant = host->variant;
  7777. void __iomem *base = host->base;
  7778. - unsigned long flags;
  7779. u32 status;
  7780. status = readl(base + MMCISTATUS);
  7781. dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
  7782. - local_irq_save(flags);
  7783. -
  7784. do {
  7785. unsigned int remain, len;
  7786. char *buffer;
  7787. @@ -1203,8 +1200,6 @@
  7788. sg_miter_stop(sg_miter);
  7789. - local_irq_restore(flags);
  7790. -
  7791. /*
  7792. * If we have less than the fifo 'half-full' threshold to transfer,
  7793. * trigger a PIO interrupt as soon as any data is available.
  7794. diff -Nur linux-4.1.20.orig/drivers/net/ethernet/3com/3c59x.c linux-4.1.20/drivers/net/ethernet/3com/3c59x.c
  7795. --- linux-4.1.20.orig/drivers/net/ethernet/3com/3c59x.c 2016-03-17 19:11:03.000000000 +0100
  7796. +++ linux-4.1.20/drivers/net/ethernet/3com/3c59x.c 2016-03-21 20:18:31.000000000 +0100
  7797. @@ -842,9 +842,9 @@
  7798. {
  7799. struct vortex_private *vp = netdev_priv(dev);
  7800. unsigned long flags;
  7801. - local_irq_save(flags);
  7802. + local_irq_save_nort(flags);
  7803. (vp->full_bus_master_rx ? boomerang_interrupt:vortex_interrupt)(dev->irq,dev);
  7804. - local_irq_restore(flags);
  7805. + local_irq_restore_nort(flags);
  7806. }
  7807. #endif
  7808. @@ -1916,12 +1916,12 @@
  7809. * Block interrupts because vortex_interrupt does a bare spin_lock()
  7810. */
  7811. unsigned long flags;
  7812. - local_irq_save(flags);
  7813. + local_irq_save_nort(flags);
  7814. if (vp->full_bus_master_tx)
  7815. boomerang_interrupt(dev->irq, dev);
  7816. else
  7817. vortex_interrupt(dev->irq, dev);
  7818. - local_irq_restore(flags);
  7819. + local_irq_restore_nort(flags);
  7820. }
  7821. }
  7822. 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
  7823. --- linux-4.1.20.orig/drivers/net/ethernet/atheros/atl1c/atl1c_main.c 2016-03-17 19:11:03.000000000 +0100
  7824. +++ linux-4.1.20/drivers/net/ethernet/atheros/atl1c/atl1c_main.c 2016-03-21 20:18:31.000000000 +0100
  7825. @@ -2212,11 +2212,7 @@
  7826. }
  7827. tpd_req = atl1c_cal_tpd_req(skb);
  7828. - if (!spin_trylock_irqsave(&adapter->tx_lock, flags)) {
  7829. - if (netif_msg_pktdata(adapter))
  7830. - dev_info(&adapter->pdev->dev, "tx locked\n");
  7831. - return NETDEV_TX_LOCKED;
  7832. - }
  7833. + spin_lock_irqsave(&adapter->tx_lock, flags);
  7834. if (atl1c_tpd_avail(adapter, type) < tpd_req) {
  7835. /* no enough descriptor, just stop queue */
  7836. 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
  7837. --- linux-4.1.20.orig/drivers/net/ethernet/atheros/atl1e/atl1e_main.c 2016-03-17 19:11:03.000000000 +0100
  7838. +++ linux-4.1.20/drivers/net/ethernet/atheros/atl1e/atl1e_main.c 2016-03-21 20:18:31.000000000 +0100
  7839. @@ -1880,8 +1880,7 @@
  7840. return NETDEV_TX_OK;
  7841. }
  7842. tpd_req = atl1e_cal_tdp_req(skb);
  7843. - if (!spin_trylock_irqsave(&adapter->tx_lock, flags))
  7844. - return NETDEV_TX_LOCKED;
  7845. + spin_lock_irqsave(&adapter->tx_lock, flags);
  7846. if (atl1e_tpd_avail(adapter) < tpd_req) {
  7847. /* no enough descriptor, just stop queue */
  7848. 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
  7849. --- linux-4.1.20.orig/drivers/net/ethernet/chelsio/cxgb/sge.c 2016-03-17 19:11:03.000000000 +0100
  7850. +++ linux-4.1.20/drivers/net/ethernet/chelsio/cxgb/sge.c 2016-03-21 20:18:31.000000000 +0100
  7851. @@ -1664,8 +1664,7 @@
  7852. struct cmdQ *q = &sge->cmdQ[qid];
  7853. unsigned int credits, pidx, genbit, count, use_sched_skb = 0;
  7854. - if (!spin_trylock(&q->lock))
  7855. - return NETDEV_TX_LOCKED;
  7856. + spin_lock(&q->lock);
  7857. reclaim_completed_tx(sge, q);
  7858. diff -Nur linux-4.1.20.orig/drivers/net/ethernet/freescale/gianfar.c linux-4.1.20/drivers/net/ethernet/freescale/gianfar.c
  7859. --- linux-4.1.20.orig/drivers/net/ethernet/freescale/gianfar.c 2016-03-17 19:11:03.000000000 +0100
  7860. +++ linux-4.1.20/drivers/net/ethernet/freescale/gianfar.c 2016-03-21 20:18:31.000000000 +0100
  7861. @@ -1540,7 +1540,7 @@
  7862. if (netif_running(ndev)) {
  7863. - local_irq_save(flags);
  7864. + local_irq_save_nort(flags);
  7865. lock_tx_qs(priv);
  7866. gfar_halt_nodisable(priv);
  7867. @@ -1556,7 +1556,7 @@
  7868. gfar_write(&regs->maccfg1, tempval);
  7869. unlock_tx_qs(priv);
  7870. - local_irq_restore(flags);
  7871. + local_irq_restore_nort(flags);
  7872. disable_napi(priv);
  7873. @@ -1598,7 +1598,7 @@
  7874. /* Disable Magic Packet mode, in case something
  7875. * else woke us up.
  7876. */
  7877. - local_irq_save(flags);
  7878. + local_irq_save_nort(flags);
  7879. lock_tx_qs(priv);
  7880. tempval = gfar_read(&regs->maccfg2);
  7881. @@ -1608,7 +1608,7 @@
  7882. gfar_start(priv);
  7883. unlock_tx_qs(priv);
  7884. - local_irq_restore(flags);
  7885. + local_irq_restore_nort(flags);
  7886. netif_device_attach(ndev);
  7887. @@ -3418,14 +3418,14 @@
  7888. dev->stats.tx_dropped++;
  7889. atomic64_inc(&priv->extra_stats.tx_underrun);
  7890. - local_irq_save(flags);
  7891. + local_irq_save_nort(flags);
  7892. lock_tx_qs(priv);
  7893. /* Reactivate the Tx Queues */
  7894. gfar_write(&regs->tstat, gfargrp->tstat);
  7895. unlock_tx_qs(priv);
  7896. - local_irq_restore(flags);
  7897. + local_irq_restore_nort(flags);
  7898. }
  7899. netif_dbg(priv, tx_err, dev, "Transmit Error\n");
  7900. }
  7901. diff -Nur linux-4.1.20.orig/drivers/net/ethernet/neterion/s2io.c linux-4.1.20/drivers/net/ethernet/neterion/s2io.c
  7902. --- linux-4.1.20.orig/drivers/net/ethernet/neterion/s2io.c 2016-03-17 19:11:03.000000000 +0100
  7903. +++ linux-4.1.20/drivers/net/ethernet/neterion/s2io.c 2016-03-21 20:18:31.000000000 +0100
  7904. @@ -4084,12 +4084,7 @@
  7905. [skb->priority & (MAX_TX_FIFOS - 1)];
  7906. fifo = &mac_control->fifos[queue];
  7907. - if (do_spin_lock)
  7908. - spin_lock_irqsave(&fifo->tx_lock, flags);
  7909. - else {
  7910. - if (unlikely(!spin_trylock_irqsave(&fifo->tx_lock, flags)))
  7911. - return NETDEV_TX_LOCKED;
  7912. - }
  7913. + spin_lock_irqsave(&fifo->tx_lock, flags);
  7914. if (sp->config.multiq) {
  7915. if (__netif_subqueue_stopped(dev, fifo->fifo_no)) {
  7916. 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
  7917. --- linux-4.1.20.orig/drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c 2016-03-17 19:11:03.000000000 +0100
  7918. +++ linux-4.1.20/drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c 2016-03-21 20:18:32.000000000 +0100
  7919. @@ -2137,10 +2137,8 @@
  7920. struct pch_gbe_tx_ring *tx_ring = adapter->tx_ring;
  7921. unsigned long flags;
  7922. - if (!spin_trylock_irqsave(&tx_ring->tx_lock, flags)) {
  7923. - /* Collision - tell upper layer to requeue */
  7924. - return NETDEV_TX_LOCKED;
  7925. - }
  7926. + spin_lock_irqsave(&tx_ring->tx_lock, flags);
  7927. +
  7928. if (unlikely(!PCH_GBE_DESC_UNUSED(tx_ring))) {
  7929. netif_stop_queue(netdev);
  7930. spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
  7931. diff -Nur linux-4.1.20.orig/drivers/net/ethernet/realtek/8139too.c linux-4.1.20/drivers/net/ethernet/realtek/8139too.c
  7932. --- linux-4.1.20.orig/drivers/net/ethernet/realtek/8139too.c 2016-03-17 19:11:03.000000000 +0100
  7933. +++ linux-4.1.20/drivers/net/ethernet/realtek/8139too.c 2016-03-21 20:18:32.000000000 +0100
  7934. @@ -2229,7 +2229,7 @@
  7935. struct rtl8139_private *tp = netdev_priv(dev);
  7936. const int irq = tp->pci_dev->irq;
  7937. - disable_irq(irq);
  7938. + disable_irq_nosync(irq);
  7939. rtl8139_interrupt(irq, dev);
  7940. enable_irq(irq);
  7941. }
  7942. diff -Nur linux-4.1.20.orig/drivers/net/ethernet/tehuti/tehuti.c linux-4.1.20/drivers/net/ethernet/tehuti/tehuti.c
  7943. --- linux-4.1.20.orig/drivers/net/ethernet/tehuti/tehuti.c 2016-03-17 19:11:03.000000000 +0100
  7944. +++ linux-4.1.20/drivers/net/ethernet/tehuti/tehuti.c 2016-03-21 20:18:32.000000000 +0100
  7945. @@ -1629,13 +1629,8 @@
  7946. unsigned long flags;
  7947. ENTER;
  7948. - local_irq_save(flags);
  7949. - if (!spin_trylock(&priv->tx_lock)) {
  7950. - local_irq_restore(flags);
  7951. - DBG("%s[%s]: TX locked, returning NETDEV_TX_LOCKED\n",
  7952. - BDX_DRV_NAME, ndev->name);
  7953. - return NETDEV_TX_LOCKED;
  7954. - }
  7955. +
  7956. + spin_lock_irqsave(&priv->tx_lock, flags);
  7957. /* build tx descriptor */
  7958. BDX_ASSERT(f->m.wptr >= f->m.memsz); /* started with valid wptr */
  7959. diff -Nur linux-4.1.20.orig/drivers/net/rionet.c linux-4.1.20/drivers/net/rionet.c
  7960. --- linux-4.1.20.orig/drivers/net/rionet.c 2016-03-17 19:11:03.000000000 +0100
  7961. +++ linux-4.1.20/drivers/net/rionet.c 2016-03-21 20:18:32.000000000 +0100
  7962. @@ -174,11 +174,7 @@
  7963. unsigned long flags;
  7964. int add_num = 1;
  7965. - local_irq_save(flags);
  7966. - if (!spin_trylock(&rnet->tx_lock)) {
  7967. - local_irq_restore(flags);
  7968. - return NETDEV_TX_LOCKED;
  7969. - }
  7970. + spin_lock_irqsave(&rnet->tx_lock, flags);
  7971. if (is_multicast_ether_addr(eth->h_dest))
  7972. add_num = nets[rnet->mport->id].nact;
  7973. 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
  7974. --- linux-4.1.20.orig/drivers/net/wireless/orinoco/orinoco_usb.c 2016-03-17 19:11:03.000000000 +0100
  7975. +++ linux-4.1.20/drivers/net/wireless/orinoco/orinoco_usb.c 2016-03-21 20:18:32.000000000 +0100
  7976. @@ -697,7 +697,7 @@
  7977. while (!ctx->done.done && msecs--)
  7978. udelay(1000);
  7979. } else {
  7980. - wait_event_interruptible(ctx->done.wait,
  7981. + swait_event_interruptible(ctx->done.wait,
  7982. ctx->done.done);
  7983. }
  7984. break;
  7985. diff -Nur linux-4.1.20.orig/drivers/pci/access.c linux-4.1.20/drivers/pci/access.c
  7986. --- linux-4.1.20.orig/drivers/pci/access.c 2016-03-17 19:11:03.000000000 +0100
  7987. +++ linux-4.1.20/drivers/pci/access.c 2016-03-21 20:18:32.000000000 +0100
  7988. @@ -561,7 +561,7 @@
  7989. WARN_ON(!dev->block_cfg_access);
  7990. dev->block_cfg_access = 0;
  7991. - wake_up_all(&pci_cfg_wait);
  7992. + wake_up_all_locked(&pci_cfg_wait);
  7993. raw_spin_unlock_irqrestore(&pci_lock, flags);
  7994. }
  7995. EXPORT_SYMBOL_GPL(pci_cfg_access_unlock);
  7996. diff -Nur linux-4.1.20.orig/drivers/scsi/fcoe/fcoe.c linux-4.1.20/drivers/scsi/fcoe/fcoe.c
  7997. --- linux-4.1.20.orig/drivers/scsi/fcoe/fcoe.c 2016-03-17 19:11:03.000000000 +0100
  7998. +++ linux-4.1.20/drivers/scsi/fcoe/fcoe.c 2016-03-21 20:18:32.000000000 +0100
  7999. @@ -1287,7 +1287,7 @@
  8000. struct sk_buff *skb;
  8001. #ifdef CONFIG_SMP
  8002. struct fcoe_percpu_s *p0;
  8003. - unsigned targ_cpu = get_cpu();
  8004. + unsigned targ_cpu = get_cpu_light();
  8005. #endif /* CONFIG_SMP */
  8006. FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
  8007. @@ -1343,7 +1343,7 @@
  8008. kfree_skb(skb);
  8009. spin_unlock_bh(&p->fcoe_rx_list.lock);
  8010. }
  8011. - put_cpu();
  8012. + put_cpu_light();
  8013. #else
  8014. /*
  8015. * This a non-SMP scenario where the singular Rx thread is
  8016. @@ -1567,11 +1567,11 @@
  8017. static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
  8018. {
  8019. struct fcoe_percpu_s *fps;
  8020. - int rc;
  8021. + int rc, cpu = get_cpu_light();
  8022. - fps = &get_cpu_var(fcoe_percpu);
  8023. + fps = &per_cpu(fcoe_percpu, cpu);
  8024. rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
  8025. - put_cpu_var(fcoe_percpu);
  8026. + put_cpu_light();
  8027. return rc;
  8028. }
  8029. @@ -1767,11 +1767,11 @@
  8030. return 0;
  8031. }
  8032. - stats = per_cpu_ptr(lport->stats, get_cpu());
  8033. + stats = per_cpu_ptr(lport->stats, get_cpu_light());
  8034. stats->InvalidCRCCount++;
  8035. if (stats->InvalidCRCCount < 5)
  8036. printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
  8037. - put_cpu();
  8038. + put_cpu_light();
  8039. return -EINVAL;
  8040. }
  8041. @@ -1847,13 +1847,13 @@
  8042. goto drop;
  8043. if (!fcoe_filter_frames(lport, fp)) {
  8044. - put_cpu();
  8045. + put_cpu_light();
  8046. fc_exch_recv(lport, fp);
  8047. return;
  8048. }
  8049. drop:
  8050. stats->ErrorFrames++;
  8051. - put_cpu();
  8052. + put_cpu_light();
  8053. kfree_skb(skb);
  8054. }
  8055. diff -Nur linux-4.1.20.orig/drivers/scsi/fcoe/fcoe_ctlr.c linux-4.1.20/drivers/scsi/fcoe/fcoe_ctlr.c
  8056. --- linux-4.1.20.orig/drivers/scsi/fcoe/fcoe_ctlr.c 2016-03-17 19:11:03.000000000 +0100
  8057. +++ linux-4.1.20/drivers/scsi/fcoe/fcoe_ctlr.c 2016-03-21 20:18:32.000000000 +0100
  8058. @@ -831,7 +831,7 @@
  8059. INIT_LIST_HEAD(&del_list);
  8060. - stats = per_cpu_ptr(fip->lp->stats, get_cpu());
  8061. + stats = per_cpu_ptr(fip->lp->stats, get_cpu_light());
  8062. list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
  8063. deadline = fcf->time + fcf->fka_period + fcf->fka_period / 2;
  8064. @@ -867,7 +867,7 @@
  8065. sel_time = fcf->time;
  8066. }
  8067. }
  8068. - put_cpu();
  8069. + put_cpu_light();
  8070. list_for_each_entry_safe(fcf, next, &del_list, list) {
  8071. /* Removes fcf from current list */
  8072. diff -Nur linux-4.1.20.orig/drivers/scsi/libfc/fc_exch.c linux-4.1.20/drivers/scsi/libfc/fc_exch.c
  8073. --- linux-4.1.20.orig/drivers/scsi/libfc/fc_exch.c 2016-03-17 19:11:03.000000000 +0100
  8074. +++ linux-4.1.20/drivers/scsi/libfc/fc_exch.c 2016-03-21 20:18:32.000000000 +0100
  8075. @@ -814,10 +814,10 @@
  8076. }
  8077. memset(ep, 0, sizeof(*ep));
  8078. - cpu = get_cpu();
  8079. + cpu = get_cpu_light();
  8080. pool = per_cpu_ptr(mp->pool, cpu);
  8081. spin_lock_bh(&pool->lock);
  8082. - put_cpu();
  8083. + put_cpu_light();
  8084. /* peek cache of free slot */
  8085. if (pool->left != FC_XID_UNKNOWN) {
  8086. diff -Nur linux-4.1.20.orig/drivers/scsi/libsas/sas_ata.c linux-4.1.20/drivers/scsi/libsas/sas_ata.c
  8087. --- linux-4.1.20.orig/drivers/scsi/libsas/sas_ata.c 2016-03-17 19:11:03.000000000 +0100
  8088. +++ linux-4.1.20/drivers/scsi/libsas/sas_ata.c 2016-03-21 20:18:32.000000000 +0100
  8089. @@ -190,7 +190,7 @@
  8090. /* TODO: audit callers to ensure they are ready for qc_issue to
  8091. * unconditionally re-enable interrupts
  8092. */
  8093. - local_irq_save(flags);
  8094. + local_irq_save_nort(flags);
  8095. spin_unlock(ap->lock);
  8096. /* If the device fell off, no sense in issuing commands */
  8097. @@ -255,7 +255,7 @@
  8098. out:
  8099. spin_lock(ap->lock);
  8100. - local_irq_restore(flags);
  8101. + local_irq_restore_nort(flags);
  8102. return ret;
  8103. }
  8104. diff -Nur linux-4.1.20.orig/drivers/scsi/qla2xxx/qla_inline.h linux-4.1.20/drivers/scsi/qla2xxx/qla_inline.h
  8105. --- linux-4.1.20.orig/drivers/scsi/qla2xxx/qla_inline.h 2016-03-17 19:11:03.000000000 +0100
  8106. +++ linux-4.1.20/drivers/scsi/qla2xxx/qla_inline.h 2016-03-21 20:18:32.000000000 +0100
  8107. @@ -59,12 +59,12 @@
  8108. {
  8109. unsigned long flags;
  8110. struct qla_hw_data *ha = rsp->hw;
  8111. - local_irq_save(flags);
  8112. + local_irq_save_nort(flags);
  8113. if (IS_P3P_TYPE(ha))
  8114. qla82xx_poll(0, rsp);
  8115. else
  8116. ha->isp_ops->intr_handler(0, rsp);
  8117. - local_irq_restore(flags);
  8118. + local_irq_restore_nort(flags);
  8119. }
  8120. static inline uint8_t *
  8121. 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
  8122. --- linux-4.1.20.orig/drivers/thermal/x86_pkg_temp_thermal.c 2016-03-17 19:11:03.000000000 +0100
  8123. +++ linux-4.1.20/drivers/thermal/x86_pkg_temp_thermal.c 2016-03-21 20:18:32.000000000 +0100
  8124. @@ -29,6 +29,7 @@
  8125. #include <linux/pm.h>
  8126. #include <linux/thermal.h>
  8127. #include <linux/debugfs.h>
  8128. +#include <linux/work-simple.h>
  8129. #include <asm/cpu_device_id.h>
  8130. #include <asm/mce.h>
  8131. @@ -352,7 +353,7 @@
  8132. }
  8133. }
  8134. -static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  8135. +static void platform_thermal_notify_work(struct swork_event *event)
  8136. {
  8137. unsigned long flags;
  8138. int cpu = smp_processor_id();
  8139. @@ -369,7 +370,7 @@
  8140. pkg_work_scheduled[phy_id]) {
  8141. disable_pkg_thres_interrupt();
  8142. spin_unlock_irqrestore(&pkg_work_lock, flags);
  8143. - return -EINVAL;
  8144. + return;
  8145. }
  8146. pkg_work_scheduled[phy_id] = 1;
  8147. spin_unlock_irqrestore(&pkg_work_lock, flags);
  8148. @@ -378,9 +379,48 @@
  8149. schedule_delayed_work_on(cpu,
  8150. &per_cpu(pkg_temp_thermal_threshold_work, cpu),
  8151. msecs_to_jiffies(notify_delay_ms));
  8152. +}
  8153. +
  8154. +#ifdef CONFIG_PREEMPT_RT_FULL
  8155. +static struct swork_event notify_work;
  8156. +
  8157. +static int thermal_notify_work_init(void)
  8158. +{
  8159. + int err;
  8160. +
  8161. + err = swork_get();
  8162. + if (err)
  8163. + return err;
  8164. +
  8165. + INIT_SWORK(&notify_work, platform_thermal_notify_work);
  8166. return 0;
  8167. }
  8168. +static void thermal_notify_work_cleanup(void)
  8169. +{
  8170. + swork_put();
  8171. +}
  8172. +
  8173. +static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  8174. +{
  8175. + swork_queue(&notify_work);
  8176. + return 0;
  8177. +}
  8178. +
  8179. +#else /* !CONFIG_PREEMPT_RT_FULL */
  8180. +
  8181. +static int thermal_notify_work_init(void) { return 0; }
  8182. +
  8183. +static void thermal_notify_work_cleanup(void) { }
  8184. +
  8185. +static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  8186. +{
  8187. + platform_thermal_notify_work(NULL);
  8188. +
  8189. + return 0;
  8190. +}
  8191. +#endif /* CONFIG_PREEMPT_RT_FULL */
  8192. +
  8193. static int find_siblings_cpu(int cpu)
  8194. {
  8195. int i;
  8196. @@ -584,6 +624,9 @@
  8197. if (!x86_match_cpu(pkg_temp_thermal_ids))
  8198. return -ENODEV;
  8199. + if (!thermal_notify_work_init())
  8200. + return -ENODEV;
  8201. +
  8202. spin_lock_init(&pkg_work_lock);
  8203. platform_thermal_package_notify =
  8204. pkg_temp_thermal_platform_thermal_notify;
  8205. @@ -608,7 +651,7 @@
  8206. kfree(pkg_work_scheduled);
  8207. platform_thermal_package_notify = NULL;
  8208. platform_thermal_package_rate_control = NULL;
  8209. -
  8210. + thermal_notify_work_cleanup();
  8211. return -ENODEV;
  8212. }
  8213. @@ -633,6 +676,7 @@
  8214. mutex_unlock(&phy_dev_list_mutex);
  8215. platform_thermal_package_notify = NULL;
  8216. platform_thermal_package_rate_control = NULL;
  8217. + thermal_notify_work_cleanup();
  8218. for_each_online_cpu(i)
  8219. cancel_delayed_work_sync(
  8220. &per_cpu(pkg_temp_thermal_threshold_work, i));
  8221. 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
  8222. --- linux-4.1.20.orig/drivers/tty/serial/8250/8250_core.c 2016-03-17 19:11:03.000000000 +0100
  8223. +++ linux-4.1.20/drivers/tty/serial/8250/8250_core.c 2016-03-21 20:18:32.000000000 +0100
  8224. @@ -36,6 +36,7 @@
  8225. #include <linux/nmi.h>
  8226. #include <linux/mutex.h>
  8227. #include <linux/slab.h>
  8228. +#include <linux/kdb.h>
  8229. #include <linux/uaccess.h>
  8230. #include <linux/pm_runtime.h>
  8231. #ifdef CONFIG_SPARC
  8232. @@ -80,7 +81,16 @@
  8233. #define DEBUG_INTR(fmt...) do { } while (0)
  8234. #endif
  8235. -#define PASS_LIMIT 512
  8236. +/*
  8237. + * On -rt we can have a more delays, and legitimately
  8238. + * so - so don't drop work spuriously and spam the
  8239. + * syslog:
  8240. + */
  8241. +#ifdef CONFIG_PREEMPT_RT_FULL
  8242. +# define PASS_LIMIT 1000000
  8243. +#else
  8244. +# define PASS_LIMIT 512
  8245. +#endif
  8246. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  8247. @@ -3372,7 +3382,7 @@
  8248. if (port->sysrq)
  8249. locked = 0;
  8250. - else if (oops_in_progress)
  8251. + else if (oops_in_progress || in_kdb_printk())
  8252. locked = spin_trylock_irqsave(&port->lock, flags);
  8253. else
  8254. spin_lock_irqsave(&port->lock, flags);
  8255. diff -Nur linux-4.1.20.orig/drivers/tty/serial/amba-pl011.c linux-4.1.20/drivers/tty/serial/amba-pl011.c
  8256. --- linux-4.1.20.orig/drivers/tty/serial/amba-pl011.c 2016-03-17 19:11:03.000000000 +0100
  8257. +++ linux-4.1.20/drivers/tty/serial/amba-pl011.c 2016-03-21 20:18:32.000000000 +0100
  8258. @@ -2000,13 +2000,19 @@
  8259. clk_enable(uap->clk);
  8260. - local_irq_save(flags);
  8261. + /*
  8262. + * local_irq_save(flags);
  8263. + *
  8264. + * This local_irq_save() is nonsense. If we come in via sysrq
  8265. + * handling then interrupts are already disabled. Aside of
  8266. + * that the port.sysrq check is racy on SMP regardless.
  8267. + */
  8268. if (uap->port.sysrq)
  8269. locked = 0;
  8270. else if (oops_in_progress)
  8271. - locked = spin_trylock(&uap->port.lock);
  8272. + locked = spin_trylock_irqsave(&uap->port.lock, flags);
  8273. else
  8274. - spin_lock(&uap->port.lock);
  8275. + spin_lock_irqsave(&uap->port.lock, flags);
  8276. /*
  8277. * First save the CR then disable the interrupts
  8278. @@ -2028,8 +2034,7 @@
  8279. writew(old_cr, uap->port.membase + UART011_CR);
  8280. if (locked)
  8281. - spin_unlock(&uap->port.lock);
  8282. - local_irq_restore(flags);
  8283. + spin_unlock_irqrestore(&uap->port.lock, flags);
  8284. clk_disable(uap->clk);
  8285. }
  8286. diff -Nur linux-4.1.20.orig/drivers/tty/serial/omap-serial.c linux-4.1.20/drivers/tty/serial/omap-serial.c
  8287. --- linux-4.1.20.orig/drivers/tty/serial/omap-serial.c 2016-03-17 19:11:03.000000000 +0100
  8288. +++ linux-4.1.20/drivers/tty/serial/omap-serial.c 2016-03-21 20:18:32.000000000 +0100
  8289. @@ -1282,13 +1282,10 @@
  8290. pm_runtime_get_sync(up->dev);
  8291. - local_irq_save(flags);
  8292. - if (up->port.sysrq)
  8293. - locked = 0;
  8294. - else if (oops_in_progress)
  8295. - locked = spin_trylock(&up->port.lock);
  8296. + if (up->port.sysrq || oops_in_progress)
  8297. + locked = spin_trylock_irqsave(&up->port.lock, flags);
  8298. else
  8299. - spin_lock(&up->port.lock);
  8300. + spin_lock_irqsave(&up->port.lock, flags);
  8301. /*
  8302. * First save the IER then disable the interrupts
  8303. @@ -1317,8 +1314,7 @@
  8304. pm_runtime_mark_last_busy(up->dev);
  8305. pm_runtime_put_autosuspend(up->dev);
  8306. if (locked)
  8307. - spin_unlock(&up->port.lock);
  8308. - local_irq_restore(flags);
  8309. + spin_unlock_irqrestore(&up->port.lock, flags);
  8310. }
  8311. static int __init
  8312. diff -Nur linux-4.1.20.orig/drivers/usb/core/hcd.c linux-4.1.20/drivers/usb/core/hcd.c
  8313. --- linux-4.1.20.orig/drivers/usb/core/hcd.c 2016-03-17 19:11:03.000000000 +0100
  8314. +++ linux-4.1.20/drivers/usb/core/hcd.c 2016-03-21 20:18:32.000000000 +0100
  8315. @@ -1684,9 +1684,9 @@
  8316. * and no one may trigger the above deadlock situation when
  8317. * running complete() in tasklet.
  8318. */
  8319. - local_irq_save(flags);
  8320. + local_irq_save_nort(flags);
  8321. urb->complete(urb);
  8322. - local_irq_restore(flags);
  8323. + local_irq_restore_nort(flags);
  8324. usb_anchor_resume_wakeups(anchor);
  8325. atomic_dec(&urb->use_count);
  8326. 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
  8327. --- linux-4.1.20.orig/drivers/usb/gadget/function/f_fs.c 2016-03-17 19:11:03.000000000 +0100
  8328. +++ linux-4.1.20/drivers/usb/gadget/function/f_fs.c 2016-03-21 20:18:32.000000000 +0100
  8329. @@ -1405,7 +1405,7 @@
  8330. pr_info("%s(): freeing\n", __func__);
  8331. ffs_data_clear(ffs);
  8332. BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
  8333. - waitqueue_active(&ffs->ep0req_completion.wait));
  8334. + swaitqueue_active(&ffs->ep0req_completion.wait));
  8335. kfree(ffs->dev_name);
  8336. kfree(ffs);
  8337. }
  8338. diff -Nur linux-4.1.20.orig/drivers/usb/gadget/legacy/inode.c linux-4.1.20/drivers/usb/gadget/legacy/inode.c
  8339. --- linux-4.1.20.orig/drivers/usb/gadget/legacy/inode.c 2016-03-17 19:11:03.000000000 +0100
  8340. +++ linux-4.1.20/drivers/usb/gadget/legacy/inode.c 2016-03-21 20:18:32.000000000 +0100
  8341. @@ -345,7 +345,7 @@
  8342. spin_unlock_irq (&epdata->dev->lock);
  8343. if (likely (value == 0)) {
  8344. - value = wait_event_interruptible (done.wait, done.done);
  8345. + value = swait_event_interruptible (done.wait, done.done);
  8346. if (value != 0) {
  8347. spin_lock_irq (&epdata->dev->lock);
  8348. if (likely (epdata->ep != NULL)) {
  8349. @@ -354,7 +354,7 @@
  8350. usb_ep_dequeue (epdata->ep, epdata->req);
  8351. spin_unlock_irq (&epdata->dev->lock);
  8352. - wait_event (done.wait, done.done);
  8353. + swait_event (done.wait, done.done);
  8354. if (epdata->status == -ECONNRESET)
  8355. epdata->status = -EINTR;
  8356. } else {
  8357. diff -Nur linux-4.1.20.orig/fs/aio.c linux-4.1.20/fs/aio.c
  8358. --- linux-4.1.20.orig/fs/aio.c 2016-03-17 19:11:03.000000000 +0100
  8359. +++ linux-4.1.20/fs/aio.c 2016-03-21 20:18:32.000000000 +0100
  8360. @@ -40,6 +40,7 @@
  8361. #include <linux/ramfs.h>
  8362. #include <linux/percpu-refcount.h>
  8363. #include <linux/mount.h>
  8364. +#include <linux/work-simple.h>
  8365. #include <asm/kmap_types.h>
  8366. #include <asm/uaccess.h>
  8367. @@ -115,7 +116,7 @@
  8368. struct page **ring_pages;
  8369. long nr_pages;
  8370. - struct work_struct free_work;
  8371. + struct swork_event free_work;
  8372. /*
  8373. * signals when all in-flight requests are done
  8374. @@ -253,6 +254,7 @@
  8375. .mount = aio_mount,
  8376. .kill_sb = kill_anon_super,
  8377. };
  8378. + BUG_ON(swork_get());
  8379. aio_mnt = kern_mount(&aio_fs);
  8380. if (IS_ERR(aio_mnt))
  8381. panic("Failed to create aio fs mount.");
  8382. @@ -559,9 +561,9 @@
  8383. return cancel(&kiocb->common);
  8384. }
  8385. -static void free_ioctx(struct work_struct *work)
  8386. +static void free_ioctx(struct swork_event *sev)
  8387. {
  8388. - struct kioctx *ctx = container_of(work, struct kioctx, free_work);
  8389. + struct kioctx *ctx = container_of(sev, struct kioctx, free_work);
  8390. pr_debug("freeing %p\n", ctx);
  8391. @@ -580,8 +582,8 @@
  8392. if (ctx->rq_wait && atomic_dec_and_test(&ctx->rq_wait->count))
  8393. complete(&ctx->rq_wait->comp);
  8394. - INIT_WORK(&ctx->free_work, free_ioctx);
  8395. - schedule_work(&ctx->free_work);
  8396. + INIT_SWORK(&ctx->free_work, free_ioctx);
  8397. + swork_queue(&ctx->free_work);
  8398. }
  8399. /*
  8400. @@ -589,9 +591,9 @@
  8401. * and ctx->users has dropped to 0, so we know no more kiocbs can be submitted -
  8402. * now it's safe to cancel any that need to be.
  8403. */
  8404. -static void free_ioctx_users(struct percpu_ref *ref)
  8405. +static void free_ioctx_users_work(struct swork_event *sev)
  8406. {
  8407. - struct kioctx *ctx = container_of(ref, struct kioctx, users);
  8408. + struct kioctx *ctx = container_of(sev, struct kioctx, free_work);
  8409. struct aio_kiocb *req;
  8410. spin_lock_irq(&ctx->ctx_lock);
  8411. @@ -610,6 +612,14 @@
  8412. percpu_ref_put(&ctx->reqs);
  8413. }
  8414. +static void free_ioctx_users(struct percpu_ref *ref)
  8415. +{
  8416. + struct kioctx *ctx = container_of(ref, struct kioctx, users);
  8417. +
  8418. + INIT_SWORK(&ctx->free_work, free_ioctx_users_work);
  8419. + swork_queue(&ctx->free_work);
  8420. +}
  8421. +
  8422. static int ioctx_add_table(struct kioctx *ctx, struct mm_struct *mm)
  8423. {
  8424. unsigned i, new_nr;
  8425. diff -Nur linux-4.1.20.orig/fs/autofs4/autofs_i.h linux-4.1.20/fs/autofs4/autofs_i.h
  8426. --- linux-4.1.20.orig/fs/autofs4/autofs_i.h 2016-03-17 19:11:03.000000000 +0100
  8427. +++ linux-4.1.20/fs/autofs4/autofs_i.h 2016-03-21 20:18:32.000000000 +0100
  8428. @@ -34,6 +34,7 @@
  8429. #include <linux/sched.h>
  8430. #include <linux/mount.h>
  8431. #include <linux/namei.h>
  8432. +#include <linux/delay.h>
  8433. #include <asm/current.h>
  8434. #include <asm/uaccess.h>
  8435. diff -Nur linux-4.1.20.orig/fs/autofs4/expire.c linux-4.1.20/fs/autofs4/expire.c
  8436. --- linux-4.1.20.orig/fs/autofs4/expire.c 2016-03-17 19:11:03.000000000 +0100
  8437. +++ linux-4.1.20/fs/autofs4/expire.c 2016-03-21 20:18:32.000000000 +0100
  8438. @@ -150,7 +150,7 @@
  8439. parent = p->d_parent;
  8440. if (!spin_trylock(&parent->d_lock)) {
  8441. spin_unlock(&p->d_lock);
  8442. - cpu_relax();
  8443. + cpu_chill();
  8444. goto relock;
  8445. }
  8446. spin_unlock(&p->d_lock);
  8447. diff -Nur linux-4.1.20.orig/fs/buffer.c linux-4.1.20/fs/buffer.c
  8448. --- linux-4.1.20.orig/fs/buffer.c 2016-03-17 19:11:03.000000000 +0100
  8449. +++ linux-4.1.20/fs/buffer.c 2016-03-21 20:18:32.000000000 +0100
  8450. @@ -301,8 +301,7 @@
  8451. * decide that the page is now completely done.
  8452. */
  8453. first = page_buffers(page);
  8454. - local_irq_save(flags);
  8455. - bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  8456. + flags = bh_uptodate_lock_irqsave(first);
  8457. clear_buffer_async_read(bh);
  8458. unlock_buffer(bh);
  8459. tmp = bh;
  8460. @@ -315,8 +314,7 @@
  8461. }
  8462. tmp = tmp->b_this_page;
  8463. } while (tmp != bh);
  8464. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  8465. - local_irq_restore(flags);
  8466. + bh_uptodate_unlock_irqrestore(first, flags);
  8467. /*
  8468. * If none of the buffers had errors and they are all
  8469. @@ -328,9 +326,7 @@
  8470. return;
  8471. still_busy:
  8472. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  8473. - local_irq_restore(flags);
  8474. - return;
  8475. + bh_uptodate_unlock_irqrestore(first, flags);
  8476. }
  8477. /*
  8478. @@ -358,8 +354,7 @@
  8479. }
  8480. first = page_buffers(page);
  8481. - local_irq_save(flags);
  8482. - bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  8483. + flags = bh_uptodate_lock_irqsave(first);
  8484. clear_buffer_async_write(bh);
  8485. unlock_buffer(bh);
  8486. @@ -371,15 +366,12 @@
  8487. }
  8488. tmp = tmp->b_this_page;
  8489. }
  8490. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  8491. - local_irq_restore(flags);
  8492. + bh_uptodate_unlock_irqrestore(first, flags);
  8493. end_page_writeback(page);
  8494. return;
  8495. still_busy:
  8496. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  8497. - local_irq_restore(flags);
  8498. - return;
  8499. + bh_uptodate_unlock_irqrestore(first, flags);
  8500. }
  8501. EXPORT_SYMBOL(end_buffer_async_write);
  8502. @@ -3325,6 +3317,7 @@
  8503. struct buffer_head *ret = kmem_cache_zalloc(bh_cachep, gfp_flags);
  8504. if (ret) {
  8505. INIT_LIST_HEAD(&ret->b_assoc_buffers);
  8506. + buffer_head_init_locks(ret);
  8507. preempt_disable();
  8508. __this_cpu_inc(bh_accounting.nr);
  8509. recalc_bh_state();
  8510. diff -Nur linux-4.1.20.orig/fs/dcache.c linux-4.1.20/fs/dcache.c
  8511. --- linux-4.1.20.orig/fs/dcache.c 2016-03-17 19:11:03.000000000 +0100
  8512. +++ linux-4.1.20/fs/dcache.c 2016-03-21 20:18:32.000000000 +0100
  8513. @@ -19,6 +19,7 @@
  8514. #include <linux/mm.h>
  8515. #include <linux/fs.h>
  8516. #include <linux/fsnotify.h>
  8517. +#include <linux/delay.h>
  8518. #include <linux/slab.h>
  8519. #include <linux/init.h>
  8520. #include <linux/hash.h>
  8521. @@ -578,7 +579,7 @@
  8522. failed:
  8523. spin_unlock(&dentry->d_lock);
  8524. - cpu_relax();
  8525. + cpu_chill();
  8526. return dentry; /* try again with same dentry */
  8527. }
  8528. @@ -2388,7 +2389,7 @@
  8529. if (dentry->d_lockref.count == 1) {
  8530. if (!spin_trylock(&inode->i_lock)) {
  8531. spin_unlock(&dentry->d_lock);
  8532. - cpu_relax();
  8533. + cpu_chill();
  8534. goto again;
  8535. }
  8536. dentry->d_flags &= ~DCACHE_CANT_MOUNT;
  8537. diff -Nur linux-4.1.20.orig/fs/eventpoll.c linux-4.1.20/fs/eventpoll.c
  8538. --- linux-4.1.20.orig/fs/eventpoll.c 2016-03-17 19:11:03.000000000 +0100
  8539. +++ linux-4.1.20/fs/eventpoll.c 2016-03-21 20:18:32.000000000 +0100
  8540. @@ -505,12 +505,12 @@
  8541. */
  8542. static void ep_poll_safewake(wait_queue_head_t *wq)
  8543. {
  8544. - int this_cpu = get_cpu();
  8545. + int this_cpu = get_cpu_light();
  8546. ep_call_nested(&poll_safewake_ncalls, EP_MAX_NESTS,
  8547. ep_poll_wakeup_proc, NULL, wq, (void *) (long) this_cpu);
  8548. - put_cpu();
  8549. + put_cpu_light();
  8550. }
  8551. static void ep_remove_wait_queue(struct eppoll_entry *pwq)
  8552. diff -Nur linux-4.1.20.orig/fs/exec.c linux-4.1.20/fs/exec.c
  8553. --- linux-4.1.20.orig/fs/exec.c 2016-03-17 19:11:03.000000000 +0100
  8554. +++ linux-4.1.20/fs/exec.c 2016-03-21 20:18:32.000000000 +0100
  8555. @@ -859,12 +859,14 @@
  8556. }
  8557. }
  8558. task_lock(tsk);
  8559. + preempt_disable_rt();
  8560. active_mm = tsk->active_mm;
  8561. tsk->mm = mm;
  8562. tsk->active_mm = mm;
  8563. activate_mm(active_mm, mm);
  8564. tsk->mm->vmacache_seqnum = 0;
  8565. vmacache_flush(tsk);
  8566. + preempt_enable_rt();
  8567. task_unlock(tsk);
  8568. if (old_mm) {
  8569. up_read(&old_mm->mmap_sem);
  8570. diff -Nur linux-4.1.20.orig/fs/f2fs/f2fs.h linux-4.1.20/fs/f2fs/f2fs.h
  8571. --- linux-4.1.20.orig/fs/f2fs/f2fs.h 2016-03-17 19:11:03.000000000 +0100
  8572. +++ linux-4.1.20/fs/f2fs/f2fs.h 2016-03-21 20:18:32.000000000 +0100
  8573. @@ -22,7 +22,6 @@
  8574. #ifdef CONFIG_F2FS_CHECK_FS
  8575. #define f2fs_bug_on(sbi, condition) BUG_ON(condition)
  8576. -#define f2fs_down_write(x, y) down_write_nest_lock(x, y)
  8577. #else
  8578. #define f2fs_bug_on(sbi, condition) \
  8579. do { \
  8580. @@ -31,7 +30,6 @@
  8581. set_sbi_flag(sbi, SBI_NEED_FSCK); \
  8582. } \
  8583. } while (0)
  8584. -#define f2fs_down_write(x, y) down_write(x)
  8585. #endif
  8586. /*
  8587. @@ -838,7 +836,7 @@
  8588. static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
  8589. {
  8590. - f2fs_down_write(&sbi->cp_rwsem, &sbi->cp_mutex);
  8591. + down_write(&sbi->cp_rwsem);
  8592. }
  8593. static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
  8594. diff -Nur linux-4.1.20.orig/fs/jbd/checkpoint.c linux-4.1.20/fs/jbd/checkpoint.c
  8595. --- linux-4.1.20.orig/fs/jbd/checkpoint.c 2016-03-17 19:11:03.000000000 +0100
  8596. +++ linux-4.1.20/fs/jbd/checkpoint.c 2016-03-21 20:18:32.000000000 +0100
  8597. @@ -129,6 +129,8 @@
  8598. if (journal->j_flags & JFS_ABORT)
  8599. return;
  8600. spin_unlock(&journal->j_state_lock);
  8601. + if (current->plug)
  8602. + io_schedule();
  8603. mutex_lock(&journal->j_checkpoint_mutex);
  8604. /*
  8605. diff -Nur linux-4.1.20.orig/fs/jbd2/checkpoint.c linux-4.1.20/fs/jbd2/checkpoint.c
  8606. --- linux-4.1.20.orig/fs/jbd2/checkpoint.c 2016-03-17 19:11:03.000000000 +0100
  8607. +++ linux-4.1.20/fs/jbd2/checkpoint.c 2016-03-21 20:18:32.000000000 +0100
  8608. @@ -116,6 +116,8 @@
  8609. nblocks = jbd2_space_needed(journal);
  8610. while (jbd2_log_space_left(journal) < nblocks) {
  8611. write_unlock(&journal->j_state_lock);
  8612. + if (current->plug)
  8613. + io_schedule();
  8614. mutex_lock(&journal->j_checkpoint_mutex);
  8615. /*
  8616. diff -Nur linux-4.1.20.orig/fs/namespace.c linux-4.1.20/fs/namespace.c
  8617. --- linux-4.1.20.orig/fs/namespace.c 2016-03-17 19:11:03.000000000 +0100
  8618. +++ linux-4.1.20/fs/namespace.c 2016-03-21 20:18:32.000000000 +0100
  8619. @@ -14,6 +14,7 @@
  8620. #include <linux/mnt_namespace.h>
  8621. #include <linux/user_namespace.h>
  8622. #include <linux/namei.h>
  8623. +#include <linux/delay.h>
  8624. #include <linux/security.h>
  8625. #include <linux/idr.h>
  8626. #include <linux/init.h> /* init_rootfs */
  8627. @@ -353,8 +354,11 @@
  8628. * incremented count after it has set MNT_WRITE_HOLD.
  8629. */
  8630. smp_mb();
  8631. - while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD)
  8632. - cpu_relax();
  8633. + while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD) {
  8634. + preempt_enable();
  8635. + cpu_chill();
  8636. + preempt_disable();
  8637. + }
  8638. /*
  8639. * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
  8640. * be set to match its requirements. So we must not load that until
  8641. diff -Nur linux-4.1.20.orig/fs/ntfs/aops.c linux-4.1.20/fs/ntfs/aops.c
  8642. --- linux-4.1.20.orig/fs/ntfs/aops.c 2016-03-17 19:11:03.000000000 +0100
  8643. +++ linux-4.1.20/fs/ntfs/aops.c 2016-03-21 20:18:32.000000000 +0100
  8644. @@ -107,8 +107,7 @@
  8645. "0x%llx.", (unsigned long long)bh->b_blocknr);
  8646. }
  8647. first = page_buffers(page);
  8648. - local_irq_save(flags);
  8649. - bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  8650. + flags = bh_uptodate_lock_irqsave(first);
  8651. clear_buffer_async_read(bh);
  8652. unlock_buffer(bh);
  8653. tmp = bh;
  8654. @@ -123,8 +122,7 @@
  8655. }
  8656. tmp = tmp->b_this_page;
  8657. } while (tmp != bh);
  8658. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  8659. - local_irq_restore(flags);
  8660. + bh_uptodate_unlock_irqrestore(first, flags);
  8661. /*
  8662. * If none of the buffers had errors then we can set the page uptodate,
  8663. * but we first have to perform the post read mst fixups, if the
  8664. @@ -145,13 +143,13 @@
  8665. recs = PAGE_CACHE_SIZE / rec_size;
  8666. /* Should have been verified before we got here... */
  8667. BUG_ON(!recs);
  8668. - local_irq_save(flags);
  8669. + local_irq_save_nort(flags);
  8670. kaddr = kmap_atomic(page);
  8671. for (i = 0; i < recs; i++)
  8672. post_read_mst_fixup((NTFS_RECORD*)(kaddr +
  8673. i * rec_size), rec_size);
  8674. kunmap_atomic(kaddr);
  8675. - local_irq_restore(flags);
  8676. + local_irq_restore_nort(flags);
  8677. flush_dcache_page(page);
  8678. if (likely(page_uptodate && !PageError(page)))
  8679. SetPageUptodate(page);
  8680. @@ -159,9 +157,7 @@
  8681. unlock_page(page);
  8682. return;
  8683. still_busy:
  8684. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  8685. - local_irq_restore(flags);
  8686. - return;
  8687. + bh_uptodate_unlock_irqrestore(first, flags);
  8688. }
  8689. /**
  8690. diff -Nur linux-4.1.20.orig/fs/timerfd.c linux-4.1.20/fs/timerfd.c
  8691. --- linux-4.1.20.orig/fs/timerfd.c 2016-03-17 19:11:03.000000000 +0100
  8692. +++ linux-4.1.20/fs/timerfd.c 2016-03-21 20:18:32.000000000 +0100
  8693. @@ -450,7 +450,10 @@
  8694. break;
  8695. }
  8696. spin_unlock_irq(&ctx->wqh.lock);
  8697. - cpu_relax();
  8698. + if (isalarm(ctx))
  8699. + hrtimer_wait_for_timer(&ctx->t.alarm.timer);
  8700. + else
  8701. + hrtimer_wait_for_timer(&ctx->t.tmr);
  8702. }
  8703. /*
  8704. diff -Nur linux-4.1.20.orig/fs/xfs/xfs_inode.c linux-4.1.20/fs/xfs/xfs_inode.c
  8705. --- linux-4.1.20.orig/fs/xfs/xfs_inode.c 2016-03-17 19:11:03.000000000 +0100
  8706. +++ linux-4.1.20/fs/xfs/xfs_inode.c 2016-03-21 20:18:32.000000000 +0100
  8707. @@ -164,7 +164,7 @@
  8708. (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
  8709. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  8710. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  8711. - ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
  8712. + ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
  8713. if (lock_flags & XFS_IOLOCK_EXCL)
  8714. mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
  8715. @@ -212,7 +212,7 @@
  8716. (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
  8717. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  8718. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  8719. - ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
  8720. + ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
  8721. if (lock_flags & XFS_IOLOCK_EXCL) {
  8722. if (!mrtryupdate(&ip->i_iolock))
  8723. @@ -281,7 +281,7 @@
  8724. (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
  8725. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  8726. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  8727. - ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
  8728. + ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
  8729. ASSERT(lock_flags != 0);
  8730. if (lock_flags & XFS_IOLOCK_EXCL)
  8731. @@ -364,30 +364,38 @@
  8732. /*
  8733. * Bump the subclass so xfs_lock_inodes() acquires each lock with a different
  8734. - * value. This shouldn't be called for page fault locking, but we also need to
  8735. - * ensure we don't overrun the number of lockdep subclasses for the iolock or
  8736. - * mmaplock as that is limited to 12 by the mmap lock lockdep annotations.
  8737. + * value. This can be called for any type of inode lock combination, including
  8738. + * parent locking. Care must be taken to ensure we don't overrun the subclass
  8739. + * storage fields in the class mask we build.
  8740. */
  8741. static inline int
  8742. xfs_lock_inumorder(int lock_mode, int subclass)
  8743. {
  8744. + int class = 0;
  8745. +
  8746. + ASSERT(!(lock_mode & (XFS_ILOCK_PARENT | XFS_ILOCK_RTBITMAP |
  8747. + XFS_ILOCK_RTSUM)));
  8748. +
  8749. if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) {
  8750. - ASSERT(subclass + XFS_LOCK_INUMORDER <
  8751. - (1 << (XFS_MMAPLOCK_SHIFT - XFS_IOLOCK_SHIFT)));
  8752. - lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
  8753. + ASSERT(subclass <= XFS_IOLOCK_MAX_SUBCLASS);
  8754. + ASSERT(subclass + XFS_IOLOCK_PARENT_VAL <
  8755. + MAX_LOCKDEP_SUBCLASSES);
  8756. + class += subclass << XFS_IOLOCK_SHIFT;
  8757. + if (lock_mode & XFS_IOLOCK_PARENT)
  8758. + class += XFS_IOLOCK_PARENT_VAL << XFS_IOLOCK_SHIFT;
  8759. }
  8760. if (lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) {
  8761. - ASSERT(subclass + XFS_LOCK_INUMORDER <
  8762. - (1 << (XFS_ILOCK_SHIFT - XFS_MMAPLOCK_SHIFT)));
  8763. - lock_mode |= (subclass + XFS_LOCK_INUMORDER) <<
  8764. - XFS_MMAPLOCK_SHIFT;
  8765. + ASSERT(subclass <= XFS_MMAPLOCK_MAX_SUBCLASS);
  8766. + class += subclass << XFS_MMAPLOCK_SHIFT;
  8767. }
  8768. - if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
  8769. - lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
  8770. + if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) {
  8771. + ASSERT(subclass <= XFS_ILOCK_MAX_SUBCLASS);
  8772. + class += subclass << XFS_ILOCK_SHIFT;
  8773. + }
  8774. - return lock_mode;
  8775. + return (lock_mode & ~XFS_LOCK_SUBCLASS_MASK) | class;
  8776. }
  8777. /*
  8778. @@ -399,6 +407,11 @@
  8779. * transaction (such as truncate). This can result in deadlock since the long
  8780. * running trans might need to wait for the inode we just locked in order to
  8781. * push the tail and free space in the log.
  8782. + *
  8783. + * xfs_lock_inodes() can only be used to lock one type of lock at a time -
  8784. + * the iolock, the mmaplock or the ilock, but not more than one at a time. If we
  8785. + * lock more than one at a time, lockdep will report false positives saying we
  8786. + * have violated locking orders.
  8787. */
  8788. void
  8789. xfs_lock_inodes(
  8790. @@ -409,8 +422,29 @@
  8791. int attempts = 0, i, j, try_lock;
  8792. xfs_log_item_t *lp;
  8793. - /* currently supports between 2 and 5 inodes */
  8794. + /*
  8795. + * Currently supports between 2 and 5 inodes with exclusive locking. We
  8796. + * support an arbitrary depth of locking here, but absolute limits on
  8797. + * inodes depend on the the type of locking and the limits placed by
  8798. + * lockdep annotations in xfs_lock_inumorder. These are all checked by
  8799. + * the asserts.
  8800. + */
  8801. ASSERT(ips && inodes >= 2 && inodes <= 5);
  8802. + ASSERT(lock_mode & (XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL |
  8803. + XFS_ILOCK_EXCL));
  8804. + ASSERT(!(lock_mode & (XFS_IOLOCK_SHARED | XFS_MMAPLOCK_SHARED |
  8805. + XFS_ILOCK_SHARED)));
  8806. + ASSERT(!(lock_mode & XFS_IOLOCK_EXCL) ||
  8807. + inodes <= XFS_IOLOCK_MAX_SUBCLASS + 1);
  8808. + ASSERT(!(lock_mode & XFS_MMAPLOCK_EXCL) ||
  8809. + inodes <= XFS_MMAPLOCK_MAX_SUBCLASS + 1);
  8810. + ASSERT(!(lock_mode & XFS_ILOCK_EXCL) ||
  8811. + inodes <= XFS_ILOCK_MAX_SUBCLASS + 1);
  8812. +
  8813. + if (lock_mode & XFS_IOLOCK_EXCL) {
  8814. + ASSERT(!(lock_mode & (XFS_MMAPLOCK_EXCL | XFS_ILOCK_EXCL)));
  8815. + } else if (lock_mode & XFS_MMAPLOCK_EXCL)
  8816. + ASSERT(!(lock_mode & XFS_ILOCK_EXCL));
  8817. try_lock = 0;
  8818. i = 0;
  8819. diff -Nur linux-4.1.20.orig/fs/xfs/xfs_inode.h linux-4.1.20/fs/xfs/xfs_inode.h
  8820. --- linux-4.1.20.orig/fs/xfs/xfs_inode.h 2016-03-17 19:11:03.000000000 +0100
  8821. +++ linux-4.1.20/fs/xfs/xfs_inode.h 2016-03-21 20:18:32.000000000 +0100
  8822. @@ -284,9 +284,9 @@
  8823. * Flags for lockdep annotations.
  8824. *
  8825. * XFS_LOCK_PARENT - for directory operations that require locking a
  8826. - * parent directory inode and a child entry inode. The parent gets locked
  8827. - * with this flag so it gets a lockdep subclass of 1 and the child entry
  8828. - * lock will have a lockdep subclass of 0.
  8829. + * parent directory inode and a child entry inode. IOLOCK requires nesting,
  8830. + * MMAPLOCK does not support this class, ILOCK requires a single subclass
  8831. + * to differentiate parent from child.
  8832. *
  8833. * XFS_LOCK_RTBITMAP/XFS_LOCK_RTSUM - the realtime device bitmap and summary
  8834. * inodes do not participate in the normal lock order, and thus have their
  8835. @@ -295,30 +295,63 @@
  8836. * XFS_LOCK_INUMORDER - for locking several inodes at the some time
  8837. * with xfs_lock_inodes(). This flag is used as the starting subclass
  8838. * and each subsequent lock acquired will increment the subclass by one.
  8839. - * So the first lock acquired will have a lockdep subclass of 4, the
  8840. - * second lock will have a lockdep subclass of 5, and so on. It is
  8841. - * the responsibility of the class builder to shift this to the correct
  8842. - * portion of the lock_mode lockdep mask.
  8843. + * However, MAX_LOCKDEP_SUBCLASSES == 8, which means we are greatly
  8844. + * limited to the subclasses we can represent via nesting. We need at least
  8845. + * 5 inodes nest depth for the ILOCK through rename, and we also have to support
  8846. + * XFS_ILOCK_PARENT, which gives 6 subclasses. Then we have XFS_ILOCK_RTBITMAP
  8847. + * and XFS_ILOCK_RTSUM, which are another 2 unique subclasses, so that's all
  8848. + * 8 subclasses supported by lockdep.
  8849. + *
  8850. + * This also means we have to number the sub-classes in the lowest bits of
  8851. + * the mask we keep, and we have to ensure we never exceed 3 bits of lockdep
  8852. + * mask and we can't use bit-masking to build the subclasses. What a mess.
  8853. + *
  8854. + * Bit layout:
  8855. + *
  8856. + * Bit Lock Region
  8857. + * 16-19 XFS_IOLOCK_SHIFT dependencies
  8858. + * 20-23 XFS_MMAPLOCK_SHIFT dependencies
  8859. + * 24-31 XFS_ILOCK_SHIFT dependencies
  8860. + *
  8861. + * IOLOCK values
  8862. + *
  8863. + * 0-3 subclass value
  8864. + * 4-7 PARENT subclass values
  8865. + *
  8866. + * MMAPLOCK values
  8867. + *
  8868. + * 0-3 subclass value
  8869. + * 4-7 unused
  8870. + *
  8871. + * ILOCK values
  8872. + * 0-4 subclass values
  8873. + * 5 PARENT subclass (not nestable)
  8874. + * 6 RTBITMAP subclass (not nestable)
  8875. + * 7 RTSUM subclass (not nestable)
  8876. + *
  8877. */
  8878. -#define XFS_LOCK_PARENT 1
  8879. -#define XFS_LOCK_RTBITMAP 2
  8880. -#define XFS_LOCK_RTSUM 3
  8881. -#define XFS_LOCK_INUMORDER 4
  8882. -
  8883. -#define XFS_IOLOCK_SHIFT 16
  8884. -#define XFS_IOLOCK_PARENT (XFS_LOCK_PARENT << XFS_IOLOCK_SHIFT)
  8885. -
  8886. -#define XFS_MMAPLOCK_SHIFT 20
  8887. -
  8888. -#define XFS_ILOCK_SHIFT 24
  8889. -#define XFS_ILOCK_PARENT (XFS_LOCK_PARENT << XFS_ILOCK_SHIFT)
  8890. -#define XFS_ILOCK_RTBITMAP (XFS_LOCK_RTBITMAP << XFS_ILOCK_SHIFT)
  8891. -#define XFS_ILOCK_RTSUM (XFS_LOCK_RTSUM << XFS_ILOCK_SHIFT)
  8892. -
  8893. -#define XFS_IOLOCK_DEP_MASK 0x000f0000
  8894. -#define XFS_MMAPLOCK_DEP_MASK 0x00f00000
  8895. -#define XFS_ILOCK_DEP_MASK 0xff000000
  8896. -#define XFS_LOCK_DEP_MASK (XFS_IOLOCK_DEP_MASK | \
  8897. +#define XFS_IOLOCK_SHIFT 16
  8898. +#define XFS_IOLOCK_PARENT_VAL 4
  8899. +#define XFS_IOLOCK_MAX_SUBCLASS (XFS_IOLOCK_PARENT_VAL - 1)
  8900. +#define XFS_IOLOCK_DEP_MASK 0x000f0000
  8901. +#define XFS_IOLOCK_PARENT (XFS_IOLOCK_PARENT_VAL << XFS_IOLOCK_SHIFT)
  8902. +
  8903. +#define XFS_MMAPLOCK_SHIFT 20
  8904. +#define XFS_MMAPLOCK_NUMORDER 0
  8905. +#define XFS_MMAPLOCK_MAX_SUBCLASS 3
  8906. +#define XFS_MMAPLOCK_DEP_MASK 0x00f00000
  8907. +
  8908. +#define XFS_ILOCK_SHIFT 24
  8909. +#define XFS_ILOCK_PARENT_VAL 5
  8910. +#define XFS_ILOCK_MAX_SUBCLASS (XFS_ILOCK_PARENT_VAL - 1)
  8911. +#define XFS_ILOCK_RTBITMAP_VAL 6
  8912. +#define XFS_ILOCK_RTSUM_VAL 7
  8913. +#define XFS_ILOCK_DEP_MASK 0xff000000
  8914. +#define XFS_ILOCK_PARENT (XFS_ILOCK_PARENT_VAL << XFS_ILOCK_SHIFT)
  8915. +#define XFS_ILOCK_RTBITMAP (XFS_ILOCK_RTBITMAP_VAL << XFS_ILOCK_SHIFT)
  8916. +#define XFS_ILOCK_RTSUM (XFS_ILOCK_RTSUM_VAL << XFS_ILOCK_SHIFT)
  8917. +
  8918. +#define XFS_LOCK_SUBCLASS_MASK (XFS_IOLOCK_DEP_MASK | \
  8919. XFS_MMAPLOCK_DEP_MASK | \
  8920. XFS_ILOCK_DEP_MASK)
  8921. diff -Nur linux-4.1.20.orig/include/acpi/platform/aclinux.h linux-4.1.20/include/acpi/platform/aclinux.h
  8922. --- linux-4.1.20.orig/include/acpi/platform/aclinux.h 2016-03-17 19:11:03.000000000 +0100
  8923. +++ linux-4.1.20/include/acpi/platform/aclinux.h 2016-03-21 20:18:32.000000000 +0100
  8924. @@ -123,6 +123,7 @@
  8925. #define acpi_cache_t struct kmem_cache
  8926. #define acpi_spinlock spinlock_t *
  8927. +#define acpi_raw_spinlock raw_spinlock_t *
  8928. #define acpi_cpu_flags unsigned long
  8929. /* Use native linux version of acpi_os_allocate_zeroed */
  8930. @@ -141,6 +142,20 @@
  8931. #define ACPI_USE_ALTERNATE_PROTOTYPE_acpi_os_get_thread_id
  8932. #define ACPI_USE_ALTERNATE_PROTOTYPE_acpi_os_create_lock
  8933. +#define acpi_os_create_raw_lock(__handle) \
  8934. +({ \
  8935. + raw_spinlock_t *lock = ACPI_ALLOCATE(sizeof(*lock)); \
  8936. + \
  8937. + if (lock) { \
  8938. + *(__handle) = lock; \
  8939. + raw_spin_lock_init(*(__handle)); \
  8940. + } \
  8941. + lock ? AE_OK : AE_NO_MEMORY; \
  8942. + })
  8943. +
  8944. +#define acpi_os_delete_raw_lock(__handle) kfree(__handle)
  8945. +
  8946. +
  8947. /*
  8948. * OSL interfaces used by debugger/disassembler
  8949. */
  8950. diff -Nur linux-4.1.20.orig/include/asm-generic/bug.h linux-4.1.20/include/asm-generic/bug.h
  8951. --- linux-4.1.20.orig/include/asm-generic/bug.h 2016-03-17 19:11:03.000000000 +0100
  8952. +++ linux-4.1.20/include/asm-generic/bug.h 2016-03-21 20:18:33.000000000 +0100
  8953. @@ -206,6 +206,20 @@
  8954. # define WARN_ON_SMP(x) ({0;})
  8955. #endif
  8956. +#ifdef CONFIG_PREEMPT_RT_BASE
  8957. +# define BUG_ON_RT(c) BUG_ON(c)
  8958. +# define BUG_ON_NONRT(c) do { } while (0)
  8959. +# define WARN_ON_RT(condition) WARN_ON(condition)
  8960. +# define WARN_ON_NONRT(condition) do { } while (0)
  8961. +# define WARN_ON_ONCE_NONRT(condition) do { } while (0)
  8962. +#else
  8963. +# define BUG_ON_RT(c) do { } while (0)
  8964. +# define BUG_ON_NONRT(c) BUG_ON(c)
  8965. +# define WARN_ON_RT(condition) do { } while (0)
  8966. +# define WARN_ON_NONRT(condition) WARN_ON(condition)
  8967. +# define WARN_ON_ONCE_NONRT(condition) WARN_ON_ONCE(condition)
  8968. +#endif
  8969. +
  8970. #endif /* __ASSEMBLY__ */
  8971. #endif
  8972. diff -Nur linux-4.1.20.orig/include/asm-generic/futex.h linux-4.1.20/include/asm-generic/futex.h
  8973. --- linux-4.1.20.orig/include/asm-generic/futex.h 2016-03-17 19:11:03.000000000 +0100
  8974. +++ linux-4.1.20/include/asm-generic/futex.h 2016-03-21 20:18:33.000000000 +0100
  8975. @@ -8,8 +8,7 @@
  8976. #ifndef CONFIG_SMP
  8977. /*
  8978. * The following implementation only for uniprocessor machines.
  8979. - * For UP, it's relies on the fact that pagefault_disable() also disables
  8980. - * preemption to ensure mutual exclusion.
  8981. + * It relies on preempt_disable() ensuring mutual exclusion.
  8982. *
  8983. */
  8984. @@ -38,6 +37,7 @@
  8985. if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28))
  8986. oparg = 1 << oparg;
  8987. + preempt_disable();
  8988. pagefault_disable();
  8989. ret = -EFAULT;
  8990. @@ -72,6 +72,7 @@
  8991. out_pagefault_enable:
  8992. pagefault_enable();
  8993. + preempt_enable();
  8994. if (ret == 0) {
  8995. switch (cmp) {
  8996. @@ -106,6 +107,7 @@
  8997. {
  8998. u32 val;
  8999. + preempt_disable();
  9000. if (unlikely(get_user(val, uaddr) != 0))
  9001. return -EFAULT;
  9002. @@ -113,6 +115,7 @@
  9003. return -EFAULT;
  9004. *uval = val;
  9005. + preempt_enable();
  9006. return 0;
  9007. }
  9008. diff -Nur linux-4.1.20.orig/include/linux/blk-mq.h linux-4.1.20/include/linux/blk-mq.h
  9009. --- linux-4.1.20.orig/include/linux/blk-mq.h 2016-03-17 19:11:03.000000000 +0100
  9010. +++ linux-4.1.20/include/linux/blk-mq.h 2016-03-21 20:18:33.000000000 +0100
  9011. @@ -202,6 +202,7 @@
  9012. struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *, const int ctx_index);
  9013. struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_tag_set *, unsigned int, int);
  9014. +void __blk_mq_complete_request_remote_work(struct work_struct *work);
  9015. int blk_mq_request_started(struct request *rq);
  9016. void blk_mq_start_request(struct request *rq);
  9017. diff -Nur linux-4.1.20.orig/include/linux/blkdev.h linux-4.1.20/include/linux/blkdev.h
  9018. --- linux-4.1.20.orig/include/linux/blkdev.h 2016-03-17 19:11:03.000000000 +0100
  9019. +++ linux-4.1.20/include/linux/blkdev.h 2016-03-21 20:18:33.000000000 +0100
  9020. @@ -101,6 +101,7 @@
  9021. struct list_head queuelist;
  9022. union {
  9023. struct call_single_data csd;
  9024. + struct work_struct work;
  9025. unsigned long fifo_time;
  9026. };
  9027. @@ -482,7 +483,7 @@
  9028. struct throtl_data *td;
  9029. #endif
  9030. struct rcu_head rcu_head;
  9031. - wait_queue_head_t mq_freeze_wq;
  9032. + struct swait_head mq_freeze_wq;
  9033. struct percpu_ref mq_usage_counter;
  9034. struct list_head all_q_node;
  9035. diff -Nur linux-4.1.20.orig/include/linux/bottom_half.h linux-4.1.20/include/linux/bottom_half.h
  9036. --- linux-4.1.20.orig/include/linux/bottom_half.h 2016-03-17 19:11:03.000000000 +0100
  9037. +++ linux-4.1.20/include/linux/bottom_half.h 2016-03-21 20:18:33.000000000 +0100
  9038. @@ -4,6 +4,39 @@
  9039. #include <linux/preempt.h>
  9040. #include <linux/preempt_mask.h>
  9041. +#ifdef CONFIG_PREEMPT_RT_FULL
  9042. +
  9043. +extern void __local_bh_disable(void);
  9044. +extern void _local_bh_enable(void);
  9045. +extern void __local_bh_enable(void);
  9046. +
  9047. +static inline void local_bh_disable(void)
  9048. +{
  9049. + __local_bh_disable();
  9050. +}
  9051. +
  9052. +static inline void __local_bh_disable_ip(unsigned long ip, unsigned int cnt)
  9053. +{
  9054. + __local_bh_disable();
  9055. +}
  9056. +
  9057. +static inline void local_bh_enable(void)
  9058. +{
  9059. + __local_bh_enable();
  9060. +}
  9061. +
  9062. +static inline void __local_bh_enable_ip(unsigned long ip, unsigned int cnt)
  9063. +{
  9064. + __local_bh_enable();
  9065. +}
  9066. +
  9067. +static inline void local_bh_enable_ip(unsigned long ip)
  9068. +{
  9069. + __local_bh_enable();
  9070. +}
  9071. +
  9072. +#else
  9073. +
  9074. #ifdef CONFIG_TRACE_IRQFLAGS
  9075. extern void __local_bh_disable_ip(unsigned long ip, unsigned int cnt);
  9076. #else
  9077. @@ -31,5 +64,6 @@
  9078. {
  9079. __local_bh_enable_ip(_THIS_IP_, SOFTIRQ_DISABLE_OFFSET);
  9080. }
  9081. +#endif
  9082. #endif /* _LINUX_BH_H */
  9083. diff -Nur linux-4.1.20.orig/include/linux/buffer_head.h linux-4.1.20/include/linux/buffer_head.h
  9084. --- linux-4.1.20.orig/include/linux/buffer_head.h 2016-03-17 19:11:03.000000000 +0100
  9085. +++ linux-4.1.20/include/linux/buffer_head.h 2016-03-21 20:18:33.000000000 +0100
  9086. @@ -75,8 +75,52 @@
  9087. struct address_space *b_assoc_map; /* mapping this buffer is
  9088. associated with */
  9089. atomic_t b_count; /* users using this buffer_head */
  9090. +#ifdef CONFIG_PREEMPT_RT_BASE
  9091. + spinlock_t b_uptodate_lock;
  9092. +#if defined(CONFIG_JBD) || defined(CONFIG_JBD_MODULE) || \
  9093. + defined(CONFIG_JBD2) || defined(CONFIG_JBD2_MODULE)
  9094. + spinlock_t b_state_lock;
  9095. + spinlock_t b_journal_head_lock;
  9096. +#endif
  9097. +#endif
  9098. };
  9099. +static inline unsigned long bh_uptodate_lock_irqsave(struct buffer_head *bh)
  9100. +{
  9101. + unsigned long flags;
  9102. +
  9103. +#ifndef CONFIG_PREEMPT_RT_BASE
  9104. + local_irq_save(flags);
  9105. + bit_spin_lock(BH_Uptodate_Lock, &bh->b_state);
  9106. +#else
  9107. + spin_lock_irqsave(&bh->b_uptodate_lock, flags);
  9108. +#endif
  9109. + return flags;
  9110. +}
  9111. +
  9112. +static inline void
  9113. +bh_uptodate_unlock_irqrestore(struct buffer_head *bh, unsigned long flags)
  9114. +{
  9115. +#ifndef CONFIG_PREEMPT_RT_BASE
  9116. + bit_spin_unlock(BH_Uptodate_Lock, &bh->b_state);
  9117. + local_irq_restore(flags);
  9118. +#else
  9119. + spin_unlock_irqrestore(&bh->b_uptodate_lock, flags);
  9120. +#endif
  9121. +}
  9122. +
  9123. +static inline void buffer_head_init_locks(struct buffer_head *bh)
  9124. +{
  9125. +#ifdef CONFIG_PREEMPT_RT_BASE
  9126. + spin_lock_init(&bh->b_uptodate_lock);
  9127. +#if defined(CONFIG_JBD) || defined(CONFIG_JBD_MODULE) || \
  9128. + defined(CONFIG_JBD2) || defined(CONFIG_JBD2_MODULE)
  9129. + spin_lock_init(&bh->b_state_lock);
  9130. + spin_lock_init(&bh->b_journal_head_lock);
  9131. +#endif
  9132. +#endif
  9133. +}
  9134. +
  9135. /*
  9136. * macro tricks to expand the set_buffer_foo(), clear_buffer_foo()
  9137. * and buffer_foo() functions.
  9138. diff -Nur linux-4.1.20.orig/include/linux/cgroup-defs.h linux-4.1.20/include/linux/cgroup-defs.h
  9139. --- linux-4.1.20.orig/include/linux/cgroup-defs.h 2016-03-17 19:11:03.000000000 +0100
  9140. +++ linux-4.1.20/include/linux/cgroup-defs.h 2016-03-21 20:18:33.000000000 +0100
  9141. @@ -124,6 +124,7 @@
  9142. /* percpu_ref killing and RCU release */
  9143. struct rcu_head rcu_head;
  9144. struct work_struct destroy_work;
  9145. + struct swork_event destroy_swork;
  9146. };
  9147. /*
  9148. diff -Nur linux-4.1.20.orig/include/linux/cgroup.h linux-4.1.20/include/linux/cgroup.h
  9149. --- linux-4.1.20.orig/include/linux/cgroup.h 2016-03-17 19:11:03.000000000 +0100
  9150. +++ linux-4.1.20/include/linux/cgroup.h 2016-03-21 20:18:33.000000000 +0100
  9151. @@ -17,6 +17,8 @@
  9152. #include <linux/fs.h>
  9153. #include <linux/seq_file.h>
  9154. #include <linux/kernfs.h>
  9155. +#include <linux/wait.h>
  9156. +#include <linux/work-simple.h>
  9157. #include <linux/cgroup-defs.h>
  9158. diff -Nur linux-4.1.20.orig/include/linux/completion.h linux-4.1.20/include/linux/completion.h
  9159. --- linux-4.1.20.orig/include/linux/completion.h 2016-03-17 19:11:03.000000000 +0100
  9160. +++ linux-4.1.20/include/linux/completion.h 2016-03-21 20:18:33.000000000 +0100
  9161. @@ -7,8 +7,7 @@
  9162. * Atomic wait-for-completion handler data structures.
  9163. * See kernel/sched/completion.c for details.
  9164. */
  9165. -
  9166. -#include <linux/wait.h>
  9167. +#include <linux/wait-simple.h>
  9168. /*
  9169. * struct completion - structure used to maintain state for a "completion"
  9170. @@ -24,11 +23,11 @@
  9171. */
  9172. struct completion {
  9173. unsigned int done;
  9174. - wait_queue_head_t wait;
  9175. + struct swait_head wait;
  9176. };
  9177. #define COMPLETION_INITIALIZER(work) \
  9178. - { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
  9179. + { 0, SWAIT_HEAD_INITIALIZER((work).wait) }
  9180. #define COMPLETION_INITIALIZER_ONSTACK(work) \
  9181. ({ init_completion(&work); work; })
  9182. @@ -73,7 +72,7 @@
  9183. static inline void init_completion(struct completion *x)
  9184. {
  9185. x->done = 0;
  9186. - init_waitqueue_head(&x->wait);
  9187. + init_swait_head(&x->wait);
  9188. }
  9189. /**
  9190. diff -Nur linux-4.1.20.orig/include/linux/cpu.h linux-4.1.20/include/linux/cpu.h
  9191. --- linux-4.1.20.orig/include/linux/cpu.h 2016-03-17 19:11:03.000000000 +0100
  9192. +++ linux-4.1.20/include/linux/cpu.h 2016-03-21 20:18:33.000000000 +0100
  9193. @@ -231,6 +231,8 @@
  9194. extern void put_online_cpus(void);
  9195. extern void cpu_hotplug_disable(void);
  9196. extern void cpu_hotplug_enable(void);
  9197. +extern void pin_current_cpu(void);
  9198. +extern void unpin_current_cpu(void);
  9199. #define hotcpu_notifier(fn, pri) cpu_notifier(fn, pri)
  9200. #define __hotcpu_notifier(fn, pri) __cpu_notifier(fn, pri)
  9201. #define register_hotcpu_notifier(nb) register_cpu_notifier(nb)
  9202. @@ -249,6 +251,8 @@
  9203. #define put_online_cpus() do { } while (0)
  9204. #define cpu_hotplug_disable() do { } while (0)
  9205. #define cpu_hotplug_enable() do { } while (0)
  9206. +static inline void pin_current_cpu(void) { }
  9207. +static inline void unpin_current_cpu(void) { }
  9208. #define hotcpu_notifier(fn, pri) do { (void)(fn); } while (0)
  9209. #define __hotcpu_notifier(fn, pri) do { (void)(fn); } while (0)
  9210. /* These aren't inline functions due to a GCC bug. */
  9211. diff -Nur linux-4.1.20.orig/include/linux/delay.h linux-4.1.20/include/linux/delay.h
  9212. --- linux-4.1.20.orig/include/linux/delay.h 2016-03-17 19:11:03.000000000 +0100
  9213. +++ linux-4.1.20/include/linux/delay.h 2016-03-21 20:18:33.000000000 +0100
  9214. @@ -52,4 +52,10 @@
  9215. msleep(seconds * 1000);
  9216. }
  9217. +#ifdef CONFIG_PREEMPT_RT_FULL
  9218. +extern void cpu_chill(void);
  9219. +#else
  9220. +# define cpu_chill() cpu_relax()
  9221. +#endif
  9222. +
  9223. #endif /* defined(_LINUX_DELAY_H) */
  9224. diff -Nur linux-4.1.20.orig/include/linux/ftrace.h linux-4.1.20/include/linux/ftrace.h
  9225. --- linux-4.1.20.orig/include/linux/ftrace.h 2016-03-17 19:11:03.000000000 +0100
  9226. +++ linux-4.1.20/include/linux/ftrace.h 2016-03-21 20:18:33.000000000 +0100
  9227. @@ -682,6 +682,18 @@
  9228. #define CALLER_ADDR5 ((unsigned long)ftrace_return_address(5))
  9229. #define CALLER_ADDR6 ((unsigned long)ftrace_return_address(6))
  9230. +static inline unsigned long get_lock_parent_ip(void)
  9231. +{
  9232. + unsigned long addr = CALLER_ADDR0;
  9233. +
  9234. + if (!in_lock_functions(addr))
  9235. + return addr;
  9236. + addr = CALLER_ADDR1;
  9237. + if (!in_lock_functions(addr))
  9238. + return addr;
  9239. + return CALLER_ADDR2;
  9240. +}
  9241. +
  9242. #ifdef CONFIG_IRQSOFF_TRACER
  9243. extern void time_hardirqs_on(unsigned long a0, unsigned long a1);
  9244. extern void time_hardirqs_off(unsigned long a0, unsigned long a1);
  9245. diff -Nur linux-4.1.20.orig/include/linux/ftrace_event.h linux-4.1.20/include/linux/ftrace_event.h
  9246. --- linux-4.1.20.orig/include/linux/ftrace_event.h 2016-03-17 19:11:03.000000000 +0100
  9247. +++ linux-4.1.20/include/linux/ftrace_event.h 2016-03-21 20:18:33.000000000 +0100
  9248. @@ -66,6 +66,9 @@
  9249. unsigned char flags;
  9250. unsigned char preempt_count;
  9251. int pid;
  9252. + unsigned short migrate_disable;
  9253. + unsigned short padding;
  9254. + unsigned char preempt_lazy_count;
  9255. };
  9256. #define FTRACE_MAX_EVENT \
  9257. diff -Nur linux-4.1.20.orig/include/linux/highmem.h linux-4.1.20/include/linux/highmem.h
  9258. --- linux-4.1.20.orig/include/linux/highmem.h 2016-03-17 19:11:03.000000000 +0100
  9259. +++ linux-4.1.20/include/linux/highmem.h 2016-03-21 20:18:33.000000000 +0100
  9260. @@ -7,6 +7,7 @@
  9261. #include <linux/mm.h>
  9262. #include <linux/uaccess.h>
  9263. #include <linux/hardirq.h>
  9264. +#include <linux/sched.h>
  9265. #include <asm/cacheflush.h>
  9266. @@ -65,6 +66,7 @@
  9267. static inline void *kmap_atomic(struct page *page)
  9268. {
  9269. + preempt_disable_nort();
  9270. pagefault_disable();
  9271. return page_address(page);
  9272. }
  9273. @@ -73,6 +75,7 @@
  9274. static inline void __kunmap_atomic(void *addr)
  9275. {
  9276. pagefault_enable();
  9277. + preempt_enable_nort();
  9278. }
  9279. #define kmap_atomic_pfn(pfn) kmap_atomic(pfn_to_page(pfn))
  9280. @@ -85,32 +88,51 @@
  9281. #if defined(CONFIG_HIGHMEM) || defined(CONFIG_X86_32)
  9282. +#ifndef CONFIG_PREEMPT_RT_FULL
  9283. DECLARE_PER_CPU(int, __kmap_atomic_idx);
  9284. +#endif
  9285. static inline int kmap_atomic_idx_push(void)
  9286. {
  9287. +#ifndef CONFIG_PREEMPT_RT_FULL
  9288. int idx = __this_cpu_inc_return(__kmap_atomic_idx) - 1;
  9289. -#ifdef CONFIG_DEBUG_HIGHMEM
  9290. +# ifdef CONFIG_DEBUG_HIGHMEM
  9291. WARN_ON_ONCE(in_irq() && !irqs_disabled());
  9292. BUG_ON(idx >= KM_TYPE_NR);
  9293. -#endif
  9294. +# endif
  9295. return idx;
  9296. +#else
  9297. + current->kmap_idx++;
  9298. + BUG_ON(current->kmap_idx > KM_TYPE_NR);
  9299. + return current->kmap_idx - 1;
  9300. +#endif
  9301. }
  9302. static inline int kmap_atomic_idx(void)
  9303. {
  9304. +#ifndef CONFIG_PREEMPT_RT_FULL
  9305. return __this_cpu_read(__kmap_atomic_idx) - 1;
  9306. +#else
  9307. + return current->kmap_idx - 1;
  9308. +#endif
  9309. }
  9310. static inline void kmap_atomic_idx_pop(void)
  9311. {
  9312. -#ifdef CONFIG_DEBUG_HIGHMEM
  9313. +#ifndef CONFIG_PREEMPT_RT_FULL
  9314. +# ifdef CONFIG_DEBUG_HIGHMEM
  9315. int idx = __this_cpu_dec_return(__kmap_atomic_idx);
  9316. BUG_ON(idx < 0);
  9317. -#else
  9318. +# else
  9319. __this_cpu_dec(__kmap_atomic_idx);
  9320. +# endif
  9321. +#else
  9322. + current->kmap_idx--;
  9323. +# ifdef CONFIG_DEBUG_HIGHMEM
  9324. + BUG_ON(current->kmap_idx < 0);
  9325. +# endif
  9326. #endif
  9327. }
  9328. diff -Nur linux-4.1.20.orig/include/linux/hrtimer.h linux-4.1.20/include/linux/hrtimer.h
  9329. --- linux-4.1.20.orig/include/linux/hrtimer.h 2016-03-17 19:11:03.000000000 +0100
  9330. +++ linux-4.1.20/include/linux/hrtimer.h 2016-03-21 20:18:33.000000000 +0100
  9331. @@ -111,6 +111,11 @@
  9332. enum hrtimer_restart (*function)(struct hrtimer *);
  9333. struct hrtimer_clock_base *base;
  9334. unsigned long state;
  9335. + struct list_head cb_entry;
  9336. + int irqsafe;
  9337. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  9338. + ktime_t praecox;
  9339. +#endif
  9340. #ifdef CONFIG_TIMER_STATS
  9341. int start_pid;
  9342. void *start_site;
  9343. @@ -147,6 +152,7 @@
  9344. int index;
  9345. clockid_t clockid;
  9346. struct timerqueue_head active;
  9347. + struct list_head expired;
  9348. ktime_t resolution;
  9349. ktime_t (*get_time)(void);
  9350. ktime_t softirq_time;
  9351. @@ -194,6 +200,9 @@
  9352. unsigned long nr_hangs;
  9353. ktime_t max_hang_time;
  9354. #endif
  9355. +#ifdef CONFIG_PREEMPT_RT_BASE
  9356. + wait_queue_head_t wait;
  9357. +#endif
  9358. struct hrtimer_clock_base clock_base[HRTIMER_MAX_CLOCK_BASES];
  9359. };
  9360. @@ -381,6 +390,13 @@
  9361. return hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
  9362. }
  9363. +/* Softirq preemption could deadlock timer removal */
  9364. +#ifdef CONFIG_PREEMPT_RT_BASE
  9365. + extern void hrtimer_wait_for_timer(const struct hrtimer *timer);
  9366. +#else
  9367. +# define hrtimer_wait_for_timer(timer) do { cpu_relax(); } while (0)
  9368. +#endif
  9369. +
  9370. /* Query timers: */
  9371. extern ktime_t hrtimer_get_remaining(const struct hrtimer *timer);
  9372. extern int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp);
  9373. diff -Nur linux-4.1.20.orig/include/linux/idr.h linux-4.1.20/include/linux/idr.h
  9374. --- linux-4.1.20.orig/include/linux/idr.h 2016-03-17 19:11:03.000000000 +0100
  9375. +++ linux-4.1.20/include/linux/idr.h 2016-03-21 20:18:33.000000000 +0100
  9376. @@ -95,10 +95,14 @@
  9377. * Each idr_preload() should be matched with an invocation of this
  9378. * function. See idr_preload() for details.
  9379. */
  9380. +#ifdef CONFIG_PREEMPT_RT_FULL
  9381. +void idr_preload_end(void);
  9382. +#else
  9383. static inline void idr_preload_end(void)
  9384. {
  9385. preempt_enable();
  9386. }
  9387. +#endif
  9388. /**
  9389. * idr_find - return pointer for given id
  9390. diff -Nur linux-4.1.20.orig/include/linux/init_task.h linux-4.1.20/include/linux/init_task.h
  9391. --- linux-4.1.20.orig/include/linux/init_task.h 2016-03-17 19:11:03.000000000 +0100
  9392. +++ linux-4.1.20/include/linux/init_task.h 2016-03-21 20:18:33.000000000 +0100
  9393. @@ -147,9 +147,16 @@
  9394. # define INIT_PERF_EVENTS(tsk)
  9395. #endif
  9396. +#ifdef CONFIG_PREEMPT_RT_BASE
  9397. +# define INIT_TIMER_LIST .posix_timer_list = NULL,
  9398. +#else
  9399. +# define INIT_TIMER_LIST
  9400. +#endif
  9401. +
  9402. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  9403. # define INIT_VTIME(tsk) \
  9404. - .vtime_seqlock = __SEQLOCK_UNLOCKED(tsk.vtime_seqlock), \
  9405. + .vtime_lock = __RAW_SPIN_LOCK_UNLOCKED(tsk.vtime_lock), \
  9406. + .vtime_seq = SEQCNT_ZERO(tsk.vtime_seq), \
  9407. .vtime_snap = 0, \
  9408. .vtime_snap_whence = VTIME_SYS,
  9409. #else
  9410. @@ -238,6 +245,7 @@
  9411. .cpu_timers = INIT_CPU_TIMERS(tsk.cpu_timers), \
  9412. .pi_lock = __RAW_SPIN_LOCK_UNLOCKED(tsk.pi_lock), \
  9413. .timer_slack_ns = 50000, /* 50 usec default slack */ \
  9414. + INIT_TIMER_LIST \
  9415. .pids = { \
  9416. [PIDTYPE_PID] = INIT_PID_LINK(PIDTYPE_PID), \
  9417. [PIDTYPE_PGID] = INIT_PID_LINK(PIDTYPE_PGID), \
  9418. diff -Nur linux-4.1.20.orig/include/linux/interrupt.h linux-4.1.20/include/linux/interrupt.h
  9419. --- linux-4.1.20.orig/include/linux/interrupt.h 2016-03-17 19:11:03.000000000 +0100
  9420. +++ linux-4.1.20/include/linux/interrupt.h 2016-03-21 20:18:33.000000000 +0100
  9421. @@ -61,6 +61,7 @@
  9422. * interrupt handler after suspending interrupts. For system
  9423. * wakeup devices users need to implement wakeup detection in
  9424. * their interrupt handlers.
  9425. + * IRQF_NO_SOFTIRQ_CALL - Do not process softirqs in the irq thread context (RT)
  9426. */
  9427. #define IRQF_SHARED 0x00000080
  9428. #define IRQF_PROBE_SHARED 0x00000100
  9429. @@ -74,6 +75,7 @@
  9430. #define IRQF_NO_THREAD 0x00010000
  9431. #define IRQF_EARLY_RESUME 0x00020000
  9432. #define IRQF_COND_SUSPEND 0x00040000
  9433. +#define IRQF_NO_SOFTIRQ_CALL 0x00080000
  9434. #define IRQF_TIMER (__IRQF_TIMER | IRQF_NO_SUSPEND | IRQF_NO_THREAD)
  9435. @@ -102,6 +104,7 @@
  9436. * @flags: flags (see IRQF_* above)
  9437. * @thread_fn: interrupt handler function for threaded interrupts
  9438. * @thread: thread pointer for threaded interrupts
  9439. + * @secondary: pointer to secondary irqaction (force threading)
  9440. * @thread_flags: flags related to @thread
  9441. * @thread_mask: bitmask for keeping track of @thread activity
  9442. * @dir: pointer to the proc/irq/NN/name entry
  9443. @@ -113,6 +116,7 @@
  9444. struct irqaction *next;
  9445. irq_handler_t thread_fn;
  9446. struct task_struct *thread;
  9447. + struct irqaction *secondary;
  9448. unsigned int irq;
  9449. unsigned int flags;
  9450. unsigned long thread_flags;
  9451. @@ -184,7 +188,7 @@
  9452. #ifdef CONFIG_LOCKDEP
  9453. # define local_irq_enable_in_hardirq() do { } while (0)
  9454. #else
  9455. -# define local_irq_enable_in_hardirq() local_irq_enable()
  9456. +# define local_irq_enable_in_hardirq() local_irq_enable_nort()
  9457. #endif
  9458. extern void disable_irq_nosync(unsigned int irq);
  9459. @@ -215,6 +219,7 @@
  9460. unsigned int irq;
  9461. struct kref kref;
  9462. struct work_struct work;
  9463. + struct list_head list;
  9464. void (*notify)(struct irq_affinity_notify *, const cpumask_t *mask);
  9465. void (*release)(struct kref *ref);
  9466. };
  9467. @@ -377,9 +382,13 @@
  9468. bool state);
  9469. #ifdef CONFIG_IRQ_FORCED_THREADING
  9470. +# ifndef CONFIG_PREEMPT_RT_BASE
  9471. extern bool force_irqthreads;
  9472. +# else
  9473. +# define force_irqthreads (true)
  9474. +# endif
  9475. #else
  9476. -#define force_irqthreads (0)
  9477. +#define force_irqthreads (false)
  9478. #endif
  9479. #ifndef __ARCH_SET_SOFTIRQ_PENDING
  9480. @@ -435,9 +444,10 @@
  9481. void (*action)(struct softirq_action *);
  9482. };
  9483. +#ifndef CONFIG_PREEMPT_RT_FULL
  9484. asmlinkage void do_softirq(void);
  9485. asmlinkage void __do_softirq(void);
  9486. -
  9487. +static inline void thread_do_softirq(void) { do_softirq(); }
  9488. #ifdef __ARCH_HAS_DO_SOFTIRQ
  9489. void do_softirq_own_stack(void);
  9490. #else
  9491. @@ -446,13 +456,25 @@
  9492. __do_softirq();
  9493. }
  9494. #endif
  9495. +#else
  9496. +extern void thread_do_softirq(void);
  9497. +#endif
  9498. extern void open_softirq(int nr, void (*action)(struct softirq_action *));
  9499. extern void softirq_init(void);
  9500. extern void __raise_softirq_irqoff(unsigned int nr);
  9501. +#ifdef CONFIG_PREEMPT_RT_FULL
  9502. +extern void __raise_softirq_irqoff_ksoft(unsigned int nr);
  9503. +#else
  9504. +static inline void __raise_softirq_irqoff_ksoft(unsigned int nr)
  9505. +{
  9506. + __raise_softirq_irqoff(nr);
  9507. +}
  9508. +#endif
  9509. extern void raise_softirq_irqoff(unsigned int nr);
  9510. extern void raise_softirq(unsigned int nr);
  9511. +extern void softirq_check_pending_idle(void);
  9512. DECLARE_PER_CPU(struct task_struct *, ksoftirqd);
  9513. @@ -474,8 +496,9 @@
  9514. to be executed on some cpu at least once after this.
  9515. * If the tasklet is already scheduled, but its execution is still not
  9516. started, it will be executed only once.
  9517. - * If this tasklet is already running on another CPU (or schedule is called
  9518. - from tasklet itself), it is rescheduled for later.
  9519. + * If this tasklet is already running on another CPU, it is rescheduled
  9520. + for later.
  9521. + * Schedule must not be called from the tasklet itself (a lockup occurs)
  9522. * Tasklet is strictly serialized wrt itself, but not
  9523. wrt another tasklets. If client needs some intertask synchronization,
  9524. he makes it with spinlocks.
  9525. @@ -500,27 +523,36 @@
  9526. enum
  9527. {
  9528. TASKLET_STATE_SCHED, /* Tasklet is scheduled for execution */
  9529. - TASKLET_STATE_RUN /* Tasklet is running (SMP only) */
  9530. + TASKLET_STATE_RUN, /* Tasklet is running (SMP only) */
  9531. + TASKLET_STATE_PENDING /* Tasklet is pending */
  9532. };
  9533. -#ifdef CONFIG_SMP
  9534. +#define TASKLET_STATEF_SCHED (1 << TASKLET_STATE_SCHED)
  9535. +#define TASKLET_STATEF_RUN (1 << TASKLET_STATE_RUN)
  9536. +#define TASKLET_STATEF_PENDING (1 << TASKLET_STATE_PENDING)
  9537. +
  9538. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  9539. static inline int tasklet_trylock(struct tasklet_struct *t)
  9540. {
  9541. return !test_and_set_bit(TASKLET_STATE_RUN, &(t)->state);
  9542. }
  9543. +static inline int tasklet_tryunlock(struct tasklet_struct *t)
  9544. +{
  9545. + return cmpxchg(&t->state, TASKLET_STATEF_RUN, 0) == TASKLET_STATEF_RUN;
  9546. +}
  9547. +
  9548. static inline void tasklet_unlock(struct tasklet_struct *t)
  9549. {
  9550. smp_mb__before_atomic();
  9551. clear_bit(TASKLET_STATE_RUN, &(t)->state);
  9552. }
  9553. -static inline void tasklet_unlock_wait(struct tasklet_struct *t)
  9554. -{
  9555. - while (test_bit(TASKLET_STATE_RUN, &(t)->state)) { barrier(); }
  9556. -}
  9557. +extern void tasklet_unlock_wait(struct tasklet_struct *t);
  9558. +
  9559. #else
  9560. #define tasklet_trylock(t) 1
  9561. +#define tasklet_tryunlock(t) 1
  9562. #define tasklet_unlock_wait(t) do { } while (0)
  9563. #define tasklet_unlock(t) do { } while (0)
  9564. #endif
  9565. @@ -569,12 +601,7 @@
  9566. smp_mb();
  9567. }
  9568. -static inline void tasklet_enable(struct tasklet_struct *t)
  9569. -{
  9570. - smp_mb__before_atomic();
  9571. - atomic_dec(&t->count);
  9572. -}
  9573. -
  9574. +extern void tasklet_enable(struct tasklet_struct *t);
  9575. extern void tasklet_kill(struct tasklet_struct *t);
  9576. extern void tasklet_kill_immediate(struct tasklet_struct *t, unsigned int cpu);
  9577. extern void tasklet_init(struct tasklet_struct *t,
  9578. @@ -605,6 +632,12 @@
  9579. tasklet_kill(&ttimer->tasklet);
  9580. }
  9581. +#ifdef CONFIG_PREEMPT_RT_FULL
  9582. +extern void softirq_early_init(void);
  9583. +#else
  9584. +static inline void softirq_early_init(void) { }
  9585. +#endif
  9586. +
  9587. /*
  9588. * Autoprobing for irqs:
  9589. *
  9590. diff -Nur linux-4.1.20.orig/include/linux/io-mapping.h linux-4.1.20/include/linux/io-mapping.h
  9591. --- linux-4.1.20.orig/include/linux/io-mapping.h 2016-03-17 19:11:03.000000000 +0100
  9592. +++ linux-4.1.20/include/linux/io-mapping.h 2016-03-21 20:18:33.000000000 +0100
  9593. @@ -141,6 +141,7 @@
  9594. io_mapping_map_atomic_wc(struct io_mapping *mapping,
  9595. unsigned long offset)
  9596. {
  9597. + preempt_disable();
  9598. pagefault_disable();
  9599. return ((char __force __iomem *) mapping) + offset;
  9600. }
  9601. @@ -149,6 +150,7 @@
  9602. io_mapping_unmap_atomic(void __iomem *vaddr)
  9603. {
  9604. pagefault_enable();
  9605. + preempt_enable();
  9606. }
  9607. /* Non-atomic map/unmap */
  9608. diff -Nur linux-4.1.20.orig/include/linux/irq.h linux-4.1.20/include/linux/irq.h
  9609. --- linux-4.1.20.orig/include/linux/irq.h 2016-03-17 19:11:03.000000000 +0100
  9610. +++ linux-4.1.20/include/linux/irq.h 2016-03-21 20:18:33.000000000 +0100
  9611. @@ -72,6 +72,7 @@
  9612. * IRQ_IS_POLLED - Always polled by another interrupt. Exclude
  9613. * it from the spurious interrupt detection
  9614. * mechanism and from core side polling.
  9615. + * IRQ_NO_SOFTIRQ_CALL - No softirq processing in the irq thread context (RT)
  9616. */
  9617. enum {
  9618. IRQ_TYPE_NONE = 0x00000000,
  9619. @@ -97,13 +98,14 @@
  9620. IRQ_NOTHREAD = (1 << 16),
  9621. IRQ_PER_CPU_DEVID = (1 << 17),
  9622. IRQ_IS_POLLED = (1 << 18),
  9623. + IRQ_NO_SOFTIRQ_CALL = (1 << 19),
  9624. };
  9625. #define IRQF_MODIFY_MASK \
  9626. (IRQ_TYPE_SENSE_MASK | IRQ_NOPROBE | IRQ_NOREQUEST | \
  9627. IRQ_NOAUTOEN | IRQ_MOVE_PCNTXT | IRQ_LEVEL | IRQ_NO_BALANCING | \
  9628. IRQ_PER_CPU | IRQ_NESTED_THREAD | IRQ_NOTHREAD | IRQ_PER_CPU_DEVID | \
  9629. - IRQ_IS_POLLED)
  9630. + IRQ_IS_POLLED | IRQ_NO_SOFTIRQ_CALL)
  9631. #define IRQ_NO_BALANCING_MASK (IRQ_PER_CPU | IRQ_NO_BALANCING)
  9632. diff -Nur linux-4.1.20.orig/include/linux/irq_work.h linux-4.1.20/include/linux/irq_work.h
  9633. --- linux-4.1.20.orig/include/linux/irq_work.h 2016-03-17 19:11:03.000000000 +0100
  9634. +++ linux-4.1.20/include/linux/irq_work.h 2016-03-21 20:18:33.000000000 +0100
  9635. @@ -16,6 +16,7 @@
  9636. #define IRQ_WORK_BUSY 2UL
  9637. #define IRQ_WORK_FLAGS 3UL
  9638. #define IRQ_WORK_LAZY 4UL /* Doesn't want IPI, wait for tick */
  9639. +#define IRQ_WORK_HARD_IRQ 8UL /* Run hard IRQ context, even on RT */
  9640. struct irq_work {
  9641. unsigned long flags;
  9642. @@ -51,4 +52,10 @@
  9643. static inline void irq_work_run(void) { }
  9644. #endif
  9645. +#if defined(CONFIG_IRQ_WORK) && defined(CONFIG_PREEMPT_RT_FULL)
  9646. +void irq_work_tick_soft(void);
  9647. +#else
  9648. +static inline void irq_work_tick_soft(void) { }
  9649. +#endif
  9650. +
  9651. #endif /* _LINUX_IRQ_WORK_H */
  9652. diff -Nur linux-4.1.20.orig/include/linux/irqdesc.h linux-4.1.20/include/linux/irqdesc.h
  9653. --- linux-4.1.20.orig/include/linux/irqdesc.h 2016-03-17 19:11:03.000000000 +0100
  9654. +++ linux-4.1.20/include/linux/irqdesc.h 2016-03-21 20:18:33.000000000 +0100
  9655. @@ -63,6 +63,7 @@
  9656. unsigned int irqs_unhandled;
  9657. atomic_t threads_handled;
  9658. int threads_handled_last;
  9659. + u64 random_ip;
  9660. raw_spinlock_t lock;
  9661. struct cpumask *percpu_enabled;
  9662. #ifdef CONFIG_SMP
  9663. diff -Nur linux-4.1.20.orig/include/linux/irqflags.h linux-4.1.20/include/linux/irqflags.h
  9664. --- linux-4.1.20.orig/include/linux/irqflags.h 2016-03-17 19:11:03.000000000 +0100
  9665. +++ linux-4.1.20/include/linux/irqflags.h 2016-03-21 20:18:33.000000000 +0100
  9666. @@ -25,8 +25,6 @@
  9667. # define trace_softirqs_enabled(p) ((p)->softirqs_enabled)
  9668. # define trace_hardirq_enter() do { current->hardirq_context++; } while (0)
  9669. # define trace_hardirq_exit() do { current->hardirq_context--; } while (0)
  9670. -# define lockdep_softirq_enter() do { current->softirq_context++; } while (0)
  9671. -# define lockdep_softirq_exit() do { current->softirq_context--; } while (0)
  9672. # define INIT_TRACE_IRQFLAGS .softirqs_enabled = 1,
  9673. #else
  9674. # define trace_hardirqs_on() do { } while (0)
  9675. @@ -39,9 +37,15 @@
  9676. # define trace_softirqs_enabled(p) 0
  9677. # define trace_hardirq_enter() do { } while (0)
  9678. # define trace_hardirq_exit() do { } while (0)
  9679. +# define INIT_TRACE_IRQFLAGS
  9680. +#endif
  9681. +
  9682. +#if defined(CONFIG_TRACE_IRQFLAGS) && !defined(CONFIG_PREEMPT_RT_FULL)
  9683. +# define lockdep_softirq_enter() do { current->softirq_context++; } while (0)
  9684. +# define lockdep_softirq_exit() do { current->softirq_context--; } while (0)
  9685. +#else
  9686. # define lockdep_softirq_enter() do { } while (0)
  9687. # define lockdep_softirq_exit() do { } while (0)
  9688. -# define INIT_TRACE_IRQFLAGS
  9689. #endif
  9690. #if defined(CONFIG_IRQSOFF_TRACER) || \
  9691. @@ -148,4 +152,23 @@
  9692. #define irqs_disabled_flags(flags) raw_irqs_disabled_flags(flags)
  9693. +/*
  9694. + * local_irq* variants depending on RT/!RT
  9695. + */
  9696. +#ifdef CONFIG_PREEMPT_RT_FULL
  9697. +# define local_irq_disable_nort() do { } while (0)
  9698. +# define local_irq_enable_nort() do { } while (0)
  9699. +# define local_irq_save_nort(flags) local_save_flags(flags)
  9700. +# define local_irq_restore_nort(flags) (void)(flags)
  9701. +# define local_irq_disable_rt() local_irq_disable()
  9702. +# define local_irq_enable_rt() local_irq_enable()
  9703. +#else
  9704. +# define local_irq_disable_nort() local_irq_disable()
  9705. +# define local_irq_enable_nort() local_irq_enable()
  9706. +# define local_irq_save_nort(flags) local_irq_save(flags)
  9707. +# define local_irq_restore_nort(flags) local_irq_restore(flags)
  9708. +# define local_irq_disable_rt() do { } while (0)
  9709. +# define local_irq_enable_rt() do { } while (0)
  9710. +#endif
  9711. +
  9712. #endif
  9713. diff -Nur linux-4.1.20.orig/include/linux/jbd_common.h linux-4.1.20/include/linux/jbd_common.h
  9714. --- linux-4.1.20.orig/include/linux/jbd_common.h 2016-03-17 19:11:03.000000000 +0100
  9715. +++ linux-4.1.20/include/linux/jbd_common.h 2016-03-21 20:18:33.000000000 +0100
  9716. @@ -15,32 +15,56 @@
  9717. static inline void jbd_lock_bh_state(struct buffer_head *bh)
  9718. {
  9719. +#ifndef CONFIG_PREEMPT_RT_BASE
  9720. bit_spin_lock(BH_State, &bh->b_state);
  9721. +#else
  9722. + spin_lock(&bh->b_state_lock);
  9723. +#endif
  9724. }
  9725. static inline int jbd_trylock_bh_state(struct buffer_head *bh)
  9726. {
  9727. +#ifndef CONFIG_PREEMPT_RT_BASE
  9728. return bit_spin_trylock(BH_State, &bh->b_state);
  9729. +#else
  9730. + return spin_trylock(&bh->b_state_lock);
  9731. +#endif
  9732. }
  9733. static inline int jbd_is_locked_bh_state(struct buffer_head *bh)
  9734. {
  9735. +#ifndef CONFIG_PREEMPT_RT_BASE
  9736. return bit_spin_is_locked(BH_State, &bh->b_state);
  9737. +#else
  9738. + return spin_is_locked(&bh->b_state_lock);
  9739. +#endif
  9740. }
  9741. static inline void jbd_unlock_bh_state(struct buffer_head *bh)
  9742. {
  9743. +#ifndef CONFIG_PREEMPT_RT_BASE
  9744. bit_spin_unlock(BH_State, &bh->b_state);
  9745. +#else
  9746. + spin_unlock(&bh->b_state_lock);
  9747. +#endif
  9748. }
  9749. static inline void jbd_lock_bh_journal_head(struct buffer_head *bh)
  9750. {
  9751. +#ifndef CONFIG_PREEMPT_RT_BASE
  9752. bit_spin_lock(BH_JournalHead, &bh->b_state);
  9753. +#else
  9754. + spin_lock(&bh->b_journal_head_lock);
  9755. +#endif
  9756. }
  9757. static inline void jbd_unlock_bh_journal_head(struct buffer_head *bh)
  9758. {
  9759. +#ifndef CONFIG_PREEMPT_RT_BASE
  9760. bit_spin_unlock(BH_JournalHead, &bh->b_state);
  9761. +#else
  9762. + spin_unlock(&bh->b_journal_head_lock);
  9763. +#endif
  9764. }
  9765. #endif
  9766. diff -Nur linux-4.1.20.orig/include/linux/kdb.h linux-4.1.20/include/linux/kdb.h
  9767. --- linux-4.1.20.orig/include/linux/kdb.h 2016-03-17 19:11:03.000000000 +0100
  9768. +++ linux-4.1.20/include/linux/kdb.h 2016-03-21 20:18:33.000000000 +0100
  9769. @@ -167,6 +167,7 @@
  9770. extern __printf(1, 2) int kdb_printf(const char *, ...);
  9771. typedef __printf(1, 2) int (*kdb_printf_t)(const char *, ...);
  9772. +#define in_kdb_printk() (kdb_trap_printk)
  9773. extern void kdb_init(int level);
  9774. /* Access to kdb specific polling devices */
  9775. @@ -201,6 +202,7 @@
  9776. extern int kdb_unregister(char *);
  9777. #else /* ! CONFIG_KGDB_KDB */
  9778. static inline __printf(1, 2) int kdb_printf(const char *fmt, ...) { return 0; }
  9779. +#define in_kdb_printk() (0)
  9780. static inline void kdb_init(int level) {}
  9781. static inline int kdb_register(char *cmd, kdb_func_t func, char *usage,
  9782. char *help, short minlen) { return 0; }
  9783. diff -Nur linux-4.1.20.orig/include/linux/kernel.h linux-4.1.20/include/linux/kernel.h
  9784. --- linux-4.1.20.orig/include/linux/kernel.h 2016-03-17 19:11:03.000000000 +0100
  9785. +++ linux-4.1.20/include/linux/kernel.h 2016-03-21 20:18:33.000000000 +0100
  9786. @@ -188,6 +188,9 @@
  9787. */
  9788. # define might_sleep() \
  9789. do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
  9790. +
  9791. +# define might_sleep_no_state_check() \
  9792. + do { ___might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
  9793. # define sched_annotate_sleep() (current->task_state_change = 0)
  9794. #else
  9795. static inline void ___might_sleep(const char *file, int line,
  9796. @@ -195,6 +198,7 @@
  9797. static inline void __might_sleep(const char *file, int line,
  9798. int preempt_offset) { }
  9799. # define might_sleep() do { might_resched(); } while (0)
  9800. +# define might_sleep_no_state_check() do { might_resched(); } while (0)
  9801. # define sched_annotate_sleep() do { } while (0)
  9802. #endif
  9803. @@ -244,7 +248,8 @@
  9804. #if defined(CONFIG_MMU) && \
  9805. (defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP))
  9806. -void might_fault(void);
  9807. +#define might_fault() __might_fault(__FILE__, __LINE__)
  9808. +void __might_fault(const char *file, int line);
  9809. #else
  9810. static inline void might_fault(void) { }
  9811. #endif
  9812. @@ -466,6 +471,7 @@
  9813. SYSTEM_HALT,
  9814. SYSTEM_POWER_OFF,
  9815. SYSTEM_RESTART,
  9816. + SYSTEM_SUSPEND,
  9817. } system_state;
  9818. #define TAINT_PROPRIETARY_MODULE 0
  9819. diff -Nur linux-4.1.20.orig/include/linux/kvm_host.h linux-4.1.20/include/linux/kvm_host.h
  9820. --- linux-4.1.20.orig/include/linux/kvm_host.h 2016-03-17 19:11:03.000000000 +0100
  9821. +++ linux-4.1.20/include/linux/kvm_host.h 2016-03-21 20:18:33.000000000 +0100
  9822. @@ -230,7 +230,7 @@
  9823. int fpu_active;
  9824. int guest_fpu_loaded, guest_xcr0_loaded;
  9825. - wait_queue_head_t wq;
  9826. + struct swait_head wq;
  9827. struct pid *pid;
  9828. int sigset_active;
  9829. sigset_t sigset;
  9830. @@ -701,7 +701,7 @@
  9831. }
  9832. #endif
  9833. -static inline wait_queue_head_t *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu)
  9834. +static inline struct swait_head *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu)
  9835. {
  9836. #ifdef __KVM_HAVE_ARCH_WQP
  9837. return vcpu->arch.wqp;
  9838. diff -Nur linux-4.1.20.orig/include/linux/lglock.h linux-4.1.20/include/linux/lglock.h
  9839. --- linux-4.1.20.orig/include/linux/lglock.h 2016-03-17 19:11:03.000000000 +0100
  9840. +++ linux-4.1.20/include/linux/lglock.h 2016-03-21 20:18:33.000000000 +0100
  9841. @@ -34,22 +34,39 @@
  9842. #endif
  9843. struct lglock {
  9844. +#ifndef CONFIG_PREEMPT_RT_FULL
  9845. arch_spinlock_t __percpu *lock;
  9846. +#else
  9847. + struct rt_mutex __percpu *lock;
  9848. +#endif
  9849. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  9850. struct lock_class_key lock_key;
  9851. struct lockdep_map lock_dep_map;
  9852. #endif
  9853. };
  9854. -#define DEFINE_LGLOCK(name) \
  9855. +#ifndef CONFIG_PREEMPT_RT_FULL
  9856. +# define DEFINE_LGLOCK(name) \
  9857. static DEFINE_PER_CPU(arch_spinlock_t, name ## _lock) \
  9858. = __ARCH_SPIN_LOCK_UNLOCKED; \
  9859. struct lglock name = { .lock = &name ## _lock }
  9860. -#define DEFINE_STATIC_LGLOCK(name) \
  9861. +# define DEFINE_STATIC_LGLOCK(name) \
  9862. static DEFINE_PER_CPU(arch_spinlock_t, name ## _lock) \
  9863. = __ARCH_SPIN_LOCK_UNLOCKED; \
  9864. static struct lglock name = { .lock = &name ## _lock }
  9865. +#else
  9866. +
  9867. +# define DEFINE_LGLOCK(name) \
  9868. + static DEFINE_PER_CPU(struct rt_mutex, name ## _lock) \
  9869. + = __RT_MUTEX_INITIALIZER( name ## _lock); \
  9870. + struct lglock name = { .lock = &name ## _lock }
  9871. +
  9872. +# define DEFINE_STATIC_LGLOCK(name) \
  9873. + static DEFINE_PER_CPU(struct rt_mutex, name ## _lock) \
  9874. + = __RT_MUTEX_INITIALIZER( name ## _lock); \
  9875. + static struct lglock name = { .lock = &name ## _lock }
  9876. +#endif
  9877. void lg_lock_init(struct lglock *lg, char *name);
  9878. void lg_local_lock(struct lglock *lg);
  9879. @@ -59,6 +76,12 @@
  9880. void lg_global_lock(struct lglock *lg);
  9881. void lg_global_unlock(struct lglock *lg);
  9882. +#ifndef CONFIG_PREEMPT_RT_FULL
  9883. +#define lg_global_trylock_relax(name) lg_global_lock(name)
  9884. +#else
  9885. +void lg_global_trylock_relax(struct lglock *lg);
  9886. +#endif
  9887. +
  9888. #else
  9889. /* When !CONFIG_SMP, map lglock to spinlock */
  9890. #define lglock spinlock
  9891. diff -Nur linux-4.1.20.orig/include/linux/list_bl.h linux-4.1.20/include/linux/list_bl.h
  9892. --- linux-4.1.20.orig/include/linux/list_bl.h 2016-03-17 19:11:03.000000000 +0100
  9893. +++ linux-4.1.20/include/linux/list_bl.h 2016-03-21 20:18:33.000000000 +0100
  9894. @@ -2,6 +2,7 @@
  9895. #define _LINUX_LIST_BL_H
  9896. #include <linux/list.h>
  9897. +#include <linux/spinlock.h>
  9898. #include <linux/bit_spinlock.h>
  9899. /*
  9900. @@ -32,13 +33,22 @@
  9901. struct hlist_bl_head {
  9902. struct hlist_bl_node *first;
  9903. +#ifdef CONFIG_PREEMPT_RT_BASE
  9904. + raw_spinlock_t lock;
  9905. +#endif
  9906. };
  9907. struct hlist_bl_node {
  9908. struct hlist_bl_node *next, **pprev;
  9909. };
  9910. -#define INIT_HLIST_BL_HEAD(ptr) \
  9911. - ((ptr)->first = NULL)
  9912. +
  9913. +static inline void INIT_HLIST_BL_HEAD(struct hlist_bl_head *h)
  9914. +{
  9915. + h->first = NULL;
  9916. +#ifdef CONFIG_PREEMPT_RT_BASE
  9917. + raw_spin_lock_init(&h->lock);
  9918. +#endif
  9919. +}
  9920. static inline void INIT_HLIST_BL_NODE(struct hlist_bl_node *h)
  9921. {
  9922. @@ -117,12 +127,26 @@
  9923. static inline void hlist_bl_lock(struct hlist_bl_head *b)
  9924. {
  9925. +#ifndef CONFIG_PREEMPT_RT_BASE
  9926. bit_spin_lock(0, (unsigned long *)b);
  9927. +#else
  9928. + raw_spin_lock(&b->lock);
  9929. +#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  9930. + __set_bit(0, (unsigned long *)b);
  9931. +#endif
  9932. +#endif
  9933. }
  9934. static inline void hlist_bl_unlock(struct hlist_bl_head *b)
  9935. {
  9936. +#ifndef CONFIG_PREEMPT_RT_BASE
  9937. __bit_spin_unlock(0, (unsigned long *)b);
  9938. +#else
  9939. +#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  9940. + __clear_bit(0, (unsigned long *)b);
  9941. +#endif
  9942. + raw_spin_unlock(&b->lock);
  9943. +#endif
  9944. }
  9945. static inline bool hlist_bl_is_locked(struct hlist_bl_head *b)
  9946. diff -Nur linux-4.1.20.orig/include/linux/locallock.h linux-4.1.20/include/linux/locallock.h
  9947. --- linux-4.1.20.orig/include/linux/locallock.h 1970-01-01 01:00:00.000000000 +0100
  9948. +++ linux-4.1.20/include/linux/locallock.h 2016-03-21 20:18:33.000000000 +0100
  9949. @@ -0,0 +1,270 @@
  9950. +#ifndef _LINUX_LOCALLOCK_H
  9951. +#define _LINUX_LOCALLOCK_H
  9952. +
  9953. +#include <linux/percpu.h>
  9954. +#include <linux/spinlock.h>
  9955. +
  9956. +#ifdef CONFIG_PREEMPT_RT_BASE
  9957. +
  9958. +#ifdef CONFIG_DEBUG_SPINLOCK
  9959. +# define LL_WARN(cond) WARN_ON(cond)
  9960. +#else
  9961. +# define LL_WARN(cond) do { } while (0)
  9962. +#endif
  9963. +
  9964. +/*
  9965. + * per cpu lock based substitute for local_irq_*()
  9966. + */
  9967. +struct local_irq_lock {
  9968. + spinlock_t lock;
  9969. + struct task_struct *owner;
  9970. + int nestcnt;
  9971. + unsigned long flags;
  9972. +};
  9973. +
  9974. +#define DEFINE_LOCAL_IRQ_LOCK(lvar) \
  9975. + DEFINE_PER_CPU(struct local_irq_lock, lvar) = { \
  9976. + .lock = __SPIN_LOCK_UNLOCKED((lvar).lock) }
  9977. +
  9978. +#define DECLARE_LOCAL_IRQ_LOCK(lvar) \
  9979. + DECLARE_PER_CPU(struct local_irq_lock, lvar)
  9980. +
  9981. +#define local_irq_lock_init(lvar) \
  9982. + do { \
  9983. + int __cpu; \
  9984. + for_each_possible_cpu(__cpu) \
  9985. + spin_lock_init(&per_cpu(lvar, __cpu).lock); \
  9986. + } while (0)
  9987. +
  9988. +/*
  9989. + * spin_lock|trylock|unlock_local flavour that does not migrate disable
  9990. + * used for __local_lock|trylock|unlock where get_local_var/put_local_var
  9991. + * already takes care of the migrate_disable/enable
  9992. + * for CONFIG_PREEMPT_BASE map to the normal spin_* calls.
  9993. + */
  9994. +#ifdef CONFIG_PREEMPT_RT_FULL
  9995. +# define spin_lock_local(lock) rt_spin_lock(lock)
  9996. +# define spin_trylock_local(lock) rt_spin_trylock(lock)
  9997. +# define spin_unlock_local(lock) rt_spin_unlock(lock)
  9998. +#else
  9999. +# define spin_lock_local(lock) spin_lock(lock)
  10000. +# define spin_trylock_local(lock) spin_trylock(lock)
  10001. +# define spin_unlock_local(lock) spin_unlock(lock)
  10002. +#endif
  10003. +
  10004. +static inline void __local_lock(struct local_irq_lock *lv)
  10005. +{
  10006. + if (lv->owner != current) {
  10007. + spin_lock_local(&lv->lock);
  10008. + LL_WARN(lv->owner);
  10009. + LL_WARN(lv->nestcnt);
  10010. + lv->owner = current;
  10011. + }
  10012. + lv->nestcnt++;
  10013. +}
  10014. +
  10015. +#define local_lock(lvar) \
  10016. + do { __local_lock(&get_local_var(lvar)); } while (0)
  10017. +
  10018. +static inline int __local_trylock(struct local_irq_lock *lv)
  10019. +{
  10020. + if (lv->owner != current && spin_trylock_local(&lv->lock)) {
  10021. + LL_WARN(lv->owner);
  10022. + LL_WARN(lv->nestcnt);
  10023. + lv->owner = current;
  10024. + lv->nestcnt = 1;
  10025. + return 1;
  10026. + }
  10027. + return 0;
  10028. +}
  10029. +
  10030. +#define local_trylock(lvar) \
  10031. + ({ \
  10032. + int __locked; \
  10033. + __locked = __local_trylock(&get_local_var(lvar)); \
  10034. + if (!__locked) \
  10035. + put_local_var(lvar); \
  10036. + __locked; \
  10037. + })
  10038. +
  10039. +static inline void __local_unlock(struct local_irq_lock *lv)
  10040. +{
  10041. + LL_WARN(lv->nestcnt == 0);
  10042. + LL_WARN(lv->owner != current);
  10043. + if (--lv->nestcnt)
  10044. + return;
  10045. +
  10046. + lv->owner = NULL;
  10047. + spin_unlock_local(&lv->lock);
  10048. +}
  10049. +
  10050. +#define local_unlock(lvar) \
  10051. + do { \
  10052. + __local_unlock(this_cpu_ptr(&lvar)); \
  10053. + put_local_var(lvar); \
  10054. + } while (0)
  10055. +
  10056. +static inline void __local_lock_irq(struct local_irq_lock *lv)
  10057. +{
  10058. + spin_lock_irqsave(&lv->lock, lv->flags);
  10059. + LL_WARN(lv->owner);
  10060. + LL_WARN(lv->nestcnt);
  10061. + lv->owner = current;
  10062. + lv->nestcnt = 1;
  10063. +}
  10064. +
  10065. +#define local_lock_irq(lvar) \
  10066. + do { __local_lock_irq(&get_local_var(lvar)); } while (0)
  10067. +
  10068. +#define local_lock_irq_on(lvar, cpu) \
  10069. + do { __local_lock_irq(&per_cpu(lvar, cpu)); } while (0)
  10070. +
  10071. +static inline void __local_unlock_irq(struct local_irq_lock *lv)
  10072. +{
  10073. + LL_WARN(!lv->nestcnt);
  10074. + LL_WARN(lv->owner != current);
  10075. + lv->owner = NULL;
  10076. + lv->nestcnt = 0;
  10077. + spin_unlock_irq(&lv->lock);
  10078. +}
  10079. +
  10080. +#define local_unlock_irq(lvar) \
  10081. + do { \
  10082. + __local_unlock_irq(this_cpu_ptr(&lvar)); \
  10083. + put_local_var(lvar); \
  10084. + } while (0)
  10085. +
  10086. +#define local_unlock_irq_on(lvar, cpu) \
  10087. + do { \
  10088. + __local_unlock_irq(&per_cpu(lvar, cpu)); \
  10089. + } while (0)
  10090. +
  10091. +static inline int __local_lock_irqsave(struct local_irq_lock *lv)
  10092. +{
  10093. + if (lv->owner != current) {
  10094. + __local_lock_irq(lv);
  10095. + return 0;
  10096. + } else {
  10097. + lv->nestcnt++;
  10098. + return 1;
  10099. + }
  10100. +}
  10101. +
  10102. +#define local_lock_irqsave(lvar, _flags) \
  10103. + do { \
  10104. + if (__local_lock_irqsave(&get_local_var(lvar))) \
  10105. + put_local_var(lvar); \
  10106. + _flags = __this_cpu_read(lvar.flags); \
  10107. + } while (0)
  10108. +
  10109. +#define local_lock_irqsave_on(lvar, _flags, cpu) \
  10110. + do { \
  10111. + __local_lock_irqsave(&per_cpu(lvar, cpu)); \
  10112. + _flags = per_cpu(lvar, cpu).flags; \
  10113. + } while (0)
  10114. +
  10115. +static inline int __local_unlock_irqrestore(struct local_irq_lock *lv,
  10116. + unsigned long flags)
  10117. +{
  10118. + LL_WARN(!lv->nestcnt);
  10119. + LL_WARN(lv->owner != current);
  10120. + if (--lv->nestcnt)
  10121. + return 0;
  10122. +
  10123. + lv->owner = NULL;
  10124. + spin_unlock_irqrestore(&lv->lock, lv->flags);
  10125. + return 1;
  10126. +}
  10127. +
  10128. +#define local_unlock_irqrestore(lvar, flags) \
  10129. + do { \
  10130. + if (__local_unlock_irqrestore(this_cpu_ptr(&lvar), flags)) \
  10131. + put_local_var(lvar); \
  10132. + } while (0)
  10133. +
  10134. +#define local_unlock_irqrestore_on(lvar, flags, cpu) \
  10135. + do { \
  10136. + __local_unlock_irqrestore(&per_cpu(lvar, cpu), flags); \
  10137. + } while (0)
  10138. +
  10139. +#define local_spin_trylock_irq(lvar, lock) \
  10140. + ({ \
  10141. + int __locked; \
  10142. + local_lock_irq(lvar); \
  10143. + __locked = spin_trylock(lock); \
  10144. + if (!__locked) \
  10145. + local_unlock_irq(lvar); \
  10146. + __locked; \
  10147. + })
  10148. +
  10149. +#define local_spin_lock_irq(lvar, lock) \
  10150. + do { \
  10151. + local_lock_irq(lvar); \
  10152. + spin_lock(lock); \
  10153. + } while (0)
  10154. +
  10155. +#define local_spin_unlock_irq(lvar, lock) \
  10156. + do { \
  10157. + spin_unlock(lock); \
  10158. + local_unlock_irq(lvar); \
  10159. + } while (0)
  10160. +
  10161. +#define local_spin_lock_irqsave(lvar, lock, flags) \
  10162. + do { \
  10163. + local_lock_irqsave(lvar, flags); \
  10164. + spin_lock(lock); \
  10165. + } while (0)
  10166. +
  10167. +#define local_spin_unlock_irqrestore(lvar, lock, flags) \
  10168. + do { \
  10169. + spin_unlock(lock); \
  10170. + local_unlock_irqrestore(lvar, flags); \
  10171. + } while (0)
  10172. +
  10173. +#define get_locked_var(lvar, var) \
  10174. + (*({ \
  10175. + local_lock(lvar); \
  10176. + this_cpu_ptr(&var); \
  10177. + }))
  10178. +
  10179. +#define put_locked_var(lvar, var) local_unlock(lvar);
  10180. +
  10181. +#define local_lock_cpu(lvar) \
  10182. + ({ \
  10183. + local_lock(lvar); \
  10184. + smp_processor_id(); \
  10185. + })
  10186. +
  10187. +#define local_unlock_cpu(lvar) local_unlock(lvar)
  10188. +
  10189. +#else /* PREEMPT_RT_BASE */
  10190. +
  10191. +#define DEFINE_LOCAL_IRQ_LOCK(lvar) __typeof__(const int) lvar
  10192. +#define DECLARE_LOCAL_IRQ_LOCK(lvar) extern __typeof__(const int) lvar
  10193. +
  10194. +static inline void local_irq_lock_init(int lvar) { }
  10195. +
  10196. +#define local_lock(lvar) preempt_disable()
  10197. +#define local_unlock(lvar) preempt_enable()
  10198. +#define local_lock_irq(lvar) local_irq_disable()
  10199. +#define local_unlock_irq(lvar) local_irq_enable()
  10200. +#define local_lock_irqsave(lvar, flags) local_irq_save(flags)
  10201. +#define local_unlock_irqrestore(lvar, flags) local_irq_restore(flags)
  10202. +
  10203. +#define local_spin_trylock_irq(lvar, lock) spin_trylock_irq(lock)
  10204. +#define local_spin_lock_irq(lvar, lock) spin_lock_irq(lock)
  10205. +#define local_spin_unlock_irq(lvar, lock) spin_unlock_irq(lock)
  10206. +#define local_spin_lock_irqsave(lvar, lock, flags) \
  10207. + spin_lock_irqsave(lock, flags)
  10208. +#define local_spin_unlock_irqrestore(lvar, lock, flags) \
  10209. + spin_unlock_irqrestore(lock, flags)
  10210. +
  10211. +#define get_locked_var(lvar, var) get_cpu_var(var)
  10212. +#define put_locked_var(lvar, var) put_cpu_var(var)
  10213. +
  10214. +#define local_lock_cpu(lvar) get_cpu()
  10215. +#define local_unlock_cpu(lvar) put_cpu()
  10216. +
  10217. +#endif
  10218. +
  10219. +#endif
  10220. diff -Nur linux-4.1.20.orig/include/linux/mm_types.h linux-4.1.20/include/linux/mm_types.h
  10221. --- linux-4.1.20.orig/include/linux/mm_types.h 2016-03-17 19:11:03.000000000 +0100
  10222. +++ linux-4.1.20/include/linux/mm_types.h 2016-03-21 20:18:33.000000000 +0100
  10223. @@ -11,6 +11,7 @@
  10224. #include <linux/completion.h>
  10225. #include <linux/cpumask.h>
  10226. #include <linux/uprobes.h>
  10227. +#include <linux/rcupdate.h>
  10228. #include <linux/page-flags-layout.h>
  10229. #include <asm/page.h>
  10230. #include <asm/mmu.h>
  10231. @@ -453,6 +454,9 @@
  10232. bool tlb_flush_pending;
  10233. #endif
  10234. struct uprobes_state uprobes_state;
  10235. +#ifdef CONFIG_PREEMPT_RT_BASE
  10236. + struct rcu_head delayed_drop;
  10237. +#endif
  10238. #ifdef CONFIG_X86_INTEL_MPX
  10239. /* address of the bounds directory */
  10240. void __user *bd_addr;
  10241. diff -Nur linux-4.1.20.orig/include/linux/mutex.h linux-4.1.20/include/linux/mutex.h
  10242. --- linux-4.1.20.orig/include/linux/mutex.h 2016-03-17 19:11:03.000000000 +0100
  10243. +++ linux-4.1.20/include/linux/mutex.h 2016-03-21 20:18:33.000000000 +0100
  10244. @@ -19,6 +19,17 @@
  10245. #include <asm/processor.h>
  10246. #include <linux/osq_lock.h>
  10247. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  10248. +# define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
  10249. + , .dep_map = { .name = #lockname }
  10250. +#else
  10251. +# define __DEP_MAP_MUTEX_INITIALIZER(lockname)
  10252. +#endif
  10253. +
  10254. +#ifdef CONFIG_PREEMPT_RT_FULL
  10255. +# include <linux/mutex_rt.h>
  10256. +#else
  10257. +
  10258. /*
  10259. * Simple, straightforward mutexes with strict semantics:
  10260. *
  10261. @@ -99,13 +110,6 @@
  10262. static inline void mutex_destroy(struct mutex *lock) {}
  10263. #endif
  10264. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  10265. -# define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
  10266. - , .dep_map = { .name = #lockname }
  10267. -#else
  10268. -# define __DEP_MAP_MUTEX_INITIALIZER(lockname)
  10269. -#endif
  10270. -
  10271. #define __MUTEX_INITIALIZER(lockname) \
  10272. { .count = ATOMIC_INIT(1) \
  10273. , .wait_lock = __SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
  10274. @@ -173,6 +177,8 @@
  10275. extern int mutex_trylock(struct mutex *lock);
  10276. extern void mutex_unlock(struct mutex *lock);
  10277. +#endif /* !PREEMPT_RT_FULL */
  10278. +
  10279. extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);
  10280. #endif /* __LINUX_MUTEX_H */
  10281. diff -Nur linux-4.1.20.orig/include/linux/mutex_rt.h linux-4.1.20/include/linux/mutex_rt.h
  10282. --- linux-4.1.20.orig/include/linux/mutex_rt.h 1970-01-01 01:00:00.000000000 +0100
  10283. +++ linux-4.1.20/include/linux/mutex_rt.h 2016-03-21 20:18:33.000000000 +0100
  10284. @@ -0,0 +1,84 @@
  10285. +#ifndef __LINUX_MUTEX_RT_H
  10286. +#define __LINUX_MUTEX_RT_H
  10287. +
  10288. +#ifndef __LINUX_MUTEX_H
  10289. +#error "Please include mutex.h"
  10290. +#endif
  10291. +
  10292. +#include <linux/rtmutex.h>
  10293. +
  10294. +/* FIXME: Just for __lockfunc */
  10295. +#include <linux/spinlock.h>
  10296. +
  10297. +struct mutex {
  10298. + struct rt_mutex lock;
  10299. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  10300. + struct lockdep_map dep_map;
  10301. +#endif
  10302. +};
  10303. +
  10304. +#define __MUTEX_INITIALIZER(mutexname) \
  10305. + { \
  10306. + .lock = __RT_MUTEX_INITIALIZER(mutexname.lock) \
  10307. + __DEP_MAP_MUTEX_INITIALIZER(mutexname) \
  10308. + }
  10309. +
  10310. +#define DEFINE_MUTEX(mutexname) \
  10311. + struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
  10312. +
  10313. +extern void __mutex_do_init(struct mutex *lock, const char *name, struct lock_class_key *key);
  10314. +extern void __lockfunc _mutex_lock(struct mutex *lock);
  10315. +extern int __lockfunc _mutex_lock_interruptible(struct mutex *lock);
  10316. +extern int __lockfunc _mutex_lock_killable(struct mutex *lock);
  10317. +extern void __lockfunc _mutex_lock_nested(struct mutex *lock, int subclass);
  10318. +extern void __lockfunc _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest_lock);
  10319. +extern int __lockfunc _mutex_lock_interruptible_nested(struct mutex *lock, int subclass);
  10320. +extern int __lockfunc _mutex_lock_killable_nested(struct mutex *lock, int subclass);
  10321. +extern int __lockfunc _mutex_trylock(struct mutex *lock);
  10322. +extern void __lockfunc _mutex_unlock(struct mutex *lock);
  10323. +
  10324. +#define mutex_is_locked(l) rt_mutex_is_locked(&(l)->lock)
  10325. +#define mutex_lock(l) _mutex_lock(l)
  10326. +#define mutex_lock_interruptible(l) _mutex_lock_interruptible(l)
  10327. +#define mutex_lock_killable(l) _mutex_lock_killable(l)
  10328. +#define mutex_trylock(l) _mutex_trylock(l)
  10329. +#define mutex_unlock(l) _mutex_unlock(l)
  10330. +#define mutex_destroy(l) rt_mutex_destroy(&(l)->lock)
  10331. +
  10332. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  10333. +# define mutex_lock_nested(l, s) _mutex_lock_nested(l, s)
  10334. +# define mutex_lock_interruptible_nested(l, s) \
  10335. + _mutex_lock_interruptible_nested(l, s)
  10336. +# define mutex_lock_killable_nested(l, s) \
  10337. + _mutex_lock_killable_nested(l, s)
  10338. +
  10339. +# define mutex_lock_nest_lock(lock, nest_lock) \
  10340. +do { \
  10341. + typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
  10342. + _mutex_lock_nest_lock(lock, &(nest_lock)->dep_map); \
  10343. +} while (0)
  10344. +
  10345. +#else
  10346. +# define mutex_lock_nested(l, s) _mutex_lock(l)
  10347. +# define mutex_lock_interruptible_nested(l, s) \
  10348. + _mutex_lock_interruptible(l)
  10349. +# define mutex_lock_killable_nested(l, s) \
  10350. + _mutex_lock_killable(l)
  10351. +# define mutex_lock_nest_lock(lock, nest_lock) mutex_lock(lock)
  10352. +#endif
  10353. +
  10354. +# define mutex_init(mutex) \
  10355. +do { \
  10356. + static struct lock_class_key __key; \
  10357. + \
  10358. + rt_mutex_init(&(mutex)->lock); \
  10359. + __mutex_do_init((mutex), #mutex, &__key); \
  10360. +} while (0)
  10361. +
  10362. +# define __mutex_init(mutex, name, key) \
  10363. +do { \
  10364. + rt_mutex_init(&(mutex)->lock); \
  10365. + __mutex_do_init((mutex), name, key); \
  10366. +} while (0)
  10367. +
  10368. +#endif
  10369. diff -Nur linux-4.1.20.orig/include/linux/netdevice.h linux-4.1.20/include/linux/netdevice.h
  10370. --- linux-4.1.20.orig/include/linux/netdevice.h 2016-03-17 19:11:03.000000000 +0100
  10371. +++ linux-4.1.20/include/linux/netdevice.h 2016-03-21 20:18:33.000000000 +0100
  10372. @@ -2192,11 +2192,20 @@
  10373. void synchronize_net(void);
  10374. int init_dummy_netdev(struct net_device *dev);
  10375. +#ifdef CONFIG_PREEMPT_RT_FULL
  10376. +static inline int dev_recursion_level(void)
  10377. +{
  10378. + return current->xmit_recursion;
  10379. +}
  10380. +
  10381. +#else
  10382. +
  10383. DECLARE_PER_CPU(int, xmit_recursion);
  10384. static inline int dev_recursion_level(void)
  10385. {
  10386. return this_cpu_read(xmit_recursion);
  10387. }
  10388. +#endif
  10389. struct net_device *dev_get_by_index(struct net *net, int ifindex);
  10390. struct net_device *__dev_get_by_index(struct net *net, int ifindex);
  10391. @@ -2469,6 +2478,7 @@
  10392. unsigned int dropped;
  10393. struct sk_buff_head input_pkt_queue;
  10394. struct napi_struct backlog;
  10395. + struct sk_buff_head tofree_queue;
  10396. };
  10397. diff -Nur linux-4.1.20.orig/include/linux/netfilter/x_tables.h linux-4.1.20/include/linux/netfilter/x_tables.h
  10398. --- linux-4.1.20.orig/include/linux/netfilter/x_tables.h 2016-03-17 19:11:03.000000000 +0100
  10399. +++ linux-4.1.20/include/linux/netfilter/x_tables.h 2016-03-21 20:18:33.000000000 +0100
  10400. @@ -3,6 +3,7 @@
  10401. #include <linux/netdevice.h>
  10402. +#include <linux/locallock.h>
  10403. #include <uapi/linux/netfilter/x_tables.h>
  10404. /**
  10405. @@ -282,6 +283,8 @@
  10406. */
  10407. DECLARE_PER_CPU(seqcount_t, xt_recseq);
  10408. +DECLARE_LOCAL_IRQ_LOCK(xt_write_lock);
  10409. +
  10410. /**
  10411. * xt_write_recseq_begin - start of a write section
  10412. *
  10413. @@ -296,6 +299,9 @@
  10414. {
  10415. unsigned int addend;
  10416. + /* RT protection */
  10417. + local_lock(xt_write_lock);
  10418. +
  10419. /*
  10420. * Low order bit of sequence is set if we already
  10421. * called xt_write_recseq_begin().
  10422. @@ -326,6 +332,7 @@
  10423. /* this is kind of a write_seqcount_end(), but addend is 0 or 1 */
  10424. smp_wmb();
  10425. __this_cpu_add(xt_recseq.sequence, addend);
  10426. + local_unlock(xt_write_lock);
  10427. }
  10428. /*
  10429. diff -Nur linux-4.1.20.orig/include/linux/notifier.h linux-4.1.20/include/linux/notifier.h
  10430. --- linux-4.1.20.orig/include/linux/notifier.h 2016-03-17 19:11:03.000000000 +0100
  10431. +++ linux-4.1.20/include/linux/notifier.h 2016-03-21 20:18:33.000000000 +0100
  10432. @@ -6,7 +6,7 @@
  10433. *
  10434. * Alan Cox <Alan.Cox@linux.org>
  10435. */
  10436. -
  10437. +
  10438. #ifndef _LINUX_NOTIFIER_H
  10439. #define _LINUX_NOTIFIER_H
  10440. #include <linux/errno.h>
  10441. @@ -42,9 +42,7 @@
  10442. * in srcu_notifier_call_chain(): no cache bounces and no memory barriers.
  10443. * As compensation, srcu_notifier_chain_unregister() is rather expensive.
  10444. * SRCU notifier chains should be used when the chain will be called very
  10445. - * often but notifier_blocks will seldom be removed. Also, SRCU notifier
  10446. - * chains are slightly more difficult to use because they require special
  10447. - * runtime initialization.
  10448. + * often but notifier_blocks will seldom be removed.
  10449. */
  10450. typedef int (*notifier_fn_t)(struct notifier_block *nb,
  10451. @@ -88,7 +86,7 @@
  10452. (name)->head = NULL; \
  10453. } while (0)
  10454. -/* srcu_notifier_heads must be initialized and cleaned up dynamically */
  10455. +/* srcu_notifier_heads must be cleaned up dynamically */
  10456. extern void srcu_init_notifier_head(struct srcu_notifier_head *nh);
  10457. #define srcu_cleanup_notifier_head(name) \
  10458. cleanup_srcu_struct(&(name)->srcu);
  10459. @@ -101,7 +99,13 @@
  10460. .head = NULL }
  10461. #define RAW_NOTIFIER_INIT(name) { \
  10462. .head = NULL }
  10463. -/* srcu_notifier_heads cannot be initialized statically */
  10464. +
  10465. +#define SRCU_NOTIFIER_INIT(name, pcpu) \
  10466. + { \
  10467. + .mutex = __MUTEX_INITIALIZER(name.mutex), \
  10468. + .head = NULL, \
  10469. + .srcu = __SRCU_STRUCT_INIT(name.srcu, pcpu), \
  10470. + }
  10471. #define ATOMIC_NOTIFIER_HEAD(name) \
  10472. struct atomic_notifier_head name = \
  10473. @@ -113,6 +117,18 @@
  10474. struct raw_notifier_head name = \
  10475. RAW_NOTIFIER_INIT(name)
  10476. +#define _SRCU_NOTIFIER_HEAD(name, mod) \
  10477. + static DEFINE_PER_CPU(struct srcu_struct_array, \
  10478. + name##_head_srcu_array); \
  10479. + mod struct srcu_notifier_head name = \
  10480. + SRCU_NOTIFIER_INIT(name, name##_head_srcu_array)
  10481. +
  10482. +#define SRCU_NOTIFIER_HEAD(name) \
  10483. + _SRCU_NOTIFIER_HEAD(name, )
  10484. +
  10485. +#define SRCU_NOTIFIER_HEAD_STATIC(name) \
  10486. + _SRCU_NOTIFIER_HEAD(name, static)
  10487. +
  10488. #ifdef __KERNEL__
  10489. extern int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
  10490. @@ -182,12 +198,12 @@
  10491. /*
  10492. * Declared notifiers so far. I can imagine quite a few more chains
  10493. - * over time (eg laptop power reset chains, reboot chain (to clean
  10494. + * over time (eg laptop power reset chains, reboot chain (to clean
  10495. * device units up), device [un]mount chain, module load/unload chain,
  10496. - * low memory chain, screenblank chain (for plug in modular screenblankers)
  10497. + * low memory chain, screenblank chain (for plug in modular screenblankers)
  10498. * VC switch chains (for loadable kernel svgalib VC switch helpers) etc...
  10499. */
  10500. -
  10501. +
  10502. /* CPU notfiers are defined in include/linux/cpu.h. */
  10503. /* netdevice notifiers are defined in include/linux/netdevice.h */
  10504. diff -Nur linux-4.1.20.orig/include/linux/percpu.h linux-4.1.20/include/linux/percpu.h
  10505. --- linux-4.1.20.orig/include/linux/percpu.h 2016-03-17 19:11:03.000000000 +0100
  10506. +++ linux-4.1.20/include/linux/percpu.h 2016-03-21 20:18:33.000000000 +0100
  10507. @@ -24,6 +24,35 @@
  10508. PERCPU_MODULE_RESERVE)
  10509. #endif
  10510. +#ifdef CONFIG_PREEMPT_RT_FULL
  10511. +
  10512. +#define get_local_var(var) (*({ \
  10513. + migrate_disable(); \
  10514. + this_cpu_ptr(&var); }))
  10515. +
  10516. +#define put_local_var(var) do { \
  10517. + (void)&(var); \
  10518. + migrate_enable(); \
  10519. +} while (0)
  10520. +
  10521. +# define get_local_ptr(var) ({ \
  10522. + migrate_disable(); \
  10523. + this_cpu_ptr(var); })
  10524. +
  10525. +# define put_local_ptr(var) do { \
  10526. + (void)(var); \
  10527. + migrate_enable(); \
  10528. +} while (0)
  10529. +
  10530. +#else
  10531. +
  10532. +#define get_local_var(var) get_cpu_var(var)
  10533. +#define put_local_var(var) put_cpu_var(var)
  10534. +#define get_local_ptr(var) get_cpu_ptr(var)
  10535. +#define put_local_ptr(var) put_cpu_ptr(var)
  10536. +
  10537. +#endif
  10538. +
  10539. /* minimum unit size, also is the maximum supported allocation size */
  10540. #define PCPU_MIN_UNIT_SIZE PFN_ALIGN(32 << 10)
  10541. diff -Nur linux-4.1.20.orig/include/linux/pid.h linux-4.1.20/include/linux/pid.h
  10542. --- linux-4.1.20.orig/include/linux/pid.h 2016-03-17 19:11:03.000000000 +0100
  10543. +++ linux-4.1.20/include/linux/pid.h 2016-03-21 20:18:33.000000000 +0100
  10544. @@ -2,6 +2,7 @@
  10545. #define _LINUX_PID_H
  10546. #include <linux/rcupdate.h>
  10547. +#include <linux/atomic.h>
  10548. enum pid_type
  10549. {
  10550. 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
  10551. --- linux-4.1.20.orig/include/linux/platform_data/gpio-omap.h 2016-03-17 19:11:03.000000000 +0100
  10552. +++ linux-4.1.20/include/linux/platform_data/gpio-omap.h 2016-03-21 20:18:33.000000000 +0100
  10553. @@ -198,7 +198,6 @@
  10554. int bank_width; /* GPIO bank width */
  10555. int bank_stride; /* Only needed for omap1 MPUIO */
  10556. bool dbck_flag; /* dbck required or not - True for OMAP3&4 */
  10557. - bool loses_context; /* whether the bank would ever lose context */
  10558. bool is_mpuio; /* whether the bank is of type MPUIO */
  10559. u32 non_wakeup_gpios;
  10560. @@ -208,9 +207,17 @@
  10561. int (*get_context_loss_count)(struct device *dev);
  10562. };
  10563. +#if IS_BUILTIN(CONFIG_GPIO_OMAP)
  10564. extern void omap2_gpio_prepare_for_idle(int off_mode);
  10565. extern void omap2_gpio_resume_after_idle(void);
  10566. -extern void omap_set_gpio_debounce(int gpio, int enable);
  10567. -extern void omap_set_gpio_debounce_time(int gpio, int enable);
  10568. +#else
  10569. +static inline void omap2_gpio_prepare_for_idle(int off_mode)
  10570. +{
  10571. +}
  10572. +
  10573. +static inline void omap2_gpio_resume_after_idle(void)
  10574. +{
  10575. +}
  10576. +#endif
  10577. #endif
  10578. diff -Nur linux-4.1.20.orig/include/linux/preempt.h linux-4.1.20/include/linux/preempt.h
  10579. --- linux-4.1.20.orig/include/linux/preempt.h 2016-03-17 19:11:03.000000000 +0100
  10580. +++ linux-4.1.20/include/linux/preempt.h 2016-03-21 20:18:33.000000000 +0100
  10581. @@ -34,6 +34,20 @@
  10582. #define preempt_count_inc() preempt_count_add(1)
  10583. #define preempt_count_dec() preempt_count_sub(1)
  10584. +#ifdef CONFIG_PREEMPT_LAZY
  10585. +#define add_preempt_lazy_count(val) do { preempt_lazy_count() += (val); } while (0)
  10586. +#define sub_preempt_lazy_count(val) do { preempt_lazy_count() -= (val); } while (0)
  10587. +#define inc_preempt_lazy_count() add_preempt_lazy_count(1)
  10588. +#define dec_preempt_lazy_count() sub_preempt_lazy_count(1)
  10589. +#define preempt_lazy_count() (current_thread_info()->preempt_lazy_count)
  10590. +#else
  10591. +#define add_preempt_lazy_count(val) do { } while (0)
  10592. +#define sub_preempt_lazy_count(val) do { } while (0)
  10593. +#define inc_preempt_lazy_count() do { } while (0)
  10594. +#define dec_preempt_lazy_count() do { } while (0)
  10595. +#define preempt_lazy_count() (0)
  10596. +#endif
  10597. +
  10598. #ifdef CONFIG_PREEMPT_COUNT
  10599. #define preempt_disable() \
  10600. @@ -42,13 +56,25 @@
  10601. barrier(); \
  10602. } while (0)
  10603. +#define preempt_lazy_disable() \
  10604. +do { \
  10605. + inc_preempt_lazy_count(); \
  10606. + barrier(); \
  10607. +} while (0)
  10608. +
  10609. #define sched_preempt_enable_no_resched() \
  10610. do { \
  10611. barrier(); \
  10612. preempt_count_dec(); \
  10613. } while (0)
  10614. -#define preempt_enable_no_resched() sched_preempt_enable_no_resched()
  10615. +#ifdef CONFIG_PREEMPT_RT_BASE
  10616. +# define preempt_enable_no_resched() sched_preempt_enable_no_resched()
  10617. +# define preempt_check_resched_rt() preempt_check_resched()
  10618. +#else
  10619. +# define preempt_enable_no_resched() preempt_enable()
  10620. +# define preempt_check_resched_rt() barrier();
  10621. +#endif
  10622. #ifdef CONFIG_PREEMPT
  10623. #define preempt_enable() \
  10624. @@ -64,6 +90,13 @@
  10625. __preempt_schedule(); \
  10626. } while (0)
  10627. +#define preempt_lazy_enable() \
  10628. +do { \
  10629. + dec_preempt_lazy_count(); \
  10630. + barrier(); \
  10631. + preempt_check_resched(); \
  10632. +} while (0)
  10633. +
  10634. #else
  10635. #define preempt_enable() \
  10636. do { \
  10637. @@ -122,6 +155,7 @@
  10638. #define preempt_disable_notrace() barrier()
  10639. #define preempt_enable_no_resched_notrace() barrier()
  10640. #define preempt_enable_notrace() barrier()
  10641. +#define preempt_check_resched_rt() barrier()
  10642. #endif /* CONFIG_PREEMPT_COUNT */
  10643. @@ -141,10 +175,31 @@
  10644. } while (0)
  10645. #define preempt_fold_need_resched() \
  10646. do { \
  10647. - if (tif_need_resched()) \
  10648. + if (tif_need_resched_now()) \
  10649. set_preempt_need_resched(); \
  10650. } while (0)
  10651. +#ifdef CONFIG_PREEMPT_RT_FULL
  10652. +# define preempt_disable_rt() preempt_disable()
  10653. +# define preempt_enable_rt() preempt_enable()
  10654. +# define preempt_disable_nort() barrier()
  10655. +# define preempt_enable_nort() barrier()
  10656. +# ifdef CONFIG_SMP
  10657. + extern void migrate_disable(void);
  10658. + extern void migrate_enable(void);
  10659. +# else /* CONFIG_SMP */
  10660. +# define migrate_disable() barrier()
  10661. +# define migrate_enable() barrier()
  10662. +# endif /* CONFIG_SMP */
  10663. +#else
  10664. +# define preempt_disable_rt() barrier()
  10665. +# define preempt_enable_rt() barrier()
  10666. +# define preempt_disable_nort() preempt_disable()
  10667. +# define preempt_enable_nort() preempt_enable()
  10668. +# define migrate_disable() preempt_disable()
  10669. +# define migrate_enable() preempt_enable()
  10670. +#endif
  10671. +
  10672. #ifdef CONFIG_PREEMPT_NOTIFIERS
  10673. struct preempt_notifier;
  10674. diff -Nur linux-4.1.20.orig/include/linux/preempt_mask.h linux-4.1.20/include/linux/preempt_mask.h
  10675. --- linux-4.1.20.orig/include/linux/preempt_mask.h 2016-03-17 19:11:03.000000000 +0100
  10676. +++ linux-4.1.20/include/linux/preempt_mask.h 2016-03-21 20:18:33.000000000 +0100
  10677. @@ -44,16 +44,26 @@
  10678. #define HARDIRQ_OFFSET (1UL << HARDIRQ_SHIFT)
  10679. #define NMI_OFFSET (1UL << NMI_SHIFT)
  10680. -#define SOFTIRQ_DISABLE_OFFSET (2 * SOFTIRQ_OFFSET)
  10681. +#ifndef CONFIG_PREEMPT_RT_FULL
  10682. +# define SOFTIRQ_DISABLE_OFFSET (2 * SOFTIRQ_OFFSET)
  10683. +#else
  10684. +# define SOFTIRQ_DISABLE_OFFSET (0)
  10685. +#endif
  10686. #define PREEMPT_ACTIVE_BITS 1
  10687. #define PREEMPT_ACTIVE_SHIFT (NMI_SHIFT + NMI_BITS)
  10688. #define PREEMPT_ACTIVE (__IRQ_MASK(PREEMPT_ACTIVE_BITS) << PREEMPT_ACTIVE_SHIFT)
  10689. #define hardirq_count() (preempt_count() & HARDIRQ_MASK)
  10690. -#define softirq_count() (preempt_count() & SOFTIRQ_MASK)
  10691. #define irq_count() (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_MASK \
  10692. | NMI_MASK))
  10693. +#ifndef CONFIG_PREEMPT_RT_FULL
  10694. +# define softirq_count() (preempt_count() & SOFTIRQ_MASK)
  10695. +# define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET)
  10696. +#else
  10697. +# define softirq_count() (0UL)
  10698. +extern int in_serving_softirq(void);
  10699. +#endif
  10700. /*
  10701. * Are we doing bottom half or hardware interrupt processing?
  10702. @@ -64,7 +74,6 @@
  10703. #define in_irq() (hardirq_count())
  10704. #define in_softirq() (softirq_count())
  10705. #define in_interrupt() (irq_count())
  10706. -#define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET)
  10707. /*
  10708. * Are we in NMI context?
  10709. @@ -83,7 +92,11 @@
  10710. /*
  10711. * The preempt_count offset after spin_lock()
  10712. */
  10713. +#if !defined(CONFIG_PREEMPT_RT_FULL)
  10714. #define PREEMPT_LOCK_OFFSET PREEMPT_DISABLE_OFFSET
  10715. +#else
  10716. +#define PREEMPT_LOCK_OFFSET 0
  10717. +#endif
  10718. /*
  10719. * The preempt_count offset needed for things like:
  10720. diff -Nur linux-4.1.20.orig/include/linux/printk.h linux-4.1.20/include/linux/printk.h
  10721. --- linux-4.1.20.orig/include/linux/printk.h 2016-03-17 19:11:03.000000000 +0100
  10722. +++ linux-4.1.20/include/linux/printk.h 2016-03-21 20:18:33.000000000 +0100
  10723. @@ -115,9 +115,11 @@
  10724. #ifdef CONFIG_EARLY_PRINTK
  10725. extern asmlinkage __printf(1, 2)
  10726. void early_printk(const char *fmt, ...);
  10727. +extern void printk_kill(void);
  10728. #else
  10729. static inline __printf(1, 2) __cold
  10730. void early_printk(const char *s, ...) { }
  10731. +static inline void printk_kill(void) { }
  10732. #endif
  10733. typedef int(*printk_func_t)(const char *fmt, va_list args);
  10734. diff -Nur linux-4.1.20.orig/include/linux/radix-tree.h linux-4.1.20/include/linux/radix-tree.h
  10735. --- linux-4.1.20.orig/include/linux/radix-tree.h 2016-03-17 19:11:03.000000000 +0100
  10736. +++ linux-4.1.20/include/linux/radix-tree.h 2016-03-21 20:18:33.000000000 +0100
  10737. @@ -277,8 +277,13 @@
  10738. unsigned int radix_tree_gang_lookup_slot(struct radix_tree_root *root,
  10739. void ***results, unsigned long *indices,
  10740. unsigned long first_index, unsigned int max_items);
  10741. +#ifndef CONFIG_PREEMPT_RT_FULL
  10742. int radix_tree_preload(gfp_t gfp_mask);
  10743. int radix_tree_maybe_preload(gfp_t gfp_mask);
  10744. +#else
  10745. +static inline int radix_tree_preload(gfp_t gm) { return 0; }
  10746. +static inline int radix_tree_maybe_preload(gfp_t gfp_mask) { return 0; }
  10747. +#endif
  10748. void radix_tree_init(void);
  10749. void *radix_tree_tag_set(struct radix_tree_root *root,
  10750. unsigned long index, unsigned int tag);
  10751. @@ -303,7 +308,7 @@
  10752. static inline void radix_tree_preload_end(void)
  10753. {
  10754. - preempt_enable();
  10755. + preempt_enable_nort();
  10756. }
  10757. /**
  10758. diff -Nur linux-4.1.20.orig/include/linux/random.h linux-4.1.20/include/linux/random.h
  10759. --- linux-4.1.20.orig/include/linux/random.h 2016-03-17 19:11:03.000000000 +0100
  10760. +++ linux-4.1.20/include/linux/random.h 2016-03-21 20:18:33.000000000 +0100
  10761. @@ -11,7 +11,7 @@
  10762. extern void add_device_randomness(const void *, unsigned int);
  10763. extern void add_input_randomness(unsigned int type, unsigned int code,
  10764. unsigned int value);
  10765. -extern void add_interrupt_randomness(int irq, int irq_flags);
  10766. +extern void add_interrupt_randomness(int irq, int irq_flags, __u64 ip);
  10767. extern void get_random_bytes(void *buf, int nbytes);
  10768. extern void get_random_bytes_arch(void *buf, int nbytes);
  10769. diff -Nur linux-4.1.20.orig/include/linux/rcupdate.h linux-4.1.20/include/linux/rcupdate.h
  10770. --- linux-4.1.20.orig/include/linux/rcupdate.h 2016-03-17 19:11:03.000000000 +0100
  10771. +++ linux-4.1.20/include/linux/rcupdate.h 2016-03-21 20:18:33.000000000 +0100
  10772. @@ -167,6 +167,9 @@
  10773. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  10774. +#ifdef CONFIG_PREEMPT_RT_FULL
  10775. +#define call_rcu_bh call_rcu
  10776. +#else
  10777. /**
  10778. * call_rcu_bh() - Queue an RCU for invocation after a quicker grace period.
  10779. * @head: structure to be used for queueing the RCU updates.
  10780. @@ -190,6 +193,7 @@
  10781. */
  10782. void call_rcu_bh(struct rcu_head *head,
  10783. void (*func)(struct rcu_head *head));
  10784. +#endif
  10785. /**
  10786. * call_rcu_sched() - Queue an RCU for invocation after sched grace period.
  10787. @@ -260,6 +264,11 @@
  10788. * types of kernel builds, the rcu_read_lock() nesting depth is unknowable.
  10789. */
  10790. #define rcu_preempt_depth() (current->rcu_read_lock_nesting)
  10791. +#ifndef CONFIG_PREEMPT_RT_FULL
  10792. +#define sched_rcu_preempt_depth() rcu_preempt_depth()
  10793. +#else
  10794. +static inline int sched_rcu_preempt_depth(void) { return 0; }
  10795. +#endif
  10796. #else /* #ifdef CONFIG_PREEMPT_RCU */
  10797. @@ -283,6 +292,8 @@
  10798. return 0;
  10799. }
  10800. +#define sched_rcu_preempt_depth() rcu_preempt_depth()
  10801. +
  10802. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  10803. /* Internal to kernel */
  10804. @@ -463,7 +474,14 @@
  10805. int debug_lockdep_rcu_enabled(void);
  10806. int rcu_read_lock_held(void);
  10807. +#ifdef CONFIG_PREEMPT_RT_FULL
  10808. +static inline int rcu_read_lock_bh_held(void)
  10809. +{
  10810. + return rcu_read_lock_held();
  10811. +}
  10812. +#else
  10813. int rcu_read_lock_bh_held(void);
  10814. +#endif
  10815. /**
  10816. * rcu_read_lock_sched_held() - might we be in RCU-sched read-side critical section?
  10817. @@ -990,10 +1008,14 @@
  10818. static inline void rcu_read_lock_bh(void)
  10819. {
  10820. local_bh_disable();
  10821. +#ifdef CONFIG_PREEMPT_RT_FULL
  10822. + rcu_read_lock();
  10823. +#else
  10824. __acquire(RCU_BH);
  10825. rcu_lock_acquire(&rcu_bh_lock_map);
  10826. rcu_lockdep_assert(rcu_is_watching(),
  10827. "rcu_read_lock_bh() used illegally while idle");
  10828. +#endif
  10829. }
  10830. /*
  10831. @@ -1003,10 +1025,14 @@
  10832. */
  10833. static inline void rcu_read_unlock_bh(void)
  10834. {
  10835. +#ifdef CONFIG_PREEMPT_RT_FULL
  10836. + rcu_read_unlock();
  10837. +#else
  10838. rcu_lockdep_assert(rcu_is_watching(),
  10839. "rcu_read_unlock_bh() used illegally while idle");
  10840. rcu_lock_release(&rcu_bh_lock_map);
  10841. __release(RCU_BH);
  10842. +#endif
  10843. local_bh_enable();
  10844. }
  10845. diff -Nur linux-4.1.20.orig/include/linux/rcutree.h linux-4.1.20/include/linux/rcutree.h
  10846. --- linux-4.1.20.orig/include/linux/rcutree.h 2016-03-17 19:11:03.000000000 +0100
  10847. +++ linux-4.1.20/include/linux/rcutree.h 2016-03-21 20:18:33.000000000 +0100
  10848. @@ -46,7 +46,11 @@
  10849. rcu_note_context_switch();
  10850. }
  10851. +#ifdef CONFIG_PREEMPT_RT_FULL
  10852. +# define synchronize_rcu_bh synchronize_rcu
  10853. +#else
  10854. void synchronize_rcu_bh(void);
  10855. +#endif
  10856. void synchronize_sched_expedited(void);
  10857. void synchronize_rcu_expedited(void);
  10858. @@ -74,7 +78,11 @@
  10859. }
  10860. void rcu_barrier(void);
  10861. +#ifdef CONFIG_PREEMPT_RT_FULL
  10862. +# define rcu_barrier_bh rcu_barrier
  10863. +#else
  10864. void rcu_barrier_bh(void);
  10865. +#endif
  10866. void rcu_barrier_sched(void);
  10867. unsigned long get_state_synchronize_rcu(void);
  10868. void cond_synchronize_rcu(unsigned long oldstate);
  10869. @@ -85,12 +93,10 @@
  10870. unsigned long rcu_batches_started_bh(void);
  10871. unsigned long rcu_batches_started_sched(void);
  10872. unsigned long rcu_batches_completed(void);
  10873. -unsigned long rcu_batches_completed_bh(void);
  10874. unsigned long rcu_batches_completed_sched(void);
  10875. void show_rcu_gp_kthreads(void);
  10876. void rcu_force_quiescent_state(void);
  10877. -void rcu_bh_force_quiescent_state(void);
  10878. void rcu_sched_force_quiescent_state(void);
  10879. void exit_rcu(void);
  10880. @@ -100,6 +106,14 @@
  10881. bool rcu_is_watching(void);
  10882. +#ifndef CONFIG_PREEMPT_RT_FULL
  10883. +void rcu_bh_force_quiescent_state(void);
  10884. +unsigned long rcu_batches_completed_bh(void);
  10885. +#else
  10886. +# define rcu_bh_force_quiescent_state rcu_force_quiescent_state
  10887. +# define rcu_batches_completed_bh rcu_batches_completed
  10888. +#endif
  10889. +
  10890. void rcu_all_qs(void);
  10891. #endif /* __LINUX_RCUTREE_H */
  10892. diff -Nur linux-4.1.20.orig/include/linux/rtmutex.h linux-4.1.20/include/linux/rtmutex.h
  10893. --- linux-4.1.20.orig/include/linux/rtmutex.h 2016-03-17 19:11:03.000000000 +0100
  10894. +++ linux-4.1.20/include/linux/rtmutex.h 2016-03-21 20:18:33.000000000 +0100
  10895. @@ -14,10 +14,14 @@
  10896. #include <linux/linkage.h>
  10897. #include <linux/rbtree.h>
  10898. -#include <linux/spinlock_types.h>
  10899. +#include <linux/spinlock_types_raw.h>
  10900. extern int max_lock_depth; /* for sysctl */
  10901. +#ifdef CONFIG_DEBUG_MUTEXES
  10902. +#include <linux/debug_locks.h>
  10903. +#endif
  10904. +
  10905. /**
  10906. * The rt_mutex structure
  10907. *
  10908. @@ -31,8 +35,8 @@
  10909. struct rb_root waiters;
  10910. struct rb_node *waiters_leftmost;
  10911. struct task_struct *owner;
  10912. -#ifdef CONFIG_DEBUG_RT_MUTEXES
  10913. int save_state;
  10914. +#ifdef CONFIG_DEBUG_RT_MUTEXES
  10915. const char *name, *file;
  10916. int line;
  10917. void *magic;
  10918. @@ -55,22 +59,33 @@
  10919. # define rt_mutex_debug_check_no_locks_held(task) do { } while (0)
  10920. #endif
  10921. +# define rt_mutex_init(mutex) \
  10922. + do { \
  10923. + raw_spin_lock_init(&(mutex)->wait_lock); \
  10924. + __rt_mutex_init(mutex, #mutex); \
  10925. + } while (0)
  10926. +
  10927. #ifdef CONFIG_DEBUG_RT_MUTEXES
  10928. # define __DEBUG_RT_MUTEX_INITIALIZER(mutexname) \
  10929. , .name = #mutexname, .file = __FILE__, .line = __LINE__
  10930. -# define rt_mutex_init(mutex) __rt_mutex_init(mutex, __func__)
  10931. extern void rt_mutex_debug_task_free(struct task_struct *tsk);
  10932. #else
  10933. # define __DEBUG_RT_MUTEX_INITIALIZER(mutexname)
  10934. -# define rt_mutex_init(mutex) __rt_mutex_init(mutex, NULL)
  10935. # define rt_mutex_debug_task_free(t) do { } while (0)
  10936. #endif
  10937. -#define __RT_MUTEX_INITIALIZER(mutexname) \
  10938. - { .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(mutexname.wait_lock) \
  10939. +#define __RT_MUTEX_INITIALIZER_PLAIN(mutexname) \
  10940. + .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(mutexname.wait_lock) \
  10941. , .waiters = RB_ROOT \
  10942. , .owner = NULL \
  10943. - __DEBUG_RT_MUTEX_INITIALIZER(mutexname)}
  10944. + __DEBUG_RT_MUTEX_INITIALIZER(mutexname)
  10945. +
  10946. +#define __RT_MUTEX_INITIALIZER(mutexname) \
  10947. + { __RT_MUTEX_INITIALIZER_PLAIN(mutexname) }
  10948. +
  10949. +#define __RT_MUTEX_INITIALIZER_SAVE_STATE(mutexname) \
  10950. + { __RT_MUTEX_INITIALIZER_PLAIN(mutexname) \
  10951. + , .save_state = 1 }
  10952. #define DEFINE_RT_MUTEX(mutexname) \
  10953. struct rt_mutex mutexname = __RT_MUTEX_INITIALIZER(mutexname)
  10954. @@ -91,6 +106,7 @@
  10955. extern void rt_mutex_lock(struct rt_mutex *lock);
  10956. extern int rt_mutex_lock_interruptible(struct rt_mutex *lock);
  10957. +extern int rt_mutex_lock_killable(struct rt_mutex *lock);
  10958. extern int rt_mutex_timed_lock(struct rt_mutex *lock,
  10959. struct hrtimer_sleeper *timeout);
  10960. diff -Nur linux-4.1.20.orig/include/linux/rwlock_rt.h linux-4.1.20/include/linux/rwlock_rt.h
  10961. --- linux-4.1.20.orig/include/linux/rwlock_rt.h 1970-01-01 01:00:00.000000000 +0100
  10962. +++ linux-4.1.20/include/linux/rwlock_rt.h 2016-03-21 20:18:33.000000000 +0100
  10963. @@ -0,0 +1,99 @@
  10964. +#ifndef __LINUX_RWLOCK_RT_H
  10965. +#define __LINUX_RWLOCK_RT_H
  10966. +
  10967. +#ifndef __LINUX_SPINLOCK_H
  10968. +#error Do not include directly. Use spinlock.h
  10969. +#endif
  10970. +
  10971. +#define rwlock_init(rwl) \
  10972. +do { \
  10973. + static struct lock_class_key __key; \
  10974. + \
  10975. + rt_mutex_init(&(rwl)->lock); \
  10976. + __rt_rwlock_init(rwl, #rwl, &__key); \
  10977. +} while (0)
  10978. +
  10979. +extern void __lockfunc rt_write_lock(rwlock_t *rwlock);
  10980. +extern void __lockfunc rt_read_lock(rwlock_t *rwlock);
  10981. +extern int __lockfunc rt_write_trylock(rwlock_t *rwlock);
  10982. +extern int __lockfunc rt_write_trylock_irqsave(rwlock_t *trylock, unsigned long *flags);
  10983. +extern int __lockfunc rt_read_trylock(rwlock_t *rwlock);
  10984. +extern void __lockfunc rt_write_unlock(rwlock_t *rwlock);
  10985. +extern void __lockfunc rt_read_unlock(rwlock_t *rwlock);
  10986. +extern unsigned long __lockfunc rt_write_lock_irqsave(rwlock_t *rwlock);
  10987. +extern unsigned long __lockfunc rt_read_lock_irqsave(rwlock_t *rwlock);
  10988. +extern void __rt_rwlock_init(rwlock_t *rwlock, char *name, struct lock_class_key *key);
  10989. +
  10990. +#define read_trylock(lock) __cond_lock(lock, rt_read_trylock(lock))
  10991. +#define write_trylock(lock) __cond_lock(lock, rt_write_trylock(lock))
  10992. +
  10993. +#define write_trylock_irqsave(lock, flags) \
  10994. + __cond_lock(lock, rt_write_trylock_irqsave(lock, &flags))
  10995. +
  10996. +#define read_lock_irqsave(lock, flags) \
  10997. + do { \
  10998. + typecheck(unsigned long, flags); \
  10999. + flags = rt_read_lock_irqsave(lock); \
  11000. + } while (0)
  11001. +
  11002. +#define write_lock_irqsave(lock, flags) \
  11003. + do { \
  11004. + typecheck(unsigned long, flags); \
  11005. + flags = rt_write_lock_irqsave(lock); \
  11006. + } while (0)
  11007. +
  11008. +#define read_lock(lock) rt_read_lock(lock)
  11009. +
  11010. +#define read_lock_bh(lock) \
  11011. + do { \
  11012. + local_bh_disable(); \
  11013. + rt_read_lock(lock); \
  11014. + } while (0)
  11015. +
  11016. +#define read_lock_irq(lock) read_lock(lock)
  11017. +
  11018. +#define write_lock(lock) rt_write_lock(lock)
  11019. +
  11020. +#define write_lock_bh(lock) \
  11021. + do { \
  11022. + local_bh_disable(); \
  11023. + rt_write_lock(lock); \
  11024. + } while (0)
  11025. +
  11026. +#define write_lock_irq(lock) write_lock(lock)
  11027. +
  11028. +#define read_unlock(lock) rt_read_unlock(lock)
  11029. +
  11030. +#define read_unlock_bh(lock) \
  11031. + do { \
  11032. + rt_read_unlock(lock); \
  11033. + local_bh_enable(); \
  11034. + } while (0)
  11035. +
  11036. +#define read_unlock_irq(lock) read_unlock(lock)
  11037. +
  11038. +#define write_unlock(lock) rt_write_unlock(lock)
  11039. +
  11040. +#define write_unlock_bh(lock) \
  11041. + do { \
  11042. + rt_write_unlock(lock); \
  11043. + local_bh_enable(); \
  11044. + } while (0)
  11045. +
  11046. +#define write_unlock_irq(lock) write_unlock(lock)
  11047. +
  11048. +#define read_unlock_irqrestore(lock, flags) \
  11049. + do { \
  11050. + typecheck(unsigned long, flags); \
  11051. + (void) flags; \
  11052. + rt_read_unlock(lock); \
  11053. + } while (0)
  11054. +
  11055. +#define write_unlock_irqrestore(lock, flags) \
  11056. + do { \
  11057. + typecheck(unsigned long, flags); \
  11058. + (void) flags; \
  11059. + rt_write_unlock(lock); \
  11060. + } while (0)
  11061. +
  11062. +#endif
  11063. diff -Nur linux-4.1.20.orig/include/linux/rwlock_types.h linux-4.1.20/include/linux/rwlock_types.h
  11064. --- linux-4.1.20.orig/include/linux/rwlock_types.h 2016-03-17 19:11:03.000000000 +0100
  11065. +++ linux-4.1.20/include/linux/rwlock_types.h 2016-03-21 20:18:33.000000000 +0100
  11066. @@ -1,6 +1,10 @@
  11067. #ifndef __LINUX_RWLOCK_TYPES_H
  11068. #define __LINUX_RWLOCK_TYPES_H
  11069. +#if !defined(__LINUX_SPINLOCK_TYPES_H)
  11070. +# error "Do not include directly, include spinlock_types.h"
  11071. +#endif
  11072. +
  11073. /*
  11074. * include/linux/rwlock_types.h - generic rwlock type definitions
  11075. * and initializers
  11076. @@ -43,6 +47,7 @@
  11077. RW_DEP_MAP_INIT(lockname) }
  11078. #endif
  11079. -#define DEFINE_RWLOCK(x) rwlock_t x = __RW_LOCK_UNLOCKED(x)
  11080. +#define DEFINE_RWLOCK(name) \
  11081. + rwlock_t name __cacheline_aligned_in_smp = __RW_LOCK_UNLOCKED(name)
  11082. #endif /* __LINUX_RWLOCK_TYPES_H */
  11083. diff -Nur linux-4.1.20.orig/include/linux/rwlock_types_rt.h linux-4.1.20/include/linux/rwlock_types_rt.h
  11084. --- linux-4.1.20.orig/include/linux/rwlock_types_rt.h 1970-01-01 01:00:00.000000000 +0100
  11085. +++ linux-4.1.20/include/linux/rwlock_types_rt.h 2016-03-21 20:18:33.000000000 +0100
  11086. @@ -0,0 +1,33 @@
  11087. +#ifndef __LINUX_RWLOCK_TYPES_RT_H
  11088. +#define __LINUX_RWLOCK_TYPES_RT_H
  11089. +
  11090. +#ifndef __LINUX_SPINLOCK_TYPES_H
  11091. +#error "Do not include directly. Include spinlock_types.h instead"
  11092. +#endif
  11093. +
  11094. +/*
  11095. + * rwlocks - rtmutex which allows single reader recursion
  11096. + */
  11097. +typedef struct {
  11098. + struct rt_mutex lock;
  11099. + int read_depth;
  11100. + unsigned int break_lock;
  11101. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  11102. + struct lockdep_map dep_map;
  11103. +#endif
  11104. +} rwlock_t;
  11105. +
  11106. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  11107. +# define RW_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }
  11108. +#else
  11109. +# define RW_DEP_MAP_INIT(lockname)
  11110. +#endif
  11111. +
  11112. +#define __RW_LOCK_UNLOCKED(name) \
  11113. + { .lock = __RT_MUTEX_INITIALIZER_SAVE_STATE(name.lock), \
  11114. + RW_DEP_MAP_INIT(name) }
  11115. +
  11116. +#define DEFINE_RWLOCK(name) \
  11117. + rwlock_t name __cacheline_aligned_in_smp = __RW_LOCK_UNLOCKED(name)
  11118. +
  11119. +#endif
  11120. diff -Nur linux-4.1.20.orig/include/linux/rwsem.h linux-4.1.20/include/linux/rwsem.h
  11121. --- linux-4.1.20.orig/include/linux/rwsem.h 2016-03-17 19:11:03.000000000 +0100
  11122. +++ linux-4.1.20/include/linux/rwsem.h 2016-03-21 20:18:33.000000000 +0100
  11123. @@ -18,6 +18,10 @@
  11124. #include <linux/osq_lock.h>
  11125. #endif
  11126. +#ifdef CONFIG_PREEMPT_RT_FULL
  11127. +#include <linux/rwsem_rt.h>
  11128. +#else /* PREEMPT_RT_FULL */
  11129. +
  11130. struct rw_semaphore;
  11131. #ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
  11132. @@ -177,4 +181,6 @@
  11133. # define up_read_non_owner(sem) up_read(sem)
  11134. #endif
  11135. +#endif /* !PREEMPT_RT_FULL */
  11136. +
  11137. #endif /* _LINUX_RWSEM_H */
  11138. diff -Nur linux-4.1.20.orig/include/linux/rwsem_rt.h linux-4.1.20/include/linux/rwsem_rt.h
  11139. --- linux-4.1.20.orig/include/linux/rwsem_rt.h 1970-01-01 01:00:00.000000000 +0100
  11140. +++ linux-4.1.20/include/linux/rwsem_rt.h 2016-03-21 20:18:33.000000000 +0100
  11141. @@ -0,0 +1,140 @@
  11142. +#ifndef _LINUX_RWSEM_RT_H
  11143. +#define _LINUX_RWSEM_RT_H
  11144. +
  11145. +#ifndef _LINUX_RWSEM_H
  11146. +#error "Include rwsem.h"
  11147. +#endif
  11148. +
  11149. +/*
  11150. + * RW-semaphores are a spinlock plus a reader-depth count.
  11151. + *
  11152. + * Note that the semantics are different from the usual
  11153. + * Linux rw-sems, in PREEMPT_RT mode we do not allow
  11154. + * multiple readers to hold the lock at once, we only allow
  11155. + * a read-lock owner to read-lock recursively. This is
  11156. + * better for latency, makes the implementation inherently
  11157. + * fair and makes it simpler as well.
  11158. + */
  11159. +
  11160. +#include <linux/rtmutex.h>
  11161. +
  11162. +struct rw_semaphore {
  11163. + struct rt_mutex lock;
  11164. + int read_depth;
  11165. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  11166. + struct lockdep_map dep_map;
  11167. +#endif
  11168. +};
  11169. +
  11170. +#define __RWSEM_INITIALIZER(name) \
  11171. + { .lock = __RT_MUTEX_INITIALIZER(name.lock), \
  11172. + RW_DEP_MAP_INIT(name) }
  11173. +
  11174. +#define DECLARE_RWSEM(lockname) \
  11175. + struct rw_semaphore lockname = __RWSEM_INITIALIZER(lockname)
  11176. +
  11177. +extern void __rt_rwsem_init(struct rw_semaphore *rwsem, const char *name,
  11178. + struct lock_class_key *key);
  11179. +
  11180. +#define __rt_init_rwsem(sem, name, key) \
  11181. + do { \
  11182. + rt_mutex_init(&(sem)->lock); \
  11183. + __rt_rwsem_init((sem), (name), (key));\
  11184. + } while (0)
  11185. +
  11186. +#define __init_rwsem(sem, name, key) __rt_init_rwsem(sem, name, key)
  11187. +
  11188. +# define rt_init_rwsem(sem) \
  11189. +do { \
  11190. + static struct lock_class_key __key; \
  11191. + \
  11192. + __rt_init_rwsem((sem), #sem, &__key); \
  11193. +} while (0)
  11194. +
  11195. +extern void rt_down_write(struct rw_semaphore *rwsem);
  11196. +extern void rt_down_read_nested(struct rw_semaphore *rwsem, int subclass);
  11197. +extern void rt_down_write_nested(struct rw_semaphore *rwsem, int subclass);
  11198. +extern void rt_down_write_nested_lock(struct rw_semaphore *rwsem,
  11199. + struct lockdep_map *nest);
  11200. +extern void rt_down_read(struct rw_semaphore *rwsem);
  11201. +extern int rt_down_write_trylock(struct rw_semaphore *rwsem);
  11202. +extern int rt_down_read_trylock(struct rw_semaphore *rwsem);
  11203. +extern void __rt_up_read(struct rw_semaphore *rwsem);
  11204. +extern void rt_up_read(struct rw_semaphore *rwsem);
  11205. +extern void rt_up_write(struct rw_semaphore *rwsem);
  11206. +extern void rt_downgrade_write(struct rw_semaphore *rwsem);
  11207. +
  11208. +#define init_rwsem(sem) rt_init_rwsem(sem)
  11209. +#define rwsem_is_locked(s) rt_mutex_is_locked(&(s)->lock)
  11210. +
  11211. +static inline int rwsem_is_contended(struct rw_semaphore *sem)
  11212. +{
  11213. + /* rt_mutex_has_waiters() */
  11214. + return !RB_EMPTY_ROOT(&sem->lock.waiters);
  11215. +}
  11216. +
  11217. +static inline void down_read(struct rw_semaphore *sem)
  11218. +{
  11219. + rt_down_read(sem);
  11220. +}
  11221. +
  11222. +static inline int down_read_trylock(struct rw_semaphore *sem)
  11223. +{
  11224. + return rt_down_read_trylock(sem);
  11225. +}
  11226. +
  11227. +static inline void down_write(struct rw_semaphore *sem)
  11228. +{
  11229. + rt_down_write(sem);
  11230. +}
  11231. +
  11232. +static inline int down_write_trylock(struct rw_semaphore *sem)
  11233. +{
  11234. + return rt_down_write_trylock(sem);
  11235. +}
  11236. +
  11237. +static inline void __up_read(struct rw_semaphore *sem)
  11238. +{
  11239. + __rt_up_read(sem);
  11240. +}
  11241. +
  11242. +static inline void up_read(struct rw_semaphore *sem)
  11243. +{
  11244. + rt_up_read(sem);
  11245. +}
  11246. +
  11247. +static inline void up_write(struct rw_semaphore *sem)
  11248. +{
  11249. + rt_up_write(sem);
  11250. +}
  11251. +
  11252. +static inline void downgrade_write(struct rw_semaphore *sem)
  11253. +{
  11254. + rt_downgrade_write(sem);
  11255. +}
  11256. +
  11257. +static inline void down_read_nested(struct rw_semaphore *sem, int subclass)
  11258. +{
  11259. + return rt_down_read_nested(sem, subclass);
  11260. +}
  11261. +
  11262. +static inline void down_write_nested(struct rw_semaphore *sem, int subclass)
  11263. +{
  11264. + rt_down_write_nested(sem, subclass);
  11265. +}
  11266. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  11267. +static inline void down_write_nest_lock(struct rw_semaphore *sem,
  11268. + struct rw_semaphore *nest_lock)
  11269. +{
  11270. + rt_down_write_nested_lock(sem, &nest_lock->dep_map);
  11271. +}
  11272. +
  11273. +#else
  11274. +
  11275. +static inline void down_write_nest_lock(struct rw_semaphore *sem,
  11276. + struct rw_semaphore *nest_lock)
  11277. +{
  11278. + rt_down_write_nested_lock(sem, NULL);
  11279. +}
  11280. +#endif
  11281. +#endif
  11282. diff -Nur linux-4.1.20.orig/include/linux/sched.h linux-4.1.20/include/linux/sched.h
  11283. --- linux-4.1.20.orig/include/linux/sched.h 2016-03-17 19:11:03.000000000 +0100
  11284. +++ linux-4.1.20/include/linux/sched.h 2016-03-21 20:18:33.000000000 +0100
  11285. @@ -26,6 +26,7 @@
  11286. #include <linux/nodemask.h>
  11287. #include <linux/mm_types.h>
  11288. #include <linux/preempt_mask.h>
  11289. +#include <asm/kmap_types.h>
  11290. #include <asm/page.h>
  11291. #include <asm/ptrace.h>
  11292. @@ -175,8 +176,6 @@
  11293. extern void calc_global_load(unsigned long ticks);
  11294. extern void update_cpu_load_nohz(void);
  11295. -extern unsigned long get_parent_ip(unsigned long addr);
  11296. -
  11297. extern void dump_cpu_task(int cpu);
  11298. struct seq_file;
  11299. @@ -234,10 +233,7 @@
  11300. TASK_UNINTERRUPTIBLE | __TASK_STOPPED | \
  11301. __TASK_TRACED | EXIT_ZOMBIE | EXIT_DEAD)
  11302. -#define task_is_traced(task) ((task->state & __TASK_TRACED) != 0)
  11303. #define task_is_stopped(task) ((task->state & __TASK_STOPPED) != 0)
  11304. -#define task_is_stopped_or_traced(task) \
  11305. - ((task->state & (__TASK_STOPPED | __TASK_TRACED)) != 0)
  11306. #define task_contributes_to_load(task) \
  11307. ((task->state & TASK_UNINTERRUPTIBLE) != 0 && \
  11308. (task->flags & PF_FROZEN) == 0)
  11309. @@ -302,6 +298,11 @@
  11310. #endif
  11311. +#define __set_current_state_no_track(state_value) \
  11312. + do { current->state = (state_value); } while (0)
  11313. +#define set_current_state_no_track(state_value) \
  11314. + set_mb(current->state, (state_value))
  11315. +
  11316. /* Task command name length */
  11317. #define TASK_COMM_LEN 16
  11318. @@ -901,6 +902,50 @@
  11319. #define SCHED_CAPACITY_SCALE (1L << SCHED_CAPACITY_SHIFT)
  11320. /*
  11321. + * Wake-queues are lists of tasks with a pending wakeup, whose
  11322. + * callers have already marked the task as woken internally,
  11323. + * and can thus carry on. A common use case is being able to
  11324. + * do the wakeups once the corresponding user lock as been
  11325. + * released.
  11326. + *
  11327. + * We hold reference to each task in the list across the wakeup,
  11328. + * thus guaranteeing that the memory is still valid by the time
  11329. + * the actual wakeups are performed in wake_up_q().
  11330. + *
  11331. + * One per task suffices, because there's never a need for a task to be
  11332. + * in two wake queues simultaneously; it is forbidden to abandon a task
  11333. + * in a wake queue (a call to wake_up_q() _must_ follow), so if a task is
  11334. + * already in a wake queue, the wakeup will happen soon and the second
  11335. + * waker can just skip it.
  11336. + *
  11337. + * The WAKE_Q macro declares and initializes the list head.
  11338. + * wake_up_q() does NOT reinitialize the list; it's expected to be
  11339. + * called near the end of a function, where the fact that the queue is
  11340. + * not used again will be easy to see by inspection.
  11341. + *
  11342. + * Note that this can cause spurious wakeups. schedule() callers
  11343. + * must ensure the call is done inside a loop, confirming that the
  11344. + * wakeup condition has in fact occurred.
  11345. + */
  11346. +struct wake_q_node {
  11347. + struct wake_q_node *next;
  11348. +};
  11349. +
  11350. +struct wake_q_head {
  11351. + struct wake_q_node *first;
  11352. + struct wake_q_node **lastp;
  11353. +};
  11354. +
  11355. +#define WAKE_Q_TAIL ((struct wake_q_node *) 0x01)
  11356. +
  11357. +#define WAKE_Q(name) \
  11358. + struct wake_q_head name = { WAKE_Q_TAIL, &name.first }
  11359. +
  11360. +extern void wake_q_add(struct wake_q_head *head,
  11361. + struct task_struct *task);
  11362. +extern void wake_up_q(struct wake_q_head *head);
  11363. +
  11364. +/*
  11365. * sched-domains (multiprocessor balancing) declarations:
  11366. */
  11367. #ifdef CONFIG_SMP
  11368. @@ -1292,6 +1337,7 @@
  11369. struct task_struct {
  11370. volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
  11371. + volatile long saved_state; /* saved state for "spinlock sleepers" */
  11372. void *stack;
  11373. atomic_t usage;
  11374. unsigned int flags; /* per process flags, defined below */
  11375. @@ -1328,6 +1374,12 @@
  11376. #endif
  11377. unsigned int policy;
  11378. +#ifdef CONFIG_PREEMPT_RT_FULL
  11379. + int migrate_disable;
  11380. +# ifdef CONFIG_SCHED_DEBUG
  11381. + int migrate_disable_atomic;
  11382. +# endif
  11383. +#endif
  11384. int nr_cpus_allowed;
  11385. cpumask_t cpus_allowed;
  11386. @@ -1435,7 +1487,8 @@
  11387. struct cputime prev_cputime;
  11388. #endif
  11389. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  11390. - seqlock_t vtime_seqlock;
  11391. + raw_spinlock_t vtime_lock;
  11392. + seqcount_t vtime_seq;
  11393. unsigned long long vtime_snap;
  11394. enum {
  11395. VTIME_SLEEPING = 0,
  11396. @@ -1451,6 +1504,9 @@
  11397. struct task_cputime cputime_expires;
  11398. struct list_head cpu_timers[3];
  11399. +#ifdef CONFIG_PREEMPT_RT_BASE
  11400. + struct task_struct *posix_timer_list;
  11401. +#endif
  11402. /* process credentials */
  11403. const struct cred __rcu *real_cred; /* objective and real subjective task
  11404. @@ -1483,10 +1539,15 @@
  11405. /* signal handlers */
  11406. struct signal_struct *signal;
  11407. struct sighand_struct *sighand;
  11408. + struct sigqueue *sigqueue_cache;
  11409. sigset_t blocked, real_blocked;
  11410. sigset_t saved_sigmask; /* restored if set_restore_sigmask() was used */
  11411. struct sigpending pending;
  11412. +#ifdef CONFIG_PREEMPT_RT_FULL
  11413. + /* TODO: move me into ->restart_block ? */
  11414. + struct siginfo forced_info;
  11415. +#endif
  11416. unsigned long sas_ss_sp;
  11417. size_t sas_ss_size;
  11418. @@ -1512,6 +1573,8 @@
  11419. /* Protection of the PI data structures: */
  11420. raw_spinlock_t pi_lock;
  11421. + struct wake_q_node wake_q;
  11422. +
  11423. #ifdef CONFIG_RT_MUTEXES
  11424. /* PI waiters blocked on a rt_mutex held by this task */
  11425. struct rb_root pi_waiters;
  11426. @@ -1706,6 +1769,12 @@
  11427. unsigned long trace;
  11428. /* bitmask and counter of trace recursion */
  11429. unsigned long trace_recursion;
  11430. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  11431. + u64 preempt_timestamp_hist;
  11432. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  11433. + long timer_offset;
  11434. +#endif
  11435. +#endif
  11436. #endif /* CONFIG_TRACING */
  11437. #ifdef CONFIG_MEMCG
  11438. struct memcg_oom_info {
  11439. @@ -1722,14 +1791,26 @@
  11440. unsigned int sequential_io;
  11441. unsigned int sequential_io_avg;
  11442. #endif
  11443. +#ifdef CONFIG_PREEMPT_RT_BASE
  11444. + struct rcu_head put_rcu;
  11445. + int softirq_nestcnt;
  11446. + unsigned int softirqs_raised;
  11447. +#endif
  11448. +#ifdef CONFIG_PREEMPT_RT_FULL
  11449. +# if defined CONFIG_HIGHMEM || defined CONFIG_X86_32
  11450. + int kmap_idx;
  11451. + pte_t kmap_pte[KM_TYPE_NR];
  11452. +# endif
  11453. +#endif
  11454. #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
  11455. unsigned long task_state_change;
  11456. #endif
  11457. +#ifdef CONFIG_PREEMPT_RT_FULL
  11458. + int xmit_recursion;
  11459. +#endif
  11460. + int pagefault_disabled;
  11461. };
  11462. -/* Future-safe accessor for struct task_struct's cpus_allowed. */
  11463. -#define tsk_cpus_allowed(tsk) (&(tsk)->cpus_allowed)
  11464. -
  11465. #define TNF_MIGRATED 0x01
  11466. #define TNF_NO_GROUP 0x02
  11467. #define TNF_SHARED 0x04
  11468. @@ -1918,6 +1999,15 @@
  11469. extern void free_task(struct task_struct *tsk);
  11470. #define get_task_struct(tsk) do { atomic_inc(&(tsk)->usage); } while(0)
  11471. +#ifdef CONFIG_PREEMPT_RT_BASE
  11472. +extern void __put_task_struct_cb(struct rcu_head *rhp);
  11473. +
  11474. +static inline void put_task_struct(struct task_struct *t)
  11475. +{
  11476. + if (atomic_dec_and_test(&t->usage))
  11477. + call_rcu(&t->put_rcu, __put_task_struct_cb);
  11478. +}
  11479. +#else
  11480. extern void __put_task_struct(struct task_struct *t);
  11481. static inline void put_task_struct(struct task_struct *t)
  11482. @@ -1925,6 +2015,7 @@
  11483. if (atomic_dec_and_test(&t->usage))
  11484. __put_task_struct(t);
  11485. }
  11486. +#endif
  11487. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  11488. extern void task_cputime(struct task_struct *t,
  11489. @@ -1963,6 +2054,7 @@
  11490. /*
  11491. * Per process flags
  11492. */
  11493. +#define PF_IN_SOFTIRQ 0x00000001 /* Task is serving softirq */
  11494. #define PF_EXITING 0x00000004 /* getting shut down */
  11495. #define PF_EXITPIDONE 0x00000008 /* pi exit done on shut down */
  11496. #define PF_VCPU 0x00000010 /* I'm a virtual CPU */
  11497. @@ -2127,6 +2219,10 @@
  11498. extern int set_cpus_allowed_ptr(struct task_struct *p,
  11499. const struct cpumask *new_mask);
  11500. +int migrate_me(void);
  11501. +void tell_sched_cpu_down_begin(int cpu);
  11502. +void tell_sched_cpu_down_done(int cpu);
  11503. +
  11504. #else
  11505. static inline void do_set_cpus_allowed(struct task_struct *p,
  11506. const struct cpumask *new_mask)
  11507. @@ -2139,6 +2235,9 @@
  11508. return -EINVAL;
  11509. return 0;
  11510. }
  11511. +static inline int migrate_me(void) { return 0; }
  11512. +static inline void tell_sched_cpu_down_begin(int cpu) { }
  11513. +static inline void tell_sched_cpu_down_done(int cpu) { }
  11514. #endif
  11515. #ifdef CONFIG_NO_HZ_COMMON
  11516. @@ -2355,6 +2454,7 @@
  11517. extern int wake_up_state(struct task_struct *tsk, unsigned int state);
  11518. extern int wake_up_process(struct task_struct *tsk);
  11519. +extern int wake_up_lock_sleeper(struct task_struct * tsk);
  11520. extern void wake_up_new_task(struct task_struct *tsk);
  11521. #ifdef CONFIG_SMP
  11522. extern void kick_process(struct task_struct *tsk);
  11523. @@ -2471,12 +2571,24 @@
  11524. /* mmdrop drops the mm and the page tables */
  11525. extern void __mmdrop(struct mm_struct *);
  11526. +
  11527. static inline void mmdrop(struct mm_struct * mm)
  11528. {
  11529. if (unlikely(atomic_dec_and_test(&mm->mm_count)))
  11530. __mmdrop(mm);
  11531. }
  11532. +#ifdef CONFIG_PREEMPT_RT_BASE
  11533. +extern void __mmdrop_delayed(struct rcu_head *rhp);
  11534. +static inline void mmdrop_delayed(struct mm_struct *mm)
  11535. +{
  11536. + if (atomic_dec_and_test(&mm->mm_count))
  11537. + call_rcu(&mm->delayed_drop, __mmdrop_delayed);
  11538. +}
  11539. +#else
  11540. +# define mmdrop_delayed(mm) mmdrop(mm)
  11541. +#endif
  11542. +
  11543. /* mmput gets rid of the mappings and all user-space */
  11544. extern void mmput(struct mm_struct *);
  11545. /* Grab a reference to a task's mm, if it is not already going away */
  11546. @@ -2788,6 +2900,43 @@
  11547. return unlikely(test_tsk_thread_flag(tsk,TIF_NEED_RESCHED));
  11548. }
  11549. +#ifdef CONFIG_PREEMPT_LAZY
  11550. +static inline void set_tsk_need_resched_lazy(struct task_struct *tsk)
  11551. +{
  11552. + set_tsk_thread_flag(tsk,TIF_NEED_RESCHED_LAZY);
  11553. +}
  11554. +
  11555. +static inline void clear_tsk_need_resched_lazy(struct task_struct *tsk)
  11556. +{
  11557. + clear_tsk_thread_flag(tsk,TIF_NEED_RESCHED_LAZY);
  11558. +}
  11559. +
  11560. +static inline int test_tsk_need_resched_lazy(struct task_struct *tsk)
  11561. +{
  11562. + return unlikely(test_tsk_thread_flag(tsk,TIF_NEED_RESCHED_LAZY));
  11563. +}
  11564. +
  11565. +static inline int need_resched_lazy(void)
  11566. +{
  11567. + return test_thread_flag(TIF_NEED_RESCHED_LAZY);
  11568. +}
  11569. +
  11570. +static inline int need_resched_now(void)
  11571. +{
  11572. + return test_thread_flag(TIF_NEED_RESCHED);
  11573. +}
  11574. +
  11575. +#else
  11576. +static inline void clear_tsk_need_resched_lazy(struct task_struct *tsk) { }
  11577. +static inline int need_resched_lazy(void) { return 0; }
  11578. +
  11579. +static inline int need_resched_now(void)
  11580. +{
  11581. + return test_thread_flag(TIF_NEED_RESCHED);
  11582. +}
  11583. +
  11584. +#endif
  11585. +
  11586. static inline int restart_syscall(void)
  11587. {
  11588. set_tsk_thread_flag(current, TIF_SIGPENDING);
  11589. @@ -2819,6 +2968,51 @@
  11590. return (state & TASK_INTERRUPTIBLE) || __fatal_signal_pending(p);
  11591. }
  11592. +static inline bool __task_is_stopped_or_traced(struct task_struct *task)
  11593. +{
  11594. + if (task->state & (__TASK_STOPPED | __TASK_TRACED))
  11595. + return true;
  11596. +#ifdef CONFIG_PREEMPT_RT_FULL
  11597. + if (task->saved_state & (__TASK_STOPPED | __TASK_TRACED))
  11598. + return true;
  11599. +#endif
  11600. + return false;
  11601. +}
  11602. +
  11603. +static inline bool task_is_stopped_or_traced(struct task_struct *task)
  11604. +{
  11605. + bool traced_stopped;
  11606. +
  11607. +#ifdef CONFIG_PREEMPT_RT_FULL
  11608. + unsigned long flags;
  11609. +
  11610. + raw_spin_lock_irqsave(&task->pi_lock, flags);
  11611. + traced_stopped = __task_is_stopped_or_traced(task);
  11612. + raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  11613. +#else
  11614. + traced_stopped = __task_is_stopped_or_traced(task);
  11615. +#endif
  11616. + return traced_stopped;
  11617. +}
  11618. +
  11619. +static inline bool task_is_traced(struct task_struct *task)
  11620. +{
  11621. + bool traced = false;
  11622. +
  11623. + if (task->state & __TASK_TRACED)
  11624. + return true;
  11625. +#ifdef CONFIG_PREEMPT_RT_FULL
  11626. + /* in case the task is sleeping on tasklist_lock */
  11627. + raw_spin_lock_irq(&task->pi_lock);
  11628. + if (task->state & __TASK_TRACED)
  11629. + traced = true;
  11630. + else if (task->saved_state & __TASK_TRACED)
  11631. + traced = true;
  11632. + raw_spin_unlock_irq(&task->pi_lock);
  11633. +#endif
  11634. + return traced;
  11635. +}
  11636. +
  11637. /*
  11638. * cond_resched() and cond_resched_lock(): latency reduction via
  11639. * explicit rescheduling in places that are safe. The return
  11640. @@ -2840,12 +3034,16 @@
  11641. __cond_resched_lock(lock); \
  11642. })
  11643. +#ifndef CONFIG_PREEMPT_RT_FULL
  11644. extern int __cond_resched_softirq(void);
  11645. #define cond_resched_softirq() ({ \
  11646. ___might_sleep(__FILE__, __LINE__, SOFTIRQ_DISABLE_OFFSET); \
  11647. __cond_resched_softirq(); \
  11648. })
  11649. +#else
  11650. +# define cond_resched_softirq() cond_resched()
  11651. +#endif
  11652. static inline void cond_resched_rcu(void)
  11653. {
  11654. @@ -3012,6 +3210,26 @@
  11655. #endif /* CONFIG_SMP */
  11656. +static inline int __migrate_disabled(struct task_struct *p)
  11657. +{
  11658. +#ifdef CONFIG_PREEMPT_RT_FULL
  11659. + return p->migrate_disable;
  11660. +#else
  11661. + return 0;
  11662. +#endif
  11663. +}
  11664. +
  11665. +/* Future-safe accessor for struct task_struct's cpus_allowed. */
  11666. +static inline const struct cpumask *tsk_cpus_allowed(struct task_struct *p)
  11667. +{
  11668. +#ifdef CONFIG_PREEMPT_RT_FULL
  11669. + if (p->migrate_disable)
  11670. + return cpumask_of(task_cpu(p));
  11671. +#endif
  11672. +
  11673. + return &p->cpus_allowed;
  11674. +}
  11675. +
  11676. extern long sched_setaffinity(pid_t pid, const struct cpumask *new_mask);
  11677. extern long sched_getaffinity(pid_t pid, struct cpumask *mask);
  11678. diff -Nur linux-4.1.20.orig/include/linux/seqlock.h linux-4.1.20/include/linux/seqlock.h
  11679. --- linux-4.1.20.orig/include/linux/seqlock.h 2016-03-17 19:11:03.000000000 +0100
  11680. +++ linux-4.1.20/include/linux/seqlock.h 2016-03-21 20:18:33.000000000 +0100
  11681. @@ -219,20 +219,30 @@
  11682. return __read_seqcount_retry(s, start);
  11683. }
  11684. -
  11685. -
  11686. -static inline void raw_write_seqcount_begin(seqcount_t *s)
  11687. +static inline void __raw_write_seqcount_begin(seqcount_t *s)
  11688. {
  11689. s->sequence++;
  11690. smp_wmb();
  11691. }
  11692. -static inline void raw_write_seqcount_end(seqcount_t *s)
  11693. +static inline void raw_write_seqcount_begin(seqcount_t *s)
  11694. +{
  11695. + preempt_disable_rt();
  11696. + __raw_write_seqcount_begin(s);
  11697. +}
  11698. +
  11699. +static inline void __raw_write_seqcount_end(seqcount_t *s)
  11700. {
  11701. smp_wmb();
  11702. s->sequence++;
  11703. }
  11704. +static inline void raw_write_seqcount_end(seqcount_t *s)
  11705. +{
  11706. + __raw_write_seqcount_end(s);
  11707. + preempt_enable_rt();
  11708. +}
  11709. +
  11710. /*
  11711. * raw_write_seqcount_latch - redirect readers to even/odd copy
  11712. * @s: pointer to seqcount_t
  11713. @@ -305,10 +315,32 @@
  11714. /*
  11715. * Read side functions for starting and finalizing a read side section.
  11716. */
  11717. +#ifndef CONFIG_PREEMPT_RT_FULL
  11718. static inline unsigned read_seqbegin(const seqlock_t *sl)
  11719. {
  11720. return read_seqcount_begin(&sl->seqcount);
  11721. }
  11722. +#else
  11723. +/*
  11724. + * Starvation safe read side for RT
  11725. + */
  11726. +static inline unsigned read_seqbegin(seqlock_t *sl)
  11727. +{
  11728. + unsigned ret;
  11729. +
  11730. +repeat:
  11731. + ret = ACCESS_ONCE(sl->seqcount.sequence);
  11732. + if (unlikely(ret & 1)) {
  11733. + /*
  11734. + * Take the lock and let the writer proceed (i.e. evtl
  11735. + * boost it), otherwise we could loop here forever.
  11736. + */
  11737. + spin_unlock_wait(&sl->lock);
  11738. + goto repeat;
  11739. + }
  11740. + return ret;
  11741. +}
  11742. +#endif
  11743. static inline unsigned read_seqretry(const seqlock_t *sl, unsigned start)
  11744. {
  11745. @@ -323,36 +355,36 @@
  11746. static inline void write_seqlock(seqlock_t *sl)
  11747. {
  11748. spin_lock(&sl->lock);
  11749. - write_seqcount_begin(&sl->seqcount);
  11750. + __raw_write_seqcount_begin(&sl->seqcount);
  11751. }
  11752. static inline void write_sequnlock(seqlock_t *sl)
  11753. {
  11754. - write_seqcount_end(&sl->seqcount);
  11755. + __raw_write_seqcount_end(&sl->seqcount);
  11756. spin_unlock(&sl->lock);
  11757. }
  11758. static inline void write_seqlock_bh(seqlock_t *sl)
  11759. {
  11760. spin_lock_bh(&sl->lock);
  11761. - write_seqcount_begin(&sl->seqcount);
  11762. + __raw_write_seqcount_begin(&sl->seqcount);
  11763. }
  11764. static inline void write_sequnlock_bh(seqlock_t *sl)
  11765. {
  11766. - write_seqcount_end(&sl->seqcount);
  11767. + __raw_write_seqcount_end(&sl->seqcount);
  11768. spin_unlock_bh(&sl->lock);
  11769. }
  11770. static inline void write_seqlock_irq(seqlock_t *sl)
  11771. {
  11772. spin_lock_irq(&sl->lock);
  11773. - write_seqcount_begin(&sl->seqcount);
  11774. + __raw_write_seqcount_begin(&sl->seqcount);
  11775. }
  11776. static inline void write_sequnlock_irq(seqlock_t *sl)
  11777. {
  11778. - write_seqcount_end(&sl->seqcount);
  11779. + __raw_write_seqcount_end(&sl->seqcount);
  11780. spin_unlock_irq(&sl->lock);
  11781. }
  11782. @@ -361,7 +393,7 @@
  11783. unsigned long flags;
  11784. spin_lock_irqsave(&sl->lock, flags);
  11785. - write_seqcount_begin(&sl->seqcount);
  11786. + __raw_write_seqcount_begin(&sl->seqcount);
  11787. return flags;
  11788. }
  11789. @@ -371,7 +403,7 @@
  11790. static inline void
  11791. write_sequnlock_irqrestore(seqlock_t *sl, unsigned long flags)
  11792. {
  11793. - write_seqcount_end(&sl->seqcount);
  11794. + __raw_write_seqcount_end(&sl->seqcount);
  11795. spin_unlock_irqrestore(&sl->lock, flags);
  11796. }
  11797. diff -Nur linux-4.1.20.orig/include/linux/signal.h linux-4.1.20/include/linux/signal.h
  11798. --- linux-4.1.20.orig/include/linux/signal.h 2016-03-17 19:11:03.000000000 +0100
  11799. +++ linux-4.1.20/include/linux/signal.h 2016-03-21 20:18:33.000000000 +0100
  11800. @@ -218,6 +218,7 @@
  11801. }
  11802. extern void flush_sigqueue(struct sigpending *queue);
  11803. +extern void flush_task_sigqueue(struct task_struct *tsk);
  11804. /* Test if 'sig' is valid signal. Use this instead of testing _NSIG directly */
  11805. static inline int valid_signal(unsigned long sig)
  11806. diff -Nur linux-4.1.20.orig/include/linux/skbuff.h linux-4.1.20/include/linux/skbuff.h
  11807. --- linux-4.1.20.orig/include/linux/skbuff.h 2016-03-17 19:11:03.000000000 +0100
  11808. +++ linux-4.1.20/include/linux/skbuff.h 2016-03-21 20:18:33.000000000 +0100
  11809. @@ -187,6 +187,7 @@
  11810. __u32 qlen;
  11811. spinlock_t lock;
  11812. + raw_spinlock_t raw_lock;
  11813. };
  11814. struct sk_buff;
  11815. @@ -1337,6 +1338,12 @@
  11816. __skb_queue_head_init(list);
  11817. }
  11818. +static inline void skb_queue_head_init_raw(struct sk_buff_head *list)
  11819. +{
  11820. + raw_spin_lock_init(&list->raw_lock);
  11821. + __skb_queue_head_init(list);
  11822. +}
  11823. +
  11824. static inline void skb_queue_head_init_class(struct sk_buff_head *list,
  11825. struct lock_class_key *class)
  11826. {
  11827. diff -Nur linux-4.1.20.orig/include/linux/smp.h linux-4.1.20/include/linux/smp.h
  11828. --- linux-4.1.20.orig/include/linux/smp.h 2016-03-17 19:11:03.000000000 +0100
  11829. +++ linux-4.1.20/include/linux/smp.h 2016-03-21 20:18:33.000000000 +0100
  11830. @@ -185,6 +185,9 @@
  11831. #define get_cpu() ({ preempt_disable(); smp_processor_id(); })
  11832. #define put_cpu() preempt_enable()
  11833. +#define get_cpu_light() ({ migrate_disable(); smp_processor_id(); })
  11834. +#define put_cpu_light() migrate_enable()
  11835. +
  11836. /*
  11837. * Callback to arch code if there's nosmp or maxcpus=0 on the
  11838. * boot command line:
  11839. diff -Nur linux-4.1.20.orig/include/linux/spinlock.h linux-4.1.20/include/linux/spinlock.h
  11840. --- linux-4.1.20.orig/include/linux/spinlock.h 2016-03-17 19:11:03.000000000 +0100
  11841. +++ linux-4.1.20/include/linux/spinlock.h 2016-03-21 20:18:33.000000000 +0100
  11842. @@ -281,7 +281,11 @@
  11843. #define raw_spin_can_lock(lock) (!raw_spin_is_locked(lock))
  11844. /* Include rwlock functions */
  11845. -#include <linux/rwlock.h>
  11846. +#ifdef CONFIG_PREEMPT_RT_FULL
  11847. +# include <linux/rwlock_rt.h>
  11848. +#else
  11849. +# include <linux/rwlock.h>
  11850. +#endif
  11851. /*
  11852. * Pull the _spin_*()/_read_*()/_write_*() functions/declarations:
  11853. @@ -292,6 +296,10 @@
  11854. # include <linux/spinlock_api_up.h>
  11855. #endif
  11856. +#ifdef CONFIG_PREEMPT_RT_FULL
  11857. +# include <linux/spinlock_rt.h>
  11858. +#else /* PREEMPT_RT_FULL */
  11859. +
  11860. /*
  11861. * Map the spin_lock functions to the raw variants for PREEMPT_RT=n
  11862. */
  11863. @@ -426,4 +434,6 @@
  11864. #define atomic_dec_and_lock(atomic, lock) \
  11865. __cond_lock(lock, _atomic_dec_and_lock(atomic, lock))
  11866. +#endif /* !PREEMPT_RT_FULL */
  11867. +
  11868. #endif /* __LINUX_SPINLOCK_H */
  11869. diff -Nur linux-4.1.20.orig/include/linux/spinlock_api_smp.h linux-4.1.20/include/linux/spinlock_api_smp.h
  11870. --- linux-4.1.20.orig/include/linux/spinlock_api_smp.h 2016-03-17 19:11:03.000000000 +0100
  11871. +++ linux-4.1.20/include/linux/spinlock_api_smp.h 2016-03-21 20:18:33.000000000 +0100
  11872. @@ -189,6 +189,8 @@
  11873. return 0;
  11874. }
  11875. -#include <linux/rwlock_api_smp.h>
  11876. +#ifndef CONFIG_PREEMPT_RT_FULL
  11877. +# include <linux/rwlock_api_smp.h>
  11878. +#endif
  11879. #endif /* __LINUX_SPINLOCK_API_SMP_H */
  11880. diff -Nur linux-4.1.20.orig/include/linux/spinlock_rt.h linux-4.1.20/include/linux/spinlock_rt.h
  11881. --- linux-4.1.20.orig/include/linux/spinlock_rt.h 1970-01-01 01:00:00.000000000 +0100
  11882. +++ linux-4.1.20/include/linux/spinlock_rt.h 2016-03-21 20:18:33.000000000 +0100
  11883. @@ -0,0 +1,174 @@
  11884. +#ifndef __LINUX_SPINLOCK_RT_H
  11885. +#define __LINUX_SPINLOCK_RT_H
  11886. +
  11887. +#ifndef __LINUX_SPINLOCK_H
  11888. +#error Do not include directly. Use spinlock.h
  11889. +#endif
  11890. +
  11891. +#include <linux/bug.h>
  11892. +
  11893. +extern void
  11894. +__rt_spin_lock_init(spinlock_t *lock, char *name, struct lock_class_key *key);
  11895. +
  11896. +#define spin_lock_init(slock) \
  11897. +do { \
  11898. + static struct lock_class_key __key; \
  11899. + \
  11900. + rt_mutex_init(&(slock)->lock); \
  11901. + __rt_spin_lock_init(slock, #slock, &__key); \
  11902. +} while (0)
  11903. +
  11904. +extern void __lockfunc rt_spin_lock(spinlock_t *lock);
  11905. +extern unsigned long __lockfunc rt_spin_lock_trace_flags(spinlock_t *lock);
  11906. +extern void __lockfunc rt_spin_lock_nested(spinlock_t *lock, int subclass);
  11907. +extern void __lockfunc rt_spin_unlock(spinlock_t *lock);
  11908. +extern void __lockfunc rt_spin_unlock_wait(spinlock_t *lock);
  11909. +extern int __lockfunc rt_spin_trylock_irqsave(spinlock_t *lock, unsigned long *flags);
  11910. +extern int __lockfunc rt_spin_trylock_bh(spinlock_t *lock);
  11911. +extern int __lockfunc rt_spin_trylock(spinlock_t *lock);
  11912. +extern int atomic_dec_and_spin_lock(atomic_t *atomic, spinlock_t *lock);
  11913. +
  11914. +/*
  11915. + * lockdep-less calls, for derived types like rwlock:
  11916. + * (for trylock they can use rt_mutex_trylock() directly.
  11917. + */
  11918. +extern void __lockfunc __rt_spin_lock(struct rt_mutex *lock);
  11919. +extern void __lockfunc __rt_spin_unlock(struct rt_mutex *lock);
  11920. +extern int __lockfunc __rt_spin_trylock(struct rt_mutex *lock);
  11921. +
  11922. +#define spin_lock(lock) \
  11923. + do { \
  11924. + migrate_disable(); \
  11925. + rt_spin_lock(lock); \
  11926. + } while (0)
  11927. +
  11928. +#define spin_lock_bh(lock) \
  11929. + do { \
  11930. + local_bh_disable(); \
  11931. + migrate_disable(); \
  11932. + rt_spin_lock(lock); \
  11933. + } while (0)
  11934. +
  11935. +#define spin_lock_irq(lock) spin_lock(lock)
  11936. +
  11937. +#define spin_do_trylock(lock) __cond_lock(lock, rt_spin_trylock(lock))
  11938. +
  11939. +#define spin_trylock(lock) \
  11940. +({ \
  11941. + int __locked; \
  11942. + migrate_disable(); \
  11943. + __locked = spin_do_trylock(lock); \
  11944. + if (!__locked) \
  11945. + migrate_enable(); \
  11946. + __locked; \
  11947. +})
  11948. +
  11949. +#ifdef CONFIG_LOCKDEP
  11950. +# define spin_lock_nested(lock, subclass) \
  11951. + do { \
  11952. + migrate_disable(); \
  11953. + rt_spin_lock_nested(lock, subclass); \
  11954. + } while (0)
  11955. +
  11956. +#define spin_lock_bh_nested(lock, subclass) \
  11957. + do { \
  11958. + local_bh_disable(); \
  11959. + migrate_disable(); \
  11960. + rt_spin_lock_nested(lock, subclass); \
  11961. + } while (0)
  11962. +
  11963. +# define spin_lock_irqsave_nested(lock, flags, subclass) \
  11964. + do { \
  11965. + typecheck(unsigned long, flags); \
  11966. + flags = 0; \
  11967. + migrate_disable(); \
  11968. + rt_spin_lock_nested(lock, subclass); \
  11969. + } while (0)
  11970. +#else
  11971. +# define spin_lock_nested(lock, subclass) spin_lock(lock)
  11972. +# define spin_lock_bh_nested(lock, subclass) spin_lock_bh(lock)
  11973. +
  11974. +# define spin_lock_irqsave_nested(lock, flags, subclass) \
  11975. + do { \
  11976. + typecheck(unsigned long, flags); \
  11977. + flags = 0; \
  11978. + spin_lock(lock); \
  11979. + } while (0)
  11980. +#endif
  11981. +
  11982. +#define spin_lock_irqsave(lock, flags) \
  11983. + do { \
  11984. + typecheck(unsigned long, flags); \
  11985. + flags = 0; \
  11986. + spin_lock(lock); \
  11987. + } while (0)
  11988. +
  11989. +static inline unsigned long spin_lock_trace_flags(spinlock_t *lock)
  11990. +{
  11991. + unsigned long flags = 0;
  11992. +#ifdef CONFIG_TRACE_IRQFLAGS
  11993. + flags = rt_spin_lock_trace_flags(lock);
  11994. +#else
  11995. + spin_lock(lock); /* lock_local */
  11996. +#endif
  11997. + return flags;
  11998. +}
  11999. +
  12000. +/* FIXME: we need rt_spin_lock_nest_lock */
  12001. +#define spin_lock_nest_lock(lock, nest_lock) spin_lock_nested(lock, 0)
  12002. +
  12003. +#define spin_unlock(lock) \
  12004. + do { \
  12005. + rt_spin_unlock(lock); \
  12006. + migrate_enable(); \
  12007. + } while (0)
  12008. +
  12009. +#define spin_unlock_bh(lock) \
  12010. + do { \
  12011. + rt_spin_unlock(lock); \
  12012. + migrate_enable(); \
  12013. + local_bh_enable(); \
  12014. + } while (0)
  12015. +
  12016. +#define spin_unlock_irq(lock) spin_unlock(lock)
  12017. +
  12018. +#define spin_unlock_irqrestore(lock, flags) \
  12019. + do { \
  12020. + typecheck(unsigned long, flags); \
  12021. + (void) flags; \
  12022. + spin_unlock(lock); \
  12023. + } while (0)
  12024. +
  12025. +#define spin_trylock_bh(lock) __cond_lock(lock, rt_spin_trylock_bh(lock))
  12026. +#define spin_trylock_irq(lock) spin_trylock(lock)
  12027. +
  12028. +#define spin_trylock_irqsave(lock, flags) \
  12029. + rt_spin_trylock_irqsave(lock, &(flags))
  12030. +
  12031. +#define spin_unlock_wait(lock) rt_spin_unlock_wait(lock)
  12032. +
  12033. +#ifdef CONFIG_GENERIC_LOCKBREAK
  12034. +# define spin_is_contended(lock) ((lock)->break_lock)
  12035. +#else
  12036. +# define spin_is_contended(lock) (((void)(lock), 0))
  12037. +#endif
  12038. +
  12039. +static inline int spin_can_lock(spinlock_t *lock)
  12040. +{
  12041. + return !rt_mutex_is_locked(&lock->lock);
  12042. +}
  12043. +
  12044. +static inline int spin_is_locked(spinlock_t *lock)
  12045. +{
  12046. + return rt_mutex_is_locked(&lock->lock);
  12047. +}
  12048. +
  12049. +static inline void assert_spin_locked(spinlock_t *lock)
  12050. +{
  12051. + BUG_ON(!spin_is_locked(lock));
  12052. +}
  12053. +
  12054. +#define atomic_dec_and_lock(atomic, lock) \
  12055. + atomic_dec_and_spin_lock(atomic, lock)
  12056. +
  12057. +#endif
  12058. diff -Nur linux-4.1.20.orig/include/linux/spinlock_types.h linux-4.1.20/include/linux/spinlock_types.h
  12059. --- linux-4.1.20.orig/include/linux/spinlock_types.h 2016-03-17 19:11:03.000000000 +0100
  12060. +++ linux-4.1.20/include/linux/spinlock_types.h 2016-03-21 20:18:33.000000000 +0100
  12061. @@ -9,80 +9,15 @@
  12062. * Released under the General Public License (GPL).
  12063. */
  12064. -#if defined(CONFIG_SMP)
  12065. -# include <asm/spinlock_types.h>
  12066. -#else
  12067. -# include <linux/spinlock_types_up.h>
  12068. -#endif
  12069. -
  12070. -#include <linux/lockdep.h>
  12071. -
  12072. -typedef struct raw_spinlock {
  12073. - arch_spinlock_t raw_lock;
  12074. -#ifdef CONFIG_GENERIC_LOCKBREAK
  12075. - unsigned int break_lock;
  12076. -#endif
  12077. -#ifdef CONFIG_DEBUG_SPINLOCK
  12078. - unsigned int magic, owner_cpu;
  12079. - void *owner;
  12080. -#endif
  12081. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12082. - struct lockdep_map dep_map;
  12083. -#endif
  12084. -} raw_spinlock_t;
  12085. -
  12086. -#define SPINLOCK_MAGIC 0xdead4ead
  12087. -
  12088. -#define SPINLOCK_OWNER_INIT ((void *)-1L)
  12089. -
  12090. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12091. -# define SPIN_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }
  12092. -#else
  12093. -# define SPIN_DEP_MAP_INIT(lockname)
  12094. -#endif
  12095. +#include <linux/spinlock_types_raw.h>
  12096. -#ifdef CONFIG_DEBUG_SPINLOCK
  12097. -# define SPIN_DEBUG_INIT(lockname) \
  12098. - .magic = SPINLOCK_MAGIC, \
  12099. - .owner_cpu = -1, \
  12100. - .owner = SPINLOCK_OWNER_INIT,
  12101. +#ifndef CONFIG_PREEMPT_RT_FULL
  12102. +# include <linux/spinlock_types_nort.h>
  12103. +# include <linux/rwlock_types.h>
  12104. #else
  12105. -# define SPIN_DEBUG_INIT(lockname)
  12106. +# include <linux/rtmutex.h>
  12107. +# include <linux/spinlock_types_rt.h>
  12108. +# include <linux/rwlock_types_rt.h>
  12109. #endif
  12110. -#define __RAW_SPIN_LOCK_INITIALIZER(lockname) \
  12111. - { \
  12112. - .raw_lock = __ARCH_SPIN_LOCK_UNLOCKED, \
  12113. - SPIN_DEBUG_INIT(lockname) \
  12114. - SPIN_DEP_MAP_INIT(lockname) }
  12115. -
  12116. -#define __RAW_SPIN_LOCK_UNLOCKED(lockname) \
  12117. - (raw_spinlock_t) __RAW_SPIN_LOCK_INITIALIZER(lockname)
  12118. -
  12119. -#define DEFINE_RAW_SPINLOCK(x) raw_spinlock_t x = __RAW_SPIN_LOCK_UNLOCKED(x)
  12120. -
  12121. -typedef struct spinlock {
  12122. - union {
  12123. - struct raw_spinlock rlock;
  12124. -
  12125. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12126. -# define LOCK_PADSIZE (offsetof(struct raw_spinlock, dep_map))
  12127. - struct {
  12128. - u8 __padding[LOCK_PADSIZE];
  12129. - struct lockdep_map dep_map;
  12130. - };
  12131. -#endif
  12132. - };
  12133. -} spinlock_t;
  12134. -
  12135. -#define __SPIN_LOCK_INITIALIZER(lockname) \
  12136. - { { .rlock = __RAW_SPIN_LOCK_INITIALIZER(lockname) } }
  12137. -
  12138. -#define __SPIN_LOCK_UNLOCKED(lockname) \
  12139. - (spinlock_t ) __SPIN_LOCK_INITIALIZER(lockname)
  12140. -
  12141. -#define DEFINE_SPINLOCK(x) spinlock_t x = __SPIN_LOCK_UNLOCKED(x)
  12142. -
  12143. -#include <linux/rwlock_types.h>
  12144. -
  12145. #endif /* __LINUX_SPINLOCK_TYPES_H */
  12146. diff -Nur linux-4.1.20.orig/include/linux/spinlock_types_nort.h linux-4.1.20/include/linux/spinlock_types_nort.h
  12147. --- linux-4.1.20.orig/include/linux/spinlock_types_nort.h 1970-01-01 01:00:00.000000000 +0100
  12148. +++ linux-4.1.20/include/linux/spinlock_types_nort.h 2016-03-21 20:18:33.000000000 +0100
  12149. @@ -0,0 +1,33 @@
  12150. +#ifndef __LINUX_SPINLOCK_TYPES_NORT_H
  12151. +#define __LINUX_SPINLOCK_TYPES_NORT_H
  12152. +
  12153. +#ifndef __LINUX_SPINLOCK_TYPES_H
  12154. +#error "Do not include directly. Include spinlock_types.h instead"
  12155. +#endif
  12156. +
  12157. +/*
  12158. + * The non RT version maps spinlocks to raw_spinlocks
  12159. + */
  12160. +typedef struct spinlock {
  12161. + union {
  12162. + struct raw_spinlock rlock;
  12163. +
  12164. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12165. +# define LOCK_PADSIZE (offsetof(struct raw_spinlock, dep_map))
  12166. + struct {
  12167. + u8 __padding[LOCK_PADSIZE];
  12168. + struct lockdep_map dep_map;
  12169. + };
  12170. +#endif
  12171. + };
  12172. +} spinlock_t;
  12173. +
  12174. +#define __SPIN_LOCK_INITIALIZER(lockname) \
  12175. + { { .rlock = __RAW_SPIN_LOCK_INITIALIZER(lockname) } }
  12176. +
  12177. +#define __SPIN_LOCK_UNLOCKED(lockname) \
  12178. + (spinlock_t ) __SPIN_LOCK_INITIALIZER(lockname)
  12179. +
  12180. +#define DEFINE_SPINLOCK(x) spinlock_t x = __SPIN_LOCK_UNLOCKED(x)
  12181. +
  12182. +#endif
  12183. diff -Nur linux-4.1.20.orig/include/linux/spinlock_types_raw.h linux-4.1.20/include/linux/spinlock_types_raw.h
  12184. --- linux-4.1.20.orig/include/linux/spinlock_types_raw.h 1970-01-01 01:00:00.000000000 +0100
  12185. +++ linux-4.1.20/include/linux/spinlock_types_raw.h 2016-03-21 20:18:33.000000000 +0100
  12186. @@ -0,0 +1,56 @@
  12187. +#ifndef __LINUX_SPINLOCK_TYPES_RAW_H
  12188. +#define __LINUX_SPINLOCK_TYPES_RAW_H
  12189. +
  12190. +#if defined(CONFIG_SMP)
  12191. +# include <asm/spinlock_types.h>
  12192. +#else
  12193. +# include <linux/spinlock_types_up.h>
  12194. +#endif
  12195. +
  12196. +#include <linux/lockdep.h>
  12197. +
  12198. +typedef struct raw_spinlock {
  12199. + arch_spinlock_t raw_lock;
  12200. +#ifdef CONFIG_GENERIC_LOCKBREAK
  12201. + unsigned int break_lock;
  12202. +#endif
  12203. +#ifdef CONFIG_DEBUG_SPINLOCK
  12204. + unsigned int magic, owner_cpu;
  12205. + void *owner;
  12206. +#endif
  12207. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12208. + struct lockdep_map dep_map;
  12209. +#endif
  12210. +} raw_spinlock_t;
  12211. +
  12212. +#define SPINLOCK_MAGIC 0xdead4ead
  12213. +
  12214. +#define SPINLOCK_OWNER_INIT ((void *)-1L)
  12215. +
  12216. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12217. +# define SPIN_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }
  12218. +#else
  12219. +# define SPIN_DEP_MAP_INIT(lockname)
  12220. +#endif
  12221. +
  12222. +#ifdef CONFIG_DEBUG_SPINLOCK
  12223. +# define SPIN_DEBUG_INIT(lockname) \
  12224. + .magic = SPINLOCK_MAGIC, \
  12225. + .owner_cpu = -1, \
  12226. + .owner = SPINLOCK_OWNER_INIT,
  12227. +#else
  12228. +# define SPIN_DEBUG_INIT(lockname)
  12229. +#endif
  12230. +
  12231. +#define __RAW_SPIN_LOCK_INITIALIZER(lockname) \
  12232. + { \
  12233. + .raw_lock = __ARCH_SPIN_LOCK_UNLOCKED, \
  12234. + SPIN_DEBUG_INIT(lockname) \
  12235. + SPIN_DEP_MAP_INIT(lockname) }
  12236. +
  12237. +#define __RAW_SPIN_LOCK_UNLOCKED(lockname) \
  12238. + (raw_spinlock_t) __RAW_SPIN_LOCK_INITIALIZER(lockname)
  12239. +
  12240. +#define DEFINE_RAW_SPINLOCK(x) raw_spinlock_t x = __RAW_SPIN_LOCK_UNLOCKED(x)
  12241. +
  12242. +#endif
  12243. diff -Nur linux-4.1.20.orig/include/linux/spinlock_types_rt.h linux-4.1.20/include/linux/spinlock_types_rt.h
  12244. --- linux-4.1.20.orig/include/linux/spinlock_types_rt.h 1970-01-01 01:00:00.000000000 +0100
  12245. +++ linux-4.1.20/include/linux/spinlock_types_rt.h 2016-03-21 20:18:33.000000000 +0100
  12246. @@ -0,0 +1,51 @@
  12247. +#ifndef __LINUX_SPINLOCK_TYPES_RT_H
  12248. +#define __LINUX_SPINLOCK_TYPES_RT_H
  12249. +
  12250. +#ifndef __LINUX_SPINLOCK_TYPES_H
  12251. +#error "Do not include directly. Include spinlock_types.h instead"
  12252. +#endif
  12253. +
  12254. +#include <linux/cache.h>
  12255. +
  12256. +/*
  12257. + * PREEMPT_RT: spinlocks - an RT mutex plus lock-break field:
  12258. + */
  12259. +typedef struct spinlock {
  12260. + struct rt_mutex lock;
  12261. + unsigned int break_lock;
  12262. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12263. + struct lockdep_map dep_map;
  12264. +#endif
  12265. +} spinlock_t;
  12266. +
  12267. +#ifdef CONFIG_DEBUG_RT_MUTEXES
  12268. +# define __RT_SPIN_INITIALIZER(name) \
  12269. + { \
  12270. + .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock), \
  12271. + .save_state = 1, \
  12272. + .file = __FILE__, \
  12273. + .line = __LINE__ , \
  12274. + }
  12275. +#else
  12276. +# define __RT_SPIN_INITIALIZER(name) \
  12277. + { \
  12278. + .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock), \
  12279. + .save_state = 1, \
  12280. + }
  12281. +#endif
  12282. +
  12283. +/*
  12284. +.wait_list = PLIST_HEAD_INIT_RAW((name).lock.wait_list, (name).lock.wait_lock)
  12285. +*/
  12286. +
  12287. +#define __SPIN_LOCK_UNLOCKED(name) \
  12288. + { .lock = __RT_SPIN_INITIALIZER(name.lock), \
  12289. + SPIN_DEP_MAP_INIT(name) }
  12290. +
  12291. +#define __DEFINE_SPINLOCK(name) \
  12292. + spinlock_t name = __SPIN_LOCK_UNLOCKED(name)
  12293. +
  12294. +#define DEFINE_SPINLOCK(name) \
  12295. + spinlock_t name __cacheline_aligned_in_smp = __SPIN_LOCK_UNLOCKED(name)
  12296. +
  12297. +#endif
  12298. diff -Nur linux-4.1.20.orig/include/linux/srcu.h linux-4.1.20/include/linux/srcu.h
  12299. --- linux-4.1.20.orig/include/linux/srcu.h 2016-03-17 19:11:03.000000000 +0100
  12300. +++ linux-4.1.20/include/linux/srcu.h 2016-03-21 20:18:33.000000000 +0100
  12301. @@ -84,10 +84,10 @@
  12302. void process_srcu(struct work_struct *work);
  12303. -#define __SRCU_STRUCT_INIT(name) \
  12304. +#define __SRCU_STRUCT_INIT(name, pcpu_name) \
  12305. { \
  12306. .completed = -300, \
  12307. - .per_cpu_ref = &name##_srcu_array, \
  12308. + .per_cpu_ref = &pcpu_name, \
  12309. .queue_lock = __SPIN_LOCK_UNLOCKED(name.queue_lock), \
  12310. .running = false, \
  12311. .batch_queue = RCU_BATCH_INIT(name.batch_queue), \
  12312. @@ -104,7 +104,7 @@
  12313. */
  12314. #define __DEFINE_SRCU(name, is_static) \
  12315. static DEFINE_PER_CPU(struct srcu_struct_array, name##_srcu_array);\
  12316. - is_static struct srcu_struct name = __SRCU_STRUCT_INIT(name)
  12317. + is_static struct srcu_struct name = __SRCU_STRUCT_INIT(name, name##_srcu_array)
  12318. #define DEFINE_SRCU(name) __DEFINE_SRCU(name, /* not static */)
  12319. #define DEFINE_STATIC_SRCU(name) __DEFINE_SRCU(name, static)
  12320. diff -Nur linux-4.1.20.orig/include/linux/swap.h linux-4.1.20/include/linux/swap.h
  12321. --- linux-4.1.20.orig/include/linux/swap.h 2016-03-17 19:11:03.000000000 +0100
  12322. +++ linux-4.1.20/include/linux/swap.h 2016-03-21 20:18:33.000000000 +0100
  12323. @@ -11,6 +11,7 @@
  12324. #include <linux/fs.h>
  12325. #include <linux/atomic.h>
  12326. #include <linux/page-flags.h>
  12327. +#include <linux/locallock.h>
  12328. #include <asm/page.h>
  12329. struct notifier_block;
  12330. @@ -252,7 +253,8 @@
  12331. void *workingset_eviction(struct address_space *mapping, struct page *page);
  12332. bool workingset_refault(void *shadow);
  12333. void workingset_activation(struct page *page);
  12334. -extern struct list_lru workingset_shadow_nodes;
  12335. +extern struct list_lru __workingset_shadow_nodes;
  12336. +DECLARE_LOCAL_IRQ_LOCK(workingset_shadow_lock);
  12337. static inline unsigned int workingset_node_pages(struct radix_tree_node *node)
  12338. {
  12339. @@ -296,6 +298,7 @@
  12340. /* linux/mm/swap.c */
  12341. +DECLARE_LOCAL_IRQ_LOCK(swapvec_lock);
  12342. extern void lru_cache_add(struct page *);
  12343. extern void lru_cache_add_anon(struct page *page);
  12344. extern void lru_cache_add_file(struct page *page);
  12345. diff -Nur linux-4.1.20.orig/include/linux/thread_info.h linux-4.1.20/include/linux/thread_info.h
  12346. --- linux-4.1.20.orig/include/linux/thread_info.h 2016-03-17 19:11:03.000000000 +0100
  12347. +++ linux-4.1.20/include/linux/thread_info.h 2016-03-21 20:18:33.000000000 +0100
  12348. @@ -102,7 +102,17 @@
  12349. #define test_thread_flag(flag) \
  12350. test_ti_thread_flag(current_thread_info(), flag)
  12351. -#define tif_need_resched() test_thread_flag(TIF_NEED_RESCHED)
  12352. +#ifdef CONFIG_PREEMPT_LAZY
  12353. +#define tif_need_resched() (test_thread_flag(TIF_NEED_RESCHED) || \
  12354. + test_thread_flag(TIF_NEED_RESCHED_LAZY))
  12355. +#define tif_need_resched_now() (test_thread_flag(TIF_NEED_RESCHED))
  12356. +#define tif_need_resched_lazy() test_thread_flag(TIF_NEED_RESCHED_LAZY))
  12357. +
  12358. +#else
  12359. +#define tif_need_resched() test_thread_flag(TIF_NEED_RESCHED)
  12360. +#define tif_need_resched_now() test_thread_flag(TIF_NEED_RESCHED)
  12361. +#define tif_need_resched_lazy() 0
  12362. +#endif
  12363. #if defined TIF_RESTORE_SIGMASK && !defined HAVE_SET_RESTORE_SIGMASK
  12364. /*
  12365. diff -Nur linux-4.1.20.orig/include/linux/timer.h linux-4.1.20/include/linux/timer.h
  12366. --- linux-4.1.20.orig/include/linux/timer.h 2016-03-17 19:11:03.000000000 +0100
  12367. +++ linux-4.1.20/include/linux/timer.h 2016-03-21 20:18:33.000000000 +0100
  12368. @@ -241,7 +241,7 @@
  12369. extern int try_to_del_timer_sync(struct timer_list *timer);
  12370. -#ifdef CONFIG_SMP
  12371. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  12372. extern int del_timer_sync(struct timer_list *timer);
  12373. #else
  12374. # define del_timer_sync(t) del_timer(t)
  12375. diff -Nur linux-4.1.20.orig/include/linux/uaccess.h linux-4.1.20/include/linux/uaccess.h
  12376. --- linux-4.1.20.orig/include/linux/uaccess.h 2016-03-17 19:11:03.000000000 +0100
  12377. +++ linux-4.1.20/include/linux/uaccess.h 2016-03-21 20:18:33.000000000 +0100
  12378. @@ -1,21 +1,31 @@
  12379. #ifndef __LINUX_UACCESS_H__
  12380. #define __LINUX_UACCESS_H__
  12381. -#include <linux/preempt.h>
  12382. +#include <linux/sched.h>
  12383. #include <asm/uaccess.h>
  12384. +static __always_inline void pagefault_disabled_inc(void)
  12385. +{
  12386. + current->pagefault_disabled++;
  12387. +}
  12388. +
  12389. +static __always_inline void pagefault_disabled_dec(void)
  12390. +{
  12391. + current->pagefault_disabled--;
  12392. + WARN_ON(current->pagefault_disabled < 0);
  12393. +}
  12394. +
  12395. /*
  12396. - * These routines enable/disable the pagefault handler in that
  12397. - * it will not take any locks and go straight to the fixup table.
  12398. + * These routines enable/disable the pagefault handler. If disabled, it will
  12399. + * not take any locks and go straight to the fixup table.
  12400. *
  12401. - * They have great resemblance to the preempt_disable/enable calls
  12402. - * and in fact they are identical; this is because currently there is
  12403. - * no other way to make the pagefault handlers do this. So we do
  12404. - * disable preemption but we don't necessarily care about that.
  12405. + * User access methods will not sleep when called from a pagefault_disabled()
  12406. + * environment.
  12407. */
  12408. static inline void pagefault_disable(void)
  12409. {
  12410. - preempt_count_inc();
  12411. + migrate_disable();
  12412. + pagefault_disabled_inc();
  12413. /*
  12414. * make sure to have issued the store before a pagefault
  12415. * can hit.
  12416. @@ -25,18 +35,32 @@
  12417. static inline void pagefault_enable(void)
  12418. {
  12419. -#ifndef CONFIG_PREEMPT
  12420. /*
  12421. * make sure to issue those last loads/stores before enabling
  12422. * the pagefault handler again.
  12423. */
  12424. barrier();
  12425. - preempt_count_dec();
  12426. -#else
  12427. - preempt_enable();
  12428. -#endif
  12429. + pagefault_disabled_dec();
  12430. + migrate_enable();
  12431. }
  12432. +/*
  12433. + * Is the pagefault handler disabled? If so, user access methods will not sleep.
  12434. + */
  12435. +#define pagefault_disabled() (current->pagefault_disabled != 0)
  12436. +
  12437. +/*
  12438. + * The pagefault handler is in general disabled by pagefault_disable() or
  12439. + * when in irq context (via in_atomic()).
  12440. + *
  12441. + * This function should only be used by the fault handlers. Other users should
  12442. + * stick to pagefault_disabled().
  12443. + * Please NEVER use preempt_disable() to disable the fault handler. With
  12444. + * !CONFIG_PREEMPT_COUNT, this is like a NOP. So the handler won't be disabled.
  12445. + * in_atomic() will report different values based on !CONFIG_PREEMPT_COUNT.
  12446. + */
  12447. +#define faulthandler_disabled() (pagefault_disabled() || in_atomic())
  12448. +
  12449. #ifndef ARCH_HAS_NOCACHE_UACCESS
  12450. static inline unsigned long __copy_from_user_inatomic_nocache(void *to,
  12451. diff -Nur linux-4.1.20.orig/include/linux/uprobes.h linux-4.1.20/include/linux/uprobes.h
  12452. --- linux-4.1.20.orig/include/linux/uprobes.h 2016-03-17 19:11:03.000000000 +0100
  12453. +++ linux-4.1.20/include/linux/uprobes.h 2016-03-21 20:18:33.000000000 +0100
  12454. @@ -27,6 +27,7 @@
  12455. #include <linux/errno.h>
  12456. #include <linux/rbtree.h>
  12457. #include <linux/types.h>
  12458. +#include <linux/wait.h>
  12459. struct vm_area_struct;
  12460. struct mm_struct;
  12461. diff -Nur linux-4.1.20.orig/include/linux/vmstat.h linux-4.1.20/include/linux/vmstat.h
  12462. --- linux-4.1.20.orig/include/linux/vmstat.h 2016-03-17 19:11:03.000000000 +0100
  12463. +++ linux-4.1.20/include/linux/vmstat.h 2016-03-21 20:18:33.000000000 +0100
  12464. @@ -33,7 +33,9 @@
  12465. */
  12466. static inline void __count_vm_event(enum vm_event_item item)
  12467. {
  12468. + preempt_disable_rt();
  12469. raw_cpu_inc(vm_event_states.event[item]);
  12470. + preempt_enable_rt();
  12471. }
  12472. static inline void count_vm_event(enum vm_event_item item)
  12473. @@ -43,7 +45,9 @@
  12474. static inline void __count_vm_events(enum vm_event_item item, long delta)
  12475. {
  12476. + preempt_disable_rt();
  12477. raw_cpu_add(vm_event_states.event[item], delta);
  12478. + preempt_enable_rt();
  12479. }
  12480. static inline void count_vm_events(enum vm_event_item item, long delta)
  12481. diff -Nur linux-4.1.20.orig/include/linux/wait-simple.h linux-4.1.20/include/linux/wait-simple.h
  12482. --- linux-4.1.20.orig/include/linux/wait-simple.h 1970-01-01 01:00:00.000000000 +0100
  12483. +++ linux-4.1.20/include/linux/wait-simple.h 2016-03-21 20:18:33.000000000 +0100
  12484. @@ -0,0 +1,207 @@
  12485. +#ifndef _LINUX_WAIT_SIMPLE_H
  12486. +#define _LINUX_WAIT_SIMPLE_H
  12487. +
  12488. +#include <linux/spinlock.h>
  12489. +#include <linux/list.h>
  12490. +
  12491. +#include <asm/current.h>
  12492. +
  12493. +struct swaiter {
  12494. + struct task_struct *task;
  12495. + struct list_head node;
  12496. +};
  12497. +
  12498. +#define DEFINE_SWAITER(name) \
  12499. + struct swaiter name = { \
  12500. + .task = current, \
  12501. + .node = LIST_HEAD_INIT((name).node), \
  12502. + }
  12503. +
  12504. +struct swait_head {
  12505. + raw_spinlock_t lock;
  12506. + struct list_head list;
  12507. +};
  12508. +
  12509. +#define SWAIT_HEAD_INITIALIZER(name) { \
  12510. + .lock = __RAW_SPIN_LOCK_UNLOCKED(name.lock), \
  12511. + .list = LIST_HEAD_INIT((name).list), \
  12512. + }
  12513. +
  12514. +#define DEFINE_SWAIT_HEAD(name) \
  12515. + struct swait_head name = SWAIT_HEAD_INITIALIZER(name)
  12516. +
  12517. +extern void __init_swait_head(struct swait_head *h, struct lock_class_key *key);
  12518. +
  12519. +#define init_swait_head(swh) \
  12520. + do { \
  12521. + static struct lock_class_key __key; \
  12522. + \
  12523. + __init_swait_head((swh), &__key); \
  12524. + } while (0)
  12525. +
  12526. +/*
  12527. + * Waiter functions
  12528. + */
  12529. +extern void swait_prepare_locked(struct swait_head *head, struct swaiter *w);
  12530. +extern void swait_prepare(struct swait_head *head, struct swaiter *w, int state);
  12531. +extern void swait_finish_locked(struct swait_head *head, struct swaiter *w);
  12532. +extern void swait_finish(struct swait_head *head, struct swaiter *w);
  12533. +
  12534. +/* Check whether a head has waiters enqueued */
  12535. +static inline bool swaitqueue_active(struct swait_head *h)
  12536. +{
  12537. + /* Make sure the condition is visible before checking list_empty() */
  12538. + smp_mb();
  12539. + return !list_empty(&h->list);
  12540. +}
  12541. +
  12542. +/*
  12543. + * Wakeup functions
  12544. + */
  12545. +extern unsigned int __swait_wake(struct swait_head *head, unsigned int state, unsigned int num);
  12546. +extern unsigned int __swait_wake_locked(struct swait_head *head, unsigned int state, unsigned int num);
  12547. +
  12548. +#define swait_wake(head) __swait_wake(head, TASK_NORMAL, 1)
  12549. +#define swait_wake_interruptible(head) __swait_wake(head, TASK_INTERRUPTIBLE, 1)
  12550. +#define swait_wake_all(head) __swait_wake(head, TASK_NORMAL, 0)
  12551. +#define swait_wake_all_interruptible(head) __swait_wake(head, TASK_INTERRUPTIBLE, 0)
  12552. +
  12553. +/*
  12554. + * Event API
  12555. + */
  12556. +#define __swait_event(wq, condition) \
  12557. +do { \
  12558. + DEFINE_SWAITER(__wait); \
  12559. + \
  12560. + for (;;) { \
  12561. + swait_prepare(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
  12562. + if (condition) \
  12563. + break; \
  12564. + schedule(); \
  12565. + } \
  12566. + swait_finish(&wq, &__wait); \
  12567. +} while (0)
  12568. +
  12569. +/**
  12570. + * swait_event - sleep until a condition gets true
  12571. + * @wq: the waitqueue to wait on
  12572. + * @condition: a C expression for the event to wait for
  12573. + *
  12574. + * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
  12575. + * @condition evaluates to true. The @condition is checked each time
  12576. + * the waitqueue @wq is woken up.
  12577. + *
  12578. + * wake_up() has to be called after changing any variable that could
  12579. + * change the result of the wait condition.
  12580. + */
  12581. +#define swait_event(wq, condition) \
  12582. +do { \
  12583. + if (condition) \
  12584. + break; \
  12585. + __swait_event(wq, condition); \
  12586. +} while (0)
  12587. +
  12588. +#define __swait_event_interruptible(wq, condition, ret) \
  12589. +do { \
  12590. + DEFINE_SWAITER(__wait); \
  12591. + \
  12592. + for (;;) { \
  12593. + swait_prepare(&wq, &__wait, TASK_INTERRUPTIBLE); \
  12594. + if (condition) \
  12595. + break; \
  12596. + if (signal_pending(current)) { \
  12597. + ret = -ERESTARTSYS; \
  12598. + break; \
  12599. + } \
  12600. + schedule(); \
  12601. + } \
  12602. + swait_finish(&wq, &__wait); \
  12603. +} while (0)
  12604. +
  12605. +#define __swait_event_interruptible_timeout(wq, condition, ret) \
  12606. +do { \
  12607. + DEFINE_SWAITER(__wait); \
  12608. + \
  12609. + for (;;) { \
  12610. + swait_prepare(&wq, &__wait, TASK_INTERRUPTIBLE); \
  12611. + if (condition) \
  12612. + break; \
  12613. + if (signal_pending(current)) { \
  12614. + ret = -ERESTARTSYS; \
  12615. + break; \
  12616. + } \
  12617. + ret = schedule_timeout(ret); \
  12618. + if (!ret) \
  12619. + break; \
  12620. + } \
  12621. + swait_finish(&wq, &__wait); \
  12622. +} while (0)
  12623. +
  12624. +/**
  12625. + * swait_event_interruptible - sleep until a condition gets true
  12626. + * @wq: the waitqueue to wait on
  12627. + * @condition: a C expression for the event to wait for
  12628. + *
  12629. + * The process is put to sleep (TASK_INTERRUPTIBLE) until the
  12630. + * @condition evaluates to true. The @condition is checked each time
  12631. + * the waitqueue @wq is woken up.
  12632. + *
  12633. + * wake_up() has to be called after changing any variable that could
  12634. + * change the result of the wait condition.
  12635. + */
  12636. +#define swait_event_interruptible(wq, condition) \
  12637. +({ \
  12638. + int __ret = 0; \
  12639. + if (!(condition)) \
  12640. + __swait_event_interruptible(wq, condition, __ret); \
  12641. + __ret; \
  12642. +})
  12643. +
  12644. +#define swait_event_interruptible_timeout(wq, condition, timeout) \
  12645. +({ \
  12646. + int __ret = timeout; \
  12647. + if (!(condition)) \
  12648. + __swait_event_interruptible_timeout(wq, condition, __ret); \
  12649. + __ret; \
  12650. +})
  12651. +
  12652. +#define __swait_event_timeout(wq, condition, ret) \
  12653. +do { \
  12654. + DEFINE_SWAITER(__wait); \
  12655. + \
  12656. + for (;;) { \
  12657. + swait_prepare(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
  12658. + if (condition) \
  12659. + break; \
  12660. + ret = schedule_timeout(ret); \
  12661. + if (!ret) \
  12662. + break; \
  12663. + } \
  12664. + swait_finish(&wq, &__wait); \
  12665. +} while (0)
  12666. +
  12667. +/**
  12668. + * swait_event_timeout - sleep until a condition gets true or a timeout elapses
  12669. + * @wq: the waitqueue to wait on
  12670. + * @condition: a C expression for the event to wait for
  12671. + * @timeout: timeout, in jiffies
  12672. + *
  12673. + * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
  12674. + * @condition evaluates to true. The @condition is checked each time
  12675. + * the waitqueue @wq is woken up.
  12676. + *
  12677. + * wake_up() has to be called after changing any variable that could
  12678. + * change the result of the wait condition.
  12679. + *
  12680. + * The function returns 0 if the @timeout elapsed, and the remaining
  12681. + * jiffies if the condition evaluated to true before the timeout elapsed.
  12682. + */
  12683. +#define swait_event_timeout(wq, condition, timeout) \
  12684. +({ \
  12685. + long __ret = timeout; \
  12686. + if (!(condition)) \
  12687. + __swait_event_timeout(wq, condition, __ret); \
  12688. + __ret; \
  12689. +})
  12690. +
  12691. +#endif
  12692. diff -Nur linux-4.1.20.orig/include/linux/wait.h linux-4.1.20/include/linux/wait.h
  12693. --- linux-4.1.20.orig/include/linux/wait.h 2016-03-17 19:11:03.000000000 +0100
  12694. +++ linux-4.1.20/include/linux/wait.h 2016-03-21 20:18:33.000000000 +0100
  12695. @@ -8,6 +8,7 @@
  12696. #include <linux/spinlock.h>
  12697. #include <asm/current.h>
  12698. #include <uapi/linux/wait.h>
  12699. +#include <linux/atomic.h>
  12700. typedef struct __wait_queue wait_queue_t;
  12701. typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key);
  12702. diff -Nur linux-4.1.20.orig/include/linux/work-simple.h linux-4.1.20/include/linux/work-simple.h
  12703. --- linux-4.1.20.orig/include/linux/work-simple.h 1970-01-01 01:00:00.000000000 +0100
  12704. +++ linux-4.1.20/include/linux/work-simple.h 2016-03-21 20:18:33.000000000 +0100
  12705. @@ -0,0 +1,24 @@
  12706. +#ifndef _LINUX_SWORK_H
  12707. +#define _LINUX_SWORK_H
  12708. +
  12709. +#include <linux/list.h>
  12710. +
  12711. +struct swork_event {
  12712. + struct list_head item;
  12713. + unsigned long flags;
  12714. + void (*func)(struct swork_event *);
  12715. +};
  12716. +
  12717. +static inline void INIT_SWORK(struct swork_event *event,
  12718. + void (*func)(struct swork_event *))
  12719. +{
  12720. + event->flags = 0;
  12721. + event->func = func;
  12722. +}
  12723. +
  12724. +bool swork_queue(struct swork_event *sev);
  12725. +
  12726. +int swork_get(void);
  12727. +void swork_put(void);
  12728. +
  12729. +#endif /* _LINUX_SWORK_H */
  12730. diff -Nur linux-4.1.20.orig/include/net/dst.h linux-4.1.20/include/net/dst.h
  12731. --- linux-4.1.20.orig/include/net/dst.h 2016-03-17 19:11:03.000000000 +0100
  12732. +++ linux-4.1.20/include/net/dst.h 2016-03-21 20:18:33.000000000 +0100
  12733. @@ -436,7 +436,7 @@
  12734. static inline int dst_neigh_output(struct dst_entry *dst, struct neighbour *n,
  12735. struct sk_buff *skb)
  12736. {
  12737. - const struct hh_cache *hh;
  12738. + struct hh_cache *hh;
  12739. if (dst->pending_confirm) {
  12740. unsigned long now = jiffies;
  12741. diff -Nur linux-4.1.20.orig/include/net/neighbour.h linux-4.1.20/include/net/neighbour.h
  12742. --- linux-4.1.20.orig/include/net/neighbour.h 2016-03-17 19:11:03.000000000 +0100
  12743. +++ linux-4.1.20/include/net/neighbour.h 2016-03-21 20:18:33.000000000 +0100
  12744. @@ -445,7 +445,7 @@
  12745. }
  12746. #endif
  12747. -static inline int neigh_hh_output(const struct hh_cache *hh, struct sk_buff *skb)
  12748. +static inline int neigh_hh_output(struct hh_cache *hh, struct sk_buff *skb)
  12749. {
  12750. unsigned int seq;
  12751. int hh_len;
  12752. @@ -500,7 +500,7 @@
  12753. #define NEIGH_CB(skb) ((struct neighbour_cb *)(skb)->cb)
  12754. -static inline void neigh_ha_snapshot(char *dst, const struct neighbour *n,
  12755. +static inline void neigh_ha_snapshot(char *dst, struct neighbour *n,
  12756. const struct net_device *dev)
  12757. {
  12758. unsigned int seq;
  12759. diff -Nur linux-4.1.20.orig/include/net/netns/ipv4.h linux-4.1.20/include/net/netns/ipv4.h
  12760. --- linux-4.1.20.orig/include/net/netns/ipv4.h 2016-03-17 19:11:03.000000000 +0100
  12761. +++ linux-4.1.20/include/net/netns/ipv4.h 2016-03-21 20:18:33.000000000 +0100
  12762. @@ -69,6 +69,7 @@
  12763. int sysctl_icmp_echo_ignore_all;
  12764. int sysctl_icmp_echo_ignore_broadcasts;
  12765. + int sysctl_icmp_echo_sysrq;
  12766. int sysctl_icmp_ignore_bogus_error_responses;
  12767. int sysctl_icmp_ratelimit;
  12768. int sysctl_icmp_ratemask;
  12769. diff -Nur linux-4.1.20.orig/include/trace/events/hist.h linux-4.1.20/include/trace/events/hist.h
  12770. --- linux-4.1.20.orig/include/trace/events/hist.h 1970-01-01 01:00:00.000000000 +0100
  12771. +++ linux-4.1.20/include/trace/events/hist.h 2016-03-21 20:18:33.000000000 +0100
  12772. @@ -0,0 +1,74 @@
  12773. +#undef TRACE_SYSTEM
  12774. +#define TRACE_SYSTEM hist
  12775. +
  12776. +#if !defined(_TRACE_HIST_H) || defined(TRACE_HEADER_MULTI_READ)
  12777. +#define _TRACE_HIST_H
  12778. +
  12779. +#include "latency_hist.h"
  12780. +#include <linux/tracepoint.h>
  12781. +
  12782. +#if !defined(CONFIG_PREEMPT_OFF_HIST) && !defined(CONFIG_INTERRUPT_OFF_HIST)
  12783. +#define trace_preemptirqsoff_hist(a, b)
  12784. +#define trace_preemptirqsoff_hist_rcuidle(a, b)
  12785. +#else
  12786. +TRACE_EVENT(preemptirqsoff_hist,
  12787. +
  12788. + TP_PROTO(int reason, int starthist),
  12789. +
  12790. + TP_ARGS(reason, starthist),
  12791. +
  12792. + TP_STRUCT__entry(
  12793. + __field(int, reason)
  12794. + __field(int, starthist)
  12795. + ),
  12796. +
  12797. + TP_fast_assign(
  12798. + __entry->reason = reason;
  12799. + __entry->starthist = starthist;
  12800. + ),
  12801. +
  12802. + TP_printk("reason=%s starthist=%s", getaction(__entry->reason),
  12803. + __entry->starthist ? "start" : "stop")
  12804. +);
  12805. +#endif
  12806. +
  12807. +#ifndef CONFIG_MISSED_TIMER_OFFSETS_HIST
  12808. +#define trace_hrtimer_interrupt(a, b, c, d)
  12809. +#define trace_hrtimer_interrupt_rcuidle(a, b, c, d)
  12810. +#else
  12811. +TRACE_EVENT(hrtimer_interrupt,
  12812. +
  12813. + TP_PROTO(int cpu, long long offset, struct task_struct *curr,
  12814. + struct task_struct *task),
  12815. +
  12816. + TP_ARGS(cpu, offset, curr, task),
  12817. +
  12818. + TP_STRUCT__entry(
  12819. + __field(int, cpu)
  12820. + __field(long long, offset)
  12821. + __array(char, ccomm, TASK_COMM_LEN)
  12822. + __field(int, cprio)
  12823. + __array(char, tcomm, TASK_COMM_LEN)
  12824. + __field(int, tprio)
  12825. + ),
  12826. +
  12827. + TP_fast_assign(
  12828. + __entry->cpu = cpu;
  12829. + __entry->offset = offset;
  12830. + memcpy(__entry->ccomm, curr->comm, TASK_COMM_LEN);
  12831. + __entry->cprio = curr->prio;
  12832. + memcpy(__entry->tcomm, task != NULL ? task->comm : "<none>",
  12833. + task != NULL ? TASK_COMM_LEN : 7);
  12834. + __entry->tprio = task != NULL ? task->prio : -1;
  12835. + ),
  12836. +
  12837. + TP_printk("cpu=%d offset=%lld curr=%s[%d] thread=%s[%d]",
  12838. + __entry->cpu, __entry->offset, __entry->ccomm,
  12839. + __entry->cprio, __entry->tcomm, __entry->tprio)
  12840. +);
  12841. +#endif
  12842. +
  12843. +#endif /* _TRACE_HIST_H */
  12844. +
  12845. +/* This part must be outside protection */
  12846. +#include <trace/define_trace.h>
  12847. diff -Nur linux-4.1.20.orig/include/trace/events/latency_hist.h linux-4.1.20/include/trace/events/latency_hist.h
  12848. --- linux-4.1.20.orig/include/trace/events/latency_hist.h 1970-01-01 01:00:00.000000000 +0100
  12849. +++ linux-4.1.20/include/trace/events/latency_hist.h 2016-03-21 20:18:33.000000000 +0100
  12850. @@ -0,0 +1,29 @@
  12851. +#ifndef _LATENCY_HIST_H
  12852. +#define _LATENCY_HIST_H
  12853. +
  12854. +enum hist_action {
  12855. + IRQS_ON,
  12856. + PREEMPT_ON,
  12857. + TRACE_STOP,
  12858. + IRQS_OFF,
  12859. + PREEMPT_OFF,
  12860. + TRACE_START,
  12861. +};
  12862. +
  12863. +static char *actions[] = {
  12864. + "IRQS_ON",
  12865. + "PREEMPT_ON",
  12866. + "TRACE_STOP",
  12867. + "IRQS_OFF",
  12868. + "PREEMPT_OFF",
  12869. + "TRACE_START",
  12870. +};
  12871. +
  12872. +static inline char *getaction(int action)
  12873. +{
  12874. + if (action >= 0 && action <= sizeof(actions)/sizeof(actions[0]))
  12875. + return actions[action];
  12876. + return "unknown";
  12877. +}
  12878. +
  12879. +#endif /* _LATENCY_HIST_H */
  12880. diff -Nur linux-4.1.20.orig/include/trace/events/sched.h linux-4.1.20/include/trace/events/sched.h
  12881. --- linux-4.1.20.orig/include/trace/events/sched.h 2016-03-17 19:11:03.000000000 +0100
  12882. +++ linux-4.1.20/include/trace/events/sched.h 2016-03-21 20:18:33.000000000 +0100
  12883. @@ -55,9 +55,9 @@
  12884. */
  12885. DECLARE_EVENT_CLASS(sched_wakeup_template,
  12886. - TP_PROTO(struct task_struct *p, int success),
  12887. + TP_PROTO(struct task_struct *p),
  12888. - TP_ARGS(__perf_task(p), success),
  12889. + TP_ARGS(__perf_task(p)),
  12890. TP_STRUCT__entry(
  12891. __array( char, comm, TASK_COMM_LEN )
  12892. @@ -71,25 +71,37 @@
  12893. memcpy(__entry->comm, p->comm, TASK_COMM_LEN);
  12894. __entry->pid = p->pid;
  12895. __entry->prio = p->prio;
  12896. - __entry->success = success;
  12897. + __entry->success = 1; /* rudiment, kill when possible */
  12898. __entry->target_cpu = task_cpu(p);
  12899. ),
  12900. - TP_printk("comm=%s pid=%d prio=%d success=%d target_cpu=%03d",
  12901. + TP_printk("comm=%s pid=%d prio=%d target_cpu=%03d",
  12902. __entry->comm, __entry->pid, __entry->prio,
  12903. - __entry->success, __entry->target_cpu)
  12904. + __entry->target_cpu)
  12905. );
  12906. +/*
  12907. + * Tracepoint called when waking a task; this tracepoint is guaranteed to be
  12908. + * called from the waking context.
  12909. + */
  12910. +DEFINE_EVENT(sched_wakeup_template, sched_waking,
  12911. + TP_PROTO(struct task_struct *p),
  12912. + TP_ARGS(p));
  12913. +
  12914. +/*
  12915. + * Tracepoint called when the task is actually woken; p->state == TASK_RUNNNG.
  12916. + * It it not always called from the waking context.
  12917. + */
  12918. DEFINE_EVENT(sched_wakeup_template, sched_wakeup,
  12919. - TP_PROTO(struct task_struct *p, int success),
  12920. - TP_ARGS(p, success));
  12921. + TP_PROTO(struct task_struct *p),
  12922. + TP_ARGS(p));
  12923. /*
  12924. * Tracepoint for waking up a new task:
  12925. */
  12926. DEFINE_EVENT(sched_wakeup_template, sched_wakeup_new,
  12927. - TP_PROTO(struct task_struct *p, int success),
  12928. - TP_ARGS(p, success));
  12929. + TP_PROTO(struct task_struct *p),
  12930. + TP_ARGS(p));
  12931. #ifdef CREATE_TRACE_POINTS
  12932. static inline long __trace_sched_switch_state(struct task_struct *p)
  12933. diff -Nur linux-4.1.20.orig/init/Kconfig linux-4.1.20/init/Kconfig
  12934. --- linux-4.1.20.orig/init/Kconfig 2016-03-17 19:11:03.000000000 +0100
  12935. +++ linux-4.1.20/init/Kconfig 2016-03-21 20:18:33.000000000 +0100
  12936. @@ -637,7 +637,7 @@
  12937. config RCU_FAST_NO_HZ
  12938. bool "Accelerate last non-dyntick-idle CPU's grace periods"
  12939. - depends on NO_HZ_COMMON && SMP
  12940. + depends on NO_HZ_COMMON && SMP && !PREEMPT_RT_FULL
  12941. default n
  12942. help
  12943. This option permits CPUs to enter dynticks-idle state even if
  12944. @@ -664,7 +664,7 @@
  12945. config RCU_BOOST
  12946. bool "Enable RCU priority boosting"
  12947. depends on RT_MUTEXES && PREEMPT_RCU
  12948. - default n
  12949. + default y if PREEMPT_RT_FULL
  12950. help
  12951. This option boosts the priority of preempted RCU readers that
  12952. block the current preemptible RCU grace period for too long.
  12953. @@ -1101,6 +1101,7 @@
  12954. config RT_GROUP_SCHED
  12955. bool "Group scheduling for SCHED_RR/FIFO"
  12956. depends on CGROUP_SCHED
  12957. + depends on !PREEMPT_RT_FULL
  12958. default n
  12959. help
  12960. This feature lets you explicitly allocate real CPU bandwidth
  12961. @@ -1688,6 +1689,7 @@
  12962. config SLAB
  12963. bool "SLAB"
  12964. + depends on !PREEMPT_RT_FULL
  12965. help
  12966. The regular slab allocator that is established and known to work
  12967. well in all environments. It organizes cache hot objects in
  12968. @@ -1706,6 +1708,7 @@
  12969. config SLOB
  12970. depends on EXPERT
  12971. bool "SLOB (Simple Allocator)"
  12972. + depends on !PREEMPT_RT_FULL
  12973. help
  12974. SLOB replaces the stock allocator with a drastically simpler
  12975. allocator. SLOB is generally more space efficient but
  12976. @@ -1715,7 +1718,7 @@
  12977. config SLUB_CPU_PARTIAL
  12978. default y
  12979. - depends on SLUB && SMP
  12980. + depends on SLUB && SMP && !PREEMPT_RT_FULL
  12981. bool "SLUB per cpu partial cache"
  12982. help
  12983. Per cpu partial caches accellerate objects allocation and freeing
  12984. diff -Nur linux-4.1.20.orig/init/Makefile linux-4.1.20/init/Makefile
  12985. --- linux-4.1.20.orig/init/Makefile 2016-03-17 19:11:03.000000000 +0100
  12986. +++ linux-4.1.20/init/Makefile 2016-03-21 20:18:33.000000000 +0100
  12987. @@ -33,4 +33,4 @@
  12988. include/generated/compile.h: FORCE
  12989. @$($(quiet)chk_compile.h)
  12990. $(Q)$(CONFIG_SHELL) $(srctree)/scripts/mkcompile_h $@ \
  12991. - "$(UTS_MACHINE)" "$(CONFIG_SMP)" "$(CONFIG_PREEMPT)" "$(CC) $(KBUILD_CFLAGS)"
  12992. + "$(UTS_MACHINE)" "$(CONFIG_SMP)" "$(CONFIG_PREEMPT)" "$(CONFIG_PREEMPT_RT_FULL)" "$(CC) $(KBUILD_CFLAGS)"
  12993. diff -Nur linux-4.1.20.orig/init/main.c linux-4.1.20/init/main.c
  12994. --- linux-4.1.20.orig/init/main.c 2016-03-17 19:11:03.000000000 +0100
  12995. +++ linux-4.1.20/init/main.c 2016-03-21 20:18:33.000000000 +0100
  12996. @@ -525,6 +525,7 @@
  12997. setup_command_line(command_line);
  12998. setup_nr_cpu_ids();
  12999. setup_per_cpu_areas();
  13000. + softirq_early_init();
  13001. smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */
  13002. build_all_zonelists(NULL, NULL);
  13003. diff -Nur linux-4.1.20.orig/ipc/mqueue.c linux-4.1.20/ipc/mqueue.c
  13004. --- linux-4.1.20.orig/ipc/mqueue.c 2016-03-17 19:11:03.000000000 +0100
  13005. +++ linux-4.1.20/ipc/mqueue.c 2016-03-21 20:18:33.000000000 +0100
  13006. @@ -47,8 +47,7 @@
  13007. #define RECV 1
  13008. #define STATE_NONE 0
  13009. -#define STATE_PENDING 1
  13010. -#define STATE_READY 2
  13011. +#define STATE_READY 1
  13012. struct posix_msg_tree_node {
  13013. struct rb_node rb_node;
  13014. @@ -568,15 +567,12 @@
  13015. wq_add(info, sr, ewp);
  13016. for (;;) {
  13017. - set_current_state(TASK_INTERRUPTIBLE);
  13018. + __set_current_state(TASK_INTERRUPTIBLE);
  13019. spin_unlock(&info->lock);
  13020. time = schedule_hrtimeout_range_clock(timeout, 0,
  13021. HRTIMER_MODE_ABS, CLOCK_REALTIME);
  13022. - while (ewp->state == STATE_PENDING)
  13023. - cpu_relax();
  13024. -
  13025. if (ewp->state == STATE_READY) {
  13026. retval = 0;
  13027. goto out;
  13028. @@ -904,11 +900,15 @@
  13029. * list of waiting receivers. A sender checks that list before adding the new
  13030. * message into the message array. If there is a waiting receiver, then it
  13031. * bypasses the message array and directly hands the message over to the
  13032. - * receiver.
  13033. - * The receiver accepts the message and returns without grabbing the queue
  13034. - * spinlock. Therefore an intermediate STATE_PENDING state and memory barriers
  13035. - * are necessary. The same algorithm is used for sysv semaphores, see
  13036. - * ipc/sem.c for more details.
  13037. + * receiver. The receiver accepts the message and returns without grabbing the
  13038. + * queue spinlock:
  13039. + *
  13040. + * - Set pointer to message.
  13041. + * - Queue the receiver task for later wakeup (without the info->lock).
  13042. + * - Update its state to STATE_READY. Now the receiver can continue.
  13043. + * - Wake up the process after the lock is dropped. Should the process wake up
  13044. + * before this wakeup (due to a timeout or a signal) it will either see
  13045. + * STATE_READY and continue or acquire the lock to check the state again.
  13046. *
  13047. * The same algorithm is used for senders.
  13048. */
  13049. @@ -916,21 +916,29 @@
  13050. /* pipelined_send() - send a message directly to the task waiting in
  13051. * sys_mq_timedreceive() (without inserting message into a queue).
  13052. */
  13053. -static inline void pipelined_send(struct mqueue_inode_info *info,
  13054. +static inline void pipelined_send(struct wake_q_head *wake_q,
  13055. + struct mqueue_inode_info *info,
  13056. struct msg_msg *message,
  13057. struct ext_wait_queue *receiver)
  13058. {
  13059. receiver->msg = message;
  13060. list_del(&receiver->list);
  13061. - receiver->state = STATE_PENDING;
  13062. - wake_up_process(receiver->task);
  13063. - smp_wmb();
  13064. + wake_q_add(wake_q, receiver->task);
  13065. + /*
  13066. + * Rely on the implicit cmpxchg barrier from wake_q_add such
  13067. + * that we can ensure that updating receiver->state is the last
  13068. + * write operation: As once set, the receiver can continue,
  13069. + * and if we don't have the reference count from the wake_q,
  13070. + * yet, at that point we can later have a use-after-free
  13071. + * condition and bogus wakeup.
  13072. + */
  13073. receiver->state = STATE_READY;
  13074. }
  13075. /* pipelined_receive() - if there is task waiting in sys_mq_timedsend()
  13076. * gets its message and put to the queue (we have one free place for sure). */
  13077. -static inline void pipelined_receive(struct mqueue_inode_info *info)
  13078. +static inline void pipelined_receive(struct wake_q_head *wake_q,
  13079. + struct mqueue_inode_info *info)
  13080. {
  13081. struct ext_wait_queue *sender = wq_get_first_waiter(info, SEND);
  13082. @@ -941,10 +949,9 @@
  13083. }
  13084. if (msg_insert(sender->msg, info))
  13085. return;
  13086. +
  13087. list_del(&sender->list);
  13088. - sender->state = STATE_PENDING;
  13089. - wake_up_process(sender->task);
  13090. - smp_wmb();
  13091. + wake_q_add(wake_q, sender->task);
  13092. sender->state = STATE_READY;
  13093. }
  13094. @@ -962,6 +969,7 @@
  13095. struct timespec ts;
  13096. struct posix_msg_tree_node *new_leaf = NULL;
  13097. int ret = 0;
  13098. + WAKE_Q(wake_q);
  13099. if (u_abs_timeout) {
  13100. int res = prepare_timeout(u_abs_timeout, &expires, &ts);
  13101. @@ -1045,7 +1053,7 @@
  13102. } else {
  13103. receiver = wq_get_first_waiter(info, RECV);
  13104. if (receiver) {
  13105. - pipelined_send(info, msg_ptr, receiver);
  13106. + pipelined_send(&wake_q, info, msg_ptr, receiver);
  13107. } else {
  13108. /* adds message to the queue */
  13109. ret = msg_insert(msg_ptr, info);
  13110. @@ -1058,6 +1066,7 @@
  13111. }
  13112. out_unlock:
  13113. spin_unlock(&info->lock);
  13114. + wake_up_q(&wake_q);
  13115. out_free:
  13116. if (ret)
  13117. free_msg(msg_ptr);
  13118. @@ -1144,14 +1153,17 @@
  13119. msg_ptr = wait.msg;
  13120. }
  13121. } else {
  13122. + WAKE_Q(wake_q);
  13123. +
  13124. msg_ptr = msg_get(info);
  13125. inode->i_atime = inode->i_mtime = inode->i_ctime =
  13126. CURRENT_TIME;
  13127. /* There is now free space in queue. */
  13128. - pipelined_receive(info);
  13129. + pipelined_receive(&wake_q, info);
  13130. spin_unlock(&info->lock);
  13131. + wake_up_q(&wake_q);
  13132. ret = 0;
  13133. }
  13134. if (ret == 0) {
  13135. diff -Nur linux-4.1.20.orig/ipc/msg.c linux-4.1.20/ipc/msg.c
  13136. --- linux-4.1.20.orig/ipc/msg.c 2016-03-17 19:11:03.000000000 +0100
  13137. +++ linux-4.1.20/ipc/msg.c 2016-03-21 20:18:33.000000000 +0100
  13138. @@ -188,6 +188,12 @@
  13139. struct msg_receiver *msr, *t;
  13140. list_for_each_entry_safe(msr, t, &msq->q_receivers, r_list) {
  13141. + /*
  13142. + * Make sure that the wakeup doesnt preempt
  13143. + * this CPU prematurely. (on PREEMPT_RT)
  13144. + */
  13145. + preempt_disable_rt();
  13146. +
  13147. msr->r_msg = NULL; /* initialize expunge ordering */
  13148. wake_up_process(msr->r_tsk);
  13149. /*
  13150. @@ -198,6 +204,8 @@
  13151. */
  13152. smp_mb();
  13153. msr->r_msg = ERR_PTR(res);
  13154. +
  13155. + preempt_enable_rt();
  13156. }
  13157. }
  13158. @@ -574,6 +582,11 @@
  13159. if (testmsg(msg, msr->r_msgtype, msr->r_mode) &&
  13160. !security_msg_queue_msgrcv(msq, msg, msr->r_tsk,
  13161. msr->r_msgtype, msr->r_mode)) {
  13162. + /*
  13163. + * Make sure that the wakeup doesnt preempt
  13164. + * this CPU prematurely. (on PREEMPT_RT)
  13165. + */
  13166. + preempt_disable_rt();
  13167. list_del(&msr->r_list);
  13168. if (msr->r_maxsize < msg->m_ts) {
  13169. @@ -595,12 +608,13 @@
  13170. */
  13171. smp_mb();
  13172. msr->r_msg = msg;
  13173. + preempt_enable_rt();
  13174. return 1;
  13175. }
  13176. + preempt_enable_rt();
  13177. }
  13178. }
  13179. -
  13180. return 0;
  13181. }
  13182. diff -Nur linux-4.1.20.orig/ipc/sem.c linux-4.1.20/ipc/sem.c
  13183. --- linux-4.1.20.orig/ipc/sem.c 2016-03-17 19:11:03.000000000 +0100
  13184. +++ linux-4.1.20/ipc/sem.c 2016-03-21 20:18:33.000000000 +0100
  13185. @@ -690,6 +690,13 @@
  13186. static void wake_up_sem_queue_prepare(struct list_head *pt,
  13187. struct sem_queue *q, int error)
  13188. {
  13189. +#ifdef CONFIG_PREEMPT_RT_BASE
  13190. + struct task_struct *p = q->sleeper;
  13191. + get_task_struct(p);
  13192. + q->status = error;
  13193. + wake_up_process(p);
  13194. + put_task_struct(p);
  13195. +#else
  13196. if (list_empty(pt)) {
  13197. /*
  13198. * Hold preempt off so that we don't get preempted and have the
  13199. @@ -701,6 +708,7 @@
  13200. q->pid = error;
  13201. list_add_tail(&q->list, pt);
  13202. +#endif
  13203. }
  13204. /**
  13205. @@ -714,6 +722,7 @@
  13206. */
  13207. static void wake_up_sem_queue_do(struct list_head *pt)
  13208. {
  13209. +#ifndef CONFIG_PREEMPT_RT_BASE
  13210. struct sem_queue *q, *t;
  13211. int did_something;
  13212. @@ -726,6 +735,7 @@
  13213. }
  13214. if (did_something)
  13215. preempt_enable();
  13216. +#endif
  13217. }
  13218. static void unlink_queue(struct sem_array *sma, struct sem_queue *q)
  13219. diff -Nur linux-4.1.20.orig/kernel/Kconfig.locks linux-4.1.20/kernel/Kconfig.locks
  13220. --- linux-4.1.20.orig/kernel/Kconfig.locks 2016-03-17 19:11:03.000000000 +0100
  13221. +++ linux-4.1.20/kernel/Kconfig.locks 2016-03-21 20:18:33.000000000 +0100
  13222. @@ -225,11 +225,11 @@
  13223. config MUTEX_SPIN_ON_OWNER
  13224. def_bool y
  13225. - depends on SMP && !DEBUG_MUTEXES && ARCH_SUPPORTS_ATOMIC_RMW
  13226. + depends on SMP && !DEBUG_MUTEXES && ARCH_SUPPORTS_ATOMIC_RMW && !PREEMPT_RT_FULL
  13227. config RWSEM_SPIN_ON_OWNER
  13228. def_bool y
  13229. - depends on SMP && RWSEM_XCHGADD_ALGORITHM && ARCH_SUPPORTS_ATOMIC_RMW
  13230. + depends on SMP && RWSEM_XCHGADD_ALGORITHM && ARCH_SUPPORTS_ATOMIC_RMW && !PREEMPT_RT_FULL
  13231. config LOCK_SPIN_ON_OWNER
  13232. def_bool y
  13233. diff -Nur linux-4.1.20.orig/kernel/Kconfig.preempt linux-4.1.20/kernel/Kconfig.preempt
  13234. --- linux-4.1.20.orig/kernel/Kconfig.preempt 2016-03-17 19:11:03.000000000 +0100
  13235. +++ linux-4.1.20/kernel/Kconfig.preempt 2016-03-21 20:18:33.000000000 +0100
  13236. @@ -1,3 +1,16 @@
  13237. +config PREEMPT
  13238. + bool
  13239. + select PREEMPT_COUNT
  13240. +
  13241. +config PREEMPT_RT_BASE
  13242. + bool
  13243. + select PREEMPT
  13244. +
  13245. +config HAVE_PREEMPT_LAZY
  13246. + bool
  13247. +
  13248. +config PREEMPT_LAZY
  13249. + def_bool y if HAVE_PREEMPT_LAZY && PREEMPT_RT_FULL
  13250. choice
  13251. prompt "Preemption Model"
  13252. @@ -33,9 +46,9 @@
  13253. Select this if you are building a kernel for a desktop system.
  13254. -config PREEMPT
  13255. +config PREEMPT__LL
  13256. bool "Preemptible Kernel (Low-Latency Desktop)"
  13257. - select PREEMPT_COUNT
  13258. + select PREEMPT
  13259. select UNINLINE_SPIN_UNLOCK if !ARCH_INLINE_SPIN_UNLOCK
  13260. help
  13261. This option reduces the latency of the kernel by making
  13262. @@ -52,6 +65,22 @@
  13263. embedded system with latency requirements in the milliseconds
  13264. range.
  13265. +config PREEMPT_RTB
  13266. + bool "Preemptible Kernel (Basic RT)"
  13267. + select PREEMPT_RT_BASE
  13268. + help
  13269. + This option is basically the same as (Low-Latency Desktop) but
  13270. + enables changes which are preliminary for the full preemptible
  13271. + RT kernel.
  13272. +
  13273. +config PREEMPT_RT_FULL
  13274. + bool "Fully Preemptible Kernel (RT)"
  13275. + depends on IRQ_FORCED_THREADING
  13276. + select PREEMPT_RT_BASE
  13277. + select PREEMPT_RCU
  13278. + help
  13279. + All and everything
  13280. +
  13281. endchoice
  13282. config PREEMPT_COUNT
  13283. diff -Nur linux-4.1.20.orig/kernel/bpf/hashtab.c linux-4.1.20/kernel/bpf/hashtab.c
  13284. --- linux-4.1.20.orig/kernel/bpf/hashtab.c 2016-03-17 19:11:03.000000000 +0100
  13285. +++ linux-4.1.20/kernel/bpf/hashtab.c 2016-03-21 20:18:33.000000000 +0100
  13286. @@ -17,7 +17,7 @@
  13287. struct bpf_htab {
  13288. struct bpf_map map;
  13289. struct hlist_head *buckets;
  13290. - spinlock_t lock;
  13291. + raw_spinlock_t lock;
  13292. u32 count; /* number of elements in this hashtable */
  13293. u32 n_buckets; /* number of hash buckets */
  13294. u32 elem_size; /* size of each element in bytes */
  13295. @@ -82,7 +82,7 @@
  13296. for (i = 0; i < htab->n_buckets; i++)
  13297. INIT_HLIST_HEAD(&htab->buckets[i]);
  13298. - spin_lock_init(&htab->lock);
  13299. + raw_spin_lock_init(&htab->lock);
  13300. htab->count = 0;
  13301. htab->elem_size = sizeof(struct htab_elem) +
  13302. @@ -230,7 +230,7 @@
  13303. l_new->hash = htab_map_hash(l_new->key, key_size);
  13304. /* bpf_map_update_elem() can be called in_irq() */
  13305. - spin_lock_irqsave(&htab->lock, flags);
  13306. + raw_spin_lock_irqsave(&htab->lock, flags);
  13307. head = select_bucket(htab, l_new->hash);
  13308. @@ -266,11 +266,11 @@
  13309. } else {
  13310. htab->count++;
  13311. }
  13312. - spin_unlock_irqrestore(&htab->lock, flags);
  13313. + raw_spin_unlock_irqrestore(&htab->lock, flags);
  13314. return 0;
  13315. err:
  13316. - spin_unlock_irqrestore(&htab->lock, flags);
  13317. + raw_spin_unlock_irqrestore(&htab->lock, flags);
  13318. kfree(l_new);
  13319. return ret;
  13320. }
  13321. @@ -291,7 +291,7 @@
  13322. hash = htab_map_hash(key, key_size);
  13323. - spin_lock_irqsave(&htab->lock, flags);
  13324. + raw_spin_lock_irqsave(&htab->lock, flags);
  13325. head = select_bucket(htab, hash);
  13326. @@ -304,7 +304,7 @@
  13327. ret = 0;
  13328. }
  13329. - spin_unlock_irqrestore(&htab->lock, flags);
  13330. + raw_spin_unlock_irqrestore(&htab->lock, flags);
  13331. return ret;
  13332. }
  13333. diff -Nur linux-4.1.20.orig/kernel/cgroup.c linux-4.1.20/kernel/cgroup.c
  13334. --- linux-4.1.20.orig/kernel/cgroup.c 2016-03-17 19:11:03.000000000 +0100
  13335. +++ linux-4.1.20/kernel/cgroup.c 2016-03-21 20:18:33.000000000 +0100
  13336. @@ -4422,10 +4422,10 @@
  13337. queue_work(cgroup_destroy_wq, &css->destroy_work);
  13338. }
  13339. -static void css_release_work_fn(struct work_struct *work)
  13340. +static void css_release_work_fn(struct swork_event *sev)
  13341. {
  13342. struct cgroup_subsys_state *css =
  13343. - container_of(work, struct cgroup_subsys_state, destroy_work);
  13344. + container_of(sev, struct cgroup_subsys_state, destroy_swork);
  13345. struct cgroup_subsys *ss = css->ss;
  13346. struct cgroup *cgrp = css->cgroup;
  13347. @@ -4464,8 +4464,8 @@
  13348. struct cgroup_subsys_state *css =
  13349. container_of(ref, struct cgroup_subsys_state, refcnt);
  13350. - INIT_WORK(&css->destroy_work, css_release_work_fn);
  13351. - queue_work(cgroup_destroy_wq, &css->destroy_work);
  13352. + INIT_SWORK(&css->destroy_swork, css_release_work_fn);
  13353. + swork_queue(&css->destroy_swork);
  13354. }
  13355. static void init_and_link_css(struct cgroup_subsys_state *css,
  13356. @@ -5081,6 +5081,7 @@
  13357. */
  13358. cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
  13359. BUG_ON(!cgroup_destroy_wq);
  13360. + BUG_ON(swork_get());
  13361. /*
  13362. * Used to destroy pidlists and separate to serve as flush domain.
  13363. diff -Nur linux-4.1.20.orig/kernel/cpu.c linux-4.1.20/kernel/cpu.c
  13364. --- linux-4.1.20.orig/kernel/cpu.c 2016-03-17 19:11:03.000000000 +0100
  13365. +++ linux-4.1.20/kernel/cpu.c 2016-03-21 20:18:33.000000000 +0100
  13366. @@ -74,8 +74,8 @@
  13367. #endif
  13368. } cpu_hotplug = {
  13369. .active_writer = NULL,
  13370. - .wq = __WAIT_QUEUE_HEAD_INITIALIZER(cpu_hotplug.wq),
  13371. .lock = __MUTEX_INITIALIZER(cpu_hotplug.lock),
  13372. + .wq = __WAIT_QUEUE_HEAD_INITIALIZER(cpu_hotplug.wq),
  13373. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  13374. .dep_map = {.name = "cpu_hotplug.lock" },
  13375. #endif
  13376. @@ -88,6 +88,289 @@
  13377. #define cpuhp_lock_acquire() lock_map_acquire(&cpu_hotplug.dep_map)
  13378. #define cpuhp_lock_release() lock_map_release(&cpu_hotplug.dep_map)
  13379. +/**
  13380. + * hotplug_pcp - per cpu hotplug descriptor
  13381. + * @unplug: set when pin_current_cpu() needs to sync tasks
  13382. + * @sync_tsk: the task that waits for tasks to finish pinned sections
  13383. + * @refcount: counter of tasks in pinned sections
  13384. + * @grab_lock: set when the tasks entering pinned sections should wait
  13385. + * @synced: notifier for @sync_tsk to tell cpu_down it's finished
  13386. + * @mutex: the mutex to make tasks wait (used when @grab_lock is true)
  13387. + * @mutex_init: zero if the mutex hasn't been initialized yet.
  13388. + *
  13389. + * Although @unplug and @sync_tsk may point to the same task, the @unplug
  13390. + * is used as a flag and still exists after @sync_tsk has exited and
  13391. + * @sync_tsk set to NULL.
  13392. + */
  13393. +struct hotplug_pcp {
  13394. + struct task_struct *unplug;
  13395. + struct task_struct *sync_tsk;
  13396. + int refcount;
  13397. + int grab_lock;
  13398. + struct completion synced;
  13399. + struct completion unplug_wait;
  13400. +#ifdef CONFIG_PREEMPT_RT_FULL
  13401. + /*
  13402. + * Note, on PREEMPT_RT, the hotplug lock must save the state of
  13403. + * the task, otherwise the mutex will cause the task to fail
  13404. + * to sleep when required. (Because it's called from migrate_disable())
  13405. + *
  13406. + * The spinlock_t on PREEMPT_RT is a mutex that saves the task's
  13407. + * state.
  13408. + */
  13409. + spinlock_t lock;
  13410. +#else
  13411. + struct mutex mutex;
  13412. +#endif
  13413. + int mutex_init;
  13414. +};
  13415. +
  13416. +#ifdef CONFIG_PREEMPT_RT_FULL
  13417. +# define hotplug_lock(hp) rt_spin_lock(&(hp)->lock)
  13418. +# define hotplug_unlock(hp) rt_spin_unlock(&(hp)->lock)
  13419. +#else
  13420. +# define hotplug_lock(hp) mutex_lock(&(hp)->mutex)
  13421. +# define hotplug_unlock(hp) mutex_unlock(&(hp)->mutex)
  13422. +#endif
  13423. +
  13424. +static DEFINE_PER_CPU(struct hotplug_pcp, hotplug_pcp);
  13425. +
  13426. +/**
  13427. + * pin_current_cpu - Prevent the current cpu from being unplugged
  13428. + *
  13429. + * Lightweight version of get_online_cpus() to prevent cpu from being
  13430. + * unplugged when code runs in a migration disabled region.
  13431. + *
  13432. + * Must be called with preemption disabled (preempt_count = 1)!
  13433. + */
  13434. +void pin_current_cpu(void)
  13435. +{
  13436. + struct hotplug_pcp *hp;
  13437. + int force = 0;
  13438. +
  13439. +retry:
  13440. + hp = this_cpu_ptr(&hotplug_pcp);
  13441. +
  13442. + if (!hp->unplug || hp->refcount || force || preempt_count() > 1 ||
  13443. + hp->unplug == current) {
  13444. + hp->refcount++;
  13445. + return;
  13446. + }
  13447. + if (hp->grab_lock) {
  13448. + preempt_enable();
  13449. + hotplug_lock(hp);
  13450. + hotplug_unlock(hp);
  13451. + } else {
  13452. + preempt_enable();
  13453. + /*
  13454. + * Try to push this task off of this CPU.
  13455. + */
  13456. + if (!migrate_me()) {
  13457. + preempt_disable();
  13458. + hp = this_cpu_ptr(&hotplug_pcp);
  13459. + if (!hp->grab_lock) {
  13460. + /*
  13461. + * Just let it continue it's already pinned
  13462. + * or about to sleep.
  13463. + */
  13464. + force = 1;
  13465. + goto retry;
  13466. + }
  13467. + preempt_enable();
  13468. + }
  13469. + }
  13470. + preempt_disable();
  13471. + goto retry;
  13472. +}
  13473. +
  13474. +/**
  13475. + * unpin_current_cpu - Allow unplug of current cpu
  13476. + *
  13477. + * Must be called with preemption or interrupts disabled!
  13478. + */
  13479. +void unpin_current_cpu(void)
  13480. +{
  13481. + struct hotplug_pcp *hp = this_cpu_ptr(&hotplug_pcp);
  13482. +
  13483. + WARN_ON(hp->refcount <= 0);
  13484. +
  13485. + /* This is safe. sync_unplug_thread is pinned to this cpu */
  13486. + if (!--hp->refcount && hp->unplug && hp->unplug != current)
  13487. + wake_up_process(hp->unplug);
  13488. +}
  13489. +
  13490. +static void wait_for_pinned_cpus(struct hotplug_pcp *hp)
  13491. +{
  13492. + set_current_state(TASK_UNINTERRUPTIBLE);
  13493. + while (hp->refcount) {
  13494. + schedule_preempt_disabled();
  13495. + set_current_state(TASK_UNINTERRUPTIBLE);
  13496. + }
  13497. +}
  13498. +
  13499. +static int sync_unplug_thread(void *data)
  13500. +{
  13501. + struct hotplug_pcp *hp = data;
  13502. +
  13503. + wait_for_completion(&hp->unplug_wait);
  13504. + preempt_disable();
  13505. + hp->unplug = current;
  13506. + wait_for_pinned_cpus(hp);
  13507. +
  13508. + /*
  13509. + * This thread will synchronize the cpu_down() with threads
  13510. + * that have pinned the CPU. When the pinned CPU count reaches
  13511. + * zero, we inform the cpu_down code to continue to the next step.
  13512. + */
  13513. + set_current_state(TASK_UNINTERRUPTIBLE);
  13514. + preempt_enable();
  13515. + complete(&hp->synced);
  13516. +
  13517. + /*
  13518. + * If all succeeds, the next step will need tasks to wait till
  13519. + * the CPU is offline before continuing. To do this, the grab_lock
  13520. + * is set and tasks going into pin_current_cpu() will block on the
  13521. + * mutex. But we still need to wait for those that are already in
  13522. + * pinned CPU sections. If the cpu_down() failed, the kthread_should_stop()
  13523. + * will kick this thread out.
  13524. + */
  13525. + while (!hp->grab_lock && !kthread_should_stop()) {
  13526. + schedule();
  13527. + set_current_state(TASK_UNINTERRUPTIBLE);
  13528. + }
  13529. +
  13530. + /* Make sure grab_lock is seen before we see a stale completion */
  13531. + smp_mb();
  13532. +
  13533. + /*
  13534. + * Now just before cpu_down() enters stop machine, we need to make
  13535. + * sure all tasks that are in pinned CPU sections are out, and new
  13536. + * tasks will now grab the lock, keeping them from entering pinned
  13537. + * CPU sections.
  13538. + */
  13539. + if (!kthread_should_stop()) {
  13540. + preempt_disable();
  13541. + wait_for_pinned_cpus(hp);
  13542. + preempt_enable();
  13543. + complete(&hp->synced);
  13544. + }
  13545. +
  13546. + set_current_state(TASK_UNINTERRUPTIBLE);
  13547. + while (!kthread_should_stop()) {
  13548. + schedule();
  13549. + set_current_state(TASK_UNINTERRUPTIBLE);
  13550. + }
  13551. + set_current_state(TASK_RUNNING);
  13552. +
  13553. + /*
  13554. + * Force this thread off this CPU as it's going down and
  13555. + * we don't want any more work on this CPU.
  13556. + */
  13557. + current->flags &= ~PF_NO_SETAFFINITY;
  13558. + set_cpus_allowed_ptr(current, cpu_present_mask);
  13559. + migrate_me();
  13560. + return 0;
  13561. +}
  13562. +
  13563. +static void __cpu_unplug_sync(struct hotplug_pcp *hp)
  13564. +{
  13565. + wake_up_process(hp->sync_tsk);
  13566. + wait_for_completion(&hp->synced);
  13567. +}
  13568. +
  13569. +static void __cpu_unplug_wait(unsigned int cpu)
  13570. +{
  13571. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  13572. +
  13573. + complete(&hp->unplug_wait);
  13574. + wait_for_completion(&hp->synced);
  13575. +}
  13576. +
  13577. +/*
  13578. + * Start the sync_unplug_thread on the target cpu and wait for it to
  13579. + * complete.
  13580. + */
  13581. +static int cpu_unplug_begin(unsigned int cpu)
  13582. +{
  13583. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  13584. + int err;
  13585. +
  13586. + /* Protected by cpu_hotplug.lock */
  13587. + if (!hp->mutex_init) {
  13588. +#ifdef CONFIG_PREEMPT_RT_FULL
  13589. + spin_lock_init(&hp->lock);
  13590. +#else
  13591. + mutex_init(&hp->mutex);
  13592. +#endif
  13593. + hp->mutex_init = 1;
  13594. + }
  13595. +
  13596. + /* Inform the scheduler to migrate tasks off this CPU */
  13597. + tell_sched_cpu_down_begin(cpu);
  13598. +
  13599. + init_completion(&hp->synced);
  13600. + init_completion(&hp->unplug_wait);
  13601. +
  13602. + hp->sync_tsk = kthread_create(sync_unplug_thread, hp, "sync_unplug/%d", cpu);
  13603. + if (IS_ERR(hp->sync_tsk)) {
  13604. + err = PTR_ERR(hp->sync_tsk);
  13605. + hp->sync_tsk = NULL;
  13606. + return err;
  13607. + }
  13608. + kthread_bind(hp->sync_tsk, cpu);
  13609. +
  13610. + /*
  13611. + * Wait for tasks to get out of the pinned sections,
  13612. + * it's still OK if new tasks enter. Some CPU notifiers will
  13613. + * wait for tasks that are going to enter these sections and
  13614. + * we must not have them block.
  13615. + */
  13616. + wake_up_process(hp->sync_tsk);
  13617. + return 0;
  13618. +}
  13619. +
  13620. +static void cpu_unplug_sync(unsigned int cpu)
  13621. +{
  13622. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  13623. +
  13624. + init_completion(&hp->synced);
  13625. + /* The completion needs to be initialzied before setting grab_lock */
  13626. + smp_wmb();
  13627. +
  13628. + /* Grab the mutex before setting grab_lock */
  13629. + hotplug_lock(hp);
  13630. + hp->grab_lock = 1;
  13631. +
  13632. + /*
  13633. + * The CPU notifiers have been completed.
  13634. + * Wait for tasks to get out of pinned CPU sections and have new
  13635. + * tasks block until the CPU is completely down.
  13636. + */
  13637. + __cpu_unplug_sync(hp);
  13638. +
  13639. + /* All done with the sync thread */
  13640. + kthread_stop(hp->sync_tsk);
  13641. + hp->sync_tsk = NULL;
  13642. +}
  13643. +
  13644. +static void cpu_unplug_done(unsigned int cpu)
  13645. +{
  13646. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  13647. +
  13648. + hp->unplug = NULL;
  13649. + /* Let all tasks know cpu unplug is finished before cleaning up */
  13650. + smp_wmb();
  13651. +
  13652. + if (hp->sync_tsk)
  13653. + kthread_stop(hp->sync_tsk);
  13654. +
  13655. + if (hp->grab_lock) {
  13656. + hotplug_unlock(hp);
  13657. + /* protected by cpu_hotplug.lock */
  13658. + hp->grab_lock = 0;
  13659. + }
  13660. + tell_sched_cpu_down_done(cpu);
  13661. +}
  13662. void get_online_cpus(void)
  13663. {
  13664. @@ -349,13 +632,15 @@
  13665. /* Requires cpu_add_remove_lock to be held */
  13666. static int __ref _cpu_down(unsigned int cpu, int tasks_frozen)
  13667. {
  13668. - int err, nr_calls = 0;
  13669. + int mycpu, err, nr_calls = 0;
  13670. void *hcpu = (void *)(long)cpu;
  13671. unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
  13672. struct take_cpu_down_param tcd_param = {
  13673. .mod = mod,
  13674. .hcpu = hcpu,
  13675. };
  13676. + cpumask_var_t cpumask;
  13677. + cpumask_var_t cpumask_org;
  13678. if (num_online_cpus() == 1)
  13679. return -EBUSY;
  13680. @@ -363,7 +648,34 @@
  13681. if (!cpu_online(cpu))
  13682. return -EINVAL;
  13683. + /* Move the downtaker off the unplug cpu */
  13684. + if (!alloc_cpumask_var(&cpumask, GFP_KERNEL))
  13685. + return -ENOMEM;
  13686. + if (!alloc_cpumask_var(&cpumask_org, GFP_KERNEL)) {
  13687. + free_cpumask_var(cpumask);
  13688. + return -ENOMEM;
  13689. + }
  13690. +
  13691. + cpumask_copy(cpumask_org, tsk_cpus_allowed(current));
  13692. + cpumask_andnot(cpumask, cpu_online_mask, cpumask_of(cpu));
  13693. + set_cpus_allowed_ptr(current, cpumask);
  13694. + free_cpumask_var(cpumask);
  13695. + migrate_disable();
  13696. + mycpu = smp_processor_id();
  13697. + if (mycpu == cpu) {
  13698. + printk(KERN_ERR "Yuck! Still on unplug CPU\n!");
  13699. + migrate_enable();
  13700. + err = -EBUSY;
  13701. + goto restore_cpus;
  13702. + }
  13703. + migrate_enable();
  13704. +
  13705. cpu_hotplug_begin();
  13706. + err = cpu_unplug_begin(cpu);
  13707. + if (err) {
  13708. + printk("cpu_unplug_begin(%d) failed\n", cpu);
  13709. + goto out_cancel;
  13710. + }
  13711. err = __cpu_notify(CPU_DOWN_PREPARE | mod, hcpu, -1, &nr_calls);
  13712. if (err) {
  13713. @@ -389,8 +701,12 @@
  13714. #endif
  13715. synchronize_rcu();
  13716. + __cpu_unplug_wait(cpu);
  13717. smpboot_park_threads(cpu);
  13718. + /* Notifiers are done. Don't let any more tasks pin this CPU. */
  13719. + cpu_unplug_sync(cpu);
  13720. +
  13721. /*
  13722. * So now all preempt/rcu users must observe !cpu_active().
  13723. */
  13724. @@ -427,9 +743,14 @@
  13725. check_for_tasks(cpu);
  13726. out_release:
  13727. + cpu_unplug_done(cpu);
  13728. +out_cancel:
  13729. cpu_hotplug_done();
  13730. if (!err)
  13731. cpu_notify_nofail(CPU_POST_DEAD | mod, hcpu);
  13732. +restore_cpus:
  13733. + set_cpus_allowed_ptr(current, cpumask_org);
  13734. + free_cpumask_var(cpumask_org);
  13735. return err;
  13736. }
  13737. diff -Nur linux-4.1.20.orig/kernel/debug/kdb/kdb_io.c linux-4.1.20/kernel/debug/kdb/kdb_io.c
  13738. --- linux-4.1.20.orig/kernel/debug/kdb/kdb_io.c 2016-03-17 19:11:03.000000000 +0100
  13739. +++ linux-4.1.20/kernel/debug/kdb/kdb_io.c 2016-03-21 20:18:33.000000000 +0100
  13740. @@ -554,7 +554,6 @@
  13741. int linecount;
  13742. int colcount;
  13743. int logging, saved_loglevel = 0;
  13744. - int saved_trap_printk;
  13745. int got_printf_lock = 0;
  13746. int retlen = 0;
  13747. int fnd, len;
  13748. @@ -565,8 +564,6 @@
  13749. unsigned long uninitialized_var(flags);
  13750. preempt_disable();
  13751. - saved_trap_printk = kdb_trap_printk;
  13752. - kdb_trap_printk = 0;
  13753. /* Serialize kdb_printf if multiple cpus try to write at once.
  13754. * But if any cpu goes recursive in kdb, just print the output,
  13755. @@ -855,7 +852,6 @@
  13756. } else {
  13757. __release(kdb_printf_lock);
  13758. }
  13759. - kdb_trap_printk = saved_trap_printk;
  13760. preempt_enable();
  13761. return retlen;
  13762. }
  13763. @@ -865,9 +861,11 @@
  13764. va_list ap;
  13765. int r;
  13766. + kdb_trap_printk++;
  13767. va_start(ap, fmt);
  13768. r = vkdb_printf(KDB_MSGSRC_INTERNAL, fmt, ap);
  13769. va_end(ap);
  13770. + kdb_trap_printk--;
  13771. return r;
  13772. }
  13773. diff -Nur linux-4.1.20.orig/kernel/events/core.c linux-4.1.20/kernel/events/core.c
  13774. --- linux-4.1.20.orig/kernel/events/core.c 2016-03-17 19:11:03.000000000 +0100
  13775. +++ linux-4.1.20/kernel/events/core.c 2016-03-21 20:18:33.000000000 +0100
  13776. @@ -6925,6 +6925,7 @@
  13777. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  13778. hwc->hrtimer.function = perf_swevent_hrtimer;
  13779. + hwc->hrtimer.irqsafe = 1;
  13780. /*
  13781. * Since hrtimers have a fixed rate, we can do a static freq->period
  13782. diff -Nur linux-4.1.20.orig/kernel/exit.c linux-4.1.20/kernel/exit.c
  13783. --- linux-4.1.20.orig/kernel/exit.c 2016-03-17 19:11:03.000000000 +0100
  13784. +++ linux-4.1.20/kernel/exit.c 2016-03-21 20:18:33.000000000 +0100
  13785. @@ -144,7 +144,7 @@
  13786. * Do this under ->siglock, we can race with another thread
  13787. * doing sigqueue_free() if we have SIGQUEUE_PREALLOC signals.
  13788. */
  13789. - flush_sigqueue(&tsk->pending);
  13790. + flush_task_sigqueue(tsk);
  13791. tsk->sighand = NULL;
  13792. spin_unlock(&sighand->siglock);
  13793. diff -Nur linux-4.1.20.orig/kernel/fork.c linux-4.1.20/kernel/fork.c
  13794. --- linux-4.1.20.orig/kernel/fork.c 2016-03-17 19:11:03.000000000 +0100
  13795. +++ linux-4.1.20/kernel/fork.c 2016-03-21 20:18:33.000000000 +0100
  13796. @@ -108,7 +108,7 @@
  13797. DEFINE_PER_CPU(unsigned long, process_counts) = 0;
  13798. -__cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */
  13799. +DEFINE_RWLOCK(tasklist_lock); /* outer */
  13800. #ifdef CONFIG_PROVE_RCU
  13801. int lockdep_tasklist_lock_is_held(void)
  13802. @@ -244,7 +244,9 @@
  13803. if (atomic_dec_and_test(&sig->sigcnt))
  13804. free_signal_struct(sig);
  13805. }
  13806. -
  13807. +#ifdef CONFIG_PREEMPT_RT_BASE
  13808. +static
  13809. +#endif
  13810. void __put_task_struct(struct task_struct *tsk)
  13811. {
  13812. WARN_ON(!tsk->exit_state);
  13813. @@ -260,7 +262,18 @@
  13814. if (!profile_handoff_task(tsk))
  13815. free_task(tsk);
  13816. }
  13817. +#ifndef CONFIG_PREEMPT_RT_BASE
  13818. EXPORT_SYMBOL_GPL(__put_task_struct);
  13819. +#else
  13820. +void __put_task_struct_cb(struct rcu_head *rhp)
  13821. +{
  13822. + struct task_struct *tsk = container_of(rhp, struct task_struct, put_rcu);
  13823. +
  13824. + __put_task_struct(tsk);
  13825. +
  13826. +}
  13827. +EXPORT_SYMBOL_GPL(__put_task_struct_cb);
  13828. +#endif
  13829. void __init __weak arch_task_cache_init(void) { }
  13830. @@ -374,6 +387,7 @@
  13831. #endif
  13832. tsk->splice_pipe = NULL;
  13833. tsk->task_frag.page = NULL;
  13834. + tsk->wake_q.next = NULL;
  13835. account_kernel_stack(ti, 1);
  13836. @@ -680,6 +694,19 @@
  13837. }
  13838. EXPORT_SYMBOL_GPL(__mmdrop);
  13839. +#ifdef CONFIG_PREEMPT_RT_BASE
  13840. +/*
  13841. + * RCU callback for delayed mm drop. Not strictly rcu, but we don't
  13842. + * want another facility to make this work.
  13843. + */
  13844. +void __mmdrop_delayed(struct rcu_head *rhp)
  13845. +{
  13846. + struct mm_struct *mm = container_of(rhp, struct mm_struct, delayed_drop);
  13847. +
  13848. + __mmdrop(mm);
  13849. +}
  13850. +#endif
  13851. +
  13852. /*
  13853. * Decrement the use count and release all resources for an mm.
  13854. */
  13855. @@ -1214,6 +1241,9 @@
  13856. */
  13857. static void posix_cpu_timers_init(struct task_struct *tsk)
  13858. {
  13859. +#ifdef CONFIG_PREEMPT_RT_BASE
  13860. + tsk->posix_timer_list = NULL;
  13861. +#endif
  13862. tsk->cputime_expires.prof_exp = 0;
  13863. tsk->cputime_expires.virt_exp = 0;
  13864. tsk->cputime_expires.sched_exp = 0;
  13865. @@ -1338,6 +1368,7 @@
  13866. spin_lock_init(&p->alloc_lock);
  13867. init_sigpending(&p->pending);
  13868. + p->sigqueue_cache = NULL;
  13869. p->utime = p->stime = p->gtime = 0;
  13870. p->utimescaled = p->stimescaled = 0;
  13871. @@ -1345,7 +1376,8 @@
  13872. p->prev_cputime.utime = p->prev_cputime.stime = 0;
  13873. #endif
  13874. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  13875. - seqlock_init(&p->vtime_seqlock);
  13876. + raw_spin_lock_init(&p->vtime_lock);
  13877. + seqcount_init(&p->vtime_seq);
  13878. p->vtime_snap = 0;
  13879. p->vtime_snap_whence = VTIME_SLEEPING;
  13880. #endif
  13881. @@ -1396,6 +1428,9 @@
  13882. p->hardirq_context = 0;
  13883. p->softirq_context = 0;
  13884. #endif
  13885. +
  13886. + p->pagefault_disabled = 0;
  13887. +
  13888. #ifdef CONFIG_LOCKDEP
  13889. p->lockdep_depth = 0; /* no locks held yet */
  13890. p->curr_chain_key = 0;
  13891. diff -Nur linux-4.1.20.orig/kernel/futex.c linux-4.1.20/kernel/futex.c
  13892. --- linux-4.1.20.orig/kernel/futex.c 2016-03-17 19:11:03.000000000 +0100
  13893. +++ linux-4.1.20/kernel/futex.c 2016-03-21 20:18:33.000000000 +0100
  13894. @@ -738,7 +738,9 @@
  13895. * task still owns the PI-state:
  13896. */
  13897. if (head->next != next) {
  13898. + raw_spin_unlock_irq(&curr->pi_lock);
  13899. spin_unlock(&hb->lock);
  13900. + raw_spin_lock_irq(&curr->pi_lock);
  13901. continue;
  13902. }
  13903. @@ -1090,9 +1092,11 @@
  13904. /*
  13905. * The hash bucket lock must be held when this is called.
  13906. - * Afterwards, the futex_q must not be accessed.
  13907. + * Afterwards, the futex_q must not be accessed. Callers
  13908. + * must ensure to later call wake_up_q() for the actual
  13909. + * wakeups to occur.
  13910. */
  13911. -static void wake_futex(struct futex_q *q)
  13912. +static void mark_wake_futex(struct wake_q_head *wake_q, struct futex_q *q)
  13913. {
  13914. struct task_struct *p = q->task;
  13915. @@ -1100,14 +1104,10 @@
  13916. return;
  13917. /*
  13918. - * We set q->lock_ptr = NULL _before_ we wake up the task. If
  13919. - * a non-futex wake up happens on another CPU then the task
  13920. - * might exit and p would dereference a non-existing task
  13921. - * struct. Prevent this by holding a reference on p across the
  13922. - * wake up.
  13923. + * Queue the task for later wakeup for after we've released
  13924. + * the hb->lock. wake_q_add() grabs reference to p.
  13925. */
  13926. - get_task_struct(p);
  13927. -
  13928. + wake_q_add(wake_q, p);
  13929. __unqueue_futex(q);
  13930. /*
  13931. * The waiting task can free the futex_q as soon as
  13932. @@ -1117,16 +1117,15 @@
  13933. */
  13934. smp_wmb();
  13935. q->lock_ptr = NULL;
  13936. -
  13937. - wake_up_state(p, TASK_NORMAL);
  13938. - put_task_struct(p);
  13939. }
  13940. -static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this)
  13941. +static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this,
  13942. + struct futex_hash_bucket *hb)
  13943. {
  13944. struct task_struct *new_owner;
  13945. struct futex_pi_state *pi_state = this->pi_state;
  13946. u32 uninitialized_var(curval), newval;
  13947. + bool deboost;
  13948. int ret = 0;
  13949. if (!pi_state)
  13950. @@ -1178,7 +1177,17 @@
  13951. raw_spin_unlock_irq(&new_owner->pi_lock);
  13952. raw_spin_unlock(&pi_state->pi_mutex.wait_lock);
  13953. - rt_mutex_unlock(&pi_state->pi_mutex);
  13954. +
  13955. + deboost = rt_mutex_futex_unlock(&pi_state->pi_mutex);
  13956. +
  13957. + /*
  13958. + * We deboost after dropping hb->lock. That prevents a double
  13959. + * wakeup on RT.
  13960. + */
  13961. + spin_unlock(&hb->lock);
  13962. +
  13963. + if (deboost)
  13964. + rt_mutex_adjust_prio(current);
  13965. return 0;
  13966. }
  13967. @@ -1217,6 +1226,7 @@
  13968. struct futex_q *this, *next;
  13969. union futex_key key = FUTEX_KEY_INIT;
  13970. int ret;
  13971. + WAKE_Q(wake_q);
  13972. if (!bitset)
  13973. return -EINVAL;
  13974. @@ -1244,13 +1254,14 @@
  13975. if (!(this->bitset & bitset))
  13976. continue;
  13977. - wake_futex(this);
  13978. + mark_wake_futex(&wake_q, this);
  13979. if (++ret >= nr_wake)
  13980. break;
  13981. }
  13982. }
  13983. spin_unlock(&hb->lock);
  13984. + wake_up_q(&wake_q);
  13985. out_put_key:
  13986. put_futex_key(&key);
  13987. out:
  13988. @@ -1269,6 +1280,7 @@
  13989. struct futex_hash_bucket *hb1, *hb2;
  13990. struct futex_q *this, *next;
  13991. int ret, op_ret;
  13992. + WAKE_Q(wake_q);
  13993. retry:
  13994. ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, VERIFY_READ);
  13995. @@ -1320,7 +1332,7 @@
  13996. ret = -EINVAL;
  13997. goto out_unlock;
  13998. }
  13999. - wake_futex(this);
  14000. + mark_wake_futex(&wake_q, this);
  14001. if (++ret >= nr_wake)
  14002. break;
  14003. }
  14004. @@ -1334,7 +1346,7 @@
  14005. ret = -EINVAL;
  14006. goto out_unlock;
  14007. }
  14008. - wake_futex(this);
  14009. + mark_wake_futex(&wake_q, this);
  14010. if (++op_ret >= nr_wake2)
  14011. break;
  14012. }
  14013. @@ -1344,6 +1356,7 @@
  14014. out_unlock:
  14015. double_unlock_hb(hb1, hb2);
  14016. + wake_up_q(&wake_q);
  14017. out_put_keys:
  14018. put_futex_key(&key2);
  14019. out_put_key1:
  14020. @@ -1503,6 +1516,7 @@
  14021. struct futex_pi_state *pi_state = NULL;
  14022. struct futex_hash_bucket *hb1, *hb2;
  14023. struct futex_q *this, *next;
  14024. + WAKE_Q(wake_q);
  14025. if (requeue_pi) {
  14026. /*
  14027. @@ -1679,7 +1693,7 @@
  14028. * woken by futex_unlock_pi().
  14029. */
  14030. if (++task_count <= nr_wake && !requeue_pi) {
  14031. - wake_futex(this);
  14032. + mark_wake_futex(&wake_q, this);
  14033. continue;
  14034. }
  14035. @@ -1705,6 +1719,16 @@
  14036. requeue_pi_wake_futex(this, &key2, hb2);
  14037. drop_count++;
  14038. continue;
  14039. + } else if (ret == -EAGAIN) {
  14040. + /*
  14041. + * Waiter was woken by timeout or
  14042. + * signal and has set pi_blocked_on to
  14043. + * PI_WAKEUP_INPROGRESS before we
  14044. + * tried to enqueue it on the rtmutex.
  14045. + */
  14046. + this->pi_state = NULL;
  14047. + free_pi_state(pi_state);
  14048. + continue;
  14049. } else if (ret) {
  14050. /* -EDEADLK */
  14051. this->pi_state = NULL;
  14052. @@ -1719,6 +1743,7 @@
  14053. out_unlock:
  14054. free_pi_state(pi_state);
  14055. double_unlock_hb(hb1, hb2);
  14056. + wake_up_q(&wake_q);
  14057. hb_waiters_dec(hb2);
  14058. /*
  14059. @@ -2412,13 +2437,26 @@
  14060. */
  14061. match = futex_top_waiter(hb, &key);
  14062. if (match) {
  14063. - ret = wake_futex_pi(uaddr, uval, match);
  14064. + ret = wake_futex_pi(uaddr, uval, match, hb);
  14065. +
  14066. + /*
  14067. + * In case of success wake_futex_pi dropped the hash
  14068. + * bucket lock.
  14069. + */
  14070. + if (!ret)
  14071. + goto out_putkey;
  14072. +
  14073. /*
  14074. * The atomic access to the futex value generated a
  14075. * pagefault, so retry the user-access and the wakeup:
  14076. */
  14077. if (ret == -EFAULT)
  14078. goto pi_faulted;
  14079. +
  14080. + /*
  14081. + * wake_futex_pi has detected invalid state. Tell user
  14082. + * space.
  14083. + */
  14084. goto out_unlock;
  14085. }
  14086. @@ -2439,6 +2477,7 @@
  14087. out_unlock:
  14088. spin_unlock(&hb->lock);
  14089. +out_putkey:
  14090. put_futex_key(&key);
  14091. return ret;
  14092. @@ -2549,7 +2588,7 @@
  14093. struct hrtimer_sleeper timeout, *to = NULL;
  14094. struct rt_mutex_waiter rt_waiter;
  14095. struct rt_mutex *pi_mutex = NULL;
  14096. - struct futex_hash_bucket *hb;
  14097. + struct futex_hash_bucket *hb, *hb2;
  14098. union futex_key key2 = FUTEX_KEY_INIT;
  14099. struct futex_q q = futex_q_init;
  14100. int res, ret;
  14101. @@ -2574,10 +2613,7 @@
  14102. * The waiter is allocated on our stack, manipulated by the requeue
  14103. * code while we sleep on uaddr.
  14104. */
  14105. - debug_rt_mutex_init_waiter(&rt_waiter);
  14106. - RB_CLEAR_NODE(&rt_waiter.pi_tree_entry);
  14107. - RB_CLEAR_NODE(&rt_waiter.tree_entry);
  14108. - rt_waiter.task = NULL;
  14109. + rt_mutex_init_waiter(&rt_waiter, false);
  14110. ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE);
  14111. if (unlikely(ret != 0))
  14112. @@ -2608,20 +2644,55 @@
  14113. /* Queue the futex_q, drop the hb lock, wait for wakeup. */
  14114. futex_wait_queue_me(hb, &q, to);
  14115. - spin_lock(&hb->lock);
  14116. - ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
  14117. - spin_unlock(&hb->lock);
  14118. - if (ret)
  14119. - goto out_put_keys;
  14120. + /*
  14121. + * On RT we must avoid races with requeue and trying to block
  14122. + * on two mutexes (hb->lock and uaddr2's rtmutex) by
  14123. + * serializing access to pi_blocked_on with pi_lock.
  14124. + */
  14125. + raw_spin_lock_irq(&current->pi_lock);
  14126. + if (current->pi_blocked_on) {
  14127. + /*
  14128. + * We have been requeued or are in the process of
  14129. + * being requeued.
  14130. + */
  14131. + raw_spin_unlock_irq(&current->pi_lock);
  14132. + } else {
  14133. + /*
  14134. + * Setting pi_blocked_on to PI_WAKEUP_INPROGRESS
  14135. + * prevents a concurrent requeue from moving us to the
  14136. + * uaddr2 rtmutex. After that we can safely acquire
  14137. + * (and possibly block on) hb->lock.
  14138. + */
  14139. + current->pi_blocked_on = PI_WAKEUP_INPROGRESS;
  14140. + raw_spin_unlock_irq(&current->pi_lock);
  14141. +
  14142. + spin_lock(&hb->lock);
  14143. +
  14144. + /*
  14145. + * Clean up pi_blocked_on. We might leak it otherwise
  14146. + * when we succeeded with the hb->lock in the fast
  14147. + * path.
  14148. + */
  14149. + raw_spin_lock_irq(&current->pi_lock);
  14150. + current->pi_blocked_on = NULL;
  14151. + raw_spin_unlock_irq(&current->pi_lock);
  14152. +
  14153. + ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
  14154. + spin_unlock(&hb->lock);
  14155. + if (ret)
  14156. + goto out_put_keys;
  14157. + }
  14158. /*
  14159. - * In order for us to be here, we know our q.key == key2, and since
  14160. - * we took the hb->lock above, we also know that futex_requeue() has
  14161. - * completed and we no longer have to concern ourselves with a wakeup
  14162. - * race with the atomic proxy lock acquisition by the requeue code. The
  14163. - * futex_requeue dropped our key1 reference and incremented our key2
  14164. - * reference count.
  14165. + * In order to be here, we have either been requeued, are in
  14166. + * the process of being requeued, or requeue successfully
  14167. + * acquired uaddr2 on our behalf. If pi_blocked_on was
  14168. + * non-null above, we may be racing with a requeue. Do not
  14169. + * rely on q->lock_ptr to be hb2->lock until after blocking on
  14170. + * hb->lock or hb2->lock. The futex_requeue dropped our key1
  14171. + * reference and incremented our key2 reference count.
  14172. */
  14173. + hb2 = hash_futex(&key2);
  14174. /* Check if the requeue code acquired the second futex for us. */
  14175. if (!q.rt_waiter) {
  14176. @@ -2630,14 +2701,15 @@
  14177. * did a lock-steal - fix up the PI-state in that case.
  14178. */
  14179. if (q.pi_state && (q.pi_state->owner != current)) {
  14180. - spin_lock(q.lock_ptr);
  14181. + spin_lock(&hb2->lock);
  14182. + BUG_ON(&hb2->lock != q.lock_ptr);
  14183. ret = fixup_pi_state_owner(uaddr2, &q, current);
  14184. /*
  14185. * Drop the reference to the pi state which
  14186. * the requeue_pi() code acquired for us.
  14187. */
  14188. free_pi_state(q.pi_state);
  14189. - spin_unlock(q.lock_ptr);
  14190. + spin_unlock(&hb2->lock);
  14191. }
  14192. } else {
  14193. /*
  14194. @@ -2650,7 +2722,8 @@
  14195. ret = rt_mutex_finish_proxy_lock(pi_mutex, to, &rt_waiter);
  14196. debug_rt_mutex_free_waiter(&rt_waiter);
  14197. - spin_lock(q.lock_ptr);
  14198. + spin_lock(&hb2->lock);
  14199. + BUG_ON(&hb2->lock != q.lock_ptr);
  14200. /*
  14201. * Fixup the pi_state owner and possibly acquire the lock if we
  14202. * haven't already.
  14203. diff -Nur linux-4.1.20.orig/kernel/irq/handle.c linux-4.1.20/kernel/irq/handle.c
  14204. --- linux-4.1.20.orig/kernel/irq/handle.c 2016-03-17 19:11:03.000000000 +0100
  14205. +++ linux-4.1.20/kernel/irq/handle.c 2016-03-21 20:18:33.000000000 +0100
  14206. @@ -133,6 +133,8 @@
  14207. irqreturn_t
  14208. handle_irq_event_percpu(struct irq_desc *desc, struct irqaction *action)
  14209. {
  14210. + struct pt_regs *regs = get_irq_regs();
  14211. + u64 ip = regs ? instruction_pointer(regs) : 0;
  14212. irqreturn_t retval = IRQ_NONE;
  14213. unsigned int flags = 0, irq = desc->irq_data.irq;
  14214. @@ -173,7 +175,11 @@
  14215. action = action->next;
  14216. } while (action);
  14217. - add_interrupt_randomness(irq, flags);
  14218. +#ifndef CONFIG_PREEMPT_RT_FULL
  14219. + add_interrupt_randomness(irq, flags, ip);
  14220. +#else
  14221. + desc->random_ip = ip;
  14222. +#endif
  14223. if (!noirqdebug)
  14224. note_interrupt(irq, desc, retval);
  14225. diff -Nur linux-4.1.20.orig/kernel/irq/manage.c linux-4.1.20/kernel/irq/manage.c
  14226. --- linux-4.1.20.orig/kernel/irq/manage.c 2016-03-17 19:11:03.000000000 +0100
  14227. +++ linux-4.1.20/kernel/irq/manage.c 2016-03-21 20:18:33.000000000 +0100
  14228. @@ -22,6 +22,7 @@
  14229. #include "internals.h"
  14230. #ifdef CONFIG_IRQ_FORCED_THREADING
  14231. +# ifndef CONFIG_PREEMPT_RT_BASE
  14232. __read_mostly bool force_irqthreads;
  14233. static int __init setup_forced_irqthreads(char *arg)
  14234. @@ -30,6 +31,7 @@
  14235. return 0;
  14236. }
  14237. early_param("threadirqs", setup_forced_irqthreads);
  14238. +# endif
  14239. #endif
  14240. static void __synchronize_hardirq(struct irq_desc *desc)
  14241. @@ -179,6 +181,62 @@
  14242. irq_get_pending(struct cpumask *mask, struct irq_desc *desc) { }
  14243. #endif
  14244. +#ifdef CONFIG_PREEMPT_RT_FULL
  14245. +static void _irq_affinity_notify(struct irq_affinity_notify *notify);
  14246. +static struct task_struct *set_affinity_helper;
  14247. +static LIST_HEAD(affinity_list);
  14248. +static DEFINE_RAW_SPINLOCK(affinity_list_lock);
  14249. +
  14250. +static int set_affinity_thread(void *unused)
  14251. +{
  14252. + while (1) {
  14253. + struct irq_affinity_notify *notify;
  14254. + int empty;
  14255. +
  14256. + set_current_state(TASK_INTERRUPTIBLE);
  14257. +
  14258. + raw_spin_lock_irq(&affinity_list_lock);
  14259. + empty = list_empty(&affinity_list);
  14260. + raw_spin_unlock_irq(&affinity_list_lock);
  14261. +
  14262. + if (empty)
  14263. + schedule();
  14264. + if (kthread_should_stop())
  14265. + break;
  14266. + set_current_state(TASK_RUNNING);
  14267. +try_next:
  14268. + notify = NULL;
  14269. +
  14270. + raw_spin_lock_irq(&affinity_list_lock);
  14271. + if (!list_empty(&affinity_list)) {
  14272. + notify = list_first_entry(&affinity_list,
  14273. + struct irq_affinity_notify, list);
  14274. + list_del_init(&notify->list);
  14275. + }
  14276. + raw_spin_unlock_irq(&affinity_list_lock);
  14277. +
  14278. + if (!notify)
  14279. + continue;
  14280. + _irq_affinity_notify(notify);
  14281. + goto try_next;
  14282. + }
  14283. + return 0;
  14284. +}
  14285. +
  14286. +static void init_helper_thread(void)
  14287. +{
  14288. + if (set_affinity_helper)
  14289. + return;
  14290. + set_affinity_helper = kthread_run(set_affinity_thread, NULL,
  14291. + "affinity-cb");
  14292. + WARN_ON(IS_ERR(set_affinity_helper));
  14293. +}
  14294. +#else
  14295. +
  14296. +static inline void init_helper_thread(void) { }
  14297. +
  14298. +#endif
  14299. +
  14300. int irq_do_set_affinity(struct irq_data *data, const struct cpumask *mask,
  14301. bool force)
  14302. {
  14303. @@ -218,7 +276,17 @@
  14304. if (desc->affinity_notify) {
  14305. kref_get(&desc->affinity_notify->kref);
  14306. +
  14307. +#ifdef CONFIG_PREEMPT_RT_FULL
  14308. + raw_spin_lock(&affinity_list_lock);
  14309. + if (list_empty(&desc->affinity_notify->list))
  14310. + list_add_tail(&affinity_list,
  14311. + &desc->affinity_notify->list);
  14312. + raw_spin_unlock(&affinity_list_lock);
  14313. + wake_up_process(set_affinity_helper);
  14314. +#else
  14315. schedule_work(&desc->affinity_notify->work);
  14316. +#endif
  14317. }
  14318. irqd_set(data, IRQD_AFFINITY_SET);
  14319. @@ -256,10 +324,8 @@
  14320. }
  14321. EXPORT_SYMBOL_GPL(irq_set_affinity_hint);
  14322. -static void irq_affinity_notify(struct work_struct *work)
  14323. +static void _irq_affinity_notify(struct irq_affinity_notify *notify)
  14324. {
  14325. - struct irq_affinity_notify *notify =
  14326. - container_of(work, struct irq_affinity_notify, work);
  14327. struct irq_desc *desc = irq_to_desc(notify->irq);
  14328. cpumask_var_t cpumask;
  14329. unsigned long flags;
  14330. @@ -281,6 +347,13 @@
  14331. kref_put(&notify->kref, notify->release);
  14332. }
  14333. +static void irq_affinity_notify(struct work_struct *work)
  14334. +{
  14335. + struct irq_affinity_notify *notify =
  14336. + container_of(work, struct irq_affinity_notify, work);
  14337. + _irq_affinity_notify(notify);
  14338. +}
  14339. +
  14340. /**
  14341. * irq_set_affinity_notifier - control notification of IRQ affinity changes
  14342. * @irq: Interrupt for which to enable/disable notification
  14343. @@ -310,6 +383,8 @@
  14344. notify->irq = irq;
  14345. kref_init(&notify->kref);
  14346. INIT_WORK(&notify->work, irq_affinity_notify);
  14347. + INIT_LIST_HEAD(&notify->list);
  14348. + init_helper_thread();
  14349. }
  14350. raw_spin_lock_irqsave(&desc->lock, flags);
  14351. @@ -697,6 +772,12 @@
  14352. return IRQ_NONE;
  14353. }
  14354. +static irqreturn_t irq_forced_secondary_handler(int irq, void *dev_id)
  14355. +{
  14356. + WARN(1, "Secondary action handler called for irq %d\n", irq);
  14357. + return IRQ_NONE;
  14358. +}
  14359. +
  14360. static int irq_wait_for_interrupt(struct irqaction *action)
  14361. {
  14362. set_current_state(TASK_INTERRUPTIBLE);
  14363. @@ -723,7 +804,8 @@
  14364. static void irq_finalize_oneshot(struct irq_desc *desc,
  14365. struct irqaction *action)
  14366. {
  14367. - if (!(desc->istate & IRQS_ONESHOT))
  14368. + if (!(desc->istate & IRQS_ONESHOT) ||
  14369. + action->handler == irq_forced_secondary_handler)
  14370. return;
  14371. again:
  14372. chip_bus_lock(desc);
  14373. @@ -825,7 +907,15 @@
  14374. local_bh_disable();
  14375. ret = action->thread_fn(action->irq, action->dev_id);
  14376. irq_finalize_oneshot(desc, action);
  14377. - local_bh_enable();
  14378. + /*
  14379. + * Interrupts which have real time requirements can be set up
  14380. + * to avoid softirq processing in the thread handler. This is
  14381. + * safe as these interrupts do not raise soft interrupts.
  14382. + */
  14383. + if (irq_settings_no_softirq_call(desc))
  14384. + _local_bh_enable();
  14385. + else
  14386. + local_bh_enable();
  14387. return ret;
  14388. }
  14389. @@ -877,6 +967,18 @@
  14390. irq_finalize_oneshot(desc, action);
  14391. }
  14392. +static void irq_wake_secondary(struct irq_desc *desc, struct irqaction *action)
  14393. +{
  14394. + struct irqaction *secondary = action->secondary;
  14395. +
  14396. + if (WARN_ON_ONCE(!secondary))
  14397. + return;
  14398. +
  14399. + raw_spin_lock_irq(&desc->lock);
  14400. + __irq_wake_thread(desc, secondary);
  14401. + raw_spin_unlock_irq(&desc->lock);
  14402. +}
  14403. +
  14404. /*
  14405. * Interrupt handler thread
  14406. */
  14407. @@ -907,7 +1009,15 @@
  14408. action_ret = handler_fn(desc, action);
  14409. if (action_ret == IRQ_HANDLED)
  14410. atomic_inc(&desc->threads_handled);
  14411. + if (action_ret == IRQ_WAKE_THREAD)
  14412. + irq_wake_secondary(desc, action);
  14413. +#ifdef CONFIG_PREEMPT_RT_FULL
  14414. + migrate_disable();
  14415. + add_interrupt_randomness(action->irq, 0,
  14416. + desc->random_ip ^ (unsigned long) action);
  14417. + migrate_enable();
  14418. +#endif
  14419. wake_threads_waitq(desc);
  14420. }
  14421. @@ -951,20 +1061,36 @@
  14422. }
  14423. EXPORT_SYMBOL_GPL(irq_wake_thread);
  14424. -static void irq_setup_forced_threading(struct irqaction *new)
  14425. +static int irq_setup_forced_threading(struct irqaction *new)
  14426. {
  14427. if (!force_irqthreads)
  14428. - return;
  14429. + return 0;
  14430. if (new->flags & (IRQF_NO_THREAD | IRQF_PERCPU | IRQF_ONESHOT))
  14431. - return;
  14432. + return 0;
  14433. new->flags |= IRQF_ONESHOT;
  14434. - if (!new->thread_fn) {
  14435. - set_bit(IRQTF_FORCED_THREAD, &new->thread_flags);
  14436. - new->thread_fn = new->handler;
  14437. - new->handler = irq_default_primary_handler;
  14438. - }
  14439. + /*
  14440. + * Handle the case where we have a real primary handler and a
  14441. + * thread handler. We force thread them as well by creating a
  14442. + * secondary action.
  14443. + */
  14444. + if (new->handler != irq_default_primary_handler && new->thread_fn) {
  14445. + /* Allocate the secondary action */
  14446. + new->secondary = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
  14447. + if (!new->secondary)
  14448. + return -ENOMEM;
  14449. + new->secondary->handler = irq_forced_secondary_handler;
  14450. + new->secondary->thread_fn = new->thread_fn;
  14451. + new->secondary->dev_id = new->dev_id;
  14452. + new->secondary->irq = new->irq;
  14453. + new->secondary->name = new->name;
  14454. + }
  14455. + /* Deal with the primary handler */
  14456. + set_bit(IRQTF_FORCED_THREAD, &new->thread_flags);
  14457. + new->thread_fn = new->handler;
  14458. + new->handler = irq_default_primary_handler;
  14459. + return 0;
  14460. }
  14461. static int irq_request_resources(struct irq_desc *desc)
  14462. @@ -984,6 +1110,48 @@
  14463. c->irq_release_resources(d);
  14464. }
  14465. +static int
  14466. +setup_irq_thread(struct irqaction *new, unsigned int irq, bool secondary)
  14467. +{
  14468. + struct task_struct *t;
  14469. + struct sched_param param = {
  14470. + .sched_priority = MAX_USER_RT_PRIO/2,
  14471. + };
  14472. +
  14473. + if (!secondary) {
  14474. + t = kthread_create(irq_thread, new, "irq/%d-%s", irq,
  14475. + new->name);
  14476. + } else {
  14477. + t = kthread_create(irq_thread, new, "irq/%d-s-%s", irq,
  14478. + new->name);
  14479. + param.sched_priority += 1;
  14480. + }
  14481. +
  14482. + if (IS_ERR(t))
  14483. + return PTR_ERR(t);
  14484. +
  14485. + sched_setscheduler_nocheck(t, SCHED_FIFO, &param);
  14486. +
  14487. + /*
  14488. + * We keep the reference to the task struct even if
  14489. + * the thread dies to avoid that the interrupt code
  14490. + * references an already freed task_struct.
  14491. + */
  14492. + get_task_struct(t);
  14493. + new->thread = t;
  14494. + /*
  14495. + * Tell the thread to set its affinity. This is
  14496. + * important for shared interrupt handlers as we do
  14497. + * not invoke setup_affinity() for the secondary
  14498. + * handlers as everything is already set up. Even for
  14499. + * interrupts marked with IRQF_NO_BALANCE this is
  14500. + * correct as we want the thread to move to the cpu(s)
  14501. + * on which the requesting code placed the interrupt.
  14502. + */
  14503. + set_bit(IRQTF_AFFINITY, &new->thread_flags);
  14504. + return 0;
  14505. +}
  14506. +
  14507. /*
  14508. * Internal function to register an irqaction - typically used to
  14509. * allocate special interrupts that are part of the architecture.
  14510. @@ -1004,6 +1172,8 @@
  14511. if (!try_module_get(desc->owner))
  14512. return -ENODEV;
  14513. + new->irq = irq;
  14514. +
  14515. /*
  14516. * Check whether the interrupt nests into another interrupt
  14517. * thread.
  14518. @@ -1021,8 +1191,11 @@
  14519. */
  14520. new->handler = irq_nested_primary_handler;
  14521. } else {
  14522. - if (irq_settings_can_thread(desc))
  14523. - irq_setup_forced_threading(new);
  14524. + if (irq_settings_can_thread(desc)) {
  14525. + ret = irq_setup_forced_threading(new);
  14526. + if (ret)
  14527. + goto out_mput;
  14528. + }
  14529. }
  14530. /*
  14531. @@ -1031,37 +1204,14 @@
  14532. * thread.
  14533. */
  14534. if (new->thread_fn && !nested) {
  14535. - struct task_struct *t;
  14536. - static const struct sched_param param = {
  14537. - .sched_priority = MAX_USER_RT_PRIO/2,
  14538. - };
  14539. -
  14540. - t = kthread_create(irq_thread, new, "irq/%d-%s", irq,
  14541. - new->name);
  14542. - if (IS_ERR(t)) {
  14543. - ret = PTR_ERR(t);
  14544. + ret = setup_irq_thread(new, irq, false);
  14545. + if (ret)
  14546. goto out_mput;
  14547. + if (new->secondary) {
  14548. + ret = setup_irq_thread(new->secondary, irq, true);
  14549. + if (ret)
  14550. + goto out_thread;
  14551. }
  14552. -
  14553. - sched_setscheduler_nocheck(t, SCHED_FIFO, &param);
  14554. -
  14555. - /*
  14556. - * We keep the reference to the task struct even if
  14557. - * the thread dies to avoid that the interrupt code
  14558. - * references an already freed task_struct.
  14559. - */
  14560. - get_task_struct(t);
  14561. - new->thread = t;
  14562. - /*
  14563. - * Tell the thread to set its affinity. This is
  14564. - * important for shared interrupt handlers as we do
  14565. - * not invoke setup_affinity() for the secondary
  14566. - * handlers as everything is already set up. Even for
  14567. - * interrupts marked with IRQF_NO_BALANCE this is
  14568. - * correct as we want the thread to move to the cpu(s)
  14569. - * on which the requesting code placed the interrupt.
  14570. - */
  14571. - set_bit(IRQTF_AFFINITY, &new->thread_flags);
  14572. }
  14573. if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
  14574. @@ -1221,6 +1371,9 @@
  14575. irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
  14576. }
  14577. + if (new->flags & IRQF_NO_SOFTIRQ_CALL)
  14578. + irq_settings_set_no_softirq_call(desc);
  14579. +
  14580. /* Set default affinity mask once everything is setup */
  14581. setup_affinity(irq, desc, mask);
  14582. @@ -1234,7 +1387,6 @@
  14583. irq, nmsk, omsk);
  14584. }
  14585. - new->irq = irq;
  14586. *old_ptr = new;
  14587. irq_pm_install_action(desc, new);
  14588. @@ -1260,6 +1412,8 @@
  14589. */
  14590. if (new->thread)
  14591. wake_up_process(new->thread);
  14592. + if (new->secondary)
  14593. + wake_up_process(new->secondary->thread);
  14594. register_irq_proc(irq, desc);
  14595. new->dir = NULL;
  14596. @@ -1290,6 +1444,13 @@
  14597. kthread_stop(t);
  14598. put_task_struct(t);
  14599. }
  14600. + if (new->secondary && new->secondary->thread) {
  14601. + struct task_struct *t = new->secondary->thread;
  14602. +
  14603. + new->secondary->thread = NULL;
  14604. + kthread_stop(t);
  14605. + put_task_struct(t);
  14606. + }
  14607. out_mput:
  14608. module_put(desc->owner);
  14609. return ret;
  14610. @@ -1397,9 +1558,14 @@
  14611. if (action->thread) {
  14612. kthread_stop(action->thread);
  14613. put_task_struct(action->thread);
  14614. + if (action->secondary && action->secondary->thread) {
  14615. + kthread_stop(action->secondary->thread);
  14616. + put_task_struct(action->secondary->thread);
  14617. + }
  14618. }
  14619. module_put(desc->owner);
  14620. + kfree(action->secondary);
  14621. return action;
  14622. }
  14623. @@ -1543,8 +1709,10 @@
  14624. retval = __setup_irq(irq, desc, action);
  14625. chip_bus_sync_unlock(desc);
  14626. - if (retval)
  14627. + if (retval) {
  14628. + kfree(action->secondary);
  14629. kfree(action);
  14630. + }
  14631. #ifdef CONFIG_DEBUG_SHIRQ_FIXME
  14632. if (!retval && (irqflags & IRQF_SHARED)) {
  14633. diff -Nur linux-4.1.20.orig/kernel/irq/settings.h linux-4.1.20/kernel/irq/settings.h
  14634. --- linux-4.1.20.orig/kernel/irq/settings.h 2016-03-17 19:11:03.000000000 +0100
  14635. +++ linux-4.1.20/kernel/irq/settings.h 2016-03-21 20:18:33.000000000 +0100
  14636. @@ -15,6 +15,7 @@
  14637. _IRQ_NESTED_THREAD = IRQ_NESTED_THREAD,
  14638. _IRQ_PER_CPU_DEVID = IRQ_PER_CPU_DEVID,
  14639. _IRQ_IS_POLLED = IRQ_IS_POLLED,
  14640. + _IRQ_NO_SOFTIRQ_CALL = IRQ_NO_SOFTIRQ_CALL,
  14641. _IRQF_MODIFY_MASK = IRQF_MODIFY_MASK,
  14642. };
  14643. @@ -28,6 +29,7 @@
  14644. #define IRQ_NESTED_THREAD GOT_YOU_MORON
  14645. #define IRQ_PER_CPU_DEVID GOT_YOU_MORON
  14646. #define IRQ_IS_POLLED GOT_YOU_MORON
  14647. +#define IRQ_NO_SOFTIRQ_CALL GOT_YOU_MORON
  14648. #undef IRQF_MODIFY_MASK
  14649. #define IRQF_MODIFY_MASK GOT_YOU_MORON
  14650. @@ -38,6 +40,16 @@
  14651. desc->status_use_accessors |= (set & _IRQF_MODIFY_MASK);
  14652. }
  14653. +static inline bool irq_settings_no_softirq_call(struct irq_desc *desc)
  14654. +{
  14655. + return desc->status_use_accessors & _IRQ_NO_SOFTIRQ_CALL;
  14656. +}
  14657. +
  14658. +static inline void irq_settings_set_no_softirq_call(struct irq_desc *desc)
  14659. +{
  14660. + desc->status_use_accessors |= _IRQ_NO_SOFTIRQ_CALL;
  14661. +}
  14662. +
  14663. static inline bool irq_settings_is_per_cpu(struct irq_desc *desc)
  14664. {
  14665. return desc->status_use_accessors & _IRQ_PER_CPU;
  14666. diff -Nur linux-4.1.20.orig/kernel/irq/spurious.c linux-4.1.20/kernel/irq/spurious.c
  14667. --- linux-4.1.20.orig/kernel/irq/spurious.c 2016-03-17 19:11:03.000000000 +0100
  14668. +++ linux-4.1.20/kernel/irq/spurious.c 2016-03-21 20:18:33.000000000 +0100
  14669. @@ -444,6 +444,10 @@
  14670. static int __init irqfixup_setup(char *str)
  14671. {
  14672. +#ifdef CONFIG_PREEMPT_RT_BASE
  14673. + pr_warn("irqfixup boot option not supported w/ CONFIG_PREEMPT_RT_BASE\n");
  14674. + return 1;
  14675. +#endif
  14676. irqfixup = 1;
  14677. printk(KERN_WARNING "Misrouted IRQ fixup support enabled.\n");
  14678. printk(KERN_WARNING "This may impact system performance.\n");
  14679. @@ -456,6 +460,10 @@
  14680. static int __init irqpoll_setup(char *str)
  14681. {
  14682. +#ifdef CONFIG_PREEMPT_RT_BASE
  14683. + pr_warn("irqpoll boot option not supported w/ CONFIG_PREEMPT_RT_BASE\n");
  14684. + return 1;
  14685. +#endif
  14686. irqfixup = 2;
  14687. printk(KERN_WARNING "Misrouted IRQ fixup and polling support "
  14688. "enabled\n");
  14689. diff -Nur linux-4.1.20.orig/kernel/irq_work.c linux-4.1.20/kernel/irq_work.c
  14690. --- linux-4.1.20.orig/kernel/irq_work.c 2016-03-17 19:11:03.000000000 +0100
  14691. +++ linux-4.1.20/kernel/irq_work.c 2016-03-21 20:18:33.000000000 +0100
  14692. @@ -17,6 +17,7 @@
  14693. #include <linux/cpu.h>
  14694. #include <linux/notifier.h>
  14695. #include <linux/smp.h>
  14696. +#include <linux/interrupt.h>
  14697. #include <asm/processor.h>
  14698. @@ -65,6 +66,8 @@
  14699. */
  14700. bool irq_work_queue_on(struct irq_work *work, int cpu)
  14701. {
  14702. + struct llist_head *list;
  14703. +
  14704. /* All work should have been flushed before going offline */
  14705. WARN_ON_ONCE(cpu_is_offline(cpu));
  14706. @@ -75,7 +78,12 @@
  14707. if (!irq_work_claim(work))
  14708. return false;
  14709. - if (llist_add(&work->llnode, &per_cpu(raised_list, cpu)))
  14710. + if (IS_ENABLED(CONFIG_PREEMPT_RT_FULL) && !(work->flags & IRQ_WORK_HARD_IRQ))
  14711. + list = &per_cpu(lazy_list, cpu);
  14712. + else
  14713. + list = &per_cpu(raised_list, cpu);
  14714. +
  14715. + if (llist_add(&work->llnode, list))
  14716. arch_send_call_function_single_ipi(cpu);
  14717. return true;
  14718. @@ -86,6 +94,9 @@
  14719. /* Enqueue the irq work @work on the current CPU */
  14720. bool irq_work_queue(struct irq_work *work)
  14721. {
  14722. + struct llist_head *list;
  14723. + bool lazy_work, realtime = IS_ENABLED(CONFIG_PREEMPT_RT_FULL);
  14724. +
  14725. /* Only queue if not already pending */
  14726. if (!irq_work_claim(work))
  14727. return false;
  14728. @@ -93,13 +104,15 @@
  14729. /* Queue the entry and raise the IPI if needed. */
  14730. preempt_disable();
  14731. - /* If the work is "lazy", handle it from next tick if any */
  14732. - if (work->flags & IRQ_WORK_LAZY) {
  14733. - if (llist_add(&work->llnode, this_cpu_ptr(&lazy_list)) &&
  14734. - tick_nohz_tick_stopped())
  14735. - arch_irq_work_raise();
  14736. - } else {
  14737. - if (llist_add(&work->llnode, this_cpu_ptr(&raised_list)))
  14738. + lazy_work = work->flags & IRQ_WORK_LAZY;
  14739. +
  14740. + if (lazy_work || (realtime && !(work->flags & IRQ_WORK_HARD_IRQ)))
  14741. + list = this_cpu_ptr(&lazy_list);
  14742. + else
  14743. + list = this_cpu_ptr(&raised_list);
  14744. +
  14745. + if (llist_add(&work->llnode, list)) {
  14746. + if (!lazy_work || tick_nohz_tick_stopped())
  14747. arch_irq_work_raise();
  14748. }
  14749. @@ -116,9 +129,8 @@
  14750. raised = this_cpu_ptr(&raised_list);
  14751. lazy = this_cpu_ptr(&lazy_list);
  14752. - if (llist_empty(raised) || arch_irq_work_has_interrupt())
  14753. - if (llist_empty(lazy))
  14754. - return false;
  14755. + if (llist_empty(raised) && llist_empty(lazy))
  14756. + return false;
  14757. /* All work should have been flushed before going offline */
  14758. WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
  14759. @@ -132,7 +144,7 @@
  14760. struct irq_work *work;
  14761. struct llist_node *llnode;
  14762. - BUG_ON(!irqs_disabled());
  14763. + BUG_ON_NONRT(!irqs_disabled());
  14764. if (llist_empty(list))
  14765. return;
  14766. @@ -169,7 +181,16 @@
  14767. void irq_work_run(void)
  14768. {
  14769. irq_work_run_list(this_cpu_ptr(&raised_list));
  14770. - irq_work_run_list(this_cpu_ptr(&lazy_list));
  14771. + if (IS_ENABLED(CONFIG_PREEMPT_RT_FULL)) {
  14772. + /*
  14773. + * NOTE: we raise softirq via IPI for safety,
  14774. + * and execute in irq_work_tick() to move the
  14775. + * overhead from hard to soft irq context.
  14776. + */
  14777. + if (!llist_empty(this_cpu_ptr(&lazy_list)))
  14778. + raise_softirq(TIMER_SOFTIRQ);
  14779. + } else
  14780. + irq_work_run_list(this_cpu_ptr(&lazy_list));
  14781. }
  14782. EXPORT_SYMBOL_GPL(irq_work_run);
  14783. @@ -179,8 +200,17 @@
  14784. if (!llist_empty(raised) && !arch_irq_work_has_interrupt())
  14785. irq_work_run_list(raised);
  14786. +
  14787. + if (!IS_ENABLED(CONFIG_PREEMPT_RT_FULL))
  14788. + irq_work_run_list(this_cpu_ptr(&lazy_list));
  14789. +}
  14790. +
  14791. +#if defined(CONFIG_IRQ_WORK) && defined(CONFIG_PREEMPT_RT_FULL)
  14792. +void irq_work_tick_soft(void)
  14793. +{
  14794. irq_work_run_list(this_cpu_ptr(&lazy_list));
  14795. }
  14796. +#endif
  14797. /*
  14798. * Synchronize against the irq_work @entry, ensures the entry is not
  14799. diff -Nur linux-4.1.20.orig/kernel/ksysfs.c linux-4.1.20/kernel/ksysfs.c
  14800. --- linux-4.1.20.orig/kernel/ksysfs.c 2016-03-17 19:11:03.000000000 +0100
  14801. +++ linux-4.1.20/kernel/ksysfs.c 2016-03-21 20:18:33.000000000 +0100
  14802. @@ -136,6 +136,15 @@
  14803. #endif /* CONFIG_KEXEC */
  14804. +#if defined(CONFIG_PREEMPT_RT_FULL)
  14805. +static ssize_t realtime_show(struct kobject *kobj,
  14806. + struct kobj_attribute *attr, char *buf)
  14807. +{
  14808. + return sprintf(buf, "%d\n", 1);
  14809. +}
  14810. +KERNEL_ATTR_RO(realtime);
  14811. +#endif
  14812. +
  14813. /* whether file capabilities are enabled */
  14814. static ssize_t fscaps_show(struct kobject *kobj,
  14815. struct kobj_attribute *attr, char *buf)
  14816. @@ -203,6 +212,9 @@
  14817. &vmcoreinfo_attr.attr,
  14818. #endif
  14819. &rcu_expedited_attr.attr,
  14820. +#ifdef CONFIG_PREEMPT_RT_FULL
  14821. + &realtime_attr.attr,
  14822. +#endif
  14823. NULL
  14824. };
  14825. diff -Nur linux-4.1.20.orig/kernel/locking/Makefile linux-4.1.20/kernel/locking/Makefile
  14826. --- linux-4.1.20.orig/kernel/locking/Makefile 2016-03-17 19:11:03.000000000 +0100
  14827. +++ linux-4.1.20/kernel/locking/Makefile 2016-03-21 20:18:33.000000000 +0100
  14828. @@ -1,5 +1,5 @@
  14829. -obj-y += mutex.o semaphore.o rwsem.o
  14830. +obj-y += semaphore.o
  14831. ifdef CONFIG_FUNCTION_TRACER
  14832. CFLAGS_REMOVE_lockdep.o = $(CC_FLAGS_FTRACE)
  14833. @@ -8,7 +8,11 @@
  14834. CFLAGS_REMOVE_rtmutex-debug.o = $(CC_FLAGS_FTRACE)
  14835. endif
  14836. +ifneq ($(CONFIG_PREEMPT_RT_FULL),y)
  14837. +obj-y += mutex.o
  14838. obj-$(CONFIG_DEBUG_MUTEXES) += mutex-debug.o
  14839. +obj-y += rwsem.o
  14840. +endif
  14841. obj-$(CONFIG_LOCKDEP) += lockdep.o
  14842. ifeq ($(CONFIG_PROC_FS),y)
  14843. obj-$(CONFIG_LOCKDEP) += lockdep_proc.o
  14844. @@ -22,8 +26,11 @@
  14845. obj-$(CONFIG_RT_MUTEX_TESTER) += rtmutex-tester.o
  14846. obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock.o
  14847. obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock_debug.o
  14848. +ifneq ($(CONFIG_PREEMPT_RT_FULL),y)
  14849. obj-$(CONFIG_RWSEM_GENERIC_SPINLOCK) += rwsem-spinlock.o
  14850. obj-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem-xadd.o
  14851. +endif
  14852. obj-$(CONFIG_PERCPU_RWSEM) += percpu-rwsem.o
  14853. +obj-$(CONFIG_PREEMPT_RT_FULL) += rt.o
  14854. obj-$(CONFIG_QUEUE_RWLOCK) += qrwlock.o
  14855. obj-$(CONFIG_LOCK_TORTURE_TEST) += locktorture.o
  14856. diff -Nur linux-4.1.20.orig/kernel/locking/lglock.c linux-4.1.20/kernel/locking/lglock.c
  14857. --- linux-4.1.20.orig/kernel/locking/lglock.c 2016-03-17 19:11:03.000000000 +0100
  14858. +++ linux-4.1.20/kernel/locking/lglock.c 2016-03-21 20:18:33.000000000 +0100
  14859. @@ -4,6 +4,15 @@
  14860. #include <linux/cpu.h>
  14861. #include <linux/string.h>
  14862. +#ifndef CONFIG_PREEMPT_RT_FULL
  14863. +# define lg_lock_ptr arch_spinlock_t
  14864. +# define lg_do_lock(l) arch_spin_lock(l)
  14865. +# define lg_do_unlock(l) arch_spin_unlock(l)
  14866. +#else
  14867. +# define lg_lock_ptr struct rt_mutex
  14868. +# define lg_do_lock(l) __rt_spin_lock(l)
  14869. +# define lg_do_unlock(l) __rt_spin_unlock(l)
  14870. +#endif
  14871. /*
  14872. * Note there is no uninit, so lglocks cannot be defined in
  14873. * modules (but it's fine to use them from there)
  14874. @@ -12,51 +21,60 @@
  14875. void lg_lock_init(struct lglock *lg, char *name)
  14876. {
  14877. +#ifdef CONFIG_PREEMPT_RT_FULL
  14878. + int i;
  14879. +
  14880. + for_each_possible_cpu(i) {
  14881. + struct rt_mutex *lock = per_cpu_ptr(lg->lock, i);
  14882. +
  14883. + rt_mutex_init(lock);
  14884. + }
  14885. +#endif
  14886. LOCKDEP_INIT_MAP(&lg->lock_dep_map, name, &lg->lock_key, 0);
  14887. }
  14888. EXPORT_SYMBOL(lg_lock_init);
  14889. void lg_local_lock(struct lglock *lg)
  14890. {
  14891. - arch_spinlock_t *lock;
  14892. + lg_lock_ptr *lock;
  14893. - preempt_disable();
  14894. + migrate_disable();
  14895. lock_acquire_shared(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  14896. lock = this_cpu_ptr(lg->lock);
  14897. - arch_spin_lock(lock);
  14898. + lg_do_lock(lock);
  14899. }
  14900. EXPORT_SYMBOL(lg_local_lock);
  14901. void lg_local_unlock(struct lglock *lg)
  14902. {
  14903. - arch_spinlock_t *lock;
  14904. + lg_lock_ptr *lock;
  14905. lock_release(&lg->lock_dep_map, 1, _RET_IP_);
  14906. lock = this_cpu_ptr(lg->lock);
  14907. - arch_spin_unlock(lock);
  14908. - preempt_enable();
  14909. + lg_do_unlock(lock);
  14910. + migrate_enable();
  14911. }
  14912. EXPORT_SYMBOL(lg_local_unlock);
  14913. void lg_local_lock_cpu(struct lglock *lg, int cpu)
  14914. {
  14915. - arch_spinlock_t *lock;
  14916. + lg_lock_ptr *lock;
  14917. - preempt_disable();
  14918. + preempt_disable_nort();
  14919. lock_acquire_shared(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  14920. lock = per_cpu_ptr(lg->lock, cpu);
  14921. - arch_spin_lock(lock);
  14922. + lg_do_lock(lock);
  14923. }
  14924. EXPORT_SYMBOL(lg_local_lock_cpu);
  14925. void lg_local_unlock_cpu(struct lglock *lg, int cpu)
  14926. {
  14927. - arch_spinlock_t *lock;
  14928. + lg_lock_ptr *lock;
  14929. lock_release(&lg->lock_dep_map, 1, _RET_IP_);
  14930. lock = per_cpu_ptr(lg->lock, cpu);
  14931. - arch_spin_unlock(lock);
  14932. - preempt_enable();
  14933. + lg_do_unlock(lock);
  14934. + preempt_enable_nort();
  14935. }
  14936. EXPORT_SYMBOL(lg_local_unlock_cpu);
  14937. @@ -64,12 +82,12 @@
  14938. {
  14939. int i;
  14940. - preempt_disable();
  14941. + preempt_disable_nort();
  14942. lock_acquire_exclusive(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  14943. for_each_possible_cpu(i) {
  14944. - arch_spinlock_t *lock;
  14945. + lg_lock_ptr *lock;
  14946. lock = per_cpu_ptr(lg->lock, i);
  14947. - arch_spin_lock(lock);
  14948. + lg_do_lock(lock);
  14949. }
  14950. }
  14951. EXPORT_SYMBOL(lg_global_lock);
  14952. @@ -80,10 +98,35 @@
  14953. lock_release(&lg->lock_dep_map, 1, _RET_IP_);
  14954. for_each_possible_cpu(i) {
  14955. - arch_spinlock_t *lock;
  14956. + lg_lock_ptr *lock;
  14957. lock = per_cpu_ptr(lg->lock, i);
  14958. - arch_spin_unlock(lock);
  14959. + lg_do_unlock(lock);
  14960. }
  14961. - preempt_enable();
  14962. + preempt_enable_nort();
  14963. }
  14964. EXPORT_SYMBOL(lg_global_unlock);
  14965. +
  14966. +#ifdef CONFIG_PREEMPT_RT_FULL
  14967. +/*
  14968. + * HACK: If you use this, you get to keep the pieces.
  14969. + * Used in queue_stop_cpus_work() when stop machinery
  14970. + * is called from inactive CPU, so we can't schedule.
  14971. + */
  14972. +# define lg_do_trylock_relax(l) \
  14973. + do { \
  14974. + while (!__rt_spin_trylock(l)) \
  14975. + cpu_relax(); \
  14976. + } while (0)
  14977. +
  14978. +void lg_global_trylock_relax(struct lglock *lg)
  14979. +{
  14980. + int i;
  14981. +
  14982. + lock_acquire_exclusive(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  14983. + for_each_possible_cpu(i) {
  14984. + lg_lock_ptr *lock;
  14985. + lock = per_cpu_ptr(lg->lock, i);
  14986. + lg_do_trylock_relax(lock);
  14987. + }
  14988. +}
  14989. +#endif
  14990. diff -Nur linux-4.1.20.orig/kernel/locking/lockdep.c linux-4.1.20/kernel/locking/lockdep.c
  14991. --- linux-4.1.20.orig/kernel/locking/lockdep.c 2016-03-17 19:11:03.000000000 +0100
  14992. +++ linux-4.1.20/kernel/locking/lockdep.c 2016-03-21 20:18:33.000000000 +0100
  14993. @@ -3563,6 +3563,7 @@
  14994. }
  14995. }
  14996. +#ifndef CONFIG_PREEMPT_RT_FULL
  14997. /*
  14998. * We dont accurately track softirq state in e.g.
  14999. * hardirq contexts (such as on 4KSTACKS), so only
  15000. @@ -3577,6 +3578,7 @@
  15001. DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
  15002. }
  15003. }
  15004. +#endif
  15005. if (!debug_locks)
  15006. print_irqtrace_events(current);
  15007. diff -Nur linux-4.1.20.orig/kernel/locking/locktorture.c linux-4.1.20/kernel/locking/locktorture.c
  15008. --- linux-4.1.20.orig/kernel/locking/locktorture.c 2016-03-17 19:11:03.000000000 +0100
  15009. +++ linux-4.1.20/kernel/locking/locktorture.c 2016-03-21 20:18:33.000000000 +0100
  15010. @@ -24,7 +24,6 @@
  15011. #include <linux/module.h>
  15012. #include <linux/kthread.h>
  15013. #include <linux/spinlock.h>
  15014. -#include <linux/rwlock.h>
  15015. #include <linux/mutex.h>
  15016. #include <linux/rwsem.h>
  15017. #include <linux/smp.h>
  15018. diff -Nur linux-4.1.20.orig/kernel/locking/rt.c linux-4.1.20/kernel/locking/rt.c
  15019. --- linux-4.1.20.orig/kernel/locking/rt.c 1970-01-01 01:00:00.000000000 +0100
  15020. +++ linux-4.1.20/kernel/locking/rt.c 2016-03-21 20:18:33.000000000 +0100
  15021. @@ -0,0 +1,461 @@
  15022. +/*
  15023. + * kernel/rt.c
  15024. + *
  15025. + * Real-Time Preemption Support
  15026. + *
  15027. + * started by Ingo Molnar:
  15028. + *
  15029. + * Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  15030. + * Copyright (C) 2006, Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  15031. + *
  15032. + * historic credit for proving that Linux spinlocks can be implemented via
  15033. + * RT-aware mutexes goes to many people: The Pmutex project (Dirk Grambow
  15034. + * and others) who prototyped it on 2.4 and did lots of comparative
  15035. + * research and analysis; TimeSys, for proving that you can implement a
  15036. + * fully preemptible kernel via the use of IRQ threading and mutexes;
  15037. + * Bill Huey for persuasively arguing on lkml that the mutex model is the
  15038. + * right one; and to MontaVista, who ported pmutexes to 2.6.
  15039. + *
  15040. + * This code is a from-scratch implementation and is not based on pmutexes,
  15041. + * but the idea of converting spinlocks to mutexes is used here too.
  15042. + *
  15043. + * lock debugging, locking tree, deadlock detection:
  15044. + *
  15045. + * Copyright (C) 2004, LynuxWorks, Inc., Igor Manyilov, Bill Huey
  15046. + * Released under the General Public License (GPL).
  15047. + *
  15048. + * Includes portions of the generic R/W semaphore implementation from:
  15049. + *
  15050. + * Copyright (c) 2001 David Howells (dhowells@redhat.com).
  15051. + * - Derived partially from idea by Andrea Arcangeli <andrea@suse.de>
  15052. + * - Derived also from comments by Linus
  15053. + *
  15054. + * Pending ownership of locks and ownership stealing:
  15055. + *
  15056. + * Copyright (C) 2005, Kihon Technologies Inc., Steven Rostedt
  15057. + *
  15058. + * (also by Steven Rostedt)
  15059. + * - Converted single pi_lock to individual task locks.
  15060. + *
  15061. + * By Esben Nielsen:
  15062. + * Doing priority inheritance with help of the scheduler.
  15063. + *
  15064. + * Copyright (C) 2006, Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  15065. + * - major rework based on Esben Nielsens initial patch
  15066. + * - replaced thread_info references by task_struct refs
  15067. + * - removed task->pending_owner dependency
  15068. + * - BKL drop/reacquire for semaphore style locks to avoid deadlocks
  15069. + * in the scheduler return path as discussed with Steven Rostedt
  15070. + *
  15071. + * Copyright (C) 2006, Kihon Technologies Inc.
  15072. + * Steven Rostedt <rostedt@goodmis.org>
  15073. + * - debugged and patched Thomas Gleixner's rework.
  15074. + * - added back the cmpxchg to the rework.
  15075. + * - turned atomic require back on for SMP.
  15076. + */
  15077. +
  15078. +#include <linux/spinlock.h>
  15079. +#include <linux/rtmutex.h>
  15080. +#include <linux/sched.h>
  15081. +#include <linux/delay.h>
  15082. +#include <linux/module.h>
  15083. +#include <linux/kallsyms.h>
  15084. +#include <linux/syscalls.h>
  15085. +#include <linux/interrupt.h>
  15086. +#include <linux/plist.h>
  15087. +#include <linux/fs.h>
  15088. +#include <linux/futex.h>
  15089. +#include <linux/hrtimer.h>
  15090. +
  15091. +#include "rtmutex_common.h"
  15092. +
  15093. +/*
  15094. + * struct mutex functions
  15095. + */
  15096. +void __mutex_do_init(struct mutex *mutex, const char *name,
  15097. + struct lock_class_key *key)
  15098. +{
  15099. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  15100. + /*
  15101. + * Make sure we are not reinitializing a held lock:
  15102. + */
  15103. + debug_check_no_locks_freed((void *)mutex, sizeof(*mutex));
  15104. + lockdep_init_map(&mutex->dep_map, name, key, 0);
  15105. +#endif
  15106. + mutex->lock.save_state = 0;
  15107. +}
  15108. +EXPORT_SYMBOL(__mutex_do_init);
  15109. +
  15110. +void __lockfunc _mutex_lock(struct mutex *lock)
  15111. +{
  15112. + mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  15113. + rt_mutex_lock(&lock->lock);
  15114. +}
  15115. +EXPORT_SYMBOL(_mutex_lock);
  15116. +
  15117. +int __lockfunc _mutex_lock_interruptible(struct mutex *lock)
  15118. +{
  15119. + int ret;
  15120. +
  15121. + mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  15122. + ret = rt_mutex_lock_interruptible(&lock->lock);
  15123. + if (ret)
  15124. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  15125. + return ret;
  15126. +}
  15127. +EXPORT_SYMBOL(_mutex_lock_interruptible);
  15128. +
  15129. +int __lockfunc _mutex_lock_killable(struct mutex *lock)
  15130. +{
  15131. + int ret;
  15132. +
  15133. + mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  15134. + ret = rt_mutex_lock_killable(&lock->lock);
  15135. + if (ret)
  15136. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  15137. + return ret;
  15138. +}
  15139. +EXPORT_SYMBOL(_mutex_lock_killable);
  15140. +
  15141. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  15142. +void __lockfunc _mutex_lock_nested(struct mutex *lock, int subclass)
  15143. +{
  15144. + mutex_acquire_nest(&lock->dep_map, subclass, 0, NULL, _RET_IP_);
  15145. + rt_mutex_lock(&lock->lock);
  15146. +}
  15147. +EXPORT_SYMBOL(_mutex_lock_nested);
  15148. +
  15149. +void __lockfunc _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest)
  15150. +{
  15151. + mutex_acquire_nest(&lock->dep_map, 0, 0, nest, _RET_IP_);
  15152. + rt_mutex_lock(&lock->lock);
  15153. +}
  15154. +EXPORT_SYMBOL(_mutex_lock_nest_lock);
  15155. +
  15156. +int __lockfunc _mutex_lock_interruptible_nested(struct mutex *lock, int subclass)
  15157. +{
  15158. + int ret;
  15159. +
  15160. + mutex_acquire_nest(&lock->dep_map, subclass, 0, NULL, _RET_IP_);
  15161. + ret = rt_mutex_lock_interruptible(&lock->lock);
  15162. + if (ret)
  15163. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  15164. + return ret;
  15165. +}
  15166. +EXPORT_SYMBOL(_mutex_lock_interruptible_nested);
  15167. +
  15168. +int __lockfunc _mutex_lock_killable_nested(struct mutex *lock, int subclass)
  15169. +{
  15170. + int ret;
  15171. +
  15172. + mutex_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
  15173. + ret = rt_mutex_lock_killable(&lock->lock);
  15174. + if (ret)
  15175. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  15176. + return ret;
  15177. +}
  15178. +EXPORT_SYMBOL(_mutex_lock_killable_nested);
  15179. +#endif
  15180. +
  15181. +int __lockfunc _mutex_trylock(struct mutex *lock)
  15182. +{
  15183. + int ret = rt_mutex_trylock(&lock->lock);
  15184. +
  15185. + if (ret)
  15186. + mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  15187. +
  15188. + return ret;
  15189. +}
  15190. +EXPORT_SYMBOL(_mutex_trylock);
  15191. +
  15192. +void __lockfunc _mutex_unlock(struct mutex *lock)
  15193. +{
  15194. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  15195. + rt_mutex_unlock(&lock->lock);
  15196. +}
  15197. +EXPORT_SYMBOL(_mutex_unlock);
  15198. +
  15199. +/*
  15200. + * rwlock_t functions
  15201. + */
  15202. +int __lockfunc rt_write_trylock(rwlock_t *rwlock)
  15203. +{
  15204. + int ret;
  15205. +
  15206. + migrate_disable();
  15207. + ret = rt_mutex_trylock(&rwlock->lock);
  15208. + if (ret)
  15209. + rwlock_acquire(&rwlock->dep_map, 0, 1, _RET_IP_);
  15210. + else
  15211. + migrate_enable();
  15212. +
  15213. + return ret;
  15214. +}
  15215. +EXPORT_SYMBOL(rt_write_trylock);
  15216. +
  15217. +int __lockfunc rt_write_trylock_irqsave(rwlock_t *rwlock, unsigned long *flags)
  15218. +{
  15219. + int ret;
  15220. +
  15221. + *flags = 0;
  15222. + ret = rt_write_trylock(rwlock);
  15223. + return ret;
  15224. +}
  15225. +EXPORT_SYMBOL(rt_write_trylock_irqsave);
  15226. +
  15227. +int __lockfunc rt_read_trylock(rwlock_t *rwlock)
  15228. +{
  15229. + struct rt_mutex *lock = &rwlock->lock;
  15230. + int ret = 1;
  15231. +
  15232. + /*
  15233. + * recursive read locks succeed when current owns the lock,
  15234. + * but not when read_depth == 0 which means that the lock is
  15235. + * write locked.
  15236. + */
  15237. + if (rt_mutex_owner(lock) != current) {
  15238. + migrate_disable();
  15239. + ret = rt_mutex_trylock(lock);
  15240. + if (ret)
  15241. + rwlock_acquire(&rwlock->dep_map, 0, 1, _RET_IP_);
  15242. + else
  15243. + migrate_enable();
  15244. +
  15245. + } else if (!rwlock->read_depth) {
  15246. + ret = 0;
  15247. + }
  15248. +
  15249. + if (ret)
  15250. + rwlock->read_depth++;
  15251. +
  15252. + return ret;
  15253. +}
  15254. +EXPORT_SYMBOL(rt_read_trylock);
  15255. +
  15256. +void __lockfunc rt_write_lock(rwlock_t *rwlock)
  15257. +{
  15258. + rwlock_acquire(&rwlock->dep_map, 0, 0, _RET_IP_);
  15259. + migrate_disable();
  15260. + __rt_spin_lock(&rwlock->lock);
  15261. +}
  15262. +EXPORT_SYMBOL(rt_write_lock);
  15263. +
  15264. +void __lockfunc rt_read_lock(rwlock_t *rwlock)
  15265. +{
  15266. + struct rt_mutex *lock = &rwlock->lock;
  15267. +
  15268. +
  15269. + /*
  15270. + * recursive read locks succeed when current owns the lock
  15271. + */
  15272. + if (rt_mutex_owner(lock) != current) {
  15273. + migrate_disable();
  15274. + rwlock_acquire(&rwlock->dep_map, 0, 0, _RET_IP_);
  15275. + __rt_spin_lock(lock);
  15276. + }
  15277. + rwlock->read_depth++;
  15278. +}
  15279. +
  15280. +EXPORT_SYMBOL(rt_read_lock);
  15281. +
  15282. +void __lockfunc rt_write_unlock(rwlock_t *rwlock)
  15283. +{
  15284. + /* NOTE: we always pass in '1' for nested, for simplicity */
  15285. + rwlock_release(&rwlock->dep_map, 1, _RET_IP_);
  15286. + __rt_spin_unlock(&rwlock->lock);
  15287. + migrate_enable();
  15288. +}
  15289. +EXPORT_SYMBOL(rt_write_unlock);
  15290. +
  15291. +void __lockfunc rt_read_unlock(rwlock_t *rwlock)
  15292. +{
  15293. + /* Release the lock only when read_depth is down to 0 */
  15294. + if (--rwlock->read_depth == 0) {
  15295. + rwlock_release(&rwlock->dep_map, 1, _RET_IP_);
  15296. + __rt_spin_unlock(&rwlock->lock);
  15297. + migrate_enable();
  15298. + }
  15299. +}
  15300. +EXPORT_SYMBOL(rt_read_unlock);
  15301. +
  15302. +unsigned long __lockfunc rt_write_lock_irqsave(rwlock_t *rwlock)
  15303. +{
  15304. + rt_write_lock(rwlock);
  15305. +
  15306. + return 0;
  15307. +}
  15308. +EXPORT_SYMBOL(rt_write_lock_irqsave);
  15309. +
  15310. +unsigned long __lockfunc rt_read_lock_irqsave(rwlock_t *rwlock)
  15311. +{
  15312. + rt_read_lock(rwlock);
  15313. +
  15314. + return 0;
  15315. +}
  15316. +EXPORT_SYMBOL(rt_read_lock_irqsave);
  15317. +
  15318. +void __rt_rwlock_init(rwlock_t *rwlock, char *name, struct lock_class_key *key)
  15319. +{
  15320. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  15321. + /*
  15322. + * Make sure we are not reinitializing a held lock:
  15323. + */
  15324. + debug_check_no_locks_freed((void *)rwlock, sizeof(*rwlock));
  15325. + lockdep_init_map(&rwlock->dep_map, name, key, 0);
  15326. +#endif
  15327. + rwlock->lock.save_state = 1;
  15328. + rwlock->read_depth = 0;
  15329. +}
  15330. +EXPORT_SYMBOL(__rt_rwlock_init);
  15331. +
  15332. +/*
  15333. + * rw_semaphores
  15334. + */
  15335. +
  15336. +void rt_up_write(struct rw_semaphore *rwsem)
  15337. +{
  15338. + rwsem_release(&rwsem->dep_map, 1, _RET_IP_);
  15339. + rt_mutex_unlock(&rwsem->lock);
  15340. +}
  15341. +EXPORT_SYMBOL(rt_up_write);
  15342. +
  15343. +void __rt_up_read(struct rw_semaphore *rwsem)
  15344. +{
  15345. + if (--rwsem->read_depth == 0)
  15346. + rt_mutex_unlock(&rwsem->lock);
  15347. +}
  15348. +
  15349. +void rt_up_read(struct rw_semaphore *rwsem)
  15350. +{
  15351. + rwsem_release(&rwsem->dep_map, 1, _RET_IP_);
  15352. + __rt_up_read(rwsem);
  15353. +}
  15354. +EXPORT_SYMBOL(rt_up_read);
  15355. +
  15356. +/*
  15357. + * downgrade a write lock into a read lock
  15358. + * - just wake up any readers at the front of the queue
  15359. + */
  15360. +void rt_downgrade_write(struct rw_semaphore *rwsem)
  15361. +{
  15362. + BUG_ON(rt_mutex_owner(&rwsem->lock) != current);
  15363. + rwsem->read_depth = 1;
  15364. +}
  15365. +EXPORT_SYMBOL(rt_downgrade_write);
  15366. +
  15367. +int rt_down_write_trylock(struct rw_semaphore *rwsem)
  15368. +{
  15369. + int ret = rt_mutex_trylock(&rwsem->lock);
  15370. +
  15371. + if (ret)
  15372. + rwsem_acquire(&rwsem->dep_map, 0, 1, _RET_IP_);
  15373. + return ret;
  15374. +}
  15375. +EXPORT_SYMBOL(rt_down_write_trylock);
  15376. +
  15377. +void rt_down_write(struct rw_semaphore *rwsem)
  15378. +{
  15379. + rwsem_acquire(&rwsem->dep_map, 0, 0, _RET_IP_);
  15380. + rt_mutex_lock(&rwsem->lock);
  15381. +}
  15382. +EXPORT_SYMBOL(rt_down_write);
  15383. +
  15384. +void rt_down_write_nested(struct rw_semaphore *rwsem, int subclass)
  15385. +{
  15386. + rwsem_acquire(&rwsem->dep_map, subclass, 0, _RET_IP_);
  15387. + rt_mutex_lock(&rwsem->lock);
  15388. +}
  15389. +EXPORT_SYMBOL(rt_down_write_nested);
  15390. +
  15391. +void rt_down_write_nested_lock(struct rw_semaphore *rwsem,
  15392. + struct lockdep_map *nest)
  15393. +{
  15394. + rwsem_acquire_nest(&rwsem->dep_map, 0, 0, nest, _RET_IP_);
  15395. + rt_mutex_lock(&rwsem->lock);
  15396. +}
  15397. +EXPORT_SYMBOL(rt_down_write_nested_lock);
  15398. +
  15399. +int rt_down_read_trylock(struct rw_semaphore *rwsem)
  15400. +{
  15401. + struct rt_mutex *lock = &rwsem->lock;
  15402. + int ret = 1;
  15403. +
  15404. + /*
  15405. + * recursive read locks succeed when current owns the rwsem,
  15406. + * but not when read_depth == 0 which means that the rwsem is
  15407. + * write locked.
  15408. + */
  15409. + if (rt_mutex_owner(lock) != current)
  15410. + ret = rt_mutex_trylock(&rwsem->lock);
  15411. + else if (!rwsem->read_depth)
  15412. + ret = 0;
  15413. +
  15414. + if (ret) {
  15415. + rwsem->read_depth++;
  15416. + rwsem_acquire(&rwsem->dep_map, 0, 1, _RET_IP_);
  15417. + }
  15418. + return ret;
  15419. +}
  15420. +EXPORT_SYMBOL(rt_down_read_trylock);
  15421. +
  15422. +static void __rt_down_read(struct rw_semaphore *rwsem, int subclass)
  15423. +{
  15424. + struct rt_mutex *lock = &rwsem->lock;
  15425. +
  15426. + rwsem_acquire_read(&rwsem->dep_map, subclass, 0, _RET_IP_);
  15427. +
  15428. + if (rt_mutex_owner(lock) != current)
  15429. + rt_mutex_lock(&rwsem->lock);
  15430. + rwsem->read_depth++;
  15431. +}
  15432. +
  15433. +void rt_down_read(struct rw_semaphore *rwsem)
  15434. +{
  15435. + __rt_down_read(rwsem, 0);
  15436. +}
  15437. +EXPORT_SYMBOL(rt_down_read);
  15438. +
  15439. +void rt_down_read_nested(struct rw_semaphore *rwsem, int subclass)
  15440. +{
  15441. + __rt_down_read(rwsem, subclass);
  15442. +}
  15443. +EXPORT_SYMBOL(rt_down_read_nested);
  15444. +
  15445. +void __rt_rwsem_init(struct rw_semaphore *rwsem, const char *name,
  15446. + struct lock_class_key *key)
  15447. +{
  15448. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  15449. + /*
  15450. + * Make sure we are not reinitializing a held lock:
  15451. + */
  15452. + debug_check_no_locks_freed((void *)rwsem, sizeof(*rwsem));
  15453. + lockdep_init_map(&rwsem->dep_map, name, key, 0);
  15454. +#endif
  15455. + rwsem->read_depth = 0;
  15456. + rwsem->lock.save_state = 0;
  15457. +}
  15458. +EXPORT_SYMBOL(__rt_rwsem_init);
  15459. +
  15460. +/**
  15461. + * atomic_dec_and_mutex_lock - return holding mutex if we dec to 0
  15462. + * @cnt: the atomic which we are to dec
  15463. + * @lock: the mutex to return holding if we dec to 0
  15464. + *
  15465. + * return true and hold lock if we dec to 0, return false otherwise
  15466. + */
  15467. +int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock)
  15468. +{
  15469. + /* dec if we can't possibly hit 0 */
  15470. + if (atomic_add_unless(cnt, -1, 1))
  15471. + return 0;
  15472. + /* we might hit 0, so take the lock */
  15473. + mutex_lock(lock);
  15474. + if (!atomic_dec_and_test(cnt)) {
  15475. + /* when we actually did the dec, we didn't hit 0 */
  15476. + mutex_unlock(lock);
  15477. + return 0;
  15478. + }
  15479. + /* we hit 0, and we hold the lock */
  15480. + return 1;
  15481. +}
  15482. +EXPORT_SYMBOL(atomic_dec_and_mutex_lock);
  15483. diff -Nur linux-4.1.20.orig/kernel/locking/rtmutex.c linux-4.1.20/kernel/locking/rtmutex.c
  15484. --- linux-4.1.20.orig/kernel/locking/rtmutex.c 2016-03-17 19:11:03.000000000 +0100
  15485. +++ linux-4.1.20/kernel/locking/rtmutex.c 2016-03-21 20:18:33.000000000 +0100
  15486. @@ -7,6 +7,11 @@
  15487. * Copyright (C) 2005-2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  15488. * Copyright (C) 2005 Kihon Technologies Inc., Steven Rostedt
  15489. * Copyright (C) 2006 Esben Nielsen
  15490. + * Adaptive Spinlocks:
  15491. + * Copyright (C) 2008 Novell, Inc., Gregory Haskins, Sven Dietrich,
  15492. + * and Peter Morreale,
  15493. + * Adaptive Spinlocks simplification:
  15494. + * Copyright (C) 2008 Red Hat, Inc., Steven Rostedt <srostedt@redhat.com>
  15495. *
  15496. * See Documentation/locking/rt-mutex-design.txt for details.
  15497. */
  15498. @@ -16,6 +21,7 @@
  15499. #include <linux/sched/rt.h>
  15500. #include <linux/sched/deadline.h>
  15501. #include <linux/timer.h>
  15502. +#include <linux/ww_mutex.h>
  15503. #include "rtmutex_common.h"
  15504. @@ -69,6 +75,12 @@
  15505. clear_rt_mutex_waiters(lock);
  15506. }
  15507. +static int rt_mutex_real_waiter(struct rt_mutex_waiter *waiter)
  15508. +{
  15509. + return waiter && waiter != PI_WAKEUP_INPROGRESS &&
  15510. + waiter != PI_REQUEUE_INPROGRESS;
  15511. +}
  15512. +
  15513. /*
  15514. * We can speed up the acquire/release, if the architecture
  15515. * supports cmpxchg and if there's no debugging state to be set up
  15516. @@ -300,7 +312,7 @@
  15517. * of task. We do not use the spin_xx_mutex() variants here as we are
  15518. * outside of the debug path.)
  15519. */
  15520. -static void rt_mutex_adjust_prio(struct task_struct *task)
  15521. +void rt_mutex_adjust_prio(struct task_struct *task)
  15522. {
  15523. unsigned long flags;
  15524. @@ -335,6 +347,14 @@
  15525. return debug_rt_mutex_detect_deadlock(waiter, chwalk);
  15526. }
  15527. +static void rt_mutex_wake_waiter(struct rt_mutex_waiter *waiter)
  15528. +{
  15529. + if (waiter->savestate)
  15530. + wake_up_lock_sleeper(waiter->task);
  15531. + else
  15532. + wake_up_process(waiter->task);
  15533. +}
  15534. +
  15535. /*
  15536. * Max number of times we'll walk the boosting chain:
  15537. */
  15538. @@ -342,7 +362,8 @@
  15539. static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
  15540. {
  15541. - return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
  15542. + return rt_mutex_real_waiter(p->pi_blocked_on) ?
  15543. + p->pi_blocked_on->lock : NULL;
  15544. }
  15545. /*
  15546. @@ -479,7 +500,7 @@
  15547. * reached or the state of the chain has changed while we
  15548. * dropped the locks.
  15549. */
  15550. - if (!waiter)
  15551. + if (!rt_mutex_real_waiter(waiter))
  15552. goto out_unlock_pi;
  15553. /*
  15554. @@ -641,13 +662,16 @@
  15555. * follow here. This is the end of the chain we are walking.
  15556. */
  15557. if (!rt_mutex_owner(lock)) {
  15558. + struct rt_mutex_waiter *lock_top_waiter;
  15559. +
  15560. /*
  15561. * If the requeue [7] above changed the top waiter,
  15562. * then we need to wake the new top waiter up to try
  15563. * to get the lock.
  15564. */
  15565. - if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
  15566. - wake_up_process(rt_mutex_top_waiter(lock)->task);
  15567. + lock_top_waiter = rt_mutex_top_waiter(lock);
  15568. + if (prerequeue_top_waiter != lock_top_waiter)
  15569. + rt_mutex_wake_waiter(lock_top_waiter);
  15570. raw_spin_unlock(&lock->wait_lock);
  15571. return 0;
  15572. }
  15573. @@ -740,6 +764,25 @@
  15574. return ret;
  15575. }
  15576. +
  15577. +#define STEAL_NORMAL 0
  15578. +#define STEAL_LATERAL 1
  15579. +
  15580. +/*
  15581. + * Note that RT tasks are excluded from lateral-steals to prevent the
  15582. + * introduction of an unbounded latency
  15583. + */
  15584. +static inline int lock_is_stealable(struct task_struct *task,
  15585. + struct task_struct *pendowner, int mode)
  15586. +{
  15587. + if (mode == STEAL_NORMAL || rt_task(task)) {
  15588. + if (task->prio >= pendowner->prio)
  15589. + return 0;
  15590. + } else if (task->prio > pendowner->prio)
  15591. + return 0;
  15592. + return 1;
  15593. +}
  15594. +
  15595. /*
  15596. * Try to take an rt-mutex
  15597. *
  15598. @@ -750,8 +793,9 @@
  15599. * @waiter: The waiter that is queued to the lock's wait list if the
  15600. * callsite called task_blocked_on_lock(), otherwise NULL
  15601. */
  15602. -static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  15603. - struct rt_mutex_waiter *waiter)
  15604. +static int __try_to_take_rt_mutex(struct rt_mutex *lock,
  15605. + struct task_struct *task,
  15606. + struct rt_mutex_waiter *waiter, int mode)
  15607. {
  15608. unsigned long flags;
  15609. @@ -790,8 +834,10 @@
  15610. * If waiter is not the highest priority waiter of
  15611. * @lock, give up.
  15612. */
  15613. - if (waiter != rt_mutex_top_waiter(lock))
  15614. + if (waiter != rt_mutex_top_waiter(lock)) {
  15615. + /* XXX lock_is_stealable() ? */
  15616. return 0;
  15617. + }
  15618. /*
  15619. * We can acquire the lock. Remove the waiter from the
  15620. @@ -809,14 +855,10 @@
  15621. * not need to be dequeued.
  15622. */
  15623. if (rt_mutex_has_waiters(lock)) {
  15624. - /*
  15625. - * If @task->prio is greater than or equal to
  15626. - * the top waiter priority (kernel view),
  15627. - * @task lost.
  15628. - */
  15629. - if (task->prio >= rt_mutex_top_waiter(lock)->prio)
  15630. - return 0;
  15631. + struct task_struct *pown = rt_mutex_top_waiter(lock)->task;
  15632. + if (task != pown && !lock_is_stealable(task, pown, mode))
  15633. + return 0;
  15634. /*
  15635. * The current top waiter stays enqueued. We
  15636. * don't have to change anything in the lock
  15637. @@ -865,6 +907,347 @@
  15638. return 1;
  15639. }
  15640. +#ifdef CONFIG_PREEMPT_RT_FULL
  15641. +/*
  15642. + * preemptible spin_lock functions:
  15643. + */
  15644. +static inline void rt_spin_lock_fastlock(struct rt_mutex *lock,
  15645. + void (*slowfn)(struct rt_mutex *lock))
  15646. +{
  15647. + might_sleep_no_state_check();
  15648. +
  15649. + if (likely(rt_mutex_cmpxchg(lock, NULL, current)))
  15650. + rt_mutex_deadlock_account_lock(lock, current);
  15651. + else
  15652. + slowfn(lock);
  15653. +}
  15654. +
  15655. +static inline void rt_spin_lock_fastunlock(struct rt_mutex *lock,
  15656. + void (*slowfn)(struct rt_mutex *lock))
  15657. +{
  15658. + if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
  15659. + rt_mutex_deadlock_account_unlock(current);
  15660. + else
  15661. + slowfn(lock);
  15662. +}
  15663. +#ifdef CONFIG_SMP
  15664. +/*
  15665. + * Note that owner is a speculative pointer and dereferencing relies
  15666. + * on rcu_read_lock() and the check against the lock owner.
  15667. + */
  15668. +static int adaptive_wait(struct rt_mutex *lock,
  15669. + struct task_struct *owner)
  15670. +{
  15671. + int res = 0;
  15672. +
  15673. + rcu_read_lock();
  15674. + for (;;) {
  15675. + if (owner != rt_mutex_owner(lock))
  15676. + break;
  15677. + /*
  15678. + * Ensure that owner->on_cpu is dereferenced _after_
  15679. + * checking the above to be valid.
  15680. + */
  15681. + barrier();
  15682. + if (!owner->on_cpu) {
  15683. + res = 1;
  15684. + break;
  15685. + }
  15686. + cpu_relax();
  15687. + }
  15688. + rcu_read_unlock();
  15689. + return res;
  15690. +}
  15691. +#else
  15692. +static int adaptive_wait(struct rt_mutex *lock,
  15693. + struct task_struct *orig_owner)
  15694. +{
  15695. + return 1;
  15696. +}
  15697. +#endif
  15698. +
  15699. +# define pi_lock(lock) raw_spin_lock_irq(lock)
  15700. +# define pi_unlock(lock) raw_spin_unlock_irq(lock)
  15701. +
  15702. +static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  15703. + struct rt_mutex_waiter *waiter,
  15704. + struct task_struct *task,
  15705. + enum rtmutex_chainwalk chwalk);
  15706. +/*
  15707. + * Slow path lock function spin_lock style: this variant is very
  15708. + * careful not to miss any non-lock wakeups.
  15709. + *
  15710. + * We store the current state under p->pi_lock in p->saved_state and
  15711. + * the try_to_wake_up() code handles this accordingly.
  15712. + */
  15713. +static void noinline __sched rt_spin_lock_slowlock(struct rt_mutex *lock)
  15714. +{
  15715. + struct task_struct *lock_owner, *self = current;
  15716. + struct rt_mutex_waiter waiter, *top_waiter;
  15717. + int ret;
  15718. +
  15719. + rt_mutex_init_waiter(&waiter, true);
  15720. +
  15721. + raw_spin_lock(&lock->wait_lock);
  15722. +
  15723. + if (__try_to_take_rt_mutex(lock, self, NULL, STEAL_LATERAL)) {
  15724. + raw_spin_unlock(&lock->wait_lock);
  15725. + return;
  15726. + }
  15727. +
  15728. + BUG_ON(rt_mutex_owner(lock) == self);
  15729. +
  15730. + /*
  15731. + * We save whatever state the task is in and we'll restore it
  15732. + * after acquiring the lock taking real wakeups into account
  15733. + * as well. We are serialized via pi_lock against wakeups. See
  15734. + * try_to_wake_up().
  15735. + */
  15736. + pi_lock(&self->pi_lock);
  15737. + self->saved_state = self->state;
  15738. + __set_current_state_no_track(TASK_UNINTERRUPTIBLE);
  15739. + pi_unlock(&self->pi_lock);
  15740. +
  15741. + ret = task_blocks_on_rt_mutex(lock, &waiter, self, RT_MUTEX_MIN_CHAINWALK);
  15742. + BUG_ON(ret);
  15743. +
  15744. + for (;;) {
  15745. + /* Try to acquire the lock again. */
  15746. + if (__try_to_take_rt_mutex(lock, self, &waiter, STEAL_LATERAL))
  15747. + break;
  15748. +
  15749. + top_waiter = rt_mutex_top_waiter(lock);
  15750. + lock_owner = rt_mutex_owner(lock);
  15751. +
  15752. + raw_spin_unlock(&lock->wait_lock);
  15753. +
  15754. + debug_rt_mutex_print_deadlock(&waiter);
  15755. +
  15756. + if (top_waiter != &waiter || adaptive_wait(lock, lock_owner))
  15757. + schedule_rt_mutex(lock);
  15758. +
  15759. + raw_spin_lock(&lock->wait_lock);
  15760. +
  15761. + pi_lock(&self->pi_lock);
  15762. + __set_current_state_no_track(TASK_UNINTERRUPTIBLE);
  15763. + pi_unlock(&self->pi_lock);
  15764. + }
  15765. +
  15766. + /*
  15767. + * Restore the task state to current->saved_state. We set it
  15768. + * to the original state above and the try_to_wake_up() code
  15769. + * has possibly updated it when a real (non-rtmutex) wakeup
  15770. + * happened while we were blocked. Clear saved_state so
  15771. + * try_to_wakeup() does not get confused.
  15772. + */
  15773. + pi_lock(&self->pi_lock);
  15774. + __set_current_state_no_track(self->saved_state);
  15775. + self->saved_state = TASK_RUNNING;
  15776. + pi_unlock(&self->pi_lock);
  15777. +
  15778. + /*
  15779. + * try_to_take_rt_mutex() sets the waiter bit
  15780. + * unconditionally. We might have to fix that up:
  15781. + */
  15782. + fixup_rt_mutex_waiters(lock);
  15783. +
  15784. + BUG_ON(rt_mutex_has_waiters(lock) && &waiter == rt_mutex_top_waiter(lock));
  15785. + BUG_ON(!RB_EMPTY_NODE(&waiter.tree_entry));
  15786. +
  15787. + raw_spin_unlock(&lock->wait_lock);
  15788. +
  15789. + debug_rt_mutex_free_waiter(&waiter);
  15790. +}
  15791. +
  15792. +static void wakeup_next_waiter(struct rt_mutex *lock);
  15793. +/*
  15794. + * Slow path to release a rt_mutex spin_lock style
  15795. + */
  15796. +static void noinline __sched rt_spin_lock_slowunlock(struct rt_mutex *lock)
  15797. +{
  15798. + raw_spin_lock(&lock->wait_lock);
  15799. +
  15800. + debug_rt_mutex_unlock(lock);
  15801. +
  15802. + rt_mutex_deadlock_account_unlock(current);
  15803. +
  15804. + if (!rt_mutex_has_waiters(lock)) {
  15805. + lock->owner = NULL;
  15806. + raw_spin_unlock(&lock->wait_lock);
  15807. + return;
  15808. + }
  15809. +
  15810. + wakeup_next_waiter(lock);
  15811. +
  15812. + raw_spin_unlock(&lock->wait_lock);
  15813. +
  15814. + /* Undo pi boosting.when necessary */
  15815. + rt_mutex_adjust_prio(current);
  15816. +}
  15817. +
  15818. +void __lockfunc rt_spin_lock(spinlock_t *lock)
  15819. +{
  15820. + rt_spin_lock_fastlock(&lock->lock, rt_spin_lock_slowlock);
  15821. + spin_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  15822. +}
  15823. +EXPORT_SYMBOL(rt_spin_lock);
  15824. +
  15825. +void __lockfunc __rt_spin_lock(struct rt_mutex *lock)
  15826. +{
  15827. + rt_spin_lock_fastlock(lock, rt_spin_lock_slowlock);
  15828. +}
  15829. +EXPORT_SYMBOL(__rt_spin_lock);
  15830. +
  15831. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  15832. +void __lockfunc rt_spin_lock_nested(spinlock_t *lock, int subclass)
  15833. +{
  15834. + rt_spin_lock_fastlock(&lock->lock, rt_spin_lock_slowlock);
  15835. + spin_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
  15836. +}
  15837. +EXPORT_SYMBOL(rt_spin_lock_nested);
  15838. +#endif
  15839. +
  15840. +void __lockfunc rt_spin_unlock(spinlock_t *lock)
  15841. +{
  15842. + /* NOTE: we always pass in '1' for nested, for simplicity */
  15843. + spin_release(&lock->dep_map, 1, _RET_IP_);
  15844. + rt_spin_lock_fastunlock(&lock->lock, rt_spin_lock_slowunlock);
  15845. +}
  15846. +EXPORT_SYMBOL(rt_spin_unlock);
  15847. +
  15848. +void __lockfunc __rt_spin_unlock(struct rt_mutex *lock)
  15849. +{
  15850. + rt_spin_lock_fastunlock(lock, rt_spin_lock_slowunlock);
  15851. +}
  15852. +EXPORT_SYMBOL(__rt_spin_unlock);
  15853. +
  15854. +/*
  15855. + * Wait for the lock to get unlocked: instead of polling for an unlock
  15856. + * (like raw spinlocks do), we lock and unlock, to force the kernel to
  15857. + * schedule if there's contention:
  15858. + */
  15859. +void __lockfunc rt_spin_unlock_wait(spinlock_t *lock)
  15860. +{
  15861. + spin_lock(lock);
  15862. + spin_unlock(lock);
  15863. +}
  15864. +EXPORT_SYMBOL(rt_spin_unlock_wait);
  15865. +
  15866. +int __lockfunc __rt_spin_trylock(struct rt_mutex *lock)
  15867. +{
  15868. + return rt_mutex_trylock(lock);
  15869. +}
  15870. +
  15871. +int __lockfunc rt_spin_trylock(spinlock_t *lock)
  15872. +{
  15873. + int ret = rt_mutex_trylock(&lock->lock);
  15874. +
  15875. + if (ret)
  15876. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  15877. + return ret;
  15878. +}
  15879. +EXPORT_SYMBOL(rt_spin_trylock);
  15880. +
  15881. +int __lockfunc rt_spin_trylock_bh(spinlock_t *lock)
  15882. +{
  15883. + int ret;
  15884. +
  15885. + local_bh_disable();
  15886. + ret = rt_mutex_trylock(&lock->lock);
  15887. + if (ret) {
  15888. + migrate_disable();
  15889. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  15890. + } else
  15891. + local_bh_enable();
  15892. + return ret;
  15893. +}
  15894. +EXPORT_SYMBOL(rt_spin_trylock_bh);
  15895. +
  15896. +int __lockfunc rt_spin_trylock_irqsave(spinlock_t *lock, unsigned long *flags)
  15897. +{
  15898. + int ret;
  15899. +
  15900. + *flags = 0;
  15901. + ret = rt_mutex_trylock(&lock->lock);
  15902. + if (ret) {
  15903. + migrate_disable();
  15904. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  15905. + }
  15906. + return ret;
  15907. +}
  15908. +EXPORT_SYMBOL(rt_spin_trylock_irqsave);
  15909. +
  15910. +int atomic_dec_and_spin_lock(atomic_t *atomic, spinlock_t *lock)
  15911. +{
  15912. + /* Subtract 1 from counter unless that drops it to 0 (ie. it was 1) */
  15913. + if (atomic_add_unless(atomic, -1, 1))
  15914. + return 0;
  15915. + migrate_disable();
  15916. + rt_spin_lock(lock);
  15917. + if (atomic_dec_and_test(atomic))
  15918. + return 1;
  15919. + rt_spin_unlock(lock);
  15920. + migrate_enable();
  15921. + return 0;
  15922. +}
  15923. +EXPORT_SYMBOL(atomic_dec_and_spin_lock);
  15924. +
  15925. + void
  15926. +__rt_spin_lock_init(spinlock_t *lock, char *name, struct lock_class_key *key)
  15927. +{
  15928. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  15929. + /*
  15930. + * Make sure we are not reinitializing a held lock:
  15931. + */
  15932. + debug_check_no_locks_freed((void *)lock, sizeof(*lock));
  15933. + lockdep_init_map(&lock->dep_map, name, key, 0);
  15934. +#endif
  15935. +}
  15936. +EXPORT_SYMBOL(__rt_spin_lock_init);
  15937. +
  15938. +#endif /* PREEMPT_RT_FULL */
  15939. +
  15940. +#ifdef CONFIG_PREEMPT_RT_FULL
  15941. + static inline int __sched
  15942. +__mutex_lock_check_stamp(struct rt_mutex *lock, struct ww_acquire_ctx *ctx)
  15943. +{
  15944. + struct ww_mutex *ww = container_of(lock, struct ww_mutex, base.lock);
  15945. + struct ww_acquire_ctx *hold_ctx = ACCESS_ONCE(ww->ctx);
  15946. +
  15947. + if (!hold_ctx)
  15948. + return 0;
  15949. +
  15950. + if (unlikely(ctx == hold_ctx))
  15951. + return -EALREADY;
  15952. +
  15953. + if (ctx->stamp - hold_ctx->stamp <= LONG_MAX &&
  15954. + (ctx->stamp != hold_ctx->stamp || ctx > hold_ctx)) {
  15955. +#ifdef CONFIG_DEBUG_MUTEXES
  15956. + DEBUG_LOCKS_WARN_ON(ctx->contending_lock);
  15957. + ctx->contending_lock = ww;
  15958. +#endif
  15959. + return -EDEADLK;
  15960. + }
  15961. +
  15962. + return 0;
  15963. +}
  15964. +#else
  15965. + static inline int __sched
  15966. +__mutex_lock_check_stamp(struct rt_mutex *lock, struct ww_acquire_ctx *ctx)
  15967. +{
  15968. + BUG();
  15969. + return 0;
  15970. +}
  15971. +
  15972. +#endif
  15973. +
  15974. +static inline int
  15975. +try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  15976. + struct rt_mutex_waiter *waiter)
  15977. +{
  15978. + return __try_to_take_rt_mutex(lock, task, waiter, STEAL_NORMAL);
  15979. +}
  15980. +
  15981. /*
  15982. * Task blocks on lock.
  15983. *
  15984. @@ -896,6 +1279,23 @@
  15985. return -EDEADLK;
  15986. raw_spin_lock_irqsave(&task->pi_lock, flags);
  15987. +
  15988. + /*
  15989. + * In the case of futex requeue PI, this will be a proxy
  15990. + * lock. The task will wake unaware that it is enqueueed on
  15991. + * this lock. Avoid blocking on two locks and corrupting
  15992. + * pi_blocked_on via the PI_WAKEUP_INPROGRESS
  15993. + * flag. futex_wait_requeue_pi() sets this when it wakes up
  15994. + * before requeue (due to a signal or timeout). Do not enqueue
  15995. + * the task if PI_WAKEUP_INPROGRESS is set.
  15996. + */
  15997. + if (task != current && task->pi_blocked_on == PI_WAKEUP_INPROGRESS) {
  15998. + raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  15999. + return -EAGAIN;
  16000. + }
  16001. +
  16002. + BUG_ON(rt_mutex_real_waiter(task->pi_blocked_on));
  16003. +
  16004. __rt_mutex_adjust_prio(task);
  16005. waiter->task = task;
  16006. waiter->lock = lock;
  16007. @@ -919,7 +1319,7 @@
  16008. rt_mutex_enqueue_pi(owner, waiter);
  16009. __rt_mutex_adjust_prio(owner);
  16010. - if (owner->pi_blocked_on)
  16011. + if (rt_mutex_real_waiter(owner->pi_blocked_on))
  16012. chain_walk = 1;
  16013. } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
  16014. chain_walk = 1;
  16015. @@ -957,8 +1357,9 @@
  16016. /*
  16017. * Wake up the next waiter on the lock.
  16018. *
  16019. - * Remove the top waiter from the current tasks pi waiter list and
  16020. - * wake it up.
  16021. + * Remove the top waiter from the current tasks pi waiter list,
  16022. + * wake it up and return whether the current task needs to undo
  16023. + * a potential priority boosting.
  16024. *
  16025. * Called with lock->wait_lock held.
  16026. */
  16027. @@ -996,7 +1397,7 @@
  16028. * long as we hold lock->wait_lock. The waiter task needs to
  16029. * acquire it in order to dequeue the waiter.
  16030. */
  16031. - wake_up_process(waiter->task);
  16032. + rt_mutex_wake_waiter(waiter);
  16033. }
  16034. /*
  16035. @@ -1010,7 +1411,7 @@
  16036. {
  16037. bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
  16038. struct task_struct *owner = rt_mutex_owner(lock);
  16039. - struct rt_mutex *next_lock;
  16040. + struct rt_mutex *next_lock = NULL;
  16041. unsigned long flags;
  16042. raw_spin_lock_irqsave(&current->pi_lock, flags);
  16043. @@ -1035,7 +1436,8 @@
  16044. __rt_mutex_adjust_prio(owner);
  16045. /* Store the lock on which owner is blocked or NULL */
  16046. - next_lock = task_blocked_on_lock(owner);
  16047. + if (rt_mutex_real_waiter(owner->pi_blocked_on))
  16048. + next_lock = task_blocked_on_lock(owner);
  16049. raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
  16050. @@ -1071,17 +1473,17 @@
  16051. raw_spin_lock_irqsave(&task->pi_lock, flags);
  16052. waiter = task->pi_blocked_on;
  16053. - if (!waiter || (waiter->prio == task->prio &&
  16054. + if (!rt_mutex_real_waiter(waiter) || (waiter->prio == task->prio &&
  16055. !dl_prio(task->prio))) {
  16056. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  16057. return;
  16058. }
  16059. next_lock = waiter->lock;
  16060. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  16061. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  16062. get_task_struct(task);
  16063. + raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  16064. rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
  16065. next_lock, NULL, task);
  16066. }
  16067. @@ -1099,7 +1501,8 @@
  16068. static int __sched
  16069. __rt_mutex_slowlock(struct rt_mutex *lock, int state,
  16070. struct hrtimer_sleeper *timeout,
  16071. - struct rt_mutex_waiter *waiter)
  16072. + struct rt_mutex_waiter *waiter,
  16073. + struct ww_acquire_ctx *ww_ctx)
  16074. {
  16075. int ret = 0;
  16076. @@ -1122,6 +1525,12 @@
  16077. break;
  16078. }
  16079. + if (ww_ctx && ww_ctx->acquired > 0) {
  16080. + ret = __mutex_lock_check_stamp(lock, ww_ctx);
  16081. + if (ret)
  16082. + break;
  16083. + }
  16084. +
  16085. raw_spin_unlock(&lock->wait_lock);
  16086. debug_rt_mutex_print_deadlock(waiter);
  16087. @@ -1156,25 +1565,102 @@
  16088. }
  16089. }
  16090. +static __always_inline void ww_mutex_lock_acquired(struct ww_mutex *ww,
  16091. + struct ww_acquire_ctx *ww_ctx)
  16092. +{
  16093. +#ifdef CONFIG_DEBUG_MUTEXES
  16094. + /*
  16095. + * If this WARN_ON triggers, you used ww_mutex_lock to acquire,
  16096. + * but released with a normal mutex_unlock in this call.
  16097. + *
  16098. + * This should never happen, always use ww_mutex_unlock.
  16099. + */
  16100. + DEBUG_LOCKS_WARN_ON(ww->ctx);
  16101. +
  16102. + /*
  16103. + * Not quite done after calling ww_acquire_done() ?
  16104. + */
  16105. + DEBUG_LOCKS_WARN_ON(ww_ctx->done_acquire);
  16106. +
  16107. + if (ww_ctx->contending_lock) {
  16108. + /*
  16109. + * After -EDEADLK you tried to
  16110. + * acquire a different ww_mutex? Bad!
  16111. + */
  16112. + DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock != ww);
  16113. +
  16114. + /*
  16115. + * You called ww_mutex_lock after receiving -EDEADLK,
  16116. + * but 'forgot' to unlock everything else first?
  16117. + */
  16118. + DEBUG_LOCKS_WARN_ON(ww_ctx->acquired > 0);
  16119. + ww_ctx->contending_lock = NULL;
  16120. + }
  16121. +
  16122. + /*
  16123. + * Naughty, using a different class will lead to undefined behavior!
  16124. + */
  16125. + DEBUG_LOCKS_WARN_ON(ww_ctx->ww_class != ww->ww_class);
  16126. +#endif
  16127. + ww_ctx->acquired++;
  16128. +}
  16129. +
  16130. +#ifdef CONFIG_PREEMPT_RT_FULL
  16131. +static void ww_mutex_account_lock(struct rt_mutex *lock,
  16132. + struct ww_acquire_ctx *ww_ctx)
  16133. +{
  16134. + struct ww_mutex *ww = container_of(lock, struct ww_mutex, base.lock);
  16135. + struct rt_mutex_waiter *waiter, *n;
  16136. +
  16137. + /*
  16138. + * This branch gets optimized out for the common case,
  16139. + * and is only important for ww_mutex_lock.
  16140. + */
  16141. + ww_mutex_lock_acquired(ww, ww_ctx);
  16142. + ww->ctx = ww_ctx;
  16143. +
  16144. + /*
  16145. + * Give any possible sleeping processes the chance to wake up,
  16146. + * so they can recheck if they have to back off.
  16147. + */
  16148. + rbtree_postorder_for_each_entry_safe(waiter, n, &lock->waiters,
  16149. + tree_entry) {
  16150. + /* XXX debug rt mutex waiter wakeup */
  16151. +
  16152. + BUG_ON(waiter->lock != lock);
  16153. + rt_mutex_wake_waiter(waiter);
  16154. + }
  16155. +}
  16156. +
  16157. +#else
  16158. +
  16159. +static void ww_mutex_account_lock(struct rt_mutex *lock,
  16160. + struct ww_acquire_ctx *ww_ctx)
  16161. +{
  16162. + BUG();
  16163. +}
  16164. +#endif
  16165. +
  16166. /*
  16167. * Slow path lock function:
  16168. */
  16169. static int __sched
  16170. rt_mutex_slowlock(struct rt_mutex *lock, int state,
  16171. struct hrtimer_sleeper *timeout,
  16172. - enum rtmutex_chainwalk chwalk)
  16173. + enum rtmutex_chainwalk chwalk,
  16174. + struct ww_acquire_ctx *ww_ctx)
  16175. {
  16176. struct rt_mutex_waiter waiter;
  16177. int ret = 0;
  16178. - debug_rt_mutex_init_waiter(&waiter);
  16179. - RB_CLEAR_NODE(&waiter.pi_tree_entry);
  16180. - RB_CLEAR_NODE(&waiter.tree_entry);
  16181. + rt_mutex_init_waiter(&waiter, false);
  16182. raw_spin_lock(&lock->wait_lock);
  16183. /* Try to acquire the lock again: */
  16184. if (try_to_take_rt_mutex(lock, current, NULL)) {
  16185. + if (ww_ctx)
  16186. + ww_mutex_account_lock(lock, ww_ctx);
  16187. raw_spin_unlock(&lock->wait_lock);
  16188. return 0;
  16189. }
  16190. @@ -1192,13 +1678,23 @@
  16191. if (likely(!ret))
  16192. /* sleep on the mutex */
  16193. - ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
  16194. + ret = __rt_mutex_slowlock(lock, state, timeout, &waiter,
  16195. + ww_ctx);
  16196. + else if (ww_ctx) {
  16197. + /* ww_mutex received EDEADLK, let it become EALREADY */
  16198. + ret = __mutex_lock_check_stamp(lock, ww_ctx);
  16199. + BUG_ON(!ret);
  16200. + }
  16201. if (unlikely(ret)) {
  16202. __set_current_state(TASK_RUNNING);
  16203. if (rt_mutex_has_waiters(lock))
  16204. remove_waiter(lock, &waiter);
  16205. - rt_mutex_handle_deadlock(ret, chwalk, &waiter);
  16206. + /* ww_mutex want to report EDEADLK/EALREADY, let them */
  16207. + if (!ww_ctx)
  16208. + rt_mutex_handle_deadlock(ret, chwalk, &waiter);
  16209. + } else if (ww_ctx) {
  16210. + ww_mutex_account_lock(lock, ww_ctx);
  16211. }
  16212. /*
  16213. @@ -1255,7 +1751,7 @@
  16214. /*
  16215. * Slow path to release a rt-mutex:
  16216. */
  16217. -static void __sched
  16218. +static bool __sched
  16219. rt_mutex_slowunlock(struct rt_mutex *lock)
  16220. {
  16221. raw_spin_lock(&lock->wait_lock);
  16222. @@ -1298,7 +1794,7 @@
  16223. while (!rt_mutex_has_waiters(lock)) {
  16224. /* Drops lock->wait_lock ! */
  16225. if (unlock_rt_mutex_safe(lock) == true)
  16226. - return;
  16227. + return false;
  16228. /* Relock the rtmutex and try again */
  16229. raw_spin_lock(&lock->wait_lock);
  16230. }
  16231. @@ -1311,8 +1807,7 @@
  16232. raw_spin_unlock(&lock->wait_lock);
  16233. - /* Undo pi boosting if necessary: */
  16234. - rt_mutex_adjust_prio(current);
  16235. + return true;
  16236. }
  16237. /*
  16238. @@ -1323,31 +1818,36 @@
  16239. */
  16240. static inline int
  16241. rt_mutex_fastlock(struct rt_mutex *lock, int state,
  16242. + struct ww_acquire_ctx *ww_ctx,
  16243. int (*slowfn)(struct rt_mutex *lock, int state,
  16244. struct hrtimer_sleeper *timeout,
  16245. - enum rtmutex_chainwalk chwalk))
  16246. + enum rtmutex_chainwalk chwalk,
  16247. + struct ww_acquire_ctx *ww_ctx))
  16248. {
  16249. if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  16250. rt_mutex_deadlock_account_lock(lock, current);
  16251. return 0;
  16252. } else
  16253. - return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
  16254. + return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK,
  16255. + ww_ctx);
  16256. }
  16257. static inline int
  16258. rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
  16259. struct hrtimer_sleeper *timeout,
  16260. enum rtmutex_chainwalk chwalk,
  16261. + struct ww_acquire_ctx *ww_ctx,
  16262. int (*slowfn)(struct rt_mutex *lock, int state,
  16263. struct hrtimer_sleeper *timeout,
  16264. - enum rtmutex_chainwalk chwalk))
  16265. + enum rtmutex_chainwalk chwalk,
  16266. + struct ww_acquire_ctx *ww_ctx))
  16267. {
  16268. if (chwalk == RT_MUTEX_MIN_CHAINWALK &&
  16269. likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  16270. rt_mutex_deadlock_account_lock(lock, current);
  16271. return 0;
  16272. } else
  16273. - return slowfn(lock, state, timeout, chwalk);
  16274. + return slowfn(lock, state, timeout, chwalk, ww_ctx);
  16275. }
  16276. static inline int
  16277. @@ -1363,12 +1863,14 @@
  16278. static inline void
  16279. rt_mutex_fastunlock(struct rt_mutex *lock,
  16280. - void (*slowfn)(struct rt_mutex *lock))
  16281. + bool (*slowfn)(struct rt_mutex *lock))
  16282. {
  16283. - if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
  16284. + if (likely(rt_mutex_cmpxchg(lock, current, NULL))) {
  16285. rt_mutex_deadlock_account_unlock(current);
  16286. - else
  16287. - slowfn(lock);
  16288. + } else if (slowfn(lock)) {
  16289. + /* Undo pi boosting if necessary: */
  16290. + rt_mutex_adjust_prio(current);
  16291. + }
  16292. }
  16293. /**
  16294. @@ -1380,7 +1882,7 @@
  16295. {
  16296. might_sleep();
  16297. - rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
  16298. + rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, NULL, rt_mutex_slowlock);
  16299. }
  16300. EXPORT_SYMBOL_GPL(rt_mutex_lock);
  16301. @@ -1397,7 +1899,7 @@
  16302. {
  16303. might_sleep();
  16304. - return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
  16305. + return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, NULL, rt_mutex_slowlock);
  16306. }
  16307. EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
  16308. @@ -1410,11 +1912,30 @@
  16309. might_sleep();
  16310. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  16311. - RT_MUTEX_FULL_CHAINWALK,
  16312. + RT_MUTEX_FULL_CHAINWALK, NULL,
  16313. rt_mutex_slowlock);
  16314. }
  16315. /**
  16316. + * rt_mutex_lock_killable - lock a rt_mutex killable
  16317. + *
  16318. + * @lock: the rt_mutex to be locked
  16319. + * @detect_deadlock: deadlock detection on/off
  16320. + *
  16321. + * Returns:
  16322. + * 0 on success
  16323. + * -EINTR when interrupted by a signal
  16324. + * -EDEADLK when the lock would deadlock (when deadlock detection is on)
  16325. + */
  16326. +int __sched rt_mutex_lock_killable(struct rt_mutex *lock)
  16327. +{
  16328. + might_sleep();
  16329. +
  16330. + return rt_mutex_fastlock(lock, TASK_KILLABLE, NULL, rt_mutex_slowlock);
  16331. +}
  16332. +EXPORT_SYMBOL_GPL(rt_mutex_lock_killable);
  16333. +
  16334. +/**
  16335. * rt_mutex_timed_lock - lock a rt_mutex interruptible
  16336. * the timeout structure is provided
  16337. * by the caller
  16338. @@ -1434,6 +1955,7 @@
  16339. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  16340. RT_MUTEX_MIN_CHAINWALK,
  16341. + NULL,
  16342. rt_mutex_slowlock);
  16343. }
  16344. EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
  16345. @@ -1463,6 +1985,22 @@
  16346. EXPORT_SYMBOL_GPL(rt_mutex_unlock);
  16347. /**
  16348. + * rt_mutex_futex_unlock - Futex variant of rt_mutex_unlock
  16349. + * @lock: the rt_mutex to be unlocked
  16350. + *
  16351. + * Returns: true/false indicating whether priority adjustment is
  16352. + * required or not.
  16353. + */
  16354. +bool __sched rt_mutex_futex_unlock(struct rt_mutex *lock)
  16355. +{
  16356. + if (likely(rt_mutex_cmpxchg(lock, current, NULL))) {
  16357. + rt_mutex_deadlock_account_unlock(current);
  16358. + return false;
  16359. + }
  16360. + return rt_mutex_slowunlock(lock);
  16361. +}
  16362. +
  16363. +/**
  16364. * rt_mutex_destroy - mark a mutex unusable
  16365. * @lock: the mutex to be destroyed
  16366. *
  16367. @@ -1492,13 +2030,12 @@
  16368. void __rt_mutex_init(struct rt_mutex *lock, const char *name)
  16369. {
  16370. lock->owner = NULL;
  16371. - raw_spin_lock_init(&lock->wait_lock);
  16372. lock->waiters = RB_ROOT;
  16373. lock->waiters_leftmost = NULL;
  16374. debug_rt_mutex_init(lock, name);
  16375. }
  16376. -EXPORT_SYMBOL_GPL(__rt_mutex_init);
  16377. +EXPORT_SYMBOL(__rt_mutex_init);
  16378. /**
  16379. * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
  16380. @@ -1513,7 +2050,7 @@
  16381. void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
  16382. struct task_struct *proxy_owner)
  16383. {
  16384. - __rt_mutex_init(lock, NULL);
  16385. + rt_mutex_init(lock);
  16386. debug_rt_mutex_proxy_lock(lock, proxy_owner);
  16387. rt_mutex_set_owner(lock, proxy_owner);
  16388. rt_mutex_deadlock_account_lock(lock, proxy_owner);
  16389. @@ -1561,6 +2098,35 @@
  16390. return 1;
  16391. }
  16392. +#ifdef CONFIG_PREEMPT_RT_FULL
  16393. + /*
  16394. + * In PREEMPT_RT there's an added race.
  16395. + * If the task, that we are about to requeue, times out,
  16396. + * it can set the PI_WAKEUP_INPROGRESS. This tells the requeue
  16397. + * to skip this task. But right after the task sets
  16398. + * its pi_blocked_on to PI_WAKEUP_INPROGRESS it can then
  16399. + * block on the spin_lock(&hb->lock), which in RT is an rtmutex.
  16400. + * This will replace the PI_WAKEUP_INPROGRESS with the actual
  16401. + * lock that it blocks on. We *must not* place this task
  16402. + * on this proxy lock in that case.
  16403. + *
  16404. + * To prevent this race, we first take the task's pi_lock
  16405. + * and check if it has updated its pi_blocked_on. If it has,
  16406. + * we assume that it woke up and we return -EAGAIN.
  16407. + * Otherwise, we set the task's pi_blocked_on to
  16408. + * PI_REQUEUE_INPROGRESS, so that if the task is waking up
  16409. + * it will know that we are in the process of requeuing it.
  16410. + */
  16411. + raw_spin_lock_irq(&task->pi_lock);
  16412. + if (task->pi_blocked_on) {
  16413. + raw_spin_unlock_irq(&task->pi_lock);
  16414. + raw_spin_unlock(&lock->wait_lock);
  16415. + return -EAGAIN;
  16416. + }
  16417. + task->pi_blocked_on = PI_REQUEUE_INPROGRESS;
  16418. + raw_spin_unlock_irq(&task->pi_lock);
  16419. +#endif
  16420. +
  16421. /* We enforce deadlock detection for futexes */
  16422. ret = task_blocks_on_rt_mutex(lock, waiter, task,
  16423. RT_MUTEX_FULL_CHAINWALK);
  16424. @@ -1575,7 +2141,7 @@
  16425. ret = 0;
  16426. }
  16427. - if (unlikely(ret))
  16428. + if (ret && rt_mutex_has_waiters(lock))
  16429. remove_waiter(lock, waiter);
  16430. raw_spin_unlock(&lock->wait_lock);
  16431. @@ -1631,7 +2197,7 @@
  16432. set_current_state(TASK_INTERRUPTIBLE);
  16433. /* sleep on the mutex */
  16434. - ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
  16435. + ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter, NULL);
  16436. if (unlikely(ret))
  16437. remove_waiter(lock, waiter);
  16438. @@ -1646,3 +2212,89 @@
  16439. return ret;
  16440. }
  16441. +
  16442. +static inline int
  16443. +ww_mutex_deadlock_injection(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
  16444. +{
  16445. +#ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
  16446. + unsigned tmp;
  16447. +
  16448. + if (ctx->deadlock_inject_countdown-- == 0) {
  16449. + tmp = ctx->deadlock_inject_interval;
  16450. + if (tmp > UINT_MAX/4)
  16451. + tmp = UINT_MAX;
  16452. + else
  16453. + tmp = tmp*2 + tmp + tmp/2;
  16454. +
  16455. + ctx->deadlock_inject_interval = tmp;
  16456. + ctx->deadlock_inject_countdown = tmp;
  16457. + ctx->contending_lock = lock;
  16458. +
  16459. + ww_mutex_unlock(lock);
  16460. +
  16461. + return -EDEADLK;
  16462. + }
  16463. +#endif
  16464. +
  16465. + return 0;
  16466. +}
  16467. +
  16468. +#ifdef CONFIG_PREEMPT_RT_FULL
  16469. +int __sched
  16470. +__ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ww_ctx)
  16471. +{
  16472. + int ret;
  16473. +
  16474. + might_sleep();
  16475. +
  16476. + mutex_acquire_nest(&lock->base.dep_map, 0, 0, &ww_ctx->dep_map, _RET_IP_);
  16477. + ret = rt_mutex_slowlock(&lock->base.lock, TASK_INTERRUPTIBLE, NULL, 0, ww_ctx);
  16478. + if (ret)
  16479. + mutex_release(&lock->base.dep_map, 1, _RET_IP_);
  16480. + else if (!ret && ww_ctx->acquired > 1)
  16481. + return ww_mutex_deadlock_injection(lock, ww_ctx);
  16482. +
  16483. + return ret;
  16484. +}
  16485. +EXPORT_SYMBOL_GPL(__ww_mutex_lock_interruptible);
  16486. +
  16487. +int __sched
  16488. +__ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ww_ctx)
  16489. +{
  16490. + int ret;
  16491. +
  16492. + might_sleep();
  16493. +
  16494. + mutex_acquire_nest(&lock->base.dep_map, 0, 0, &ww_ctx->dep_map, _RET_IP_);
  16495. + ret = rt_mutex_slowlock(&lock->base.lock, TASK_UNINTERRUPTIBLE, NULL, 0, ww_ctx);
  16496. + if (ret)
  16497. + mutex_release(&lock->base.dep_map, 1, _RET_IP_);
  16498. + else if (!ret && ww_ctx->acquired > 1)
  16499. + return ww_mutex_deadlock_injection(lock, ww_ctx);
  16500. +
  16501. + return ret;
  16502. +}
  16503. +EXPORT_SYMBOL_GPL(__ww_mutex_lock);
  16504. +
  16505. +void __sched ww_mutex_unlock(struct ww_mutex *lock)
  16506. +{
  16507. + int nest = !!lock->ctx;
  16508. +
  16509. + /*
  16510. + * The unlocking fastpath is the 0->1 transition from 'locked'
  16511. + * into 'unlocked' state:
  16512. + */
  16513. + if (nest) {
  16514. +#ifdef CONFIG_DEBUG_MUTEXES
  16515. + DEBUG_LOCKS_WARN_ON(!lock->ctx->acquired);
  16516. +#endif
  16517. + if (lock->ctx->acquired > 0)
  16518. + lock->ctx->acquired--;
  16519. + lock->ctx = NULL;
  16520. + }
  16521. +
  16522. + mutex_release(&lock->base.dep_map, nest, _RET_IP_);
  16523. + rt_mutex_unlock(&lock->base.lock);
  16524. +}
  16525. +EXPORT_SYMBOL(ww_mutex_unlock);
  16526. +#endif
  16527. diff -Nur linux-4.1.20.orig/kernel/locking/rtmutex_common.h linux-4.1.20/kernel/locking/rtmutex_common.h
  16528. --- linux-4.1.20.orig/kernel/locking/rtmutex_common.h 2016-03-17 19:11:03.000000000 +0100
  16529. +++ linux-4.1.20/kernel/locking/rtmutex_common.h 2016-03-21 20:18:33.000000000 +0100
  16530. @@ -49,6 +49,7 @@
  16531. struct rb_node pi_tree_entry;
  16532. struct task_struct *task;
  16533. struct rt_mutex *lock;
  16534. + bool savestate;
  16535. #ifdef CONFIG_DEBUG_RT_MUTEXES
  16536. unsigned long ip;
  16537. struct pid *deadlock_task_pid;
  16538. @@ -119,6 +120,9 @@
  16539. /*
  16540. * PI-futex support (proxy locking functions, etc.):
  16541. */
  16542. +#define PI_WAKEUP_INPROGRESS ((struct rt_mutex_waiter *) 1)
  16543. +#define PI_REQUEUE_INPROGRESS ((struct rt_mutex_waiter *) 2)
  16544. +
  16545. extern struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock);
  16546. extern void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
  16547. struct task_struct *proxy_owner);
  16548. @@ -132,10 +136,24 @@
  16549. struct rt_mutex_waiter *waiter);
  16550. extern int rt_mutex_timed_futex_lock(struct rt_mutex *l, struct hrtimer_sleeper *to);
  16551. +extern bool rt_mutex_futex_unlock(struct rt_mutex *lock);
  16552. +
  16553. +extern void rt_mutex_adjust_prio(struct task_struct *task);
  16554. +
  16555. #ifdef CONFIG_DEBUG_RT_MUTEXES
  16556. # include "rtmutex-debug.h"
  16557. #else
  16558. # include "rtmutex.h"
  16559. #endif
  16560. +static inline void
  16561. +rt_mutex_init_waiter(struct rt_mutex_waiter *waiter, bool savestate)
  16562. +{
  16563. + debug_rt_mutex_init_waiter(waiter);
  16564. + waiter->task = NULL;
  16565. + waiter->savestate = savestate;
  16566. + RB_CLEAR_NODE(&waiter->pi_tree_entry);
  16567. + RB_CLEAR_NODE(&waiter->tree_entry);
  16568. +}
  16569. +
  16570. #endif
  16571. diff -Nur linux-4.1.20.orig/kernel/locking/spinlock.c linux-4.1.20/kernel/locking/spinlock.c
  16572. --- linux-4.1.20.orig/kernel/locking/spinlock.c 2016-03-17 19:11:03.000000000 +0100
  16573. +++ linux-4.1.20/kernel/locking/spinlock.c 2016-03-21 20:18:33.000000000 +0100
  16574. @@ -124,8 +124,11 @@
  16575. * __[spin|read|write]_lock_bh()
  16576. */
  16577. BUILD_LOCK_OPS(spin, raw_spinlock);
  16578. +
  16579. +#ifndef CONFIG_PREEMPT_RT_FULL
  16580. BUILD_LOCK_OPS(read, rwlock);
  16581. BUILD_LOCK_OPS(write, rwlock);
  16582. +#endif
  16583. #endif
  16584. @@ -209,6 +212,8 @@
  16585. EXPORT_SYMBOL(_raw_spin_unlock_bh);
  16586. #endif
  16587. +#ifndef CONFIG_PREEMPT_RT_FULL
  16588. +
  16589. #ifndef CONFIG_INLINE_READ_TRYLOCK
  16590. int __lockfunc _raw_read_trylock(rwlock_t *lock)
  16591. {
  16592. @@ -353,6 +358,8 @@
  16593. EXPORT_SYMBOL(_raw_write_unlock_bh);
  16594. #endif
  16595. +#endif /* !PREEMPT_RT_FULL */
  16596. +
  16597. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  16598. void __lockfunc _raw_spin_lock_nested(raw_spinlock_t *lock, int subclass)
  16599. diff -Nur linux-4.1.20.orig/kernel/locking/spinlock_debug.c linux-4.1.20/kernel/locking/spinlock_debug.c
  16600. --- linux-4.1.20.orig/kernel/locking/spinlock_debug.c 2016-03-17 19:11:03.000000000 +0100
  16601. +++ linux-4.1.20/kernel/locking/spinlock_debug.c 2016-03-21 20:18:33.000000000 +0100
  16602. @@ -31,6 +31,7 @@
  16603. EXPORT_SYMBOL(__raw_spin_lock_init);
  16604. +#ifndef CONFIG_PREEMPT_RT_FULL
  16605. void __rwlock_init(rwlock_t *lock, const char *name,
  16606. struct lock_class_key *key)
  16607. {
  16608. @@ -48,6 +49,7 @@
  16609. }
  16610. EXPORT_SYMBOL(__rwlock_init);
  16611. +#endif
  16612. static void spin_dump(raw_spinlock_t *lock, const char *msg)
  16613. {
  16614. @@ -159,6 +161,7 @@
  16615. arch_spin_unlock(&lock->raw_lock);
  16616. }
  16617. +#ifndef CONFIG_PREEMPT_RT_FULL
  16618. static void rwlock_bug(rwlock_t *lock, const char *msg)
  16619. {
  16620. if (!debug_locks_off())
  16621. @@ -300,3 +303,5 @@
  16622. debug_write_unlock(lock);
  16623. arch_write_unlock(&lock->raw_lock);
  16624. }
  16625. +
  16626. +#endif
  16627. diff -Nur linux-4.1.20.orig/kernel/panic.c linux-4.1.20/kernel/panic.c
  16628. --- linux-4.1.20.orig/kernel/panic.c 2016-03-17 19:11:03.000000000 +0100
  16629. +++ linux-4.1.20/kernel/panic.c 2016-03-21 20:18:33.000000000 +0100
  16630. @@ -399,9 +399,11 @@
  16631. static int init_oops_id(void)
  16632. {
  16633. +#ifndef CONFIG_PREEMPT_RT_FULL
  16634. if (!oops_id)
  16635. get_random_bytes(&oops_id, sizeof(oops_id));
  16636. else
  16637. +#endif
  16638. oops_id++;
  16639. return 0;
  16640. diff -Nur linux-4.1.20.orig/kernel/power/hibernate.c linux-4.1.20/kernel/power/hibernate.c
  16641. --- linux-4.1.20.orig/kernel/power/hibernate.c 2016-03-17 19:11:03.000000000 +0100
  16642. +++ linux-4.1.20/kernel/power/hibernate.c 2016-03-21 20:18:33.000000000 +0100
  16643. @@ -285,6 +285,8 @@
  16644. local_irq_disable();
  16645. + system_state = SYSTEM_SUSPEND;
  16646. +
  16647. error = syscore_suspend();
  16648. if (error) {
  16649. printk(KERN_ERR "PM: Some system devices failed to power down, "
  16650. @@ -314,6 +316,7 @@
  16651. syscore_resume();
  16652. Enable_irqs:
  16653. + system_state = SYSTEM_RUNNING;
  16654. local_irq_enable();
  16655. Enable_cpus:
  16656. @@ -437,6 +440,7 @@
  16657. goto Enable_cpus;
  16658. local_irq_disable();
  16659. + system_state = SYSTEM_SUSPEND;
  16660. error = syscore_suspend();
  16661. if (error)
  16662. @@ -470,6 +474,7 @@
  16663. syscore_resume();
  16664. Enable_irqs:
  16665. + system_state = SYSTEM_RUNNING;
  16666. local_irq_enable();
  16667. Enable_cpus:
  16668. @@ -555,6 +560,7 @@
  16669. goto Platform_finish;
  16670. local_irq_disable();
  16671. + system_state = SYSTEM_SUSPEND;
  16672. syscore_suspend();
  16673. if (pm_wakeup_pending()) {
  16674. error = -EAGAIN;
  16675. @@ -567,6 +573,7 @@
  16676. Power_up:
  16677. syscore_resume();
  16678. + system_state = SYSTEM_RUNNING;
  16679. local_irq_enable();
  16680. enable_nonboot_cpus();
  16681. diff -Nur linux-4.1.20.orig/kernel/power/suspend.c linux-4.1.20/kernel/power/suspend.c
  16682. --- linux-4.1.20.orig/kernel/power/suspend.c 2016-03-17 19:11:03.000000000 +0100
  16683. +++ linux-4.1.20/kernel/power/suspend.c 2016-03-21 20:18:33.000000000 +0100
  16684. @@ -356,6 +356,8 @@
  16685. arch_suspend_disable_irqs();
  16686. BUG_ON(!irqs_disabled());
  16687. + system_state = SYSTEM_SUSPEND;
  16688. +
  16689. error = syscore_suspend();
  16690. if (!error) {
  16691. *wakeup = pm_wakeup_pending();
  16692. @@ -370,6 +372,8 @@
  16693. syscore_resume();
  16694. }
  16695. + system_state = SYSTEM_RUNNING;
  16696. +
  16697. arch_suspend_enable_irqs();
  16698. BUG_ON(irqs_disabled());
  16699. diff -Nur linux-4.1.20.orig/kernel/printk/printk.c linux-4.1.20/kernel/printk/printk.c
  16700. --- linux-4.1.20.orig/kernel/printk/printk.c 2016-03-17 19:11:03.000000000 +0100
  16701. +++ linux-4.1.20/kernel/printk/printk.c 2016-03-21 20:18:33.000000000 +0100
  16702. @@ -1163,6 +1163,7 @@
  16703. {
  16704. char *text;
  16705. int len = 0;
  16706. + int attempts = 0;
  16707. text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
  16708. if (!text)
  16709. @@ -1174,7 +1175,14 @@
  16710. u64 seq;
  16711. u32 idx;
  16712. enum log_flags prev;
  16713. -
  16714. + int num_msg;
  16715. +try_again:
  16716. + attempts++;
  16717. + if (attempts > 10) {
  16718. + len = -EBUSY;
  16719. + goto out;
  16720. + }
  16721. + num_msg = 0;
  16722. if (clear_seq < log_first_seq) {
  16723. /* messages are gone, move to first available one */
  16724. clear_seq = log_first_seq;
  16725. @@ -1195,6 +1203,14 @@
  16726. prev = msg->flags;
  16727. idx = log_next(idx);
  16728. seq++;
  16729. + num_msg++;
  16730. + if (num_msg > 5) {
  16731. + num_msg = 0;
  16732. + raw_spin_unlock_irq(&logbuf_lock);
  16733. + raw_spin_lock_irq(&logbuf_lock);
  16734. + if (clear_seq < log_first_seq)
  16735. + goto try_again;
  16736. + }
  16737. }
  16738. /* move first record forward until length fits into the buffer */
  16739. @@ -1208,6 +1224,14 @@
  16740. prev = msg->flags;
  16741. idx = log_next(idx);
  16742. seq++;
  16743. + num_msg++;
  16744. + if (num_msg > 5) {
  16745. + num_msg = 0;
  16746. + raw_spin_unlock_irq(&logbuf_lock);
  16747. + raw_spin_lock_irq(&logbuf_lock);
  16748. + if (clear_seq < log_first_seq)
  16749. + goto try_again;
  16750. + }
  16751. }
  16752. /* last message fitting into this dump */
  16753. @@ -1248,6 +1272,7 @@
  16754. clear_seq = log_next_seq;
  16755. clear_idx = log_next_idx;
  16756. }
  16757. +out:
  16758. raw_spin_unlock_irq(&logbuf_lock);
  16759. kfree(text);
  16760. @@ -1401,6 +1426,7 @@
  16761. if (!console_drivers)
  16762. return;
  16763. + migrate_disable();
  16764. for_each_console(con) {
  16765. if (exclusive_console && con != exclusive_console)
  16766. continue;
  16767. @@ -1413,6 +1439,7 @@
  16768. continue;
  16769. con->write(con, text, len);
  16770. }
  16771. + migrate_enable();
  16772. }
  16773. /*
  16774. @@ -1473,6 +1500,15 @@
  16775. static int console_trylock_for_printk(void)
  16776. {
  16777. unsigned int cpu = smp_processor_id();
  16778. +#ifdef CONFIG_PREEMPT_RT_FULL
  16779. + int lock = !early_boot_irqs_disabled && (preempt_count() == 0) &&
  16780. + !irqs_disabled();
  16781. +#else
  16782. + int lock = 1;
  16783. +#endif
  16784. +
  16785. + if (!lock)
  16786. + return 0;
  16787. if (!console_trylock())
  16788. return 0;
  16789. @@ -1607,6 +1643,62 @@
  16790. return textlen;
  16791. }
  16792. +#ifdef CONFIG_EARLY_PRINTK
  16793. +struct console *early_console;
  16794. +
  16795. +static void early_vprintk(const char *fmt, va_list ap)
  16796. +{
  16797. + if (early_console) {
  16798. + char buf[512];
  16799. + int n = vscnprintf(buf, sizeof(buf), fmt, ap);
  16800. +
  16801. + early_console->write(early_console, buf, n);
  16802. + }
  16803. +}
  16804. +
  16805. +asmlinkage void early_printk(const char *fmt, ...)
  16806. +{
  16807. + va_list ap;
  16808. +
  16809. + va_start(ap, fmt);
  16810. + early_vprintk(fmt, ap);
  16811. + va_end(ap);
  16812. +}
  16813. +
  16814. +/*
  16815. + * This is independent of any log levels - a global
  16816. + * kill switch that turns off all of printk.
  16817. + *
  16818. + * Used by the NMI watchdog if early-printk is enabled.
  16819. + */
  16820. +static bool __read_mostly printk_killswitch;
  16821. +
  16822. +static int __init force_early_printk_setup(char *str)
  16823. +{
  16824. + printk_killswitch = true;
  16825. + return 0;
  16826. +}
  16827. +early_param("force_early_printk", force_early_printk_setup);
  16828. +
  16829. +void printk_kill(void)
  16830. +{
  16831. + printk_killswitch = true;
  16832. +}
  16833. +
  16834. +static int forced_early_printk(const char *fmt, va_list ap)
  16835. +{
  16836. + if (!printk_killswitch)
  16837. + return 0;
  16838. + early_vprintk(fmt, ap);
  16839. + return 1;
  16840. +}
  16841. +#else
  16842. +static inline int forced_early_printk(const char *fmt, va_list ap)
  16843. +{
  16844. + return 0;
  16845. +}
  16846. +#endif
  16847. +
  16848. asmlinkage int vprintk_emit(int facility, int level,
  16849. const char *dict, size_t dictlen,
  16850. const char *fmt, va_list args)
  16851. @@ -1623,6 +1715,13 @@
  16852. /* cpu currently holding logbuf_lock in this function */
  16853. static unsigned int logbuf_cpu = UINT_MAX;
  16854. + /*
  16855. + * Fall back to early_printk if a debugging subsystem has
  16856. + * killed printk output
  16857. + */
  16858. + if (unlikely(forced_early_printk(fmt, args)))
  16859. + return 1;
  16860. +
  16861. if (level == LOGLEVEL_SCHED) {
  16862. level = LOGLEVEL_DEFAULT;
  16863. in_sched = true;
  16864. @@ -1764,8 +1863,7 @@
  16865. * console_sem which would prevent anyone from printing to
  16866. * console
  16867. */
  16868. - preempt_disable();
  16869. -
  16870. + migrate_disable();
  16871. /*
  16872. * Try to acquire and then immediately release the console
  16873. * semaphore. The release will print out buffers and wake up
  16874. @@ -1773,7 +1871,7 @@
  16875. */
  16876. if (console_trylock_for_printk())
  16877. console_unlock();
  16878. - preempt_enable();
  16879. + migrate_enable();
  16880. lockdep_on();
  16881. }
  16882. @@ -1902,26 +2000,6 @@
  16883. #endif /* CONFIG_PRINTK */
  16884. -#ifdef CONFIG_EARLY_PRINTK
  16885. -struct console *early_console;
  16886. -
  16887. -asmlinkage __visible void early_printk(const char *fmt, ...)
  16888. -{
  16889. - va_list ap;
  16890. - char buf[512];
  16891. - int n;
  16892. -
  16893. - if (!early_console)
  16894. - return;
  16895. -
  16896. - va_start(ap, fmt);
  16897. - n = vscnprintf(buf, sizeof(buf), fmt, ap);
  16898. - va_end(ap);
  16899. -
  16900. - early_console->write(early_console, buf, n);
  16901. -}
  16902. -#endif
  16903. -
  16904. static int __add_preferred_console(char *name, int idx, char *options,
  16905. char *brl_options)
  16906. {
  16907. @@ -2143,11 +2221,16 @@
  16908. goto out;
  16909. len = cont_print_text(text, size);
  16910. +#ifndef CONFIG_PREEMPT_RT_FULL
  16911. raw_spin_unlock(&logbuf_lock);
  16912. stop_critical_timings();
  16913. call_console_drivers(cont.level, text, len);
  16914. start_critical_timings();
  16915. local_irq_restore(flags);
  16916. +#else
  16917. + raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  16918. + call_console_drivers(cont.level, text, len);
  16919. +#endif
  16920. return;
  16921. out:
  16922. raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  16923. @@ -2246,12 +2329,17 @@
  16924. console_idx = log_next(console_idx);
  16925. console_seq++;
  16926. console_prev = msg->flags;
  16927. +#ifdef CONFIG_PREEMPT_RT_FULL
  16928. + raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  16929. + call_console_drivers(level, text, len);
  16930. +#else
  16931. raw_spin_unlock(&logbuf_lock);
  16932. stop_critical_timings(); /* don't trace print latency */
  16933. call_console_drivers(level, text, len);
  16934. start_critical_timings();
  16935. local_irq_restore(flags);
  16936. +#endif
  16937. if (do_cond_resched)
  16938. cond_resched();
  16939. diff -Nur linux-4.1.20.orig/kernel/ptrace.c linux-4.1.20/kernel/ptrace.c
  16940. --- linux-4.1.20.orig/kernel/ptrace.c 2016-03-17 19:11:03.000000000 +0100
  16941. +++ linux-4.1.20/kernel/ptrace.c 2016-03-21 20:18:33.000000000 +0100
  16942. @@ -129,7 +129,14 @@
  16943. spin_lock_irq(&task->sighand->siglock);
  16944. if (task_is_traced(task) && !__fatal_signal_pending(task)) {
  16945. - task->state = __TASK_TRACED;
  16946. + unsigned long flags;
  16947. +
  16948. + raw_spin_lock_irqsave(&task->pi_lock, flags);
  16949. + if (task->state & __TASK_TRACED)
  16950. + task->state = __TASK_TRACED;
  16951. + else
  16952. + task->saved_state = __TASK_TRACED;
  16953. + raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  16954. ret = true;
  16955. }
  16956. spin_unlock_irq(&task->sighand->siglock);
  16957. diff -Nur linux-4.1.20.orig/kernel/rcu/rcutorture.c linux-4.1.20/kernel/rcu/rcutorture.c
  16958. --- linux-4.1.20.orig/kernel/rcu/rcutorture.c 2016-03-17 19:11:03.000000000 +0100
  16959. +++ linux-4.1.20/kernel/rcu/rcutorture.c 2016-03-21 20:18:33.000000000 +0100
  16960. @@ -389,6 +389,7 @@
  16961. .name = "rcu"
  16962. };
  16963. +#ifndef CONFIG_PREEMPT_RT_FULL
  16964. /*
  16965. * Definitions for rcu_bh torture testing.
  16966. */
  16967. @@ -428,6 +429,12 @@
  16968. .name = "rcu_bh"
  16969. };
  16970. +#else
  16971. +static struct rcu_torture_ops rcu_bh_ops = {
  16972. + .ttype = INVALID_RCU_FLAVOR,
  16973. +};
  16974. +#endif
  16975. +
  16976. /*
  16977. * Don't even think about trying any of these in real life!!!
  16978. * The names includes "busted", and they really means it!
  16979. diff -Nur linux-4.1.20.orig/kernel/rcu/tree.c linux-4.1.20/kernel/rcu/tree.c
  16980. --- linux-4.1.20.orig/kernel/rcu/tree.c 2016-03-17 19:11:03.000000000 +0100
  16981. +++ linux-4.1.20/kernel/rcu/tree.c 2016-03-21 20:18:33.000000000 +0100
  16982. @@ -56,6 +56,11 @@
  16983. #include <linux/random.h>
  16984. #include <linux/ftrace_event.h>
  16985. #include <linux/suspend.h>
  16986. +#include <linux/delay.h>
  16987. +#include <linux/gfp.h>
  16988. +#include <linux/oom.h>
  16989. +#include <linux/smpboot.h>
  16990. +#include "../time/tick-internal.h"
  16991. #include "tree.h"
  16992. #include "rcu.h"
  16993. @@ -220,6 +225,19 @@
  16994. }
  16995. }
  16996. +#ifdef CONFIG_PREEMPT_RT_FULL
  16997. +static void rcu_preempt_qs(void);
  16998. +
  16999. +void rcu_bh_qs(void)
  17000. +{
  17001. + unsigned long flags;
  17002. +
  17003. + /* Callers to this function, rcu_preempt_qs(), must disable irqs. */
  17004. + local_irq_save(flags);
  17005. + rcu_preempt_qs();
  17006. + local_irq_restore(flags);
  17007. +}
  17008. +#else
  17009. void rcu_bh_qs(void)
  17010. {
  17011. if (!__this_cpu_read(rcu_bh_data.passed_quiesce)) {
  17012. @@ -229,6 +247,7 @@
  17013. __this_cpu_write(rcu_bh_data.passed_quiesce, 1);
  17014. }
  17015. }
  17016. +#endif
  17017. static DEFINE_PER_CPU(int, rcu_sched_qs_mask);
  17018. @@ -404,6 +423,7 @@
  17019. }
  17020. EXPORT_SYMBOL_GPL(rcu_batches_completed_sched);
  17021. +#ifndef CONFIG_PREEMPT_RT_FULL
  17022. /*
  17023. * Return the number of RCU BH batches completed thus far for debug & stats.
  17024. */
  17025. @@ -431,6 +451,13 @@
  17026. }
  17027. EXPORT_SYMBOL_GPL(rcu_bh_force_quiescent_state);
  17028. +#else
  17029. +void rcu_force_quiescent_state(void)
  17030. +{
  17031. +}
  17032. +EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
  17033. +#endif
  17034. +
  17035. /*
  17036. * Force a quiescent state for RCU-sched.
  17037. */
  17038. @@ -1545,7 +1572,7 @@
  17039. !ACCESS_ONCE(rsp->gp_flags) ||
  17040. !rsp->gp_kthread)
  17041. return;
  17042. - wake_up(&rsp->gp_wq);
  17043. + swait_wake(&rsp->gp_wq);
  17044. }
  17045. /*
  17046. @@ -1986,7 +2013,7 @@
  17047. ACCESS_ONCE(rsp->gpnum),
  17048. TPS("reqwait"));
  17049. rsp->gp_state = RCU_GP_WAIT_GPS;
  17050. - wait_event_interruptible(rsp->gp_wq,
  17051. + swait_event_interruptible(rsp->gp_wq,
  17052. ACCESS_ONCE(rsp->gp_flags) &
  17053. RCU_GP_FLAG_INIT);
  17054. /* Locking provides needed memory barrier. */
  17055. @@ -2015,7 +2042,7 @@
  17056. ACCESS_ONCE(rsp->gpnum),
  17057. TPS("fqswait"));
  17058. rsp->gp_state = RCU_GP_WAIT_FQS;
  17059. - ret = wait_event_interruptible_timeout(rsp->gp_wq,
  17060. + ret = swait_event_interruptible_timeout(rsp->gp_wq,
  17061. ((gf = ACCESS_ONCE(rsp->gp_flags)) &
  17062. RCU_GP_FLAG_FQS) ||
  17063. (!ACCESS_ONCE(rnp->qsmask) &&
  17064. @@ -2860,18 +2887,17 @@
  17065. /*
  17066. * Do RCU core processing for the current CPU.
  17067. */
  17068. -static void rcu_process_callbacks(struct softirq_action *unused)
  17069. +static void rcu_process_callbacks(void)
  17070. {
  17071. struct rcu_state *rsp;
  17072. if (cpu_is_offline(smp_processor_id()))
  17073. return;
  17074. - trace_rcu_utilization(TPS("Start RCU core"));
  17075. for_each_rcu_flavor(rsp)
  17076. __rcu_process_callbacks(rsp);
  17077. - trace_rcu_utilization(TPS("End RCU core"));
  17078. }
  17079. +static DEFINE_PER_CPU(struct task_struct *, rcu_cpu_kthread_task);
  17080. /*
  17081. * Schedule RCU callback invocation. If the specified type of RCU
  17082. * does not support RCU priority boosting, just do a direct call,
  17083. @@ -2883,18 +2909,105 @@
  17084. {
  17085. if (unlikely(!ACCESS_ONCE(rcu_scheduler_fully_active)))
  17086. return;
  17087. - if (likely(!rsp->boost)) {
  17088. - rcu_do_batch(rsp, rdp);
  17089. + rcu_do_batch(rsp, rdp);
  17090. +}
  17091. +
  17092. +static void rcu_wake_cond(struct task_struct *t, int status)
  17093. +{
  17094. + /*
  17095. + * If the thread is yielding, only wake it when this
  17096. + * is invoked from idle
  17097. + */
  17098. + if (t && (status != RCU_KTHREAD_YIELDING || is_idle_task(current)))
  17099. + wake_up_process(t);
  17100. +}
  17101. +
  17102. +/*
  17103. + * Wake up this CPU's rcuc kthread to do RCU core processing.
  17104. + */
  17105. +static void invoke_rcu_core(void)
  17106. +{
  17107. + unsigned long flags;
  17108. + struct task_struct *t;
  17109. +
  17110. + if (!cpu_online(smp_processor_id()))
  17111. return;
  17112. + local_irq_save(flags);
  17113. + __this_cpu_write(rcu_cpu_has_work, 1);
  17114. + t = __this_cpu_read(rcu_cpu_kthread_task);
  17115. + if (t != NULL && current != t)
  17116. + rcu_wake_cond(t, __this_cpu_read(rcu_cpu_kthread_status));
  17117. + local_irq_restore(flags);
  17118. +}
  17119. +
  17120. +static void rcu_cpu_kthread_park(unsigned int cpu)
  17121. +{
  17122. + per_cpu(rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
  17123. +}
  17124. +
  17125. +static int rcu_cpu_kthread_should_run(unsigned int cpu)
  17126. +{
  17127. + return __this_cpu_read(rcu_cpu_has_work);
  17128. +}
  17129. +
  17130. +/*
  17131. + * Per-CPU kernel thread that invokes RCU callbacks. This replaces the
  17132. + * RCU softirq used in flavors and configurations of RCU that do not
  17133. + * support RCU priority boosting.
  17134. + */
  17135. +static void rcu_cpu_kthread(unsigned int cpu)
  17136. +{
  17137. + unsigned int *statusp = this_cpu_ptr(&rcu_cpu_kthread_status);
  17138. + char work, *workp = this_cpu_ptr(&rcu_cpu_has_work);
  17139. + int spincnt;
  17140. +
  17141. + for (spincnt = 0; spincnt < 10; spincnt++) {
  17142. + trace_rcu_utilization(TPS("Start CPU kthread@rcu_wait"));
  17143. + local_bh_disable();
  17144. + *statusp = RCU_KTHREAD_RUNNING;
  17145. + this_cpu_inc(rcu_cpu_kthread_loops);
  17146. + local_irq_disable();
  17147. + work = *workp;
  17148. + *workp = 0;
  17149. + local_irq_enable();
  17150. + if (work)
  17151. + rcu_process_callbacks();
  17152. + local_bh_enable();
  17153. + if (*workp == 0) {
  17154. + trace_rcu_utilization(TPS("End CPU kthread@rcu_wait"));
  17155. + *statusp = RCU_KTHREAD_WAITING;
  17156. + return;
  17157. + }
  17158. }
  17159. - invoke_rcu_callbacks_kthread();
  17160. + *statusp = RCU_KTHREAD_YIELDING;
  17161. + trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield"));
  17162. + schedule_timeout_interruptible(2);
  17163. + trace_rcu_utilization(TPS("End CPU kthread@rcu_yield"));
  17164. + *statusp = RCU_KTHREAD_WAITING;
  17165. }
  17166. -static void invoke_rcu_core(void)
  17167. +static struct smp_hotplug_thread rcu_cpu_thread_spec = {
  17168. + .store = &rcu_cpu_kthread_task,
  17169. + .thread_should_run = rcu_cpu_kthread_should_run,
  17170. + .thread_fn = rcu_cpu_kthread,
  17171. + .thread_comm = "rcuc/%u",
  17172. + .setup = rcu_cpu_kthread_setup,
  17173. + .park = rcu_cpu_kthread_park,
  17174. +};
  17175. +
  17176. +/*
  17177. + * Spawn per-CPU RCU core processing kthreads.
  17178. + */
  17179. +static int __init rcu_spawn_core_kthreads(void)
  17180. {
  17181. - if (cpu_online(smp_processor_id()))
  17182. - raise_softirq(RCU_SOFTIRQ);
  17183. + int cpu;
  17184. +
  17185. + for_each_possible_cpu(cpu)
  17186. + per_cpu(rcu_cpu_has_work, cpu) = 0;
  17187. + BUG_ON(smpboot_register_percpu_thread(&rcu_cpu_thread_spec));
  17188. + return 0;
  17189. }
  17190. +early_initcall(rcu_spawn_core_kthreads);
  17191. /*
  17192. * Handle any core-RCU processing required by a call_rcu() invocation.
  17193. @@ -3040,6 +3153,7 @@
  17194. }
  17195. EXPORT_SYMBOL_GPL(call_rcu_sched);
  17196. +#ifndef CONFIG_PREEMPT_RT_FULL
  17197. /*
  17198. * Queue an RCU callback for invocation after a quicker grace period.
  17199. */
  17200. @@ -3048,6 +3162,7 @@
  17201. __call_rcu(head, func, &rcu_bh_state, -1, 0);
  17202. }
  17203. EXPORT_SYMBOL_GPL(call_rcu_bh);
  17204. +#endif
  17205. /*
  17206. * Queue an RCU callback for lazy invocation after a grace period.
  17207. @@ -3139,6 +3254,7 @@
  17208. }
  17209. EXPORT_SYMBOL_GPL(synchronize_sched);
  17210. +#ifndef CONFIG_PREEMPT_RT_FULL
  17211. /**
  17212. * synchronize_rcu_bh - wait until an rcu_bh grace period has elapsed.
  17213. *
  17214. @@ -3165,6 +3281,7 @@
  17215. wait_rcu_gp(call_rcu_bh);
  17216. }
  17217. EXPORT_SYMBOL_GPL(synchronize_rcu_bh);
  17218. +#endif
  17219. /**
  17220. * get_state_synchronize_rcu - Snapshot current RCU state
  17221. @@ -3677,6 +3794,7 @@
  17222. mutex_unlock(&rsp->barrier_mutex);
  17223. }
  17224. +#ifndef CONFIG_PREEMPT_RT_FULL
  17225. /**
  17226. * rcu_barrier_bh - Wait until all in-flight call_rcu_bh() callbacks complete.
  17227. */
  17228. @@ -3685,6 +3803,7 @@
  17229. _rcu_barrier(&rcu_bh_state);
  17230. }
  17231. EXPORT_SYMBOL_GPL(rcu_barrier_bh);
  17232. +#endif
  17233. /**
  17234. * rcu_barrier_sched - Wait for in-flight call_rcu_sched() callbacks.
  17235. @@ -4021,7 +4140,7 @@
  17236. }
  17237. }
  17238. - init_waitqueue_head(&rsp->gp_wq);
  17239. + init_swait_head(&rsp->gp_wq);
  17240. rnp = rsp->level[rcu_num_lvls - 1];
  17241. for_each_possible_cpu(i) {
  17242. while (i > rnp->grphi)
  17243. @@ -4120,7 +4239,6 @@
  17244. rcu_init_one(&rcu_bh_state, &rcu_bh_data);
  17245. rcu_init_one(&rcu_sched_state, &rcu_sched_data);
  17246. __rcu_init_preempt();
  17247. - open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
  17248. /*
  17249. * We don't need protection against CPU-hotplug here because
  17250. diff -Nur linux-4.1.20.orig/kernel/rcu/tree.h linux-4.1.20/kernel/rcu/tree.h
  17251. --- linux-4.1.20.orig/kernel/rcu/tree.h 2016-03-17 19:11:03.000000000 +0100
  17252. +++ linux-4.1.20/kernel/rcu/tree.h 2016-03-21 20:18:33.000000000 +0100
  17253. @@ -27,6 +27,7 @@
  17254. #include <linux/threads.h>
  17255. #include <linux/cpumask.h>
  17256. #include <linux/seqlock.h>
  17257. +#include <linux/wait-simple.h>
  17258. /*
  17259. * Define shape of hierarchy based on NR_CPUS, CONFIG_RCU_FANOUT, and
  17260. @@ -210,7 +211,7 @@
  17261. /* This can happen due to race conditions. */
  17262. #endif /* #ifdef CONFIG_RCU_BOOST */
  17263. #ifdef CONFIG_RCU_NOCB_CPU
  17264. - wait_queue_head_t nocb_gp_wq[2];
  17265. + struct swait_head nocb_gp_wq[2];
  17266. /* Place for rcu_nocb_kthread() to wait GP. */
  17267. #endif /* #ifdef CONFIG_RCU_NOCB_CPU */
  17268. int need_future_gp[2];
  17269. @@ -349,7 +350,7 @@
  17270. atomic_long_t nocb_q_count_lazy; /* invocation (all stages). */
  17271. struct rcu_head *nocb_follower_head; /* CBs ready to invoke. */
  17272. struct rcu_head **nocb_follower_tail;
  17273. - wait_queue_head_t nocb_wq; /* For nocb kthreads to sleep on. */
  17274. + struct swait_head nocb_wq; /* For nocb kthreads to sleep on. */
  17275. struct task_struct *nocb_kthread;
  17276. int nocb_defer_wakeup; /* Defer wakeup of nocb_kthread. */
  17277. @@ -438,7 +439,7 @@
  17278. unsigned long gpnum; /* Current gp number. */
  17279. unsigned long completed; /* # of last completed gp. */
  17280. struct task_struct *gp_kthread; /* Task for grace periods. */
  17281. - wait_queue_head_t gp_wq; /* Where GP task waits. */
  17282. + struct swait_head gp_wq; /* Where GP task waits. */
  17283. short gp_flags; /* Commands for GP task. */
  17284. short gp_state; /* GP kthread sleep state. */
  17285. @@ -529,12 +530,10 @@
  17286. DECLARE_PER_CPU(struct rcu_data, rcu_preempt_data);
  17287. #endif /* #ifdef CONFIG_PREEMPT_RCU */
  17288. -#ifdef CONFIG_RCU_BOOST
  17289. DECLARE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
  17290. DECLARE_PER_CPU(int, rcu_cpu_kthread_cpu);
  17291. DECLARE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
  17292. DECLARE_PER_CPU(char, rcu_cpu_has_work);
  17293. -#endif /* #ifdef CONFIG_RCU_BOOST */
  17294. #ifndef RCU_TREE_NONCORE
  17295. @@ -553,10 +552,9 @@
  17296. static void __init __rcu_init_preempt(void);
  17297. static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags);
  17298. static void rcu_preempt_boost_start_gp(struct rcu_node *rnp);
  17299. -static void invoke_rcu_callbacks_kthread(void);
  17300. static bool rcu_is_callbacks_kthread(void);
  17301. +static void rcu_cpu_kthread_setup(unsigned int cpu);
  17302. #ifdef CONFIG_RCU_BOOST
  17303. -static void rcu_preempt_do_callbacks(void);
  17304. static int rcu_spawn_one_boost_kthread(struct rcu_state *rsp,
  17305. struct rcu_node *rnp);
  17306. #endif /* #ifdef CONFIG_RCU_BOOST */
  17307. diff -Nur linux-4.1.20.orig/kernel/rcu/tree_plugin.h linux-4.1.20/kernel/rcu/tree_plugin.h
  17308. --- linux-4.1.20.orig/kernel/rcu/tree_plugin.h 2016-03-17 19:11:03.000000000 +0100
  17309. +++ linux-4.1.20/kernel/rcu/tree_plugin.h 2016-03-21 20:18:33.000000000 +0100
  17310. @@ -24,27 +24,20 @@
  17311. * Paul E. McKenney <paulmck@linux.vnet.ibm.com>
  17312. */
  17313. -#include <linux/delay.h>
  17314. -#include <linux/gfp.h>
  17315. -#include <linux/oom.h>
  17316. -#include <linux/smpboot.h>
  17317. -#include "../time/tick-internal.h"
  17318. -
  17319. #ifdef CONFIG_RCU_BOOST
  17320. #include "../locking/rtmutex_common.h"
  17321. +#endif /* #ifdef CONFIG_RCU_BOOST */
  17322. +
  17323. /*
  17324. * Control variables for per-CPU and per-rcu_node kthreads. These
  17325. * handle all flavors of RCU.
  17326. */
  17327. -static DEFINE_PER_CPU(struct task_struct *, rcu_cpu_kthread_task);
  17328. DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
  17329. DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
  17330. DEFINE_PER_CPU(char, rcu_cpu_has_work);
  17331. -#endif /* #ifdef CONFIG_RCU_BOOST */
  17332. -
  17333. #ifdef CONFIG_RCU_NOCB_CPU
  17334. static cpumask_var_t rcu_nocb_mask; /* CPUs to have callbacks offloaded. */
  17335. static bool have_rcu_nocb_mask; /* Was rcu_nocb_mask allocated? */
  17336. @@ -291,7 +284,7 @@
  17337. }
  17338. /* Hardware IRQ handlers cannot block, complain if they get here. */
  17339. - if (in_irq() || in_serving_softirq()) {
  17340. + if (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_OFFSET)) {
  17341. lockdep_rcu_suspicious(__FILE__, __LINE__,
  17342. "rcu_read_unlock() from irq or softirq with blocking in critical section!!!\n");
  17343. pr_alert("->rcu_read_unlock_special: %#x (b: %d, nq: %d)\n",
  17344. @@ -496,15 +489,6 @@
  17345. t->rcu_read_unlock_special.b.need_qs = true;
  17346. }
  17347. -#ifdef CONFIG_RCU_BOOST
  17348. -
  17349. -static void rcu_preempt_do_callbacks(void)
  17350. -{
  17351. - rcu_do_batch(&rcu_preempt_state, this_cpu_ptr(&rcu_preempt_data));
  17352. -}
  17353. -
  17354. -#endif /* #ifdef CONFIG_RCU_BOOST */
  17355. -
  17356. /*
  17357. * Queue a preemptible-RCU callback for invocation after a grace period.
  17358. */
  17359. @@ -939,6 +923,19 @@
  17360. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  17361. +/*
  17362. + * If boosting, set rcuc kthreads to realtime priority.
  17363. + */
  17364. +static void rcu_cpu_kthread_setup(unsigned int cpu)
  17365. +{
  17366. +#ifdef CONFIG_RCU_BOOST
  17367. + struct sched_param sp;
  17368. +
  17369. + sp.sched_priority = kthread_prio;
  17370. + sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
  17371. +#endif /* #ifdef CONFIG_RCU_BOOST */
  17372. +}
  17373. +
  17374. #ifdef CONFIG_RCU_BOOST
  17375. #include "../locking/rtmutex_common.h"
  17376. @@ -970,16 +967,6 @@
  17377. #endif /* #else #ifdef CONFIG_RCU_TRACE */
  17378. -static void rcu_wake_cond(struct task_struct *t, int status)
  17379. -{
  17380. - /*
  17381. - * If the thread is yielding, only wake it when this
  17382. - * is invoked from idle
  17383. - */
  17384. - if (status != RCU_KTHREAD_YIELDING || is_idle_task(current))
  17385. - wake_up_process(t);
  17386. -}
  17387. -
  17388. /*
  17389. * Carry out RCU priority boosting on the task indicated by ->exp_tasks
  17390. * or ->boost_tasks, advancing the pointer to the next task in the
  17391. @@ -1125,23 +1112,6 @@
  17392. }
  17393. /*
  17394. - * Wake up the per-CPU kthread to invoke RCU callbacks.
  17395. - */
  17396. -static void invoke_rcu_callbacks_kthread(void)
  17397. -{
  17398. - unsigned long flags;
  17399. -
  17400. - local_irq_save(flags);
  17401. - __this_cpu_write(rcu_cpu_has_work, 1);
  17402. - if (__this_cpu_read(rcu_cpu_kthread_task) != NULL &&
  17403. - current != __this_cpu_read(rcu_cpu_kthread_task)) {
  17404. - rcu_wake_cond(__this_cpu_read(rcu_cpu_kthread_task),
  17405. - __this_cpu_read(rcu_cpu_kthread_status));
  17406. - }
  17407. - local_irq_restore(flags);
  17408. -}
  17409. -
  17410. -/*
  17411. * Is the current CPU running the RCU-callbacks kthread?
  17412. * Caller must have preemption disabled.
  17413. */
  17414. @@ -1196,67 +1166,6 @@
  17415. return 0;
  17416. }
  17417. -static void rcu_kthread_do_work(void)
  17418. -{
  17419. - rcu_do_batch(&rcu_sched_state, this_cpu_ptr(&rcu_sched_data));
  17420. - rcu_do_batch(&rcu_bh_state, this_cpu_ptr(&rcu_bh_data));
  17421. - rcu_preempt_do_callbacks();
  17422. -}
  17423. -
  17424. -static void rcu_cpu_kthread_setup(unsigned int cpu)
  17425. -{
  17426. - struct sched_param sp;
  17427. -
  17428. - sp.sched_priority = kthread_prio;
  17429. - sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
  17430. -}
  17431. -
  17432. -static void rcu_cpu_kthread_park(unsigned int cpu)
  17433. -{
  17434. - per_cpu(rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
  17435. -}
  17436. -
  17437. -static int rcu_cpu_kthread_should_run(unsigned int cpu)
  17438. -{
  17439. - return __this_cpu_read(rcu_cpu_has_work);
  17440. -}
  17441. -
  17442. -/*
  17443. - * Per-CPU kernel thread that invokes RCU callbacks. This replaces the
  17444. - * RCU softirq used in flavors and configurations of RCU that do not
  17445. - * support RCU priority boosting.
  17446. - */
  17447. -static void rcu_cpu_kthread(unsigned int cpu)
  17448. -{
  17449. - unsigned int *statusp = this_cpu_ptr(&rcu_cpu_kthread_status);
  17450. - char work, *workp = this_cpu_ptr(&rcu_cpu_has_work);
  17451. - int spincnt;
  17452. -
  17453. - for (spincnt = 0; spincnt < 10; spincnt++) {
  17454. - trace_rcu_utilization(TPS("Start CPU kthread@rcu_wait"));
  17455. - local_bh_disable();
  17456. - *statusp = RCU_KTHREAD_RUNNING;
  17457. - this_cpu_inc(rcu_cpu_kthread_loops);
  17458. - local_irq_disable();
  17459. - work = *workp;
  17460. - *workp = 0;
  17461. - local_irq_enable();
  17462. - if (work)
  17463. - rcu_kthread_do_work();
  17464. - local_bh_enable();
  17465. - if (*workp == 0) {
  17466. - trace_rcu_utilization(TPS("End CPU kthread@rcu_wait"));
  17467. - *statusp = RCU_KTHREAD_WAITING;
  17468. - return;
  17469. - }
  17470. - }
  17471. - *statusp = RCU_KTHREAD_YIELDING;
  17472. - trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield"));
  17473. - schedule_timeout_interruptible(2);
  17474. - trace_rcu_utilization(TPS("End CPU kthread@rcu_yield"));
  17475. - *statusp = RCU_KTHREAD_WAITING;
  17476. -}
  17477. -
  17478. /*
  17479. * Set the per-rcu_node kthread's affinity to cover all CPUs that are
  17480. * served by the rcu_node in question. The CPU hotplug lock is still
  17481. @@ -1286,26 +1195,12 @@
  17482. free_cpumask_var(cm);
  17483. }
  17484. -static struct smp_hotplug_thread rcu_cpu_thread_spec = {
  17485. - .store = &rcu_cpu_kthread_task,
  17486. - .thread_should_run = rcu_cpu_kthread_should_run,
  17487. - .thread_fn = rcu_cpu_kthread,
  17488. - .thread_comm = "rcuc/%u",
  17489. - .setup = rcu_cpu_kthread_setup,
  17490. - .park = rcu_cpu_kthread_park,
  17491. -};
  17492. -
  17493. /*
  17494. * Spawn boost kthreads -- called as soon as the scheduler is running.
  17495. */
  17496. static void __init rcu_spawn_boost_kthreads(void)
  17497. {
  17498. struct rcu_node *rnp;
  17499. - int cpu;
  17500. -
  17501. - for_each_possible_cpu(cpu)
  17502. - per_cpu(rcu_cpu_has_work, cpu) = 0;
  17503. - BUG_ON(smpboot_register_percpu_thread(&rcu_cpu_thread_spec));
  17504. rcu_for_each_leaf_node(rcu_state_p, rnp)
  17505. (void)rcu_spawn_one_boost_kthread(rcu_state_p, rnp);
  17506. }
  17507. @@ -1328,11 +1223,6 @@
  17508. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  17509. }
  17510. -static void invoke_rcu_callbacks_kthread(void)
  17511. -{
  17512. - WARN_ON_ONCE(1);
  17513. -}
  17514. -
  17515. static bool rcu_is_callbacks_kthread(void)
  17516. {
  17517. return false;
  17518. @@ -1356,7 +1246,7 @@
  17519. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  17520. -#if !defined(CONFIG_RCU_FAST_NO_HZ)
  17521. +#if !defined(CONFIG_RCU_FAST_NO_HZ) || defined(CONFIG_PREEMPT_RT_FULL)
  17522. /*
  17523. * Check to see if any future RCU-related work will need to be done
  17524. @@ -1374,7 +1264,9 @@
  17525. return rcu_cpu_has_callbacks(NULL);
  17526. }
  17527. #endif /* #ifndef CONFIG_RCU_NOCB_CPU_ALL */
  17528. +#endif /* !defined(CONFIG_RCU_FAST_NO_HZ) || defined(CONFIG_PREEMPT_RT_FULL) */
  17529. +#if !defined(CONFIG_RCU_FAST_NO_HZ)
  17530. /*
  17531. * Because we do not have RCU_FAST_NO_HZ, don't bother cleaning up
  17532. * after it.
  17533. @@ -1472,6 +1364,8 @@
  17534. return cbs_ready;
  17535. }
  17536. +#ifndef CONFIG_PREEMPT_RT_FULL
  17537. +
  17538. /*
  17539. * Allow the CPU to enter dyntick-idle mode unless it has callbacks ready
  17540. * to invoke. If the CPU has callbacks, try to advance them. Tell the
  17541. @@ -1512,7 +1406,7 @@
  17542. return 0;
  17543. }
  17544. #endif /* #ifndef CONFIG_RCU_NOCB_CPU_ALL */
  17545. -
  17546. +#endif /* #ifndef CONFIG_PREEMPT_RT_FULL */
  17547. /*
  17548. * Prepare a CPU for idle from an RCU perspective. The first major task
  17549. * is to sense whether nohz mode has been enabled or disabled via sysfs.
  17550. @@ -1859,7 +1753,7 @@
  17551. */
  17552. static void rcu_nocb_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp)
  17553. {
  17554. - wake_up_all(&rnp->nocb_gp_wq[rnp->completed & 0x1]);
  17555. + swait_wake_all(&rnp->nocb_gp_wq[rnp->completed & 0x1]);
  17556. }
  17557. /*
  17558. @@ -1877,8 +1771,8 @@
  17559. static void rcu_init_one_nocb(struct rcu_node *rnp)
  17560. {
  17561. - init_waitqueue_head(&rnp->nocb_gp_wq[0]);
  17562. - init_waitqueue_head(&rnp->nocb_gp_wq[1]);
  17563. + init_swait_head(&rnp->nocb_gp_wq[0]);
  17564. + init_swait_head(&rnp->nocb_gp_wq[1]);
  17565. }
  17566. #ifndef CONFIG_RCU_NOCB_CPU_ALL
  17567. @@ -1903,7 +1797,7 @@
  17568. if (ACCESS_ONCE(rdp_leader->nocb_leader_sleep) || force) {
  17569. /* Prior smp_mb__after_atomic() orders against prior enqueue. */
  17570. ACCESS_ONCE(rdp_leader->nocb_leader_sleep) = false;
  17571. - wake_up(&rdp_leader->nocb_wq);
  17572. + swait_wake(&rdp_leader->nocb_wq);
  17573. }
  17574. }
  17575. @@ -2116,7 +2010,7 @@
  17576. */
  17577. trace_rcu_future_gp(rnp, rdp, c, TPS("StartWait"));
  17578. for (;;) {
  17579. - wait_event_interruptible(
  17580. + swait_event_interruptible(
  17581. rnp->nocb_gp_wq[c & 0x1],
  17582. (d = ULONG_CMP_GE(ACCESS_ONCE(rnp->completed), c)));
  17583. if (likely(d))
  17584. @@ -2144,7 +2038,7 @@
  17585. /* Wait for callbacks to appear. */
  17586. if (!rcu_nocb_poll) {
  17587. trace_rcu_nocb_wake(my_rdp->rsp->name, my_rdp->cpu, "Sleep");
  17588. - wait_event_interruptible(my_rdp->nocb_wq,
  17589. + swait_event_interruptible(my_rdp->nocb_wq,
  17590. !ACCESS_ONCE(my_rdp->nocb_leader_sleep));
  17591. /* Memory barrier handled by smp_mb() calls below and repoll. */
  17592. } else if (firsttime) {
  17593. @@ -2219,7 +2113,7 @@
  17594. * List was empty, wake up the follower.
  17595. * Memory barriers supplied by atomic_long_add().
  17596. */
  17597. - wake_up(&rdp->nocb_wq);
  17598. + swait_wake(&rdp->nocb_wq);
  17599. }
  17600. }
  17601. @@ -2240,7 +2134,7 @@
  17602. if (!rcu_nocb_poll) {
  17603. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  17604. "FollowerSleep");
  17605. - wait_event_interruptible(rdp->nocb_wq,
  17606. + swait_event_interruptible(rdp->nocb_wq,
  17607. ACCESS_ONCE(rdp->nocb_follower_head));
  17608. } else if (firsttime) {
  17609. /* Don't drown trace log with "Poll"! */
  17610. @@ -2399,7 +2293,7 @@
  17611. static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
  17612. {
  17613. rdp->nocb_tail = &rdp->nocb_head;
  17614. - init_waitqueue_head(&rdp->nocb_wq);
  17615. + init_swait_head(&rdp->nocb_wq);
  17616. rdp->nocb_follower_tail = &rdp->nocb_follower_head;
  17617. }
  17618. diff -Nur linux-4.1.20.orig/kernel/rcu/update.c linux-4.1.20/kernel/rcu/update.c
  17619. --- linux-4.1.20.orig/kernel/rcu/update.c 2016-03-17 19:11:03.000000000 +0100
  17620. +++ linux-4.1.20/kernel/rcu/update.c 2016-03-21 20:18:33.000000000 +0100
  17621. @@ -227,6 +227,7 @@
  17622. }
  17623. EXPORT_SYMBOL_GPL(rcu_read_lock_held);
  17624. +#ifndef CONFIG_PREEMPT_RT_FULL
  17625. /**
  17626. * rcu_read_lock_bh_held() - might we be in RCU-bh read-side critical section?
  17627. *
  17628. @@ -253,6 +254,7 @@
  17629. return in_softirq() || irqs_disabled();
  17630. }
  17631. EXPORT_SYMBOL_GPL(rcu_read_lock_bh_held);
  17632. +#endif
  17633. #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  17634. diff -Nur linux-4.1.20.orig/kernel/relay.c linux-4.1.20/kernel/relay.c
  17635. --- linux-4.1.20.orig/kernel/relay.c 2016-03-17 19:11:03.000000000 +0100
  17636. +++ linux-4.1.20/kernel/relay.c 2016-03-21 20:18:33.000000000 +0100
  17637. @@ -339,6 +339,10 @@
  17638. {
  17639. struct rchan_buf *buf = (struct rchan_buf *)data;
  17640. wake_up_interruptible(&buf->read_wait);
  17641. + /*
  17642. + * Stupid polling for now:
  17643. + */
  17644. + mod_timer(&buf->timer, jiffies + 1);
  17645. }
  17646. /**
  17647. @@ -356,6 +360,7 @@
  17648. init_waitqueue_head(&buf->read_wait);
  17649. kref_init(&buf->kref);
  17650. setup_timer(&buf->timer, wakeup_readers, (unsigned long)buf);
  17651. + mod_timer(&buf->timer, jiffies + 1);
  17652. } else
  17653. del_timer_sync(&buf->timer);
  17654. @@ -739,15 +744,6 @@
  17655. else
  17656. buf->early_bytes += buf->chan->subbuf_size -
  17657. buf->padding[old_subbuf];
  17658. - smp_mb();
  17659. - if (waitqueue_active(&buf->read_wait))
  17660. - /*
  17661. - * Calling wake_up_interruptible() from here
  17662. - * will deadlock if we happen to be logging
  17663. - * from the scheduler (trying to re-grab
  17664. - * rq->lock), so defer it.
  17665. - */
  17666. - mod_timer(&buf->timer, jiffies + 1);
  17667. }
  17668. old = buf->data;
  17669. diff -Nur linux-4.1.20.orig/kernel/sched/Makefile linux-4.1.20/kernel/sched/Makefile
  17670. --- linux-4.1.20.orig/kernel/sched/Makefile 2016-03-17 19:11:03.000000000 +0100
  17671. +++ linux-4.1.20/kernel/sched/Makefile 2016-03-21 20:18:33.000000000 +0100
  17672. @@ -13,7 +13,7 @@
  17673. obj-y += core.o proc.o clock.o cputime.o
  17674. obj-y += idle_task.o fair.o rt.o deadline.o stop_task.o
  17675. -obj-y += wait.o completion.o idle.o
  17676. +obj-y += wait.o wait-simple.o work-simple.o completion.o idle.o
  17677. obj-$(CONFIG_SMP) += cpupri.o cpudeadline.o
  17678. obj-$(CONFIG_SCHED_AUTOGROUP) += auto_group.o
  17679. obj-$(CONFIG_SCHEDSTATS) += stats.o
  17680. diff -Nur linux-4.1.20.orig/kernel/sched/completion.c linux-4.1.20/kernel/sched/completion.c
  17681. --- linux-4.1.20.orig/kernel/sched/completion.c 2016-03-17 19:11:03.000000000 +0100
  17682. +++ linux-4.1.20/kernel/sched/completion.c 2016-03-21 20:18:33.000000000 +0100
  17683. @@ -30,10 +30,10 @@
  17684. {
  17685. unsigned long flags;
  17686. - spin_lock_irqsave(&x->wait.lock, flags);
  17687. + raw_spin_lock_irqsave(&x->wait.lock, flags);
  17688. x->done++;
  17689. - __wake_up_locked(&x->wait, TASK_NORMAL, 1);
  17690. - spin_unlock_irqrestore(&x->wait.lock, flags);
  17691. + __swait_wake_locked(&x->wait, TASK_NORMAL, 1);
  17692. + raw_spin_unlock_irqrestore(&x->wait.lock, flags);
  17693. }
  17694. EXPORT_SYMBOL(complete);
  17695. @@ -50,10 +50,10 @@
  17696. {
  17697. unsigned long flags;
  17698. - spin_lock_irqsave(&x->wait.lock, flags);
  17699. + raw_spin_lock_irqsave(&x->wait.lock, flags);
  17700. x->done += UINT_MAX/2;
  17701. - __wake_up_locked(&x->wait, TASK_NORMAL, 0);
  17702. - spin_unlock_irqrestore(&x->wait.lock, flags);
  17703. + __swait_wake_locked(&x->wait, TASK_NORMAL, 0);
  17704. + raw_spin_unlock_irqrestore(&x->wait.lock, flags);
  17705. }
  17706. EXPORT_SYMBOL(complete_all);
  17707. @@ -62,20 +62,20 @@
  17708. long (*action)(long), long timeout, int state)
  17709. {
  17710. if (!x->done) {
  17711. - DECLARE_WAITQUEUE(wait, current);
  17712. + DEFINE_SWAITER(wait);
  17713. - __add_wait_queue_tail_exclusive(&x->wait, &wait);
  17714. + swait_prepare_locked(&x->wait, &wait);
  17715. do {
  17716. if (signal_pending_state(state, current)) {
  17717. timeout = -ERESTARTSYS;
  17718. break;
  17719. }
  17720. __set_current_state(state);
  17721. - spin_unlock_irq(&x->wait.lock);
  17722. + raw_spin_unlock_irq(&x->wait.lock);
  17723. timeout = action(timeout);
  17724. - spin_lock_irq(&x->wait.lock);
  17725. + raw_spin_lock_irq(&x->wait.lock);
  17726. } while (!x->done && timeout);
  17727. - __remove_wait_queue(&x->wait, &wait);
  17728. + swait_finish_locked(&x->wait, &wait);
  17729. if (!x->done)
  17730. return timeout;
  17731. }
  17732. @@ -89,9 +89,9 @@
  17733. {
  17734. might_sleep();
  17735. - spin_lock_irq(&x->wait.lock);
  17736. + raw_spin_lock_irq(&x->wait.lock);
  17737. timeout = do_wait_for_common(x, action, timeout, state);
  17738. - spin_unlock_irq(&x->wait.lock);
  17739. + raw_spin_unlock_irq(&x->wait.lock);
  17740. return timeout;
  17741. }
  17742. @@ -277,12 +277,12 @@
  17743. if (!READ_ONCE(x->done))
  17744. return 0;
  17745. - spin_lock_irqsave(&x->wait.lock, flags);
  17746. + raw_spin_lock_irqsave(&x->wait.lock, flags);
  17747. if (!x->done)
  17748. ret = 0;
  17749. else
  17750. x->done--;
  17751. - spin_unlock_irqrestore(&x->wait.lock, flags);
  17752. + raw_spin_unlock_irqrestore(&x->wait.lock, flags);
  17753. return ret;
  17754. }
  17755. EXPORT_SYMBOL(try_wait_for_completion);
  17756. @@ -311,7 +311,7 @@
  17757. * after it's acquired the lock.
  17758. */
  17759. smp_rmb();
  17760. - spin_unlock_wait(&x->wait.lock);
  17761. + raw_spin_unlock_wait(&x->wait.lock);
  17762. return true;
  17763. }
  17764. EXPORT_SYMBOL(completion_done);
  17765. diff -Nur linux-4.1.20.orig/kernel/sched/core.c linux-4.1.20/kernel/sched/core.c
  17766. --- linux-4.1.20.orig/kernel/sched/core.c 2016-03-17 19:11:03.000000000 +0100
  17767. +++ linux-4.1.20/kernel/sched/core.c 2016-03-21 20:18:33.000000000 +0100
  17768. @@ -282,7 +282,11 @@
  17769. * Number of tasks to iterate in a single balance run.
  17770. * Limited because this is done with IRQs disabled.
  17771. */
  17772. +#ifndef CONFIG_PREEMPT_RT_FULL
  17773. const_debug unsigned int sysctl_sched_nr_migrate = 32;
  17774. +#else
  17775. +const_debug unsigned int sysctl_sched_nr_migrate = 8;
  17776. +#endif
  17777. /*
  17778. * period over which we average the RT time consumption, measured
  17779. @@ -461,6 +465,7 @@
  17780. hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  17781. rq->hrtick_timer.function = hrtick;
  17782. + rq->hrtick_timer.irqsafe = 1;
  17783. }
  17784. #else /* CONFIG_SCHED_HRTICK */
  17785. static inline void hrtick_clear(struct rq *rq)
  17786. @@ -541,6 +546,52 @@
  17787. #endif
  17788. #endif
  17789. +void wake_q_add(struct wake_q_head *head, struct task_struct *task)
  17790. +{
  17791. + struct wake_q_node *node = &task->wake_q;
  17792. +
  17793. + /*
  17794. + * Atomically grab the task, if ->wake_q is !nil already it means
  17795. + * its already queued (either by us or someone else) and will get the
  17796. + * wakeup due to that.
  17797. + *
  17798. + * This cmpxchg() implies a full barrier, which pairs with the write
  17799. + * barrier implied by the wakeup in wake_up_list().
  17800. + */
  17801. + if (cmpxchg(&node->next, NULL, WAKE_Q_TAIL))
  17802. + return;
  17803. +
  17804. + get_task_struct(task);
  17805. +
  17806. + /*
  17807. + * The head is context local, there can be no concurrency.
  17808. + */
  17809. + *head->lastp = node;
  17810. + head->lastp = &node->next;
  17811. +}
  17812. +
  17813. +void wake_up_q(struct wake_q_head *head)
  17814. +{
  17815. + struct wake_q_node *node = head->first;
  17816. +
  17817. + while (node != WAKE_Q_TAIL) {
  17818. + struct task_struct *task;
  17819. +
  17820. + task = container_of(node, struct task_struct, wake_q);
  17821. + BUG_ON(!task);
  17822. + /* task can safely be re-inserted now */
  17823. + node = node->next;
  17824. + task->wake_q.next = NULL;
  17825. +
  17826. + /*
  17827. + * wake_up_process() implies a wmb() to pair with the queueing
  17828. + * in wake_q_add() so as not to miss wakeups.
  17829. + */
  17830. + wake_up_process(task);
  17831. + put_task_struct(task);
  17832. + }
  17833. +}
  17834. +
  17835. /*
  17836. * resched_curr - mark rq's current task 'to be rescheduled now'.
  17837. *
  17838. @@ -572,6 +623,38 @@
  17839. trace_sched_wake_idle_without_ipi(cpu);
  17840. }
  17841. +#ifdef CONFIG_PREEMPT_LAZY
  17842. +void resched_curr_lazy(struct rq *rq)
  17843. +{
  17844. + struct task_struct *curr = rq->curr;
  17845. + int cpu;
  17846. +
  17847. + if (!sched_feat(PREEMPT_LAZY)) {
  17848. + resched_curr(rq);
  17849. + return;
  17850. + }
  17851. +
  17852. + lockdep_assert_held(&rq->lock);
  17853. +
  17854. + if (test_tsk_need_resched(curr))
  17855. + return;
  17856. +
  17857. + if (test_tsk_need_resched_lazy(curr))
  17858. + return;
  17859. +
  17860. + set_tsk_need_resched_lazy(curr);
  17861. +
  17862. + cpu = cpu_of(rq);
  17863. + if (cpu == smp_processor_id())
  17864. + return;
  17865. +
  17866. + /* NEED_RESCHED_LAZY must be visible before we test polling */
  17867. + smp_mb();
  17868. + if (!tsk_is_polling(curr))
  17869. + smp_send_reschedule(cpu);
  17870. +}
  17871. +#endif
  17872. +
  17873. void resched_cpu(int cpu)
  17874. {
  17875. struct rq *rq = cpu_rq(cpu);
  17876. @@ -595,12 +678,14 @@
  17877. */
  17878. int get_nohz_timer_target(int pinned)
  17879. {
  17880. - int cpu = smp_processor_id();
  17881. + int cpu;
  17882. int i;
  17883. struct sched_domain *sd;
  17884. + preempt_disable_rt();
  17885. + cpu = smp_processor_id();
  17886. if (pinned || !get_sysctl_timer_migration() || !idle_cpu(cpu))
  17887. - return cpu;
  17888. + goto preempt_en_rt;
  17889. rcu_read_lock();
  17890. for_each_domain(cpu, sd) {
  17891. @@ -613,6 +698,8 @@
  17892. }
  17893. unlock:
  17894. rcu_read_unlock();
  17895. +preempt_en_rt:
  17896. + preempt_enable_rt();
  17897. return cpu;
  17898. }
  17899. /*
  17900. @@ -1164,6 +1251,18 @@
  17901. static int migration_cpu_stop(void *data);
  17902. +static bool check_task_state(struct task_struct *p, long match_state)
  17903. +{
  17904. + bool match = false;
  17905. +
  17906. + raw_spin_lock_irq(&p->pi_lock);
  17907. + if (p->state == match_state || p->saved_state == match_state)
  17908. + match = true;
  17909. + raw_spin_unlock_irq(&p->pi_lock);
  17910. +
  17911. + return match;
  17912. +}
  17913. +
  17914. /*
  17915. * wait_task_inactive - wait for a thread to unschedule.
  17916. *
  17917. @@ -1208,7 +1307,7 @@
  17918. * is actually now running somewhere else!
  17919. */
  17920. while (task_running(rq, p)) {
  17921. - if (match_state && unlikely(p->state != match_state))
  17922. + if (match_state && !check_task_state(p, match_state))
  17923. return 0;
  17924. cpu_relax();
  17925. }
  17926. @@ -1223,7 +1322,8 @@
  17927. running = task_running(rq, p);
  17928. queued = task_on_rq_queued(p);
  17929. ncsw = 0;
  17930. - if (!match_state || p->state == match_state)
  17931. + if (!match_state || p->state == match_state ||
  17932. + p->saved_state == match_state)
  17933. ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
  17934. task_rq_unlock(rq, p, &flags);
  17935. @@ -1449,10 +1549,6 @@
  17936. {
  17937. activate_task(rq, p, en_flags);
  17938. p->on_rq = TASK_ON_RQ_QUEUED;
  17939. -
  17940. - /* if a worker is waking up, notify workqueue */
  17941. - if (p->flags & PF_WQ_WORKER)
  17942. - wq_worker_waking_up(p, cpu_of(rq));
  17943. }
  17944. /*
  17945. @@ -1462,9 +1558,9 @@
  17946. ttwu_do_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
  17947. {
  17948. check_preempt_curr(rq, p, wake_flags);
  17949. - trace_sched_wakeup(p, true);
  17950. -
  17951. p->state = TASK_RUNNING;
  17952. + trace_sched_wakeup(p);
  17953. +
  17954. #ifdef CONFIG_SMP
  17955. if (p->sched_class->task_woken)
  17956. p->sched_class->task_woken(rq, p);
  17957. @@ -1666,8 +1762,29 @@
  17958. */
  17959. smp_mb__before_spinlock();
  17960. raw_spin_lock_irqsave(&p->pi_lock, flags);
  17961. - if (!(p->state & state))
  17962. + if (!(p->state & state)) {
  17963. + /*
  17964. + * The task might be running due to a spinlock sleeper
  17965. + * wakeup. Check the saved state and set it to running
  17966. + * if the wakeup condition is true.
  17967. + */
  17968. + if (!(wake_flags & WF_LOCK_SLEEPER)) {
  17969. + if (p->saved_state & state) {
  17970. + p->saved_state = TASK_RUNNING;
  17971. + success = 1;
  17972. + }
  17973. + }
  17974. goto out;
  17975. + }
  17976. +
  17977. + /*
  17978. + * If this is a regular wakeup, then we can unconditionally
  17979. + * clear the saved state of a "lock sleeper".
  17980. + */
  17981. + if (!(wake_flags & WF_LOCK_SLEEPER))
  17982. + p->saved_state = TASK_RUNNING;
  17983. +
  17984. + trace_sched_waking(p);
  17985. success = 1; /* we're going to change ->state */
  17986. cpu = task_cpu(p);
  17987. @@ -1710,42 +1827,6 @@
  17988. }
  17989. /**
  17990. - * try_to_wake_up_local - try to wake up a local task with rq lock held
  17991. - * @p: the thread to be awakened
  17992. - *
  17993. - * Put @p on the run-queue if it's not already there. The caller must
  17994. - * ensure that this_rq() is locked, @p is bound to this_rq() and not
  17995. - * the current task.
  17996. - */
  17997. -static void try_to_wake_up_local(struct task_struct *p)
  17998. -{
  17999. - struct rq *rq = task_rq(p);
  18000. -
  18001. - if (WARN_ON_ONCE(rq != this_rq()) ||
  18002. - WARN_ON_ONCE(p == current))
  18003. - return;
  18004. -
  18005. - lockdep_assert_held(&rq->lock);
  18006. -
  18007. - if (!raw_spin_trylock(&p->pi_lock)) {
  18008. - raw_spin_unlock(&rq->lock);
  18009. - raw_spin_lock(&p->pi_lock);
  18010. - raw_spin_lock(&rq->lock);
  18011. - }
  18012. -
  18013. - if (!(p->state & TASK_NORMAL))
  18014. - goto out;
  18015. -
  18016. - if (!task_on_rq_queued(p))
  18017. - ttwu_activate(rq, p, ENQUEUE_WAKEUP);
  18018. -
  18019. - ttwu_do_wakeup(rq, p, 0);
  18020. - ttwu_stat(p, smp_processor_id(), 0);
  18021. -out:
  18022. - raw_spin_unlock(&p->pi_lock);
  18023. -}
  18024. -
  18025. -/**
  18026. * wake_up_process - Wake up a specific process
  18027. * @p: The process to be woken up.
  18028. *
  18029. @@ -1759,11 +1840,23 @@
  18030. */
  18031. int wake_up_process(struct task_struct *p)
  18032. {
  18033. - WARN_ON(task_is_stopped_or_traced(p));
  18034. + WARN_ON(__task_is_stopped_or_traced(p));
  18035. return try_to_wake_up(p, TASK_NORMAL, 0);
  18036. }
  18037. EXPORT_SYMBOL(wake_up_process);
  18038. +/**
  18039. + * wake_up_lock_sleeper - Wake up a specific process blocked on a "sleeping lock"
  18040. + * @p: The process to be woken up.
  18041. + *
  18042. + * Same as wake_up_process() above, but wake_flags=WF_LOCK_SLEEPER to indicate
  18043. + * the nature of the wakeup.
  18044. + */
  18045. +int wake_up_lock_sleeper(struct task_struct *p)
  18046. +{
  18047. + return try_to_wake_up(p, TASK_ALL, WF_LOCK_SLEEPER);
  18048. +}
  18049. +
  18050. int wake_up_state(struct task_struct *p, unsigned int state)
  18051. {
  18052. return try_to_wake_up(p, state, 0);
  18053. @@ -1959,6 +2052,9 @@
  18054. p->on_cpu = 0;
  18055. #endif
  18056. init_task_preempt_count(p);
  18057. +#ifdef CONFIG_HAVE_PREEMPT_LAZY
  18058. + task_thread_info(p)->preempt_lazy_count = 0;
  18059. +#endif
  18060. #ifdef CONFIG_SMP
  18061. plist_node_init(&p->pushable_tasks, MAX_PRIO);
  18062. RB_CLEAR_NODE(&p->pushable_dl_tasks);
  18063. @@ -2094,7 +2190,7 @@
  18064. rq = __task_rq_lock(p);
  18065. activate_task(rq, p, 0);
  18066. p->on_rq = TASK_ON_RQ_QUEUED;
  18067. - trace_sched_wakeup_new(p, true);
  18068. + trace_sched_wakeup_new(p);
  18069. check_preempt_curr(rq, p, WF_FORK);
  18070. #ifdef CONFIG_SMP
  18071. if (p->sched_class->task_woken)
  18072. @@ -2231,8 +2327,12 @@
  18073. finish_arch_post_lock_switch();
  18074. fire_sched_in_preempt_notifiers(current);
  18075. + /*
  18076. + * We use mmdrop_delayed() here so we don't have to do the
  18077. + * full __mmdrop() when we are the last user.
  18078. + */
  18079. if (mm)
  18080. - mmdrop(mm);
  18081. + mmdrop_delayed(mm);
  18082. if (unlikely(prev_state == TASK_DEAD)) {
  18083. if (prev->sched_class->task_dead)
  18084. prev->sched_class->task_dead(prev);
  18085. @@ -2543,16 +2643,6 @@
  18086. }
  18087. #endif
  18088. -notrace unsigned long get_parent_ip(unsigned long addr)
  18089. -{
  18090. - if (in_lock_functions(addr)) {
  18091. - addr = CALLER_ADDR2;
  18092. - if (in_lock_functions(addr))
  18093. - addr = CALLER_ADDR3;
  18094. - }
  18095. - return addr;
  18096. -}
  18097. -
  18098. #if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
  18099. defined(CONFIG_PREEMPT_TRACER))
  18100. @@ -2574,7 +2664,7 @@
  18101. PREEMPT_MASK - 10);
  18102. #endif
  18103. if (preempt_count() == val) {
  18104. - unsigned long ip = get_parent_ip(CALLER_ADDR1);
  18105. + unsigned long ip = get_lock_parent_ip();
  18106. #ifdef CONFIG_DEBUG_PREEMPT
  18107. current->preempt_disable_ip = ip;
  18108. #endif
  18109. @@ -2601,7 +2691,7 @@
  18110. #endif
  18111. if (preempt_count() == val)
  18112. - trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
  18113. + trace_preempt_on(CALLER_ADDR0, get_lock_parent_ip());
  18114. __preempt_count_sub(val);
  18115. }
  18116. EXPORT_SYMBOL(preempt_count_sub);
  18117. @@ -2657,6 +2747,133 @@
  18118. schedstat_inc(this_rq(), sched_count);
  18119. }
  18120. +#if defined(CONFIG_PREEMPT_RT_FULL) && defined(CONFIG_SMP)
  18121. +#define MIGRATE_DISABLE_SET_AFFIN (1<<30) /* Can't make a negative */
  18122. +#define migrate_disabled_updated(p) ((p)->migrate_disable & MIGRATE_DISABLE_SET_AFFIN)
  18123. +#define migrate_disable_count(p) ((p)->migrate_disable & ~MIGRATE_DISABLE_SET_AFFIN)
  18124. +
  18125. +static inline void update_migrate_disable(struct task_struct *p)
  18126. +{
  18127. + const struct cpumask *mask;
  18128. +
  18129. + if (likely(!p->migrate_disable))
  18130. + return;
  18131. +
  18132. + /* Did we already update affinity? */
  18133. + if (unlikely(migrate_disabled_updated(p)))
  18134. + return;
  18135. +
  18136. + /*
  18137. + * Since this is always current we can get away with only locking
  18138. + * rq->lock, the ->cpus_allowed value can normally only be changed
  18139. + * while holding both p->pi_lock and rq->lock, but seeing that this
  18140. + * is current, we cannot actually be waking up, so all code that
  18141. + * relies on serialization against p->pi_lock is out of scope.
  18142. + *
  18143. + * Having rq->lock serializes us against things like
  18144. + * set_cpus_allowed_ptr() that can still happen concurrently.
  18145. + */
  18146. + mask = tsk_cpus_allowed(p);
  18147. +
  18148. + if (p->sched_class->set_cpus_allowed)
  18149. + p->sched_class->set_cpus_allowed(p, mask);
  18150. + /* mask==cpumask_of(task_cpu(p)) which has a cpumask_weight==1 */
  18151. + p->nr_cpus_allowed = 1;
  18152. +
  18153. + /* Let migrate_enable know to fix things back up */
  18154. + p->migrate_disable |= MIGRATE_DISABLE_SET_AFFIN;
  18155. +}
  18156. +
  18157. +void migrate_disable(void)
  18158. +{
  18159. + struct task_struct *p = current;
  18160. +
  18161. + if (in_atomic() || irqs_disabled()) {
  18162. +#ifdef CONFIG_SCHED_DEBUG
  18163. + p->migrate_disable_atomic++;
  18164. +#endif
  18165. + return;
  18166. + }
  18167. +
  18168. +#ifdef CONFIG_SCHED_DEBUG
  18169. + if (unlikely(p->migrate_disable_atomic)) {
  18170. + tracing_off();
  18171. + WARN_ON_ONCE(1);
  18172. + }
  18173. +#endif
  18174. +
  18175. + if (p->migrate_disable) {
  18176. + p->migrate_disable++;
  18177. + return;
  18178. + }
  18179. +
  18180. + preempt_disable();
  18181. + preempt_lazy_disable();
  18182. + pin_current_cpu();
  18183. + p->migrate_disable = 1;
  18184. + preempt_enable();
  18185. +}
  18186. +EXPORT_SYMBOL(migrate_disable);
  18187. +
  18188. +void migrate_enable(void)
  18189. +{
  18190. + struct task_struct *p = current;
  18191. + const struct cpumask *mask;
  18192. + unsigned long flags;
  18193. + struct rq *rq;
  18194. +
  18195. + if (in_atomic() || irqs_disabled()) {
  18196. +#ifdef CONFIG_SCHED_DEBUG
  18197. + p->migrate_disable_atomic--;
  18198. +#endif
  18199. + return;
  18200. + }
  18201. +
  18202. +#ifdef CONFIG_SCHED_DEBUG
  18203. + if (unlikely(p->migrate_disable_atomic)) {
  18204. + tracing_off();
  18205. + WARN_ON_ONCE(1);
  18206. + }
  18207. +#endif
  18208. + WARN_ON_ONCE(p->migrate_disable <= 0);
  18209. +
  18210. + if (migrate_disable_count(p) > 1) {
  18211. + p->migrate_disable--;
  18212. + return;
  18213. + }
  18214. +
  18215. + preempt_disable();
  18216. + if (unlikely(migrate_disabled_updated(p))) {
  18217. + /*
  18218. + * Undo whatever update_migrate_disable() did, also see there
  18219. + * about locking.
  18220. + */
  18221. + rq = this_rq();
  18222. + raw_spin_lock_irqsave(&rq->lock, flags);
  18223. +
  18224. + /*
  18225. + * Clearing migrate_disable causes tsk_cpus_allowed to
  18226. + * show the tasks original cpu affinity.
  18227. + */
  18228. + p->migrate_disable = 0;
  18229. + mask = tsk_cpus_allowed(p);
  18230. + if (p->sched_class->set_cpus_allowed)
  18231. + p->sched_class->set_cpus_allowed(p, mask);
  18232. + p->nr_cpus_allowed = cpumask_weight(mask);
  18233. + raw_spin_unlock_irqrestore(&rq->lock, flags);
  18234. + } else
  18235. + p->migrate_disable = 0;
  18236. +
  18237. + unpin_current_cpu();
  18238. + preempt_enable();
  18239. + preempt_lazy_enable();
  18240. +}
  18241. +EXPORT_SYMBOL(migrate_enable);
  18242. +#else
  18243. +static inline void update_migrate_disable(struct task_struct *p) { }
  18244. +#define migrate_disabled_updated(p) 0
  18245. +#endif
  18246. +
  18247. /*
  18248. * Pick up the highest-prio task:
  18249. */
  18250. @@ -2763,6 +2980,8 @@
  18251. smp_mb__before_spinlock();
  18252. raw_spin_lock_irq(&rq->lock);
  18253. + update_migrate_disable(prev);
  18254. +
  18255. rq->clock_skip_update <<= 1; /* promote REQ to ACT */
  18256. switch_count = &prev->nivcsw;
  18257. @@ -2772,19 +2991,6 @@
  18258. } else {
  18259. deactivate_task(rq, prev, DEQUEUE_SLEEP);
  18260. prev->on_rq = 0;
  18261. -
  18262. - /*
  18263. - * If a worker went to sleep, notify and ask workqueue
  18264. - * whether it wants to wake up a task to maintain
  18265. - * concurrency.
  18266. - */
  18267. - if (prev->flags & PF_WQ_WORKER) {
  18268. - struct task_struct *to_wakeup;
  18269. -
  18270. - to_wakeup = wq_worker_sleeping(prev, cpu);
  18271. - if (to_wakeup)
  18272. - try_to_wake_up_local(to_wakeup);
  18273. - }
  18274. }
  18275. switch_count = &prev->nvcsw;
  18276. }
  18277. @@ -2794,6 +3000,7 @@
  18278. next = pick_next_task(rq, prev);
  18279. clear_tsk_need_resched(prev);
  18280. + clear_tsk_need_resched_lazy(prev);
  18281. clear_preempt_need_resched();
  18282. rq->clock_skip_update = 0;
  18283. @@ -2814,8 +3021,19 @@
  18284. static inline void sched_submit_work(struct task_struct *tsk)
  18285. {
  18286. - if (!tsk->state || tsk_is_pi_blocked(tsk))
  18287. + if (!tsk->state)
  18288. + return;
  18289. + /*
  18290. + * If a worker went to sleep, notify and ask workqueue whether
  18291. + * it wants to wake up a task to maintain concurrency.
  18292. + */
  18293. + if (tsk->flags & PF_WQ_WORKER)
  18294. + wq_worker_sleeping(tsk);
  18295. +
  18296. +
  18297. + if (tsk_is_pi_blocked(tsk))
  18298. return;
  18299. +
  18300. /*
  18301. * If we are going to sleep and we have plugged IO queued,
  18302. * make sure to submit it to avoid deadlocks.
  18303. @@ -2824,6 +3042,12 @@
  18304. blk_schedule_flush_plug(tsk);
  18305. }
  18306. +static void sched_update_worker(struct task_struct *tsk)
  18307. +{
  18308. + if (tsk->flags & PF_WQ_WORKER)
  18309. + wq_worker_running(tsk);
  18310. +}
  18311. +
  18312. asmlinkage __visible void __sched schedule(void)
  18313. {
  18314. struct task_struct *tsk = current;
  18315. @@ -2832,6 +3056,7 @@
  18316. do {
  18317. __schedule();
  18318. } while (need_resched());
  18319. + sched_update_worker(tsk);
  18320. }
  18321. EXPORT_SYMBOL(schedule);
  18322. @@ -2881,6 +3106,30 @@
  18323. } while (need_resched());
  18324. }
  18325. +#ifdef CONFIG_PREEMPT_LAZY
  18326. +/*
  18327. + * If TIF_NEED_RESCHED is then we allow to be scheduled away since this is
  18328. + * set by a RT task. Oterwise we try to avoid beeing scheduled out as long as
  18329. + * preempt_lazy_count counter >0.
  18330. + */
  18331. +static __always_inline int preemptible_lazy(void)
  18332. +{
  18333. + if (test_thread_flag(TIF_NEED_RESCHED))
  18334. + return 1;
  18335. + if (current_thread_info()->preempt_lazy_count)
  18336. + return 0;
  18337. + return 1;
  18338. +}
  18339. +
  18340. +#else
  18341. +
  18342. +static int preemptible_lazy(void)
  18343. +{
  18344. + return 1;
  18345. +}
  18346. +
  18347. +#endif
  18348. +
  18349. #ifdef CONFIG_PREEMPT
  18350. /*
  18351. * this is the entry point to schedule() from in-kernel preemption
  18352. @@ -2895,6 +3144,8 @@
  18353. */
  18354. if (likely(!preemptible()))
  18355. return;
  18356. + if (!preemptible_lazy())
  18357. + return;
  18358. preempt_schedule_common();
  18359. }
  18360. @@ -2922,6 +3173,8 @@
  18361. if (likely(!preemptible()))
  18362. return;
  18363. + if (!preemptible_lazy())
  18364. + return;
  18365. do {
  18366. __preempt_count_add(PREEMPT_ACTIVE);
  18367. @@ -2931,7 +3184,16 @@
  18368. * an infinite recursion.
  18369. */
  18370. prev_ctx = exception_enter();
  18371. + /*
  18372. + * The add/subtract must not be traced by the function
  18373. + * tracer. But we still want to account for the
  18374. + * preempt off latency tracer. Since the _notrace versions
  18375. + * of add/subtract skip the accounting for latency tracer
  18376. + * we must force it manually.
  18377. + */
  18378. + start_critical_timings();
  18379. __schedule();
  18380. + stop_critical_timings();
  18381. exception_exit(prev_ctx);
  18382. __preempt_count_sub(PREEMPT_ACTIVE);
  18383. @@ -4268,6 +4530,7 @@
  18384. }
  18385. EXPORT_SYMBOL(__cond_resched_lock);
  18386. +#ifndef CONFIG_PREEMPT_RT_FULL
  18387. int __sched __cond_resched_softirq(void)
  18388. {
  18389. BUG_ON(!in_softirq());
  18390. @@ -4281,6 +4544,7 @@
  18391. return 0;
  18392. }
  18393. EXPORT_SYMBOL(__cond_resched_softirq);
  18394. +#endif
  18395. /**
  18396. * yield - yield the current processor to other threads.
  18397. @@ -4635,7 +4899,9 @@
  18398. /* Set the preempt count _outside_ the spinlocks! */
  18399. init_idle_preempt_count(idle, cpu);
  18400. -
  18401. +#ifdef CONFIG_HAVE_PREEMPT_LAZY
  18402. + task_thread_info(idle)->preempt_lazy_count = 0;
  18403. +#endif
  18404. /*
  18405. * The idle tasks have their own, simple scheduling class:
  18406. */
  18407. @@ -4755,11 +5021,91 @@
  18408. void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
  18409. {
  18410. - if (p->sched_class->set_cpus_allowed)
  18411. - p->sched_class->set_cpus_allowed(p, new_mask);
  18412. + if (!migrate_disabled_updated(p)) {
  18413. + if (p->sched_class->set_cpus_allowed)
  18414. + p->sched_class->set_cpus_allowed(p, new_mask);
  18415. + p->nr_cpus_allowed = cpumask_weight(new_mask);
  18416. + }
  18417. cpumask_copy(&p->cpus_allowed, new_mask);
  18418. - p->nr_cpus_allowed = cpumask_weight(new_mask);
  18419. +}
  18420. +
  18421. +static DEFINE_PER_CPU(struct cpumask, sched_cpumasks);
  18422. +static DEFINE_MUTEX(sched_down_mutex);
  18423. +static cpumask_t sched_down_cpumask;
  18424. +
  18425. +void tell_sched_cpu_down_begin(int cpu)
  18426. +{
  18427. + mutex_lock(&sched_down_mutex);
  18428. + cpumask_set_cpu(cpu, &sched_down_cpumask);
  18429. + mutex_unlock(&sched_down_mutex);
  18430. +}
  18431. +
  18432. +void tell_sched_cpu_down_done(int cpu)
  18433. +{
  18434. + mutex_lock(&sched_down_mutex);
  18435. + cpumask_clear_cpu(cpu, &sched_down_cpumask);
  18436. + mutex_unlock(&sched_down_mutex);
  18437. +}
  18438. +
  18439. +/**
  18440. + * migrate_me - try to move the current task off this cpu
  18441. + *
  18442. + * Used by the pin_current_cpu() code to try to get tasks
  18443. + * to move off the current CPU as it is going down.
  18444. + * It will only move the task if the task isn't pinned to
  18445. + * the CPU (with migrate_disable, affinity or NO_SETAFFINITY)
  18446. + * and the task has to be in a RUNNING state. Otherwise the
  18447. + * movement of the task will wake it up (change its state
  18448. + * to running) when the task did not expect it.
  18449. + *
  18450. + * Returns 1 if it succeeded in moving the current task
  18451. + * 0 otherwise.
  18452. + */
  18453. +int migrate_me(void)
  18454. +{
  18455. + struct task_struct *p = current;
  18456. + struct migration_arg arg;
  18457. + struct cpumask *cpumask;
  18458. + struct cpumask *mask;
  18459. + unsigned long flags;
  18460. + unsigned int dest_cpu;
  18461. + struct rq *rq;
  18462. +
  18463. + /*
  18464. + * We can not migrate tasks bounded to a CPU or tasks not
  18465. + * running. The movement of the task will wake it up.
  18466. + */
  18467. + if (p->flags & PF_NO_SETAFFINITY || p->state)
  18468. + return 0;
  18469. +
  18470. + mutex_lock(&sched_down_mutex);
  18471. + rq = task_rq_lock(p, &flags);
  18472. +
  18473. + cpumask = this_cpu_ptr(&sched_cpumasks);
  18474. + mask = &p->cpus_allowed;
  18475. +
  18476. + cpumask_andnot(cpumask, mask, &sched_down_cpumask);
  18477. +
  18478. + if (!cpumask_weight(cpumask)) {
  18479. + /* It's only on this CPU? */
  18480. + task_rq_unlock(rq, p, &flags);
  18481. + mutex_unlock(&sched_down_mutex);
  18482. + return 0;
  18483. + }
  18484. +
  18485. + dest_cpu = cpumask_any_and(cpu_active_mask, cpumask);
  18486. +
  18487. + arg.task = p;
  18488. + arg.dest_cpu = dest_cpu;
  18489. +
  18490. + task_rq_unlock(rq, p, &flags);
  18491. +
  18492. + stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
  18493. + tlb_migrate_finish(p->mm);
  18494. + mutex_unlock(&sched_down_mutex);
  18495. +
  18496. + return 1;
  18497. }
  18498. /*
  18499. @@ -4805,7 +5151,7 @@
  18500. do_set_cpus_allowed(p, new_mask);
  18501. /* Can the task run on the task's current CPU? If so, we're done */
  18502. - if (cpumask_test_cpu(task_cpu(p), new_mask))
  18503. + if (cpumask_test_cpu(task_cpu(p), new_mask) || __migrate_disabled(p))
  18504. goto out;
  18505. dest_cpu = cpumask_any_and(cpu_active_mask, new_mask);
  18506. @@ -4945,6 +5291,8 @@
  18507. #ifdef CONFIG_HOTPLUG_CPU
  18508. +static DEFINE_PER_CPU(struct mm_struct *, idle_last_mm);
  18509. +
  18510. /*
  18511. * Ensures that the idle task is using init_mm right before its cpu goes
  18512. * offline.
  18513. @@ -4959,7 +5307,11 @@
  18514. switch_mm(mm, &init_mm, current);
  18515. finish_arch_post_lock_switch();
  18516. }
  18517. - mmdrop(mm);
  18518. + /*
  18519. + * Defer the cleanup to an alive cpu. On RT we can neither
  18520. + * call mmdrop() nor mmdrop_delayed() from here.
  18521. + */
  18522. + per_cpu(idle_last_mm, smp_processor_id()) = mm;
  18523. }
  18524. /*
  18525. @@ -5302,6 +5654,10 @@
  18526. case CPU_DEAD:
  18527. calc_load_migrate(rq);
  18528. + if (per_cpu(idle_last_mm, cpu)) {
  18529. + mmdrop(per_cpu(idle_last_mm, cpu));
  18530. + per_cpu(idle_last_mm, cpu) = NULL;
  18531. + }
  18532. break;
  18533. #endif
  18534. }
  18535. @@ -7281,7 +7637,8 @@
  18536. #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
  18537. static inline int preempt_count_equals(int preempt_offset)
  18538. {
  18539. - int nested = (preempt_count() & ~PREEMPT_ACTIVE) + rcu_preempt_depth();
  18540. + int nested = (preempt_count() & ~PREEMPT_ACTIVE) +
  18541. + sched_rcu_preempt_depth();
  18542. return (nested == preempt_offset);
  18543. }
  18544. diff -Nur linux-4.1.20.orig/kernel/sched/cputime.c linux-4.1.20/kernel/sched/cputime.c
  18545. --- linux-4.1.20.orig/kernel/sched/cputime.c 2016-03-17 19:11:03.000000000 +0100
  18546. +++ linux-4.1.20/kernel/sched/cputime.c 2016-03-21 20:18:33.000000000 +0100
  18547. @@ -675,37 +675,45 @@
  18548. void vtime_account_system(struct task_struct *tsk)
  18549. {
  18550. - write_seqlock(&tsk->vtime_seqlock);
  18551. + raw_spin_lock(&tsk->vtime_lock);
  18552. + write_seqcount_begin(&tsk->vtime_seq);
  18553. __vtime_account_system(tsk);
  18554. - write_sequnlock(&tsk->vtime_seqlock);
  18555. + write_seqcount_end(&tsk->vtime_seq);
  18556. + raw_spin_unlock(&tsk->vtime_lock);
  18557. }
  18558. void vtime_gen_account_irq_exit(struct task_struct *tsk)
  18559. {
  18560. - write_seqlock(&tsk->vtime_seqlock);
  18561. + raw_spin_lock(&tsk->vtime_lock);
  18562. + write_seqcount_begin(&tsk->vtime_seq);
  18563. __vtime_account_system(tsk);
  18564. if (context_tracking_in_user())
  18565. tsk->vtime_snap_whence = VTIME_USER;
  18566. - write_sequnlock(&tsk->vtime_seqlock);
  18567. + write_seqcount_end(&tsk->vtime_seq);
  18568. + raw_spin_unlock(&tsk->vtime_lock);
  18569. }
  18570. void vtime_account_user(struct task_struct *tsk)
  18571. {
  18572. cputime_t delta_cpu;
  18573. - write_seqlock(&tsk->vtime_seqlock);
  18574. + raw_spin_lock(&tsk->vtime_lock);
  18575. + write_seqcount_begin(&tsk->vtime_seq);
  18576. delta_cpu = get_vtime_delta(tsk);
  18577. tsk->vtime_snap_whence = VTIME_SYS;
  18578. account_user_time(tsk, delta_cpu, cputime_to_scaled(delta_cpu));
  18579. - write_sequnlock(&tsk->vtime_seqlock);
  18580. + write_seqcount_end(&tsk->vtime_seq);
  18581. + raw_spin_unlock(&tsk->vtime_lock);
  18582. }
  18583. void vtime_user_enter(struct task_struct *tsk)
  18584. {
  18585. - write_seqlock(&tsk->vtime_seqlock);
  18586. + raw_spin_lock(&tsk->vtime_lock);
  18587. + write_seqcount_begin(&tsk->vtime_seq);
  18588. __vtime_account_system(tsk);
  18589. tsk->vtime_snap_whence = VTIME_USER;
  18590. - write_sequnlock(&tsk->vtime_seqlock);
  18591. + write_seqcount_end(&tsk->vtime_seq);
  18592. + raw_spin_unlock(&tsk->vtime_lock);
  18593. }
  18594. void vtime_guest_enter(struct task_struct *tsk)
  18595. @@ -717,19 +725,23 @@
  18596. * synchronization against the reader (task_gtime())
  18597. * that can thus safely catch up with a tickless delta.
  18598. */
  18599. - write_seqlock(&tsk->vtime_seqlock);
  18600. + raw_spin_lock(&tsk->vtime_lock);
  18601. + write_seqcount_begin(&tsk->vtime_seq);
  18602. __vtime_account_system(tsk);
  18603. current->flags |= PF_VCPU;
  18604. - write_sequnlock(&tsk->vtime_seqlock);
  18605. + write_seqcount_end(&tsk->vtime_seq);
  18606. + raw_spin_unlock(&tsk->vtime_lock);
  18607. }
  18608. EXPORT_SYMBOL_GPL(vtime_guest_enter);
  18609. void vtime_guest_exit(struct task_struct *tsk)
  18610. {
  18611. - write_seqlock(&tsk->vtime_seqlock);
  18612. + raw_spin_lock(&tsk->vtime_lock);
  18613. + write_seqcount_begin(&tsk->vtime_seq);
  18614. __vtime_account_system(tsk);
  18615. current->flags &= ~PF_VCPU;
  18616. - write_sequnlock(&tsk->vtime_seqlock);
  18617. + write_seqcount_end(&tsk->vtime_seq);
  18618. + raw_spin_unlock(&tsk->vtime_lock);
  18619. }
  18620. EXPORT_SYMBOL_GPL(vtime_guest_exit);
  18621. @@ -742,24 +754,30 @@
  18622. void arch_vtime_task_switch(struct task_struct *prev)
  18623. {
  18624. - write_seqlock(&prev->vtime_seqlock);
  18625. + raw_spin_lock(&prev->vtime_lock);
  18626. + write_seqcount_begin(&prev->vtime_seq);
  18627. prev->vtime_snap_whence = VTIME_SLEEPING;
  18628. - write_sequnlock(&prev->vtime_seqlock);
  18629. + write_seqcount_end(&prev->vtime_seq);
  18630. + raw_spin_unlock(&prev->vtime_lock);
  18631. - write_seqlock(&current->vtime_seqlock);
  18632. + raw_spin_lock(&current->vtime_lock);
  18633. + write_seqcount_begin(&current->vtime_seq);
  18634. current->vtime_snap_whence = VTIME_SYS;
  18635. current->vtime_snap = sched_clock_cpu(smp_processor_id());
  18636. - write_sequnlock(&current->vtime_seqlock);
  18637. + write_seqcount_end(&current->vtime_seq);
  18638. + raw_spin_unlock(&current->vtime_lock);
  18639. }
  18640. void vtime_init_idle(struct task_struct *t, int cpu)
  18641. {
  18642. unsigned long flags;
  18643. - write_seqlock_irqsave(&t->vtime_seqlock, flags);
  18644. + raw_spin_lock_irqsave(&t->vtime_lock, flags);
  18645. + write_seqcount_begin(&t->vtime_seq);
  18646. t->vtime_snap_whence = VTIME_SYS;
  18647. t->vtime_snap = sched_clock_cpu(cpu);
  18648. - write_sequnlock_irqrestore(&t->vtime_seqlock, flags);
  18649. + write_seqcount_end(&t->vtime_seq);
  18650. + raw_spin_unlock_irqrestore(&t->vtime_lock, flags);
  18651. }
  18652. cputime_t task_gtime(struct task_struct *t)
  18653. @@ -768,13 +786,13 @@
  18654. cputime_t gtime;
  18655. do {
  18656. - seq = read_seqbegin(&t->vtime_seqlock);
  18657. + seq = read_seqcount_begin(&t->vtime_seq);
  18658. gtime = t->gtime;
  18659. if (t->flags & PF_VCPU)
  18660. gtime += vtime_delta(t);
  18661. - } while (read_seqretry(&t->vtime_seqlock, seq));
  18662. + } while (read_seqcount_retry(&t->vtime_seq, seq));
  18663. return gtime;
  18664. }
  18665. @@ -797,7 +815,7 @@
  18666. *udelta = 0;
  18667. *sdelta = 0;
  18668. - seq = read_seqbegin(&t->vtime_seqlock);
  18669. + seq = read_seqcount_begin(&t->vtime_seq);
  18670. if (u_dst)
  18671. *u_dst = *u_src;
  18672. @@ -821,7 +839,7 @@
  18673. if (t->vtime_snap_whence == VTIME_SYS)
  18674. *sdelta = delta;
  18675. }
  18676. - } while (read_seqretry(&t->vtime_seqlock, seq));
  18677. + } while (read_seqcount_retry(&t->vtime_seq, seq));
  18678. }
  18679. diff -Nur linux-4.1.20.orig/kernel/sched/deadline.c linux-4.1.20/kernel/sched/deadline.c
  18680. --- linux-4.1.20.orig/kernel/sched/deadline.c 2016-03-17 19:11:03.000000000 +0100
  18681. +++ linux-4.1.20/kernel/sched/deadline.c 2016-03-21 20:18:33.000000000 +0100
  18682. @@ -637,6 +637,7 @@
  18683. hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  18684. timer->function = dl_task_timer;
  18685. + timer->irqsafe = 1;
  18686. }
  18687. static
  18688. diff -Nur linux-4.1.20.orig/kernel/sched/debug.c linux-4.1.20/kernel/sched/debug.c
  18689. --- linux-4.1.20.orig/kernel/sched/debug.c 2016-03-17 19:11:03.000000000 +0100
  18690. +++ linux-4.1.20/kernel/sched/debug.c 2016-03-21 20:18:33.000000000 +0100
  18691. @@ -260,6 +260,9 @@
  18692. P(rt_throttled);
  18693. PN(rt_time);
  18694. PN(rt_runtime);
  18695. +#ifdef CONFIG_SMP
  18696. + P(rt_nr_migratory);
  18697. +#endif
  18698. #undef PN
  18699. #undef P
  18700. @@ -648,6 +651,10 @@
  18701. #endif
  18702. P(policy);
  18703. P(prio);
  18704. +#ifdef CONFIG_PREEMPT_RT_FULL
  18705. + P(migrate_disable);
  18706. +#endif
  18707. + P(nr_cpus_allowed);
  18708. #undef PN
  18709. #undef __PN
  18710. #undef P
  18711. diff -Nur linux-4.1.20.orig/kernel/sched/fair.c linux-4.1.20/kernel/sched/fair.c
  18712. --- linux-4.1.20.orig/kernel/sched/fair.c 2016-03-17 19:11:03.000000000 +0100
  18713. +++ linux-4.1.20/kernel/sched/fair.c 2016-03-21 20:18:33.000000000 +0100
  18714. @@ -3201,7 +3201,7 @@
  18715. ideal_runtime = sched_slice(cfs_rq, curr);
  18716. delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
  18717. if (delta_exec > ideal_runtime) {
  18718. - resched_curr(rq_of(cfs_rq));
  18719. + resched_curr_lazy(rq_of(cfs_rq));
  18720. /*
  18721. * The current task ran long enough, ensure it doesn't get
  18722. * re-elected due to buddy favours.
  18723. @@ -3225,7 +3225,7 @@
  18724. return;
  18725. if (delta > ideal_runtime)
  18726. - resched_curr(rq_of(cfs_rq));
  18727. + resched_curr_lazy(rq_of(cfs_rq));
  18728. }
  18729. static void
  18730. @@ -3366,7 +3366,7 @@
  18731. * validating it and just reschedule.
  18732. */
  18733. if (queued) {
  18734. - resched_curr(rq_of(cfs_rq));
  18735. + resched_curr_lazy(rq_of(cfs_rq));
  18736. return;
  18737. }
  18738. /*
  18739. @@ -3557,7 +3557,7 @@
  18740. * hierarchy can be throttled
  18741. */
  18742. if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr))
  18743. - resched_curr(rq_of(cfs_rq));
  18744. + resched_curr_lazy(rq_of(cfs_rq));
  18745. }
  18746. static __always_inline
  18747. @@ -4180,7 +4180,7 @@
  18748. if (delta < 0) {
  18749. if (rq->curr == p)
  18750. - resched_curr(rq);
  18751. + resched_curr_lazy(rq);
  18752. return;
  18753. }
  18754. hrtick_start(rq, delta);
  18755. @@ -5076,7 +5076,7 @@
  18756. return;
  18757. preempt:
  18758. - resched_curr(rq);
  18759. + resched_curr_lazy(rq);
  18760. /*
  18761. * Only set the backward buddy when the current task is still
  18762. * on the rq. This can happen when a wakeup gets interleaved
  18763. @@ -7869,7 +7869,7 @@
  18764. * 'current' within the tree based on its new key value.
  18765. */
  18766. swap(curr->vruntime, se->vruntime);
  18767. - resched_curr(rq);
  18768. + resched_curr_lazy(rq);
  18769. }
  18770. se->vruntime -= cfs_rq->min_vruntime;
  18771. @@ -7894,7 +7894,7 @@
  18772. */
  18773. if (rq->curr == p) {
  18774. if (p->prio > oldprio)
  18775. - resched_curr(rq);
  18776. + resched_curr_lazy(rq);
  18777. } else
  18778. check_preempt_curr(rq, p, 0);
  18779. }
  18780. diff -Nur linux-4.1.20.orig/kernel/sched/features.h linux-4.1.20/kernel/sched/features.h
  18781. --- linux-4.1.20.orig/kernel/sched/features.h 2016-03-17 19:11:03.000000000 +0100
  18782. +++ linux-4.1.20/kernel/sched/features.h 2016-03-21 20:18:33.000000000 +0100
  18783. @@ -50,11 +50,19 @@
  18784. */
  18785. SCHED_FEAT(NONTASK_CAPACITY, true)
  18786. +#ifdef CONFIG_PREEMPT_RT_FULL
  18787. +SCHED_FEAT(TTWU_QUEUE, false)
  18788. +# ifdef CONFIG_PREEMPT_LAZY
  18789. +SCHED_FEAT(PREEMPT_LAZY, true)
  18790. +# endif
  18791. +#else
  18792. +
  18793. /*
  18794. * Queue remote wakeups on the target CPU and process them
  18795. * using the scheduler IPI. Reduces rq->lock contention/bounces.
  18796. */
  18797. SCHED_FEAT(TTWU_QUEUE, true)
  18798. +#endif
  18799. #ifdef HAVE_RT_PUSH_IPI
  18800. /*
  18801. diff -Nur linux-4.1.20.orig/kernel/sched/rt.c linux-4.1.20/kernel/sched/rt.c
  18802. --- linux-4.1.20.orig/kernel/sched/rt.c 2016-03-17 19:11:03.000000000 +0100
  18803. +++ linux-4.1.20/kernel/sched/rt.c 2016-03-21 20:18:33.000000000 +0100
  18804. @@ -44,6 +44,7 @@
  18805. hrtimer_init(&rt_b->rt_period_timer,
  18806. CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  18807. + rt_b->rt_period_timer.irqsafe = 1;
  18808. rt_b->rt_period_timer.function = sched_rt_period_timer;
  18809. }
  18810. @@ -89,6 +90,7 @@
  18811. rt_rq->push_cpu = nr_cpu_ids;
  18812. raw_spin_lock_init(&rt_rq->push_lock);
  18813. init_irq_work(&rt_rq->push_work, push_irq_work_func);
  18814. + rt_rq->push_work.flags |= IRQ_WORK_HARD_IRQ;
  18815. #endif
  18816. #endif /* CONFIG_SMP */
  18817. /* We start is dequeued state, because no RT tasks are queued */
  18818. diff -Nur linux-4.1.20.orig/kernel/sched/sched.h linux-4.1.20/kernel/sched/sched.h
  18819. --- linux-4.1.20.orig/kernel/sched/sched.h 2016-03-17 19:11:03.000000000 +0100
  18820. +++ linux-4.1.20/kernel/sched/sched.h 2016-03-21 20:18:33.000000000 +0100
  18821. @@ -1093,6 +1093,7 @@
  18822. #define WF_SYNC 0x01 /* waker goes to sleep after wakeup */
  18823. #define WF_FORK 0x02 /* child wakeup after fork */
  18824. #define WF_MIGRATED 0x4 /* internal use, task got migrated */
  18825. +#define WF_LOCK_SLEEPER 0x08 /* wakeup spinlock "sleeper" */
  18826. /*
  18827. * To aid in avoiding the subversion of "niceness" due to uneven distribution
  18828. @@ -1290,6 +1291,15 @@
  18829. extern void resched_curr(struct rq *rq);
  18830. extern void resched_cpu(int cpu);
  18831. +#ifdef CONFIG_PREEMPT_LAZY
  18832. +extern void resched_curr_lazy(struct rq *rq);
  18833. +#else
  18834. +static inline void resched_curr_lazy(struct rq *rq)
  18835. +{
  18836. + resched_curr(rq);
  18837. +}
  18838. +#endif
  18839. +
  18840. extern struct rt_bandwidth def_rt_bandwidth;
  18841. extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
  18842. diff -Nur linux-4.1.20.orig/kernel/sched/wait-simple.c linux-4.1.20/kernel/sched/wait-simple.c
  18843. --- linux-4.1.20.orig/kernel/sched/wait-simple.c 1970-01-01 01:00:00.000000000 +0100
  18844. +++ linux-4.1.20/kernel/sched/wait-simple.c 2016-03-21 20:18:33.000000000 +0100
  18845. @@ -0,0 +1,115 @@
  18846. +/*
  18847. + * Simple waitqueues without fancy flags and callbacks
  18848. + *
  18849. + * (C) 2011 Thomas Gleixner <tglx@linutronix.de>
  18850. + *
  18851. + * Based on kernel/wait.c
  18852. + *
  18853. + * For licencing details see kernel-base/COPYING
  18854. + */
  18855. +#include <linux/init.h>
  18856. +#include <linux/export.h>
  18857. +#include <linux/sched.h>
  18858. +#include <linux/wait-simple.h>
  18859. +
  18860. +/* Adds w to head->list. Must be called with head->lock locked. */
  18861. +static inline void __swait_enqueue(struct swait_head *head, struct swaiter *w)
  18862. +{
  18863. + list_add(&w->node, &head->list);
  18864. + /* We can't let the condition leak before the setting of head */
  18865. + smp_mb();
  18866. +}
  18867. +
  18868. +/* Removes w from head->list. Must be called with head->lock locked. */
  18869. +static inline void __swait_dequeue(struct swaiter *w)
  18870. +{
  18871. + list_del_init(&w->node);
  18872. +}
  18873. +
  18874. +void __init_swait_head(struct swait_head *head, struct lock_class_key *key)
  18875. +{
  18876. + raw_spin_lock_init(&head->lock);
  18877. + lockdep_set_class(&head->lock, key);
  18878. + INIT_LIST_HEAD(&head->list);
  18879. +}
  18880. +EXPORT_SYMBOL(__init_swait_head);
  18881. +
  18882. +void swait_prepare_locked(struct swait_head *head, struct swaiter *w)
  18883. +{
  18884. + w->task = current;
  18885. + if (list_empty(&w->node))
  18886. + __swait_enqueue(head, w);
  18887. +}
  18888. +
  18889. +void swait_prepare(struct swait_head *head, struct swaiter *w, int state)
  18890. +{
  18891. + unsigned long flags;
  18892. +
  18893. + raw_spin_lock_irqsave(&head->lock, flags);
  18894. + swait_prepare_locked(head, w);
  18895. + __set_current_state(state);
  18896. + raw_spin_unlock_irqrestore(&head->lock, flags);
  18897. +}
  18898. +EXPORT_SYMBOL(swait_prepare);
  18899. +
  18900. +void swait_finish_locked(struct swait_head *head, struct swaiter *w)
  18901. +{
  18902. + __set_current_state(TASK_RUNNING);
  18903. + if (w->task)
  18904. + __swait_dequeue(w);
  18905. +}
  18906. +
  18907. +void swait_finish(struct swait_head *head, struct swaiter *w)
  18908. +{
  18909. + unsigned long flags;
  18910. +
  18911. + __set_current_state(TASK_RUNNING);
  18912. + if (w->task) {
  18913. + raw_spin_lock_irqsave(&head->lock, flags);
  18914. + __swait_dequeue(w);
  18915. + raw_spin_unlock_irqrestore(&head->lock, flags);
  18916. + }
  18917. +}
  18918. +EXPORT_SYMBOL(swait_finish);
  18919. +
  18920. +unsigned int
  18921. +__swait_wake_locked(struct swait_head *head, unsigned int state, unsigned int num)
  18922. +{
  18923. + struct swaiter *curr, *next;
  18924. + int woken = 0;
  18925. +
  18926. + list_for_each_entry_safe(curr, next, &head->list, node) {
  18927. + if (wake_up_state(curr->task, state)) {
  18928. + __swait_dequeue(curr);
  18929. + /*
  18930. + * The waiting task can free the waiter as
  18931. + * soon as curr->task = NULL is written,
  18932. + * without taking any locks. A memory barrier
  18933. + * is required here to prevent the following
  18934. + * store to curr->task from getting ahead of
  18935. + * the dequeue operation.
  18936. + */
  18937. + smp_wmb();
  18938. + curr->task = NULL;
  18939. + if (++woken == num)
  18940. + break;
  18941. + }
  18942. + }
  18943. + return woken;
  18944. +}
  18945. +
  18946. +unsigned int
  18947. +__swait_wake(struct swait_head *head, unsigned int state, unsigned int num)
  18948. +{
  18949. + unsigned long flags;
  18950. + int woken;
  18951. +
  18952. + if (!swaitqueue_active(head))
  18953. + return 0;
  18954. +
  18955. + raw_spin_lock_irqsave(&head->lock, flags);
  18956. + woken = __swait_wake_locked(head, state, num);
  18957. + raw_spin_unlock_irqrestore(&head->lock, flags);
  18958. + return woken;
  18959. +}
  18960. +EXPORT_SYMBOL(__swait_wake);
  18961. diff -Nur linux-4.1.20.orig/kernel/sched/work-simple.c linux-4.1.20/kernel/sched/work-simple.c
  18962. --- linux-4.1.20.orig/kernel/sched/work-simple.c 1970-01-01 01:00:00.000000000 +0100
  18963. +++ linux-4.1.20/kernel/sched/work-simple.c 2016-03-21 20:18:33.000000000 +0100
  18964. @@ -0,0 +1,173 @@
  18965. +/*
  18966. + * Copyright (C) 2014 BMW Car IT GmbH, Daniel Wagner daniel.wagner@bmw-carit.de
  18967. + *
  18968. + * Provides a framework for enqueuing callbacks from irq context
  18969. + * PREEMPT_RT_FULL safe. The callbacks are executed in kthread context.
  18970. + */
  18971. +
  18972. +#include <linux/wait-simple.h>
  18973. +#include <linux/work-simple.h>
  18974. +#include <linux/kthread.h>
  18975. +#include <linux/slab.h>
  18976. +#include <linux/spinlock.h>
  18977. +#include <linux/export.h>
  18978. +
  18979. +#define SWORK_EVENT_PENDING (1 << 0)
  18980. +
  18981. +static DEFINE_MUTEX(worker_mutex);
  18982. +static struct sworker *glob_worker;
  18983. +
  18984. +struct sworker {
  18985. + struct list_head events;
  18986. + struct swait_head wq;
  18987. +
  18988. + raw_spinlock_t lock;
  18989. +
  18990. + struct task_struct *task;
  18991. + int refs;
  18992. +};
  18993. +
  18994. +static bool swork_readable(struct sworker *worker)
  18995. +{
  18996. + bool r;
  18997. +
  18998. + if (kthread_should_stop())
  18999. + return true;
  19000. +
  19001. + raw_spin_lock_irq(&worker->lock);
  19002. + r = !list_empty(&worker->events);
  19003. + raw_spin_unlock_irq(&worker->lock);
  19004. +
  19005. + return r;
  19006. +}
  19007. +
  19008. +static int swork_kthread(void *arg)
  19009. +{
  19010. + struct sworker *worker = arg;
  19011. +
  19012. + for (;;) {
  19013. + swait_event_interruptible(worker->wq,
  19014. + swork_readable(worker));
  19015. + if (kthread_should_stop())
  19016. + break;
  19017. +
  19018. + raw_spin_lock_irq(&worker->lock);
  19019. + while (!list_empty(&worker->events)) {
  19020. + struct swork_event *sev;
  19021. +
  19022. + sev = list_first_entry(&worker->events,
  19023. + struct swork_event, item);
  19024. + list_del(&sev->item);
  19025. + raw_spin_unlock_irq(&worker->lock);
  19026. +
  19027. + WARN_ON_ONCE(!test_and_clear_bit(SWORK_EVENT_PENDING,
  19028. + &sev->flags));
  19029. + sev->func(sev);
  19030. + raw_spin_lock_irq(&worker->lock);
  19031. + }
  19032. + raw_spin_unlock_irq(&worker->lock);
  19033. + }
  19034. + return 0;
  19035. +}
  19036. +
  19037. +static struct sworker *swork_create(void)
  19038. +{
  19039. + struct sworker *worker;
  19040. +
  19041. + worker = kzalloc(sizeof(*worker), GFP_KERNEL);
  19042. + if (!worker)
  19043. + return ERR_PTR(-ENOMEM);
  19044. +
  19045. + INIT_LIST_HEAD(&worker->events);
  19046. + raw_spin_lock_init(&worker->lock);
  19047. + init_swait_head(&worker->wq);
  19048. +
  19049. + worker->task = kthread_run(swork_kthread, worker, "kswork");
  19050. + if (IS_ERR(worker->task)) {
  19051. + kfree(worker);
  19052. + return ERR_PTR(-ENOMEM);
  19053. + }
  19054. +
  19055. + return worker;
  19056. +}
  19057. +
  19058. +static void swork_destroy(struct sworker *worker)
  19059. +{
  19060. + kthread_stop(worker->task);
  19061. +
  19062. + WARN_ON(!list_empty(&worker->events));
  19063. + kfree(worker);
  19064. +}
  19065. +
  19066. +/**
  19067. + * swork_queue - queue swork
  19068. + *
  19069. + * Returns %false if @work was already on a queue, %true otherwise.
  19070. + *
  19071. + * The work is queued and processed on a random CPU
  19072. + */
  19073. +bool swork_queue(struct swork_event *sev)
  19074. +{
  19075. + unsigned long flags;
  19076. +
  19077. + if (test_and_set_bit(SWORK_EVENT_PENDING, &sev->flags))
  19078. + return false;
  19079. +
  19080. + raw_spin_lock_irqsave(&glob_worker->lock, flags);
  19081. + list_add_tail(&sev->item, &glob_worker->events);
  19082. + raw_spin_unlock_irqrestore(&glob_worker->lock, flags);
  19083. +
  19084. + swait_wake(&glob_worker->wq);
  19085. + return true;
  19086. +}
  19087. +EXPORT_SYMBOL_GPL(swork_queue);
  19088. +
  19089. +/**
  19090. + * swork_get - get an instance of the sworker
  19091. + *
  19092. + * Returns an negative error code if the initialization if the worker did not
  19093. + * work, %0 otherwise.
  19094. + *
  19095. + */
  19096. +int swork_get(void)
  19097. +{
  19098. + struct sworker *worker;
  19099. +
  19100. + mutex_lock(&worker_mutex);
  19101. + if (!glob_worker) {
  19102. + worker = swork_create();
  19103. + if (IS_ERR(worker)) {
  19104. + mutex_unlock(&worker_mutex);
  19105. + return -ENOMEM;
  19106. + }
  19107. +
  19108. + glob_worker = worker;
  19109. + }
  19110. +
  19111. + glob_worker->refs++;
  19112. + mutex_unlock(&worker_mutex);
  19113. +
  19114. + return 0;
  19115. +}
  19116. +EXPORT_SYMBOL_GPL(swork_get);
  19117. +
  19118. +/**
  19119. + * swork_put - puts an instance of the sworker
  19120. + *
  19121. + * Will destroy the sworker thread. This function must not be called until all
  19122. + * queued events have been completed.
  19123. + */
  19124. +void swork_put(void)
  19125. +{
  19126. + mutex_lock(&worker_mutex);
  19127. +
  19128. + glob_worker->refs--;
  19129. + if (glob_worker->refs > 0)
  19130. + goto out;
  19131. +
  19132. + swork_destroy(glob_worker);
  19133. + glob_worker = NULL;
  19134. +out:
  19135. + mutex_unlock(&worker_mutex);
  19136. +}
  19137. +EXPORT_SYMBOL_GPL(swork_put);
  19138. diff -Nur linux-4.1.20.orig/kernel/signal.c linux-4.1.20/kernel/signal.c
  19139. --- linux-4.1.20.orig/kernel/signal.c 2016-03-17 19:11:03.000000000 +0100
  19140. +++ linux-4.1.20/kernel/signal.c 2016-03-21 20:18:33.000000000 +0100
  19141. @@ -14,6 +14,7 @@
  19142. #include <linux/export.h>
  19143. #include <linux/init.h>
  19144. #include <linux/sched.h>
  19145. +#include <linux/sched/rt.h>
  19146. #include <linux/fs.h>
  19147. #include <linux/tty.h>
  19148. #include <linux/binfmts.h>
  19149. @@ -352,13 +353,45 @@
  19150. return false;
  19151. }
  19152. +#ifdef __HAVE_ARCH_CMPXCHG
  19153. +static inline struct sigqueue *get_task_cache(struct task_struct *t)
  19154. +{
  19155. + struct sigqueue *q = t->sigqueue_cache;
  19156. +
  19157. + if (cmpxchg(&t->sigqueue_cache, q, NULL) != q)
  19158. + return NULL;
  19159. + return q;
  19160. +}
  19161. +
  19162. +static inline int put_task_cache(struct task_struct *t, struct sigqueue *q)
  19163. +{
  19164. + if (cmpxchg(&t->sigqueue_cache, NULL, q) == NULL)
  19165. + return 0;
  19166. + return 1;
  19167. +}
  19168. +
  19169. +#else
  19170. +
  19171. +static inline struct sigqueue *get_task_cache(struct task_struct *t)
  19172. +{
  19173. + return NULL;
  19174. +}
  19175. +
  19176. +static inline int put_task_cache(struct task_struct *t, struct sigqueue *q)
  19177. +{
  19178. + return 1;
  19179. +}
  19180. +
  19181. +#endif
  19182. +
  19183. /*
  19184. * allocate a new signal queue record
  19185. * - this may be called without locks if and only if t == current, otherwise an
  19186. * appropriate lock must be held to stop the target task from exiting
  19187. */
  19188. static struct sigqueue *
  19189. -__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
  19190. +__sigqueue_do_alloc(int sig, struct task_struct *t, gfp_t flags,
  19191. + int override_rlimit, int fromslab)
  19192. {
  19193. struct sigqueue *q = NULL;
  19194. struct user_struct *user;
  19195. @@ -375,7 +408,10 @@
  19196. if (override_rlimit ||
  19197. atomic_read(&user->sigpending) <=
  19198. task_rlimit(t, RLIMIT_SIGPENDING)) {
  19199. - q = kmem_cache_alloc(sigqueue_cachep, flags);
  19200. + if (!fromslab)
  19201. + q = get_task_cache(t);
  19202. + if (!q)
  19203. + q = kmem_cache_alloc(sigqueue_cachep, flags);
  19204. } else {
  19205. print_dropped_signal(sig);
  19206. }
  19207. @@ -392,6 +428,13 @@
  19208. return q;
  19209. }
  19210. +static struct sigqueue *
  19211. +__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags,
  19212. + int override_rlimit)
  19213. +{
  19214. + return __sigqueue_do_alloc(sig, t, flags, override_rlimit, 0);
  19215. +}
  19216. +
  19217. static void __sigqueue_free(struct sigqueue *q)
  19218. {
  19219. if (q->flags & SIGQUEUE_PREALLOC)
  19220. @@ -401,6 +444,21 @@
  19221. kmem_cache_free(sigqueue_cachep, q);
  19222. }
  19223. +static void sigqueue_free_current(struct sigqueue *q)
  19224. +{
  19225. + struct user_struct *up;
  19226. +
  19227. + if (q->flags & SIGQUEUE_PREALLOC)
  19228. + return;
  19229. +
  19230. + up = q->user;
  19231. + if (rt_prio(current->normal_prio) && !put_task_cache(current, q)) {
  19232. + atomic_dec(&up->sigpending);
  19233. + free_uid(up);
  19234. + } else
  19235. + __sigqueue_free(q);
  19236. +}
  19237. +
  19238. void flush_sigqueue(struct sigpending *queue)
  19239. {
  19240. struct sigqueue *q;
  19241. @@ -414,6 +472,21 @@
  19242. }
  19243. /*
  19244. + * Called from __exit_signal. Flush tsk->pending and
  19245. + * tsk->sigqueue_cache
  19246. + */
  19247. +void flush_task_sigqueue(struct task_struct *tsk)
  19248. +{
  19249. + struct sigqueue *q;
  19250. +
  19251. + flush_sigqueue(&tsk->pending);
  19252. +
  19253. + q = get_task_cache(tsk);
  19254. + if (q)
  19255. + kmem_cache_free(sigqueue_cachep, q);
  19256. +}
  19257. +
  19258. +/*
  19259. * Flush all pending signals for a task.
  19260. */
  19261. void __flush_signals(struct task_struct *t)
  19262. @@ -565,7 +638,7 @@
  19263. still_pending:
  19264. list_del_init(&first->list);
  19265. copy_siginfo(info, &first->info);
  19266. - __sigqueue_free(first);
  19267. + sigqueue_free_current(first);
  19268. } else {
  19269. /*
  19270. * Ok, it wasn't in the queue. This must be
  19271. @@ -611,6 +684,8 @@
  19272. {
  19273. int signr;
  19274. + WARN_ON_ONCE(tsk != current);
  19275. +
  19276. /* We only dequeue private signals from ourselves, we don't let
  19277. * signalfd steal them
  19278. */
  19279. @@ -1207,8 +1282,8 @@
  19280. * We don't want to have recursive SIGSEGV's etc, for example,
  19281. * that is why we also clear SIGNAL_UNKILLABLE.
  19282. */
  19283. -int
  19284. -force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  19285. +static int
  19286. +do_force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  19287. {
  19288. unsigned long int flags;
  19289. int ret, blocked, ignored;
  19290. @@ -1233,6 +1308,39 @@
  19291. return ret;
  19292. }
  19293. +int force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  19294. +{
  19295. +/*
  19296. + * On some archs, PREEMPT_RT has to delay sending a signal from a trap
  19297. + * since it can not enable preemption, and the signal code's spin_locks
  19298. + * turn into mutexes. Instead, it must set TIF_NOTIFY_RESUME which will
  19299. + * send the signal on exit of the trap.
  19300. + */
  19301. +#ifdef ARCH_RT_DELAYS_SIGNAL_SEND
  19302. + if (in_atomic()) {
  19303. + if (WARN_ON_ONCE(t != current))
  19304. + return 0;
  19305. + if (WARN_ON_ONCE(t->forced_info.si_signo))
  19306. + return 0;
  19307. +
  19308. + if (is_si_special(info)) {
  19309. + WARN_ON_ONCE(info != SEND_SIG_PRIV);
  19310. + t->forced_info.si_signo = sig;
  19311. + t->forced_info.si_errno = 0;
  19312. + t->forced_info.si_code = SI_KERNEL;
  19313. + t->forced_info.si_pid = 0;
  19314. + t->forced_info.si_uid = 0;
  19315. + } else {
  19316. + t->forced_info = *info;
  19317. + }
  19318. +
  19319. + set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
  19320. + return 0;
  19321. + }
  19322. +#endif
  19323. + return do_force_sig_info(sig, info, t);
  19324. +}
  19325. +
  19326. /*
  19327. * Nuke all other threads in the group.
  19328. */
  19329. @@ -1267,12 +1375,12 @@
  19330. * Disable interrupts early to avoid deadlocks.
  19331. * See rcu_read_unlock() comment header for details.
  19332. */
  19333. - local_irq_save(*flags);
  19334. + local_irq_save_nort(*flags);
  19335. rcu_read_lock();
  19336. sighand = rcu_dereference(tsk->sighand);
  19337. if (unlikely(sighand == NULL)) {
  19338. rcu_read_unlock();
  19339. - local_irq_restore(*flags);
  19340. + local_irq_restore_nort(*flags);
  19341. break;
  19342. }
  19343. /*
  19344. @@ -1293,7 +1401,7 @@
  19345. }
  19346. spin_unlock(&sighand->siglock);
  19347. rcu_read_unlock();
  19348. - local_irq_restore(*flags);
  19349. + local_irq_restore_nort(*flags);
  19350. }
  19351. return sighand;
  19352. @@ -1536,7 +1644,8 @@
  19353. */
  19354. struct sigqueue *sigqueue_alloc(void)
  19355. {
  19356. - struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
  19357. + /* Preallocated sigqueue objects always from the slabcache ! */
  19358. + struct sigqueue *q = __sigqueue_do_alloc(-1, current, GFP_KERNEL, 0, 1);
  19359. if (q)
  19360. q->flags |= SIGQUEUE_PREALLOC;
  19361. @@ -1897,15 +2006,7 @@
  19362. if (gstop_done && ptrace_reparented(current))
  19363. do_notify_parent_cldstop(current, false, why);
  19364. - /*
  19365. - * Don't want to allow preemption here, because
  19366. - * sys_ptrace() needs this task to be inactive.
  19367. - *
  19368. - * XXX: implement read_unlock_no_resched().
  19369. - */
  19370. - preempt_disable();
  19371. read_unlock(&tasklist_lock);
  19372. - preempt_enable_no_resched();
  19373. freezable_schedule();
  19374. } else {
  19375. /*
  19376. diff -Nur linux-4.1.20.orig/kernel/softirq.c linux-4.1.20/kernel/softirq.c
  19377. --- linux-4.1.20.orig/kernel/softirq.c 2016-03-17 19:11:03.000000000 +0100
  19378. +++ linux-4.1.20/kernel/softirq.c 2016-03-21 20:18:33.000000000 +0100
  19379. @@ -21,10 +21,12 @@
  19380. #include <linux/freezer.h>
  19381. #include <linux/kthread.h>
  19382. #include <linux/rcupdate.h>
  19383. +#include <linux/delay.h>
  19384. #include <linux/ftrace.h>
  19385. #include <linux/smp.h>
  19386. #include <linux/smpboot.h>
  19387. #include <linux/tick.h>
  19388. +#include <linux/locallock.h>
  19389. #include <linux/irq.h>
  19390. #define CREATE_TRACE_POINTS
  19391. @@ -56,12 +58,108 @@
  19392. static struct softirq_action softirq_vec[NR_SOFTIRQS] __cacheline_aligned_in_smp;
  19393. DEFINE_PER_CPU(struct task_struct *, ksoftirqd);
  19394. +#ifdef CONFIG_PREEMPT_RT_FULL
  19395. +#define TIMER_SOFTIRQS ((1 << TIMER_SOFTIRQ) | (1 << HRTIMER_SOFTIRQ))
  19396. +DEFINE_PER_CPU(struct task_struct *, ktimer_softirqd);
  19397. +#endif
  19398. const char * const softirq_to_name[NR_SOFTIRQS] = {
  19399. "HI", "TIMER", "NET_TX", "NET_RX", "BLOCK", "BLOCK_IOPOLL",
  19400. "TASKLET", "SCHED", "HRTIMER", "RCU"
  19401. };
  19402. +#ifdef CONFIG_NO_HZ_COMMON
  19403. +# ifdef CONFIG_PREEMPT_RT_FULL
  19404. +
  19405. +struct softirq_runner {
  19406. + struct task_struct *runner[NR_SOFTIRQS];
  19407. +};
  19408. +
  19409. +static DEFINE_PER_CPU(struct softirq_runner, softirq_runners);
  19410. +
  19411. +static inline void softirq_set_runner(unsigned int sirq)
  19412. +{
  19413. + struct softirq_runner *sr = this_cpu_ptr(&softirq_runners);
  19414. +
  19415. + sr->runner[sirq] = current;
  19416. +}
  19417. +
  19418. +static inline void softirq_clr_runner(unsigned int sirq)
  19419. +{
  19420. + struct softirq_runner *sr = this_cpu_ptr(&softirq_runners);
  19421. +
  19422. + sr->runner[sirq] = NULL;
  19423. +}
  19424. +
  19425. +/*
  19426. + * On preempt-rt a softirq running context might be blocked on a
  19427. + * lock. There might be no other runnable task on this CPU because the
  19428. + * lock owner runs on some other CPU. So we have to go into idle with
  19429. + * the pending bit set. Therefor we need to check this otherwise we
  19430. + * warn about false positives which confuses users and defeats the
  19431. + * whole purpose of this test.
  19432. + *
  19433. + * This code is called with interrupts disabled.
  19434. + */
  19435. +void softirq_check_pending_idle(void)
  19436. +{
  19437. + static int rate_limit;
  19438. + struct softirq_runner *sr = this_cpu_ptr(&softirq_runners);
  19439. + u32 warnpending;
  19440. + int i;
  19441. +
  19442. + if (rate_limit >= 10)
  19443. + return;
  19444. +
  19445. + warnpending = local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK;
  19446. + for (i = 0; i < NR_SOFTIRQS; i++) {
  19447. + struct task_struct *tsk = sr->runner[i];
  19448. +
  19449. + /*
  19450. + * The wakeup code in rtmutex.c wakes up the task
  19451. + * _before_ it sets pi_blocked_on to NULL under
  19452. + * tsk->pi_lock. So we need to check for both: state
  19453. + * and pi_blocked_on.
  19454. + */
  19455. + if (tsk) {
  19456. + raw_spin_lock(&tsk->pi_lock);
  19457. + if (tsk->pi_blocked_on || tsk->state == TASK_RUNNING) {
  19458. + /* Clear all bits pending in that task */
  19459. + warnpending &= ~(tsk->softirqs_raised);
  19460. + warnpending &= ~(1 << i);
  19461. + }
  19462. + raw_spin_unlock(&tsk->pi_lock);
  19463. + }
  19464. + }
  19465. +
  19466. + if (warnpending) {
  19467. + printk(KERN_ERR "NOHZ: local_softirq_pending %02x\n",
  19468. + warnpending);
  19469. + rate_limit++;
  19470. + }
  19471. +}
  19472. +# else
  19473. +/*
  19474. + * On !PREEMPT_RT we just printk rate limited:
  19475. + */
  19476. +void softirq_check_pending_idle(void)
  19477. +{
  19478. + static int rate_limit;
  19479. +
  19480. + if (rate_limit < 10 &&
  19481. + (local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK)) {
  19482. + printk(KERN_ERR "NOHZ: local_softirq_pending %02x\n",
  19483. + local_softirq_pending());
  19484. + rate_limit++;
  19485. + }
  19486. +}
  19487. +# endif
  19488. +
  19489. +#else /* !CONFIG_NO_HZ_COMMON */
  19490. +static inline void softirq_set_runner(unsigned int sirq) { }
  19491. +static inline void softirq_clr_runner(unsigned int sirq) { }
  19492. +#endif
  19493. +
  19494. /*
  19495. * we cannot loop indefinitely here to avoid userspace starvation,
  19496. * but we also don't want to introduce a worst case 1/HZ latency
  19497. @@ -77,6 +175,79 @@
  19498. wake_up_process(tsk);
  19499. }
  19500. +#ifdef CONFIG_PREEMPT_RT_FULL
  19501. +static void wakeup_timer_softirqd(void)
  19502. +{
  19503. + /* Interrupts are disabled: no need to stop preemption */
  19504. + struct task_struct *tsk = __this_cpu_read(ktimer_softirqd);
  19505. +
  19506. + if (tsk && tsk->state != TASK_RUNNING)
  19507. + wake_up_process(tsk);
  19508. +}
  19509. +#endif
  19510. +
  19511. +static void handle_softirq(unsigned int vec_nr)
  19512. +{
  19513. + struct softirq_action *h = softirq_vec + vec_nr;
  19514. + int prev_count;
  19515. +
  19516. + prev_count = preempt_count();
  19517. +
  19518. + kstat_incr_softirqs_this_cpu(vec_nr);
  19519. +
  19520. + trace_softirq_entry(vec_nr);
  19521. + h->action(h);
  19522. + trace_softirq_exit(vec_nr);
  19523. + if (unlikely(prev_count != preempt_count())) {
  19524. + pr_err("huh, entered softirq %u %s %p with preempt_count %08x, exited with %08x?\n",
  19525. + vec_nr, softirq_to_name[vec_nr], h->action,
  19526. + prev_count, preempt_count());
  19527. + preempt_count_set(prev_count);
  19528. + }
  19529. +}
  19530. +
  19531. +#ifndef CONFIG_PREEMPT_RT_FULL
  19532. +static inline int ksoftirqd_softirq_pending(void)
  19533. +{
  19534. + return local_softirq_pending();
  19535. +}
  19536. +
  19537. +static void handle_pending_softirqs(u32 pending)
  19538. +{
  19539. + struct softirq_action *h = softirq_vec;
  19540. + int softirq_bit;
  19541. +
  19542. + local_irq_enable();
  19543. +
  19544. + h = softirq_vec;
  19545. +
  19546. + while ((softirq_bit = ffs(pending))) {
  19547. + unsigned int vec_nr;
  19548. +
  19549. + h += softirq_bit - 1;
  19550. + vec_nr = h - softirq_vec;
  19551. + handle_softirq(vec_nr);
  19552. +
  19553. + h++;
  19554. + pending >>= softirq_bit;
  19555. + }
  19556. +
  19557. + rcu_bh_qs();
  19558. + local_irq_disable();
  19559. +}
  19560. +
  19561. +static void run_ksoftirqd(unsigned int cpu)
  19562. +{
  19563. + local_irq_disable();
  19564. + if (ksoftirqd_softirq_pending()) {
  19565. + __do_softirq();
  19566. + local_irq_enable();
  19567. + cond_resched_rcu_qs();
  19568. + return;
  19569. + }
  19570. + local_irq_enable();
  19571. +}
  19572. +
  19573. /*
  19574. * preempt_count and SOFTIRQ_OFFSET usage:
  19575. * - preempt_count is changed by SOFTIRQ_OFFSET on entering or leaving
  19576. @@ -116,9 +287,9 @@
  19577. if (preempt_count() == cnt) {
  19578. #ifdef CONFIG_DEBUG_PREEMPT
  19579. - current->preempt_disable_ip = get_parent_ip(CALLER_ADDR1);
  19580. + current->preempt_disable_ip = get_lock_parent_ip();
  19581. #endif
  19582. - trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
  19583. + trace_preempt_off(CALLER_ADDR0, get_lock_parent_ip());
  19584. }
  19585. }
  19586. EXPORT_SYMBOL(__local_bh_disable_ip);
  19587. @@ -232,10 +403,8 @@
  19588. unsigned long end = jiffies + MAX_SOFTIRQ_TIME;
  19589. unsigned long old_flags = current->flags;
  19590. int max_restart = MAX_SOFTIRQ_RESTART;
  19591. - struct softirq_action *h;
  19592. bool in_hardirq;
  19593. __u32 pending;
  19594. - int softirq_bit;
  19595. /*
  19596. * Mask out PF_MEMALLOC s current task context is borrowed for the
  19597. @@ -254,36 +423,7 @@
  19598. /* Reset the pending bitmask before enabling irqs */
  19599. set_softirq_pending(0);
  19600. - local_irq_enable();
  19601. -
  19602. - h = softirq_vec;
  19603. -
  19604. - while ((softirq_bit = ffs(pending))) {
  19605. - unsigned int vec_nr;
  19606. - int prev_count;
  19607. -
  19608. - h += softirq_bit - 1;
  19609. -
  19610. - vec_nr = h - softirq_vec;
  19611. - prev_count = preempt_count();
  19612. -
  19613. - kstat_incr_softirqs_this_cpu(vec_nr);
  19614. -
  19615. - trace_softirq_entry(vec_nr);
  19616. - h->action(h);
  19617. - trace_softirq_exit(vec_nr);
  19618. - if (unlikely(prev_count != preempt_count())) {
  19619. - pr_err("huh, entered softirq %u %s %p with preempt_count %08x, exited with %08x?\n",
  19620. - vec_nr, softirq_to_name[vec_nr], h->action,
  19621. - prev_count, preempt_count());
  19622. - preempt_count_set(prev_count);
  19623. - }
  19624. - h++;
  19625. - pending >>= softirq_bit;
  19626. - }
  19627. -
  19628. - rcu_bh_qs();
  19629. - local_irq_disable();
  19630. + handle_pending_softirqs(pending);
  19631. pending = local_softirq_pending();
  19632. if (pending) {
  19633. @@ -320,6 +460,310 @@
  19634. }
  19635. /*
  19636. + * This function must run with irqs disabled!
  19637. + */
  19638. +void raise_softirq_irqoff(unsigned int nr)
  19639. +{
  19640. + __raise_softirq_irqoff(nr);
  19641. +
  19642. + /*
  19643. + * If we're in an interrupt or softirq, we're done
  19644. + * (this also catches softirq-disabled code). We will
  19645. + * actually run the softirq once we return from
  19646. + * the irq or softirq.
  19647. + *
  19648. + * Otherwise we wake up ksoftirqd to make sure we
  19649. + * schedule the softirq soon.
  19650. + */
  19651. + if (!in_interrupt())
  19652. + wakeup_softirqd();
  19653. +}
  19654. +
  19655. +void __raise_softirq_irqoff(unsigned int nr)
  19656. +{
  19657. + trace_softirq_raise(nr);
  19658. + or_softirq_pending(1UL << nr);
  19659. +}
  19660. +
  19661. +static inline void local_bh_disable_nort(void) { local_bh_disable(); }
  19662. +static inline void _local_bh_enable_nort(void) { _local_bh_enable(); }
  19663. +static void ksoftirqd_set_sched_params(unsigned int cpu) { }
  19664. +
  19665. +#else /* !PREEMPT_RT_FULL */
  19666. +
  19667. +/*
  19668. + * On RT we serialize softirq execution with a cpu local lock per softirq
  19669. + */
  19670. +static DEFINE_PER_CPU(struct local_irq_lock [NR_SOFTIRQS], local_softirq_locks);
  19671. +
  19672. +void __init softirq_early_init(void)
  19673. +{
  19674. + int i;
  19675. +
  19676. + for (i = 0; i < NR_SOFTIRQS; i++)
  19677. + local_irq_lock_init(local_softirq_locks[i]);
  19678. +}
  19679. +
  19680. +static void lock_softirq(int which)
  19681. +{
  19682. + local_lock(local_softirq_locks[which]);
  19683. +}
  19684. +
  19685. +static void unlock_softirq(int which)
  19686. +{
  19687. + local_unlock(local_softirq_locks[which]);
  19688. +}
  19689. +
  19690. +static void do_single_softirq(int which)
  19691. +{
  19692. + unsigned long old_flags = current->flags;
  19693. +
  19694. + current->flags &= ~PF_MEMALLOC;
  19695. + vtime_account_irq_enter(current);
  19696. + current->flags |= PF_IN_SOFTIRQ;
  19697. + lockdep_softirq_enter();
  19698. + local_irq_enable();
  19699. + handle_softirq(which);
  19700. + local_irq_disable();
  19701. + lockdep_softirq_exit();
  19702. + current->flags &= ~PF_IN_SOFTIRQ;
  19703. + vtime_account_irq_enter(current);
  19704. + tsk_restore_flags(current, old_flags, PF_MEMALLOC);
  19705. +}
  19706. +
  19707. +/*
  19708. + * Called with interrupts disabled. Process softirqs which were raised
  19709. + * in current context (or on behalf of ksoftirqd).
  19710. + */
  19711. +static void do_current_softirqs(void)
  19712. +{
  19713. + while (current->softirqs_raised) {
  19714. + int i = __ffs(current->softirqs_raised);
  19715. + unsigned int pending, mask = (1U << i);
  19716. +
  19717. + current->softirqs_raised &= ~mask;
  19718. + local_irq_enable();
  19719. +
  19720. + /*
  19721. + * If the lock is contended, we boost the owner to
  19722. + * process the softirq or leave the critical section
  19723. + * now.
  19724. + */
  19725. + lock_softirq(i);
  19726. + local_irq_disable();
  19727. + softirq_set_runner(i);
  19728. + /*
  19729. + * Check with the local_softirq_pending() bits,
  19730. + * whether we need to process this still or if someone
  19731. + * else took care of it.
  19732. + */
  19733. + pending = local_softirq_pending();
  19734. + if (pending & mask) {
  19735. + set_softirq_pending(pending & ~mask);
  19736. + do_single_softirq(i);
  19737. + }
  19738. + softirq_clr_runner(i);
  19739. + WARN_ON(current->softirq_nestcnt != 1);
  19740. + local_irq_enable();
  19741. + unlock_softirq(i);
  19742. + local_irq_disable();
  19743. + }
  19744. +}
  19745. +
  19746. +void __local_bh_disable(void)
  19747. +{
  19748. + if (++current->softirq_nestcnt == 1)
  19749. + migrate_disable();
  19750. +}
  19751. +EXPORT_SYMBOL(__local_bh_disable);
  19752. +
  19753. +void __local_bh_enable(void)
  19754. +{
  19755. + if (WARN_ON(current->softirq_nestcnt == 0))
  19756. + return;
  19757. +
  19758. + local_irq_disable();
  19759. + if (current->softirq_nestcnt == 1 && current->softirqs_raised)
  19760. + do_current_softirqs();
  19761. + local_irq_enable();
  19762. +
  19763. + if (--current->softirq_nestcnt == 0)
  19764. + migrate_enable();
  19765. +}
  19766. +EXPORT_SYMBOL(__local_bh_enable);
  19767. +
  19768. +void _local_bh_enable(void)
  19769. +{
  19770. + if (WARN_ON(current->softirq_nestcnt == 0))
  19771. + return;
  19772. + if (--current->softirq_nestcnt == 0)
  19773. + migrate_enable();
  19774. +}
  19775. +EXPORT_SYMBOL(_local_bh_enable);
  19776. +
  19777. +int in_serving_softirq(void)
  19778. +{
  19779. + return current->flags & PF_IN_SOFTIRQ;
  19780. +}
  19781. +EXPORT_SYMBOL(in_serving_softirq);
  19782. +
  19783. +/* Called with preemption disabled */
  19784. +static void run_ksoftirqd(unsigned int cpu)
  19785. +{
  19786. + local_irq_disable();
  19787. + current->softirq_nestcnt++;
  19788. +
  19789. + do_current_softirqs();
  19790. + current->softirq_nestcnt--;
  19791. + local_irq_enable();
  19792. + cond_resched_rcu_qs();
  19793. +}
  19794. +
  19795. +/*
  19796. + * Called from netif_rx_ni(). Preemption enabled, but migration
  19797. + * disabled. So the cpu can't go away under us.
  19798. + */
  19799. +void thread_do_softirq(void)
  19800. +{
  19801. + if (!in_serving_softirq() && current->softirqs_raised) {
  19802. + current->softirq_nestcnt++;
  19803. + do_current_softirqs();
  19804. + current->softirq_nestcnt--;
  19805. + }
  19806. +}
  19807. +
  19808. +static void do_raise_softirq_irqoff(unsigned int nr)
  19809. +{
  19810. + unsigned int mask;
  19811. +
  19812. + mask = 1UL << nr;
  19813. +
  19814. + trace_softirq_raise(nr);
  19815. + or_softirq_pending(mask);
  19816. +
  19817. + /*
  19818. + * If we are not in a hard interrupt and inside a bh disabled
  19819. + * region, we simply raise the flag on current. local_bh_enable()
  19820. + * will make sure that the softirq is executed. Otherwise we
  19821. + * delegate it to ksoftirqd.
  19822. + */
  19823. + if (!in_irq() && current->softirq_nestcnt)
  19824. + current->softirqs_raised |= mask;
  19825. + else if (!__this_cpu_read(ksoftirqd) || !__this_cpu_read(ktimer_softirqd))
  19826. + return;
  19827. +
  19828. + if (mask & TIMER_SOFTIRQS)
  19829. + __this_cpu_read(ktimer_softirqd)->softirqs_raised |= mask;
  19830. + else
  19831. + __this_cpu_read(ksoftirqd)->softirqs_raised |= mask;
  19832. +}
  19833. +
  19834. +static void wakeup_proper_softirq(unsigned int nr)
  19835. +{
  19836. + if ((1UL << nr) & TIMER_SOFTIRQS)
  19837. + wakeup_timer_softirqd();
  19838. + else
  19839. + wakeup_softirqd();
  19840. +}
  19841. +
  19842. +
  19843. +void __raise_softirq_irqoff(unsigned int nr)
  19844. +{
  19845. + do_raise_softirq_irqoff(nr);
  19846. + if (!in_irq() && !current->softirq_nestcnt)
  19847. + wakeup_proper_softirq(nr);
  19848. +}
  19849. +
  19850. +/*
  19851. + * Same as __raise_softirq_irqoff() but will process them in ksoftirqd
  19852. + */
  19853. +void __raise_softirq_irqoff_ksoft(unsigned int nr)
  19854. +{
  19855. + unsigned int mask;
  19856. +
  19857. + if (WARN_ON_ONCE(!__this_cpu_read(ksoftirqd) ||
  19858. + !__this_cpu_read(ktimer_softirqd)))
  19859. + return;
  19860. + mask = 1UL << nr;
  19861. +
  19862. + trace_softirq_raise(nr);
  19863. + or_softirq_pending(mask);
  19864. + if (mask & TIMER_SOFTIRQS)
  19865. + __this_cpu_read(ktimer_softirqd)->softirqs_raised |= mask;
  19866. + else
  19867. + __this_cpu_read(ksoftirqd)->softirqs_raised |= mask;
  19868. + wakeup_proper_softirq(nr);
  19869. +}
  19870. +
  19871. +/*
  19872. + * This function must run with irqs disabled!
  19873. + */
  19874. +void raise_softirq_irqoff(unsigned int nr)
  19875. +{
  19876. + do_raise_softirq_irqoff(nr);
  19877. +
  19878. + /*
  19879. + * If we're in an hard interrupt we let irq return code deal
  19880. + * with the wakeup of ksoftirqd.
  19881. + */
  19882. + if (in_irq())
  19883. + return;
  19884. + /*
  19885. + * If we are in thread context but outside of a bh disabled
  19886. + * region, we need to wake ksoftirqd as well.
  19887. + *
  19888. + * CHECKME: Some of the places which do that could be wrapped
  19889. + * into local_bh_disable/enable pairs. Though it's unclear
  19890. + * whether this is worth the effort. To find those places just
  19891. + * raise a WARN() if the condition is met.
  19892. + */
  19893. + if (!current->softirq_nestcnt)
  19894. + wakeup_proper_softirq(nr);
  19895. +}
  19896. +
  19897. +static inline int ksoftirqd_softirq_pending(void)
  19898. +{
  19899. + return current->softirqs_raised;
  19900. +}
  19901. +
  19902. +static inline void local_bh_disable_nort(void) { }
  19903. +static inline void _local_bh_enable_nort(void) { }
  19904. +
  19905. +static inline void ksoftirqd_set_sched_params(unsigned int cpu)
  19906. +{
  19907. + /* Take over all but timer pending softirqs when starting */
  19908. + local_irq_disable();
  19909. + current->softirqs_raised = local_softirq_pending() & ~TIMER_SOFTIRQS;
  19910. + local_irq_enable();
  19911. +}
  19912. +
  19913. +static inline void ktimer_softirqd_set_sched_params(unsigned int cpu)
  19914. +{
  19915. + struct sched_param param = { .sched_priority = 1 };
  19916. +
  19917. + sched_setscheduler(current, SCHED_FIFO, &param);
  19918. +
  19919. + /* Take over timer pending softirqs when starting */
  19920. + local_irq_disable();
  19921. + current->softirqs_raised = local_softirq_pending() & TIMER_SOFTIRQS;
  19922. + local_irq_enable();
  19923. +}
  19924. +
  19925. +static inline void ktimer_softirqd_clr_sched_params(unsigned int cpu,
  19926. + bool online)
  19927. +{
  19928. + struct sched_param param = { .sched_priority = 0 };
  19929. +
  19930. + sched_setscheduler(current, SCHED_NORMAL, &param);
  19931. +}
  19932. +
  19933. +static int ktimer_softirqd_should_run(unsigned int cpu)
  19934. +{
  19935. + return current->softirqs_raised;
  19936. +}
  19937. +
  19938. +#endif /* PREEMPT_RT_FULL */
  19939. +/*
  19940. * Enter an interrupt context.
  19941. */
  19942. void irq_enter(void)
  19943. @@ -330,9 +774,9 @@
  19944. * Prevent raise_softirq from needlessly waking up ksoftirqd
  19945. * here, as softirq will be serviced on return from interrupt.
  19946. */
  19947. - local_bh_disable();
  19948. + local_bh_disable_nort();
  19949. tick_irq_enter();
  19950. - _local_bh_enable();
  19951. + _local_bh_enable_nort();
  19952. }
  19953. __irq_enter();
  19954. @@ -340,6 +784,7 @@
  19955. static inline void invoke_softirq(void)
  19956. {
  19957. +#ifndef CONFIG_PREEMPT_RT_FULL
  19958. if (!force_irqthreads) {
  19959. #ifdef CONFIG_HAVE_IRQ_EXIT_ON_IRQ_STACK
  19960. /*
  19961. @@ -359,6 +804,18 @@
  19962. } else {
  19963. wakeup_softirqd();
  19964. }
  19965. +#else /* PREEMPT_RT_FULL */
  19966. + unsigned long flags;
  19967. +
  19968. + local_irq_save(flags);
  19969. + if (__this_cpu_read(ksoftirqd) &&
  19970. + __this_cpu_read(ksoftirqd)->softirqs_raised)
  19971. + wakeup_softirqd();
  19972. + if (__this_cpu_read(ktimer_softirqd) &&
  19973. + __this_cpu_read(ktimer_softirqd)->softirqs_raised)
  19974. + wakeup_timer_softirqd();
  19975. + local_irq_restore(flags);
  19976. +#endif
  19977. }
  19978. static inline void tick_irq_exit(void)
  19979. @@ -395,26 +852,6 @@
  19980. trace_hardirq_exit(); /* must be last! */
  19981. }
  19982. -/*
  19983. - * This function must run with irqs disabled!
  19984. - */
  19985. -inline void raise_softirq_irqoff(unsigned int nr)
  19986. -{
  19987. - __raise_softirq_irqoff(nr);
  19988. -
  19989. - /*
  19990. - * If we're in an interrupt or softirq, we're done
  19991. - * (this also catches softirq-disabled code). We will
  19992. - * actually run the softirq once we return from
  19993. - * the irq or softirq.
  19994. - *
  19995. - * Otherwise we wake up ksoftirqd to make sure we
  19996. - * schedule the softirq soon.
  19997. - */
  19998. - if (!in_interrupt())
  19999. - wakeup_softirqd();
  20000. -}
  20001. -
  20002. void raise_softirq(unsigned int nr)
  20003. {
  20004. unsigned long flags;
  20005. @@ -424,12 +861,6 @@
  20006. local_irq_restore(flags);
  20007. }
  20008. -void __raise_softirq_irqoff(unsigned int nr)
  20009. -{
  20010. - trace_softirq_raise(nr);
  20011. - or_softirq_pending(1UL << nr);
  20012. -}
  20013. -
  20014. void open_softirq(int nr, void (*action)(struct softirq_action *))
  20015. {
  20016. softirq_vec[nr].action = action;
  20017. @@ -446,15 +877,45 @@
  20018. static DEFINE_PER_CPU(struct tasklet_head, tasklet_vec);
  20019. static DEFINE_PER_CPU(struct tasklet_head, tasklet_hi_vec);
  20020. +static void inline
  20021. +__tasklet_common_schedule(struct tasklet_struct *t, struct tasklet_head *head, unsigned int nr)
  20022. +{
  20023. + if (tasklet_trylock(t)) {
  20024. +again:
  20025. + /* We may have been preempted before tasklet_trylock
  20026. + * and __tasklet_action may have already run.
  20027. + * So double check the sched bit while the takslet
  20028. + * is locked before adding it to the list.
  20029. + */
  20030. + if (test_bit(TASKLET_STATE_SCHED, &t->state)) {
  20031. + t->next = NULL;
  20032. + *head->tail = t;
  20033. + head->tail = &(t->next);
  20034. + raise_softirq_irqoff(nr);
  20035. + tasklet_unlock(t);
  20036. + } else {
  20037. + /* This is subtle. If we hit the corner case above
  20038. + * It is possible that we get preempted right here,
  20039. + * and another task has successfully called
  20040. + * tasklet_schedule(), then this function, and
  20041. + * failed on the trylock. Thus we must be sure
  20042. + * before releasing the tasklet lock, that the
  20043. + * SCHED_BIT is clear. Otherwise the tasklet
  20044. + * may get its SCHED_BIT set, but not added to the
  20045. + * list
  20046. + */
  20047. + if (!tasklet_tryunlock(t))
  20048. + goto again;
  20049. + }
  20050. + }
  20051. +}
  20052. +
  20053. void __tasklet_schedule(struct tasklet_struct *t)
  20054. {
  20055. unsigned long flags;
  20056. local_irq_save(flags);
  20057. - t->next = NULL;
  20058. - *__this_cpu_read(tasklet_vec.tail) = t;
  20059. - __this_cpu_write(tasklet_vec.tail, &(t->next));
  20060. - raise_softirq_irqoff(TASKLET_SOFTIRQ);
  20061. + __tasklet_common_schedule(t, this_cpu_ptr(&tasklet_vec), TASKLET_SOFTIRQ);
  20062. local_irq_restore(flags);
  20063. }
  20064. EXPORT_SYMBOL(__tasklet_schedule);
  20065. @@ -464,10 +925,7 @@
  20066. unsigned long flags;
  20067. local_irq_save(flags);
  20068. - t->next = NULL;
  20069. - *__this_cpu_read(tasklet_hi_vec.tail) = t;
  20070. - __this_cpu_write(tasklet_hi_vec.tail, &(t->next));
  20071. - raise_softirq_irqoff(HI_SOFTIRQ);
  20072. + __tasklet_common_schedule(t, this_cpu_ptr(&tasklet_hi_vec), HI_SOFTIRQ);
  20073. local_irq_restore(flags);
  20074. }
  20075. EXPORT_SYMBOL(__tasklet_hi_schedule);
  20076. @@ -476,82 +934,122 @@
  20077. {
  20078. BUG_ON(!irqs_disabled());
  20079. - t->next = __this_cpu_read(tasklet_hi_vec.head);
  20080. - __this_cpu_write(tasklet_hi_vec.head, t);
  20081. - __raise_softirq_irqoff(HI_SOFTIRQ);
  20082. + __tasklet_hi_schedule(t);
  20083. }
  20084. EXPORT_SYMBOL(__tasklet_hi_schedule_first);
  20085. -static void tasklet_action(struct softirq_action *a)
  20086. +void tasklet_enable(struct tasklet_struct *t)
  20087. {
  20088. - struct tasklet_struct *list;
  20089. + if (!atomic_dec_and_test(&t->count))
  20090. + return;
  20091. + if (test_and_clear_bit(TASKLET_STATE_PENDING, &t->state))
  20092. + tasklet_schedule(t);
  20093. +}
  20094. +EXPORT_SYMBOL(tasklet_enable);
  20095. - local_irq_disable();
  20096. - list = __this_cpu_read(tasklet_vec.head);
  20097. - __this_cpu_write(tasklet_vec.head, NULL);
  20098. - __this_cpu_write(tasklet_vec.tail, this_cpu_ptr(&tasklet_vec.head));
  20099. - local_irq_enable();
  20100. +static void __tasklet_action(struct softirq_action *a,
  20101. + struct tasklet_struct *list)
  20102. +{
  20103. + int loops = 1000000;
  20104. while (list) {
  20105. struct tasklet_struct *t = list;
  20106. list = list->next;
  20107. - if (tasklet_trylock(t)) {
  20108. - if (!atomic_read(&t->count)) {
  20109. - if (!test_and_clear_bit(TASKLET_STATE_SCHED,
  20110. - &t->state))
  20111. - BUG();
  20112. - t->func(t->data);
  20113. - tasklet_unlock(t);
  20114. - continue;
  20115. - }
  20116. - tasklet_unlock(t);
  20117. + /*
  20118. + * Should always succeed - after a tasklist got on the
  20119. + * list (after getting the SCHED bit set from 0 to 1),
  20120. + * nothing but the tasklet softirq it got queued to can
  20121. + * lock it:
  20122. + */
  20123. + if (!tasklet_trylock(t)) {
  20124. + WARN_ON(1);
  20125. + continue;
  20126. }
  20127. - local_irq_disable();
  20128. t->next = NULL;
  20129. - *__this_cpu_read(tasklet_vec.tail) = t;
  20130. - __this_cpu_write(tasklet_vec.tail, &(t->next));
  20131. - __raise_softirq_irqoff(TASKLET_SOFTIRQ);
  20132. - local_irq_enable();
  20133. +
  20134. + /*
  20135. + * If we cannot handle the tasklet because it's disabled,
  20136. + * mark it as pending. tasklet_enable() will later
  20137. + * re-schedule the tasklet.
  20138. + */
  20139. + if (unlikely(atomic_read(&t->count))) {
  20140. +out_disabled:
  20141. + /* implicit unlock: */
  20142. + wmb();
  20143. + t->state = TASKLET_STATEF_PENDING;
  20144. + continue;
  20145. + }
  20146. +
  20147. + /*
  20148. + * After this point on the tasklet might be rescheduled
  20149. + * on another CPU, but it can only be added to another
  20150. + * CPU's tasklet list if we unlock the tasklet (which we
  20151. + * dont do yet).
  20152. + */
  20153. + if (!test_and_clear_bit(TASKLET_STATE_SCHED, &t->state))
  20154. + WARN_ON(1);
  20155. +
  20156. +again:
  20157. + t->func(t->data);
  20158. +
  20159. + /*
  20160. + * Try to unlock the tasklet. We must use cmpxchg, because
  20161. + * another CPU might have scheduled or disabled the tasklet.
  20162. + * We only allow the STATE_RUN -> 0 transition here.
  20163. + */
  20164. + while (!tasklet_tryunlock(t)) {
  20165. + /*
  20166. + * If it got disabled meanwhile, bail out:
  20167. + */
  20168. + if (atomic_read(&t->count))
  20169. + goto out_disabled;
  20170. + /*
  20171. + * If it got scheduled meanwhile, re-execute
  20172. + * the tasklet function:
  20173. + */
  20174. + if (test_and_clear_bit(TASKLET_STATE_SCHED, &t->state))
  20175. + goto again;
  20176. + if (!--loops) {
  20177. + printk("hm, tasklet state: %08lx\n", t->state);
  20178. + WARN_ON(1);
  20179. + tasklet_unlock(t);
  20180. + break;
  20181. + }
  20182. + }
  20183. }
  20184. }
  20185. +static void tasklet_action(struct softirq_action *a)
  20186. +{
  20187. + struct tasklet_struct *list;
  20188. +
  20189. + local_irq_disable();
  20190. +
  20191. + list = __this_cpu_read(tasklet_vec.head);
  20192. + __this_cpu_write(tasklet_vec.head, NULL);
  20193. + __this_cpu_write(tasklet_vec.tail, this_cpu_ptr(&tasklet_vec.head));
  20194. +
  20195. + local_irq_enable();
  20196. +
  20197. + __tasklet_action(a, list);
  20198. +}
  20199. +
  20200. static void tasklet_hi_action(struct softirq_action *a)
  20201. {
  20202. struct tasklet_struct *list;
  20203. local_irq_disable();
  20204. +
  20205. list = __this_cpu_read(tasklet_hi_vec.head);
  20206. __this_cpu_write(tasklet_hi_vec.head, NULL);
  20207. __this_cpu_write(tasklet_hi_vec.tail, this_cpu_ptr(&tasklet_hi_vec.head));
  20208. - local_irq_enable();
  20209. - while (list) {
  20210. - struct tasklet_struct *t = list;
  20211. -
  20212. - list = list->next;
  20213. -
  20214. - if (tasklet_trylock(t)) {
  20215. - if (!atomic_read(&t->count)) {
  20216. - if (!test_and_clear_bit(TASKLET_STATE_SCHED,
  20217. - &t->state))
  20218. - BUG();
  20219. - t->func(t->data);
  20220. - tasklet_unlock(t);
  20221. - continue;
  20222. - }
  20223. - tasklet_unlock(t);
  20224. - }
  20225. + local_irq_enable();
  20226. - local_irq_disable();
  20227. - t->next = NULL;
  20228. - *__this_cpu_read(tasklet_hi_vec.tail) = t;
  20229. - __this_cpu_write(tasklet_hi_vec.tail, &(t->next));
  20230. - __raise_softirq_irqoff(HI_SOFTIRQ);
  20231. - local_irq_enable();
  20232. - }
  20233. + __tasklet_action(a, list);
  20234. }
  20235. void tasklet_init(struct tasklet_struct *t,
  20236. @@ -572,7 +1070,7 @@
  20237. while (test_and_set_bit(TASKLET_STATE_SCHED, &t->state)) {
  20238. do {
  20239. - yield();
  20240. + msleep(1);
  20241. } while (test_bit(TASKLET_STATE_SCHED, &t->state));
  20242. }
  20243. tasklet_unlock_wait(t);
  20244. @@ -646,25 +1144,26 @@
  20245. open_softirq(HI_SOFTIRQ, tasklet_hi_action);
  20246. }
  20247. -static int ksoftirqd_should_run(unsigned int cpu)
  20248. -{
  20249. - return local_softirq_pending();
  20250. -}
  20251. -
  20252. -static void run_ksoftirqd(unsigned int cpu)
  20253. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  20254. +void tasklet_unlock_wait(struct tasklet_struct *t)
  20255. {
  20256. - local_irq_disable();
  20257. - if (local_softirq_pending()) {
  20258. + while (test_bit(TASKLET_STATE_RUN, &(t)->state)) {
  20259. /*
  20260. - * We can safely run softirq on inline stack, as we are not deep
  20261. - * in the task stack here.
  20262. + * Hack for now to avoid this busy-loop:
  20263. */
  20264. - __do_softirq();
  20265. - local_irq_enable();
  20266. - cond_resched_rcu_qs();
  20267. - return;
  20268. +#ifdef CONFIG_PREEMPT_RT_FULL
  20269. + msleep(1);
  20270. +#else
  20271. + barrier();
  20272. +#endif
  20273. }
  20274. - local_irq_enable();
  20275. +}
  20276. +EXPORT_SYMBOL(tasklet_unlock_wait);
  20277. +#endif
  20278. +
  20279. +static int ksoftirqd_should_run(unsigned int cpu)
  20280. +{
  20281. + return ksoftirqd_softirq_pending();
  20282. }
  20283. #ifdef CONFIG_HOTPLUG_CPU
  20284. @@ -746,16 +1245,31 @@
  20285. static struct smp_hotplug_thread softirq_threads = {
  20286. .store = &ksoftirqd,
  20287. + .setup = ksoftirqd_set_sched_params,
  20288. .thread_should_run = ksoftirqd_should_run,
  20289. .thread_fn = run_ksoftirqd,
  20290. .thread_comm = "ksoftirqd/%u",
  20291. };
  20292. +#ifdef CONFIG_PREEMPT_RT_FULL
  20293. +static struct smp_hotplug_thread softirq_timer_threads = {
  20294. + .store = &ktimer_softirqd,
  20295. + .setup = ktimer_softirqd_set_sched_params,
  20296. + .cleanup = ktimer_softirqd_clr_sched_params,
  20297. + .thread_should_run = ktimer_softirqd_should_run,
  20298. + .thread_fn = run_ksoftirqd,
  20299. + .thread_comm = "ktimersoftd/%u",
  20300. +};
  20301. +#endif
  20302. +
  20303. static __init int spawn_ksoftirqd(void)
  20304. {
  20305. register_cpu_notifier(&cpu_nfb);
  20306. BUG_ON(smpboot_register_percpu_thread(&softirq_threads));
  20307. +#ifdef CONFIG_PREEMPT_RT_FULL
  20308. + BUG_ON(smpboot_register_percpu_thread(&softirq_timer_threads));
  20309. +#endif
  20310. return 0;
  20311. }
  20312. diff -Nur linux-4.1.20.orig/kernel/stop_machine.c linux-4.1.20/kernel/stop_machine.c
  20313. --- linux-4.1.20.orig/kernel/stop_machine.c 2016-03-17 19:11:03.000000000 +0100
  20314. +++ linux-4.1.20/kernel/stop_machine.c 2016-03-21 20:18:33.000000000 +0100
  20315. @@ -35,7 +35,7 @@
  20316. /* the actual stopper, one per every possible cpu, enabled on online cpus */
  20317. struct cpu_stopper {
  20318. - spinlock_t lock;
  20319. + raw_spinlock_t lock;
  20320. bool enabled; /* is this stopper enabled? */
  20321. struct list_head works; /* list of pending works */
  20322. };
  20323. @@ -78,7 +78,7 @@
  20324. unsigned long flags;
  20325. - spin_lock_irqsave(&stopper->lock, flags);
  20326. + raw_spin_lock_irqsave(&stopper->lock, flags);
  20327. if (stopper->enabled) {
  20328. list_add_tail(&work->list, &stopper->works);
  20329. @@ -86,7 +86,7 @@
  20330. } else
  20331. cpu_stop_signal_done(work->done, false);
  20332. - spin_unlock_irqrestore(&stopper->lock, flags);
  20333. + raw_spin_unlock_irqrestore(&stopper->lock, flags);
  20334. }
  20335. /**
  20336. @@ -248,7 +248,7 @@
  20337. struct irq_cpu_stop_queue_work_info call_args;
  20338. struct multi_stop_data msdata;
  20339. - preempt_disable();
  20340. + preempt_disable_nort();
  20341. msdata = (struct multi_stop_data){
  20342. .fn = fn,
  20343. .data = arg,
  20344. @@ -281,7 +281,7 @@
  20345. * This relies on the stopper workqueues to be FIFO.
  20346. */
  20347. if (!cpu_active(cpu1) || !cpu_active(cpu2)) {
  20348. - preempt_enable();
  20349. + preempt_enable_nort();
  20350. return -ENOENT;
  20351. }
  20352. @@ -295,7 +295,7 @@
  20353. &irq_cpu_stop_queue_work,
  20354. &call_args, 1);
  20355. lg_local_unlock(&stop_cpus_lock);
  20356. - preempt_enable();
  20357. + preempt_enable_nort();
  20358. wait_for_completion(&done.completion);
  20359. @@ -329,7 +329,7 @@
  20360. static void queue_stop_cpus_work(const struct cpumask *cpumask,
  20361. cpu_stop_fn_t fn, void *arg,
  20362. - struct cpu_stop_done *done)
  20363. + struct cpu_stop_done *done, bool inactive)
  20364. {
  20365. struct cpu_stop_work *work;
  20366. unsigned int cpu;
  20367. @@ -343,11 +343,13 @@
  20368. }
  20369. /*
  20370. - * Disable preemption while queueing to avoid getting
  20371. - * preempted by a stopper which might wait for other stoppers
  20372. - * to enter @fn which can lead to deadlock.
  20373. + * Make sure that all work is queued on all cpus before
  20374. + * any of the cpus can execute it.
  20375. */
  20376. - lg_global_lock(&stop_cpus_lock);
  20377. + if (!inactive)
  20378. + lg_global_lock(&stop_cpus_lock);
  20379. + else
  20380. + lg_global_trylock_relax(&stop_cpus_lock);
  20381. for_each_cpu(cpu, cpumask)
  20382. cpu_stop_queue_work(cpu, &per_cpu(stop_cpus_work, cpu));
  20383. lg_global_unlock(&stop_cpus_lock);
  20384. @@ -359,7 +361,7 @@
  20385. struct cpu_stop_done done;
  20386. cpu_stop_init_done(&done, cpumask_weight(cpumask));
  20387. - queue_stop_cpus_work(cpumask, fn, arg, &done);
  20388. + queue_stop_cpus_work(cpumask, fn, arg, &done, false);
  20389. wait_for_completion(&done.completion);
  20390. return done.executed ? done.ret : -ENOENT;
  20391. }
  20392. @@ -439,9 +441,9 @@
  20393. unsigned long flags;
  20394. int run;
  20395. - spin_lock_irqsave(&stopper->lock, flags);
  20396. + raw_spin_lock_irqsave(&stopper->lock, flags);
  20397. run = !list_empty(&stopper->works);
  20398. - spin_unlock_irqrestore(&stopper->lock, flags);
  20399. + raw_spin_unlock_irqrestore(&stopper->lock, flags);
  20400. return run;
  20401. }
  20402. @@ -453,13 +455,13 @@
  20403. repeat:
  20404. work = NULL;
  20405. - spin_lock_irq(&stopper->lock);
  20406. + raw_spin_lock_irq(&stopper->lock);
  20407. if (!list_empty(&stopper->works)) {
  20408. work = list_first_entry(&stopper->works,
  20409. struct cpu_stop_work, list);
  20410. list_del_init(&work->list);
  20411. }
  20412. - spin_unlock_irq(&stopper->lock);
  20413. + raw_spin_unlock_irq(&stopper->lock);
  20414. if (work) {
  20415. cpu_stop_fn_t fn = work->fn;
  20416. @@ -467,6 +469,16 @@
  20417. struct cpu_stop_done *done = work->done;
  20418. char ksym_buf[KSYM_NAME_LEN] __maybe_unused;
  20419. + /*
  20420. + * Wait until the stopper finished scheduling on all
  20421. + * cpus
  20422. + */
  20423. + lg_global_lock(&stop_cpus_lock);
  20424. + /*
  20425. + * Let other cpu threads continue as well
  20426. + */
  20427. + lg_global_unlock(&stop_cpus_lock);
  20428. +
  20429. /* cpu stop callbacks are not allowed to sleep */
  20430. preempt_disable();
  20431. @@ -500,20 +512,20 @@
  20432. unsigned long flags;
  20433. /* drain remaining works */
  20434. - spin_lock_irqsave(&stopper->lock, flags);
  20435. + raw_spin_lock_irqsave(&stopper->lock, flags);
  20436. list_for_each_entry(work, &stopper->works, list)
  20437. cpu_stop_signal_done(work->done, false);
  20438. stopper->enabled = false;
  20439. - spin_unlock_irqrestore(&stopper->lock, flags);
  20440. + raw_spin_unlock_irqrestore(&stopper->lock, flags);
  20441. }
  20442. static void cpu_stop_unpark(unsigned int cpu)
  20443. {
  20444. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  20445. - spin_lock_irq(&stopper->lock);
  20446. + raw_spin_lock_irq(&stopper->lock);
  20447. stopper->enabled = true;
  20448. - spin_unlock_irq(&stopper->lock);
  20449. + raw_spin_unlock_irq(&stopper->lock);
  20450. }
  20451. static struct smp_hotplug_thread cpu_stop_threads = {
  20452. @@ -535,10 +547,12 @@
  20453. for_each_possible_cpu(cpu) {
  20454. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  20455. - spin_lock_init(&stopper->lock);
  20456. + raw_spin_lock_init(&stopper->lock);
  20457. INIT_LIST_HEAD(&stopper->works);
  20458. }
  20459. + lg_lock_init(&stop_cpus_lock, "stop_cpus_lock");
  20460. +
  20461. BUG_ON(smpboot_register_percpu_thread(&cpu_stop_threads));
  20462. stop_machine_initialized = true;
  20463. return 0;
  20464. @@ -634,7 +648,7 @@
  20465. set_state(&msdata, MULTI_STOP_PREPARE);
  20466. cpu_stop_init_done(&done, num_active_cpus());
  20467. queue_stop_cpus_work(cpu_active_mask, multi_cpu_stop, &msdata,
  20468. - &done);
  20469. + &done, true);
  20470. ret = multi_cpu_stop(&msdata);
  20471. /* Busy wait for completion. */
  20472. diff -Nur linux-4.1.20.orig/kernel/time/hrtimer.c linux-4.1.20/kernel/time/hrtimer.c
  20473. --- linux-4.1.20.orig/kernel/time/hrtimer.c 2016-03-17 19:11:03.000000000 +0100
  20474. +++ linux-4.1.20/kernel/time/hrtimer.c 2016-03-21 20:18:33.000000000 +0100
  20475. @@ -48,11 +48,13 @@
  20476. #include <linux/sched/rt.h>
  20477. #include <linux/sched/deadline.h>
  20478. #include <linux/timer.h>
  20479. +#include <linux/kthread.h>
  20480. #include <linux/freezer.h>
  20481. #include <asm/uaccess.h>
  20482. #include <trace/events/timer.h>
  20483. +#include <trace/events/hist.h>
  20484. #include "tick-internal.h"
  20485. @@ -576,8 +578,7 @@
  20486. * When the callback is running, we do not reprogram the clock event
  20487. * device. The timer callback is either running on a different CPU or
  20488. * the callback is executed in the hrtimer_interrupt context. The
  20489. - * reprogramming is handled either by the softirq, which called the
  20490. - * callback or at the end of the hrtimer_interrupt.
  20491. + * reprogramming is handled at the end of the hrtimer_interrupt.
  20492. */
  20493. if (hrtimer_callback_running(timer))
  20494. return 0;
  20495. @@ -621,6 +622,9 @@
  20496. return res;
  20497. }
  20498. +static void __run_hrtimer(struct hrtimer *timer, ktime_t *now);
  20499. +static int hrtimer_rt_defer(struct hrtimer *timer);
  20500. +
  20501. /*
  20502. * Initialize the high resolution related parts of cpu_base
  20503. */
  20504. @@ -630,6 +634,21 @@
  20505. base->hres_active = 0;
  20506. }
  20507. +static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
  20508. + struct hrtimer_clock_base *base,
  20509. + int wakeup)
  20510. +{
  20511. + if (!hrtimer_reprogram(timer, base))
  20512. + return 0;
  20513. + if (!wakeup)
  20514. + return -ETIME;
  20515. +#ifdef CONFIG_PREEMPT_RT_BASE
  20516. + if (!hrtimer_rt_defer(timer))
  20517. + return -ETIME;
  20518. +#endif
  20519. + return 1;
  20520. +}
  20521. +
  20522. static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base)
  20523. {
  20524. ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset;
  20525. @@ -695,6 +714,44 @@
  20526. static DECLARE_WORK(hrtimer_work, clock_was_set_work);
  20527. +#ifdef CONFIG_PREEMPT_RT_FULL
  20528. +/*
  20529. + * RT can not call schedule_work from real interrupt context.
  20530. + * Need to make a thread to do the real work.
  20531. + */
  20532. +static struct task_struct *clock_set_delay_thread;
  20533. +static bool do_clock_set_delay;
  20534. +
  20535. +static int run_clock_set_delay(void *ignore)
  20536. +{
  20537. + while (!kthread_should_stop()) {
  20538. + set_current_state(TASK_INTERRUPTIBLE);
  20539. + if (do_clock_set_delay) {
  20540. + do_clock_set_delay = false;
  20541. + schedule_work(&hrtimer_work);
  20542. + }
  20543. + schedule();
  20544. + }
  20545. + __set_current_state(TASK_RUNNING);
  20546. + return 0;
  20547. +}
  20548. +
  20549. +void clock_was_set_delayed(void)
  20550. +{
  20551. + do_clock_set_delay = true;
  20552. + /* Make visible before waking up process */
  20553. + smp_wmb();
  20554. + wake_up_process(clock_set_delay_thread);
  20555. +}
  20556. +
  20557. +static __init int create_clock_set_delay_thread(void)
  20558. +{
  20559. + clock_set_delay_thread = kthread_run(run_clock_set_delay, NULL, "kclksetdelayd");
  20560. + BUG_ON(!clock_set_delay_thread);
  20561. + return 0;
  20562. +}
  20563. +early_initcall(create_clock_set_delay_thread);
  20564. +#else /* PREEMPT_RT_FULL */
  20565. /*
  20566. * Called from timekeeping and resume code to reprogramm the hrtimer
  20567. * interrupt device on all cpus.
  20568. @@ -703,6 +760,7 @@
  20569. {
  20570. schedule_work(&hrtimer_work);
  20571. }
  20572. +#endif
  20573. #else
  20574. @@ -711,6 +769,13 @@
  20575. static inline int hrtimer_switch_to_hres(void) { return 0; }
  20576. static inline void
  20577. hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
  20578. +static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
  20579. + struct hrtimer_clock_base *base,
  20580. + int wakeup)
  20581. +{
  20582. + return 0;
  20583. +}
  20584. +
  20585. static inline int hrtimer_reprogram(struct hrtimer *timer,
  20586. struct hrtimer_clock_base *base)
  20587. {
  20588. @@ -718,7 +783,6 @@
  20589. }
  20590. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
  20591. static inline void retrigger_next_event(void *arg) { }
  20592. -
  20593. #endif /* CONFIG_HIGH_RES_TIMERS */
  20594. /*
  20595. @@ -836,6 +900,32 @@
  20596. }
  20597. EXPORT_SYMBOL_GPL(hrtimer_forward);
  20598. +#ifdef CONFIG_PREEMPT_RT_BASE
  20599. +# define wake_up_timer_waiters(b) wake_up(&(b)->wait)
  20600. +
  20601. +/**
  20602. + * hrtimer_wait_for_timer - Wait for a running timer
  20603. + *
  20604. + * @timer: timer to wait for
  20605. + *
  20606. + * The function waits in case the timers callback function is
  20607. + * currently executed on the waitqueue of the timer base. The
  20608. + * waitqueue is woken up after the timer callback function has
  20609. + * finished execution.
  20610. + */
  20611. +void hrtimer_wait_for_timer(const struct hrtimer *timer)
  20612. +{
  20613. + struct hrtimer_clock_base *base = timer->base;
  20614. +
  20615. + if (base && base->cpu_base && !timer->irqsafe)
  20616. + wait_event(base->cpu_base->wait,
  20617. + !(timer->state & HRTIMER_STATE_CALLBACK));
  20618. +}
  20619. +
  20620. +#else
  20621. +# define wake_up_timer_waiters(b) do { } while (0)
  20622. +#endif
  20623. +
  20624. /*
  20625. * enqueue_hrtimer - internal function to (re)start a timer
  20626. *
  20627. @@ -879,6 +969,11 @@
  20628. if (!(timer->state & HRTIMER_STATE_ENQUEUED))
  20629. goto out;
  20630. + if (unlikely(!list_empty(&timer->cb_entry))) {
  20631. + list_del_init(&timer->cb_entry);
  20632. + goto out;
  20633. + }
  20634. +
  20635. next_timer = timerqueue_getnext(&base->active);
  20636. timerqueue_del(&base->active, &timer->node);
  20637. if (&timer->node == next_timer) {
  20638. @@ -966,7 +1061,16 @@
  20639. new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
  20640. timer_stats_hrtimer_set_start_info(timer);
  20641. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  20642. + {
  20643. + ktime_t now = new_base->get_time();
  20644. + if (ktime_to_ns(tim) < ktime_to_ns(now))
  20645. + timer->praecox = now;
  20646. + else
  20647. + timer->praecox = ktime_set(0, 0);
  20648. + }
  20649. +#endif
  20650. leftmost = enqueue_hrtimer(timer, new_base);
  20651. if (!leftmost) {
  20652. @@ -980,15 +1084,26 @@
  20653. * on dynticks target.
  20654. */
  20655. wake_up_nohz_cpu(new_base->cpu_base->cpu);
  20656. - } else if (new_base->cpu_base == this_cpu_ptr(&hrtimer_bases) &&
  20657. - hrtimer_reprogram(timer, new_base)) {
  20658. + } else if (new_base->cpu_base == this_cpu_ptr(&hrtimer_bases)) {
  20659. +
  20660. + ret = hrtimer_enqueue_reprogram(timer, new_base, wakeup);
  20661. + if (ret < 0) {
  20662. + /*
  20663. + * In case we failed to reprogram the timer (mostly
  20664. + * because out current timer is already elapsed),
  20665. + * remove it again and report a failure. This avoids
  20666. + * stale base->first entries.
  20667. + */
  20668. + debug_deactivate(timer);
  20669. + __remove_hrtimer(timer, new_base,
  20670. + timer->state & HRTIMER_STATE_CALLBACK, 0);
  20671. + } else if (ret > 0) {
  20672. /*
  20673. * Only allow reprogramming if the new base is on this CPU.
  20674. * (it might still be on another CPU if the timer was pending)
  20675. *
  20676. * XXX send_remote_softirq() ?
  20677. */
  20678. - if (wakeup) {
  20679. /*
  20680. * We need to drop cpu_base->lock to avoid a
  20681. * lock ordering issue vs. rq->lock.
  20682. @@ -996,9 +1111,7 @@
  20683. raw_spin_unlock(&new_base->cpu_base->lock);
  20684. raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  20685. local_irq_restore(flags);
  20686. - return ret;
  20687. - } else {
  20688. - __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  20689. + return 0;
  20690. }
  20691. }
  20692. @@ -1089,7 +1202,7 @@
  20693. if (ret >= 0)
  20694. return ret;
  20695. - cpu_relax();
  20696. + hrtimer_wait_for_timer(timer);
  20697. }
  20698. }
  20699. EXPORT_SYMBOL_GPL(hrtimer_cancel);
  20700. @@ -1153,6 +1266,7 @@
  20701. base = hrtimer_clockid_to_base(clock_id);
  20702. timer->base = &cpu_base->clock_base[base];
  20703. + INIT_LIST_HEAD(&timer->cb_entry);
  20704. timerqueue_init(&timer->node);
  20705. #ifdef CONFIG_TIMER_STATS
  20706. @@ -1236,6 +1350,126 @@
  20707. timer->state &= ~HRTIMER_STATE_CALLBACK;
  20708. }
  20709. +static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer);
  20710. +
  20711. +#ifdef CONFIG_PREEMPT_RT_BASE
  20712. +static void hrtimer_rt_reprogram(int restart, struct hrtimer *timer,
  20713. + struct hrtimer_clock_base *base)
  20714. +{
  20715. + /*
  20716. + * Note, we clear the callback flag before we requeue the
  20717. + * timer otherwise we trigger the callback_running() check
  20718. + * in hrtimer_reprogram().
  20719. + */
  20720. + timer->state &= ~HRTIMER_STATE_CALLBACK;
  20721. +
  20722. + if (restart != HRTIMER_NORESTART) {
  20723. + BUG_ON(hrtimer_active(timer));
  20724. + /*
  20725. + * Enqueue the timer, if it's the leftmost timer then
  20726. + * we need to reprogram it.
  20727. + */
  20728. + if (!enqueue_hrtimer(timer, base))
  20729. + return;
  20730. +
  20731. +#ifndef CONFIG_HIGH_RES_TIMERS
  20732. + }
  20733. +#else
  20734. + if (base->cpu_base->hres_active &&
  20735. + hrtimer_reprogram(timer, base))
  20736. + goto requeue;
  20737. +
  20738. + } else if (hrtimer_active(timer)) {
  20739. + /*
  20740. + * If the timer was rearmed on another CPU, reprogram
  20741. + * the event device.
  20742. + */
  20743. + if (&timer->node == base->active.next &&
  20744. + base->cpu_base->hres_active &&
  20745. + hrtimer_reprogram(timer, base))
  20746. + goto requeue;
  20747. + }
  20748. + return;
  20749. +
  20750. +requeue:
  20751. + /*
  20752. + * Timer is expired. Thus move it from tree to pending list
  20753. + * again.
  20754. + */
  20755. + __remove_hrtimer(timer, base, timer->state, 0);
  20756. + list_add_tail(&timer->cb_entry, &base->expired);
  20757. +#endif
  20758. +}
  20759. +
  20760. +/*
  20761. + * The changes in mainline which removed the callback modes from
  20762. + * hrtimer are not yet working with -rt. The non wakeup_process()
  20763. + * based callbacks which involve sleeping locks need to be treated
  20764. + * seperately.
  20765. + */
  20766. +static void hrtimer_rt_run_pending(void)
  20767. +{
  20768. + enum hrtimer_restart (*fn)(struct hrtimer *);
  20769. + struct hrtimer_cpu_base *cpu_base;
  20770. + struct hrtimer_clock_base *base;
  20771. + struct hrtimer *timer;
  20772. + int index, restart;
  20773. +
  20774. + local_irq_disable();
  20775. + cpu_base = &per_cpu(hrtimer_bases, smp_processor_id());
  20776. +
  20777. + raw_spin_lock(&cpu_base->lock);
  20778. +
  20779. + for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
  20780. + base = &cpu_base->clock_base[index];
  20781. +
  20782. + while (!list_empty(&base->expired)) {
  20783. + timer = list_first_entry(&base->expired,
  20784. + struct hrtimer, cb_entry);
  20785. +
  20786. + /*
  20787. + * Same as the above __run_hrtimer function
  20788. + * just we run with interrupts enabled.
  20789. + */
  20790. + debug_hrtimer_deactivate(timer);
  20791. + __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
  20792. + timer_stats_account_hrtimer(timer);
  20793. + fn = timer->function;
  20794. +
  20795. + raw_spin_unlock_irq(&cpu_base->lock);
  20796. + restart = fn(timer);
  20797. + raw_spin_lock_irq(&cpu_base->lock);
  20798. +
  20799. + hrtimer_rt_reprogram(restart, timer, base);
  20800. + }
  20801. + }
  20802. +
  20803. + raw_spin_unlock_irq(&cpu_base->lock);
  20804. +
  20805. + wake_up_timer_waiters(cpu_base);
  20806. +}
  20807. +
  20808. +static int hrtimer_rt_defer(struct hrtimer *timer)
  20809. +{
  20810. + if (timer->irqsafe)
  20811. + return 0;
  20812. +
  20813. + __remove_hrtimer(timer, timer->base, timer->state, 0);
  20814. + list_add_tail(&timer->cb_entry, &timer->base->expired);
  20815. + return 1;
  20816. +}
  20817. +
  20818. +#else
  20819. +
  20820. +static inline void hrtimer_rt_run_pending(void)
  20821. +{
  20822. + hrtimer_peek_ahead_timers();
  20823. +}
  20824. +
  20825. +static inline int hrtimer_rt_defer(struct hrtimer *timer) { return 0; }
  20826. +
  20827. +#endif
  20828. +
  20829. #ifdef CONFIG_HIGH_RES_TIMERS
  20830. /*
  20831. @@ -1246,7 +1480,7 @@
  20832. {
  20833. struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
  20834. ktime_t expires_next, now, entry_time, delta;
  20835. - int i, retries = 0;
  20836. + int i, retries = 0, raise = 0;
  20837. BUG_ON(!cpu_base->hres_active);
  20838. cpu_base->nr_events++;
  20839. @@ -1281,6 +1515,15 @@
  20840. timer = container_of(node, struct hrtimer, node);
  20841. + trace_hrtimer_interrupt(raw_smp_processor_id(),
  20842. + ktime_to_ns(ktime_sub(ktime_to_ns(timer->praecox) ?
  20843. + timer->praecox : hrtimer_get_expires(timer),
  20844. + basenow)),
  20845. + current,
  20846. + timer->function == hrtimer_wakeup ?
  20847. + container_of(timer, struct hrtimer_sleeper,
  20848. + timer)->task : NULL);
  20849. +
  20850. /*
  20851. * The immediate goal for using the softexpires is
  20852. * minimizing wakeups, not running timers at the
  20853. @@ -1296,7 +1539,10 @@
  20854. if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer))
  20855. break;
  20856. - __run_hrtimer(timer, &basenow);
  20857. + if (!hrtimer_rt_defer(timer))
  20858. + __run_hrtimer(timer, &basenow);
  20859. + else
  20860. + raise = 1;
  20861. }
  20862. }
  20863. /* Reevaluate the clock bases for the next expiry */
  20864. @@ -1313,7 +1559,7 @@
  20865. if (expires_next.tv64 == KTIME_MAX ||
  20866. !tick_program_event(expires_next, 0)) {
  20867. cpu_base->hang_detected = 0;
  20868. - return;
  20869. + goto out;
  20870. }
  20871. /*
  20872. @@ -1357,6 +1603,9 @@
  20873. tick_program_event(expires_next, 1);
  20874. printk_once(KERN_WARNING "hrtimer: interrupt took %llu ns\n",
  20875. ktime_to_ns(delta));
  20876. +out:
  20877. + if (raise)
  20878. + raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  20879. }
  20880. /*
  20881. @@ -1392,18 +1641,18 @@
  20882. __hrtimer_peek_ahead_timers();
  20883. local_irq_restore(flags);
  20884. }
  20885. -
  20886. -static void run_hrtimer_softirq(struct softirq_action *h)
  20887. -{
  20888. - hrtimer_peek_ahead_timers();
  20889. -}
  20890. -
  20891. #else /* CONFIG_HIGH_RES_TIMERS */
  20892. static inline void __hrtimer_peek_ahead_timers(void) { }
  20893. #endif /* !CONFIG_HIGH_RES_TIMERS */
  20894. +
  20895. +static void run_hrtimer_softirq(struct softirq_action *h)
  20896. +{
  20897. + hrtimer_rt_run_pending();
  20898. +}
  20899. +
  20900. /*
  20901. * Called from timer softirq every jiffy, expire hrtimers:
  20902. *
  20903. @@ -1436,7 +1685,7 @@
  20904. struct timerqueue_node *node;
  20905. struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
  20906. struct hrtimer_clock_base *base;
  20907. - int index, gettime = 1;
  20908. + int index, gettime = 1, raise = 0;
  20909. if (hrtimer_hres_active())
  20910. return;
  20911. @@ -1461,10 +1710,16 @@
  20912. hrtimer_get_expires_tv64(timer))
  20913. break;
  20914. - __run_hrtimer(timer, &base->softirq_time);
  20915. + if (!hrtimer_rt_defer(timer))
  20916. + __run_hrtimer(timer, &base->softirq_time);
  20917. + else
  20918. + raise = 1;
  20919. }
  20920. raw_spin_unlock(&cpu_base->lock);
  20921. }
  20922. +
  20923. + if (raise)
  20924. + raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  20925. }
  20926. /*
  20927. @@ -1486,16 +1741,18 @@
  20928. void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
  20929. {
  20930. sl->timer.function = hrtimer_wakeup;
  20931. + sl->timer.irqsafe = 1;
  20932. sl->task = task;
  20933. }
  20934. EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
  20935. -static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
  20936. +static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode,
  20937. + unsigned long state)
  20938. {
  20939. hrtimer_init_sleeper(t, current);
  20940. do {
  20941. - set_current_state(TASK_INTERRUPTIBLE);
  20942. + set_current_state(state);
  20943. hrtimer_start_expires(&t->timer, mode);
  20944. if (!hrtimer_active(&t->timer))
  20945. t->task = NULL;
  20946. @@ -1539,7 +1796,8 @@
  20947. HRTIMER_MODE_ABS);
  20948. hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
  20949. - if (do_nanosleep(&t, HRTIMER_MODE_ABS))
  20950. + /* cpu_chill() does not care about restart state. */
  20951. + if (do_nanosleep(&t, HRTIMER_MODE_ABS, TASK_INTERRUPTIBLE))
  20952. goto out;
  20953. rmtp = restart->nanosleep.rmtp;
  20954. @@ -1556,8 +1814,10 @@
  20955. return ret;
  20956. }
  20957. -long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  20958. - const enum hrtimer_mode mode, const clockid_t clockid)
  20959. +static long
  20960. +__hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  20961. + const enum hrtimer_mode mode, const clockid_t clockid,
  20962. + unsigned long state)
  20963. {
  20964. struct restart_block *restart;
  20965. struct hrtimer_sleeper t;
  20966. @@ -1570,7 +1830,7 @@
  20967. hrtimer_init_on_stack(&t.timer, clockid, mode);
  20968. hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
  20969. - if (do_nanosleep(&t, mode))
  20970. + if (do_nanosleep(&t, mode, state))
  20971. goto out;
  20972. /* Absolute timers do not update the rmtp value and restart: */
  20973. @@ -1597,6 +1857,12 @@
  20974. return ret;
  20975. }
  20976. +long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  20977. + const enum hrtimer_mode mode, const clockid_t clockid)
  20978. +{
  20979. + return __hrtimer_nanosleep(rqtp, rmtp, mode, clockid, TASK_INTERRUPTIBLE);
  20980. +}
  20981. +
  20982. SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
  20983. struct timespec __user *, rmtp)
  20984. {
  20985. @@ -1611,6 +1877,26 @@
  20986. return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
  20987. }
  20988. +#ifdef CONFIG_PREEMPT_RT_FULL
  20989. +/*
  20990. + * Sleep for 1 ms in hope whoever holds what we want will let it go.
  20991. + */
  20992. +void cpu_chill(void)
  20993. +{
  20994. + struct timespec tu = {
  20995. + .tv_nsec = NSEC_PER_MSEC,
  20996. + };
  20997. + unsigned int freeze_flag = current->flags & PF_NOFREEZE;
  20998. +
  20999. + current->flags |= PF_NOFREEZE;
  21000. + __hrtimer_nanosleep(&tu, NULL, HRTIMER_MODE_REL, CLOCK_MONOTONIC,
  21001. + TASK_UNINTERRUPTIBLE);
  21002. + if (!freeze_flag)
  21003. + current->flags &= ~PF_NOFREEZE;
  21004. +}
  21005. +EXPORT_SYMBOL(cpu_chill);
  21006. +#endif
  21007. +
  21008. /*
  21009. * Functions related to boot-time initialization:
  21010. */
  21011. @@ -1622,10 +1908,14 @@
  21012. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  21013. cpu_base->clock_base[i].cpu_base = cpu_base;
  21014. timerqueue_init_head(&cpu_base->clock_base[i].active);
  21015. + INIT_LIST_HEAD(&cpu_base->clock_base[i].expired);
  21016. }
  21017. cpu_base->cpu = cpu;
  21018. hrtimer_init_hres(cpu_base);
  21019. +#ifdef CONFIG_PREEMPT_RT_BASE
  21020. + init_waitqueue_head(&cpu_base->wait);
  21021. +#endif
  21022. }
  21023. #ifdef CONFIG_HOTPLUG_CPU
  21024. @@ -1731,9 +2021,7 @@
  21025. hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
  21026. (void *)(long)smp_processor_id());
  21027. register_cpu_notifier(&hrtimers_nb);
  21028. -#ifdef CONFIG_HIGH_RES_TIMERS
  21029. open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
  21030. -#endif
  21031. }
  21032. /**
  21033. diff -Nur linux-4.1.20.orig/kernel/time/itimer.c linux-4.1.20/kernel/time/itimer.c
  21034. --- linux-4.1.20.orig/kernel/time/itimer.c 2016-03-17 19:11:03.000000000 +0100
  21035. +++ linux-4.1.20/kernel/time/itimer.c 2016-03-21 20:18:33.000000000 +0100
  21036. @@ -213,6 +213,7 @@
  21037. /* We are sharing ->siglock with it_real_fn() */
  21038. if (hrtimer_try_to_cancel(timer) < 0) {
  21039. spin_unlock_irq(&tsk->sighand->siglock);
  21040. + hrtimer_wait_for_timer(&tsk->signal->real_timer);
  21041. goto again;
  21042. }
  21043. expires = timeval_to_ktime(value->it_value);
  21044. diff -Nur linux-4.1.20.orig/kernel/time/jiffies.c linux-4.1.20/kernel/time/jiffies.c
  21045. --- linux-4.1.20.orig/kernel/time/jiffies.c 2016-03-17 19:11:03.000000000 +0100
  21046. +++ linux-4.1.20/kernel/time/jiffies.c 2016-03-21 20:18:33.000000000 +0100
  21047. @@ -74,7 +74,8 @@
  21048. .max_cycles = 10,
  21049. };
  21050. -__cacheline_aligned_in_smp DEFINE_SEQLOCK(jiffies_lock);
  21051. +__cacheline_aligned_in_smp DEFINE_RAW_SPINLOCK(jiffies_lock);
  21052. +__cacheline_aligned_in_smp seqcount_t jiffies_seq;
  21053. #if (BITS_PER_LONG < 64)
  21054. u64 get_jiffies_64(void)
  21055. @@ -83,9 +84,9 @@
  21056. u64 ret;
  21057. do {
  21058. - seq = read_seqbegin(&jiffies_lock);
  21059. + seq = read_seqcount_begin(&jiffies_seq);
  21060. ret = jiffies_64;
  21061. - } while (read_seqretry(&jiffies_lock, seq));
  21062. + } while (read_seqcount_retry(&jiffies_seq, seq));
  21063. return ret;
  21064. }
  21065. EXPORT_SYMBOL(get_jiffies_64);
  21066. diff -Nur linux-4.1.20.orig/kernel/time/ntp.c linux-4.1.20/kernel/time/ntp.c
  21067. --- linux-4.1.20.orig/kernel/time/ntp.c 2016-03-17 19:11:03.000000000 +0100
  21068. +++ linux-4.1.20/kernel/time/ntp.c 2016-03-21 20:18:33.000000000 +0100
  21069. @@ -10,6 +10,7 @@
  21070. #include <linux/workqueue.h>
  21071. #include <linux/hrtimer.h>
  21072. #include <linux/jiffies.h>
  21073. +#include <linux/kthread.h>
  21074. #include <linux/math64.h>
  21075. #include <linux/timex.h>
  21076. #include <linux/time.h>
  21077. @@ -529,10 +530,52 @@
  21078. &sync_cmos_work, timespec_to_jiffies(&next));
  21079. }
  21080. +#ifdef CONFIG_PREEMPT_RT_FULL
  21081. +/*
  21082. + * RT can not call schedule_delayed_work from real interrupt context.
  21083. + * Need to make a thread to do the real work.
  21084. + */
  21085. +static struct task_struct *cmos_delay_thread;
  21086. +static bool do_cmos_delay;
  21087. +
  21088. +static int run_cmos_delay(void *ignore)
  21089. +{
  21090. + while (!kthread_should_stop()) {
  21091. + set_current_state(TASK_INTERRUPTIBLE);
  21092. + if (do_cmos_delay) {
  21093. + do_cmos_delay = false;
  21094. + queue_delayed_work(system_power_efficient_wq,
  21095. + &sync_cmos_work, 0);
  21096. + }
  21097. + schedule();
  21098. + }
  21099. + __set_current_state(TASK_RUNNING);
  21100. + return 0;
  21101. +}
  21102. +
  21103. +void ntp_notify_cmos_timer(void)
  21104. +{
  21105. + do_cmos_delay = true;
  21106. + /* Make visible before waking up process */
  21107. + smp_wmb();
  21108. + wake_up_process(cmos_delay_thread);
  21109. +}
  21110. +
  21111. +static __init int create_cmos_delay_thread(void)
  21112. +{
  21113. + cmos_delay_thread = kthread_run(run_cmos_delay, NULL, "kcmosdelayd");
  21114. + BUG_ON(!cmos_delay_thread);
  21115. + return 0;
  21116. +}
  21117. +early_initcall(create_cmos_delay_thread);
  21118. +
  21119. +#else
  21120. +
  21121. void ntp_notify_cmos_timer(void)
  21122. {
  21123. queue_delayed_work(system_power_efficient_wq, &sync_cmos_work, 0);
  21124. }
  21125. +#endif /* CONFIG_PREEMPT_RT_FULL */
  21126. #else
  21127. void ntp_notify_cmos_timer(void) { }
  21128. diff -Nur linux-4.1.20.orig/kernel/time/posix-cpu-timers.c linux-4.1.20/kernel/time/posix-cpu-timers.c
  21129. --- linux-4.1.20.orig/kernel/time/posix-cpu-timers.c 2016-03-17 19:11:03.000000000 +0100
  21130. +++ linux-4.1.20/kernel/time/posix-cpu-timers.c 2016-03-21 20:18:33.000000000 +0100
  21131. @@ -3,6 +3,7 @@
  21132. */
  21133. #include <linux/sched.h>
  21134. +#include <linux/sched/rt.h>
  21135. #include <linux/posix-timers.h>
  21136. #include <linux/errno.h>
  21137. #include <linux/math64.h>
  21138. @@ -626,7 +627,7 @@
  21139. /*
  21140. * Disarm any old timer after extracting its expiry time.
  21141. */
  21142. - WARN_ON_ONCE(!irqs_disabled());
  21143. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  21144. ret = 0;
  21145. old_incr = timer->it.cpu.incr;
  21146. @@ -1047,7 +1048,7 @@
  21147. /*
  21148. * Now re-arm for the new expiry time.
  21149. */
  21150. - WARN_ON_ONCE(!irqs_disabled());
  21151. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  21152. arm_timer(timer);
  21153. unlock_task_sighand(p, &flags);
  21154. @@ -1113,10 +1114,11 @@
  21155. sig = tsk->signal;
  21156. if (sig->cputimer.running) {
  21157. struct task_cputime group_sample;
  21158. + unsigned long flags;
  21159. - raw_spin_lock(&sig->cputimer.lock);
  21160. + raw_spin_lock_irqsave(&sig->cputimer.lock, flags);
  21161. group_sample = sig->cputimer.cputime;
  21162. - raw_spin_unlock(&sig->cputimer.lock);
  21163. + raw_spin_unlock_irqrestore(&sig->cputimer.lock, flags);
  21164. if (task_cputime_expired(&group_sample, &sig->cputime_expires))
  21165. return 1;
  21166. @@ -1130,13 +1132,13 @@
  21167. * already updated our counts. We need to check if any timers fire now.
  21168. * Interrupts are disabled.
  21169. */
  21170. -void run_posix_cpu_timers(struct task_struct *tsk)
  21171. +static void __run_posix_cpu_timers(struct task_struct *tsk)
  21172. {
  21173. LIST_HEAD(firing);
  21174. struct k_itimer *timer, *next;
  21175. unsigned long flags;
  21176. - WARN_ON_ONCE(!irqs_disabled());
  21177. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  21178. /*
  21179. * The fast path checks that there are no expired thread or thread
  21180. @@ -1194,6 +1196,190 @@
  21181. }
  21182. }
  21183. +#ifdef CONFIG_PREEMPT_RT_BASE
  21184. +#include <linux/kthread.h>
  21185. +#include <linux/cpu.h>
  21186. +DEFINE_PER_CPU(struct task_struct *, posix_timer_task);
  21187. +DEFINE_PER_CPU(struct task_struct *, posix_timer_tasklist);
  21188. +
  21189. +static int posix_cpu_timers_thread(void *data)
  21190. +{
  21191. + int cpu = (long)data;
  21192. +
  21193. + BUG_ON(per_cpu(posix_timer_task,cpu) != current);
  21194. +
  21195. + while (!kthread_should_stop()) {
  21196. + struct task_struct *tsk = NULL;
  21197. + struct task_struct *next = NULL;
  21198. +
  21199. + if (cpu_is_offline(cpu))
  21200. + goto wait_to_die;
  21201. +
  21202. + /* grab task list */
  21203. + raw_local_irq_disable();
  21204. + tsk = per_cpu(posix_timer_tasklist, cpu);
  21205. + per_cpu(posix_timer_tasklist, cpu) = NULL;
  21206. + raw_local_irq_enable();
  21207. +
  21208. + /* its possible the list is empty, just return */
  21209. + if (!tsk) {
  21210. + set_current_state(TASK_INTERRUPTIBLE);
  21211. + schedule();
  21212. + __set_current_state(TASK_RUNNING);
  21213. + continue;
  21214. + }
  21215. +
  21216. + /* Process task list */
  21217. + while (1) {
  21218. + /* save next */
  21219. + next = tsk->posix_timer_list;
  21220. +
  21221. + /* run the task timers, clear its ptr and
  21222. + * unreference it
  21223. + */
  21224. + __run_posix_cpu_timers(tsk);
  21225. + tsk->posix_timer_list = NULL;
  21226. + put_task_struct(tsk);
  21227. +
  21228. + /* check if this is the last on the list */
  21229. + if (next == tsk)
  21230. + break;
  21231. + tsk = next;
  21232. + }
  21233. + }
  21234. + return 0;
  21235. +
  21236. +wait_to_die:
  21237. + /* Wait for kthread_stop */
  21238. + set_current_state(TASK_INTERRUPTIBLE);
  21239. + while (!kthread_should_stop()) {
  21240. + schedule();
  21241. + set_current_state(TASK_INTERRUPTIBLE);
  21242. + }
  21243. + __set_current_state(TASK_RUNNING);
  21244. + return 0;
  21245. +}
  21246. +
  21247. +static inline int __fastpath_timer_check(struct task_struct *tsk)
  21248. +{
  21249. + /* tsk == current, ensure it is safe to use ->signal/sighand */
  21250. + if (unlikely(tsk->exit_state))
  21251. + return 0;
  21252. +
  21253. + if (!task_cputime_zero(&tsk->cputime_expires))
  21254. + return 1;
  21255. +
  21256. + if (!task_cputime_zero(&tsk->signal->cputime_expires))
  21257. + return 1;
  21258. +
  21259. + return 0;
  21260. +}
  21261. +
  21262. +void run_posix_cpu_timers(struct task_struct *tsk)
  21263. +{
  21264. + unsigned long cpu = smp_processor_id();
  21265. + struct task_struct *tasklist;
  21266. +
  21267. + BUG_ON(!irqs_disabled());
  21268. + if(!per_cpu(posix_timer_task, cpu))
  21269. + return;
  21270. + /* get per-cpu references */
  21271. + tasklist = per_cpu(posix_timer_tasklist, cpu);
  21272. +
  21273. + /* check to see if we're already queued */
  21274. + if (!tsk->posix_timer_list && __fastpath_timer_check(tsk)) {
  21275. + get_task_struct(tsk);
  21276. + if (tasklist) {
  21277. + tsk->posix_timer_list = tasklist;
  21278. + } else {
  21279. + /*
  21280. + * The list is terminated by a self-pointing
  21281. + * task_struct
  21282. + */
  21283. + tsk->posix_timer_list = tsk;
  21284. + }
  21285. + per_cpu(posix_timer_tasklist, cpu) = tsk;
  21286. +
  21287. + wake_up_process(per_cpu(posix_timer_task, cpu));
  21288. + }
  21289. +}
  21290. +
  21291. +/*
  21292. + * posix_cpu_thread_call - callback that gets triggered when a CPU is added.
  21293. + * Here we can start up the necessary migration thread for the new CPU.
  21294. + */
  21295. +static int posix_cpu_thread_call(struct notifier_block *nfb,
  21296. + unsigned long action, void *hcpu)
  21297. +{
  21298. + int cpu = (long)hcpu;
  21299. + struct task_struct *p;
  21300. + struct sched_param param;
  21301. +
  21302. + switch (action) {
  21303. + case CPU_UP_PREPARE:
  21304. + p = kthread_create(posix_cpu_timers_thread, hcpu,
  21305. + "posixcputmr/%d",cpu);
  21306. + if (IS_ERR(p))
  21307. + return NOTIFY_BAD;
  21308. + p->flags |= PF_NOFREEZE;
  21309. + kthread_bind(p, cpu);
  21310. + /* Must be high prio to avoid getting starved */
  21311. + param.sched_priority = MAX_RT_PRIO-1;
  21312. + sched_setscheduler(p, SCHED_FIFO, &param);
  21313. + per_cpu(posix_timer_task,cpu) = p;
  21314. + break;
  21315. + case CPU_ONLINE:
  21316. + /* Strictly unneccessary, as first user will wake it. */
  21317. + wake_up_process(per_cpu(posix_timer_task,cpu));
  21318. + break;
  21319. +#ifdef CONFIG_HOTPLUG_CPU
  21320. + case CPU_UP_CANCELED:
  21321. + /* Unbind it from offline cpu so it can run. Fall thru. */
  21322. + kthread_bind(per_cpu(posix_timer_task, cpu),
  21323. + cpumask_any(cpu_online_mask));
  21324. + kthread_stop(per_cpu(posix_timer_task,cpu));
  21325. + per_cpu(posix_timer_task,cpu) = NULL;
  21326. + break;
  21327. + case CPU_DEAD:
  21328. + kthread_stop(per_cpu(posix_timer_task,cpu));
  21329. + per_cpu(posix_timer_task,cpu) = NULL;
  21330. + break;
  21331. +#endif
  21332. + }
  21333. + return NOTIFY_OK;
  21334. +}
  21335. +
  21336. +/* Register at highest priority so that task migration (migrate_all_tasks)
  21337. + * happens before everything else.
  21338. + */
  21339. +static struct notifier_block posix_cpu_thread_notifier = {
  21340. + .notifier_call = posix_cpu_thread_call,
  21341. + .priority = 10
  21342. +};
  21343. +
  21344. +static int __init posix_cpu_thread_init(void)
  21345. +{
  21346. + void *hcpu = (void *)(long)smp_processor_id();
  21347. + /* Start one for boot CPU. */
  21348. + unsigned long cpu;
  21349. +
  21350. + /* init the per-cpu posix_timer_tasklets */
  21351. + for_each_possible_cpu(cpu)
  21352. + per_cpu(posix_timer_tasklist, cpu) = NULL;
  21353. +
  21354. + posix_cpu_thread_call(&posix_cpu_thread_notifier, CPU_UP_PREPARE, hcpu);
  21355. + posix_cpu_thread_call(&posix_cpu_thread_notifier, CPU_ONLINE, hcpu);
  21356. + register_cpu_notifier(&posix_cpu_thread_notifier);
  21357. + return 0;
  21358. +}
  21359. +early_initcall(posix_cpu_thread_init);
  21360. +#else /* CONFIG_PREEMPT_RT_BASE */
  21361. +void run_posix_cpu_timers(struct task_struct *tsk)
  21362. +{
  21363. + __run_posix_cpu_timers(tsk);
  21364. +}
  21365. +#endif /* CONFIG_PREEMPT_RT_BASE */
  21366. +
  21367. /*
  21368. * Set one of the process-wide special case CPU timers or RLIMIT_CPU.
  21369. * The tsk->sighand->siglock must be held by the caller.
  21370. diff -Nur linux-4.1.20.orig/kernel/time/posix-timers.c linux-4.1.20/kernel/time/posix-timers.c
  21371. --- linux-4.1.20.orig/kernel/time/posix-timers.c 2016-03-17 19:11:03.000000000 +0100
  21372. +++ linux-4.1.20/kernel/time/posix-timers.c 2016-03-21 20:18:34.000000000 +0100
  21373. @@ -499,6 +499,7 @@
  21374. static struct pid *good_sigevent(sigevent_t * event)
  21375. {
  21376. struct task_struct *rtn = current->group_leader;
  21377. + int sig = event->sigev_signo;
  21378. if ((event->sigev_notify & SIGEV_THREAD_ID ) &&
  21379. (!(rtn = find_task_by_vpid(event->sigev_notify_thread_id)) ||
  21380. @@ -507,7 +508,8 @@
  21381. return NULL;
  21382. if (((event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) &&
  21383. - ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX)))
  21384. + (sig <= 0 || sig > SIGRTMAX || sig_kernel_only(sig) ||
  21385. + sig_kernel_coredump(sig)))
  21386. return NULL;
  21387. return task_pid(rtn);
  21388. @@ -819,6 +821,20 @@
  21389. return overrun;
  21390. }
  21391. +/*
  21392. + * Protected by RCU!
  21393. + */
  21394. +static void timer_wait_for_callback(struct k_clock *kc, struct k_itimer *timr)
  21395. +{
  21396. +#ifdef CONFIG_PREEMPT_RT_FULL
  21397. + if (kc->timer_set == common_timer_set)
  21398. + hrtimer_wait_for_timer(&timr->it.real.timer);
  21399. + else
  21400. + /* FIXME: Whacky hack for posix-cpu-timers */
  21401. + schedule_timeout(1);
  21402. +#endif
  21403. +}
  21404. +
  21405. /* Set a POSIX.1b interval timer. */
  21406. /* timr->it_lock is taken. */
  21407. static int
  21408. @@ -896,6 +912,7 @@
  21409. if (!timr)
  21410. return -EINVAL;
  21411. + rcu_read_lock();
  21412. kc = clockid_to_kclock(timr->it_clock);
  21413. if (WARN_ON_ONCE(!kc || !kc->timer_set))
  21414. error = -EINVAL;
  21415. @@ -904,9 +921,12 @@
  21416. unlock_timer(timr, flag);
  21417. if (error == TIMER_RETRY) {
  21418. + timer_wait_for_callback(kc, timr);
  21419. rtn = NULL; // We already got the old time...
  21420. + rcu_read_unlock();
  21421. goto retry;
  21422. }
  21423. + rcu_read_unlock();
  21424. if (old_setting && !error &&
  21425. copy_to_user(old_setting, &old_spec, sizeof (old_spec)))
  21426. @@ -944,10 +964,15 @@
  21427. if (!timer)
  21428. return -EINVAL;
  21429. + rcu_read_lock();
  21430. if (timer_delete_hook(timer) == TIMER_RETRY) {
  21431. unlock_timer(timer, flags);
  21432. + timer_wait_for_callback(clockid_to_kclock(timer->it_clock),
  21433. + timer);
  21434. + rcu_read_unlock();
  21435. goto retry_delete;
  21436. }
  21437. + rcu_read_unlock();
  21438. spin_lock(&current->sighand->siglock);
  21439. list_del(&timer->list);
  21440. @@ -973,8 +998,18 @@
  21441. retry_delete:
  21442. spin_lock_irqsave(&timer->it_lock, flags);
  21443. + /* On RT we can race with a deletion */
  21444. + if (!timer->it_signal) {
  21445. + unlock_timer(timer, flags);
  21446. + return;
  21447. + }
  21448. +
  21449. if (timer_delete_hook(timer) == TIMER_RETRY) {
  21450. + rcu_read_lock();
  21451. unlock_timer(timer, flags);
  21452. + timer_wait_for_callback(clockid_to_kclock(timer->it_clock),
  21453. + timer);
  21454. + rcu_read_unlock();
  21455. goto retry_delete;
  21456. }
  21457. list_del(&timer->list);
  21458. diff -Nur linux-4.1.20.orig/kernel/time/tick-broadcast-hrtimer.c linux-4.1.20/kernel/time/tick-broadcast-hrtimer.c
  21459. --- linux-4.1.20.orig/kernel/time/tick-broadcast-hrtimer.c 2016-03-17 19:11:03.000000000 +0100
  21460. +++ linux-4.1.20/kernel/time/tick-broadcast-hrtimer.c 2016-03-21 20:18:34.000000000 +0100
  21461. @@ -109,5 +109,6 @@
  21462. {
  21463. hrtimer_init(&bctimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  21464. bctimer.function = bc_handler;
  21465. + bctimer.irqsafe = true;
  21466. clockevents_register_device(&ce_broadcast_hrtimer);
  21467. }
  21468. diff -Nur linux-4.1.20.orig/kernel/time/tick-common.c linux-4.1.20/kernel/time/tick-common.c
  21469. --- linux-4.1.20.orig/kernel/time/tick-common.c 2016-03-17 19:11:03.000000000 +0100
  21470. +++ linux-4.1.20/kernel/time/tick-common.c 2016-03-21 20:18:34.000000000 +0100
  21471. @@ -78,13 +78,15 @@
  21472. static void tick_periodic(int cpu)
  21473. {
  21474. if (tick_do_timer_cpu == cpu) {
  21475. - write_seqlock(&jiffies_lock);
  21476. + raw_spin_lock(&jiffies_lock);
  21477. + write_seqcount_begin(&jiffies_seq);
  21478. /* Keep track of the next tick event */
  21479. tick_next_period = ktime_add(tick_next_period, tick_period);
  21480. do_timer(1);
  21481. - write_sequnlock(&jiffies_lock);
  21482. + write_seqcount_end(&jiffies_seq);
  21483. + raw_spin_unlock(&jiffies_lock);
  21484. update_wall_time();
  21485. }
  21486. @@ -146,9 +148,9 @@
  21487. ktime_t next;
  21488. do {
  21489. - seq = read_seqbegin(&jiffies_lock);
  21490. + seq = read_seqcount_begin(&jiffies_seq);
  21491. next = tick_next_period;
  21492. - } while (read_seqretry(&jiffies_lock, seq));
  21493. + } while (read_seqcount_retry(&jiffies_seq, seq));
  21494. clockevents_set_state(dev, CLOCK_EVT_STATE_ONESHOT);
  21495. diff -Nur linux-4.1.20.orig/kernel/time/tick-sched.c linux-4.1.20/kernel/time/tick-sched.c
  21496. --- linux-4.1.20.orig/kernel/time/tick-sched.c 2016-03-17 19:11:03.000000000 +0100
  21497. +++ linux-4.1.20/kernel/time/tick-sched.c 2016-03-21 20:18:34.000000000 +0100
  21498. @@ -62,7 +62,8 @@
  21499. return;
  21500. /* Reevalute with jiffies_lock held */
  21501. - write_seqlock(&jiffies_lock);
  21502. + raw_spin_lock(&jiffies_lock);
  21503. + write_seqcount_begin(&jiffies_seq);
  21504. delta = ktime_sub(now, last_jiffies_update);
  21505. if (delta.tv64 >= tick_period.tv64) {
  21506. @@ -85,10 +86,12 @@
  21507. /* Keep the tick_next_period variable up to date */
  21508. tick_next_period = ktime_add(last_jiffies_update, tick_period);
  21509. } else {
  21510. - write_sequnlock(&jiffies_lock);
  21511. + write_seqcount_end(&jiffies_seq);
  21512. + raw_spin_unlock(&jiffies_lock);
  21513. return;
  21514. }
  21515. - write_sequnlock(&jiffies_lock);
  21516. + write_seqcount_end(&jiffies_seq);
  21517. + raw_spin_unlock(&jiffies_lock);
  21518. update_wall_time();
  21519. }
  21520. @@ -99,12 +102,14 @@
  21521. {
  21522. ktime_t period;
  21523. - write_seqlock(&jiffies_lock);
  21524. + raw_spin_lock(&jiffies_lock);
  21525. + write_seqcount_begin(&jiffies_seq);
  21526. /* Did we start the jiffies update yet ? */
  21527. if (last_jiffies_update.tv64 == 0)
  21528. last_jiffies_update = tick_next_period;
  21529. period = last_jiffies_update;
  21530. - write_sequnlock(&jiffies_lock);
  21531. + write_seqcount_end(&jiffies_seq);
  21532. + raw_spin_unlock(&jiffies_lock);
  21533. return period;
  21534. }
  21535. @@ -176,6 +181,11 @@
  21536. return false;
  21537. }
  21538. + if (!arch_irq_work_has_interrupt()) {
  21539. + trace_tick_stop(0, "missing irq work interrupt\n");
  21540. + return false;
  21541. + }
  21542. +
  21543. /* sched_clock_tick() needs us? */
  21544. #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
  21545. /*
  21546. @@ -222,6 +232,7 @@
  21547. static DEFINE_PER_CPU(struct irq_work, nohz_full_kick_work) = {
  21548. .func = nohz_full_kick_work_func,
  21549. + .flags = IRQ_WORK_HARD_IRQ,
  21550. };
  21551. /*
  21552. @@ -578,10 +589,10 @@
  21553. /* Read jiffies and the time when jiffies were updated last */
  21554. do {
  21555. - seq = read_seqbegin(&jiffies_lock);
  21556. + seq = read_seqcount_begin(&jiffies_seq);
  21557. last_update = last_jiffies_update;
  21558. last_jiffies = jiffies;
  21559. - } while (read_seqretry(&jiffies_lock, seq));
  21560. + } while (read_seqcount_retry(&jiffies_seq, seq));
  21561. if (rcu_needs_cpu(&rcu_delta_jiffies) ||
  21562. arch_needs_cpu() || irq_work_needs_cpu()) {
  21563. @@ -759,14 +770,7 @@
  21564. return false;
  21565. if (unlikely(local_softirq_pending() && cpu_online(cpu))) {
  21566. - static int ratelimit;
  21567. -
  21568. - if (ratelimit < 10 &&
  21569. - (local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK)) {
  21570. - pr_warn("NOHZ: local_softirq_pending %02x\n",
  21571. - (unsigned int) local_softirq_pending());
  21572. - ratelimit++;
  21573. - }
  21574. + softirq_check_pending_idle();
  21575. return false;
  21576. }
  21577. @@ -1154,6 +1158,7 @@
  21578. * Emulate tick processing via per-CPU hrtimers:
  21579. */
  21580. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  21581. + ts->sched_timer.irqsafe = 1;
  21582. ts->sched_timer.function = tick_sched_timer;
  21583. /* Get the next period (per cpu) */
  21584. diff -Nur linux-4.1.20.orig/kernel/time/timekeeping.c linux-4.1.20/kernel/time/timekeeping.c
  21585. --- linux-4.1.20.orig/kernel/time/timekeeping.c 2016-03-17 19:11:03.000000000 +0100
  21586. +++ linux-4.1.20/kernel/time/timekeeping.c 2016-03-21 20:18:34.000000000 +0100
  21587. @@ -2064,8 +2064,10 @@
  21588. */
  21589. void xtime_update(unsigned long ticks)
  21590. {
  21591. - write_seqlock(&jiffies_lock);
  21592. + raw_spin_lock(&jiffies_lock);
  21593. + write_seqcount_begin(&jiffies_seq);
  21594. do_timer(ticks);
  21595. - write_sequnlock(&jiffies_lock);
  21596. + write_seqcount_end(&jiffies_seq);
  21597. + raw_spin_unlock(&jiffies_lock);
  21598. update_wall_time();
  21599. }
  21600. diff -Nur linux-4.1.20.orig/kernel/time/timekeeping.h linux-4.1.20/kernel/time/timekeeping.h
  21601. --- linux-4.1.20.orig/kernel/time/timekeeping.h 2016-03-17 19:11:03.000000000 +0100
  21602. +++ linux-4.1.20/kernel/time/timekeeping.h 2016-03-21 20:18:34.000000000 +0100
  21603. @@ -22,7 +22,8 @@
  21604. extern void do_timer(unsigned long ticks);
  21605. extern void update_wall_time(void);
  21606. -extern seqlock_t jiffies_lock;
  21607. +extern raw_spinlock_t jiffies_lock;
  21608. +extern seqcount_t jiffies_seq;
  21609. #define CS_NAME_LEN 32
  21610. diff -Nur linux-4.1.20.orig/kernel/time/timer.c linux-4.1.20/kernel/time/timer.c
  21611. --- linux-4.1.20.orig/kernel/time/timer.c 2016-03-17 19:11:03.000000000 +0100
  21612. +++ linux-4.1.20/kernel/time/timer.c 2016-03-21 20:18:34.000000000 +0100
  21613. @@ -78,6 +78,9 @@
  21614. struct tvec_base {
  21615. spinlock_t lock;
  21616. struct timer_list *running_timer;
  21617. +#ifdef CONFIG_PREEMPT_RT_FULL
  21618. + wait_queue_head_t wait_for_running_timer;
  21619. +#endif
  21620. unsigned long timer_jiffies;
  21621. unsigned long next_timer;
  21622. unsigned long active_timers;
  21623. @@ -768,6 +771,36 @@
  21624. }
  21625. }
  21626. +#ifndef CONFIG_PREEMPT_RT_FULL
  21627. +static inline struct tvec_base *switch_timer_base(struct timer_list *timer,
  21628. + struct tvec_base *old,
  21629. + struct tvec_base *new)
  21630. +{
  21631. + /* See the comment in lock_timer_base() */
  21632. + timer_set_base(timer, NULL);
  21633. + spin_unlock(&old->lock);
  21634. + spin_lock(&new->lock);
  21635. + timer_set_base(timer, new);
  21636. + return new;
  21637. +}
  21638. +#else
  21639. +static inline struct tvec_base *switch_timer_base(struct timer_list *timer,
  21640. + struct tvec_base *old,
  21641. + struct tvec_base *new)
  21642. +{
  21643. + /*
  21644. + * We cannot do the above because we might be preempted and
  21645. + * then the preempter would see NULL and loop forever.
  21646. + */
  21647. + if (spin_trylock(&new->lock)) {
  21648. + timer_set_base(timer, new);
  21649. + spin_unlock(&old->lock);
  21650. + return new;
  21651. + }
  21652. + return old;
  21653. +}
  21654. +#endif
  21655. +
  21656. static inline int
  21657. __mod_timer(struct timer_list *timer, unsigned long expires,
  21658. bool pending_only, int pinned)
  21659. @@ -798,14 +831,8 @@
  21660. * handler yet has not finished. This also guarantees that
  21661. * the timer is serialized wrt itself.
  21662. */
  21663. - if (likely(base->running_timer != timer)) {
  21664. - /* See the comment in lock_timer_base() */
  21665. - timer_set_base(timer, NULL);
  21666. - spin_unlock(&base->lock);
  21667. - base = new_base;
  21668. - spin_lock(&base->lock);
  21669. - timer_set_base(timer, base);
  21670. - }
  21671. + if (likely(base->running_timer != timer))
  21672. + base = switch_timer_base(timer, base, new_base);
  21673. }
  21674. timer->expires = expires;
  21675. @@ -979,6 +1006,29 @@
  21676. }
  21677. EXPORT_SYMBOL_GPL(add_timer_on);
  21678. +#ifdef CONFIG_PREEMPT_RT_FULL
  21679. +/*
  21680. + * Wait for a running timer
  21681. + */
  21682. +static void wait_for_running_timer(struct timer_list *timer)
  21683. +{
  21684. + struct tvec_base *base = timer->base;
  21685. +
  21686. + if (base->running_timer == timer)
  21687. + wait_event(base->wait_for_running_timer,
  21688. + base->running_timer != timer);
  21689. +}
  21690. +
  21691. +# define wakeup_timer_waiters(b) wake_up(&(b)->wait_for_running_timer)
  21692. +#else
  21693. +static inline void wait_for_running_timer(struct timer_list *timer)
  21694. +{
  21695. + cpu_relax();
  21696. +}
  21697. +
  21698. +# define wakeup_timer_waiters(b) do { } while (0)
  21699. +#endif
  21700. +
  21701. /**
  21702. * del_timer - deactive a timer.
  21703. * @timer: the timer to be deactivated
  21704. @@ -1036,7 +1086,7 @@
  21705. }
  21706. EXPORT_SYMBOL(try_to_del_timer_sync);
  21707. -#ifdef CONFIG_SMP
  21708. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  21709. static DEFINE_PER_CPU(struct tvec_base, __tvec_bases);
  21710. /**
  21711. @@ -1098,7 +1148,7 @@
  21712. int ret = try_to_del_timer_sync(timer);
  21713. if (ret >= 0)
  21714. return ret;
  21715. - cpu_relax();
  21716. + wait_for_running_timer(timer);
  21717. }
  21718. }
  21719. EXPORT_SYMBOL(del_timer_sync);
  21720. @@ -1219,15 +1269,17 @@
  21721. if (irqsafe) {
  21722. spin_unlock(&base->lock);
  21723. call_timer_fn(timer, fn, data);
  21724. + base->running_timer = NULL;
  21725. spin_lock(&base->lock);
  21726. } else {
  21727. spin_unlock_irq(&base->lock);
  21728. call_timer_fn(timer, fn, data);
  21729. + base->running_timer = NULL;
  21730. spin_lock_irq(&base->lock);
  21731. }
  21732. }
  21733. }
  21734. - base->running_timer = NULL;
  21735. + wakeup_timer_waiters(base);
  21736. spin_unlock_irq(&base->lock);
  21737. }
  21738. @@ -1367,6 +1419,14 @@
  21739. if (cpu_is_offline(smp_processor_id()))
  21740. return expires;
  21741. +#ifdef CONFIG_PREEMPT_RT_FULL
  21742. + /*
  21743. + * On PREEMPT_RT we cannot sleep here. As a result we can't take
  21744. + * the base lock to check when the next timer is pending and so
  21745. + * we assume the next jiffy.
  21746. + */
  21747. + return now + 1;
  21748. +#endif
  21749. spin_lock(&base->lock);
  21750. if (base->active_timers) {
  21751. if (time_before_eq(base->next_timer, base->timer_jiffies))
  21752. @@ -1392,13 +1452,13 @@
  21753. /* Note: this timer irq context must be accounted for as well. */
  21754. account_process_tick(p, user_tick);
  21755. + scheduler_tick();
  21756. run_local_timers();
  21757. rcu_check_callbacks(user_tick);
  21758. -#ifdef CONFIG_IRQ_WORK
  21759. +#if defined(CONFIG_IRQ_WORK)
  21760. if (in_irq())
  21761. irq_work_tick();
  21762. #endif
  21763. - scheduler_tick();
  21764. run_posix_cpu_timers(p);
  21765. }
  21766. @@ -1411,6 +1471,8 @@
  21767. hrtimer_run_pending();
  21768. + irq_work_tick_soft();
  21769. +
  21770. if (time_after_eq(jiffies, base->timer_jiffies))
  21771. __run_timers(base);
  21772. }
  21773. @@ -1566,7 +1628,7 @@
  21774. BUG_ON(cpu_online(cpu));
  21775. old_base = per_cpu(tvec_bases, cpu);
  21776. - new_base = get_cpu_var(tvec_bases);
  21777. + new_base = get_local_var(tvec_bases);
  21778. /*
  21779. * The caller is globally serialized and nobody else
  21780. * takes two locks at once, deadlock is not possible.
  21781. @@ -1590,7 +1652,7 @@
  21782. spin_unlock(&old_base->lock);
  21783. spin_unlock_irq(&new_base->lock);
  21784. - put_cpu_var(tvec_bases);
  21785. + put_local_var(tvec_bases);
  21786. }
  21787. static int timer_cpu_notify(struct notifier_block *self,
  21788. @@ -1625,6 +1687,9 @@
  21789. base->cpu = cpu;
  21790. per_cpu(tvec_bases, cpu) = base;
  21791. spin_lock_init(&base->lock);
  21792. +#ifdef CONFIG_PREEMPT_RT_FULL
  21793. + init_waitqueue_head(&base->wait_for_running_timer);
  21794. +#endif
  21795. for (j = 0; j < TVN_SIZE; j++) {
  21796. INIT_LIST_HEAD(base->tv5.vec + j);
  21797. diff -Nur linux-4.1.20.orig/kernel/trace/Kconfig linux-4.1.20/kernel/trace/Kconfig
  21798. --- linux-4.1.20.orig/kernel/trace/Kconfig 2016-03-17 19:11:03.000000000 +0100
  21799. +++ linux-4.1.20/kernel/trace/Kconfig 2016-03-21 20:18:34.000000000 +0100
  21800. @@ -187,6 +187,24 @@
  21801. enabled. This option and the preempt-off timing option can be
  21802. used together or separately.)
  21803. +config INTERRUPT_OFF_HIST
  21804. + bool "Interrupts-off Latency Histogram"
  21805. + depends on IRQSOFF_TRACER
  21806. + help
  21807. + This option generates continuously updated histograms (one per cpu)
  21808. + of the duration of time periods with interrupts disabled. The
  21809. + histograms are disabled by default. To enable them, write a non-zero
  21810. + number to
  21811. +
  21812. + /sys/kernel/debug/tracing/latency_hist/enable/preemptirqsoff
  21813. +
  21814. + If PREEMPT_OFF_HIST is also selected, additional histograms (one
  21815. + per cpu) are generated that accumulate the duration of time periods
  21816. + when both interrupts and preemption are disabled. The histogram data
  21817. + will be located in the debug file system at
  21818. +
  21819. + /sys/kernel/debug/tracing/latency_hist/irqsoff
  21820. +
  21821. config PREEMPT_TRACER
  21822. bool "Preemption-off Latency Tracer"
  21823. default n
  21824. @@ -211,6 +229,24 @@
  21825. enabled. This option and the irqs-off timing option can be
  21826. used together or separately.)
  21827. +config PREEMPT_OFF_HIST
  21828. + bool "Preemption-off Latency Histogram"
  21829. + depends on PREEMPT_TRACER
  21830. + help
  21831. + This option generates continuously updated histograms (one per cpu)
  21832. + of the duration of time periods with preemption disabled. The
  21833. + histograms are disabled by default. To enable them, write a non-zero
  21834. + number to
  21835. +
  21836. + /sys/kernel/debug/tracing/latency_hist/enable/preemptirqsoff
  21837. +
  21838. + If INTERRUPT_OFF_HIST is also selected, additional histograms (one
  21839. + per cpu) are generated that accumulate the duration of time periods
  21840. + when both interrupts and preemption are disabled. The histogram data
  21841. + will be located in the debug file system at
  21842. +
  21843. + /sys/kernel/debug/tracing/latency_hist/preemptoff
  21844. +
  21845. config SCHED_TRACER
  21846. bool "Scheduling Latency Tracer"
  21847. select GENERIC_TRACER
  21848. @@ -221,6 +257,74 @@
  21849. This tracer tracks the latency of the highest priority task
  21850. to be scheduled in, starting from the point it has woken up.
  21851. +config WAKEUP_LATENCY_HIST
  21852. + bool "Scheduling Latency Histogram"
  21853. + depends on SCHED_TRACER
  21854. + help
  21855. + This option generates continuously updated histograms (one per cpu)
  21856. + of the scheduling latency of the highest priority task.
  21857. + The histograms are disabled by default. To enable them, write a
  21858. + non-zero number to
  21859. +
  21860. + /sys/kernel/debug/tracing/latency_hist/enable/wakeup
  21861. +
  21862. + Two different algorithms are used, one to determine the latency of
  21863. + processes that exclusively use the highest priority of the system and
  21864. + another one to determine the latency of processes that share the
  21865. + highest system priority with other processes. The former is used to
  21866. + improve hardware and system software, the latter to optimize the
  21867. + priority design of a given system. The histogram data will be
  21868. + located in the debug file system at
  21869. +
  21870. + /sys/kernel/debug/tracing/latency_hist/wakeup
  21871. +
  21872. + and
  21873. +
  21874. + /sys/kernel/debug/tracing/latency_hist/wakeup/sharedprio
  21875. +
  21876. + If both Scheduling Latency Histogram and Missed Timer Offsets
  21877. + Histogram are selected, additional histogram data will be collected
  21878. + that contain, in addition to the wakeup latency, the timer latency, in
  21879. + case the wakeup was triggered by an expired timer. These histograms
  21880. + are available in the
  21881. +
  21882. + /sys/kernel/debug/tracing/latency_hist/timerandwakeup
  21883. +
  21884. + directory. They reflect the apparent interrupt and scheduling latency
  21885. + and are best suitable to determine the worst-case latency of a given
  21886. + system. To enable these histograms, write a non-zero number to
  21887. +
  21888. + /sys/kernel/debug/tracing/latency_hist/enable/timerandwakeup
  21889. +
  21890. +config MISSED_TIMER_OFFSETS_HIST
  21891. + depends on HIGH_RES_TIMERS
  21892. + select GENERIC_TRACER
  21893. + bool "Missed Timer Offsets Histogram"
  21894. + help
  21895. + Generate a histogram of missed timer offsets in microseconds. The
  21896. + histograms are disabled by default. To enable them, write a non-zero
  21897. + number to
  21898. +
  21899. + /sys/kernel/debug/tracing/latency_hist/enable/missed_timer_offsets
  21900. +
  21901. + The histogram data will be located in the debug file system at
  21902. +
  21903. + /sys/kernel/debug/tracing/latency_hist/missed_timer_offsets
  21904. +
  21905. + If both Scheduling Latency Histogram and Missed Timer Offsets
  21906. + Histogram are selected, additional histogram data will be collected
  21907. + that contain, in addition to the wakeup latency, the timer latency, in
  21908. + case the wakeup was triggered by an expired timer. These histograms
  21909. + are available in the
  21910. +
  21911. + /sys/kernel/debug/tracing/latency_hist/timerandwakeup
  21912. +
  21913. + directory. They reflect the apparent interrupt and scheduling latency
  21914. + and are best suitable to determine the worst-case latency of a given
  21915. + system. To enable these histograms, write a non-zero number to
  21916. +
  21917. + /sys/kernel/debug/tracing/latency_hist/enable/timerandwakeup
  21918. +
  21919. config ENABLE_DEFAULT_TRACERS
  21920. bool "Trace process context switches and events"
  21921. depends on !GENERIC_TRACER
  21922. diff -Nur linux-4.1.20.orig/kernel/trace/Makefile linux-4.1.20/kernel/trace/Makefile
  21923. --- linux-4.1.20.orig/kernel/trace/Makefile 2016-03-17 19:11:03.000000000 +0100
  21924. +++ linux-4.1.20/kernel/trace/Makefile 2016-03-21 20:18:34.000000000 +0100
  21925. @@ -36,6 +36,10 @@
  21926. obj-$(CONFIG_IRQSOFF_TRACER) += trace_irqsoff.o
  21927. obj-$(CONFIG_PREEMPT_TRACER) += trace_irqsoff.o
  21928. obj-$(CONFIG_SCHED_TRACER) += trace_sched_wakeup.o
  21929. +obj-$(CONFIG_INTERRUPT_OFF_HIST) += latency_hist.o
  21930. +obj-$(CONFIG_PREEMPT_OFF_HIST) += latency_hist.o
  21931. +obj-$(CONFIG_WAKEUP_LATENCY_HIST) += latency_hist.o
  21932. +obj-$(CONFIG_MISSED_TIMER_OFFSETS_HIST) += latency_hist.o
  21933. obj-$(CONFIG_NOP_TRACER) += trace_nop.o
  21934. obj-$(CONFIG_STACK_TRACER) += trace_stack.o
  21935. obj-$(CONFIG_MMIOTRACE) += trace_mmiotrace.o
  21936. diff -Nur linux-4.1.20.orig/kernel/trace/latency_hist.c linux-4.1.20/kernel/trace/latency_hist.c
  21937. --- linux-4.1.20.orig/kernel/trace/latency_hist.c 1970-01-01 01:00:00.000000000 +0100
  21938. +++ linux-4.1.20/kernel/trace/latency_hist.c 2016-03-21 20:18:34.000000000 +0100
  21939. @@ -0,0 +1,1178 @@
  21940. +/*
  21941. + * kernel/trace/latency_hist.c
  21942. + *
  21943. + * Add support for histograms of preemption-off latency and
  21944. + * interrupt-off latency and wakeup latency, it depends on
  21945. + * Real-Time Preemption Support.
  21946. + *
  21947. + * Copyright (C) 2005 MontaVista Software, Inc.
  21948. + * Yi Yang <yyang@ch.mvista.com>
  21949. + *
  21950. + * Converted to work with the new latency tracer.
  21951. + * Copyright (C) 2008 Red Hat, Inc.
  21952. + * Steven Rostedt <srostedt@redhat.com>
  21953. + *
  21954. + */
  21955. +#include <linux/module.h>
  21956. +#include <linux/debugfs.h>
  21957. +#include <linux/seq_file.h>
  21958. +#include <linux/percpu.h>
  21959. +#include <linux/kallsyms.h>
  21960. +#include <linux/uaccess.h>
  21961. +#include <linux/sched.h>
  21962. +#include <linux/sched/rt.h>
  21963. +#include <linux/slab.h>
  21964. +#include <linux/atomic.h>
  21965. +#include <asm/div64.h>
  21966. +
  21967. +#include "trace.h"
  21968. +#include <trace/events/sched.h>
  21969. +
  21970. +#define NSECS_PER_USECS 1000L
  21971. +
  21972. +#define CREATE_TRACE_POINTS
  21973. +#include <trace/events/hist.h>
  21974. +
  21975. +enum {
  21976. + IRQSOFF_LATENCY = 0,
  21977. + PREEMPTOFF_LATENCY,
  21978. + PREEMPTIRQSOFF_LATENCY,
  21979. + WAKEUP_LATENCY,
  21980. + WAKEUP_LATENCY_SHAREDPRIO,
  21981. + MISSED_TIMER_OFFSETS,
  21982. + TIMERANDWAKEUP_LATENCY,
  21983. + MAX_LATENCY_TYPE,
  21984. +};
  21985. +
  21986. +#define MAX_ENTRY_NUM 10240
  21987. +
  21988. +struct hist_data {
  21989. + atomic_t hist_mode; /* 0 log, 1 don't log */
  21990. + long offset; /* set it to MAX_ENTRY_NUM/2 for a bipolar scale */
  21991. + long min_lat;
  21992. + long max_lat;
  21993. + unsigned long long below_hist_bound_samples;
  21994. + unsigned long long above_hist_bound_samples;
  21995. + long long accumulate_lat;
  21996. + unsigned long long total_samples;
  21997. + unsigned long long hist_array[MAX_ENTRY_NUM];
  21998. +};
  21999. +
  22000. +struct enable_data {
  22001. + int latency_type;
  22002. + int enabled;
  22003. +};
  22004. +
  22005. +static char *latency_hist_dir_root = "latency_hist";
  22006. +
  22007. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  22008. +static DEFINE_PER_CPU(struct hist_data, irqsoff_hist);
  22009. +static char *irqsoff_hist_dir = "irqsoff";
  22010. +static DEFINE_PER_CPU(cycles_t, hist_irqsoff_start);
  22011. +static DEFINE_PER_CPU(int, hist_irqsoff_counting);
  22012. +#endif
  22013. +
  22014. +#ifdef CONFIG_PREEMPT_OFF_HIST
  22015. +static DEFINE_PER_CPU(struct hist_data, preemptoff_hist);
  22016. +static char *preemptoff_hist_dir = "preemptoff";
  22017. +static DEFINE_PER_CPU(cycles_t, hist_preemptoff_start);
  22018. +static DEFINE_PER_CPU(int, hist_preemptoff_counting);
  22019. +#endif
  22020. +
  22021. +#if defined(CONFIG_PREEMPT_OFF_HIST) && defined(CONFIG_INTERRUPT_OFF_HIST)
  22022. +static DEFINE_PER_CPU(struct hist_data, preemptirqsoff_hist);
  22023. +static char *preemptirqsoff_hist_dir = "preemptirqsoff";
  22024. +static DEFINE_PER_CPU(cycles_t, hist_preemptirqsoff_start);
  22025. +static DEFINE_PER_CPU(int, hist_preemptirqsoff_counting);
  22026. +#endif
  22027. +
  22028. +#if defined(CONFIG_PREEMPT_OFF_HIST) || defined(CONFIG_INTERRUPT_OFF_HIST)
  22029. +static notrace void probe_preemptirqsoff_hist(void *v, int reason, int start);
  22030. +static struct enable_data preemptirqsoff_enabled_data = {
  22031. + .latency_type = PREEMPTIRQSOFF_LATENCY,
  22032. + .enabled = 0,
  22033. +};
  22034. +#endif
  22035. +
  22036. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  22037. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  22038. +struct maxlatproc_data {
  22039. + char comm[FIELD_SIZEOF(struct task_struct, comm)];
  22040. + char current_comm[FIELD_SIZEOF(struct task_struct, comm)];
  22041. + int pid;
  22042. + int current_pid;
  22043. + int prio;
  22044. + int current_prio;
  22045. + long latency;
  22046. + long timeroffset;
  22047. + cycle_t timestamp;
  22048. +};
  22049. +#endif
  22050. +
  22051. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  22052. +static DEFINE_PER_CPU(struct hist_data, wakeup_latency_hist);
  22053. +static DEFINE_PER_CPU(struct hist_data, wakeup_latency_hist_sharedprio);
  22054. +static char *wakeup_latency_hist_dir = "wakeup";
  22055. +static char *wakeup_latency_hist_dir_sharedprio = "sharedprio";
  22056. +static notrace void probe_wakeup_latency_hist_start(void *v,
  22057. + struct task_struct *p);
  22058. +static notrace void probe_wakeup_latency_hist_stop(void *v,
  22059. + struct task_struct *prev, struct task_struct *next);
  22060. +static notrace void probe_sched_migrate_task(void *,
  22061. + struct task_struct *task, int cpu);
  22062. +static struct enable_data wakeup_latency_enabled_data = {
  22063. + .latency_type = WAKEUP_LATENCY,
  22064. + .enabled = 0,
  22065. +};
  22066. +static DEFINE_PER_CPU(struct maxlatproc_data, wakeup_maxlatproc);
  22067. +static DEFINE_PER_CPU(struct maxlatproc_data, wakeup_maxlatproc_sharedprio);
  22068. +static DEFINE_PER_CPU(struct task_struct *, wakeup_task);
  22069. +static DEFINE_PER_CPU(int, wakeup_sharedprio);
  22070. +static unsigned long wakeup_pid;
  22071. +#endif
  22072. +
  22073. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  22074. +static DEFINE_PER_CPU(struct hist_data, missed_timer_offsets);
  22075. +static char *missed_timer_offsets_dir = "missed_timer_offsets";
  22076. +static notrace void probe_hrtimer_interrupt(void *v, int cpu,
  22077. + long long offset, struct task_struct *curr, struct task_struct *task);
  22078. +static struct enable_data missed_timer_offsets_enabled_data = {
  22079. + .latency_type = MISSED_TIMER_OFFSETS,
  22080. + .enabled = 0,
  22081. +};
  22082. +static DEFINE_PER_CPU(struct maxlatproc_data, missed_timer_offsets_maxlatproc);
  22083. +static unsigned long missed_timer_offsets_pid;
  22084. +#endif
  22085. +
  22086. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  22087. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  22088. +static DEFINE_PER_CPU(struct hist_data, timerandwakeup_latency_hist);
  22089. +static char *timerandwakeup_latency_hist_dir = "timerandwakeup";
  22090. +static struct enable_data timerandwakeup_enabled_data = {
  22091. + .latency_type = TIMERANDWAKEUP_LATENCY,
  22092. + .enabled = 0,
  22093. +};
  22094. +static DEFINE_PER_CPU(struct maxlatproc_data, timerandwakeup_maxlatproc);
  22095. +#endif
  22096. +
  22097. +void notrace latency_hist(int latency_type, int cpu, long latency,
  22098. + long timeroffset, cycle_t stop,
  22099. + struct task_struct *p)
  22100. +{
  22101. + struct hist_data *my_hist;
  22102. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  22103. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  22104. + struct maxlatproc_data *mp = NULL;
  22105. +#endif
  22106. +
  22107. + if (!cpu_possible(cpu) || latency_type < 0 ||
  22108. + latency_type >= MAX_LATENCY_TYPE)
  22109. + return;
  22110. +
  22111. + switch (latency_type) {
  22112. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  22113. + case IRQSOFF_LATENCY:
  22114. + my_hist = &per_cpu(irqsoff_hist, cpu);
  22115. + break;
  22116. +#endif
  22117. +#ifdef CONFIG_PREEMPT_OFF_HIST
  22118. + case PREEMPTOFF_LATENCY:
  22119. + my_hist = &per_cpu(preemptoff_hist, cpu);
  22120. + break;
  22121. +#endif
  22122. +#if defined(CONFIG_PREEMPT_OFF_HIST) && defined(CONFIG_INTERRUPT_OFF_HIST)
  22123. + case PREEMPTIRQSOFF_LATENCY:
  22124. + my_hist = &per_cpu(preemptirqsoff_hist, cpu);
  22125. + break;
  22126. +#endif
  22127. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  22128. + case WAKEUP_LATENCY:
  22129. + my_hist = &per_cpu(wakeup_latency_hist, cpu);
  22130. + mp = &per_cpu(wakeup_maxlatproc, cpu);
  22131. + break;
  22132. + case WAKEUP_LATENCY_SHAREDPRIO:
  22133. + my_hist = &per_cpu(wakeup_latency_hist_sharedprio, cpu);
  22134. + mp = &per_cpu(wakeup_maxlatproc_sharedprio, cpu);
  22135. + break;
  22136. +#endif
  22137. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  22138. + case MISSED_TIMER_OFFSETS:
  22139. + my_hist = &per_cpu(missed_timer_offsets, cpu);
  22140. + mp = &per_cpu(missed_timer_offsets_maxlatproc, cpu);
  22141. + break;
  22142. +#endif
  22143. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  22144. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  22145. + case TIMERANDWAKEUP_LATENCY:
  22146. + my_hist = &per_cpu(timerandwakeup_latency_hist, cpu);
  22147. + mp = &per_cpu(timerandwakeup_maxlatproc, cpu);
  22148. + break;
  22149. +#endif
  22150. +
  22151. + default:
  22152. + return;
  22153. + }
  22154. +
  22155. + latency += my_hist->offset;
  22156. +
  22157. + if (atomic_read(&my_hist->hist_mode) == 0)
  22158. + return;
  22159. +
  22160. + if (latency < 0 || latency >= MAX_ENTRY_NUM) {
  22161. + if (latency < 0)
  22162. + my_hist->below_hist_bound_samples++;
  22163. + else
  22164. + my_hist->above_hist_bound_samples++;
  22165. + } else
  22166. + my_hist->hist_array[latency]++;
  22167. +
  22168. + if (unlikely(latency > my_hist->max_lat ||
  22169. + my_hist->min_lat == LONG_MAX)) {
  22170. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  22171. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  22172. + if (latency_type == WAKEUP_LATENCY ||
  22173. + latency_type == WAKEUP_LATENCY_SHAREDPRIO ||
  22174. + latency_type == MISSED_TIMER_OFFSETS ||
  22175. + latency_type == TIMERANDWAKEUP_LATENCY) {
  22176. + strncpy(mp->comm, p->comm, sizeof(mp->comm));
  22177. + strncpy(mp->current_comm, current->comm,
  22178. + sizeof(mp->current_comm));
  22179. + mp->pid = task_pid_nr(p);
  22180. + mp->current_pid = task_pid_nr(current);
  22181. + mp->prio = p->prio;
  22182. + mp->current_prio = current->prio;
  22183. + mp->latency = latency;
  22184. + mp->timeroffset = timeroffset;
  22185. + mp->timestamp = stop;
  22186. + }
  22187. +#endif
  22188. + my_hist->max_lat = latency;
  22189. + }
  22190. + if (unlikely(latency < my_hist->min_lat))
  22191. + my_hist->min_lat = latency;
  22192. + my_hist->total_samples++;
  22193. + my_hist->accumulate_lat += latency;
  22194. +}
  22195. +
  22196. +static void *l_start(struct seq_file *m, loff_t *pos)
  22197. +{
  22198. + loff_t *index_ptr = NULL;
  22199. + loff_t index = *pos;
  22200. + struct hist_data *my_hist = m->private;
  22201. +
  22202. + if (index == 0) {
  22203. + char minstr[32], avgstr[32], maxstr[32];
  22204. +
  22205. + atomic_dec(&my_hist->hist_mode);
  22206. +
  22207. + if (likely(my_hist->total_samples)) {
  22208. + long avg = (long) div64_s64(my_hist->accumulate_lat,
  22209. + my_hist->total_samples);
  22210. + snprintf(minstr, sizeof(minstr), "%ld",
  22211. + my_hist->min_lat - my_hist->offset);
  22212. + snprintf(avgstr, sizeof(avgstr), "%ld",
  22213. + avg - my_hist->offset);
  22214. + snprintf(maxstr, sizeof(maxstr), "%ld",
  22215. + my_hist->max_lat - my_hist->offset);
  22216. + } else {
  22217. + strcpy(minstr, "<undef>");
  22218. + strcpy(avgstr, minstr);
  22219. + strcpy(maxstr, minstr);
  22220. + }
  22221. +
  22222. + seq_printf(m, "#Minimum latency: %s microseconds\n"
  22223. + "#Average latency: %s microseconds\n"
  22224. + "#Maximum latency: %s microseconds\n"
  22225. + "#Total samples: %llu\n"
  22226. + "#There are %llu samples lower than %ld"
  22227. + " microseconds.\n"
  22228. + "#There are %llu samples greater or equal"
  22229. + " than %ld microseconds.\n"
  22230. + "#usecs\t%16s\n",
  22231. + minstr, avgstr, maxstr,
  22232. + my_hist->total_samples,
  22233. + my_hist->below_hist_bound_samples,
  22234. + -my_hist->offset,
  22235. + my_hist->above_hist_bound_samples,
  22236. + MAX_ENTRY_NUM - my_hist->offset,
  22237. + "samples");
  22238. + }
  22239. + if (index < MAX_ENTRY_NUM) {
  22240. + index_ptr = kmalloc(sizeof(loff_t), GFP_KERNEL);
  22241. + if (index_ptr)
  22242. + *index_ptr = index;
  22243. + }
  22244. +
  22245. + return index_ptr;
  22246. +}
  22247. +
  22248. +static void *l_next(struct seq_file *m, void *p, loff_t *pos)
  22249. +{
  22250. + loff_t *index_ptr = p;
  22251. + struct hist_data *my_hist = m->private;
  22252. +
  22253. + if (++*pos >= MAX_ENTRY_NUM) {
  22254. + atomic_inc(&my_hist->hist_mode);
  22255. + return NULL;
  22256. + }
  22257. + *index_ptr = *pos;
  22258. + return index_ptr;
  22259. +}
  22260. +
  22261. +static void l_stop(struct seq_file *m, void *p)
  22262. +{
  22263. + kfree(p);
  22264. +}
  22265. +
  22266. +static int l_show(struct seq_file *m, void *p)
  22267. +{
  22268. + int index = *(loff_t *) p;
  22269. + struct hist_data *my_hist = m->private;
  22270. +
  22271. + seq_printf(m, "%6ld\t%16llu\n", index - my_hist->offset,
  22272. + my_hist->hist_array[index]);
  22273. + return 0;
  22274. +}
  22275. +
  22276. +static const struct seq_operations latency_hist_seq_op = {
  22277. + .start = l_start,
  22278. + .next = l_next,
  22279. + .stop = l_stop,
  22280. + .show = l_show
  22281. +};
  22282. +
  22283. +static int latency_hist_open(struct inode *inode, struct file *file)
  22284. +{
  22285. + int ret;
  22286. +
  22287. + ret = seq_open(file, &latency_hist_seq_op);
  22288. + if (!ret) {
  22289. + struct seq_file *seq = file->private_data;
  22290. + seq->private = inode->i_private;
  22291. + }
  22292. + return ret;
  22293. +}
  22294. +
  22295. +static const struct file_operations latency_hist_fops = {
  22296. + .open = latency_hist_open,
  22297. + .read = seq_read,
  22298. + .llseek = seq_lseek,
  22299. + .release = seq_release,
  22300. +};
  22301. +
  22302. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  22303. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  22304. +static void clear_maxlatprocdata(struct maxlatproc_data *mp)
  22305. +{
  22306. + mp->comm[0] = mp->current_comm[0] = '\0';
  22307. + mp->prio = mp->current_prio = mp->pid = mp->current_pid =
  22308. + mp->latency = mp->timeroffset = -1;
  22309. + mp->timestamp = 0;
  22310. +}
  22311. +#endif
  22312. +
  22313. +static void hist_reset(struct hist_data *hist)
  22314. +{
  22315. + atomic_dec(&hist->hist_mode);
  22316. +
  22317. + memset(hist->hist_array, 0, sizeof(hist->hist_array));
  22318. + hist->below_hist_bound_samples = 0ULL;
  22319. + hist->above_hist_bound_samples = 0ULL;
  22320. + hist->min_lat = LONG_MAX;
  22321. + hist->max_lat = LONG_MIN;
  22322. + hist->total_samples = 0ULL;
  22323. + hist->accumulate_lat = 0LL;
  22324. +
  22325. + atomic_inc(&hist->hist_mode);
  22326. +}
  22327. +
  22328. +static ssize_t
  22329. +latency_hist_reset(struct file *file, const char __user *a,
  22330. + size_t size, loff_t *off)
  22331. +{
  22332. + int cpu;
  22333. + struct hist_data *hist = NULL;
  22334. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  22335. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  22336. + struct maxlatproc_data *mp = NULL;
  22337. +#endif
  22338. + off_t latency_type = (off_t) file->private_data;
  22339. +
  22340. + for_each_online_cpu(cpu) {
  22341. +
  22342. + switch (latency_type) {
  22343. +#ifdef CONFIG_PREEMPT_OFF_HIST
  22344. + case PREEMPTOFF_LATENCY:
  22345. + hist = &per_cpu(preemptoff_hist, cpu);
  22346. + break;
  22347. +#endif
  22348. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  22349. + case IRQSOFF_LATENCY:
  22350. + hist = &per_cpu(irqsoff_hist, cpu);
  22351. + break;
  22352. +#endif
  22353. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  22354. + case PREEMPTIRQSOFF_LATENCY:
  22355. + hist = &per_cpu(preemptirqsoff_hist, cpu);
  22356. + break;
  22357. +#endif
  22358. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  22359. + case WAKEUP_LATENCY:
  22360. + hist = &per_cpu(wakeup_latency_hist, cpu);
  22361. + mp = &per_cpu(wakeup_maxlatproc, cpu);
  22362. + break;
  22363. + case WAKEUP_LATENCY_SHAREDPRIO:
  22364. + hist = &per_cpu(wakeup_latency_hist_sharedprio, cpu);
  22365. + mp = &per_cpu(wakeup_maxlatproc_sharedprio, cpu);
  22366. + break;
  22367. +#endif
  22368. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  22369. + case MISSED_TIMER_OFFSETS:
  22370. + hist = &per_cpu(missed_timer_offsets, cpu);
  22371. + mp = &per_cpu(missed_timer_offsets_maxlatproc, cpu);
  22372. + break;
  22373. +#endif
  22374. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  22375. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  22376. + case TIMERANDWAKEUP_LATENCY:
  22377. + hist = &per_cpu(timerandwakeup_latency_hist, cpu);
  22378. + mp = &per_cpu(timerandwakeup_maxlatproc, cpu);
  22379. + break;
  22380. +#endif
  22381. + }
  22382. +
  22383. + hist_reset(hist);
  22384. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  22385. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  22386. + if (latency_type == WAKEUP_LATENCY ||
  22387. + latency_type == WAKEUP_LATENCY_SHAREDPRIO ||
  22388. + latency_type == MISSED_TIMER_OFFSETS ||
  22389. + latency_type == TIMERANDWAKEUP_LATENCY)
  22390. + clear_maxlatprocdata(mp);
  22391. +#endif
  22392. + }
  22393. +
  22394. + return size;
  22395. +}
  22396. +
  22397. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  22398. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  22399. +static ssize_t
  22400. +show_pid(struct file *file, char __user *ubuf, size_t cnt, loff_t *ppos)
  22401. +{
  22402. + char buf[64];
  22403. + int r;
  22404. + unsigned long *this_pid = file->private_data;
  22405. +
  22406. + r = snprintf(buf, sizeof(buf), "%lu\n", *this_pid);
  22407. + return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  22408. +}
  22409. +
  22410. +static ssize_t do_pid(struct file *file, const char __user *ubuf,
  22411. + size_t cnt, loff_t *ppos)
  22412. +{
  22413. + char buf[64];
  22414. + unsigned long pid;
  22415. + unsigned long *this_pid = file->private_data;
  22416. +
  22417. + if (cnt >= sizeof(buf))
  22418. + return -EINVAL;
  22419. +
  22420. + if (copy_from_user(&buf, ubuf, cnt))
  22421. + return -EFAULT;
  22422. +
  22423. + buf[cnt] = '\0';
  22424. +
  22425. + if (kstrtoul(buf, 10, &pid))
  22426. + return -EINVAL;
  22427. +
  22428. + *this_pid = pid;
  22429. +
  22430. + return cnt;
  22431. +}
  22432. +#endif
  22433. +
  22434. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  22435. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  22436. +static ssize_t
  22437. +show_maxlatproc(struct file *file, char __user *ubuf, size_t cnt, loff_t *ppos)
  22438. +{
  22439. + int r;
  22440. + struct maxlatproc_data *mp = file->private_data;
  22441. + int strmaxlen = (TASK_COMM_LEN * 2) + (8 * 8);
  22442. + unsigned long long t;
  22443. + unsigned long usecs, secs;
  22444. + char *buf;
  22445. +
  22446. + if (mp->pid == -1 || mp->current_pid == -1) {
  22447. + buf = "(none)\n";
  22448. + return simple_read_from_buffer(ubuf, cnt, ppos, buf,
  22449. + strlen(buf));
  22450. + }
  22451. +
  22452. + buf = kmalloc(strmaxlen, GFP_KERNEL);
  22453. + if (buf == NULL)
  22454. + return -ENOMEM;
  22455. +
  22456. + t = ns2usecs(mp->timestamp);
  22457. + usecs = do_div(t, USEC_PER_SEC);
  22458. + secs = (unsigned long) t;
  22459. + r = snprintf(buf, strmaxlen,
  22460. + "%d %d %ld (%ld) %s <- %d %d %s %lu.%06lu\n", mp->pid,
  22461. + MAX_RT_PRIO-1 - mp->prio, mp->latency, mp->timeroffset, mp->comm,
  22462. + mp->current_pid, MAX_RT_PRIO-1 - mp->current_prio, mp->current_comm,
  22463. + secs, usecs);
  22464. + r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  22465. + kfree(buf);
  22466. + return r;
  22467. +}
  22468. +#endif
  22469. +
  22470. +static ssize_t
  22471. +show_enable(struct file *file, char __user *ubuf, size_t cnt, loff_t *ppos)
  22472. +{
  22473. + char buf[64];
  22474. + struct enable_data *ed = file->private_data;
  22475. + int r;
  22476. +
  22477. + r = snprintf(buf, sizeof(buf), "%d\n", ed->enabled);
  22478. + return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  22479. +}
  22480. +
  22481. +static ssize_t
  22482. +do_enable(struct file *file, const char __user *ubuf, size_t cnt, loff_t *ppos)
  22483. +{
  22484. + char buf[64];
  22485. + long enable;
  22486. + struct enable_data *ed = file->private_data;
  22487. +
  22488. + if (cnt >= sizeof(buf))
  22489. + return -EINVAL;
  22490. +
  22491. + if (copy_from_user(&buf, ubuf, cnt))
  22492. + return -EFAULT;
  22493. +
  22494. + buf[cnt] = 0;
  22495. +
  22496. + if (kstrtoul(buf, 10, &enable))
  22497. + return -EINVAL;
  22498. +
  22499. + if ((enable && ed->enabled) || (!enable && !ed->enabled))
  22500. + return cnt;
  22501. +
  22502. + if (enable) {
  22503. + int ret;
  22504. +
  22505. + switch (ed->latency_type) {
  22506. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  22507. + case PREEMPTIRQSOFF_LATENCY:
  22508. + ret = register_trace_preemptirqsoff_hist(
  22509. + probe_preemptirqsoff_hist, NULL);
  22510. + if (ret) {
  22511. + pr_info("wakeup trace: Couldn't assign "
  22512. + "probe_preemptirqsoff_hist "
  22513. + "to trace_preemptirqsoff_hist\n");
  22514. + return ret;
  22515. + }
  22516. + break;
  22517. +#endif
  22518. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  22519. + case WAKEUP_LATENCY:
  22520. + ret = register_trace_sched_wakeup(
  22521. + probe_wakeup_latency_hist_start, NULL);
  22522. + if (ret) {
  22523. + pr_info("wakeup trace: Couldn't assign "
  22524. + "probe_wakeup_latency_hist_start "
  22525. + "to trace_sched_wakeup\n");
  22526. + return ret;
  22527. + }
  22528. + ret = register_trace_sched_wakeup_new(
  22529. + probe_wakeup_latency_hist_start, NULL);
  22530. + if (ret) {
  22531. + pr_info("wakeup trace: Couldn't assign "
  22532. + "probe_wakeup_latency_hist_start "
  22533. + "to trace_sched_wakeup_new\n");
  22534. + unregister_trace_sched_wakeup(
  22535. + probe_wakeup_latency_hist_start, NULL);
  22536. + return ret;
  22537. + }
  22538. + ret = register_trace_sched_switch(
  22539. + probe_wakeup_latency_hist_stop, NULL);
  22540. + if (ret) {
  22541. + pr_info("wakeup trace: Couldn't assign "
  22542. + "probe_wakeup_latency_hist_stop "
  22543. + "to trace_sched_switch\n");
  22544. + unregister_trace_sched_wakeup(
  22545. + probe_wakeup_latency_hist_start, NULL);
  22546. + unregister_trace_sched_wakeup_new(
  22547. + probe_wakeup_latency_hist_start, NULL);
  22548. + return ret;
  22549. + }
  22550. + ret = register_trace_sched_migrate_task(
  22551. + probe_sched_migrate_task, NULL);
  22552. + if (ret) {
  22553. + pr_info("wakeup trace: Couldn't assign "
  22554. + "probe_sched_migrate_task "
  22555. + "to trace_sched_migrate_task\n");
  22556. + unregister_trace_sched_wakeup(
  22557. + probe_wakeup_latency_hist_start, NULL);
  22558. + unregister_trace_sched_wakeup_new(
  22559. + probe_wakeup_latency_hist_start, NULL);
  22560. + unregister_trace_sched_switch(
  22561. + probe_wakeup_latency_hist_stop, NULL);
  22562. + return ret;
  22563. + }
  22564. + break;
  22565. +#endif
  22566. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  22567. + case MISSED_TIMER_OFFSETS:
  22568. + ret = register_trace_hrtimer_interrupt(
  22569. + probe_hrtimer_interrupt, NULL);
  22570. + if (ret) {
  22571. + pr_info("wakeup trace: Couldn't assign "
  22572. + "probe_hrtimer_interrupt "
  22573. + "to trace_hrtimer_interrupt\n");
  22574. + return ret;
  22575. + }
  22576. + break;
  22577. +#endif
  22578. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  22579. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  22580. + case TIMERANDWAKEUP_LATENCY:
  22581. + if (!wakeup_latency_enabled_data.enabled ||
  22582. + !missed_timer_offsets_enabled_data.enabled)
  22583. + return -EINVAL;
  22584. + break;
  22585. +#endif
  22586. + default:
  22587. + break;
  22588. + }
  22589. + } else {
  22590. + switch (ed->latency_type) {
  22591. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  22592. + case PREEMPTIRQSOFF_LATENCY:
  22593. + {
  22594. + int cpu;
  22595. +
  22596. + unregister_trace_preemptirqsoff_hist(
  22597. + probe_preemptirqsoff_hist, NULL);
  22598. + for_each_online_cpu(cpu) {
  22599. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  22600. + per_cpu(hist_irqsoff_counting,
  22601. + cpu) = 0;
  22602. +#endif
  22603. +#ifdef CONFIG_PREEMPT_OFF_HIST
  22604. + per_cpu(hist_preemptoff_counting,
  22605. + cpu) = 0;
  22606. +#endif
  22607. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  22608. + per_cpu(hist_preemptirqsoff_counting,
  22609. + cpu) = 0;
  22610. +#endif
  22611. + }
  22612. + }
  22613. + break;
  22614. +#endif
  22615. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  22616. + case WAKEUP_LATENCY:
  22617. + {
  22618. + int cpu;
  22619. +
  22620. + unregister_trace_sched_wakeup(
  22621. + probe_wakeup_latency_hist_start, NULL);
  22622. + unregister_trace_sched_wakeup_new(
  22623. + probe_wakeup_latency_hist_start, NULL);
  22624. + unregister_trace_sched_switch(
  22625. + probe_wakeup_latency_hist_stop, NULL);
  22626. + unregister_trace_sched_migrate_task(
  22627. + probe_sched_migrate_task, NULL);
  22628. +
  22629. + for_each_online_cpu(cpu) {
  22630. + per_cpu(wakeup_task, cpu) = NULL;
  22631. + per_cpu(wakeup_sharedprio, cpu) = 0;
  22632. + }
  22633. + }
  22634. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  22635. + timerandwakeup_enabled_data.enabled = 0;
  22636. +#endif
  22637. + break;
  22638. +#endif
  22639. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  22640. + case MISSED_TIMER_OFFSETS:
  22641. + unregister_trace_hrtimer_interrupt(
  22642. + probe_hrtimer_interrupt, NULL);
  22643. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  22644. + timerandwakeup_enabled_data.enabled = 0;
  22645. +#endif
  22646. + break;
  22647. +#endif
  22648. + default:
  22649. + break;
  22650. + }
  22651. + }
  22652. + ed->enabled = enable;
  22653. + return cnt;
  22654. +}
  22655. +
  22656. +static const struct file_operations latency_hist_reset_fops = {
  22657. + .open = tracing_open_generic,
  22658. + .write = latency_hist_reset,
  22659. +};
  22660. +
  22661. +static const struct file_operations enable_fops = {
  22662. + .open = tracing_open_generic,
  22663. + .read = show_enable,
  22664. + .write = do_enable,
  22665. +};
  22666. +
  22667. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  22668. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  22669. +static const struct file_operations pid_fops = {
  22670. + .open = tracing_open_generic,
  22671. + .read = show_pid,
  22672. + .write = do_pid,
  22673. +};
  22674. +
  22675. +static const struct file_operations maxlatproc_fops = {
  22676. + .open = tracing_open_generic,
  22677. + .read = show_maxlatproc,
  22678. +};
  22679. +#endif
  22680. +
  22681. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  22682. +static notrace void probe_preemptirqsoff_hist(void *v, int reason,
  22683. + int starthist)
  22684. +{
  22685. + int cpu = raw_smp_processor_id();
  22686. + int time_set = 0;
  22687. +
  22688. + if (starthist) {
  22689. + cycle_t uninitialized_var(start);
  22690. +
  22691. + if (!preempt_count() && !irqs_disabled())
  22692. + return;
  22693. +
  22694. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  22695. + if ((reason == IRQS_OFF || reason == TRACE_START) &&
  22696. + !per_cpu(hist_irqsoff_counting, cpu)) {
  22697. + per_cpu(hist_irqsoff_counting, cpu) = 1;
  22698. + start = ftrace_now(cpu);
  22699. + time_set++;
  22700. + per_cpu(hist_irqsoff_start, cpu) = start;
  22701. + }
  22702. +#endif
  22703. +
  22704. +#ifdef CONFIG_PREEMPT_OFF_HIST
  22705. + if ((reason == PREEMPT_OFF || reason == TRACE_START) &&
  22706. + !per_cpu(hist_preemptoff_counting, cpu)) {
  22707. + per_cpu(hist_preemptoff_counting, cpu) = 1;
  22708. + if (!(time_set++))
  22709. + start = ftrace_now(cpu);
  22710. + per_cpu(hist_preemptoff_start, cpu) = start;
  22711. + }
  22712. +#endif
  22713. +
  22714. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  22715. + if (per_cpu(hist_irqsoff_counting, cpu) &&
  22716. + per_cpu(hist_preemptoff_counting, cpu) &&
  22717. + !per_cpu(hist_preemptirqsoff_counting, cpu)) {
  22718. + per_cpu(hist_preemptirqsoff_counting, cpu) = 1;
  22719. + if (!time_set)
  22720. + start = ftrace_now(cpu);
  22721. + per_cpu(hist_preemptirqsoff_start, cpu) = start;
  22722. + }
  22723. +#endif
  22724. + } else {
  22725. + cycle_t uninitialized_var(stop);
  22726. +
  22727. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  22728. + if ((reason == IRQS_ON || reason == TRACE_STOP) &&
  22729. + per_cpu(hist_irqsoff_counting, cpu)) {
  22730. + cycle_t start = per_cpu(hist_irqsoff_start, cpu);
  22731. +
  22732. + stop = ftrace_now(cpu);
  22733. + time_set++;
  22734. + if (start) {
  22735. + long latency = ((long) (stop - start)) /
  22736. + NSECS_PER_USECS;
  22737. +
  22738. + latency_hist(IRQSOFF_LATENCY, cpu, latency, 0,
  22739. + stop, NULL);
  22740. + }
  22741. + per_cpu(hist_irqsoff_counting, cpu) = 0;
  22742. + }
  22743. +#endif
  22744. +
  22745. +#ifdef CONFIG_PREEMPT_OFF_HIST
  22746. + if ((reason == PREEMPT_ON || reason == TRACE_STOP) &&
  22747. + per_cpu(hist_preemptoff_counting, cpu)) {
  22748. + cycle_t start = per_cpu(hist_preemptoff_start, cpu);
  22749. +
  22750. + if (!(time_set++))
  22751. + stop = ftrace_now(cpu);
  22752. + if (start) {
  22753. + long latency = ((long) (stop - start)) /
  22754. + NSECS_PER_USECS;
  22755. +
  22756. + latency_hist(PREEMPTOFF_LATENCY, cpu, latency,
  22757. + 0, stop, NULL);
  22758. + }
  22759. + per_cpu(hist_preemptoff_counting, cpu) = 0;
  22760. + }
  22761. +#endif
  22762. +
  22763. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  22764. + if ((!per_cpu(hist_irqsoff_counting, cpu) ||
  22765. + !per_cpu(hist_preemptoff_counting, cpu)) &&
  22766. + per_cpu(hist_preemptirqsoff_counting, cpu)) {
  22767. + cycle_t start = per_cpu(hist_preemptirqsoff_start, cpu);
  22768. +
  22769. + if (!time_set)
  22770. + stop = ftrace_now(cpu);
  22771. + if (start) {
  22772. + long latency = ((long) (stop - start)) /
  22773. + NSECS_PER_USECS;
  22774. +
  22775. + latency_hist(PREEMPTIRQSOFF_LATENCY, cpu,
  22776. + latency, 0, stop, NULL);
  22777. + }
  22778. + per_cpu(hist_preemptirqsoff_counting, cpu) = 0;
  22779. + }
  22780. +#endif
  22781. + }
  22782. +}
  22783. +#endif
  22784. +
  22785. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  22786. +static DEFINE_RAW_SPINLOCK(wakeup_lock);
  22787. +static notrace void probe_sched_migrate_task(void *v, struct task_struct *task,
  22788. + int cpu)
  22789. +{
  22790. + int old_cpu = task_cpu(task);
  22791. +
  22792. + if (cpu != old_cpu) {
  22793. + unsigned long flags;
  22794. + struct task_struct *cpu_wakeup_task;
  22795. +
  22796. + raw_spin_lock_irqsave(&wakeup_lock, flags);
  22797. +
  22798. + cpu_wakeup_task = per_cpu(wakeup_task, old_cpu);
  22799. + if (task == cpu_wakeup_task) {
  22800. + put_task_struct(cpu_wakeup_task);
  22801. + per_cpu(wakeup_task, old_cpu) = NULL;
  22802. + cpu_wakeup_task = per_cpu(wakeup_task, cpu) = task;
  22803. + get_task_struct(cpu_wakeup_task);
  22804. + }
  22805. +
  22806. + raw_spin_unlock_irqrestore(&wakeup_lock, flags);
  22807. + }
  22808. +}
  22809. +
  22810. +static notrace void probe_wakeup_latency_hist_start(void *v,
  22811. + struct task_struct *p)
  22812. +{
  22813. + unsigned long flags;
  22814. + struct task_struct *curr = current;
  22815. + int cpu = task_cpu(p);
  22816. + struct task_struct *cpu_wakeup_task;
  22817. +
  22818. + raw_spin_lock_irqsave(&wakeup_lock, flags);
  22819. +
  22820. + cpu_wakeup_task = per_cpu(wakeup_task, cpu);
  22821. +
  22822. + if (wakeup_pid) {
  22823. + if ((cpu_wakeup_task && p->prio == cpu_wakeup_task->prio) ||
  22824. + p->prio == curr->prio)
  22825. + per_cpu(wakeup_sharedprio, cpu) = 1;
  22826. + if (likely(wakeup_pid != task_pid_nr(p)))
  22827. + goto out;
  22828. + } else {
  22829. + if (likely(!rt_task(p)) ||
  22830. + (cpu_wakeup_task && p->prio > cpu_wakeup_task->prio) ||
  22831. + p->prio > curr->prio)
  22832. + goto out;
  22833. + if ((cpu_wakeup_task && p->prio == cpu_wakeup_task->prio) ||
  22834. + p->prio == curr->prio)
  22835. + per_cpu(wakeup_sharedprio, cpu) = 1;
  22836. + }
  22837. +
  22838. + if (cpu_wakeup_task)
  22839. + put_task_struct(cpu_wakeup_task);
  22840. + cpu_wakeup_task = per_cpu(wakeup_task, cpu) = p;
  22841. + get_task_struct(cpu_wakeup_task);
  22842. + cpu_wakeup_task->preempt_timestamp_hist =
  22843. + ftrace_now(raw_smp_processor_id());
  22844. +out:
  22845. + raw_spin_unlock_irqrestore(&wakeup_lock, flags);
  22846. +}
  22847. +
  22848. +static notrace void probe_wakeup_latency_hist_stop(void *v,
  22849. + struct task_struct *prev, struct task_struct *next)
  22850. +{
  22851. + unsigned long flags;
  22852. + int cpu = task_cpu(next);
  22853. + long latency;
  22854. + cycle_t stop;
  22855. + struct task_struct *cpu_wakeup_task;
  22856. +
  22857. + raw_spin_lock_irqsave(&wakeup_lock, flags);
  22858. +
  22859. + cpu_wakeup_task = per_cpu(wakeup_task, cpu);
  22860. +
  22861. + if (cpu_wakeup_task == NULL)
  22862. + goto out;
  22863. +
  22864. + /* Already running? */
  22865. + if (unlikely(current == cpu_wakeup_task))
  22866. + goto out_reset;
  22867. +
  22868. + if (next != cpu_wakeup_task) {
  22869. + if (next->prio < cpu_wakeup_task->prio)
  22870. + goto out_reset;
  22871. +
  22872. + if (next->prio == cpu_wakeup_task->prio)
  22873. + per_cpu(wakeup_sharedprio, cpu) = 1;
  22874. +
  22875. + goto out;
  22876. + }
  22877. +
  22878. + if (current->prio == cpu_wakeup_task->prio)
  22879. + per_cpu(wakeup_sharedprio, cpu) = 1;
  22880. +
  22881. + /*
  22882. + * The task we are waiting for is about to be switched to.
  22883. + * Calculate latency and store it in histogram.
  22884. + */
  22885. + stop = ftrace_now(raw_smp_processor_id());
  22886. +
  22887. + latency = ((long) (stop - next->preempt_timestamp_hist)) /
  22888. + NSECS_PER_USECS;
  22889. +
  22890. + if (per_cpu(wakeup_sharedprio, cpu)) {
  22891. + latency_hist(WAKEUP_LATENCY_SHAREDPRIO, cpu, latency, 0, stop,
  22892. + next);
  22893. + per_cpu(wakeup_sharedprio, cpu) = 0;
  22894. + } else {
  22895. + latency_hist(WAKEUP_LATENCY, cpu, latency, 0, stop, next);
  22896. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  22897. + if (timerandwakeup_enabled_data.enabled) {
  22898. + latency_hist(TIMERANDWAKEUP_LATENCY, cpu,
  22899. + next->timer_offset + latency, next->timer_offset,
  22900. + stop, next);
  22901. + }
  22902. +#endif
  22903. + }
  22904. +
  22905. +out_reset:
  22906. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  22907. + next->timer_offset = 0;
  22908. +#endif
  22909. + put_task_struct(cpu_wakeup_task);
  22910. + per_cpu(wakeup_task, cpu) = NULL;
  22911. +out:
  22912. + raw_spin_unlock_irqrestore(&wakeup_lock, flags);
  22913. +}
  22914. +#endif
  22915. +
  22916. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  22917. +static notrace void probe_hrtimer_interrupt(void *v, int cpu,
  22918. + long long latency_ns, struct task_struct *curr,
  22919. + struct task_struct *task)
  22920. +{
  22921. + if (latency_ns <= 0 && task != NULL && rt_task(task) &&
  22922. + (task->prio < curr->prio ||
  22923. + (task->prio == curr->prio &&
  22924. + !cpumask_test_cpu(cpu, &task->cpus_allowed)))) {
  22925. + long latency;
  22926. + cycle_t now;
  22927. +
  22928. + if (missed_timer_offsets_pid) {
  22929. + if (likely(missed_timer_offsets_pid !=
  22930. + task_pid_nr(task)))
  22931. + return;
  22932. + }
  22933. +
  22934. + now = ftrace_now(cpu);
  22935. + latency = (long) div_s64(-latency_ns, NSECS_PER_USECS);
  22936. + latency_hist(MISSED_TIMER_OFFSETS, cpu, latency, latency, now,
  22937. + task);
  22938. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  22939. + task->timer_offset = latency;
  22940. +#endif
  22941. + }
  22942. +}
  22943. +#endif
  22944. +
  22945. +static __init int latency_hist_init(void)
  22946. +{
  22947. + struct dentry *latency_hist_root = NULL;
  22948. + struct dentry *dentry;
  22949. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  22950. + struct dentry *dentry_sharedprio;
  22951. +#endif
  22952. + struct dentry *entry;
  22953. + struct dentry *enable_root;
  22954. + int i = 0;
  22955. + struct hist_data *my_hist;
  22956. + char name[64];
  22957. + char *cpufmt = "CPU%d";
  22958. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  22959. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  22960. + char *cpufmt_maxlatproc = "max_latency-CPU%d";
  22961. + struct maxlatproc_data *mp = NULL;
  22962. +#endif
  22963. +
  22964. + dentry = tracing_init_dentry();
  22965. + latency_hist_root = debugfs_create_dir(latency_hist_dir_root, dentry);
  22966. + enable_root = debugfs_create_dir("enable", latency_hist_root);
  22967. +
  22968. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  22969. + dentry = debugfs_create_dir(irqsoff_hist_dir, latency_hist_root);
  22970. + for_each_possible_cpu(i) {
  22971. + sprintf(name, cpufmt, i);
  22972. + entry = debugfs_create_file(name, 0444, dentry,
  22973. + &per_cpu(irqsoff_hist, i), &latency_hist_fops);
  22974. + my_hist = &per_cpu(irqsoff_hist, i);
  22975. + atomic_set(&my_hist->hist_mode, 1);
  22976. + my_hist->min_lat = LONG_MAX;
  22977. + }
  22978. + entry = debugfs_create_file("reset", 0644, dentry,
  22979. + (void *)IRQSOFF_LATENCY, &latency_hist_reset_fops);
  22980. +#endif
  22981. +
  22982. +#ifdef CONFIG_PREEMPT_OFF_HIST
  22983. + dentry = debugfs_create_dir(preemptoff_hist_dir,
  22984. + latency_hist_root);
  22985. + for_each_possible_cpu(i) {
  22986. + sprintf(name, cpufmt, i);
  22987. + entry = debugfs_create_file(name, 0444, dentry,
  22988. + &per_cpu(preemptoff_hist, i), &latency_hist_fops);
  22989. + my_hist = &per_cpu(preemptoff_hist, i);
  22990. + atomic_set(&my_hist->hist_mode, 1);
  22991. + my_hist->min_lat = LONG_MAX;
  22992. + }
  22993. + entry = debugfs_create_file("reset", 0644, dentry,
  22994. + (void *)PREEMPTOFF_LATENCY, &latency_hist_reset_fops);
  22995. +#endif
  22996. +
  22997. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  22998. + dentry = debugfs_create_dir(preemptirqsoff_hist_dir,
  22999. + latency_hist_root);
  23000. + for_each_possible_cpu(i) {
  23001. + sprintf(name, cpufmt, i);
  23002. + entry = debugfs_create_file(name, 0444, dentry,
  23003. + &per_cpu(preemptirqsoff_hist, i), &latency_hist_fops);
  23004. + my_hist = &per_cpu(preemptirqsoff_hist, i);
  23005. + atomic_set(&my_hist->hist_mode, 1);
  23006. + my_hist->min_lat = LONG_MAX;
  23007. + }
  23008. + entry = debugfs_create_file("reset", 0644, dentry,
  23009. + (void *)PREEMPTIRQSOFF_LATENCY, &latency_hist_reset_fops);
  23010. +#endif
  23011. +
  23012. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  23013. + entry = debugfs_create_file("preemptirqsoff", 0644,
  23014. + enable_root, (void *)&preemptirqsoff_enabled_data,
  23015. + &enable_fops);
  23016. +#endif
  23017. +
  23018. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  23019. + dentry = debugfs_create_dir(wakeup_latency_hist_dir,
  23020. + latency_hist_root);
  23021. + dentry_sharedprio = debugfs_create_dir(
  23022. + wakeup_latency_hist_dir_sharedprio, dentry);
  23023. + for_each_possible_cpu(i) {
  23024. + sprintf(name, cpufmt, i);
  23025. +
  23026. + entry = debugfs_create_file(name, 0444, dentry,
  23027. + &per_cpu(wakeup_latency_hist, i),
  23028. + &latency_hist_fops);
  23029. + my_hist = &per_cpu(wakeup_latency_hist, i);
  23030. + atomic_set(&my_hist->hist_mode, 1);
  23031. + my_hist->min_lat = LONG_MAX;
  23032. +
  23033. + entry = debugfs_create_file(name, 0444, dentry_sharedprio,
  23034. + &per_cpu(wakeup_latency_hist_sharedprio, i),
  23035. + &latency_hist_fops);
  23036. + my_hist = &per_cpu(wakeup_latency_hist_sharedprio, i);
  23037. + atomic_set(&my_hist->hist_mode, 1);
  23038. + my_hist->min_lat = LONG_MAX;
  23039. +
  23040. + sprintf(name, cpufmt_maxlatproc, i);
  23041. +
  23042. + mp = &per_cpu(wakeup_maxlatproc, i);
  23043. + entry = debugfs_create_file(name, 0444, dentry, mp,
  23044. + &maxlatproc_fops);
  23045. + clear_maxlatprocdata(mp);
  23046. +
  23047. + mp = &per_cpu(wakeup_maxlatproc_sharedprio, i);
  23048. + entry = debugfs_create_file(name, 0444, dentry_sharedprio, mp,
  23049. + &maxlatproc_fops);
  23050. + clear_maxlatprocdata(mp);
  23051. + }
  23052. + entry = debugfs_create_file("pid", 0644, dentry,
  23053. + (void *)&wakeup_pid, &pid_fops);
  23054. + entry = debugfs_create_file("reset", 0644, dentry,
  23055. + (void *)WAKEUP_LATENCY, &latency_hist_reset_fops);
  23056. + entry = debugfs_create_file("reset", 0644, dentry_sharedprio,
  23057. + (void *)WAKEUP_LATENCY_SHAREDPRIO, &latency_hist_reset_fops);
  23058. + entry = debugfs_create_file("wakeup", 0644,
  23059. + enable_root, (void *)&wakeup_latency_enabled_data,
  23060. + &enable_fops);
  23061. +#endif
  23062. +
  23063. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  23064. + dentry = debugfs_create_dir(missed_timer_offsets_dir,
  23065. + latency_hist_root);
  23066. + for_each_possible_cpu(i) {
  23067. + sprintf(name, cpufmt, i);
  23068. + entry = debugfs_create_file(name, 0444, dentry,
  23069. + &per_cpu(missed_timer_offsets, i), &latency_hist_fops);
  23070. + my_hist = &per_cpu(missed_timer_offsets, i);
  23071. + atomic_set(&my_hist->hist_mode, 1);
  23072. + my_hist->min_lat = LONG_MAX;
  23073. +
  23074. + sprintf(name, cpufmt_maxlatproc, i);
  23075. + mp = &per_cpu(missed_timer_offsets_maxlatproc, i);
  23076. + entry = debugfs_create_file(name, 0444, dentry, mp,
  23077. + &maxlatproc_fops);
  23078. + clear_maxlatprocdata(mp);
  23079. + }
  23080. + entry = debugfs_create_file("pid", 0644, dentry,
  23081. + (void *)&missed_timer_offsets_pid, &pid_fops);
  23082. + entry = debugfs_create_file("reset", 0644, dentry,
  23083. + (void *)MISSED_TIMER_OFFSETS, &latency_hist_reset_fops);
  23084. + entry = debugfs_create_file("missed_timer_offsets", 0644,
  23085. + enable_root, (void *)&missed_timer_offsets_enabled_data,
  23086. + &enable_fops);
  23087. +#endif
  23088. +
  23089. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  23090. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  23091. + dentry = debugfs_create_dir(timerandwakeup_latency_hist_dir,
  23092. + latency_hist_root);
  23093. + for_each_possible_cpu(i) {
  23094. + sprintf(name, cpufmt, i);
  23095. + entry = debugfs_create_file(name, 0444, dentry,
  23096. + &per_cpu(timerandwakeup_latency_hist, i),
  23097. + &latency_hist_fops);
  23098. + my_hist = &per_cpu(timerandwakeup_latency_hist, i);
  23099. + atomic_set(&my_hist->hist_mode, 1);
  23100. + my_hist->min_lat = LONG_MAX;
  23101. +
  23102. + sprintf(name, cpufmt_maxlatproc, i);
  23103. + mp = &per_cpu(timerandwakeup_maxlatproc, i);
  23104. + entry = debugfs_create_file(name, 0444, dentry, mp,
  23105. + &maxlatproc_fops);
  23106. + clear_maxlatprocdata(mp);
  23107. + }
  23108. + entry = debugfs_create_file("reset", 0644, dentry,
  23109. + (void *)TIMERANDWAKEUP_LATENCY, &latency_hist_reset_fops);
  23110. + entry = debugfs_create_file("timerandwakeup", 0644,
  23111. + enable_root, (void *)&timerandwakeup_enabled_data,
  23112. + &enable_fops);
  23113. +#endif
  23114. + return 0;
  23115. +}
  23116. +
  23117. +device_initcall(latency_hist_init);
  23118. diff -Nur linux-4.1.20.orig/kernel/trace/trace.c linux-4.1.20/kernel/trace/trace.c
  23119. --- linux-4.1.20.orig/kernel/trace/trace.c 2016-03-17 19:11:03.000000000 +0100
  23120. +++ linux-4.1.20/kernel/trace/trace.c 2016-03-21 20:18:34.000000000 +0100
  23121. @@ -1630,6 +1630,7 @@
  23122. struct task_struct *tsk = current;
  23123. entry->preempt_count = pc & 0xff;
  23124. + entry->preempt_lazy_count = preempt_lazy_count();
  23125. entry->pid = (tsk) ? tsk->pid : 0;
  23126. entry->flags =
  23127. #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
  23128. @@ -1639,8 +1640,11 @@
  23129. #endif
  23130. ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
  23131. ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
  23132. - (tif_need_resched() ? TRACE_FLAG_NEED_RESCHED : 0) |
  23133. + (tif_need_resched_now() ? TRACE_FLAG_NEED_RESCHED : 0) |
  23134. + (need_resched_lazy() ? TRACE_FLAG_NEED_RESCHED_LAZY : 0) |
  23135. (test_preempt_need_resched() ? TRACE_FLAG_PREEMPT_RESCHED : 0);
  23136. +
  23137. + entry->migrate_disable = (tsk) ? __migrate_disabled(tsk) & 0xFF : 0;
  23138. }
  23139. EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
  23140. @@ -2558,14 +2562,17 @@
  23141. static void print_lat_help_header(struct seq_file *m)
  23142. {
  23143. - seq_puts(m, "# _------=> CPU# \n"
  23144. - "# / _-----=> irqs-off \n"
  23145. - "# | / _----=> need-resched \n"
  23146. - "# || / _---=> hardirq/softirq \n"
  23147. - "# ||| / _--=> preempt-depth \n"
  23148. - "# |||| / delay \n"
  23149. - "# cmd pid ||||| time | caller \n"
  23150. - "# \\ / ||||| \\ | / \n");
  23151. + seq_puts(m, "# _--------=> CPU# \n"
  23152. + "# / _-------=> irqs-off \n"
  23153. + "# | / _------=> need-resched \n"
  23154. + "# || / _-----=> need-resched_lazy \n"
  23155. + "# ||| / _----=> hardirq/softirq \n"
  23156. + "# |||| / _---=> preempt-depth \n"
  23157. + "# ||||| / _--=> preempt-lazy-depth\n"
  23158. + "# |||||| / _-=> migrate-disable \n"
  23159. + "# ||||||| / delay \n"
  23160. + "# cmd pid |||||||| time | caller \n"
  23161. + "# \\ / |||||||| \\ | / \n");
  23162. }
  23163. static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
  23164. @@ -2591,11 +2598,14 @@
  23165. print_event_info(buf, m);
  23166. seq_puts(m, "# _-----=> irqs-off\n"
  23167. "# / _----=> need-resched\n"
  23168. - "# | / _---=> hardirq/softirq\n"
  23169. - "# || / _--=> preempt-depth\n"
  23170. - "# ||| / delay\n"
  23171. - "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n"
  23172. - "# | | | |||| | |\n");
  23173. + "# |/ _-----=> need-resched_lazy\n"
  23174. + "# || / _---=> hardirq/softirq\n"
  23175. + "# ||| / _--=> preempt-depth\n"
  23176. + "# |||| /_--=> preempt-lazy-depth\n"
  23177. + "# ||||| _-=> migrate-disable \n"
  23178. + "# ||||| / delay\n"
  23179. + "# TASK-PID CPU# |||||| TIMESTAMP FUNCTION\n"
  23180. + "# | | | |||||| | |\n");
  23181. }
  23182. void
  23183. diff -Nur linux-4.1.20.orig/kernel/trace/trace.h linux-4.1.20/kernel/trace/trace.h
  23184. --- linux-4.1.20.orig/kernel/trace/trace.h 2016-03-17 19:11:03.000000000 +0100
  23185. +++ linux-4.1.20/kernel/trace/trace.h 2016-03-21 20:18:34.000000000 +0100
  23186. @@ -120,6 +120,7 @@
  23187. * NEED_RESCHED - reschedule is requested
  23188. * HARDIRQ - inside an interrupt handler
  23189. * SOFTIRQ - inside a softirq handler
  23190. + * NEED_RESCHED_LAZY - lazy reschedule is requested
  23191. */
  23192. enum trace_flag_type {
  23193. TRACE_FLAG_IRQS_OFF = 0x01,
  23194. @@ -128,6 +129,7 @@
  23195. TRACE_FLAG_HARDIRQ = 0x08,
  23196. TRACE_FLAG_SOFTIRQ = 0x10,
  23197. TRACE_FLAG_PREEMPT_RESCHED = 0x20,
  23198. + TRACE_FLAG_NEED_RESCHED_LAZY = 0x40,
  23199. };
  23200. #define TRACE_BUF_SIZE 1024
  23201. diff -Nur linux-4.1.20.orig/kernel/trace/trace_events.c linux-4.1.20/kernel/trace/trace_events.c
  23202. --- linux-4.1.20.orig/kernel/trace/trace_events.c 2016-03-17 19:11:03.000000000 +0100
  23203. +++ linux-4.1.20/kernel/trace/trace_events.c 2016-03-21 20:18:34.000000000 +0100
  23204. @@ -162,6 +162,8 @@
  23205. __common_field(unsigned char, flags);
  23206. __common_field(unsigned char, preempt_count);
  23207. __common_field(int, pid);
  23208. + __common_field(unsigned short, migrate_disable);
  23209. + __common_field(unsigned short, padding);
  23210. return ret;
  23211. }
  23212. diff -Nur linux-4.1.20.orig/kernel/trace/trace_irqsoff.c linux-4.1.20/kernel/trace/trace_irqsoff.c
  23213. --- linux-4.1.20.orig/kernel/trace/trace_irqsoff.c 2016-03-17 19:11:03.000000000 +0100
  23214. +++ linux-4.1.20/kernel/trace/trace_irqsoff.c 2016-03-21 20:18:34.000000000 +0100
  23215. @@ -13,6 +13,7 @@
  23216. #include <linux/uaccess.h>
  23217. #include <linux/module.h>
  23218. #include <linux/ftrace.h>
  23219. +#include <trace/events/hist.h>
  23220. #include "trace.h"
  23221. @@ -433,11 +434,13 @@
  23222. {
  23223. if (preempt_trace() || irq_trace())
  23224. start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  23225. + trace_preemptirqsoff_hist_rcuidle(TRACE_START, 1);
  23226. }
  23227. EXPORT_SYMBOL_GPL(start_critical_timings);
  23228. void stop_critical_timings(void)
  23229. {
  23230. + trace_preemptirqsoff_hist_rcuidle(TRACE_STOP, 0);
  23231. if (preempt_trace() || irq_trace())
  23232. stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  23233. }
  23234. @@ -447,6 +450,7 @@
  23235. #ifdef CONFIG_PROVE_LOCKING
  23236. void time_hardirqs_on(unsigned long a0, unsigned long a1)
  23237. {
  23238. + trace_preemptirqsoff_hist_rcuidle(IRQS_ON, 0);
  23239. if (!preempt_trace() && irq_trace())
  23240. stop_critical_timing(a0, a1);
  23241. }
  23242. @@ -455,6 +459,7 @@
  23243. {
  23244. if (!preempt_trace() && irq_trace())
  23245. start_critical_timing(a0, a1);
  23246. + trace_preemptirqsoff_hist_rcuidle(IRQS_OFF, 1);
  23247. }
  23248. #else /* !CONFIG_PROVE_LOCKING */
  23249. @@ -480,6 +485,7 @@
  23250. */
  23251. void trace_hardirqs_on(void)
  23252. {
  23253. + trace_preemptirqsoff_hist_rcuidle(IRQS_ON, 0);
  23254. if (!preempt_trace() && irq_trace())
  23255. stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  23256. }
  23257. @@ -489,11 +495,13 @@
  23258. {
  23259. if (!preempt_trace() && irq_trace())
  23260. start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  23261. + trace_preemptirqsoff_hist_rcuidle(IRQS_OFF, 1);
  23262. }
  23263. EXPORT_SYMBOL(trace_hardirqs_off);
  23264. __visible void trace_hardirqs_on_caller(unsigned long caller_addr)
  23265. {
  23266. + trace_preemptirqsoff_hist(IRQS_ON, 0);
  23267. if (!preempt_trace() && irq_trace())
  23268. stop_critical_timing(CALLER_ADDR0, caller_addr);
  23269. }
  23270. @@ -503,6 +511,7 @@
  23271. {
  23272. if (!preempt_trace() && irq_trace())
  23273. start_critical_timing(CALLER_ADDR0, caller_addr);
  23274. + trace_preemptirqsoff_hist(IRQS_OFF, 1);
  23275. }
  23276. EXPORT_SYMBOL(trace_hardirqs_off_caller);
  23277. @@ -512,12 +521,14 @@
  23278. #ifdef CONFIG_PREEMPT_TRACER
  23279. void trace_preempt_on(unsigned long a0, unsigned long a1)
  23280. {
  23281. + trace_preemptirqsoff_hist(PREEMPT_ON, 0);
  23282. if (preempt_trace() && !irq_trace())
  23283. stop_critical_timing(a0, a1);
  23284. }
  23285. void trace_preempt_off(unsigned long a0, unsigned long a1)
  23286. {
  23287. + trace_preemptirqsoff_hist(PREEMPT_ON, 1);
  23288. if (preempt_trace() && !irq_trace())
  23289. start_critical_timing(a0, a1);
  23290. }
  23291. diff -Nur linux-4.1.20.orig/kernel/trace/trace_output.c linux-4.1.20/kernel/trace/trace_output.c
  23292. --- linux-4.1.20.orig/kernel/trace/trace_output.c 2016-03-17 19:11:03.000000000 +0100
  23293. +++ linux-4.1.20/kernel/trace/trace_output.c 2016-03-21 20:18:34.000000000 +0100
  23294. @@ -430,6 +430,7 @@
  23295. {
  23296. char hardsoft_irq;
  23297. char need_resched;
  23298. + char need_resched_lazy;
  23299. char irqs_off;
  23300. int hardirq;
  23301. int softirq;
  23302. @@ -457,6 +458,8 @@
  23303. need_resched = '.';
  23304. break;
  23305. }
  23306. + need_resched_lazy =
  23307. + (entry->flags & TRACE_FLAG_NEED_RESCHED_LAZY) ? 'L' : '.';
  23308. hardsoft_irq =
  23309. (hardirq && softirq) ? 'H' :
  23310. @@ -464,14 +467,25 @@
  23311. softirq ? 's' :
  23312. '.';
  23313. - trace_seq_printf(s, "%c%c%c",
  23314. - irqs_off, need_resched, hardsoft_irq);
  23315. + trace_seq_printf(s, "%c%c%c%c",
  23316. + irqs_off, need_resched, need_resched_lazy,
  23317. + hardsoft_irq);
  23318. if (entry->preempt_count)
  23319. trace_seq_printf(s, "%x", entry->preempt_count);
  23320. else
  23321. trace_seq_putc(s, '.');
  23322. + if (entry->preempt_lazy_count)
  23323. + trace_seq_printf(s, "%x", entry->preempt_lazy_count);
  23324. + else
  23325. + trace_seq_putc(s, '.');
  23326. +
  23327. + if (entry->migrate_disable)
  23328. + trace_seq_printf(s, "%x", entry->migrate_disable);
  23329. + else
  23330. + trace_seq_putc(s, '.');
  23331. +
  23332. return !trace_seq_has_overflowed(s);
  23333. }
  23334. diff -Nur linux-4.1.20.orig/kernel/trace/trace_sched_switch.c linux-4.1.20/kernel/trace/trace_sched_switch.c
  23335. --- linux-4.1.20.orig/kernel/trace/trace_sched_switch.c 2016-03-17 19:11:03.000000000 +0100
  23336. +++ linux-4.1.20/kernel/trace/trace_sched_switch.c 2016-03-21 20:18:34.000000000 +0100
  23337. @@ -26,7 +26,7 @@
  23338. }
  23339. static void
  23340. -probe_sched_wakeup(void *ignore, struct task_struct *wakee, int success)
  23341. +probe_sched_wakeup(void *ignore, struct task_struct *wakee)
  23342. {
  23343. if (unlikely(!sched_ref))
  23344. return;
  23345. diff -Nur linux-4.1.20.orig/kernel/trace/trace_sched_wakeup.c linux-4.1.20/kernel/trace/trace_sched_wakeup.c
  23346. --- linux-4.1.20.orig/kernel/trace/trace_sched_wakeup.c 2016-03-17 19:11:03.000000000 +0100
  23347. +++ linux-4.1.20/kernel/trace/trace_sched_wakeup.c 2016-03-21 20:18:34.000000000 +0100
  23348. @@ -514,7 +514,7 @@
  23349. }
  23350. static void
  23351. -probe_wakeup(void *ignore, struct task_struct *p, int success)
  23352. +probe_wakeup(void *ignore, struct task_struct *p)
  23353. {
  23354. struct trace_array_cpu *data;
  23355. int cpu = smp_processor_id();
  23356. diff -Nur linux-4.1.20.orig/kernel/user.c linux-4.1.20/kernel/user.c
  23357. --- linux-4.1.20.orig/kernel/user.c 2016-03-17 19:11:03.000000000 +0100
  23358. +++ linux-4.1.20/kernel/user.c 2016-03-21 20:18:34.000000000 +0100
  23359. @@ -161,11 +161,11 @@
  23360. if (!up)
  23361. return;
  23362. - local_irq_save(flags);
  23363. + local_irq_save_nort(flags);
  23364. if (atomic_dec_and_lock(&up->__count, &uidhash_lock))
  23365. free_user(up, flags);
  23366. else
  23367. - local_irq_restore(flags);
  23368. + local_irq_restore_nort(flags);
  23369. }
  23370. struct user_struct *alloc_uid(kuid_t uid)
  23371. diff -Nur linux-4.1.20.orig/kernel/watchdog.c linux-4.1.20/kernel/watchdog.c
  23372. --- linux-4.1.20.orig/kernel/watchdog.c 2016-03-17 19:11:03.000000000 +0100
  23373. +++ linux-4.1.20/kernel/watchdog.c 2016-03-21 20:18:34.000000000 +0100
  23374. @@ -262,6 +262,8 @@
  23375. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  23376. +static DEFINE_RAW_SPINLOCK(watchdog_output_lock);
  23377. +
  23378. static struct perf_event_attr wd_hw_attr = {
  23379. .type = PERF_TYPE_HARDWARE,
  23380. .config = PERF_COUNT_HW_CPU_CYCLES,
  23381. @@ -295,13 +297,21 @@
  23382. /* only print hardlockups once */
  23383. if (__this_cpu_read(hard_watchdog_warn) == true)
  23384. return;
  23385. + /*
  23386. + * If early-printk is enabled then make sure we do not
  23387. + * lock up in printk() and kill console logging:
  23388. + */
  23389. + printk_kill();
  23390. - if (hardlockup_panic)
  23391. + if (hardlockup_panic) {
  23392. panic("Watchdog detected hard LOCKUP on cpu %d",
  23393. this_cpu);
  23394. - else
  23395. + } else {
  23396. + raw_spin_lock(&watchdog_output_lock);
  23397. WARN(1, "Watchdog detected hard LOCKUP on cpu %d",
  23398. this_cpu);
  23399. + raw_spin_unlock(&watchdog_output_lock);
  23400. + }
  23401. __this_cpu_write(hard_watchdog_warn, true);
  23402. return;
  23403. @@ -444,6 +454,7 @@
  23404. /* kick off the timer for the hardlockup detector */
  23405. hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  23406. hrtimer->function = watchdog_timer_fn;
  23407. + hrtimer->irqsafe = 1;
  23408. /* Enable the perf event */
  23409. watchdog_nmi_enable(cpu);
  23410. diff -Nur linux-4.1.20.orig/kernel/workqueue.c linux-4.1.20/kernel/workqueue.c
  23411. --- linux-4.1.20.orig/kernel/workqueue.c 2016-03-17 19:11:03.000000000 +0100
  23412. +++ linux-4.1.20/kernel/workqueue.c 2016-03-21 20:18:34.000000000 +0100
  23413. @@ -48,6 +48,8 @@
  23414. #include <linux/nodemask.h>
  23415. #include <linux/moduleparam.h>
  23416. #include <linux/uaccess.h>
  23417. +#include <linux/locallock.h>
  23418. +#include <linux/delay.h>
  23419. #include "workqueue_internal.h"
  23420. @@ -121,11 +123,16 @@
  23421. * cpu or grabbing pool->lock is enough for read access. If
  23422. * POOL_DISASSOCIATED is set, it's identical to L.
  23423. *
  23424. + * On RT we need the extra protection via rt_lock_idle_list() for
  23425. + * the list manipulations against read access from
  23426. + * wq_worker_sleeping(). All other places are nicely serialized via
  23427. + * pool->lock.
  23428. + *
  23429. * A: pool->attach_mutex protected.
  23430. *
  23431. * PL: wq_pool_mutex protected.
  23432. *
  23433. - * PR: wq_pool_mutex protected for writes. Sched-RCU protected for reads.
  23434. + * PR: wq_pool_mutex protected for writes. RCU protected for reads.
  23435. *
  23436. * PW: wq_pool_mutex and wq->mutex protected for writes. Either for reads.
  23437. *
  23438. @@ -134,7 +141,7 @@
  23439. *
  23440. * WQ: wq->mutex protected.
  23441. *
  23442. - * WR: wq->mutex protected for writes. Sched-RCU protected for reads.
  23443. + * WR: wq->mutex protected for writes. RCU protected for reads.
  23444. *
  23445. * MD: wq_mayday_lock protected.
  23446. */
  23447. @@ -183,7 +190,7 @@
  23448. atomic_t nr_running ____cacheline_aligned_in_smp;
  23449. /*
  23450. - * Destruction of pool is sched-RCU protected to allow dereferences
  23451. + * Destruction of pool is RCU protected to allow dereferences
  23452. * from get_work_pool().
  23453. */
  23454. struct rcu_head rcu;
  23455. @@ -212,7 +219,7 @@
  23456. /*
  23457. * Release of unbound pwq is punted to system_wq. See put_pwq()
  23458. * and pwq_unbound_release_workfn() for details. pool_workqueue
  23459. - * itself is also sched-RCU protected so that the first pwq can be
  23460. + * itself is also RCU protected so that the first pwq can be
  23461. * determined without grabbing wq->mutex.
  23462. */
  23463. struct work_struct unbound_release_work;
  23464. @@ -334,6 +341,8 @@
  23465. struct workqueue_struct *system_freezable_power_efficient_wq __read_mostly;
  23466. EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq);
  23467. +static DEFINE_LOCAL_IRQ_LOCK(pendingb_lock);
  23468. +
  23469. static int worker_thread(void *__worker);
  23470. static void copy_workqueue_attrs(struct workqueue_attrs *to,
  23471. const struct workqueue_attrs *from);
  23472. @@ -343,14 +352,14 @@
  23473. #include <trace/events/workqueue.h>
  23474. #define assert_rcu_or_pool_mutex() \
  23475. - rcu_lockdep_assert(rcu_read_lock_sched_held() || \
  23476. + rcu_lockdep_assert(rcu_read_lock_held() || \
  23477. lockdep_is_held(&wq_pool_mutex), \
  23478. - "sched RCU or wq_pool_mutex should be held")
  23479. + "RCU or wq_pool_mutex should be held")
  23480. #define assert_rcu_or_wq_mutex(wq) \
  23481. - rcu_lockdep_assert(rcu_read_lock_sched_held() || \
  23482. + rcu_lockdep_assert(rcu_read_lock_held() || \
  23483. lockdep_is_held(&wq->mutex), \
  23484. - "sched RCU or wq->mutex should be held")
  23485. + "RCU or wq->mutex should be held")
  23486. #define assert_rcu_or_wq_mutex_or_pool_mutex(wq) \
  23487. rcu_lockdep_assert(rcu_read_lock_sched_held() || \
  23488. @@ -368,7 +377,7 @@
  23489. * @pool: iteration cursor
  23490. * @pi: integer used for iteration
  23491. *
  23492. - * This must be called either with wq_pool_mutex held or sched RCU read
  23493. + * This must be called either with wq_pool_mutex held or RCU read
  23494. * locked. If the pool needs to be used beyond the locking in effect, the
  23495. * caller is responsible for guaranteeing that the pool stays online.
  23496. *
  23497. @@ -400,7 +409,7 @@
  23498. * @pwq: iteration cursor
  23499. * @wq: the target workqueue
  23500. *
  23501. - * This must be called either with wq->mutex held or sched RCU read locked.
  23502. + * This must be called either with wq->mutex held or RCU read locked.
  23503. * If the pwq needs to be used beyond the locking in effect, the caller is
  23504. * responsible for guaranteeing that the pwq stays online.
  23505. *
  23506. @@ -412,6 +421,31 @@
  23507. if (({ assert_rcu_or_wq_mutex(wq); false; })) { } \
  23508. else
  23509. +#ifdef CONFIG_PREEMPT_RT_BASE
  23510. +static inline void rt_lock_idle_list(struct worker_pool *pool)
  23511. +{
  23512. + preempt_disable();
  23513. +}
  23514. +static inline void rt_unlock_idle_list(struct worker_pool *pool)
  23515. +{
  23516. + preempt_enable();
  23517. +}
  23518. +static inline void sched_lock_idle_list(struct worker_pool *pool) { }
  23519. +static inline void sched_unlock_idle_list(struct worker_pool *pool) { }
  23520. +#else
  23521. +static inline void rt_lock_idle_list(struct worker_pool *pool) { }
  23522. +static inline void rt_unlock_idle_list(struct worker_pool *pool) { }
  23523. +static inline void sched_lock_idle_list(struct worker_pool *pool)
  23524. +{
  23525. + spin_lock_irq(&pool->lock);
  23526. +}
  23527. +static inline void sched_unlock_idle_list(struct worker_pool *pool)
  23528. +{
  23529. + spin_unlock_irq(&pool->lock);
  23530. +}
  23531. +#endif
  23532. +
  23533. +
  23534. #ifdef CONFIG_DEBUG_OBJECTS_WORK
  23535. static struct debug_obj_descr work_debug_descr;
  23536. @@ -562,8 +596,7 @@
  23537. * @wq: the target workqueue
  23538. * @node: the node ID
  23539. *
  23540. - * This must be called with any of wq_pool_mutex, wq->mutex or sched RCU
  23541. - * read locked.
  23542. + * This must be called with any of wq_pool_mutex, wq->mutex or RCU read locked.
  23543. * If the pwq needs to be used beyond the locking in effect, the caller is
  23544. * responsible for guaranteeing that the pwq stays online.
  23545. *
  23546. @@ -677,8 +710,8 @@
  23547. * @work: the work item of interest
  23548. *
  23549. * Pools are created and destroyed under wq_pool_mutex, and allows read
  23550. - * access under sched-RCU read lock. As such, this function should be
  23551. - * called under wq_pool_mutex or with preemption disabled.
  23552. + * access under RCU read lock. As such, this function should be
  23553. + * called under wq_pool_mutex or inside of a rcu_read_lock() region.
  23554. *
  23555. * All fields of the returned pool are accessible as long as the above
  23556. * mentioned locking is in effect. If the returned pool needs to be used
  23557. @@ -815,51 +848,44 @@
  23558. */
  23559. static void wake_up_worker(struct worker_pool *pool)
  23560. {
  23561. - struct worker *worker = first_idle_worker(pool);
  23562. + struct worker *worker;
  23563. +
  23564. + rt_lock_idle_list(pool);
  23565. +
  23566. + worker = first_idle_worker(pool);
  23567. if (likely(worker))
  23568. wake_up_process(worker->task);
  23569. +
  23570. + rt_unlock_idle_list(pool);
  23571. }
  23572. /**
  23573. - * wq_worker_waking_up - a worker is waking up
  23574. - * @task: task waking up
  23575. - * @cpu: CPU @task is waking up to
  23576. - *
  23577. - * This function is called during try_to_wake_up() when a worker is
  23578. - * being awoken.
  23579. + * wq_worker_running - a worker is running again
  23580. + * @task: task returning from sleep
  23581. *
  23582. - * CONTEXT:
  23583. - * spin_lock_irq(rq->lock)
  23584. + * This function is called when a worker returns from schedule()
  23585. */
  23586. -void wq_worker_waking_up(struct task_struct *task, int cpu)
  23587. +void wq_worker_running(struct task_struct *task)
  23588. {
  23589. struct worker *worker = kthread_data(task);
  23590. - if (!(worker->flags & WORKER_NOT_RUNNING)) {
  23591. - WARN_ON_ONCE(worker->pool->cpu != cpu);
  23592. + if (!worker->sleeping)
  23593. + return;
  23594. + if (!(worker->flags & WORKER_NOT_RUNNING))
  23595. atomic_inc(&worker->pool->nr_running);
  23596. - }
  23597. + worker->sleeping = 0;
  23598. }
  23599. /**
  23600. * wq_worker_sleeping - a worker is going to sleep
  23601. * @task: task going to sleep
  23602. - * @cpu: CPU in question, must be the current CPU number
  23603. - *
  23604. - * This function is called during schedule() when a busy worker is
  23605. - * going to sleep. Worker on the same cpu can be woken up by
  23606. - * returning pointer to its task.
  23607. - *
  23608. - * CONTEXT:
  23609. - * spin_lock_irq(rq->lock)
  23610. - *
  23611. - * Return:
  23612. - * Worker task on @cpu to wake up, %NULL if none.
  23613. + * This function is called from schedule() when a busy worker is
  23614. + * going to sleep.
  23615. */
  23616. -struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
  23617. +void wq_worker_sleeping(struct task_struct *task)
  23618. {
  23619. - struct worker *worker = kthread_data(task), *to_wakeup = NULL;
  23620. + struct worker *worker = kthread_data(task);
  23621. struct worker_pool *pool;
  23622. /*
  23623. @@ -868,29 +894,26 @@
  23624. * checking NOT_RUNNING.
  23625. */
  23626. if (worker->flags & WORKER_NOT_RUNNING)
  23627. - return NULL;
  23628. + return;
  23629. pool = worker->pool;
  23630. - /* this can only happen on the local cpu */
  23631. - if (WARN_ON_ONCE(cpu != raw_smp_processor_id() || pool->cpu != cpu))
  23632. - return NULL;
  23633. + if (WARN_ON_ONCE(worker->sleeping))
  23634. + return;
  23635. +
  23636. + worker->sleeping = 1;
  23637. /*
  23638. * The counterpart of the following dec_and_test, implied mb,
  23639. * worklist not empty test sequence is in insert_work().
  23640. * Please read comment there.
  23641. - *
  23642. - * NOT_RUNNING is clear. This means that we're bound to and
  23643. - * running on the local cpu w/ rq lock held and preemption
  23644. - * disabled, which in turn means that none else could be
  23645. - * manipulating idle_list, so dereferencing idle_list without pool
  23646. - * lock is safe.
  23647. */
  23648. if (atomic_dec_and_test(&pool->nr_running) &&
  23649. - !list_empty(&pool->worklist))
  23650. - to_wakeup = first_idle_worker(pool);
  23651. - return to_wakeup ? to_wakeup->task : NULL;
  23652. + !list_empty(&pool->worklist)) {
  23653. + sched_lock_idle_list(pool);
  23654. + wake_up_worker(pool);
  23655. + sched_unlock_idle_list(pool);
  23656. + }
  23657. }
  23658. /**
  23659. @@ -1084,12 +1107,12 @@
  23660. {
  23661. if (pwq) {
  23662. /*
  23663. - * As both pwqs and pools are sched-RCU protected, the
  23664. + * As both pwqs and pools are RCU protected, the
  23665. * following lock operations are safe.
  23666. */
  23667. - spin_lock_irq(&pwq->pool->lock);
  23668. + local_spin_lock_irq(pendingb_lock, &pwq->pool->lock);
  23669. put_pwq(pwq);
  23670. - spin_unlock_irq(&pwq->pool->lock);
  23671. + local_spin_unlock_irq(pendingb_lock, &pwq->pool->lock);
  23672. }
  23673. }
  23674. @@ -1191,7 +1214,7 @@
  23675. struct worker_pool *pool;
  23676. struct pool_workqueue *pwq;
  23677. - local_irq_save(*flags);
  23678. + local_lock_irqsave(pendingb_lock, *flags);
  23679. /* try to steal the timer if it exists */
  23680. if (is_dwork) {
  23681. @@ -1210,6 +1233,7 @@
  23682. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
  23683. return 0;
  23684. + rcu_read_lock();
  23685. /*
  23686. * The queueing is in progress, or it is already queued. Try to
  23687. * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
  23688. @@ -1248,14 +1272,16 @@
  23689. set_work_pool_and_keep_pending(work, pool->id);
  23690. spin_unlock(&pool->lock);
  23691. + rcu_read_unlock();
  23692. return 1;
  23693. }
  23694. spin_unlock(&pool->lock);
  23695. fail:
  23696. - local_irq_restore(*flags);
  23697. + rcu_read_unlock();
  23698. + local_unlock_irqrestore(pendingb_lock, *flags);
  23699. if (work_is_canceling(work))
  23700. return -ENOENT;
  23701. - cpu_relax();
  23702. + cpu_chill();
  23703. return -EAGAIN;
  23704. }
  23705. @@ -1324,7 +1350,7 @@
  23706. * queued or lose PENDING. Grabbing PENDING and queueing should
  23707. * happen with IRQ disabled.
  23708. */
  23709. - WARN_ON_ONCE(!irqs_disabled());
  23710. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  23711. debug_work_activate(work);
  23712. @@ -1332,6 +1358,8 @@
  23713. if (unlikely(wq->flags & __WQ_DRAINING) &&
  23714. WARN_ON_ONCE(!is_chained_work(wq)))
  23715. return;
  23716. +
  23717. + rcu_read_lock();
  23718. retry:
  23719. if (req_cpu == WORK_CPU_UNBOUND)
  23720. cpu = raw_smp_processor_id();
  23721. @@ -1388,10 +1416,8 @@
  23722. /* pwq determined, queue */
  23723. trace_workqueue_queue_work(req_cpu, pwq, work);
  23724. - if (WARN_ON(!list_empty(&work->entry))) {
  23725. - spin_unlock(&pwq->pool->lock);
  23726. - return;
  23727. - }
  23728. + if (WARN_ON(!list_empty(&work->entry)))
  23729. + goto out;
  23730. pwq->nr_in_flight[pwq->work_color]++;
  23731. work_flags = work_color_to_flags(pwq->work_color);
  23732. @@ -1407,7 +1433,9 @@
  23733. insert_work(pwq, work, worklist, work_flags);
  23734. +out:
  23735. spin_unlock(&pwq->pool->lock);
  23736. + rcu_read_unlock();
  23737. }
  23738. /**
  23739. @@ -1427,14 +1455,14 @@
  23740. bool ret = false;
  23741. unsigned long flags;
  23742. - local_irq_save(flags);
  23743. + local_lock_irqsave(pendingb_lock,flags);
  23744. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
  23745. __queue_work(cpu, wq, work);
  23746. ret = true;
  23747. }
  23748. - local_irq_restore(flags);
  23749. + local_unlock_irqrestore(pendingb_lock, flags);
  23750. return ret;
  23751. }
  23752. EXPORT_SYMBOL(queue_work_on);
  23753. @@ -1501,14 +1529,14 @@
  23754. unsigned long flags;
  23755. /* read the comment in __queue_work() */
  23756. - local_irq_save(flags);
  23757. + local_lock_irqsave(pendingb_lock, flags);
  23758. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
  23759. __queue_delayed_work(cpu, wq, dwork, delay);
  23760. ret = true;
  23761. }
  23762. - local_irq_restore(flags);
  23763. + local_unlock_irqrestore(pendingb_lock, flags);
  23764. return ret;
  23765. }
  23766. EXPORT_SYMBOL(queue_delayed_work_on);
  23767. @@ -1543,7 +1571,7 @@
  23768. if (likely(ret >= 0)) {
  23769. __queue_delayed_work(cpu, wq, dwork, delay);
  23770. - local_irq_restore(flags);
  23771. + local_unlock_irqrestore(pendingb_lock, flags);
  23772. }
  23773. /* -ENOENT from try_to_grab_pending() becomes %true */
  23774. @@ -1576,7 +1604,9 @@
  23775. worker->last_active = jiffies;
  23776. /* idle_list is LIFO */
  23777. + rt_lock_idle_list(pool);
  23778. list_add(&worker->entry, &pool->idle_list);
  23779. + rt_unlock_idle_list(pool);
  23780. if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
  23781. mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
  23782. @@ -1609,7 +1639,9 @@
  23783. return;
  23784. worker_clr_flags(worker, WORKER_IDLE);
  23785. pool->nr_idle--;
  23786. + rt_lock_idle_list(pool);
  23787. list_del_init(&worker->entry);
  23788. + rt_unlock_idle_list(pool);
  23789. }
  23790. static struct worker *alloc_worker(int node)
  23791. @@ -1777,7 +1809,9 @@
  23792. pool->nr_workers--;
  23793. pool->nr_idle--;
  23794. + rt_lock_idle_list(pool);
  23795. list_del_init(&worker->entry);
  23796. + rt_unlock_idle_list(pool);
  23797. worker->flags |= WORKER_DIE;
  23798. wake_up_process(worker->task);
  23799. }
  23800. @@ -2694,14 +2728,14 @@
  23801. might_sleep();
  23802. - local_irq_disable();
  23803. + rcu_read_lock();
  23804. pool = get_work_pool(work);
  23805. if (!pool) {
  23806. - local_irq_enable();
  23807. + rcu_read_unlock();
  23808. return false;
  23809. }
  23810. - spin_lock(&pool->lock);
  23811. + spin_lock_irq(&pool->lock);
  23812. /* see the comment in try_to_grab_pending() with the same code */
  23813. pwq = get_work_pwq(work);
  23814. if (pwq) {
  23815. @@ -2728,10 +2762,11 @@
  23816. else
  23817. lock_map_acquire_read(&pwq->wq->lockdep_map);
  23818. lock_map_release(&pwq->wq->lockdep_map);
  23819. -
  23820. + rcu_read_unlock();
  23821. return true;
  23822. already_gone:
  23823. spin_unlock_irq(&pool->lock);
  23824. + rcu_read_unlock();
  23825. return false;
  23826. }
  23827. @@ -2818,7 +2853,7 @@
  23828. /* tell other tasks trying to grab @work to back off */
  23829. mark_work_canceling(work);
  23830. - local_irq_restore(flags);
  23831. + local_unlock_irqrestore(pendingb_lock, flags);
  23832. flush_work(work);
  23833. clear_work_data(work);
  23834. @@ -2873,10 +2908,10 @@
  23835. */
  23836. bool flush_delayed_work(struct delayed_work *dwork)
  23837. {
  23838. - local_irq_disable();
  23839. + local_lock_irq(pendingb_lock);
  23840. if (del_timer_sync(&dwork->timer))
  23841. __queue_work(dwork->cpu, dwork->wq, &dwork->work);
  23842. - local_irq_enable();
  23843. + local_unlock_irq(pendingb_lock);
  23844. return flush_work(&dwork->work);
  23845. }
  23846. EXPORT_SYMBOL(flush_delayed_work);
  23847. @@ -2911,7 +2946,7 @@
  23848. set_work_pool_and_clear_pending(&dwork->work,
  23849. get_work_pool_id(&dwork->work));
  23850. - local_irq_restore(flags);
  23851. + local_unlock_irqrestore(pendingb_lock, flags);
  23852. return ret;
  23853. }
  23854. EXPORT_SYMBOL(cancel_delayed_work);
  23855. @@ -3169,7 +3204,7 @@
  23856. * put_unbound_pool - put a worker_pool
  23857. * @pool: worker_pool to put
  23858. *
  23859. - * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU
  23860. + * Put @pool. If its refcnt reaches zero, it gets destroyed in RCU
  23861. * safe manner. get_unbound_pool() calls this function on its failure path
  23862. * and this function should be able to release pools which went through,
  23863. * successfully or not, init_worker_pool().
  23864. @@ -3223,8 +3258,8 @@
  23865. del_timer_sync(&pool->idle_timer);
  23866. del_timer_sync(&pool->mayday_timer);
  23867. - /* sched-RCU protected to allow dereferences from get_work_pool() */
  23868. - call_rcu_sched(&pool->rcu, rcu_free_pool);
  23869. + /* RCU protected to allow dereferences from get_work_pool() */
  23870. + call_rcu(&pool->rcu, rcu_free_pool);
  23871. }
  23872. /**
  23873. @@ -3329,14 +3364,14 @@
  23874. put_unbound_pool(pool);
  23875. mutex_unlock(&wq_pool_mutex);
  23876. - call_rcu_sched(&pwq->rcu, rcu_free_pwq);
  23877. + call_rcu(&pwq->rcu, rcu_free_pwq);
  23878. /*
  23879. * If we're the last pwq going away, @wq is already dead and no one
  23880. * is gonna access it anymore. Schedule RCU free.
  23881. */
  23882. if (is_last)
  23883. - call_rcu_sched(&wq->rcu, rcu_free_wq);
  23884. + call_rcu(&wq->rcu, rcu_free_wq);
  23885. }
  23886. /**
  23887. @@ -3974,7 +4009,7 @@
  23888. * The base ref is never dropped on per-cpu pwqs. Directly
  23889. * schedule RCU free.
  23890. */
  23891. - call_rcu_sched(&wq->rcu, rcu_free_wq);
  23892. + call_rcu(&wq->rcu, rcu_free_wq);
  23893. } else {
  23894. /*
  23895. * We're the sole accessor of @wq at this point. Directly
  23896. @@ -4067,7 +4102,8 @@
  23897. struct pool_workqueue *pwq;
  23898. bool ret;
  23899. - rcu_read_lock_sched();
  23900. + rcu_read_lock();
  23901. + preempt_disable();
  23902. if (cpu == WORK_CPU_UNBOUND)
  23903. cpu = smp_processor_id();
  23904. @@ -4078,7 +4114,8 @@
  23905. pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
  23906. ret = !list_empty(&pwq->delayed_works);
  23907. - rcu_read_unlock_sched();
  23908. + preempt_enable();
  23909. + rcu_read_unlock();
  23910. return ret;
  23911. }
  23912. @@ -4104,15 +4141,15 @@
  23913. if (work_pending(work))
  23914. ret |= WORK_BUSY_PENDING;
  23915. - local_irq_save(flags);
  23916. + rcu_read_lock();
  23917. pool = get_work_pool(work);
  23918. if (pool) {
  23919. - spin_lock(&pool->lock);
  23920. + spin_lock_irqsave(&pool->lock, flags);
  23921. if (find_worker_executing_work(pool, work))
  23922. ret |= WORK_BUSY_RUNNING;
  23923. - spin_unlock(&pool->lock);
  23924. + spin_unlock_irqrestore(&pool->lock, flags);
  23925. }
  23926. - local_irq_restore(flags);
  23927. + rcu_read_unlock();
  23928. return ret;
  23929. }
  23930. @@ -4301,7 +4338,7 @@
  23931. unsigned long flags;
  23932. int pi;
  23933. - rcu_read_lock_sched();
  23934. + rcu_read_lock();
  23935. pr_info("Showing busy workqueues and worker pools:\n");
  23936. @@ -4352,7 +4389,7 @@
  23937. spin_unlock_irqrestore(&pool->lock, flags);
  23938. }
  23939. - rcu_read_unlock_sched();
  23940. + rcu_read_unlock();
  23941. }
  23942. /*
  23943. @@ -4702,16 +4739,16 @@
  23944. * nr_active is monotonically decreasing. It's safe
  23945. * to peek without lock.
  23946. */
  23947. - rcu_read_lock_sched();
  23948. + rcu_read_lock();
  23949. for_each_pwq(pwq, wq) {
  23950. WARN_ON_ONCE(pwq->nr_active < 0);
  23951. if (pwq->nr_active) {
  23952. busy = true;
  23953. - rcu_read_unlock_sched();
  23954. + rcu_read_unlock();
  23955. goto out_unlock;
  23956. }
  23957. }
  23958. - rcu_read_unlock_sched();
  23959. + rcu_read_unlock();
  23960. }
  23961. out_unlock:
  23962. mutex_unlock(&wq_pool_mutex);
  23963. @@ -4825,7 +4862,8 @@
  23964. const char *delim = "";
  23965. int node, written = 0;
  23966. - rcu_read_lock_sched();
  23967. + get_online_cpus();
  23968. + rcu_read_lock();
  23969. for_each_node(node) {
  23970. written += scnprintf(buf + written, PAGE_SIZE - written,
  23971. "%s%d:%d", delim, node,
  23972. @@ -4833,7 +4871,8 @@
  23973. delim = " ";
  23974. }
  23975. written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
  23976. - rcu_read_unlock_sched();
  23977. + rcu_read_unlock();
  23978. + put_online_cpus();
  23979. return written;
  23980. }
  23981. diff -Nur linux-4.1.20.orig/kernel/workqueue_internal.h linux-4.1.20/kernel/workqueue_internal.h
  23982. --- linux-4.1.20.orig/kernel/workqueue_internal.h 2016-03-17 19:11:03.000000000 +0100
  23983. +++ linux-4.1.20/kernel/workqueue_internal.h 2016-03-21 20:18:34.000000000 +0100
  23984. @@ -43,6 +43,7 @@
  23985. unsigned long last_active; /* L: last active timestamp */
  23986. unsigned int flags; /* X: flags */
  23987. int id; /* I: worker id */
  23988. + int sleeping; /* None */
  23989. /*
  23990. * Opaque string set with work_set_desc(). Printed out with task
  23991. @@ -68,7 +69,7 @@
  23992. * Scheduler hooks for concurrency managed workqueue. Only to be used from
  23993. * sched/core.c and workqueue.c.
  23994. */
  23995. -void wq_worker_waking_up(struct task_struct *task, int cpu);
  23996. -struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu);
  23997. +void wq_worker_running(struct task_struct *task);
  23998. +void wq_worker_sleeping(struct task_struct *task);
  23999. #endif /* _KERNEL_WORKQUEUE_INTERNAL_H */
  24000. diff -Nur linux-4.1.20.orig/lib/Kconfig linux-4.1.20/lib/Kconfig
  24001. --- linux-4.1.20.orig/lib/Kconfig 2016-03-17 19:11:03.000000000 +0100
  24002. +++ linux-4.1.20/lib/Kconfig 2016-03-21 20:18:34.000000000 +0100
  24003. @@ -391,6 +391,7 @@
  24004. config CPUMASK_OFFSTACK
  24005. bool "Force CPU masks off stack" if DEBUG_PER_CPU_MAPS
  24006. + depends on !PREEMPT_RT_FULL
  24007. help
  24008. Use dynamic allocation for cpumask_var_t, instead of putting
  24009. them on the stack. This is a bit more expensive, but avoids
  24010. diff -Nur linux-4.1.20.orig/lib/debugobjects.c linux-4.1.20/lib/debugobjects.c
  24011. --- linux-4.1.20.orig/lib/debugobjects.c 2016-03-17 19:11:03.000000000 +0100
  24012. +++ linux-4.1.20/lib/debugobjects.c 2016-03-21 20:18:34.000000000 +0100
  24013. @@ -309,7 +309,10 @@
  24014. struct debug_obj *obj;
  24015. unsigned long flags;
  24016. - fill_pool();
  24017. +#ifdef CONFIG_PREEMPT_RT_FULL
  24018. + if (preempt_count() == 0 && !irqs_disabled())
  24019. +#endif
  24020. + fill_pool();
  24021. db = get_bucket((unsigned long) addr);
  24022. diff -Nur linux-4.1.20.orig/lib/dump_stack.c linux-4.1.20/lib/dump_stack.c
  24023. --- linux-4.1.20.orig/lib/dump_stack.c 2016-03-17 19:11:03.000000000 +0100
  24024. +++ linux-4.1.20/lib/dump_stack.c 2016-03-21 20:18:34.000000000 +0100
  24025. @@ -8,6 +8,7 @@
  24026. #include <linux/sched.h>
  24027. #include <linux/smp.h>
  24028. #include <linux/atomic.h>
  24029. +#include <linux/locallock.h>
  24030. static void __dump_stack(void)
  24031. {
  24032. diff -Nur linux-4.1.20.orig/lib/idr.c linux-4.1.20/lib/idr.c
  24033. --- linux-4.1.20.orig/lib/idr.c 2016-03-17 19:11:03.000000000 +0100
  24034. +++ linux-4.1.20/lib/idr.c 2016-03-21 20:18:34.000000000 +0100
  24035. @@ -30,6 +30,7 @@
  24036. #include <linux/idr.h>
  24037. #include <linux/spinlock.h>
  24038. #include <linux/percpu.h>
  24039. +#include <linux/locallock.h>
  24040. #define MAX_IDR_SHIFT (sizeof(int) * 8 - 1)
  24041. #define MAX_IDR_BIT (1U << MAX_IDR_SHIFT)
  24042. @@ -366,6 +367,35 @@
  24043. idr_mark_full(pa, id);
  24044. }
  24045. +#ifdef CONFIG_PREEMPT_RT_FULL
  24046. +static DEFINE_LOCAL_IRQ_LOCK(idr_lock);
  24047. +
  24048. +static inline void idr_preload_lock(void)
  24049. +{
  24050. + local_lock(idr_lock);
  24051. +}
  24052. +
  24053. +static inline void idr_preload_unlock(void)
  24054. +{
  24055. + local_unlock(idr_lock);
  24056. +}
  24057. +
  24058. +void idr_preload_end(void)
  24059. +{
  24060. + idr_preload_unlock();
  24061. +}
  24062. +EXPORT_SYMBOL(idr_preload_end);
  24063. +#else
  24064. +static inline void idr_preload_lock(void)
  24065. +{
  24066. + preempt_disable();
  24067. +}
  24068. +
  24069. +static inline void idr_preload_unlock(void)
  24070. +{
  24071. + preempt_enable();
  24072. +}
  24073. +#endif
  24074. /**
  24075. * idr_preload - preload for idr_alloc()
  24076. @@ -401,7 +431,7 @@
  24077. WARN_ON_ONCE(in_interrupt());
  24078. might_sleep_if(gfp_mask & __GFP_WAIT);
  24079. - preempt_disable();
  24080. + idr_preload_lock();
  24081. /*
  24082. * idr_alloc() is likely to succeed w/o full idr_layer buffer and
  24083. @@ -413,9 +443,9 @@
  24084. while (__this_cpu_read(idr_preload_cnt) < MAX_IDR_FREE) {
  24085. struct idr_layer *new;
  24086. - preempt_enable();
  24087. + idr_preload_unlock();
  24088. new = kmem_cache_zalloc(idr_layer_cache, gfp_mask);
  24089. - preempt_disable();
  24090. + idr_preload_lock();
  24091. if (!new)
  24092. break;
  24093. diff -Nur linux-4.1.20.orig/lib/locking-selftest.c linux-4.1.20/lib/locking-selftest.c
  24094. --- linux-4.1.20.orig/lib/locking-selftest.c 2016-03-17 19:11:03.000000000 +0100
  24095. +++ linux-4.1.20/lib/locking-selftest.c 2016-03-21 20:18:34.000000000 +0100
  24096. @@ -590,6 +590,8 @@
  24097. #include "locking-selftest-spin-hardirq.h"
  24098. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_spin)
  24099. +#ifndef CONFIG_PREEMPT_RT_FULL
  24100. +
  24101. #include "locking-selftest-rlock-hardirq.h"
  24102. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_rlock)
  24103. @@ -605,9 +607,12 @@
  24104. #include "locking-selftest-wlock-softirq.h"
  24105. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_wlock)
  24106. +#endif
  24107. +
  24108. #undef E1
  24109. #undef E2
  24110. +#ifndef CONFIG_PREEMPT_RT_FULL
  24111. /*
  24112. * Enabling hardirqs with a softirq-safe lock held:
  24113. */
  24114. @@ -640,6 +645,8 @@
  24115. #undef E1
  24116. #undef E2
  24117. +#endif
  24118. +
  24119. /*
  24120. * Enabling irqs with an irq-safe lock held:
  24121. */
  24122. @@ -663,6 +670,8 @@
  24123. #include "locking-selftest-spin-hardirq.h"
  24124. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_spin)
  24125. +#ifndef CONFIG_PREEMPT_RT_FULL
  24126. +
  24127. #include "locking-selftest-rlock-hardirq.h"
  24128. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_rlock)
  24129. @@ -678,6 +687,8 @@
  24130. #include "locking-selftest-wlock-softirq.h"
  24131. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock)
  24132. +#endif
  24133. +
  24134. #undef E1
  24135. #undef E2
  24136. @@ -709,6 +720,8 @@
  24137. #include "locking-selftest-spin-hardirq.h"
  24138. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_spin)
  24139. +#ifndef CONFIG_PREEMPT_RT_FULL
  24140. +
  24141. #include "locking-selftest-rlock-hardirq.h"
  24142. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_rlock)
  24143. @@ -724,6 +737,8 @@
  24144. #include "locking-selftest-wlock-softirq.h"
  24145. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock)
  24146. +#endif
  24147. +
  24148. #undef E1
  24149. #undef E2
  24150. #undef E3
  24151. @@ -757,6 +772,8 @@
  24152. #include "locking-selftest-spin-hardirq.h"
  24153. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_spin)
  24154. +#ifndef CONFIG_PREEMPT_RT_FULL
  24155. +
  24156. #include "locking-selftest-rlock-hardirq.h"
  24157. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_rlock)
  24158. @@ -772,10 +789,14 @@
  24159. #include "locking-selftest-wlock-softirq.h"
  24160. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_wlock)
  24161. +#endif
  24162. +
  24163. #undef E1
  24164. #undef E2
  24165. #undef E3
  24166. +#ifndef CONFIG_PREEMPT_RT_FULL
  24167. +
  24168. /*
  24169. * read-lock / write-lock irq inversion.
  24170. *
  24171. @@ -838,6 +859,10 @@
  24172. #undef E2
  24173. #undef E3
  24174. +#endif
  24175. +
  24176. +#ifndef CONFIG_PREEMPT_RT_FULL
  24177. +
  24178. /*
  24179. * read-lock / write-lock recursion that is actually safe.
  24180. */
  24181. @@ -876,6 +901,8 @@
  24182. #undef E2
  24183. #undef E3
  24184. +#endif
  24185. +
  24186. /*
  24187. * read-lock / write-lock recursion that is unsafe.
  24188. */
  24189. @@ -1858,6 +1885,7 @@
  24190. printk(" --------------------------------------------------------------------------\n");
  24191. +#ifndef CONFIG_PREEMPT_RT_FULL
  24192. /*
  24193. * irq-context testcases:
  24194. */
  24195. @@ -1870,6 +1898,28 @@
  24196. DO_TESTCASE_6x2("irq read-recursion", irq_read_recursion);
  24197. // DO_TESTCASE_6x2B("irq read-recursion #2", irq_read_recursion2);
  24198. +#else
  24199. + /* On -rt, we only do hardirq context test for raw spinlock */
  24200. + DO_TESTCASE_1B("hard-irqs-on + irq-safe-A", irqsafe1_hard_spin, 12);
  24201. + DO_TESTCASE_1B("hard-irqs-on + irq-safe-A", irqsafe1_hard_spin, 21);
  24202. +
  24203. + DO_TESTCASE_1B("hard-safe-A + irqs-on", irqsafe2B_hard_spin, 12);
  24204. + DO_TESTCASE_1B("hard-safe-A + irqs-on", irqsafe2B_hard_spin, 21);
  24205. +
  24206. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 123);
  24207. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 132);
  24208. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 213);
  24209. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 231);
  24210. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 312);
  24211. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 321);
  24212. +
  24213. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 123);
  24214. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 132);
  24215. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 213);
  24216. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 231);
  24217. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 312);
  24218. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 321);
  24219. +#endif
  24220. ww_tests();
  24221. diff -Nur linux-4.1.20.orig/lib/percpu_ida.c linux-4.1.20/lib/percpu_ida.c
  24222. --- linux-4.1.20.orig/lib/percpu_ida.c 2016-03-17 19:11:03.000000000 +0100
  24223. +++ linux-4.1.20/lib/percpu_ida.c 2016-03-21 20:18:34.000000000 +0100
  24224. @@ -26,6 +26,9 @@
  24225. #include <linux/string.h>
  24226. #include <linux/spinlock.h>
  24227. #include <linux/percpu_ida.h>
  24228. +#include <linux/locallock.h>
  24229. +
  24230. +static DEFINE_LOCAL_IRQ_LOCK(irq_off_lock);
  24231. struct percpu_ida_cpu {
  24232. /*
  24233. @@ -148,13 +151,13 @@
  24234. unsigned long flags;
  24235. int tag;
  24236. - local_irq_save(flags);
  24237. + local_lock_irqsave(irq_off_lock, flags);
  24238. tags = this_cpu_ptr(pool->tag_cpu);
  24239. /* Fastpath */
  24240. tag = alloc_local_tag(tags);
  24241. if (likely(tag >= 0)) {
  24242. - local_irq_restore(flags);
  24243. + local_unlock_irqrestore(irq_off_lock, flags);
  24244. return tag;
  24245. }
  24246. @@ -173,6 +176,7 @@
  24247. if (!tags->nr_free)
  24248. alloc_global_tags(pool, tags);
  24249. +
  24250. if (!tags->nr_free)
  24251. steal_tags(pool, tags);
  24252. @@ -184,7 +188,7 @@
  24253. }
  24254. spin_unlock(&pool->lock);
  24255. - local_irq_restore(flags);
  24256. + local_unlock_irqrestore(irq_off_lock, flags);
  24257. if (tag >= 0 || state == TASK_RUNNING)
  24258. break;
  24259. @@ -196,7 +200,7 @@
  24260. schedule();
  24261. - local_irq_save(flags);
  24262. + local_lock_irqsave(irq_off_lock, flags);
  24263. tags = this_cpu_ptr(pool->tag_cpu);
  24264. }
  24265. if (state != TASK_RUNNING)
  24266. @@ -221,7 +225,7 @@
  24267. BUG_ON(tag >= pool->nr_tags);
  24268. - local_irq_save(flags);
  24269. + local_lock_irqsave(irq_off_lock, flags);
  24270. tags = this_cpu_ptr(pool->tag_cpu);
  24271. spin_lock(&tags->lock);
  24272. @@ -253,7 +257,7 @@
  24273. spin_unlock(&pool->lock);
  24274. }
  24275. - local_irq_restore(flags);
  24276. + local_unlock_irqrestore(irq_off_lock, flags);
  24277. }
  24278. EXPORT_SYMBOL_GPL(percpu_ida_free);
  24279. @@ -345,7 +349,7 @@
  24280. struct percpu_ida_cpu *remote;
  24281. unsigned cpu, i, err = 0;
  24282. - local_irq_save(flags);
  24283. + local_lock_irqsave(irq_off_lock, flags);
  24284. for_each_possible_cpu(cpu) {
  24285. remote = per_cpu_ptr(pool->tag_cpu, cpu);
  24286. spin_lock(&remote->lock);
  24287. @@ -367,7 +371,7 @@
  24288. }
  24289. spin_unlock(&pool->lock);
  24290. out:
  24291. - local_irq_restore(flags);
  24292. + local_unlock_irqrestore(irq_off_lock, flags);
  24293. return err;
  24294. }
  24295. EXPORT_SYMBOL_GPL(percpu_ida_for_each_free);
  24296. diff -Nur linux-4.1.20.orig/lib/radix-tree.c linux-4.1.20/lib/radix-tree.c
  24297. --- linux-4.1.20.orig/lib/radix-tree.c 2016-03-17 19:11:03.000000000 +0100
  24298. +++ linux-4.1.20/lib/radix-tree.c 2016-03-21 20:18:34.000000000 +0100
  24299. @@ -195,12 +195,13 @@
  24300. * succeed in getting a node here (and never reach
  24301. * kmem_cache_alloc)
  24302. */
  24303. - rtp = this_cpu_ptr(&radix_tree_preloads);
  24304. + rtp = &get_cpu_var(radix_tree_preloads);
  24305. if (rtp->nr) {
  24306. ret = rtp->nodes[rtp->nr - 1];
  24307. rtp->nodes[rtp->nr - 1] = NULL;
  24308. rtp->nr--;
  24309. }
  24310. + put_cpu_var(radix_tree_preloads);
  24311. /*
  24312. * Update the allocation stack trace as this is more useful
  24313. * for debugging.
  24314. @@ -240,6 +241,7 @@
  24315. call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
  24316. }
  24317. +#ifndef CONFIG_PREEMPT_RT_FULL
  24318. /*
  24319. * Load up this CPU's radix_tree_node buffer with sufficient objects to
  24320. * ensure that the addition of a single element in the tree cannot fail. On
  24321. @@ -305,6 +307,7 @@
  24322. return 0;
  24323. }
  24324. EXPORT_SYMBOL(radix_tree_maybe_preload);
  24325. +#endif
  24326. /*
  24327. * Return the maximum key which can be store into a
  24328. diff -Nur linux-4.1.20.orig/lib/scatterlist.c linux-4.1.20/lib/scatterlist.c
  24329. --- linux-4.1.20.orig/lib/scatterlist.c 2016-03-17 19:11:03.000000000 +0100
  24330. +++ linux-4.1.20/lib/scatterlist.c 2016-03-21 20:18:34.000000000 +0100
  24331. @@ -592,7 +592,7 @@
  24332. flush_kernel_dcache_page(miter->page);
  24333. if (miter->__flags & SG_MITER_ATOMIC) {
  24334. - WARN_ON_ONCE(preemptible());
  24335. + WARN_ON_ONCE(!pagefault_disabled());
  24336. kunmap_atomic(miter->addr);
  24337. } else
  24338. kunmap(miter->page);
  24339. @@ -637,7 +637,7 @@
  24340. if (!sg_miter_skip(&miter, skip))
  24341. return false;
  24342. - local_irq_save(flags);
  24343. + local_irq_save_nort(flags);
  24344. while (sg_miter_next(&miter) && offset < buflen) {
  24345. unsigned int len;
  24346. @@ -654,7 +654,7 @@
  24347. sg_miter_stop(&miter);
  24348. - local_irq_restore(flags);
  24349. + local_irq_restore_nort(flags);
  24350. return offset;
  24351. }
  24352. diff -Nur linux-4.1.20.orig/lib/smp_processor_id.c linux-4.1.20/lib/smp_processor_id.c
  24353. --- linux-4.1.20.orig/lib/smp_processor_id.c 2016-03-17 19:11:03.000000000 +0100
  24354. +++ linux-4.1.20/lib/smp_processor_id.c 2016-03-21 20:18:34.000000000 +0100
  24355. @@ -39,8 +39,9 @@
  24356. if (!printk_ratelimit())
  24357. goto out_enable;
  24358. - printk(KERN_ERR "BUG: using %s%s() in preemptible [%08x] code: %s/%d\n",
  24359. - what1, what2, preempt_count() - 1, current->comm, current->pid);
  24360. + printk(KERN_ERR "BUG: using %s%s() in preemptible [%08x %08x] code: %s/%d\n",
  24361. + what1, what2, preempt_count() - 1, __migrate_disabled(current),
  24362. + current->comm, current->pid);
  24363. print_symbol("caller is %s\n", (long)__builtin_return_address(0));
  24364. dump_stack();
  24365. diff -Nur linux-4.1.20.orig/lib/strnlen_user.c linux-4.1.20/lib/strnlen_user.c
  24366. --- linux-4.1.20.orig/lib/strnlen_user.c 2016-03-17 19:11:03.000000000 +0100
  24367. +++ linux-4.1.20/lib/strnlen_user.c 2016-03-21 20:18:34.000000000 +0100
  24368. @@ -85,7 +85,8 @@
  24369. * @str: The string to measure.
  24370. * @count: Maximum count (including NUL character)
  24371. *
  24372. - * Context: User context only. This function may sleep.
  24373. + * Context: User context only. This function may sleep if pagefaults are
  24374. + * enabled.
  24375. *
  24376. * Get the size of a NUL-terminated string in user space.
  24377. *
  24378. @@ -121,7 +122,8 @@
  24379. * strlen_user: - Get the size of a user string INCLUDING final NUL.
  24380. * @str: The string to measure.
  24381. *
  24382. - * Context: User context only. This function may sleep.
  24383. + * Context: User context only. This function may sleep if pagefaults are
  24384. + * enabled.
  24385. *
  24386. * Get the size of a NUL-terminated string in user space.
  24387. *
  24388. diff -Nur linux-4.1.20.orig/localversion-rt linux-4.1.20/localversion-rt
  24389. --- linux-4.1.20.orig/localversion-rt 1970-01-01 01:00:00.000000000 +0100
  24390. +++ linux-4.1.20/localversion-rt 2016-03-21 20:18:34.000000000 +0100
  24391. @@ -0,0 +1 @@
  24392. +-rt22
  24393. diff -Nur linux-4.1.20.orig/mm/Kconfig linux-4.1.20/mm/Kconfig
  24394. --- linux-4.1.20.orig/mm/Kconfig 2016-03-17 19:11:03.000000000 +0100
  24395. +++ linux-4.1.20/mm/Kconfig 2016-03-21 20:18:34.000000000 +0100
  24396. @@ -409,7 +409,7 @@
  24397. config TRANSPARENT_HUGEPAGE
  24398. bool "Transparent Hugepage Support"
  24399. - depends on HAVE_ARCH_TRANSPARENT_HUGEPAGE
  24400. + depends on HAVE_ARCH_TRANSPARENT_HUGEPAGE && !PREEMPT_RT_FULL
  24401. select COMPACTION
  24402. help
  24403. Transparent Hugepages allows the kernel to use huge pages and
  24404. diff -Nur linux-4.1.20.orig/mm/compaction.c linux-4.1.20/mm/compaction.c
  24405. --- linux-4.1.20.orig/mm/compaction.c 2016-03-17 19:11:03.000000000 +0100
  24406. +++ linux-4.1.20/mm/compaction.c 2016-03-21 20:18:34.000000000 +0100
  24407. @@ -1406,10 +1406,12 @@
  24408. cc->migrate_pfn & ~((1UL << cc->order) - 1);
  24409. if (last_migrated_pfn < current_block_start) {
  24410. - cpu = get_cpu();
  24411. + cpu = get_cpu_light();
  24412. + local_lock_irq(swapvec_lock);
  24413. lru_add_drain_cpu(cpu);
  24414. + local_unlock_irq(swapvec_lock);
  24415. drain_local_pages(zone);
  24416. - put_cpu();
  24417. + put_cpu_light();
  24418. /* No more flushing until we migrate again */
  24419. last_migrated_pfn = 0;
  24420. }
  24421. diff -Nur linux-4.1.20.orig/mm/filemap.c linux-4.1.20/mm/filemap.c
  24422. --- linux-4.1.20.orig/mm/filemap.c 2016-03-17 19:11:03.000000000 +0100
  24423. +++ linux-4.1.20/mm/filemap.c 2016-03-21 20:18:34.000000000 +0100
  24424. @@ -167,7 +167,9 @@
  24425. if (!workingset_node_pages(node) &&
  24426. list_empty(&node->private_list)) {
  24427. node->private_data = mapping;
  24428. - list_lru_add(&workingset_shadow_nodes, &node->private_list);
  24429. + local_lock(workingset_shadow_lock);
  24430. + list_lru_add(&__workingset_shadow_nodes, &node->private_list);
  24431. + local_unlock(workingset_shadow_lock);
  24432. }
  24433. }
  24434. @@ -533,9 +535,12 @@
  24435. * node->private_list is protected by
  24436. * mapping->tree_lock.
  24437. */
  24438. - if (!list_empty(&node->private_list))
  24439. - list_lru_del(&workingset_shadow_nodes,
  24440. + if (!list_empty(&node->private_list)) {
  24441. + local_lock(workingset_shadow_lock);
  24442. + list_lru_del(&__workingset_shadow_nodes,
  24443. &node->private_list);
  24444. + local_unlock(workingset_shadow_lock);
  24445. + }
  24446. }
  24447. return 0;
  24448. }
  24449. diff -Nur linux-4.1.20.orig/mm/highmem.c linux-4.1.20/mm/highmem.c
  24450. --- linux-4.1.20.orig/mm/highmem.c 2016-03-17 19:11:03.000000000 +0100
  24451. +++ linux-4.1.20/mm/highmem.c 2016-03-21 20:18:34.000000000 +0100
  24452. @@ -29,10 +29,11 @@
  24453. #include <linux/kgdb.h>
  24454. #include <asm/tlbflush.h>
  24455. -
  24456. +#ifndef CONFIG_PREEMPT_RT_FULL
  24457. #if defined(CONFIG_HIGHMEM) || defined(CONFIG_X86_32)
  24458. DEFINE_PER_CPU(int, __kmap_atomic_idx);
  24459. #endif
  24460. +#endif
  24461. /*
  24462. * Virtual_count is not a pure "count".
  24463. @@ -107,8 +108,9 @@
  24464. unsigned long totalhigh_pages __read_mostly;
  24465. EXPORT_SYMBOL(totalhigh_pages);
  24466. -
  24467. +#ifndef CONFIG_PREEMPT_RT_FULL
  24468. EXPORT_PER_CPU_SYMBOL(__kmap_atomic_idx);
  24469. +#endif
  24470. unsigned int nr_free_highpages (void)
  24471. {
  24472. diff -Nur linux-4.1.20.orig/mm/memcontrol.c linux-4.1.20/mm/memcontrol.c
  24473. --- linux-4.1.20.orig/mm/memcontrol.c 2016-03-17 19:11:03.000000000 +0100
  24474. +++ linux-4.1.20/mm/memcontrol.c 2016-03-21 20:18:34.000000000 +0100
  24475. @@ -66,6 +66,8 @@
  24476. #include <net/sock.h>
  24477. #include <net/ip.h>
  24478. #include <net/tcp_memcontrol.h>
  24479. +#include <linux/locallock.h>
  24480. +
  24481. #include "slab.h"
  24482. #include <asm/uaccess.h>
  24483. @@ -85,6 +87,7 @@
  24484. #define do_swap_account 0
  24485. #endif
  24486. +static DEFINE_LOCAL_IRQ_LOCK(event_lock);
  24487. static const char * const mem_cgroup_stat_names[] = {
  24488. "cache",
  24489. "rss",
  24490. @@ -2124,14 +2127,17 @@
  24491. */
  24492. static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
  24493. {
  24494. - struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
  24495. + struct memcg_stock_pcp *stock;
  24496. + int cpu = get_cpu_light();
  24497. +
  24498. + stock = &per_cpu(memcg_stock, cpu);
  24499. if (stock->cached != memcg) { /* reset if necessary */
  24500. drain_stock(stock);
  24501. stock->cached = memcg;
  24502. }
  24503. stock->nr_pages += nr_pages;
  24504. - put_cpu_var(memcg_stock);
  24505. + put_cpu_light();
  24506. }
  24507. /*
  24508. @@ -2147,7 +2153,7 @@
  24509. return;
  24510. /* Notify other cpus that system-wide "drain" is running */
  24511. get_online_cpus();
  24512. - curcpu = get_cpu();
  24513. + curcpu = get_cpu_light();
  24514. for_each_online_cpu(cpu) {
  24515. struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
  24516. struct mem_cgroup *memcg;
  24517. @@ -2164,7 +2170,7 @@
  24518. schedule_work_on(cpu, &stock->work);
  24519. }
  24520. }
  24521. - put_cpu();
  24522. + put_cpu_light();
  24523. put_online_cpus();
  24524. mutex_unlock(&percpu_charge_mutex);
  24525. }
  24526. @@ -4803,12 +4809,12 @@
  24527. ret = 0;
  24528. - local_irq_disable();
  24529. + local_lock_irq(event_lock);
  24530. mem_cgroup_charge_statistics(to, page, nr_pages);
  24531. memcg_check_events(to, page);
  24532. mem_cgroup_charge_statistics(from, page, -nr_pages);
  24533. memcg_check_events(from, page);
  24534. - local_irq_enable();
  24535. + local_unlock_irq(event_lock);
  24536. out_unlock:
  24537. unlock_page(page);
  24538. out:
  24539. @@ -5545,10 +5551,10 @@
  24540. VM_BUG_ON_PAGE(!PageTransHuge(page), page);
  24541. }
  24542. - local_irq_disable();
  24543. + local_lock_irq(event_lock);
  24544. mem_cgroup_charge_statistics(memcg, page, nr_pages);
  24545. memcg_check_events(memcg, page);
  24546. - local_irq_enable();
  24547. + local_unlock_irq(event_lock);
  24548. if (do_swap_account && PageSwapCache(page)) {
  24549. swp_entry_t entry = { .val = page_private(page) };
  24550. @@ -5604,14 +5610,14 @@
  24551. memcg_oom_recover(memcg);
  24552. }
  24553. - local_irq_save(flags);
  24554. + local_lock_irqsave(event_lock, flags);
  24555. __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS], nr_anon);
  24556. __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_CACHE], nr_file);
  24557. __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE], nr_huge);
  24558. __this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT], pgpgout);
  24559. __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
  24560. memcg_check_events(memcg, dummy_page);
  24561. - local_irq_restore(flags);
  24562. + local_unlock_irqrestore(event_lock, flags);
  24563. if (!mem_cgroup_is_root(memcg))
  24564. css_put_many(&memcg->css, nr_pages);
  24565. @@ -5815,6 +5821,7 @@
  24566. {
  24567. struct mem_cgroup *memcg;
  24568. unsigned short oldid;
  24569. + unsigned long flags;
  24570. VM_BUG_ON_PAGE(PageLRU(page), page);
  24571. VM_BUG_ON_PAGE(page_count(page), page);
  24572. @@ -5837,9 +5844,11 @@
  24573. if (!mem_cgroup_is_root(memcg))
  24574. page_counter_uncharge(&memcg->memory, 1);
  24575. + local_lock_irqsave(event_lock, flags);
  24576. /* Caller disabled preemption with mapping->tree_lock */
  24577. mem_cgroup_charge_statistics(memcg, page, -1);
  24578. memcg_check_events(memcg, page);
  24579. + local_unlock_irqrestore(event_lock, flags);
  24580. }
  24581. /**
  24582. diff -Nur linux-4.1.20.orig/mm/memory.c linux-4.1.20/mm/memory.c
  24583. --- linux-4.1.20.orig/mm/memory.c 2016-03-17 19:11:03.000000000 +0100
  24584. +++ linux-4.1.20/mm/memory.c 2016-03-21 20:18:34.000000000 +0100
  24585. @@ -3753,7 +3753,7 @@
  24586. }
  24587. #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)
  24588. -void might_fault(void)
  24589. +void __might_fault(const char *file, int line)
  24590. {
  24591. /*
  24592. * Some code (nfs/sunrpc) uses socket ops on kernel memory while
  24593. @@ -3763,21 +3763,15 @@
  24594. */
  24595. if (segment_eq(get_fs(), KERNEL_DS))
  24596. return;
  24597. -
  24598. - /*
  24599. - * it would be nicer only to annotate paths which are not under
  24600. - * pagefault_disable, however that requires a larger audit and
  24601. - * providing helpers like get_user_atomic.
  24602. - */
  24603. - if (in_atomic())
  24604. + if (pagefault_disabled())
  24605. return;
  24606. -
  24607. - __might_sleep(__FILE__, __LINE__, 0);
  24608. -
  24609. + __might_sleep(file, line, 0);
  24610. +#if defined(CONFIG_DEBUG_ATOMIC_SLEEP)
  24611. if (current->mm)
  24612. might_lock_read(&current->mm->mmap_sem);
  24613. +#endif
  24614. }
  24615. -EXPORT_SYMBOL(might_fault);
  24616. +EXPORT_SYMBOL(__might_fault);
  24617. #endif
  24618. #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
  24619. diff -Nur linux-4.1.20.orig/mm/mmu_context.c linux-4.1.20/mm/mmu_context.c
  24620. --- linux-4.1.20.orig/mm/mmu_context.c 2016-03-17 19:11:03.000000000 +0100
  24621. +++ linux-4.1.20/mm/mmu_context.c 2016-03-21 20:18:34.000000000 +0100
  24622. @@ -23,6 +23,7 @@
  24623. struct task_struct *tsk = current;
  24624. task_lock(tsk);
  24625. + preempt_disable_rt();
  24626. active_mm = tsk->active_mm;
  24627. if (active_mm != mm) {
  24628. atomic_inc(&mm->mm_count);
  24629. @@ -30,6 +31,7 @@
  24630. }
  24631. tsk->mm = mm;
  24632. switch_mm(active_mm, mm, tsk);
  24633. + preempt_enable_rt();
  24634. task_unlock(tsk);
  24635. #ifdef finish_arch_post_lock_switch
  24636. finish_arch_post_lock_switch();
  24637. diff -Nur linux-4.1.20.orig/mm/page_alloc.c linux-4.1.20/mm/page_alloc.c
  24638. --- linux-4.1.20.orig/mm/page_alloc.c 2016-03-17 19:11:03.000000000 +0100
  24639. +++ linux-4.1.20/mm/page_alloc.c 2016-03-21 20:18:34.000000000 +0100
  24640. @@ -60,6 +60,7 @@
  24641. #include <linux/page_ext.h>
  24642. #include <linux/hugetlb.h>
  24643. #include <linux/sched/rt.h>
  24644. +#include <linux/locallock.h>
  24645. #include <linux/page_owner.h>
  24646. #include <asm/sections.h>
  24647. @@ -233,6 +234,18 @@
  24648. EXPORT_SYMBOL(nr_online_nodes);
  24649. #endif
  24650. +static DEFINE_LOCAL_IRQ_LOCK(pa_lock);
  24651. +
  24652. +#ifdef CONFIG_PREEMPT_RT_BASE
  24653. +# define cpu_lock_irqsave(cpu, flags) \
  24654. + local_lock_irqsave_on(pa_lock, flags, cpu)
  24655. +# define cpu_unlock_irqrestore(cpu, flags) \
  24656. + local_unlock_irqrestore_on(pa_lock, flags, cpu)
  24657. +#else
  24658. +# define cpu_lock_irqsave(cpu, flags) local_irq_save(flags)
  24659. +# define cpu_unlock_irqrestore(cpu, flags) local_irq_restore(flags)
  24660. +#endif
  24661. +
  24662. int page_group_by_mobility_disabled __read_mostly;
  24663. void set_pageblock_migratetype(struct page *page, int migratetype)
  24664. @@ -681,7 +694,7 @@
  24665. }
  24666. /*
  24667. - * Frees a number of pages from the PCP lists
  24668. + * Frees a number of pages which have been collected from the pcp lists.
  24669. * Assumes all pages on list are in same zone, and of same order.
  24670. * count is the number of pages to free.
  24671. *
  24672. @@ -692,18 +705,51 @@
  24673. * pinned" detection logic.
  24674. */
  24675. static void free_pcppages_bulk(struct zone *zone, int count,
  24676. - struct per_cpu_pages *pcp)
  24677. + struct list_head *list)
  24678. {
  24679. - int migratetype = 0;
  24680. - int batch_free = 0;
  24681. int to_free = count;
  24682. unsigned long nr_scanned;
  24683. + unsigned long flags;
  24684. +
  24685. + spin_lock_irqsave(&zone->lock, flags);
  24686. - spin_lock(&zone->lock);
  24687. nr_scanned = zone_page_state(zone, NR_PAGES_SCANNED);
  24688. if (nr_scanned)
  24689. __mod_zone_page_state(zone, NR_PAGES_SCANNED, -nr_scanned);
  24690. + while (!list_empty(list)) {
  24691. + struct page *page = list_first_entry(list, struct page, lru);
  24692. + int mt; /* migratetype of the to-be-freed page */
  24693. +
  24694. + /* must delete as __free_one_page list manipulates */
  24695. + list_del(&page->lru);
  24696. +
  24697. + mt = get_freepage_migratetype(page);
  24698. + if (unlikely(has_isolate_pageblock(zone)))
  24699. + mt = get_pageblock_migratetype(page);
  24700. +
  24701. + /* MIGRATE_MOVABLE list may include MIGRATE_RESERVEs */
  24702. + __free_one_page(page, page_to_pfn(page), zone, 0, mt);
  24703. + trace_mm_page_pcpu_drain(page, 0, mt);
  24704. + to_free--;
  24705. + }
  24706. + WARN_ON(to_free != 0);
  24707. + spin_unlock_irqrestore(&zone->lock, flags);
  24708. +}
  24709. +
  24710. +/*
  24711. + * Moves a number of pages from the PCP lists to free list which
  24712. + * is freed outside of the locked region.
  24713. + *
  24714. + * Assumes all pages on list are in same zone, and of same order.
  24715. + * count is the number of pages to free.
  24716. + */
  24717. +static void isolate_pcp_pages(int to_free, struct per_cpu_pages *src,
  24718. + struct list_head *dst)
  24719. +{
  24720. + int migratetype = 0;
  24721. + int batch_free = 0;
  24722. +
  24723. while (to_free) {
  24724. struct page *page;
  24725. struct list_head *list;
  24726. @@ -719,7 +765,7 @@
  24727. batch_free++;
  24728. if (++migratetype == MIGRATE_PCPTYPES)
  24729. migratetype = 0;
  24730. - list = &pcp->lists[migratetype];
  24731. + list = &src->lists[migratetype];
  24732. } while (list_empty(list));
  24733. /* This is the only non-empty list. Free them all. */
  24734. @@ -727,21 +773,11 @@
  24735. batch_free = to_free;
  24736. do {
  24737. - int mt; /* migratetype of the to-be-freed page */
  24738. -
  24739. - page = list_entry(list->prev, struct page, lru);
  24740. - /* must delete as __free_one_page list manipulates */
  24741. + page = list_last_entry(list, struct page, lru);
  24742. list_del(&page->lru);
  24743. - mt = get_freepage_migratetype(page);
  24744. - if (unlikely(has_isolate_pageblock(zone)))
  24745. - mt = get_pageblock_migratetype(page);
  24746. -
  24747. - /* MIGRATE_MOVABLE list may include MIGRATE_RESERVEs */
  24748. - __free_one_page(page, page_to_pfn(page), zone, 0, mt);
  24749. - trace_mm_page_pcpu_drain(page, 0, mt);
  24750. + list_add(&page->lru, dst);
  24751. } while (--to_free && --batch_free && !list_empty(list));
  24752. }
  24753. - spin_unlock(&zone->lock);
  24754. }
  24755. static void free_one_page(struct zone *zone,
  24756. @@ -750,7 +786,9 @@
  24757. int migratetype)
  24758. {
  24759. unsigned long nr_scanned;
  24760. - spin_lock(&zone->lock);
  24761. + unsigned long flags;
  24762. +
  24763. + spin_lock_irqsave(&zone->lock, flags);
  24764. nr_scanned = zone_page_state(zone, NR_PAGES_SCANNED);
  24765. if (nr_scanned)
  24766. __mod_zone_page_state(zone, NR_PAGES_SCANNED, -nr_scanned);
  24767. @@ -760,7 +798,7 @@
  24768. migratetype = get_pfnblock_migratetype(page, pfn);
  24769. }
  24770. __free_one_page(page, pfn, zone, order, migratetype);
  24771. - spin_unlock(&zone->lock);
  24772. + spin_unlock_irqrestore(&zone->lock, flags);
  24773. }
  24774. static int free_tail_pages_check(struct page *head_page, struct page *page)
  24775. @@ -825,11 +863,11 @@
  24776. return;
  24777. migratetype = get_pfnblock_migratetype(page, pfn);
  24778. - local_irq_save(flags);
  24779. + local_lock_irqsave(pa_lock, flags);
  24780. __count_vm_events(PGFREE, 1 << order);
  24781. set_freepage_migratetype(page, migratetype);
  24782. free_one_page(page_zone(page), page, pfn, order, migratetype);
  24783. - local_irq_restore(flags);
  24784. + local_unlock_irqrestore(pa_lock, flags);
  24785. }
  24786. void __init __free_pages_bootmem(struct page *page, unsigned int order)
  24787. @@ -1371,16 +1409,18 @@
  24788. void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp)
  24789. {
  24790. unsigned long flags;
  24791. + LIST_HEAD(dst);
  24792. int to_drain, batch;
  24793. - local_irq_save(flags);
  24794. + local_lock_irqsave(pa_lock, flags);
  24795. batch = READ_ONCE(pcp->batch);
  24796. to_drain = min(pcp->count, batch);
  24797. if (to_drain > 0) {
  24798. - free_pcppages_bulk(zone, to_drain, pcp);
  24799. + isolate_pcp_pages(to_drain, pcp, &dst);
  24800. pcp->count -= to_drain;
  24801. }
  24802. - local_irq_restore(flags);
  24803. + local_unlock_irqrestore(pa_lock, flags);
  24804. + free_pcppages_bulk(zone, to_drain, &dst);
  24805. }
  24806. #endif
  24807. @@ -1396,16 +1436,21 @@
  24808. unsigned long flags;
  24809. struct per_cpu_pageset *pset;
  24810. struct per_cpu_pages *pcp;
  24811. + LIST_HEAD(dst);
  24812. + int count;
  24813. - local_irq_save(flags);
  24814. + cpu_lock_irqsave(cpu, flags);
  24815. pset = per_cpu_ptr(zone->pageset, cpu);
  24816. pcp = &pset->pcp;
  24817. - if (pcp->count) {
  24818. - free_pcppages_bulk(zone, pcp->count, pcp);
  24819. + count = pcp->count;
  24820. + if (count) {
  24821. + isolate_pcp_pages(count, pcp, &dst);
  24822. pcp->count = 0;
  24823. }
  24824. - local_irq_restore(flags);
  24825. + cpu_unlock_irqrestore(cpu, flags);
  24826. + if (count)
  24827. + free_pcppages_bulk(zone, count, &dst);
  24828. }
  24829. /*
  24830. @@ -1491,8 +1536,17 @@
  24831. else
  24832. cpumask_clear_cpu(cpu, &cpus_with_pcps);
  24833. }
  24834. +#ifndef CONFIG_PREEMPT_RT_BASE
  24835. on_each_cpu_mask(&cpus_with_pcps, (smp_call_func_t) drain_local_pages,
  24836. zone, 1);
  24837. +#else
  24838. + for_each_cpu(cpu, &cpus_with_pcps) {
  24839. + if (zone)
  24840. + drain_pages_zone(cpu, zone);
  24841. + else
  24842. + drain_pages(cpu);
  24843. + }
  24844. +#endif
  24845. }
  24846. #ifdef CONFIG_HIBERNATION
  24847. @@ -1548,7 +1602,7 @@
  24848. migratetype = get_pfnblock_migratetype(page, pfn);
  24849. set_freepage_migratetype(page, migratetype);
  24850. - local_irq_save(flags);
  24851. + local_lock_irqsave(pa_lock, flags);
  24852. __count_vm_event(PGFREE);
  24853. /*
  24854. @@ -1574,12 +1628,17 @@
  24855. pcp->count++;
  24856. if (pcp->count >= pcp->high) {
  24857. unsigned long batch = READ_ONCE(pcp->batch);
  24858. - free_pcppages_bulk(zone, batch, pcp);
  24859. + LIST_HEAD(dst);
  24860. +
  24861. + isolate_pcp_pages(batch, pcp, &dst);
  24862. pcp->count -= batch;
  24863. + local_unlock_irqrestore(pa_lock, flags);
  24864. + free_pcppages_bulk(zone, batch, &dst);
  24865. + return;
  24866. }
  24867. out:
  24868. - local_irq_restore(flags);
  24869. + local_unlock_irqrestore(pa_lock, flags);
  24870. }
  24871. /*
  24872. @@ -1710,7 +1769,7 @@
  24873. struct per_cpu_pages *pcp;
  24874. struct list_head *list;
  24875. - local_irq_save(flags);
  24876. + local_lock_irqsave(pa_lock, flags);
  24877. pcp = &this_cpu_ptr(zone->pageset)->pcp;
  24878. list = &pcp->lists[migratetype];
  24879. if (list_empty(list)) {
  24880. @@ -1742,13 +1801,15 @@
  24881. */
  24882. WARN_ON_ONCE(order > 1);
  24883. }
  24884. - spin_lock_irqsave(&zone->lock, flags);
  24885. + local_spin_lock_irqsave(pa_lock, &zone->lock, flags);
  24886. page = __rmqueue(zone, order, migratetype);
  24887. - spin_unlock(&zone->lock);
  24888. - if (!page)
  24889. + if (!page) {
  24890. + spin_unlock(&zone->lock);
  24891. goto failed;
  24892. + }
  24893. __mod_zone_freepage_state(zone, -(1 << order),
  24894. get_freepage_migratetype(page));
  24895. + spin_unlock(&zone->lock);
  24896. }
  24897. __mod_zone_page_state(zone, NR_ALLOC_BATCH, -(1 << order));
  24898. @@ -1758,13 +1819,13 @@
  24899. __count_zone_vm_events(PGALLOC, zone, 1 << order);
  24900. zone_statistics(preferred_zone, zone, gfp_flags);
  24901. - local_irq_restore(flags);
  24902. + local_unlock_irqrestore(pa_lock, flags);
  24903. VM_BUG_ON_PAGE(bad_range(zone, page), page);
  24904. return page;
  24905. failed:
  24906. - local_irq_restore(flags);
  24907. + local_unlock_irqrestore(pa_lock, flags);
  24908. return NULL;
  24909. }
  24910. @@ -5653,6 +5714,7 @@
  24911. void __init page_alloc_init(void)
  24912. {
  24913. hotcpu_notifier(page_alloc_cpu_notify, 0);
  24914. + local_irq_lock_init(pa_lock);
  24915. }
  24916. /*
  24917. @@ -6547,7 +6609,7 @@
  24918. struct per_cpu_pageset *pset;
  24919. /* avoid races with drain_pages() */
  24920. - local_irq_save(flags);
  24921. + local_lock_irqsave(pa_lock, flags);
  24922. if (zone->pageset != &boot_pageset) {
  24923. for_each_online_cpu(cpu) {
  24924. pset = per_cpu_ptr(zone->pageset, cpu);
  24925. @@ -6556,7 +6618,7 @@
  24926. free_percpu(zone->pageset);
  24927. zone->pageset = &boot_pageset;
  24928. }
  24929. - local_irq_restore(flags);
  24930. + local_unlock_irqrestore(pa_lock, flags);
  24931. }
  24932. #ifdef CONFIG_MEMORY_HOTREMOVE
  24933. diff -Nur linux-4.1.20.orig/mm/slab.h linux-4.1.20/mm/slab.h
  24934. --- linux-4.1.20.orig/mm/slab.h 2016-03-17 19:11:03.000000000 +0100
  24935. +++ linux-4.1.20/mm/slab.h 2016-03-21 20:18:34.000000000 +0100
  24936. @@ -330,7 +330,11 @@
  24937. * The slab lists for all objects.
  24938. */
  24939. struct kmem_cache_node {
  24940. +#ifdef CONFIG_SLUB
  24941. + raw_spinlock_t list_lock;
  24942. +#else
  24943. spinlock_t list_lock;
  24944. +#endif
  24945. #ifdef CONFIG_SLAB
  24946. struct list_head slabs_partial; /* partial list first, better asm code */
  24947. diff -Nur linux-4.1.20.orig/mm/slub.c linux-4.1.20/mm/slub.c
  24948. --- linux-4.1.20.orig/mm/slub.c 2016-03-17 19:11:03.000000000 +0100
  24949. +++ linux-4.1.20/mm/slub.c 2016-03-21 20:18:34.000000000 +0100
  24950. @@ -1069,7 +1069,7 @@
  24951. {
  24952. struct kmem_cache_node *n = get_node(s, page_to_nid(page));
  24953. - spin_lock_irqsave(&n->list_lock, *flags);
  24954. + raw_spin_lock_irqsave(&n->list_lock, *flags);
  24955. slab_lock(page);
  24956. if (!check_slab(s, page))
  24957. @@ -1116,7 +1116,7 @@
  24958. fail:
  24959. slab_unlock(page);
  24960. - spin_unlock_irqrestore(&n->list_lock, *flags);
  24961. + raw_spin_unlock_irqrestore(&n->list_lock, *flags);
  24962. slab_fix(s, "Object at 0x%p not freed", object);
  24963. return NULL;
  24964. }
  24965. @@ -1242,6 +1242,12 @@
  24966. #endif /* CONFIG_SLUB_DEBUG */
  24967. +struct slub_free_list {
  24968. + raw_spinlock_t lock;
  24969. + struct list_head list;
  24970. +};
  24971. +static DEFINE_PER_CPU(struct slub_free_list, slub_free_list);
  24972. +
  24973. /*
  24974. * Hooks for other subsystems that check memory allocations. In a typical
  24975. * production configuration these hooks all should produce no code at all.
  24976. @@ -1306,6 +1312,17 @@
  24977. kasan_slab_free(s, x);
  24978. }
  24979. +static void setup_object(struct kmem_cache *s, struct page *page,
  24980. + void *object)
  24981. +{
  24982. + setup_object_debug(s, page, object);
  24983. + if (unlikely(s->ctor)) {
  24984. + kasan_unpoison_object_data(s, object);
  24985. + s->ctor(object);
  24986. + kasan_poison_object_data(s, object);
  24987. + }
  24988. +}
  24989. +
  24990. /*
  24991. * Slab allocation and freeing
  24992. */
  24993. @@ -1336,10 +1353,17 @@
  24994. struct page *page;
  24995. struct kmem_cache_order_objects oo = s->oo;
  24996. gfp_t alloc_gfp;
  24997. + void *start, *p;
  24998. + int idx, order;
  24999. + bool enableirqs;
  25000. flags &= gfp_allowed_mask;
  25001. - if (flags & __GFP_WAIT)
  25002. + enableirqs = (flags & __GFP_WAIT) != 0;
  25003. +#ifdef CONFIG_PREEMPT_RT_FULL
  25004. + enableirqs |= system_state == SYSTEM_RUNNING;
  25005. +#endif
  25006. + if (enableirqs)
  25007. local_irq_enable();
  25008. flags |= s->allocflags;
  25009. @@ -1359,13 +1383,13 @@
  25010. * Try a lower order alloc if possible
  25011. */
  25012. page = alloc_slab_page(s, alloc_gfp, node, oo);
  25013. -
  25014. - if (page)
  25015. - stat(s, ORDER_FALLBACK);
  25016. + if (unlikely(!page))
  25017. + goto out;
  25018. + stat(s, ORDER_FALLBACK);
  25019. }
  25020. - if (kmemcheck_enabled && page
  25021. - && !(s->flags & (SLAB_NOTRACK | DEBUG_DEFAULT_FLAGS))) {
  25022. + if (kmemcheck_enabled &&
  25023. + !(s->flags & (SLAB_NOTRACK | DEBUG_DEFAULT_FLAGS))) {
  25024. int pages = 1 << oo_order(oo);
  25025. kmemcheck_alloc_shadow(page, oo_order(oo), alloc_gfp, node);
  25026. @@ -1380,51 +1404,9 @@
  25027. kmemcheck_mark_unallocated_pages(page, pages);
  25028. }
  25029. - if (flags & __GFP_WAIT)
  25030. - local_irq_disable();
  25031. - if (!page)
  25032. - return NULL;
  25033. -
  25034. page->objects = oo_objects(oo);
  25035. - mod_zone_page_state(page_zone(page),
  25036. - (s->flags & SLAB_RECLAIM_ACCOUNT) ?
  25037. - NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
  25038. - 1 << oo_order(oo));
  25039. -
  25040. - return page;
  25041. -}
  25042. -
  25043. -static void setup_object(struct kmem_cache *s, struct page *page,
  25044. - void *object)
  25045. -{
  25046. - setup_object_debug(s, page, object);
  25047. - if (unlikely(s->ctor)) {
  25048. - kasan_unpoison_object_data(s, object);
  25049. - s->ctor(object);
  25050. - kasan_poison_object_data(s, object);
  25051. - }
  25052. -}
  25053. -
  25054. -static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
  25055. -{
  25056. - struct page *page;
  25057. - void *start;
  25058. - void *p;
  25059. - int order;
  25060. - int idx;
  25061. -
  25062. - if (unlikely(flags & GFP_SLAB_BUG_MASK)) {
  25063. - pr_emerg("gfp: %u\n", flags & GFP_SLAB_BUG_MASK);
  25064. - BUG();
  25065. - }
  25066. -
  25067. - page = allocate_slab(s,
  25068. - flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
  25069. - if (!page)
  25070. - goto out;
  25071. order = compound_order(page);
  25072. - inc_slabs_node(s, page_to_nid(page), page->objects);
  25073. page->slab_cache = s;
  25074. __SetPageSlab(page);
  25075. if (page_is_pfmemalloc(page))
  25076. @@ -1448,10 +1430,34 @@
  25077. page->freelist = start;
  25078. page->inuse = page->objects;
  25079. page->frozen = 1;
  25080. +
  25081. out:
  25082. + if (enableirqs)
  25083. + local_irq_disable();
  25084. + if (!page)
  25085. + return NULL;
  25086. +
  25087. + mod_zone_page_state(page_zone(page),
  25088. + (s->flags & SLAB_RECLAIM_ACCOUNT) ?
  25089. + NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
  25090. + 1 << oo_order(oo));
  25091. +
  25092. + inc_slabs_node(s, page_to_nid(page), page->objects);
  25093. +
  25094. return page;
  25095. }
  25096. +static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
  25097. +{
  25098. + if (unlikely(flags & GFP_SLAB_BUG_MASK)) {
  25099. + pr_emerg("gfp: %u\n", flags & GFP_SLAB_BUG_MASK);
  25100. + BUG();
  25101. + }
  25102. +
  25103. + return allocate_slab(s,
  25104. + flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
  25105. +}
  25106. +
  25107. static void __free_slab(struct kmem_cache *s, struct page *page)
  25108. {
  25109. int order = compound_order(page);
  25110. @@ -1483,6 +1489,16 @@
  25111. memcg_uncharge_slab(s, order);
  25112. }
  25113. +static void free_delayed(struct list_head *h)
  25114. +{
  25115. + while(!list_empty(h)) {
  25116. + struct page *page = list_first_entry(h, struct page, lru);
  25117. +
  25118. + list_del(&page->lru);
  25119. + __free_slab(page->slab_cache, page);
  25120. + }
  25121. +}
  25122. +
  25123. #define need_reserve_slab_rcu \
  25124. (sizeof(((struct page *)NULL)->lru) < sizeof(struct rcu_head))
  25125. @@ -1517,6 +1533,12 @@
  25126. }
  25127. call_rcu(head, rcu_free_slab);
  25128. + } else if (irqs_disabled()) {
  25129. + struct slub_free_list *f = this_cpu_ptr(&slub_free_list);
  25130. +
  25131. + raw_spin_lock(&f->lock);
  25132. + list_add(&page->lru, &f->list);
  25133. + raw_spin_unlock(&f->lock);
  25134. } else
  25135. __free_slab(s, page);
  25136. }
  25137. @@ -1630,7 +1652,7 @@
  25138. if (!n || !n->nr_partial)
  25139. return NULL;
  25140. - spin_lock(&n->list_lock);
  25141. + raw_spin_lock(&n->list_lock);
  25142. list_for_each_entry_safe(page, page2, &n->partial, lru) {
  25143. void *t;
  25144. @@ -1655,7 +1677,7 @@
  25145. break;
  25146. }
  25147. - spin_unlock(&n->list_lock);
  25148. + raw_spin_unlock(&n->list_lock);
  25149. return object;
  25150. }
  25151. @@ -1901,7 +1923,7 @@
  25152. * that acquire_slab() will see a slab page that
  25153. * is frozen
  25154. */
  25155. - spin_lock(&n->list_lock);
  25156. + raw_spin_lock(&n->list_lock);
  25157. }
  25158. } else {
  25159. m = M_FULL;
  25160. @@ -1912,7 +1934,7 @@
  25161. * slabs from diagnostic functions will not see
  25162. * any frozen slabs.
  25163. */
  25164. - spin_lock(&n->list_lock);
  25165. + raw_spin_lock(&n->list_lock);
  25166. }
  25167. }
  25168. @@ -1947,7 +1969,7 @@
  25169. goto redo;
  25170. if (lock)
  25171. - spin_unlock(&n->list_lock);
  25172. + raw_spin_unlock(&n->list_lock);
  25173. if (m == M_FREE) {
  25174. stat(s, DEACTIVATE_EMPTY);
  25175. @@ -1979,10 +2001,10 @@
  25176. n2 = get_node(s, page_to_nid(page));
  25177. if (n != n2) {
  25178. if (n)
  25179. - spin_unlock(&n->list_lock);
  25180. + raw_spin_unlock(&n->list_lock);
  25181. n = n2;
  25182. - spin_lock(&n->list_lock);
  25183. + raw_spin_lock(&n->list_lock);
  25184. }
  25185. do {
  25186. @@ -2011,7 +2033,7 @@
  25187. }
  25188. if (n)
  25189. - spin_unlock(&n->list_lock);
  25190. + raw_spin_unlock(&n->list_lock);
  25191. while (discard_page) {
  25192. page = discard_page;
  25193. @@ -2050,14 +2072,21 @@
  25194. pobjects = oldpage->pobjects;
  25195. pages = oldpage->pages;
  25196. if (drain && pobjects > s->cpu_partial) {
  25197. + struct slub_free_list *f;
  25198. unsigned long flags;
  25199. + LIST_HEAD(tofree);
  25200. /*
  25201. * partial array is full. Move the existing
  25202. * set to the per node partial list.
  25203. */
  25204. local_irq_save(flags);
  25205. unfreeze_partials(s, this_cpu_ptr(s->cpu_slab));
  25206. + f = this_cpu_ptr(&slub_free_list);
  25207. + raw_spin_lock(&f->lock);
  25208. + list_splice_init(&f->list, &tofree);
  25209. + raw_spin_unlock(&f->lock);
  25210. local_irq_restore(flags);
  25211. + free_delayed(&tofree);
  25212. oldpage = NULL;
  25213. pobjects = 0;
  25214. pages = 0;
  25215. @@ -2129,7 +2158,22 @@
  25216. static void flush_all(struct kmem_cache *s)
  25217. {
  25218. + LIST_HEAD(tofree);
  25219. + int cpu;
  25220. +
  25221. on_each_cpu_cond(has_cpu_slab, flush_cpu_slab, s, 1, GFP_ATOMIC);
  25222. + for_each_online_cpu(cpu) {
  25223. + struct slub_free_list *f;
  25224. +
  25225. + if (!has_cpu_slab(cpu, s))
  25226. + continue;
  25227. +
  25228. + f = &per_cpu(slub_free_list, cpu);
  25229. + raw_spin_lock_irq(&f->lock);
  25230. + list_splice_init(&f->list, &tofree);
  25231. + raw_spin_unlock_irq(&f->lock);
  25232. + free_delayed(&tofree);
  25233. + }
  25234. }
  25235. /*
  25236. @@ -2165,10 +2209,10 @@
  25237. unsigned long x = 0;
  25238. struct page *page;
  25239. - spin_lock_irqsave(&n->list_lock, flags);
  25240. + raw_spin_lock_irqsave(&n->list_lock, flags);
  25241. list_for_each_entry(page, &n->partial, lru)
  25242. x += get_count(page);
  25243. - spin_unlock_irqrestore(&n->list_lock, flags);
  25244. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  25245. return x;
  25246. }
  25247. #endif /* CONFIG_SLUB_DEBUG || CONFIG_SYSFS */
  25248. @@ -2305,9 +2349,11 @@
  25249. static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
  25250. unsigned long addr, struct kmem_cache_cpu *c)
  25251. {
  25252. + struct slub_free_list *f;
  25253. void *freelist;
  25254. struct page *page;
  25255. unsigned long flags;
  25256. + LIST_HEAD(tofree);
  25257. local_irq_save(flags);
  25258. #ifdef CONFIG_PREEMPT
  25259. @@ -2375,7 +2421,13 @@
  25260. VM_BUG_ON(!c->page->frozen);
  25261. c->freelist = get_freepointer(s, freelist);
  25262. c->tid = next_tid(c->tid);
  25263. +out:
  25264. + f = this_cpu_ptr(&slub_free_list);
  25265. + raw_spin_lock(&f->lock);
  25266. + list_splice_init(&f->list, &tofree);
  25267. + raw_spin_unlock(&f->lock);
  25268. local_irq_restore(flags);
  25269. + free_delayed(&tofree);
  25270. return freelist;
  25271. new_slab:
  25272. @@ -2392,8 +2444,7 @@
  25273. if (unlikely(!freelist)) {
  25274. slab_out_of_memory(s, gfpflags, node);
  25275. - local_irq_restore(flags);
  25276. - return NULL;
  25277. + goto out;
  25278. }
  25279. page = c->page;
  25280. @@ -2408,8 +2459,7 @@
  25281. deactivate_slab(s, page, get_freepointer(s, freelist));
  25282. c->page = NULL;
  25283. c->freelist = NULL;
  25284. - local_irq_restore(flags);
  25285. - return freelist;
  25286. + goto out;
  25287. }
  25288. /*
  25289. @@ -2593,7 +2643,7 @@
  25290. do {
  25291. if (unlikely(n)) {
  25292. - spin_unlock_irqrestore(&n->list_lock, flags);
  25293. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  25294. n = NULL;
  25295. }
  25296. prior = page->freelist;
  25297. @@ -2625,7 +2675,7 @@
  25298. * Otherwise the list_lock will synchronize with
  25299. * other processors updating the list of slabs.
  25300. */
  25301. - spin_lock_irqsave(&n->list_lock, flags);
  25302. + raw_spin_lock_irqsave(&n->list_lock, flags);
  25303. }
  25304. }
  25305. @@ -2667,7 +2717,7 @@
  25306. add_partial(n, page, DEACTIVATE_TO_TAIL);
  25307. stat(s, FREE_ADD_PARTIAL);
  25308. }
  25309. - spin_unlock_irqrestore(&n->list_lock, flags);
  25310. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  25311. return;
  25312. slab_empty:
  25313. @@ -2682,7 +2732,7 @@
  25314. remove_full(s, n, page);
  25315. }
  25316. - spin_unlock_irqrestore(&n->list_lock, flags);
  25317. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  25318. stat(s, FREE_SLAB);
  25319. discard_slab(s, page);
  25320. }
  25321. @@ -2881,7 +2931,7 @@
  25322. init_kmem_cache_node(struct kmem_cache_node *n)
  25323. {
  25324. n->nr_partial = 0;
  25325. - spin_lock_init(&n->list_lock);
  25326. + raw_spin_lock_init(&n->list_lock);
  25327. INIT_LIST_HEAD(&n->partial);
  25328. #ifdef CONFIG_SLUB_DEBUG
  25329. atomic_long_set(&n->nr_slabs, 0);
  25330. @@ -3463,7 +3513,7 @@
  25331. for (i = 0; i < SHRINK_PROMOTE_MAX; i++)
  25332. INIT_LIST_HEAD(promote + i);
  25333. - spin_lock_irqsave(&n->list_lock, flags);
  25334. + raw_spin_lock_irqsave(&n->list_lock, flags);
  25335. /*
  25336. * Build lists of slabs to discard or promote.
  25337. @@ -3494,7 +3544,7 @@
  25338. for (i = SHRINK_PROMOTE_MAX - 1; i >= 0; i--)
  25339. list_splice(promote + i, &n->partial);
  25340. - spin_unlock_irqrestore(&n->list_lock, flags);
  25341. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  25342. /* Release empty slabs */
  25343. list_for_each_entry_safe(page, t, &discard, lru)
  25344. @@ -3670,6 +3720,12 @@
  25345. {
  25346. static __initdata struct kmem_cache boot_kmem_cache,
  25347. boot_kmem_cache_node;
  25348. + int cpu;
  25349. +
  25350. + for_each_possible_cpu(cpu) {
  25351. + raw_spin_lock_init(&per_cpu(slub_free_list, cpu).lock);
  25352. + INIT_LIST_HEAD(&per_cpu(slub_free_list, cpu).list);
  25353. + }
  25354. if (debug_guardpage_minorder())
  25355. slub_max_order = 0;
  25356. @@ -3912,7 +3968,7 @@
  25357. struct page *page;
  25358. unsigned long flags;
  25359. - spin_lock_irqsave(&n->list_lock, flags);
  25360. + raw_spin_lock_irqsave(&n->list_lock, flags);
  25361. list_for_each_entry(page, &n->partial, lru) {
  25362. validate_slab_slab(s, page, map);
  25363. @@ -3934,7 +3990,7 @@
  25364. s->name, count, atomic_long_read(&n->nr_slabs));
  25365. out:
  25366. - spin_unlock_irqrestore(&n->list_lock, flags);
  25367. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  25368. return count;
  25369. }
  25370. @@ -4122,12 +4178,12 @@
  25371. if (!atomic_long_read(&n->nr_slabs))
  25372. continue;
  25373. - spin_lock_irqsave(&n->list_lock, flags);
  25374. + raw_spin_lock_irqsave(&n->list_lock, flags);
  25375. list_for_each_entry(page, &n->partial, lru)
  25376. process_slab(&t, s, page, alloc, map);
  25377. list_for_each_entry(page, &n->full, lru)
  25378. process_slab(&t, s, page, alloc, map);
  25379. - spin_unlock_irqrestore(&n->list_lock, flags);
  25380. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  25381. }
  25382. for (i = 0; i < t.count; i++) {
  25383. diff -Nur linux-4.1.20.orig/mm/swap.c linux-4.1.20/mm/swap.c
  25384. --- linux-4.1.20.orig/mm/swap.c 2016-03-17 19:11:03.000000000 +0100
  25385. +++ linux-4.1.20/mm/swap.c 2016-03-21 20:18:34.000000000 +0100
  25386. @@ -32,6 +32,7 @@
  25387. #include <linux/gfp.h>
  25388. #include <linux/uio.h>
  25389. #include <linux/hugetlb.h>
  25390. +#include <linux/locallock.h>
  25391. #include "internal.h"
  25392. @@ -45,6 +46,9 @@
  25393. static DEFINE_PER_CPU(struct pagevec, lru_rotate_pvecs);
  25394. static DEFINE_PER_CPU(struct pagevec, lru_deactivate_file_pvecs);
  25395. +static DEFINE_LOCAL_IRQ_LOCK(rotate_lock);
  25396. +DEFINE_LOCAL_IRQ_LOCK(swapvec_lock);
  25397. +
  25398. /*
  25399. * This path almost never happens for VM activity - pages are normally
  25400. * freed via pagevecs. But it gets used by networking.
  25401. @@ -481,11 +485,11 @@
  25402. unsigned long flags;
  25403. page_cache_get(page);
  25404. - local_irq_save(flags);
  25405. + local_lock_irqsave(rotate_lock, flags);
  25406. pvec = this_cpu_ptr(&lru_rotate_pvecs);
  25407. if (!pagevec_add(pvec, page))
  25408. pagevec_move_tail(pvec);
  25409. - local_irq_restore(flags);
  25410. + local_unlock_irqrestore(rotate_lock, flags);
  25411. }
  25412. }
  25413. @@ -536,12 +540,13 @@
  25414. void activate_page(struct page *page)
  25415. {
  25416. if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
  25417. - struct pagevec *pvec = &get_cpu_var(activate_page_pvecs);
  25418. + struct pagevec *pvec = &get_locked_var(swapvec_lock,
  25419. + activate_page_pvecs);
  25420. page_cache_get(page);
  25421. if (!pagevec_add(pvec, page))
  25422. pagevec_lru_move_fn(pvec, __activate_page, NULL);
  25423. - put_cpu_var(activate_page_pvecs);
  25424. + put_locked_var(swapvec_lock, activate_page_pvecs);
  25425. }
  25426. }
  25427. @@ -567,7 +572,7 @@
  25428. static void __lru_cache_activate_page(struct page *page)
  25429. {
  25430. - struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
  25431. + struct pagevec *pvec = &get_locked_var(swapvec_lock, lru_add_pvec);
  25432. int i;
  25433. /*
  25434. @@ -589,7 +594,7 @@
  25435. }
  25436. }
  25437. - put_cpu_var(lru_add_pvec);
  25438. + put_locked_var(swapvec_lock, lru_add_pvec);
  25439. }
  25440. /*
  25441. @@ -628,13 +633,13 @@
  25442. static void __lru_cache_add(struct page *page)
  25443. {
  25444. - struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
  25445. + struct pagevec *pvec = &get_locked_var(swapvec_lock, lru_add_pvec);
  25446. page_cache_get(page);
  25447. if (!pagevec_space(pvec))
  25448. __pagevec_lru_add(pvec);
  25449. pagevec_add(pvec, page);
  25450. - put_cpu_var(lru_add_pvec);
  25451. + put_locked_var(swapvec_lock, lru_add_pvec);
  25452. }
  25453. /**
  25454. @@ -814,9 +819,9 @@
  25455. unsigned long flags;
  25456. /* No harm done if a racing interrupt already did this */
  25457. - local_irq_save(flags);
  25458. + local_lock_irqsave(rotate_lock, flags);
  25459. pagevec_move_tail(pvec);
  25460. - local_irq_restore(flags);
  25461. + local_unlock_irqrestore(rotate_lock, flags);
  25462. }
  25463. pvec = &per_cpu(lru_deactivate_file_pvecs, cpu);
  25464. @@ -844,18 +849,19 @@
  25465. return;
  25466. if (likely(get_page_unless_zero(page))) {
  25467. - struct pagevec *pvec = &get_cpu_var(lru_deactivate_file_pvecs);
  25468. + struct pagevec *pvec = &get_locked_var(swapvec_lock,
  25469. + lru_deactivate_file_pvecs);
  25470. if (!pagevec_add(pvec, page))
  25471. pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
  25472. - put_cpu_var(lru_deactivate_file_pvecs);
  25473. + put_locked_var(swapvec_lock, lru_deactivate_file_pvecs);
  25474. }
  25475. }
  25476. void lru_add_drain(void)
  25477. {
  25478. - lru_add_drain_cpu(get_cpu());
  25479. - put_cpu();
  25480. + lru_add_drain_cpu(local_lock_cpu(swapvec_lock));
  25481. + local_unlock_cpu(swapvec_lock);
  25482. }
  25483. static void lru_add_drain_per_cpu(struct work_struct *dummy)
  25484. diff -Nur linux-4.1.20.orig/mm/truncate.c linux-4.1.20/mm/truncate.c
  25485. --- linux-4.1.20.orig/mm/truncate.c 2016-03-17 19:11:03.000000000 +0100
  25486. +++ linux-4.1.20/mm/truncate.c 2016-03-21 20:18:34.000000000 +0100
  25487. @@ -56,8 +56,11 @@
  25488. * protected by mapping->tree_lock.
  25489. */
  25490. if (!workingset_node_shadows(node) &&
  25491. - !list_empty(&node->private_list))
  25492. - list_lru_del(&workingset_shadow_nodes, &node->private_list);
  25493. + !list_empty(&node->private_list)) {
  25494. + local_lock(workingset_shadow_lock);
  25495. + list_lru_del(&__workingset_shadow_nodes, &node->private_list);
  25496. + local_unlock(workingset_shadow_lock);
  25497. + }
  25498. __radix_tree_delete_node(&mapping->page_tree, node);
  25499. unlock:
  25500. spin_unlock_irq(&mapping->tree_lock);
  25501. diff -Nur linux-4.1.20.orig/mm/vmalloc.c linux-4.1.20/mm/vmalloc.c
  25502. --- linux-4.1.20.orig/mm/vmalloc.c 2016-03-17 19:11:03.000000000 +0100
  25503. +++ linux-4.1.20/mm/vmalloc.c 2016-03-21 20:18:34.000000000 +0100
  25504. @@ -819,7 +819,7 @@
  25505. struct vmap_block *vb;
  25506. struct vmap_area *va;
  25507. unsigned long vb_idx;
  25508. - int node, err;
  25509. + int node, err, cpu;
  25510. void *vaddr;
  25511. node = numa_node_id();
  25512. @@ -862,11 +862,12 @@
  25513. BUG_ON(err);
  25514. radix_tree_preload_end();
  25515. - vbq = &get_cpu_var(vmap_block_queue);
  25516. + cpu = get_cpu_light();
  25517. + vbq = this_cpu_ptr(&vmap_block_queue);
  25518. spin_lock(&vbq->lock);
  25519. list_add_tail_rcu(&vb->free_list, &vbq->free);
  25520. spin_unlock(&vbq->lock);
  25521. - put_cpu_var(vmap_block_queue);
  25522. + put_cpu_light();
  25523. return vaddr;
  25524. }
  25525. @@ -935,6 +936,7 @@
  25526. struct vmap_block *vb;
  25527. void *vaddr = NULL;
  25528. unsigned int order;
  25529. + int cpu;
  25530. BUG_ON(size & ~PAGE_MASK);
  25531. BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
  25532. @@ -949,7 +951,8 @@
  25533. order = get_order(size);
  25534. rcu_read_lock();
  25535. - vbq = &get_cpu_var(vmap_block_queue);
  25536. + cpu = get_cpu_light();
  25537. + vbq = this_cpu_ptr(&vmap_block_queue);
  25538. list_for_each_entry_rcu(vb, &vbq->free, free_list) {
  25539. unsigned long pages_off;
  25540. @@ -972,7 +975,7 @@
  25541. break;
  25542. }
  25543. - put_cpu_var(vmap_block_queue);
  25544. + put_cpu_light();
  25545. rcu_read_unlock();
  25546. /* Allocate new block if nothing was found */
  25547. diff -Nur linux-4.1.20.orig/mm/vmstat.c linux-4.1.20/mm/vmstat.c
  25548. --- linux-4.1.20.orig/mm/vmstat.c 2016-03-17 19:11:03.000000000 +0100
  25549. +++ linux-4.1.20/mm/vmstat.c 2016-03-21 20:18:34.000000000 +0100
  25550. @@ -226,6 +226,7 @@
  25551. long x;
  25552. long t;
  25553. + preempt_disable_rt();
  25554. x = delta + __this_cpu_read(*p);
  25555. t = __this_cpu_read(pcp->stat_threshold);
  25556. @@ -235,6 +236,7 @@
  25557. x = 0;
  25558. }
  25559. __this_cpu_write(*p, x);
  25560. + preempt_enable_rt();
  25561. }
  25562. EXPORT_SYMBOL(__mod_zone_page_state);
  25563. @@ -267,6 +269,7 @@
  25564. s8 __percpu *p = pcp->vm_stat_diff + item;
  25565. s8 v, t;
  25566. + preempt_disable_rt();
  25567. v = __this_cpu_inc_return(*p);
  25568. t = __this_cpu_read(pcp->stat_threshold);
  25569. if (unlikely(v > t)) {
  25570. @@ -275,6 +278,7 @@
  25571. zone_page_state_add(v + overstep, zone, item);
  25572. __this_cpu_write(*p, -overstep);
  25573. }
  25574. + preempt_enable_rt();
  25575. }
  25576. void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
  25577. @@ -289,6 +293,7 @@
  25578. s8 __percpu *p = pcp->vm_stat_diff + item;
  25579. s8 v, t;
  25580. + preempt_disable_rt();
  25581. v = __this_cpu_dec_return(*p);
  25582. t = __this_cpu_read(pcp->stat_threshold);
  25583. if (unlikely(v < - t)) {
  25584. @@ -297,6 +302,7 @@
  25585. zone_page_state_add(v - overstep, zone, item);
  25586. __this_cpu_write(*p, overstep);
  25587. }
  25588. + preempt_enable_rt();
  25589. }
  25590. void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
  25591. diff -Nur linux-4.1.20.orig/mm/workingset.c linux-4.1.20/mm/workingset.c
  25592. --- linux-4.1.20.orig/mm/workingset.c 2016-03-17 19:11:03.000000000 +0100
  25593. +++ linux-4.1.20/mm/workingset.c 2016-03-21 20:18:34.000000000 +0100
  25594. @@ -264,7 +264,8 @@
  25595. * point where they would still be useful.
  25596. */
  25597. -struct list_lru workingset_shadow_nodes;
  25598. +struct list_lru __workingset_shadow_nodes;
  25599. +DEFINE_LOCAL_IRQ_LOCK(workingset_shadow_lock);
  25600. static unsigned long count_shadow_nodes(struct shrinker *shrinker,
  25601. struct shrink_control *sc)
  25602. @@ -274,9 +275,9 @@
  25603. unsigned long pages;
  25604. /* list_lru lock nests inside IRQ-safe mapping->tree_lock */
  25605. - local_irq_disable();
  25606. - shadow_nodes = list_lru_shrink_count(&workingset_shadow_nodes, sc);
  25607. - local_irq_enable();
  25608. + local_lock_irq(workingset_shadow_lock);
  25609. + shadow_nodes = list_lru_shrink_count(&__workingset_shadow_nodes, sc);
  25610. + local_unlock_irq(workingset_shadow_lock);
  25611. pages = node_present_pages(sc->nid);
  25612. /*
  25613. @@ -363,9 +364,9 @@
  25614. spin_unlock(&mapping->tree_lock);
  25615. ret = LRU_REMOVED_RETRY;
  25616. out:
  25617. - local_irq_enable();
  25618. + local_unlock_irq(workingset_shadow_lock);
  25619. cond_resched();
  25620. - local_irq_disable();
  25621. + local_lock_irq(workingset_shadow_lock);
  25622. spin_lock(lru_lock);
  25623. return ret;
  25624. }
  25625. @@ -376,10 +377,10 @@
  25626. unsigned long ret;
  25627. /* list_lru lock nests inside IRQ-safe mapping->tree_lock */
  25628. - local_irq_disable();
  25629. - ret = list_lru_shrink_walk(&workingset_shadow_nodes, sc,
  25630. + local_lock_irq(workingset_shadow_lock);
  25631. + ret = list_lru_shrink_walk(&__workingset_shadow_nodes, sc,
  25632. shadow_lru_isolate, NULL);
  25633. - local_irq_enable();
  25634. + local_unlock_irq(workingset_shadow_lock);
  25635. return ret;
  25636. }
  25637. @@ -400,7 +401,7 @@
  25638. {
  25639. int ret;
  25640. - ret = list_lru_init_key(&workingset_shadow_nodes, &shadow_nodes_key);
  25641. + ret = list_lru_init_key(&__workingset_shadow_nodes, &shadow_nodes_key);
  25642. if (ret)
  25643. goto err;
  25644. ret = register_shrinker(&workingset_shadow_shrinker);
  25645. @@ -408,7 +409,7 @@
  25646. goto err_list_lru;
  25647. return 0;
  25648. err_list_lru:
  25649. - list_lru_destroy(&workingset_shadow_nodes);
  25650. + list_lru_destroy(&__workingset_shadow_nodes);
  25651. err:
  25652. return ret;
  25653. }
  25654. diff -Nur linux-4.1.20.orig/net/core/dev.c linux-4.1.20/net/core/dev.c
  25655. --- linux-4.1.20.orig/net/core/dev.c 2016-03-17 19:11:03.000000000 +0100
  25656. +++ linux-4.1.20/net/core/dev.c 2016-03-21 20:18:34.000000000 +0100
  25657. @@ -184,6 +184,7 @@
  25658. static DEFINE_HASHTABLE(napi_hash, 8);
  25659. static seqcount_t devnet_rename_seq;
  25660. +static DEFINE_MUTEX(devnet_rename_mutex);
  25661. static inline void dev_base_seq_inc(struct net *net)
  25662. {
  25663. @@ -205,14 +206,14 @@
  25664. static inline void rps_lock(struct softnet_data *sd)
  25665. {
  25666. #ifdef CONFIG_RPS
  25667. - spin_lock(&sd->input_pkt_queue.lock);
  25668. + raw_spin_lock(&sd->input_pkt_queue.raw_lock);
  25669. #endif
  25670. }
  25671. static inline void rps_unlock(struct softnet_data *sd)
  25672. {
  25673. #ifdef CONFIG_RPS
  25674. - spin_unlock(&sd->input_pkt_queue.lock);
  25675. + raw_spin_unlock(&sd->input_pkt_queue.raw_lock);
  25676. #endif
  25677. }
  25678. @@ -852,7 +853,8 @@
  25679. strcpy(name, dev->name);
  25680. rcu_read_unlock();
  25681. if (read_seqcount_retry(&devnet_rename_seq, seq)) {
  25682. - cond_resched();
  25683. + mutex_lock(&devnet_rename_mutex);
  25684. + mutex_unlock(&devnet_rename_mutex);
  25685. goto retry;
  25686. }
  25687. @@ -1121,20 +1123,17 @@
  25688. if (dev->flags & IFF_UP)
  25689. return -EBUSY;
  25690. - write_seqcount_begin(&devnet_rename_seq);
  25691. + mutex_lock(&devnet_rename_mutex);
  25692. + __raw_write_seqcount_begin(&devnet_rename_seq);
  25693. - if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
  25694. - write_seqcount_end(&devnet_rename_seq);
  25695. - return 0;
  25696. - }
  25697. + if (strncmp(newname, dev->name, IFNAMSIZ) == 0)
  25698. + goto outunlock;
  25699. memcpy(oldname, dev->name, IFNAMSIZ);
  25700. err = dev_get_valid_name(net, dev, newname);
  25701. - if (err < 0) {
  25702. - write_seqcount_end(&devnet_rename_seq);
  25703. - return err;
  25704. - }
  25705. + if (err < 0)
  25706. + goto outunlock;
  25707. if (oldname[0] && !strchr(oldname, '%'))
  25708. netdev_info(dev, "renamed from %s\n", oldname);
  25709. @@ -1147,11 +1146,12 @@
  25710. if (ret) {
  25711. memcpy(dev->name, oldname, IFNAMSIZ);
  25712. dev->name_assign_type = old_assign_type;
  25713. - write_seqcount_end(&devnet_rename_seq);
  25714. - return ret;
  25715. + err = ret;
  25716. + goto outunlock;
  25717. }
  25718. - write_seqcount_end(&devnet_rename_seq);
  25719. + __raw_write_seqcount_end(&devnet_rename_seq);
  25720. + mutex_unlock(&devnet_rename_mutex);
  25721. netdev_adjacent_rename_links(dev, oldname);
  25722. @@ -1172,7 +1172,8 @@
  25723. /* err >= 0 after dev_alloc_name() or stores the first errno */
  25724. if (err >= 0) {
  25725. err = ret;
  25726. - write_seqcount_begin(&devnet_rename_seq);
  25727. + mutex_lock(&devnet_rename_mutex);
  25728. + __raw_write_seqcount_begin(&devnet_rename_seq);
  25729. memcpy(dev->name, oldname, IFNAMSIZ);
  25730. memcpy(oldname, newname, IFNAMSIZ);
  25731. dev->name_assign_type = old_assign_type;
  25732. @@ -1185,6 +1186,11 @@
  25733. }
  25734. return err;
  25735. +
  25736. +outunlock:
  25737. + __raw_write_seqcount_end(&devnet_rename_seq);
  25738. + mutex_unlock(&devnet_rename_mutex);
  25739. + return err;
  25740. }
  25741. /**
  25742. @@ -2214,6 +2220,7 @@
  25743. sd->output_queue_tailp = &q->next_sched;
  25744. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  25745. local_irq_restore(flags);
  25746. + preempt_check_resched_rt();
  25747. }
  25748. void __netif_schedule(struct Qdisc *q)
  25749. @@ -2295,6 +2302,7 @@
  25750. __this_cpu_write(softnet_data.completion_queue, skb);
  25751. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  25752. local_irq_restore(flags);
  25753. + preempt_check_resched_rt();
  25754. }
  25755. EXPORT_SYMBOL(__dev_kfree_skb_irq);
  25756. @@ -2880,9 +2888,44 @@
  25757. #define skb_update_prio(skb)
  25758. #endif
  25759. +#ifdef CONFIG_PREEMPT_RT_FULL
  25760. +
  25761. +static inline int xmit_rec_read(void)
  25762. +{
  25763. + return current->xmit_recursion;
  25764. +}
  25765. +
  25766. +static inline void xmit_rec_inc(void)
  25767. +{
  25768. + current->xmit_recursion++;
  25769. +}
  25770. +
  25771. +static inline void xmit_rec_dec(void)
  25772. +{
  25773. + current->xmit_recursion--;
  25774. +}
  25775. +
  25776. +#else
  25777. +
  25778. DEFINE_PER_CPU(int, xmit_recursion);
  25779. EXPORT_SYMBOL(xmit_recursion);
  25780. +static inline int xmit_rec_read(void)
  25781. +{
  25782. + return __this_cpu_read(xmit_recursion);
  25783. +}
  25784. +
  25785. +static inline void xmit_rec_inc(void)
  25786. +{
  25787. + __this_cpu_inc(xmit_recursion);
  25788. +}
  25789. +
  25790. +static inline int xmit_rec_dec(void)
  25791. +{
  25792. + __this_cpu_dec(xmit_recursion);
  25793. +}
  25794. +#endif
  25795. +
  25796. #define RECURSION_LIMIT 10
  25797. /**
  25798. @@ -2984,7 +3027,7 @@
  25799. if (txq->xmit_lock_owner != cpu) {
  25800. - if (__this_cpu_read(xmit_recursion) > RECURSION_LIMIT)
  25801. + if (xmit_rec_read() > RECURSION_LIMIT)
  25802. goto recursion_alert;
  25803. skb = validate_xmit_skb(skb, dev);
  25804. @@ -2994,9 +3037,9 @@
  25805. HARD_TX_LOCK(dev, txq, cpu);
  25806. if (!netif_xmit_stopped(txq)) {
  25807. - __this_cpu_inc(xmit_recursion);
  25808. + xmit_rec_inc();
  25809. skb = dev_hard_start_xmit(skb, dev, txq, &rc);
  25810. - __this_cpu_dec(xmit_recursion);
  25811. + xmit_rec_dec();
  25812. if (dev_xmit_complete(rc)) {
  25813. HARD_TX_UNLOCK(dev, txq);
  25814. goto out;
  25815. @@ -3370,6 +3413,7 @@
  25816. rps_unlock(sd);
  25817. local_irq_restore(flags);
  25818. + preempt_check_resched_rt();
  25819. atomic_long_inc(&skb->dev->rx_dropped);
  25820. kfree_skb(skb);
  25821. @@ -3388,7 +3432,7 @@
  25822. struct rps_dev_flow voidflow, *rflow = &voidflow;
  25823. int cpu;
  25824. - preempt_disable();
  25825. + migrate_disable();
  25826. rcu_read_lock();
  25827. cpu = get_rps_cpu(skb->dev, skb, &rflow);
  25828. @@ -3398,13 +3442,13 @@
  25829. ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
  25830. rcu_read_unlock();
  25831. - preempt_enable();
  25832. + migrate_enable();
  25833. } else
  25834. #endif
  25835. {
  25836. unsigned int qtail;
  25837. - ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
  25838. - put_cpu();
  25839. + ret = enqueue_to_backlog(skb, get_cpu_light(), &qtail);
  25840. + put_cpu_light();
  25841. }
  25842. return ret;
  25843. }
  25844. @@ -3438,16 +3482,44 @@
  25845. trace_netif_rx_ni_entry(skb);
  25846. - preempt_disable();
  25847. + local_bh_disable();
  25848. err = netif_rx_internal(skb);
  25849. - if (local_softirq_pending())
  25850. - do_softirq();
  25851. - preempt_enable();
  25852. + local_bh_enable();
  25853. return err;
  25854. }
  25855. EXPORT_SYMBOL(netif_rx_ni);
  25856. +#ifdef CONFIG_PREEMPT_RT_FULL
  25857. +/*
  25858. + * RT runs ksoftirqd as a real time thread and the root_lock is a
  25859. + * "sleeping spinlock". If the trylock fails then we can go into an
  25860. + * infinite loop when ksoftirqd preempted the task which actually
  25861. + * holds the lock, because we requeue q and raise NET_TX softirq
  25862. + * causing ksoftirqd to loop forever.
  25863. + *
  25864. + * It's safe to use spin_lock on RT here as softirqs run in thread
  25865. + * context and cannot deadlock against the thread which is holding
  25866. + * root_lock.
  25867. + *
  25868. + * On !RT the trylock might fail, but there we bail out from the
  25869. + * softirq loop after 10 attempts which we can't do on RT. And the
  25870. + * task holding root_lock cannot be preempted, so the only downside of
  25871. + * that trylock is that we need 10 loops to decide that we should have
  25872. + * given up in the first one :)
  25873. + */
  25874. +static inline int take_root_lock(spinlock_t *lock)
  25875. +{
  25876. + spin_lock(lock);
  25877. + return 1;
  25878. +}
  25879. +#else
  25880. +static inline int take_root_lock(spinlock_t *lock)
  25881. +{
  25882. + return spin_trylock(lock);
  25883. +}
  25884. +#endif
  25885. +
  25886. static void net_tx_action(struct softirq_action *h)
  25887. {
  25888. struct softnet_data *sd = this_cpu_ptr(&softnet_data);
  25889. @@ -3489,7 +3561,7 @@
  25890. head = head->next_sched;
  25891. root_lock = qdisc_lock(q);
  25892. - if (spin_trylock(root_lock)) {
  25893. + if (take_root_lock(root_lock)) {
  25894. smp_mb__before_atomic();
  25895. clear_bit(__QDISC_STATE_SCHED,
  25896. &q->state);
  25897. @@ -3886,7 +3958,7 @@
  25898. skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
  25899. if (skb->dev == dev) {
  25900. __skb_unlink(skb, &sd->input_pkt_queue);
  25901. - kfree_skb(skb);
  25902. + __skb_queue_tail(&sd->tofree_queue, skb);
  25903. input_queue_head_incr(sd);
  25904. }
  25905. }
  25906. @@ -3895,10 +3967,13 @@
  25907. skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
  25908. if (skb->dev == dev) {
  25909. __skb_unlink(skb, &sd->process_queue);
  25910. - kfree_skb(skb);
  25911. + __skb_queue_tail(&sd->tofree_queue, skb);
  25912. input_queue_head_incr(sd);
  25913. }
  25914. }
  25915. +
  25916. + if (!skb_queue_empty(&sd->tofree_queue))
  25917. + raise_softirq_irqoff(NET_RX_SOFTIRQ);
  25918. }
  25919. static int napi_gro_complete(struct sk_buff *skb)
  25920. @@ -4349,6 +4424,7 @@
  25921. sd->rps_ipi_list = NULL;
  25922. local_irq_enable();
  25923. + preempt_check_resched_rt();
  25924. /* Send pending IPI's to kick RPS processing on remote cpus. */
  25925. while (remsd) {
  25926. @@ -4362,6 +4438,7 @@
  25927. } else
  25928. #endif
  25929. local_irq_enable();
  25930. + preempt_check_resched_rt();
  25931. }
  25932. static bool sd_has_rps_ipi_waiting(struct softnet_data *sd)
  25933. @@ -4443,6 +4520,7 @@
  25934. local_irq_save(flags);
  25935. ____napi_schedule(this_cpu_ptr(&softnet_data), n);
  25936. local_irq_restore(flags);
  25937. + preempt_check_resched_rt();
  25938. }
  25939. EXPORT_SYMBOL(__napi_schedule);
  25940. @@ -4717,7 +4795,7 @@
  25941. list_splice_tail(&repoll, &list);
  25942. list_splice(&list, &sd->poll_list);
  25943. if (!list_empty(&sd->poll_list))
  25944. - __raise_softirq_irqoff(NET_RX_SOFTIRQ);
  25945. + __raise_softirq_irqoff_ksoft(NET_RX_SOFTIRQ);
  25946. net_rps_action_and_irq_enable(sd);
  25947. }
  25948. @@ -6931,7 +7009,7 @@
  25949. void synchronize_net(void)
  25950. {
  25951. might_sleep();
  25952. - if (rtnl_is_locked())
  25953. + if (rtnl_is_locked() && !IS_ENABLED(CONFIG_PREEMPT_RT_FULL))
  25954. synchronize_rcu_expedited();
  25955. else
  25956. synchronize_rcu();
  25957. @@ -7172,16 +7250,20 @@
  25958. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  25959. local_irq_enable();
  25960. + preempt_check_resched_rt();
  25961. /* Process offline CPU's input_pkt_queue */
  25962. while ((skb = __skb_dequeue(&oldsd->process_queue))) {
  25963. netif_rx_ni(skb);
  25964. input_queue_head_incr(oldsd);
  25965. }
  25966. - while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) {
  25967. + while ((skb = __skb_dequeue(&oldsd->input_pkt_queue))) {
  25968. netif_rx_ni(skb);
  25969. input_queue_head_incr(oldsd);
  25970. }
  25971. + while ((skb = __skb_dequeue(&oldsd->tofree_queue))) {
  25972. + kfree_skb(skb);
  25973. + }
  25974. return NOTIFY_OK;
  25975. }
  25976. @@ -7483,8 +7565,9 @@
  25977. for_each_possible_cpu(i) {
  25978. struct softnet_data *sd = &per_cpu(softnet_data, i);
  25979. - skb_queue_head_init(&sd->input_pkt_queue);
  25980. - skb_queue_head_init(&sd->process_queue);
  25981. + skb_queue_head_init_raw(&sd->input_pkt_queue);
  25982. + skb_queue_head_init_raw(&sd->process_queue);
  25983. + skb_queue_head_init_raw(&sd->tofree_queue);
  25984. INIT_LIST_HEAD(&sd->poll_list);
  25985. sd->output_queue_tailp = &sd->output_queue;
  25986. #ifdef CONFIG_RPS
  25987. diff -Nur linux-4.1.20.orig/net/core/skbuff.c linux-4.1.20/net/core/skbuff.c
  25988. --- linux-4.1.20.orig/net/core/skbuff.c 2016-03-17 19:11:03.000000000 +0100
  25989. +++ linux-4.1.20/net/core/skbuff.c 2016-03-21 20:18:34.000000000 +0100
  25990. @@ -63,6 +63,7 @@
  25991. #include <linux/errqueue.h>
  25992. #include <linux/prefetch.h>
  25993. #include <linux/if_vlan.h>
  25994. +#include <linux/locallock.h>
  25995. #include <net/protocol.h>
  25996. #include <net/dst.h>
  25997. @@ -358,6 +359,8 @@
  25998. };
  25999. static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache);
  26000. static DEFINE_PER_CPU(struct netdev_alloc_cache, napi_alloc_cache);
  26001. +static DEFINE_LOCAL_IRQ_LOCK(netdev_alloc_lock);
  26002. +static DEFINE_LOCAL_IRQ_LOCK(napi_alloc_cache_lock);
  26003. static struct page *__page_frag_refill(struct netdev_alloc_cache *nc,
  26004. gfp_t gfp_mask)
  26005. @@ -435,9 +438,9 @@
  26006. unsigned long flags;
  26007. void *data;
  26008. - local_irq_save(flags);
  26009. + local_lock_irqsave(netdev_alloc_lock, flags);
  26010. data = __alloc_page_frag(&netdev_alloc_cache, fragsz, gfp_mask);
  26011. - local_irq_restore(flags);
  26012. + local_unlock_irqrestore(netdev_alloc_lock, flags);
  26013. return data;
  26014. }
  26015. @@ -456,7 +459,12 @@
  26016. static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
  26017. {
  26018. - return __alloc_page_frag(&napi_alloc_cache, fragsz, gfp_mask);
  26019. + void *data;
  26020. +
  26021. + local_lock(napi_alloc_cache_lock);
  26022. + data = __alloc_page_frag(&napi_alloc_cache, fragsz, gfp_mask);
  26023. + local_unlock(napi_alloc_cache_lock);
  26024. + return data;
  26025. }
  26026. void *napi_alloc_frag(unsigned int fragsz)
  26027. diff -Nur linux-4.1.20.orig/net/core/sock.c linux-4.1.20/net/core/sock.c
  26028. --- linux-4.1.20.orig/net/core/sock.c 2016-03-17 19:11:03.000000000 +0100
  26029. +++ linux-4.1.20/net/core/sock.c 2016-03-21 20:18:34.000000000 +0100
  26030. @@ -2369,12 +2369,11 @@
  26031. if (sk->sk_lock.owned)
  26032. __lock_sock(sk);
  26033. sk->sk_lock.owned = 1;
  26034. - spin_unlock(&sk->sk_lock.slock);
  26035. + spin_unlock_bh(&sk->sk_lock.slock);
  26036. /*
  26037. * The sk_lock has mutex_lock() semantics here:
  26038. */
  26039. mutex_acquire(&sk->sk_lock.dep_map, subclass, 0, _RET_IP_);
  26040. - local_bh_enable();
  26041. }
  26042. EXPORT_SYMBOL(lock_sock_nested);
  26043. diff -Nur linux-4.1.20.orig/net/ipv4/icmp.c linux-4.1.20/net/ipv4/icmp.c
  26044. --- linux-4.1.20.orig/net/ipv4/icmp.c 2016-03-17 19:11:03.000000000 +0100
  26045. +++ linux-4.1.20/net/ipv4/icmp.c 2016-03-21 20:18:34.000000000 +0100
  26046. @@ -69,6 +69,7 @@
  26047. #include <linux/jiffies.h>
  26048. #include <linux/kernel.h>
  26049. #include <linux/fcntl.h>
  26050. +#include <linux/sysrq.h>
  26051. #include <linux/socket.h>
  26052. #include <linux/in.h>
  26053. #include <linux/inet.h>
  26054. @@ -867,6 +868,30 @@
  26055. }
  26056. /*
  26057. + * 32bit and 64bit have different timestamp length, so we check for
  26058. + * the cookie at offset 20 and verify it is repeated at offset 50
  26059. + */
  26060. +#define CO_POS0 20
  26061. +#define CO_POS1 50
  26062. +#define CO_SIZE sizeof(int)
  26063. +#define ICMP_SYSRQ_SIZE 57
  26064. +
  26065. +/*
  26066. + * We got a ICMP_SYSRQ_SIZE sized ping request. Check for the cookie
  26067. + * pattern and if it matches send the next byte as a trigger to sysrq.
  26068. + */
  26069. +static void icmp_check_sysrq(struct net *net, struct sk_buff *skb)
  26070. +{
  26071. + int cookie = htonl(net->ipv4.sysctl_icmp_echo_sysrq);
  26072. + char *p = skb->data;
  26073. +
  26074. + if (!memcmp(&cookie, p + CO_POS0, CO_SIZE) &&
  26075. + !memcmp(&cookie, p + CO_POS1, CO_SIZE) &&
  26076. + p[CO_POS0 + CO_SIZE] == p[CO_POS1 + CO_SIZE])
  26077. + handle_sysrq(p[CO_POS0 + CO_SIZE]);
  26078. +}
  26079. +
  26080. +/*
  26081. * Handle ICMP_ECHO ("ping") requests.
  26082. *
  26083. * RFC 1122: 3.2.2.6 MUST have an echo server that answers ICMP echo
  26084. @@ -893,6 +918,11 @@
  26085. icmp_param.data_len = skb->len;
  26086. icmp_param.head_len = sizeof(struct icmphdr);
  26087. icmp_reply(&icmp_param, skb);
  26088. +
  26089. + if (skb->len == ICMP_SYSRQ_SIZE &&
  26090. + net->ipv4.sysctl_icmp_echo_sysrq) {
  26091. + icmp_check_sysrq(net, skb);
  26092. + }
  26093. }
  26094. /* should there be an ICMP stat for ignored echos? */
  26095. return true;
  26096. diff -Nur linux-4.1.20.orig/net/ipv4/sysctl_net_ipv4.c linux-4.1.20/net/ipv4/sysctl_net_ipv4.c
  26097. --- linux-4.1.20.orig/net/ipv4/sysctl_net_ipv4.c 2016-03-17 19:11:03.000000000 +0100
  26098. +++ linux-4.1.20/net/ipv4/sysctl_net_ipv4.c 2016-03-21 20:18:34.000000000 +0100
  26099. @@ -779,6 +779,13 @@
  26100. .proc_handler = proc_dointvec
  26101. },
  26102. {
  26103. + .procname = "icmp_echo_sysrq",
  26104. + .data = &init_net.ipv4.sysctl_icmp_echo_sysrq,
  26105. + .maxlen = sizeof(int),
  26106. + .mode = 0644,
  26107. + .proc_handler = proc_dointvec
  26108. + },
  26109. + {
  26110. .procname = "icmp_ignore_bogus_error_responses",
  26111. .data = &init_net.ipv4.sysctl_icmp_ignore_bogus_error_responses,
  26112. .maxlen = sizeof(int),
  26113. diff -Nur linux-4.1.20.orig/net/mac80211/rx.c linux-4.1.20/net/mac80211/rx.c
  26114. --- linux-4.1.20.orig/net/mac80211/rx.c 2016-03-17 19:11:03.000000000 +0100
  26115. +++ linux-4.1.20/net/mac80211/rx.c 2016-03-21 20:18:34.000000000 +0100
  26116. @@ -3554,7 +3554,7 @@
  26117. struct ieee80211_supported_band *sband;
  26118. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  26119. - WARN_ON_ONCE(softirq_count() == 0);
  26120. + WARN_ON_ONCE_NONRT(softirq_count() == 0);
  26121. if (WARN_ON(status->band >= IEEE80211_NUM_BANDS))
  26122. goto drop;
  26123. diff -Nur linux-4.1.20.orig/net/netfilter/core.c linux-4.1.20/net/netfilter/core.c
  26124. --- linux-4.1.20.orig/net/netfilter/core.c 2016-03-17 19:11:03.000000000 +0100
  26125. +++ linux-4.1.20/net/netfilter/core.c 2016-03-21 20:18:34.000000000 +0100
  26126. @@ -22,11 +22,17 @@
  26127. #include <linux/proc_fs.h>
  26128. #include <linux/mutex.h>
  26129. #include <linux/slab.h>
  26130. +#include <linux/locallock.h>
  26131. #include <net/net_namespace.h>
  26132. #include <net/sock.h>
  26133. #include "nf_internals.h"
  26134. +#ifdef CONFIG_PREEMPT_RT_BASE
  26135. +DEFINE_LOCAL_IRQ_LOCK(xt_write_lock);
  26136. +EXPORT_PER_CPU_SYMBOL(xt_write_lock);
  26137. +#endif
  26138. +
  26139. static DEFINE_MUTEX(afinfo_mutex);
  26140. const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO] __read_mostly;
  26141. diff -Nur linux-4.1.20.orig/net/packet/af_packet.c linux-4.1.20/net/packet/af_packet.c
  26142. --- linux-4.1.20.orig/net/packet/af_packet.c 2016-03-17 19:11:03.000000000 +0100
  26143. +++ linux-4.1.20/net/packet/af_packet.c 2016-03-21 20:18:34.000000000 +0100
  26144. @@ -63,6 +63,7 @@
  26145. #include <linux/if_packet.h>
  26146. #include <linux/wireless.h>
  26147. #include <linux/kernel.h>
  26148. +#include <linux/delay.h>
  26149. #include <linux/kmod.h>
  26150. #include <linux/slab.h>
  26151. #include <linux/vmalloc.h>
  26152. @@ -698,7 +699,7 @@
  26153. if (BLOCK_NUM_PKTS(pbd)) {
  26154. while (atomic_read(&pkc->blk_fill_in_prog)) {
  26155. /* Waiting for skb_copy_bits to finish... */
  26156. - cpu_relax();
  26157. + cpu_chill();
  26158. }
  26159. }
  26160. @@ -960,7 +961,7 @@
  26161. if (!(status & TP_STATUS_BLK_TMO)) {
  26162. while (atomic_read(&pkc->blk_fill_in_prog)) {
  26163. /* Waiting for skb_copy_bits to finish... */
  26164. - cpu_relax();
  26165. + cpu_chill();
  26166. }
  26167. }
  26168. prb_close_block(pkc, pbd, po, status);
  26169. diff -Nur linux-4.1.20.orig/net/rds/ib_rdma.c linux-4.1.20/net/rds/ib_rdma.c
  26170. --- linux-4.1.20.orig/net/rds/ib_rdma.c 2016-03-17 19:11:03.000000000 +0100
  26171. +++ linux-4.1.20/net/rds/ib_rdma.c 2016-03-21 20:18:34.000000000 +0100
  26172. @@ -34,6 +34,7 @@
  26173. #include <linux/slab.h>
  26174. #include <linux/rculist.h>
  26175. #include <linux/llist.h>
  26176. +#include <linux/delay.h>
  26177. #include "rds.h"
  26178. #include "ib.h"
  26179. @@ -286,7 +287,7 @@
  26180. for_each_online_cpu(cpu) {
  26181. flag = &per_cpu(clean_list_grace, cpu);
  26182. while (test_bit(CLEAN_LIST_BUSY_BIT, flag))
  26183. - cpu_relax();
  26184. + cpu_chill();
  26185. }
  26186. }
  26187. diff -Nur linux-4.1.20.orig/net/sched/sch_generic.c linux-4.1.20/net/sched/sch_generic.c
  26188. --- linux-4.1.20.orig/net/sched/sch_generic.c 2016-03-17 19:11:03.000000000 +0100
  26189. +++ linux-4.1.20/net/sched/sch_generic.c 2016-03-21 20:18:34.000000000 +0100
  26190. @@ -896,7 +896,7 @@
  26191. /* Wait for outstanding qdisc_run calls. */
  26192. list_for_each_entry(dev, head, close_list)
  26193. while (some_qdisc_is_busy(dev))
  26194. - yield();
  26195. + msleep(1);
  26196. }
  26197. void dev_deactivate(struct net_device *dev)
  26198. diff -Nur linux-4.1.20.orig/net/sunrpc/svc_xprt.c linux-4.1.20/net/sunrpc/svc_xprt.c
  26199. --- linux-4.1.20.orig/net/sunrpc/svc_xprt.c 2016-03-17 19:11:03.000000000 +0100
  26200. +++ linux-4.1.20/net/sunrpc/svc_xprt.c 2016-03-21 20:18:34.000000000 +0100
  26201. @@ -341,7 +341,7 @@
  26202. goto out;
  26203. }
  26204. - cpu = get_cpu();
  26205. + cpu = get_cpu_light();
  26206. pool = svc_pool_for_cpu(xprt->xpt_server, cpu);
  26207. atomic_long_inc(&pool->sp_stats.packets);
  26208. @@ -377,7 +377,7 @@
  26209. atomic_long_inc(&pool->sp_stats.threads_woken);
  26210. wake_up_process(rqstp->rq_task);
  26211. - put_cpu();
  26212. + put_cpu_light();
  26213. goto out;
  26214. }
  26215. rcu_read_unlock();
  26216. @@ -398,7 +398,7 @@
  26217. goto redo_search;
  26218. }
  26219. rqstp = NULL;
  26220. - put_cpu();
  26221. + put_cpu_light();
  26222. out:
  26223. trace_svc_xprt_do_enqueue(xprt, rqstp);
  26224. }
  26225. diff -Nur linux-4.1.20.orig/scripts/mkcompile_h linux-4.1.20/scripts/mkcompile_h
  26226. --- linux-4.1.20.orig/scripts/mkcompile_h 2016-03-17 19:11:03.000000000 +0100
  26227. +++ linux-4.1.20/scripts/mkcompile_h 2016-03-21 20:18:34.000000000 +0100
  26228. @@ -4,7 +4,8 @@
  26229. ARCH=$2
  26230. SMP=$3
  26231. PREEMPT=$4
  26232. -CC=$5
  26233. +RT=$5
  26234. +CC=$6
  26235. vecho() { [ "${quiet}" = "silent_" ] || echo "$@" ; }
  26236. @@ -57,6 +58,7 @@
  26237. CONFIG_FLAGS=""
  26238. if [ -n "$SMP" ] ; then CONFIG_FLAGS="SMP"; fi
  26239. if [ -n "$PREEMPT" ] ; then CONFIG_FLAGS="$CONFIG_FLAGS PREEMPT"; fi
  26240. +if [ -n "$RT" ] ; then CONFIG_FLAGS="$CONFIG_FLAGS RT"; fi
  26241. UTS_VERSION="$UTS_VERSION $CONFIG_FLAGS $TIMESTAMP"
  26242. # Truncate to maximum length
  26243. diff -Nur linux-4.1.20.orig/sound/core/pcm_native.c linux-4.1.20/sound/core/pcm_native.c
  26244. --- linux-4.1.20.orig/sound/core/pcm_native.c 2016-03-17 19:11:03.000000000 +0100
  26245. +++ linux-4.1.20/sound/core/pcm_native.c 2016-03-21 20:18:34.000000000 +0100
  26246. @@ -135,7 +135,7 @@
  26247. void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
  26248. {
  26249. if (!substream->pcm->nonatomic)
  26250. - local_irq_disable();
  26251. + local_irq_disable_nort();
  26252. snd_pcm_stream_lock(substream);
  26253. }
  26254. EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
  26255. @@ -150,7 +150,7 @@
  26256. {
  26257. snd_pcm_stream_unlock(substream);
  26258. if (!substream->pcm->nonatomic)
  26259. - local_irq_enable();
  26260. + local_irq_enable_nort();
  26261. }
  26262. EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
  26263. @@ -158,7 +158,7 @@
  26264. {
  26265. unsigned long flags = 0;
  26266. if (!substream->pcm->nonatomic)
  26267. - local_irq_save(flags);
  26268. + local_irq_save_nort(flags);
  26269. snd_pcm_stream_lock(substream);
  26270. return flags;
  26271. }
  26272. @@ -176,7 +176,7 @@
  26273. {
  26274. snd_pcm_stream_unlock(substream);
  26275. if (!substream->pcm->nonatomic)
  26276. - local_irq_restore(flags);
  26277. + local_irq_restore_nort(flags);
  26278. }
  26279. EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
  26280. 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
  26281. --- linux-4.1.20.orig/sound/soc/intel/atom/sst/sst.c 2016-03-17 19:11:03.000000000 +0100
  26282. +++ linux-4.1.20/sound/soc/intel/atom/sst/sst.c 2016-03-21 20:18:34.000000000 +0100
  26283. @@ -368,8 +368,8 @@
  26284. * initialize by FW or driver when firmware is loaded
  26285. */
  26286. spin_lock_irqsave(&ctx->ipc_spin_lock, irq_flags);
  26287. - sst_shim_write64(shim, SST_IMRX, shim_regs->imrx),
  26288. - sst_shim_write64(shim, SST_CSR, shim_regs->csr),
  26289. + sst_shim_write64(shim, SST_IMRX, shim_regs->imrx);
  26290. + sst_shim_write64(shim, SST_CSR, shim_regs->csr);
  26291. spin_unlock_irqrestore(&ctx->ipc_spin_lock, irq_flags);
  26292. }
  26293. diff -Nur linux-4.1.20.orig/virt/kvm/async_pf.c linux-4.1.20/virt/kvm/async_pf.c
  26294. --- linux-4.1.20.orig/virt/kvm/async_pf.c 2016-03-17 19:11:03.000000000 +0100
  26295. +++ linux-4.1.20/virt/kvm/async_pf.c 2016-03-21 20:18:34.000000000 +0100
  26296. @@ -94,8 +94,8 @@
  26297. trace_kvm_async_pf_completed(addr, gva);
  26298. - if (waitqueue_active(&vcpu->wq))
  26299. - wake_up_interruptible(&vcpu->wq);
  26300. + if (swaitqueue_active(&vcpu->wq))
  26301. + swait_wake_interruptible(&vcpu->wq);
  26302. mmput(mm);
  26303. kvm_put_kvm(vcpu->kvm);
  26304. diff -Nur linux-4.1.20.orig/virt/kvm/kvm_main.c linux-4.1.20/virt/kvm/kvm_main.c
  26305. --- linux-4.1.20.orig/virt/kvm/kvm_main.c 2016-03-17 19:11:03.000000000 +0100
  26306. +++ linux-4.1.20/virt/kvm/kvm_main.c 2016-03-21 20:18:34.000000000 +0100
  26307. @@ -218,7 +218,7 @@
  26308. vcpu->kvm = kvm;
  26309. vcpu->vcpu_id = id;
  26310. vcpu->pid = NULL;
  26311. - init_waitqueue_head(&vcpu->wq);
  26312. + init_swait_head(&vcpu->wq);
  26313. kvm_async_pf_vcpu_init(vcpu);
  26314. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  26315. @@ -1779,7 +1779,7 @@
  26316. void kvm_vcpu_block(struct kvm_vcpu *vcpu)
  26317. {
  26318. ktime_t start, cur;
  26319. - DEFINE_WAIT(wait);
  26320. + DEFINE_SWAITER(wait);
  26321. bool waited = false;
  26322. start = cur = ktime_get();
  26323. @@ -1800,7 +1800,7 @@
  26324. }
  26325. for (;;) {
  26326. - prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
  26327. + swait_prepare(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
  26328. if (kvm_vcpu_check_block(vcpu) < 0)
  26329. break;
  26330. @@ -1809,7 +1809,7 @@
  26331. schedule();
  26332. }
  26333. - finish_wait(&vcpu->wq, &wait);
  26334. + swait_finish(&vcpu->wq, &wait);
  26335. cur = ktime_get();
  26336. out:
  26337. @@ -1825,11 +1825,11 @@
  26338. {
  26339. int me;
  26340. int cpu = vcpu->cpu;
  26341. - wait_queue_head_t *wqp;
  26342. + struct swait_head *wqp;
  26343. wqp = kvm_arch_vcpu_wq(vcpu);
  26344. - if (waitqueue_active(wqp)) {
  26345. - wake_up_interruptible(wqp);
  26346. + if (swaitqueue_active(wqp)) {
  26347. + swait_wake_interruptible(wqp);
  26348. ++vcpu->stat.halt_wakeup;
  26349. }
  26350. @@ -1930,7 +1930,7 @@
  26351. continue;
  26352. if (vcpu == me)
  26353. continue;
  26354. - if (waitqueue_active(&vcpu->wq) && !kvm_arch_vcpu_runnable(vcpu))
  26355. + if (swaitqueue_active(&vcpu->wq) && !kvm_arch_vcpu_runnable(vcpu))
  26356. continue;
  26357. if (!kvm_vcpu_eligible_for_directed_yield(vcpu))
  26358. continue;