patch-realtime 767 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970
  1. diff -Nur linux-4.9.28.orig/arch/arm/include/asm/irq.h linux-4.9.28/arch/arm/include/asm/irq.h
  2. --- linux-4.9.28.orig/arch/arm/include/asm/irq.h 2017-05-14 14:00:37.000000000 +0200
  3. +++ linux-4.9.28/arch/arm/include/asm/irq.h 2017-05-19 03:37:25.122174217 +0200
  4. @@ -22,6 +22,8 @@
  5. #endif
  6. #ifndef __ASSEMBLY__
  7. +#include <linux/cpumask.h>
  8. +
  9. struct irqaction;
  10. struct pt_regs;
  11. extern void migrate_irqs(void);
  12. diff -Nur linux-4.9.28.orig/arch/arm/include/asm/switch_to.h linux-4.9.28/arch/arm/include/asm/switch_to.h
  13. --- linux-4.9.28.orig/arch/arm/include/asm/switch_to.h 2017-05-14 14:00:37.000000000 +0200
  14. +++ linux-4.9.28/arch/arm/include/asm/switch_to.h 2017-05-19 03:37:25.122174217 +0200
  15. @@ -3,6 +3,13 @@
  16. #include <linux/thread_info.h>
  17. +#if defined CONFIG_PREEMPT_RT_FULL && defined CONFIG_HIGHMEM
  18. +void switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p);
  19. +#else
  20. +static inline void
  21. +switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p) { }
  22. +#endif
  23. +
  24. /*
  25. * For v7 SMP cores running a preemptible kernel we may be pre-empted
  26. * during a TLB maintenance operation, so execute an inner-shareable dsb
  27. @@ -25,6 +32,7 @@
  28. #define switch_to(prev,next,last) \
  29. do { \
  30. __complete_pending_tlbi(); \
  31. + switch_kmaps(prev, next); \
  32. last = __switch_to(prev,task_thread_info(prev), task_thread_info(next)); \
  33. } while (0)
  34. diff -Nur linux-4.9.28.orig/arch/arm/include/asm/thread_info.h linux-4.9.28/arch/arm/include/asm/thread_info.h
  35. --- linux-4.9.28.orig/arch/arm/include/asm/thread_info.h 2017-05-14 14:00:37.000000000 +0200
  36. +++ linux-4.9.28/arch/arm/include/asm/thread_info.h 2017-05-19 03:37:25.122174217 +0200
  37. @@ -49,6 +49,7 @@
  38. struct thread_info {
  39. unsigned long flags; /* low level flags */
  40. int preempt_count; /* 0 => preemptable, <0 => bug */
  41. + int preempt_lazy_count; /* 0 => preemptable, <0 => bug */
  42. mm_segment_t addr_limit; /* address limit */
  43. struct task_struct *task; /* main task structure */
  44. __u32 cpu; /* cpu */
  45. @@ -142,7 +143,8 @@
  46. #define TIF_SYSCALL_TRACE 4 /* syscall trace active */
  47. #define TIF_SYSCALL_AUDIT 5 /* syscall auditing active */
  48. #define TIF_SYSCALL_TRACEPOINT 6 /* syscall tracepoint instrumentation */
  49. -#define TIF_SECCOMP 7 /* seccomp syscall filtering active */
  50. +#define TIF_SECCOMP 8 /* seccomp syscall filtering active */
  51. +#define TIF_NEED_RESCHED_LAZY 7
  52. #define TIF_NOHZ 12 /* in adaptive nohz mode */
  53. #define TIF_USING_IWMMXT 17
  54. @@ -152,6 +154,7 @@
  55. #define _TIF_SIGPENDING (1 << TIF_SIGPENDING)
  56. #define _TIF_NEED_RESCHED (1 << TIF_NEED_RESCHED)
  57. #define _TIF_NOTIFY_RESUME (1 << TIF_NOTIFY_RESUME)
  58. +#define _TIF_NEED_RESCHED_LAZY (1 << TIF_NEED_RESCHED_LAZY)
  59. #define _TIF_UPROBE (1 << TIF_UPROBE)
  60. #define _TIF_SYSCALL_TRACE (1 << TIF_SYSCALL_TRACE)
  61. #define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
  62. @@ -167,7 +170,8 @@
  63. * Change these and you break ASM code in entry-common.S
  64. */
  65. #define _TIF_WORK_MASK (_TIF_NEED_RESCHED | _TIF_SIGPENDING | \
  66. - _TIF_NOTIFY_RESUME | _TIF_UPROBE)
  67. + _TIF_NOTIFY_RESUME | _TIF_UPROBE | \
  68. + _TIF_NEED_RESCHED_LAZY)
  69. #endif /* __KERNEL__ */
  70. #endif /* __ASM_ARM_THREAD_INFO_H */
  71. diff -Nur linux-4.9.28.orig/arch/arm/Kconfig linux-4.9.28/arch/arm/Kconfig
  72. --- linux-4.9.28.orig/arch/arm/Kconfig 2017-05-14 14:00:37.000000000 +0200
  73. +++ linux-4.9.28/arch/arm/Kconfig 2017-05-19 03:37:25.122174217 +0200
  74. @@ -36,7 +36,7 @@
  75. select HAVE_ARCH_AUDITSYSCALL if (AEABI && !OABI_COMPAT)
  76. select HAVE_ARCH_BITREVERSE if (CPU_32v7M || CPU_32v7) && !CPU_32v6
  77. select HAVE_ARCH_HARDENED_USERCOPY
  78. - select HAVE_ARCH_JUMP_LABEL if !XIP_KERNEL && !CPU_ENDIAN_BE32 && MMU
  79. + select HAVE_ARCH_JUMP_LABEL if !XIP_KERNEL && !CPU_ENDIAN_BE32 && MMU && !PREEMPT_RT_BASE
  80. select HAVE_ARCH_KGDB if !CPU_ENDIAN_BE32 && MMU
  81. select HAVE_ARCH_MMAP_RND_BITS if MMU
  82. select HAVE_ARCH_SECCOMP_FILTER if (AEABI && !OABI_COMPAT)
  83. @@ -75,6 +75,7 @@
  84. select HAVE_PERF_EVENTS
  85. select HAVE_PERF_REGS
  86. select HAVE_PERF_USER_STACK_DUMP
  87. + select HAVE_PREEMPT_LAZY
  88. select HAVE_RCU_TABLE_FREE if (SMP && ARM_LPAE)
  89. select HAVE_REGS_AND_STACK_ACCESS_API
  90. select HAVE_SYSCALL_TRACEPOINTS
  91. diff -Nur linux-4.9.28.orig/arch/arm/kernel/asm-offsets.c linux-4.9.28/arch/arm/kernel/asm-offsets.c
  92. --- linux-4.9.28.orig/arch/arm/kernel/asm-offsets.c 2017-05-14 14:00:37.000000000 +0200
  93. +++ linux-4.9.28/arch/arm/kernel/asm-offsets.c 2017-05-19 03:37:25.122174217 +0200
  94. @@ -65,6 +65,7 @@
  95. BLANK();
  96. DEFINE(TI_FLAGS, offsetof(struct thread_info, flags));
  97. DEFINE(TI_PREEMPT, offsetof(struct thread_info, preempt_count));
  98. + DEFINE(TI_PREEMPT_LAZY, offsetof(struct thread_info, preempt_lazy_count));
  99. DEFINE(TI_ADDR_LIMIT, offsetof(struct thread_info, addr_limit));
  100. DEFINE(TI_TASK, offsetof(struct thread_info, task));
  101. DEFINE(TI_CPU, offsetof(struct thread_info, cpu));
  102. diff -Nur linux-4.9.28.orig/arch/arm/kernel/entry-armv.S linux-4.9.28/arch/arm/kernel/entry-armv.S
  103. --- linux-4.9.28.orig/arch/arm/kernel/entry-armv.S 2017-05-14 14:00:37.000000000 +0200
  104. +++ linux-4.9.28/arch/arm/kernel/entry-armv.S 2017-05-19 03:37:25.122174217 +0200
  105. @@ -220,11 +220,18 @@
  106. #ifdef CONFIG_PREEMPT
  107. ldr r8, [tsk, #TI_PREEMPT] @ get preempt count
  108. - ldr r0, [tsk, #TI_FLAGS] @ get flags
  109. teq r8, #0 @ if preempt count != 0
  110. + bne 1f @ return from exeption
  111. + ldr r0, [tsk, #TI_FLAGS] @ get flags
  112. + tst r0, #_TIF_NEED_RESCHED @ if NEED_RESCHED is set
  113. + blne svc_preempt @ preempt!
  114. +
  115. + ldr r8, [tsk, #TI_PREEMPT_LAZY] @ get preempt lazy count
  116. + teq r8, #0 @ if preempt lazy count != 0
  117. movne r0, #0 @ force flags to 0
  118. - tst r0, #_TIF_NEED_RESCHED
  119. + tst r0, #_TIF_NEED_RESCHED_LAZY
  120. blne svc_preempt
  121. +1:
  122. #endif
  123. svc_exit r5, irq = 1 @ return from exception
  124. @@ -239,8 +246,14 @@
  125. 1: bl preempt_schedule_irq @ irq en/disable is done inside
  126. ldr r0, [tsk, #TI_FLAGS] @ get new tasks TI_FLAGS
  127. tst r0, #_TIF_NEED_RESCHED
  128. + bne 1b
  129. + tst r0, #_TIF_NEED_RESCHED_LAZY
  130. reteq r8 @ go again
  131. - b 1b
  132. + ldr r0, [tsk, #TI_PREEMPT_LAZY] @ get preempt lazy count
  133. + teq r0, #0 @ if preempt lazy count != 0
  134. + beq 1b
  135. + ret r8 @ go again
  136. +
  137. #endif
  138. __und_fault:
  139. diff -Nur linux-4.9.28.orig/arch/arm/kernel/entry-common.S linux-4.9.28/arch/arm/kernel/entry-common.S
  140. --- linux-4.9.28.orig/arch/arm/kernel/entry-common.S 2017-05-14 14:00:37.000000000 +0200
  141. +++ linux-4.9.28/arch/arm/kernel/entry-common.S 2017-05-19 03:37:25.122174217 +0200
  142. @@ -36,7 +36,9 @@
  143. UNWIND(.cantunwind )
  144. disable_irq_notrace @ disable interrupts
  145. ldr r1, [tsk, #TI_FLAGS] @ re-check for syscall tracing
  146. - tst r1, #_TIF_SYSCALL_WORK | _TIF_WORK_MASK
  147. + tst r1, #((_TIF_SYSCALL_WORK | _TIF_WORK_MASK) & ~_TIF_SECCOMP)
  148. + bne fast_work_pending
  149. + tst r1, #_TIF_SECCOMP
  150. bne fast_work_pending
  151. /* perform architecture specific actions before user return */
  152. @@ -62,8 +64,11 @@
  153. str r0, [sp, #S_R0 + S_OFF]! @ save returned r0
  154. disable_irq_notrace @ disable interrupts
  155. ldr r1, [tsk, #TI_FLAGS] @ re-check for syscall tracing
  156. - tst r1, #_TIF_SYSCALL_WORK | _TIF_WORK_MASK
  157. + tst r1, #((_TIF_SYSCALL_WORK | _TIF_WORK_MASK) & ~_TIF_SECCOMP)
  158. + bne do_slower_path
  159. + tst r1, #_TIF_SECCOMP
  160. beq no_work_pending
  161. +do_slower_path:
  162. UNWIND(.fnend )
  163. ENDPROC(ret_fast_syscall)
  164. diff -Nur linux-4.9.28.orig/arch/arm/kernel/patch.c linux-4.9.28/arch/arm/kernel/patch.c
  165. --- linux-4.9.28.orig/arch/arm/kernel/patch.c 2017-05-14 14:00:37.000000000 +0200
  166. +++ linux-4.9.28/arch/arm/kernel/patch.c 2017-05-19 03:37:25.122174217 +0200
  167. @@ -15,7 +15,7 @@
  168. unsigned int insn;
  169. };
  170. -static DEFINE_SPINLOCK(patch_lock);
  171. +static DEFINE_RAW_SPINLOCK(patch_lock);
  172. static void __kprobes *patch_map(void *addr, int fixmap, unsigned long *flags)
  173. __acquires(&patch_lock)
  174. @@ -32,7 +32,7 @@
  175. return addr;
  176. if (flags)
  177. - spin_lock_irqsave(&patch_lock, *flags);
  178. + raw_spin_lock_irqsave(&patch_lock, *flags);
  179. else
  180. __acquire(&patch_lock);
  181. @@ -47,7 +47,7 @@
  182. clear_fixmap(fixmap);
  183. if (flags)
  184. - spin_unlock_irqrestore(&patch_lock, *flags);
  185. + raw_spin_unlock_irqrestore(&patch_lock, *flags);
  186. else
  187. __release(&patch_lock);
  188. }
  189. diff -Nur linux-4.9.28.orig/arch/arm/kernel/process.c linux-4.9.28/arch/arm/kernel/process.c
  190. --- linux-4.9.28.orig/arch/arm/kernel/process.c 2017-05-14 14:00:37.000000000 +0200
  191. +++ linux-4.9.28/arch/arm/kernel/process.c 2017-05-19 03:37:25.122174217 +0200
  192. @@ -322,6 +322,30 @@
  193. }
  194. #ifdef CONFIG_MMU
  195. +/*
  196. + * CONFIG_SPLIT_PTLOCK_CPUS results in a page->ptl lock. If the lock is not
  197. + * initialized by pgtable_page_ctor() then a coredump of the vector page will
  198. + * fail.
  199. + */
  200. +static int __init vectors_user_mapping_init_page(void)
  201. +{
  202. + struct page *page;
  203. + unsigned long addr = 0xffff0000;
  204. + pgd_t *pgd;
  205. + pud_t *pud;
  206. + pmd_t *pmd;
  207. +
  208. + pgd = pgd_offset_k(addr);
  209. + pud = pud_offset(pgd, addr);
  210. + pmd = pmd_offset(pud, addr);
  211. + page = pmd_page(*(pmd));
  212. +
  213. + pgtable_page_ctor(page);
  214. +
  215. + return 0;
  216. +}
  217. +late_initcall(vectors_user_mapping_init_page);
  218. +
  219. #ifdef CONFIG_KUSER_HELPERS
  220. /*
  221. * The vectors page is always readable from user space for the
  222. diff -Nur linux-4.9.28.orig/arch/arm/kernel/signal.c linux-4.9.28/arch/arm/kernel/signal.c
  223. --- linux-4.9.28.orig/arch/arm/kernel/signal.c 2017-05-14 14:00:37.000000000 +0200
  224. +++ linux-4.9.28/arch/arm/kernel/signal.c 2017-05-19 03:37:25.122174217 +0200
  225. @@ -572,7 +572,8 @@
  226. */
  227. trace_hardirqs_off();
  228. do {
  229. - if (likely(thread_flags & _TIF_NEED_RESCHED)) {
  230. + if (likely(thread_flags & (_TIF_NEED_RESCHED |
  231. + _TIF_NEED_RESCHED_LAZY))) {
  232. schedule();
  233. } else {
  234. if (unlikely(!user_mode(regs)))
  235. diff -Nur linux-4.9.28.orig/arch/arm/kernel/smp.c linux-4.9.28/arch/arm/kernel/smp.c
  236. --- linux-4.9.28.orig/arch/arm/kernel/smp.c 2017-05-14 14:00:37.000000000 +0200
  237. +++ linux-4.9.28/arch/arm/kernel/smp.c 2017-05-19 03:37:25.122174217 +0200
  238. @@ -234,8 +234,6 @@
  239. flush_cache_louis();
  240. local_flush_tlb_all();
  241. - clear_tasks_mm_cpumask(cpu);
  242. -
  243. return 0;
  244. }
  245. @@ -251,6 +249,9 @@
  246. pr_err("CPU%u: cpu didn't die\n", cpu);
  247. return;
  248. }
  249. +
  250. + clear_tasks_mm_cpumask(cpu);
  251. +
  252. pr_notice("CPU%u: shutdown\n", cpu);
  253. /*
  254. diff -Nur linux-4.9.28.orig/arch/arm/kernel/unwind.c linux-4.9.28/arch/arm/kernel/unwind.c
  255. --- linux-4.9.28.orig/arch/arm/kernel/unwind.c 2017-05-14 14:00:37.000000000 +0200
  256. +++ linux-4.9.28/arch/arm/kernel/unwind.c 2017-05-19 03:37:25.122174217 +0200
  257. @@ -93,7 +93,7 @@
  258. static const struct unwind_idx *__origin_unwind_idx;
  259. extern const struct unwind_idx __stop_unwind_idx[];
  260. -static DEFINE_SPINLOCK(unwind_lock);
  261. +static DEFINE_RAW_SPINLOCK(unwind_lock);
  262. static LIST_HEAD(unwind_tables);
  263. /* Convert a prel31 symbol to an absolute address */
  264. @@ -201,7 +201,7 @@
  265. /* module unwind tables */
  266. struct unwind_table *table;
  267. - spin_lock_irqsave(&unwind_lock, flags);
  268. + raw_spin_lock_irqsave(&unwind_lock, flags);
  269. list_for_each_entry(table, &unwind_tables, list) {
  270. if (addr >= table->begin_addr &&
  271. addr < table->end_addr) {
  272. @@ -213,7 +213,7 @@
  273. break;
  274. }
  275. }
  276. - spin_unlock_irqrestore(&unwind_lock, flags);
  277. + raw_spin_unlock_irqrestore(&unwind_lock, flags);
  278. }
  279. pr_debug("%s: idx = %p\n", __func__, idx);
  280. @@ -529,9 +529,9 @@
  281. tab->begin_addr = text_addr;
  282. tab->end_addr = text_addr + text_size;
  283. - spin_lock_irqsave(&unwind_lock, flags);
  284. + raw_spin_lock_irqsave(&unwind_lock, flags);
  285. list_add_tail(&tab->list, &unwind_tables);
  286. - spin_unlock_irqrestore(&unwind_lock, flags);
  287. + raw_spin_unlock_irqrestore(&unwind_lock, flags);
  288. return tab;
  289. }
  290. @@ -543,9 +543,9 @@
  291. if (!tab)
  292. return;
  293. - spin_lock_irqsave(&unwind_lock, flags);
  294. + raw_spin_lock_irqsave(&unwind_lock, flags);
  295. list_del(&tab->list);
  296. - spin_unlock_irqrestore(&unwind_lock, flags);
  297. + raw_spin_unlock_irqrestore(&unwind_lock, flags);
  298. kfree(tab);
  299. }
  300. diff -Nur linux-4.9.28.orig/arch/arm/kvm/arm.c linux-4.9.28/arch/arm/kvm/arm.c
  301. --- linux-4.9.28.orig/arch/arm/kvm/arm.c 2017-05-14 14:00:37.000000000 +0200
  302. +++ linux-4.9.28/arch/arm/kvm/arm.c 2017-05-19 03:37:25.122174217 +0200
  303. @@ -619,7 +619,7 @@
  304. * involves poking the GIC, which must be done in a
  305. * non-preemptible context.
  306. */
  307. - preempt_disable();
  308. + migrate_disable();
  309. kvm_pmu_flush_hwstate(vcpu);
  310. kvm_timer_flush_hwstate(vcpu);
  311. kvm_vgic_flush_hwstate(vcpu);
  312. @@ -640,7 +640,7 @@
  313. kvm_pmu_sync_hwstate(vcpu);
  314. kvm_timer_sync_hwstate(vcpu);
  315. kvm_vgic_sync_hwstate(vcpu);
  316. - preempt_enable();
  317. + migrate_enable();
  318. continue;
  319. }
  320. @@ -696,7 +696,7 @@
  321. kvm_vgic_sync_hwstate(vcpu);
  322. - preempt_enable();
  323. + migrate_enable();
  324. ret = handle_exit(vcpu, run, ret);
  325. }
  326. diff -Nur linux-4.9.28.orig/arch/arm/mach-exynos/platsmp.c linux-4.9.28/arch/arm/mach-exynos/platsmp.c
  327. --- linux-4.9.28.orig/arch/arm/mach-exynos/platsmp.c 2017-05-14 14:00:37.000000000 +0200
  328. +++ linux-4.9.28/arch/arm/mach-exynos/platsmp.c 2017-05-19 03:37:25.122174217 +0200
  329. @@ -229,7 +229,7 @@
  330. return (void __iomem *)(S5P_VA_SCU);
  331. }
  332. -static DEFINE_SPINLOCK(boot_lock);
  333. +static DEFINE_RAW_SPINLOCK(boot_lock);
  334. static void exynos_secondary_init(unsigned int cpu)
  335. {
  336. @@ -242,8 +242,8 @@
  337. /*
  338. * Synchronise with the boot thread.
  339. */
  340. - spin_lock(&boot_lock);
  341. - spin_unlock(&boot_lock);
  342. + raw_spin_lock(&boot_lock);
  343. + raw_spin_unlock(&boot_lock);
  344. }
  345. int exynos_set_boot_addr(u32 core_id, unsigned long boot_addr)
  346. @@ -307,7 +307,7 @@
  347. * Set synchronisation state between this boot processor
  348. * and the secondary one
  349. */
  350. - spin_lock(&boot_lock);
  351. + raw_spin_lock(&boot_lock);
  352. /*
  353. * The secondary processor is waiting to be released from
  354. @@ -334,7 +334,7 @@
  355. if (timeout == 0) {
  356. printk(KERN_ERR "cpu1 power enable failed");
  357. - spin_unlock(&boot_lock);
  358. + raw_spin_unlock(&boot_lock);
  359. return -ETIMEDOUT;
  360. }
  361. }
  362. @@ -380,7 +380,7 @@
  363. * calibrations, then wait for it to finish
  364. */
  365. fail:
  366. - spin_unlock(&boot_lock);
  367. + raw_spin_unlock(&boot_lock);
  368. return pen_release != -1 ? ret : 0;
  369. }
  370. diff -Nur linux-4.9.28.orig/arch/arm/mach-hisi/platmcpm.c linux-4.9.28/arch/arm/mach-hisi/platmcpm.c
  371. --- linux-4.9.28.orig/arch/arm/mach-hisi/platmcpm.c 2017-05-14 14:00:37.000000000 +0200
  372. +++ linux-4.9.28/arch/arm/mach-hisi/platmcpm.c 2017-05-19 03:37:25.122174217 +0200
  373. @@ -61,7 +61,7 @@
  374. static void __iomem *sysctrl, *fabric;
  375. static int hip04_cpu_table[HIP04_MAX_CLUSTERS][HIP04_MAX_CPUS_PER_CLUSTER];
  376. -static DEFINE_SPINLOCK(boot_lock);
  377. +static DEFINE_RAW_SPINLOCK(boot_lock);
  378. static u32 fabric_phys_addr;
  379. /*
  380. * [0]: bootwrapper physical address
  381. @@ -113,7 +113,7 @@
  382. if (cluster >= HIP04_MAX_CLUSTERS || cpu >= HIP04_MAX_CPUS_PER_CLUSTER)
  383. return -EINVAL;
  384. - spin_lock_irq(&boot_lock);
  385. + raw_spin_lock_irq(&boot_lock);
  386. if (hip04_cpu_table[cluster][cpu])
  387. goto out;
  388. @@ -147,7 +147,7 @@
  389. out:
  390. hip04_cpu_table[cluster][cpu]++;
  391. - spin_unlock_irq(&boot_lock);
  392. + raw_spin_unlock_irq(&boot_lock);
  393. return 0;
  394. }
  395. @@ -162,11 +162,11 @@
  396. cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  397. cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  398. - spin_lock(&boot_lock);
  399. + raw_spin_lock(&boot_lock);
  400. hip04_cpu_table[cluster][cpu]--;
  401. if (hip04_cpu_table[cluster][cpu] == 1) {
  402. /* A power_up request went ahead of us. */
  403. - spin_unlock(&boot_lock);
  404. + raw_spin_unlock(&boot_lock);
  405. return;
  406. } else if (hip04_cpu_table[cluster][cpu] > 1) {
  407. pr_err("Cluster %d CPU%d boots multiple times\n", cluster, cpu);
  408. @@ -174,7 +174,7 @@
  409. }
  410. last_man = hip04_cluster_is_down(cluster);
  411. - spin_unlock(&boot_lock);
  412. + raw_spin_unlock(&boot_lock);
  413. if (last_man) {
  414. /* Since it's Cortex A15, disable L2 prefetching. */
  415. asm volatile(
  416. @@ -203,7 +203,7 @@
  417. cpu >= HIP04_MAX_CPUS_PER_CLUSTER);
  418. count = TIMEOUT_MSEC / POLL_MSEC;
  419. - spin_lock_irq(&boot_lock);
  420. + raw_spin_lock_irq(&boot_lock);
  421. for (tries = 0; tries < count; tries++) {
  422. if (hip04_cpu_table[cluster][cpu])
  423. goto err;
  424. @@ -211,10 +211,10 @@
  425. data = readl_relaxed(sysctrl + SC_CPU_RESET_STATUS(cluster));
  426. if (data & CORE_WFI_STATUS(cpu))
  427. break;
  428. - spin_unlock_irq(&boot_lock);
  429. + raw_spin_unlock_irq(&boot_lock);
  430. /* Wait for clean L2 when the whole cluster is down. */
  431. msleep(POLL_MSEC);
  432. - spin_lock_irq(&boot_lock);
  433. + raw_spin_lock_irq(&boot_lock);
  434. }
  435. if (tries >= count)
  436. goto err;
  437. @@ -231,10 +231,10 @@
  438. goto err;
  439. if (hip04_cluster_is_down(cluster))
  440. hip04_set_snoop_filter(cluster, 0);
  441. - spin_unlock_irq(&boot_lock);
  442. + raw_spin_unlock_irq(&boot_lock);
  443. return 1;
  444. err:
  445. - spin_unlock_irq(&boot_lock);
  446. + raw_spin_unlock_irq(&boot_lock);
  447. return 0;
  448. }
  449. #endif
  450. diff -Nur linux-4.9.28.orig/arch/arm/mach-omap2/omap-smp.c linux-4.9.28/arch/arm/mach-omap2/omap-smp.c
  451. --- linux-4.9.28.orig/arch/arm/mach-omap2/omap-smp.c 2017-05-14 14:00:37.000000000 +0200
  452. +++ linux-4.9.28/arch/arm/mach-omap2/omap-smp.c 2017-05-19 03:37:25.122174217 +0200
  453. @@ -64,7 +64,7 @@
  454. .startup_addr = omap5_secondary_startup,
  455. };
  456. -static DEFINE_SPINLOCK(boot_lock);
  457. +static DEFINE_RAW_SPINLOCK(boot_lock);
  458. void __iomem *omap4_get_scu_base(void)
  459. {
  460. @@ -131,8 +131,8 @@
  461. /*
  462. * Synchronise with the boot thread.
  463. */
  464. - spin_lock(&boot_lock);
  465. - spin_unlock(&boot_lock);
  466. + raw_spin_lock(&boot_lock);
  467. + raw_spin_unlock(&boot_lock);
  468. }
  469. static int omap4_boot_secondary(unsigned int cpu, struct task_struct *idle)
  470. @@ -146,7 +146,7 @@
  471. * Set synchronisation state between this boot processor
  472. * and the secondary one
  473. */
  474. - spin_lock(&boot_lock);
  475. + raw_spin_lock(&boot_lock);
  476. /*
  477. * Update the AuxCoreBoot0 with boot state for secondary core.
  478. @@ -223,7 +223,7 @@
  479. * Now the secondary core is starting up let it run its
  480. * calibrations, then wait for it to finish
  481. */
  482. - spin_unlock(&boot_lock);
  483. + raw_spin_unlock(&boot_lock);
  484. return 0;
  485. }
  486. diff -Nur linux-4.9.28.orig/arch/arm/mach-prima2/platsmp.c linux-4.9.28/arch/arm/mach-prima2/platsmp.c
  487. --- linux-4.9.28.orig/arch/arm/mach-prima2/platsmp.c 2017-05-14 14:00:37.000000000 +0200
  488. +++ linux-4.9.28/arch/arm/mach-prima2/platsmp.c 2017-05-19 03:37:25.122174217 +0200
  489. @@ -22,7 +22,7 @@
  490. static void __iomem *clk_base;
  491. -static DEFINE_SPINLOCK(boot_lock);
  492. +static DEFINE_RAW_SPINLOCK(boot_lock);
  493. static void sirfsoc_secondary_init(unsigned int cpu)
  494. {
  495. @@ -36,8 +36,8 @@
  496. /*
  497. * Synchronise with the boot thread.
  498. */
  499. - spin_lock(&boot_lock);
  500. - spin_unlock(&boot_lock);
  501. + raw_spin_lock(&boot_lock);
  502. + raw_spin_unlock(&boot_lock);
  503. }
  504. static const struct of_device_id clk_ids[] = {
  505. @@ -75,7 +75,7 @@
  506. /* make sure write buffer is drained */
  507. mb();
  508. - spin_lock(&boot_lock);
  509. + raw_spin_lock(&boot_lock);
  510. /*
  511. * The secondary processor is waiting to be released from
  512. @@ -107,7 +107,7 @@
  513. * now the secondary core is starting up let it run its
  514. * calibrations, then wait for it to finish
  515. */
  516. - spin_unlock(&boot_lock);
  517. + raw_spin_unlock(&boot_lock);
  518. return pen_release != -1 ? -ENOSYS : 0;
  519. }
  520. diff -Nur linux-4.9.28.orig/arch/arm/mach-qcom/platsmp.c linux-4.9.28/arch/arm/mach-qcom/platsmp.c
  521. --- linux-4.9.28.orig/arch/arm/mach-qcom/platsmp.c 2017-05-14 14:00:37.000000000 +0200
  522. +++ linux-4.9.28/arch/arm/mach-qcom/platsmp.c 2017-05-19 03:37:25.122174217 +0200
  523. @@ -46,7 +46,7 @@
  524. extern void secondary_startup_arm(void);
  525. -static DEFINE_SPINLOCK(boot_lock);
  526. +static DEFINE_RAW_SPINLOCK(boot_lock);
  527. #ifdef CONFIG_HOTPLUG_CPU
  528. static void qcom_cpu_die(unsigned int cpu)
  529. @@ -60,8 +60,8 @@
  530. /*
  531. * Synchronise with the boot thread.
  532. */
  533. - spin_lock(&boot_lock);
  534. - spin_unlock(&boot_lock);
  535. + raw_spin_lock(&boot_lock);
  536. + raw_spin_unlock(&boot_lock);
  537. }
  538. static int scss_release_secondary(unsigned int cpu)
  539. @@ -284,7 +284,7 @@
  540. * set synchronisation state between this boot processor
  541. * and the secondary one
  542. */
  543. - spin_lock(&boot_lock);
  544. + raw_spin_lock(&boot_lock);
  545. /*
  546. * Send the secondary CPU a soft interrupt, thereby causing
  547. @@ -297,7 +297,7 @@
  548. * now the secondary core is starting up let it run its
  549. * calibrations, then wait for it to finish
  550. */
  551. - spin_unlock(&boot_lock);
  552. + raw_spin_unlock(&boot_lock);
  553. return ret;
  554. }
  555. diff -Nur linux-4.9.28.orig/arch/arm/mach-spear/platsmp.c linux-4.9.28/arch/arm/mach-spear/platsmp.c
  556. --- linux-4.9.28.orig/arch/arm/mach-spear/platsmp.c 2017-05-14 14:00:37.000000000 +0200
  557. +++ linux-4.9.28/arch/arm/mach-spear/platsmp.c 2017-05-19 03:37:25.122174217 +0200
  558. @@ -32,7 +32,7 @@
  559. sync_cache_w(&pen_release);
  560. }
  561. -static DEFINE_SPINLOCK(boot_lock);
  562. +static DEFINE_RAW_SPINLOCK(boot_lock);
  563. static void __iomem *scu_base = IOMEM(VA_SCU_BASE);
  564. @@ -47,8 +47,8 @@
  565. /*
  566. * Synchronise with the boot thread.
  567. */
  568. - spin_lock(&boot_lock);
  569. - spin_unlock(&boot_lock);
  570. + raw_spin_lock(&boot_lock);
  571. + raw_spin_unlock(&boot_lock);
  572. }
  573. static int spear13xx_boot_secondary(unsigned int cpu, struct task_struct *idle)
  574. @@ -59,7 +59,7 @@
  575. * set synchronisation state between this boot processor
  576. * and the secondary one
  577. */
  578. - spin_lock(&boot_lock);
  579. + raw_spin_lock(&boot_lock);
  580. /*
  581. * The secondary processor is waiting to be released from
  582. @@ -84,7 +84,7 @@
  583. * now the secondary core is starting up let it run its
  584. * calibrations, then wait for it to finish
  585. */
  586. - spin_unlock(&boot_lock);
  587. + raw_spin_unlock(&boot_lock);
  588. return pen_release != -1 ? -ENOSYS : 0;
  589. }
  590. diff -Nur linux-4.9.28.orig/arch/arm/mach-sti/platsmp.c linux-4.9.28/arch/arm/mach-sti/platsmp.c
  591. --- linux-4.9.28.orig/arch/arm/mach-sti/platsmp.c 2017-05-14 14:00:37.000000000 +0200
  592. +++ linux-4.9.28/arch/arm/mach-sti/platsmp.c 2017-05-19 03:37:25.122174217 +0200
  593. @@ -35,7 +35,7 @@
  594. sync_cache_w(&pen_release);
  595. }
  596. -static DEFINE_SPINLOCK(boot_lock);
  597. +static DEFINE_RAW_SPINLOCK(boot_lock);
  598. static void sti_secondary_init(unsigned int cpu)
  599. {
  600. @@ -48,8 +48,8 @@
  601. /*
  602. * Synchronise with the boot thread.
  603. */
  604. - spin_lock(&boot_lock);
  605. - spin_unlock(&boot_lock);
  606. + raw_spin_lock(&boot_lock);
  607. + raw_spin_unlock(&boot_lock);
  608. }
  609. static int sti_boot_secondary(unsigned int cpu, struct task_struct *idle)
  610. @@ -60,7 +60,7 @@
  611. * set synchronisation state between this boot processor
  612. * and the secondary one
  613. */
  614. - spin_lock(&boot_lock);
  615. + raw_spin_lock(&boot_lock);
  616. /*
  617. * The secondary processor is waiting to be released from
  618. @@ -91,7 +91,7 @@
  619. * now the secondary core is starting up let it run its
  620. * calibrations, then wait for it to finish
  621. */
  622. - spin_unlock(&boot_lock);
  623. + raw_spin_unlock(&boot_lock);
  624. return pen_release != -1 ? -ENOSYS : 0;
  625. }
  626. diff -Nur linux-4.9.28.orig/arch/arm/mm/fault.c linux-4.9.28/arch/arm/mm/fault.c
  627. --- linux-4.9.28.orig/arch/arm/mm/fault.c 2017-05-14 14:00:37.000000000 +0200
  628. +++ linux-4.9.28/arch/arm/mm/fault.c 2017-05-19 03:37:25.122174217 +0200
  629. @@ -430,6 +430,9 @@
  630. if (addr < TASK_SIZE)
  631. return do_page_fault(addr, fsr, regs);
  632. + if (interrupts_enabled(regs))
  633. + local_irq_enable();
  634. +
  635. if (user_mode(regs))
  636. goto bad_area;
  637. @@ -497,6 +500,9 @@
  638. static int
  639. do_sect_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  640. {
  641. + if (interrupts_enabled(regs))
  642. + local_irq_enable();
  643. +
  644. do_bad_area(addr, fsr, regs);
  645. return 0;
  646. }
  647. diff -Nur linux-4.9.28.orig/arch/arm/mm/highmem.c linux-4.9.28/arch/arm/mm/highmem.c
  648. --- linux-4.9.28.orig/arch/arm/mm/highmem.c 2017-05-14 14:00:37.000000000 +0200
  649. +++ linux-4.9.28/arch/arm/mm/highmem.c 2017-05-19 03:37:25.126174470 +0200
  650. @@ -34,6 +34,11 @@
  651. return *ptep;
  652. }
  653. +static unsigned int fixmap_idx(int type)
  654. +{
  655. + return FIX_KMAP_BEGIN + type + KM_TYPE_NR * smp_processor_id();
  656. +}
  657. +
  658. void *kmap(struct page *page)
  659. {
  660. might_sleep();
  661. @@ -54,12 +59,13 @@
  662. void *kmap_atomic(struct page *page)
  663. {
  664. + pte_t pte = mk_pte(page, kmap_prot);
  665. unsigned int idx;
  666. unsigned long vaddr;
  667. void *kmap;
  668. int type;
  669. - preempt_disable();
  670. + preempt_disable_nort();
  671. pagefault_disable();
  672. if (!PageHighMem(page))
  673. return page_address(page);
  674. @@ -79,7 +85,7 @@
  675. type = kmap_atomic_idx_push();
  676. - idx = FIX_KMAP_BEGIN + type + KM_TYPE_NR * smp_processor_id();
  677. + idx = fixmap_idx(type);
  678. vaddr = __fix_to_virt(idx);
  679. #ifdef CONFIG_DEBUG_HIGHMEM
  680. /*
  681. @@ -93,7 +99,10 @@
  682. * in place, so the contained TLB flush ensures the TLB is updated
  683. * with the new mapping.
  684. */
  685. - set_fixmap_pte(idx, mk_pte(page, kmap_prot));
  686. +#ifdef CONFIG_PREEMPT_RT_FULL
  687. + current->kmap_pte[type] = pte;
  688. +#endif
  689. + set_fixmap_pte(idx, pte);
  690. return (void *)vaddr;
  691. }
  692. @@ -106,44 +115,75 @@
  693. if (kvaddr >= (void *)FIXADDR_START) {
  694. type = kmap_atomic_idx();
  695. - idx = FIX_KMAP_BEGIN + type + KM_TYPE_NR * smp_processor_id();
  696. + idx = fixmap_idx(type);
  697. if (cache_is_vivt())
  698. __cpuc_flush_dcache_area((void *)vaddr, PAGE_SIZE);
  699. +#ifdef CONFIG_PREEMPT_RT_FULL
  700. + current->kmap_pte[type] = __pte(0);
  701. +#endif
  702. #ifdef CONFIG_DEBUG_HIGHMEM
  703. BUG_ON(vaddr != __fix_to_virt(idx));
  704. - set_fixmap_pte(idx, __pte(0));
  705. #else
  706. (void) idx; /* to kill a warning */
  707. #endif
  708. + set_fixmap_pte(idx, __pte(0));
  709. kmap_atomic_idx_pop();
  710. } else if (vaddr >= PKMAP_ADDR(0) && vaddr < PKMAP_ADDR(LAST_PKMAP)) {
  711. /* this address was obtained through kmap_high_get() */
  712. kunmap_high(pte_page(pkmap_page_table[PKMAP_NR(vaddr)]));
  713. }
  714. pagefault_enable();
  715. - preempt_enable();
  716. + preempt_enable_nort();
  717. }
  718. EXPORT_SYMBOL(__kunmap_atomic);
  719. void *kmap_atomic_pfn(unsigned long pfn)
  720. {
  721. + pte_t pte = pfn_pte(pfn, kmap_prot);
  722. unsigned long vaddr;
  723. int idx, type;
  724. struct page *page = pfn_to_page(pfn);
  725. - preempt_disable();
  726. + preempt_disable_nort();
  727. pagefault_disable();
  728. if (!PageHighMem(page))
  729. return page_address(page);
  730. type = kmap_atomic_idx_push();
  731. - idx = FIX_KMAP_BEGIN + type + KM_TYPE_NR * smp_processor_id();
  732. + idx = fixmap_idx(type);
  733. vaddr = __fix_to_virt(idx);
  734. #ifdef CONFIG_DEBUG_HIGHMEM
  735. BUG_ON(!pte_none(get_fixmap_pte(vaddr)));
  736. #endif
  737. - set_fixmap_pte(idx, pfn_pte(pfn, kmap_prot));
  738. +#ifdef CONFIG_PREEMPT_RT_FULL
  739. + current->kmap_pte[type] = pte;
  740. +#endif
  741. + set_fixmap_pte(idx, pte);
  742. return (void *)vaddr;
  743. }
  744. +#if defined CONFIG_PREEMPT_RT_FULL
  745. +void switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p)
  746. +{
  747. + int i;
  748. +
  749. + /*
  750. + * Clear @prev's kmap_atomic mappings
  751. + */
  752. + for (i = 0; i < prev_p->kmap_idx; i++) {
  753. + int idx = fixmap_idx(i);
  754. +
  755. + set_fixmap_pte(idx, __pte(0));
  756. + }
  757. + /*
  758. + * Restore @next_p's kmap_atomic mappings
  759. + */
  760. + for (i = 0; i < next_p->kmap_idx; i++) {
  761. + int idx = fixmap_idx(i);
  762. +
  763. + if (!pte_none(next_p->kmap_pte[i]))
  764. + set_fixmap_pte(idx, next_p->kmap_pte[i]);
  765. + }
  766. +}
  767. +#endif
  768. diff -Nur linux-4.9.28.orig/arch/arm/plat-versatile/platsmp.c linux-4.9.28/arch/arm/plat-versatile/platsmp.c
  769. --- linux-4.9.28.orig/arch/arm/plat-versatile/platsmp.c 2017-05-14 14:00:37.000000000 +0200
  770. +++ linux-4.9.28/arch/arm/plat-versatile/platsmp.c 2017-05-19 03:37:25.126174470 +0200
  771. @@ -32,7 +32,7 @@
  772. sync_cache_w(&pen_release);
  773. }
  774. -static DEFINE_SPINLOCK(boot_lock);
  775. +static DEFINE_RAW_SPINLOCK(boot_lock);
  776. void versatile_secondary_init(unsigned int cpu)
  777. {
  778. @@ -45,8 +45,8 @@
  779. /*
  780. * Synchronise with the boot thread.
  781. */
  782. - spin_lock(&boot_lock);
  783. - spin_unlock(&boot_lock);
  784. + raw_spin_lock(&boot_lock);
  785. + raw_spin_unlock(&boot_lock);
  786. }
  787. int versatile_boot_secondary(unsigned int cpu, struct task_struct *idle)
  788. @@ -57,7 +57,7 @@
  789. * Set synchronisation state between this boot processor
  790. * and the secondary one
  791. */
  792. - spin_lock(&boot_lock);
  793. + raw_spin_lock(&boot_lock);
  794. /*
  795. * This is really belt and braces; we hold unintended secondary
  796. @@ -87,7 +87,7 @@
  797. * now the secondary core is starting up let it run its
  798. * calibrations, then wait for it to finish
  799. */
  800. - spin_unlock(&boot_lock);
  801. + raw_spin_unlock(&boot_lock);
  802. return pen_release != -1 ? -ENOSYS : 0;
  803. }
  804. diff -Nur linux-4.9.28.orig/arch/arm64/include/asm/thread_info.h linux-4.9.28/arch/arm64/include/asm/thread_info.h
  805. --- linux-4.9.28.orig/arch/arm64/include/asm/thread_info.h 2017-05-14 14:00:37.000000000 +0200
  806. +++ linux-4.9.28/arch/arm64/include/asm/thread_info.h 2017-05-19 03:37:25.126174470 +0200
  807. @@ -49,6 +49,7 @@
  808. mm_segment_t addr_limit; /* address limit */
  809. struct task_struct *task; /* main task structure */
  810. int preempt_count; /* 0 => preemptable, <0 => bug */
  811. + int preempt_lazy_count; /* 0 => preemptable, <0 => bug */
  812. int cpu; /* cpu */
  813. };
  814. @@ -112,6 +113,7 @@
  815. #define TIF_NEED_RESCHED 1
  816. #define TIF_NOTIFY_RESUME 2 /* callback before returning to user */
  817. #define TIF_FOREIGN_FPSTATE 3 /* CPU's FP state is not current's */
  818. +#define TIF_NEED_RESCHED_LAZY 4
  819. #define TIF_NOHZ 7
  820. #define TIF_SYSCALL_TRACE 8
  821. #define TIF_SYSCALL_AUDIT 9
  822. @@ -127,6 +129,7 @@
  823. #define _TIF_NEED_RESCHED (1 << TIF_NEED_RESCHED)
  824. #define _TIF_NOTIFY_RESUME (1 << TIF_NOTIFY_RESUME)
  825. #define _TIF_FOREIGN_FPSTATE (1 << TIF_FOREIGN_FPSTATE)
  826. +#define _TIF_NEED_RESCHED_LAZY (1 << TIF_NEED_RESCHED_LAZY)
  827. #define _TIF_NOHZ (1 << TIF_NOHZ)
  828. #define _TIF_SYSCALL_TRACE (1 << TIF_SYSCALL_TRACE)
  829. #define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
  830. @@ -135,7 +138,9 @@
  831. #define _TIF_32BIT (1 << TIF_32BIT)
  832. #define _TIF_WORK_MASK (_TIF_NEED_RESCHED | _TIF_SIGPENDING | \
  833. - _TIF_NOTIFY_RESUME | _TIF_FOREIGN_FPSTATE)
  834. + _TIF_NOTIFY_RESUME | _TIF_FOREIGN_FPSTATE | \
  835. + _TIF_NEED_RESCHED_LAZY)
  836. +#define _TIF_NEED_RESCHED_MASK (_TIF_NEED_RESCHED | _TIF_NEED_RESCHED_LAZY)
  837. #define _TIF_SYSCALL_WORK (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | \
  838. _TIF_SYSCALL_TRACEPOINT | _TIF_SECCOMP | \
  839. diff -Nur linux-4.9.28.orig/arch/arm64/Kconfig linux-4.9.28/arch/arm64/Kconfig
  840. --- linux-4.9.28.orig/arch/arm64/Kconfig 2017-05-14 14:00:37.000000000 +0200
  841. +++ linux-4.9.28/arch/arm64/Kconfig 2017-05-19 03:37:25.126174470 +0200
  842. @@ -91,6 +91,7 @@
  843. select HAVE_PERF_EVENTS
  844. select HAVE_PERF_REGS
  845. select HAVE_PERF_USER_STACK_DUMP
  846. + select HAVE_PREEMPT_LAZY
  847. select HAVE_REGS_AND_STACK_ACCESS_API
  848. select HAVE_RCU_TABLE_FREE
  849. select HAVE_SYSCALL_TRACEPOINTS
  850. @@ -704,7 +705,7 @@
  851. config XEN
  852. bool "Xen guest support on ARM64"
  853. - depends on ARM64 && OF
  854. + depends on ARM64 && OF && !PREEMPT_RT_FULL
  855. select SWIOTLB_XEN
  856. select PARAVIRT
  857. help
  858. diff -Nur linux-4.9.28.orig/arch/arm64/kernel/asm-offsets.c linux-4.9.28/arch/arm64/kernel/asm-offsets.c
  859. --- linux-4.9.28.orig/arch/arm64/kernel/asm-offsets.c 2017-05-14 14:00:37.000000000 +0200
  860. +++ linux-4.9.28/arch/arm64/kernel/asm-offsets.c 2017-05-19 03:37:25.126174470 +0200
  861. @@ -38,6 +38,7 @@
  862. BLANK();
  863. DEFINE(TI_FLAGS, offsetof(struct thread_info, flags));
  864. DEFINE(TI_PREEMPT, offsetof(struct thread_info, preempt_count));
  865. + DEFINE(TI_PREEMPT_LAZY, offsetof(struct thread_info, preempt_lazy_count));
  866. DEFINE(TI_ADDR_LIMIT, offsetof(struct thread_info, addr_limit));
  867. DEFINE(TI_TASK, offsetof(struct thread_info, task));
  868. DEFINE(TI_CPU, offsetof(struct thread_info, cpu));
  869. diff -Nur linux-4.9.28.orig/arch/arm64/kernel/entry.S linux-4.9.28/arch/arm64/kernel/entry.S
  870. --- linux-4.9.28.orig/arch/arm64/kernel/entry.S 2017-05-14 14:00:37.000000000 +0200
  871. +++ linux-4.9.28/arch/arm64/kernel/entry.S 2017-05-19 03:37:25.126174470 +0200
  872. @@ -428,11 +428,16 @@
  873. #ifdef CONFIG_PREEMPT
  874. ldr w24, [tsk, #TI_PREEMPT] // get preempt count
  875. - cbnz w24, 1f // preempt count != 0
  876. + cbnz w24, 2f // preempt count != 0
  877. ldr x0, [tsk, #TI_FLAGS] // get flags
  878. - tbz x0, #TIF_NEED_RESCHED, 1f // needs rescheduling?
  879. - bl el1_preempt
  880. + tbnz x0, #TIF_NEED_RESCHED, 1f // needs rescheduling?
  881. +
  882. + ldr w24, [tsk, #TI_PREEMPT_LAZY] // get preempt lazy count
  883. + cbnz w24, 2f // preempt lazy count != 0
  884. + tbz x0, #TIF_NEED_RESCHED_LAZY, 2f // needs rescheduling?
  885. 1:
  886. + bl el1_preempt
  887. +2:
  888. #endif
  889. #ifdef CONFIG_TRACE_IRQFLAGS
  890. bl trace_hardirqs_on
  891. @@ -446,6 +451,7 @@
  892. 1: bl preempt_schedule_irq // irq en/disable is done inside
  893. ldr x0, [tsk, #TI_FLAGS] // get new tasks TI_FLAGS
  894. tbnz x0, #TIF_NEED_RESCHED, 1b // needs rescheduling?
  895. + tbnz x0, #TIF_NEED_RESCHED_LAZY, 1b // needs rescheduling?
  896. ret x24
  897. #endif
  898. diff -Nur linux-4.9.28.orig/arch/arm64/kernel/signal.c linux-4.9.28/arch/arm64/kernel/signal.c
  899. --- linux-4.9.28.orig/arch/arm64/kernel/signal.c 2017-05-14 14:00:37.000000000 +0200
  900. +++ linux-4.9.28/arch/arm64/kernel/signal.c 2017-05-19 03:37:25.126174470 +0200
  901. @@ -409,7 +409,7 @@
  902. */
  903. trace_hardirqs_off();
  904. do {
  905. - if (thread_flags & _TIF_NEED_RESCHED) {
  906. + if (thread_flags & _TIF_NEED_RESCHED_MASK) {
  907. schedule();
  908. } else {
  909. local_irq_enable();
  910. diff -Nur linux-4.9.28.orig/arch/Kconfig linux-4.9.28/arch/Kconfig
  911. --- linux-4.9.28.orig/arch/Kconfig 2017-05-14 14:00:37.000000000 +0200
  912. +++ linux-4.9.28/arch/Kconfig 2017-05-19 03:37:25.122174217 +0200
  913. @@ -9,6 +9,7 @@
  914. tristate "OProfile system profiling"
  915. depends on PROFILING
  916. depends on HAVE_OPROFILE
  917. + depends on !PREEMPT_RT_FULL
  918. select RING_BUFFER
  919. select RING_BUFFER_ALLOW_SWAP
  920. help
  921. @@ -52,6 +53,7 @@
  922. config JUMP_LABEL
  923. bool "Optimize very unlikely/likely branches"
  924. depends on HAVE_ARCH_JUMP_LABEL
  925. + depends on (!INTERRUPT_OFF_HIST && !PREEMPT_OFF_HIST && !WAKEUP_LATENCY_HIST && !MISSED_TIMER_OFFSETS_HIST)
  926. help
  927. This option enables a transparent branch optimization that
  928. makes certain almost-always-true or almost-always-false branch
  929. diff -Nur linux-4.9.28.orig/arch/mips/Kconfig linux-4.9.28/arch/mips/Kconfig
  930. --- linux-4.9.28.orig/arch/mips/Kconfig 2017-05-14 14:00:37.000000000 +0200
  931. +++ linux-4.9.28/arch/mips/Kconfig 2017-05-19 03:37:25.126174470 +0200
  932. @@ -2515,7 +2515,7 @@
  933. #
  934. config HIGHMEM
  935. bool "High Memory Support"
  936. - depends on 32BIT && CPU_SUPPORTS_HIGHMEM && SYS_SUPPORTS_HIGHMEM && !CPU_MIPS32_3_5_EVA
  937. + depends on 32BIT && CPU_SUPPORTS_HIGHMEM && SYS_SUPPORTS_HIGHMEM && !CPU_MIPS32_3_5_EVA && !PREEMPT_RT_FULL
  938. config CPU_SUPPORTS_HIGHMEM
  939. bool
  940. diff -Nur linux-4.9.28.orig/arch/powerpc/include/asm/thread_info.h linux-4.9.28/arch/powerpc/include/asm/thread_info.h
  941. --- linux-4.9.28.orig/arch/powerpc/include/asm/thread_info.h 2017-05-14 14:00:37.000000000 +0200
  942. +++ linux-4.9.28/arch/powerpc/include/asm/thread_info.h 2017-05-19 03:37:25.126174470 +0200
  943. @@ -43,6 +43,8 @@
  944. int cpu; /* cpu we're on */
  945. int preempt_count; /* 0 => preemptable,
  946. <0 => BUG */
  947. + int preempt_lazy_count; /* 0 => preemptable,
  948. + <0 => BUG */
  949. unsigned long local_flags; /* private flags for thread */
  950. #ifdef CONFIG_LIVEPATCH
  951. unsigned long *livepatch_sp;
  952. @@ -88,8 +90,7 @@
  953. #define TIF_SYSCALL_TRACE 0 /* syscall trace active */
  954. #define TIF_SIGPENDING 1 /* signal pending */
  955. #define TIF_NEED_RESCHED 2 /* rescheduling necessary */
  956. -#define TIF_POLLING_NRFLAG 3 /* true if poll_idle() is polling
  957. - TIF_NEED_RESCHED */
  958. +#define TIF_NEED_RESCHED_LAZY 3 /* lazy rescheduling necessary */
  959. #define TIF_32BIT 4 /* 32 bit binary */
  960. #define TIF_RESTORE_TM 5 /* need to restore TM FP/VEC/VSX */
  961. #define TIF_SYSCALL_AUDIT 7 /* syscall auditing active */
  962. @@ -107,6 +108,8 @@
  963. #if defined(CONFIG_PPC64)
  964. #define TIF_ELF2ABI 18 /* function descriptors must die! */
  965. #endif
  966. +#define TIF_POLLING_NRFLAG 19 /* true if poll_idle() is polling
  967. + TIF_NEED_RESCHED */
  968. /* as above, but as bit values */
  969. #define _TIF_SYSCALL_TRACE (1<<TIF_SYSCALL_TRACE)
  970. @@ -125,14 +128,16 @@
  971. #define _TIF_SYSCALL_TRACEPOINT (1<<TIF_SYSCALL_TRACEPOINT)
  972. #define _TIF_EMULATE_STACK_STORE (1<<TIF_EMULATE_STACK_STORE)
  973. #define _TIF_NOHZ (1<<TIF_NOHZ)
  974. +#define _TIF_NEED_RESCHED_LAZY (1<<TIF_NEED_RESCHED_LAZY)
  975. #define _TIF_SYSCALL_DOTRACE (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | \
  976. _TIF_SECCOMP | _TIF_SYSCALL_TRACEPOINT | \
  977. _TIF_NOHZ)
  978. #define _TIF_USER_WORK_MASK (_TIF_SIGPENDING | _TIF_NEED_RESCHED | \
  979. _TIF_NOTIFY_RESUME | _TIF_UPROBE | \
  980. - _TIF_RESTORE_TM)
  981. + _TIF_RESTORE_TM | _TIF_NEED_RESCHED_LAZY)
  982. #define _TIF_PERSYSCALL_MASK (_TIF_RESTOREALL|_TIF_NOERROR)
  983. +#define _TIF_NEED_RESCHED_MASK (_TIF_NEED_RESCHED | _TIF_NEED_RESCHED_LAZY)
  984. /* Bits in local_flags */
  985. /* Don't move TLF_NAPPING without adjusting the code in entry_32.S */
  986. diff -Nur linux-4.9.28.orig/arch/powerpc/Kconfig linux-4.9.28/arch/powerpc/Kconfig
  987. --- linux-4.9.28.orig/arch/powerpc/Kconfig 2017-05-14 14:00:37.000000000 +0200
  988. +++ linux-4.9.28/arch/powerpc/Kconfig 2017-05-19 03:37:25.126174470 +0200
  989. @@ -52,10 +52,11 @@
  990. config RWSEM_GENERIC_SPINLOCK
  991. bool
  992. + default y if PREEMPT_RT_FULL
  993. config RWSEM_XCHGADD_ALGORITHM
  994. bool
  995. - default y
  996. + default y if !PREEMPT_RT_FULL
  997. config GENERIC_LOCKBREAK
  998. bool
  999. @@ -134,6 +135,7 @@
  1000. select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST
  1001. select GENERIC_STRNCPY_FROM_USER
  1002. select GENERIC_STRNLEN_USER
  1003. + select HAVE_PREEMPT_LAZY
  1004. select HAVE_MOD_ARCH_SPECIFIC
  1005. select MODULES_USE_ELF_RELA
  1006. select CLONE_BACKWARDS
  1007. @@ -321,7 +323,7 @@
  1008. config HIGHMEM
  1009. bool "High memory support"
  1010. - depends on PPC32
  1011. + depends on PPC32 && !PREEMPT_RT_FULL
  1012. source kernel/Kconfig.hz
  1013. source kernel/Kconfig.preempt
  1014. diff -Nur linux-4.9.28.orig/arch/powerpc/kernel/asm-offsets.c linux-4.9.28/arch/powerpc/kernel/asm-offsets.c
  1015. --- linux-4.9.28.orig/arch/powerpc/kernel/asm-offsets.c 2017-05-14 14:00:37.000000000 +0200
  1016. +++ linux-4.9.28/arch/powerpc/kernel/asm-offsets.c 2017-05-19 03:37:25.126174470 +0200
  1017. @@ -156,6 +156,7 @@
  1018. DEFINE(TI_FLAGS, offsetof(struct thread_info, flags));
  1019. DEFINE(TI_LOCAL_FLAGS, offsetof(struct thread_info, local_flags));
  1020. DEFINE(TI_PREEMPT, offsetof(struct thread_info, preempt_count));
  1021. + DEFINE(TI_PREEMPT_LAZY, offsetof(struct thread_info, preempt_lazy_count));
  1022. DEFINE(TI_TASK, offsetof(struct thread_info, task));
  1023. DEFINE(TI_CPU, offsetof(struct thread_info, cpu));
  1024. diff -Nur linux-4.9.28.orig/arch/powerpc/kernel/entry_32.S linux-4.9.28/arch/powerpc/kernel/entry_32.S
  1025. --- linux-4.9.28.orig/arch/powerpc/kernel/entry_32.S 2017-05-14 14:00:37.000000000 +0200
  1026. +++ linux-4.9.28/arch/powerpc/kernel/entry_32.S 2017-05-19 03:37:25.126174470 +0200
  1027. @@ -835,7 +835,14 @@
  1028. cmpwi 0,r0,0 /* if non-zero, just restore regs and return */
  1029. bne restore
  1030. andi. r8,r8,_TIF_NEED_RESCHED
  1031. + bne+ 1f
  1032. + lwz r0,TI_PREEMPT_LAZY(r9)
  1033. + cmpwi 0,r0,0 /* if non-zero, just restore regs and return */
  1034. + bne restore
  1035. + lwz r0,TI_FLAGS(r9)
  1036. + andi. r0,r0,_TIF_NEED_RESCHED_LAZY
  1037. beq+ restore
  1038. +1:
  1039. lwz r3,_MSR(r1)
  1040. andi. r0,r3,MSR_EE /* interrupts off? */
  1041. beq restore /* don't schedule if so */
  1042. @@ -846,11 +853,11 @@
  1043. */
  1044. bl trace_hardirqs_off
  1045. #endif
  1046. -1: bl preempt_schedule_irq
  1047. +2: bl preempt_schedule_irq
  1048. CURRENT_THREAD_INFO(r9, r1)
  1049. lwz r3,TI_FLAGS(r9)
  1050. - andi. r0,r3,_TIF_NEED_RESCHED
  1051. - bne- 1b
  1052. + andi. r0,r3,_TIF_NEED_RESCHED_MASK
  1053. + bne- 2b
  1054. #ifdef CONFIG_TRACE_IRQFLAGS
  1055. /* And now, to properly rebalance the above, we tell lockdep they
  1056. * are being turned back on, which will happen when we return
  1057. @@ -1171,7 +1178,7 @@
  1058. #endif /* !(CONFIG_4xx || CONFIG_BOOKE) */
  1059. do_work: /* r10 contains MSR_KERNEL here */
  1060. - andi. r0,r9,_TIF_NEED_RESCHED
  1061. + andi. r0,r9,_TIF_NEED_RESCHED_MASK
  1062. beq do_user_signal
  1063. do_resched: /* r10 contains MSR_KERNEL here */
  1064. @@ -1192,7 +1199,7 @@
  1065. MTMSRD(r10) /* disable interrupts */
  1066. CURRENT_THREAD_INFO(r9, r1)
  1067. lwz r9,TI_FLAGS(r9)
  1068. - andi. r0,r9,_TIF_NEED_RESCHED
  1069. + andi. r0,r9,_TIF_NEED_RESCHED_MASK
  1070. bne- do_resched
  1071. andi. r0,r9,_TIF_USER_WORK_MASK
  1072. beq restore_user
  1073. diff -Nur linux-4.9.28.orig/arch/powerpc/kernel/entry_64.S linux-4.9.28/arch/powerpc/kernel/entry_64.S
  1074. --- linux-4.9.28.orig/arch/powerpc/kernel/entry_64.S 2017-05-14 14:00:37.000000000 +0200
  1075. +++ linux-4.9.28/arch/powerpc/kernel/entry_64.S 2017-05-19 03:37:25.126174470 +0200
  1076. @@ -656,7 +656,7 @@
  1077. bl restore_math
  1078. b restore
  1079. #endif
  1080. -1: andi. r0,r4,_TIF_NEED_RESCHED
  1081. +1: andi. r0,r4,_TIF_NEED_RESCHED_MASK
  1082. beq 2f
  1083. bl restore_interrupts
  1084. SCHEDULE_USER
  1085. @@ -718,10 +718,18 @@
  1086. #ifdef CONFIG_PREEMPT
  1087. /* Check if we need to preempt */
  1088. + lwz r8,TI_PREEMPT(r9)
  1089. + cmpwi 0,r8,0 /* if non-zero, just restore regs and return */
  1090. + bne restore
  1091. andi. r0,r4,_TIF_NEED_RESCHED
  1092. + bne+ check_count
  1093. +
  1094. + andi. r0,r4,_TIF_NEED_RESCHED_LAZY
  1095. beq+ restore
  1096. + lwz r8,TI_PREEMPT_LAZY(r9)
  1097. +
  1098. /* Check that preempt_count() == 0 and interrupts are enabled */
  1099. - lwz r8,TI_PREEMPT(r9)
  1100. +check_count:
  1101. cmpwi cr1,r8,0
  1102. ld r0,SOFTE(r1)
  1103. cmpdi r0,0
  1104. @@ -738,7 +746,7 @@
  1105. /* Re-test flags and eventually loop */
  1106. CURRENT_THREAD_INFO(r9, r1)
  1107. ld r4,TI_FLAGS(r9)
  1108. - andi. r0,r4,_TIF_NEED_RESCHED
  1109. + andi. r0,r4,_TIF_NEED_RESCHED_MASK
  1110. bne 1b
  1111. /*
  1112. diff -Nur linux-4.9.28.orig/arch/powerpc/kernel/irq.c linux-4.9.28/arch/powerpc/kernel/irq.c
  1113. --- linux-4.9.28.orig/arch/powerpc/kernel/irq.c 2017-05-14 14:00:37.000000000 +0200
  1114. +++ linux-4.9.28/arch/powerpc/kernel/irq.c 2017-05-19 03:37:25.126174470 +0200
  1115. @@ -638,6 +638,7 @@
  1116. }
  1117. }
  1118. +#ifndef CONFIG_PREEMPT_RT_FULL
  1119. void do_softirq_own_stack(void)
  1120. {
  1121. struct thread_info *curtp, *irqtp;
  1122. @@ -655,6 +656,7 @@
  1123. if (irqtp->flags)
  1124. set_bits(irqtp->flags, &curtp->flags);
  1125. }
  1126. +#endif
  1127. irq_hw_number_t virq_to_hw(unsigned int virq)
  1128. {
  1129. diff -Nur linux-4.9.28.orig/arch/powerpc/kernel/misc_32.S linux-4.9.28/arch/powerpc/kernel/misc_32.S
  1130. --- linux-4.9.28.orig/arch/powerpc/kernel/misc_32.S 2017-05-14 14:00:37.000000000 +0200
  1131. +++ linux-4.9.28/arch/powerpc/kernel/misc_32.S 2017-05-19 03:37:25.126174470 +0200
  1132. @@ -41,6 +41,7 @@
  1133. * We store the saved ksp_limit in the unused part
  1134. * of the STACK_FRAME_OVERHEAD
  1135. */
  1136. +#ifndef CONFIG_PREEMPT_RT_FULL
  1137. _GLOBAL(call_do_softirq)
  1138. mflr r0
  1139. stw r0,4(r1)
  1140. @@ -57,6 +58,7 @@
  1141. stw r10,THREAD+KSP_LIMIT(r2)
  1142. mtlr r0
  1143. blr
  1144. +#endif
  1145. /*
  1146. * void call_do_irq(struct pt_regs *regs, struct thread_info *irqtp);
  1147. diff -Nur linux-4.9.28.orig/arch/powerpc/kernel/misc_64.S linux-4.9.28/arch/powerpc/kernel/misc_64.S
  1148. --- linux-4.9.28.orig/arch/powerpc/kernel/misc_64.S 2017-05-14 14:00:37.000000000 +0200
  1149. +++ linux-4.9.28/arch/powerpc/kernel/misc_64.S 2017-05-19 03:37:25.126174470 +0200
  1150. @@ -31,6 +31,7 @@
  1151. .text
  1152. +#ifndef CONFIG_PREEMPT_RT_FULL
  1153. _GLOBAL(call_do_softirq)
  1154. mflr r0
  1155. std r0,16(r1)
  1156. @@ -41,6 +42,7 @@
  1157. ld r0,16(r1)
  1158. mtlr r0
  1159. blr
  1160. +#endif
  1161. _GLOBAL(call_do_irq)
  1162. mflr r0
  1163. diff -Nur linux-4.9.28.orig/arch/powerpc/kvm/Kconfig linux-4.9.28/arch/powerpc/kvm/Kconfig
  1164. --- linux-4.9.28.orig/arch/powerpc/kvm/Kconfig 2017-05-14 14:00:37.000000000 +0200
  1165. +++ linux-4.9.28/arch/powerpc/kvm/Kconfig 2017-05-19 03:37:25.126174470 +0200
  1166. @@ -175,6 +175,7 @@
  1167. config KVM_MPIC
  1168. bool "KVM in-kernel MPIC emulation"
  1169. depends on KVM && E500
  1170. + depends on !PREEMPT_RT_FULL
  1171. select HAVE_KVM_IRQCHIP
  1172. select HAVE_KVM_IRQFD
  1173. select HAVE_KVM_IRQ_ROUTING
  1174. diff -Nur linux-4.9.28.orig/arch/powerpc/platforms/ps3/device-init.c linux-4.9.28/arch/powerpc/platforms/ps3/device-init.c
  1175. --- linux-4.9.28.orig/arch/powerpc/platforms/ps3/device-init.c 2017-05-14 14:00:37.000000000 +0200
  1176. +++ linux-4.9.28/arch/powerpc/platforms/ps3/device-init.c 2017-05-19 03:37:25.126174470 +0200
  1177. @@ -752,7 +752,7 @@
  1178. }
  1179. pr_debug("%s:%u: notification %s issued\n", __func__, __LINE__, op);
  1180. - res = wait_event_interruptible(dev->done.wait,
  1181. + res = swait_event_interruptible(dev->done.wait,
  1182. dev->done.done || kthread_should_stop());
  1183. if (kthread_should_stop())
  1184. res = -EINTR;
  1185. diff -Nur linux-4.9.28.orig/arch/sh/kernel/irq.c linux-4.9.28/arch/sh/kernel/irq.c
  1186. --- linux-4.9.28.orig/arch/sh/kernel/irq.c 2017-05-14 14:00:37.000000000 +0200
  1187. +++ linux-4.9.28/arch/sh/kernel/irq.c 2017-05-19 03:37:25.126174470 +0200
  1188. @@ -147,6 +147,7 @@
  1189. hardirq_ctx[cpu] = NULL;
  1190. }
  1191. +#ifndef CONFIG_PREEMPT_RT_FULL
  1192. void do_softirq_own_stack(void)
  1193. {
  1194. struct thread_info *curctx;
  1195. @@ -174,6 +175,7 @@
  1196. "r5", "r6", "r7", "r8", "r9", "r15", "t", "pr"
  1197. );
  1198. }
  1199. +#endif
  1200. #else
  1201. static inline void handle_one_irq(unsigned int irq)
  1202. {
  1203. diff -Nur linux-4.9.28.orig/arch/sparc/Kconfig linux-4.9.28/arch/sparc/Kconfig
  1204. --- linux-4.9.28.orig/arch/sparc/Kconfig 2017-05-14 14:00:37.000000000 +0200
  1205. +++ linux-4.9.28/arch/sparc/Kconfig 2017-05-19 03:37:25.126174470 +0200
  1206. @@ -194,12 +194,10 @@
  1207. source kernel/Kconfig.hz
  1208. config RWSEM_GENERIC_SPINLOCK
  1209. - bool
  1210. - default y if SPARC32
  1211. + def_bool PREEMPT_RT_FULL
  1212. config RWSEM_XCHGADD_ALGORITHM
  1213. - bool
  1214. - default y if SPARC64
  1215. + def_bool !RWSEM_GENERIC_SPINLOCK && !PREEMPT_RT_FULL
  1216. config GENERIC_HWEIGHT
  1217. bool
  1218. diff -Nur linux-4.9.28.orig/arch/sparc/kernel/irq_64.c linux-4.9.28/arch/sparc/kernel/irq_64.c
  1219. --- linux-4.9.28.orig/arch/sparc/kernel/irq_64.c 2017-05-14 14:00:37.000000000 +0200
  1220. +++ linux-4.9.28/arch/sparc/kernel/irq_64.c 2017-05-19 03:37:25.126174470 +0200
  1221. @@ -854,6 +854,7 @@
  1222. set_irq_regs(old_regs);
  1223. }
  1224. +#ifndef CONFIG_PREEMPT_RT_FULL
  1225. void do_softirq_own_stack(void)
  1226. {
  1227. void *orig_sp, *sp = softirq_stack[smp_processor_id()];
  1228. @@ -868,6 +869,7 @@
  1229. __asm__ __volatile__("mov %0, %%sp"
  1230. : : "r" (orig_sp));
  1231. }
  1232. +#endif
  1233. #ifdef CONFIG_HOTPLUG_CPU
  1234. void fixup_irqs(void)
  1235. diff -Nur linux-4.9.28.orig/arch/x86/crypto/aesni-intel_glue.c linux-4.9.28/arch/x86/crypto/aesni-intel_glue.c
  1236. --- linux-4.9.28.orig/arch/x86/crypto/aesni-intel_glue.c 2017-05-14 14:00:37.000000000 +0200
  1237. +++ linux-4.9.28/arch/x86/crypto/aesni-intel_glue.c 2017-05-19 03:37:25.130174719 +0200
  1238. @@ -372,14 +372,14 @@
  1239. err = blkcipher_walk_virt(desc, &walk);
  1240. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  1241. - kernel_fpu_begin();
  1242. while ((nbytes = walk.nbytes)) {
  1243. + kernel_fpu_begin();
  1244. aesni_ecb_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  1245. - nbytes & AES_BLOCK_MASK);
  1246. + nbytes & AES_BLOCK_MASK);
  1247. + kernel_fpu_end();
  1248. nbytes &= AES_BLOCK_SIZE - 1;
  1249. err = blkcipher_walk_done(desc, &walk, nbytes);
  1250. }
  1251. - kernel_fpu_end();
  1252. return err;
  1253. }
  1254. @@ -396,14 +396,14 @@
  1255. err = blkcipher_walk_virt(desc, &walk);
  1256. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  1257. - kernel_fpu_begin();
  1258. while ((nbytes = walk.nbytes)) {
  1259. + kernel_fpu_begin();
  1260. aesni_ecb_dec(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  1261. nbytes & AES_BLOCK_MASK);
  1262. + kernel_fpu_end();
  1263. nbytes &= AES_BLOCK_SIZE - 1;
  1264. err = blkcipher_walk_done(desc, &walk, nbytes);
  1265. }
  1266. - kernel_fpu_end();
  1267. return err;
  1268. }
  1269. @@ -420,14 +420,14 @@
  1270. err = blkcipher_walk_virt(desc, &walk);
  1271. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  1272. - kernel_fpu_begin();
  1273. while ((nbytes = walk.nbytes)) {
  1274. + kernel_fpu_begin();
  1275. aesni_cbc_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  1276. nbytes & AES_BLOCK_MASK, walk.iv);
  1277. + kernel_fpu_end();
  1278. nbytes &= AES_BLOCK_SIZE - 1;
  1279. err = blkcipher_walk_done(desc, &walk, nbytes);
  1280. }
  1281. - kernel_fpu_end();
  1282. return err;
  1283. }
  1284. @@ -444,14 +444,14 @@
  1285. err = blkcipher_walk_virt(desc, &walk);
  1286. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  1287. - kernel_fpu_begin();
  1288. while ((nbytes = walk.nbytes)) {
  1289. + kernel_fpu_begin();
  1290. aesni_cbc_dec(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  1291. nbytes & AES_BLOCK_MASK, walk.iv);
  1292. + kernel_fpu_end();
  1293. nbytes &= AES_BLOCK_SIZE - 1;
  1294. err = blkcipher_walk_done(desc, &walk, nbytes);
  1295. }
  1296. - kernel_fpu_end();
  1297. return err;
  1298. }
  1299. @@ -503,18 +503,20 @@
  1300. err = blkcipher_walk_virt_block(desc, &walk, AES_BLOCK_SIZE);
  1301. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  1302. - kernel_fpu_begin();
  1303. while ((nbytes = walk.nbytes) >= AES_BLOCK_SIZE) {
  1304. + kernel_fpu_begin();
  1305. aesni_ctr_enc_tfm(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  1306. nbytes & AES_BLOCK_MASK, walk.iv);
  1307. + kernel_fpu_end();
  1308. nbytes &= AES_BLOCK_SIZE - 1;
  1309. err = blkcipher_walk_done(desc, &walk, nbytes);
  1310. }
  1311. if (walk.nbytes) {
  1312. + kernel_fpu_begin();
  1313. ctr_crypt_final(ctx, &walk);
  1314. + kernel_fpu_end();
  1315. err = blkcipher_walk_done(desc, &walk, 0);
  1316. }
  1317. - kernel_fpu_end();
  1318. return err;
  1319. }
  1320. diff -Nur linux-4.9.28.orig/arch/x86/crypto/cast5_avx_glue.c linux-4.9.28/arch/x86/crypto/cast5_avx_glue.c
  1321. --- linux-4.9.28.orig/arch/x86/crypto/cast5_avx_glue.c 2017-05-14 14:00:37.000000000 +0200
  1322. +++ linux-4.9.28/arch/x86/crypto/cast5_avx_glue.c 2017-05-19 03:37:25.130174719 +0200
  1323. @@ -59,7 +59,7 @@
  1324. static int ecb_crypt(struct blkcipher_desc *desc, struct blkcipher_walk *walk,
  1325. bool enc)
  1326. {
  1327. - bool fpu_enabled = false;
  1328. + bool fpu_enabled;
  1329. struct cast5_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  1330. const unsigned int bsize = CAST5_BLOCK_SIZE;
  1331. unsigned int nbytes;
  1332. @@ -75,7 +75,7 @@
  1333. u8 *wsrc = walk->src.virt.addr;
  1334. u8 *wdst = walk->dst.virt.addr;
  1335. - fpu_enabled = cast5_fpu_begin(fpu_enabled, nbytes);
  1336. + fpu_enabled = cast5_fpu_begin(false, nbytes);
  1337. /* Process multi-block batch */
  1338. if (nbytes >= bsize * CAST5_PARALLEL_BLOCKS) {
  1339. @@ -103,10 +103,9 @@
  1340. } while (nbytes >= bsize);
  1341. done:
  1342. + cast5_fpu_end(fpu_enabled);
  1343. err = blkcipher_walk_done(desc, walk, nbytes);
  1344. }
  1345. -
  1346. - cast5_fpu_end(fpu_enabled);
  1347. return err;
  1348. }
  1349. @@ -227,7 +226,7 @@
  1350. static int cbc_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  1351. struct scatterlist *src, unsigned int nbytes)
  1352. {
  1353. - bool fpu_enabled = false;
  1354. + bool fpu_enabled;
  1355. struct blkcipher_walk walk;
  1356. int err;
  1357. @@ -236,12 +235,11 @@
  1358. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  1359. while ((nbytes = walk.nbytes)) {
  1360. - fpu_enabled = cast5_fpu_begin(fpu_enabled, nbytes);
  1361. + fpu_enabled = cast5_fpu_begin(false, nbytes);
  1362. nbytes = __cbc_decrypt(desc, &walk);
  1363. + cast5_fpu_end(fpu_enabled);
  1364. err = blkcipher_walk_done(desc, &walk, nbytes);
  1365. }
  1366. -
  1367. - cast5_fpu_end(fpu_enabled);
  1368. return err;
  1369. }
  1370. @@ -311,7 +309,7 @@
  1371. static int ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  1372. struct scatterlist *src, unsigned int nbytes)
  1373. {
  1374. - bool fpu_enabled = false;
  1375. + bool fpu_enabled;
  1376. struct blkcipher_walk walk;
  1377. int err;
  1378. @@ -320,13 +318,12 @@
  1379. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  1380. while ((nbytes = walk.nbytes) >= CAST5_BLOCK_SIZE) {
  1381. - fpu_enabled = cast5_fpu_begin(fpu_enabled, nbytes);
  1382. + fpu_enabled = cast5_fpu_begin(false, nbytes);
  1383. nbytes = __ctr_crypt(desc, &walk);
  1384. + cast5_fpu_end(fpu_enabled);
  1385. err = blkcipher_walk_done(desc, &walk, nbytes);
  1386. }
  1387. - cast5_fpu_end(fpu_enabled);
  1388. -
  1389. if (walk.nbytes) {
  1390. ctr_crypt_final(desc, &walk);
  1391. err = blkcipher_walk_done(desc, &walk, 0);
  1392. diff -Nur linux-4.9.28.orig/arch/x86/crypto/glue_helper.c linux-4.9.28/arch/x86/crypto/glue_helper.c
  1393. --- linux-4.9.28.orig/arch/x86/crypto/glue_helper.c 2017-05-14 14:00:37.000000000 +0200
  1394. +++ linux-4.9.28/arch/x86/crypto/glue_helper.c 2017-05-19 03:37:25.130174719 +0200
  1395. @@ -39,7 +39,7 @@
  1396. void *ctx = crypto_blkcipher_ctx(desc->tfm);
  1397. const unsigned int bsize = 128 / 8;
  1398. unsigned int nbytes, i, func_bytes;
  1399. - bool fpu_enabled = false;
  1400. + bool fpu_enabled;
  1401. int err;
  1402. err = blkcipher_walk_virt(desc, walk);
  1403. @@ -49,7 +49,7 @@
  1404. u8 *wdst = walk->dst.virt.addr;
  1405. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  1406. - desc, fpu_enabled, nbytes);
  1407. + desc, false, nbytes);
  1408. for (i = 0; i < gctx->num_funcs; i++) {
  1409. func_bytes = bsize * gctx->funcs[i].num_blocks;
  1410. @@ -71,10 +71,10 @@
  1411. }
  1412. done:
  1413. + glue_fpu_end(fpu_enabled);
  1414. err = blkcipher_walk_done(desc, walk, nbytes);
  1415. }
  1416. - glue_fpu_end(fpu_enabled);
  1417. return err;
  1418. }
  1419. @@ -194,7 +194,7 @@
  1420. struct scatterlist *src, unsigned int nbytes)
  1421. {
  1422. const unsigned int bsize = 128 / 8;
  1423. - bool fpu_enabled = false;
  1424. + bool fpu_enabled;
  1425. struct blkcipher_walk walk;
  1426. int err;
  1427. @@ -203,12 +203,12 @@
  1428. while ((nbytes = walk.nbytes)) {
  1429. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  1430. - desc, fpu_enabled, nbytes);
  1431. + desc, false, nbytes);
  1432. nbytes = __glue_cbc_decrypt_128bit(gctx, desc, &walk);
  1433. + glue_fpu_end(fpu_enabled);
  1434. err = blkcipher_walk_done(desc, &walk, nbytes);
  1435. }
  1436. - glue_fpu_end(fpu_enabled);
  1437. return err;
  1438. }
  1439. EXPORT_SYMBOL_GPL(glue_cbc_decrypt_128bit);
  1440. @@ -277,7 +277,7 @@
  1441. struct scatterlist *src, unsigned int nbytes)
  1442. {
  1443. const unsigned int bsize = 128 / 8;
  1444. - bool fpu_enabled = false;
  1445. + bool fpu_enabled;
  1446. struct blkcipher_walk walk;
  1447. int err;
  1448. @@ -286,13 +286,12 @@
  1449. while ((nbytes = walk.nbytes) >= bsize) {
  1450. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  1451. - desc, fpu_enabled, nbytes);
  1452. + desc, false, nbytes);
  1453. nbytes = __glue_ctr_crypt_128bit(gctx, desc, &walk);
  1454. + glue_fpu_end(fpu_enabled);
  1455. err = blkcipher_walk_done(desc, &walk, nbytes);
  1456. }
  1457. - glue_fpu_end(fpu_enabled);
  1458. -
  1459. if (walk.nbytes) {
  1460. glue_ctr_crypt_final_128bit(
  1461. gctx->funcs[gctx->num_funcs - 1].fn_u.ctr, desc, &walk);
  1462. @@ -347,7 +346,7 @@
  1463. void *tweak_ctx, void *crypt_ctx)
  1464. {
  1465. const unsigned int bsize = 128 / 8;
  1466. - bool fpu_enabled = false;
  1467. + bool fpu_enabled;
  1468. struct blkcipher_walk walk;
  1469. int err;
  1470. @@ -360,21 +359,21 @@
  1471. /* set minimum length to bsize, for tweak_fn */
  1472. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  1473. - desc, fpu_enabled,
  1474. + desc, false,
  1475. nbytes < bsize ? bsize : nbytes);
  1476. -
  1477. /* calculate first value of T */
  1478. tweak_fn(tweak_ctx, walk.iv, walk.iv);
  1479. + glue_fpu_end(fpu_enabled);
  1480. while (nbytes) {
  1481. + fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  1482. + desc, false, nbytes);
  1483. nbytes = __glue_xts_crypt_128bit(gctx, crypt_ctx, desc, &walk);
  1484. + glue_fpu_end(fpu_enabled);
  1485. err = blkcipher_walk_done(desc, &walk, nbytes);
  1486. nbytes = walk.nbytes;
  1487. }
  1488. -
  1489. - glue_fpu_end(fpu_enabled);
  1490. -
  1491. return err;
  1492. }
  1493. EXPORT_SYMBOL_GPL(glue_xts_crypt_128bit);
  1494. diff -Nur linux-4.9.28.orig/arch/x86/entry/common.c linux-4.9.28/arch/x86/entry/common.c
  1495. --- linux-4.9.28.orig/arch/x86/entry/common.c 2017-05-14 14:00:37.000000000 +0200
  1496. +++ linux-4.9.28/arch/x86/entry/common.c 2017-05-19 03:37:25.130174719 +0200
  1497. @@ -129,7 +129,7 @@
  1498. #define EXIT_TO_USERMODE_LOOP_FLAGS \
  1499. (_TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_UPROBE | \
  1500. - _TIF_NEED_RESCHED | _TIF_USER_RETURN_NOTIFY)
  1501. + _TIF_NEED_RESCHED_MASK | _TIF_USER_RETURN_NOTIFY)
  1502. static void exit_to_usermode_loop(struct pt_regs *regs, u32 cached_flags)
  1503. {
  1504. @@ -145,9 +145,16 @@
  1505. /* We have work to do. */
  1506. local_irq_enable();
  1507. - if (cached_flags & _TIF_NEED_RESCHED)
  1508. + if (cached_flags & _TIF_NEED_RESCHED_MASK)
  1509. schedule();
  1510. +#ifdef ARCH_RT_DELAYS_SIGNAL_SEND
  1511. + if (unlikely(current->forced_info.si_signo)) {
  1512. + struct task_struct *t = current;
  1513. + force_sig_info(t->forced_info.si_signo, &t->forced_info, t);
  1514. + t->forced_info.si_signo = 0;
  1515. + }
  1516. +#endif
  1517. if (cached_flags & _TIF_UPROBE)
  1518. uprobe_notify_resume(regs);
  1519. diff -Nur linux-4.9.28.orig/arch/x86/entry/entry_32.S linux-4.9.28/arch/x86/entry/entry_32.S
  1520. --- linux-4.9.28.orig/arch/x86/entry/entry_32.S 2017-05-14 14:00:37.000000000 +0200
  1521. +++ linux-4.9.28/arch/x86/entry/entry_32.S 2017-05-19 03:37:25.130174719 +0200
  1522. @@ -308,8 +308,25 @@
  1523. ENTRY(resume_kernel)
  1524. DISABLE_INTERRUPTS(CLBR_ANY)
  1525. need_resched:
  1526. + # preempt count == 0 + NEED_RS set?
  1527. cmpl $0, PER_CPU_VAR(__preempt_count)
  1528. +#ifndef CONFIG_PREEMPT_LAZY
  1529. jnz restore_all
  1530. +#else
  1531. + jz test_int_off
  1532. +
  1533. + # atleast preempt count == 0 ?
  1534. + cmpl $_PREEMPT_ENABLED,PER_CPU_VAR(__preempt_count)
  1535. + jne restore_all
  1536. +
  1537. + movl PER_CPU_VAR(current_task), %ebp
  1538. + cmpl $0,TASK_TI_preempt_lazy_count(%ebp) # non-zero preempt_lazy_count ?
  1539. + jnz restore_all
  1540. +
  1541. + testl $_TIF_NEED_RESCHED_LAZY, TASK_TI_flags(%ebp)
  1542. + jz restore_all
  1543. +test_int_off:
  1544. +#endif
  1545. testl $X86_EFLAGS_IF, PT_EFLAGS(%esp) # interrupts off (exception path) ?
  1546. jz restore_all
  1547. call preempt_schedule_irq
  1548. diff -Nur linux-4.9.28.orig/arch/x86/entry/entry_64.S linux-4.9.28/arch/x86/entry/entry_64.S
  1549. --- linux-4.9.28.orig/arch/x86/entry/entry_64.S 2017-05-14 14:00:37.000000000 +0200
  1550. +++ linux-4.9.28/arch/x86/entry/entry_64.S 2017-05-19 03:37:25.130174719 +0200
  1551. @@ -546,7 +546,23 @@
  1552. bt $9, EFLAGS(%rsp) /* were interrupts off? */
  1553. jnc 1f
  1554. 0: cmpl $0, PER_CPU_VAR(__preempt_count)
  1555. +#ifndef CONFIG_PREEMPT_LAZY
  1556. jnz 1f
  1557. +#else
  1558. + jz do_preempt_schedule_irq
  1559. +
  1560. + # atleast preempt count == 0 ?
  1561. + cmpl $_PREEMPT_ENABLED,PER_CPU_VAR(__preempt_count)
  1562. + jnz 1f
  1563. +
  1564. + movq PER_CPU_VAR(current_task), %rcx
  1565. + cmpl $0, TASK_TI_preempt_lazy_count(%rcx)
  1566. + jnz 1f
  1567. +
  1568. + bt $TIF_NEED_RESCHED_LAZY,TASK_TI_flags(%rcx)
  1569. + jnc 1f
  1570. +do_preempt_schedule_irq:
  1571. +#endif
  1572. call preempt_schedule_irq
  1573. jmp 0b
  1574. 1:
  1575. @@ -894,6 +910,7 @@
  1576. jmp 2b
  1577. .previous
  1578. +#ifndef CONFIG_PREEMPT_RT_FULL
  1579. /* Call softirq on interrupt stack. Interrupts are off. */
  1580. ENTRY(do_softirq_own_stack)
  1581. pushq %rbp
  1582. @@ -906,6 +923,7 @@
  1583. decl PER_CPU_VAR(irq_count)
  1584. ret
  1585. END(do_softirq_own_stack)
  1586. +#endif
  1587. #ifdef CONFIG_XEN
  1588. idtentry xen_hypervisor_callback xen_do_hypervisor_callback has_error_code=0
  1589. diff -Nur linux-4.9.28.orig/arch/x86/include/asm/preempt.h linux-4.9.28/arch/x86/include/asm/preempt.h
  1590. --- linux-4.9.28.orig/arch/x86/include/asm/preempt.h 2017-05-14 14:00:37.000000000 +0200
  1591. +++ linux-4.9.28/arch/x86/include/asm/preempt.h 2017-05-19 03:37:25.130174719 +0200
  1592. @@ -79,17 +79,46 @@
  1593. * a decrement which hits zero means we have no preempt_count and should
  1594. * reschedule.
  1595. */
  1596. -static __always_inline bool __preempt_count_dec_and_test(void)
  1597. +static __always_inline bool ____preempt_count_dec_and_test(void)
  1598. {
  1599. GEN_UNARY_RMWcc("decl", __preempt_count, __percpu_arg(0), e);
  1600. }
  1601. +static __always_inline bool __preempt_count_dec_and_test(void)
  1602. +{
  1603. + if (____preempt_count_dec_and_test())
  1604. + return true;
  1605. +#ifdef CONFIG_PREEMPT_LAZY
  1606. + if (current_thread_info()->preempt_lazy_count)
  1607. + return false;
  1608. + return test_thread_flag(TIF_NEED_RESCHED_LAZY);
  1609. +#else
  1610. + return false;
  1611. +#endif
  1612. +}
  1613. +
  1614. /*
  1615. * Returns true when we need to resched and can (barring IRQ state).
  1616. */
  1617. static __always_inline bool should_resched(int preempt_offset)
  1618. {
  1619. +#ifdef CONFIG_PREEMPT_LAZY
  1620. + u32 tmp;
  1621. +
  1622. + tmp = raw_cpu_read_4(__preempt_count);
  1623. + if (tmp == preempt_offset)
  1624. + return true;
  1625. +
  1626. + /* preempt count == 0 ? */
  1627. + tmp &= ~PREEMPT_NEED_RESCHED;
  1628. + if (tmp)
  1629. + return false;
  1630. + if (current_thread_info()->preempt_lazy_count)
  1631. + return false;
  1632. + return test_thread_flag(TIF_NEED_RESCHED_LAZY);
  1633. +#else
  1634. return unlikely(raw_cpu_read_4(__preempt_count) == preempt_offset);
  1635. +#endif
  1636. }
  1637. #ifdef CONFIG_PREEMPT
  1638. diff -Nur linux-4.9.28.orig/arch/x86/include/asm/signal.h linux-4.9.28/arch/x86/include/asm/signal.h
  1639. --- linux-4.9.28.orig/arch/x86/include/asm/signal.h 2017-05-14 14:00:37.000000000 +0200
  1640. +++ linux-4.9.28/arch/x86/include/asm/signal.h 2017-05-19 03:37:25.130174719 +0200
  1641. @@ -27,6 +27,19 @@
  1642. #define SA_IA32_ABI 0x02000000u
  1643. #define SA_X32_ABI 0x01000000u
  1644. +/*
  1645. + * Because some traps use the IST stack, we must keep preemption
  1646. + * disabled while calling do_trap(), but do_trap() may call
  1647. + * force_sig_info() which will grab the signal spin_locks for the
  1648. + * task, which in PREEMPT_RT_FULL are mutexes. By defining
  1649. + * ARCH_RT_DELAYS_SIGNAL_SEND the force_sig_info() will set
  1650. + * TIF_NOTIFY_RESUME and set up the signal to be sent on exit of the
  1651. + * trap.
  1652. + */
  1653. +#if defined(CONFIG_PREEMPT_RT_FULL)
  1654. +#define ARCH_RT_DELAYS_SIGNAL_SEND
  1655. +#endif
  1656. +
  1657. #ifndef CONFIG_COMPAT
  1658. typedef sigset_t compat_sigset_t;
  1659. #endif
  1660. diff -Nur linux-4.9.28.orig/arch/x86/include/asm/stackprotector.h linux-4.9.28/arch/x86/include/asm/stackprotector.h
  1661. --- linux-4.9.28.orig/arch/x86/include/asm/stackprotector.h 2017-05-14 14:00:37.000000000 +0200
  1662. +++ linux-4.9.28/arch/x86/include/asm/stackprotector.h 2017-05-19 03:37:25.130174719 +0200
  1663. @@ -59,7 +59,7 @@
  1664. */
  1665. static __always_inline void boot_init_stack_canary(void)
  1666. {
  1667. - u64 canary;
  1668. + u64 uninitialized_var(canary);
  1669. u64 tsc;
  1670. #ifdef CONFIG_X86_64
  1671. @@ -70,8 +70,15 @@
  1672. * of randomness. The TSC only matters for very early init,
  1673. * there it already has some randomness on most systems. Later
  1674. * on during the bootup the random pool has true entropy too.
  1675. + *
  1676. + * For preempt-rt we need to weaken the randomness a bit, as
  1677. + * we can't call into the random generator from atomic context
  1678. + * due to locking constraints. We just leave canary
  1679. + * uninitialized and use the TSC based randomness on top of it.
  1680. */
  1681. +#ifndef CONFIG_PREEMPT_RT_FULL
  1682. get_random_bytes(&canary, sizeof(canary));
  1683. +#endif
  1684. tsc = rdtsc();
  1685. canary += tsc + (tsc << 32UL);
  1686. diff -Nur linux-4.9.28.orig/arch/x86/include/asm/thread_info.h linux-4.9.28/arch/x86/include/asm/thread_info.h
  1687. --- linux-4.9.28.orig/arch/x86/include/asm/thread_info.h 2017-05-14 14:00:37.000000000 +0200
  1688. +++ linux-4.9.28/arch/x86/include/asm/thread_info.h 2017-05-19 03:37:25.130174719 +0200
  1689. @@ -54,11 +54,14 @@
  1690. struct thread_info {
  1691. unsigned long flags; /* low level flags */
  1692. + int preempt_lazy_count; /* 0 => lazy preemptable
  1693. + <0 => BUG */
  1694. };
  1695. #define INIT_THREAD_INFO(tsk) \
  1696. { \
  1697. .flags = 0, \
  1698. + .preempt_lazy_count = 0, \
  1699. }
  1700. #define init_stack (init_thread_union.stack)
  1701. @@ -67,6 +70,10 @@
  1702. #include <asm/asm-offsets.h>
  1703. +#define GET_THREAD_INFO(reg) \
  1704. + _ASM_MOV PER_CPU_VAR(cpu_current_top_of_stack),reg ; \
  1705. + _ASM_SUB $(THREAD_SIZE),reg ;
  1706. +
  1707. #endif
  1708. /*
  1709. @@ -85,6 +92,7 @@
  1710. #define TIF_SYSCALL_EMU 6 /* syscall emulation active */
  1711. #define TIF_SYSCALL_AUDIT 7 /* syscall auditing active */
  1712. #define TIF_SECCOMP 8 /* secure computing */
  1713. +#define TIF_NEED_RESCHED_LAZY 9 /* lazy rescheduling necessary */
  1714. #define TIF_USER_RETURN_NOTIFY 11 /* notify kernel of userspace return */
  1715. #define TIF_UPROBE 12 /* breakpointed or singlestepping */
  1716. #define TIF_NOTSC 16 /* TSC is not accessible in userland */
  1717. @@ -108,6 +116,7 @@
  1718. #define _TIF_SYSCALL_EMU (1 << TIF_SYSCALL_EMU)
  1719. #define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
  1720. #define _TIF_SECCOMP (1 << TIF_SECCOMP)
  1721. +#define _TIF_NEED_RESCHED_LAZY (1 << TIF_NEED_RESCHED_LAZY)
  1722. #define _TIF_USER_RETURN_NOTIFY (1 << TIF_USER_RETURN_NOTIFY)
  1723. #define _TIF_UPROBE (1 << TIF_UPROBE)
  1724. #define _TIF_NOTSC (1 << TIF_NOTSC)
  1725. @@ -143,6 +152,8 @@
  1726. #define _TIF_WORK_CTXSW_PREV (_TIF_WORK_CTXSW|_TIF_USER_RETURN_NOTIFY)
  1727. #define _TIF_WORK_CTXSW_NEXT (_TIF_WORK_CTXSW)
  1728. +#define _TIF_NEED_RESCHED_MASK (_TIF_NEED_RESCHED | _TIF_NEED_RESCHED_LAZY)
  1729. +
  1730. #define STACK_WARN (THREAD_SIZE/8)
  1731. /*
  1732. diff -Nur linux-4.9.28.orig/arch/x86/include/asm/uv/uv_bau.h linux-4.9.28/arch/x86/include/asm/uv/uv_bau.h
  1733. --- linux-4.9.28.orig/arch/x86/include/asm/uv/uv_bau.h 2017-05-14 14:00:37.000000000 +0200
  1734. +++ linux-4.9.28/arch/x86/include/asm/uv/uv_bau.h 2017-05-19 03:37:25.130174719 +0200
  1735. @@ -624,9 +624,9 @@
  1736. cycles_t send_message;
  1737. cycles_t period_end;
  1738. cycles_t period_time;
  1739. - spinlock_t uvhub_lock;
  1740. - spinlock_t queue_lock;
  1741. - spinlock_t disable_lock;
  1742. + raw_spinlock_t uvhub_lock;
  1743. + raw_spinlock_t queue_lock;
  1744. + raw_spinlock_t disable_lock;
  1745. /* tunables */
  1746. int max_concurr;
  1747. int max_concurr_const;
  1748. @@ -815,15 +815,15 @@
  1749. * to be lowered below the current 'v'. atomic_add_unless can only stop
  1750. * on equal.
  1751. */
  1752. -static inline int atomic_inc_unless_ge(spinlock_t *lock, atomic_t *v, int u)
  1753. +static inline int atomic_inc_unless_ge(raw_spinlock_t *lock, atomic_t *v, int u)
  1754. {
  1755. - spin_lock(lock);
  1756. + raw_spin_lock(lock);
  1757. if (atomic_read(v) >= u) {
  1758. - spin_unlock(lock);
  1759. + raw_spin_unlock(lock);
  1760. return 0;
  1761. }
  1762. atomic_inc(v);
  1763. - spin_unlock(lock);
  1764. + raw_spin_unlock(lock);
  1765. return 1;
  1766. }
  1767. diff -Nur linux-4.9.28.orig/arch/x86/Kconfig linux-4.9.28/arch/x86/Kconfig
  1768. --- linux-4.9.28.orig/arch/x86/Kconfig 2017-05-14 14:00:37.000000000 +0200
  1769. +++ linux-4.9.28/arch/x86/Kconfig 2017-05-19 03:37:25.130174719 +0200
  1770. @@ -17,6 +17,7 @@
  1771. ### Arch settings
  1772. config X86
  1773. def_bool y
  1774. + select HAVE_PREEMPT_LAZY
  1775. select ACPI_LEGACY_TABLES_LOOKUP if ACPI
  1776. select ACPI_SYSTEM_POWER_STATES_SUPPORT if ACPI
  1777. select ANON_INODES
  1778. @@ -232,8 +233,11 @@
  1779. def_bool y
  1780. depends on ISA_DMA_API
  1781. +config RWSEM_GENERIC_SPINLOCK
  1782. + def_bool PREEMPT_RT_FULL
  1783. +
  1784. config RWSEM_XCHGADD_ALGORITHM
  1785. - def_bool y
  1786. + def_bool !RWSEM_GENERIC_SPINLOCK && !PREEMPT_RT_FULL
  1787. config GENERIC_CALIBRATE_DELAY
  1788. def_bool y
  1789. @@ -897,7 +901,7 @@
  1790. config MAXSMP
  1791. bool "Enable Maximum number of SMP Processors and NUMA Nodes"
  1792. depends on X86_64 && SMP && DEBUG_KERNEL
  1793. - select CPUMASK_OFFSTACK
  1794. + select CPUMASK_OFFSTACK if !PREEMPT_RT_FULL
  1795. ---help---
  1796. Enable maximum number of CPUS and NUMA Nodes for this architecture.
  1797. If unsure, say N.
  1798. diff -Nur linux-4.9.28.orig/arch/x86/kernel/acpi/boot.c linux-4.9.28/arch/x86/kernel/acpi/boot.c
  1799. --- linux-4.9.28.orig/arch/x86/kernel/acpi/boot.c 2017-05-14 14:00:37.000000000 +0200
  1800. +++ linux-4.9.28/arch/x86/kernel/acpi/boot.c 2017-05-19 03:37:25.130174719 +0200
  1801. @@ -87,7 +87,9 @@
  1802. * ->ioapic_mutex
  1803. * ->ioapic_lock
  1804. */
  1805. +#ifdef CONFIG_X86_IO_APIC
  1806. static DEFINE_MUTEX(acpi_ioapic_lock);
  1807. +#endif
  1808. /* --------------------------------------------------------------------------
  1809. Boot-time Configuration
  1810. diff -Nur linux-4.9.28.orig/arch/x86/kernel/apic/io_apic.c linux-4.9.28/arch/x86/kernel/apic/io_apic.c
  1811. --- linux-4.9.28.orig/arch/x86/kernel/apic/io_apic.c 2017-05-14 14:00:37.000000000 +0200
  1812. +++ linux-4.9.28/arch/x86/kernel/apic/io_apic.c 2017-05-19 03:37:25.130174719 +0200
  1813. @@ -1712,7 +1712,8 @@
  1814. static inline bool ioapic_irqd_mask(struct irq_data *data)
  1815. {
  1816. /* If we are moving the irq we need to mask it */
  1817. - if (unlikely(irqd_is_setaffinity_pending(data))) {
  1818. + if (unlikely(irqd_is_setaffinity_pending(data) &&
  1819. + !irqd_irq_inprogress(data))) {
  1820. mask_ioapic_irq(data);
  1821. return true;
  1822. }
  1823. diff -Nur linux-4.9.28.orig/arch/x86/kernel/asm-offsets.c linux-4.9.28/arch/x86/kernel/asm-offsets.c
  1824. --- linux-4.9.28.orig/arch/x86/kernel/asm-offsets.c 2017-05-14 14:00:37.000000000 +0200
  1825. +++ linux-4.9.28/arch/x86/kernel/asm-offsets.c 2017-05-19 03:37:25.130174719 +0200
  1826. @@ -36,6 +36,7 @@
  1827. BLANK();
  1828. OFFSET(TASK_TI_flags, task_struct, thread_info.flags);
  1829. + OFFSET(TASK_TI_preempt_lazy_count, task_struct, thread_info.preempt_lazy_count);
  1830. OFFSET(TASK_addr_limit, task_struct, thread.addr_limit);
  1831. BLANK();
  1832. @@ -91,4 +92,5 @@
  1833. BLANK();
  1834. DEFINE(PTREGS_SIZE, sizeof(struct pt_regs));
  1835. + DEFINE(_PREEMPT_ENABLED, PREEMPT_ENABLED);
  1836. }
  1837. diff -Nur linux-4.9.28.orig/arch/x86/kernel/cpu/mcheck/mce.c linux-4.9.28/arch/x86/kernel/cpu/mcheck/mce.c
  1838. --- linux-4.9.28.orig/arch/x86/kernel/cpu/mcheck/mce.c 2017-05-14 14:00:37.000000000 +0200
  1839. +++ linux-4.9.28/arch/x86/kernel/cpu/mcheck/mce.c 2017-05-19 03:37:25.130174719 +0200
  1840. @@ -41,6 +41,8 @@
  1841. #include <linux/debugfs.h>
  1842. #include <linux/irq_work.h>
  1843. #include <linux/export.h>
  1844. +#include <linux/jiffies.h>
  1845. +#include <linux/swork.h>
  1846. #include <linux/jump_label.h>
  1847. #include <asm/processor.h>
  1848. @@ -1307,7 +1309,7 @@
  1849. static unsigned long check_interval = INITIAL_CHECK_INTERVAL;
  1850. static DEFINE_PER_CPU(unsigned long, mce_next_interval); /* in jiffies */
  1851. -static DEFINE_PER_CPU(struct timer_list, mce_timer);
  1852. +static DEFINE_PER_CPU(struct hrtimer, mce_timer);
  1853. static unsigned long mce_adjust_timer_default(unsigned long interval)
  1854. {
  1855. @@ -1316,32 +1318,18 @@
  1856. static unsigned long (*mce_adjust_timer)(unsigned long interval) = mce_adjust_timer_default;
  1857. -static void __restart_timer(struct timer_list *t, unsigned long interval)
  1858. +static enum hrtimer_restart __restart_timer(struct hrtimer *timer, unsigned long interval)
  1859. {
  1860. - unsigned long when = jiffies + interval;
  1861. - unsigned long flags;
  1862. -
  1863. - local_irq_save(flags);
  1864. -
  1865. - if (timer_pending(t)) {
  1866. - if (time_before(when, t->expires))
  1867. - mod_timer(t, when);
  1868. - } else {
  1869. - t->expires = round_jiffies(when);
  1870. - add_timer_on(t, smp_processor_id());
  1871. - }
  1872. -
  1873. - local_irq_restore(flags);
  1874. + if (!interval)
  1875. + return HRTIMER_NORESTART;
  1876. + hrtimer_forward_now(timer, ns_to_ktime(jiffies_to_nsecs(interval)));
  1877. + return HRTIMER_RESTART;
  1878. }
  1879. -static void mce_timer_fn(unsigned long data)
  1880. +static enum hrtimer_restart mce_timer_fn(struct hrtimer *timer)
  1881. {
  1882. - struct timer_list *t = this_cpu_ptr(&mce_timer);
  1883. - int cpu = smp_processor_id();
  1884. unsigned long iv;
  1885. - WARN_ON(cpu != data);
  1886. -
  1887. iv = __this_cpu_read(mce_next_interval);
  1888. if (mce_available(this_cpu_ptr(&cpu_info))) {
  1889. @@ -1364,7 +1352,7 @@
  1890. done:
  1891. __this_cpu_write(mce_next_interval, iv);
  1892. - __restart_timer(t, iv);
  1893. + return __restart_timer(timer, iv);
  1894. }
  1895. /*
  1896. @@ -1372,7 +1360,7 @@
  1897. */
  1898. void mce_timer_kick(unsigned long interval)
  1899. {
  1900. - struct timer_list *t = this_cpu_ptr(&mce_timer);
  1901. + struct hrtimer *t = this_cpu_ptr(&mce_timer);
  1902. unsigned long iv = __this_cpu_read(mce_next_interval);
  1903. __restart_timer(t, interval);
  1904. @@ -1387,7 +1375,7 @@
  1905. int cpu;
  1906. for_each_online_cpu(cpu)
  1907. - del_timer_sync(&per_cpu(mce_timer, cpu));
  1908. + hrtimer_cancel(&per_cpu(mce_timer, cpu));
  1909. }
  1910. static void mce_do_trigger(struct work_struct *work)
  1911. @@ -1397,6 +1385,56 @@
  1912. static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
  1913. +static void __mce_notify_work(struct swork_event *event)
  1914. +{
  1915. + /* Not more than two messages every minute */
  1916. + static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
  1917. +
  1918. + /* wake processes polling /dev/mcelog */
  1919. + wake_up_interruptible(&mce_chrdev_wait);
  1920. +
  1921. + /*
  1922. + * There is no risk of missing notifications because
  1923. + * work_pending is always cleared before the function is
  1924. + * executed.
  1925. + */
  1926. + if (mce_helper[0] && !work_pending(&mce_trigger_work))
  1927. + schedule_work(&mce_trigger_work);
  1928. +
  1929. + if (__ratelimit(&ratelimit))
  1930. + pr_info(HW_ERR "Machine check events logged\n");
  1931. +}
  1932. +
  1933. +#ifdef CONFIG_PREEMPT_RT_FULL
  1934. +static bool notify_work_ready __read_mostly;
  1935. +static struct swork_event notify_work;
  1936. +
  1937. +static int mce_notify_work_init(void)
  1938. +{
  1939. + int err;
  1940. +
  1941. + err = swork_get();
  1942. + if (err)
  1943. + return err;
  1944. +
  1945. + INIT_SWORK(&notify_work, __mce_notify_work);
  1946. + notify_work_ready = true;
  1947. + return 0;
  1948. +}
  1949. +
  1950. +static void mce_notify_work(void)
  1951. +{
  1952. + if (notify_work_ready)
  1953. + swork_queue(&notify_work);
  1954. +}
  1955. +#else
  1956. +static void mce_notify_work(void)
  1957. +{
  1958. + __mce_notify_work(NULL);
  1959. +}
  1960. +static inline int mce_notify_work_init(void) { return 0; }
  1961. +#endif
  1962. +
  1963. /*
  1964. * Notify the user(s) about new machine check events.
  1965. * Can be called from interrupt context, but not from machine check/NMI
  1966. @@ -1404,19 +1442,8 @@
  1967. */
  1968. int mce_notify_irq(void)
  1969. {
  1970. - /* Not more than two messages every minute */
  1971. - static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
  1972. -
  1973. if (test_and_clear_bit(0, &mce_need_notify)) {
  1974. - /* wake processes polling /dev/mcelog */
  1975. - wake_up_interruptible(&mce_chrdev_wait);
  1976. -
  1977. - if (mce_helper[0])
  1978. - schedule_work(&mce_trigger_work);
  1979. -
  1980. - if (__ratelimit(&ratelimit))
  1981. - pr_info(HW_ERR "Machine check events logged\n");
  1982. -
  1983. + mce_notify_work();
  1984. return 1;
  1985. }
  1986. return 0;
  1987. @@ -1722,7 +1749,7 @@
  1988. }
  1989. }
  1990. -static void mce_start_timer(unsigned int cpu, struct timer_list *t)
  1991. +static void mce_start_timer(unsigned int cpu, struct hrtimer *t)
  1992. {
  1993. unsigned long iv = check_interval * HZ;
  1994. @@ -1731,16 +1758,17 @@
  1995. per_cpu(mce_next_interval, cpu) = iv;
  1996. - t->expires = round_jiffies(jiffies + iv);
  1997. - add_timer_on(t, cpu);
  1998. + hrtimer_start_range_ns(t, ns_to_ktime(jiffies_to_usecs(iv) * 1000ULL),
  1999. + 0, HRTIMER_MODE_REL_PINNED);
  2000. }
  2001. static void __mcheck_cpu_init_timer(void)
  2002. {
  2003. - struct timer_list *t = this_cpu_ptr(&mce_timer);
  2004. + struct hrtimer *t = this_cpu_ptr(&mce_timer);
  2005. unsigned int cpu = smp_processor_id();
  2006. - setup_pinned_timer(t, mce_timer_fn, cpu);
  2007. + hrtimer_init(t, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  2008. + t->function = mce_timer_fn;
  2009. mce_start_timer(cpu, t);
  2010. }
  2011. @@ -2465,6 +2493,8 @@
  2012. if (!mce_available(raw_cpu_ptr(&cpu_info)))
  2013. return;
  2014. + hrtimer_cancel(this_cpu_ptr(&mce_timer));
  2015. +
  2016. if (!(action & CPU_TASKS_FROZEN))
  2017. cmci_clear();
  2018. @@ -2487,6 +2517,7 @@
  2019. if (b->init)
  2020. wrmsrl(msr_ops.ctl(i), b->ctl);
  2021. }
  2022. + __mcheck_cpu_init_timer();
  2023. }
  2024. /* Get notified when a cpu comes on/off. Be hotplug friendly. */
  2025. @@ -2494,7 +2525,6 @@
  2026. mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
  2027. {
  2028. unsigned int cpu = (unsigned long)hcpu;
  2029. - struct timer_list *t = &per_cpu(mce_timer, cpu);
  2030. switch (action & ~CPU_TASKS_FROZEN) {
  2031. case CPU_ONLINE:
  2032. @@ -2514,11 +2544,9 @@
  2033. break;
  2034. case CPU_DOWN_PREPARE:
  2035. smp_call_function_single(cpu, mce_disable_cpu, &action, 1);
  2036. - del_timer_sync(t);
  2037. break;
  2038. case CPU_DOWN_FAILED:
  2039. smp_call_function_single(cpu, mce_reenable_cpu, &action, 1);
  2040. - mce_start_timer(cpu, t);
  2041. break;
  2042. }
  2043. @@ -2557,6 +2585,10 @@
  2044. goto err_out;
  2045. }
  2046. + err = mce_notify_work_init();
  2047. + if (err)
  2048. + goto err_out;
  2049. +
  2050. if (!zalloc_cpumask_var(&mce_device_initialized, GFP_KERNEL)) {
  2051. err = -ENOMEM;
  2052. goto err_out;
  2053. diff -Nur linux-4.9.28.orig/arch/x86/kernel/irq_32.c linux-4.9.28/arch/x86/kernel/irq_32.c
  2054. --- linux-4.9.28.orig/arch/x86/kernel/irq_32.c 2017-05-14 14:00:37.000000000 +0200
  2055. +++ linux-4.9.28/arch/x86/kernel/irq_32.c 2017-05-19 03:37:25.130174719 +0200
  2056. @@ -127,6 +127,7 @@
  2057. cpu, per_cpu(hardirq_stack, cpu), per_cpu(softirq_stack, cpu));
  2058. }
  2059. +#ifndef CONFIG_PREEMPT_RT_FULL
  2060. void do_softirq_own_stack(void)
  2061. {
  2062. struct irq_stack *irqstk;
  2063. @@ -143,6 +144,7 @@
  2064. call_on_stack(__do_softirq, isp);
  2065. }
  2066. +#endif
  2067. bool handle_irq(struct irq_desc *desc, struct pt_regs *regs)
  2068. {
  2069. diff -Nur linux-4.9.28.orig/arch/x86/kernel/process_32.c linux-4.9.28/arch/x86/kernel/process_32.c
  2070. --- linux-4.9.28.orig/arch/x86/kernel/process_32.c 2017-05-14 14:00:37.000000000 +0200
  2071. +++ linux-4.9.28/arch/x86/kernel/process_32.c 2017-05-19 03:37:25.130174719 +0200
  2072. @@ -35,6 +35,7 @@
  2073. #include <linux/uaccess.h>
  2074. #include <linux/io.h>
  2075. #include <linux/kdebug.h>
  2076. +#include <linux/highmem.h>
  2077. #include <asm/pgtable.h>
  2078. #include <asm/ldt.h>
  2079. @@ -195,6 +196,35 @@
  2080. }
  2081. EXPORT_SYMBOL_GPL(start_thread);
  2082. +#ifdef CONFIG_PREEMPT_RT_FULL
  2083. +static void switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p)
  2084. +{
  2085. + int i;
  2086. +
  2087. + /*
  2088. + * Clear @prev's kmap_atomic mappings
  2089. + */
  2090. + for (i = 0; i < prev_p->kmap_idx; i++) {
  2091. + int idx = i + KM_TYPE_NR * smp_processor_id();
  2092. + pte_t *ptep = kmap_pte - idx;
  2093. +
  2094. + kpte_clear_flush(ptep, __fix_to_virt(FIX_KMAP_BEGIN + idx));
  2095. + }
  2096. + /*
  2097. + * Restore @next_p's kmap_atomic mappings
  2098. + */
  2099. + for (i = 0; i < next_p->kmap_idx; i++) {
  2100. + int idx = i + KM_TYPE_NR * smp_processor_id();
  2101. +
  2102. + if (!pte_none(next_p->kmap_pte[i]))
  2103. + set_pte(kmap_pte - idx, next_p->kmap_pte[i]);
  2104. + }
  2105. +}
  2106. +#else
  2107. +static inline void
  2108. +switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p) { }
  2109. +#endif
  2110. +
  2111. /*
  2112. * switch_to(x,y) should switch tasks from x to y.
  2113. @@ -271,6 +301,8 @@
  2114. task_thread_info(next_p)->flags & _TIF_WORK_CTXSW_NEXT))
  2115. __switch_to_xtra(prev_p, next_p, tss);
  2116. + switch_kmaps(prev_p, next_p);
  2117. +
  2118. /*
  2119. * Leave lazy mode, flushing any hypercalls made here.
  2120. * This must be done before restoring TLS segments so
  2121. diff -Nur linux-4.9.28.orig/arch/x86/kvm/lapic.c linux-4.9.28/arch/x86/kvm/lapic.c
  2122. --- linux-4.9.28.orig/arch/x86/kvm/lapic.c 2017-05-14 14:00:37.000000000 +0200
  2123. +++ linux-4.9.28/arch/x86/kvm/lapic.c 2017-05-19 03:37:25.134174924 +0200
  2124. @@ -1939,6 +1939,7 @@
  2125. hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
  2126. HRTIMER_MODE_ABS_PINNED);
  2127. apic->lapic_timer.timer.function = apic_timer_fn;
  2128. + apic->lapic_timer.timer.irqsafe = 1;
  2129. /*
  2130. * APIC is created enabled. This will prevent kvm_lapic_set_base from
  2131. diff -Nur linux-4.9.28.orig/arch/x86/kvm/x86.c linux-4.9.28/arch/x86/kvm/x86.c
  2132. --- linux-4.9.28.orig/arch/x86/kvm/x86.c 2017-05-14 14:00:37.000000000 +0200
  2133. +++ linux-4.9.28/arch/x86/kvm/x86.c 2017-05-19 03:37:25.134174924 +0200
  2134. @@ -5933,6 +5933,13 @@
  2135. goto out;
  2136. }
  2137. +#ifdef CONFIG_PREEMPT_RT_FULL
  2138. + if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
  2139. + printk(KERN_ERR "RT requires X86_FEATURE_CONSTANT_TSC\n");
  2140. + return -EOPNOTSUPP;
  2141. + }
  2142. +#endif
  2143. +
  2144. r = kvm_mmu_module_init();
  2145. if (r)
  2146. goto out_free_percpu;
  2147. diff -Nur linux-4.9.28.orig/arch/x86/mm/highmem_32.c linux-4.9.28/arch/x86/mm/highmem_32.c
  2148. --- linux-4.9.28.orig/arch/x86/mm/highmem_32.c 2017-05-14 14:00:37.000000000 +0200
  2149. +++ linux-4.9.28/arch/x86/mm/highmem_32.c 2017-05-19 03:37:25.134174924 +0200
  2150. @@ -32,10 +32,11 @@
  2151. */
  2152. void *kmap_atomic_prot(struct page *page, pgprot_t prot)
  2153. {
  2154. + pte_t pte = mk_pte(page, prot);
  2155. unsigned long vaddr;
  2156. int idx, type;
  2157. - preempt_disable();
  2158. + preempt_disable_nort();
  2159. pagefault_disable();
  2160. if (!PageHighMem(page))
  2161. @@ -45,7 +46,10 @@
  2162. idx = type + KM_TYPE_NR*smp_processor_id();
  2163. vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
  2164. BUG_ON(!pte_none(*(kmap_pte-idx)));
  2165. - set_pte(kmap_pte-idx, mk_pte(page, prot));
  2166. +#ifdef CONFIG_PREEMPT_RT_FULL
  2167. + current->kmap_pte[type] = pte;
  2168. +#endif
  2169. + set_pte(kmap_pte-idx, pte);
  2170. arch_flush_lazy_mmu_mode();
  2171. return (void *)vaddr;
  2172. @@ -88,6 +92,9 @@
  2173. * is a bad idea also, in case the page changes cacheability
  2174. * attributes or becomes a protected page in a hypervisor.
  2175. */
  2176. +#ifdef CONFIG_PREEMPT_RT_FULL
  2177. + current->kmap_pte[type] = __pte(0);
  2178. +#endif
  2179. kpte_clear_flush(kmap_pte-idx, vaddr);
  2180. kmap_atomic_idx_pop();
  2181. arch_flush_lazy_mmu_mode();
  2182. @@ -100,7 +107,7 @@
  2183. #endif
  2184. pagefault_enable();
  2185. - preempt_enable();
  2186. + preempt_enable_nort();
  2187. }
  2188. EXPORT_SYMBOL(__kunmap_atomic);
  2189. diff -Nur linux-4.9.28.orig/arch/x86/mm/iomap_32.c linux-4.9.28/arch/x86/mm/iomap_32.c
  2190. --- linux-4.9.28.orig/arch/x86/mm/iomap_32.c 2017-05-14 14:00:37.000000000 +0200
  2191. +++ linux-4.9.28/arch/x86/mm/iomap_32.c 2017-05-19 03:37:25.134174924 +0200
  2192. @@ -56,6 +56,7 @@
  2193. void *kmap_atomic_prot_pfn(unsigned long pfn, pgprot_t prot)
  2194. {
  2195. + pte_t pte = pfn_pte(pfn, prot);
  2196. unsigned long vaddr;
  2197. int idx, type;
  2198. @@ -65,7 +66,12 @@
  2199. type = kmap_atomic_idx_push();
  2200. idx = type + KM_TYPE_NR * smp_processor_id();
  2201. vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
  2202. - set_pte(kmap_pte - idx, pfn_pte(pfn, prot));
  2203. + WARN_ON(!pte_none(*(kmap_pte - idx)));
  2204. +
  2205. +#ifdef CONFIG_PREEMPT_RT_FULL
  2206. + current->kmap_pte[type] = pte;
  2207. +#endif
  2208. + set_pte(kmap_pte - idx, pte);
  2209. arch_flush_lazy_mmu_mode();
  2210. return (void *)vaddr;
  2211. @@ -113,6 +119,9 @@
  2212. * is a bad idea also, in case the page changes cacheability
  2213. * attributes or becomes a protected page in a hypervisor.
  2214. */
  2215. +#ifdef CONFIG_PREEMPT_RT_FULL
  2216. + current->kmap_pte[type] = __pte(0);
  2217. +#endif
  2218. kpte_clear_flush(kmap_pte-idx, vaddr);
  2219. kmap_atomic_idx_pop();
  2220. }
  2221. diff -Nur linux-4.9.28.orig/arch/x86/mm/pageattr.c linux-4.9.28/arch/x86/mm/pageattr.c
  2222. --- linux-4.9.28.orig/arch/x86/mm/pageattr.c 2017-05-14 14:00:37.000000000 +0200
  2223. +++ linux-4.9.28/arch/x86/mm/pageattr.c 2017-05-19 03:37:25.134174924 +0200
  2224. @@ -214,7 +214,15 @@
  2225. int in_flags, struct page **pages)
  2226. {
  2227. unsigned int i, level;
  2228. +#ifdef CONFIG_PREEMPT
  2229. + /*
  2230. + * Avoid wbinvd() because it causes latencies on all CPUs,
  2231. + * regardless of any CPU isolation that may be in effect.
  2232. + */
  2233. + unsigned long do_wbinvd = 0;
  2234. +#else
  2235. unsigned long do_wbinvd = cache && numpages >= 1024; /* 4M threshold */
  2236. +#endif
  2237. BUG_ON(irqs_disabled());
  2238. diff -Nur linux-4.9.28.orig/arch/x86/platform/uv/tlb_uv.c linux-4.9.28/arch/x86/platform/uv/tlb_uv.c
  2239. --- linux-4.9.28.orig/arch/x86/platform/uv/tlb_uv.c 2017-05-14 14:00:37.000000000 +0200
  2240. +++ linux-4.9.28/arch/x86/platform/uv/tlb_uv.c 2017-05-19 03:37:25.134174924 +0200
  2241. @@ -748,9 +748,9 @@
  2242. quiesce_local_uvhub(hmaster);
  2243. - spin_lock(&hmaster->queue_lock);
  2244. + raw_spin_lock(&hmaster->queue_lock);
  2245. reset_with_ipi(&bau_desc->distribution, bcp);
  2246. - spin_unlock(&hmaster->queue_lock);
  2247. + raw_spin_unlock(&hmaster->queue_lock);
  2248. end_uvhub_quiesce(hmaster);
  2249. @@ -770,9 +770,9 @@
  2250. quiesce_local_uvhub(hmaster);
  2251. - spin_lock(&hmaster->queue_lock);
  2252. + raw_spin_lock(&hmaster->queue_lock);
  2253. reset_with_ipi(&bau_desc->distribution, bcp);
  2254. - spin_unlock(&hmaster->queue_lock);
  2255. + raw_spin_unlock(&hmaster->queue_lock);
  2256. end_uvhub_quiesce(hmaster);
  2257. @@ -793,7 +793,7 @@
  2258. cycles_t tm1;
  2259. hmaster = bcp->uvhub_master;
  2260. - spin_lock(&hmaster->disable_lock);
  2261. + raw_spin_lock(&hmaster->disable_lock);
  2262. if (!bcp->baudisabled) {
  2263. stat->s_bau_disabled++;
  2264. tm1 = get_cycles();
  2265. @@ -806,7 +806,7 @@
  2266. }
  2267. }
  2268. }
  2269. - spin_unlock(&hmaster->disable_lock);
  2270. + raw_spin_unlock(&hmaster->disable_lock);
  2271. }
  2272. static void count_max_concurr(int stat, struct bau_control *bcp,
  2273. @@ -869,7 +869,7 @@
  2274. */
  2275. static void uv1_throttle(struct bau_control *hmaster, struct ptc_stats *stat)
  2276. {
  2277. - spinlock_t *lock = &hmaster->uvhub_lock;
  2278. + raw_spinlock_t *lock = &hmaster->uvhub_lock;
  2279. atomic_t *v;
  2280. v = &hmaster->active_descriptor_count;
  2281. @@ -1002,7 +1002,7 @@
  2282. struct bau_control *hmaster;
  2283. hmaster = bcp->uvhub_master;
  2284. - spin_lock(&hmaster->disable_lock);
  2285. + raw_spin_lock(&hmaster->disable_lock);
  2286. if (bcp->baudisabled && (get_cycles() >= bcp->set_bau_on_time)) {
  2287. stat->s_bau_reenabled++;
  2288. for_each_present_cpu(tcpu) {
  2289. @@ -1014,10 +1014,10 @@
  2290. tbcp->period_giveups = 0;
  2291. }
  2292. }
  2293. - spin_unlock(&hmaster->disable_lock);
  2294. + raw_spin_unlock(&hmaster->disable_lock);
  2295. return 0;
  2296. }
  2297. - spin_unlock(&hmaster->disable_lock);
  2298. + raw_spin_unlock(&hmaster->disable_lock);
  2299. return -1;
  2300. }
  2301. @@ -1940,9 +1940,9 @@
  2302. bcp->cong_reps = congested_reps;
  2303. bcp->disabled_period = sec_2_cycles(disabled_period);
  2304. bcp->giveup_limit = giveup_limit;
  2305. - spin_lock_init(&bcp->queue_lock);
  2306. - spin_lock_init(&bcp->uvhub_lock);
  2307. - spin_lock_init(&bcp->disable_lock);
  2308. + raw_spin_lock_init(&bcp->queue_lock);
  2309. + raw_spin_lock_init(&bcp->uvhub_lock);
  2310. + raw_spin_lock_init(&bcp->disable_lock);
  2311. }
  2312. }
  2313. diff -Nur linux-4.9.28.orig/arch/x86/platform/uv/uv_time.c linux-4.9.28/arch/x86/platform/uv/uv_time.c
  2314. --- linux-4.9.28.orig/arch/x86/platform/uv/uv_time.c 2017-05-14 14:00:37.000000000 +0200
  2315. +++ linux-4.9.28/arch/x86/platform/uv/uv_time.c 2017-05-19 03:37:25.134174924 +0200
  2316. @@ -57,7 +57,7 @@
  2317. /* There is one of these allocated per node */
  2318. struct uv_rtc_timer_head {
  2319. - spinlock_t lock;
  2320. + raw_spinlock_t lock;
  2321. /* next cpu waiting for timer, local node relative: */
  2322. int next_cpu;
  2323. /* number of cpus on this node: */
  2324. @@ -177,7 +177,7 @@
  2325. uv_rtc_deallocate_timers();
  2326. return -ENOMEM;
  2327. }
  2328. - spin_lock_init(&head->lock);
  2329. + raw_spin_lock_init(&head->lock);
  2330. head->ncpus = uv_blade_nr_possible_cpus(bid);
  2331. head->next_cpu = -1;
  2332. blade_info[bid] = head;
  2333. @@ -231,7 +231,7 @@
  2334. unsigned long flags;
  2335. int next_cpu;
  2336. - spin_lock_irqsave(&head->lock, flags);
  2337. + raw_spin_lock_irqsave(&head->lock, flags);
  2338. next_cpu = head->next_cpu;
  2339. *t = expires;
  2340. @@ -243,12 +243,12 @@
  2341. if (uv_setup_intr(cpu, expires)) {
  2342. *t = ULLONG_MAX;
  2343. uv_rtc_find_next_timer(head, pnode);
  2344. - spin_unlock_irqrestore(&head->lock, flags);
  2345. + raw_spin_unlock_irqrestore(&head->lock, flags);
  2346. return -ETIME;
  2347. }
  2348. }
  2349. - spin_unlock_irqrestore(&head->lock, flags);
  2350. + raw_spin_unlock_irqrestore(&head->lock, flags);
  2351. return 0;
  2352. }
  2353. @@ -267,7 +267,7 @@
  2354. unsigned long flags;
  2355. int rc = 0;
  2356. - spin_lock_irqsave(&head->lock, flags);
  2357. + raw_spin_lock_irqsave(&head->lock, flags);
  2358. if ((head->next_cpu == bcpu && uv_read_rtc(NULL) >= *t) || force)
  2359. rc = 1;
  2360. @@ -279,7 +279,7 @@
  2361. uv_rtc_find_next_timer(head, pnode);
  2362. }
  2363. - spin_unlock_irqrestore(&head->lock, flags);
  2364. + raw_spin_unlock_irqrestore(&head->lock, flags);
  2365. return rc;
  2366. }
  2367. @@ -299,13 +299,18 @@
  2368. static cycle_t uv_read_rtc(struct clocksource *cs)
  2369. {
  2370. unsigned long offset;
  2371. + cycle_t cycles;
  2372. + preempt_disable();
  2373. if (uv_get_min_hub_revision_id() == 1)
  2374. offset = 0;
  2375. else
  2376. offset = (uv_blade_processor_id() * L1_CACHE_BYTES) % PAGE_SIZE;
  2377. - return (cycle_t)uv_read_local_mmr(UVH_RTC | offset);
  2378. + cycles = (cycle_t)uv_read_local_mmr(UVH_RTC | offset);
  2379. + preempt_enable();
  2380. +
  2381. + return cycles;
  2382. }
  2383. /*
  2384. diff -Nur linux-4.9.28.orig/block/blk-core.c linux-4.9.28/block/blk-core.c
  2385. --- linux-4.9.28.orig/block/blk-core.c 2017-05-14 14:00:37.000000000 +0200
  2386. +++ linux-4.9.28/block/blk-core.c 2017-05-19 03:37:25.134174924 +0200
  2387. @@ -125,6 +125,9 @@
  2388. INIT_LIST_HEAD(&rq->queuelist);
  2389. INIT_LIST_HEAD(&rq->timeout_list);
  2390. +#ifdef CONFIG_PREEMPT_RT_FULL
  2391. + INIT_WORK(&rq->work, __blk_mq_complete_request_remote_work);
  2392. +#endif
  2393. rq->cpu = -1;
  2394. rq->q = q;
  2395. rq->__sector = (sector_t) -1;
  2396. @@ -233,7 +236,7 @@
  2397. **/
  2398. void blk_start_queue(struct request_queue *q)
  2399. {
  2400. - WARN_ON(!irqs_disabled());
  2401. + WARN_ON_NONRT(!irqs_disabled());
  2402. queue_flag_clear(QUEUE_FLAG_STOPPED, q);
  2403. __blk_run_queue(q);
  2404. @@ -659,7 +662,7 @@
  2405. if (nowait)
  2406. return -EBUSY;
  2407. - ret = wait_event_interruptible(q->mq_freeze_wq,
  2408. + ret = swait_event_interruptible(q->mq_freeze_wq,
  2409. !atomic_read(&q->mq_freeze_depth) ||
  2410. blk_queue_dying(q));
  2411. if (blk_queue_dying(q))
  2412. @@ -679,7 +682,7 @@
  2413. struct request_queue *q =
  2414. container_of(ref, struct request_queue, q_usage_counter);
  2415. - wake_up_all(&q->mq_freeze_wq);
  2416. + swake_up_all(&q->mq_freeze_wq);
  2417. }
  2418. static void blk_rq_timed_out_timer(unsigned long data)
  2419. @@ -748,7 +751,7 @@
  2420. q->bypass_depth = 1;
  2421. __set_bit(QUEUE_FLAG_BYPASS, &q->queue_flags);
  2422. - init_waitqueue_head(&q->mq_freeze_wq);
  2423. + init_swait_queue_head(&q->mq_freeze_wq);
  2424. /*
  2425. * Init percpu_ref in atomic mode so that it's faster to shutdown.
  2426. @@ -3200,7 +3203,7 @@
  2427. blk_run_queue_async(q);
  2428. else
  2429. __blk_run_queue(q);
  2430. - spin_unlock(q->queue_lock);
  2431. + spin_unlock_irq(q->queue_lock);
  2432. }
  2433. static void flush_plug_callbacks(struct blk_plug *plug, bool from_schedule)
  2434. @@ -3248,7 +3251,6 @@
  2435. void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
  2436. {
  2437. struct request_queue *q;
  2438. - unsigned long flags;
  2439. struct request *rq;
  2440. LIST_HEAD(list);
  2441. unsigned int depth;
  2442. @@ -3268,11 +3270,6 @@
  2443. q = NULL;
  2444. depth = 0;
  2445. - /*
  2446. - * Save and disable interrupts here, to avoid doing it for every
  2447. - * queue lock we have to take.
  2448. - */
  2449. - local_irq_save(flags);
  2450. while (!list_empty(&list)) {
  2451. rq = list_entry_rq(list.next);
  2452. list_del_init(&rq->queuelist);
  2453. @@ -3285,7 +3282,7 @@
  2454. queue_unplugged(q, depth, from_schedule);
  2455. q = rq->q;
  2456. depth = 0;
  2457. - spin_lock(q->queue_lock);
  2458. + spin_lock_irq(q->queue_lock);
  2459. }
  2460. /*
  2461. @@ -3312,8 +3309,6 @@
  2462. */
  2463. if (q)
  2464. queue_unplugged(q, depth, from_schedule);
  2465. -
  2466. - local_irq_restore(flags);
  2467. }
  2468. void blk_finish_plug(struct blk_plug *plug)
  2469. diff -Nur linux-4.9.28.orig/block/blk-ioc.c linux-4.9.28/block/blk-ioc.c
  2470. --- linux-4.9.28.orig/block/blk-ioc.c 2017-05-14 14:00:37.000000000 +0200
  2471. +++ linux-4.9.28/block/blk-ioc.c 2017-05-19 03:37:25.134174924 +0200
  2472. @@ -7,6 +7,7 @@
  2473. #include <linux/bio.h>
  2474. #include <linux/blkdev.h>
  2475. #include <linux/slab.h>
  2476. +#include <linux/delay.h>
  2477. #include "blk.h"
  2478. @@ -109,7 +110,7 @@
  2479. spin_unlock(q->queue_lock);
  2480. } else {
  2481. spin_unlock_irqrestore(&ioc->lock, flags);
  2482. - cpu_relax();
  2483. + cpu_chill();
  2484. spin_lock_irqsave_nested(&ioc->lock, flags, 1);
  2485. }
  2486. }
  2487. @@ -187,7 +188,7 @@
  2488. spin_unlock(icq->q->queue_lock);
  2489. } else {
  2490. spin_unlock_irqrestore(&ioc->lock, flags);
  2491. - cpu_relax();
  2492. + cpu_chill();
  2493. goto retry;
  2494. }
  2495. }
  2496. diff -Nur linux-4.9.28.orig/block/blk-mq.c linux-4.9.28/block/blk-mq.c
  2497. --- linux-4.9.28.orig/block/blk-mq.c 2017-05-14 14:00:37.000000000 +0200
  2498. +++ linux-4.9.28/block/blk-mq.c 2017-05-19 03:37:25.134174924 +0200
  2499. @@ -72,7 +72,7 @@
  2500. static void blk_mq_freeze_queue_wait(struct request_queue *q)
  2501. {
  2502. - wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
  2503. + swait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
  2504. }
  2505. /*
  2506. @@ -110,7 +110,7 @@
  2507. WARN_ON_ONCE(freeze_depth < 0);
  2508. if (!freeze_depth) {
  2509. percpu_ref_reinit(&q->q_usage_counter);
  2510. - wake_up_all(&q->mq_freeze_wq);
  2511. + swake_up_all(&q->mq_freeze_wq);
  2512. }
  2513. }
  2514. EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
  2515. @@ -129,7 +129,7 @@
  2516. * dying, we need to ensure that processes currently waiting on
  2517. * the queue are notified as well.
  2518. */
  2519. - wake_up_all(&q->mq_freeze_wq);
  2520. + swake_up_all(&q->mq_freeze_wq);
  2521. }
  2522. bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
  2523. @@ -177,6 +177,9 @@
  2524. rq->resid_len = 0;
  2525. rq->sense = NULL;
  2526. +#ifdef CONFIG_PREEMPT_RT_FULL
  2527. + INIT_WORK(&rq->work, __blk_mq_complete_request_remote_work);
  2528. +#endif
  2529. INIT_LIST_HEAD(&rq->timeout_list);
  2530. rq->timeout = 0;
  2531. @@ -345,6 +348,17 @@
  2532. }
  2533. EXPORT_SYMBOL(blk_mq_end_request);
  2534. +#ifdef CONFIG_PREEMPT_RT_FULL
  2535. +
  2536. +void __blk_mq_complete_request_remote_work(struct work_struct *work)
  2537. +{
  2538. + struct request *rq = container_of(work, struct request, work);
  2539. +
  2540. + rq->q->softirq_done_fn(rq);
  2541. +}
  2542. +
  2543. +#else
  2544. +
  2545. static void __blk_mq_complete_request_remote(void *data)
  2546. {
  2547. struct request *rq = data;
  2548. @@ -352,6 +366,8 @@
  2549. rq->q->softirq_done_fn(rq);
  2550. }
  2551. +#endif
  2552. +
  2553. static void blk_mq_ipi_complete_request(struct request *rq)
  2554. {
  2555. struct blk_mq_ctx *ctx = rq->mq_ctx;
  2556. @@ -363,19 +379,23 @@
  2557. return;
  2558. }
  2559. - cpu = get_cpu();
  2560. + cpu = get_cpu_light();
  2561. if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
  2562. shared = cpus_share_cache(cpu, ctx->cpu);
  2563. if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
  2564. +#ifdef CONFIG_PREEMPT_RT_FULL
  2565. + schedule_work_on(ctx->cpu, &rq->work);
  2566. +#else
  2567. rq->csd.func = __blk_mq_complete_request_remote;
  2568. rq->csd.info = rq;
  2569. rq->csd.flags = 0;
  2570. smp_call_function_single_async(ctx->cpu, &rq->csd);
  2571. +#endif
  2572. } else {
  2573. rq->q->softirq_done_fn(rq);
  2574. }
  2575. - put_cpu();
  2576. + put_cpu_light();
  2577. }
  2578. static void __blk_mq_complete_request(struct request *rq)
  2579. @@ -906,14 +926,14 @@
  2580. return;
  2581. if (!async && !(hctx->flags & BLK_MQ_F_BLOCKING)) {
  2582. - int cpu = get_cpu();
  2583. + int cpu = get_cpu_light();
  2584. if (cpumask_test_cpu(cpu, hctx->cpumask)) {
  2585. __blk_mq_run_hw_queue(hctx);
  2586. - put_cpu();
  2587. + put_cpu_light();
  2588. return;
  2589. }
  2590. - put_cpu();
  2591. + put_cpu_light();
  2592. }
  2593. kblockd_schedule_work_on(blk_mq_hctx_next_cpu(hctx), &hctx->run_work);
  2594. diff -Nur linux-4.9.28.orig/block/blk-mq.h linux-4.9.28/block/blk-mq.h
  2595. --- linux-4.9.28.orig/block/blk-mq.h 2017-05-14 14:00:37.000000000 +0200
  2596. +++ linux-4.9.28/block/blk-mq.h 2017-05-19 03:37:25.134174924 +0200
  2597. @@ -72,12 +72,12 @@
  2598. */
  2599. static inline struct blk_mq_ctx *blk_mq_get_ctx(struct request_queue *q)
  2600. {
  2601. - return __blk_mq_get_ctx(q, get_cpu());
  2602. + return __blk_mq_get_ctx(q, get_cpu_light());
  2603. }
  2604. static inline void blk_mq_put_ctx(struct blk_mq_ctx *ctx)
  2605. {
  2606. - put_cpu();
  2607. + put_cpu_light();
  2608. }
  2609. struct blk_mq_alloc_data {
  2610. diff -Nur linux-4.9.28.orig/block/blk-softirq.c linux-4.9.28/block/blk-softirq.c
  2611. --- linux-4.9.28.orig/block/blk-softirq.c 2017-05-14 14:00:37.000000000 +0200
  2612. +++ linux-4.9.28/block/blk-softirq.c 2017-05-19 03:37:25.134174924 +0200
  2613. @@ -51,6 +51,7 @@
  2614. raise_softirq_irqoff(BLOCK_SOFTIRQ);
  2615. local_irq_restore(flags);
  2616. + preempt_check_resched_rt();
  2617. }
  2618. /*
  2619. @@ -89,6 +90,7 @@
  2620. this_cpu_ptr(&blk_cpu_done));
  2621. raise_softirq_irqoff(BLOCK_SOFTIRQ);
  2622. local_irq_enable();
  2623. + preempt_check_resched_rt();
  2624. return 0;
  2625. }
  2626. @@ -141,6 +143,7 @@
  2627. goto do_local;
  2628. local_irq_restore(flags);
  2629. + preempt_check_resched_rt();
  2630. }
  2631. /**
  2632. diff -Nur linux-4.9.28.orig/block/bounce.c linux-4.9.28/block/bounce.c
  2633. --- linux-4.9.28.orig/block/bounce.c 2017-05-14 14:00:37.000000000 +0200
  2634. +++ linux-4.9.28/block/bounce.c 2017-05-19 03:37:25.134174924 +0200
  2635. @@ -55,11 +55,11 @@
  2636. unsigned long flags;
  2637. unsigned char *vto;
  2638. - local_irq_save(flags);
  2639. + local_irq_save_nort(flags);
  2640. vto = kmap_atomic(to->bv_page);
  2641. memcpy(vto + to->bv_offset, vfrom, to->bv_len);
  2642. kunmap_atomic(vto);
  2643. - local_irq_restore(flags);
  2644. + local_irq_restore_nort(flags);
  2645. }
  2646. #else /* CONFIG_HIGHMEM */
  2647. diff -Nur linux-4.9.28.orig/crypto/algapi.c linux-4.9.28/crypto/algapi.c
  2648. --- linux-4.9.28.orig/crypto/algapi.c 2017-05-14 14:00:37.000000000 +0200
  2649. +++ linux-4.9.28/crypto/algapi.c 2017-05-19 03:37:25.134174924 +0200
  2650. @@ -719,13 +719,13 @@
  2651. int crypto_register_notifier(struct notifier_block *nb)
  2652. {
  2653. - return blocking_notifier_chain_register(&crypto_chain, nb);
  2654. + return srcu_notifier_chain_register(&crypto_chain, nb);
  2655. }
  2656. EXPORT_SYMBOL_GPL(crypto_register_notifier);
  2657. int crypto_unregister_notifier(struct notifier_block *nb)
  2658. {
  2659. - return blocking_notifier_chain_unregister(&crypto_chain, nb);
  2660. + return srcu_notifier_chain_unregister(&crypto_chain, nb);
  2661. }
  2662. EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
  2663. diff -Nur linux-4.9.28.orig/crypto/api.c linux-4.9.28/crypto/api.c
  2664. --- linux-4.9.28.orig/crypto/api.c 2017-05-14 14:00:37.000000000 +0200
  2665. +++ linux-4.9.28/crypto/api.c 2017-05-19 03:37:25.134174924 +0200
  2666. @@ -31,7 +31,7 @@
  2667. DECLARE_RWSEM(crypto_alg_sem);
  2668. EXPORT_SYMBOL_GPL(crypto_alg_sem);
  2669. -BLOCKING_NOTIFIER_HEAD(crypto_chain);
  2670. +SRCU_NOTIFIER_HEAD(crypto_chain);
  2671. EXPORT_SYMBOL_GPL(crypto_chain);
  2672. static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg);
  2673. @@ -236,10 +236,10 @@
  2674. {
  2675. int ok;
  2676. - ok = blocking_notifier_call_chain(&crypto_chain, val, v);
  2677. + ok = srcu_notifier_call_chain(&crypto_chain, val, v);
  2678. if (ok == NOTIFY_DONE) {
  2679. request_module("cryptomgr");
  2680. - ok = blocking_notifier_call_chain(&crypto_chain, val, v);
  2681. + ok = srcu_notifier_call_chain(&crypto_chain, val, v);
  2682. }
  2683. return ok;
  2684. diff -Nur linux-4.9.28.orig/crypto/internal.h linux-4.9.28/crypto/internal.h
  2685. --- linux-4.9.28.orig/crypto/internal.h 2017-05-14 14:00:37.000000000 +0200
  2686. +++ linux-4.9.28/crypto/internal.h 2017-05-19 03:37:25.138175108 +0200
  2687. @@ -47,7 +47,7 @@
  2688. extern struct list_head crypto_alg_list;
  2689. extern struct rw_semaphore crypto_alg_sem;
  2690. -extern struct blocking_notifier_head crypto_chain;
  2691. +extern struct srcu_notifier_head crypto_chain;
  2692. #ifdef CONFIG_PROC_FS
  2693. void __init crypto_init_proc(void);
  2694. @@ -146,7 +146,7 @@
  2695. static inline void crypto_notify(unsigned long val, void *v)
  2696. {
  2697. - blocking_notifier_call_chain(&crypto_chain, val, v);
  2698. + srcu_notifier_call_chain(&crypto_chain, val, v);
  2699. }
  2700. #endif /* _CRYPTO_INTERNAL_H */
  2701. diff -Nur linux-4.9.28.orig/Documentation/sysrq.txt linux-4.9.28/Documentation/sysrq.txt
  2702. --- linux-4.9.28.orig/Documentation/sysrq.txt 2017-05-14 14:00:37.000000000 +0200
  2703. +++ linux-4.9.28/Documentation/sysrq.txt 2017-05-19 03:37:25.118174016 +0200
  2704. @@ -59,10 +59,17 @@
  2705. On other - If you know of the key combos for other architectures, please
  2706. let me know so I can add them to this section.
  2707. -On all - write a character to /proc/sysrq-trigger. e.g.:
  2708. -
  2709. +On all - write a character to /proc/sysrq-trigger, e.g.:
  2710. echo t > /proc/sysrq-trigger
  2711. +On all - Enable network SysRq by writing a cookie to icmp_echo_sysrq, e.g.
  2712. + echo 0x01020304 >/proc/sys/net/ipv4/icmp_echo_sysrq
  2713. + Send an ICMP echo request with this pattern plus the particular
  2714. + SysRq command key. Example:
  2715. + # ping -c1 -s57 -p0102030468
  2716. + will trigger the SysRq-H (help) command.
  2717. +
  2718. +
  2719. * What are the 'command' keys?
  2720. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2721. 'b' - Will immediately reboot the system without syncing or unmounting
  2722. diff -Nur linux-4.9.28.orig/Documentation/trace/histograms.txt linux-4.9.28/Documentation/trace/histograms.txt
  2723. --- linux-4.9.28.orig/Documentation/trace/histograms.txt 1970-01-01 01:00:00.000000000 +0100
  2724. +++ linux-4.9.28/Documentation/trace/histograms.txt 2017-05-19 03:37:25.118174016 +0200
  2725. @@ -0,0 +1,186 @@
  2726. + Using the Linux Kernel Latency Histograms
  2727. +
  2728. +
  2729. +This document gives a short explanation how to enable, configure and use
  2730. +latency histograms. Latency histograms are primarily relevant in the
  2731. +context of real-time enabled kernels (CONFIG_PREEMPT/CONFIG_PREEMPT_RT)
  2732. +and are used in the quality management of the Linux real-time
  2733. +capabilities.
  2734. +
  2735. +
  2736. +* Purpose of latency histograms
  2737. +
  2738. +A latency histogram continuously accumulates the frequencies of latency
  2739. +data. There are two types of histograms
  2740. +- potential sources of latencies
  2741. +- effective latencies
  2742. +
  2743. +
  2744. +* Potential sources of latencies
  2745. +
  2746. +Potential sources of latencies are code segments where interrupts,
  2747. +preemption or both are disabled (aka critical sections). To create
  2748. +histograms of potential sources of latency, the kernel stores the time
  2749. +stamp at the start of a critical section, determines the time elapsed
  2750. +when the end of the section is reached, and increments the frequency
  2751. +counter of that latency value - irrespective of whether any concurrently
  2752. +running process is affected by latency or not.
  2753. +- Configuration items (in the Kernel hacking/Tracers submenu)
  2754. + CONFIG_INTERRUPT_OFF_LATENCY
  2755. + CONFIG_PREEMPT_OFF_LATENCY
  2756. +
  2757. +
  2758. +* Effective latencies
  2759. +
  2760. +Effective latencies are actually occuring during wakeup of a process. To
  2761. +determine effective latencies, the kernel stores the time stamp when a
  2762. +process is scheduled to be woken up, and determines the duration of the
  2763. +wakeup time shortly before control is passed over to this process. Note
  2764. +that the apparent latency in user space may be somewhat longer, since the
  2765. +process may be interrupted after control is passed over to it but before
  2766. +the execution in user space takes place. Simply measuring the interval
  2767. +between enqueuing and wakeup may also not appropriate in cases when a
  2768. +process is scheduled as a result of a timer expiration. The timer may have
  2769. +missed its deadline, e.g. due to disabled interrupts, but this latency
  2770. +would not be registered. Therefore, the offsets of missed timers are
  2771. +recorded in a separate histogram. If both wakeup latency and missed timer
  2772. +offsets are configured and enabled, a third histogram may be enabled that
  2773. +records the overall latency as a sum of the timer latency, if any, and the
  2774. +wakeup latency. This histogram is called "timerandwakeup".
  2775. +- Configuration items (in the Kernel hacking/Tracers submenu)
  2776. + CONFIG_WAKEUP_LATENCY
  2777. + CONFIG_MISSED_TIMER_OFSETS
  2778. +
  2779. +
  2780. +* Usage
  2781. +
  2782. +The interface to the administration of the latency histograms is located
  2783. +in the debugfs file system. To mount it, either enter
  2784. +
  2785. +mount -t sysfs nodev /sys
  2786. +mount -t debugfs nodev /sys/kernel/debug
  2787. +
  2788. +from shell command line level, or add
  2789. +
  2790. +nodev /sys sysfs defaults 0 0
  2791. +nodev /sys/kernel/debug debugfs defaults 0 0
  2792. +
  2793. +to the file /etc/fstab. All latency histogram related files are then
  2794. +available in the directory /sys/kernel/debug/tracing/latency_hist. A
  2795. +particular histogram type is enabled by writing non-zero to the related
  2796. +variable in the /sys/kernel/debug/tracing/latency_hist/enable directory.
  2797. +Select "preemptirqsoff" for the histograms of potential sources of
  2798. +latencies and "wakeup" for histograms of effective latencies etc. The
  2799. +histogram data - one per CPU - are available in the files
  2800. +
  2801. +/sys/kernel/debug/tracing/latency_hist/preemptoff/CPUx
  2802. +/sys/kernel/debug/tracing/latency_hist/irqsoff/CPUx
  2803. +/sys/kernel/debug/tracing/latency_hist/preemptirqsoff/CPUx
  2804. +/sys/kernel/debug/tracing/latency_hist/wakeup/CPUx
  2805. +/sys/kernel/debug/tracing/latency_hist/wakeup/sharedprio/CPUx
  2806. +/sys/kernel/debug/tracing/latency_hist/missed_timer_offsets/CPUx
  2807. +/sys/kernel/debug/tracing/latency_hist/timerandwakeup/CPUx
  2808. +
  2809. +The histograms are reset by writing non-zero to the file "reset" in a
  2810. +particular latency directory. To reset all latency data, use
  2811. +
  2812. +#!/bin/sh
  2813. +
  2814. +TRACINGDIR=/sys/kernel/debug/tracing
  2815. +HISTDIR=$TRACINGDIR/latency_hist
  2816. +
  2817. +if test -d $HISTDIR
  2818. +then
  2819. + cd $HISTDIR
  2820. + for i in `find . | grep /reset$`
  2821. + do
  2822. + echo 1 >$i
  2823. + done
  2824. +fi
  2825. +
  2826. +
  2827. +* Data format
  2828. +
  2829. +Latency data are stored with a resolution of one microsecond. The
  2830. +maximum latency is 10,240 microseconds. The data are only valid, if the
  2831. +overflow register is empty. Every output line contains the latency in
  2832. +microseconds in the first row and the number of samples in the second
  2833. +row. To display only lines with a positive latency count, use, for
  2834. +example,
  2835. +
  2836. +grep -v " 0$" /sys/kernel/debug/tracing/latency_hist/preemptoff/CPU0
  2837. +
  2838. +#Minimum latency: 0 microseconds.
  2839. +#Average latency: 0 microseconds.
  2840. +#Maximum latency: 25 microseconds.
  2841. +#Total samples: 3104770694
  2842. +#There are 0 samples greater or equal than 10240 microseconds
  2843. +#usecs samples
  2844. + 0 2984486876
  2845. + 1 49843506
  2846. + 2 58219047
  2847. + 3 5348126
  2848. + 4 2187960
  2849. + 5 3388262
  2850. + 6 959289
  2851. + 7 208294
  2852. + 8 40420
  2853. + 9 4485
  2854. + 10 14918
  2855. + 11 18340
  2856. + 12 25052
  2857. + 13 19455
  2858. + 14 5602
  2859. + 15 969
  2860. + 16 47
  2861. + 17 18
  2862. + 18 14
  2863. + 19 1
  2864. + 20 3
  2865. + 21 2
  2866. + 22 5
  2867. + 23 2
  2868. + 25 1
  2869. +
  2870. +
  2871. +* Wakeup latency of a selected process
  2872. +
  2873. +To only collect wakeup latency data of a particular process, write the
  2874. +PID of the requested process to
  2875. +
  2876. +/sys/kernel/debug/tracing/latency_hist/wakeup/pid
  2877. +
  2878. +PIDs are not considered, if this variable is set to 0.
  2879. +
  2880. +
  2881. +* Details of the process with the highest wakeup latency so far
  2882. +
  2883. +Selected data of the process that suffered from the highest wakeup
  2884. +latency that occurred in a particular CPU are available in the file
  2885. +
  2886. +/sys/kernel/debug/tracing/latency_hist/wakeup/max_latency-CPUx.
  2887. +
  2888. +In addition, other relevant system data at the time when the
  2889. +latency occurred are given.
  2890. +
  2891. +The format of the data is (all in one line):
  2892. +<PID> <Priority> <Latency> (<Timeroffset>) <Command> \
  2893. +<- <PID> <Priority> <Command> <Timestamp>
  2894. +
  2895. +The value of <Timeroffset> is only relevant in the combined timer
  2896. +and wakeup latency recording. In the wakeup recording, it is
  2897. +always 0, in the missed_timer_offsets recording, it is the same
  2898. +as <Latency>.
  2899. +
  2900. +When retrospectively searching for the origin of a latency and
  2901. +tracing was not enabled, it may be helpful to know the name and
  2902. +some basic data of the task that (finally) was switching to the
  2903. +late real-tlme task. In addition to the victim's data, also the
  2904. +data of the possible culprit are therefore displayed after the
  2905. +"<-" symbol.
  2906. +
  2907. +Finally, the timestamp of the time when the latency occurred
  2908. +in <seconds>.<microseconds> after the most recent system boot
  2909. +is provided.
  2910. +
  2911. +These data are also reset when the wakeup histogram is reset.
  2912. diff -Nur linux-4.9.28.orig/drivers/acpi/acpica/acglobal.h linux-4.9.28/drivers/acpi/acpica/acglobal.h
  2913. --- linux-4.9.28.orig/drivers/acpi/acpica/acglobal.h 2017-05-14 14:00:37.000000000 +0200
  2914. +++ linux-4.9.28/drivers/acpi/acpica/acglobal.h 2017-05-19 03:37:25.138175108 +0200
  2915. @@ -116,7 +116,7 @@
  2916. * interrupt level
  2917. */
  2918. ACPI_GLOBAL(acpi_spinlock, acpi_gbl_gpe_lock); /* For GPE data structs and registers */
  2919. -ACPI_GLOBAL(acpi_spinlock, acpi_gbl_hardware_lock); /* For ACPI H/W except GPE registers */
  2920. +ACPI_GLOBAL(acpi_raw_spinlock, acpi_gbl_hardware_lock); /* For ACPI H/W except GPE registers */
  2921. ACPI_GLOBAL(acpi_spinlock, acpi_gbl_reference_count_lock);
  2922. /* Mutex for _OSI support */
  2923. diff -Nur linux-4.9.28.orig/drivers/acpi/acpica/hwregs.c linux-4.9.28/drivers/acpi/acpica/hwregs.c
  2924. --- linux-4.9.28.orig/drivers/acpi/acpica/hwregs.c 2017-05-14 14:00:37.000000000 +0200
  2925. +++ linux-4.9.28/drivers/acpi/acpica/hwregs.c 2017-05-19 03:37:25.138175108 +0200
  2926. @@ -363,14 +363,14 @@
  2927. ACPI_BITMASK_ALL_FIXED_STATUS,
  2928. ACPI_FORMAT_UINT64(acpi_gbl_xpm1a_status.address)));
  2929. - lock_flags = acpi_os_acquire_lock(acpi_gbl_hardware_lock);
  2930. + raw_spin_lock_irqsave(acpi_gbl_hardware_lock, lock_flags);
  2931. /* Clear the fixed events in PM1 A/B */
  2932. status = acpi_hw_register_write(ACPI_REGISTER_PM1_STATUS,
  2933. ACPI_BITMASK_ALL_FIXED_STATUS);
  2934. - acpi_os_release_lock(acpi_gbl_hardware_lock, lock_flags);
  2935. + raw_spin_unlock_irqrestore(acpi_gbl_hardware_lock, lock_flags);
  2936. if (ACPI_FAILURE(status)) {
  2937. goto exit;
  2938. diff -Nur linux-4.9.28.orig/drivers/acpi/acpica/hwxface.c linux-4.9.28/drivers/acpi/acpica/hwxface.c
  2939. --- linux-4.9.28.orig/drivers/acpi/acpica/hwxface.c 2017-05-14 14:00:37.000000000 +0200
  2940. +++ linux-4.9.28/drivers/acpi/acpica/hwxface.c 2017-05-19 03:37:25.138175108 +0200
  2941. @@ -373,7 +373,7 @@
  2942. return_ACPI_STATUS(AE_BAD_PARAMETER);
  2943. }
  2944. - lock_flags = acpi_os_acquire_lock(acpi_gbl_hardware_lock);
  2945. + raw_spin_lock_irqsave(acpi_gbl_hardware_lock, lock_flags);
  2946. /*
  2947. * At this point, we know that the parent register is one of the
  2948. @@ -434,7 +434,7 @@
  2949. unlock_and_exit:
  2950. - acpi_os_release_lock(acpi_gbl_hardware_lock, lock_flags);
  2951. + raw_spin_unlock_irqrestore(acpi_gbl_hardware_lock, lock_flags);
  2952. return_ACPI_STATUS(status);
  2953. }
  2954. diff -Nur linux-4.9.28.orig/drivers/acpi/acpica/utmutex.c linux-4.9.28/drivers/acpi/acpica/utmutex.c
  2955. --- linux-4.9.28.orig/drivers/acpi/acpica/utmutex.c 2017-05-14 14:00:37.000000000 +0200
  2956. +++ linux-4.9.28/drivers/acpi/acpica/utmutex.c 2017-05-19 03:37:25.138175108 +0200
  2957. @@ -88,7 +88,7 @@
  2958. return_ACPI_STATUS (status);
  2959. }
  2960. - status = acpi_os_create_lock (&acpi_gbl_hardware_lock);
  2961. + status = acpi_os_create_raw_lock (&acpi_gbl_hardware_lock);
  2962. if (ACPI_FAILURE (status)) {
  2963. return_ACPI_STATUS (status);
  2964. }
  2965. @@ -145,7 +145,7 @@
  2966. /* Delete the spinlocks */
  2967. acpi_os_delete_lock(acpi_gbl_gpe_lock);
  2968. - acpi_os_delete_lock(acpi_gbl_hardware_lock);
  2969. + acpi_os_delete_raw_lock(acpi_gbl_hardware_lock);
  2970. acpi_os_delete_lock(acpi_gbl_reference_count_lock);
  2971. /* Delete the reader/writer lock */
  2972. diff -Nur linux-4.9.28.orig/drivers/ata/libata-sff.c linux-4.9.28/drivers/ata/libata-sff.c
  2973. --- linux-4.9.28.orig/drivers/ata/libata-sff.c 2017-05-14 14:00:37.000000000 +0200
  2974. +++ linux-4.9.28/drivers/ata/libata-sff.c 2017-05-19 03:37:25.138175108 +0200
  2975. @@ -678,9 +678,9 @@
  2976. unsigned long flags;
  2977. unsigned int consumed;
  2978. - local_irq_save(flags);
  2979. + local_irq_save_nort(flags);
  2980. consumed = ata_sff_data_xfer32(dev, buf, buflen, rw);
  2981. - local_irq_restore(flags);
  2982. + local_irq_restore_nort(flags);
  2983. return consumed;
  2984. }
  2985. @@ -719,7 +719,7 @@
  2986. unsigned long flags;
  2987. /* FIXME: use a bounce buffer */
  2988. - local_irq_save(flags);
  2989. + local_irq_save_nort(flags);
  2990. buf = kmap_atomic(page);
  2991. /* do the actual data transfer */
  2992. @@ -727,7 +727,7 @@
  2993. do_write);
  2994. kunmap_atomic(buf);
  2995. - local_irq_restore(flags);
  2996. + local_irq_restore_nort(flags);
  2997. } else {
  2998. buf = page_address(page);
  2999. ap->ops->sff_data_xfer(qc->dev, buf + offset, qc->sect_size,
  3000. @@ -864,7 +864,7 @@
  3001. unsigned long flags;
  3002. /* FIXME: use bounce buffer */
  3003. - local_irq_save(flags);
  3004. + local_irq_save_nort(flags);
  3005. buf = kmap_atomic(page);
  3006. /* do the actual data transfer */
  3007. @@ -872,7 +872,7 @@
  3008. count, rw);
  3009. kunmap_atomic(buf);
  3010. - local_irq_restore(flags);
  3011. + local_irq_restore_nort(flags);
  3012. } else {
  3013. buf = page_address(page);
  3014. consumed = ap->ops->sff_data_xfer(dev, buf + offset,
  3015. diff -Nur linux-4.9.28.orig/drivers/block/zram/zcomp.c linux-4.9.28/drivers/block/zram/zcomp.c
  3016. --- linux-4.9.28.orig/drivers/block/zram/zcomp.c 2017-05-14 14:00:37.000000000 +0200
  3017. +++ linux-4.9.28/drivers/block/zram/zcomp.c 2017-05-19 03:37:25.138175108 +0200
  3018. @@ -118,12 +118,19 @@
  3019. struct zcomp_strm *zcomp_stream_get(struct zcomp *comp)
  3020. {
  3021. - return *get_cpu_ptr(comp->stream);
  3022. + struct zcomp_strm *zstrm;
  3023. +
  3024. + zstrm = *this_cpu_ptr(comp->stream);
  3025. + spin_lock(&zstrm->zcomp_lock);
  3026. + return zstrm;
  3027. }
  3028. void zcomp_stream_put(struct zcomp *comp)
  3029. {
  3030. - put_cpu_ptr(comp->stream);
  3031. + struct zcomp_strm *zstrm;
  3032. +
  3033. + zstrm = *this_cpu_ptr(comp->stream);
  3034. + spin_unlock(&zstrm->zcomp_lock);
  3035. }
  3036. int zcomp_compress(struct zcomp_strm *zstrm,
  3037. @@ -174,6 +181,7 @@
  3038. pr_err("Can't allocate a compression stream\n");
  3039. return NOTIFY_BAD;
  3040. }
  3041. + spin_lock_init(&zstrm->zcomp_lock);
  3042. *per_cpu_ptr(comp->stream, cpu) = zstrm;
  3043. break;
  3044. case CPU_DEAD:
  3045. diff -Nur linux-4.9.28.orig/drivers/block/zram/zcomp.h linux-4.9.28/drivers/block/zram/zcomp.h
  3046. --- linux-4.9.28.orig/drivers/block/zram/zcomp.h 2017-05-14 14:00:37.000000000 +0200
  3047. +++ linux-4.9.28/drivers/block/zram/zcomp.h 2017-05-19 03:37:25.138175108 +0200
  3048. @@ -14,6 +14,7 @@
  3049. /* compression/decompression buffer */
  3050. void *buffer;
  3051. struct crypto_comp *tfm;
  3052. + spinlock_t zcomp_lock;
  3053. };
  3054. /* dynamic per-device compression frontend */
  3055. diff -Nur linux-4.9.28.orig/drivers/block/zram/zram_drv.c linux-4.9.28/drivers/block/zram/zram_drv.c
  3056. --- linux-4.9.28.orig/drivers/block/zram/zram_drv.c 2017-05-14 14:00:37.000000000 +0200
  3057. +++ linux-4.9.28/drivers/block/zram/zram_drv.c 2017-05-19 03:37:25.138175108 +0200
  3058. @@ -528,6 +528,8 @@
  3059. goto out_error;
  3060. }
  3061. + zram_meta_init_table_locks(meta, disksize);
  3062. +
  3063. return meta;
  3064. out_error:
  3065. @@ -575,28 +577,28 @@
  3066. struct zram_meta *meta = zram->meta;
  3067. unsigned long handle;
  3068. unsigned int size;
  3069. + struct zcomp_strm *zstrm;
  3070. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  3071. + zram_lock_table(&meta->table[index]);
  3072. handle = meta->table[index].handle;
  3073. size = zram_get_obj_size(meta, index);
  3074. if (!handle || zram_test_flag(meta, index, ZRAM_ZERO)) {
  3075. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  3076. + zram_unlock_table(&meta->table[index]);
  3077. memset(mem, 0, PAGE_SIZE);
  3078. return 0;
  3079. }
  3080. + zstrm = zcomp_stream_get(zram->comp);
  3081. cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_RO);
  3082. if (size == PAGE_SIZE) {
  3083. memcpy(mem, cmem, PAGE_SIZE);
  3084. } else {
  3085. - struct zcomp_strm *zstrm = zcomp_stream_get(zram->comp);
  3086. -
  3087. ret = zcomp_decompress(zstrm, cmem, size, mem);
  3088. - zcomp_stream_put(zram->comp);
  3089. }
  3090. zs_unmap_object(meta->mem_pool, handle);
  3091. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  3092. + zcomp_stream_put(zram->comp);
  3093. + zram_unlock_table(&meta->table[index]);
  3094. /* Should NEVER happen. Return bio error if it does. */
  3095. if (unlikely(ret)) {
  3096. @@ -616,14 +618,14 @@
  3097. struct zram_meta *meta = zram->meta;
  3098. page = bvec->bv_page;
  3099. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  3100. + zram_lock_table(&meta->table[index]);
  3101. if (unlikely(!meta->table[index].handle) ||
  3102. zram_test_flag(meta, index, ZRAM_ZERO)) {
  3103. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  3104. + zram_unlock_table(&meta->table[index]);
  3105. handle_zero_page(bvec);
  3106. return 0;
  3107. }
  3108. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  3109. + zram_unlock_table(&meta->table[index]);
  3110. if (is_partial_io(bvec))
  3111. /* Use a temporary buffer to decompress the page */
  3112. @@ -700,10 +702,10 @@
  3113. if (user_mem)
  3114. kunmap_atomic(user_mem);
  3115. /* Free memory associated with this sector now. */
  3116. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  3117. + zram_lock_table(&meta->table[index]);
  3118. zram_free_page(zram, index);
  3119. zram_set_flag(meta, index, ZRAM_ZERO);
  3120. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  3121. + zram_unlock_table(&meta->table[index]);
  3122. atomic64_inc(&zram->stats.zero_pages);
  3123. ret = 0;
  3124. @@ -794,12 +796,12 @@
  3125. * Free memory associated with this sector
  3126. * before overwriting unused sectors.
  3127. */
  3128. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  3129. + zram_lock_table(&meta->table[index]);
  3130. zram_free_page(zram, index);
  3131. meta->table[index].handle = handle;
  3132. zram_set_obj_size(meta, index, clen);
  3133. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  3134. + zram_unlock_table(&meta->table[index]);
  3135. /* Update stats */
  3136. atomic64_add(clen, &zram->stats.compr_data_size);
  3137. @@ -842,9 +844,9 @@
  3138. }
  3139. while (n >= PAGE_SIZE) {
  3140. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  3141. + zram_lock_table(&meta->table[index]);
  3142. zram_free_page(zram, index);
  3143. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  3144. + zram_unlock_table(&meta->table[index]);
  3145. atomic64_inc(&zram->stats.notify_free);
  3146. index++;
  3147. n -= PAGE_SIZE;
  3148. @@ -973,9 +975,9 @@
  3149. zram = bdev->bd_disk->private_data;
  3150. meta = zram->meta;
  3151. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  3152. + zram_lock_table(&meta->table[index]);
  3153. zram_free_page(zram, index);
  3154. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  3155. + zram_unlock_table(&meta->table[index]);
  3156. atomic64_inc(&zram->stats.notify_free);
  3157. }
  3158. diff -Nur linux-4.9.28.orig/drivers/block/zram/zram_drv.h linux-4.9.28/drivers/block/zram/zram_drv.h
  3159. --- linux-4.9.28.orig/drivers/block/zram/zram_drv.h 2017-05-14 14:00:37.000000000 +0200
  3160. +++ linux-4.9.28/drivers/block/zram/zram_drv.h 2017-05-19 03:37:25.138175108 +0200
  3161. @@ -73,6 +73,9 @@
  3162. struct zram_table_entry {
  3163. unsigned long handle;
  3164. unsigned long value;
  3165. +#ifdef CONFIG_PREEMPT_RT_BASE
  3166. + spinlock_t lock;
  3167. +#endif
  3168. };
  3169. struct zram_stats {
  3170. @@ -120,4 +123,42 @@
  3171. */
  3172. bool claim; /* Protected by bdev->bd_mutex */
  3173. };
  3174. +
  3175. +#ifndef CONFIG_PREEMPT_RT_BASE
  3176. +static inline void zram_lock_table(struct zram_table_entry *table)
  3177. +{
  3178. + bit_spin_lock(ZRAM_ACCESS, &table->value);
  3179. +}
  3180. +
  3181. +static inline void zram_unlock_table(struct zram_table_entry *table)
  3182. +{
  3183. + bit_spin_unlock(ZRAM_ACCESS, &table->value);
  3184. +}
  3185. +
  3186. +static inline void zram_meta_init_table_locks(struct zram_meta *meta, u64 disksize) { }
  3187. +#else /* CONFIG_PREEMPT_RT_BASE */
  3188. +static inline void zram_lock_table(struct zram_table_entry *table)
  3189. +{
  3190. + spin_lock(&table->lock);
  3191. + __set_bit(ZRAM_ACCESS, &table->value);
  3192. +}
  3193. +
  3194. +static inline void zram_unlock_table(struct zram_table_entry *table)
  3195. +{
  3196. + __clear_bit(ZRAM_ACCESS, &table->value);
  3197. + spin_unlock(&table->lock);
  3198. +}
  3199. +
  3200. +static inline void zram_meta_init_table_locks(struct zram_meta *meta, u64 disksize)
  3201. +{
  3202. + size_t num_pages = disksize >> PAGE_SHIFT;
  3203. + size_t index;
  3204. +
  3205. + for (index = 0; index < num_pages; index++) {
  3206. + spinlock_t *lock = &meta->table[index].lock;
  3207. + spin_lock_init(lock);
  3208. + }
  3209. +}
  3210. +#endif /* CONFIG_PREEMPT_RT_BASE */
  3211. +
  3212. #endif
  3213. diff -Nur linux-4.9.28.orig/drivers/char/random.c linux-4.9.28/drivers/char/random.c
  3214. --- linux-4.9.28.orig/drivers/char/random.c 2017-05-14 14:00:37.000000000 +0200
  3215. +++ linux-4.9.28/drivers/char/random.c 2017-05-19 03:37:25.138175108 +0200
  3216. @@ -262,6 +262,7 @@
  3217. #include <linux/syscalls.h>
  3218. #include <linux/completion.h>
  3219. #include <linux/uuid.h>
  3220. +#include <linux/locallock.h>
  3221. #include <crypto/chacha20.h>
  3222. #include <asm/processor.h>
  3223. @@ -1028,8 +1029,6 @@
  3224. } sample;
  3225. long delta, delta2, delta3;
  3226. - preempt_disable();
  3227. -
  3228. sample.jiffies = jiffies;
  3229. sample.cycles = random_get_entropy();
  3230. sample.num = num;
  3231. @@ -1070,7 +1069,6 @@
  3232. */
  3233. credit_entropy_bits(r, min_t(int, fls(delta>>1), 11));
  3234. }
  3235. - preempt_enable();
  3236. }
  3237. void add_input_randomness(unsigned int type, unsigned int code,
  3238. @@ -1123,28 +1121,27 @@
  3239. return *(ptr + f->reg_idx++);
  3240. }
  3241. -void add_interrupt_randomness(int irq, int irq_flags)
  3242. +void add_interrupt_randomness(int irq, int irq_flags, __u64 ip)
  3243. {
  3244. struct entropy_store *r;
  3245. struct fast_pool *fast_pool = this_cpu_ptr(&irq_randomness);
  3246. - struct pt_regs *regs = get_irq_regs();
  3247. unsigned long now = jiffies;
  3248. cycles_t cycles = random_get_entropy();
  3249. __u32 c_high, j_high;
  3250. - __u64 ip;
  3251. unsigned long seed;
  3252. int credit = 0;
  3253. if (cycles == 0)
  3254. - cycles = get_reg(fast_pool, regs);
  3255. + cycles = get_reg(fast_pool, NULL);
  3256. c_high = (sizeof(cycles) > 4) ? cycles >> 32 : 0;
  3257. j_high = (sizeof(now) > 4) ? now >> 32 : 0;
  3258. fast_pool->pool[0] ^= cycles ^ j_high ^ irq;
  3259. fast_pool->pool[1] ^= now ^ c_high;
  3260. - ip = regs ? instruction_pointer(regs) : _RET_IP_;
  3261. + if (!ip)
  3262. + ip = _RET_IP_;
  3263. fast_pool->pool[2] ^= ip;
  3264. fast_pool->pool[3] ^= (sizeof(ip) > 4) ? ip >> 32 :
  3265. - get_reg(fast_pool, regs);
  3266. + get_reg(fast_pool, NULL);
  3267. fast_mix(fast_pool);
  3268. add_interrupt_bench(cycles);
  3269. @@ -2056,6 +2053,7 @@
  3270. * goal of being quite fast and not depleting entropy.
  3271. */
  3272. static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_long);
  3273. +static DEFINE_LOCAL_IRQ_LOCK(batched_entropy_long_lock);
  3274. unsigned long get_random_long(void)
  3275. {
  3276. unsigned long ret;
  3277. @@ -2064,13 +2062,13 @@
  3278. if (arch_get_random_long(&ret))
  3279. return ret;
  3280. - batch = &get_cpu_var(batched_entropy_long);
  3281. + batch = &get_locked_var(batched_entropy_long_lock, batched_entropy_long);
  3282. if (batch->position % ARRAY_SIZE(batch->entropy_long) == 0) {
  3283. extract_crng((u8 *)batch->entropy_long);
  3284. batch->position = 0;
  3285. }
  3286. ret = batch->entropy_long[batch->position++];
  3287. - put_cpu_var(batched_entropy_long);
  3288. + put_locked_var(batched_entropy_long_lock, batched_entropy_long);
  3289. return ret;
  3290. }
  3291. EXPORT_SYMBOL(get_random_long);
  3292. @@ -2082,6 +2080,8 @@
  3293. }
  3294. #else
  3295. static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_int);
  3296. +static DEFINE_LOCAL_IRQ_LOCK(batched_entropy_int_lock);
  3297. +
  3298. unsigned int get_random_int(void)
  3299. {
  3300. unsigned int ret;
  3301. @@ -2090,13 +2090,13 @@
  3302. if (arch_get_random_int(&ret))
  3303. return ret;
  3304. - batch = &get_cpu_var(batched_entropy_int);
  3305. + batch = &get_locked_var(batched_entropy_int_lock, batched_entropy_int);
  3306. if (batch->position % ARRAY_SIZE(batch->entropy_int) == 0) {
  3307. extract_crng((u8 *)batch->entropy_int);
  3308. batch->position = 0;
  3309. }
  3310. ret = batch->entropy_int[batch->position++];
  3311. - put_cpu_var(batched_entropy_int);
  3312. + put_locked_var(batched_entropy_int_lock, batched_entropy_int);
  3313. return ret;
  3314. }
  3315. #endif
  3316. diff -Nur linux-4.9.28.orig/drivers/clocksource/tcb_clksrc.c linux-4.9.28/drivers/clocksource/tcb_clksrc.c
  3317. --- linux-4.9.28.orig/drivers/clocksource/tcb_clksrc.c 2017-05-14 14:00:37.000000000 +0200
  3318. +++ linux-4.9.28/drivers/clocksource/tcb_clksrc.c 2017-05-19 03:37:25.138175108 +0200
  3319. @@ -23,8 +23,7 @@
  3320. * this 32 bit free-running counter. the second channel is not used.
  3321. *
  3322. * - The third channel may be used to provide a 16-bit clockevent
  3323. - * source, used in either periodic or oneshot mode. This runs
  3324. - * at 32 KiHZ, and can handle delays of up to two seconds.
  3325. + * source, used in either periodic or oneshot mode.
  3326. *
  3327. * A boot clocksource and clockevent source are also currently needed,
  3328. * unless the relevant platforms (ARM/AT91, AVR32/AT32) are changed so
  3329. @@ -74,6 +73,8 @@
  3330. struct tc_clkevt_device {
  3331. struct clock_event_device clkevt;
  3332. struct clk *clk;
  3333. + bool clk_enabled;
  3334. + u32 freq;
  3335. void __iomem *regs;
  3336. };
  3337. @@ -82,15 +83,26 @@
  3338. return container_of(clkevt, struct tc_clkevt_device, clkevt);
  3339. }
  3340. -/* For now, we always use the 32K clock ... this optimizes for NO_HZ,
  3341. - * because using one of the divided clocks would usually mean the
  3342. - * tick rate can never be less than several dozen Hz (vs 0.5 Hz).
  3343. - *
  3344. - * A divided clock could be good for high resolution timers, since
  3345. - * 30.5 usec resolution can seem "low".
  3346. - */
  3347. static u32 timer_clock;
  3348. +static void tc_clk_disable(struct clock_event_device *d)
  3349. +{
  3350. + struct tc_clkevt_device *tcd = to_tc_clkevt(d);
  3351. +
  3352. + clk_disable(tcd->clk);
  3353. + tcd->clk_enabled = false;
  3354. +}
  3355. +
  3356. +static void tc_clk_enable(struct clock_event_device *d)
  3357. +{
  3358. + struct tc_clkevt_device *tcd = to_tc_clkevt(d);
  3359. +
  3360. + if (tcd->clk_enabled)
  3361. + return;
  3362. + clk_enable(tcd->clk);
  3363. + tcd->clk_enabled = true;
  3364. +}
  3365. +
  3366. static int tc_shutdown(struct clock_event_device *d)
  3367. {
  3368. struct tc_clkevt_device *tcd = to_tc_clkevt(d);
  3369. @@ -98,8 +110,14 @@
  3370. __raw_writel(0xff, regs + ATMEL_TC_REG(2, IDR));
  3371. __raw_writel(ATMEL_TC_CLKDIS, regs + ATMEL_TC_REG(2, CCR));
  3372. + return 0;
  3373. +}
  3374. +
  3375. +static int tc_shutdown_clk_off(struct clock_event_device *d)
  3376. +{
  3377. + tc_shutdown(d);
  3378. if (!clockevent_state_detached(d))
  3379. - clk_disable(tcd->clk);
  3380. + tc_clk_disable(d);
  3381. return 0;
  3382. }
  3383. @@ -112,9 +130,9 @@
  3384. if (clockevent_state_oneshot(d) || clockevent_state_periodic(d))
  3385. tc_shutdown(d);
  3386. - clk_enable(tcd->clk);
  3387. + tc_clk_enable(d);
  3388. - /* slow clock, count up to RC, then irq and stop */
  3389. + /* count up to RC, then irq and stop */
  3390. __raw_writel(timer_clock | ATMEL_TC_CPCSTOP | ATMEL_TC_WAVE |
  3391. ATMEL_TC_WAVESEL_UP_AUTO, regs + ATMEL_TC_REG(2, CMR));
  3392. __raw_writel(ATMEL_TC_CPCS, regs + ATMEL_TC_REG(2, IER));
  3393. @@ -134,12 +152,12 @@
  3394. /* By not making the gentime core emulate periodic mode on top
  3395. * of oneshot, we get lower overhead and improved accuracy.
  3396. */
  3397. - clk_enable(tcd->clk);
  3398. + tc_clk_enable(d);
  3399. - /* slow clock, count up to RC, then irq and restart */
  3400. + /* count up to RC, then irq and restart */
  3401. __raw_writel(timer_clock | ATMEL_TC_WAVE | ATMEL_TC_WAVESEL_UP_AUTO,
  3402. regs + ATMEL_TC_REG(2, CMR));
  3403. - __raw_writel((32768 + HZ / 2) / HZ, tcaddr + ATMEL_TC_REG(2, RC));
  3404. + __raw_writel((tcd->freq + HZ / 2) / HZ, tcaddr + ATMEL_TC_REG(2, RC));
  3405. /* Enable clock and interrupts on RC compare */
  3406. __raw_writel(ATMEL_TC_CPCS, regs + ATMEL_TC_REG(2, IER));
  3407. @@ -166,9 +184,13 @@
  3408. .features = CLOCK_EVT_FEAT_PERIODIC |
  3409. CLOCK_EVT_FEAT_ONESHOT,
  3410. /* Should be lower than at91rm9200's system timer */
  3411. +#ifdef CONFIG_ATMEL_TCB_CLKSRC_USE_SLOW_CLOCK
  3412. .rating = 125,
  3413. +#else
  3414. + .rating = 200,
  3415. +#endif
  3416. .set_next_event = tc_next_event,
  3417. - .set_state_shutdown = tc_shutdown,
  3418. + .set_state_shutdown = tc_shutdown_clk_off,
  3419. .set_state_periodic = tc_set_periodic,
  3420. .set_state_oneshot = tc_set_oneshot,
  3421. },
  3422. @@ -188,8 +210,9 @@
  3423. return IRQ_NONE;
  3424. }
  3425. -static int __init setup_clkevents(struct atmel_tc *tc, int clk32k_divisor_idx)
  3426. +static int __init setup_clkevents(struct atmel_tc *tc, int divisor_idx)
  3427. {
  3428. + unsigned divisor = atmel_tc_divisors[divisor_idx];
  3429. int ret;
  3430. struct clk *t2_clk = tc->clk[2];
  3431. int irq = tc->irq[2];
  3432. @@ -210,7 +233,11 @@
  3433. clkevt.regs = tc->regs;
  3434. clkevt.clk = t2_clk;
  3435. - timer_clock = clk32k_divisor_idx;
  3436. + timer_clock = divisor_idx;
  3437. + if (!divisor)
  3438. + clkevt.freq = 32768;
  3439. + else
  3440. + clkevt.freq = clk_get_rate(t2_clk) / divisor;
  3441. clkevt.clkevt.cpumask = cpumask_of(0);
  3442. @@ -221,7 +248,7 @@
  3443. return ret;
  3444. }
  3445. - clockevents_config_and_register(&clkevt.clkevt, 32768, 1, 0xffff);
  3446. + clockevents_config_and_register(&clkevt.clkevt, clkevt.freq, 1, 0xffff);
  3447. return ret;
  3448. }
  3449. @@ -358,7 +385,11 @@
  3450. goto err_disable_t1;
  3451. /* channel 2: periodic and oneshot timer support */
  3452. +#ifdef CONFIG_ATMEL_TCB_CLKSRC_USE_SLOW_CLOCK
  3453. ret = setup_clkevents(tc, clk32k_divisor_idx);
  3454. +#else
  3455. + ret = setup_clkevents(tc, best_divisor_idx);
  3456. +#endif
  3457. if (ret)
  3458. goto err_unregister_clksrc;
  3459. diff -Nur linux-4.9.28.orig/drivers/clocksource/timer-atmel-pit.c linux-4.9.28/drivers/clocksource/timer-atmel-pit.c
  3460. --- linux-4.9.28.orig/drivers/clocksource/timer-atmel-pit.c 2017-05-14 14:00:37.000000000 +0200
  3461. +++ linux-4.9.28/drivers/clocksource/timer-atmel-pit.c 2017-05-19 03:37:25.138175108 +0200
  3462. @@ -46,6 +46,7 @@
  3463. u32 cycle;
  3464. u32 cnt;
  3465. unsigned int irq;
  3466. + bool irq_requested;
  3467. struct clk *mck;
  3468. };
  3469. @@ -96,15 +97,29 @@
  3470. /* disable irq, leaving the clocksource active */
  3471. pit_write(data->base, AT91_PIT_MR, (data->cycle - 1) | AT91_PIT_PITEN);
  3472. + if (data->irq_requested) {
  3473. + free_irq(data->irq, data);
  3474. + data->irq_requested = false;
  3475. + }
  3476. return 0;
  3477. }
  3478. +static irqreturn_t at91sam926x_pit_interrupt(int irq, void *dev_id);
  3479. /*
  3480. * Clockevent device: interrupts every 1/HZ (== pit_cycles * MCK/16)
  3481. */
  3482. static int pit_clkevt_set_periodic(struct clock_event_device *dev)
  3483. {
  3484. struct pit_data *data = clkevt_to_pit_data(dev);
  3485. + int ret;
  3486. +
  3487. + ret = request_irq(data->irq, at91sam926x_pit_interrupt,
  3488. + IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
  3489. + "at91_tick", data);
  3490. + if (ret)
  3491. + panic(pr_fmt("Unable to setup IRQ\n"));
  3492. +
  3493. + data->irq_requested = true;
  3494. /* update clocksource counter */
  3495. data->cnt += data->cycle * PIT_PICNT(pit_read(data->base, AT91_PIT_PIVR));
  3496. @@ -230,15 +245,6 @@
  3497. return ret;
  3498. }
  3499. - /* Set up irq handler */
  3500. - ret = request_irq(data->irq, at91sam926x_pit_interrupt,
  3501. - IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
  3502. - "at91_tick", data);
  3503. - if (ret) {
  3504. - pr_err("Unable to setup IRQ\n");
  3505. - return ret;
  3506. - }
  3507. -
  3508. /* Set up and register clockevents */
  3509. data->clkevt.name = "pit";
  3510. data->clkevt.features = CLOCK_EVT_FEAT_PERIODIC;
  3511. diff -Nur linux-4.9.28.orig/drivers/clocksource/timer-atmel-st.c linux-4.9.28/drivers/clocksource/timer-atmel-st.c
  3512. --- linux-4.9.28.orig/drivers/clocksource/timer-atmel-st.c 2017-05-14 14:00:37.000000000 +0200
  3513. +++ linux-4.9.28/drivers/clocksource/timer-atmel-st.c 2017-05-19 03:37:25.138175108 +0200
  3514. @@ -115,18 +115,29 @@
  3515. last_crtr = read_CRTR();
  3516. }
  3517. +static int atmel_st_irq;
  3518. +
  3519. static int clkevt32k_shutdown(struct clock_event_device *evt)
  3520. {
  3521. clkdev32k_disable_and_flush_irq();
  3522. irqmask = 0;
  3523. regmap_write(regmap_st, AT91_ST_IER, irqmask);
  3524. + free_irq(atmel_st_irq, regmap_st);
  3525. return 0;
  3526. }
  3527. static int clkevt32k_set_oneshot(struct clock_event_device *dev)
  3528. {
  3529. + int ret;
  3530. +
  3531. clkdev32k_disable_and_flush_irq();
  3532. + ret = request_irq(atmel_st_irq, at91rm9200_timer_interrupt,
  3533. + IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
  3534. + "at91_tick", regmap_st);
  3535. + if (ret)
  3536. + panic(pr_fmt("Unable to setup IRQ\n"));
  3537. +
  3538. /*
  3539. * ALM for oneshot irqs, set by next_event()
  3540. * before 32 seconds have passed.
  3541. @@ -139,8 +150,16 @@
  3542. static int clkevt32k_set_periodic(struct clock_event_device *dev)
  3543. {
  3544. + int ret;
  3545. +
  3546. clkdev32k_disable_and_flush_irq();
  3547. + ret = request_irq(atmel_st_irq, at91rm9200_timer_interrupt,
  3548. + IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
  3549. + "at91_tick", regmap_st);
  3550. + if (ret)
  3551. + panic(pr_fmt("Unable to setup IRQ\n"));
  3552. +
  3553. /* PIT for periodic irqs; fixed rate of 1/HZ */
  3554. irqmask = AT91_ST_PITS;
  3555. regmap_write(regmap_st, AT91_ST_PIMR, timer_latch);
  3556. @@ -198,7 +217,7 @@
  3557. {
  3558. struct clk *sclk;
  3559. unsigned int sclk_rate, val;
  3560. - int irq, ret;
  3561. + int ret;
  3562. regmap_st = syscon_node_to_regmap(node);
  3563. if (IS_ERR(regmap_st)) {
  3564. @@ -212,21 +231,12 @@
  3565. regmap_read(regmap_st, AT91_ST_SR, &val);
  3566. /* Get the interrupts property */
  3567. - irq = irq_of_parse_and_map(node, 0);
  3568. - if (!irq) {
  3569. + atmel_st_irq = irq_of_parse_and_map(node, 0);
  3570. + if (!atmel_st_irq) {
  3571. pr_err("Unable to get IRQ from DT\n");
  3572. return -EINVAL;
  3573. }
  3574. - /* Make IRQs happen for the system timer */
  3575. - ret = request_irq(irq, at91rm9200_timer_interrupt,
  3576. - IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
  3577. - "at91_tick", regmap_st);
  3578. - if (ret) {
  3579. - pr_err("Unable to setup IRQ\n");
  3580. - return ret;
  3581. - }
  3582. -
  3583. sclk = of_clk_get(node, 0);
  3584. if (IS_ERR(sclk)) {
  3585. pr_err("Unable to get slow clock\n");
  3586. diff -Nur linux-4.9.28.orig/drivers/connector/cn_proc.c linux-4.9.28/drivers/connector/cn_proc.c
  3587. --- linux-4.9.28.orig/drivers/connector/cn_proc.c 2017-05-14 14:00:37.000000000 +0200
  3588. +++ linux-4.9.28/drivers/connector/cn_proc.c 2017-05-19 03:37:25.138175108 +0200
  3589. @@ -32,6 +32,7 @@
  3590. #include <linux/pid_namespace.h>
  3591. #include <linux/cn_proc.h>
  3592. +#include <linux/locallock.h>
  3593. /*
  3594. * Size of a cn_msg followed by a proc_event structure. Since the
  3595. @@ -54,10 +55,11 @@
  3596. /* proc_event_counts is used as the sequence number of the netlink message */
  3597. static DEFINE_PER_CPU(__u32, proc_event_counts) = { 0 };
  3598. +static DEFINE_LOCAL_IRQ_LOCK(send_msg_lock);
  3599. static inline void send_msg(struct cn_msg *msg)
  3600. {
  3601. - preempt_disable();
  3602. + local_lock(send_msg_lock);
  3603. msg->seq = __this_cpu_inc_return(proc_event_counts) - 1;
  3604. ((struct proc_event *)msg->data)->cpu = smp_processor_id();
  3605. @@ -70,7 +72,7 @@
  3606. */
  3607. cn_netlink_send(msg, 0, CN_IDX_PROC, GFP_NOWAIT);
  3608. - preempt_enable();
  3609. + local_unlock(send_msg_lock);
  3610. }
  3611. void proc_fork_connector(struct task_struct *task)
  3612. diff -Nur linux-4.9.28.orig/drivers/cpufreq/Kconfig.x86 linux-4.9.28/drivers/cpufreq/Kconfig.x86
  3613. --- linux-4.9.28.orig/drivers/cpufreq/Kconfig.x86 2017-05-14 14:00:37.000000000 +0200
  3614. +++ linux-4.9.28/drivers/cpufreq/Kconfig.x86 2017-05-19 03:37:25.138175108 +0200
  3615. @@ -124,7 +124,7 @@
  3616. config X86_POWERNOW_K8
  3617. tristate "AMD Opteron/Athlon64 PowerNow!"
  3618. - depends on ACPI && ACPI_PROCESSOR && X86_ACPI_CPUFREQ
  3619. + depends on ACPI && ACPI_PROCESSOR && X86_ACPI_CPUFREQ && !PREEMPT_RT_BASE
  3620. help
  3621. This adds the CPUFreq driver for K8/early Opteron/Athlon64 processors.
  3622. Support for K10 and newer processors is now in acpi-cpufreq.
  3623. diff -Nur linux-4.9.28.orig/drivers/gpu/drm/i915/i915_gem_execbuffer.c linux-4.9.28/drivers/gpu/drm/i915/i915_gem_execbuffer.c
  3624. --- linux-4.9.28.orig/drivers/gpu/drm/i915/i915_gem_execbuffer.c 2017-05-14 14:00:37.000000000 +0200
  3625. +++ linux-4.9.28/drivers/gpu/drm/i915/i915_gem_execbuffer.c 2017-05-19 03:37:25.138175108 +0200
  3626. @@ -1489,7 +1489,9 @@
  3627. if (ret)
  3628. return ret;
  3629. +#ifndef CONFIG_PREEMPT_RT_BASE
  3630. trace_i915_gem_ring_dispatch(params->request, params->dispatch_flags);
  3631. +#endif
  3632. i915_gem_execbuffer_move_to_active(vmas, params->request);
  3633. diff -Nur linux-4.9.28.orig/drivers/gpu/drm/i915/i915_gem_shrinker.c linux-4.9.28/drivers/gpu/drm/i915/i915_gem_shrinker.c
  3634. --- linux-4.9.28.orig/drivers/gpu/drm/i915/i915_gem_shrinker.c 2017-05-14 14:00:37.000000000 +0200
  3635. +++ linux-4.9.28/drivers/gpu/drm/i915/i915_gem_shrinker.c 2017-05-19 03:37:25.138175108 +0200
  3636. @@ -40,7 +40,7 @@
  3637. if (!mutex_is_locked(mutex))
  3638. return false;
  3639. -#if defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_MUTEX_SPIN_ON_OWNER)
  3640. +#if (defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_MUTEX_SPIN_ON_OWNER)) && !defined(CONFIG_PREEMPT_RT_BASE)
  3641. return mutex->owner == task;
  3642. #else
  3643. /* Since UP may be pre-empted, we cannot assume that we own the lock */
  3644. diff -Nur linux-4.9.28.orig/drivers/gpu/drm/i915/i915_irq.c linux-4.9.28/drivers/gpu/drm/i915/i915_irq.c
  3645. --- linux-4.9.28.orig/drivers/gpu/drm/i915/i915_irq.c 2017-05-14 14:00:37.000000000 +0200
  3646. +++ linux-4.9.28/drivers/gpu/drm/i915/i915_irq.c 2017-05-19 03:37:25.142175268 +0200
  3647. @@ -812,6 +812,7 @@
  3648. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  3649. /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
  3650. + preempt_disable_rt();
  3651. /* Get optional system timestamp before query. */
  3652. if (stime)
  3653. @@ -863,6 +864,7 @@
  3654. *etime = ktime_get();
  3655. /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
  3656. + preempt_enable_rt();
  3657. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  3658. diff -Nur linux-4.9.28.orig/drivers/gpu/drm/i915/intel_display.c linux-4.9.28/drivers/gpu/drm/i915/intel_display.c
  3659. --- linux-4.9.28.orig/drivers/gpu/drm/i915/intel_display.c 2017-05-14 14:00:37.000000000 +0200
  3660. +++ linux-4.9.28/drivers/gpu/drm/i915/intel_display.c 2017-05-19 03:37:25.142175268 +0200
  3661. @@ -12131,7 +12131,7 @@
  3662. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3663. struct intel_flip_work *work;
  3664. - WARN_ON(!in_interrupt());
  3665. + WARN_ON_NONRT(!in_interrupt());
  3666. if (crtc == NULL)
  3667. return;
  3668. diff -Nur linux-4.9.28.orig/drivers/gpu/drm/i915/intel_sprite.c linux-4.9.28/drivers/gpu/drm/i915/intel_sprite.c
  3669. --- linux-4.9.28.orig/drivers/gpu/drm/i915/intel_sprite.c 2017-05-14 14:00:37.000000000 +0200
  3670. +++ linux-4.9.28/drivers/gpu/drm/i915/intel_sprite.c 2017-05-19 03:37:25.142175268 +0200
  3671. @@ -35,6 +35,7 @@
  3672. #include <drm/drm_rect.h>
  3673. #include <drm/drm_atomic.h>
  3674. #include <drm/drm_plane_helper.h>
  3675. +#include <linux/locallock.h>
  3676. #include "intel_drv.h"
  3677. #include "intel_frontbuffer.h"
  3678. #include <drm/i915_drm.h>
  3679. @@ -65,6 +66,8 @@
  3680. 1000 * adjusted_mode->crtc_htotal);
  3681. }
  3682. +static DEFINE_LOCAL_IRQ_LOCK(pipe_update_lock);
  3683. +
  3684. /**
  3685. * intel_pipe_update_start() - start update of a set of display registers
  3686. * @crtc: the crtc of which the registers are going to be updated
  3687. @@ -95,7 +98,7 @@
  3688. min = vblank_start - intel_usecs_to_scanlines(adjusted_mode, 100);
  3689. max = vblank_start - 1;
  3690. - local_irq_disable();
  3691. + local_lock_irq(pipe_update_lock);
  3692. if (min <= 0 || max <= 0)
  3693. return;
  3694. @@ -125,11 +128,11 @@
  3695. break;
  3696. }
  3697. - local_irq_enable();
  3698. + local_unlock_irq(pipe_update_lock);
  3699. timeout = schedule_timeout(timeout);
  3700. - local_irq_disable();
  3701. + local_lock_irq(pipe_update_lock);
  3702. }
  3703. finish_wait(wq, &wait);
  3704. @@ -181,7 +184,7 @@
  3705. crtc->base.state->event = NULL;
  3706. }
  3707. - local_irq_enable();
  3708. + local_unlock_irq(pipe_update_lock);
  3709. if (crtc->debug.start_vbl_count &&
  3710. crtc->debug.start_vbl_count != end_vbl_count) {
  3711. diff -Nur linux-4.9.28.orig/drivers/gpu/drm/msm/msm_gem_shrinker.c linux-4.9.28/drivers/gpu/drm/msm/msm_gem_shrinker.c
  3712. --- linux-4.9.28.orig/drivers/gpu/drm/msm/msm_gem_shrinker.c 2017-05-14 14:00:37.000000000 +0200
  3713. +++ linux-4.9.28/drivers/gpu/drm/msm/msm_gem_shrinker.c 2017-05-19 03:37:25.142175268 +0200
  3714. @@ -23,7 +23,7 @@
  3715. if (!mutex_is_locked(mutex))
  3716. return false;
  3717. -#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_MUTEXES)
  3718. +#if (defined(CONFIG_SMP) || defined(CONFIG_DEBUG_MUTEXES)) && !defined(CONFIG_PREEMPT_RT_BASE)
  3719. return mutex->owner == task;
  3720. #else
  3721. /* Since UP may be pre-empted, we cannot assume that we own the lock */
  3722. diff -Nur linux-4.9.28.orig/drivers/gpu/drm/radeon/radeon_display.c linux-4.9.28/drivers/gpu/drm/radeon/radeon_display.c
  3723. --- linux-4.9.28.orig/drivers/gpu/drm/radeon/radeon_display.c 2017-05-14 14:00:37.000000000 +0200
  3724. +++ linux-4.9.28/drivers/gpu/drm/radeon/radeon_display.c 2017-05-19 03:37:25.142175268 +0200
  3725. @@ -1845,6 +1845,7 @@
  3726. struct radeon_device *rdev = dev->dev_private;
  3727. /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
  3728. + preempt_disable_rt();
  3729. /* Get optional system timestamp before query. */
  3730. if (stime)
  3731. @@ -1937,6 +1938,7 @@
  3732. *etime = ktime_get();
  3733. /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
  3734. + preempt_enable_rt();
  3735. /* Decode into vertical and horizontal scanout position. */
  3736. *vpos = position & 0x1fff;
  3737. diff -Nur linux-4.9.28.orig/drivers/hv/vmbus_drv.c linux-4.9.28/drivers/hv/vmbus_drv.c
  3738. --- linux-4.9.28.orig/drivers/hv/vmbus_drv.c 2017-05-14 14:00:37.000000000 +0200
  3739. +++ linux-4.9.28/drivers/hv/vmbus_drv.c 2017-05-19 03:37:25.142175268 +0200
  3740. @@ -761,6 +761,8 @@
  3741. void *page_addr;
  3742. struct hv_message *msg;
  3743. union hv_synic_event_flags *event;
  3744. + struct pt_regs *regs = get_irq_regs();
  3745. + u64 ip = regs ? instruction_pointer(regs) : 0;
  3746. bool handled = false;
  3747. page_addr = hv_context.synic_event_page[cpu];
  3748. @@ -808,7 +810,7 @@
  3749. tasklet_schedule(hv_context.msg_dpc[cpu]);
  3750. }
  3751. - add_interrupt_randomness(HYPERVISOR_CALLBACK_VECTOR, 0);
  3752. + add_interrupt_randomness(HYPERVISOR_CALLBACK_VECTOR, 0, ip);
  3753. }
  3754. diff -Nur linux-4.9.28.orig/drivers/ide/alim15x3.c linux-4.9.28/drivers/ide/alim15x3.c
  3755. --- linux-4.9.28.orig/drivers/ide/alim15x3.c 2017-05-14 14:00:37.000000000 +0200
  3756. +++ linux-4.9.28/drivers/ide/alim15x3.c 2017-05-19 03:37:25.142175268 +0200
  3757. @@ -234,7 +234,7 @@
  3758. isa_dev = pci_get_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533, NULL);
  3759. - local_irq_save(flags);
  3760. + local_irq_save_nort(flags);
  3761. if (m5229_revision < 0xC2) {
  3762. /*
  3763. @@ -325,7 +325,7 @@
  3764. }
  3765. pci_dev_put(north);
  3766. pci_dev_put(isa_dev);
  3767. - local_irq_restore(flags);
  3768. + local_irq_restore_nort(flags);
  3769. return 0;
  3770. }
  3771. diff -Nur linux-4.9.28.orig/drivers/ide/hpt366.c linux-4.9.28/drivers/ide/hpt366.c
  3772. --- linux-4.9.28.orig/drivers/ide/hpt366.c 2017-05-14 14:00:37.000000000 +0200
  3773. +++ linux-4.9.28/drivers/ide/hpt366.c 2017-05-19 03:37:25.142175268 +0200
  3774. @@ -1236,7 +1236,7 @@
  3775. dma_old = inb(base + 2);
  3776. - local_irq_save(flags);
  3777. + local_irq_save_nort(flags);
  3778. dma_new = dma_old;
  3779. pci_read_config_byte(dev, hwif->channel ? 0x4b : 0x43, &masterdma);
  3780. @@ -1247,7 +1247,7 @@
  3781. if (dma_new != dma_old)
  3782. outb(dma_new, base + 2);
  3783. - local_irq_restore(flags);
  3784. + local_irq_restore_nort(flags);
  3785. printk(KERN_INFO " %s: BM-DMA at 0x%04lx-0x%04lx\n",
  3786. hwif->name, base, base + 7);
  3787. diff -Nur linux-4.9.28.orig/drivers/ide/ide-io.c linux-4.9.28/drivers/ide/ide-io.c
  3788. --- linux-4.9.28.orig/drivers/ide/ide-io.c 2017-05-14 14:00:37.000000000 +0200
  3789. +++ linux-4.9.28/drivers/ide/ide-io.c 2017-05-19 03:37:25.146175423 +0200
  3790. @@ -659,7 +659,7 @@
  3791. /* disable_irq_nosync ?? */
  3792. disable_irq(hwif->irq);
  3793. /* local CPU only, as if we were handling an interrupt */
  3794. - local_irq_disable();
  3795. + local_irq_disable_nort();
  3796. if (hwif->polling) {
  3797. startstop = handler(drive);
  3798. } else if (drive_is_ready(drive)) {
  3799. diff -Nur linux-4.9.28.orig/drivers/ide/ide-iops.c linux-4.9.28/drivers/ide/ide-iops.c
  3800. --- linux-4.9.28.orig/drivers/ide/ide-iops.c 2017-05-14 14:00:37.000000000 +0200
  3801. +++ linux-4.9.28/drivers/ide/ide-iops.c 2017-05-19 03:37:25.146175423 +0200
  3802. @@ -129,12 +129,12 @@
  3803. if ((stat & ATA_BUSY) == 0)
  3804. break;
  3805. - local_irq_restore(flags);
  3806. + local_irq_restore_nort(flags);
  3807. *rstat = stat;
  3808. return -EBUSY;
  3809. }
  3810. }
  3811. - local_irq_restore(flags);
  3812. + local_irq_restore_nort(flags);
  3813. }
  3814. /*
  3815. * Allow status to settle, then read it again.
  3816. diff -Nur linux-4.9.28.orig/drivers/ide/ide-io-std.c linux-4.9.28/drivers/ide/ide-io-std.c
  3817. --- linux-4.9.28.orig/drivers/ide/ide-io-std.c 2017-05-14 14:00:37.000000000 +0200
  3818. +++ linux-4.9.28/drivers/ide/ide-io-std.c 2017-05-19 03:37:25.142175268 +0200
  3819. @@ -175,7 +175,7 @@
  3820. unsigned long uninitialized_var(flags);
  3821. if ((io_32bit & 2) && !mmio) {
  3822. - local_irq_save(flags);
  3823. + local_irq_save_nort(flags);
  3824. ata_vlb_sync(io_ports->nsect_addr);
  3825. }
  3826. @@ -186,7 +186,7 @@
  3827. insl(data_addr, buf, words);
  3828. if ((io_32bit & 2) && !mmio)
  3829. - local_irq_restore(flags);
  3830. + local_irq_restore_nort(flags);
  3831. if (((len + 1) & 3) < 2)
  3832. return;
  3833. @@ -219,7 +219,7 @@
  3834. unsigned long uninitialized_var(flags);
  3835. if ((io_32bit & 2) && !mmio) {
  3836. - local_irq_save(flags);
  3837. + local_irq_save_nort(flags);
  3838. ata_vlb_sync(io_ports->nsect_addr);
  3839. }
  3840. @@ -230,7 +230,7 @@
  3841. outsl(data_addr, buf, words);
  3842. if ((io_32bit & 2) && !mmio)
  3843. - local_irq_restore(flags);
  3844. + local_irq_restore_nort(flags);
  3845. if (((len + 1) & 3) < 2)
  3846. return;
  3847. diff -Nur linux-4.9.28.orig/drivers/ide/ide-probe.c linux-4.9.28/drivers/ide/ide-probe.c
  3848. --- linux-4.9.28.orig/drivers/ide/ide-probe.c 2017-05-14 14:00:37.000000000 +0200
  3849. +++ linux-4.9.28/drivers/ide/ide-probe.c 2017-05-19 03:37:25.146175423 +0200
  3850. @@ -196,10 +196,10 @@
  3851. int bswap = 1;
  3852. /* local CPU only; some systems need this */
  3853. - local_irq_save(flags);
  3854. + local_irq_save_nort(flags);
  3855. /* read 512 bytes of id info */
  3856. hwif->tp_ops->input_data(drive, NULL, id, SECTOR_SIZE);
  3857. - local_irq_restore(flags);
  3858. + local_irq_restore_nort(flags);
  3859. drive->dev_flags |= IDE_DFLAG_ID_READ;
  3860. #ifdef DEBUG
  3861. diff -Nur linux-4.9.28.orig/drivers/ide/ide-taskfile.c linux-4.9.28/drivers/ide/ide-taskfile.c
  3862. --- linux-4.9.28.orig/drivers/ide/ide-taskfile.c 2017-05-14 14:00:37.000000000 +0200
  3863. +++ linux-4.9.28/drivers/ide/ide-taskfile.c 2017-05-19 03:37:25.146175423 +0200
  3864. @@ -250,7 +250,7 @@
  3865. page_is_high = PageHighMem(page);
  3866. if (page_is_high)
  3867. - local_irq_save(flags);
  3868. + local_irq_save_nort(flags);
  3869. buf = kmap_atomic(page) + offset;
  3870. @@ -271,7 +271,7 @@
  3871. kunmap_atomic(buf);
  3872. if (page_is_high)
  3873. - local_irq_restore(flags);
  3874. + local_irq_restore_nort(flags);
  3875. len -= nr_bytes;
  3876. }
  3877. @@ -414,7 +414,7 @@
  3878. }
  3879. if ((drive->dev_flags & IDE_DFLAG_UNMASK) == 0)
  3880. - local_irq_disable();
  3881. + local_irq_disable_nort();
  3882. ide_set_handler(drive, &task_pio_intr, WAIT_WORSTCASE);
  3883. diff -Nur linux-4.9.28.orig/drivers/infiniband/ulp/ipoib/ipoib_multicast.c linux-4.9.28/drivers/infiniband/ulp/ipoib/ipoib_multicast.c
  3884. --- linux-4.9.28.orig/drivers/infiniband/ulp/ipoib/ipoib_multicast.c 2017-05-14 14:00:37.000000000 +0200
  3885. +++ linux-4.9.28/drivers/infiniband/ulp/ipoib/ipoib_multicast.c 2017-05-19 03:37:25.146175423 +0200
  3886. @@ -902,7 +902,7 @@
  3887. ipoib_dbg_mcast(priv, "restarting multicast task\n");
  3888. - local_irq_save(flags);
  3889. + local_irq_save_nort(flags);
  3890. netif_addr_lock(dev);
  3891. spin_lock(&priv->lock);
  3892. @@ -984,7 +984,7 @@
  3893. spin_unlock(&priv->lock);
  3894. netif_addr_unlock(dev);
  3895. - local_irq_restore(flags);
  3896. + local_irq_restore_nort(flags);
  3897. /*
  3898. * make sure the in-flight joins have finished before we attempt
  3899. diff -Nur linux-4.9.28.orig/drivers/input/gameport/gameport.c linux-4.9.28/drivers/input/gameport/gameport.c
  3900. --- linux-4.9.28.orig/drivers/input/gameport/gameport.c 2017-05-14 14:00:37.000000000 +0200
  3901. +++ linux-4.9.28/drivers/input/gameport/gameport.c 2017-05-19 03:37:25.146175423 +0200
  3902. @@ -91,13 +91,13 @@
  3903. tx = ~0;
  3904. for (i = 0; i < 50; i++) {
  3905. - local_irq_save(flags);
  3906. + local_irq_save_nort(flags);
  3907. t1 = ktime_get_ns();
  3908. for (t = 0; t < 50; t++)
  3909. gameport_read(gameport);
  3910. t2 = ktime_get_ns();
  3911. t3 = ktime_get_ns();
  3912. - local_irq_restore(flags);
  3913. + local_irq_restore_nort(flags);
  3914. udelay(i * 10);
  3915. t = (t2 - t1) - (t3 - t2);
  3916. if (t < tx)
  3917. @@ -124,12 +124,12 @@
  3918. tx = 1 << 30;
  3919. for(i = 0; i < 50; i++) {
  3920. - local_irq_save(flags);
  3921. + local_irq_save_nort(flags);
  3922. GET_TIME(t1);
  3923. for (t = 0; t < 50; t++) gameport_read(gameport);
  3924. GET_TIME(t2);
  3925. GET_TIME(t3);
  3926. - local_irq_restore(flags);
  3927. + local_irq_restore_nort(flags);
  3928. udelay(i * 10);
  3929. if ((t = DELTA(t2,t1) - DELTA(t3,t2)) < tx) tx = t;
  3930. }
  3931. @@ -148,11 +148,11 @@
  3932. tx = 1 << 30;
  3933. for(i = 0; i < 50; i++) {
  3934. - local_irq_save(flags);
  3935. + local_irq_save_nort(flags);
  3936. t1 = rdtsc();
  3937. for (t = 0; t < 50; t++) gameport_read(gameport);
  3938. t2 = rdtsc();
  3939. - local_irq_restore(flags);
  3940. + local_irq_restore_nort(flags);
  3941. udelay(i * 10);
  3942. if (t2 - t1 < tx) tx = t2 - t1;
  3943. }
  3944. diff -Nur linux-4.9.28.orig/drivers/iommu/amd_iommu.c linux-4.9.28/drivers/iommu/amd_iommu.c
  3945. --- linux-4.9.28.orig/drivers/iommu/amd_iommu.c 2017-05-14 14:00:37.000000000 +0200
  3946. +++ linux-4.9.28/drivers/iommu/amd_iommu.c 2017-05-19 03:37:25.146175423 +0200
  3947. @@ -1923,10 +1923,10 @@
  3948. int ret;
  3949. /*
  3950. - * Must be called with IRQs disabled. Warn here to detect early
  3951. - * when its not.
  3952. + * Must be called with IRQs disabled on a non RT kernel. Warn here to
  3953. + * detect early when its not.
  3954. */
  3955. - WARN_ON(!irqs_disabled());
  3956. + WARN_ON_NONRT(!irqs_disabled());
  3957. /* lock domain */
  3958. spin_lock(&domain->lock);
  3959. @@ -2094,10 +2094,10 @@
  3960. struct protection_domain *domain;
  3961. /*
  3962. - * Must be called with IRQs disabled. Warn here to detect early
  3963. - * when its not.
  3964. + * Must be called with IRQs disabled on a non RT kernel. Warn here to
  3965. + * detect early when its not.
  3966. */
  3967. - WARN_ON(!irqs_disabled());
  3968. + WARN_ON_NONRT(!irqs_disabled());
  3969. if (WARN_ON(!dev_data->domain))
  3970. return;
  3971. diff -Nur linux-4.9.28.orig/drivers/iommu/intel-iommu.c linux-4.9.28/drivers/iommu/intel-iommu.c
  3972. --- linux-4.9.28.orig/drivers/iommu/intel-iommu.c 2017-05-14 14:00:37.000000000 +0200
  3973. +++ linux-4.9.28/drivers/iommu/intel-iommu.c 2017-05-19 03:37:25.146175423 +0200
  3974. @@ -479,7 +479,7 @@
  3975. struct deferred_flush_table *tables;
  3976. };
  3977. -DEFINE_PER_CPU(struct deferred_flush_data, deferred_flush);
  3978. +static DEFINE_PER_CPU(struct deferred_flush_data, deferred_flush);
  3979. /* bitmap for indexing intel_iommus */
  3980. static int g_num_of_iommus;
  3981. @@ -3716,10 +3716,8 @@
  3982. struct intel_iommu *iommu;
  3983. struct deferred_flush_entry *entry;
  3984. struct deferred_flush_data *flush_data;
  3985. - unsigned int cpuid;
  3986. - cpuid = get_cpu();
  3987. - flush_data = per_cpu_ptr(&deferred_flush, cpuid);
  3988. + flush_data = raw_cpu_ptr(&deferred_flush);
  3989. /* Flush all CPUs' entries to avoid deferring too much. If
  3990. * this becomes a bottleneck, can just flush us, and rely on
  3991. @@ -3752,8 +3750,6 @@
  3992. }
  3993. flush_data->size++;
  3994. spin_unlock_irqrestore(&flush_data->lock, flags);
  3995. -
  3996. - put_cpu();
  3997. }
  3998. static void intel_unmap(struct device *dev, dma_addr_t dev_addr, size_t size)
  3999. diff -Nur linux-4.9.28.orig/drivers/iommu/iova.c linux-4.9.28/drivers/iommu/iova.c
  4000. --- linux-4.9.28.orig/drivers/iommu/iova.c 2017-05-14 14:00:37.000000000 +0200
  4001. +++ linux-4.9.28/drivers/iommu/iova.c 2017-05-19 03:37:25.146175423 +0200
  4002. @@ -22,6 +22,7 @@
  4003. #include <linux/slab.h>
  4004. #include <linux/smp.h>
  4005. #include <linux/bitops.h>
  4006. +#include <linux/cpu.h>
  4007. static bool iova_rcache_insert(struct iova_domain *iovad,
  4008. unsigned long pfn,
  4009. @@ -420,10 +421,8 @@
  4010. /* Try replenishing IOVAs by flushing rcache. */
  4011. flushed_rcache = true;
  4012. - preempt_disable();
  4013. for_each_online_cpu(cpu)
  4014. free_cpu_cached_iovas(cpu, iovad);
  4015. - preempt_enable();
  4016. goto retry;
  4017. }
  4018. @@ -751,7 +750,7 @@
  4019. bool can_insert = false;
  4020. unsigned long flags;
  4021. - cpu_rcache = get_cpu_ptr(rcache->cpu_rcaches);
  4022. + cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
  4023. spin_lock_irqsave(&cpu_rcache->lock, flags);
  4024. if (!iova_magazine_full(cpu_rcache->loaded)) {
  4025. @@ -781,7 +780,6 @@
  4026. iova_magazine_push(cpu_rcache->loaded, iova_pfn);
  4027. spin_unlock_irqrestore(&cpu_rcache->lock, flags);
  4028. - put_cpu_ptr(rcache->cpu_rcaches);
  4029. if (mag_to_free) {
  4030. iova_magazine_free_pfns(mag_to_free, iovad);
  4031. @@ -815,7 +813,7 @@
  4032. bool has_pfn = false;
  4033. unsigned long flags;
  4034. - cpu_rcache = get_cpu_ptr(rcache->cpu_rcaches);
  4035. + cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
  4036. spin_lock_irqsave(&cpu_rcache->lock, flags);
  4037. if (!iova_magazine_empty(cpu_rcache->loaded)) {
  4038. @@ -837,7 +835,6 @@
  4039. iova_pfn = iova_magazine_pop(cpu_rcache->loaded, limit_pfn);
  4040. spin_unlock_irqrestore(&cpu_rcache->lock, flags);
  4041. - put_cpu_ptr(rcache->cpu_rcaches);
  4042. return iova_pfn;
  4043. }
  4044. diff -Nur linux-4.9.28.orig/drivers/leds/trigger/Kconfig linux-4.9.28/drivers/leds/trigger/Kconfig
  4045. --- linux-4.9.28.orig/drivers/leds/trigger/Kconfig 2017-05-14 14:00:37.000000000 +0200
  4046. +++ linux-4.9.28/drivers/leds/trigger/Kconfig 2017-05-19 03:37:25.146175423 +0200
  4047. @@ -69,7 +69,7 @@
  4048. config LEDS_TRIGGER_CPU
  4049. bool "LED CPU Trigger"
  4050. - depends on LEDS_TRIGGERS
  4051. + depends on LEDS_TRIGGERS && !PREEMPT_RT_BASE
  4052. help
  4053. This allows LEDs to be controlled by active CPUs. This shows
  4054. the active CPUs across an array of LEDs so you can see which
  4055. diff -Nur linux-4.9.28.orig/drivers/md/bcache/Kconfig linux-4.9.28/drivers/md/bcache/Kconfig
  4056. --- linux-4.9.28.orig/drivers/md/bcache/Kconfig 2017-05-14 14:00:37.000000000 +0200
  4057. +++ linux-4.9.28/drivers/md/bcache/Kconfig 2017-05-19 03:37:25.146175423 +0200
  4058. @@ -1,6 +1,7 @@
  4059. config BCACHE
  4060. tristate "Block device as cache"
  4061. + depends on !PREEMPT_RT_FULL
  4062. ---help---
  4063. Allows a block device to be used as cache for other devices; uses
  4064. a btree for indexing and the layout is optimized for SSDs.
  4065. diff -Nur linux-4.9.28.orig/drivers/md/dm-rq.c linux-4.9.28/drivers/md/dm-rq.c
  4066. --- linux-4.9.28.orig/drivers/md/dm-rq.c 2017-05-14 14:00:37.000000000 +0200
  4067. +++ linux-4.9.28/drivers/md/dm-rq.c 2017-05-19 03:37:25.146175423 +0200
  4068. @@ -842,7 +842,7 @@
  4069. /* Establish tio->ti before queuing work (map_tio_request) */
  4070. tio->ti = ti;
  4071. kthread_queue_work(&md->kworker, &tio->work);
  4072. - BUG_ON(!irqs_disabled());
  4073. + BUG_ON_NONRT(!irqs_disabled());
  4074. }
  4075. }
  4076. diff -Nur linux-4.9.28.orig/drivers/md/raid5.c linux-4.9.28/drivers/md/raid5.c
  4077. --- linux-4.9.28.orig/drivers/md/raid5.c 2017-05-14 14:00:37.000000000 +0200
  4078. +++ linux-4.9.28/drivers/md/raid5.c 2017-05-19 03:37:25.146175423 +0200
  4079. @@ -1928,8 +1928,9 @@
  4080. struct raid5_percpu *percpu;
  4081. unsigned long cpu;
  4082. - cpu = get_cpu();
  4083. + cpu = get_cpu_light();
  4084. percpu = per_cpu_ptr(conf->percpu, cpu);
  4085. + spin_lock(&percpu->lock);
  4086. if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
  4087. ops_run_biofill(sh);
  4088. overlap_clear++;
  4089. @@ -1985,7 +1986,8 @@
  4090. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  4091. wake_up(&sh->raid_conf->wait_for_overlap);
  4092. }
  4093. - put_cpu();
  4094. + spin_unlock(&percpu->lock);
  4095. + put_cpu_light();
  4096. }
  4097. static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp,
  4098. @@ -6391,6 +6393,7 @@
  4099. __func__, cpu);
  4100. return -ENOMEM;
  4101. }
  4102. + spin_lock_init(&per_cpu_ptr(conf->percpu, cpu)->lock);
  4103. return 0;
  4104. }
  4105. @@ -6401,7 +6404,6 @@
  4106. conf->percpu = alloc_percpu(struct raid5_percpu);
  4107. if (!conf->percpu)
  4108. return -ENOMEM;
  4109. -
  4110. err = cpuhp_state_add_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
  4111. if (!err) {
  4112. conf->scribble_disks = max(conf->raid_disks,
  4113. diff -Nur linux-4.9.28.orig/drivers/md/raid5.h linux-4.9.28/drivers/md/raid5.h
  4114. --- linux-4.9.28.orig/drivers/md/raid5.h 2017-05-14 14:00:37.000000000 +0200
  4115. +++ linux-4.9.28/drivers/md/raid5.h 2017-05-19 03:37:25.150175574 +0200
  4116. @@ -504,6 +504,7 @@
  4117. int recovery_disabled;
  4118. /* per cpu variables */
  4119. struct raid5_percpu {
  4120. + spinlock_t lock; /* Protection for -RT */
  4121. struct page *spare_page; /* Used when checking P/Q in raid6 */
  4122. struct flex_array *scribble; /* space for constructing buffer
  4123. * lists and performing address
  4124. diff -Nur linux-4.9.28.orig/drivers/misc/Kconfig linux-4.9.28/drivers/misc/Kconfig
  4125. --- linux-4.9.28.orig/drivers/misc/Kconfig 2017-05-14 14:00:37.000000000 +0200
  4126. +++ linux-4.9.28/drivers/misc/Kconfig 2017-05-19 03:37:25.150175574 +0200
  4127. @@ -54,6 +54,7 @@
  4128. config ATMEL_TCLIB
  4129. bool "Atmel AT32/AT91 Timer/Counter Library"
  4130. depends on (AVR32 || ARCH_AT91)
  4131. + default y if PREEMPT_RT_FULL
  4132. help
  4133. Select this if you want a library to allocate the Timer/Counter
  4134. blocks found on many Atmel processors. This facilitates using
  4135. @@ -69,8 +70,7 @@
  4136. are combined to make a single 32-bit timer.
  4137. When GENERIC_CLOCKEVENTS is defined, the third timer channel
  4138. - may be used as a clock event device supporting oneshot mode
  4139. - (delays of up to two seconds) based on the 32 KiHz clock.
  4140. + may be used as a clock event device supporting oneshot mode.
  4141. config ATMEL_TCB_CLKSRC_BLOCK
  4142. int
  4143. @@ -84,6 +84,15 @@
  4144. TC can be used for other purposes, such as PWM generation and
  4145. interval timing.
  4146. +config ATMEL_TCB_CLKSRC_USE_SLOW_CLOCK
  4147. + bool "TC Block use 32 KiHz clock"
  4148. + depends on ATMEL_TCB_CLKSRC
  4149. + default y if !PREEMPT_RT_FULL
  4150. + help
  4151. + Select this to use 32 KiHz base clock rate as TC block clock
  4152. + source for clock events.
  4153. +
  4154. +
  4155. config DUMMY_IRQ
  4156. tristate "Dummy IRQ handler"
  4157. default n
  4158. diff -Nur linux-4.9.28.orig/drivers/mmc/host/mmci.c linux-4.9.28/drivers/mmc/host/mmci.c
  4159. --- linux-4.9.28.orig/drivers/mmc/host/mmci.c 2017-05-14 14:00:37.000000000 +0200
  4160. +++ linux-4.9.28/drivers/mmc/host/mmci.c 2017-05-19 03:37:25.150175574 +0200
  4161. @@ -1147,15 +1147,12 @@
  4162. struct sg_mapping_iter *sg_miter = &host->sg_miter;
  4163. struct variant_data *variant = host->variant;
  4164. void __iomem *base = host->base;
  4165. - unsigned long flags;
  4166. u32 status;
  4167. status = readl(base + MMCISTATUS);
  4168. dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
  4169. - local_irq_save(flags);
  4170. -
  4171. do {
  4172. unsigned int remain, len;
  4173. char *buffer;
  4174. @@ -1195,8 +1192,6 @@
  4175. sg_miter_stop(sg_miter);
  4176. - local_irq_restore(flags);
  4177. -
  4178. /*
  4179. * If we have less than the fifo 'half-full' threshold to transfer,
  4180. * trigger a PIO interrupt as soon as any data is available.
  4181. diff -Nur linux-4.9.28.orig/drivers/net/ethernet/3com/3c59x.c linux-4.9.28/drivers/net/ethernet/3com/3c59x.c
  4182. --- linux-4.9.28.orig/drivers/net/ethernet/3com/3c59x.c 2017-05-14 14:00:37.000000000 +0200
  4183. +++ linux-4.9.28/drivers/net/ethernet/3com/3c59x.c 2017-05-19 03:37:25.150175574 +0200
  4184. @@ -842,9 +842,9 @@
  4185. {
  4186. struct vortex_private *vp = netdev_priv(dev);
  4187. unsigned long flags;
  4188. - local_irq_save(flags);
  4189. + local_irq_save_nort(flags);
  4190. (vp->full_bus_master_rx ? boomerang_interrupt:vortex_interrupt)(dev->irq,dev);
  4191. - local_irq_restore(flags);
  4192. + local_irq_restore_nort(flags);
  4193. }
  4194. #endif
  4195. @@ -1910,12 +1910,12 @@
  4196. * Block interrupts because vortex_interrupt does a bare spin_lock()
  4197. */
  4198. unsigned long flags;
  4199. - local_irq_save(flags);
  4200. + local_irq_save_nort(flags);
  4201. if (vp->full_bus_master_tx)
  4202. boomerang_interrupt(dev->irq, dev);
  4203. else
  4204. vortex_interrupt(dev->irq, dev);
  4205. - local_irq_restore(flags);
  4206. + local_irq_restore_nort(flags);
  4207. }
  4208. }
  4209. diff -Nur linux-4.9.28.orig/drivers/net/ethernet/realtek/8139too.c linux-4.9.28/drivers/net/ethernet/realtek/8139too.c
  4210. --- linux-4.9.28.orig/drivers/net/ethernet/realtek/8139too.c 2017-05-14 14:00:37.000000000 +0200
  4211. +++ linux-4.9.28/drivers/net/ethernet/realtek/8139too.c 2017-05-19 03:37:25.150175574 +0200
  4212. @@ -2233,7 +2233,7 @@
  4213. struct rtl8139_private *tp = netdev_priv(dev);
  4214. const int irq = tp->pci_dev->irq;
  4215. - disable_irq(irq);
  4216. + disable_irq_nosync(irq);
  4217. rtl8139_interrupt(irq, dev);
  4218. enable_irq(irq);
  4219. }
  4220. diff -Nur linux-4.9.28.orig/drivers/net/wireless/intersil/orinoco/orinoco_usb.c linux-4.9.28/drivers/net/wireless/intersil/orinoco/orinoco_usb.c
  4221. --- linux-4.9.28.orig/drivers/net/wireless/intersil/orinoco/orinoco_usb.c 2017-05-14 14:00:37.000000000 +0200
  4222. +++ linux-4.9.28/drivers/net/wireless/intersil/orinoco/orinoco_usb.c 2017-05-19 03:37:25.150175574 +0200
  4223. @@ -697,7 +697,7 @@
  4224. while (!ctx->done.done && msecs--)
  4225. udelay(1000);
  4226. } else {
  4227. - wait_event_interruptible(ctx->done.wait,
  4228. + swait_event_interruptible(ctx->done.wait,
  4229. ctx->done.done);
  4230. }
  4231. break;
  4232. diff -Nur linux-4.9.28.orig/drivers/pci/access.c linux-4.9.28/drivers/pci/access.c
  4233. --- linux-4.9.28.orig/drivers/pci/access.c 2017-05-14 14:00:37.000000000 +0200
  4234. +++ linux-4.9.28/drivers/pci/access.c 2017-05-19 03:37:25.150175574 +0200
  4235. @@ -672,7 +672,7 @@
  4236. WARN_ON(!dev->block_cfg_access);
  4237. dev->block_cfg_access = 0;
  4238. - wake_up_all(&pci_cfg_wait);
  4239. + wake_up_all_locked(&pci_cfg_wait);
  4240. raw_spin_unlock_irqrestore(&pci_lock, flags);
  4241. }
  4242. EXPORT_SYMBOL_GPL(pci_cfg_access_unlock);
  4243. diff -Nur linux-4.9.28.orig/drivers/pinctrl/qcom/pinctrl-msm.c linux-4.9.28/drivers/pinctrl/qcom/pinctrl-msm.c
  4244. --- linux-4.9.28.orig/drivers/pinctrl/qcom/pinctrl-msm.c 2017-05-14 14:00:37.000000000 +0200
  4245. +++ linux-4.9.28/drivers/pinctrl/qcom/pinctrl-msm.c 2017-05-19 03:37:25.150175574 +0200
  4246. @@ -61,7 +61,7 @@
  4247. struct notifier_block restart_nb;
  4248. int irq;
  4249. - spinlock_t lock;
  4250. + raw_spinlock_t lock;
  4251. DECLARE_BITMAP(dual_edge_irqs, MAX_NR_GPIO);
  4252. DECLARE_BITMAP(enabled_irqs, MAX_NR_GPIO);
  4253. @@ -153,14 +153,14 @@
  4254. if (WARN_ON(i == g->nfuncs))
  4255. return -EINVAL;
  4256. - spin_lock_irqsave(&pctrl->lock, flags);
  4257. + raw_spin_lock_irqsave(&pctrl->lock, flags);
  4258. val = readl(pctrl->regs + g->ctl_reg);
  4259. val &= ~mask;
  4260. val |= i << g->mux_bit;
  4261. writel(val, pctrl->regs + g->ctl_reg);
  4262. - spin_unlock_irqrestore(&pctrl->lock, flags);
  4263. + raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  4264. return 0;
  4265. }
  4266. @@ -323,14 +323,14 @@
  4267. break;
  4268. case PIN_CONFIG_OUTPUT:
  4269. /* set output value */
  4270. - spin_lock_irqsave(&pctrl->lock, flags);
  4271. + raw_spin_lock_irqsave(&pctrl->lock, flags);
  4272. val = readl(pctrl->regs + g->io_reg);
  4273. if (arg)
  4274. val |= BIT(g->out_bit);
  4275. else
  4276. val &= ~BIT(g->out_bit);
  4277. writel(val, pctrl->regs + g->io_reg);
  4278. - spin_unlock_irqrestore(&pctrl->lock, flags);
  4279. + raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  4280. /* enable output */
  4281. arg = 1;
  4282. @@ -351,12 +351,12 @@
  4283. return -EINVAL;
  4284. }
  4285. - spin_lock_irqsave(&pctrl->lock, flags);
  4286. + raw_spin_lock_irqsave(&pctrl->lock, flags);
  4287. val = readl(pctrl->regs + g->ctl_reg);
  4288. val &= ~(mask << bit);
  4289. val |= arg << bit;
  4290. writel(val, pctrl->regs + g->ctl_reg);
  4291. - spin_unlock_irqrestore(&pctrl->lock, flags);
  4292. + raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  4293. }
  4294. return 0;
  4295. @@ -384,13 +384,13 @@
  4296. g = &pctrl->soc->groups[offset];
  4297. - spin_lock_irqsave(&pctrl->lock, flags);
  4298. + raw_spin_lock_irqsave(&pctrl->lock, flags);
  4299. val = readl(pctrl->regs + g->ctl_reg);
  4300. val &= ~BIT(g->oe_bit);
  4301. writel(val, pctrl->regs + g->ctl_reg);
  4302. - spin_unlock_irqrestore(&pctrl->lock, flags);
  4303. + raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  4304. return 0;
  4305. }
  4306. @@ -404,7 +404,7 @@
  4307. g = &pctrl->soc->groups[offset];
  4308. - spin_lock_irqsave(&pctrl->lock, flags);
  4309. + raw_spin_lock_irqsave(&pctrl->lock, flags);
  4310. val = readl(pctrl->regs + g->io_reg);
  4311. if (value)
  4312. @@ -417,7 +417,7 @@
  4313. val |= BIT(g->oe_bit);
  4314. writel(val, pctrl->regs + g->ctl_reg);
  4315. - spin_unlock_irqrestore(&pctrl->lock, flags);
  4316. + raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  4317. return 0;
  4318. }
  4319. @@ -443,7 +443,7 @@
  4320. g = &pctrl->soc->groups[offset];
  4321. - spin_lock_irqsave(&pctrl->lock, flags);
  4322. + raw_spin_lock_irqsave(&pctrl->lock, flags);
  4323. val = readl(pctrl->regs + g->io_reg);
  4324. if (value)
  4325. @@ -452,7 +452,7 @@
  4326. val &= ~BIT(g->out_bit);
  4327. writel(val, pctrl->regs + g->io_reg);
  4328. - spin_unlock_irqrestore(&pctrl->lock, flags);
  4329. + raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  4330. }
  4331. #ifdef CONFIG_DEBUG_FS
  4332. @@ -571,7 +571,7 @@
  4333. g = &pctrl->soc->groups[d->hwirq];
  4334. - spin_lock_irqsave(&pctrl->lock, flags);
  4335. + raw_spin_lock_irqsave(&pctrl->lock, flags);
  4336. val = readl(pctrl->regs + g->intr_cfg_reg);
  4337. val &= ~BIT(g->intr_enable_bit);
  4338. @@ -579,7 +579,7 @@
  4339. clear_bit(d->hwirq, pctrl->enabled_irqs);
  4340. - spin_unlock_irqrestore(&pctrl->lock, flags);
  4341. + raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  4342. }
  4343. static void msm_gpio_irq_unmask(struct irq_data *d)
  4344. @@ -592,7 +592,7 @@
  4345. g = &pctrl->soc->groups[d->hwirq];
  4346. - spin_lock_irqsave(&pctrl->lock, flags);
  4347. + raw_spin_lock_irqsave(&pctrl->lock, flags);
  4348. val = readl(pctrl->regs + g->intr_cfg_reg);
  4349. val |= BIT(g->intr_enable_bit);
  4350. @@ -600,7 +600,7 @@
  4351. set_bit(d->hwirq, pctrl->enabled_irqs);
  4352. - spin_unlock_irqrestore(&pctrl->lock, flags);
  4353. + raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  4354. }
  4355. static void msm_gpio_irq_ack(struct irq_data *d)
  4356. @@ -613,7 +613,7 @@
  4357. g = &pctrl->soc->groups[d->hwirq];
  4358. - spin_lock_irqsave(&pctrl->lock, flags);
  4359. + raw_spin_lock_irqsave(&pctrl->lock, flags);
  4360. val = readl(pctrl->regs + g->intr_status_reg);
  4361. if (g->intr_ack_high)
  4362. @@ -625,7 +625,7 @@
  4363. if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
  4364. msm_gpio_update_dual_edge_pos(pctrl, g, d);
  4365. - spin_unlock_irqrestore(&pctrl->lock, flags);
  4366. + raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  4367. }
  4368. static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  4369. @@ -638,7 +638,7 @@
  4370. g = &pctrl->soc->groups[d->hwirq];
  4371. - spin_lock_irqsave(&pctrl->lock, flags);
  4372. + raw_spin_lock_irqsave(&pctrl->lock, flags);
  4373. /*
  4374. * For hw without possibility of detecting both edges
  4375. @@ -712,7 +712,7 @@
  4376. if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
  4377. msm_gpio_update_dual_edge_pos(pctrl, g, d);
  4378. - spin_unlock_irqrestore(&pctrl->lock, flags);
  4379. + raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  4380. if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
  4381. irq_set_handler_locked(d, handle_level_irq);
  4382. @@ -728,11 +728,11 @@
  4383. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  4384. unsigned long flags;
  4385. - spin_lock_irqsave(&pctrl->lock, flags);
  4386. + raw_spin_lock_irqsave(&pctrl->lock, flags);
  4387. irq_set_irq_wake(pctrl->irq, on);
  4388. - spin_unlock_irqrestore(&pctrl->lock, flags);
  4389. + raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  4390. return 0;
  4391. }
  4392. @@ -878,7 +878,7 @@
  4393. pctrl->soc = soc_data;
  4394. pctrl->chip = msm_gpio_template;
  4395. - spin_lock_init(&pctrl->lock);
  4396. + raw_spin_lock_init(&pctrl->lock);
  4397. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  4398. pctrl->regs = devm_ioremap_resource(&pdev->dev, res);
  4399. diff -Nur linux-4.9.28.orig/drivers/scsi/fcoe/fcoe.c linux-4.9.28/drivers/scsi/fcoe/fcoe.c
  4400. --- linux-4.9.28.orig/drivers/scsi/fcoe/fcoe.c 2017-05-14 14:00:37.000000000 +0200
  4401. +++ linux-4.9.28/drivers/scsi/fcoe/fcoe.c 2017-05-19 03:37:25.150175574 +0200
  4402. @@ -1455,11 +1455,11 @@
  4403. static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
  4404. {
  4405. struct fcoe_percpu_s *fps;
  4406. - int rc;
  4407. + int rc, cpu = get_cpu_light();
  4408. - fps = &get_cpu_var(fcoe_percpu);
  4409. + fps = &per_cpu(fcoe_percpu, cpu);
  4410. rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
  4411. - put_cpu_var(fcoe_percpu);
  4412. + put_cpu_light();
  4413. return rc;
  4414. }
  4415. @@ -1646,11 +1646,11 @@
  4416. return 0;
  4417. }
  4418. - stats = per_cpu_ptr(lport->stats, get_cpu());
  4419. + stats = per_cpu_ptr(lport->stats, get_cpu_light());
  4420. stats->InvalidCRCCount++;
  4421. if (stats->InvalidCRCCount < 5)
  4422. printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
  4423. - put_cpu();
  4424. + put_cpu_light();
  4425. return -EINVAL;
  4426. }
  4427. @@ -1693,7 +1693,7 @@
  4428. */
  4429. hp = (struct fcoe_hdr *) skb_network_header(skb);
  4430. - stats = per_cpu_ptr(lport->stats, get_cpu());
  4431. + stats = per_cpu_ptr(lport->stats, get_cpu_light());
  4432. if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
  4433. if (stats->ErrorFrames < 5)
  4434. printk(KERN_WARNING "fcoe: FCoE version "
  4435. @@ -1725,13 +1725,13 @@
  4436. goto drop;
  4437. if (!fcoe_filter_frames(lport, fp)) {
  4438. - put_cpu();
  4439. + put_cpu_light();
  4440. fc_exch_recv(lport, fp);
  4441. return;
  4442. }
  4443. drop:
  4444. stats->ErrorFrames++;
  4445. - put_cpu();
  4446. + put_cpu_light();
  4447. kfree_skb(skb);
  4448. }
  4449. diff -Nur linux-4.9.28.orig/drivers/scsi/fcoe/fcoe_ctlr.c linux-4.9.28/drivers/scsi/fcoe/fcoe_ctlr.c
  4450. --- linux-4.9.28.orig/drivers/scsi/fcoe/fcoe_ctlr.c 2017-05-14 14:00:37.000000000 +0200
  4451. +++ linux-4.9.28/drivers/scsi/fcoe/fcoe_ctlr.c 2017-05-19 03:37:25.150175574 +0200
  4452. @@ -834,7 +834,7 @@
  4453. INIT_LIST_HEAD(&del_list);
  4454. - stats = per_cpu_ptr(fip->lp->stats, get_cpu());
  4455. + stats = per_cpu_ptr(fip->lp->stats, get_cpu_light());
  4456. list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
  4457. deadline = fcf->time + fcf->fka_period + fcf->fka_period / 2;
  4458. @@ -870,7 +870,7 @@
  4459. sel_time = fcf->time;
  4460. }
  4461. }
  4462. - put_cpu();
  4463. + put_cpu_light();
  4464. list_for_each_entry_safe(fcf, next, &del_list, list) {
  4465. /* Removes fcf from current list */
  4466. diff -Nur linux-4.9.28.orig/drivers/scsi/libfc/fc_exch.c linux-4.9.28/drivers/scsi/libfc/fc_exch.c
  4467. --- linux-4.9.28.orig/drivers/scsi/libfc/fc_exch.c 2017-05-14 14:00:37.000000000 +0200
  4468. +++ linux-4.9.28/drivers/scsi/libfc/fc_exch.c 2017-05-19 03:37:25.150175574 +0200
  4469. @@ -814,10 +814,10 @@
  4470. }
  4471. memset(ep, 0, sizeof(*ep));
  4472. - cpu = get_cpu();
  4473. + cpu = get_cpu_light();
  4474. pool = per_cpu_ptr(mp->pool, cpu);
  4475. spin_lock_bh(&pool->lock);
  4476. - put_cpu();
  4477. + put_cpu_light();
  4478. /* peek cache of free slot */
  4479. if (pool->left != FC_XID_UNKNOWN) {
  4480. diff -Nur linux-4.9.28.orig/drivers/scsi/libsas/sas_ata.c linux-4.9.28/drivers/scsi/libsas/sas_ata.c
  4481. --- linux-4.9.28.orig/drivers/scsi/libsas/sas_ata.c 2017-05-14 14:00:37.000000000 +0200
  4482. +++ linux-4.9.28/drivers/scsi/libsas/sas_ata.c 2017-05-19 03:37:25.150175574 +0200
  4483. @@ -190,7 +190,7 @@
  4484. /* TODO: audit callers to ensure they are ready for qc_issue to
  4485. * unconditionally re-enable interrupts
  4486. */
  4487. - local_irq_save(flags);
  4488. + local_irq_save_nort(flags);
  4489. spin_unlock(ap->lock);
  4490. /* If the device fell off, no sense in issuing commands */
  4491. @@ -252,7 +252,7 @@
  4492. out:
  4493. spin_lock(ap->lock);
  4494. - local_irq_restore(flags);
  4495. + local_irq_restore_nort(flags);
  4496. return ret;
  4497. }
  4498. diff -Nur linux-4.9.28.orig/drivers/scsi/qla2xxx/qla_inline.h linux-4.9.28/drivers/scsi/qla2xxx/qla_inline.h
  4499. --- linux-4.9.28.orig/drivers/scsi/qla2xxx/qla_inline.h 2017-05-14 14:00:37.000000000 +0200
  4500. +++ linux-4.9.28/drivers/scsi/qla2xxx/qla_inline.h 2017-05-19 03:37:25.150175574 +0200
  4501. @@ -59,12 +59,12 @@
  4502. {
  4503. unsigned long flags;
  4504. struct qla_hw_data *ha = rsp->hw;
  4505. - local_irq_save(flags);
  4506. + local_irq_save_nort(flags);
  4507. if (IS_P3P_TYPE(ha))
  4508. qla82xx_poll(0, rsp);
  4509. else
  4510. ha->isp_ops->intr_handler(0, rsp);
  4511. - local_irq_restore(flags);
  4512. + local_irq_restore_nort(flags);
  4513. }
  4514. static inline uint8_t *
  4515. diff -Nur linux-4.9.28.orig/drivers/scsi/qla2xxx/qla_isr.c linux-4.9.28/drivers/scsi/qla2xxx/qla_isr.c
  4516. --- linux-4.9.28.orig/drivers/scsi/qla2xxx/qla_isr.c 2017-05-14 14:00:37.000000000 +0200
  4517. +++ linux-4.9.28/drivers/scsi/qla2xxx/qla_isr.c 2017-05-19 03:37:25.154175725 +0200
  4518. @@ -3125,7 +3125,11 @@
  4519. * kref_put().
  4520. */
  4521. kref_get(&qentry->irq_notify.kref);
  4522. +#ifdef CONFIG_PREEMPT_RT_BASE
  4523. + swork_queue(&qentry->irq_notify.swork);
  4524. +#else
  4525. schedule_work(&qentry->irq_notify.work);
  4526. +#endif
  4527. }
  4528. /*
  4529. diff -Nur linux-4.9.28.orig/drivers/thermal/x86_pkg_temp_thermal.c linux-4.9.28/drivers/thermal/x86_pkg_temp_thermal.c
  4530. --- linux-4.9.28.orig/drivers/thermal/x86_pkg_temp_thermal.c 2017-05-14 14:00:37.000000000 +0200
  4531. +++ linux-4.9.28/drivers/thermal/x86_pkg_temp_thermal.c 2017-05-19 03:37:25.154175725 +0200
  4532. @@ -29,6 +29,7 @@
  4533. #include <linux/pm.h>
  4534. #include <linux/thermal.h>
  4535. #include <linux/debugfs.h>
  4536. +#include <linux/swork.h>
  4537. #include <asm/cpu_device_id.h>
  4538. #include <asm/mce.h>
  4539. @@ -353,7 +354,7 @@
  4540. }
  4541. }
  4542. -static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  4543. +static void platform_thermal_notify_work(struct swork_event *event)
  4544. {
  4545. unsigned long flags;
  4546. int cpu = smp_processor_id();
  4547. @@ -370,7 +371,7 @@
  4548. pkg_work_scheduled[phy_id]) {
  4549. disable_pkg_thres_interrupt();
  4550. spin_unlock_irqrestore(&pkg_work_lock, flags);
  4551. - return -EINVAL;
  4552. + return;
  4553. }
  4554. pkg_work_scheduled[phy_id] = 1;
  4555. spin_unlock_irqrestore(&pkg_work_lock, flags);
  4556. @@ -379,9 +380,48 @@
  4557. schedule_delayed_work_on(cpu,
  4558. &per_cpu(pkg_temp_thermal_threshold_work, cpu),
  4559. msecs_to_jiffies(notify_delay_ms));
  4560. +}
  4561. +
  4562. +#ifdef CONFIG_PREEMPT_RT_FULL
  4563. +static struct swork_event notify_work;
  4564. +
  4565. +static int thermal_notify_work_init(void)
  4566. +{
  4567. + int err;
  4568. +
  4569. + err = swork_get();
  4570. + if (err)
  4571. + return err;
  4572. +
  4573. + INIT_SWORK(&notify_work, platform_thermal_notify_work);
  4574. return 0;
  4575. }
  4576. +static void thermal_notify_work_cleanup(void)
  4577. +{
  4578. + swork_put();
  4579. +}
  4580. +
  4581. +static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  4582. +{
  4583. + swork_queue(&notify_work);
  4584. + return 0;
  4585. +}
  4586. +
  4587. +#else /* !CONFIG_PREEMPT_RT_FULL */
  4588. +
  4589. +static int thermal_notify_work_init(void) { return 0; }
  4590. +
  4591. +static void thermal_notify_work_cleanup(void) { }
  4592. +
  4593. +static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  4594. +{
  4595. + platform_thermal_notify_work(NULL);
  4596. +
  4597. + return 0;
  4598. +}
  4599. +#endif /* CONFIG_PREEMPT_RT_FULL */
  4600. +
  4601. static int find_siblings_cpu(int cpu)
  4602. {
  4603. int i;
  4604. @@ -585,6 +625,9 @@
  4605. if (!x86_match_cpu(pkg_temp_thermal_ids))
  4606. return -ENODEV;
  4607. + if (!thermal_notify_work_init())
  4608. + return -ENODEV;
  4609. +
  4610. spin_lock_init(&pkg_work_lock);
  4611. platform_thermal_package_notify =
  4612. pkg_temp_thermal_platform_thermal_notify;
  4613. @@ -609,7 +652,7 @@
  4614. kfree(pkg_work_scheduled);
  4615. platform_thermal_package_notify = NULL;
  4616. platform_thermal_package_rate_control = NULL;
  4617. -
  4618. + thermal_notify_work_cleanup();
  4619. return -ENODEV;
  4620. }
  4621. @@ -634,6 +677,7 @@
  4622. mutex_unlock(&phy_dev_list_mutex);
  4623. platform_thermal_package_notify = NULL;
  4624. platform_thermal_package_rate_control = NULL;
  4625. + thermal_notify_work_cleanup();
  4626. for_each_online_cpu(i)
  4627. cancel_delayed_work_sync(
  4628. &per_cpu(pkg_temp_thermal_threshold_work, i));
  4629. diff -Nur linux-4.9.28.orig/drivers/tty/serial/8250/8250_core.c linux-4.9.28/drivers/tty/serial/8250/8250_core.c
  4630. --- linux-4.9.28.orig/drivers/tty/serial/8250/8250_core.c 2017-05-14 14:00:37.000000000 +0200
  4631. +++ linux-4.9.28/drivers/tty/serial/8250/8250_core.c 2017-05-19 03:37:25.154175725 +0200
  4632. @@ -58,7 +58,16 @@
  4633. static unsigned int skip_txen_test; /* force skip of txen test at init time */
  4634. -#define PASS_LIMIT 512
  4635. +/*
  4636. + * On -rt we can have a more delays, and legitimately
  4637. + * so - so don't drop work spuriously and spam the
  4638. + * syslog:
  4639. + */
  4640. +#ifdef CONFIG_PREEMPT_RT_FULL
  4641. +# define PASS_LIMIT 1000000
  4642. +#else
  4643. +# define PASS_LIMIT 512
  4644. +#endif
  4645. #include <asm/serial.h>
  4646. /*
  4647. diff -Nur linux-4.9.28.orig/drivers/tty/serial/8250/8250_port.c linux-4.9.28/drivers/tty/serial/8250/8250_port.c
  4648. --- linux-4.9.28.orig/drivers/tty/serial/8250/8250_port.c 2017-05-14 14:00:37.000000000 +0200
  4649. +++ linux-4.9.28/drivers/tty/serial/8250/8250_port.c 2017-05-19 03:37:25.154175725 +0200
  4650. @@ -35,6 +35,7 @@
  4651. #include <linux/nmi.h>
  4652. #include <linux/mutex.h>
  4653. #include <linux/slab.h>
  4654. +#include <linux/kdb.h>
  4655. #include <linux/uaccess.h>
  4656. #include <linux/pm_runtime.h>
  4657. #include <linux/timer.h>
  4658. @@ -3144,9 +3145,9 @@
  4659. serial8250_rpm_get(up);
  4660. - if (port->sysrq)
  4661. + if (port->sysrq || oops_in_progress)
  4662. locked = 0;
  4663. - else if (oops_in_progress)
  4664. + else if (in_kdb_printk())
  4665. locked = spin_trylock_irqsave(&port->lock, flags);
  4666. else
  4667. spin_lock_irqsave(&port->lock, flags);
  4668. diff -Nur linux-4.9.28.orig/drivers/tty/serial/amba-pl011.c linux-4.9.28/drivers/tty/serial/amba-pl011.c
  4669. --- linux-4.9.28.orig/drivers/tty/serial/amba-pl011.c 2017-05-14 14:00:37.000000000 +0200
  4670. +++ linux-4.9.28/drivers/tty/serial/amba-pl011.c 2017-05-19 03:37:25.154175725 +0200
  4671. @@ -2194,13 +2194,19 @@
  4672. clk_enable(uap->clk);
  4673. - local_irq_save(flags);
  4674. + /*
  4675. + * local_irq_save(flags);
  4676. + *
  4677. + * This local_irq_save() is nonsense. If we come in via sysrq
  4678. + * handling then interrupts are already disabled. Aside of
  4679. + * that the port.sysrq check is racy on SMP regardless.
  4680. + */
  4681. if (uap->port.sysrq)
  4682. locked = 0;
  4683. else if (oops_in_progress)
  4684. - locked = spin_trylock(&uap->port.lock);
  4685. + locked = spin_trylock_irqsave(&uap->port.lock, flags);
  4686. else
  4687. - spin_lock(&uap->port.lock);
  4688. + spin_lock_irqsave(&uap->port.lock, flags);
  4689. /*
  4690. * First save the CR then disable the interrupts
  4691. @@ -2224,8 +2230,7 @@
  4692. pl011_write(old_cr, uap, REG_CR);
  4693. if (locked)
  4694. - spin_unlock(&uap->port.lock);
  4695. - local_irq_restore(flags);
  4696. + spin_unlock_irqrestore(&uap->port.lock, flags);
  4697. clk_disable(uap->clk);
  4698. }
  4699. diff -Nur linux-4.9.28.orig/drivers/tty/serial/omap-serial.c linux-4.9.28/drivers/tty/serial/omap-serial.c
  4700. --- linux-4.9.28.orig/drivers/tty/serial/omap-serial.c 2017-05-14 14:00:37.000000000 +0200
  4701. +++ linux-4.9.28/drivers/tty/serial/omap-serial.c 2017-05-19 03:37:25.154175725 +0200
  4702. @@ -1257,13 +1257,10 @@
  4703. pm_runtime_get_sync(up->dev);
  4704. - local_irq_save(flags);
  4705. - if (up->port.sysrq)
  4706. - locked = 0;
  4707. - else if (oops_in_progress)
  4708. - locked = spin_trylock(&up->port.lock);
  4709. + if (up->port.sysrq || oops_in_progress)
  4710. + locked = spin_trylock_irqsave(&up->port.lock, flags);
  4711. else
  4712. - spin_lock(&up->port.lock);
  4713. + spin_lock_irqsave(&up->port.lock, flags);
  4714. /*
  4715. * First save the IER then disable the interrupts
  4716. @@ -1292,8 +1289,7 @@
  4717. pm_runtime_mark_last_busy(up->dev);
  4718. pm_runtime_put_autosuspend(up->dev);
  4719. if (locked)
  4720. - spin_unlock(&up->port.lock);
  4721. - local_irq_restore(flags);
  4722. + spin_unlock_irqrestore(&up->port.lock, flags);
  4723. }
  4724. static int __init
  4725. diff -Nur linux-4.9.28.orig/drivers/usb/core/hcd.c linux-4.9.28/drivers/usb/core/hcd.c
  4726. --- linux-4.9.28.orig/drivers/usb/core/hcd.c 2017-05-14 14:00:37.000000000 +0200
  4727. +++ linux-4.9.28/drivers/usb/core/hcd.c 2017-05-19 03:37:25.154175725 +0200
  4728. @@ -1764,9 +1764,9 @@
  4729. * and no one may trigger the above deadlock situation when
  4730. * running complete() in tasklet.
  4731. */
  4732. - local_irq_save(flags);
  4733. + local_irq_save_nort(flags);
  4734. urb->complete(urb);
  4735. - local_irq_restore(flags);
  4736. + local_irq_restore_nort(flags);
  4737. usb_anchor_resume_wakeups(anchor);
  4738. atomic_dec(&urb->use_count);
  4739. diff -Nur linux-4.9.28.orig/drivers/usb/gadget/function/f_fs.c linux-4.9.28/drivers/usb/gadget/function/f_fs.c
  4740. --- linux-4.9.28.orig/drivers/usb/gadget/function/f_fs.c 2017-05-14 14:00:37.000000000 +0200
  4741. +++ linux-4.9.28/drivers/usb/gadget/function/f_fs.c 2017-05-19 03:37:25.154175725 +0200
  4742. @@ -1593,7 +1593,7 @@
  4743. pr_info("%s(): freeing\n", __func__);
  4744. ffs_data_clear(ffs);
  4745. BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
  4746. - waitqueue_active(&ffs->ep0req_completion.wait));
  4747. + swait_active(&ffs->ep0req_completion.wait));
  4748. kfree(ffs->dev_name);
  4749. kfree(ffs);
  4750. }
  4751. diff -Nur linux-4.9.28.orig/drivers/usb/gadget/legacy/inode.c linux-4.9.28/drivers/usb/gadget/legacy/inode.c
  4752. --- linux-4.9.28.orig/drivers/usb/gadget/legacy/inode.c 2017-05-14 14:00:37.000000000 +0200
  4753. +++ linux-4.9.28/drivers/usb/gadget/legacy/inode.c 2017-05-19 03:37:25.154175725 +0200
  4754. @@ -346,7 +346,7 @@
  4755. spin_unlock_irq (&epdata->dev->lock);
  4756. if (likely (value == 0)) {
  4757. - value = wait_event_interruptible (done.wait, done.done);
  4758. + value = swait_event_interruptible (done.wait, done.done);
  4759. if (value != 0) {
  4760. spin_lock_irq (&epdata->dev->lock);
  4761. if (likely (epdata->ep != NULL)) {
  4762. @@ -355,7 +355,7 @@
  4763. usb_ep_dequeue (epdata->ep, epdata->req);
  4764. spin_unlock_irq (&epdata->dev->lock);
  4765. - wait_event (done.wait, done.done);
  4766. + swait_event (done.wait, done.done);
  4767. if (epdata->status == -ECONNRESET)
  4768. epdata->status = -EINTR;
  4769. } else {
  4770. diff -Nur linux-4.9.28.orig/fs/aio.c linux-4.9.28/fs/aio.c
  4771. --- linux-4.9.28.orig/fs/aio.c 2017-05-14 14:00:37.000000000 +0200
  4772. +++ linux-4.9.28/fs/aio.c 2017-05-19 03:37:25.154175725 +0200
  4773. @@ -40,6 +40,7 @@
  4774. #include <linux/ramfs.h>
  4775. #include <linux/percpu-refcount.h>
  4776. #include <linux/mount.h>
  4777. +#include <linux/swork.h>
  4778. #include <asm/kmap_types.h>
  4779. #include <asm/uaccess.h>
  4780. @@ -115,7 +116,7 @@
  4781. struct page **ring_pages;
  4782. long nr_pages;
  4783. - struct work_struct free_work;
  4784. + struct swork_event free_work;
  4785. /*
  4786. * signals when all in-flight requests are done
  4787. @@ -258,6 +259,7 @@
  4788. .mount = aio_mount,
  4789. .kill_sb = kill_anon_super,
  4790. };
  4791. + BUG_ON(swork_get());
  4792. aio_mnt = kern_mount(&aio_fs);
  4793. if (IS_ERR(aio_mnt))
  4794. panic("Failed to create aio fs mount.");
  4795. @@ -581,9 +583,9 @@
  4796. return cancel(&kiocb->common);
  4797. }
  4798. -static void free_ioctx(struct work_struct *work)
  4799. +static void free_ioctx(struct swork_event *sev)
  4800. {
  4801. - struct kioctx *ctx = container_of(work, struct kioctx, free_work);
  4802. + struct kioctx *ctx = container_of(sev, struct kioctx, free_work);
  4803. pr_debug("freeing %p\n", ctx);
  4804. @@ -602,8 +604,8 @@
  4805. if (ctx->rq_wait && atomic_dec_and_test(&ctx->rq_wait->count))
  4806. complete(&ctx->rq_wait->comp);
  4807. - INIT_WORK(&ctx->free_work, free_ioctx);
  4808. - schedule_work(&ctx->free_work);
  4809. + INIT_SWORK(&ctx->free_work, free_ioctx);
  4810. + swork_queue(&ctx->free_work);
  4811. }
  4812. /*
  4813. @@ -611,9 +613,9 @@
  4814. * and ctx->users has dropped to 0, so we know no more kiocbs can be submitted -
  4815. * now it's safe to cancel any that need to be.
  4816. */
  4817. -static void free_ioctx_users(struct percpu_ref *ref)
  4818. +static void free_ioctx_users_work(struct swork_event *sev)
  4819. {
  4820. - struct kioctx *ctx = container_of(ref, struct kioctx, users);
  4821. + struct kioctx *ctx = container_of(sev, struct kioctx, free_work);
  4822. struct aio_kiocb *req;
  4823. spin_lock_irq(&ctx->ctx_lock);
  4824. @@ -632,6 +634,14 @@
  4825. percpu_ref_put(&ctx->reqs);
  4826. }
  4827. +static void free_ioctx_users(struct percpu_ref *ref)
  4828. +{
  4829. + struct kioctx *ctx = container_of(ref, struct kioctx, users);
  4830. +
  4831. + INIT_SWORK(&ctx->free_work, free_ioctx_users_work);
  4832. + swork_queue(&ctx->free_work);
  4833. +}
  4834. +
  4835. static int ioctx_add_table(struct kioctx *ctx, struct mm_struct *mm)
  4836. {
  4837. unsigned i, new_nr;
  4838. diff -Nur linux-4.9.28.orig/fs/autofs4/autofs_i.h linux-4.9.28/fs/autofs4/autofs_i.h
  4839. --- linux-4.9.28.orig/fs/autofs4/autofs_i.h 2017-05-14 14:00:37.000000000 +0200
  4840. +++ linux-4.9.28/fs/autofs4/autofs_i.h 2017-05-19 03:37:25.154175725 +0200
  4841. @@ -31,6 +31,7 @@
  4842. #include <linux/sched.h>
  4843. #include <linux/mount.h>
  4844. #include <linux/namei.h>
  4845. +#include <linux/delay.h>
  4846. #include <asm/current.h>
  4847. #include <linux/uaccess.h>
  4848. diff -Nur linux-4.9.28.orig/fs/autofs4/expire.c linux-4.9.28/fs/autofs4/expire.c
  4849. --- linux-4.9.28.orig/fs/autofs4/expire.c 2017-05-14 14:00:37.000000000 +0200
  4850. +++ linux-4.9.28/fs/autofs4/expire.c 2017-05-19 03:37:25.154175725 +0200
  4851. @@ -148,7 +148,7 @@
  4852. parent = p->d_parent;
  4853. if (!spin_trylock(&parent->d_lock)) {
  4854. spin_unlock(&p->d_lock);
  4855. - cpu_relax();
  4856. + cpu_chill();
  4857. goto relock;
  4858. }
  4859. spin_unlock(&p->d_lock);
  4860. diff -Nur linux-4.9.28.orig/fs/buffer.c linux-4.9.28/fs/buffer.c
  4861. --- linux-4.9.28.orig/fs/buffer.c 2017-05-14 14:00:37.000000000 +0200
  4862. +++ linux-4.9.28/fs/buffer.c 2017-05-19 03:37:25.158175874 +0200
  4863. @@ -301,8 +301,7 @@
  4864. * decide that the page is now completely done.
  4865. */
  4866. first = page_buffers(page);
  4867. - local_irq_save(flags);
  4868. - bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  4869. + flags = bh_uptodate_lock_irqsave(first);
  4870. clear_buffer_async_read(bh);
  4871. unlock_buffer(bh);
  4872. tmp = bh;
  4873. @@ -315,8 +314,7 @@
  4874. }
  4875. tmp = tmp->b_this_page;
  4876. } while (tmp != bh);
  4877. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  4878. - local_irq_restore(flags);
  4879. + bh_uptodate_unlock_irqrestore(first, flags);
  4880. /*
  4881. * If none of the buffers had errors and they are all
  4882. @@ -328,9 +326,7 @@
  4883. return;
  4884. still_busy:
  4885. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  4886. - local_irq_restore(flags);
  4887. - return;
  4888. + bh_uptodate_unlock_irqrestore(first, flags);
  4889. }
  4890. /*
  4891. @@ -358,8 +354,7 @@
  4892. }
  4893. first = page_buffers(page);
  4894. - local_irq_save(flags);
  4895. - bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  4896. + flags = bh_uptodate_lock_irqsave(first);
  4897. clear_buffer_async_write(bh);
  4898. unlock_buffer(bh);
  4899. @@ -371,15 +366,12 @@
  4900. }
  4901. tmp = tmp->b_this_page;
  4902. }
  4903. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  4904. - local_irq_restore(flags);
  4905. + bh_uptodate_unlock_irqrestore(first, flags);
  4906. end_page_writeback(page);
  4907. return;
  4908. still_busy:
  4909. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  4910. - local_irq_restore(flags);
  4911. - return;
  4912. + bh_uptodate_unlock_irqrestore(first, flags);
  4913. }
  4914. EXPORT_SYMBOL(end_buffer_async_write);
  4915. @@ -3383,6 +3375,7 @@
  4916. struct buffer_head *ret = kmem_cache_zalloc(bh_cachep, gfp_flags);
  4917. if (ret) {
  4918. INIT_LIST_HEAD(&ret->b_assoc_buffers);
  4919. + buffer_head_init_locks(ret);
  4920. preempt_disable();
  4921. __this_cpu_inc(bh_accounting.nr);
  4922. recalc_bh_state();
  4923. diff -Nur linux-4.9.28.orig/fs/cifs/readdir.c linux-4.9.28/fs/cifs/readdir.c
  4924. --- linux-4.9.28.orig/fs/cifs/readdir.c 2017-05-14 14:00:37.000000000 +0200
  4925. +++ linux-4.9.28/fs/cifs/readdir.c 2017-05-19 03:37:25.158175874 +0200
  4926. @@ -80,7 +80,7 @@
  4927. struct inode *inode;
  4928. struct super_block *sb = parent->d_sb;
  4929. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  4930. - DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  4931. + DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(wq);
  4932. cifs_dbg(FYI, "%s: for %s\n", __func__, name->name);
  4933. diff -Nur linux-4.9.28.orig/fs/dcache.c linux-4.9.28/fs/dcache.c
  4934. --- linux-4.9.28.orig/fs/dcache.c 2017-05-14 14:00:37.000000000 +0200
  4935. +++ linux-4.9.28/fs/dcache.c 2017-05-19 03:37:25.158175874 +0200
  4936. @@ -19,6 +19,7 @@
  4937. #include <linux/mm.h>
  4938. #include <linux/fs.h>
  4939. #include <linux/fsnotify.h>
  4940. +#include <linux/delay.h>
  4941. #include <linux/slab.h>
  4942. #include <linux/init.h>
  4943. #include <linux/hash.h>
  4944. @@ -750,6 +751,8 @@
  4945. */
  4946. void dput(struct dentry *dentry)
  4947. {
  4948. + struct dentry *parent;
  4949. +
  4950. if (unlikely(!dentry))
  4951. return;
  4952. @@ -788,9 +791,18 @@
  4953. return;
  4954. kill_it:
  4955. - dentry = dentry_kill(dentry);
  4956. - if (dentry) {
  4957. - cond_resched();
  4958. + parent = dentry_kill(dentry);
  4959. + if (parent) {
  4960. + int r;
  4961. +
  4962. + if (parent == dentry) {
  4963. + /* the task with the highest priority won't schedule */
  4964. + r = cond_resched();
  4965. + if (!r)
  4966. + cpu_chill();
  4967. + } else {
  4968. + dentry = parent;
  4969. + }
  4970. goto repeat;
  4971. }
  4972. }
  4973. @@ -2324,7 +2336,7 @@
  4974. if (dentry->d_lockref.count == 1) {
  4975. if (!spin_trylock(&inode->i_lock)) {
  4976. spin_unlock(&dentry->d_lock);
  4977. - cpu_relax();
  4978. + cpu_chill();
  4979. goto again;
  4980. }
  4981. dentry->d_flags &= ~DCACHE_CANT_MOUNT;
  4982. @@ -2384,21 +2396,24 @@
  4983. static void d_wait_lookup(struct dentry *dentry)
  4984. {
  4985. - if (d_in_lookup(dentry)) {
  4986. - DECLARE_WAITQUEUE(wait, current);
  4987. - add_wait_queue(dentry->d_wait, &wait);
  4988. - do {
  4989. - set_current_state(TASK_UNINTERRUPTIBLE);
  4990. - spin_unlock(&dentry->d_lock);
  4991. - schedule();
  4992. - spin_lock(&dentry->d_lock);
  4993. - } while (d_in_lookup(dentry));
  4994. - }
  4995. + struct swait_queue __wait;
  4996. +
  4997. + if (!d_in_lookup(dentry))
  4998. + return;
  4999. +
  5000. + INIT_LIST_HEAD(&__wait.task_list);
  5001. + do {
  5002. + prepare_to_swait(dentry->d_wait, &__wait, TASK_UNINTERRUPTIBLE);
  5003. + spin_unlock(&dentry->d_lock);
  5004. + schedule();
  5005. + spin_lock(&dentry->d_lock);
  5006. + } while (d_in_lookup(dentry));
  5007. + finish_swait(dentry->d_wait, &__wait);
  5008. }
  5009. struct dentry *d_alloc_parallel(struct dentry *parent,
  5010. const struct qstr *name,
  5011. - wait_queue_head_t *wq)
  5012. + struct swait_queue_head *wq)
  5013. {
  5014. unsigned int hash = name->hash;
  5015. struct hlist_bl_head *b = in_lookup_hash(parent, hash);
  5016. @@ -2507,7 +2522,7 @@
  5017. hlist_bl_lock(b);
  5018. dentry->d_flags &= ~DCACHE_PAR_LOOKUP;
  5019. __hlist_bl_del(&dentry->d_u.d_in_lookup_hash);
  5020. - wake_up_all(dentry->d_wait);
  5021. + swake_up_all(dentry->d_wait);
  5022. dentry->d_wait = NULL;
  5023. hlist_bl_unlock(b);
  5024. INIT_HLIST_NODE(&dentry->d_u.d_alias);
  5025. @@ -3604,6 +3619,11 @@
  5026. void __init vfs_caches_init_early(void)
  5027. {
  5028. + int i;
  5029. +
  5030. + for (i = 0; i < ARRAY_SIZE(in_lookup_hashtable); i++)
  5031. + INIT_HLIST_BL_HEAD(&in_lookup_hashtable[i]);
  5032. +
  5033. dcache_init_early();
  5034. inode_init_early();
  5035. }
  5036. diff -Nur linux-4.9.28.orig/fs/eventpoll.c linux-4.9.28/fs/eventpoll.c
  5037. --- linux-4.9.28.orig/fs/eventpoll.c 2017-05-14 14:00:37.000000000 +0200
  5038. +++ linux-4.9.28/fs/eventpoll.c 2017-05-19 03:37:25.158175874 +0200
  5039. @@ -510,12 +510,12 @@
  5040. */
  5041. static void ep_poll_safewake(wait_queue_head_t *wq)
  5042. {
  5043. - int this_cpu = get_cpu();
  5044. + int this_cpu = get_cpu_light();
  5045. ep_call_nested(&poll_safewake_ncalls, EP_MAX_NESTS,
  5046. ep_poll_wakeup_proc, NULL, wq, (void *) (long) this_cpu);
  5047. - put_cpu();
  5048. + put_cpu_light();
  5049. }
  5050. static void ep_remove_wait_queue(struct eppoll_entry *pwq)
  5051. diff -Nur linux-4.9.28.orig/fs/exec.c linux-4.9.28/fs/exec.c
  5052. --- linux-4.9.28.orig/fs/exec.c 2017-05-14 14:00:37.000000000 +0200
  5053. +++ linux-4.9.28/fs/exec.c 2017-05-19 03:37:25.158175874 +0200
  5054. @@ -1017,12 +1017,14 @@
  5055. }
  5056. }
  5057. task_lock(tsk);
  5058. + preempt_disable_rt();
  5059. active_mm = tsk->active_mm;
  5060. tsk->mm = mm;
  5061. tsk->active_mm = mm;
  5062. activate_mm(active_mm, mm);
  5063. tsk->mm->vmacache_seqnum = 0;
  5064. vmacache_flush(tsk);
  5065. + preempt_enable_rt();
  5066. task_unlock(tsk);
  5067. if (old_mm) {
  5068. up_read(&old_mm->mmap_sem);
  5069. diff -Nur linux-4.9.28.orig/fs/fuse/dir.c linux-4.9.28/fs/fuse/dir.c
  5070. --- linux-4.9.28.orig/fs/fuse/dir.c 2017-05-14 14:00:37.000000000 +0200
  5071. +++ linux-4.9.28/fs/fuse/dir.c 2017-05-19 03:37:25.158175874 +0200
  5072. @@ -1191,7 +1191,7 @@
  5073. struct inode *dir = d_inode(parent);
  5074. struct fuse_conn *fc;
  5075. struct inode *inode;
  5076. - DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  5077. + DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(wq);
  5078. if (!o->nodeid) {
  5079. /*
  5080. diff -Nur linux-4.9.28.orig/fs/jbd2/checkpoint.c linux-4.9.28/fs/jbd2/checkpoint.c
  5081. --- linux-4.9.28.orig/fs/jbd2/checkpoint.c 2017-05-14 14:00:37.000000000 +0200
  5082. +++ linux-4.9.28/fs/jbd2/checkpoint.c 2017-05-19 03:37:25.158175874 +0200
  5083. @@ -116,6 +116,8 @@
  5084. nblocks = jbd2_space_needed(journal);
  5085. while (jbd2_log_space_left(journal) < nblocks) {
  5086. write_unlock(&journal->j_state_lock);
  5087. + if (current->plug)
  5088. + io_schedule();
  5089. mutex_lock(&journal->j_checkpoint_mutex);
  5090. /*
  5091. diff -Nur linux-4.9.28.orig/fs/locks.c linux-4.9.28/fs/locks.c
  5092. --- linux-4.9.28.orig/fs/locks.c 2017-05-14 14:00:37.000000000 +0200
  5093. +++ linux-4.9.28/fs/locks.c 2017-05-19 03:37:25.158175874 +0200
  5094. @@ -935,7 +935,7 @@
  5095. return -ENOMEM;
  5096. }
  5097. - percpu_down_read_preempt_disable(&file_rwsem);
  5098. + percpu_down_read(&file_rwsem);
  5099. spin_lock(&ctx->flc_lock);
  5100. if (request->fl_flags & FL_ACCESS)
  5101. goto find_conflict;
  5102. @@ -976,7 +976,7 @@
  5103. out:
  5104. spin_unlock(&ctx->flc_lock);
  5105. - percpu_up_read_preempt_enable(&file_rwsem);
  5106. + percpu_up_read(&file_rwsem);
  5107. if (new_fl)
  5108. locks_free_lock(new_fl);
  5109. locks_dispose_list(&dispose);
  5110. @@ -1013,7 +1013,7 @@
  5111. new_fl2 = locks_alloc_lock();
  5112. }
  5113. - percpu_down_read_preempt_disable(&file_rwsem);
  5114. + percpu_down_read(&file_rwsem);
  5115. spin_lock(&ctx->flc_lock);
  5116. /*
  5117. * New lock request. Walk all POSIX locks and look for conflicts. If
  5118. @@ -1185,7 +1185,7 @@
  5119. }
  5120. out:
  5121. spin_unlock(&ctx->flc_lock);
  5122. - percpu_up_read_preempt_enable(&file_rwsem);
  5123. + percpu_up_read(&file_rwsem);
  5124. /*
  5125. * Free any unused locks.
  5126. */
  5127. @@ -1460,7 +1460,7 @@
  5128. return error;
  5129. }
  5130. - percpu_down_read_preempt_disable(&file_rwsem);
  5131. + percpu_down_read(&file_rwsem);
  5132. spin_lock(&ctx->flc_lock);
  5133. time_out_leases(inode, &dispose);
  5134. @@ -1512,13 +1512,13 @@
  5135. locks_insert_block(fl, new_fl);
  5136. trace_break_lease_block(inode, new_fl);
  5137. spin_unlock(&ctx->flc_lock);
  5138. - percpu_up_read_preempt_enable(&file_rwsem);
  5139. + percpu_up_read(&file_rwsem);
  5140. locks_dispose_list(&dispose);
  5141. error = wait_event_interruptible_timeout(new_fl->fl_wait,
  5142. !new_fl->fl_next, break_time);
  5143. - percpu_down_read_preempt_disable(&file_rwsem);
  5144. + percpu_down_read(&file_rwsem);
  5145. spin_lock(&ctx->flc_lock);
  5146. trace_break_lease_unblock(inode, new_fl);
  5147. locks_delete_block(new_fl);
  5148. @@ -1535,7 +1535,7 @@
  5149. }
  5150. out:
  5151. spin_unlock(&ctx->flc_lock);
  5152. - percpu_up_read_preempt_enable(&file_rwsem);
  5153. + percpu_up_read(&file_rwsem);
  5154. locks_dispose_list(&dispose);
  5155. locks_free_lock(new_fl);
  5156. return error;
  5157. @@ -1609,7 +1609,7 @@
  5158. ctx = smp_load_acquire(&inode->i_flctx);
  5159. if (ctx && !list_empty_careful(&ctx->flc_lease)) {
  5160. - percpu_down_read_preempt_disable(&file_rwsem);
  5161. + percpu_down_read(&file_rwsem);
  5162. spin_lock(&ctx->flc_lock);
  5163. time_out_leases(inode, &dispose);
  5164. list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
  5165. @@ -1619,7 +1619,7 @@
  5166. break;
  5167. }
  5168. spin_unlock(&ctx->flc_lock);
  5169. - percpu_up_read_preempt_enable(&file_rwsem);
  5170. + percpu_up_read(&file_rwsem);
  5171. locks_dispose_list(&dispose);
  5172. }
  5173. @@ -1694,7 +1694,7 @@
  5174. return -EINVAL;
  5175. }
  5176. - percpu_down_read_preempt_disable(&file_rwsem);
  5177. + percpu_down_read(&file_rwsem);
  5178. spin_lock(&ctx->flc_lock);
  5179. time_out_leases(inode, &dispose);
  5180. error = check_conflicting_open(dentry, arg, lease->fl_flags);
  5181. @@ -1765,7 +1765,7 @@
  5182. lease->fl_lmops->lm_setup(lease, priv);
  5183. out:
  5184. spin_unlock(&ctx->flc_lock);
  5185. - percpu_up_read_preempt_enable(&file_rwsem);
  5186. + percpu_up_read(&file_rwsem);
  5187. locks_dispose_list(&dispose);
  5188. if (is_deleg)
  5189. inode_unlock(inode);
  5190. @@ -1788,7 +1788,7 @@
  5191. return error;
  5192. }
  5193. - percpu_down_read_preempt_disable(&file_rwsem);
  5194. + percpu_down_read(&file_rwsem);
  5195. spin_lock(&ctx->flc_lock);
  5196. list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
  5197. if (fl->fl_file == filp &&
  5198. @@ -1801,7 +1801,7 @@
  5199. if (victim)
  5200. error = fl->fl_lmops->lm_change(victim, F_UNLCK, &dispose);
  5201. spin_unlock(&ctx->flc_lock);
  5202. - percpu_up_read_preempt_enable(&file_rwsem);
  5203. + percpu_up_read(&file_rwsem);
  5204. locks_dispose_list(&dispose);
  5205. return error;
  5206. }
  5207. @@ -2532,13 +2532,13 @@
  5208. if (list_empty(&ctx->flc_lease))
  5209. return;
  5210. - percpu_down_read_preempt_disable(&file_rwsem);
  5211. + percpu_down_read(&file_rwsem);
  5212. spin_lock(&ctx->flc_lock);
  5213. list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list)
  5214. if (filp == fl->fl_file)
  5215. lease_modify(fl, F_UNLCK, &dispose);
  5216. spin_unlock(&ctx->flc_lock);
  5217. - percpu_up_read_preempt_enable(&file_rwsem);
  5218. + percpu_up_read(&file_rwsem);
  5219. locks_dispose_list(&dispose);
  5220. }
  5221. diff -Nur linux-4.9.28.orig/fs/namei.c linux-4.9.28/fs/namei.c
  5222. --- linux-4.9.28.orig/fs/namei.c 2017-05-14 14:00:37.000000000 +0200
  5223. +++ linux-4.9.28/fs/namei.c 2017-05-19 03:37:25.158175874 +0200
  5224. @@ -1626,7 +1626,7 @@
  5225. {
  5226. struct dentry *dentry = ERR_PTR(-ENOENT), *old;
  5227. struct inode *inode = dir->d_inode;
  5228. - DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  5229. + DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(wq);
  5230. inode_lock_shared(inode);
  5231. /* Don't go there if it's already dead */
  5232. @@ -3083,7 +3083,7 @@
  5233. struct dentry *dentry;
  5234. int error, create_error = 0;
  5235. umode_t mode = op->mode;
  5236. - DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  5237. + DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(wq);
  5238. if (unlikely(IS_DEADDIR(dir_inode)))
  5239. return -ENOENT;
  5240. diff -Nur linux-4.9.28.orig/fs/namespace.c linux-4.9.28/fs/namespace.c
  5241. --- linux-4.9.28.orig/fs/namespace.c 2017-05-14 14:00:37.000000000 +0200
  5242. +++ linux-4.9.28/fs/namespace.c 2017-05-19 03:37:25.158175874 +0200
  5243. @@ -14,6 +14,7 @@
  5244. #include <linux/mnt_namespace.h>
  5245. #include <linux/user_namespace.h>
  5246. #include <linux/namei.h>
  5247. +#include <linux/delay.h>
  5248. #include <linux/security.h>
  5249. #include <linux/idr.h>
  5250. #include <linux/init.h> /* init_rootfs */
  5251. @@ -356,8 +357,11 @@
  5252. * incremented count after it has set MNT_WRITE_HOLD.
  5253. */
  5254. smp_mb();
  5255. - while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD)
  5256. - cpu_relax();
  5257. + while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD) {
  5258. + preempt_enable();
  5259. + cpu_chill();
  5260. + preempt_disable();
  5261. + }
  5262. /*
  5263. * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
  5264. * be set to match its requirements. So we must not load that until
  5265. diff -Nur linux-4.9.28.orig/fs/nfs/delegation.c linux-4.9.28/fs/nfs/delegation.c
  5266. --- linux-4.9.28.orig/fs/nfs/delegation.c 2017-05-14 14:00:37.000000000 +0200
  5267. +++ linux-4.9.28/fs/nfs/delegation.c 2017-05-19 03:37:25.158175874 +0200
  5268. @@ -150,11 +150,11 @@
  5269. sp = state->owner;
  5270. /* Block nfs4_proc_unlck */
  5271. mutex_lock(&sp->so_delegreturn_mutex);
  5272. - seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
  5273. + seq = read_seqbegin(&sp->so_reclaim_seqlock);
  5274. err = nfs4_open_delegation_recall(ctx, state, stateid, type);
  5275. if (!err)
  5276. err = nfs_delegation_claim_locks(ctx, state, stateid);
  5277. - if (!err && read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
  5278. + if (!err && read_seqretry(&sp->so_reclaim_seqlock, seq))
  5279. err = -EAGAIN;
  5280. mutex_unlock(&sp->so_delegreturn_mutex);
  5281. put_nfs_open_context(ctx);
  5282. diff -Nur linux-4.9.28.orig/fs/nfs/dir.c linux-4.9.28/fs/nfs/dir.c
  5283. --- linux-4.9.28.orig/fs/nfs/dir.c 2017-05-14 14:00:37.000000000 +0200
  5284. +++ linux-4.9.28/fs/nfs/dir.c 2017-05-19 03:37:25.162176025 +0200
  5285. @@ -485,7 +485,7 @@
  5286. void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
  5287. {
  5288. struct qstr filename = QSTR_INIT(entry->name, entry->len);
  5289. - DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  5290. + DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(wq);
  5291. struct dentry *dentry;
  5292. struct dentry *alias;
  5293. struct inode *dir = d_inode(parent);
  5294. @@ -1487,7 +1487,7 @@
  5295. struct file *file, unsigned open_flags,
  5296. umode_t mode, int *opened)
  5297. {
  5298. - DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  5299. + DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(wq);
  5300. struct nfs_open_context *ctx;
  5301. struct dentry *res;
  5302. struct iattr attr = { .ia_valid = ATTR_OPEN };
  5303. @@ -1802,7 +1802,11 @@
  5304. trace_nfs_rmdir_enter(dir, dentry);
  5305. if (d_really_is_positive(dentry)) {
  5306. +#ifdef CONFIG_PREEMPT_RT_BASE
  5307. + down(&NFS_I(d_inode(dentry))->rmdir_sem);
  5308. +#else
  5309. down_write(&NFS_I(d_inode(dentry))->rmdir_sem);
  5310. +#endif
  5311. error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
  5312. /* Ensure the VFS deletes this inode */
  5313. switch (error) {
  5314. @@ -1812,7 +1816,11 @@
  5315. case -ENOENT:
  5316. nfs_dentry_handle_enoent(dentry);
  5317. }
  5318. +#ifdef CONFIG_PREEMPT_RT_BASE
  5319. + up(&NFS_I(d_inode(dentry))->rmdir_sem);
  5320. +#else
  5321. up_write(&NFS_I(d_inode(dentry))->rmdir_sem);
  5322. +#endif
  5323. } else
  5324. error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
  5325. trace_nfs_rmdir_exit(dir, dentry, error);
  5326. diff -Nur linux-4.9.28.orig/fs/nfs/inode.c linux-4.9.28/fs/nfs/inode.c
  5327. --- linux-4.9.28.orig/fs/nfs/inode.c 2017-05-14 14:00:37.000000000 +0200
  5328. +++ linux-4.9.28/fs/nfs/inode.c 2017-05-19 03:37:25.162176025 +0200
  5329. @@ -1957,7 +1957,11 @@
  5330. nfsi->nrequests = 0;
  5331. nfsi->commit_info.ncommit = 0;
  5332. atomic_set(&nfsi->commit_info.rpcs_out, 0);
  5333. +#ifdef CONFIG_PREEMPT_RT_BASE
  5334. + sema_init(&nfsi->rmdir_sem, 1);
  5335. +#else
  5336. init_rwsem(&nfsi->rmdir_sem);
  5337. +#endif
  5338. nfs4_init_once(nfsi);
  5339. }
  5340. diff -Nur linux-4.9.28.orig/fs/nfs/nfs4_fs.h linux-4.9.28/fs/nfs/nfs4_fs.h
  5341. --- linux-4.9.28.orig/fs/nfs/nfs4_fs.h 2017-05-14 14:00:37.000000000 +0200
  5342. +++ linux-4.9.28/fs/nfs/nfs4_fs.h 2017-05-19 03:37:25.162176025 +0200
  5343. @@ -111,7 +111,7 @@
  5344. unsigned long so_flags;
  5345. struct list_head so_states;
  5346. struct nfs_seqid_counter so_seqid;
  5347. - seqcount_t so_reclaim_seqcount;
  5348. + seqlock_t so_reclaim_seqlock;
  5349. struct mutex so_delegreturn_mutex;
  5350. };
  5351. diff -Nur linux-4.9.28.orig/fs/nfs/nfs4proc.c linux-4.9.28/fs/nfs/nfs4proc.c
  5352. --- linux-4.9.28.orig/fs/nfs/nfs4proc.c 2017-05-14 14:00:37.000000000 +0200
  5353. +++ linux-4.9.28/fs/nfs/nfs4proc.c 2017-05-19 03:37:25.162176025 +0200
  5354. @@ -2695,7 +2695,7 @@
  5355. unsigned int seq;
  5356. int ret;
  5357. - seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
  5358. + seq = raw_seqcount_begin(&sp->so_reclaim_seqlock.seqcount);
  5359. ret = _nfs4_proc_open(opendata);
  5360. if (ret != 0)
  5361. @@ -2733,7 +2733,7 @@
  5362. if (d_inode(dentry) == state->inode) {
  5363. nfs_inode_attach_open_context(ctx);
  5364. - if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
  5365. + if (read_seqretry(&sp->so_reclaim_seqlock, seq))
  5366. nfs4_schedule_stateid_recovery(server, state);
  5367. }
  5368. out:
  5369. diff -Nur linux-4.9.28.orig/fs/nfs/nfs4state.c linux-4.9.28/fs/nfs/nfs4state.c
  5370. --- linux-4.9.28.orig/fs/nfs/nfs4state.c 2017-05-14 14:00:37.000000000 +0200
  5371. +++ linux-4.9.28/fs/nfs/nfs4state.c 2017-05-19 03:37:25.162176025 +0200
  5372. @@ -488,7 +488,7 @@
  5373. nfs4_init_seqid_counter(&sp->so_seqid);
  5374. atomic_set(&sp->so_count, 1);
  5375. INIT_LIST_HEAD(&sp->so_lru);
  5376. - seqcount_init(&sp->so_reclaim_seqcount);
  5377. + seqlock_init(&sp->so_reclaim_seqlock);
  5378. mutex_init(&sp->so_delegreturn_mutex);
  5379. return sp;
  5380. }
  5381. @@ -1497,8 +1497,12 @@
  5382. * recovering after a network partition or a reboot from a
  5383. * server that doesn't support a grace period.
  5384. */
  5385. +#ifdef CONFIG_PREEMPT_RT_FULL
  5386. + write_seqlock(&sp->so_reclaim_seqlock);
  5387. +#else
  5388. + write_seqcount_begin(&sp->so_reclaim_seqlock.seqcount);
  5389. +#endif
  5390. spin_lock(&sp->so_lock);
  5391. - raw_write_seqcount_begin(&sp->so_reclaim_seqcount);
  5392. restart:
  5393. list_for_each_entry(state, &sp->so_states, open_states) {
  5394. if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
  5395. @@ -1567,14 +1571,20 @@
  5396. spin_lock(&sp->so_lock);
  5397. goto restart;
  5398. }
  5399. - raw_write_seqcount_end(&sp->so_reclaim_seqcount);
  5400. spin_unlock(&sp->so_lock);
  5401. +#ifdef CONFIG_PREEMPT_RT_FULL
  5402. + write_sequnlock(&sp->so_reclaim_seqlock);
  5403. +#else
  5404. + write_seqcount_end(&sp->so_reclaim_seqlock.seqcount);
  5405. +#endif
  5406. return 0;
  5407. out_err:
  5408. nfs4_put_open_state(state);
  5409. - spin_lock(&sp->so_lock);
  5410. - raw_write_seqcount_end(&sp->so_reclaim_seqcount);
  5411. - spin_unlock(&sp->so_lock);
  5412. +#ifdef CONFIG_PREEMPT_RT_FULL
  5413. + write_sequnlock(&sp->so_reclaim_seqlock);
  5414. +#else
  5415. + write_seqcount_end(&sp->so_reclaim_seqlock.seqcount);
  5416. +#endif
  5417. return status;
  5418. }
  5419. diff -Nur linux-4.9.28.orig/fs/nfs/unlink.c linux-4.9.28/fs/nfs/unlink.c
  5420. --- linux-4.9.28.orig/fs/nfs/unlink.c 2017-05-14 14:00:37.000000000 +0200
  5421. +++ linux-4.9.28/fs/nfs/unlink.c 2017-05-19 03:37:25.162176025 +0200
  5422. @@ -12,7 +12,7 @@
  5423. #include <linux/sunrpc/clnt.h>
  5424. #include <linux/nfs_fs.h>
  5425. #include <linux/sched.h>
  5426. -#include <linux/wait.h>
  5427. +#include <linux/swait.h>
  5428. #include <linux/namei.h>
  5429. #include <linux/fsnotify.h>
  5430. @@ -51,6 +51,29 @@
  5431. rpc_restart_call_prepare(task);
  5432. }
  5433. +#ifdef CONFIG_PREEMPT_RT_BASE
  5434. +static void nfs_down_anon(struct semaphore *sema)
  5435. +{
  5436. + down(sema);
  5437. +}
  5438. +
  5439. +static void nfs_up_anon(struct semaphore *sema)
  5440. +{
  5441. + up(sema);
  5442. +}
  5443. +
  5444. +#else
  5445. +static void nfs_down_anon(struct rw_semaphore *rwsem)
  5446. +{
  5447. + down_read_non_owner(rwsem);
  5448. +}
  5449. +
  5450. +static void nfs_up_anon(struct rw_semaphore *rwsem)
  5451. +{
  5452. + up_read_non_owner(rwsem);
  5453. +}
  5454. +#endif
  5455. +
  5456. /**
  5457. * nfs_async_unlink_release - Release the sillydelete data.
  5458. * @task: rpc_task of the sillydelete
  5459. @@ -64,7 +87,7 @@
  5460. struct dentry *dentry = data->dentry;
  5461. struct super_block *sb = dentry->d_sb;
  5462. - up_read_non_owner(&NFS_I(d_inode(dentry->d_parent))->rmdir_sem);
  5463. + nfs_up_anon(&NFS_I(d_inode(dentry->d_parent))->rmdir_sem);
  5464. d_lookup_done(dentry);
  5465. nfs_free_unlinkdata(data);
  5466. dput(dentry);
  5467. @@ -117,10 +140,10 @@
  5468. struct inode *dir = d_inode(dentry->d_parent);
  5469. struct dentry *alias;
  5470. - down_read_non_owner(&NFS_I(dir)->rmdir_sem);
  5471. + nfs_down_anon(&NFS_I(dir)->rmdir_sem);
  5472. alias = d_alloc_parallel(dentry->d_parent, &data->args.name, &data->wq);
  5473. if (IS_ERR(alias)) {
  5474. - up_read_non_owner(&NFS_I(dir)->rmdir_sem);
  5475. + nfs_up_anon(&NFS_I(dir)->rmdir_sem);
  5476. return 0;
  5477. }
  5478. if (!d_in_lookup(alias)) {
  5479. @@ -142,7 +165,7 @@
  5480. ret = 0;
  5481. spin_unlock(&alias->d_lock);
  5482. dput(alias);
  5483. - up_read_non_owner(&NFS_I(dir)->rmdir_sem);
  5484. + nfs_up_anon(&NFS_I(dir)->rmdir_sem);
  5485. /*
  5486. * If we'd displaced old cached devname, free it. At that
  5487. * point dentry is definitely not a root, so we won't need
  5488. @@ -182,7 +205,7 @@
  5489. goto out_free_name;
  5490. }
  5491. data->res.dir_attr = &data->dir_attr;
  5492. - init_waitqueue_head(&data->wq);
  5493. + init_swait_queue_head(&data->wq);
  5494. status = -EBUSY;
  5495. spin_lock(&dentry->d_lock);
  5496. diff -Nur linux-4.9.28.orig/fs/ntfs/aops.c linux-4.9.28/fs/ntfs/aops.c
  5497. --- linux-4.9.28.orig/fs/ntfs/aops.c 2017-05-14 14:00:37.000000000 +0200
  5498. +++ linux-4.9.28/fs/ntfs/aops.c 2017-05-19 03:37:25.162176025 +0200
  5499. @@ -92,13 +92,13 @@
  5500. ofs = 0;
  5501. if (file_ofs < init_size)
  5502. ofs = init_size - file_ofs;
  5503. - local_irq_save(flags);
  5504. + local_irq_save_nort(flags);
  5505. kaddr = kmap_atomic(page);
  5506. memset(kaddr + bh_offset(bh) + ofs, 0,
  5507. bh->b_size - ofs);
  5508. flush_dcache_page(page);
  5509. kunmap_atomic(kaddr);
  5510. - local_irq_restore(flags);
  5511. + local_irq_restore_nort(flags);
  5512. }
  5513. } else {
  5514. clear_buffer_uptodate(bh);
  5515. @@ -107,8 +107,7 @@
  5516. "0x%llx.", (unsigned long long)bh->b_blocknr);
  5517. }
  5518. first = page_buffers(page);
  5519. - local_irq_save(flags);
  5520. - bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  5521. + flags = bh_uptodate_lock_irqsave(first);
  5522. clear_buffer_async_read(bh);
  5523. unlock_buffer(bh);
  5524. tmp = bh;
  5525. @@ -123,8 +122,7 @@
  5526. }
  5527. tmp = tmp->b_this_page;
  5528. } while (tmp != bh);
  5529. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  5530. - local_irq_restore(flags);
  5531. + bh_uptodate_unlock_irqrestore(first, flags);
  5532. /*
  5533. * If none of the buffers had errors then we can set the page uptodate,
  5534. * but we first have to perform the post read mst fixups, if the
  5535. @@ -145,13 +143,13 @@
  5536. recs = PAGE_SIZE / rec_size;
  5537. /* Should have been verified before we got here... */
  5538. BUG_ON(!recs);
  5539. - local_irq_save(flags);
  5540. + local_irq_save_nort(flags);
  5541. kaddr = kmap_atomic(page);
  5542. for (i = 0; i < recs; i++)
  5543. post_read_mst_fixup((NTFS_RECORD*)(kaddr +
  5544. i * rec_size), rec_size);
  5545. kunmap_atomic(kaddr);
  5546. - local_irq_restore(flags);
  5547. + local_irq_restore_nort(flags);
  5548. flush_dcache_page(page);
  5549. if (likely(page_uptodate && !PageError(page)))
  5550. SetPageUptodate(page);
  5551. @@ -159,9 +157,7 @@
  5552. unlock_page(page);
  5553. return;
  5554. still_busy:
  5555. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  5556. - local_irq_restore(flags);
  5557. - return;
  5558. + bh_uptodate_unlock_irqrestore(first, flags);
  5559. }
  5560. /**
  5561. diff -Nur linux-4.9.28.orig/fs/proc/base.c linux-4.9.28/fs/proc/base.c
  5562. --- linux-4.9.28.orig/fs/proc/base.c 2017-05-14 14:00:37.000000000 +0200
  5563. +++ linux-4.9.28/fs/proc/base.c 2017-05-19 03:37:25.162176025 +0200
  5564. @@ -1834,7 +1834,7 @@
  5565. child = d_hash_and_lookup(dir, &qname);
  5566. if (!child) {
  5567. - DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  5568. + DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(wq);
  5569. child = d_alloc_parallel(dir, &qname, &wq);
  5570. if (IS_ERR(child))
  5571. goto end_instantiate;
  5572. diff -Nur linux-4.9.28.orig/fs/proc/proc_sysctl.c linux-4.9.28/fs/proc/proc_sysctl.c
  5573. --- linux-4.9.28.orig/fs/proc/proc_sysctl.c 2017-05-14 14:00:37.000000000 +0200
  5574. +++ linux-4.9.28/fs/proc/proc_sysctl.c 2017-05-19 03:37:25.162176025 +0200
  5575. @@ -632,7 +632,7 @@
  5576. child = d_lookup(dir, &qname);
  5577. if (!child) {
  5578. - DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  5579. + DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(wq);
  5580. child = d_alloc_parallel(dir, &qname, &wq);
  5581. if (IS_ERR(child))
  5582. return false;
  5583. diff -Nur linux-4.9.28.orig/fs/timerfd.c linux-4.9.28/fs/timerfd.c
  5584. --- linux-4.9.28.orig/fs/timerfd.c 2017-05-14 14:00:37.000000000 +0200
  5585. +++ linux-4.9.28/fs/timerfd.c 2017-05-19 03:37:25.162176025 +0200
  5586. @@ -471,7 +471,10 @@
  5587. break;
  5588. }
  5589. spin_unlock_irq(&ctx->wqh.lock);
  5590. - cpu_relax();
  5591. + if (isalarm(ctx))
  5592. + hrtimer_wait_for_timer(&ctx->t.alarm.timer);
  5593. + else
  5594. + hrtimer_wait_for_timer(&ctx->t.tmr);
  5595. }
  5596. /*
  5597. diff -Nur linux-4.9.28.orig/include/acpi/platform/aclinux.h linux-4.9.28/include/acpi/platform/aclinux.h
  5598. --- linux-4.9.28.orig/include/acpi/platform/aclinux.h 2017-05-14 14:00:37.000000000 +0200
  5599. +++ linux-4.9.28/include/acpi/platform/aclinux.h 2017-05-19 03:37:25.166176182 +0200
  5600. @@ -133,6 +133,7 @@
  5601. #define acpi_cache_t struct kmem_cache
  5602. #define acpi_spinlock spinlock_t *
  5603. +#define acpi_raw_spinlock raw_spinlock_t *
  5604. #define acpi_cpu_flags unsigned long
  5605. /* Use native linux version of acpi_os_allocate_zeroed */
  5606. @@ -151,6 +152,20 @@
  5607. #define ACPI_USE_ALTERNATE_PROTOTYPE_acpi_os_get_thread_id
  5608. #define ACPI_USE_ALTERNATE_PROTOTYPE_acpi_os_create_lock
  5609. +#define acpi_os_create_raw_lock(__handle) \
  5610. +({ \
  5611. + raw_spinlock_t *lock = ACPI_ALLOCATE(sizeof(*lock)); \
  5612. + \
  5613. + if (lock) { \
  5614. + *(__handle) = lock; \
  5615. + raw_spin_lock_init(*(__handle)); \
  5616. + } \
  5617. + lock ? AE_OK : AE_NO_MEMORY; \
  5618. + })
  5619. +
  5620. +#define acpi_os_delete_raw_lock(__handle) kfree(__handle)
  5621. +
  5622. +
  5623. /*
  5624. * OSL interfaces used by debugger/disassembler
  5625. */
  5626. diff -Nur linux-4.9.28.orig/include/asm-generic/bug.h linux-4.9.28/include/asm-generic/bug.h
  5627. --- linux-4.9.28.orig/include/asm-generic/bug.h 2017-05-14 14:00:37.000000000 +0200
  5628. +++ linux-4.9.28/include/asm-generic/bug.h 2017-05-19 03:37:25.166176182 +0200
  5629. @@ -215,6 +215,20 @@
  5630. # define WARN_ON_SMP(x) ({0;})
  5631. #endif
  5632. +#ifdef CONFIG_PREEMPT_RT_BASE
  5633. +# define BUG_ON_RT(c) BUG_ON(c)
  5634. +# define BUG_ON_NONRT(c) do { } while (0)
  5635. +# define WARN_ON_RT(condition) WARN_ON(condition)
  5636. +# define WARN_ON_NONRT(condition) do { } while (0)
  5637. +# define WARN_ON_ONCE_NONRT(condition) do { } while (0)
  5638. +#else
  5639. +# define BUG_ON_RT(c) do { } while (0)
  5640. +# define BUG_ON_NONRT(c) BUG_ON(c)
  5641. +# define WARN_ON_RT(condition) do { } while (0)
  5642. +# define WARN_ON_NONRT(condition) WARN_ON(condition)
  5643. +# define WARN_ON_ONCE_NONRT(condition) WARN_ON_ONCE(condition)
  5644. +#endif
  5645. +
  5646. #endif /* __ASSEMBLY__ */
  5647. #endif
  5648. diff -Nur linux-4.9.28.orig/include/linux/blkdev.h linux-4.9.28/include/linux/blkdev.h
  5649. --- linux-4.9.28.orig/include/linux/blkdev.h 2017-05-14 14:00:37.000000000 +0200
  5650. +++ linux-4.9.28/include/linux/blkdev.h 2017-05-19 03:37:25.166176182 +0200
  5651. @@ -89,6 +89,7 @@
  5652. struct list_head queuelist;
  5653. union {
  5654. struct call_single_data csd;
  5655. + struct work_struct work;
  5656. u64 fifo_time;
  5657. };
  5658. @@ -467,7 +468,7 @@
  5659. struct throtl_data *td;
  5660. #endif
  5661. struct rcu_head rcu_head;
  5662. - wait_queue_head_t mq_freeze_wq;
  5663. + struct swait_queue_head mq_freeze_wq;
  5664. struct percpu_ref q_usage_counter;
  5665. struct list_head all_q_node;
  5666. diff -Nur linux-4.9.28.orig/include/linux/blk-mq.h linux-4.9.28/include/linux/blk-mq.h
  5667. --- linux-4.9.28.orig/include/linux/blk-mq.h 2017-05-14 14:00:37.000000000 +0200
  5668. +++ linux-4.9.28/include/linux/blk-mq.h 2017-05-19 03:37:25.166176182 +0200
  5669. @@ -209,7 +209,7 @@
  5670. return unique_tag & BLK_MQ_UNIQUE_TAG_MASK;
  5671. }
  5672. -
  5673. +void __blk_mq_complete_request_remote_work(struct work_struct *work);
  5674. int blk_mq_request_started(struct request *rq);
  5675. void blk_mq_start_request(struct request *rq);
  5676. void blk_mq_end_request(struct request *rq, int error);
  5677. diff -Nur linux-4.9.28.orig/include/linux/bottom_half.h linux-4.9.28/include/linux/bottom_half.h
  5678. --- linux-4.9.28.orig/include/linux/bottom_half.h 2017-05-14 14:00:37.000000000 +0200
  5679. +++ linux-4.9.28/include/linux/bottom_half.h 2017-05-19 03:37:25.166176182 +0200
  5680. @@ -3,6 +3,39 @@
  5681. #include <linux/preempt.h>
  5682. +#ifdef CONFIG_PREEMPT_RT_FULL
  5683. +
  5684. +extern void __local_bh_disable(void);
  5685. +extern void _local_bh_enable(void);
  5686. +extern void __local_bh_enable(void);
  5687. +
  5688. +static inline void local_bh_disable(void)
  5689. +{
  5690. + __local_bh_disable();
  5691. +}
  5692. +
  5693. +static inline void __local_bh_disable_ip(unsigned long ip, unsigned int cnt)
  5694. +{
  5695. + __local_bh_disable();
  5696. +}
  5697. +
  5698. +static inline void local_bh_enable(void)
  5699. +{
  5700. + __local_bh_enable();
  5701. +}
  5702. +
  5703. +static inline void __local_bh_enable_ip(unsigned long ip, unsigned int cnt)
  5704. +{
  5705. + __local_bh_enable();
  5706. +}
  5707. +
  5708. +static inline void local_bh_enable_ip(unsigned long ip)
  5709. +{
  5710. + __local_bh_enable();
  5711. +}
  5712. +
  5713. +#else
  5714. +
  5715. #ifdef CONFIG_TRACE_IRQFLAGS
  5716. extern void __local_bh_disable_ip(unsigned long ip, unsigned int cnt);
  5717. #else
  5718. @@ -30,5 +63,6 @@
  5719. {
  5720. __local_bh_enable_ip(_THIS_IP_, SOFTIRQ_DISABLE_OFFSET);
  5721. }
  5722. +#endif
  5723. #endif /* _LINUX_BH_H */
  5724. diff -Nur linux-4.9.28.orig/include/linux/buffer_head.h linux-4.9.28/include/linux/buffer_head.h
  5725. --- linux-4.9.28.orig/include/linux/buffer_head.h 2017-05-14 14:00:37.000000000 +0200
  5726. +++ linux-4.9.28/include/linux/buffer_head.h 2017-05-19 03:37:25.166176182 +0200
  5727. @@ -75,8 +75,50 @@
  5728. struct address_space *b_assoc_map; /* mapping this buffer is
  5729. associated with */
  5730. atomic_t b_count; /* users using this buffer_head */
  5731. +#ifdef CONFIG_PREEMPT_RT_BASE
  5732. + spinlock_t b_uptodate_lock;
  5733. +#if IS_ENABLED(CONFIG_JBD2)
  5734. + spinlock_t b_state_lock;
  5735. + spinlock_t b_journal_head_lock;
  5736. +#endif
  5737. +#endif
  5738. };
  5739. +static inline unsigned long bh_uptodate_lock_irqsave(struct buffer_head *bh)
  5740. +{
  5741. + unsigned long flags;
  5742. +
  5743. +#ifndef CONFIG_PREEMPT_RT_BASE
  5744. + local_irq_save(flags);
  5745. + bit_spin_lock(BH_Uptodate_Lock, &bh->b_state);
  5746. +#else
  5747. + spin_lock_irqsave(&bh->b_uptodate_lock, flags);
  5748. +#endif
  5749. + return flags;
  5750. +}
  5751. +
  5752. +static inline void
  5753. +bh_uptodate_unlock_irqrestore(struct buffer_head *bh, unsigned long flags)
  5754. +{
  5755. +#ifndef CONFIG_PREEMPT_RT_BASE
  5756. + bit_spin_unlock(BH_Uptodate_Lock, &bh->b_state);
  5757. + local_irq_restore(flags);
  5758. +#else
  5759. + spin_unlock_irqrestore(&bh->b_uptodate_lock, flags);
  5760. +#endif
  5761. +}
  5762. +
  5763. +static inline void buffer_head_init_locks(struct buffer_head *bh)
  5764. +{
  5765. +#ifdef CONFIG_PREEMPT_RT_BASE
  5766. + spin_lock_init(&bh->b_uptodate_lock);
  5767. +#if IS_ENABLED(CONFIG_JBD2)
  5768. + spin_lock_init(&bh->b_state_lock);
  5769. + spin_lock_init(&bh->b_journal_head_lock);
  5770. +#endif
  5771. +#endif
  5772. +}
  5773. +
  5774. /*
  5775. * macro tricks to expand the set_buffer_foo(), clear_buffer_foo()
  5776. * and buffer_foo() functions.
  5777. diff -Nur linux-4.9.28.orig/include/linux/cgroup-defs.h linux-4.9.28/include/linux/cgroup-defs.h
  5778. --- linux-4.9.28.orig/include/linux/cgroup-defs.h 2017-05-14 14:00:37.000000000 +0200
  5779. +++ linux-4.9.28/include/linux/cgroup-defs.h 2017-05-19 03:37:25.166176182 +0200
  5780. @@ -16,6 +16,7 @@
  5781. #include <linux/percpu-refcount.h>
  5782. #include <linux/percpu-rwsem.h>
  5783. #include <linux/workqueue.h>
  5784. +#include <linux/swork.h>
  5785. #ifdef CONFIG_CGROUPS
  5786. @@ -137,6 +138,7 @@
  5787. /* percpu_ref killing and RCU release */
  5788. struct rcu_head rcu_head;
  5789. struct work_struct destroy_work;
  5790. + struct swork_event destroy_swork;
  5791. };
  5792. /*
  5793. diff -Nur linux-4.9.28.orig/include/linux/completion.h linux-4.9.28/include/linux/completion.h
  5794. --- linux-4.9.28.orig/include/linux/completion.h 2017-05-14 14:00:37.000000000 +0200
  5795. +++ linux-4.9.28/include/linux/completion.h 2017-05-19 03:37:25.166176182 +0200
  5796. @@ -7,8 +7,7 @@
  5797. * Atomic wait-for-completion handler data structures.
  5798. * See kernel/sched/completion.c for details.
  5799. */
  5800. -
  5801. -#include <linux/wait.h>
  5802. +#include <linux/swait.h>
  5803. /*
  5804. * struct completion - structure used to maintain state for a "completion"
  5805. @@ -24,11 +23,11 @@
  5806. */
  5807. struct completion {
  5808. unsigned int done;
  5809. - wait_queue_head_t wait;
  5810. + struct swait_queue_head wait;
  5811. };
  5812. #define COMPLETION_INITIALIZER(work) \
  5813. - { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
  5814. + { 0, __SWAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
  5815. #define COMPLETION_INITIALIZER_ONSTACK(work) \
  5816. ({ init_completion(&work); work; })
  5817. @@ -73,7 +72,7 @@
  5818. static inline void init_completion(struct completion *x)
  5819. {
  5820. x->done = 0;
  5821. - init_waitqueue_head(&x->wait);
  5822. + init_swait_queue_head(&x->wait);
  5823. }
  5824. /**
  5825. diff -Nur linux-4.9.28.orig/include/linux/cpu.h linux-4.9.28/include/linux/cpu.h
  5826. --- linux-4.9.28.orig/include/linux/cpu.h 2017-05-14 14:00:37.000000000 +0200
  5827. +++ linux-4.9.28/include/linux/cpu.h 2017-05-19 03:37:25.166176182 +0200
  5828. @@ -182,6 +182,8 @@
  5829. extern void put_online_cpus(void);
  5830. extern void cpu_hotplug_disable(void);
  5831. extern void cpu_hotplug_enable(void);
  5832. +extern void pin_current_cpu(void);
  5833. +extern void unpin_current_cpu(void);
  5834. #define hotcpu_notifier(fn, pri) cpu_notifier(fn, pri)
  5835. #define __hotcpu_notifier(fn, pri) __cpu_notifier(fn, pri)
  5836. #define register_hotcpu_notifier(nb) register_cpu_notifier(nb)
  5837. @@ -199,6 +201,8 @@
  5838. #define put_online_cpus() do { } while (0)
  5839. #define cpu_hotplug_disable() do { } while (0)
  5840. #define cpu_hotplug_enable() do { } while (0)
  5841. +static inline void pin_current_cpu(void) { }
  5842. +static inline void unpin_current_cpu(void) { }
  5843. #define hotcpu_notifier(fn, pri) do { (void)(fn); } while (0)
  5844. #define __hotcpu_notifier(fn, pri) do { (void)(fn); } while (0)
  5845. /* These aren't inline functions due to a GCC bug. */
  5846. diff -Nur linux-4.9.28.orig/include/linux/dcache.h linux-4.9.28/include/linux/dcache.h
  5847. --- linux-4.9.28.orig/include/linux/dcache.h 2017-05-14 14:00:37.000000000 +0200
  5848. +++ linux-4.9.28/include/linux/dcache.h 2017-05-19 03:37:25.166176182 +0200
  5849. @@ -11,6 +11,7 @@
  5850. #include <linux/rcupdate.h>
  5851. #include <linux/lockref.h>
  5852. #include <linux/stringhash.h>
  5853. +#include <linux/wait.h>
  5854. struct path;
  5855. struct vfsmount;
  5856. @@ -100,7 +101,7 @@
  5857. union {
  5858. struct list_head d_lru; /* LRU list */
  5859. - wait_queue_head_t *d_wait; /* in-lookup ones only */
  5860. + struct swait_queue_head *d_wait; /* in-lookup ones only */
  5861. };
  5862. struct list_head d_child; /* child of parent list */
  5863. struct list_head d_subdirs; /* our children */
  5864. @@ -230,7 +231,7 @@
  5865. extern struct dentry * d_alloc(struct dentry *, const struct qstr *);
  5866. extern struct dentry * d_alloc_pseudo(struct super_block *, const struct qstr *);
  5867. extern struct dentry * d_alloc_parallel(struct dentry *, const struct qstr *,
  5868. - wait_queue_head_t *);
  5869. + struct swait_queue_head *);
  5870. extern struct dentry * d_splice_alias(struct inode *, struct dentry *);
  5871. extern struct dentry * d_add_ci(struct dentry *, struct inode *, struct qstr *);
  5872. extern struct dentry * d_exact_alias(struct dentry *, struct inode *);
  5873. diff -Nur linux-4.9.28.orig/include/linux/delay.h linux-4.9.28/include/linux/delay.h
  5874. --- linux-4.9.28.orig/include/linux/delay.h 2017-05-14 14:00:37.000000000 +0200
  5875. +++ linux-4.9.28/include/linux/delay.h 2017-05-19 03:37:25.166176182 +0200
  5876. @@ -52,4 +52,10 @@
  5877. msleep(seconds * 1000);
  5878. }
  5879. +#ifdef CONFIG_PREEMPT_RT_FULL
  5880. +extern void cpu_chill(void);
  5881. +#else
  5882. +# define cpu_chill() cpu_relax()
  5883. +#endif
  5884. +
  5885. #endif /* defined(_LINUX_DELAY_H) */
  5886. diff -Nur linux-4.9.28.orig/include/linux/highmem.h linux-4.9.28/include/linux/highmem.h
  5887. --- linux-4.9.28.orig/include/linux/highmem.h 2017-05-14 14:00:37.000000000 +0200
  5888. +++ linux-4.9.28/include/linux/highmem.h 2017-05-19 03:37:25.166176182 +0200
  5889. @@ -7,6 +7,7 @@
  5890. #include <linux/mm.h>
  5891. #include <linux/uaccess.h>
  5892. #include <linux/hardirq.h>
  5893. +#include <linux/sched.h>
  5894. #include <asm/cacheflush.h>
  5895. @@ -65,7 +66,7 @@
  5896. static inline void *kmap_atomic(struct page *page)
  5897. {
  5898. - preempt_disable();
  5899. + preempt_disable_nort();
  5900. pagefault_disable();
  5901. return page_address(page);
  5902. }
  5903. @@ -74,7 +75,7 @@
  5904. static inline void __kunmap_atomic(void *addr)
  5905. {
  5906. pagefault_enable();
  5907. - preempt_enable();
  5908. + preempt_enable_nort();
  5909. }
  5910. #define kmap_atomic_pfn(pfn) kmap_atomic(pfn_to_page(pfn))
  5911. @@ -86,32 +87,51 @@
  5912. #if defined(CONFIG_HIGHMEM) || defined(CONFIG_X86_32)
  5913. +#ifndef CONFIG_PREEMPT_RT_FULL
  5914. DECLARE_PER_CPU(int, __kmap_atomic_idx);
  5915. +#endif
  5916. static inline int kmap_atomic_idx_push(void)
  5917. {
  5918. +#ifndef CONFIG_PREEMPT_RT_FULL
  5919. int idx = __this_cpu_inc_return(__kmap_atomic_idx) - 1;
  5920. -#ifdef CONFIG_DEBUG_HIGHMEM
  5921. +# ifdef CONFIG_DEBUG_HIGHMEM
  5922. WARN_ON_ONCE(in_irq() && !irqs_disabled());
  5923. BUG_ON(idx >= KM_TYPE_NR);
  5924. -#endif
  5925. +# endif
  5926. return idx;
  5927. +#else
  5928. + current->kmap_idx++;
  5929. + BUG_ON(current->kmap_idx > KM_TYPE_NR);
  5930. + return current->kmap_idx - 1;
  5931. +#endif
  5932. }
  5933. static inline int kmap_atomic_idx(void)
  5934. {
  5935. +#ifndef CONFIG_PREEMPT_RT_FULL
  5936. return __this_cpu_read(__kmap_atomic_idx) - 1;
  5937. +#else
  5938. + return current->kmap_idx - 1;
  5939. +#endif
  5940. }
  5941. static inline void kmap_atomic_idx_pop(void)
  5942. {
  5943. -#ifdef CONFIG_DEBUG_HIGHMEM
  5944. +#ifndef CONFIG_PREEMPT_RT_FULL
  5945. +# ifdef CONFIG_DEBUG_HIGHMEM
  5946. int idx = __this_cpu_dec_return(__kmap_atomic_idx);
  5947. BUG_ON(idx < 0);
  5948. -#else
  5949. +# else
  5950. __this_cpu_dec(__kmap_atomic_idx);
  5951. +# endif
  5952. +#else
  5953. + current->kmap_idx--;
  5954. +# ifdef CONFIG_DEBUG_HIGHMEM
  5955. + BUG_ON(current->kmap_idx < 0);
  5956. +# endif
  5957. #endif
  5958. }
  5959. diff -Nur linux-4.9.28.orig/include/linux/hrtimer.h linux-4.9.28/include/linux/hrtimer.h
  5960. --- linux-4.9.28.orig/include/linux/hrtimer.h 2017-05-14 14:00:37.000000000 +0200
  5961. +++ linux-4.9.28/include/linux/hrtimer.h 2017-05-19 03:37:25.166176182 +0200
  5962. @@ -87,6 +87,9 @@
  5963. * @function: timer expiry callback function
  5964. * @base: pointer to the timer base (per cpu and per clock)
  5965. * @state: state information (See bit values above)
  5966. + * @cb_entry: list entry to defer timers from hardirq context
  5967. + * @irqsafe: timer can run in hardirq context
  5968. + * @praecox: timer expiry time if expired at the time of programming
  5969. * @is_rel: Set if the timer was armed relative
  5970. * @start_pid: timer statistics field to store the pid of the task which
  5971. * started the timer
  5972. @@ -103,6 +106,11 @@
  5973. enum hrtimer_restart (*function)(struct hrtimer *);
  5974. struct hrtimer_clock_base *base;
  5975. u8 state;
  5976. + struct list_head cb_entry;
  5977. + int irqsafe;
  5978. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  5979. + ktime_t praecox;
  5980. +#endif
  5981. u8 is_rel;
  5982. #ifdef CONFIG_TIMER_STATS
  5983. int start_pid;
  5984. @@ -123,11 +131,7 @@
  5985. struct task_struct *task;
  5986. };
  5987. -#ifdef CONFIG_64BIT
  5988. # define HRTIMER_CLOCK_BASE_ALIGN 64
  5989. -#else
  5990. -# define HRTIMER_CLOCK_BASE_ALIGN 32
  5991. -#endif
  5992. /**
  5993. * struct hrtimer_clock_base - the timer base for a specific clock
  5994. @@ -136,6 +140,7 @@
  5995. * timer to a base on another cpu.
  5996. * @clockid: clock id for per_cpu support
  5997. * @active: red black tree root node for the active timers
  5998. + * @expired: list head for deferred timers.
  5999. * @get_time: function to retrieve the current time of the clock
  6000. * @offset: offset of this clock to the monotonic base
  6001. */
  6002. @@ -144,6 +149,7 @@
  6003. int index;
  6004. clockid_t clockid;
  6005. struct timerqueue_head active;
  6006. + struct list_head expired;
  6007. ktime_t (*get_time)(void);
  6008. ktime_t offset;
  6009. } __attribute__((__aligned__(HRTIMER_CLOCK_BASE_ALIGN)));
  6010. @@ -187,6 +193,7 @@
  6011. raw_spinlock_t lock;
  6012. seqcount_t seq;
  6013. struct hrtimer *running;
  6014. + struct hrtimer *running_soft;
  6015. unsigned int cpu;
  6016. unsigned int active_bases;
  6017. unsigned int clock_was_set_seq;
  6018. @@ -203,6 +210,9 @@
  6019. unsigned int nr_hangs;
  6020. unsigned int max_hang_time;
  6021. #endif
  6022. +#ifdef CONFIG_PREEMPT_RT_BASE
  6023. + wait_queue_head_t wait;
  6024. +#endif
  6025. struct hrtimer_clock_base clock_base[HRTIMER_MAX_CLOCK_BASES];
  6026. } ____cacheline_aligned;
  6027. @@ -412,6 +422,13 @@
  6028. hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
  6029. }
  6030. +/* Softirq preemption could deadlock timer removal */
  6031. +#ifdef CONFIG_PREEMPT_RT_BASE
  6032. + extern void hrtimer_wait_for_timer(const struct hrtimer *timer);
  6033. +#else
  6034. +# define hrtimer_wait_for_timer(timer) do { cpu_relax(); } while (0)
  6035. +#endif
  6036. +
  6037. /* Query timers: */
  6038. extern ktime_t __hrtimer_get_remaining(const struct hrtimer *timer, bool adjust);
  6039. @@ -436,9 +453,15 @@
  6040. * Helper function to check, whether the timer is running the callback
  6041. * function
  6042. */
  6043. -static inline int hrtimer_callback_running(struct hrtimer *timer)
  6044. +static inline int hrtimer_callback_running(const struct hrtimer *timer)
  6045. {
  6046. - return timer->base->cpu_base->running == timer;
  6047. + if (timer->base->cpu_base->running == timer)
  6048. + return 1;
  6049. +#ifdef CONFIG_PREEMPT_RT_BASE
  6050. + if (timer->base->cpu_base->running_soft == timer)
  6051. + return 1;
  6052. +#endif
  6053. + return 0;
  6054. }
  6055. /* Forward a hrtimer so it expires after now: */
  6056. diff -Nur linux-4.9.28.orig/include/linux/idr.h linux-4.9.28/include/linux/idr.h
  6057. --- linux-4.9.28.orig/include/linux/idr.h 2017-05-14 14:00:37.000000000 +0200
  6058. +++ linux-4.9.28/include/linux/idr.h 2017-05-19 03:37:25.166176182 +0200
  6059. @@ -95,10 +95,14 @@
  6060. * Each idr_preload() should be matched with an invocation of this
  6061. * function. See idr_preload() for details.
  6062. */
  6063. +#ifdef CONFIG_PREEMPT_RT_FULL
  6064. +void idr_preload_end(void);
  6065. +#else
  6066. static inline void idr_preload_end(void)
  6067. {
  6068. preempt_enable();
  6069. }
  6070. +#endif
  6071. /**
  6072. * idr_find - return pointer for given id
  6073. diff -Nur linux-4.9.28.orig/include/linux/init_task.h linux-4.9.28/include/linux/init_task.h
  6074. --- linux-4.9.28.orig/include/linux/init_task.h 2017-05-14 14:00:37.000000000 +0200
  6075. +++ linux-4.9.28/include/linux/init_task.h 2017-05-19 03:37:25.166176182 +0200
  6076. @@ -150,6 +150,12 @@
  6077. # define INIT_PERF_EVENTS(tsk)
  6078. #endif
  6079. +#ifdef CONFIG_PREEMPT_RT_BASE
  6080. +# define INIT_TIMER_LIST .posix_timer_list = NULL,
  6081. +#else
  6082. +# define INIT_TIMER_LIST
  6083. +#endif
  6084. +
  6085. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  6086. # define INIT_VTIME(tsk) \
  6087. .vtime_seqcount = SEQCNT_ZERO(tsk.vtime_seqcount), \
  6088. @@ -164,6 +170,7 @@
  6089. #ifdef CONFIG_RT_MUTEXES
  6090. # define INIT_RT_MUTEXES(tsk) \
  6091. .pi_waiters = RB_ROOT, \
  6092. + .pi_top_task = NULL, \
  6093. .pi_waiters_leftmost = NULL,
  6094. #else
  6095. # define INIT_RT_MUTEXES(tsk)
  6096. @@ -250,6 +257,7 @@
  6097. .cpu_timers = INIT_CPU_TIMERS(tsk.cpu_timers), \
  6098. .pi_lock = __RAW_SPIN_LOCK_UNLOCKED(tsk.pi_lock), \
  6099. .timer_slack_ns = 50000, /* 50 usec default slack */ \
  6100. + INIT_TIMER_LIST \
  6101. .pids = { \
  6102. [PIDTYPE_PID] = INIT_PID_LINK(PIDTYPE_PID), \
  6103. [PIDTYPE_PGID] = INIT_PID_LINK(PIDTYPE_PGID), \
  6104. diff -Nur linux-4.9.28.orig/include/linux/interrupt.h linux-4.9.28/include/linux/interrupt.h
  6105. --- linux-4.9.28.orig/include/linux/interrupt.h 2017-05-14 14:00:37.000000000 +0200
  6106. +++ linux-4.9.28/include/linux/interrupt.h 2017-05-19 03:37:25.166176182 +0200
  6107. @@ -14,6 +14,7 @@
  6108. #include <linux/hrtimer.h>
  6109. #include <linux/kref.h>
  6110. #include <linux/workqueue.h>
  6111. +#include <linux/swork.h>
  6112. #include <linux/atomic.h>
  6113. #include <asm/ptrace.h>
  6114. @@ -61,6 +62,7 @@
  6115. * interrupt handler after suspending interrupts. For system
  6116. * wakeup devices users need to implement wakeup detection in
  6117. * their interrupt handlers.
  6118. + * IRQF_NO_SOFTIRQ_CALL - Do not process softirqs in the irq thread context (RT)
  6119. */
  6120. #define IRQF_SHARED 0x00000080
  6121. #define IRQF_PROBE_SHARED 0x00000100
  6122. @@ -74,6 +76,7 @@
  6123. #define IRQF_NO_THREAD 0x00010000
  6124. #define IRQF_EARLY_RESUME 0x00020000
  6125. #define IRQF_COND_SUSPEND 0x00040000
  6126. +#define IRQF_NO_SOFTIRQ_CALL 0x00080000
  6127. #define IRQF_TIMER (__IRQF_TIMER | IRQF_NO_SUSPEND | IRQF_NO_THREAD)
  6128. @@ -196,7 +199,7 @@
  6129. #ifdef CONFIG_LOCKDEP
  6130. # define local_irq_enable_in_hardirq() do { } while (0)
  6131. #else
  6132. -# define local_irq_enable_in_hardirq() local_irq_enable()
  6133. +# define local_irq_enable_in_hardirq() local_irq_enable_nort()
  6134. #endif
  6135. extern void disable_irq_nosync(unsigned int irq);
  6136. @@ -216,6 +219,7 @@
  6137. * struct irq_affinity_notify - context for notification of IRQ affinity changes
  6138. * @irq: Interrupt to which notification applies
  6139. * @kref: Reference count, for internal use
  6140. + * @swork: Swork item, for internal use
  6141. * @work: Work item, for internal use
  6142. * @notify: Function to be called on change. This will be
  6143. * called in process context.
  6144. @@ -227,7 +231,11 @@
  6145. struct irq_affinity_notify {
  6146. unsigned int irq;
  6147. struct kref kref;
  6148. +#ifdef CONFIG_PREEMPT_RT_BASE
  6149. + struct swork_event swork;
  6150. +#else
  6151. struct work_struct work;
  6152. +#endif
  6153. void (*notify)(struct irq_affinity_notify *, const cpumask_t *mask);
  6154. void (*release)(struct kref *ref);
  6155. };
  6156. @@ -406,9 +414,13 @@
  6157. bool state);
  6158. #ifdef CONFIG_IRQ_FORCED_THREADING
  6159. +# ifndef CONFIG_PREEMPT_RT_BASE
  6160. extern bool force_irqthreads;
  6161. +# else
  6162. +# define force_irqthreads (true)
  6163. +# endif
  6164. #else
  6165. -#define force_irqthreads (0)
  6166. +#define force_irqthreads (false)
  6167. #endif
  6168. #ifndef __ARCH_SET_SOFTIRQ_PENDING
  6169. @@ -465,9 +477,10 @@
  6170. void (*action)(struct softirq_action *);
  6171. };
  6172. +#ifndef CONFIG_PREEMPT_RT_FULL
  6173. asmlinkage void do_softirq(void);
  6174. asmlinkage void __do_softirq(void);
  6175. -
  6176. +static inline void thread_do_softirq(void) { do_softirq(); }
  6177. #ifdef __ARCH_HAS_DO_SOFTIRQ
  6178. void do_softirq_own_stack(void);
  6179. #else
  6180. @@ -476,13 +489,25 @@
  6181. __do_softirq();
  6182. }
  6183. #endif
  6184. +#else
  6185. +extern void thread_do_softirq(void);
  6186. +#endif
  6187. extern void open_softirq(int nr, void (*action)(struct softirq_action *));
  6188. extern void softirq_init(void);
  6189. extern void __raise_softirq_irqoff(unsigned int nr);
  6190. +#ifdef CONFIG_PREEMPT_RT_FULL
  6191. +extern void __raise_softirq_irqoff_ksoft(unsigned int nr);
  6192. +#else
  6193. +static inline void __raise_softirq_irqoff_ksoft(unsigned int nr)
  6194. +{
  6195. + __raise_softirq_irqoff(nr);
  6196. +}
  6197. +#endif
  6198. extern void raise_softirq_irqoff(unsigned int nr);
  6199. extern void raise_softirq(unsigned int nr);
  6200. +extern void softirq_check_pending_idle(void);
  6201. DECLARE_PER_CPU(struct task_struct *, ksoftirqd);
  6202. @@ -504,8 +529,9 @@
  6203. to be executed on some cpu at least once after this.
  6204. * If the tasklet is already scheduled, but its execution is still not
  6205. started, it will be executed only once.
  6206. - * If this tasklet is already running on another CPU (or schedule is called
  6207. - from tasklet itself), it is rescheduled for later.
  6208. + * If this tasklet is already running on another CPU, it is rescheduled
  6209. + for later.
  6210. + * Schedule must not be called from the tasklet itself (a lockup occurs)
  6211. * Tasklet is strictly serialized wrt itself, but not
  6212. wrt another tasklets. If client needs some intertask synchronization,
  6213. he makes it with spinlocks.
  6214. @@ -530,27 +556,36 @@
  6215. enum
  6216. {
  6217. TASKLET_STATE_SCHED, /* Tasklet is scheduled for execution */
  6218. - TASKLET_STATE_RUN /* Tasklet is running (SMP only) */
  6219. + TASKLET_STATE_RUN, /* Tasklet is running (SMP only) */
  6220. + TASKLET_STATE_PENDING /* Tasklet is pending */
  6221. };
  6222. -#ifdef CONFIG_SMP
  6223. +#define TASKLET_STATEF_SCHED (1 << TASKLET_STATE_SCHED)
  6224. +#define TASKLET_STATEF_RUN (1 << TASKLET_STATE_RUN)
  6225. +#define TASKLET_STATEF_PENDING (1 << TASKLET_STATE_PENDING)
  6226. +
  6227. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  6228. static inline int tasklet_trylock(struct tasklet_struct *t)
  6229. {
  6230. return !test_and_set_bit(TASKLET_STATE_RUN, &(t)->state);
  6231. }
  6232. +static inline int tasklet_tryunlock(struct tasklet_struct *t)
  6233. +{
  6234. + return cmpxchg(&t->state, TASKLET_STATEF_RUN, 0) == TASKLET_STATEF_RUN;
  6235. +}
  6236. +
  6237. static inline void tasklet_unlock(struct tasklet_struct *t)
  6238. {
  6239. smp_mb__before_atomic();
  6240. clear_bit(TASKLET_STATE_RUN, &(t)->state);
  6241. }
  6242. -static inline void tasklet_unlock_wait(struct tasklet_struct *t)
  6243. -{
  6244. - while (test_bit(TASKLET_STATE_RUN, &(t)->state)) { barrier(); }
  6245. -}
  6246. +extern void tasklet_unlock_wait(struct tasklet_struct *t);
  6247. +
  6248. #else
  6249. #define tasklet_trylock(t) 1
  6250. +#define tasklet_tryunlock(t) 1
  6251. #define tasklet_unlock_wait(t) do { } while (0)
  6252. #define tasklet_unlock(t) do { } while (0)
  6253. #endif
  6254. @@ -599,12 +634,7 @@
  6255. smp_mb();
  6256. }
  6257. -static inline void tasklet_enable(struct tasklet_struct *t)
  6258. -{
  6259. - smp_mb__before_atomic();
  6260. - atomic_dec(&t->count);
  6261. -}
  6262. -
  6263. +extern void tasklet_enable(struct tasklet_struct *t);
  6264. extern void tasklet_kill(struct tasklet_struct *t);
  6265. extern void tasklet_kill_immediate(struct tasklet_struct *t, unsigned int cpu);
  6266. extern void tasklet_init(struct tasklet_struct *t,
  6267. @@ -635,6 +665,12 @@
  6268. tasklet_kill(&ttimer->tasklet);
  6269. }
  6270. +#ifdef CONFIG_PREEMPT_RT_FULL
  6271. +extern void softirq_early_init(void);
  6272. +#else
  6273. +static inline void softirq_early_init(void) { }
  6274. +#endif
  6275. +
  6276. /*
  6277. * Autoprobing for irqs:
  6278. *
  6279. diff -Nur linux-4.9.28.orig/include/linux/irqdesc.h linux-4.9.28/include/linux/irqdesc.h
  6280. --- linux-4.9.28.orig/include/linux/irqdesc.h 2017-05-14 14:00:37.000000000 +0200
  6281. +++ linux-4.9.28/include/linux/irqdesc.h 2017-05-19 03:37:25.166176182 +0200
  6282. @@ -66,6 +66,7 @@
  6283. unsigned int irqs_unhandled;
  6284. atomic_t threads_handled;
  6285. int threads_handled_last;
  6286. + u64 random_ip;
  6287. raw_spinlock_t lock;
  6288. struct cpumask *percpu_enabled;
  6289. const struct cpumask *percpu_affinity;
  6290. diff -Nur linux-4.9.28.orig/include/linux/irqflags.h linux-4.9.28/include/linux/irqflags.h
  6291. --- linux-4.9.28.orig/include/linux/irqflags.h 2017-05-14 14:00:37.000000000 +0200
  6292. +++ linux-4.9.28/include/linux/irqflags.h 2017-05-19 03:37:25.166176182 +0200
  6293. @@ -25,8 +25,6 @@
  6294. # define trace_softirqs_enabled(p) ((p)->softirqs_enabled)
  6295. # define trace_hardirq_enter() do { current->hardirq_context++; } while (0)
  6296. # define trace_hardirq_exit() do { current->hardirq_context--; } while (0)
  6297. -# define lockdep_softirq_enter() do { current->softirq_context++; } while (0)
  6298. -# define lockdep_softirq_exit() do { current->softirq_context--; } while (0)
  6299. # define INIT_TRACE_IRQFLAGS .softirqs_enabled = 1,
  6300. #else
  6301. # define trace_hardirqs_on() do { } while (0)
  6302. @@ -39,9 +37,15 @@
  6303. # define trace_softirqs_enabled(p) 0
  6304. # define trace_hardirq_enter() do { } while (0)
  6305. # define trace_hardirq_exit() do { } while (0)
  6306. +# define INIT_TRACE_IRQFLAGS
  6307. +#endif
  6308. +
  6309. +#if defined(CONFIG_TRACE_IRQFLAGS) && !defined(CONFIG_PREEMPT_RT_FULL)
  6310. +# define lockdep_softirq_enter() do { current->softirq_context++; } while (0)
  6311. +# define lockdep_softirq_exit() do { current->softirq_context--; } while (0)
  6312. +#else
  6313. # define lockdep_softirq_enter() do { } while (0)
  6314. # define lockdep_softirq_exit() do { } while (0)
  6315. -# define INIT_TRACE_IRQFLAGS
  6316. #endif
  6317. #if defined(CONFIG_IRQSOFF_TRACER) || \
  6318. @@ -148,4 +152,23 @@
  6319. #define irqs_disabled_flags(flags) raw_irqs_disabled_flags(flags)
  6320. +/*
  6321. + * local_irq* variants depending on RT/!RT
  6322. + */
  6323. +#ifdef CONFIG_PREEMPT_RT_FULL
  6324. +# define local_irq_disable_nort() do { } while (0)
  6325. +# define local_irq_enable_nort() do { } while (0)
  6326. +# define local_irq_save_nort(flags) local_save_flags(flags)
  6327. +# define local_irq_restore_nort(flags) (void)(flags)
  6328. +# define local_irq_disable_rt() local_irq_disable()
  6329. +# define local_irq_enable_rt() local_irq_enable()
  6330. +#else
  6331. +# define local_irq_disable_nort() local_irq_disable()
  6332. +# define local_irq_enable_nort() local_irq_enable()
  6333. +# define local_irq_save_nort(flags) local_irq_save(flags)
  6334. +# define local_irq_restore_nort(flags) local_irq_restore(flags)
  6335. +# define local_irq_disable_rt() do { } while (0)
  6336. +# define local_irq_enable_rt() do { } while (0)
  6337. +#endif
  6338. +
  6339. #endif
  6340. diff -Nur linux-4.9.28.orig/include/linux/irq.h linux-4.9.28/include/linux/irq.h
  6341. --- linux-4.9.28.orig/include/linux/irq.h 2017-05-14 14:00:37.000000000 +0200
  6342. +++ linux-4.9.28/include/linux/irq.h 2017-05-19 03:37:25.166176182 +0200
  6343. @@ -72,6 +72,7 @@
  6344. * IRQ_IS_POLLED - Always polled by another interrupt. Exclude
  6345. * it from the spurious interrupt detection
  6346. * mechanism and from core side polling.
  6347. + * IRQ_NO_SOFTIRQ_CALL - No softirq processing in the irq thread context (RT)
  6348. * IRQ_DISABLE_UNLAZY - Disable lazy irq disable
  6349. */
  6350. enum {
  6351. @@ -99,13 +100,14 @@
  6352. IRQ_PER_CPU_DEVID = (1 << 17),
  6353. IRQ_IS_POLLED = (1 << 18),
  6354. IRQ_DISABLE_UNLAZY = (1 << 19),
  6355. + IRQ_NO_SOFTIRQ_CALL = (1 << 20),
  6356. };
  6357. #define IRQF_MODIFY_MASK \
  6358. (IRQ_TYPE_SENSE_MASK | IRQ_NOPROBE | IRQ_NOREQUEST | \
  6359. IRQ_NOAUTOEN | IRQ_MOVE_PCNTXT | IRQ_LEVEL | IRQ_NO_BALANCING | \
  6360. IRQ_PER_CPU | IRQ_NESTED_THREAD | IRQ_NOTHREAD | IRQ_PER_CPU_DEVID | \
  6361. - IRQ_IS_POLLED | IRQ_DISABLE_UNLAZY)
  6362. + IRQ_IS_POLLED | IRQ_DISABLE_UNLAZY | IRQ_NO_SOFTIRQ_CALL)
  6363. #define IRQ_NO_BALANCING_MASK (IRQ_PER_CPU | IRQ_NO_BALANCING)
  6364. diff -Nur linux-4.9.28.orig/include/linux/irq_work.h linux-4.9.28/include/linux/irq_work.h
  6365. --- linux-4.9.28.orig/include/linux/irq_work.h 2017-05-14 14:00:37.000000000 +0200
  6366. +++ linux-4.9.28/include/linux/irq_work.h 2017-05-19 03:37:25.166176182 +0200
  6367. @@ -16,6 +16,7 @@
  6368. #define IRQ_WORK_BUSY 2UL
  6369. #define IRQ_WORK_FLAGS 3UL
  6370. #define IRQ_WORK_LAZY 4UL /* Doesn't want IPI, wait for tick */
  6371. +#define IRQ_WORK_HARD_IRQ 8UL /* Run hard IRQ context, even on RT */
  6372. struct irq_work {
  6373. unsigned long flags;
  6374. @@ -51,4 +52,10 @@
  6375. static inline void irq_work_run(void) { }
  6376. #endif
  6377. +#if defined(CONFIG_IRQ_WORK) && defined(CONFIG_PREEMPT_RT_FULL)
  6378. +void irq_work_tick_soft(void);
  6379. +#else
  6380. +static inline void irq_work_tick_soft(void) { }
  6381. +#endif
  6382. +
  6383. #endif /* _LINUX_IRQ_WORK_H */
  6384. diff -Nur linux-4.9.28.orig/include/linux/jbd2.h linux-4.9.28/include/linux/jbd2.h
  6385. --- linux-4.9.28.orig/include/linux/jbd2.h 2017-05-14 14:00:37.000000000 +0200
  6386. +++ linux-4.9.28/include/linux/jbd2.h 2017-05-19 03:37:25.166176182 +0200
  6387. @@ -347,32 +347,56 @@
  6388. static inline void jbd_lock_bh_state(struct buffer_head *bh)
  6389. {
  6390. +#ifndef CONFIG_PREEMPT_RT_BASE
  6391. bit_spin_lock(BH_State, &bh->b_state);
  6392. +#else
  6393. + spin_lock(&bh->b_state_lock);
  6394. +#endif
  6395. }
  6396. static inline int jbd_trylock_bh_state(struct buffer_head *bh)
  6397. {
  6398. +#ifndef CONFIG_PREEMPT_RT_BASE
  6399. return bit_spin_trylock(BH_State, &bh->b_state);
  6400. +#else
  6401. + return spin_trylock(&bh->b_state_lock);
  6402. +#endif
  6403. }
  6404. static inline int jbd_is_locked_bh_state(struct buffer_head *bh)
  6405. {
  6406. +#ifndef CONFIG_PREEMPT_RT_BASE
  6407. return bit_spin_is_locked(BH_State, &bh->b_state);
  6408. +#else
  6409. + return spin_is_locked(&bh->b_state_lock);
  6410. +#endif
  6411. }
  6412. static inline void jbd_unlock_bh_state(struct buffer_head *bh)
  6413. {
  6414. +#ifndef CONFIG_PREEMPT_RT_BASE
  6415. bit_spin_unlock(BH_State, &bh->b_state);
  6416. +#else
  6417. + spin_unlock(&bh->b_state_lock);
  6418. +#endif
  6419. }
  6420. static inline void jbd_lock_bh_journal_head(struct buffer_head *bh)
  6421. {
  6422. +#ifndef CONFIG_PREEMPT_RT_BASE
  6423. bit_spin_lock(BH_JournalHead, &bh->b_state);
  6424. +#else
  6425. + spin_lock(&bh->b_journal_head_lock);
  6426. +#endif
  6427. }
  6428. static inline void jbd_unlock_bh_journal_head(struct buffer_head *bh)
  6429. {
  6430. +#ifndef CONFIG_PREEMPT_RT_BASE
  6431. bit_spin_unlock(BH_JournalHead, &bh->b_state);
  6432. +#else
  6433. + spin_unlock(&bh->b_journal_head_lock);
  6434. +#endif
  6435. }
  6436. #define J_ASSERT(assert) BUG_ON(!(assert))
  6437. diff -Nur linux-4.9.28.orig/include/linux/kdb.h linux-4.9.28/include/linux/kdb.h
  6438. --- linux-4.9.28.orig/include/linux/kdb.h 2017-05-14 14:00:37.000000000 +0200
  6439. +++ linux-4.9.28/include/linux/kdb.h 2017-05-19 03:37:25.166176182 +0200
  6440. @@ -167,6 +167,7 @@
  6441. extern __printf(1, 2) int kdb_printf(const char *, ...);
  6442. typedef __printf(1, 2) int (*kdb_printf_t)(const char *, ...);
  6443. +#define in_kdb_printk() (kdb_trap_printk)
  6444. extern void kdb_init(int level);
  6445. /* Access to kdb specific polling devices */
  6446. @@ -201,6 +202,7 @@
  6447. extern int kdb_unregister(char *);
  6448. #else /* ! CONFIG_KGDB_KDB */
  6449. static inline __printf(1, 2) int kdb_printf(const char *fmt, ...) { return 0; }
  6450. +#define in_kdb_printk() (0)
  6451. static inline void kdb_init(int level) {}
  6452. static inline int kdb_register(char *cmd, kdb_func_t func, char *usage,
  6453. char *help, short minlen) { return 0; }
  6454. diff -Nur linux-4.9.28.orig/include/linux/kernel.h linux-4.9.28/include/linux/kernel.h
  6455. --- linux-4.9.28.orig/include/linux/kernel.h 2017-05-14 14:00:37.000000000 +0200
  6456. +++ linux-4.9.28/include/linux/kernel.h 2017-05-19 03:37:25.166176182 +0200
  6457. @@ -194,6 +194,9 @@
  6458. */
  6459. # define might_sleep() \
  6460. do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
  6461. +
  6462. +# define might_sleep_no_state_check() \
  6463. + do { ___might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
  6464. # define sched_annotate_sleep() (current->task_state_change = 0)
  6465. #else
  6466. static inline void ___might_sleep(const char *file, int line,
  6467. @@ -201,6 +204,7 @@
  6468. static inline void __might_sleep(const char *file, int line,
  6469. int preempt_offset) { }
  6470. # define might_sleep() do { might_resched(); } while (0)
  6471. +# define might_sleep_no_state_check() do { might_resched(); } while (0)
  6472. # define sched_annotate_sleep() do { } while (0)
  6473. #endif
  6474. @@ -488,6 +492,7 @@
  6475. SYSTEM_HALT,
  6476. SYSTEM_POWER_OFF,
  6477. SYSTEM_RESTART,
  6478. + SYSTEM_SUSPEND,
  6479. } system_state;
  6480. #define TAINT_PROPRIETARY_MODULE 0
  6481. diff -Nur linux-4.9.28.orig/include/linux/list_bl.h linux-4.9.28/include/linux/list_bl.h
  6482. --- linux-4.9.28.orig/include/linux/list_bl.h 2017-05-14 14:00:37.000000000 +0200
  6483. +++ linux-4.9.28/include/linux/list_bl.h 2017-05-19 03:37:25.166176182 +0200
  6484. @@ -2,6 +2,7 @@
  6485. #define _LINUX_LIST_BL_H
  6486. #include <linux/list.h>
  6487. +#include <linux/spinlock.h>
  6488. #include <linux/bit_spinlock.h>
  6489. /*
  6490. @@ -32,13 +33,24 @@
  6491. struct hlist_bl_head {
  6492. struct hlist_bl_node *first;
  6493. +#ifdef CONFIG_PREEMPT_RT_BASE
  6494. + raw_spinlock_t lock;
  6495. +#endif
  6496. };
  6497. struct hlist_bl_node {
  6498. struct hlist_bl_node *next, **pprev;
  6499. };
  6500. -#define INIT_HLIST_BL_HEAD(ptr) \
  6501. - ((ptr)->first = NULL)
  6502. +
  6503. +#ifdef CONFIG_PREEMPT_RT_BASE
  6504. +#define INIT_HLIST_BL_HEAD(h) \
  6505. +do { \
  6506. + (h)->first = NULL; \
  6507. + raw_spin_lock_init(&(h)->lock); \
  6508. +} while (0)
  6509. +#else
  6510. +#define INIT_HLIST_BL_HEAD(h) (h)->first = NULL
  6511. +#endif
  6512. static inline void INIT_HLIST_BL_NODE(struct hlist_bl_node *h)
  6513. {
  6514. @@ -118,12 +130,26 @@
  6515. static inline void hlist_bl_lock(struct hlist_bl_head *b)
  6516. {
  6517. +#ifndef CONFIG_PREEMPT_RT_BASE
  6518. bit_spin_lock(0, (unsigned long *)b);
  6519. +#else
  6520. + raw_spin_lock(&b->lock);
  6521. +#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  6522. + __set_bit(0, (unsigned long *)b);
  6523. +#endif
  6524. +#endif
  6525. }
  6526. static inline void hlist_bl_unlock(struct hlist_bl_head *b)
  6527. {
  6528. +#ifndef CONFIG_PREEMPT_RT_BASE
  6529. __bit_spin_unlock(0, (unsigned long *)b);
  6530. +#else
  6531. +#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  6532. + __clear_bit(0, (unsigned long *)b);
  6533. +#endif
  6534. + raw_spin_unlock(&b->lock);
  6535. +#endif
  6536. }
  6537. static inline bool hlist_bl_is_locked(struct hlist_bl_head *b)
  6538. diff -Nur linux-4.9.28.orig/include/linux/locallock.h linux-4.9.28/include/linux/locallock.h
  6539. --- linux-4.9.28.orig/include/linux/locallock.h 1970-01-01 01:00:00.000000000 +0100
  6540. +++ linux-4.9.28/include/linux/locallock.h 2017-05-19 03:37:25.166176182 +0200
  6541. @@ -0,0 +1,278 @@
  6542. +#ifndef _LINUX_LOCALLOCK_H
  6543. +#define _LINUX_LOCALLOCK_H
  6544. +
  6545. +#include <linux/percpu.h>
  6546. +#include <linux/spinlock.h>
  6547. +
  6548. +#ifdef CONFIG_PREEMPT_RT_BASE
  6549. +
  6550. +#ifdef CONFIG_DEBUG_SPINLOCK
  6551. +# define LL_WARN(cond) WARN_ON(cond)
  6552. +#else
  6553. +# define LL_WARN(cond) do { } while (0)
  6554. +#endif
  6555. +
  6556. +/*
  6557. + * per cpu lock based substitute for local_irq_*()
  6558. + */
  6559. +struct local_irq_lock {
  6560. + spinlock_t lock;
  6561. + struct task_struct *owner;
  6562. + int nestcnt;
  6563. + unsigned long flags;
  6564. +};
  6565. +
  6566. +#define DEFINE_LOCAL_IRQ_LOCK(lvar) \
  6567. + DEFINE_PER_CPU(struct local_irq_lock, lvar) = { \
  6568. + .lock = __SPIN_LOCK_UNLOCKED((lvar).lock) }
  6569. +
  6570. +#define DECLARE_LOCAL_IRQ_LOCK(lvar) \
  6571. + DECLARE_PER_CPU(struct local_irq_lock, lvar)
  6572. +
  6573. +#define local_irq_lock_init(lvar) \
  6574. + do { \
  6575. + int __cpu; \
  6576. + for_each_possible_cpu(__cpu) \
  6577. + spin_lock_init(&per_cpu(lvar, __cpu).lock); \
  6578. + } while (0)
  6579. +
  6580. +/*
  6581. + * spin_lock|trylock|unlock_local flavour that does not migrate disable
  6582. + * used for __local_lock|trylock|unlock where get_local_var/put_local_var
  6583. + * already takes care of the migrate_disable/enable
  6584. + * for CONFIG_PREEMPT_BASE map to the normal spin_* calls.
  6585. + */
  6586. +#ifdef CONFIG_PREEMPT_RT_FULL
  6587. +# define spin_lock_local(lock) rt_spin_lock__no_mg(lock)
  6588. +# define spin_trylock_local(lock) rt_spin_trylock__no_mg(lock)
  6589. +# define spin_unlock_local(lock) rt_spin_unlock__no_mg(lock)
  6590. +#else
  6591. +# define spin_lock_local(lock) spin_lock(lock)
  6592. +# define spin_trylock_local(lock) spin_trylock(lock)
  6593. +# define spin_unlock_local(lock) spin_unlock(lock)
  6594. +#endif
  6595. +
  6596. +static inline void __local_lock(struct local_irq_lock *lv)
  6597. +{
  6598. + if (lv->owner != current) {
  6599. + spin_lock_local(&lv->lock);
  6600. + LL_WARN(lv->owner);
  6601. + LL_WARN(lv->nestcnt);
  6602. + lv->owner = current;
  6603. + }
  6604. + lv->nestcnt++;
  6605. +}
  6606. +
  6607. +#define local_lock(lvar) \
  6608. + do { __local_lock(&get_local_var(lvar)); } while (0)
  6609. +
  6610. +#define local_lock_on(lvar, cpu) \
  6611. + do { __local_lock(&per_cpu(lvar, cpu)); } while (0)
  6612. +
  6613. +static inline int __local_trylock(struct local_irq_lock *lv)
  6614. +{
  6615. + if (lv->owner != current && spin_trylock_local(&lv->lock)) {
  6616. + LL_WARN(lv->owner);
  6617. + LL_WARN(lv->nestcnt);
  6618. + lv->owner = current;
  6619. + lv->nestcnt = 1;
  6620. + return 1;
  6621. + }
  6622. + return 0;
  6623. +}
  6624. +
  6625. +#define local_trylock(lvar) \
  6626. + ({ \
  6627. + int __locked; \
  6628. + __locked = __local_trylock(&get_local_var(lvar)); \
  6629. + if (!__locked) \
  6630. + put_local_var(lvar); \
  6631. + __locked; \
  6632. + })
  6633. +
  6634. +static inline void __local_unlock(struct local_irq_lock *lv)
  6635. +{
  6636. + LL_WARN(lv->nestcnt == 0);
  6637. + LL_WARN(lv->owner != current);
  6638. + if (--lv->nestcnt)
  6639. + return;
  6640. +
  6641. + lv->owner = NULL;
  6642. + spin_unlock_local(&lv->lock);
  6643. +}
  6644. +
  6645. +#define local_unlock(lvar) \
  6646. + do { \
  6647. + __local_unlock(this_cpu_ptr(&lvar)); \
  6648. + put_local_var(lvar); \
  6649. + } while (0)
  6650. +
  6651. +#define local_unlock_on(lvar, cpu) \
  6652. + do { __local_unlock(&per_cpu(lvar, cpu)); } while (0)
  6653. +
  6654. +static inline void __local_lock_irq(struct local_irq_lock *lv)
  6655. +{
  6656. + spin_lock_irqsave(&lv->lock, lv->flags);
  6657. + LL_WARN(lv->owner);
  6658. + LL_WARN(lv->nestcnt);
  6659. + lv->owner = current;
  6660. + lv->nestcnt = 1;
  6661. +}
  6662. +
  6663. +#define local_lock_irq(lvar) \
  6664. + do { __local_lock_irq(&get_local_var(lvar)); } while (0)
  6665. +
  6666. +#define local_lock_irq_on(lvar, cpu) \
  6667. + do { __local_lock_irq(&per_cpu(lvar, cpu)); } while (0)
  6668. +
  6669. +static inline void __local_unlock_irq(struct local_irq_lock *lv)
  6670. +{
  6671. + LL_WARN(!lv->nestcnt);
  6672. + LL_WARN(lv->owner != current);
  6673. + lv->owner = NULL;
  6674. + lv->nestcnt = 0;
  6675. + spin_unlock_irq(&lv->lock);
  6676. +}
  6677. +
  6678. +#define local_unlock_irq(lvar) \
  6679. + do { \
  6680. + __local_unlock_irq(this_cpu_ptr(&lvar)); \
  6681. + put_local_var(lvar); \
  6682. + } while (0)
  6683. +
  6684. +#define local_unlock_irq_on(lvar, cpu) \
  6685. + do { \
  6686. + __local_unlock_irq(&per_cpu(lvar, cpu)); \
  6687. + } while (0)
  6688. +
  6689. +static inline int __local_lock_irqsave(struct local_irq_lock *lv)
  6690. +{
  6691. + if (lv->owner != current) {
  6692. + __local_lock_irq(lv);
  6693. + return 0;
  6694. + } else {
  6695. + lv->nestcnt++;
  6696. + return 1;
  6697. + }
  6698. +}
  6699. +
  6700. +#define local_lock_irqsave(lvar, _flags) \
  6701. + do { \
  6702. + if (__local_lock_irqsave(&get_local_var(lvar))) \
  6703. + put_local_var(lvar); \
  6704. + _flags = __this_cpu_read(lvar.flags); \
  6705. + } while (0)
  6706. +
  6707. +#define local_lock_irqsave_on(lvar, _flags, cpu) \
  6708. + do { \
  6709. + __local_lock_irqsave(&per_cpu(lvar, cpu)); \
  6710. + _flags = per_cpu(lvar, cpu).flags; \
  6711. + } while (0)
  6712. +
  6713. +static inline int __local_unlock_irqrestore(struct local_irq_lock *lv,
  6714. + unsigned long flags)
  6715. +{
  6716. + LL_WARN(!lv->nestcnt);
  6717. + LL_WARN(lv->owner != current);
  6718. + if (--lv->nestcnt)
  6719. + return 0;
  6720. +
  6721. + lv->owner = NULL;
  6722. + spin_unlock_irqrestore(&lv->lock, lv->flags);
  6723. + return 1;
  6724. +}
  6725. +
  6726. +#define local_unlock_irqrestore(lvar, flags) \
  6727. + do { \
  6728. + if (__local_unlock_irqrestore(this_cpu_ptr(&lvar), flags)) \
  6729. + put_local_var(lvar); \
  6730. + } while (0)
  6731. +
  6732. +#define local_unlock_irqrestore_on(lvar, flags, cpu) \
  6733. + do { \
  6734. + __local_unlock_irqrestore(&per_cpu(lvar, cpu), flags); \
  6735. + } while (0)
  6736. +
  6737. +#define local_spin_trylock_irq(lvar, lock) \
  6738. + ({ \
  6739. + int __locked; \
  6740. + local_lock_irq(lvar); \
  6741. + __locked = spin_trylock(lock); \
  6742. + if (!__locked) \
  6743. + local_unlock_irq(lvar); \
  6744. + __locked; \
  6745. + })
  6746. +
  6747. +#define local_spin_lock_irq(lvar, lock) \
  6748. + do { \
  6749. + local_lock_irq(lvar); \
  6750. + spin_lock(lock); \
  6751. + } while (0)
  6752. +
  6753. +#define local_spin_unlock_irq(lvar, lock) \
  6754. + do { \
  6755. + spin_unlock(lock); \
  6756. + local_unlock_irq(lvar); \
  6757. + } while (0)
  6758. +
  6759. +#define local_spin_lock_irqsave(lvar, lock, flags) \
  6760. + do { \
  6761. + local_lock_irqsave(lvar, flags); \
  6762. + spin_lock(lock); \
  6763. + } while (0)
  6764. +
  6765. +#define local_spin_unlock_irqrestore(lvar, lock, flags) \
  6766. + do { \
  6767. + spin_unlock(lock); \
  6768. + local_unlock_irqrestore(lvar, flags); \
  6769. + } while (0)
  6770. +
  6771. +#define get_locked_var(lvar, var) \
  6772. + (*({ \
  6773. + local_lock(lvar); \
  6774. + this_cpu_ptr(&var); \
  6775. + }))
  6776. +
  6777. +#define put_locked_var(lvar, var) local_unlock(lvar);
  6778. +
  6779. +#define local_lock_cpu(lvar) \
  6780. + ({ \
  6781. + local_lock(lvar); \
  6782. + smp_processor_id(); \
  6783. + })
  6784. +
  6785. +#define local_unlock_cpu(lvar) local_unlock(lvar)
  6786. +
  6787. +#else /* PREEMPT_RT_BASE */
  6788. +
  6789. +#define DEFINE_LOCAL_IRQ_LOCK(lvar) __typeof__(const int) lvar
  6790. +#define DECLARE_LOCAL_IRQ_LOCK(lvar) extern __typeof__(const int) lvar
  6791. +
  6792. +static inline void local_irq_lock_init(int lvar) { }
  6793. +
  6794. +#define local_lock(lvar) preempt_disable()
  6795. +#define local_unlock(lvar) preempt_enable()
  6796. +#define local_lock_irq(lvar) local_irq_disable()
  6797. +#define local_lock_irq_on(lvar, cpu) local_irq_disable()
  6798. +#define local_unlock_irq(lvar) local_irq_enable()
  6799. +#define local_unlock_irq_on(lvar, cpu) local_irq_enable()
  6800. +#define local_lock_irqsave(lvar, flags) local_irq_save(flags)
  6801. +#define local_unlock_irqrestore(lvar, flags) local_irq_restore(flags)
  6802. +
  6803. +#define local_spin_trylock_irq(lvar, lock) spin_trylock_irq(lock)
  6804. +#define local_spin_lock_irq(lvar, lock) spin_lock_irq(lock)
  6805. +#define local_spin_unlock_irq(lvar, lock) spin_unlock_irq(lock)
  6806. +#define local_spin_lock_irqsave(lvar, lock, flags) \
  6807. + spin_lock_irqsave(lock, flags)
  6808. +#define local_spin_unlock_irqrestore(lvar, lock, flags) \
  6809. + spin_unlock_irqrestore(lock, flags)
  6810. +
  6811. +#define get_locked_var(lvar, var) get_cpu_var(var)
  6812. +#define put_locked_var(lvar, var) put_cpu_var(var)
  6813. +
  6814. +#define local_lock_cpu(lvar) get_cpu()
  6815. +#define local_unlock_cpu(lvar) put_cpu()
  6816. +
  6817. +#endif
  6818. +
  6819. +#endif
  6820. diff -Nur linux-4.9.28.orig/include/linux/mm_types.h linux-4.9.28/include/linux/mm_types.h
  6821. --- linux-4.9.28.orig/include/linux/mm_types.h 2017-05-14 14:00:37.000000000 +0200
  6822. +++ linux-4.9.28/include/linux/mm_types.h 2017-05-19 03:37:25.170176339 +0200
  6823. @@ -11,6 +11,7 @@
  6824. #include <linux/completion.h>
  6825. #include <linux/cpumask.h>
  6826. #include <linux/uprobes.h>
  6827. +#include <linux/rcupdate.h>
  6828. #include <linux/page-flags-layout.h>
  6829. #include <linux/workqueue.h>
  6830. #include <asm/page.h>
  6831. @@ -509,6 +510,9 @@
  6832. bool tlb_flush_pending;
  6833. #endif
  6834. struct uprobes_state uprobes_state;
  6835. +#ifdef CONFIG_PREEMPT_RT_BASE
  6836. + struct rcu_head delayed_drop;
  6837. +#endif
  6838. #ifdef CONFIG_X86_INTEL_MPX
  6839. /* address of the bounds directory */
  6840. void __user *bd_addr;
  6841. diff -Nur linux-4.9.28.orig/include/linux/module.h linux-4.9.28/include/linux/module.h
  6842. --- linux-4.9.28.orig/include/linux/module.h 2017-05-14 14:00:37.000000000 +0200
  6843. +++ linux-4.9.28/include/linux/module.h 2017-05-19 03:37:25.170176339 +0200
  6844. @@ -496,6 +496,7 @@
  6845. struct module *__module_text_address(unsigned long addr);
  6846. struct module *__module_address(unsigned long addr);
  6847. bool is_module_address(unsigned long addr);
  6848. +bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr);
  6849. bool is_module_percpu_address(unsigned long addr);
  6850. bool is_module_text_address(unsigned long addr);
  6851. @@ -662,6 +663,11 @@
  6852. {
  6853. return false;
  6854. }
  6855. +
  6856. +static inline bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
  6857. +{
  6858. + return false;
  6859. +}
  6860. static inline bool is_module_text_address(unsigned long addr)
  6861. {
  6862. diff -Nur linux-4.9.28.orig/include/linux/mutex.h linux-4.9.28/include/linux/mutex.h
  6863. --- linux-4.9.28.orig/include/linux/mutex.h 2017-05-14 14:00:37.000000000 +0200
  6864. +++ linux-4.9.28/include/linux/mutex.h 2017-05-19 03:37:25.170176339 +0200
  6865. @@ -19,6 +19,17 @@
  6866. #include <asm/processor.h>
  6867. #include <linux/osq_lock.h>
  6868. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  6869. +# define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
  6870. + , .dep_map = { .name = #lockname }
  6871. +#else
  6872. +# define __DEP_MAP_MUTEX_INITIALIZER(lockname)
  6873. +#endif
  6874. +
  6875. +#ifdef CONFIG_PREEMPT_RT_FULL
  6876. +# include <linux/mutex_rt.h>
  6877. +#else
  6878. +
  6879. /*
  6880. * Simple, straightforward mutexes with strict semantics:
  6881. *
  6882. @@ -99,13 +110,6 @@
  6883. static inline void mutex_destroy(struct mutex *lock) {}
  6884. #endif
  6885. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  6886. -# define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
  6887. - , .dep_map = { .name = #lockname }
  6888. -#else
  6889. -# define __DEP_MAP_MUTEX_INITIALIZER(lockname)
  6890. -#endif
  6891. -
  6892. #define __MUTEX_INITIALIZER(lockname) \
  6893. { .count = ATOMIC_INIT(1) \
  6894. , .wait_lock = __SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
  6895. @@ -173,6 +177,8 @@
  6896. extern int mutex_trylock(struct mutex *lock);
  6897. extern void mutex_unlock(struct mutex *lock);
  6898. +#endif /* !PREEMPT_RT_FULL */
  6899. +
  6900. extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);
  6901. #endif /* __LINUX_MUTEX_H */
  6902. diff -Nur linux-4.9.28.orig/include/linux/mutex_rt.h linux-4.9.28/include/linux/mutex_rt.h
  6903. --- linux-4.9.28.orig/include/linux/mutex_rt.h 1970-01-01 01:00:00.000000000 +0100
  6904. +++ linux-4.9.28/include/linux/mutex_rt.h 2017-05-19 03:37:25.170176339 +0200
  6905. @@ -0,0 +1,89 @@
  6906. +#ifndef __LINUX_MUTEX_RT_H
  6907. +#define __LINUX_MUTEX_RT_H
  6908. +
  6909. +#ifndef __LINUX_MUTEX_H
  6910. +#error "Please include mutex.h"
  6911. +#endif
  6912. +
  6913. +#include <linux/rtmutex.h>
  6914. +
  6915. +/* FIXME: Just for __lockfunc */
  6916. +#include <linux/spinlock.h>
  6917. +
  6918. +struct mutex {
  6919. + struct rt_mutex lock;
  6920. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  6921. + struct lockdep_map dep_map;
  6922. +#endif
  6923. +};
  6924. +
  6925. +#define __MUTEX_INITIALIZER(mutexname) \
  6926. + { \
  6927. + .lock = __RT_MUTEX_INITIALIZER(mutexname.lock) \
  6928. + __DEP_MAP_MUTEX_INITIALIZER(mutexname) \
  6929. + }
  6930. +
  6931. +#define DEFINE_MUTEX(mutexname) \
  6932. + struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
  6933. +
  6934. +extern void __mutex_do_init(struct mutex *lock, const char *name, struct lock_class_key *key);
  6935. +extern void __lockfunc _mutex_lock(struct mutex *lock);
  6936. +extern int __lockfunc _mutex_lock_interruptible(struct mutex *lock);
  6937. +extern int __lockfunc _mutex_lock_killable(struct mutex *lock);
  6938. +extern void __lockfunc _mutex_lock_nested(struct mutex *lock, int subclass);
  6939. +extern void __lockfunc _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest_lock);
  6940. +extern int __lockfunc _mutex_lock_interruptible_nested(struct mutex *lock, int subclass);
  6941. +extern int __lockfunc _mutex_lock_killable_nested(struct mutex *lock, int subclass);
  6942. +extern int __lockfunc _mutex_trylock(struct mutex *lock);
  6943. +extern void __lockfunc _mutex_unlock(struct mutex *lock);
  6944. +
  6945. +#define mutex_is_locked(l) rt_mutex_is_locked(&(l)->lock)
  6946. +#define mutex_lock(l) _mutex_lock(l)
  6947. +#define mutex_lock_interruptible(l) _mutex_lock_interruptible(l)
  6948. +#define mutex_lock_killable(l) _mutex_lock_killable(l)
  6949. +#define mutex_trylock(l) _mutex_trylock(l)
  6950. +#define mutex_unlock(l) _mutex_unlock(l)
  6951. +
  6952. +#ifdef CONFIG_DEBUG_MUTEXES
  6953. +#define mutex_destroy(l) rt_mutex_destroy(&(l)->lock)
  6954. +#else
  6955. +static inline void mutex_destroy(struct mutex *lock) {}
  6956. +#endif
  6957. +
  6958. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  6959. +# define mutex_lock_nested(l, s) _mutex_lock_nested(l, s)
  6960. +# define mutex_lock_interruptible_nested(l, s) \
  6961. + _mutex_lock_interruptible_nested(l, s)
  6962. +# define mutex_lock_killable_nested(l, s) \
  6963. + _mutex_lock_killable_nested(l, s)
  6964. +
  6965. +# define mutex_lock_nest_lock(lock, nest_lock) \
  6966. +do { \
  6967. + typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
  6968. + _mutex_lock_nest_lock(lock, &(nest_lock)->dep_map); \
  6969. +} while (0)
  6970. +
  6971. +#else
  6972. +# define mutex_lock_nested(l, s) _mutex_lock(l)
  6973. +# define mutex_lock_interruptible_nested(l, s) \
  6974. + _mutex_lock_interruptible(l)
  6975. +# define mutex_lock_killable_nested(l, s) \
  6976. + _mutex_lock_killable(l)
  6977. +# define mutex_lock_nest_lock(lock, nest_lock) mutex_lock(lock)
  6978. +#endif
  6979. +
  6980. +# define mutex_init(mutex) \
  6981. +do { \
  6982. + static struct lock_class_key __key; \
  6983. + \
  6984. + rt_mutex_init(&(mutex)->lock); \
  6985. + __mutex_do_init((mutex), #mutex, &__key); \
  6986. +} while (0)
  6987. +
  6988. +# define __mutex_init(mutex, name, key) \
  6989. +do { \
  6990. + rt_mutex_init(&(mutex)->lock); \
  6991. + __mutex_do_init((mutex), name, key); \
  6992. +} while (0)
  6993. +
  6994. +#endif
  6995. diff -Nur linux-4.9.28.orig/include/linux/netdevice.h linux-4.9.28/include/linux/netdevice.h
  6996. --- linux-4.9.28.orig/include/linux/netdevice.h 2017-05-14 14:00:37.000000000 +0200
  6997. +++ linux-4.9.28/include/linux/netdevice.h 2017-05-19 03:37:25.170176339 +0200
  6998. @@ -396,7 +396,19 @@
  6999. typedef rx_handler_result_t rx_handler_func_t(struct sk_buff **pskb);
  7000. void __napi_schedule(struct napi_struct *n);
  7001. +
  7002. +/*
  7003. + * When PREEMPT_RT_FULL is defined, all device interrupt handlers
  7004. + * run as threads, and they can also be preempted (without PREEMPT_RT
  7005. + * interrupt threads can not be preempted). Which means that calling
  7006. + * __napi_schedule_irqoff() from an interrupt handler can be preempted
  7007. + * and can corrupt the napi->poll_list.
  7008. + */
  7009. +#ifdef CONFIG_PREEMPT_RT_FULL
  7010. +#define __napi_schedule_irqoff(n) __napi_schedule(n)
  7011. +#else
  7012. void __napi_schedule_irqoff(struct napi_struct *n);
  7013. +#endif
  7014. static inline bool napi_disable_pending(struct napi_struct *n)
  7015. {
  7016. @@ -2463,14 +2475,53 @@
  7017. void synchronize_net(void);
  7018. int init_dummy_netdev(struct net_device *dev);
  7019. -DECLARE_PER_CPU(int, xmit_recursion);
  7020. #define XMIT_RECURSION_LIMIT 10
  7021. +#ifdef CONFIG_PREEMPT_RT_FULL
  7022. +static inline int dev_recursion_level(void)
  7023. +{
  7024. + return current->xmit_recursion;
  7025. +}
  7026. +
  7027. +static inline int xmit_rec_read(void)
  7028. +{
  7029. + return current->xmit_recursion;
  7030. +}
  7031. +
  7032. +static inline void xmit_rec_inc(void)
  7033. +{
  7034. + current->xmit_recursion++;
  7035. +}
  7036. +
  7037. +static inline void xmit_rec_dec(void)
  7038. +{
  7039. + current->xmit_recursion--;
  7040. +}
  7041. +
  7042. +#else
  7043. +
  7044. +DECLARE_PER_CPU(int, xmit_recursion);
  7045. static inline int dev_recursion_level(void)
  7046. {
  7047. return this_cpu_read(xmit_recursion);
  7048. }
  7049. +static inline int xmit_rec_read(void)
  7050. +{
  7051. + return __this_cpu_read(xmit_recursion);
  7052. +}
  7053. +
  7054. +static inline void xmit_rec_inc(void)
  7055. +{
  7056. + __this_cpu_inc(xmit_recursion);
  7057. +}
  7058. +
  7059. +static inline void xmit_rec_dec(void)
  7060. +{
  7061. + __this_cpu_dec(xmit_recursion);
  7062. +}
  7063. +#endif
  7064. +
  7065. struct net_device *dev_get_by_index(struct net *net, int ifindex);
  7066. struct net_device *__dev_get_by_index(struct net *net, int ifindex);
  7067. struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex);
  7068. @@ -2855,6 +2906,7 @@
  7069. unsigned int dropped;
  7070. struct sk_buff_head input_pkt_queue;
  7071. struct napi_struct backlog;
  7072. + struct sk_buff_head tofree_queue;
  7073. };
  7074. diff -Nur linux-4.9.28.orig/include/linux/netfilter/x_tables.h linux-4.9.28/include/linux/netfilter/x_tables.h
  7075. --- linux-4.9.28.orig/include/linux/netfilter/x_tables.h 2017-05-14 14:00:37.000000000 +0200
  7076. +++ linux-4.9.28/include/linux/netfilter/x_tables.h 2017-05-19 03:37:25.170176339 +0200
  7077. @@ -4,6 +4,7 @@
  7078. #include <linux/netdevice.h>
  7079. #include <linux/static_key.h>
  7080. +#include <linux/locallock.h>
  7081. #include <uapi/linux/netfilter/x_tables.h>
  7082. /* Test a struct->invflags and a boolean for inequality */
  7083. @@ -300,6 +301,8 @@
  7084. */
  7085. DECLARE_PER_CPU(seqcount_t, xt_recseq);
  7086. +DECLARE_LOCAL_IRQ_LOCK(xt_write_lock);
  7087. +
  7088. /* xt_tee_enabled - true if x_tables needs to handle reentrancy
  7089. *
  7090. * Enabled if current ip(6)tables ruleset has at least one -j TEE rule.
  7091. @@ -320,6 +323,9 @@
  7092. {
  7093. unsigned int addend;
  7094. + /* RT protection */
  7095. + local_lock(xt_write_lock);
  7096. +
  7097. /*
  7098. * Low order bit of sequence is set if we already
  7099. * called xt_write_recseq_begin().
  7100. @@ -350,6 +356,7 @@
  7101. /* this is kind of a write_seqcount_end(), but addend is 0 or 1 */
  7102. smp_wmb();
  7103. __this_cpu_add(xt_recseq.sequence, addend);
  7104. + local_unlock(xt_write_lock);
  7105. }
  7106. /*
  7107. diff -Nur linux-4.9.28.orig/include/linux/nfs_fs.h linux-4.9.28/include/linux/nfs_fs.h
  7108. --- linux-4.9.28.orig/include/linux/nfs_fs.h 2017-05-14 14:00:37.000000000 +0200
  7109. +++ linux-4.9.28/include/linux/nfs_fs.h 2017-05-19 03:37:25.170176339 +0200
  7110. @@ -165,7 +165,11 @@
  7111. /* Readers: in-flight sillydelete RPC calls */
  7112. /* Writers: rmdir */
  7113. +#ifdef CONFIG_PREEMPT_RT_BASE
  7114. + struct semaphore rmdir_sem;
  7115. +#else
  7116. struct rw_semaphore rmdir_sem;
  7117. +#endif
  7118. #if IS_ENABLED(CONFIG_NFS_V4)
  7119. struct nfs4_cached_acl *nfs4_acl;
  7120. diff -Nur linux-4.9.28.orig/include/linux/nfs_xdr.h linux-4.9.28/include/linux/nfs_xdr.h
  7121. --- linux-4.9.28.orig/include/linux/nfs_xdr.h 2017-05-14 14:00:37.000000000 +0200
  7122. +++ linux-4.9.28/include/linux/nfs_xdr.h 2017-05-19 03:37:25.170176339 +0200
  7123. @@ -1490,7 +1490,7 @@
  7124. struct nfs_removeargs args;
  7125. struct nfs_removeres res;
  7126. struct dentry *dentry;
  7127. - wait_queue_head_t wq;
  7128. + struct swait_queue_head wq;
  7129. struct rpc_cred *cred;
  7130. struct nfs_fattr dir_attr;
  7131. long timeout;
  7132. diff -Nur linux-4.9.28.orig/include/linux/notifier.h linux-4.9.28/include/linux/notifier.h
  7133. --- linux-4.9.28.orig/include/linux/notifier.h 2017-05-14 14:00:37.000000000 +0200
  7134. +++ linux-4.9.28/include/linux/notifier.h 2017-05-19 03:37:25.170176339 +0200
  7135. @@ -6,7 +6,7 @@
  7136. *
  7137. * Alan Cox <Alan.Cox@linux.org>
  7138. */
  7139. -
  7140. +
  7141. #ifndef _LINUX_NOTIFIER_H
  7142. #define _LINUX_NOTIFIER_H
  7143. #include <linux/errno.h>
  7144. @@ -42,9 +42,7 @@
  7145. * in srcu_notifier_call_chain(): no cache bounces and no memory barriers.
  7146. * As compensation, srcu_notifier_chain_unregister() is rather expensive.
  7147. * SRCU notifier chains should be used when the chain will be called very
  7148. - * often but notifier_blocks will seldom be removed. Also, SRCU notifier
  7149. - * chains are slightly more difficult to use because they require special
  7150. - * runtime initialization.
  7151. + * often but notifier_blocks will seldom be removed.
  7152. */
  7153. struct notifier_block;
  7154. @@ -90,7 +88,7 @@
  7155. (name)->head = NULL; \
  7156. } while (0)
  7157. -/* srcu_notifier_heads must be initialized and cleaned up dynamically */
  7158. +/* srcu_notifier_heads must be cleaned up dynamically */
  7159. extern void srcu_init_notifier_head(struct srcu_notifier_head *nh);
  7160. #define srcu_cleanup_notifier_head(name) \
  7161. cleanup_srcu_struct(&(name)->srcu);
  7162. @@ -103,7 +101,13 @@
  7163. .head = NULL }
  7164. #define RAW_NOTIFIER_INIT(name) { \
  7165. .head = NULL }
  7166. -/* srcu_notifier_heads cannot be initialized statically */
  7167. +
  7168. +#define SRCU_NOTIFIER_INIT(name, pcpu) \
  7169. + { \
  7170. + .mutex = __MUTEX_INITIALIZER(name.mutex), \
  7171. + .head = NULL, \
  7172. + .srcu = __SRCU_STRUCT_INIT(name.srcu, pcpu), \
  7173. + }
  7174. #define ATOMIC_NOTIFIER_HEAD(name) \
  7175. struct atomic_notifier_head name = \
  7176. @@ -115,6 +119,18 @@
  7177. struct raw_notifier_head name = \
  7178. RAW_NOTIFIER_INIT(name)
  7179. +#define _SRCU_NOTIFIER_HEAD(name, mod) \
  7180. + static DEFINE_PER_CPU(struct srcu_struct_array, \
  7181. + name##_head_srcu_array); \
  7182. + mod struct srcu_notifier_head name = \
  7183. + SRCU_NOTIFIER_INIT(name, name##_head_srcu_array)
  7184. +
  7185. +#define SRCU_NOTIFIER_HEAD(name) \
  7186. + _SRCU_NOTIFIER_HEAD(name, )
  7187. +
  7188. +#define SRCU_NOTIFIER_HEAD_STATIC(name) \
  7189. + _SRCU_NOTIFIER_HEAD(name, static)
  7190. +
  7191. #ifdef __KERNEL__
  7192. extern int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
  7193. @@ -184,12 +200,12 @@
  7194. /*
  7195. * Declared notifiers so far. I can imagine quite a few more chains
  7196. - * over time (eg laptop power reset chains, reboot chain (to clean
  7197. + * over time (eg laptop power reset chains, reboot chain (to clean
  7198. * device units up), device [un]mount chain, module load/unload chain,
  7199. - * low memory chain, screenblank chain (for plug in modular screenblankers)
  7200. + * low memory chain, screenblank chain (for plug in modular screenblankers)
  7201. * VC switch chains (for loadable kernel svgalib VC switch helpers) etc...
  7202. */
  7203. -
  7204. +
  7205. /* CPU notfiers are defined in include/linux/cpu.h. */
  7206. /* netdevice notifiers are defined in include/linux/netdevice.h */
  7207. diff -Nur linux-4.9.28.orig/include/linux/percpu.h linux-4.9.28/include/linux/percpu.h
  7208. --- linux-4.9.28.orig/include/linux/percpu.h 2017-05-14 14:00:37.000000000 +0200
  7209. +++ linux-4.9.28/include/linux/percpu.h 2017-05-19 03:37:25.170176339 +0200
  7210. @@ -18,6 +18,35 @@
  7211. #define PERCPU_MODULE_RESERVE 0
  7212. #endif
  7213. +#ifdef CONFIG_PREEMPT_RT_FULL
  7214. +
  7215. +#define get_local_var(var) (*({ \
  7216. + migrate_disable(); \
  7217. + this_cpu_ptr(&var); }))
  7218. +
  7219. +#define put_local_var(var) do { \
  7220. + (void)&(var); \
  7221. + migrate_enable(); \
  7222. +} while (0)
  7223. +
  7224. +# define get_local_ptr(var) ({ \
  7225. + migrate_disable(); \
  7226. + this_cpu_ptr(var); })
  7227. +
  7228. +# define put_local_ptr(var) do { \
  7229. + (void)(var); \
  7230. + migrate_enable(); \
  7231. +} while (0)
  7232. +
  7233. +#else
  7234. +
  7235. +#define get_local_var(var) get_cpu_var(var)
  7236. +#define put_local_var(var) put_cpu_var(var)
  7237. +#define get_local_ptr(var) get_cpu_ptr(var)
  7238. +#define put_local_ptr(var) put_cpu_ptr(var)
  7239. +
  7240. +#endif
  7241. +
  7242. /* minimum unit size, also is the maximum supported allocation size */
  7243. #define PCPU_MIN_UNIT_SIZE PFN_ALIGN(32 << 10)
  7244. @@ -110,6 +139,7 @@
  7245. #endif
  7246. extern void __percpu *__alloc_reserved_percpu(size_t size, size_t align);
  7247. +extern bool __is_kernel_percpu_address(unsigned long addr, unsigned long *can_addr);
  7248. extern bool is_kernel_percpu_address(unsigned long addr);
  7249. #if !defined(CONFIG_SMP) || !defined(CONFIG_HAVE_SETUP_PER_CPU_AREA)
  7250. diff -Nur linux-4.9.28.orig/include/linux/percpu-rwsem.h linux-4.9.28/include/linux/percpu-rwsem.h
  7251. --- linux-4.9.28.orig/include/linux/percpu-rwsem.h 2017-05-14 14:00:37.000000000 +0200
  7252. +++ linux-4.9.28/include/linux/percpu-rwsem.h 2017-05-19 03:37:25.170176339 +0200
  7253. @@ -4,7 +4,7 @@
  7254. #include <linux/atomic.h>
  7255. #include <linux/rwsem.h>
  7256. #include <linux/percpu.h>
  7257. -#include <linux/wait.h>
  7258. +#include <linux/swait.h>
  7259. #include <linux/rcu_sync.h>
  7260. #include <linux/lockdep.h>
  7261. @@ -12,7 +12,7 @@
  7262. struct rcu_sync rss;
  7263. unsigned int __percpu *read_count;
  7264. struct rw_semaphore rw_sem;
  7265. - wait_queue_head_t writer;
  7266. + struct swait_queue_head writer;
  7267. int readers_block;
  7268. };
  7269. @@ -22,13 +22,13 @@
  7270. .rss = __RCU_SYNC_INITIALIZER(name.rss, RCU_SCHED_SYNC), \
  7271. .read_count = &__percpu_rwsem_rc_##name, \
  7272. .rw_sem = __RWSEM_INITIALIZER(name.rw_sem), \
  7273. - .writer = __WAIT_QUEUE_HEAD_INITIALIZER(name.writer), \
  7274. + .writer = __SWAIT_QUEUE_HEAD_INITIALIZER(name.writer), \
  7275. }
  7276. extern int __percpu_down_read(struct percpu_rw_semaphore *, int);
  7277. extern void __percpu_up_read(struct percpu_rw_semaphore *);
  7278. -static inline void percpu_down_read_preempt_disable(struct percpu_rw_semaphore *sem)
  7279. +static inline void percpu_down_read(struct percpu_rw_semaphore *sem)
  7280. {
  7281. might_sleep();
  7282. @@ -46,16 +46,10 @@
  7283. __this_cpu_inc(*sem->read_count);
  7284. if (unlikely(!rcu_sync_is_idle(&sem->rss)))
  7285. __percpu_down_read(sem, false); /* Unconditional memory barrier */
  7286. - barrier();
  7287. /*
  7288. - * The barrier() prevents the compiler from
  7289. + * The preempt_enable() prevents the compiler from
  7290. * bleeding the critical section out.
  7291. */
  7292. -}
  7293. -
  7294. -static inline void percpu_down_read(struct percpu_rw_semaphore *sem)
  7295. -{
  7296. - percpu_down_read_preempt_disable(sem);
  7297. preempt_enable();
  7298. }
  7299. @@ -82,13 +76,9 @@
  7300. return ret;
  7301. }
  7302. -static inline void percpu_up_read_preempt_enable(struct percpu_rw_semaphore *sem)
  7303. +static inline void percpu_up_read(struct percpu_rw_semaphore *sem)
  7304. {
  7305. - /*
  7306. - * The barrier() prevents the compiler from
  7307. - * bleeding the critical section out.
  7308. - */
  7309. - barrier();
  7310. + preempt_disable();
  7311. /*
  7312. * Same as in percpu_down_read().
  7313. */
  7314. @@ -101,12 +91,6 @@
  7315. rwsem_release(&sem->rw_sem.dep_map, 1, _RET_IP_);
  7316. }
  7317. -static inline void percpu_up_read(struct percpu_rw_semaphore *sem)
  7318. -{
  7319. - preempt_disable();
  7320. - percpu_up_read_preempt_enable(sem);
  7321. -}
  7322. -
  7323. extern void percpu_down_write(struct percpu_rw_semaphore *);
  7324. extern void percpu_up_write(struct percpu_rw_semaphore *);
  7325. diff -Nur linux-4.9.28.orig/include/linux/pid.h linux-4.9.28/include/linux/pid.h
  7326. --- linux-4.9.28.orig/include/linux/pid.h 2017-05-14 14:00:37.000000000 +0200
  7327. +++ linux-4.9.28/include/linux/pid.h 2017-05-19 03:37:25.170176339 +0200
  7328. @@ -2,6 +2,7 @@
  7329. #define _LINUX_PID_H
  7330. #include <linux/rcupdate.h>
  7331. +#include <linux/atomic.h>
  7332. enum pid_type
  7333. {
  7334. diff -Nur linux-4.9.28.orig/include/linux/preempt.h linux-4.9.28/include/linux/preempt.h
  7335. --- linux-4.9.28.orig/include/linux/preempt.h 2017-05-14 14:00:37.000000000 +0200
  7336. +++ linux-4.9.28/include/linux/preempt.h 2017-05-19 03:37:25.170176339 +0200
  7337. @@ -50,7 +50,11 @@
  7338. #define HARDIRQ_OFFSET (1UL << HARDIRQ_SHIFT)
  7339. #define NMI_OFFSET (1UL << NMI_SHIFT)
  7340. -#define SOFTIRQ_DISABLE_OFFSET (2 * SOFTIRQ_OFFSET)
  7341. +#ifndef CONFIG_PREEMPT_RT_FULL
  7342. +# define SOFTIRQ_DISABLE_OFFSET (2 * SOFTIRQ_OFFSET)
  7343. +#else
  7344. +# define SOFTIRQ_DISABLE_OFFSET (0)
  7345. +#endif
  7346. /* We use the MSB mostly because its available */
  7347. #define PREEMPT_NEED_RESCHED 0x80000000
  7348. @@ -59,9 +63,15 @@
  7349. #include <asm/preempt.h>
  7350. #define hardirq_count() (preempt_count() & HARDIRQ_MASK)
  7351. -#define softirq_count() (preempt_count() & SOFTIRQ_MASK)
  7352. #define irq_count() (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_MASK \
  7353. | NMI_MASK))
  7354. +#ifndef CONFIG_PREEMPT_RT_FULL
  7355. +# define softirq_count() (preempt_count() & SOFTIRQ_MASK)
  7356. +# define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET)
  7357. +#else
  7358. +# define softirq_count() (0UL)
  7359. +extern int in_serving_softirq(void);
  7360. +#endif
  7361. /*
  7362. * Are we doing bottom half or hardware interrupt processing?
  7363. @@ -72,7 +82,6 @@
  7364. #define in_irq() (hardirq_count())
  7365. #define in_softirq() (softirq_count())
  7366. #define in_interrupt() (irq_count())
  7367. -#define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET)
  7368. /*
  7369. * Are we in NMI context?
  7370. @@ -91,7 +100,11 @@
  7371. /*
  7372. * The preempt_count offset after spin_lock()
  7373. */
  7374. +#if !defined(CONFIG_PREEMPT_RT_FULL)
  7375. #define PREEMPT_LOCK_OFFSET PREEMPT_DISABLE_OFFSET
  7376. +#else
  7377. +#define PREEMPT_LOCK_OFFSET 0
  7378. +#endif
  7379. /*
  7380. * The preempt_count offset needed for things like:
  7381. @@ -140,6 +153,20 @@
  7382. #define preempt_count_inc() preempt_count_add(1)
  7383. #define preempt_count_dec() preempt_count_sub(1)
  7384. +#ifdef CONFIG_PREEMPT_LAZY
  7385. +#define add_preempt_lazy_count(val) do { preempt_lazy_count() += (val); } while (0)
  7386. +#define sub_preempt_lazy_count(val) do { preempt_lazy_count() -= (val); } while (0)
  7387. +#define inc_preempt_lazy_count() add_preempt_lazy_count(1)
  7388. +#define dec_preempt_lazy_count() sub_preempt_lazy_count(1)
  7389. +#define preempt_lazy_count() (current_thread_info()->preempt_lazy_count)
  7390. +#else
  7391. +#define add_preempt_lazy_count(val) do { } while (0)
  7392. +#define sub_preempt_lazy_count(val) do { } while (0)
  7393. +#define inc_preempt_lazy_count() do { } while (0)
  7394. +#define dec_preempt_lazy_count() do { } while (0)
  7395. +#define preempt_lazy_count() (0)
  7396. +#endif
  7397. +
  7398. #ifdef CONFIG_PREEMPT_COUNT
  7399. #define preempt_disable() \
  7400. @@ -148,13 +175,25 @@
  7401. barrier(); \
  7402. } while (0)
  7403. +#define preempt_lazy_disable() \
  7404. +do { \
  7405. + inc_preempt_lazy_count(); \
  7406. + barrier(); \
  7407. +} while (0)
  7408. +
  7409. #define sched_preempt_enable_no_resched() \
  7410. do { \
  7411. barrier(); \
  7412. preempt_count_dec(); \
  7413. } while (0)
  7414. -#define preempt_enable_no_resched() sched_preempt_enable_no_resched()
  7415. +#ifdef CONFIG_PREEMPT_RT_BASE
  7416. +# define preempt_enable_no_resched() sched_preempt_enable_no_resched()
  7417. +# define preempt_check_resched_rt() preempt_check_resched()
  7418. +#else
  7419. +# define preempt_enable_no_resched() preempt_enable()
  7420. +# define preempt_check_resched_rt() barrier();
  7421. +#endif
  7422. #define preemptible() (preempt_count() == 0 && !irqs_disabled())
  7423. @@ -179,6 +218,13 @@
  7424. __preempt_schedule(); \
  7425. } while (0)
  7426. +#define preempt_lazy_enable() \
  7427. +do { \
  7428. + dec_preempt_lazy_count(); \
  7429. + barrier(); \
  7430. + preempt_check_resched(); \
  7431. +} while (0)
  7432. +
  7433. #else /* !CONFIG_PREEMPT */
  7434. #define preempt_enable() \
  7435. do { \
  7436. @@ -224,6 +270,7 @@
  7437. #define preempt_disable_notrace() barrier()
  7438. #define preempt_enable_no_resched_notrace() barrier()
  7439. #define preempt_enable_notrace() barrier()
  7440. +#define preempt_check_resched_rt() barrier()
  7441. #define preemptible() 0
  7442. #endif /* CONFIG_PREEMPT_COUNT */
  7443. @@ -244,10 +291,31 @@
  7444. } while (0)
  7445. #define preempt_fold_need_resched() \
  7446. do { \
  7447. - if (tif_need_resched()) \
  7448. + if (tif_need_resched_now()) \
  7449. set_preempt_need_resched(); \
  7450. } while (0)
  7451. +#ifdef CONFIG_PREEMPT_RT_FULL
  7452. +# define preempt_disable_rt() preempt_disable()
  7453. +# define preempt_enable_rt() preempt_enable()
  7454. +# define preempt_disable_nort() barrier()
  7455. +# define preempt_enable_nort() barrier()
  7456. +# ifdef CONFIG_SMP
  7457. + extern void migrate_disable(void);
  7458. + extern void migrate_enable(void);
  7459. +# else /* CONFIG_SMP */
  7460. +# define migrate_disable() barrier()
  7461. +# define migrate_enable() barrier()
  7462. +# endif /* CONFIG_SMP */
  7463. +#else
  7464. +# define preempt_disable_rt() barrier()
  7465. +# define preempt_enable_rt() barrier()
  7466. +# define preempt_disable_nort() preempt_disable()
  7467. +# define preempt_enable_nort() preempt_enable()
  7468. +# define migrate_disable() preempt_disable()
  7469. +# define migrate_enable() preempt_enable()
  7470. +#endif
  7471. +
  7472. #ifdef CONFIG_PREEMPT_NOTIFIERS
  7473. struct preempt_notifier;
  7474. diff -Nur linux-4.9.28.orig/include/linux/printk.h linux-4.9.28/include/linux/printk.h
  7475. --- linux-4.9.28.orig/include/linux/printk.h 2017-05-14 14:00:37.000000000 +0200
  7476. +++ linux-4.9.28/include/linux/printk.h 2017-05-19 03:37:25.170176339 +0200
  7477. @@ -126,9 +126,11 @@
  7478. #ifdef CONFIG_EARLY_PRINTK
  7479. extern asmlinkage __printf(1, 2)
  7480. void early_printk(const char *fmt, ...);
  7481. +extern void printk_kill(void);
  7482. #else
  7483. static inline __printf(1, 2) __cold
  7484. void early_printk(const char *s, ...) { }
  7485. +static inline void printk_kill(void) { }
  7486. #endif
  7487. #ifdef CONFIG_PRINTK_NMI
  7488. diff -Nur linux-4.9.28.orig/include/linux/radix-tree.h linux-4.9.28/include/linux/radix-tree.h
  7489. --- linux-4.9.28.orig/include/linux/radix-tree.h 2017-05-14 14:00:37.000000000 +0200
  7490. +++ linux-4.9.28/include/linux/radix-tree.h 2017-05-19 03:37:25.170176339 +0200
  7491. @@ -292,6 +292,8 @@
  7492. int radix_tree_preload(gfp_t gfp_mask);
  7493. int radix_tree_maybe_preload(gfp_t gfp_mask);
  7494. int radix_tree_maybe_preload_order(gfp_t gfp_mask, int order);
  7495. +void radix_tree_preload_end(void);
  7496. +
  7497. void radix_tree_init(void);
  7498. void *radix_tree_tag_set(struct radix_tree_root *root,
  7499. unsigned long index, unsigned int tag);
  7500. @@ -314,11 +316,6 @@
  7501. int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag);
  7502. unsigned long radix_tree_locate_item(struct radix_tree_root *root, void *item);
  7503. -static inline void radix_tree_preload_end(void)
  7504. -{
  7505. - preempt_enable();
  7506. -}
  7507. -
  7508. /**
  7509. * struct radix_tree_iter - radix tree iterator state
  7510. *
  7511. diff -Nur linux-4.9.28.orig/include/linux/random.h linux-4.9.28/include/linux/random.h
  7512. --- linux-4.9.28.orig/include/linux/random.h 2017-05-14 14:00:37.000000000 +0200
  7513. +++ linux-4.9.28/include/linux/random.h 2017-05-19 03:37:25.170176339 +0200
  7514. @@ -31,7 +31,7 @@
  7515. extern void add_input_randomness(unsigned int type, unsigned int code,
  7516. unsigned int value) __latent_entropy;
  7517. -extern void add_interrupt_randomness(int irq, int irq_flags) __latent_entropy;
  7518. +extern void add_interrupt_randomness(int irq, int irq_flags, __u64 ip) __latent_entropy;
  7519. extern void get_random_bytes(void *buf, int nbytes);
  7520. extern int add_random_ready_callback(struct random_ready_callback *rdy);
  7521. diff -Nur linux-4.9.28.orig/include/linux/rbtree_augmented.h linux-4.9.28/include/linux/rbtree_augmented.h
  7522. --- linux-4.9.28.orig/include/linux/rbtree_augmented.h 2017-05-14 14:00:37.000000000 +0200
  7523. +++ linux-4.9.28/include/linux/rbtree_augmented.h 2017-05-19 03:37:25.170176339 +0200
  7524. @@ -26,6 +26,7 @@
  7525. #include <linux/compiler.h>
  7526. #include <linux/rbtree.h>
  7527. +#include <linux/rcupdate.h>
  7528. /*
  7529. * Please note - only struct rb_augment_callbacks and the prototypes for
  7530. diff -Nur linux-4.9.28.orig/include/linux/rbtree.h linux-4.9.28/include/linux/rbtree.h
  7531. --- linux-4.9.28.orig/include/linux/rbtree.h 2017-05-14 14:00:37.000000000 +0200
  7532. +++ linux-4.9.28/include/linux/rbtree.h 2017-05-19 03:37:25.170176339 +0200
  7533. @@ -31,7 +31,7 @@
  7534. #include <linux/kernel.h>
  7535. #include <linux/stddef.h>
  7536. -#include <linux/rcupdate.h>
  7537. +#include <linux/rcu_assign_pointer.h>
  7538. struct rb_node {
  7539. unsigned long __rb_parent_color;
  7540. diff -Nur linux-4.9.28.orig/include/linux/rcu_assign_pointer.h linux-4.9.28/include/linux/rcu_assign_pointer.h
  7541. --- linux-4.9.28.orig/include/linux/rcu_assign_pointer.h 1970-01-01 01:00:00.000000000 +0100
  7542. +++ linux-4.9.28/include/linux/rcu_assign_pointer.h 2017-05-19 03:37:25.170176339 +0200
  7543. @@ -0,0 +1,54 @@
  7544. +#ifndef __LINUX_RCU_ASSIGN_POINTER_H__
  7545. +#define __LINUX_RCU_ASSIGN_POINTER_H__
  7546. +#include <linux/compiler.h>
  7547. +#include <asm/barrier.h>
  7548. +
  7549. +/**
  7550. + * RCU_INITIALIZER() - statically initialize an RCU-protected global variable
  7551. + * @v: The value to statically initialize with.
  7552. + */
  7553. +#define RCU_INITIALIZER(v) (typeof(*(v)) __force __rcu *)(v)
  7554. +
  7555. +/**
  7556. + * rcu_assign_pointer() - assign to RCU-protected pointer
  7557. + * @p: pointer to assign to
  7558. + * @v: value to assign (publish)
  7559. + *
  7560. + * Assigns the specified value to the specified RCU-protected
  7561. + * pointer, ensuring that any concurrent RCU readers will see
  7562. + * any prior initialization.
  7563. + *
  7564. + * Inserts memory barriers on architectures that require them
  7565. + * (which is most of them), and also prevents the compiler from
  7566. + * reordering the code that initializes the structure after the pointer
  7567. + * assignment. More importantly, this call documents which pointers
  7568. + * will be dereferenced by RCU read-side code.
  7569. + *
  7570. + * In some special cases, you may use RCU_INIT_POINTER() instead
  7571. + * of rcu_assign_pointer(). RCU_INIT_POINTER() is a bit faster due
  7572. + * to the fact that it does not constrain either the CPU or the compiler.
  7573. + * That said, using RCU_INIT_POINTER() when you should have used
  7574. + * rcu_assign_pointer() is a very bad thing that results in
  7575. + * impossible-to-diagnose memory corruption. So please be careful.
  7576. + * See the RCU_INIT_POINTER() comment header for details.
  7577. + *
  7578. + * Note that rcu_assign_pointer() evaluates each of its arguments only
  7579. + * once, appearances notwithstanding. One of the "extra" evaluations
  7580. + * is in typeof() and the other visible only to sparse (__CHECKER__),
  7581. + * neither of which actually execute the argument. As with most cpp
  7582. + * macros, this execute-arguments-only-once property is important, so
  7583. + * please be careful when making changes to rcu_assign_pointer() and the
  7584. + * other macros that it invokes.
  7585. + */
  7586. +#define rcu_assign_pointer(p, v) \
  7587. +({ \
  7588. + uintptr_t _r_a_p__v = (uintptr_t)(v); \
  7589. + \
  7590. + if (__builtin_constant_p(v) && (_r_a_p__v) == (uintptr_t)NULL) \
  7591. + WRITE_ONCE((p), (typeof(p))(_r_a_p__v)); \
  7592. + else \
  7593. + smp_store_release(&p, RCU_INITIALIZER((typeof(p))_r_a_p__v)); \
  7594. + _r_a_p__v; \
  7595. +})
  7596. +
  7597. +#endif
  7598. diff -Nur linux-4.9.28.orig/include/linux/rcupdate.h linux-4.9.28/include/linux/rcupdate.h
  7599. --- linux-4.9.28.orig/include/linux/rcupdate.h 2017-05-14 14:00:37.000000000 +0200
  7600. +++ linux-4.9.28/include/linux/rcupdate.h 2017-05-19 03:37:25.170176339 +0200
  7601. @@ -46,6 +46,7 @@
  7602. #include <linux/compiler.h>
  7603. #include <linux/ktime.h>
  7604. #include <linux/irqflags.h>
  7605. +#include <linux/rcu_assign_pointer.h>
  7606. #include <asm/barrier.h>
  7607. @@ -178,6 +179,9 @@
  7608. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  7609. +#ifdef CONFIG_PREEMPT_RT_FULL
  7610. +#define call_rcu_bh call_rcu
  7611. +#else
  7612. /**
  7613. * call_rcu_bh() - Queue an RCU for invocation after a quicker grace period.
  7614. * @head: structure to be used for queueing the RCU updates.
  7615. @@ -201,6 +205,7 @@
  7616. */
  7617. void call_rcu_bh(struct rcu_head *head,
  7618. rcu_callback_t func);
  7619. +#endif
  7620. /**
  7621. * call_rcu_sched() - Queue an RCU for invocation after sched grace period.
  7622. @@ -301,6 +306,11 @@
  7623. * types of kernel builds, the rcu_read_lock() nesting depth is unknowable.
  7624. */
  7625. #define rcu_preempt_depth() (current->rcu_read_lock_nesting)
  7626. +#ifndef CONFIG_PREEMPT_RT_FULL
  7627. +#define sched_rcu_preempt_depth() rcu_preempt_depth()
  7628. +#else
  7629. +static inline int sched_rcu_preempt_depth(void) { return 0; }
  7630. +#endif
  7631. #else /* #ifdef CONFIG_PREEMPT_RCU */
  7632. @@ -326,6 +336,8 @@
  7633. return 0;
  7634. }
  7635. +#define sched_rcu_preempt_depth() rcu_preempt_depth()
  7636. +
  7637. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  7638. /* Internal to kernel */
  7639. @@ -505,7 +517,14 @@
  7640. int debug_lockdep_rcu_enabled(void);
  7641. int rcu_read_lock_held(void);
  7642. +#ifdef CONFIG_PREEMPT_RT_FULL
  7643. +static inline int rcu_read_lock_bh_held(void)
  7644. +{
  7645. + return rcu_read_lock_held();
  7646. +}
  7647. +#else
  7648. int rcu_read_lock_bh_held(void);
  7649. +#endif
  7650. /**
  7651. * rcu_read_lock_sched_held() - might we be in RCU-sched read-side critical section?
  7652. @@ -626,54 +645,6 @@
  7653. })
  7654. /**
  7655. - * RCU_INITIALIZER() - statically initialize an RCU-protected global variable
  7656. - * @v: The value to statically initialize with.
  7657. - */
  7658. -#define RCU_INITIALIZER(v) (typeof(*(v)) __force __rcu *)(v)
  7659. -
  7660. -/**
  7661. - * rcu_assign_pointer() - assign to RCU-protected pointer
  7662. - * @p: pointer to assign to
  7663. - * @v: value to assign (publish)
  7664. - *
  7665. - * Assigns the specified value to the specified RCU-protected
  7666. - * pointer, ensuring that any concurrent RCU readers will see
  7667. - * any prior initialization.
  7668. - *
  7669. - * Inserts memory barriers on architectures that require them
  7670. - * (which is most of them), and also prevents the compiler from
  7671. - * reordering the code that initializes the structure after the pointer
  7672. - * assignment. More importantly, this call documents which pointers
  7673. - * will be dereferenced by RCU read-side code.
  7674. - *
  7675. - * In some special cases, you may use RCU_INIT_POINTER() instead
  7676. - * of rcu_assign_pointer(). RCU_INIT_POINTER() is a bit faster due
  7677. - * to the fact that it does not constrain either the CPU or the compiler.
  7678. - * That said, using RCU_INIT_POINTER() when you should have used
  7679. - * rcu_assign_pointer() is a very bad thing that results in
  7680. - * impossible-to-diagnose memory corruption. So please be careful.
  7681. - * See the RCU_INIT_POINTER() comment header for details.
  7682. - *
  7683. - * Note that rcu_assign_pointer() evaluates each of its arguments only
  7684. - * once, appearances notwithstanding. One of the "extra" evaluations
  7685. - * is in typeof() and the other visible only to sparse (__CHECKER__),
  7686. - * neither of which actually execute the argument. As with most cpp
  7687. - * macros, this execute-arguments-only-once property is important, so
  7688. - * please be careful when making changes to rcu_assign_pointer() and the
  7689. - * other macros that it invokes.
  7690. - */
  7691. -#define rcu_assign_pointer(p, v) \
  7692. -({ \
  7693. - uintptr_t _r_a_p__v = (uintptr_t)(v); \
  7694. - \
  7695. - if (__builtin_constant_p(v) && (_r_a_p__v) == (uintptr_t)NULL) \
  7696. - WRITE_ONCE((p), (typeof(p))(_r_a_p__v)); \
  7697. - else \
  7698. - smp_store_release(&p, RCU_INITIALIZER((typeof(p))_r_a_p__v)); \
  7699. - _r_a_p__v; \
  7700. -})
  7701. -
  7702. -/**
  7703. * rcu_access_pointer() - fetch RCU pointer with no dereferencing
  7704. * @p: The pointer to read
  7705. *
  7706. @@ -951,10 +922,14 @@
  7707. static inline void rcu_read_lock_bh(void)
  7708. {
  7709. local_bh_disable();
  7710. +#ifdef CONFIG_PREEMPT_RT_FULL
  7711. + rcu_read_lock();
  7712. +#else
  7713. __acquire(RCU_BH);
  7714. rcu_lock_acquire(&rcu_bh_lock_map);
  7715. RCU_LOCKDEP_WARN(!rcu_is_watching(),
  7716. "rcu_read_lock_bh() used illegally while idle");
  7717. +#endif
  7718. }
  7719. /*
  7720. @@ -964,10 +939,14 @@
  7721. */
  7722. static inline void rcu_read_unlock_bh(void)
  7723. {
  7724. +#ifdef CONFIG_PREEMPT_RT_FULL
  7725. + rcu_read_unlock();
  7726. +#else
  7727. RCU_LOCKDEP_WARN(!rcu_is_watching(),
  7728. "rcu_read_unlock_bh() used illegally while idle");
  7729. rcu_lock_release(&rcu_bh_lock_map);
  7730. __release(RCU_BH);
  7731. +#endif
  7732. local_bh_enable();
  7733. }
  7734. diff -Nur linux-4.9.28.orig/include/linux/rcutree.h linux-4.9.28/include/linux/rcutree.h
  7735. --- linux-4.9.28.orig/include/linux/rcutree.h 2017-05-14 14:00:37.000000000 +0200
  7736. +++ linux-4.9.28/include/linux/rcutree.h 2017-05-19 03:37:25.170176339 +0200
  7737. @@ -44,7 +44,11 @@
  7738. rcu_note_context_switch();
  7739. }
  7740. +#ifdef CONFIG_PREEMPT_RT_FULL
  7741. +# define synchronize_rcu_bh synchronize_rcu
  7742. +#else
  7743. void synchronize_rcu_bh(void);
  7744. +#endif
  7745. void synchronize_sched_expedited(void);
  7746. void synchronize_rcu_expedited(void);
  7747. @@ -72,7 +76,11 @@
  7748. }
  7749. void rcu_barrier(void);
  7750. +#ifdef CONFIG_PREEMPT_RT_FULL
  7751. +# define rcu_barrier_bh rcu_barrier
  7752. +#else
  7753. void rcu_barrier_bh(void);
  7754. +#endif
  7755. void rcu_barrier_sched(void);
  7756. unsigned long get_state_synchronize_rcu(void);
  7757. void cond_synchronize_rcu(unsigned long oldstate);
  7758. @@ -82,17 +90,14 @@
  7759. extern unsigned long rcutorture_testseq;
  7760. extern unsigned long rcutorture_vernum;
  7761. unsigned long rcu_batches_started(void);
  7762. -unsigned long rcu_batches_started_bh(void);
  7763. unsigned long rcu_batches_started_sched(void);
  7764. unsigned long rcu_batches_completed(void);
  7765. -unsigned long rcu_batches_completed_bh(void);
  7766. unsigned long rcu_batches_completed_sched(void);
  7767. unsigned long rcu_exp_batches_completed(void);
  7768. unsigned long rcu_exp_batches_completed_sched(void);
  7769. void show_rcu_gp_kthreads(void);
  7770. void rcu_force_quiescent_state(void);
  7771. -void rcu_bh_force_quiescent_state(void);
  7772. void rcu_sched_force_quiescent_state(void);
  7773. void rcu_idle_enter(void);
  7774. @@ -109,6 +114,16 @@
  7775. bool rcu_is_watching(void);
  7776. +#ifndef CONFIG_PREEMPT_RT_FULL
  7777. +void rcu_bh_force_quiescent_state(void);
  7778. +unsigned long rcu_batches_started_bh(void);
  7779. +unsigned long rcu_batches_completed_bh(void);
  7780. +#else
  7781. +# define rcu_bh_force_quiescent_state rcu_force_quiescent_state
  7782. +# define rcu_batches_completed_bh rcu_batches_completed
  7783. +# define rcu_batches_started_bh rcu_batches_completed
  7784. +#endif
  7785. +
  7786. void rcu_all_qs(void);
  7787. /* RCUtree hotplug events */
  7788. diff -Nur linux-4.9.28.orig/include/linux/rtmutex.h linux-4.9.28/include/linux/rtmutex.h
  7789. --- linux-4.9.28.orig/include/linux/rtmutex.h 2017-05-14 14:00:37.000000000 +0200
  7790. +++ linux-4.9.28/include/linux/rtmutex.h 2017-05-19 03:37:25.170176339 +0200
  7791. @@ -13,11 +13,15 @@
  7792. #define __LINUX_RT_MUTEX_H
  7793. #include <linux/linkage.h>
  7794. +#include <linux/spinlock_types_raw.h>
  7795. #include <linux/rbtree.h>
  7796. -#include <linux/spinlock_types.h>
  7797. extern int max_lock_depth; /* for sysctl */
  7798. +#ifdef CONFIG_DEBUG_MUTEXES
  7799. +#include <linux/debug_locks.h>
  7800. +#endif
  7801. +
  7802. /**
  7803. * The rt_mutex structure
  7804. *
  7805. @@ -31,8 +35,8 @@
  7806. struct rb_root waiters;
  7807. struct rb_node *waiters_leftmost;
  7808. struct task_struct *owner;
  7809. -#ifdef CONFIG_DEBUG_RT_MUTEXES
  7810. int save_state;
  7811. +#ifdef CONFIG_DEBUG_RT_MUTEXES
  7812. const char *name, *file;
  7813. int line;
  7814. void *magic;
  7815. @@ -55,22 +59,33 @@
  7816. # define rt_mutex_debug_check_no_locks_held(task) do { } while (0)
  7817. #endif
  7818. +# define rt_mutex_init(mutex) \
  7819. + do { \
  7820. + raw_spin_lock_init(&(mutex)->wait_lock); \
  7821. + __rt_mutex_init(mutex, #mutex); \
  7822. + } while (0)
  7823. +
  7824. #ifdef CONFIG_DEBUG_RT_MUTEXES
  7825. # define __DEBUG_RT_MUTEX_INITIALIZER(mutexname) \
  7826. , .name = #mutexname, .file = __FILE__, .line = __LINE__
  7827. -# define rt_mutex_init(mutex) __rt_mutex_init(mutex, __func__)
  7828. extern void rt_mutex_debug_task_free(struct task_struct *tsk);
  7829. #else
  7830. # define __DEBUG_RT_MUTEX_INITIALIZER(mutexname)
  7831. -# define rt_mutex_init(mutex) __rt_mutex_init(mutex, NULL)
  7832. # define rt_mutex_debug_task_free(t) do { } while (0)
  7833. #endif
  7834. -#define __RT_MUTEX_INITIALIZER(mutexname) \
  7835. - { .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(mutexname.wait_lock) \
  7836. +#define __RT_MUTEX_INITIALIZER_PLAIN(mutexname) \
  7837. + .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(mutexname.wait_lock) \
  7838. , .waiters = RB_ROOT \
  7839. , .owner = NULL \
  7840. - __DEBUG_RT_MUTEX_INITIALIZER(mutexname)}
  7841. + __DEBUG_RT_MUTEX_INITIALIZER(mutexname)
  7842. +
  7843. +#define __RT_MUTEX_INITIALIZER(mutexname) \
  7844. + { __RT_MUTEX_INITIALIZER_PLAIN(mutexname) }
  7845. +
  7846. +#define __RT_MUTEX_INITIALIZER_SAVE_STATE(mutexname) \
  7847. + { __RT_MUTEX_INITIALIZER_PLAIN(mutexname) \
  7848. + , .save_state = 1 }
  7849. #define DEFINE_RT_MUTEX(mutexname) \
  7850. struct rt_mutex mutexname = __RT_MUTEX_INITIALIZER(mutexname)
  7851. @@ -90,7 +105,9 @@
  7852. extern void rt_mutex_destroy(struct rt_mutex *lock);
  7853. extern void rt_mutex_lock(struct rt_mutex *lock);
  7854. +extern int rt_mutex_lock_state(struct rt_mutex *lock, int state);
  7855. extern int rt_mutex_lock_interruptible(struct rt_mutex *lock);
  7856. +extern int rt_mutex_lock_killable(struct rt_mutex *lock);
  7857. extern int rt_mutex_timed_lock(struct rt_mutex *lock,
  7858. struct hrtimer_sleeper *timeout);
  7859. diff -Nur linux-4.9.28.orig/include/linux/rwlock_rt.h linux-4.9.28/include/linux/rwlock_rt.h
  7860. --- linux-4.9.28.orig/include/linux/rwlock_rt.h 1970-01-01 01:00:00.000000000 +0100
  7861. +++ linux-4.9.28/include/linux/rwlock_rt.h 2017-05-19 03:37:25.170176339 +0200
  7862. @@ -0,0 +1,99 @@
  7863. +#ifndef __LINUX_RWLOCK_RT_H
  7864. +#define __LINUX_RWLOCK_RT_H
  7865. +
  7866. +#ifndef __LINUX_SPINLOCK_H
  7867. +#error Do not include directly. Use spinlock.h
  7868. +#endif
  7869. +
  7870. +#define rwlock_init(rwl) \
  7871. +do { \
  7872. + static struct lock_class_key __key; \
  7873. + \
  7874. + rt_mutex_init(&(rwl)->lock); \
  7875. + __rt_rwlock_init(rwl, #rwl, &__key); \
  7876. +} while (0)
  7877. +
  7878. +extern void __lockfunc rt_write_lock(rwlock_t *rwlock);
  7879. +extern void __lockfunc rt_read_lock(rwlock_t *rwlock);
  7880. +extern int __lockfunc rt_write_trylock(rwlock_t *rwlock);
  7881. +extern int __lockfunc rt_write_trylock_irqsave(rwlock_t *trylock, unsigned long *flags);
  7882. +extern int __lockfunc rt_read_trylock(rwlock_t *rwlock);
  7883. +extern void __lockfunc rt_write_unlock(rwlock_t *rwlock);
  7884. +extern void __lockfunc rt_read_unlock(rwlock_t *rwlock);
  7885. +extern unsigned long __lockfunc rt_write_lock_irqsave(rwlock_t *rwlock);
  7886. +extern unsigned long __lockfunc rt_read_lock_irqsave(rwlock_t *rwlock);
  7887. +extern void __rt_rwlock_init(rwlock_t *rwlock, char *name, struct lock_class_key *key);
  7888. +
  7889. +#define read_trylock(lock) __cond_lock(lock, rt_read_trylock(lock))
  7890. +#define write_trylock(lock) __cond_lock(lock, rt_write_trylock(lock))
  7891. +
  7892. +#define write_trylock_irqsave(lock, flags) \
  7893. + __cond_lock(lock, rt_write_trylock_irqsave(lock, &flags))
  7894. +
  7895. +#define read_lock_irqsave(lock, flags) \
  7896. + do { \
  7897. + typecheck(unsigned long, flags); \
  7898. + flags = rt_read_lock_irqsave(lock); \
  7899. + } while (0)
  7900. +
  7901. +#define write_lock_irqsave(lock, flags) \
  7902. + do { \
  7903. + typecheck(unsigned long, flags); \
  7904. + flags = rt_write_lock_irqsave(lock); \
  7905. + } while (0)
  7906. +
  7907. +#define read_lock(lock) rt_read_lock(lock)
  7908. +
  7909. +#define read_lock_bh(lock) \
  7910. + do { \
  7911. + local_bh_disable(); \
  7912. + rt_read_lock(lock); \
  7913. + } while (0)
  7914. +
  7915. +#define read_lock_irq(lock) read_lock(lock)
  7916. +
  7917. +#define write_lock(lock) rt_write_lock(lock)
  7918. +
  7919. +#define write_lock_bh(lock) \
  7920. + do { \
  7921. + local_bh_disable(); \
  7922. + rt_write_lock(lock); \
  7923. + } while (0)
  7924. +
  7925. +#define write_lock_irq(lock) write_lock(lock)
  7926. +
  7927. +#define read_unlock(lock) rt_read_unlock(lock)
  7928. +
  7929. +#define read_unlock_bh(lock) \
  7930. + do { \
  7931. + rt_read_unlock(lock); \
  7932. + local_bh_enable(); \
  7933. + } while (0)
  7934. +
  7935. +#define read_unlock_irq(lock) read_unlock(lock)
  7936. +
  7937. +#define write_unlock(lock) rt_write_unlock(lock)
  7938. +
  7939. +#define write_unlock_bh(lock) \
  7940. + do { \
  7941. + rt_write_unlock(lock); \
  7942. + local_bh_enable(); \
  7943. + } while (0)
  7944. +
  7945. +#define write_unlock_irq(lock) write_unlock(lock)
  7946. +
  7947. +#define read_unlock_irqrestore(lock, flags) \
  7948. + do { \
  7949. + typecheck(unsigned long, flags); \
  7950. + (void) flags; \
  7951. + rt_read_unlock(lock); \
  7952. + } while (0)
  7953. +
  7954. +#define write_unlock_irqrestore(lock, flags) \
  7955. + do { \
  7956. + typecheck(unsigned long, flags); \
  7957. + (void) flags; \
  7958. + rt_write_unlock(lock); \
  7959. + } while (0)
  7960. +
  7961. +#endif
  7962. diff -Nur linux-4.9.28.orig/include/linux/rwlock_types.h linux-4.9.28/include/linux/rwlock_types.h
  7963. --- linux-4.9.28.orig/include/linux/rwlock_types.h 2017-05-14 14:00:37.000000000 +0200
  7964. +++ linux-4.9.28/include/linux/rwlock_types.h 2017-05-19 03:37:25.170176339 +0200
  7965. @@ -1,6 +1,10 @@
  7966. #ifndef __LINUX_RWLOCK_TYPES_H
  7967. #define __LINUX_RWLOCK_TYPES_H
  7968. +#if !defined(__LINUX_SPINLOCK_TYPES_H)
  7969. +# error "Do not include directly, include spinlock_types.h"
  7970. +#endif
  7971. +
  7972. /*
  7973. * include/linux/rwlock_types.h - generic rwlock type definitions
  7974. * and initializers
  7975. diff -Nur linux-4.9.28.orig/include/linux/rwlock_types_rt.h linux-4.9.28/include/linux/rwlock_types_rt.h
  7976. --- linux-4.9.28.orig/include/linux/rwlock_types_rt.h 1970-01-01 01:00:00.000000000 +0100
  7977. +++ linux-4.9.28/include/linux/rwlock_types_rt.h 2017-05-19 03:37:25.170176339 +0200
  7978. @@ -0,0 +1,33 @@
  7979. +#ifndef __LINUX_RWLOCK_TYPES_RT_H
  7980. +#define __LINUX_RWLOCK_TYPES_RT_H
  7981. +
  7982. +#ifndef __LINUX_SPINLOCK_TYPES_H
  7983. +#error "Do not include directly. Include spinlock_types.h instead"
  7984. +#endif
  7985. +
  7986. +/*
  7987. + * rwlocks - rtmutex which allows single reader recursion
  7988. + */
  7989. +typedef struct {
  7990. + struct rt_mutex lock;
  7991. + int read_depth;
  7992. + unsigned int break_lock;
  7993. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  7994. + struct lockdep_map dep_map;
  7995. +#endif
  7996. +} rwlock_t;
  7997. +
  7998. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  7999. +# define RW_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }
  8000. +#else
  8001. +# define RW_DEP_MAP_INIT(lockname)
  8002. +#endif
  8003. +
  8004. +#define __RW_LOCK_UNLOCKED(name) \
  8005. + { .lock = __RT_MUTEX_INITIALIZER_SAVE_STATE(name.lock), \
  8006. + RW_DEP_MAP_INIT(name) }
  8007. +
  8008. +#define DEFINE_RWLOCK(name) \
  8009. + rwlock_t name = __RW_LOCK_UNLOCKED(name)
  8010. +
  8011. +#endif
  8012. diff -Nur linux-4.9.28.orig/include/linux/rwsem.h linux-4.9.28/include/linux/rwsem.h
  8013. --- linux-4.9.28.orig/include/linux/rwsem.h 2017-05-14 14:00:37.000000000 +0200
  8014. +++ linux-4.9.28/include/linux/rwsem.h 2017-05-19 03:37:25.174176493 +0200
  8015. @@ -19,6 +19,10 @@
  8016. #include <linux/osq_lock.h>
  8017. #endif
  8018. +#ifdef CONFIG_PREEMPT_RT_FULL
  8019. +#include <linux/rwsem_rt.h>
  8020. +#else /* PREEMPT_RT_FULL */
  8021. +
  8022. struct rw_semaphore;
  8023. #ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
  8024. @@ -106,6 +110,13 @@
  8025. return !list_empty(&sem->wait_list);
  8026. }
  8027. +#endif /* !PREEMPT_RT_FULL */
  8028. +
  8029. +/*
  8030. + * The functions below are the same for all rwsem implementations including
  8031. + * the RT specific variant.
  8032. + */
  8033. +
  8034. /*
  8035. * lock for reading
  8036. */
  8037. diff -Nur linux-4.9.28.orig/include/linux/rwsem_rt.h linux-4.9.28/include/linux/rwsem_rt.h
  8038. --- linux-4.9.28.orig/include/linux/rwsem_rt.h 1970-01-01 01:00:00.000000000 +0100
  8039. +++ linux-4.9.28/include/linux/rwsem_rt.h 2017-05-19 03:37:25.174176493 +0200
  8040. @@ -0,0 +1,67 @@
  8041. +#ifndef _LINUX_RWSEM_RT_H
  8042. +#define _LINUX_RWSEM_RT_H
  8043. +
  8044. +#ifndef _LINUX_RWSEM_H
  8045. +#error "Include rwsem.h"
  8046. +#endif
  8047. +
  8048. +#include <linux/rtmutex.h>
  8049. +#include <linux/swait.h>
  8050. +
  8051. +#define READER_BIAS (1U << 31)
  8052. +#define WRITER_BIAS (1U << 30)
  8053. +
  8054. +struct rw_semaphore {
  8055. + atomic_t readers;
  8056. + struct rt_mutex rtmutex;
  8057. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  8058. + struct lockdep_map dep_map;
  8059. +#endif
  8060. +};
  8061. +
  8062. +#define __RWSEM_INITIALIZER(name) \
  8063. +{ \
  8064. + .readers = ATOMIC_INIT(READER_BIAS), \
  8065. + .rtmutex = __RT_MUTEX_INITIALIZER(name.rtmutex), \
  8066. + RW_DEP_MAP_INIT(name) \
  8067. +}
  8068. +
  8069. +#define DECLARE_RWSEM(lockname) \
  8070. + struct rw_semaphore lockname = __RWSEM_INITIALIZER(lockname)
  8071. +
  8072. +extern void __rwsem_init(struct rw_semaphore *rwsem, const char *name,
  8073. + struct lock_class_key *key);
  8074. +
  8075. +#define __init_rwsem(sem, name, key) \
  8076. +do { \
  8077. + rt_mutex_init(&(sem)->rtmutex); \
  8078. + __rwsem_init((sem), (name), (key)); \
  8079. +} while (0)
  8080. +
  8081. +#define init_rwsem(sem) \
  8082. +do { \
  8083. + static struct lock_class_key __key; \
  8084. + \
  8085. + __init_rwsem((sem), #sem, &__key); \
  8086. +} while (0)
  8087. +
  8088. +static inline int rwsem_is_locked(struct rw_semaphore *sem)
  8089. +{
  8090. + return atomic_read(&sem->readers) != READER_BIAS;
  8091. +}
  8092. +
  8093. +static inline int rwsem_is_contended(struct rw_semaphore *sem)
  8094. +{
  8095. + return atomic_read(&sem->readers) > 0;
  8096. +}
  8097. +
  8098. +extern void __down_read(struct rw_semaphore *sem);
  8099. +extern int __down_read_trylock(struct rw_semaphore *sem);
  8100. +extern void __down_write(struct rw_semaphore *sem);
  8101. +extern int __must_check __down_write_killable(struct rw_semaphore *sem);
  8102. +extern int __down_write_trylock(struct rw_semaphore *sem);
  8103. +extern void __up_read(struct rw_semaphore *sem);
  8104. +extern void __up_write(struct rw_semaphore *sem);
  8105. +extern void __downgrade_write(struct rw_semaphore *sem);
  8106. +
  8107. +#endif
  8108. diff -Nur linux-4.9.28.orig/include/linux/sched/rt.h linux-4.9.28/include/linux/sched/rt.h
  8109. --- linux-4.9.28.orig/include/linux/sched/rt.h 2017-05-14 14:00:37.000000000 +0200
  8110. +++ linux-4.9.28/include/linux/sched/rt.h 2017-05-19 03:37:25.174176493 +0200
  8111. @@ -16,27 +16,20 @@
  8112. }
  8113. #ifdef CONFIG_RT_MUTEXES
  8114. -extern int rt_mutex_getprio(struct task_struct *p);
  8115. -extern void rt_mutex_setprio(struct task_struct *p, int prio);
  8116. -extern int rt_mutex_get_effective_prio(struct task_struct *task, int newprio);
  8117. -extern struct task_struct *rt_mutex_get_top_task(struct task_struct *task);
  8118. +/*
  8119. + * Must hold either p->pi_lock or task_rq(p)->lock.
  8120. + */
  8121. +static inline struct task_struct *rt_mutex_get_top_task(struct task_struct *p)
  8122. +{
  8123. + return p->pi_top_task;
  8124. +}
  8125. +extern void rt_mutex_setprio(struct task_struct *p, struct task_struct *pi_task);
  8126. extern void rt_mutex_adjust_pi(struct task_struct *p);
  8127. static inline bool tsk_is_pi_blocked(struct task_struct *tsk)
  8128. {
  8129. return tsk->pi_blocked_on != NULL;
  8130. }
  8131. #else
  8132. -static inline int rt_mutex_getprio(struct task_struct *p)
  8133. -{
  8134. - return p->normal_prio;
  8135. -}
  8136. -
  8137. -static inline int rt_mutex_get_effective_prio(struct task_struct *task,
  8138. - int newprio)
  8139. -{
  8140. - return newprio;
  8141. -}
  8142. -
  8143. static inline struct task_struct *rt_mutex_get_top_task(struct task_struct *task)
  8144. {
  8145. return NULL;
  8146. diff -Nur linux-4.9.28.orig/include/linux/sched.h linux-4.9.28/include/linux/sched.h
  8147. --- linux-4.9.28.orig/include/linux/sched.h 2017-05-14 14:00:37.000000000 +0200
  8148. +++ linux-4.9.28/include/linux/sched.h 2017-05-19 03:37:25.174176493 +0200
  8149. @@ -26,6 +26,7 @@
  8150. #include <linux/nodemask.h>
  8151. #include <linux/mm_types.h>
  8152. #include <linux/preempt.h>
  8153. +#include <asm/kmap_types.h>
  8154. #include <asm/page.h>
  8155. #include <asm/ptrace.h>
  8156. @@ -243,10 +244,7 @@
  8157. TASK_UNINTERRUPTIBLE | __TASK_STOPPED | \
  8158. __TASK_TRACED | EXIT_ZOMBIE | EXIT_DEAD)
  8159. -#define task_is_traced(task) ((task->state & __TASK_TRACED) != 0)
  8160. #define task_is_stopped(task) ((task->state & __TASK_STOPPED) != 0)
  8161. -#define task_is_stopped_or_traced(task) \
  8162. - ((task->state & (__TASK_STOPPED | __TASK_TRACED)) != 0)
  8163. #define task_contributes_to_load(task) \
  8164. ((task->state & TASK_UNINTERRUPTIBLE) != 0 && \
  8165. (task->flags & PF_FROZEN) == 0 && \
  8166. @@ -312,6 +310,11 @@
  8167. #endif
  8168. +#define __set_current_state_no_track(state_value) \
  8169. + do { current->state = (state_value); } while (0)
  8170. +#define set_current_state_no_track(state_value) \
  8171. + set_mb(current->state, (state_value))
  8172. +
  8173. /* Task command name length */
  8174. #define TASK_COMM_LEN 16
  8175. @@ -1013,8 +1016,18 @@
  8176. struct wake_q_head name = { WAKE_Q_TAIL, &name.first }
  8177. extern void wake_q_add(struct wake_q_head *head,
  8178. - struct task_struct *task);
  8179. -extern void wake_up_q(struct wake_q_head *head);
  8180. + struct task_struct *task);
  8181. +extern void __wake_up_q(struct wake_q_head *head, bool sleeper);
  8182. +
  8183. +static inline void wake_up_q(struct wake_q_head *head)
  8184. +{
  8185. + __wake_up_q(head, false);
  8186. +}
  8187. +
  8188. +static inline void wake_up_q_sleeper(struct wake_q_head *head)
  8189. +{
  8190. + __wake_up_q(head, true);
  8191. +}
  8192. /*
  8193. * sched-domains (multiprocessor balancing) declarations:
  8194. @@ -1481,6 +1494,7 @@
  8195. struct thread_info thread_info;
  8196. #endif
  8197. volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
  8198. + volatile long saved_state; /* saved state for "spinlock sleepers" */
  8199. void *stack;
  8200. atomic_t usage;
  8201. unsigned int flags; /* per process flags, defined below */
  8202. @@ -1520,6 +1534,12 @@
  8203. #endif
  8204. unsigned int policy;
  8205. +#ifdef CONFIG_PREEMPT_RT_FULL
  8206. + int migrate_disable;
  8207. +# ifdef CONFIG_SCHED_DEBUG
  8208. + int migrate_disable_atomic;
  8209. +# endif
  8210. +#endif
  8211. int nr_cpus_allowed;
  8212. cpumask_t cpus_allowed;
  8213. @@ -1658,6 +1678,9 @@
  8214. struct task_cputime cputime_expires;
  8215. struct list_head cpu_timers[3];
  8216. +#ifdef CONFIG_PREEMPT_RT_BASE
  8217. + struct task_struct *posix_timer_list;
  8218. +#endif
  8219. /* process credentials */
  8220. const struct cred __rcu *ptracer_cred; /* Tracer's credentials at attach */
  8221. @@ -1689,10 +1712,15 @@
  8222. /* signal handlers */
  8223. struct signal_struct *signal;
  8224. struct sighand_struct *sighand;
  8225. + struct sigqueue *sigqueue_cache;
  8226. sigset_t blocked, real_blocked;
  8227. sigset_t saved_sigmask; /* restored if set_restore_sigmask() was used */
  8228. struct sigpending pending;
  8229. +#ifdef CONFIG_PREEMPT_RT_FULL
  8230. + /* TODO: move me into ->restart_block ? */
  8231. + struct siginfo forced_info;
  8232. +#endif
  8233. unsigned long sas_ss_sp;
  8234. size_t sas_ss_size;
  8235. @@ -1723,6 +1751,8 @@
  8236. /* PI waiters blocked on a rt_mutex held by this task */
  8237. struct rb_root pi_waiters;
  8238. struct rb_node *pi_waiters_leftmost;
  8239. + /* Updated under owner's pi_lock and rq lock */
  8240. + struct task_struct *pi_top_task;
  8241. /* Deadlock detection and priority inheritance handling */
  8242. struct rt_mutex_waiter *pi_blocked_on;
  8243. #endif
  8244. @@ -1921,6 +1951,12 @@
  8245. /* bitmask and counter of trace recursion */
  8246. unsigned long trace_recursion;
  8247. #endif /* CONFIG_TRACING */
  8248. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  8249. + u64 preempt_timestamp_hist;
  8250. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  8251. + long timer_offset;
  8252. +#endif
  8253. +#endif
  8254. #ifdef CONFIG_KCOV
  8255. /* Coverage collection mode enabled for this task (0 if disabled). */
  8256. enum kcov_mode kcov_mode;
  8257. @@ -1946,9 +1982,23 @@
  8258. unsigned int sequential_io;
  8259. unsigned int sequential_io_avg;
  8260. #endif
  8261. +#ifdef CONFIG_PREEMPT_RT_BASE
  8262. + struct rcu_head put_rcu;
  8263. + int softirq_nestcnt;
  8264. + unsigned int softirqs_raised;
  8265. +#endif
  8266. +#ifdef CONFIG_PREEMPT_RT_FULL
  8267. +# if defined CONFIG_HIGHMEM || defined CONFIG_X86_32
  8268. + int kmap_idx;
  8269. + pte_t kmap_pte[KM_TYPE_NR];
  8270. +# endif
  8271. +#endif
  8272. #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
  8273. unsigned long task_state_change;
  8274. #endif
  8275. +#ifdef CONFIG_PREEMPT_RT_FULL
  8276. + int xmit_recursion;
  8277. +#endif
  8278. int pagefault_disabled;
  8279. #ifdef CONFIG_MMU
  8280. struct task_struct *oom_reaper_list;
  8281. @@ -1988,14 +2038,6 @@
  8282. }
  8283. #endif
  8284. -/* Future-safe accessor for struct task_struct's cpus_allowed. */
  8285. -#define tsk_cpus_allowed(tsk) (&(tsk)->cpus_allowed)
  8286. -
  8287. -static inline int tsk_nr_cpus_allowed(struct task_struct *p)
  8288. -{
  8289. - return p->nr_cpus_allowed;
  8290. -}
  8291. -
  8292. #define TNF_MIGRATED 0x01
  8293. #define TNF_NO_GROUP 0x02
  8294. #define TNF_SHARED 0x04
  8295. @@ -2211,6 +2253,15 @@
  8296. extern void free_task(struct task_struct *tsk);
  8297. #define get_task_struct(tsk) do { atomic_inc(&(tsk)->usage); } while(0)
  8298. +#ifdef CONFIG_PREEMPT_RT_BASE
  8299. +extern void __put_task_struct_cb(struct rcu_head *rhp);
  8300. +
  8301. +static inline void put_task_struct(struct task_struct *t)
  8302. +{
  8303. + if (atomic_dec_and_test(&t->usage))
  8304. + call_rcu(&t->put_rcu, __put_task_struct_cb);
  8305. +}
  8306. +#else
  8307. extern void __put_task_struct(struct task_struct *t);
  8308. static inline void put_task_struct(struct task_struct *t)
  8309. @@ -2218,6 +2269,7 @@
  8310. if (atomic_dec_and_test(&t->usage))
  8311. __put_task_struct(t);
  8312. }
  8313. +#endif
  8314. struct task_struct *task_rcu_dereference(struct task_struct **ptask);
  8315. struct task_struct *try_get_task_struct(struct task_struct **ptask);
  8316. @@ -2259,6 +2311,7 @@
  8317. /*
  8318. * Per process flags
  8319. */
  8320. +#define PF_IN_SOFTIRQ 0x00000001 /* Task is serving softirq */
  8321. #define PF_EXITING 0x00000004 /* getting shut down */
  8322. #define PF_EXITPIDONE 0x00000008 /* pi exit done on shut down */
  8323. #define PF_VCPU 0x00000010 /* I'm a virtual CPU */
  8324. @@ -2427,6 +2480,10 @@
  8325. extern int set_cpus_allowed_ptr(struct task_struct *p,
  8326. const struct cpumask *new_mask);
  8327. +int migrate_me(void);
  8328. +void tell_sched_cpu_down_begin(int cpu);
  8329. +void tell_sched_cpu_down_done(int cpu);
  8330. +
  8331. #else
  8332. static inline void do_set_cpus_allowed(struct task_struct *p,
  8333. const struct cpumask *new_mask)
  8334. @@ -2439,6 +2496,9 @@
  8335. return -EINVAL;
  8336. return 0;
  8337. }
  8338. +static inline int migrate_me(void) { return 0; }
  8339. +static inline void tell_sched_cpu_down_begin(int cpu) { }
  8340. +static inline void tell_sched_cpu_down_done(int cpu) { }
  8341. #endif
  8342. #ifdef CONFIG_NO_HZ_COMMON
  8343. @@ -2677,6 +2737,7 @@
  8344. extern int wake_up_state(struct task_struct *tsk, unsigned int state);
  8345. extern int wake_up_process(struct task_struct *tsk);
  8346. +extern int wake_up_lock_sleeper(struct task_struct * tsk);
  8347. extern void wake_up_new_task(struct task_struct *tsk);
  8348. #ifdef CONFIG_SMP
  8349. extern void kick_process(struct task_struct *tsk);
  8350. @@ -2885,6 +2946,17 @@
  8351. __mmdrop(mm);
  8352. }
  8353. +#ifdef CONFIG_PREEMPT_RT_BASE
  8354. +extern void __mmdrop_delayed(struct rcu_head *rhp);
  8355. +static inline void mmdrop_delayed(struct mm_struct *mm)
  8356. +{
  8357. + if (atomic_dec_and_test(&mm->mm_count))
  8358. + call_rcu(&mm->delayed_drop, __mmdrop_delayed);
  8359. +}
  8360. +#else
  8361. +# define mmdrop_delayed(mm) mmdrop(mm)
  8362. +#endif
  8363. +
  8364. static inline void mmdrop_async_fn(struct work_struct *work)
  8365. {
  8366. struct mm_struct *mm = container_of(work, struct mm_struct, async_put_work);
  8367. @@ -3277,6 +3349,43 @@
  8368. return unlikely(test_tsk_thread_flag(tsk,TIF_NEED_RESCHED));
  8369. }
  8370. +#ifdef CONFIG_PREEMPT_LAZY
  8371. +static inline void set_tsk_need_resched_lazy(struct task_struct *tsk)
  8372. +{
  8373. + set_tsk_thread_flag(tsk,TIF_NEED_RESCHED_LAZY);
  8374. +}
  8375. +
  8376. +static inline void clear_tsk_need_resched_lazy(struct task_struct *tsk)
  8377. +{
  8378. + clear_tsk_thread_flag(tsk,TIF_NEED_RESCHED_LAZY);
  8379. +}
  8380. +
  8381. +static inline int test_tsk_need_resched_lazy(struct task_struct *tsk)
  8382. +{
  8383. + return unlikely(test_tsk_thread_flag(tsk,TIF_NEED_RESCHED_LAZY));
  8384. +}
  8385. +
  8386. +static inline int need_resched_lazy(void)
  8387. +{
  8388. + return test_thread_flag(TIF_NEED_RESCHED_LAZY);
  8389. +}
  8390. +
  8391. +static inline int need_resched_now(void)
  8392. +{
  8393. + return test_thread_flag(TIF_NEED_RESCHED);
  8394. +}
  8395. +
  8396. +#else
  8397. +static inline void clear_tsk_need_resched_lazy(struct task_struct *tsk) { }
  8398. +static inline int need_resched_lazy(void) { return 0; }
  8399. +
  8400. +static inline int need_resched_now(void)
  8401. +{
  8402. + return test_thread_flag(TIF_NEED_RESCHED);
  8403. +}
  8404. +
  8405. +#endif
  8406. +
  8407. static inline int restart_syscall(void)
  8408. {
  8409. set_tsk_thread_flag(current, TIF_SIGPENDING);
  8410. @@ -3308,6 +3417,51 @@
  8411. return (state & TASK_INTERRUPTIBLE) || __fatal_signal_pending(p);
  8412. }
  8413. +static inline bool __task_is_stopped_or_traced(struct task_struct *task)
  8414. +{
  8415. + if (task->state & (__TASK_STOPPED | __TASK_TRACED))
  8416. + return true;
  8417. +#ifdef CONFIG_PREEMPT_RT_FULL
  8418. + if (task->saved_state & (__TASK_STOPPED | __TASK_TRACED))
  8419. + return true;
  8420. +#endif
  8421. + return false;
  8422. +}
  8423. +
  8424. +static inline bool task_is_stopped_or_traced(struct task_struct *task)
  8425. +{
  8426. + bool traced_stopped;
  8427. +
  8428. +#ifdef CONFIG_PREEMPT_RT_FULL
  8429. + unsigned long flags;
  8430. +
  8431. + raw_spin_lock_irqsave(&task->pi_lock, flags);
  8432. + traced_stopped = __task_is_stopped_or_traced(task);
  8433. + raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  8434. +#else
  8435. + traced_stopped = __task_is_stopped_or_traced(task);
  8436. +#endif
  8437. + return traced_stopped;
  8438. +}
  8439. +
  8440. +static inline bool task_is_traced(struct task_struct *task)
  8441. +{
  8442. + bool traced = false;
  8443. +
  8444. + if (task->state & __TASK_TRACED)
  8445. + return true;
  8446. +#ifdef CONFIG_PREEMPT_RT_FULL
  8447. + /* in case the task is sleeping on tasklist_lock */
  8448. + raw_spin_lock_irq(&task->pi_lock);
  8449. + if (task->state & __TASK_TRACED)
  8450. + traced = true;
  8451. + else if (task->saved_state & __TASK_TRACED)
  8452. + traced = true;
  8453. + raw_spin_unlock_irq(&task->pi_lock);
  8454. +#endif
  8455. + return traced;
  8456. +}
  8457. +
  8458. /*
  8459. * cond_resched() and cond_resched_lock(): latency reduction via
  8460. * explicit rescheduling in places that are safe. The return
  8461. @@ -3333,12 +3487,16 @@
  8462. __cond_resched_lock(lock); \
  8463. })
  8464. +#ifndef CONFIG_PREEMPT_RT_FULL
  8465. extern int __cond_resched_softirq(void);
  8466. #define cond_resched_softirq() ({ \
  8467. ___might_sleep(__FILE__, __LINE__, SOFTIRQ_DISABLE_OFFSET); \
  8468. __cond_resched_softirq(); \
  8469. })
  8470. +#else
  8471. +# define cond_resched_softirq() cond_resched()
  8472. +#endif
  8473. static inline void cond_resched_rcu(void)
  8474. {
  8475. @@ -3513,6 +3671,31 @@
  8476. #endif /* CONFIG_SMP */
  8477. +static inline int __migrate_disabled(struct task_struct *p)
  8478. +{
  8479. +#ifdef CONFIG_PREEMPT_RT_FULL
  8480. + return p->migrate_disable;
  8481. +#else
  8482. + return 0;
  8483. +#endif
  8484. +}
  8485. +
  8486. +/* Future-safe accessor for struct task_struct's cpus_allowed. */
  8487. +static inline const struct cpumask *tsk_cpus_allowed(struct task_struct *p)
  8488. +{
  8489. + if (__migrate_disabled(p))
  8490. + return cpumask_of(task_cpu(p));
  8491. +
  8492. + return &p->cpus_allowed;
  8493. +}
  8494. +
  8495. +static inline int tsk_nr_cpus_allowed(struct task_struct *p)
  8496. +{
  8497. + if (__migrate_disabled(p))
  8498. + return 1;
  8499. + return p->nr_cpus_allowed;
  8500. +}
  8501. +
  8502. extern long sched_setaffinity(pid_t pid, const struct cpumask *new_mask);
  8503. extern long sched_getaffinity(pid_t pid, struct cpumask *mask);
  8504. diff -Nur linux-4.9.28.orig/include/linux/seqlock.h linux-4.9.28/include/linux/seqlock.h
  8505. --- linux-4.9.28.orig/include/linux/seqlock.h 2017-05-14 14:00:37.000000000 +0200
  8506. +++ linux-4.9.28/include/linux/seqlock.h 2017-05-19 03:37:25.174176493 +0200
  8507. @@ -220,20 +220,30 @@
  8508. return __read_seqcount_retry(s, start);
  8509. }
  8510. -
  8511. -
  8512. -static inline void raw_write_seqcount_begin(seqcount_t *s)
  8513. +static inline void __raw_write_seqcount_begin(seqcount_t *s)
  8514. {
  8515. s->sequence++;
  8516. smp_wmb();
  8517. }
  8518. -static inline void raw_write_seqcount_end(seqcount_t *s)
  8519. +static inline void raw_write_seqcount_begin(seqcount_t *s)
  8520. +{
  8521. + preempt_disable_rt();
  8522. + __raw_write_seqcount_begin(s);
  8523. +}
  8524. +
  8525. +static inline void __raw_write_seqcount_end(seqcount_t *s)
  8526. {
  8527. smp_wmb();
  8528. s->sequence++;
  8529. }
  8530. +static inline void raw_write_seqcount_end(seqcount_t *s)
  8531. +{
  8532. + __raw_write_seqcount_end(s);
  8533. + preempt_enable_rt();
  8534. +}
  8535. +
  8536. /**
  8537. * raw_write_seqcount_barrier - do a seq write barrier
  8538. * @s: pointer to seqcount_t
  8539. @@ -428,10 +438,32 @@
  8540. /*
  8541. * Read side functions for starting and finalizing a read side section.
  8542. */
  8543. +#ifndef CONFIG_PREEMPT_RT_FULL
  8544. static inline unsigned read_seqbegin(const seqlock_t *sl)
  8545. {
  8546. return read_seqcount_begin(&sl->seqcount);
  8547. }
  8548. +#else
  8549. +/*
  8550. + * Starvation safe read side for RT
  8551. + */
  8552. +static inline unsigned read_seqbegin(seqlock_t *sl)
  8553. +{
  8554. + unsigned ret;
  8555. +
  8556. +repeat:
  8557. + ret = ACCESS_ONCE(sl->seqcount.sequence);
  8558. + if (unlikely(ret & 1)) {
  8559. + /*
  8560. + * Take the lock and let the writer proceed (i.e. evtl
  8561. + * boost it), otherwise we could loop here forever.
  8562. + */
  8563. + spin_unlock_wait(&sl->lock);
  8564. + goto repeat;
  8565. + }
  8566. + return ret;
  8567. +}
  8568. +#endif
  8569. static inline unsigned read_seqretry(const seqlock_t *sl, unsigned start)
  8570. {
  8571. @@ -446,36 +478,45 @@
  8572. static inline void write_seqlock(seqlock_t *sl)
  8573. {
  8574. spin_lock(&sl->lock);
  8575. - write_seqcount_begin(&sl->seqcount);
  8576. + __raw_write_seqcount_begin(&sl->seqcount);
  8577. +}
  8578. +
  8579. +static inline int try_write_seqlock(seqlock_t *sl)
  8580. +{
  8581. + if (spin_trylock(&sl->lock)) {
  8582. + __raw_write_seqcount_begin(&sl->seqcount);
  8583. + return 1;
  8584. + }
  8585. + return 0;
  8586. }
  8587. static inline void write_sequnlock(seqlock_t *sl)
  8588. {
  8589. - write_seqcount_end(&sl->seqcount);
  8590. + __raw_write_seqcount_end(&sl->seqcount);
  8591. spin_unlock(&sl->lock);
  8592. }
  8593. static inline void write_seqlock_bh(seqlock_t *sl)
  8594. {
  8595. spin_lock_bh(&sl->lock);
  8596. - write_seqcount_begin(&sl->seqcount);
  8597. + __raw_write_seqcount_begin(&sl->seqcount);
  8598. }
  8599. static inline void write_sequnlock_bh(seqlock_t *sl)
  8600. {
  8601. - write_seqcount_end(&sl->seqcount);
  8602. + __raw_write_seqcount_end(&sl->seqcount);
  8603. spin_unlock_bh(&sl->lock);
  8604. }
  8605. static inline void write_seqlock_irq(seqlock_t *sl)
  8606. {
  8607. spin_lock_irq(&sl->lock);
  8608. - write_seqcount_begin(&sl->seqcount);
  8609. + __raw_write_seqcount_begin(&sl->seqcount);
  8610. }
  8611. static inline void write_sequnlock_irq(seqlock_t *sl)
  8612. {
  8613. - write_seqcount_end(&sl->seqcount);
  8614. + __raw_write_seqcount_end(&sl->seqcount);
  8615. spin_unlock_irq(&sl->lock);
  8616. }
  8617. @@ -484,7 +525,7 @@
  8618. unsigned long flags;
  8619. spin_lock_irqsave(&sl->lock, flags);
  8620. - write_seqcount_begin(&sl->seqcount);
  8621. + __raw_write_seqcount_begin(&sl->seqcount);
  8622. return flags;
  8623. }
  8624. @@ -494,7 +535,7 @@
  8625. static inline void
  8626. write_sequnlock_irqrestore(seqlock_t *sl, unsigned long flags)
  8627. {
  8628. - write_seqcount_end(&sl->seqcount);
  8629. + __raw_write_seqcount_end(&sl->seqcount);
  8630. spin_unlock_irqrestore(&sl->lock, flags);
  8631. }
  8632. diff -Nur linux-4.9.28.orig/include/linux/signal.h linux-4.9.28/include/linux/signal.h
  8633. --- linux-4.9.28.orig/include/linux/signal.h 2017-05-14 14:00:37.000000000 +0200
  8634. +++ linux-4.9.28/include/linux/signal.h 2017-05-19 03:37:25.174176493 +0200
  8635. @@ -233,6 +233,7 @@
  8636. }
  8637. extern void flush_sigqueue(struct sigpending *queue);
  8638. +extern void flush_task_sigqueue(struct task_struct *tsk);
  8639. /* Test if 'sig' is valid signal. Use this instead of testing _NSIG directly */
  8640. static inline int valid_signal(unsigned long sig)
  8641. diff -Nur linux-4.9.28.orig/include/linux/skbuff.h linux-4.9.28/include/linux/skbuff.h
  8642. --- linux-4.9.28.orig/include/linux/skbuff.h 2017-05-14 14:00:37.000000000 +0200
  8643. +++ linux-4.9.28/include/linux/skbuff.h 2017-05-19 03:37:25.174176493 +0200
  8644. @@ -284,6 +284,7 @@
  8645. __u32 qlen;
  8646. spinlock_t lock;
  8647. + raw_spinlock_t raw_lock;
  8648. };
  8649. struct sk_buff;
  8650. @@ -1573,6 +1574,12 @@
  8651. __skb_queue_head_init(list);
  8652. }
  8653. +static inline void skb_queue_head_init_raw(struct sk_buff_head *list)
  8654. +{
  8655. + raw_spin_lock_init(&list->raw_lock);
  8656. + __skb_queue_head_init(list);
  8657. +}
  8658. +
  8659. static inline void skb_queue_head_init_class(struct sk_buff_head *list,
  8660. struct lock_class_key *class)
  8661. {
  8662. diff -Nur linux-4.9.28.orig/include/linux/smp.h linux-4.9.28/include/linux/smp.h
  8663. --- linux-4.9.28.orig/include/linux/smp.h 2017-05-14 14:00:37.000000000 +0200
  8664. +++ linux-4.9.28/include/linux/smp.h 2017-05-19 03:37:25.174176493 +0200
  8665. @@ -120,6 +120,13 @@
  8666. extern void __init setup_nr_cpu_ids(void);
  8667. extern void __init smp_init(void);
  8668. +extern int __boot_cpu_id;
  8669. +
  8670. +static inline int get_boot_cpu_id(void)
  8671. +{
  8672. + return __boot_cpu_id;
  8673. +}
  8674. +
  8675. #else /* !SMP */
  8676. static inline void smp_send_stop(void) { }
  8677. @@ -158,6 +165,11 @@
  8678. static inline void smp_init(void) { }
  8679. #endif
  8680. +static inline int get_boot_cpu_id(void)
  8681. +{
  8682. + return 0;
  8683. +}
  8684. +
  8685. #endif /* !SMP */
  8686. /*
  8687. @@ -185,6 +197,9 @@
  8688. #define get_cpu() ({ preempt_disable(); smp_processor_id(); })
  8689. #define put_cpu() preempt_enable()
  8690. +#define get_cpu_light() ({ migrate_disable(); smp_processor_id(); })
  8691. +#define put_cpu_light() migrate_enable()
  8692. +
  8693. /*
  8694. * Callback to arch code if there's nosmp or maxcpus=0 on the
  8695. * boot command line:
  8696. diff -Nur linux-4.9.28.orig/include/linux/spinlock_api_smp.h linux-4.9.28/include/linux/spinlock_api_smp.h
  8697. --- linux-4.9.28.orig/include/linux/spinlock_api_smp.h 2017-05-14 14:00:37.000000000 +0200
  8698. +++ linux-4.9.28/include/linux/spinlock_api_smp.h 2017-05-19 03:37:25.174176493 +0200
  8699. @@ -189,6 +189,8 @@
  8700. return 0;
  8701. }
  8702. -#include <linux/rwlock_api_smp.h>
  8703. +#ifndef CONFIG_PREEMPT_RT_FULL
  8704. +# include <linux/rwlock_api_smp.h>
  8705. +#endif
  8706. #endif /* __LINUX_SPINLOCK_API_SMP_H */
  8707. diff -Nur linux-4.9.28.orig/include/linux/spinlock.h linux-4.9.28/include/linux/spinlock.h
  8708. --- linux-4.9.28.orig/include/linux/spinlock.h 2017-05-14 14:00:37.000000000 +0200
  8709. +++ linux-4.9.28/include/linux/spinlock.h 2017-05-19 03:37:25.174176493 +0200
  8710. @@ -271,7 +271,11 @@
  8711. #define raw_spin_can_lock(lock) (!raw_spin_is_locked(lock))
  8712. /* Include rwlock functions */
  8713. -#include <linux/rwlock.h>
  8714. +#ifdef CONFIG_PREEMPT_RT_FULL
  8715. +# include <linux/rwlock_rt.h>
  8716. +#else
  8717. +# include <linux/rwlock.h>
  8718. +#endif
  8719. /*
  8720. * Pull the _spin_*()/_read_*()/_write_*() functions/declarations:
  8721. @@ -282,6 +286,10 @@
  8722. # include <linux/spinlock_api_up.h>
  8723. #endif
  8724. +#ifdef CONFIG_PREEMPT_RT_FULL
  8725. +# include <linux/spinlock_rt.h>
  8726. +#else /* PREEMPT_RT_FULL */
  8727. +
  8728. /*
  8729. * Map the spin_lock functions to the raw variants for PREEMPT_RT=n
  8730. */
  8731. @@ -416,4 +424,6 @@
  8732. #define atomic_dec_and_lock(atomic, lock) \
  8733. __cond_lock(lock, _atomic_dec_and_lock(atomic, lock))
  8734. +#endif /* !PREEMPT_RT_FULL */
  8735. +
  8736. #endif /* __LINUX_SPINLOCK_H */
  8737. diff -Nur linux-4.9.28.orig/include/linux/spinlock_rt.h linux-4.9.28/include/linux/spinlock_rt.h
  8738. --- linux-4.9.28.orig/include/linux/spinlock_rt.h 1970-01-01 01:00:00.000000000 +0100
  8739. +++ linux-4.9.28/include/linux/spinlock_rt.h 2017-05-19 03:37:25.174176493 +0200
  8740. @@ -0,0 +1,162 @@
  8741. +#ifndef __LINUX_SPINLOCK_RT_H
  8742. +#define __LINUX_SPINLOCK_RT_H
  8743. +
  8744. +#ifndef __LINUX_SPINLOCK_H
  8745. +#error Do not include directly. Use spinlock.h
  8746. +#endif
  8747. +
  8748. +#include <linux/bug.h>
  8749. +
  8750. +extern void
  8751. +__rt_spin_lock_init(spinlock_t *lock, char *name, struct lock_class_key *key);
  8752. +
  8753. +#define spin_lock_init(slock) \
  8754. +do { \
  8755. + static struct lock_class_key __key; \
  8756. + \
  8757. + rt_mutex_init(&(slock)->lock); \
  8758. + __rt_spin_lock_init(slock, #slock, &__key); \
  8759. +} while (0)
  8760. +
  8761. +void __lockfunc rt_spin_lock__no_mg(spinlock_t *lock);
  8762. +void __lockfunc rt_spin_unlock__no_mg(spinlock_t *lock);
  8763. +int __lockfunc rt_spin_trylock__no_mg(spinlock_t *lock);
  8764. +
  8765. +extern void __lockfunc rt_spin_lock(spinlock_t *lock);
  8766. +extern unsigned long __lockfunc rt_spin_lock_trace_flags(spinlock_t *lock);
  8767. +extern void __lockfunc rt_spin_lock_nested(spinlock_t *lock, int subclass);
  8768. +extern void __lockfunc rt_spin_unlock(spinlock_t *lock);
  8769. +extern void __lockfunc rt_spin_unlock_wait(spinlock_t *lock);
  8770. +extern int __lockfunc rt_spin_trylock_irqsave(spinlock_t *lock, unsigned long *flags);
  8771. +extern int __lockfunc rt_spin_trylock_bh(spinlock_t *lock);
  8772. +extern int __lockfunc rt_spin_trylock(spinlock_t *lock);
  8773. +extern int atomic_dec_and_spin_lock(atomic_t *atomic, spinlock_t *lock);
  8774. +
  8775. +/*
  8776. + * lockdep-less calls, for derived types like rwlock:
  8777. + * (for trylock they can use rt_mutex_trylock() directly.
  8778. + */
  8779. +extern void __lockfunc __rt_spin_lock__no_mg(struct rt_mutex *lock);
  8780. +extern void __lockfunc __rt_spin_lock(struct rt_mutex *lock);
  8781. +extern void __lockfunc __rt_spin_unlock(struct rt_mutex *lock);
  8782. +
  8783. +#define spin_lock(lock) rt_spin_lock(lock)
  8784. +
  8785. +#define spin_lock_bh(lock) \
  8786. + do { \
  8787. + local_bh_disable(); \
  8788. + rt_spin_lock(lock); \
  8789. + } while (0)
  8790. +
  8791. +#define spin_lock_irq(lock) spin_lock(lock)
  8792. +
  8793. +#define spin_do_trylock(lock) __cond_lock(lock, rt_spin_trylock(lock))
  8794. +
  8795. +#define spin_trylock(lock) \
  8796. +({ \
  8797. + int __locked; \
  8798. + __locked = spin_do_trylock(lock); \
  8799. + __locked; \
  8800. +})
  8801. +
  8802. +#ifdef CONFIG_LOCKDEP
  8803. +# define spin_lock_nested(lock, subclass) \
  8804. + do { \
  8805. + rt_spin_lock_nested(lock, subclass); \
  8806. + } while (0)
  8807. +
  8808. +#define spin_lock_bh_nested(lock, subclass) \
  8809. + do { \
  8810. + local_bh_disable(); \
  8811. + rt_spin_lock_nested(lock, subclass); \
  8812. + } while (0)
  8813. +
  8814. +# define spin_lock_irqsave_nested(lock, flags, subclass) \
  8815. + do { \
  8816. + typecheck(unsigned long, flags); \
  8817. + flags = 0; \
  8818. + rt_spin_lock_nested(lock, subclass); \
  8819. + } while (0)
  8820. +#else
  8821. +# define spin_lock_nested(lock, subclass) spin_lock(lock)
  8822. +# define spin_lock_bh_nested(lock, subclass) spin_lock_bh(lock)
  8823. +
  8824. +# define spin_lock_irqsave_nested(lock, flags, subclass) \
  8825. + do { \
  8826. + typecheck(unsigned long, flags); \
  8827. + flags = 0; \
  8828. + spin_lock(lock); \
  8829. + } while (0)
  8830. +#endif
  8831. +
  8832. +#define spin_lock_irqsave(lock, flags) \
  8833. + do { \
  8834. + typecheck(unsigned long, flags); \
  8835. + flags = 0; \
  8836. + spin_lock(lock); \
  8837. + } while (0)
  8838. +
  8839. +static inline unsigned long spin_lock_trace_flags(spinlock_t *lock)
  8840. +{
  8841. + unsigned long flags = 0;
  8842. +#ifdef CONFIG_TRACE_IRQFLAGS
  8843. + flags = rt_spin_lock_trace_flags(lock);
  8844. +#else
  8845. + spin_lock(lock); /* lock_local */
  8846. +#endif
  8847. + return flags;
  8848. +}
  8849. +
  8850. +/* FIXME: we need rt_spin_lock_nest_lock */
  8851. +#define spin_lock_nest_lock(lock, nest_lock) spin_lock_nested(lock, 0)
  8852. +
  8853. +#define spin_unlock(lock) rt_spin_unlock(lock)
  8854. +
  8855. +#define spin_unlock_bh(lock) \
  8856. + do { \
  8857. + rt_spin_unlock(lock); \
  8858. + local_bh_enable(); \
  8859. + } while (0)
  8860. +
  8861. +#define spin_unlock_irq(lock) spin_unlock(lock)
  8862. +
  8863. +#define spin_unlock_irqrestore(lock, flags) \
  8864. + do { \
  8865. + typecheck(unsigned long, flags); \
  8866. + (void) flags; \
  8867. + spin_unlock(lock); \
  8868. + } while (0)
  8869. +
  8870. +#define spin_trylock_bh(lock) __cond_lock(lock, rt_spin_trylock_bh(lock))
  8871. +#define spin_trylock_irq(lock) spin_trylock(lock)
  8872. +
  8873. +#define spin_trylock_irqsave(lock, flags) \
  8874. + rt_spin_trylock_irqsave(lock, &(flags))
  8875. +
  8876. +#define spin_unlock_wait(lock) rt_spin_unlock_wait(lock)
  8877. +
  8878. +#ifdef CONFIG_GENERIC_LOCKBREAK
  8879. +# define spin_is_contended(lock) ((lock)->break_lock)
  8880. +#else
  8881. +# define spin_is_contended(lock) (((void)(lock), 0))
  8882. +#endif
  8883. +
  8884. +static inline int spin_can_lock(spinlock_t *lock)
  8885. +{
  8886. + return !rt_mutex_is_locked(&lock->lock);
  8887. +}
  8888. +
  8889. +static inline int spin_is_locked(spinlock_t *lock)
  8890. +{
  8891. + return rt_mutex_is_locked(&lock->lock);
  8892. +}
  8893. +
  8894. +static inline void assert_spin_locked(spinlock_t *lock)
  8895. +{
  8896. + BUG_ON(!spin_is_locked(lock));
  8897. +}
  8898. +
  8899. +#define atomic_dec_and_lock(atomic, lock) \
  8900. + atomic_dec_and_spin_lock(atomic, lock)
  8901. +
  8902. +#endif
  8903. diff -Nur linux-4.9.28.orig/include/linux/spinlock_types.h linux-4.9.28/include/linux/spinlock_types.h
  8904. --- linux-4.9.28.orig/include/linux/spinlock_types.h 2017-05-14 14:00:37.000000000 +0200
  8905. +++ linux-4.9.28/include/linux/spinlock_types.h 2017-05-19 03:37:25.174176493 +0200
  8906. @@ -9,80 +9,15 @@
  8907. * Released under the General Public License (GPL).
  8908. */
  8909. -#if defined(CONFIG_SMP)
  8910. -# include <asm/spinlock_types.h>
  8911. -#else
  8912. -# include <linux/spinlock_types_up.h>
  8913. -#endif
  8914. -
  8915. -#include <linux/lockdep.h>
  8916. -
  8917. -typedef struct raw_spinlock {
  8918. - arch_spinlock_t raw_lock;
  8919. -#ifdef CONFIG_GENERIC_LOCKBREAK
  8920. - unsigned int break_lock;
  8921. -#endif
  8922. -#ifdef CONFIG_DEBUG_SPINLOCK
  8923. - unsigned int magic, owner_cpu;
  8924. - void *owner;
  8925. -#endif
  8926. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  8927. - struct lockdep_map dep_map;
  8928. -#endif
  8929. -} raw_spinlock_t;
  8930. -
  8931. -#define SPINLOCK_MAGIC 0xdead4ead
  8932. -
  8933. -#define SPINLOCK_OWNER_INIT ((void *)-1L)
  8934. -
  8935. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  8936. -# define SPIN_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }
  8937. -#else
  8938. -# define SPIN_DEP_MAP_INIT(lockname)
  8939. -#endif
  8940. +#include <linux/spinlock_types_raw.h>
  8941. -#ifdef CONFIG_DEBUG_SPINLOCK
  8942. -# define SPIN_DEBUG_INIT(lockname) \
  8943. - .magic = SPINLOCK_MAGIC, \
  8944. - .owner_cpu = -1, \
  8945. - .owner = SPINLOCK_OWNER_INIT,
  8946. +#ifndef CONFIG_PREEMPT_RT_FULL
  8947. +# include <linux/spinlock_types_nort.h>
  8948. +# include <linux/rwlock_types.h>
  8949. #else
  8950. -# define SPIN_DEBUG_INIT(lockname)
  8951. +# include <linux/rtmutex.h>
  8952. +# include <linux/spinlock_types_rt.h>
  8953. +# include <linux/rwlock_types_rt.h>
  8954. #endif
  8955. -#define __RAW_SPIN_LOCK_INITIALIZER(lockname) \
  8956. - { \
  8957. - .raw_lock = __ARCH_SPIN_LOCK_UNLOCKED, \
  8958. - SPIN_DEBUG_INIT(lockname) \
  8959. - SPIN_DEP_MAP_INIT(lockname) }
  8960. -
  8961. -#define __RAW_SPIN_LOCK_UNLOCKED(lockname) \
  8962. - (raw_spinlock_t) __RAW_SPIN_LOCK_INITIALIZER(lockname)
  8963. -
  8964. -#define DEFINE_RAW_SPINLOCK(x) raw_spinlock_t x = __RAW_SPIN_LOCK_UNLOCKED(x)
  8965. -
  8966. -typedef struct spinlock {
  8967. - union {
  8968. - struct raw_spinlock rlock;
  8969. -
  8970. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  8971. -# define LOCK_PADSIZE (offsetof(struct raw_spinlock, dep_map))
  8972. - struct {
  8973. - u8 __padding[LOCK_PADSIZE];
  8974. - struct lockdep_map dep_map;
  8975. - };
  8976. -#endif
  8977. - };
  8978. -} spinlock_t;
  8979. -
  8980. -#define __SPIN_LOCK_INITIALIZER(lockname) \
  8981. - { { .rlock = __RAW_SPIN_LOCK_INITIALIZER(lockname) } }
  8982. -
  8983. -#define __SPIN_LOCK_UNLOCKED(lockname) \
  8984. - (spinlock_t ) __SPIN_LOCK_INITIALIZER(lockname)
  8985. -
  8986. -#define DEFINE_SPINLOCK(x) spinlock_t x = __SPIN_LOCK_UNLOCKED(x)
  8987. -
  8988. -#include <linux/rwlock_types.h>
  8989. -
  8990. #endif /* __LINUX_SPINLOCK_TYPES_H */
  8991. diff -Nur linux-4.9.28.orig/include/linux/spinlock_types_nort.h linux-4.9.28/include/linux/spinlock_types_nort.h
  8992. --- linux-4.9.28.orig/include/linux/spinlock_types_nort.h 1970-01-01 01:00:00.000000000 +0100
  8993. +++ linux-4.9.28/include/linux/spinlock_types_nort.h 2017-05-19 03:37:25.174176493 +0200
  8994. @@ -0,0 +1,33 @@
  8995. +#ifndef __LINUX_SPINLOCK_TYPES_NORT_H
  8996. +#define __LINUX_SPINLOCK_TYPES_NORT_H
  8997. +
  8998. +#ifndef __LINUX_SPINLOCK_TYPES_H
  8999. +#error "Do not include directly. Include spinlock_types.h instead"
  9000. +#endif
  9001. +
  9002. +/*
  9003. + * The non RT version maps spinlocks to raw_spinlocks
  9004. + */
  9005. +typedef struct spinlock {
  9006. + union {
  9007. + struct raw_spinlock rlock;
  9008. +
  9009. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  9010. +# define LOCK_PADSIZE (offsetof(struct raw_spinlock, dep_map))
  9011. + struct {
  9012. + u8 __padding[LOCK_PADSIZE];
  9013. + struct lockdep_map dep_map;
  9014. + };
  9015. +#endif
  9016. + };
  9017. +} spinlock_t;
  9018. +
  9019. +#define __SPIN_LOCK_INITIALIZER(lockname) \
  9020. + { { .rlock = __RAW_SPIN_LOCK_INITIALIZER(lockname) } }
  9021. +
  9022. +#define __SPIN_LOCK_UNLOCKED(lockname) \
  9023. + (spinlock_t ) __SPIN_LOCK_INITIALIZER(lockname)
  9024. +
  9025. +#define DEFINE_SPINLOCK(x) spinlock_t x = __SPIN_LOCK_UNLOCKED(x)
  9026. +
  9027. +#endif
  9028. diff -Nur linux-4.9.28.orig/include/linux/spinlock_types_raw.h linux-4.9.28/include/linux/spinlock_types_raw.h
  9029. --- linux-4.9.28.orig/include/linux/spinlock_types_raw.h 1970-01-01 01:00:00.000000000 +0100
  9030. +++ linux-4.9.28/include/linux/spinlock_types_raw.h 2017-05-19 03:37:25.174176493 +0200
  9031. @@ -0,0 +1,56 @@
  9032. +#ifndef __LINUX_SPINLOCK_TYPES_RAW_H
  9033. +#define __LINUX_SPINLOCK_TYPES_RAW_H
  9034. +
  9035. +#if defined(CONFIG_SMP)
  9036. +# include <asm/spinlock_types.h>
  9037. +#else
  9038. +# include <linux/spinlock_types_up.h>
  9039. +#endif
  9040. +
  9041. +#include <linux/lockdep.h>
  9042. +
  9043. +typedef struct raw_spinlock {
  9044. + arch_spinlock_t raw_lock;
  9045. +#ifdef CONFIG_GENERIC_LOCKBREAK
  9046. + unsigned int break_lock;
  9047. +#endif
  9048. +#ifdef CONFIG_DEBUG_SPINLOCK
  9049. + unsigned int magic, owner_cpu;
  9050. + void *owner;
  9051. +#endif
  9052. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  9053. + struct lockdep_map dep_map;
  9054. +#endif
  9055. +} raw_spinlock_t;
  9056. +
  9057. +#define SPINLOCK_MAGIC 0xdead4ead
  9058. +
  9059. +#define SPINLOCK_OWNER_INIT ((void *)-1L)
  9060. +
  9061. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  9062. +# define SPIN_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }
  9063. +#else
  9064. +# define SPIN_DEP_MAP_INIT(lockname)
  9065. +#endif
  9066. +
  9067. +#ifdef CONFIG_DEBUG_SPINLOCK
  9068. +# define SPIN_DEBUG_INIT(lockname) \
  9069. + .magic = SPINLOCK_MAGIC, \
  9070. + .owner_cpu = -1, \
  9071. + .owner = SPINLOCK_OWNER_INIT,
  9072. +#else
  9073. +# define SPIN_DEBUG_INIT(lockname)
  9074. +#endif
  9075. +
  9076. +#define __RAW_SPIN_LOCK_INITIALIZER(lockname) \
  9077. + { \
  9078. + .raw_lock = __ARCH_SPIN_LOCK_UNLOCKED, \
  9079. + SPIN_DEBUG_INIT(lockname) \
  9080. + SPIN_DEP_MAP_INIT(lockname) }
  9081. +
  9082. +#define __RAW_SPIN_LOCK_UNLOCKED(lockname) \
  9083. + (raw_spinlock_t) __RAW_SPIN_LOCK_INITIALIZER(lockname)
  9084. +
  9085. +#define DEFINE_RAW_SPINLOCK(x) raw_spinlock_t x = __RAW_SPIN_LOCK_UNLOCKED(x)
  9086. +
  9087. +#endif
  9088. diff -Nur linux-4.9.28.orig/include/linux/spinlock_types_rt.h linux-4.9.28/include/linux/spinlock_types_rt.h
  9089. --- linux-4.9.28.orig/include/linux/spinlock_types_rt.h 1970-01-01 01:00:00.000000000 +0100
  9090. +++ linux-4.9.28/include/linux/spinlock_types_rt.h 2017-05-19 03:37:25.174176493 +0200
  9091. @@ -0,0 +1,48 @@
  9092. +#ifndef __LINUX_SPINLOCK_TYPES_RT_H
  9093. +#define __LINUX_SPINLOCK_TYPES_RT_H
  9094. +
  9095. +#ifndef __LINUX_SPINLOCK_TYPES_H
  9096. +#error "Do not include directly. Include spinlock_types.h instead"
  9097. +#endif
  9098. +
  9099. +#include <linux/cache.h>
  9100. +
  9101. +/*
  9102. + * PREEMPT_RT: spinlocks - an RT mutex plus lock-break field:
  9103. + */
  9104. +typedef struct spinlock {
  9105. + struct rt_mutex lock;
  9106. + unsigned int break_lock;
  9107. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  9108. + struct lockdep_map dep_map;
  9109. +#endif
  9110. +} spinlock_t;
  9111. +
  9112. +#ifdef CONFIG_DEBUG_RT_MUTEXES
  9113. +# define __RT_SPIN_INITIALIZER(name) \
  9114. + { \
  9115. + .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock), \
  9116. + .save_state = 1, \
  9117. + .file = __FILE__, \
  9118. + .line = __LINE__ , \
  9119. + }
  9120. +#else
  9121. +# define __RT_SPIN_INITIALIZER(name) \
  9122. + { \
  9123. + .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock), \
  9124. + .save_state = 1, \
  9125. + }
  9126. +#endif
  9127. +
  9128. +/*
  9129. +.wait_list = PLIST_HEAD_INIT_RAW((name).lock.wait_list, (name).lock.wait_lock)
  9130. +*/
  9131. +
  9132. +#define __SPIN_LOCK_UNLOCKED(name) \
  9133. + { .lock = __RT_SPIN_INITIALIZER(name.lock), \
  9134. + SPIN_DEP_MAP_INIT(name) }
  9135. +
  9136. +#define DEFINE_SPINLOCK(name) \
  9137. + spinlock_t name = __SPIN_LOCK_UNLOCKED(name)
  9138. +
  9139. +#endif
  9140. diff -Nur linux-4.9.28.orig/include/linux/srcu.h linux-4.9.28/include/linux/srcu.h
  9141. --- linux-4.9.28.orig/include/linux/srcu.h 2017-05-14 14:00:37.000000000 +0200
  9142. +++ linux-4.9.28/include/linux/srcu.h 2017-05-19 03:37:25.174176493 +0200
  9143. @@ -84,10 +84,10 @@
  9144. void process_srcu(struct work_struct *work);
  9145. -#define __SRCU_STRUCT_INIT(name) \
  9146. +#define __SRCU_STRUCT_INIT(name, pcpu_name) \
  9147. { \
  9148. .completed = -300, \
  9149. - .per_cpu_ref = &name##_srcu_array, \
  9150. + .per_cpu_ref = &pcpu_name, \
  9151. .queue_lock = __SPIN_LOCK_UNLOCKED(name.queue_lock), \
  9152. .running = false, \
  9153. .batch_queue = RCU_BATCH_INIT(name.batch_queue), \
  9154. @@ -119,7 +119,7 @@
  9155. */
  9156. #define __DEFINE_SRCU(name, is_static) \
  9157. static DEFINE_PER_CPU(struct srcu_struct_array, name##_srcu_array);\
  9158. - is_static struct srcu_struct name = __SRCU_STRUCT_INIT(name)
  9159. + is_static struct srcu_struct name = __SRCU_STRUCT_INIT(name, name##_srcu_array)
  9160. #define DEFINE_SRCU(name) __DEFINE_SRCU(name, /* not static */)
  9161. #define DEFINE_STATIC_SRCU(name) __DEFINE_SRCU(name, static)
  9162. diff -Nur linux-4.9.28.orig/include/linux/suspend.h linux-4.9.28/include/linux/suspend.h
  9163. --- linux-4.9.28.orig/include/linux/suspend.h 2017-05-14 14:00:37.000000000 +0200
  9164. +++ linux-4.9.28/include/linux/suspend.h 2017-05-19 03:37:25.174176493 +0200
  9165. @@ -193,6 +193,12 @@
  9166. void (*end)(void);
  9167. };
  9168. +#if defined(CONFIG_SUSPEND) || defined(CONFIG_HIBERNATION)
  9169. +extern bool pm_in_action;
  9170. +#else
  9171. +# define pm_in_action false
  9172. +#endif
  9173. +
  9174. #ifdef CONFIG_SUSPEND
  9175. /**
  9176. * suspend_set_ops - set platform dependent suspend operations
  9177. diff -Nur linux-4.9.28.orig/include/linux/swait.h linux-4.9.28/include/linux/swait.h
  9178. --- linux-4.9.28.orig/include/linux/swait.h 2017-05-14 14:00:37.000000000 +0200
  9179. +++ linux-4.9.28/include/linux/swait.h 2017-05-19 03:37:25.174176493 +0200
  9180. @@ -87,6 +87,7 @@
  9181. extern void swake_up(struct swait_queue_head *q);
  9182. extern void swake_up_all(struct swait_queue_head *q);
  9183. extern void swake_up_locked(struct swait_queue_head *q);
  9184. +extern void swake_up_all_locked(struct swait_queue_head *q);
  9185. extern void __prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait);
  9186. extern void prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait, int state);
  9187. diff -Nur linux-4.9.28.orig/include/linux/swap.h linux-4.9.28/include/linux/swap.h
  9188. --- linux-4.9.28.orig/include/linux/swap.h 2017-05-14 14:00:37.000000000 +0200
  9189. +++ linux-4.9.28/include/linux/swap.h 2017-05-19 03:37:25.174176493 +0200
  9190. @@ -11,6 +11,7 @@
  9191. #include <linux/fs.h>
  9192. #include <linux/atomic.h>
  9193. #include <linux/page-flags.h>
  9194. +#include <linux/locallock.h>
  9195. #include <asm/page.h>
  9196. struct notifier_block;
  9197. @@ -247,7 +248,8 @@
  9198. void *workingset_eviction(struct address_space *mapping, struct page *page);
  9199. bool workingset_refault(void *shadow);
  9200. void workingset_activation(struct page *page);
  9201. -extern struct list_lru workingset_shadow_nodes;
  9202. +extern struct list_lru __workingset_shadow_nodes;
  9203. +DECLARE_LOCAL_IRQ_LOCK(workingset_shadow_lock);
  9204. static inline unsigned int workingset_node_pages(struct radix_tree_node *node)
  9205. {
  9206. @@ -292,6 +294,7 @@
  9207. /* linux/mm/swap.c */
  9208. +DECLARE_LOCAL_IRQ_LOCK(swapvec_lock);
  9209. extern void lru_cache_add(struct page *);
  9210. extern void lru_cache_add_anon(struct page *page);
  9211. extern void lru_cache_add_file(struct page *page);
  9212. diff -Nur linux-4.9.28.orig/include/linux/swork.h linux-4.9.28/include/linux/swork.h
  9213. --- linux-4.9.28.orig/include/linux/swork.h 1970-01-01 01:00:00.000000000 +0100
  9214. +++ linux-4.9.28/include/linux/swork.h 2017-05-19 03:37:25.174176493 +0200
  9215. @@ -0,0 +1,24 @@
  9216. +#ifndef _LINUX_SWORK_H
  9217. +#define _LINUX_SWORK_H
  9218. +
  9219. +#include <linux/list.h>
  9220. +
  9221. +struct swork_event {
  9222. + struct list_head item;
  9223. + unsigned long flags;
  9224. + void (*func)(struct swork_event *);
  9225. +};
  9226. +
  9227. +static inline void INIT_SWORK(struct swork_event *event,
  9228. + void (*func)(struct swork_event *))
  9229. +{
  9230. + event->flags = 0;
  9231. + event->func = func;
  9232. +}
  9233. +
  9234. +bool swork_queue(struct swork_event *sev);
  9235. +
  9236. +int swork_get(void);
  9237. +void swork_put(void);
  9238. +
  9239. +#endif /* _LINUX_SWORK_H */
  9240. diff -Nur linux-4.9.28.orig/include/linux/thread_info.h linux-4.9.28/include/linux/thread_info.h
  9241. --- linux-4.9.28.orig/include/linux/thread_info.h 2017-05-14 14:00:37.000000000 +0200
  9242. +++ linux-4.9.28/include/linux/thread_info.h 2017-05-19 03:37:25.174176493 +0200
  9243. @@ -107,7 +107,17 @@
  9244. #define test_thread_flag(flag) \
  9245. test_ti_thread_flag(current_thread_info(), flag)
  9246. -#define tif_need_resched() test_thread_flag(TIF_NEED_RESCHED)
  9247. +#ifdef CONFIG_PREEMPT_LAZY
  9248. +#define tif_need_resched() (test_thread_flag(TIF_NEED_RESCHED) || \
  9249. + test_thread_flag(TIF_NEED_RESCHED_LAZY))
  9250. +#define tif_need_resched_now() (test_thread_flag(TIF_NEED_RESCHED))
  9251. +#define tif_need_resched_lazy() test_thread_flag(TIF_NEED_RESCHED_LAZY))
  9252. +
  9253. +#else
  9254. +#define tif_need_resched() test_thread_flag(TIF_NEED_RESCHED)
  9255. +#define tif_need_resched_now() test_thread_flag(TIF_NEED_RESCHED)
  9256. +#define tif_need_resched_lazy() 0
  9257. +#endif
  9258. #ifndef CONFIG_HAVE_ARCH_WITHIN_STACK_FRAMES
  9259. static inline int arch_within_stack_frames(const void * const stack,
  9260. diff -Nur linux-4.9.28.orig/include/linux/timer.h linux-4.9.28/include/linux/timer.h
  9261. --- linux-4.9.28.orig/include/linux/timer.h 2017-05-14 14:00:37.000000000 +0200
  9262. +++ linux-4.9.28/include/linux/timer.h 2017-05-19 03:37:25.174176493 +0200
  9263. @@ -241,7 +241,7 @@
  9264. extern int try_to_del_timer_sync(struct timer_list *timer);
  9265. -#ifdef CONFIG_SMP
  9266. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  9267. extern int del_timer_sync(struct timer_list *timer);
  9268. #else
  9269. # define del_timer_sync(t) del_timer(t)
  9270. diff -Nur linux-4.9.28.orig/include/linux/trace_events.h linux-4.9.28/include/linux/trace_events.h
  9271. --- linux-4.9.28.orig/include/linux/trace_events.h 2017-05-14 14:00:37.000000000 +0200
  9272. +++ linux-4.9.28/include/linux/trace_events.h 2017-05-19 03:37:25.174176493 +0200
  9273. @@ -56,6 +56,9 @@
  9274. unsigned char flags;
  9275. unsigned char preempt_count;
  9276. int pid;
  9277. + unsigned short migrate_disable;
  9278. + unsigned short padding;
  9279. + unsigned char preempt_lazy_count;
  9280. };
  9281. #define TRACE_EVENT_TYPE_MAX \
  9282. diff -Nur linux-4.9.28.orig/include/linux/uaccess.h linux-4.9.28/include/linux/uaccess.h
  9283. --- linux-4.9.28.orig/include/linux/uaccess.h 2017-05-14 14:00:37.000000000 +0200
  9284. +++ linux-4.9.28/include/linux/uaccess.h 2017-05-19 03:37:25.178176648 +0200
  9285. @@ -24,6 +24,7 @@
  9286. */
  9287. static inline void pagefault_disable(void)
  9288. {
  9289. + migrate_disable();
  9290. pagefault_disabled_inc();
  9291. /*
  9292. * make sure to have issued the store before a pagefault
  9293. @@ -40,6 +41,7 @@
  9294. */
  9295. barrier();
  9296. pagefault_disabled_dec();
  9297. + migrate_enable();
  9298. }
  9299. /*
  9300. diff -Nur linux-4.9.28.orig/include/linux/uprobes.h linux-4.9.28/include/linux/uprobes.h
  9301. --- linux-4.9.28.orig/include/linux/uprobes.h 2017-05-14 14:00:37.000000000 +0200
  9302. +++ linux-4.9.28/include/linux/uprobes.h 2017-05-19 03:37:25.178176648 +0200
  9303. @@ -27,6 +27,7 @@
  9304. #include <linux/errno.h>
  9305. #include <linux/rbtree.h>
  9306. #include <linux/types.h>
  9307. +#include <linux/wait.h>
  9308. struct vm_area_struct;
  9309. struct mm_struct;
  9310. diff -Nur linux-4.9.28.orig/include/linux/vmstat.h linux-4.9.28/include/linux/vmstat.h
  9311. --- linux-4.9.28.orig/include/linux/vmstat.h 2017-05-14 14:00:37.000000000 +0200
  9312. +++ linux-4.9.28/include/linux/vmstat.h 2017-05-19 03:37:25.178176648 +0200
  9313. @@ -33,7 +33,9 @@
  9314. */
  9315. static inline void __count_vm_event(enum vm_event_item item)
  9316. {
  9317. + preempt_disable_rt();
  9318. raw_cpu_inc(vm_event_states.event[item]);
  9319. + preempt_enable_rt();
  9320. }
  9321. static inline void count_vm_event(enum vm_event_item item)
  9322. @@ -43,7 +45,9 @@
  9323. static inline void __count_vm_events(enum vm_event_item item, long delta)
  9324. {
  9325. + preempt_disable_rt();
  9326. raw_cpu_add(vm_event_states.event[item], delta);
  9327. + preempt_enable_rt();
  9328. }
  9329. static inline void count_vm_events(enum vm_event_item item, long delta)
  9330. diff -Nur linux-4.9.28.orig/include/linux/wait.h linux-4.9.28/include/linux/wait.h
  9331. --- linux-4.9.28.orig/include/linux/wait.h 2017-05-14 14:00:37.000000000 +0200
  9332. +++ linux-4.9.28/include/linux/wait.h 2017-05-19 03:37:25.178176648 +0200
  9333. @@ -8,6 +8,7 @@
  9334. #include <linux/spinlock.h>
  9335. #include <asm/current.h>
  9336. #include <uapi/linux/wait.h>
  9337. +#include <linux/atomic.h>
  9338. typedef struct __wait_queue wait_queue_t;
  9339. typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key);
  9340. diff -Nur linux-4.9.28.orig/include/net/dst.h linux-4.9.28/include/net/dst.h
  9341. --- linux-4.9.28.orig/include/net/dst.h 2017-05-14 14:00:37.000000000 +0200
  9342. +++ linux-4.9.28/include/net/dst.h 2017-05-19 03:37:25.178176648 +0200
  9343. @@ -446,7 +446,7 @@
  9344. static inline int dst_neigh_output(struct dst_entry *dst, struct neighbour *n,
  9345. struct sk_buff *skb)
  9346. {
  9347. - const struct hh_cache *hh;
  9348. + struct hh_cache *hh;
  9349. if (dst->pending_confirm) {
  9350. unsigned long now = jiffies;
  9351. diff -Nur linux-4.9.28.orig/include/net/gen_stats.h linux-4.9.28/include/net/gen_stats.h
  9352. --- linux-4.9.28.orig/include/net/gen_stats.h 2017-05-14 14:00:37.000000000 +0200
  9353. +++ linux-4.9.28/include/net/gen_stats.h 2017-05-19 03:37:25.178176648 +0200
  9354. @@ -5,6 +5,7 @@
  9355. #include <linux/socket.h>
  9356. #include <linux/rtnetlink.h>
  9357. #include <linux/pkt_sched.h>
  9358. +#include <net/net_seq_lock.h>
  9359. struct gnet_stats_basic_cpu {
  9360. struct gnet_stats_basic_packed bstats;
  9361. @@ -33,11 +34,11 @@
  9362. spinlock_t *lock, struct gnet_dump *d,
  9363. int padattr);
  9364. -int gnet_stats_copy_basic(const seqcount_t *running,
  9365. +int gnet_stats_copy_basic(net_seqlock_t *running,
  9366. struct gnet_dump *d,
  9367. struct gnet_stats_basic_cpu __percpu *cpu,
  9368. struct gnet_stats_basic_packed *b);
  9369. -void __gnet_stats_copy_basic(const seqcount_t *running,
  9370. +void __gnet_stats_copy_basic(net_seqlock_t *running,
  9371. struct gnet_stats_basic_packed *bstats,
  9372. struct gnet_stats_basic_cpu __percpu *cpu,
  9373. struct gnet_stats_basic_packed *b);
  9374. @@ -55,14 +56,14 @@
  9375. struct gnet_stats_basic_cpu __percpu *cpu_bstats,
  9376. struct gnet_stats_rate_est64 *rate_est,
  9377. spinlock_t *stats_lock,
  9378. - seqcount_t *running, struct nlattr *opt);
  9379. + net_seqlock_t *running, struct nlattr *opt);
  9380. void gen_kill_estimator(struct gnet_stats_basic_packed *bstats,
  9381. struct gnet_stats_rate_est64 *rate_est);
  9382. int gen_replace_estimator(struct gnet_stats_basic_packed *bstats,
  9383. struct gnet_stats_basic_cpu __percpu *cpu_bstats,
  9384. struct gnet_stats_rate_est64 *rate_est,
  9385. spinlock_t *stats_lock,
  9386. - seqcount_t *running, struct nlattr *opt);
  9387. + net_seqlock_t *running, struct nlattr *opt);
  9388. bool gen_estimator_active(const struct gnet_stats_basic_packed *bstats,
  9389. const struct gnet_stats_rate_est64 *rate_est);
  9390. #endif
  9391. diff -Nur linux-4.9.28.orig/include/net/neighbour.h linux-4.9.28/include/net/neighbour.h
  9392. --- linux-4.9.28.orig/include/net/neighbour.h 2017-05-14 14:00:37.000000000 +0200
  9393. +++ linux-4.9.28/include/net/neighbour.h 2017-05-19 03:37:25.178176648 +0200
  9394. @@ -446,7 +446,7 @@
  9395. }
  9396. #endif
  9397. -static inline int neigh_hh_output(const struct hh_cache *hh, struct sk_buff *skb)
  9398. +static inline int neigh_hh_output(struct hh_cache *hh, struct sk_buff *skb)
  9399. {
  9400. unsigned int seq;
  9401. int hh_len;
  9402. @@ -501,7 +501,7 @@
  9403. #define NEIGH_CB(skb) ((struct neighbour_cb *)(skb)->cb)
  9404. -static inline void neigh_ha_snapshot(char *dst, const struct neighbour *n,
  9405. +static inline void neigh_ha_snapshot(char *dst, struct neighbour *n,
  9406. const struct net_device *dev)
  9407. {
  9408. unsigned int seq;
  9409. diff -Nur linux-4.9.28.orig/include/net/netns/ipv4.h linux-4.9.28/include/net/netns/ipv4.h
  9410. --- linux-4.9.28.orig/include/net/netns/ipv4.h 2017-05-14 14:00:37.000000000 +0200
  9411. +++ linux-4.9.28/include/net/netns/ipv4.h 2017-05-19 03:37:25.178176648 +0200
  9412. @@ -69,6 +69,7 @@
  9413. int sysctl_icmp_echo_ignore_all;
  9414. int sysctl_icmp_echo_ignore_broadcasts;
  9415. + int sysctl_icmp_echo_sysrq;
  9416. int sysctl_icmp_ignore_bogus_error_responses;
  9417. int sysctl_icmp_ratelimit;
  9418. int sysctl_icmp_ratemask;
  9419. diff -Nur linux-4.9.28.orig/include/net/net_seq_lock.h linux-4.9.28/include/net/net_seq_lock.h
  9420. --- linux-4.9.28.orig/include/net/net_seq_lock.h 1970-01-01 01:00:00.000000000 +0100
  9421. +++ linux-4.9.28/include/net/net_seq_lock.h 2017-05-19 03:37:25.178176648 +0200
  9422. @@ -0,0 +1,15 @@
  9423. +#ifndef __NET_NET_SEQ_LOCK_H__
  9424. +#define __NET_NET_SEQ_LOCK_H__
  9425. +
  9426. +#ifdef CONFIG_PREEMPT_RT_BASE
  9427. +# define net_seqlock_t seqlock_t
  9428. +# define net_seq_begin(__r) read_seqbegin(__r)
  9429. +# define net_seq_retry(__r, __s) read_seqretry(__r, __s)
  9430. +
  9431. +#else
  9432. +# define net_seqlock_t seqcount_t
  9433. +# define net_seq_begin(__r) read_seqcount_begin(__r)
  9434. +# define net_seq_retry(__r, __s) read_seqcount_retry(__r, __s)
  9435. +#endif
  9436. +
  9437. +#endif
  9438. diff -Nur linux-4.9.28.orig/include/net/sch_generic.h linux-4.9.28/include/net/sch_generic.h
  9439. --- linux-4.9.28.orig/include/net/sch_generic.h 2017-05-14 14:00:37.000000000 +0200
  9440. +++ linux-4.9.28/include/net/sch_generic.h 2017-05-19 03:37:25.178176648 +0200
  9441. @@ -10,6 +10,7 @@
  9442. #include <linux/dynamic_queue_limits.h>
  9443. #include <net/gen_stats.h>
  9444. #include <net/rtnetlink.h>
  9445. +#include <net/net_seq_lock.h>
  9446. struct Qdisc_ops;
  9447. struct qdisc_walker;
  9448. @@ -86,7 +87,7 @@
  9449. struct sk_buff *gso_skb ____cacheline_aligned_in_smp;
  9450. struct qdisc_skb_head q;
  9451. struct gnet_stats_basic_packed bstats;
  9452. - seqcount_t running;
  9453. + net_seqlock_t running;
  9454. struct gnet_stats_queue qstats;
  9455. unsigned long state;
  9456. struct Qdisc *next_sched;
  9457. @@ -98,13 +99,22 @@
  9458. spinlock_t busylock ____cacheline_aligned_in_smp;
  9459. };
  9460. -static inline bool qdisc_is_running(const struct Qdisc *qdisc)
  9461. +static inline bool qdisc_is_running(struct Qdisc *qdisc)
  9462. {
  9463. +#ifdef CONFIG_PREEMPT_RT_BASE
  9464. + return spin_is_locked(&qdisc->running.lock) ? true : false;
  9465. +#else
  9466. return (raw_read_seqcount(&qdisc->running) & 1) ? true : false;
  9467. +#endif
  9468. }
  9469. static inline bool qdisc_run_begin(struct Qdisc *qdisc)
  9470. {
  9471. +#ifdef CONFIG_PREEMPT_RT_BASE
  9472. + if (try_write_seqlock(&qdisc->running))
  9473. + return true;
  9474. + return false;
  9475. +#else
  9476. if (qdisc_is_running(qdisc))
  9477. return false;
  9478. /* Variant of write_seqcount_begin() telling lockdep a trylock
  9479. @@ -113,11 +123,16 @@
  9480. raw_write_seqcount_begin(&qdisc->running);
  9481. seqcount_acquire(&qdisc->running.dep_map, 0, 1, _RET_IP_);
  9482. return true;
  9483. +#endif
  9484. }
  9485. static inline void qdisc_run_end(struct Qdisc *qdisc)
  9486. {
  9487. +#ifdef CONFIG_PREEMPT_RT_BASE
  9488. + write_sequnlock(&qdisc->running);
  9489. +#else
  9490. write_seqcount_end(&qdisc->running);
  9491. +#endif
  9492. }
  9493. static inline bool qdisc_may_bulk(const struct Qdisc *qdisc)
  9494. @@ -308,7 +323,7 @@
  9495. return qdisc_lock(root);
  9496. }
  9497. -static inline seqcount_t *qdisc_root_sleeping_running(const struct Qdisc *qdisc)
  9498. +static inline net_seqlock_t *qdisc_root_sleeping_running(const struct Qdisc *qdisc)
  9499. {
  9500. struct Qdisc *root = qdisc_root_sleeping(qdisc);
  9501. diff -Nur linux-4.9.28.orig/include/trace/events/hist.h linux-4.9.28/include/trace/events/hist.h
  9502. --- linux-4.9.28.orig/include/trace/events/hist.h 1970-01-01 01:00:00.000000000 +0100
  9503. +++ linux-4.9.28/include/trace/events/hist.h 2017-05-19 03:37:25.178176648 +0200
  9504. @@ -0,0 +1,73 @@
  9505. +#undef TRACE_SYSTEM
  9506. +#define TRACE_SYSTEM hist
  9507. +
  9508. +#if !defined(_TRACE_HIST_H) || defined(TRACE_HEADER_MULTI_READ)
  9509. +#define _TRACE_HIST_H
  9510. +
  9511. +#include "latency_hist.h"
  9512. +#include <linux/tracepoint.h>
  9513. +
  9514. +#if !defined(CONFIG_PREEMPT_OFF_HIST) && !defined(CONFIG_INTERRUPT_OFF_HIST)
  9515. +#define trace_preemptirqsoff_hist(a, b)
  9516. +#define trace_preemptirqsoff_hist_rcuidle(a, b)
  9517. +#else
  9518. +TRACE_EVENT(preemptirqsoff_hist,
  9519. +
  9520. + TP_PROTO(int reason, int starthist),
  9521. +
  9522. + TP_ARGS(reason, starthist),
  9523. +
  9524. + TP_STRUCT__entry(
  9525. + __field(int, reason)
  9526. + __field(int, starthist)
  9527. + ),
  9528. +
  9529. + TP_fast_assign(
  9530. + __entry->reason = reason;
  9531. + __entry->starthist = starthist;
  9532. + ),
  9533. +
  9534. + TP_printk("reason=%s starthist=%s", getaction(__entry->reason),
  9535. + __entry->starthist ? "start" : "stop")
  9536. +);
  9537. +#endif
  9538. +
  9539. +#ifndef CONFIG_MISSED_TIMER_OFFSETS_HIST
  9540. +#define trace_hrtimer_interrupt(a, b, c, d)
  9541. +#else
  9542. +TRACE_EVENT(hrtimer_interrupt,
  9543. +
  9544. + TP_PROTO(int cpu, long long offset, struct task_struct *curr,
  9545. + struct task_struct *task),
  9546. +
  9547. + TP_ARGS(cpu, offset, curr, task),
  9548. +
  9549. + TP_STRUCT__entry(
  9550. + __field(int, cpu)
  9551. + __field(long long, offset)
  9552. + __array(char, ccomm, TASK_COMM_LEN)
  9553. + __field(int, cprio)
  9554. + __array(char, tcomm, TASK_COMM_LEN)
  9555. + __field(int, tprio)
  9556. + ),
  9557. +
  9558. + TP_fast_assign(
  9559. + __entry->cpu = cpu;
  9560. + __entry->offset = offset;
  9561. + memcpy(__entry->ccomm, curr->comm, TASK_COMM_LEN);
  9562. + __entry->cprio = curr->prio;
  9563. + memcpy(__entry->tcomm, task != NULL ? task->comm : "<none>",
  9564. + task != NULL ? TASK_COMM_LEN : 7);
  9565. + __entry->tprio = task != NULL ? task->prio : -1;
  9566. + ),
  9567. +
  9568. + TP_printk("cpu=%d offset=%lld curr=%s[%d] thread=%s[%d]",
  9569. + __entry->cpu, __entry->offset, __entry->ccomm,
  9570. + __entry->cprio, __entry->tcomm, __entry->tprio)
  9571. +);
  9572. +#endif
  9573. +
  9574. +#endif /* _TRACE_HIST_H */
  9575. +
  9576. +/* This part must be outside protection */
  9577. +#include <trace/define_trace.h>
  9578. diff -Nur linux-4.9.28.orig/include/trace/events/latency_hist.h linux-4.9.28/include/trace/events/latency_hist.h
  9579. --- linux-4.9.28.orig/include/trace/events/latency_hist.h 1970-01-01 01:00:00.000000000 +0100
  9580. +++ linux-4.9.28/include/trace/events/latency_hist.h 2017-05-19 03:37:25.178176648 +0200
  9581. @@ -0,0 +1,29 @@
  9582. +#ifndef _LATENCY_HIST_H
  9583. +#define _LATENCY_HIST_H
  9584. +
  9585. +enum hist_action {
  9586. + IRQS_ON,
  9587. + PREEMPT_ON,
  9588. + TRACE_STOP,
  9589. + IRQS_OFF,
  9590. + PREEMPT_OFF,
  9591. + TRACE_START,
  9592. +};
  9593. +
  9594. +static char *actions[] = {
  9595. + "IRQS_ON",
  9596. + "PREEMPT_ON",
  9597. + "TRACE_STOP",
  9598. + "IRQS_OFF",
  9599. + "PREEMPT_OFF",
  9600. + "TRACE_START",
  9601. +};
  9602. +
  9603. +static inline char *getaction(int action)
  9604. +{
  9605. + if (action >= 0 && action <= sizeof(actions)/sizeof(actions[0]))
  9606. + return actions[action];
  9607. + return "unknown";
  9608. +}
  9609. +
  9610. +#endif /* _LATENCY_HIST_H */
  9611. diff -Nur linux-4.9.28.orig/include/trace/events/sched.h linux-4.9.28/include/trace/events/sched.h
  9612. --- linux-4.9.28.orig/include/trace/events/sched.h 2017-05-14 14:00:37.000000000 +0200
  9613. +++ linux-4.9.28/include/trace/events/sched.h 2017-05-19 03:37:25.178176648 +0200
  9614. @@ -70,7 +70,7 @@
  9615. TP_fast_assign(
  9616. memcpy(__entry->comm, p->comm, TASK_COMM_LEN);
  9617. __entry->pid = p->pid;
  9618. - __entry->prio = p->prio;
  9619. + __entry->prio = p->prio; /* XXX SCHED_DEADLINE */
  9620. __entry->success = 1; /* rudiment, kill when possible */
  9621. __entry->target_cpu = task_cpu(p);
  9622. ),
  9623. @@ -147,6 +147,7 @@
  9624. memcpy(__entry->prev_comm, prev->comm, TASK_COMM_LEN);
  9625. __entry->next_pid = next->pid;
  9626. __entry->next_prio = next->prio;
  9627. + /* XXX SCHED_DEADLINE */
  9628. ),
  9629. TP_printk("prev_comm=%s prev_pid=%d prev_prio=%d prev_state=%s%s ==> next_comm=%s next_pid=%d next_prio=%d",
  9630. @@ -181,7 +182,7 @@
  9631. TP_fast_assign(
  9632. memcpy(__entry->comm, p->comm, TASK_COMM_LEN);
  9633. __entry->pid = p->pid;
  9634. - __entry->prio = p->prio;
  9635. + __entry->prio = p->prio; /* XXX SCHED_DEADLINE */
  9636. __entry->orig_cpu = task_cpu(p);
  9637. __entry->dest_cpu = dest_cpu;
  9638. ),
  9639. @@ -206,7 +207,7 @@
  9640. TP_fast_assign(
  9641. memcpy(__entry->comm, p->comm, TASK_COMM_LEN);
  9642. __entry->pid = p->pid;
  9643. - __entry->prio = p->prio;
  9644. + __entry->prio = p->prio; /* XXX SCHED_DEADLINE */
  9645. ),
  9646. TP_printk("comm=%s pid=%d prio=%d",
  9647. @@ -253,7 +254,7 @@
  9648. TP_fast_assign(
  9649. memcpy(__entry->comm, current->comm, TASK_COMM_LEN);
  9650. __entry->pid = pid_nr(pid);
  9651. - __entry->prio = current->prio;
  9652. + __entry->prio = current->prio; /* XXX SCHED_DEADLINE */
  9653. ),
  9654. TP_printk("comm=%s pid=%d prio=%d",
  9655. @@ -413,9 +414,9 @@
  9656. */
  9657. TRACE_EVENT(sched_pi_setprio,
  9658. - TP_PROTO(struct task_struct *tsk, int newprio),
  9659. + TP_PROTO(struct task_struct *tsk, struct task_struct *pi_task),
  9660. - TP_ARGS(tsk, newprio),
  9661. + TP_ARGS(tsk, pi_task),
  9662. TP_STRUCT__entry(
  9663. __array( char, comm, TASK_COMM_LEN )
  9664. @@ -428,7 +429,8 @@
  9665. memcpy(__entry->comm, tsk->comm, TASK_COMM_LEN);
  9666. __entry->pid = tsk->pid;
  9667. __entry->oldprio = tsk->prio;
  9668. - __entry->newprio = newprio;
  9669. + __entry->newprio = pi_task ? pi_task->prio : tsk->prio;
  9670. + /* XXX SCHED_DEADLINE bits missing */
  9671. ),
  9672. TP_printk("comm=%s pid=%d oldprio=%d newprio=%d",
  9673. diff -Nur linux-4.9.28.orig/init/Kconfig linux-4.9.28/init/Kconfig
  9674. --- linux-4.9.28.orig/init/Kconfig 2017-05-14 14:00:37.000000000 +0200
  9675. +++ linux-4.9.28/init/Kconfig 2017-05-19 03:37:25.178176648 +0200
  9676. @@ -506,7 +506,7 @@
  9677. config RCU_EXPERT
  9678. bool "Make expert-level adjustments to RCU configuration"
  9679. - default n
  9680. + default y if PREEMPT_RT_FULL
  9681. help
  9682. This option needs to be enabled if you wish to make
  9683. expert-level adjustments to RCU configuration. By default,
  9684. @@ -623,7 +623,7 @@
  9685. config RCU_FAST_NO_HZ
  9686. bool "Accelerate last non-dyntick-idle CPU's grace periods"
  9687. - depends on NO_HZ_COMMON && SMP && RCU_EXPERT
  9688. + depends on NO_HZ_COMMON && SMP && RCU_EXPERT && !PREEMPT_RT_FULL
  9689. default n
  9690. help
  9691. This option permits CPUs to enter dynticks-idle state even if
  9692. @@ -650,7 +650,7 @@
  9693. config RCU_BOOST
  9694. bool "Enable RCU priority boosting"
  9695. depends on RT_MUTEXES && PREEMPT_RCU && RCU_EXPERT
  9696. - default n
  9697. + default y if PREEMPT_RT_FULL
  9698. help
  9699. This option boosts the priority of preempted RCU readers that
  9700. block the current preemptible RCU grace period for too long.
  9701. @@ -781,19 +781,6 @@
  9702. endchoice
  9703. -config RCU_EXPEDITE_BOOT
  9704. - bool
  9705. - default n
  9706. - help
  9707. - This option enables expedited grace periods at boot time,
  9708. - as if rcu_expedite_gp() had been invoked early in boot.
  9709. - The corresponding rcu_unexpedite_gp() is invoked from
  9710. - rcu_end_inkernel_boot(), which is intended to be invoked
  9711. - at the end of the kernel-only boot sequence, just before
  9712. - init is exec'ed.
  9713. -
  9714. - Accept the default if unsure.
  9715. -
  9716. endmenu # "RCU Subsystem"
  9717. config BUILD_BIN2C
  9718. @@ -1064,6 +1051,7 @@
  9719. config RT_GROUP_SCHED
  9720. bool "Group scheduling for SCHED_RR/FIFO"
  9721. depends on CGROUP_SCHED
  9722. + depends on !PREEMPT_RT_FULL
  9723. default n
  9724. help
  9725. This feature lets you explicitly allocate real CPU bandwidth
  9726. @@ -1772,6 +1760,7 @@
  9727. config SLAB
  9728. bool "SLAB"
  9729. + depends on !PREEMPT_RT_FULL
  9730. select HAVE_HARDENED_USERCOPY_ALLOCATOR
  9731. help
  9732. The regular slab allocator that is established and known to work
  9733. @@ -1792,6 +1781,7 @@
  9734. config SLOB
  9735. depends on EXPERT
  9736. bool "SLOB (Simple Allocator)"
  9737. + depends on !PREEMPT_RT_FULL
  9738. help
  9739. SLOB replaces the stock allocator with a drastically simpler
  9740. allocator. SLOB is generally more space efficient but
  9741. @@ -1810,7 +1800,7 @@
  9742. config SLUB_CPU_PARTIAL
  9743. default y
  9744. - depends on SLUB && SMP
  9745. + depends on SLUB && SMP && !PREEMPT_RT_FULL
  9746. bool "SLUB per cpu partial cache"
  9747. help
  9748. Per cpu partial caches accellerate objects allocation and freeing
  9749. diff -Nur linux-4.9.28.orig/init/main.c linux-4.9.28/init/main.c
  9750. --- linux-4.9.28.orig/init/main.c 2017-05-14 14:00:37.000000000 +0200
  9751. +++ linux-4.9.28/init/main.c 2017-05-19 03:37:25.178176648 +0200
  9752. @@ -507,6 +507,7 @@
  9753. setup_command_line(command_line);
  9754. setup_nr_cpu_ids();
  9755. setup_per_cpu_areas();
  9756. + softirq_early_init();
  9757. boot_cpu_state_init();
  9758. smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */
  9759. diff -Nur linux-4.9.28.orig/init/Makefile linux-4.9.28/init/Makefile
  9760. --- linux-4.9.28.orig/init/Makefile 2017-05-14 14:00:37.000000000 +0200
  9761. +++ linux-4.9.28/init/Makefile 2017-05-19 03:37:25.178176648 +0200
  9762. @@ -35,4 +35,4 @@
  9763. include/generated/compile.h: FORCE
  9764. @$($(quiet)chk_compile.h)
  9765. $(Q)$(CONFIG_SHELL) $(srctree)/scripts/mkcompile_h $@ \
  9766. - "$(UTS_MACHINE)" "$(CONFIG_SMP)" "$(CONFIG_PREEMPT)" "$(CC) $(KBUILD_CFLAGS)"
  9767. + "$(UTS_MACHINE)" "$(CONFIG_SMP)" "$(CONFIG_PREEMPT)" "$(CONFIG_PREEMPT_RT_FULL)" "$(CC) $(KBUILD_CFLAGS)"
  9768. diff -Nur linux-4.9.28.orig/ipc/sem.c linux-4.9.28/ipc/sem.c
  9769. --- linux-4.9.28.orig/ipc/sem.c 2017-05-14 14:00:37.000000000 +0200
  9770. +++ linux-4.9.28/ipc/sem.c 2017-05-19 03:37:25.178176648 +0200
  9771. @@ -712,6 +712,13 @@
  9772. static void wake_up_sem_queue_prepare(struct list_head *pt,
  9773. struct sem_queue *q, int error)
  9774. {
  9775. +#ifdef CONFIG_PREEMPT_RT_BASE
  9776. + struct task_struct *p = q->sleeper;
  9777. + get_task_struct(p);
  9778. + q->status = error;
  9779. + wake_up_process(p);
  9780. + put_task_struct(p);
  9781. +#else
  9782. if (list_empty(pt)) {
  9783. /*
  9784. * Hold preempt off so that we don't get preempted and have the
  9785. @@ -723,6 +730,7 @@
  9786. q->pid = error;
  9787. list_add_tail(&q->list, pt);
  9788. +#endif
  9789. }
  9790. /**
  9791. @@ -736,6 +744,7 @@
  9792. */
  9793. static void wake_up_sem_queue_do(struct list_head *pt)
  9794. {
  9795. +#ifndef CONFIG_PREEMPT_RT_BASE
  9796. struct sem_queue *q, *t;
  9797. int did_something;
  9798. @@ -748,6 +757,7 @@
  9799. }
  9800. if (did_something)
  9801. preempt_enable();
  9802. +#endif
  9803. }
  9804. static void unlink_queue(struct sem_array *sma, struct sem_queue *q)
  9805. diff -Nur linux-4.9.28.orig/kernel/cgroup.c linux-4.9.28/kernel/cgroup.c
  9806. --- linux-4.9.28.orig/kernel/cgroup.c 2017-05-14 14:00:37.000000000 +0200
  9807. +++ linux-4.9.28/kernel/cgroup.c 2017-05-19 03:37:25.178176648 +0200
  9808. @@ -5041,10 +5041,10 @@
  9809. queue_work(cgroup_destroy_wq, &css->destroy_work);
  9810. }
  9811. -static void css_release_work_fn(struct work_struct *work)
  9812. +static void css_release_work_fn(struct swork_event *sev)
  9813. {
  9814. struct cgroup_subsys_state *css =
  9815. - container_of(work, struct cgroup_subsys_state, destroy_work);
  9816. + container_of(sev, struct cgroup_subsys_state, destroy_swork);
  9817. struct cgroup_subsys *ss = css->ss;
  9818. struct cgroup *cgrp = css->cgroup;
  9819. @@ -5087,8 +5087,8 @@
  9820. struct cgroup_subsys_state *css =
  9821. container_of(ref, struct cgroup_subsys_state, refcnt);
  9822. - INIT_WORK(&css->destroy_work, css_release_work_fn);
  9823. - queue_work(cgroup_destroy_wq, &css->destroy_work);
  9824. + INIT_SWORK(&css->destroy_swork, css_release_work_fn);
  9825. + swork_queue(&css->destroy_swork);
  9826. }
  9827. static void init_and_link_css(struct cgroup_subsys_state *css,
  9828. @@ -5740,6 +5740,7 @@
  9829. */
  9830. cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
  9831. BUG_ON(!cgroup_destroy_wq);
  9832. + BUG_ON(swork_get());
  9833. /*
  9834. * Used to destroy pidlists and separate to serve as flush domain.
  9835. diff -Nur linux-4.9.28.orig/kernel/cpu.c linux-4.9.28/kernel/cpu.c
  9836. --- linux-4.9.28.orig/kernel/cpu.c 2017-05-14 14:00:37.000000000 +0200
  9837. +++ linux-4.9.28/kernel/cpu.c 2017-05-19 03:37:25.182176801 +0200
  9838. @@ -239,6 +239,289 @@
  9839. #define cpuhp_lock_acquire() lock_map_acquire(&cpu_hotplug.dep_map)
  9840. #define cpuhp_lock_release() lock_map_release(&cpu_hotplug.dep_map)
  9841. +/**
  9842. + * hotplug_pcp - per cpu hotplug descriptor
  9843. + * @unplug: set when pin_current_cpu() needs to sync tasks
  9844. + * @sync_tsk: the task that waits for tasks to finish pinned sections
  9845. + * @refcount: counter of tasks in pinned sections
  9846. + * @grab_lock: set when the tasks entering pinned sections should wait
  9847. + * @synced: notifier for @sync_tsk to tell cpu_down it's finished
  9848. + * @mutex: the mutex to make tasks wait (used when @grab_lock is true)
  9849. + * @mutex_init: zero if the mutex hasn't been initialized yet.
  9850. + *
  9851. + * Although @unplug and @sync_tsk may point to the same task, the @unplug
  9852. + * is used as a flag and still exists after @sync_tsk has exited and
  9853. + * @sync_tsk set to NULL.
  9854. + */
  9855. +struct hotplug_pcp {
  9856. + struct task_struct *unplug;
  9857. + struct task_struct *sync_tsk;
  9858. + int refcount;
  9859. + int grab_lock;
  9860. + struct completion synced;
  9861. + struct completion unplug_wait;
  9862. +#ifdef CONFIG_PREEMPT_RT_FULL
  9863. + /*
  9864. + * Note, on PREEMPT_RT, the hotplug lock must save the state of
  9865. + * the task, otherwise the mutex will cause the task to fail
  9866. + * to sleep when required. (Because it's called from migrate_disable())
  9867. + *
  9868. + * The spinlock_t on PREEMPT_RT is a mutex that saves the task's
  9869. + * state.
  9870. + */
  9871. + spinlock_t lock;
  9872. +#else
  9873. + struct mutex mutex;
  9874. +#endif
  9875. + int mutex_init;
  9876. +};
  9877. +
  9878. +#ifdef CONFIG_PREEMPT_RT_FULL
  9879. +# define hotplug_lock(hp) rt_spin_lock__no_mg(&(hp)->lock)
  9880. +# define hotplug_unlock(hp) rt_spin_unlock__no_mg(&(hp)->lock)
  9881. +#else
  9882. +# define hotplug_lock(hp) mutex_lock(&(hp)->mutex)
  9883. +# define hotplug_unlock(hp) mutex_unlock(&(hp)->mutex)
  9884. +#endif
  9885. +
  9886. +static DEFINE_PER_CPU(struct hotplug_pcp, hotplug_pcp);
  9887. +
  9888. +/**
  9889. + * pin_current_cpu - Prevent the current cpu from being unplugged
  9890. + *
  9891. + * Lightweight version of get_online_cpus() to prevent cpu from being
  9892. + * unplugged when code runs in a migration disabled region.
  9893. + *
  9894. + * Must be called with preemption disabled (preempt_count = 1)!
  9895. + */
  9896. +void pin_current_cpu(void)
  9897. +{
  9898. + struct hotplug_pcp *hp;
  9899. + int force = 0;
  9900. +
  9901. +retry:
  9902. + hp = this_cpu_ptr(&hotplug_pcp);
  9903. +
  9904. + if (!hp->unplug || hp->refcount || force || preempt_count() > 1 ||
  9905. + hp->unplug == current) {
  9906. + hp->refcount++;
  9907. + return;
  9908. + }
  9909. + if (hp->grab_lock) {
  9910. + preempt_enable();
  9911. + hotplug_lock(hp);
  9912. + hotplug_unlock(hp);
  9913. + } else {
  9914. + preempt_enable();
  9915. + /*
  9916. + * Try to push this task off of this CPU.
  9917. + */
  9918. + if (!migrate_me()) {
  9919. + preempt_disable();
  9920. + hp = this_cpu_ptr(&hotplug_pcp);
  9921. + if (!hp->grab_lock) {
  9922. + /*
  9923. + * Just let it continue it's already pinned
  9924. + * or about to sleep.
  9925. + */
  9926. + force = 1;
  9927. + goto retry;
  9928. + }
  9929. + preempt_enable();
  9930. + }
  9931. + }
  9932. + preempt_disable();
  9933. + goto retry;
  9934. +}
  9935. +
  9936. +/**
  9937. + * unpin_current_cpu - Allow unplug of current cpu
  9938. + *
  9939. + * Must be called with preemption or interrupts disabled!
  9940. + */
  9941. +void unpin_current_cpu(void)
  9942. +{
  9943. + struct hotplug_pcp *hp = this_cpu_ptr(&hotplug_pcp);
  9944. +
  9945. + WARN_ON(hp->refcount <= 0);
  9946. +
  9947. + /* This is safe. sync_unplug_thread is pinned to this cpu */
  9948. + if (!--hp->refcount && hp->unplug && hp->unplug != current)
  9949. + wake_up_process(hp->unplug);
  9950. +}
  9951. +
  9952. +static void wait_for_pinned_cpus(struct hotplug_pcp *hp)
  9953. +{
  9954. + set_current_state(TASK_UNINTERRUPTIBLE);
  9955. + while (hp->refcount) {
  9956. + schedule_preempt_disabled();
  9957. + set_current_state(TASK_UNINTERRUPTIBLE);
  9958. + }
  9959. +}
  9960. +
  9961. +static int sync_unplug_thread(void *data)
  9962. +{
  9963. + struct hotplug_pcp *hp = data;
  9964. +
  9965. + wait_for_completion(&hp->unplug_wait);
  9966. + preempt_disable();
  9967. + hp->unplug = current;
  9968. + wait_for_pinned_cpus(hp);
  9969. +
  9970. + /*
  9971. + * This thread will synchronize the cpu_down() with threads
  9972. + * that have pinned the CPU. When the pinned CPU count reaches
  9973. + * zero, we inform the cpu_down code to continue to the next step.
  9974. + */
  9975. + set_current_state(TASK_UNINTERRUPTIBLE);
  9976. + preempt_enable();
  9977. + complete(&hp->synced);
  9978. +
  9979. + /*
  9980. + * If all succeeds, the next step will need tasks to wait till
  9981. + * the CPU is offline before continuing. To do this, the grab_lock
  9982. + * is set and tasks going into pin_current_cpu() will block on the
  9983. + * mutex. But we still need to wait for those that are already in
  9984. + * pinned CPU sections. If the cpu_down() failed, the kthread_should_stop()
  9985. + * will kick this thread out.
  9986. + */
  9987. + while (!hp->grab_lock && !kthread_should_stop()) {
  9988. + schedule();
  9989. + set_current_state(TASK_UNINTERRUPTIBLE);
  9990. + }
  9991. +
  9992. + /* Make sure grab_lock is seen before we see a stale completion */
  9993. + smp_mb();
  9994. +
  9995. + /*
  9996. + * Now just before cpu_down() enters stop machine, we need to make
  9997. + * sure all tasks that are in pinned CPU sections are out, and new
  9998. + * tasks will now grab the lock, keeping them from entering pinned
  9999. + * CPU sections.
  10000. + */
  10001. + if (!kthread_should_stop()) {
  10002. + preempt_disable();
  10003. + wait_for_pinned_cpus(hp);
  10004. + preempt_enable();
  10005. + complete(&hp->synced);
  10006. + }
  10007. +
  10008. + set_current_state(TASK_UNINTERRUPTIBLE);
  10009. + while (!kthread_should_stop()) {
  10010. + schedule();
  10011. + set_current_state(TASK_UNINTERRUPTIBLE);
  10012. + }
  10013. + set_current_state(TASK_RUNNING);
  10014. +
  10015. + /*
  10016. + * Force this thread off this CPU as it's going down and
  10017. + * we don't want any more work on this CPU.
  10018. + */
  10019. + current->flags &= ~PF_NO_SETAFFINITY;
  10020. + set_cpus_allowed_ptr(current, cpu_present_mask);
  10021. + migrate_me();
  10022. + return 0;
  10023. +}
  10024. +
  10025. +static void __cpu_unplug_sync(struct hotplug_pcp *hp)
  10026. +{
  10027. + wake_up_process(hp->sync_tsk);
  10028. + wait_for_completion(&hp->synced);
  10029. +}
  10030. +
  10031. +static void __cpu_unplug_wait(unsigned int cpu)
  10032. +{
  10033. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  10034. +
  10035. + complete(&hp->unplug_wait);
  10036. + wait_for_completion(&hp->synced);
  10037. +}
  10038. +
  10039. +/*
  10040. + * Start the sync_unplug_thread on the target cpu and wait for it to
  10041. + * complete.
  10042. + */
  10043. +static int cpu_unplug_begin(unsigned int cpu)
  10044. +{
  10045. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  10046. + int err;
  10047. +
  10048. + /* Protected by cpu_hotplug.lock */
  10049. + if (!hp->mutex_init) {
  10050. +#ifdef CONFIG_PREEMPT_RT_FULL
  10051. + spin_lock_init(&hp->lock);
  10052. +#else
  10053. + mutex_init(&hp->mutex);
  10054. +#endif
  10055. + hp->mutex_init = 1;
  10056. + }
  10057. +
  10058. + /* Inform the scheduler to migrate tasks off this CPU */
  10059. + tell_sched_cpu_down_begin(cpu);
  10060. +
  10061. + init_completion(&hp->synced);
  10062. + init_completion(&hp->unplug_wait);
  10063. +
  10064. + hp->sync_tsk = kthread_create(sync_unplug_thread, hp, "sync_unplug/%d", cpu);
  10065. + if (IS_ERR(hp->sync_tsk)) {
  10066. + err = PTR_ERR(hp->sync_tsk);
  10067. + hp->sync_tsk = NULL;
  10068. + return err;
  10069. + }
  10070. + kthread_bind(hp->sync_tsk, cpu);
  10071. +
  10072. + /*
  10073. + * Wait for tasks to get out of the pinned sections,
  10074. + * it's still OK if new tasks enter. Some CPU notifiers will
  10075. + * wait for tasks that are going to enter these sections and
  10076. + * we must not have them block.
  10077. + */
  10078. + wake_up_process(hp->sync_tsk);
  10079. + return 0;
  10080. +}
  10081. +
  10082. +static void cpu_unplug_sync(unsigned int cpu)
  10083. +{
  10084. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  10085. +
  10086. + init_completion(&hp->synced);
  10087. + /* The completion needs to be initialzied before setting grab_lock */
  10088. + smp_wmb();
  10089. +
  10090. + /* Grab the mutex before setting grab_lock */
  10091. + hotplug_lock(hp);
  10092. + hp->grab_lock = 1;
  10093. +
  10094. + /*
  10095. + * The CPU notifiers have been completed.
  10096. + * Wait for tasks to get out of pinned CPU sections and have new
  10097. + * tasks block until the CPU is completely down.
  10098. + */
  10099. + __cpu_unplug_sync(hp);
  10100. +
  10101. + /* All done with the sync thread */
  10102. + kthread_stop(hp->sync_tsk);
  10103. + hp->sync_tsk = NULL;
  10104. +}
  10105. +
  10106. +static void cpu_unplug_done(unsigned int cpu)
  10107. +{
  10108. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  10109. +
  10110. + hp->unplug = NULL;
  10111. + /* Let all tasks know cpu unplug is finished before cleaning up */
  10112. + smp_wmb();
  10113. +
  10114. + if (hp->sync_tsk)
  10115. + kthread_stop(hp->sync_tsk);
  10116. +
  10117. + if (hp->grab_lock) {
  10118. + hotplug_unlock(hp);
  10119. + /* protected by cpu_hotplug.lock */
  10120. + hp->grab_lock = 0;
  10121. + }
  10122. + tell_sched_cpu_down_done(cpu);
  10123. +}
  10124. void get_online_cpus(void)
  10125. {
  10126. @@ -789,10 +1072,14 @@
  10127. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  10128. int err;
  10129. + __cpu_unplug_wait(cpu);
  10130. /* Park the smpboot threads */
  10131. kthread_park(per_cpu_ptr(&cpuhp_state, cpu)->thread);
  10132. smpboot_park_threads(cpu);
  10133. + /* Notifiers are done. Don't let any more tasks pin this CPU. */
  10134. + cpu_unplug_sync(cpu);
  10135. +
  10136. /*
  10137. * Prevent irq alloc/free while the dying cpu reorganizes the
  10138. * interrupt affinities.
  10139. @@ -877,6 +1164,9 @@
  10140. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  10141. int prev_state, ret = 0;
  10142. bool hasdied = false;
  10143. + int mycpu;
  10144. + cpumask_var_t cpumask;
  10145. + cpumask_var_t cpumask_org;
  10146. if (num_online_cpus() == 1)
  10147. return -EBUSY;
  10148. @@ -884,7 +1174,34 @@
  10149. if (!cpu_present(cpu))
  10150. return -EINVAL;
  10151. + /* Move the downtaker off the unplug cpu */
  10152. + if (!alloc_cpumask_var(&cpumask, GFP_KERNEL))
  10153. + return -ENOMEM;
  10154. + if (!alloc_cpumask_var(&cpumask_org, GFP_KERNEL)) {
  10155. + free_cpumask_var(cpumask);
  10156. + return -ENOMEM;
  10157. + }
  10158. +
  10159. + cpumask_copy(cpumask_org, tsk_cpus_allowed(current));
  10160. + cpumask_andnot(cpumask, cpu_online_mask, cpumask_of(cpu));
  10161. + set_cpus_allowed_ptr(current, cpumask);
  10162. + free_cpumask_var(cpumask);
  10163. + migrate_disable();
  10164. + mycpu = smp_processor_id();
  10165. + if (mycpu == cpu) {
  10166. + printk(KERN_ERR "Yuck! Still on unplug CPU\n!");
  10167. + migrate_enable();
  10168. + ret = -EBUSY;
  10169. + goto restore_cpus;
  10170. + }
  10171. +
  10172. + migrate_enable();
  10173. cpu_hotplug_begin();
  10174. + ret = cpu_unplug_begin(cpu);
  10175. + if (ret) {
  10176. + printk("cpu_unplug_begin(%d) failed\n", cpu);
  10177. + goto out_cancel;
  10178. + }
  10179. cpuhp_tasks_frozen = tasks_frozen;
  10180. @@ -923,10 +1240,15 @@
  10181. hasdied = prev_state != st->state && st->state == CPUHP_OFFLINE;
  10182. out:
  10183. + cpu_unplug_done(cpu);
  10184. +out_cancel:
  10185. cpu_hotplug_done();
  10186. /* This post dead nonsense must die */
  10187. if (!ret && hasdied)
  10188. cpu_notify_nofail(CPU_POST_DEAD, cpu);
  10189. +restore_cpus:
  10190. + set_cpus_allowed_ptr(current, cpumask_org);
  10191. + free_cpumask_var(cpumask_org);
  10192. return ret;
  10193. }
  10194. @@ -1240,6 +1562,8 @@
  10195. #endif /* CONFIG_PM_SLEEP_SMP */
  10196. +int __boot_cpu_id;
  10197. +
  10198. #endif /* CONFIG_SMP */
  10199. /* Boot processor state steps */
  10200. @@ -1924,6 +2248,10 @@
  10201. set_cpu_active(cpu, true);
  10202. set_cpu_present(cpu, true);
  10203. set_cpu_possible(cpu, true);
  10204. +
  10205. +#ifdef CONFIG_SMP
  10206. + __boot_cpu_id = cpu;
  10207. +#endif
  10208. }
  10209. /*
  10210. diff -Nur linux-4.9.28.orig/kernel/cpuset.c linux-4.9.28/kernel/cpuset.c
  10211. --- linux-4.9.28.orig/kernel/cpuset.c 2017-05-14 14:00:37.000000000 +0200
  10212. +++ linux-4.9.28/kernel/cpuset.c 2017-05-19 03:37:25.182176801 +0200
  10213. @@ -284,7 +284,7 @@
  10214. */
  10215. static DEFINE_MUTEX(cpuset_mutex);
  10216. -static DEFINE_SPINLOCK(callback_lock);
  10217. +static DEFINE_RAW_SPINLOCK(callback_lock);
  10218. static struct workqueue_struct *cpuset_migrate_mm_wq;
  10219. @@ -907,9 +907,9 @@
  10220. continue;
  10221. rcu_read_unlock();
  10222. - spin_lock_irq(&callback_lock);
  10223. + raw_spin_lock_irq(&callback_lock);
  10224. cpumask_copy(cp->effective_cpus, new_cpus);
  10225. - spin_unlock_irq(&callback_lock);
  10226. + raw_spin_unlock_irq(&callback_lock);
  10227. WARN_ON(!cgroup_subsys_on_dfl(cpuset_cgrp_subsys) &&
  10228. !cpumask_equal(cp->cpus_allowed, cp->effective_cpus));
  10229. @@ -974,9 +974,9 @@
  10230. if (retval < 0)
  10231. return retval;
  10232. - spin_lock_irq(&callback_lock);
  10233. + raw_spin_lock_irq(&callback_lock);
  10234. cpumask_copy(cs->cpus_allowed, trialcs->cpus_allowed);
  10235. - spin_unlock_irq(&callback_lock);
  10236. + raw_spin_unlock_irq(&callback_lock);
  10237. /* use trialcs->cpus_allowed as a temp variable */
  10238. update_cpumasks_hier(cs, trialcs->cpus_allowed);
  10239. @@ -1176,9 +1176,9 @@
  10240. continue;
  10241. rcu_read_unlock();
  10242. - spin_lock_irq(&callback_lock);
  10243. + raw_spin_lock_irq(&callback_lock);
  10244. cp->effective_mems = *new_mems;
  10245. - spin_unlock_irq(&callback_lock);
  10246. + raw_spin_unlock_irq(&callback_lock);
  10247. WARN_ON(!cgroup_subsys_on_dfl(cpuset_cgrp_subsys) &&
  10248. !nodes_equal(cp->mems_allowed, cp->effective_mems));
  10249. @@ -1246,9 +1246,9 @@
  10250. if (retval < 0)
  10251. goto done;
  10252. - spin_lock_irq(&callback_lock);
  10253. + raw_spin_lock_irq(&callback_lock);
  10254. cs->mems_allowed = trialcs->mems_allowed;
  10255. - spin_unlock_irq(&callback_lock);
  10256. + raw_spin_unlock_irq(&callback_lock);
  10257. /* use trialcs->mems_allowed as a temp variable */
  10258. update_nodemasks_hier(cs, &trialcs->mems_allowed);
  10259. @@ -1339,9 +1339,9 @@
  10260. spread_flag_changed = ((is_spread_slab(cs) != is_spread_slab(trialcs))
  10261. || (is_spread_page(cs) != is_spread_page(trialcs)));
  10262. - spin_lock_irq(&callback_lock);
  10263. + raw_spin_lock_irq(&callback_lock);
  10264. cs->flags = trialcs->flags;
  10265. - spin_unlock_irq(&callback_lock);
  10266. + raw_spin_unlock_irq(&callback_lock);
  10267. if (!cpumask_empty(trialcs->cpus_allowed) && balance_flag_changed)
  10268. rebuild_sched_domains_locked();
  10269. @@ -1756,7 +1756,7 @@
  10270. cpuset_filetype_t type = seq_cft(sf)->private;
  10271. int ret = 0;
  10272. - spin_lock_irq(&callback_lock);
  10273. + raw_spin_lock_irq(&callback_lock);
  10274. switch (type) {
  10275. case FILE_CPULIST:
  10276. @@ -1775,7 +1775,7 @@
  10277. ret = -EINVAL;
  10278. }
  10279. - spin_unlock_irq(&callback_lock);
  10280. + raw_spin_unlock_irq(&callback_lock);
  10281. return ret;
  10282. }
  10283. @@ -1989,12 +1989,12 @@
  10284. cpuset_inc();
  10285. - spin_lock_irq(&callback_lock);
  10286. + raw_spin_lock_irq(&callback_lock);
  10287. if (cgroup_subsys_on_dfl(cpuset_cgrp_subsys)) {
  10288. cpumask_copy(cs->effective_cpus, parent->effective_cpus);
  10289. cs->effective_mems = parent->effective_mems;
  10290. }
  10291. - spin_unlock_irq(&callback_lock);
  10292. + raw_spin_unlock_irq(&callback_lock);
  10293. if (!test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags))
  10294. goto out_unlock;
  10295. @@ -2021,12 +2021,12 @@
  10296. }
  10297. rcu_read_unlock();
  10298. - spin_lock_irq(&callback_lock);
  10299. + raw_spin_lock_irq(&callback_lock);
  10300. cs->mems_allowed = parent->mems_allowed;
  10301. cs->effective_mems = parent->mems_allowed;
  10302. cpumask_copy(cs->cpus_allowed, parent->cpus_allowed);
  10303. cpumask_copy(cs->effective_cpus, parent->cpus_allowed);
  10304. - spin_unlock_irq(&callback_lock);
  10305. + raw_spin_unlock_irq(&callback_lock);
  10306. out_unlock:
  10307. mutex_unlock(&cpuset_mutex);
  10308. return 0;
  10309. @@ -2065,7 +2065,7 @@
  10310. static void cpuset_bind(struct cgroup_subsys_state *root_css)
  10311. {
  10312. mutex_lock(&cpuset_mutex);
  10313. - spin_lock_irq(&callback_lock);
  10314. + raw_spin_lock_irq(&callback_lock);
  10315. if (cgroup_subsys_on_dfl(cpuset_cgrp_subsys)) {
  10316. cpumask_copy(top_cpuset.cpus_allowed, cpu_possible_mask);
  10317. @@ -2076,7 +2076,7 @@
  10318. top_cpuset.mems_allowed = top_cpuset.effective_mems;
  10319. }
  10320. - spin_unlock_irq(&callback_lock);
  10321. + raw_spin_unlock_irq(&callback_lock);
  10322. mutex_unlock(&cpuset_mutex);
  10323. }
  10324. @@ -2177,12 +2177,12 @@
  10325. {
  10326. bool is_empty;
  10327. - spin_lock_irq(&callback_lock);
  10328. + raw_spin_lock_irq(&callback_lock);
  10329. cpumask_copy(cs->cpus_allowed, new_cpus);
  10330. cpumask_copy(cs->effective_cpus, new_cpus);
  10331. cs->mems_allowed = *new_mems;
  10332. cs->effective_mems = *new_mems;
  10333. - spin_unlock_irq(&callback_lock);
  10334. + raw_spin_unlock_irq(&callback_lock);
  10335. /*
  10336. * Don't call update_tasks_cpumask() if the cpuset becomes empty,
  10337. @@ -2219,10 +2219,10 @@
  10338. if (nodes_empty(*new_mems))
  10339. *new_mems = parent_cs(cs)->effective_mems;
  10340. - spin_lock_irq(&callback_lock);
  10341. + raw_spin_lock_irq(&callback_lock);
  10342. cpumask_copy(cs->effective_cpus, new_cpus);
  10343. cs->effective_mems = *new_mems;
  10344. - spin_unlock_irq(&callback_lock);
  10345. + raw_spin_unlock_irq(&callback_lock);
  10346. if (cpus_updated)
  10347. update_tasks_cpumask(cs);
  10348. @@ -2308,21 +2308,21 @@
  10349. /* synchronize cpus_allowed to cpu_active_mask */
  10350. if (cpus_updated) {
  10351. - spin_lock_irq(&callback_lock);
  10352. + raw_spin_lock_irq(&callback_lock);
  10353. if (!on_dfl)
  10354. cpumask_copy(top_cpuset.cpus_allowed, &new_cpus);
  10355. cpumask_copy(top_cpuset.effective_cpus, &new_cpus);
  10356. - spin_unlock_irq(&callback_lock);
  10357. + raw_spin_unlock_irq(&callback_lock);
  10358. /* we don't mess with cpumasks of tasks in top_cpuset */
  10359. }
  10360. /* synchronize mems_allowed to N_MEMORY */
  10361. if (mems_updated) {
  10362. - spin_lock_irq(&callback_lock);
  10363. + raw_spin_lock_irq(&callback_lock);
  10364. if (!on_dfl)
  10365. top_cpuset.mems_allowed = new_mems;
  10366. top_cpuset.effective_mems = new_mems;
  10367. - spin_unlock_irq(&callback_lock);
  10368. + raw_spin_unlock_irq(&callback_lock);
  10369. update_tasks_nodemask(&top_cpuset);
  10370. }
  10371. @@ -2420,11 +2420,11 @@
  10372. {
  10373. unsigned long flags;
  10374. - spin_lock_irqsave(&callback_lock, flags);
  10375. + raw_spin_lock_irqsave(&callback_lock, flags);
  10376. rcu_read_lock();
  10377. guarantee_online_cpus(task_cs(tsk), pmask);
  10378. rcu_read_unlock();
  10379. - spin_unlock_irqrestore(&callback_lock, flags);
  10380. + raw_spin_unlock_irqrestore(&callback_lock, flags);
  10381. }
  10382. void cpuset_cpus_allowed_fallback(struct task_struct *tsk)
  10383. @@ -2472,11 +2472,11 @@
  10384. nodemask_t mask;
  10385. unsigned long flags;
  10386. - spin_lock_irqsave(&callback_lock, flags);
  10387. + raw_spin_lock_irqsave(&callback_lock, flags);
  10388. rcu_read_lock();
  10389. guarantee_online_mems(task_cs(tsk), &mask);
  10390. rcu_read_unlock();
  10391. - spin_unlock_irqrestore(&callback_lock, flags);
  10392. + raw_spin_unlock_irqrestore(&callback_lock, flags);
  10393. return mask;
  10394. }
  10395. @@ -2568,14 +2568,14 @@
  10396. return true;
  10397. /* Not hardwall and node outside mems_allowed: scan up cpusets */
  10398. - spin_lock_irqsave(&callback_lock, flags);
  10399. + raw_spin_lock_irqsave(&callback_lock, flags);
  10400. rcu_read_lock();
  10401. cs = nearest_hardwall_ancestor(task_cs(current));
  10402. allowed = node_isset(node, cs->mems_allowed);
  10403. rcu_read_unlock();
  10404. - spin_unlock_irqrestore(&callback_lock, flags);
  10405. + raw_spin_unlock_irqrestore(&callback_lock, flags);
  10406. return allowed;
  10407. }
  10408. diff -Nur linux-4.9.28.orig/kernel/debug/kdb/kdb_io.c linux-4.9.28/kernel/debug/kdb/kdb_io.c
  10409. --- linux-4.9.28.orig/kernel/debug/kdb/kdb_io.c 2017-05-14 14:00:37.000000000 +0200
  10410. +++ linux-4.9.28/kernel/debug/kdb/kdb_io.c 2017-05-19 03:37:25.182176801 +0200
  10411. @@ -554,7 +554,6 @@
  10412. int linecount;
  10413. int colcount;
  10414. int logging, saved_loglevel = 0;
  10415. - int saved_trap_printk;
  10416. int got_printf_lock = 0;
  10417. int retlen = 0;
  10418. int fnd, len;
  10419. @@ -565,8 +564,6 @@
  10420. unsigned long uninitialized_var(flags);
  10421. preempt_disable();
  10422. - saved_trap_printk = kdb_trap_printk;
  10423. - kdb_trap_printk = 0;
  10424. /* Serialize kdb_printf if multiple cpus try to write at once.
  10425. * But if any cpu goes recursive in kdb, just print the output,
  10426. @@ -855,7 +852,6 @@
  10427. } else {
  10428. __release(kdb_printf_lock);
  10429. }
  10430. - kdb_trap_printk = saved_trap_printk;
  10431. preempt_enable();
  10432. return retlen;
  10433. }
  10434. @@ -865,9 +861,11 @@
  10435. va_list ap;
  10436. int r;
  10437. + kdb_trap_printk++;
  10438. va_start(ap, fmt);
  10439. r = vkdb_printf(KDB_MSGSRC_INTERNAL, fmt, ap);
  10440. va_end(ap);
  10441. + kdb_trap_printk--;
  10442. return r;
  10443. }
  10444. diff -Nur linux-4.9.28.orig/kernel/events/core.c linux-4.9.28/kernel/events/core.c
  10445. --- linux-4.9.28.orig/kernel/events/core.c 2017-05-14 14:00:37.000000000 +0200
  10446. +++ linux-4.9.28/kernel/events/core.c 2017-05-19 03:37:25.182176801 +0200
  10447. @@ -1050,6 +1050,7 @@
  10448. raw_spin_lock_init(&cpuctx->hrtimer_lock);
  10449. hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
  10450. timer->function = perf_mux_hrtimer_handler;
  10451. + timer->irqsafe = 1;
  10452. }
  10453. static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx)
  10454. @@ -8363,6 +8364,7 @@
  10455. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  10456. hwc->hrtimer.function = perf_swevent_hrtimer;
  10457. + hwc->hrtimer.irqsafe = 1;
  10458. /*
  10459. * Since hrtimers have a fixed rate, we can do a static freq->period
  10460. diff -Nur linux-4.9.28.orig/kernel/exit.c linux-4.9.28/kernel/exit.c
  10461. --- linux-4.9.28.orig/kernel/exit.c 2017-05-14 14:00:37.000000000 +0200
  10462. +++ linux-4.9.28/kernel/exit.c 2017-05-19 03:37:25.182176801 +0200
  10463. @@ -143,7 +143,7 @@
  10464. * Do this under ->siglock, we can race with another thread
  10465. * doing sigqueue_free() if we have SIGQUEUE_PREALLOC signals.
  10466. */
  10467. - flush_sigqueue(&tsk->pending);
  10468. + flush_task_sigqueue(tsk);
  10469. tsk->sighand = NULL;
  10470. spin_unlock(&sighand->siglock);
  10471. diff -Nur linux-4.9.28.orig/kernel/fork.c linux-4.9.28/kernel/fork.c
  10472. --- linux-4.9.28.orig/kernel/fork.c 2017-05-14 14:00:37.000000000 +0200
  10473. +++ linux-4.9.28/kernel/fork.c 2017-05-19 03:37:25.182176801 +0200
  10474. @@ -76,6 +76,7 @@
  10475. #include <linux/compiler.h>
  10476. #include <linux/sysctl.h>
  10477. #include <linux/kcov.h>
  10478. +#include <linux/kprobes.h>
  10479. #include <asm/pgtable.h>
  10480. #include <asm/pgalloc.h>
  10481. @@ -376,13 +377,24 @@
  10482. if (atomic_dec_and_test(&sig->sigcnt))
  10483. free_signal_struct(sig);
  10484. }
  10485. -
  10486. +#ifdef CONFIG_PREEMPT_RT_BASE
  10487. +static
  10488. +#endif
  10489. void __put_task_struct(struct task_struct *tsk)
  10490. {
  10491. WARN_ON(!tsk->exit_state);
  10492. WARN_ON(atomic_read(&tsk->usage));
  10493. WARN_ON(tsk == current);
  10494. + /*
  10495. + * Remove function-return probe instances associated with this
  10496. + * task and put them back on the free list.
  10497. + */
  10498. + kprobe_flush_task(tsk);
  10499. +
  10500. + /* Task is done with its stack. */
  10501. + put_task_stack(tsk);
  10502. +
  10503. cgroup_free(tsk);
  10504. task_numa_free(tsk);
  10505. security_task_free(tsk);
  10506. @@ -393,7 +405,18 @@
  10507. if (!profile_handoff_task(tsk))
  10508. free_task(tsk);
  10509. }
  10510. +#ifndef CONFIG_PREEMPT_RT_BASE
  10511. EXPORT_SYMBOL_GPL(__put_task_struct);
  10512. +#else
  10513. +void __put_task_struct_cb(struct rcu_head *rhp)
  10514. +{
  10515. + struct task_struct *tsk = container_of(rhp, struct task_struct, put_rcu);
  10516. +
  10517. + __put_task_struct(tsk);
  10518. +
  10519. +}
  10520. +EXPORT_SYMBOL_GPL(__put_task_struct_cb);
  10521. +#endif
  10522. void __init __weak arch_task_cache_init(void) { }
  10523. @@ -852,6 +875,19 @@
  10524. }
  10525. EXPORT_SYMBOL_GPL(__mmdrop);
  10526. +#ifdef CONFIG_PREEMPT_RT_BASE
  10527. +/*
  10528. + * RCU callback for delayed mm drop. Not strictly rcu, but we don't
  10529. + * want another facility to make this work.
  10530. + */
  10531. +void __mmdrop_delayed(struct rcu_head *rhp)
  10532. +{
  10533. + struct mm_struct *mm = container_of(rhp, struct mm_struct, delayed_drop);
  10534. +
  10535. + __mmdrop(mm);
  10536. +}
  10537. +#endif
  10538. +
  10539. static inline void __mmput(struct mm_struct *mm)
  10540. {
  10541. VM_BUG_ON(atomic_read(&mm->mm_users));
  10542. @@ -1417,6 +1453,7 @@
  10543. #ifdef CONFIG_RT_MUTEXES
  10544. p->pi_waiters = RB_ROOT;
  10545. p->pi_waiters_leftmost = NULL;
  10546. + p->pi_top_task = NULL;
  10547. p->pi_blocked_on = NULL;
  10548. #endif
  10549. }
  10550. @@ -1426,6 +1463,9 @@
  10551. */
  10552. static void posix_cpu_timers_init(struct task_struct *tsk)
  10553. {
  10554. +#ifdef CONFIG_PREEMPT_RT_BASE
  10555. + tsk->posix_timer_list = NULL;
  10556. +#endif
  10557. tsk->cputime_expires.prof_exp = 0;
  10558. tsk->cputime_expires.virt_exp = 0;
  10559. tsk->cputime_expires.sched_exp = 0;
  10560. @@ -1552,6 +1592,7 @@
  10561. spin_lock_init(&p->alloc_lock);
  10562. init_sigpending(&p->pending);
  10563. + p->sigqueue_cache = NULL;
  10564. p->utime = p->stime = p->gtime = 0;
  10565. p->utimescaled = p->stimescaled = 0;
  10566. diff -Nur linux-4.9.28.orig/kernel/futex.c linux-4.9.28/kernel/futex.c
  10567. --- linux-4.9.28.orig/kernel/futex.c 2017-05-14 14:00:37.000000000 +0200
  10568. +++ linux-4.9.28/kernel/futex.c 2017-05-19 03:37:25.182176801 +0200
  10569. @@ -800,7 +800,7 @@
  10570. return 0;
  10571. }
  10572. -static struct futex_pi_state * alloc_pi_state(void)
  10573. +static struct futex_pi_state *alloc_pi_state(void)
  10574. {
  10575. struct futex_pi_state *pi_state = current->pi_state_cache;
  10576. @@ -810,6 +810,11 @@
  10577. return pi_state;
  10578. }
  10579. +static void get_pi_state(struct futex_pi_state *pi_state)
  10580. +{
  10581. + WARN_ON_ONCE(!atomic_inc_not_zero(&pi_state->refcount));
  10582. +}
  10583. +
  10584. /*
  10585. * Drops a reference to the pi_state object and frees or caches it
  10586. * when the last reference is gone.
  10587. @@ -854,7 +859,7 @@
  10588. * Look up the task based on what TID userspace gave us.
  10589. * We dont trust it.
  10590. */
  10591. -static struct task_struct * futex_find_get_task(pid_t pid)
  10592. +static struct task_struct *futex_find_get_task(pid_t pid)
  10593. {
  10594. struct task_struct *p;
  10595. @@ -904,7 +909,9 @@
  10596. * task still owns the PI-state:
  10597. */
  10598. if (head->next != next) {
  10599. + raw_spin_unlock_irq(&curr->pi_lock);
  10600. spin_unlock(&hb->lock);
  10601. + raw_spin_lock_irq(&curr->pi_lock);
  10602. continue;
  10603. }
  10604. @@ -914,10 +921,12 @@
  10605. pi_state->owner = NULL;
  10606. raw_spin_unlock_irq(&curr->pi_lock);
  10607. - rt_mutex_unlock(&pi_state->pi_mutex);
  10608. -
  10609. + get_pi_state(pi_state);
  10610. spin_unlock(&hb->lock);
  10611. + rt_mutex_futex_unlock(&pi_state->pi_mutex);
  10612. + put_pi_state(pi_state);
  10613. +
  10614. raw_spin_lock_irq(&curr->pi_lock);
  10615. }
  10616. raw_spin_unlock_irq(&curr->pi_lock);
  10617. @@ -971,6 +980,39 @@
  10618. *
  10619. * [10] There is no transient state which leaves owner and user space
  10620. * TID out of sync.
  10621. + *
  10622. + *
  10623. + * Serialization and lifetime rules:
  10624. + *
  10625. + * hb->lock:
  10626. + *
  10627. + * hb -> futex_q, relation
  10628. + * futex_q -> pi_state, relation
  10629. + *
  10630. + * (cannot be raw because hb can contain arbitrary amount
  10631. + * of futex_q's)
  10632. + *
  10633. + * pi_mutex->wait_lock:
  10634. + *
  10635. + * {uval, pi_state}
  10636. + *
  10637. + * (and pi_mutex 'obviously')
  10638. + *
  10639. + * p->pi_lock:
  10640. + *
  10641. + * p->pi_state_list -> pi_state->list, relation
  10642. + *
  10643. + * pi_state->refcount:
  10644. + *
  10645. + * pi_state lifetime
  10646. + *
  10647. + *
  10648. + * Lock order:
  10649. + *
  10650. + * hb->lock
  10651. + * pi_mutex->wait_lock
  10652. + * p->pi_lock
  10653. + *
  10654. */
  10655. /*
  10656. @@ -978,10 +1020,13 @@
  10657. * the pi_state against the user space value. If correct, attach to
  10658. * it.
  10659. */
  10660. -static int attach_to_pi_state(u32 uval, struct futex_pi_state *pi_state,
  10661. +static int attach_to_pi_state(u32 __user *uaddr, u32 uval,
  10662. + struct futex_pi_state *pi_state,
  10663. struct futex_pi_state **ps)
  10664. {
  10665. pid_t pid = uval & FUTEX_TID_MASK;
  10666. + u32 uval2;
  10667. + int ret;
  10668. /*
  10669. * Userspace might have messed up non-PI and PI futexes [3]
  10670. @@ -989,9 +1034,39 @@
  10671. if (unlikely(!pi_state))
  10672. return -EINVAL;
  10673. + /*
  10674. + * We get here with hb->lock held, and having found a
  10675. + * futex_top_waiter(). This means that futex_lock_pi() of said futex_q
  10676. + * has dropped the hb->lock in between queue_me() and unqueue_me_pi(),
  10677. + * which in turn means that futex_lock_pi() still has a reference on
  10678. + * our pi_state.
  10679. + *
  10680. + * The waiter holding a reference on @pi_state also protects against
  10681. + * the unlocked put_pi_state() in futex_unlock_pi(), futex_lock_pi()
  10682. + * and futex_wait_requeue_pi() as it cannot go to 0 and consequently
  10683. + * free pi_state before we can take a reference ourselves.
  10684. + */
  10685. WARN_ON(!atomic_read(&pi_state->refcount));
  10686. /*
  10687. + * Now that we have a pi_state, we can acquire wait_lock
  10688. + * and do the state validation.
  10689. + */
  10690. + raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
  10691. +
  10692. + /*
  10693. + * Since {uval, pi_state} is serialized by wait_lock, and our current
  10694. + * uval was read without holding it, it can have changed. Verify it
  10695. + * still is what we expect it to be, otherwise retry the entire
  10696. + * operation.
  10697. + */
  10698. + if (get_futex_value_locked(&uval2, uaddr))
  10699. + goto out_efault;
  10700. +
  10701. + if (uval != uval2)
  10702. + goto out_eagain;
  10703. +
  10704. + /*
  10705. * Handle the owner died case:
  10706. */
  10707. if (uval & FUTEX_OWNER_DIED) {
  10708. @@ -1006,11 +1081,11 @@
  10709. * is not 0. Inconsistent state. [5]
  10710. */
  10711. if (pid)
  10712. - return -EINVAL;
  10713. + goto out_einval;
  10714. /*
  10715. * Take a ref on the state and return success. [4]
  10716. */
  10717. - goto out_state;
  10718. + goto out_attach;
  10719. }
  10720. /*
  10721. @@ -1022,14 +1097,14 @@
  10722. * Take a ref on the state and return success. [6]
  10723. */
  10724. if (!pid)
  10725. - goto out_state;
  10726. + goto out_attach;
  10727. } else {
  10728. /*
  10729. * If the owner died bit is not set, then the pi_state
  10730. * must have an owner. [7]
  10731. */
  10732. if (!pi_state->owner)
  10733. - return -EINVAL;
  10734. + goto out_einval;
  10735. }
  10736. /*
  10737. @@ -1038,11 +1113,29 @@
  10738. * user space TID. [9/10]
  10739. */
  10740. if (pid != task_pid_vnr(pi_state->owner))
  10741. - return -EINVAL;
  10742. -out_state:
  10743. - atomic_inc(&pi_state->refcount);
  10744. + goto out_einval;
  10745. +
  10746. +out_attach:
  10747. + get_pi_state(pi_state);
  10748. + raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
  10749. *ps = pi_state;
  10750. return 0;
  10751. +
  10752. +out_einval:
  10753. + ret = -EINVAL;
  10754. + goto out_error;
  10755. +
  10756. +out_eagain:
  10757. + ret = -EAGAIN;
  10758. + goto out_error;
  10759. +
  10760. +out_efault:
  10761. + ret = -EFAULT;
  10762. + goto out_error;
  10763. +
  10764. +out_error:
  10765. + raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
  10766. + return ret;
  10767. }
  10768. /*
  10769. @@ -1093,6 +1186,9 @@
  10770. /*
  10771. * No existing pi state. First waiter. [2]
  10772. + *
  10773. + * This creates pi_state, we have hb->lock held, this means nothing can
  10774. + * observe this state, wait_lock is irrelevant.
  10775. */
  10776. pi_state = alloc_pi_state();
  10777. @@ -1117,17 +1213,18 @@
  10778. return 0;
  10779. }
  10780. -static int lookup_pi_state(u32 uval, struct futex_hash_bucket *hb,
  10781. +static int lookup_pi_state(u32 __user *uaddr, u32 uval,
  10782. + struct futex_hash_bucket *hb,
  10783. union futex_key *key, struct futex_pi_state **ps)
  10784. {
  10785. - struct futex_q *match = futex_top_waiter(hb, key);
  10786. + struct futex_q *top_waiter = futex_top_waiter(hb, key);
  10787. /*
  10788. * If there is a waiter on that futex, validate it and
  10789. * attach to the pi_state when the validation succeeds.
  10790. */
  10791. - if (match)
  10792. - return attach_to_pi_state(uval, match->pi_state, ps);
  10793. + if (top_waiter)
  10794. + return attach_to_pi_state(uaddr, uval, top_waiter->pi_state, ps);
  10795. /*
  10796. * We are the first waiter - try to look up the owner based on
  10797. @@ -1146,7 +1243,7 @@
  10798. if (unlikely(cmpxchg_futex_value_locked(&curval, uaddr, uval, newval)))
  10799. return -EFAULT;
  10800. - /*If user space value changed, let the caller retry */
  10801. + /* If user space value changed, let the caller retry */
  10802. return curval != uval ? -EAGAIN : 0;
  10803. }
  10804. @@ -1174,7 +1271,7 @@
  10805. struct task_struct *task, int set_waiters)
  10806. {
  10807. u32 uval, newval, vpid = task_pid_vnr(task);
  10808. - struct futex_q *match;
  10809. + struct futex_q *top_waiter;
  10810. int ret;
  10811. /*
  10812. @@ -1200,9 +1297,9 @@
  10813. * Lookup existing state first. If it exists, try to attach to
  10814. * its pi_state.
  10815. */
  10816. - match = futex_top_waiter(hb, key);
  10817. - if (match)
  10818. - return attach_to_pi_state(uval, match->pi_state, ps);
  10819. + top_waiter = futex_top_waiter(hb, key);
  10820. + if (top_waiter)
  10821. + return attach_to_pi_state(uaddr, uval, top_waiter->pi_state, ps);
  10822. /*
  10823. * No waiter and user TID is 0. We are here because the
  10824. @@ -1283,50 +1380,45 @@
  10825. wake_q_add(wake_q, p);
  10826. __unqueue_futex(q);
  10827. /*
  10828. - * The waiting task can free the futex_q as soon as
  10829. - * q->lock_ptr = NULL is written, without taking any locks. A
  10830. - * memory barrier is required here to prevent the following
  10831. - * store to lock_ptr from getting ahead of the plist_del.
  10832. + * The waiting task can free the futex_q as soon as q->lock_ptr = NULL
  10833. + * is written, without taking any locks. This is possible in the event
  10834. + * of a spurious wakeup, for example. A memory barrier is required here
  10835. + * to prevent the following store to lock_ptr from getting ahead of the
  10836. + * plist_del in __unqueue_futex().
  10837. */
  10838. - smp_wmb();
  10839. - q->lock_ptr = NULL;
  10840. + smp_store_release(&q->lock_ptr, NULL);
  10841. }
  10842. -static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this,
  10843. - struct futex_hash_bucket *hb)
  10844. +/*
  10845. + * Caller must hold a reference on @pi_state.
  10846. + */
  10847. +static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_pi_state *pi_state)
  10848. {
  10849. - struct task_struct *new_owner;
  10850. - struct futex_pi_state *pi_state = this->pi_state;
  10851. u32 uninitialized_var(curval), newval;
  10852. + struct task_struct *new_owner;
  10853. + bool postunlock = false;
  10854. WAKE_Q(wake_q);
  10855. - bool deboost;
  10856. + WAKE_Q(wake_sleeper_q);
  10857. int ret = 0;
  10858. - if (!pi_state)
  10859. - return -EINVAL;
  10860. -
  10861. - /*
  10862. - * If current does not own the pi_state then the futex is
  10863. - * inconsistent and user space fiddled with the futex value.
  10864. - */
  10865. - if (pi_state->owner != current)
  10866. - return -EINVAL;
  10867. -
  10868. - raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
  10869. new_owner = rt_mutex_next_owner(&pi_state->pi_mutex);
  10870. + if (WARN_ON_ONCE(!new_owner)) {
  10871. + /*
  10872. + * As per the comment in futex_unlock_pi() this should not happen.
  10873. + *
  10874. + * When this happens, give up our locks and try again, giving
  10875. + * the futex_lock_pi() instance time to complete, either by
  10876. + * waiting on the rtmutex or removing itself from the futex
  10877. + * queue.
  10878. + */
  10879. + ret = -EAGAIN;
  10880. + goto out_unlock;
  10881. + }
  10882. /*
  10883. - * It is possible that the next waiter (the one that brought
  10884. - * this owner to the kernel) timed out and is no longer
  10885. - * waiting on the lock.
  10886. - */
  10887. - if (!new_owner)
  10888. - new_owner = this->task;
  10889. -
  10890. - /*
  10891. - * We pass it to the next owner. The WAITERS bit is always
  10892. - * kept enabled while there is PI state around. We cleanup the
  10893. - * owner died bit, because we are the owner.
  10894. + * We pass it to the next owner. The WAITERS bit is always kept
  10895. + * enabled while there is PI state around. We cleanup the owner
  10896. + * died bit, because we are the owner.
  10897. */
  10898. newval = FUTEX_WAITERS | task_pid_vnr(new_owner);
  10899. @@ -1335,6 +1427,7 @@
  10900. if (cmpxchg_futex_value_locked(&curval, uaddr, uval, newval)) {
  10901. ret = -EFAULT;
  10902. +
  10903. } else if (curval != uval) {
  10904. /*
  10905. * If a unconditional UNLOCK_PI operation (user space did not
  10906. @@ -1347,10 +1440,14 @@
  10907. else
  10908. ret = -EINVAL;
  10909. }
  10910. - if (ret) {
  10911. - raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
  10912. - return ret;
  10913. - }
  10914. +
  10915. + if (ret)
  10916. + goto out_unlock;
  10917. +
  10918. + /*
  10919. + * This is a point of no return; once we modify the uval there is no
  10920. + * going back and subsequent operations must not fail.
  10921. + */
  10922. raw_spin_lock(&pi_state->owner->pi_lock);
  10923. WARN_ON(list_empty(&pi_state->list));
  10924. @@ -1363,22 +1460,15 @@
  10925. pi_state->owner = new_owner;
  10926. raw_spin_unlock(&new_owner->pi_lock);
  10927. + postunlock = __rt_mutex_futex_unlock(&pi_state->pi_mutex, &wake_q,
  10928. + &wake_sleeper_q);
  10929. +out_unlock:
  10930. raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
  10931. - deboost = rt_mutex_futex_unlock(&pi_state->pi_mutex, &wake_q);
  10932. + if (postunlock)
  10933. + rt_mutex_postunlock(&wake_q, &wake_sleeper_q);
  10934. - /*
  10935. - * First unlock HB so the waiter does not spin on it once he got woken
  10936. - * up. Second wake up the waiter before the priority is adjusted. If we
  10937. - * deboost first (and lose our higher priority), then the task might get
  10938. - * scheduled away before the wake up can take place.
  10939. - */
  10940. - spin_unlock(&hb->lock);
  10941. - wake_up_q(&wake_q);
  10942. - if (deboost)
  10943. - rt_mutex_adjust_prio(current);
  10944. -
  10945. - return 0;
  10946. + return ret;
  10947. }
  10948. /*
  10949. @@ -1824,7 +1914,7 @@
  10950. * If that call succeeds then we have pi_state and an
  10951. * initial refcount on it.
  10952. */
  10953. - ret = lookup_pi_state(ret, hb2, &key2, &pi_state);
  10954. + ret = lookup_pi_state(uaddr2, ret, hb2, &key2, &pi_state);
  10955. }
  10956. switch (ret) {
  10957. @@ -1907,7 +1997,7 @@
  10958. * refcount on the pi_state and store the pointer in
  10959. * the futex_q object of the waiter.
  10960. */
  10961. - atomic_inc(&pi_state->refcount);
  10962. + get_pi_state(pi_state);
  10963. this->pi_state = pi_state;
  10964. ret = rt_mutex_start_proxy_lock(&pi_state->pi_mutex,
  10965. this->rt_waiter,
  10966. @@ -1924,6 +2014,16 @@
  10967. requeue_pi_wake_futex(this, &key2, hb2);
  10968. drop_count++;
  10969. continue;
  10970. + } else if (ret == -EAGAIN) {
  10971. + /*
  10972. + * Waiter was woken by timeout or
  10973. + * signal and has set pi_blocked_on to
  10974. + * PI_WAKEUP_INPROGRESS before we
  10975. + * tried to enqueue it on the rtmutex.
  10976. + */
  10977. + this->pi_state = NULL;
  10978. + put_pi_state(pi_state);
  10979. + continue;
  10980. } else if (ret) {
  10981. /*
  10982. * rt_mutex_start_proxy_lock() detected a
  10983. @@ -2007,20 +2107,7 @@
  10984. hb_waiters_dec(hb);
  10985. }
  10986. -/**
  10987. - * queue_me() - Enqueue the futex_q on the futex_hash_bucket
  10988. - * @q: The futex_q to enqueue
  10989. - * @hb: The destination hash bucket
  10990. - *
  10991. - * The hb->lock must be held by the caller, and is released here. A call to
  10992. - * queue_me() is typically paired with exactly one call to unqueue_me(). The
  10993. - * exceptions involve the PI related operations, which may use unqueue_me_pi()
  10994. - * or nothing if the unqueue is done as part of the wake process and the unqueue
  10995. - * state is implicit in the state of woken task (see futex_wait_requeue_pi() for
  10996. - * an example).
  10997. - */
  10998. -static inline void queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
  10999. - __releases(&hb->lock)
  11000. +static inline void __queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
  11001. {
  11002. int prio;
  11003. @@ -2037,6 +2124,24 @@
  11004. plist_node_init(&q->list, prio);
  11005. plist_add(&q->list, &hb->chain);
  11006. q->task = current;
  11007. +}
  11008. +
  11009. +/**
  11010. + * queue_me() - Enqueue the futex_q on the futex_hash_bucket
  11011. + * @q: The futex_q to enqueue
  11012. + * @hb: The destination hash bucket
  11013. + *
  11014. + * The hb->lock must be held by the caller, and is released here. A call to
  11015. + * queue_me() is typically paired with exactly one call to unqueue_me(). The
  11016. + * exceptions involve the PI related operations, which may use unqueue_me_pi()
  11017. + * or nothing if the unqueue is done as part of the wake process and the unqueue
  11018. + * state is implicit in the state of woken task (see futex_wait_requeue_pi() for
  11019. + * an example).
  11020. + */
  11021. +static inline void queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
  11022. + __releases(&hb->lock)
  11023. +{
  11024. + __queue_me(q, hb);
  11025. spin_unlock(&hb->lock);
  11026. }
  11027. @@ -2123,10 +2228,13 @@
  11028. {
  11029. u32 newtid = task_pid_vnr(newowner) | FUTEX_WAITERS;
  11030. struct futex_pi_state *pi_state = q->pi_state;
  11031. - struct task_struct *oldowner = pi_state->owner;
  11032. u32 uval, uninitialized_var(curval), newval;
  11033. + struct task_struct *oldowner;
  11034. int ret;
  11035. + raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
  11036. +
  11037. + oldowner = pi_state->owner;
  11038. /* Owner died? */
  11039. if (!pi_state->owner)
  11040. newtid |= FUTEX_OWNER_DIED;
  11041. @@ -2134,7 +2242,8 @@
  11042. /*
  11043. * We are here either because we stole the rtmutex from the
  11044. * previous highest priority waiter or we are the highest priority
  11045. - * waiter but failed to get the rtmutex the first time.
  11046. + * waiter but have failed to get the rtmutex the first time.
  11047. + *
  11048. * We have to replace the newowner TID in the user space variable.
  11049. * This must be atomic as we have to preserve the owner died bit here.
  11050. *
  11051. @@ -2142,17 +2251,16 @@
  11052. * because we can fault here. Imagine swapped out pages or a fork
  11053. * that marked all the anonymous memory readonly for cow.
  11054. *
  11055. - * Modifying pi_state _before_ the user space value would
  11056. - * leave the pi_state in an inconsistent state when we fault
  11057. - * here, because we need to drop the hash bucket lock to
  11058. - * handle the fault. This might be observed in the PID check
  11059. - * in lookup_pi_state.
  11060. + * Modifying pi_state _before_ the user space value would leave the
  11061. + * pi_state in an inconsistent state when we fault here, because we
  11062. + * need to drop the locks to handle the fault. This might be observed
  11063. + * in the PID check in lookup_pi_state.
  11064. */
  11065. retry:
  11066. if (get_futex_value_locked(&uval, uaddr))
  11067. goto handle_fault;
  11068. - while (1) {
  11069. + for (;;) {
  11070. newval = (uval & FUTEX_OWNER_DIED) | newtid;
  11071. if (cmpxchg_futex_value_locked(&curval, uaddr, uval, newval))
  11072. @@ -2167,47 +2275,60 @@
  11073. * itself.
  11074. */
  11075. if (pi_state->owner != NULL) {
  11076. - raw_spin_lock_irq(&pi_state->owner->pi_lock);
  11077. + raw_spin_lock(&pi_state->owner->pi_lock);
  11078. WARN_ON(list_empty(&pi_state->list));
  11079. list_del_init(&pi_state->list);
  11080. - raw_spin_unlock_irq(&pi_state->owner->pi_lock);
  11081. + raw_spin_unlock(&pi_state->owner->pi_lock);
  11082. }
  11083. pi_state->owner = newowner;
  11084. - raw_spin_lock_irq(&newowner->pi_lock);
  11085. + raw_spin_lock(&newowner->pi_lock);
  11086. WARN_ON(!list_empty(&pi_state->list));
  11087. list_add(&pi_state->list, &newowner->pi_state_list);
  11088. - raw_spin_unlock_irq(&newowner->pi_lock);
  11089. + raw_spin_unlock(&newowner->pi_lock);
  11090. + raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
  11091. +
  11092. return 0;
  11093. /*
  11094. - * To handle the page fault we need to drop the hash bucket
  11095. - * lock here. That gives the other task (either the highest priority
  11096. - * waiter itself or the task which stole the rtmutex) the
  11097. - * chance to try the fixup of the pi_state. So once we are
  11098. - * back from handling the fault we need to check the pi_state
  11099. - * after reacquiring the hash bucket lock and before trying to
  11100. - * do another fixup. When the fixup has been done already we
  11101. - * simply return.
  11102. + * To handle the page fault we need to drop the locks here. That gives
  11103. + * the other task (either the highest priority waiter itself or the
  11104. + * task which stole the rtmutex) the chance to try the fixup of the
  11105. + * pi_state. So once we are back from handling the fault we need to
  11106. + * check the pi_state after reacquiring the locks and before trying to
  11107. + * do another fixup. When the fixup has been done already we simply
  11108. + * return.
  11109. + *
  11110. + * Note: we hold both hb->lock and pi_mutex->wait_lock. We can safely
  11111. + * drop hb->lock since the caller owns the hb -> futex_q relation.
  11112. + * Dropping the pi_mutex->wait_lock requires the state revalidate.
  11113. */
  11114. handle_fault:
  11115. + raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
  11116. spin_unlock(q->lock_ptr);
  11117. ret = fault_in_user_writeable(uaddr);
  11118. spin_lock(q->lock_ptr);
  11119. + raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
  11120. /*
  11121. * Check if someone else fixed it for us:
  11122. */
  11123. - if (pi_state->owner != oldowner)
  11124. - return 0;
  11125. + if (pi_state->owner != oldowner) {
  11126. + ret = 0;
  11127. + goto out_unlock;
  11128. + }
  11129. if (ret)
  11130. - return ret;
  11131. + goto out_unlock;
  11132. goto retry;
  11133. +
  11134. +out_unlock:
  11135. + raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
  11136. + return ret;
  11137. }
  11138. static long futex_wait_restart(struct restart_block *restart);
  11139. @@ -2229,13 +2350,16 @@
  11140. */
  11141. static int fixup_owner(u32 __user *uaddr, struct futex_q *q, int locked)
  11142. {
  11143. - struct task_struct *owner;
  11144. int ret = 0;
  11145. if (locked) {
  11146. /*
  11147. * Got the lock. We might not be the anticipated owner if we
  11148. * did a lock-steal - fix up the PI-state in that case:
  11149. + *
  11150. + * We can safely read pi_state->owner without holding wait_lock
  11151. + * because we now own the rt_mutex, only the owner will attempt
  11152. + * to change it.
  11153. */
  11154. if (q->pi_state->owner != current)
  11155. ret = fixup_pi_state_owner(uaddr, q, current);
  11156. @@ -2243,43 +2367,15 @@
  11157. }
  11158. /*
  11159. - * Catch the rare case, where the lock was released when we were on the
  11160. - * way back before we locked the hash bucket.
  11161. - */
  11162. - if (q->pi_state->owner == current) {
  11163. - /*
  11164. - * Try to get the rt_mutex now. This might fail as some other
  11165. - * task acquired the rt_mutex after we removed ourself from the
  11166. - * rt_mutex waiters list.
  11167. - */
  11168. - if (rt_mutex_trylock(&q->pi_state->pi_mutex)) {
  11169. - locked = 1;
  11170. - goto out;
  11171. - }
  11172. -
  11173. - /*
  11174. - * pi_state is incorrect, some other task did a lock steal and
  11175. - * we returned due to timeout or signal without taking the
  11176. - * rt_mutex. Too late.
  11177. - */
  11178. - raw_spin_lock_irq(&q->pi_state->pi_mutex.wait_lock);
  11179. - owner = rt_mutex_owner(&q->pi_state->pi_mutex);
  11180. - if (!owner)
  11181. - owner = rt_mutex_next_owner(&q->pi_state->pi_mutex);
  11182. - raw_spin_unlock_irq(&q->pi_state->pi_mutex.wait_lock);
  11183. - ret = fixup_pi_state_owner(uaddr, q, owner);
  11184. - goto out;
  11185. - }
  11186. -
  11187. - /*
  11188. * Paranoia check. If we did not take the lock, then we should not be
  11189. * the owner of the rt_mutex.
  11190. */
  11191. - if (rt_mutex_owner(&q->pi_state->pi_mutex) == current)
  11192. + if (rt_mutex_owner(&q->pi_state->pi_mutex) == current) {
  11193. printk(KERN_ERR "fixup_owner: ret = %d pi-mutex: %p "
  11194. "pi-state %p\n", ret,
  11195. q->pi_state->pi_mutex.owner,
  11196. q->pi_state->owner);
  11197. + }
  11198. out:
  11199. return ret ? ret : locked;
  11200. @@ -2503,6 +2599,8 @@
  11201. ktime_t *time, int trylock)
  11202. {
  11203. struct hrtimer_sleeper timeout, *to = NULL;
  11204. + struct futex_pi_state *pi_state = NULL;
  11205. + struct rt_mutex_waiter rt_waiter;
  11206. struct futex_hash_bucket *hb;
  11207. struct futex_q q = futex_q_init;
  11208. int res, ret;
  11209. @@ -2555,25 +2653,77 @@
  11210. }
  11211. }
  11212. + WARN_ON(!q.pi_state);
  11213. +
  11214. /*
  11215. * Only actually queue now that the atomic ops are done:
  11216. */
  11217. - queue_me(&q, hb);
  11218. + __queue_me(&q, hb);
  11219. - WARN_ON(!q.pi_state);
  11220. - /*
  11221. - * Block on the PI mutex:
  11222. - */
  11223. - if (!trylock) {
  11224. - ret = rt_mutex_timed_futex_lock(&q.pi_state->pi_mutex, to);
  11225. - } else {
  11226. - ret = rt_mutex_trylock(&q.pi_state->pi_mutex);
  11227. + if (trylock) {
  11228. + ret = rt_mutex_futex_trylock(&q.pi_state->pi_mutex);
  11229. /* Fixup the trylock return value: */
  11230. ret = ret ? 0 : -EWOULDBLOCK;
  11231. + goto no_block;
  11232. }
  11233. + rt_mutex_init_waiter(&rt_waiter, false);
  11234. +
  11235. + /*
  11236. + * On PREEMPT_RT_FULL, when hb->lock becomes an rt_mutex, we must not
  11237. + * hold it while doing rt_mutex_start_proxy(), because then it will
  11238. + * include hb->lock in the blocking chain, even through we'll not in
  11239. + * fact hold it while blocking. This will lead it to report -EDEADLK
  11240. + * and BUG when futex_unlock_pi() interleaves with this.
  11241. + *
  11242. + * Therefore acquire wait_lock while holding hb->lock, but drop the
  11243. + * latter before calling rt_mutex_start_proxy_lock(). This still fully
  11244. + * serializes against futex_unlock_pi() as that does the exact same
  11245. + * lock handoff sequence.
  11246. + */
  11247. + raw_spin_lock_irq(&q.pi_state->pi_mutex.wait_lock);
  11248. + /*
  11249. + * the migrate_disable() here disables migration in the in_atomic() fast
  11250. + * path which is enabled again in the following spin_unlock(). We have
  11251. + * one migrate_disable() pending in the slow-path which is reversed
  11252. + * after the raw_spin_unlock_irq() where we leave the atomic context.
  11253. + */
  11254. + migrate_disable();
  11255. +
  11256. + spin_unlock(q.lock_ptr);
  11257. + ret = __rt_mutex_start_proxy_lock(&q.pi_state->pi_mutex, &rt_waiter, current);
  11258. + raw_spin_unlock_irq(&q.pi_state->pi_mutex.wait_lock);
  11259. + migrate_enable();
  11260. +
  11261. + if (ret) {
  11262. + if (ret == 1)
  11263. + ret = 0;
  11264. +
  11265. + spin_lock(q.lock_ptr);
  11266. + goto no_block;
  11267. + }
  11268. +
  11269. +
  11270. + if (unlikely(to))
  11271. + hrtimer_start_expires(&to->timer, HRTIMER_MODE_ABS);
  11272. +
  11273. + ret = rt_mutex_wait_proxy_lock(&q.pi_state->pi_mutex, to, &rt_waiter);
  11274. +
  11275. spin_lock(q.lock_ptr);
  11276. /*
  11277. + * If we failed to acquire the lock (signal/timeout), we must
  11278. + * first acquire the hb->lock before removing the lock from the
  11279. + * rt_mutex waitqueue, such that we can keep the hb and rt_mutex
  11280. + * wait lists consistent.
  11281. + *
  11282. + * In particular; it is important that futex_unlock_pi() can not
  11283. + * observe this inconsistency.
  11284. + */
  11285. + if (ret && !rt_mutex_cleanup_proxy_lock(&q.pi_state->pi_mutex, &rt_waiter))
  11286. + ret = 0;
  11287. +
  11288. +no_block:
  11289. + /*
  11290. * Fixup the pi_state owner and possibly acquire the lock if we
  11291. * haven't already.
  11292. */
  11293. @@ -2589,12 +2739,19 @@
  11294. * If fixup_owner() faulted and was unable to handle the fault, unlock
  11295. * it and return the fault to userspace.
  11296. */
  11297. - if (ret && (rt_mutex_owner(&q.pi_state->pi_mutex) == current))
  11298. - rt_mutex_unlock(&q.pi_state->pi_mutex);
  11299. + if (ret && (rt_mutex_owner(&q.pi_state->pi_mutex) == current)) {
  11300. + pi_state = q.pi_state;
  11301. + get_pi_state(pi_state);
  11302. + }
  11303. /* Unqueue and drop the lock */
  11304. unqueue_me_pi(&q);
  11305. + if (pi_state) {
  11306. + rt_mutex_futex_unlock(&pi_state->pi_mutex);
  11307. + put_pi_state(pi_state);
  11308. + }
  11309. +
  11310. goto out_put_key;
  11311. out_unlock_put_key:
  11312. @@ -2603,8 +2760,10 @@
  11313. out_put_key:
  11314. put_futex_key(&q.key);
  11315. out:
  11316. - if (to)
  11317. + if (to) {
  11318. + hrtimer_cancel(&to->timer);
  11319. destroy_hrtimer_on_stack(&to->timer);
  11320. + }
  11321. return ret != -EINTR ? ret : -ERESTARTNOINTR;
  11322. uaddr_faulted:
  11323. @@ -2631,7 +2790,7 @@
  11324. u32 uninitialized_var(curval), uval, vpid = task_pid_vnr(current);
  11325. union futex_key key = FUTEX_KEY_INIT;
  11326. struct futex_hash_bucket *hb;
  11327. - struct futex_q *match;
  11328. + struct futex_q *top_waiter;
  11329. int ret;
  11330. retry:
  11331. @@ -2655,12 +2814,48 @@
  11332. * all and we at least want to know if user space fiddled
  11333. * with the futex value instead of blindly unlocking.
  11334. */
  11335. - match = futex_top_waiter(hb, &key);
  11336. - if (match) {
  11337. - ret = wake_futex_pi(uaddr, uval, match, hb);
  11338. + top_waiter = futex_top_waiter(hb, &key);
  11339. + if (top_waiter) {
  11340. + struct futex_pi_state *pi_state = top_waiter->pi_state;
  11341. +
  11342. + ret = -EINVAL;
  11343. + if (!pi_state)
  11344. + goto out_unlock;
  11345. +
  11346. /*
  11347. - * In case of success wake_futex_pi dropped the hash
  11348. - * bucket lock.
  11349. + * If current does not own the pi_state then the futex is
  11350. + * inconsistent and user space fiddled with the futex value.
  11351. + */
  11352. + if (pi_state->owner != current)
  11353. + goto out_unlock;
  11354. +
  11355. + get_pi_state(pi_state);
  11356. + /*
  11357. + * By taking wait_lock while still holding hb->lock, we ensure
  11358. + * there is no point where we hold neither; and therefore
  11359. + * wake_futex_pi() must observe a state consistent with what we
  11360. + * observed.
  11361. + */
  11362. + raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
  11363. + /*
  11364. + * Magic trickery for now to make the RT migrate disable
  11365. + * logic happy. The following spin_unlock() happens with
  11366. + * interrupts disabled so the internal migrate_enable()
  11367. + * won't undo the migrate_disable() which was issued when
  11368. + * locking hb->lock.
  11369. + */
  11370. + migrate_disable();
  11371. + spin_unlock(&hb->lock);
  11372. +
  11373. + /* Drops pi_state->pi_mutex.wait_lock */
  11374. + ret = wake_futex_pi(uaddr, uval, pi_state);
  11375. +
  11376. + migrate_enable();
  11377. +
  11378. + put_pi_state(pi_state);
  11379. +
  11380. + /*
  11381. + * Success, we're done! No tricky corner cases.
  11382. */
  11383. if (!ret)
  11384. goto out_putkey;
  11385. @@ -2675,7 +2870,6 @@
  11386. * setting the FUTEX_WAITERS bit. Try again.
  11387. */
  11388. if (ret == -EAGAIN) {
  11389. - spin_unlock(&hb->lock);
  11390. put_futex_key(&key);
  11391. goto retry;
  11392. }
  11393. @@ -2683,7 +2877,7 @@
  11394. * wake_futex_pi has detected invalid state. Tell user
  11395. * space.
  11396. */
  11397. - goto out_unlock;
  11398. + goto out_putkey;
  11399. }
  11400. /*
  11401. @@ -2693,8 +2887,10 @@
  11402. * preserve the WAITERS bit not the OWNER_DIED one. We are the
  11403. * owner.
  11404. */
  11405. - if (cmpxchg_futex_value_locked(&curval, uaddr, uval, 0))
  11406. + if (cmpxchg_futex_value_locked(&curval, uaddr, uval, 0)) {
  11407. + spin_unlock(&hb->lock);
  11408. goto pi_faulted;
  11409. + }
  11410. /*
  11411. * If uval has changed, let user space handle it.
  11412. @@ -2708,7 +2904,6 @@
  11413. return ret;
  11414. pi_faulted:
  11415. - spin_unlock(&hb->lock);
  11416. put_futex_key(&key);
  11417. ret = fault_in_user_writeable(uaddr);
  11418. @@ -2812,8 +3007,9 @@
  11419. u32 __user *uaddr2)
  11420. {
  11421. struct hrtimer_sleeper timeout, *to = NULL;
  11422. + struct futex_pi_state *pi_state = NULL;
  11423. struct rt_mutex_waiter rt_waiter;
  11424. - struct futex_hash_bucket *hb;
  11425. + struct futex_hash_bucket *hb, *hb2;
  11426. union futex_key key2 = FUTEX_KEY_INIT;
  11427. struct futex_q q = futex_q_init;
  11428. int res, ret;
  11429. @@ -2838,10 +3034,7 @@
  11430. * The waiter is allocated on our stack, manipulated by the requeue
  11431. * code while we sleep on uaddr.
  11432. */
  11433. - debug_rt_mutex_init_waiter(&rt_waiter);
  11434. - RB_CLEAR_NODE(&rt_waiter.pi_tree_entry);
  11435. - RB_CLEAR_NODE(&rt_waiter.tree_entry);
  11436. - rt_waiter.task = NULL;
  11437. + rt_mutex_init_waiter(&rt_waiter, false);
  11438. ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE);
  11439. if (unlikely(ret != 0))
  11440. @@ -2872,20 +3065,55 @@
  11441. /* Queue the futex_q, drop the hb lock, wait for wakeup. */
  11442. futex_wait_queue_me(hb, &q, to);
  11443. - spin_lock(&hb->lock);
  11444. - ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
  11445. - spin_unlock(&hb->lock);
  11446. - if (ret)
  11447. - goto out_put_keys;
  11448. + /*
  11449. + * On RT we must avoid races with requeue and trying to block
  11450. + * on two mutexes (hb->lock and uaddr2's rtmutex) by
  11451. + * serializing access to pi_blocked_on with pi_lock.
  11452. + */
  11453. + raw_spin_lock_irq(&current->pi_lock);
  11454. + if (current->pi_blocked_on) {
  11455. + /*
  11456. + * We have been requeued or are in the process of
  11457. + * being requeued.
  11458. + */
  11459. + raw_spin_unlock_irq(&current->pi_lock);
  11460. + } else {
  11461. + /*
  11462. + * Setting pi_blocked_on to PI_WAKEUP_INPROGRESS
  11463. + * prevents a concurrent requeue from moving us to the
  11464. + * uaddr2 rtmutex. After that we can safely acquire
  11465. + * (and possibly block on) hb->lock.
  11466. + */
  11467. + current->pi_blocked_on = PI_WAKEUP_INPROGRESS;
  11468. + raw_spin_unlock_irq(&current->pi_lock);
  11469. +
  11470. + spin_lock(&hb->lock);
  11471. +
  11472. + /*
  11473. + * Clean up pi_blocked_on. We might leak it otherwise
  11474. + * when we succeeded with the hb->lock in the fast
  11475. + * path.
  11476. + */
  11477. + raw_spin_lock_irq(&current->pi_lock);
  11478. + current->pi_blocked_on = NULL;
  11479. + raw_spin_unlock_irq(&current->pi_lock);
  11480. +
  11481. + ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
  11482. + spin_unlock(&hb->lock);
  11483. + if (ret)
  11484. + goto out_put_keys;
  11485. + }
  11486. /*
  11487. - * In order for us to be here, we know our q.key == key2, and since
  11488. - * we took the hb->lock above, we also know that futex_requeue() has
  11489. - * completed and we no longer have to concern ourselves with a wakeup
  11490. - * race with the atomic proxy lock acquisition by the requeue code. The
  11491. - * futex_requeue dropped our key1 reference and incremented our key2
  11492. - * reference count.
  11493. + * In order to be here, we have either been requeued, are in
  11494. + * the process of being requeued, or requeue successfully
  11495. + * acquired uaddr2 on our behalf. If pi_blocked_on was
  11496. + * non-null above, we may be racing with a requeue. Do not
  11497. + * rely on q->lock_ptr to be hb2->lock until after blocking on
  11498. + * hb->lock or hb2->lock. The futex_requeue dropped our key1
  11499. + * reference and incremented our key2 reference count.
  11500. */
  11501. + hb2 = hash_futex(&key2);
  11502. /* Check if the requeue code acquired the second futex for us. */
  11503. if (!q.rt_waiter) {
  11504. @@ -2894,16 +3122,19 @@
  11505. * did a lock-steal - fix up the PI-state in that case.
  11506. */
  11507. if (q.pi_state && (q.pi_state->owner != current)) {
  11508. - spin_lock(q.lock_ptr);
  11509. + spin_lock(&hb2->lock);
  11510. + BUG_ON(&hb2->lock != q.lock_ptr);
  11511. ret = fixup_pi_state_owner(uaddr2, &q, current);
  11512. - if (ret && rt_mutex_owner(&q.pi_state->pi_mutex) == current)
  11513. - rt_mutex_unlock(&q.pi_state->pi_mutex);
  11514. + if (ret && rt_mutex_owner(&q.pi_state->pi_mutex) == current) {
  11515. + pi_state = q.pi_state;
  11516. + get_pi_state(pi_state);
  11517. + }
  11518. /*
  11519. * Drop the reference to the pi state which
  11520. * the requeue_pi() code acquired for us.
  11521. */
  11522. put_pi_state(q.pi_state);
  11523. - spin_unlock(q.lock_ptr);
  11524. + spin_unlock(&hb2->lock);
  11525. }
  11526. } else {
  11527. struct rt_mutex *pi_mutex;
  11528. @@ -2915,10 +3146,14 @@
  11529. */
  11530. WARN_ON(!q.pi_state);
  11531. pi_mutex = &q.pi_state->pi_mutex;
  11532. - ret = rt_mutex_finish_proxy_lock(pi_mutex, to, &rt_waiter);
  11533. - debug_rt_mutex_free_waiter(&rt_waiter);
  11534. + ret = rt_mutex_wait_proxy_lock(pi_mutex, to, &rt_waiter);
  11535. - spin_lock(q.lock_ptr);
  11536. + spin_lock(&hb2->lock);
  11537. + BUG_ON(&hb2->lock != q.lock_ptr);
  11538. + if (ret && !rt_mutex_cleanup_proxy_lock(pi_mutex, &rt_waiter))
  11539. + ret = 0;
  11540. +
  11541. + debug_rt_mutex_free_waiter(&rt_waiter);
  11542. /*
  11543. * Fixup the pi_state owner and possibly acquire the lock if we
  11544. * haven't already.
  11545. @@ -2936,13 +3171,20 @@
  11546. * the fault, unlock the rt_mutex and return the fault to
  11547. * userspace.
  11548. */
  11549. - if (ret && rt_mutex_owner(pi_mutex) == current)
  11550. - rt_mutex_unlock(pi_mutex);
  11551. + if (ret && rt_mutex_owner(&q.pi_state->pi_mutex) == current) {
  11552. + pi_state = q.pi_state;
  11553. + get_pi_state(pi_state);
  11554. + }
  11555. /* Unqueue and drop the lock. */
  11556. unqueue_me_pi(&q);
  11557. }
  11558. + if (pi_state) {
  11559. + rt_mutex_futex_unlock(&pi_state->pi_mutex);
  11560. + put_pi_state(pi_state);
  11561. + }
  11562. +
  11563. if (ret == -EINTR) {
  11564. /*
  11565. * We've already been requeued, but cannot restart by calling
  11566. diff -Nur linux-4.9.28.orig/kernel/irq/handle.c linux-4.9.28/kernel/irq/handle.c
  11567. --- linux-4.9.28.orig/kernel/irq/handle.c 2017-05-14 14:00:37.000000000 +0200
  11568. +++ linux-4.9.28/kernel/irq/handle.c 2017-05-19 03:37:25.186176955 +0200
  11569. @@ -181,10 +181,16 @@
  11570. {
  11571. irqreturn_t retval;
  11572. unsigned int flags = 0;
  11573. + struct pt_regs *regs = get_irq_regs();
  11574. + u64 ip = regs ? instruction_pointer(regs) : 0;
  11575. retval = __handle_irq_event_percpu(desc, &flags);
  11576. - add_interrupt_randomness(desc->irq_data.irq, flags);
  11577. +#ifdef CONFIG_PREEMPT_RT_FULL
  11578. + desc->random_ip = ip;
  11579. +#else
  11580. + add_interrupt_randomness(desc->irq_data.irq, flags, ip);
  11581. +#endif
  11582. if (!noirqdebug)
  11583. note_interrupt(desc, retval);
  11584. diff -Nur linux-4.9.28.orig/kernel/irq/manage.c linux-4.9.28/kernel/irq/manage.c
  11585. --- linux-4.9.28.orig/kernel/irq/manage.c 2017-05-14 14:00:37.000000000 +0200
  11586. +++ linux-4.9.28/kernel/irq/manage.c 2017-05-19 03:37:25.186176955 +0200
  11587. @@ -22,6 +22,7 @@
  11588. #include "internals.h"
  11589. #ifdef CONFIG_IRQ_FORCED_THREADING
  11590. +# ifndef CONFIG_PREEMPT_RT_BASE
  11591. __read_mostly bool force_irqthreads;
  11592. static int __init setup_forced_irqthreads(char *arg)
  11593. @@ -30,6 +31,7 @@
  11594. return 0;
  11595. }
  11596. early_param("threadirqs", setup_forced_irqthreads);
  11597. +# endif
  11598. #endif
  11599. static void __synchronize_hardirq(struct irq_desc *desc)
  11600. @@ -233,7 +235,12 @@
  11601. if (desc->affinity_notify) {
  11602. kref_get(&desc->affinity_notify->kref);
  11603. +
  11604. +#ifdef CONFIG_PREEMPT_RT_BASE
  11605. + swork_queue(&desc->affinity_notify->swork);
  11606. +#else
  11607. schedule_work(&desc->affinity_notify->work);
  11608. +#endif
  11609. }
  11610. irqd_set(data, IRQD_AFFINITY_SET);
  11611. @@ -271,10 +278,8 @@
  11612. }
  11613. EXPORT_SYMBOL_GPL(irq_set_affinity_hint);
  11614. -static void irq_affinity_notify(struct work_struct *work)
  11615. +static void _irq_affinity_notify(struct irq_affinity_notify *notify)
  11616. {
  11617. - struct irq_affinity_notify *notify =
  11618. - container_of(work, struct irq_affinity_notify, work);
  11619. struct irq_desc *desc = irq_to_desc(notify->irq);
  11620. cpumask_var_t cpumask;
  11621. unsigned long flags;
  11622. @@ -296,6 +301,35 @@
  11623. kref_put(&notify->kref, notify->release);
  11624. }
  11625. +#ifdef CONFIG_PREEMPT_RT_BASE
  11626. +static void init_helper_thread(void)
  11627. +{
  11628. + static int init_sworker_once;
  11629. +
  11630. + if (init_sworker_once)
  11631. + return;
  11632. + if (WARN_ON(swork_get()))
  11633. + return;
  11634. + init_sworker_once = 1;
  11635. +}
  11636. +
  11637. +static void irq_affinity_notify(struct swork_event *swork)
  11638. +{
  11639. + struct irq_affinity_notify *notify =
  11640. + container_of(swork, struct irq_affinity_notify, swork);
  11641. + _irq_affinity_notify(notify);
  11642. +}
  11643. +
  11644. +#else
  11645. +
  11646. +static void irq_affinity_notify(struct work_struct *work)
  11647. +{
  11648. + struct irq_affinity_notify *notify =
  11649. + container_of(work, struct irq_affinity_notify, work);
  11650. + _irq_affinity_notify(notify);
  11651. +}
  11652. +#endif
  11653. +
  11654. /**
  11655. * irq_set_affinity_notifier - control notification of IRQ affinity changes
  11656. * @irq: Interrupt for which to enable/disable notification
  11657. @@ -324,7 +358,12 @@
  11658. if (notify) {
  11659. notify->irq = irq;
  11660. kref_init(&notify->kref);
  11661. +#ifdef CONFIG_PREEMPT_RT_BASE
  11662. + INIT_SWORK(&notify->swork, irq_affinity_notify);
  11663. + init_helper_thread();
  11664. +#else
  11665. INIT_WORK(&notify->work, irq_affinity_notify);
  11666. +#endif
  11667. }
  11668. raw_spin_lock_irqsave(&desc->lock, flags);
  11669. @@ -879,7 +918,15 @@
  11670. local_bh_disable();
  11671. ret = action->thread_fn(action->irq, action->dev_id);
  11672. irq_finalize_oneshot(desc, action);
  11673. - local_bh_enable();
  11674. + /*
  11675. + * Interrupts which have real time requirements can be set up
  11676. + * to avoid softirq processing in the thread handler. This is
  11677. + * safe as these interrupts do not raise soft interrupts.
  11678. + */
  11679. + if (irq_settings_no_softirq_call(desc))
  11680. + _local_bh_enable();
  11681. + else
  11682. + local_bh_enable();
  11683. return ret;
  11684. }
  11685. @@ -976,6 +1023,12 @@
  11686. if (action_ret == IRQ_WAKE_THREAD)
  11687. irq_wake_secondary(desc, action);
  11688. +#ifdef CONFIG_PREEMPT_RT_FULL
  11689. + migrate_disable();
  11690. + add_interrupt_randomness(action->irq, 0,
  11691. + desc->random_ip ^ (unsigned long) action);
  11692. + migrate_enable();
  11693. +#endif
  11694. wake_threads_waitq(desc);
  11695. }
  11696. @@ -1336,6 +1389,9 @@
  11697. irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
  11698. }
  11699. + if (new->flags & IRQF_NO_SOFTIRQ_CALL)
  11700. + irq_settings_set_no_softirq_call(desc);
  11701. +
  11702. /* Set default affinity mask once everything is setup */
  11703. setup_affinity(desc, mask);
  11704. @@ -2061,7 +2117,7 @@
  11705. * This call sets the internal irqchip state of an interrupt,
  11706. * depending on the value of @which.
  11707. *
  11708. - * This function should be called with preemption disabled if the
  11709. + * This function should be called with migration disabled if the
  11710. * interrupt controller has per-cpu registers.
  11711. */
  11712. int irq_set_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
  11713. diff -Nur linux-4.9.28.orig/kernel/irq/settings.h linux-4.9.28/kernel/irq/settings.h
  11714. --- linux-4.9.28.orig/kernel/irq/settings.h 2017-05-14 14:00:37.000000000 +0200
  11715. +++ linux-4.9.28/kernel/irq/settings.h 2017-05-19 03:37:25.186176955 +0200
  11716. @@ -16,6 +16,7 @@
  11717. _IRQ_PER_CPU_DEVID = IRQ_PER_CPU_DEVID,
  11718. _IRQ_IS_POLLED = IRQ_IS_POLLED,
  11719. _IRQ_DISABLE_UNLAZY = IRQ_DISABLE_UNLAZY,
  11720. + _IRQ_NO_SOFTIRQ_CALL = IRQ_NO_SOFTIRQ_CALL,
  11721. _IRQF_MODIFY_MASK = IRQF_MODIFY_MASK,
  11722. };
  11723. @@ -30,6 +31,7 @@
  11724. #define IRQ_PER_CPU_DEVID GOT_YOU_MORON
  11725. #define IRQ_IS_POLLED GOT_YOU_MORON
  11726. #define IRQ_DISABLE_UNLAZY GOT_YOU_MORON
  11727. +#define IRQ_NO_SOFTIRQ_CALL GOT_YOU_MORON
  11728. #undef IRQF_MODIFY_MASK
  11729. #define IRQF_MODIFY_MASK GOT_YOU_MORON
  11730. @@ -40,6 +42,16 @@
  11731. desc->status_use_accessors |= (set & _IRQF_MODIFY_MASK);
  11732. }
  11733. +static inline bool irq_settings_no_softirq_call(struct irq_desc *desc)
  11734. +{
  11735. + return desc->status_use_accessors & _IRQ_NO_SOFTIRQ_CALL;
  11736. +}
  11737. +
  11738. +static inline void irq_settings_set_no_softirq_call(struct irq_desc *desc)
  11739. +{
  11740. + desc->status_use_accessors |= _IRQ_NO_SOFTIRQ_CALL;
  11741. +}
  11742. +
  11743. static inline bool irq_settings_is_per_cpu(struct irq_desc *desc)
  11744. {
  11745. return desc->status_use_accessors & _IRQ_PER_CPU;
  11746. diff -Nur linux-4.9.28.orig/kernel/irq/spurious.c linux-4.9.28/kernel/irq/spurious.c
  11747. --- linux-4.9.28.orig/kernel/irq/spurious.c 2017-05-14 14:00:37.000000000 +0200
  11748. +++ linux-4.9.28/kernel/irq/spurious.c 2017-05-19 03:37:25.186176955 +0200
  11749. @@ -442,6 +442,10 @@
  11750. static int __init irqfixup_setup(char *str)
  11751. {
  11752. +#ifdef CONFIG_PREEMPT_RT_BASE
  11753. + pr_warn("irqfixup boot option not supported w/ CONFIG_PREEMPT_RT_BASE\n");
  11754. + return 1;
  11755. +#endif
  11756. irqfixup = 1;
  11757. printk(KERN_WARNING "Misrouted IRQ fixup support enabled.\n");
  11758. printk(KERN_WARNING "This may impact system performance.\n");
  11759. @@ -454,6 +458,10 @@
  11760. static int __init irqpoll_setup(char *str)
  11761. {
  11762. +#ifdef CONFIG_PREEMPT_RT_BASE
  11763. + pr_warn("irqpoll boot option not supported w/ CONFIG_PREEMPT_RT_BASE\n");
  11764. + return 1;
  11765. +#endif
  11766. irqfixup = 2;
  11767. printk(KERN_WARNING "Misrouted IRQ fixup and polling support "
  11768. "enabled\n");
  11769. diff -Nur linux-4.9.28.orig/kernel/irq_work.c linux-4.9.28/kernel/irq_work.c
  11770. --- linux-4.9.28.orig/kernel/irq_work.c 2017-05-14 14:00:37.000000000 +0200
  11771. +++ linux-4.9.28/kernel/irq_work.c 2017-05-19 03:37:25.186176955 +0200
  11772. @@ -17,6 +17,7 @@
  11773. #include <linux/cpu.h>
  11774. #include <linux/notifier.h>
  11775. #include <linux/smp.h>
  11776. +#include <linux/interrupt.h>
  11777. #include <asm/processor.h>
  11778. @@ -65,6 +66,8 @@
  11779. */
  11780. bool irq_work_queue_on(struct irq_work *work, int cpu)
  11781. {
  11782. + struct llist_head *list;
  11783. +
  11784. /* All work should have been flushed before going offline */
  11785. WARN_ON_ONCE(cpu_is_offline(cpu));
  11786. @@ -75,7 +78,12 @@
  11787. if (!irq_work_claim(work))
  11788. return false;
  11789. - if (llist_add(&work->llnode, &per_cpu(raised_list, cpu)))
  11790. + if (IS_ENABLED(CONFIG_PREEMPT_RT_FULL) && !(work->flags & IRQ_WORK_HARD_IRQ))
  11791. + list = &per_cpu(lazy_list, cpu);
  11792. + else
  11793. + list = &per_cpu(raised_list, cpu);
  11794. +
  11795. + if (llist_add(&work->llnode, list))
  11796. arch_send_call_function_single_ipi(cpu);
  11797. return true;
  11798. @@ -86,6 +94,9 @@
  11799. /* Enqueue the irq work @work on the current CPU */
  11800. bool irq_work_queue(struct irq_work *work)
  11801. {
  11802. + struct llist_head *list;
  11803. + bool lazy_work, realtime = IS_ENABLED(CONFIG_PREEMPT_RT_FULL);
  11804. +
  11805. /* Only queue if not already pending */
  11806. if (!irq_work_claim(work))
  11807. return false;
  11808. @@ -93,13 +104,15 @@
  11809. /* Queue the entry and raise the IPI if needed. */
  11810. preempt_disable();
  11811. - /* If the work is "lazy", handle it from next tick if any */
  11812. - if (work->flags & IRQ_WORK_LAZY) {
  11813. - if (llist_add(&work->llnode, this_cpu_ptr(&lazy_list)) &&
  11814. - tick_nohz_tick_stopped())
  11815. - arch_irq_work_raise();
  11816. - } else {
  11817. - if (llist_add(&work->llnode, this_cpu_ptr(&raised_list)))
  11818. + lazy_work = work->flags & IRQ_WORK_LAZY;
  11819. +
  11820. + if (lazy_work || (realtime && !(work->flags & IRQ_WORK_HARD_IRQ)))
  11821. + list = this_cpu_ptr(&lazy_list);
  11822. + else
  11823. + list = this_cpu_ptr(&raised_list);
  11824. +
  11825. + if (llist_add(&work->llnode, list)) {
  11826. + if (!lazy_work || tick_nohz_tick_stopped())
  11827. arch_irq_work_raise();
  11828. }
  11829. @@ -116,9 +129,8 @@
  11830. raised = this_cpu_ptr(&raised_list);
  11831. lazy = this_cpu_ptr(&lazy_list);
  11832. - if (llist_empty(raised) || arch_irq_work_has_interrupt())
  11833. - if (llist_empty(lazy))
  11834. - return false;
  11835. + if (llist_empty(raised) && llist_empty(lazy))
  11836. + return false;
  11837. /* All work should have been flushed before going offline */
  11838. WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
  11839. @@ -132,7 +144,7 @@
  11840. struct irq_work *work;
  11841. struct llist_node *llnode;
  11842. - BUG_ON(!irqs_disabled());
  11843. + BUG_ON_NONRT(!irqs_disabled());
  11844. if (llist_empty(list))
  11845. return;
  11846. @@ -169,7 +181,16 @@
  11847. void irq_work_run(void)
  11848. {
  11849. irq_work_run_list(this_cpu_ptr(&raised_list));
  11850. - irq_work_run_list(this_cpu_ptr(&lazy_list));
  11851. + if (IS_ENABLED(CONFIG_PREEMPT_RT_FULL)) {
  11852. + /*
  11853. + * NOTE: we raise softirq via IPI for safety,
  11854. + * and execute in irq_work_tick() to move the
  11855. + * overhead from hard to soft irq context.
  11856. + */
  11857. + if (!llist_empty(this_cpu_ptr(&lazy_list)))
  11858. + raise_softirq(TIMER_SOFTIRQ);
  11859. + } else
  11860. + irq_work_run_list(this_cpu_ptr(&lazy_list));
  11861. }
  11862. EXPORT_SYMBOL_GPL(irq_work_run);
  11863. @@ -179,8 +200,17 @@
  11864. if (!llist_empty(raised) && !arch_irq_work_has_interrupt())
  11865. irq_work_run_list(raised);
  11866. +
  11867. + if (!IS_ENABLED(CONFIG_PREEMPT_RT_FULL))
  11868. + irq_work_run_list(this_cpu_ptr(&lazy_list));
  11869. +}
  11870. +
  11871. +#if defined(CONFIG_IRQ_WORK) && defined(CONFIG_PREEMPT_RT_FULL)
  11872. +void irq_work_tick_soft(void)
  11873. +{
  11874. irq_work_run_list(this_cpu_ptr(&lazy_list));
  11875. }
  11876. +#endif
  11877. /*
  11878. * Synchronize against the irq_work @entry, ensures the entry is not
  11879. diff -Nur linux-4.9.28.orig/kernel/Kconfig.locks linux-4.9.28/kernel/Kconfig.locks
  11880. --- linux-4.9.28.orig/kernel/Kconfig.locks 2017-05-14 14:00:37.000000000 +0200
  11881. +++ linux-4.9.28/kernel/Kconfig.locks 2017-05-19 03:37:25.178176648 +0200
  11882. @@ -225,11 +225,11 @@
  11883. config MUTEX_SPIN_ON_OWNER
  11884. def_bool y
  11885. - depends on SMP && !DEBUG_MUTEXES && ARCH_SUPPORTS_ATOMIC_RMW
  11886. + depends on SMP && !DEBUG_MUTEXES && ARCH_SUPPORTS_ATOMIC_RMW && !PREEMPT_RT_FULL
  11887. config RWSEM_SPIN_ON_OWNER
  11888. def_bool y
  11889. - depends on SMP && RWSEM_XCHGADD_ALGORITHM && ARCH_SUPPORTS_ATOMIC_RMW
  11890. + depends on SMP && RWSEM_XCHGADD_ALGORITHM && ARCH_SUPPORTS_ATOMIC_RMW && !PREEMPT_RT_FULL
  11891. config LOCK_SPIN_ON_OWNER
  11892. def_bool y
  11893. diff -Nur linux-4.9.28.orig/kernel/Kconfig.preempt linux-4.9.28/kernel/Kconfig.preempt
  11894. --- linux-4.9.28.orig/kernel/Kconfig.preempt 2017-05-14 14:00:37.000000000 +0200
  11895. +++ linux-4.9.28/kernel/Kconfig.preempt 2017-05-19 03:37:25.178176648 +0200
  11896. @@ -1,3 +1,16 @@
  11897. +config PREEMPT
  11898. + bool
  11899. + select PREEMPT_COUNT
  11900. +
  11901. +config PREEMPT_RT_BASE
  11902. + bool
  11903. + select PREEMPT
  11904. +
  11905. +config HAVE_PREEMPT_LAZY
  11906. + bool
  11907. +
  11908. +config PREEMPT_LAZY
  11909. + def_bool y if HAVE_PREEMPT_LAZY && PREEMPT_RT_FULL
  11910. choice
  11911. prompt "Preemption Model"
  11912. @@ -33,9 +46,9 @@
  11913. Select this if you are building a kernel for a desktop system.
  11914. -config PREEMPT
  11915. +config PREEMPT__LL
  11916. bool "Preemptible Kernel (Low-Latency Desktop)"
  11917. - select PREEMPT_COUNT
  11918. + select PREEMPT
  11919. select UNINLINE_SPIN_UNLOCK if !ARCH_INLINE_SPIN_UNLOCK
  11920. help
  11921. This option reduces the latency of the kernel by making
  11922. @@ -52,6 +65,22 @@
  11923. embedded system with latency requirements in the milliseconds
  11924. range.
  11925. +config PREEMPT_RTB
  11926. + bool "Preemptible Kernel (Basic RT)"
  11927. + select PREEMPT_RT_BASE
  11928. + help
  11929. + This option is basically the same as (Low-Latency Desktop) but
  11930. + enables changes which are preliminary for the full preemptible
  11931. + RT kernel.
  11932. +
  11933. +config PREEMPT_RT_FULL
  11934. + bool "Fully Preemptible Kernel (RT)"
  11935. + depends on IRQ_FORCED_THREADING
  11936. + select PREEMPT_RT_BASE
  11937. + select PREEMPT_RCU
  11938. + help
  11939. + All and everything
  11940. +
  11941. endchoice
  11942. config PREEMPT_COUNT
  11943. diff -Nur linux-4.9.28.orig/kernel/ksysfs.c linux-4.9.28/kernel/ksysfs.c
  11944. --- linux-4.9.28.orig/kernel/ksysfs.c 2017-05-14 14:00:37.000000000 +0200
  11945. +++ linux-4.9.28/kernel/ksysfs.c 2017-05-19 03:37:25.186176955 +0200
  11946. @@ -136,6 +136,15 @@
  11947. #endif /* CONFIG_KEXEC_CORE */
  11948. +#if defined(CONFIG_PREEMPT_RT_FULL)
  11949. +static ssize_t realtime_show(struct kobject *kobj,
  11950. + struct kobj_attribute *attr, char *buf)
  11951. +{
  11952. + return sprintf(buf, "%d\n", 1);
  11953. +}
  11954. +KERNEL_ATTR_RO(realtime);
  11955. +#endif
  11956. +
  11957. /* whether file capabilities are enabled */
  11958. static ssize_t fscaps_show(struct kobject *kobj,
  11959. struct kobj_attribute *attr, char *buf)
  11960. @@ -225,6 +234,9 @@
  11961. &rcu_expedited_attr.attr,
  11962. &rcu_normal_attr.attr,
  11963. #endif
  11964. +#ifdef CONFIG_PREEMPT_RT_FULL
  11965. + &realtime_attr.attr,
  11966. +#endif
  11967. NULL
  11968. };
  11969. diff -Nur linux-4.9.28.orig/kernel/locking/lockdep.c linux-4.9.28/kernel/locking/lockdep.c
  11970. --- linux-4.9.28.orig/kernel/locking/lockdep.c 2017-05-14 14:00:37.000000000 +0200
  11971. +++ linux-4.9.28/kernel/locking/lockdep.c 2017-05-19 03:37:25.186176955 +0200
  11972. @@ -658,6 +658,7 @@
  11973. struct lockdep_subclass_key *key;
  11974. struct hlist_head *hash_head;
  11975. struct lock_class *class;
  11976. + bool is_static = false;
  11977. if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
  11978. debug_locks_off();
  11979. @@ -671,10 +672,23 @@
  11980. /*
  11981. * Static locks do not have their class-keys yet - for them the key
  11982. - * is the lock object itself:
  11983. - */
  11984. - if (unlikely(!lock->key))
  11985. - lock->key = (void *)lock;
  11986. + * is the lock object itself. If the lock is in the per cpu area,
  11987. + * the canonical address of the lock (per cpu offset removed) is
  11988. + * used.
  11989. + */
  11990. + if (unlikely(!lock->key)) {
  11991. + unsigned long can_addr, addr = (unsigned long)lock;
  11992. +
  11993. + if (__is_kernel_percpu_address(addr, &can_addr))
  11994. + lock->key = (void *)can_addr;
  11995. + else if (__is_module_percpu_address(addr, &can_addr))
  11996. + lock->key = (void *)can_addr;
  11997. + else if (static_obj(lock))
  11998. + lock->key = (void *)lock;
  11999. + else
  12000. + return ERR_PTR(-EINVAL);
  12001. + is_static = true;
  12002. + }
  12003. /*
  12004. * NOTE: the class-key must be unique. For dynamic locks, a static
  12005. @@ -706,7 +720,7 @@
  12006. }
  12007. }
  12008. - return NULL;
  12009. + return is_static || static_obj(lock->key) ? NULL : ERR_PTR(-EINVAL);
  12010. }
  12011. /*
  12012. @@ -724,19 +738,18 @@
  12013. DEBUG_LOCKS_WARN_ON(!irqs_disabled());
  12014. class = look_up_lock_class(lock, subclass);
  12015. - if (likely(class))
  12016. + if (likely(!IS_ERR_OR_NULL(class)))
  12017. goto out_set_class_cache;
  12018. /*
  12019. * Debug-check: all keys must be persistent!
  12020. - */
  12021. - if (!static_obj(lock->key)) {
  12022. + */
  12023. + if (IS_ERR(class)) {
  12024. debug_locks_off();
  12025. printk("INFO: trying to register non-static key.\n");
  12026. printk("the code is fine but needs lockdep annotation.\n");
  12027. printk("turning off the locking correctness validator.\n");
  12028. dump_stack();
  12029. -
  12030. return NULL;
  12031. }
  12032. @@ -3410,7 +3423,7 @@
  12033. * Clearly if the lock hasn't been acquired _ever_, we're not
  12034. * holding it either, so report failure.
  12035. */
  12036. - if (!class)
  12037. + if (IS_ERR_OR_NULL(class))
  12038. return 0;
  12039. /*
  12040. @@ -3689,6 +3702,7 @@
  12041. }
  12042. }
  12043. +#ifndef CONFIG_PREEMPT_RT_FULL
  12044. /*
  12045. * We dont accurately track softirq state in e.g.
  12046. * hardirq contexts (such as on 4KSTACKS), so only
  12047. @@ -3703,6 +3717,7 @@
  12048. DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
  12049. }
  12050. }
  12051. +#endif
  12052. if (!debug_locks)
  12053. print_irqtrace_events(current);
  12054. @@ -4159,7 +4174,7 @@
  12055. * If the class exists we look it up and zap it:
  12056. */
  12057. class = look_up_lock_class(lock, j);
  12058. - if (class)
  12059. + if (!IS_ERR_OR_NULL(class))
  12060. zap_class(class);
  12061. }
  12062. /*
  12063. diff -Nur linux-4.9.28.orig/kernel/locking/locktorture.c linux-4.9.28/kernel/locking/locktorture.c
  12064. --- linux-4.9.28.orig/kernel/locking/locktorture.c 2017-05-14 14:00:37.000000000 +0200
  12065. +++ linux-4.9.28/kernel/locking/locktorture.c 2017-05-19 03:37:25.186176955 +0200
  12066. @@ -26,7 +26,6 @@
  12067. #include <linux/kthread.h>
  12068. #include <linux/sched/rt.h>
  12069. #include <linux/spinlock.h>
  12070. -#include <linux/rwlock.h>
  12071. #include <linux/mutex.h>
  12072. #include <linux/rwsem.h>
  12073. #include <linux/smp.h>
  12074. diff -Nur linux-4.9.28.orig/kernel/locking/Makefile linux-4.9.28/kernel/locking/Makefile
  12075. --- linux-4.9.28.orig/kernel/locking/Makefile 2017-05-14 14:00:37.000000000 +0200
  12076. +++ linux-4.9.28/kernel/locking/Makefile 2017-05-19 03:37:25.186176955 +0200
  12077. @@ -2,7 +2,7 @@
  12078. # and is generally not a function of system call inputs.
  12079. KCOV_INSTRUMENT := n
  12080. -obj-y += mutex.o semaphore.o rwsem.o percpu-rwsem.o
  12081. +obj-y += semaphore.o percpu-rwsem.o
  12082. ifdef CONFIG_FUNCTION_TRACER
  12083. CFLAGS_REMOVE_lockdep.o = $(CC_FLAGS_FTRACE)
  12084. @@ -11,7 +11,11 @@
  12085. CFLAGS_REMOVE_rtmutex-debug.o = $(CC_FLAGS_FTRACE)
  12086. endif
  12087. +ifneq ($(CONFIG_PREEMPT_RT_FULL),y)
  12088. +obj-y += mutex.o
  12089. obj-$(CONFIG_DEBUG_MUTEXES) += mutex-debug.o
  12090. +endif
  12091. +obj-y += rwsem.o
  12092. obj-$(CONFIG_LOCKDEP) += lockdep.o
  12093. ifeq ($(CONFIG_PROC_FS),y)
  12094. obj-$(CONFIG_LOCKDEP) += lockdep_proc.o
  12095. @@ -24,7 +28,10 @@
  12096. obj-$(CONFIG_DEBUG_RT_MUTEXES) += rtmutex-debug.o
  12097. obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock.o
  12098. obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock_debug.o
  12099. +ifneq ($(CONFIG_PREEMPT_RT_FULL),y)
  12100. obj-$(CONFIG_RWSEM_GENERIC_SPINLOCK) += rwsem-spinlock.o
  12101. obj-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem-xadd.o
  12102. +endif
  12103. +obj-$(CONFIG_PREEMPT_RT_FULL) += rt.o rwsem-rt.o
  12104. obj-$(CONFIG_QUEUED_RWLOCKS) += qrwlock.o
  12105. obj-$(CONFIG_LOCK_TORTURE_TEST) += locktorture.o
  12106. diff -Nur linux-4.9.28.orig/kernel/locking/percpu-rwsem.c linux-4.9.28/kernel/locking/percpu-rwsem.c
  12107. --- linux-4.9.28.orig/kernel/locking/percpu-rwsem.c 2017-05-14 14:00:37.000000000 +0200
  12108. +++ linux-4.9.28/kernel/locking/percpu-rwsem.c 2017-05-19 03:37:25.186176955 +0200
  12109. @@ -18,7 +18,7 @@
  12110. /* ->rw_sem represents the whole percpu_rw_semaphore for lockdep */
  12111. rcu_sync_init(&sem->rss, RCU_SCHED_SYNC);
  12112. __init_rwsem(&sem->rw_sem, name, rwsem_key);
  12113. - init_waitqueue_head(&sem->writer);
  12114. + init_swait_queue_head(&sem->writer);
  12115. sem->readers_block = 0;
  12116. return 0;
  12117. }
  12118. @@ -103,7 +103,7 @@
  12119. __this_cpu_dec(*sem->read_count);
  12120. /* Prod writer to recheck readers_active */
  12121. - wake_up(&sem->writer);
  12122. + swake_up(&sem->writer);
  12123. }
  12124. EXPORT_SYMBOL_GPL(__percpu_up_read);
  12125. @@ -160,7 +160,7 @@
  12126. */
  12127. /* Wait for all now active readers to complete. */
  12128. - wait_event(sem->writer, readers_active_check(sem));
  12129. + swait_event(sem->writer, readers_active_check(sem));
  12130. }
  12131. EXPORT_SYMBOL_GPL(percpu_down_write);
  12132. diff -Nur linux-4.9.28.orig/kernel/locking/rt.c linux-4.9.28/kernel/locking/rt.c
  12133. --- linux-4.9.28.orig/kernel/locking/rt.c 1970-01-01 01:00:00.000000000 +0100
  12134. +++ linux-4.9.28/kernel/locking/rt.c 2017-05-19 03:37:25.186176955 +0200
  12135. @@ -0,0 +1,331 @@
  12136. +/*
  12137. + * kernel/rt.c
  12138. + *
  12139. + * Real-Time Preemption Support
  12140. + *
  12141. + * started by Ingo Molnar:
  12142. + *
  12143. + * Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  12144. + * Copyright (C) 2006, Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  12145. + *
  12146. + * historic credit for proving that Linux spinlocks can be implemented via
  12147. + * RT-aware mutexes goes to many people: The Pmutex project (Dirk Grambow
  12148. + * and others) who prototyped it on 2.4 and did lots of comparative
  12149. + * research and analysis; TimeSys, for proving that you can implement a
  12150. + * fully preemptible kernel via the use of IRQ threading and mutexes;
  12151. + * Bill Huey for persuasively arguing on lkml that the mutex model is the
  12152. + * right one; and to MontaVista, who ported pmutexes to 2.6.
  12153. + *
  12154. + * This code is a from-scratch implementation and is not based on pmutexes,
  12155. + * but the idea of converting spinlocks to mutexes is used here too.
  12156. + *
  12157. + * lock debugging, locking tree, deadlock detection:
  12158. + *
  12159. + * Copyright (C) 2004, LynuxWorks, Inc., Igor Manyilov, Bill Huey
  12160. + * Released under the General Public License (GPL).
  12161. + *
  12162. + * Includes portions of the generic R/W semaphore implementation from:
  12163. + *
  12164. + * Copyright (c) 2001 David Howells (dhowells@redhat.com).
  12165. + * - Derived partially from idea by Andrea Arcangeli <andrea@suse.de>
  12166. + * - Derived also from comments by Linus
  12167. + *
  12168. + * Pending ownership of locks and ownership stealing:
  12169. + *
  12170. + * Copyright (C) 2005, Kihon Technologies Inc., Steven Rostedt
  12171. + *
  12172. + * (also by Steven Rostedt)
  12173. + * - Converted single pi_lock to individual task locks.
  12174. + *
  12175. + * By Esben Nielsen:
  12176. + * Doing priority inheritance with help of the scheduler.
  12177. + *
  12178. + * Copyright (C) 2006, Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  12179. + * - major rework based on Esben Nielsens initial patch
  12180. + * - replaced thread_info references by task_struct refs
  12181. + * - removed task->pending_owner dependency
  12182. + * - BKL drop/reacquire for semaphore style locks to avoid deadlocks
  12183. + * in the scheduler return path as discussed with Steven Rostedt
  12184. + *
  12185. + * Copyright (C) 2006, Kihon Technologies Inc.
  12186. + * Steven Rostedt <rostedt@goodmis.org>
  12187. + * - debugged and patched Thomas Gleixner's rework.
  12188. + * - added back the cmpxchg to the rework.
  12189. + * - turned atomic require back on for SMP.
  12190. + */
  12191. +
  12192. +#include <linux/spinlock.h>
  12193. +#include <linux/rtmutex.h>
  12194. +#include <linux/sched.h>
  12195. +#include <linux/delay.h>
  12196. +#include <linux/module.h>
  12197. +#include <linux/kallsyms.h>
  12198. +#include <linux/syscalls.h>
  12199. +#include <linux/interrupt.h>
  12200. +#include <linux/plist.h>
  12201. +#include <linux/fs.h>
  12202. +#include <linux/futex.h>
  12203. +#include <linux/hrtimer.h>
  12204. +
  12205. +#include "rtmutex_common.h"
  12206. +
  12207. +/*
  12208. + * struct mutex functions
  12209. + */
  12210. +void __mutex_do_init(struct mutex *mutex, const char *name,
  12211. + struct lock_class_key *key)
  12212. +{
  12213. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12214. + /*
  12215. + * Make sure we are not reinitializing a held lock:
  12216. + */
  12217. + debug_check_no_locks_freed((void *)mutex, sizeof(*mutex));
  12218. + lockdep_init_map(&mutex->dep_map, name, key, 0);
  12219. +#endif
  12220. + mutex->lock.save_state = 0;
  12221. +}
  12222. +EXPORT_SYMBOL(__mutex_do_init);
  12223. +
  12224. +void __lockfunc _mutex_lock(struct mutex *lock)
  12225. +{
  12226. + mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  12227. + rt_mutex_lock(&lock->lock);
  12228. +}
  12229. +EXPORT_SYMBOL(_mutex_lock);
  12230. +
  12231. +int __lockfunc _mutex_lock_interruptible(struct mutex *lock)
  12232. +{
  12233. + int ret;
  12234. +
  12235. + mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  12236. + ret = rt_mutex_lock_interruptible(&lock->lock);
  12237. + if (ret)
  12238. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  12239. + return ret;
  12240. +}
  12241. +EXPORT_SYMBOL(_mutex_lock_interruptible);
  12242. +
  12243. +int __lockfunc _mutex_lock_killable(struct mutex *lock)
  12244. +{
  12245. + int ret;
  12246. +
  12247. + mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  12248. + ret = rt_mutex_lock_killable(&lock->lock);
  12249. + if (ret)
  12250. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  12251. + return ret;
  12252. +}
  12253. +EXPORT_SYMBOL(_mutex_lock_killable);
  12254. +
  12255. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12256. +void __lockfunc _mutex_lock_nested(struct mutex *lock, int subclass)
  12257. +{
  12258. + mutex_acquire_nest(&lock->dep_map, subclass, 0, NULL, _RET_IP_);
  12259. + rt_mutex_lock(&lock->lock);
  12260. +}
  12261. +EXPORT_SYMBOL(_mutex_lock_nested);
  12262. +
  12263. +void __lockfunc _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest)
  12264. +{
  12265. + mutex_acquire_nest(&lock->dep_map, 0, 0, nest, _RET_IP_);
  12266. + rt_mutex_lock(&lock->lock);
  12267. +}
  12268. +EXPORT_SYMBOL(_mutex_lock_nest_lock);
  12269. +
  12270. +int __lockfunc _mutex_lock_interruptible_nested(struct mutex *lock, int subclass)
  12271. +{
  12272. + int ret;
  12273. +
  12274. + mutex_acquire_nest(&lock->dep_map, subclass, 0, NULL, _RET_IP_);
  12275. + ret = rt_mutex_lock_interruptible(&lock->lock);
  12276. + if (ret)
  12277. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  12278. + return ret;
  12279. +}
  12280. +EXPORT_SYMBOL(_mutex_lock_interruptible_nested);
  12281. +
  12282. +int __lockfunc _mutex_lock_killable_nested(struct mutex *lock, int subclass)
  12283. +{
  12284. + int ret;
  12285. +
  12286. + mutex_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
  12287. + ret = rt_mutex_lock_killable(&lock->lock);
  12288. + if (ret)
  12289. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  12290. + return ret;
  12291. +}
  12292. +EXPORT_SYMBOL(_mutex_lock_killable_nested);
  12293. +#endif
  12294. +
  12295. +int __lockfunc _mutex_trylock(struct mutex *lock)
  12296. +{
  12297. + int ret = rt_mutex_trylock(&lock->lock);
  12298. +
  12299. + if (ret)
  12300. + mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  12301. +
  12302. + return ret;
  12303. +}
  12304. +EXPORT_SYMBOL(_mutex_trylock);
  12305. +
  12306. +void __lockfunc _mutex_unlock(struct mutex *lock)
  12307. +{
  12308. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  12309. + rt_mutex_unlock(&lock->lock);
  12310. +}
  12311. +EXPORT_SYMBOL(_mutex_unlock);
  12312. +
  12313. +/*
  12314. + * rwlock_t functions
  12315. + */
  12316. +int __lockfunc rt_write_trylock(rwlock_t *rwlock)
  12317. +{
  12318. + int ret;
  12319. +
  12320. + migrate_disable();
  12321. + ret = rt_mutex_trylock(&rwlock->lock);
  12322. + if (ret)
  12323. + rwlock_acquire(&rwlock->dep_map, 0, 1, _RET_IP_);
  12324. + else
  12325. + migrate_enable();
  12326. +
  12327. + return ret;
  12328. +}
  12329. +EXPORT_SYMBOL(rt_write_trylock);
  12330. +
  12331. +int __lockfunc rt_write_trylock_irqsave(rwlock_t *rwlock, unsigned long *flags)
  12332. +{
  12333. + int ret;
  12334. +
  12335. + *flags = 0;
  12336. + ret = rt_write_trylock(rwlock);
  12337. + return ret;
  12338. +}
  12339. +EXPORT_SYMBOL(rt_write_trylock_irqsave);
  12340. +
  12341. +int __lockfunc rt_read_trylock(rwlock_t *rwlock)
  12342. +{
  12343. + struct rt_mutex *lock = &rwlock->lock;
  12344. + int ret = 1;
  12345. +
  12346. + /*
  12347. + * recursive read locks succeed when current owns the lock,
  12348. + * but not when read_depth == 0 which means that the lock is
  12349. + * write locked.
  12350. + */
  12351. + if (rt_mutex_owner(lock) != current) {
  12352. + migrate_disable();
  12353. + ret = rt_mutex_trylock(lock);
  12354. + if (ret)
  12355. + rwlock_acquire(&rwlock->dep_map, 0, 1, _RET_IP_);
  12356. + else
  12357. + migrate_enable();
  12358. +
  12359. + } else if (!rwlock->read_depth) {
  12360. + ret = 0;
  12361. + }
  12362. +
  12363. + if (ret)
  12364. + rwlock->read_depth++;
  12365. +
  12366. + return ret;
  12367. +}
  12368. +EXPORT_SYMBOL(rt_read_trylock);
  12369. +
  12370. +void __lockfunc rt_write_lock(rwlock_t *rwlock)
  12371. +{
  12372. + rwlock_acquire(&rwlock->dep_map, 0, 0, _RET_IP_);
  12373. + __rt_spin_lock(&rwlock->lock);
  12374. +}
  12375. +EXPORT_SYMBOL(rt_write_lock);
  12376. +
  12377. +void __lockfunc rt_read_lock(rwlock_t *rwlock)
  12378. +{
  12379. + struct rt_mutex *lock = &rwlock->lock;
  12380. +
  12381. +
  12382. + /*
  12383. + * recursive read locks succeed when current owns the lock
  12384. + */
  12385. + if (rt_mutex_owner(lock) != current) {
  12386. + rwlock_acquire(&rwlock->dep_map, 0, 0, _RET_IP_);
  12387. + __rt_spin_lock(lock);
  12388. + }
  12389. + rwlock->read_depth++;
  12390. +}
  12391. +
  12392. +EXPORT_SYMBOL(rt_read_lock);
  12393. +
  12394. +void __lockfunc rt_write_unlock(rwlock_t *rwlock)
  12395. +{
  12396. + /* NOTE: we always pass in '1' for nested, for simplicity */
  12397. + rwlock_release(&rwlock->dep_map, 1, _RET_IP_);
  12398. + __rt_spin_unlock(&rwlock->lock);
  12399. + migrate_enable();
  12400. +}
  12401. +EXPORT_SYMBOL(rt_write_unlock);
  12402. +
  12403. +void __lockfunc rt_read_unlock(rwlock_t *rwlock)
  12404. +{
  12405. + /* Release the lock only when read_depth is down to 0 */
  12406. + if (--rwlock->read_depth == 0) {
  12407. + rwlock_release(&rwlock->dep_map, 1, _RET_IP_);
  12408. + __rt_spin_unlock(&rwlock->lock);
  12409. + migrate_enable();
  12410. + }
  12411. +}
  12412. +EXPORT_SYMBOL(rt_read_unlock);
  12413. +
  12414. +unsigned long __lockfunc rt_write_lock_irqsave(rwlock_t *rwlock)
  12415. +{
  12416. + rt_write_lock(rwlock);
  12417. +
  12418. + return 0;
  12419. +}
  12420. +EXPORT_SYMBOL(rt_write_lock_irqsave);
  12421. +
  12422. +unsigned long __lockfunc rt_read_lock_irqsave(rwlock_t *rwlock)
  12423. +{
  12424. + rt_read_lock(rwlock);
  12425. +
  12426. + return 0;
  12427. +}
  12428. +EXPORT_SYMBOL(rt_read_lock_irqsave);
  12429. +
  12430. +void __rt_rwlock_init(rwlock_t *rwlock, char *name, struct lock_class_key *key)
  12431. +{
  12432. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12433. + /*
  12434. + * Make sure we are not reinitializing a held lock:
  12435. + */
  12436. + debug_check_no_locks_freed((void *)rwlock, sizeof(*rwlock));
  12437. + lockdep_init_map(&rwlock->dep_map, name, key, 0);
  12438. +#endif
  12439. + rwlock->lock.save_state = 1;
  12440. + rwlock->read_depth = 0;
  12441. +}
  12442. +EXPORT_SYMBOL(__rt_rwlock_init);
  12443. +
  12444. +/**
  12445. + * atomic_dec_and_mutex_lock - return holding mutex if we dec to 0
  12446. + * @cnt: the atomic which we are to dec
  12447. + * @lock: the mutex to return holding if we dec to 0
  12448. + *
  12449. + * return true and hold lock if we dec to 0, return false otherwise
  12450. + */
  12451. +int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock)
  12452. +{
  12453. + /* dec if we can't possibly hit 0 */
  12454. + if (atomic_add_unless(cnt, -1, 1))
  12455. + return 0;
  12456. + /* we might hit 0, so take the lock */
  12457. + mutex_lock(lock);
  12458. + if (!atomic_dec_and_test(cnt)) {
  12459. + /* when we actually did the dec, we didn't hit 0 */
  12460. + mutex_unlock(lock);
  12461. + return 0;
  12462. + }
  12463. + /* we hit 0, and we hold the lock */
  12464. + return 1;
  12465. +}
  12466. +EXPORT_SYMBOL(atomic_dec_and_mutex_lock);
  12467. diff -Nur linux-4.9.28.orig/kernel/locking/rtmutex.c linux-4.9.28/kernel/locking/rtmutex.c
  12468. --- linux-4.9.28.orig/kernel/locking/rtmutex.c 2017-05-14 14:00:37.000000000 +0200
  12469. +++ linux-4.9.28/kernel/locking/rtmutex.c 2017-05-19 03:37:25.186176955 +0200
  12470. @@ -7,6 +7,11 @@
  12471. * Copyright (C) 2005-2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  12472. * Copyright (C) 2005 Kihon Technologies Inc., Steven Rostedt
  12473. * Copyright (C) 2006 Esben Nielsen
  12474. + * Adaptive Spinlocks:
  12475. + * Copyright (C) 2008 Novell, Inc., Gregory Haskins, Sven Dietrich,
  12476. + * and Peter Morreale,
  12477. + * Adaptive Spinlocks simplification:
  12478. + * Copyright (C) 2008 Red Hat, Inc., Steven Rostedt <srostedt@redhat.com>
  12479. *
  12480. * See Documentation/locking/rt-mutex-design.txt for details.
  12481. */
  12482. @@ -16,6 +21,7 @@
  12483. #include <linux/sched/rt.h>
  12484. #include <linux/sched/deadline.h>
  12485. #include <linux/timer.h>
  12486. +#include <linux/ww_mutex.h>
  12487. #include "rtmutex_common.h"
  12488. @@ -133,6 +139,12 @@
  12489. WRITE_ONCE(*p, owner & ~RT_MUTEX_HAS_WAITERS);
  12490. }
  12491. +static int rt_mutex_real_waiter(struct rt_mutex_waiter *waiter)
  12492. +{
  12493. + return waiter && waiter != PI_WAKEUP_INPROGRESS &&
  12494. + waiter != PI_REQUEUE_INPROGRESS;
  12495. +}
  12496. +
  12497. /*
  12498. * We can speed up the acquire/release, if there's no debugging state to be
  12499. * set up.
  12500. @@ -222,12 +234,25 @@
  12501. }
  12502. #endif
  12503. +#define STEAL_NORMAL 0
  12504. +#define STEAL_LATERAL 1
  12505. +/*
  12506. + * Only use with rt_mutex_waiter_{less,equal}()
  12507. + */
  12508. +#define task_to_waiter(p) \
  12509. + &(struct rt_mutex_waiter){ .prio = (p)->prio, .deadline = (p)->dl.deadline }
  12510. +
  12511. static inline int
  12512. rt_mutex_waiter_less(struct rt_mutex_waiter *left,
  12513. - struct rt_mutex_waiter *right)
  12514. + struct rt_mutex_waiter *right, int mode)
  12515. {
  12516. - if (left->prio < right->prio)
  12517. - return 1;
  12518. + if (mode == STEAL_NORMAL) {
  12519. + if (left->prio < right->prio)
  12520. + return 1;
  12521. + } else {
  12522. + if (left->prio <= right->prio)
  12523. + return 1;
  12524. + }
  12525. /*
  12526. * If both waiters have dl_prio(), we check the deadlines of the
  12527. @@ -236,12 +261,30 @@
  12528. * then right waiter has a dl_prio() too.
  12529. */
  12530. if (dl_prio(left->prio))
  12531. - return dl_time_before(left->task->dl.deadline,
  12532. - right->task->dl.deadline);
  12533. + return dl_time_before(left->deadline, right->deadline);
  12534. return 0;
  12535. }
  12536. +static inline int
  12537. +rt_mutex_waiter_equal(struct rt_mutex_waiter *left,
  12538. + struct rt_mutex_waiter *right)
  12539. +{
  12540. + if (left->prio != right->prio)
  12541. + return 0;
  12542. +
  12543. + /*
  12544. + * If both waiters have dl_prio(), we check the deadlines of the
  12545. + * associated tasks.
  12546. + * If left waiter has a dl_prio(), and we didn't return 0 above,
  12547. + * then right waiter has a dl_prio() too.
  12548. + */
  12549. + if (dl_prio(left->prio))
  12550. + return left->deadline == right->deadline;
  12551. +
  12552. + return 1;
  12553. +}
  12554. +
  12555. static void
  12556. rt_mutex_enqueue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
  12557. {
  12558. @@ -253,7 +296,7 @@
  12559. while (*link) {
  12560. parent = *link;
  12561. entry = rb_entry(parent, struct rt_mutex_waiter, tree_entry);
  12562. - if (rt_mutex_waiter_less(waiter, entry)) {
  12563. + if (rt_mutex_waiter_less(waiter, entry, STEAL_NORMAL)) {
  12564. link = &parent->rb_left;
  12565. } else {
  12566. link = &parent->rb_right;
  12567. @@ -292,7 +335,7 @@
  12568. while (*link) {
  12569. parent = *link;
  12570. entry = rb_entry(parent, struct rt_mutex_waiter, pi_tree_entry);
  12571. - if (rt_mutex_waiter_less(waiter, entry)) {
  12572. + if (rt_mutex_waiter_less(waiter, entry, STEAL_NORMAL)) {
  12573. link = &parent->rb_left;
  12574. } else {
  12575. link = &parent->rb_right;
  12576. @@ -320,72 +363,16 @@
  12577. RB_CLEAR_NODE(&waiter->pi_tree_entry);
  12578. }
  12579. -/*
  12580. - * Calculate task priority from the waiter tree priority
  12581. - *
  12582. - * Return task->normal_prio when the waiter tree is empty or when
  12583. - * the waiter is not allowed to do priority boosting
  12584. - */
  12585. -int rt_mutex_getprio(struct task_struct *task)
  12586. -{
  12587. - if (likely(!task_has_pi_waiters(task)))
  12588. - return task->normal_prio;
  12589. -
  12590. - return min(task_top_pi_waiter(task)->prio,
  12591. - task->normal_prio);
  12592. -}
  12593. -
  12594. -struct task_struct *rt_mutex_get_top_task(struct task_struct *task)
  12595. -{
  12596. - if (likely(!task_has_pi_waiters(task)))
  12597. - return NULL;
  12598. -
  12599. - return task_top_pi_waiter(task)->task;
  12600. -}
  12601. -
  12602. -/*
  12603. - * Called by sched_setscheduler() to get the priority which will be
  12604. - * effective after the change.
  12605. - */
  12606. -int rt_mutex_get_effective_prio(struct task_struct *task, int newprio)
  12607. -{
  12608. - if (!task_has_pi_waiters(task))
  12609. - return newprio;
  12610. -
  12611. - if (task_top_pi_waiter(task)->task->prio <= newprio)
  12612. - return task_top_pi_waiter(task)->task->prio;
  12613. - return newprio;
  12614. -}
  12615. -
  12616. -/*
  12617. - * Adjust the priority of a task, after its pi_waiters got modified.
  12618. - *
  12619. - * This can be both boosting and unboosting. task->pi_lock must be held.
  12620. - */
  12621. -static void __rt_mutex_adjust_prio(struct task_struct *task)
  12622. +static void rt_mutex_adjust_prio(struct task_struct *p)
  12623. {
  12624. - int prio = rt_mutex_getprio(task);
  12625. + struct task_struct *pi_task = NULL;
  12626. - if (task->prio != prio || dl_prio(prio))
  12627. - rt_mutex_setprio(task, prio);
  12628. -}
  12629. + lockdep_assert_held(&p->pi_lock);
  12630. -/*
  12631. - * Adjust task priority (undo boosting). Called from the exit path of
  12632. - * rt_mutex_slowunlock() and rt_mutex_slowlock().
  12633. - *
  12634. - * (Note: We do this outside of the protection of lock->wait_lock to
  12635. - * allow the lock to be taken while or before we readjust the priority
  12636. - * of task. We do not use the spin_xx_mutex() variants here as we are
  12637. - * outside of the debug path.)
  12638. - */
  12639. -void rt_mutex_adjust_prio(struct task_struct *task)
  12640. -{
  12641. - unsigned long flags;
  12642. + if (task_has_pi_waiters(p))
  12643. + pi_task = task_top_pi_waiter(p)->task;
  12644. - raw_spin_lock_irqsave(&task->pi_lock, flags);
  12645. - __rt_mutex_adjust_prio(task);
  12646. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  12647. + rt_mutex_setprio(p, pi_task);
  12648. }
  12649. /*
  12650. @@ -414,6 +401,14 @@
  12651. return debug_rt_mutex_detect_deadlock(waiter, chwalk);
  12652. }
  12653. +static void rt_mutex_wake_waiter(struct rt_mutex_waiter *waiter)
  12654. +{
  12655. + if (waiter->savestate)
  12656. + wake_up_lock_sleeper(waiter->task);
  12657. + else
  12658. + wake_up_process(waiter->task);
  12659. +}
  12660. +
  12661. /*
  12662. * Max number of times we'll walk the boosting chain:
  12663. */
  12664. @@ -421,7 +416,8 @@
  12665. static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
  12666. {
  12667. - return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
  12668. + return rt_mutex_real_waiter(p->pi_blocked_on) ?
  12669. + p->pi_blocked_on->lock : NULL;
  12670. }
  12671. /*
  12672. @@ -557,7 +553,7 @@
  12673. * reached or the state of the chain has changed while we
  12674. * dropped the locks.
  12675. */
  12676. - if (!waiter)
  12677. + if (!rt_mutex_real_waiter(waiter))
  12678. goto out_unlock_pi;
  12679. /*
  12680. @@ -608,7 +604,7 @@
  12681. * enabled we continue, but stop the requeueing in the chain
  12682. * walk.
  12683. */
  12684. - if (waiter->prio == task->prio) {
  12685. + if (rt_mutex_waiter_equal(waiter, task_to_waiter(task))) {
  12686. if (!detect_deadlock)
  12687. goto out_unlock_pi;
  12688. else
  12689. @@ -704,7 +700,26 @@
  12690. /* [7] Requeue the waiter in the lock waiter tree. */
  12691. rt_mutex_dequeue(lock, waiter);
  12692. +
  12693. + /*
  12694. + * Update the waiter prio fields now that we're dequeued.
  12695. + *
  12696. + * These values can have changed through either:
  12697. + *
  12698. + * sys_sched_set_scheduler() / sys_sched_setattr()
  12699. + *
  12700. + * or
  12701. + *
  12702. + * DL CBS enforcement advancing the effective deadline.
  12703. + *
  12704. + * Even though pi_waiters also uses these fields, and that tree is only
  12705. + * updated in [11], we can do this here, since we hold [L], which
  12706. + * serializes all pi_waiters access and rb_erase() does not care about
  12707. + * the values of the node being removed.
  12708. + */
  12709. waiter->prio = task->prio;
  12710. + waiter->deadline = task->dl.deadline;
  12711. +
  12712. rt_mutex_enqueue(lock, waiter);
  12713. /* [8] Release the task */
  12714. @@ -719,13 +734,16 @@
  12715. * follow here. This is the end of the chain we are walking.
  12716. */
  12717. if (!rt_mutex_owner(lock)) {
  12718. + struct rt_mutex_waiter *lock_top_waiter;
  12719. +
  12720. /*
  12721. * If the requeue [7] above changed the top waiter,
  12722. * then we need to wake the new top waiter up to try
  12723. * to get the lock.
  12724. */
  12725. - if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
  12726. - wake_up_process(rt_mutex_top_waiter(lock)->task);
  12727. + lock_top_waiter = rt_mutex_top_waiter(lock);
  12728. + if (prerequeue_top_waiter != lock_top_waiter)
  12729. + rt_mutex_wake_waiter(lock_top_waiter);
  12730. raw_spin_unlock_irq(&lock->wait_lock);
  12731. return 0;
  12732. }
  12733. @@ -745,7 +763,7 @@
  12734. */
  12735. rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
  12736. rt_mutex_enqueue_pi(task, waiter);
  12737. - __rt_mutex_adjust_prio(task);
  12738. + rt_mutex_adjust_prio(task);
  12739. } else if (prerequeue_top_waiter == waiter) {
  12740. /*
  12741. @@ -761,7 +779,7 @@
  12742. rt_mutex_dequeue_pi(task, waiter);
  12743. waiter = rt_mutex_top_waiter(lock);
  12744. rt_mutex_enqueue_pi(task, waiter);
  12745. - __rt_mutex_adjust_prio(task);
  12746. + rt_mutex_adjust_prio(task);
  12747. } else {
  12748. /*
  12749. * Nothing changed. No need to do any priority
  12750. @@ -818,6 +836,7 @@
  12751. return ret;
  12752. }
  12753. +
  12754. /*
  12755. * Try to take an rt-mutex
  12756. *
  12757. @@ -828,9 +847,12 @@
  12758. * @waiter: The waiter that is queued to the lock's wait tree if the
  12759. * callsite called task_blocked_on_lock(), otherwise NULL
  12760. */
  12761. -static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  12762. - struct rt_mutex_waiter *waiter)
  12763. +static int __try_to_take_rt_mutex(struct rt_mutex *lock,
  12764. + struct task_struct *task,
  12765. + struct rt_mutex_waiter *waiter, int mode)
  12766. {
  12767. + lockdep_assert_held(&lock->wait_lock);
  12768. +
  12769. /*
  12770. * Before testing whether we can acquire @lock, we set the
  12771. * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
  12772. @@ -866,8 +888,10 @@
  12773. * If waiter is not the highest priority waiter of
  12774. * @lock, give up.
  12775. */
  12776. - if (waiter != rt_mutex_top_waiter(lock))
  12777. + if (waiter != rt_mutex_top_waiter(lock)) {
  12778. + /* XXX rt_mutex_waiter_less() ? */
  12779. return 0;
  12780. + }
  12781. /*
  12782. * We can acquire the lock. Remove the waiter from the
  12783. @@ -885,14 +909,26 @@
  12784. * not need to be dequeued.
  12785. */
  12786. if (rt_mutex_has_waiters(lock)) {
  12787. + struct task_struct *pown = rt_mutex_top_waiter(lock)->task;
  12788. +
  12789. + if (task != pown)
  12790. + return 0;
  12791. +
  12792. + /*
  12793. + * Note that RT tasks are excluded from lateral-steals
  12794. + * to prevent the introduction of an unbounded latency.
  12795. + */
  12796. + if (rt_task(task))
  12797. + mode = STEAL_NORMAL;
  12798. /*
  12799. * If @task->prio is greater than or equal to
  12800. * the top waiter priority (kernel view),
  12801. * @task lost.
  12802. */
  12803. - if (task->prio >= rt_mutex_top_waiter(lock)->prio)
  12804. + if (!rt_mutex_waiter_less(task_to_waiter(task),
  12805. + rt_mutex_top_waiter(lock),
  12806. + mode))
  12807. return 0;
  12808. -
  12809. /*
  12810. * The current top waiter stays enqueued. We
  12811. * don't have to change anything in the lock
  12812. @@ -936,180 +972,592 @@
  12813. */
  12814. rt_mutex_set_owner(lock, task);
  12815. - rt_mutex_deadlock_account_lock(lock, task);
  12816. -
  12817. return 1;
  12818. }
  12819. +#ifdef CONFIG_PREEMPT_RT_FULL
  12820. /*
  12821. - * Task blocks on lock.
  12822. - *
  12823. - * Prepare waiter and propagate pi chain
  12824. - *
  12825. - * This must be called with lock->wait_lock held and interrupts disabled
  12826. + * preemptible spin_lock functions:
  12827. */
  12828. -static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  12829. - struct rt_mutex_waiter *waiter,
  12830. - struct task_struct *task,
  12831. - enum rtmutex_chainwalk chwalk)
  12832. +static inline void rt_spin_lock_fastlock(struct rt_mutex *lock,
  12833. + void (*slowfn)(struct rt_mutex *lock,
  12834. + bool mg_off),
  12835. + bool do_mig_dis)
  12836. {
  12837. - struct task_struct *owner = rt_mutex_owner(lock);
  12838. - struct rt_mutex_waiter *top_waiter = waiter;
  12839. - struct rt_mutex *next_lock;
  12840. - int chain_walk = 0, res;
  12841. + might_sleep_no_state_check();
  12842. - /*
  12843. - * Early deadlock detection. We really don't want the task to
  12844. - * enqueue on itself just to untangle the mess later. It's not
  12845. - * only an optimization. We drop the locks, so another waiter
  12846. - * can come in before the chain walk detects the deadlock. So
  12847. - * the other will detect the deadlock and return -EDEADLOCK,
  12848. - * which is wrong, as the other waiter is not in a deadlock
  12849. - * situation.
  12850. - */
  12851. - if (owner == task)
  12852. - return -EDEADLK;
  12853. + if (do_mig_dis)
  12854. + migrate_disable();
  12855. - raw_spin_lock(&task->pi_lock);
  12856. - __rt_mutex_adjust_prio(task);
  12857. - waiter->task = task;
  12858. - waiter->lock = lock;
  12859. - waiter->prio = task->prio;
  12860. + if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
  12861. + return;
  12862. + else
  12863. + slowfn(lock, do_mig_dis);
  12864. +}
  12865. - /* Get the top priority waiter on the lock */
  12866. - if (rt_mutex_has_waiters(lock))
  12867. - top_waiter = rt_mutex_top_waiter(lock);
  12868. - rt_mutex_enqueue(lock, waiter);
  12869. +static inline void rt_spin_lock_fastunlock(struct rt_mutex *lock,
  12870. + void (*slowfn)(struct rt_mutex *lock))
  12871. +{
  12872. + if (likely(rt_mutex_cmpxchg_release(lock, current, NULL)))
  12873. + return;
  12874. + else
  12875. + slowfn(lock);
  12876. +}
  12877. +#ifdef CONFIG_SMP
  12878. +/*
  12879. + * Note that owner is a speculative pointer and dereferencing relies
  12880. + * on rcu_read_lock() and the check against the lock owner.
  12881. + */
  12882. +static int adaptive_wait(struct rt_mutex *lock,
  12883. + struct task_struct *owner)
  12884. +{
  12885. + int res = 0;
  12886. - task->pi_blocked_on = waiter;
  12887. + rcu_read_lock();
  12888. + for (;;) {
  12889. + if (owner != rt_mutex_owner(lock))
  12890. + break;
  12891. + /*
  12892. + * Ensure that owner->on_cpu is dereferenced _after_
  12893. + * checking the above to be valid.
  12894. + */
  12895. + barrier();
  12896. + if (!owner->on_cpu) {
  12897. + res = 1;
  12898. + break;
  12899. + }
  12900. + cpu_relax();
  12901. + }
  12902. + rcu_read_unlock();
  12903. + return res;
  12904. +}
  12905. +#else
  12906. +static int adaptive_wait(struct rt_mutex *lock,
  12907. + struct task_struct *orig_owner)
  12908. +{
  12909. + return 1;
  12910. +}
  12911. +#endif
  12912. - raw_spin_unlock(&task->pi_lock);
  12913. +static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  12914. + struct rt_mutex_waiter *waiter,
  12915. + struct task_struct *task,
  12916. + enum rtmutex_chainwalk chwalk);
  12917. +/*
  12918. + * Slow path lock function spin_lock style: this variant is very
  12919. + * careful not to miss any non-lock wakeups.
  12920. + *
  12921. + * We store the current state under p->pi_lock in p->saved_state and
  12922. + * the try_to_wake_up() code handles this accordingly.
  12923. + */
  12924. +static void noinline __sched rt_spin_lock_slowlock(struct rt_mutex *lock,
  12925. + bool mg_off)
  12926. +{
  12927. + struct task_struct *lock_owner, *self = current;
  12928. + struct rt_mutex_waiter waiter, *top_waiter;
  12929. + unsigned long flags;
  12930. + int ret;
  12931. - if (!owner)
  12932. - return 0;
  12933. + rt_mutex_init_waiter(&waiter, true);
  12934. - raw_spin_lock(&owner->pi_lock);
  12935. - if (waiter == rt_mutex_top_waiter(lock)) {
  12936. - rt_mutex_dequeue_pi(owner, top_waiter);
  12937. - rt_mutex_enqueue_pi(owner, waiter);
  12938. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  12939. - __rt_mutex_adjust_prio(owner);
  12940. - if (owner->pi_blocked_on)
  12941. - chain_walk = 1;
  12942. - } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
  12943. - chain_walk = 1;
  12944. + if (__try_to_take_rt_mutex(lock, self, NULL, STEAL_LATERAL)) {
  12945. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  12946. + return;
  12947. }
  12948. - /* Store the lock on which owner is blocked or NULL */
  12949. - next_lock = task_blocked_on_lock(owner);
  12950. + BUG_ON(rt_mutex_owner(lock) == self);
  12951. - raw_spin_unlock(&owner->pi_lock);
  12952. /*
  12953. - * Even if full deadlock detection is on, if the owner is not
  12954. - * blocked itself, we can avoid finding this out in the chain
  12955. - * walk.
  12956. + * We save whatever state the task is in and we'll restore it
  12957. + * after acquiring the lock taking real wakeups into account
  12958. + * as well. We are serialized via pi_lock against wakeups. See
  12959. + * try_to_wake_up().
  12960. */
  12961. - if (!chain_walk || !next_lock)
  12962. - return 0;
  12963. + raw_spin_lock(&self->pi_lock);
  12964. + self->saved_state = self->state;
  12965. + __set_current_state_no_track(TASK_UNINTERRUPTIBLE);
  12966. + raw_spin_unlock(&self->pi_lock);
  12967. - /*
  12968. - * The owner can't disappear while holding a lock,
  12969. - * so the owner struct is protected by wait_lock.
  12970. - * Gets dropped in rt_mutex_adjust_prio_chain()!
  12971. - */
  12972. - get_task_struct(owner);
  12973. + ret = task_blocks_on_rt_mutex(lock, &waiter, self, RT_MUTEX_MIN_CHAINWALK);
  12974. + BUG_ON(ret);
  12975. - raw_spin_unlock_irq(&lock->wait_lock);
  12976. + for (;;) {
  12977. + /* Try to acquire the lock again. */
  12978. + if (__try_to_take_rt_mutex(lock, self, &waiter, STEAL_LATERAL))
  12979. + break;
  12980. - res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
  12981. - next_lock, waiter, task);
  12982. + top_waiter = rt_mutex_top_waiter(lock);
  12983. + lock_owner = rt_mutex_owner(lock);
  12984. - raw_spin_lock_irq(&lock->wait_lock);
  12985. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  12986. - return res;
  12987. -}
  12988. + debug_rt_mutex_print_deadlock(&waiter);
  12989. -/*
  12990. - * Remove the top waiter from the current tasks pi waiter tree and
  12991. - * queue it up.
  12992. - *
  12993. - * Called with lock->wait_lock held and interrupts disabled.
  12994. - */
  12995. -static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
  12996. - struct rt_mutex *lock)
  12997. -{
  12998. - struct rt_mutex_waiter *waiter;
  12999. + if (top_waiter != &waiter || adaptive_wait(lock, lock_owner)) {
  13000. + if (mg_off)
  13001. + migrate_enable();
  13002. + schedule();
  13003. + if (mg_off)
  13004. + migrate_disable();
  13005. + }
  13006. - raw_spin_lock(&current->pi_lock);
  13007. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  13008. - waiter = rt_mutex_top_waiter(lock);
  13009. + raw_spin_lock(&self->pi_lock);
  13010. + __set_current_state_no_track(TASK_UNINTERRUPTIBLE);
  13011. + raw_spin_unlock(&self->pi_lock);
  13012. + }
  13013. /*
  13014. - * Remove it from current->pi_waiters. We do not adjust a
  13015. - * possible priority boost right now. We execute wakeup in the
  13016. - * boosted mode and go back to normal after releasing
  13017. - * lock->wait_lock.
  13018. - */
  13019. - rt_mutex_dequeue_pi(current, waiter);
  13020. + * Restore the task state to current->saved_state. We set it
  13021. + * to the original state above and the try_to_wake_up() code
  13022. + * has possibly updated it when a real (non-rtmutex) wakeup
  13023. + * happened while we were blocked. Clear saved_state so
  13024. + * try_to_wakeup() does not get confused.
  13025. + */
  13026. + raw_spin_lock(&self->pi_lock);
  13027. + __set_current_state_no_track(self->saved_state);
  13028. + self->saved_state = TASK_RUNNING;
  13029. + raw_spin_unlock(&self->pi_lock);
  13030. /*
  13031. - * As we are waking up the top waiter, and the waiter stays
  13032. - * queued on the lock until it gets the lock, this lock
  13033. - * obviously has waiters. Just set the bit here and this has
  13034. - * the added benefit of forcing all new tasks into the
  13035. - * slow path making sure no task of lower priority than
  13036. - * the top waiter can steal this lock.
  13037. + * try_to_take_rt_mutex() sets the waiter bit
  13038. + * unconditionally. We might have to fix that up:
  13039. */
  13040. - lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
  13041. + fixup_rt_mutex_waiters(lock);
  13042. - raw_spin_unlock(&current->pi_lock);
  13043. + BUG_ON(rt_mutex_has_waiters(lock) && &waiter == rt_mutex_top_waiter(lock));
  13044. + BUG_ON(!RB_EMPTY_NODE(&waiter.tree_entry));
  13045. +
  13046. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  13047. - wake_q_add(wake_q, waiter->task);
  13048. + debug_rt_mutex_free_waiter(&waiter);
  13049. }
  13050. +static bool __sched __rt_mutex_unlock_common(struct rt_mutex *lock,
  13051. + struct wake_q_head *wake_q,
  13052. + struct wake_q_head *wq_sleeper);
  13053. /*
  13054. - * Remove a waiter from a lock and give up
  13055. - *
  13056. - * Must be called with lock->wait_lock held and interrupts disabled. I must
  13057. - * have just failed to try_to_take_rt_mutex().
  13058. + * Slow path to release a rt_mutex spin_lock style
  13059. */
  13060. -static void remove_waiter(struct rt_mutex *lock,
  13061. - struct rt_mutex_waiter *waiter)
  13062. +static void noinline __sched rt_spin_lock_slowunlock(struct rt_mutex *lock)
  13063. {
  13064. - bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
  13065. - struct task_struct *owner = rt_mutex_owner(lock);
  13066. - struct rt_mutex *next_lock;
  13067. + unsigned long flags;
  13068. + WAKE_Q(wake_q);
  13069. + WAKE_Q(wake_sleeper_q);
  13070. + bool postunlock;
  13071. - raw_spin_lock(&current->pi_lock);
  13072. - rt_mutex_dequeue(lock, waiter);
  13073. - current->pi_blocked_on = NULL;
  13074. - raw_spin_unlock(&current->pi_lock);
  13075. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  13076. + postunlock = __rt_mutex_unlock_common(lock, &wake_q, &wake_sleeper_q);
  13077. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  13078. - /*
  13079. - * Only update priority if the waiter was the highest priority
  13080. - * waiter of the lock and there is an owner to update.
  13081. - */
  13082. - if (!owner || !is_top_waiter)
  13083. - return;
  13084. + if (postunlock)
  13085. + rt_mutex_postunlock(&wake_q, &wake_sleeper_q);
  13086. +}
  13087. - raw_spin_lock(&owner->pi_lock);
  13088. +void __lockfunc rt_spin_lock__no_mg(spinlock_t *lock)
  13089. +{
  13090. + rt_spin_lock_fastlock(&lock->lock, rt_spin_lock_slowlock, false);
  13091. + spin_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  13092. +}
  13093. +EXPORT_SYMBOL(rt_spin_lock__no_mg);
  13094. - rt_mutex_dequeue_pi(owner, waiter);
  13095. +void __lockfunc rt_spin_lock(spinlock_t *lock)
  13096. +{
  13097. + rt_spin_lock_fastlock(&lock->lock, rt_spin_lock_slowlock, true);
  13098. + spin_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  13099. +}
  13100. +EXPORT_SYMBOL(rt_spin_lock);
  13101. - if (rt_mutex_has_waiters(lock))
  13102. - rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
  13103. +void __lockfunc __rt_spin_lock(struct rt_mutex *lock)
  13104. +{
  13105. + rt_spin_lock_fastlock(lock, rt_spin_lock_slowlock, true);
  13106. +}
  13107. +EXPORT_SYMBOL(__rt_spin_lock);
  13108. - __rt_mutex_adjust_prio(owner);
  13109. +void __lockfunc __rt_spin_lock__no_mg(struct rt_mutex *lock)
  13110. +{
  13111. + rt_spin_lock_fastlock(lock, rt_spin_lock_slowlock, false);
  13112. +}
  13113. +EXPORT_SYMBOL(__rt_spin_lock__no_mg);
  13114. - /* Store the lock on which owner is blocked or NULL */
  13115. - next_lock = task_blocked_on_lock(owner);
  13116. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  13117. +void __lockfunc rt_spin_lock_nested(spinlock_t *lock, int subclass)
  13118. +{
  13119. + spin_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
  13120. + rt_spin_lock_fastlock(&lock->lock, rt_spin_lock_slowlock, true);
  13121. +}
  13122. +EXPORT_SYMBOL(rt_spin_lock_nested);
  13123. +#endif
  13124. - raw_spin_unlock(&owner->pi_lock);
  13125. +void __lockfunc rt_spin_unlock__no_mg(spinlock_t *lock)
  13126. +{
  13127. + /* NOTE: we always pass in '1' for nested, for simplicity */
  13128. + spin_release(&lock->dep_map, 1, _RET_IP_);
  13129. + rt_spin_lock_fastunlock(&lock->lock, rt_spin_lock_slowunlock);
  13130. +}
  13131. +EXPORT_SYMBOL(rt_spin_unlock__no_mg);
  13132. - /*
  13133. - * Don't walk the chain, if the owner task is not blocked
  13134. - * itself.
  13135. - */
  13136. +void __lockfunc rt_spin_unlock(spinlock_t *lock)
  13137. +{
  13138. + /* NOTE: we always pass in '1' for nested, for simplicity */
  13139. + spin_release(&lock->dep_map, 1, _RET_IP_);
  13140. + rt_spin_lock_fastunlock(&lock->lock, rt_spin_lock_slowunlock);
  13141. + migrate_enable();
  13142. +}
  13143. +EXPORT_SYMBOL(rt_spin_unlock);
  13144. +
  13145. +void __lockfunc __rt_spin_unlock(struct rt_mutex *lock)
  13146. +{
  13147. + rt_spin_lock_fastunlock(lock, rt_spin_lock_slowunlock);
  13148. +}
  13149. +EXPORT_SYMBOL(__rt_spin_unlock);
  13150. +
  13151. +/*
  13152. + * Wait for the lock to get unlocked: instead of polling for an unlock
  13153. + * (like raw spinlocks do), we lock and unlock, to force the kernel to
  13154. + * schedule if there's contention:
  13155. + */
  13156. +void __lockfunc rt_spin_unlock_wait(spinlock_t *lock)
  13157. +{
  13158. + spin_lock(lock);
  13159. + spin_unlock(lock);
  13160. +}
  13161. +EXPORT_SYMBOL(rt_spin_unlock_wait);
  13162. +
  13163. +int __lockfunc rt_spin_trylock__no_mg(spinlock_t *lock)
  13164. +{
  13165. + int ret;
  13166. +
  13167. + ret = rt_mutex_trylock(&lock->lock);
  13168. + if (ret)
  13169. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  13170. + return ret;
  13171. +}
  13172. +EXPORT_SYMBOL(rt_spin_trylock__no_mg);
  13173. +
  13174. +int __lockfunc rt_spin_trylock(spinlock_t *lock)
  13175. +{
  13176. + int ret;
  13177. +
  13178. + migrate_disable();
  13179. + ret = rt_mutex_trylock(&lock->lock);
  13180. + if (ret)
  13181. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  13182. + else
  13183. + migrate_enable();
  13184. + return ret;
  13185. +}
  13186. +EXPORT_SYMBOL(rt_spin_trylock);
  13187. +
  13188. +int __lockfunc rt_spin_trylock_bh(spinlock_t *lock)
  13189. +{
  13190. + int ret;
  13191. +
  13192. + local_bh_disable();
  13193. + ret = rt_mutex_trylock(&lock->lock);
  13194. + if (ret) {
  13195. + migrate_disable();
  13196. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  13197. + } else
  13198. + local_bh_enable();
  13199. + return ret;
  13200. +}
  13201. +EXPORT_SYMBOL(rt_spin_trylock_bh);
  13202. +
  13203. +int __lockfunc rt_spin_trylock_irqsave(spinlock_t *lock, unsigned long *flags)
  13204. +{
  13205. + int ret;
  13206. +
  13207. + *flags = 0;
  13208. + ret = rt_mutex_trylock(&lock->lock);
  13209. + if (ret) {
  13210. + migrate_disable();
  13211. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  13212. + }
  13213. + return ret;
  13214. +}
  13215. +EXPORT_SYMBOL(rt_spin_trylock_irqsave);
  13216. +
  13217. +int atomic_dec_and_spin_lock(atomic_t *atomic, spinlock_t *lock)
  13218. +{
  13219. + /* Subtract 1 from counter unless that drops it to 0 (ie. it was 1) */
  13220. + if (atomic_add_unless(atomic, -1, 1))
  13221. + return 0;
  13222. + rt_spin_lock(lock);
  13223. + if (atomic_dec_and_test(atomic))
  13224. + return 1;
  13225. + rt_spin_unlock(lock);
  13226. + return 0;
  13227. +}
  13228. +EXPORT_SYMBOL(atomic_dec_and_spin_lock);
  13229. +
  13230. + void
  13231. +__rt_spin_lock_init(spinlock_t *lock, char *name, struct lock_class_key *key)
  13232. +{
  13233. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  13234. + /*
  13235. + * Make sure we are not reinitializing a held lock:
  13236. + */
  13237. + debug_check_no_locks_freed((void *)lock, sizeof(*lock));
  13238. + lockdep_init_map(&lock->dep_map, name, key, 0);
  13239. +#endif
  13240. +}
  13241. +EXPORT_SYMBOL(__rt_spin_lock_init);
  13242. +
  13243. +#endif /* PREEMPT_RT_FULL */
  13244. +
  13245. +#ifdef CONFIG_PREEMPT_RT_FULL
  13246. + static inline int __sched
  13247. +__mutex_lock_check_stamp(struct rt_mutex *lock, struct ww_acquire_ctx *ctx)
  13248. +{
  13249. + struct ww_mutex *ww = container_of(lock, struct ww_mutex, base.lock);
  13250. + struct ww_acquire_ctx *hold_ctx = ACCESS_ONCE(ww->ctx);
  13251. +
  13252. + if (!hold_ctx)
  13253. + return 0;
  13254. +
  13255. + if (unlikely(ctx == hold_ctx))
  13256. + return -EALREADY;
  13257. +
  13258. + if (ctx->stamp - hold_ctx->stamp <= LONG_MAX &&
  13259. + (ctx->stamp != hold_ctx->stamp || ctx > hold_ctx)) {
  13260. +#ifdef CONFIG_DEBUG_MUTEXES
  13261. + DEBUG_LOCKS_WARN_ON(ctx->contending_lock);
  13262. + ctx->contending_lock = ww;
  13263. +#endif
  13264. + return -EDEADLK;
  13265. + }
  13266. +
  13267. + return 0;
  13268. +}
  13269. +#else
  13270. + static inline int __sched
  13271. +__mutex_lock_check_stamp(struct rt_mutex *lock, struct ww_acquire_ctx *ctx)
  13272. +{
  13273. + BUG();
  13274. + return 0;
  13275. +}
  13276. +
  13277. +#endif
  13278. +
  13279. +static inline int
  13280. +try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  13281. + struct rt_mutex_waiter *waiter)
  13282. +{
  13283. + return __try_to_take_rt_mutex(lock, task, waiter, STEAL_NORMAL);
  13284. +}
  13285. +
  13286. +/*
  13287. + * Task blocks on lock.
  13288. + *
  13289. + * Prepare waiter and propagate pi chain
  13290. + *
  13291. + * This must be called with lock->wait_lock held and interrupts disabled
  13292. + */
  13293. +static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  13294. + struct rt_mutex_waiter *waiter,
  13295. + struct task_struct *task,
  13296. + enum rtmutex_chainwalk chwalk)
  13297. +{
  13298. + struct task_struct *owner = rt_mutex_owner(lock);
  13299. + struct rt_mutex_waiter *top_waiter = waiter;
  13300. + struct rt_mutex *next_lock;
  13301. + int chain_walk = 0, res;
  13302. +
  13303. + lockdep_assert_held(&lock->wait_lock);
  13304. +
  13305. + /*
  13306. + * Early deadlock detection. We really don't want the task to
  13307. + * enqueue on itself just to untangle the mess later. It's not
  13308. + * only an optimization. We drop the locks, so another waiter
  13309. + * can come in before the chain walk detects the deadlock. So
  13310. + * the other will detect the deadlock and return -EDEADLOCK,
  13311. + * which is wrong, as the other waiter is not in a deadlock
  13312. + * situation.
  13313. + */
  13314. + if (owner == task)
  13315. + return -EDEADLK;
  13316. +
  13317. + raw_spin_lock(&task->pi_lock);
  13318. +
  13319. + /*
  13320. + * In the case of futex requeue PI, this will be a proxy
  13321. + * lock. The task will wake unaware that it is enqueueed on
  13322. + * this lock. Avoid blocking on two locks and corrupting
  13323. + * pi_blocked_on via the PI_WAKEUP_INPROGRESS
  13324. + * flag. futex_wait_requeue_pi() sets this when it wakes up
  13325. + * before requeue (due to a signal or timeout). Do not enqueue
  13326. + * the task if PI_WAKEUP_INPROGRESS is set.
  13327. + */
  13328. + if (task != current && task->pi_blocked_on == PI_WAKEUP_INPROGRESS) {
  13329. + raw_spin_unlock(&task->pi_lock);
  13330. + return -EAGAIN;
  13331. + }
  13332. +
  13333. + BUG_ON(rt_mutex_real_waiter(task->pi_blocked_on));
  13334. +
  13335. + rt_mutex_adjust_prio(task);
  13336. + waiter->task = task;
  13337. + waiter->lock = lock;
  13338. + waiter->prio = task->prio;
  13339. + waiter->deadline = task->dl.deadline;
  13340. +
  13341. + /* Get the top priority waiter on the lock */
  13342. + if (rt_mutex_has_waiters(lock))
  13343. + top_waiter = rt_mutex_top_waiter(lock);
  13344. + rt_mutex_enqueue(lock, waiter);
  13345. +
  13346. + task->pi_blocked_on = waiter;
  13347. +
  13348. + raw_spin_unlock(&task->pi_lock);
  13349. +
  13350. + if (!owner)
  13351. + return 0;
  13352. +
  13353. + raw_spin_lock(&owner->pi_lock);
  13354. + if (waiter == rt_mutex_top_waiter(lock)) {
  13355. + rt_mutex_dequeue_pi(owner, top_waiter);
  13356. + rt_mutex_enqueue_pi(owner, waiter);
  13357. +
  13358. + rt_mutex_adjust_prio(owner);
  13359. + if (rt_mutex_real_waiter(owner->pi_blocked_on))
  13360. + chain_walk = 1;
  13361. + } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
  13362. + chain_walk = 1;
  13363. + }
  13364. +
  13365. + /* Store the lock on which owner is blocked or NULL */
  13366. + next_lock = task_blocked_on_lock(owner);
  13367. +
  13368. + raw_spin_unlock(&owner->pi_lock);
  13369. + /*
  13370. + * Even if full deadlock detection is on, if the owner is not
  13371. + * blocked itself, we can avoid finding this out in the chain
  13372. + * walk.
  13373. + */
  13374. + if (!chain_walk || !next_lock)
  13375. + return 0;
  13376. +
  13377. + /*
  13378. + * The owner can't disappear while holding a lock,
  13379. + * so the owner struct is protected by wait_lock.
  13380. + * Gets dropped in rt_mutex_adjust_prio_chain()!
  13381. + */
  13382. + get_task_struct(owner);
  13383. +
  13384. + raw_spin_unlock_irq(&lock->wait_lock);
  13385. +
  13386. + res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
  13387. + next_lock, waiter, task);
  13388. +
  13389. + raw_spin_lock_irq(&lock->wait_lock);
  13390. +
  13391. + return res;
  13392. +}
  13393. +
  13394. +/*
  13395. + * Remove the top waiter from the current tasks pi waiter tree and
  13396. + * queue it up.
  13397. + *
  13398. + * Called with lock->wait_lock held and interrupts disabled.
  13399. + */
  13400. +static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
  13401. + struct wake_q_head *wake_sleeper_q,
  13402. + struct rt_mutex *lock)
  13403. +{
  13404. + struct rt_mutex_waiter *waiter;
  13405. +
  13406. + raw_spin_lock(&current->pi_lock);
  13407. +
  13408. + waiter = rt_mutex_top_waiter(lock);
  13409. +
  13410. + /*
  13411. + * Remove it from current->pi_waiters and deboost.
  13412. + *
  13413. + * We must in fact deboost here in order to ensure we call
  13414. + * rt_mutex_setprio() to update p->pi_top_task before the
  13415. + * task unblocks.
  13416. + */
  13417. + rt_mutex_dequeue_pi(current, waiter);
  13418. + rt_mutex_adjust_prio(current);
  13419. +
  13420. + /*
  13421. + * As we are waking up the top waiter, and the waiter stays
  13422. + * queued on the lock until it gets the lock, this lock
  13423. + * obviously has waiters. Just set the bit here and this has
  13424. + * the added benefit of forcing all new tasks into the
  13425. + * slow path making sure no task of lower priority than
  13426. + * the top waiter can steal this lock.
  13427. + */
  13428. + lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
  13429. +
  13430. + /*
  13431. + * We deboosted before waking the top waiter task such that we don't
  13432. + * run two tasks with the 'same' priority (and ensure the
  13433. + * p->pi_top_task pointer points to a blocked task). This however can
  13434. + * lead to priority inversion if we would get preempted after the
  13435. + * deboost but before waking our donor task, hence the preempt_disable()
  13436. + * before unlock.
  13437. + *
  13438. + * Pairs with preempt_enable() in rt_mutex_postunlock();
  13439. + */
  13440. + preempt_disable();
  13441. + if (waiter->savestate)
  13442. + wake_q_add(wake_sleeper_q, waiter->task);
  13443. + else
  13444. + wake_q_add(wake_q, waiter->task);
  13445. + raw_spin_unlock(&current->pi_lock);
  13446. +}
  13447. +
  13448. +/*
  13449. + * Remove a waiter from a lock and give up
  13450. + *
  13451. + * Must be called with lock->wait_lock held and interrupts disabled. I must
  13452. + * have just failed to try_to_take_rt_mutex().
  13453. + */
  13454. +static void remove_waiter(struct rt_mutex *lock,
  13455. + struct rt_mutex_waiter *waiter)
  13456. +{
  13457. + bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
  13458. + struct task_struct *owner = rt_mutex_owner(lock);
  13459. + struct rt_mutex *next_lock = NULL;
  13460. +
  13461. + lockdep_assert_held(&lock->wait_lock);
  13462. +
  13463. + raw_spin_lock(&current->pi_lock);
  13464. + rt_mutex_dequeue(lock, waiter);
  13465. + current->pi_blocked_on = NULL;
  13466. + raw_spin_unlock(&current->pi_lock);
  13467. +
  13468. + /*
  13469. + * Only update priority if the waiter was the highest priority
  13470. + * waiter of the lock and there is an owner to update.
  13471. + */
  13472. + if (!owner || !is_top_waiter)
  13473. + return;
  13474. +
  13475. + raw_spin_lock(&owner->pi_lock);
  13476. +
  13477. + rt_mutex_dequeue_pi(owner, waiter);
  13478. +
  13479. + if (rt_mutex_has_waiters(lock))
  13480. + rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
  13481. +
  13482. + rt_mutex_adjust_prio(owner);
  13483. +
  13484. + /* Store the lock on which owner is blocked or NULL */
  13485. + if (rt_mutex_real_waiter(owner->pi_blocked_on))
  13486. + next_lock = task_blocked_on_lock(owner);
  13487. +
  13488. + raw_spin_unlock(&owner->pi_lock);
  13489. +
  13490. + /*
  13491. + * Don't walk the chain, if the owner task is not blocked
  13492. + * itself.
  13493. + */
  13494. if (!next_lock)
  13495. return;
  13496. @@ -1138,21 +1586,30 @@
  13497. raw_spin_lock_irqsave(&task->pi_lock, flags);
  13498. waiter = task->pi_blocked_on;
  13499. - if (!waiter || (waiter->prio == task->prio &&
  13500. - !dl_prio(task->prio))) {
  13501. + if (!rt_mutex_real_waiter(waiter) ||
  13502. + rt_mutex_waiter_equal(waiter, task_to_waiter(task))) {
  13503. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  13504. return;
  13505. }
  13506. next_lock = waiter->lock;
  13507. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  13508. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  13509. get_task_struct(task);
  13510. + raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  13511. rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
  13512. next_lock, NULL, task);
  13513. }
  13514. +void rt_mutex_init_waiter(struct rt_mutex_waiter *waiter, bool savestate)
  13515. +{
  13516. + debug_rt_mutex_init_waiter(waiter);
  13517. + RB_CLEAR_NODE(&waiter->pi_tree_entry);
  13518. + RB_CLEAR_NODE(&waiter->tree_entry);
  13519. + waiter->task = NULL;
  13520. + waiter->savestate = savestate;
  13521. +}
  13522. +
  13523. /**
  13524. * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
  13525. * @lock: the rt_mutex to take
  13526. @@ -1166,7 +1623,8 @@
  13527. static int __sched
  13528. __rt_mutex_slowlock(struct rt_mutex *lock, int state,
  13529. struct hrtimer_sleeper *timeout,
  13530. - struct rt_mutex_waiter *waiter)
  13531. + struct rt_mutex_waiter *waiter,
  13532. + struct ww_acquire_ctx *ww_ctx)
  13533. {
  13534. int ret = 0;
  13535. @@ -1175,16 +1633,17 @@
  13536. if (try_to_take_rt_mutex(lock, current, waiter))
  13537. break;
  13538. - /*
  13539. - * TASK_INTERRUPTIBLE checks for signals and
  13540. - * timeout. Ignored otherwise.
  13541. - */
  13542. - if (unlikely(state == TASK_INTERRUPTIBLE)) {
  13543. - /* Signal pending? */
  13544. - if (signal_pending(current))
  13545. - ret = -EINTR;
  13546. - if (timeout && !timeout->task)
  13547. - ret = -ETIMEDOUT;
  13548. + if (timeout && !timeout->task) {
  13549. + ret = -ETIMEDOUT;
  13550. + break;
  13551. + }
  13552. + if (signal_pending_state(state, current)) {
  13553. + ret = -EINTR;
  13554. + break;
  13555. + }
  13556. +
  13557. + if (ww_ctx && ww_ctx->acquired > 0) {
  13558. + ret = __mutex_lock_check_stamp(lock, ww_ctx);
  13559. if (ret)
  13560. break;
  13561. }
  13562. @@ -1199,59 +1658,118 @@
  13563. set_current_state(state);
  13564. }
  13565. - __set_current_state(TASK_RUNNING);
  13566. - return ret;
  13567. + __set_current_state(TASK_RUNNING);
  13568. + return ret;
  13569. +}
  13570. +
  13571. +static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
  13572. + struct rt_mutex_waiter *w)
  13573. +{
  13574. + /*
  13575. + * If the result is not -EDEADLOCK or the caller requested
  13576. + * deadlock detection, nothing to do here.
  13577. + */
  13578. + if (res != -EDEADLOCK || detect_deadlock)
  13579. + return;
  13580. +
  13581. + /*
  13582. + * Yell lowdly and stop the task right here.
  13583. + */
  13584. + rt_mutex_print_deadlock(w);
  13585. + while (1) {
  13586. + set_current_state(TASK_INTERRUPTIBLE);
  13587. + schedule();
  13588. + }
  13589. +}
  13590. +
  13591. +static __always_inline void ww_mutex_lock_acquired(struct ww_mutex *ww,
  13592. + struct ww_acquire_ctx *ww_ctx)
  13593. +{
  13594. +#ifdef CONFIG_DEBUG_MUTEXES
  13595. + /*
  13596. + * If this WARN_ON triggers, you used ww_mutex_lock to acquire,
  13597. + * but released with a normal mutex_unlock in this call.
  13598. + *
  13599. + * This should never happen, always use ww_mutex_unlock.
  13600. + */
  13601. + DEBUG_LOCKS_WARN_ON(ww->ctx);
  13602. +
  13603. + /*
  13604. + * Not quite done after calling ww_acquire_done() ?
  13605. + */
  13606. + DEBUG_LOCKS_WARN_ON(ww_ctx->done_acquire);
  13607. +
  13608. + if (ww_ctx->contending_lock) {
  13609. + /*
  13610. + * After -EDEADLK you tried to
  13611. + * acquire a different ww_mutex? Bad!
  13612. + */
  13613. + DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock != ww);
  13614. +
  13615. + /*
  13616. + * You called ww_mutex_lock after receiving -EDEADLK,
  13617. + * but 'forgot' to unlock everything else first?
  13618. + */
  13619. + DEBUG_LOCKS_WARN_ON(ww_ctx->acquired > 0);
  13620. + ww_ctx->contending_lock = NULL;
  13621. + }
  13622. +
  13623. + /*
  13624. + * Naughty, using a different class will lead to undefined behavior!
  13625. + */
  13626. + DEBUG_LOCKS_WARN_ON(ww_ctx->ww_class != ww->ww_class);
  13627. +#endif
  13628. + ww_ctx->acquired++;
  13629. }
  13630. -static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
  13631. - struct rt_mutex_waiter *w)
  13632. +#ifdef CONFIG_PREEMPT_RT_FULL
  13633. +static void ww_mutex_account_lock(struct rt_mutex *lock,
  13634. + struct ww_acquire_ctx *ww_ctx)
  13635. {
  13636. + struct ww_mutex *ww = container_of(lock, struct ww_mutex, base.lock);
  13637. + struct rt_mutex_waiter *waiter, *n;
  13638. +
  13639. /*
  13640. - * If the result is not -EDEADLOCK or the caller requested
  13641. - * deadlock detection, nothing to do here.
  13642. + * This branch gets optimized out for the common case,
  13643. + * and is only important for ww_mutex_lock.
  13644. */
  13645. - if (res != -EDEADLOCK || detect_deadlock)
  13646. - return;
  13647. + ww_mutex_lock_acquired(ww, ww_ctx);
  13648. + ww->ctx = ww_ctx;
  13649. /*
  13650. - * Yell lowdly and stop the task right here.
  13651. + * Give any possible sleeping processes the chance to wake up,
  13652. + * so they can recheck if they have to back off.
  13653. */
  13654. - rt_mutex_print_deadlock(w);
  13655. - while (1) {
  13656. - set_current_state(TASK_INTERRUPTIBLE);
  13657. - schedule();
  13658. + rbtree_postorder_for_each_entry_safe(waiter, n, &lock->waiters,
  13659. + tree_entry) {
  13660. + /* XXX debug rt mutex waiter wakeup */
  13661. +
  13662. + BUG_ON(waiter->lock != lock);
  13663. + rt_mutex_wake_waiter(waiter);
  13664. }
  13665. }
  13666. -/*
  13667. - * Slow path lock function:
  13668. - */
  13669. -static int __sched
  13670. -rt_mutex_slowlock(struct rt_mutex *lock, int state,
  13671. - struct hrtimer_sleeper *timeout,
  13672. - enum rtmutex_chainwalk chwalk)
  13673. -{
  13674. - struct rt_mutex_waiter waiter;
  13675. - unsigned long flags;
  13676. - int ret = 0;
  13677. +#else
  13678. - debug_rt_mutex_init_waiter(&waiter);
  13679. - RB_CLEAR_NODE(&waiter.pi_tree_entry);
  13680. - RB_CLEAR_NODE(&waiter.tree_entry);
  13681. +static void ww_mutex_account_lock(struct rt_mutex *lock,
  13682. + struct ww_acquire_ctx *ww_ctx)
  13683. +{
  13684. + BUG();
  13685. +}
  13686. +#endif
  13687. - /*
  13688. - * Technically we could use raw_spin_[un]lock_irq() here, but this can
  13689. - * be called in early boot if the cmpxchg() fast path is disabled
  13690. - * (debug, no architecture support). In this case we will acquire the
  13691. - * rtmutex with lock->wait_lock held. But we cannot unconditionally
  13692. - * enable interrupts in that early boot case. So we need to use the
  13693. - * irqsave/restore variants.
  13694. - */
  13695. - raw_spin_lock_irqsave(&lock->wait_lock, flags);
  13696. +int __sched rt_mutex_slowlock_locked(struct rt_mutex *lock, int state,
  13697. + struct hrtimer_sleeper *timeout,
  13698. + enum rtmutex_chainwalk chwalk,
  13699. + struct ww_acquire_ctx *ww_ctx,
  13700. + struct rt_mutex_waiter *waiter)
  13701. +{
  13702. + int ret;
  13703. /* Try to acquire the lock again: */
  13704. if (try_to_take_rt_mutex(lock, current, NULL)) {
  13705. - raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  13706. + if (ww_ctx)
  13707. + ww_mutex_account_lock(lock, ww_ctx);
  13708. return 0;
  13709. }
  13710. @@ -1261,17 +1779,27 @@
  13711. if (unlikely(timeout))
  13712. hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
  13713. - ret = task_blocks_on_rt_mutex(lock, &waiter, current, chwalk);
  13714. + ret = task_blocks_on_rt_mutex(lock, waiter, current, chwalk);
  13715. - if (likely(!ret))
  13716. + if (likely(!ret)) {
  13717. /* sleep on the mutex */
  13718. - ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
  13719. + ret = __rt_mutex_slowlock(lock, state, timeout, waiter,
  13720. + ww_ctx);
  13721. + } else if (ww_ctx) {
  13722. + /* ww_mutex received EDEADLK, let it become EALREADY */
  13723. + ret = __mutex_lock_check_stamp(lock, ww_ctx);
  13724. + BUG_ON(!ret);
  13725. + }
  13726. if (unlikely(ret)) {
  13727. __set_current_state(TASK_RUNNING);
  13728. if (rt_mutex_has_waiters(lock))
  13729. - remove_waiter(lock, &waiter);
  13730. - rt_mutex_handle_deadlock(ret, chwalk, &waiter);
  13731. + remove_waiter(lock, waiter);
  13732. + /* ww_mutex want to report EDEADLK/EALREADY, let them */
  13733. + if (!ww_ctx)
  13734. + rt_mutex_handle_deadlock(ret, chwalk, waiter);
  13735. + } else if (ww_ctx) {
  13736. + ww_mutex_account_lock(lock, ww_ctx);
  13737. }
  13738. /*
  13739. @@ -1279,6 +1807,36 @@
  13740. * unconditionally. We might have to fix that up.
  13741. */
  13742. fixup_rt_mutex_waiters(lock);
  13743. + return ret;
  13744. +}
  13745. +
  13746. +/*
  13747. + * Slow path lock function:
  13748. + */
  13749. +static int __sched
  13750. +rt_mutex_slowlock(struct rt_mutex *lock, int state,
  13751. + struct hrtimer_sleeper *timeout,
  13752. + enum rtmutex_chainwalk chwalk,
  13753. + struct ww_acquire_ctx *ww_ctx)
  13754. +{
  13755. + struct rt_mutex_waiter waiter;
  13756. + unsigned long flags;
  13757. + int ret = 0;
  13758. +
  13759. + rt_mutex_init_waiter(&waiter, false);
  13760. +
  13761. + /*
  13762. + * Technically we could use raw_spin_[un]lock_irq() here, but this can
  13763. + * be called in early boot if the cmpxchg() fast path is disabled
  13764. + * (debug, no architecture support). In this case we will acquire the
  13765. + * rtmutex with lock->wait_lock held. But we cannot unconditionally
  13766. + * enable interrupts in that early boot case. So we need to use the
  13767. + * irqsave/restore variants.
  13768. + */
  13769. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  13770. +
  13771. + ret = rt_mutex_slowlock_locked(lock, state, timeout, chwalk, ww_ctx,
  13772. + &waiter);
  13773. raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  13774. @@ -1328,10 +1886,12 @@
  13775. /*
  13776. * Slow path to release a rt-mutex.
  13777. - * Return whether the current task needs to undo a potential priority boosting.
  13778. + *
  13779. + * Return whether the current task needs to call rt_mutex_postunlock().
  13780. */
  13781. static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
  13782. - struct wake_q_head *wake_q)
  13783. + struct wake_q_head *wake_q,
  13784. + struct wake_q_head *wake_sleeper_q)
  13785. {
  13786. unsigned long flags;
  13787. @@ -1340,8 +1900,6 @@
  13788. debug_rt_mutex_unlock(lock);
  13789. - rt_mutex_deadlock_account_unlock(current);
  13790. -
  13791. /*
  13792. * We must be careful here if the fast path is enabled. If we
  13793. * have no waiters queued we cannot set owner to NULL here
  13794. @@ -1387,12 +1945,10 @@
  13795. *
  13796. * Queue the next waiter for wakeup once we release the wait_lock.
  13797. */
  13798. - mark_wakeup_next_waiter(wake_q, lock);
  13799. -
  13800. + mark_wakeup_next_waiter(wake_q, wake_sleeper_q, lock);
  13801. raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  13802. - /* check PI boosting */
  13803. - return true;
  13804. + return true; /* call rt_mutex_postunlock() */
  13805. }
  13806. /*
  13807. @@ -1403,63 +1959,85 @@
  13808. */
  13809. static inline int
  13810. rt_mutex_fastlock(struct rt_mutex *lock, int state,
  13811. + struct ww_acquire_ctx *ww_ctx,
  13812. int (*slowfn)(struct rt_mutex *lock, int state,
  13813. struct hrtimer_sleeper *timeout,
  13814. - enum rtmutex_chainwalk chwalk))
  13815. + enum rtmutex_chainwalk chwalk,
  13816. + struct ww_acquire_ctx *ww_ctx))
  13817. {
  13818. - if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
  13819. - rt_mutex_deadlock_account_lock(lock, current);
  13820. + if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
  13821. return 0;
  13822. - } else
  13823. - return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
  13824. +
  13825. + return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK, ww_ctx);
  13826. }
  13827. static inline int
  13828. rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
  13829. struct hrtimer_sleeper *timeout,
  13830. enum rtmutex_chainwalk chwalk,
  13831. + struct ww_acquire_ctx *ww_ctx,
  13832. int (*slowfn)(struct rt_mutex *lock, int state,
  13833. struct hrtimer_sleeper *timeout,
  13834. - enum rtmutex_chainwalk chwalk))
  13835. + enum rtmutex_chainwalk chwalk,
  13836. + struct ww_acquire_ctx *ww_ctx))
  13837. {
  13838. if (chwalk == RT_MUTEX_MIN_CHAINWALK &&
  13839. - likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
  13840. - rt_mutex_deadlock_account_lock(lock, current);
  13841. + likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
  13842. return 0;
  13843. - } else
  13844. - return slowfn(lock, state, timeout, chwalk);
  13845. +
  13846. + return slowfn(lock, state, timeout, chwalk, ww_ctx);
  13847. }
  13848. static inline int
  13849. rt_mutex_fasttrylock(struct rt_mutex *lock,
  13850. int (*slowfn)(struct rt_mutex *lock))
  13851. {
  13852. - if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
  13853. - rt_mutex_deadlock_account_lock(lock, current);
  13854. + if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
  13855. return 1;
  13856. - }
  13857. +
  13858. return slowfn(lock);
  13859. }
  13860. +/*
  13861. + * Performs the wakeup of the the top-waiter and re-enables preemption.
  13862. + */
  13863. +void rt_mutex_postunlock(struct wake_q_head *wake_q,
  13864. + struct wake_q_head *wq_sleeper)
  13865. +{
  13866. + wake_up_q(wake_q);
  13867. + wake_up_q_sleeper(wq_sleeper);
  13868. +
  13869. + /* Pairs with preempt_disable() in rt_mutex_slowunlock() */
  13870. + preempt_enable();
  13871. +}
  13872. +
  13873. static inline void
  13874. rt_mutex_fastunlock(struct rt_mutex *lock,
  13875. bool (*slowfn)(struct rt_mutex *lock,
  13876. - struct wake_q_head *wqh))
  13877. + struct wake_q_head *wqh,
  13878. + struct wake_q_head *wq_sleeper))
  13879. {
  13880. WAKE_Q(wake_q);
  13881. + WAKE_Q(wake_sleeper_q);
  13882. - if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
  13883. - rt_mutex_deadlock_account_unlock(current);
  13884. + if (likely(rt_mutex_cmpxchg_release(lock, current, NULL)))
  13885. + return;
  13886. - } else {
  13887. - bool deboost = slowfn(lock, &wake_q);
  13888. + if (slowfn(lock, &wake_q, &wake_sleeper_q))
  13889. + rt_mutex_postunlock(&wake_q, &wake_sleeper_q);
  13890. +}
  13891. - wake_up_q(&wake_q);
  13892. +/**
  13893. + * rt_mutex_lock_state - lock a rt_mutex with a given state
  13894. + *
  13895. + * @lock: The rt_mutex to be locked
  13896. + * @state: The state to set when blocking on the rt_mutex
  13897. + */
  13898. +int __sched rt_mutex_lock_state(struct rt_mutex *lock, int state)
  13899. +{
  13900. + might_sleep();
  13901. - /* Undo pi boosting if necessary: */
  13902. - if (deboost)
  13903. - rt_mutex_adjust_prio(current);
  13904. - }
  13905. + return rt_mutex_fastlock(lock, state, NULL, rt_mutex_slowlock);
  13906. }
  13907. /**
  13908. @@ -1469,15 +2047,13 @@
  13909. */
  13910. void __sched rt_mutex_lock(struct rt_mutex *lock)
  13911. {
  13912. - might_sleep();
  13913. -
  13914. - rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
  13915. + rt_mutex_lock_state(lock, TASK_UNINTERRUPTIBLE);
  13916. }
  13917. EXPORT_SYMBOL_GPL(rt_mutex_lock);
  13918. /**
  13919. * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
  13920. - *
  13921. + **
  13922. * @lock: the rt_mutex to be locked
  13923. *
  13924. * Returns:
  13925. @@ -1486,23 +2062,32 @@
  13926. */
  13927. int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
  13928. {
  13929. - might_sleep();
  13930. -
  13931. - return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
  13932. + return rt_mutex_lock_state(lock, TASK_INTERRUPTIBLE);
  13933. }
  13934. EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
  13935. -/*
  13936. - * Futex variant with full deadlock detection.
  13937. +/**
  13938. + * rt_mutex_lock_killable - lock a rt_mutex killable
  13939. + *
  13940. + * @lock: the rt_mutex to be locked
  13941. + * @detect_deadlock: deadlock detection on/off
  13942. + *
  13943. + * Returns:
  13944. + * 0 on success
  13945. + * -EINTR when interrupted by a signal
  13946. */
  13947. -int rt_mutex_timed_futex_lock(struct rt_mutex *lock,
  13948. - struct hrtimer_sleeper *timeout)
  13949. +int __sched rt_mutex_lock_killable(struct rt_mutex *lock)
  13950. {
  13951. - might_sleep();
  13952. + return rt_mutex_lock_state(lock, TASK_KILLABLE);
  13953. +}
  13954. +EXPORT_SYMBOL_GPL(rt_mutex_lock_killable);
  13955. - return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  13956. - RT_MUTEX_FULL_CHAINWALK,
  13957. - rt_mutex_slowlock);
  13958. +/*
  13959. + * Futex variant, must not use fastpath.
  13960. + */
  13961. +int __sched rt_mutex_futex_trylock(struct rt_mutex *lock)
  13962. +{
  13963. + return rt_mutex_slowtrylock(lock);
  13964. }
  13965. /**
  13966. @@ -1525,6 +2110,7 @@
  13967. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  13968. RT_MUTEX_MIN_CHAINWALK,
  13969. + NULL,
  13970. rt_mutex_slowlock);
  13971. }
  13972. EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
  13973. @@ -1542,7 +2128,11 @@
  13974. */
  13975. int __sched rt_mutex_trylock(struct rt_mutex *lock)
  13976. {
  13977. +#ifdef CONFIG_PREEMPT_RT_FULL
  13978. + if (WARN_ON_ONCE(in_irq() || in_nmi()))
  13979. +#else
  13980. if (WARN_ON_ONCE(in_irq() || in_nmi() || in_serving_softirq()))
  13981. +#endif
  13982. return 0;
  13983. return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
  13984. @@ -1560,21 +2150,53 @@
  13985. }
  13986. EXPORT_SYMBOL_GPL(rt_mutex_unlock);
  13987. +static bool __sched __rt_mutex_unlock_common(struct rt_mutex *lock,
  13988. + struct wake_q_head *wake_q,
  13989. + struct wake_q_head *wq_sleeper)
  13990. +{
  13991. + lockdep_assert_held(&lock->wait_lock);
  13992. +
  13993. + debug_rt_mutex_unlock(lock);
  13994. +
  13995. + if (!rt_mutex_has_waiters(lock)) {
  13996. + lock->owner = NULL;
  13997. + return false; /* done */
  13998. + }
  13999. +
  14000. + /*
  14001. + * We've already deboosted, mark_wakeup_next_waiter() will
  14002. + * retain preempt_disabled when we drop the wait_lock, to
  14003. + * avoid inversion prior to the wakeup. preempt_disable()
  14004. + * therein pairs with rt_mutex_postunlock().
  14005. + */
  14006. + mark_wakeup_next_waiter(wake_q, wq_sleeper, lock);
  14007. +
  14008. + return true; /* call postunlock() */
  14009. +}
  14010. +
  14011. /**
  14012. - * rt_mutex_futex_unlock - Futex variant of rt_mutex_unlock
  14013. - * @lock: the rt_mutex to be unlocked
  14014. - *
  14015. - * Returns: true/false indicating whether priority adjustment is
  14016. - * required or not.
  14017. + * Futex variant, that since futex variants do not use the fast-path, can be
  14018. + * simple and will not need to retry.
  14019. */
  14020. -bool __sched rt_mutex_futex_unlock(struct rt_mutex *lock,
  14021. - struct wake_q_head *wqh)
  14022. +bool __sched __rt_mutex_futex_unlock(struct rt_mutex *lock,
  14023. + struct wake_q_head *wake_q,
  14024. + struct wake_q_head *wq_sleeper)
  14025. {
  14026. - if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
  14027. - rt_mutex_deadlock_account_unlock(current);
  14028. - return false;
  14029. - }
  14030. - return rt_mutex_slowunlock(lock, wqh);
  14031. + return __rt_mutex_unlock_common(lock, wake_q, wq_sleeper);
  14032. +}
  14033. +
  14034. +void __sched rt_mutex_futex_unlock(struct rt_mutex *lock)
  14035. +{
  14036. + WAKE_Q(wake_q);
  14037. + WAKE_Q(wake_sleeper_q);
  14038. + bool postunlock;
  14039. +
  14040. + raw_spin_lock_irq(&lock->wait_lock);
  14041. + postunlock = __rt_mutex_futex_unlock(lock, &wake_q, &wake_sleeper_q);
  14042. + raw_spin_unlock_irq(&lock->wait_lock);
  14043. +
  14044. + if (postunlock)
  14045. + rt_mutex_postunlock(&wake_q, &wake_sleeper_q);
  14046. }
  14047. /**
  14048. @@ -1607,13 +2229,12 @@
  14049. void __rt_mutex_init(struct rt_mutex *lock, const char *name)
  14050. {
  14051. lock->owner = NULL;
  14052. - raw_spin_lock_init(&lock->wait_lock);
  14053. lock->waiters = RB_ROOT;
  14054. lock->waiters_leftmost = NULL;
  14055. debug_rt_mutex_init(lock, name);
  14056. }
  14057. -EXPORT_SYMBOL_GPL(__rt_mutex_init);
  14058. +EXPORT_SYMBOL(__rt_mutex_init);
  14059. /**
  14060. * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
  14061. @@ -1628,10 +2249,9 @@
  14062. void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
  14063. struct task_struct *proxy_owner)
  14064. {
  14065. - __rt_mutex_init(lock, NULL);
  14066. + rt_mutex_init(lock);
  14067. debug_rt_mutex_proxy_lock(lock, proxy_owner);
  14068. rt_mutex_set_owner(lock, proxy_owner);
  14069. - rt_mutex_deadlock_account_lock(lock, proxy_owner);
  14070. }
  14071. /**
  14072. @@ -1647,34 +2267,45 @@
  14073. {
  14074. debug_rt_mutex_proxy_unlock(lock);
  14075. rt_mutex_set_owner(lock, NULL);
  14076. - rt_mutex_deadlock_account_unlock(proxy_owner);
  14077. }
  14078. -/**
  14079. - * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
  14080. - * @lock: the rt_mutex to take
  14081. - * @waiter: the pre-initialized rt_mutex_waiter
  14082. - * @task: the task to prepare
  14083. - *
  14084. - * Returns:
  14085. - * 0 - task blocked on lock
  14086. - * 1 - acquired the lock for task, caller should wake it up
  14087. - * <0 - error
  14088. - *
  14089. - * Special API call for FUTEX_REQUEUE_PI support.
  14090. - */
  14091. -int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
  14092. +int __rt_mutex_start_proxy_lock(struct rt_mutex *lock,
  14093. struct rt_mutex_waiter *waiter,
  14094. struct task_struct *task)
  14095. {
  14096. int ret;
  14097. - raw_spin_lock_irq(&lock->wait_lock);
  14098. + if (try_to_take_rt_mutex(lock, task, NULL))
  14099. + return 1;
  14100. - if (try_to_take_rt_mutex(lock, task, NULL)) {
  14101. +#ifdef CONFIG_PREEMPT_RT_FULL
  14102. + /*
  14103. + * In PREEMPT_RT there's an added race.
  14104. + * If the task, that we are about to requeue, times out,
  14105. + * it can set the PI_WAKEUP_INPROGRESS. This tells the requeue
  14106. + * to skip this task. But right after the task sets
  14107. + * its pi_blocked_on to PI_WAKEUP_INPROGRESS it can then
  14108. + * block on the spin_lock(&hb->lock), which in RT is an rtmutex.
  14109. + * This will replace the PI_WAKEUP_INPROGRESS with the actual
  14110. + * lock that it blocks on. We *must not* place this task
  14111. + * on this proxy lock in that case.
  14112. + *
  14113. + * To prevent this race, we first take the task's pi_lock
  14114. + * and check if it has updated its pi_blocked_on. If it has,
  14115. + * we assume that it woke up and we return -EAGAIN.
  14116. + * Otherwise, we set the task's pi_blocked_on to
  14117. + * PI_REQUEUE_INPROGRESS, so that if the task is waking up
  14118. + * it will know that we are in the process of requeuing it.
  14119. + */
  14120. + raw_spin_lock(&task->pi_lock);
  14121. + if (task->pi_blocked_on) {
  14122. + raw_spin_unlock(&task->pi_lock);
  14123. raw_spin_unlock_irq(&lock->wait_lock);
  14124. - return 1;
  14125. + return -EAGAIN;
  14126. }
  14127. + task->pi_blocked_on = PI_REQUEUE_INPROGRESS;
  14128. + raw_spin_unlock(&task->pi_lock);
  14129. +#endif
  14130. /* We enforce deadlock detection for futexes */
  14131. ret = task_blocks_on_rt_mutex(lock, waiter, task,
  14132. @@ -1690,17 +2321,41 @@
  14133. ret = 0;
  14134. }
  14135. - if (unlikely(ret))
  14136. + if (ret && rt_mutex_has_waiters(lock))
  14137. remove_waiter(lock, waiter);
  14138. - raw_spin_unlock_irq(&lock->wait_lock);
  14139. -
  14140. debug_rt_mutex_print_deadlock(waiter);
  14141. return ret;
  14142. }
  14143. /**
  14144. + * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
  14145. + * @lock: the rt_mutex to take
  14146. + * @waiter: the pre-initialized rt_mutex_waiter
  14147. + * @task: the task to prepare
  14148. + *
  14149. + * Returns:
  14150. + * 0 - task blocked on lock
  14151. + * 1 - acquired the lock for task, caller should wake it up
  14152. + * <0 - error
  14153. + *
  14154. + * Special API call for FUTEX_REQUEUE_PI support.
  14155. + */
  14156. +int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
  14157. + struct rt_mutex_waiter *waiter,
  14158. + struct task_struct *task)
  14159. +{
  14160. + int ret;
  14161. +
  14162. + raw_spin_lock_irq(&lock->wait_lock);
  14163. + ret = __rt_mutex_start_proxy_lock(lock, waiter, task);
  14164. + raw_spin_unlock_irq(&lock->wait_lock);
  14165. +
  14166. + return ret;
  14167. +}
  14168. +
  14169. +/**
  14170. * rt_mutex_next_owner - return the next owner of the lock
  14171. *
  14172. * @lock: the rt lock query
  14173. @@ -1721,24 +2376,27 @@
  14174. }
  14175. /**
  14176. - * rt_mutex_finish_proxy_lock() - Complete lock acquisition
  14177. + * rt_mutex_wait_proxy_lock() - Wait for lock acquisition
  14178. * @lock: the rt_mutex we were woken on
  14179. * @to: the timeout, null if none. hrtimer should already have
  14180. * been started.
  14181. * @waiter: the pre-initialized rt_mutex_waiter
  14182. *
  14183. - * Complete the lock acquisition started our behalf by another thread.
  14184. + * Wait for the the lock acquisition started on our behalf by
  14185. + * rt_mutex_start_proxy_lock(). Upon failure, the caller must call
  14186. + * rt_mutex_cleanup_proxy_lock().
  14187. *
  14188. * Returns:
  14189. * 0 - success
  14190. * <0 - error, one of -EINTR, -ETIMEDOUT
  14191. *
  14192. - * Special API call for PI-futex requeue support
  14193. + * Special API call for PI-futex support
  14194. */
  14195. -int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
  14196. +int rt_mutex_wait_proxy_lock(struct rt_mutex *lock,
  14197. struct hrtimer_sleeper *to,
  14198. struct rt_mutex_waiter *waiter)
  14199. {
  14200. + struct task_struct *tsk = current;
  14201. int ret;
  14202. raw_spin_lock_irq(&lock->wait_lock);
  14203. @@ -1746,10 +2404,65 @@
  14204. set_current_state(TASK_INTERRUPTIBLE);
  14205. /* sleep on the mutex */
  14206. - ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
  14207. + ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter, NULL);
  14208. +
  14209. + /*
  14210. + * RT has a problem here when the wait got interrupted by a timeout
  14211. + * or a signal. task->pi_blocked_on is still set. The task must
  14212. + * acquire the hash bucket lock when returning from this function.
  14213. + *
  14214. + * If the hash bucket lock is contended then the
  14215. + * BUG_ON(rt_mutex_real_waiter(task->pi_blocked_on)) in
  14216. + * task_blocks_on_rt_mutex() will trigger. This can be avoided by
  14217. + * clearing task->pi_blocked_on which removes the task from the
  14218. + * boosting chain of the rtmutex. That's correct because the task
  14219. + * is not longer blocked on it.
  14220. + */
  14221. + if (ret) {
  14222. + raw_spin_lock(&tsk->pi_lock);
  14223. + tsk->pi_blocked_on = NULL;
  14224. + raw_spin_unlock(&tsk->pi_lock);
  14225. + }
  14226. +
  14227. + raw_spin_unlock_irq(&lock->wait_lock);
  14228. +
  14229. + return ret;
  14230. +}
  14231. +
  14232. +/**
  14233. + * rt_mutex_cleanup_proxy_lock() - Cleanup failed lock acquisition
  14234. + * @lock: the rt_mutex we were woken on
  14235. + * @waiter: the pre-initialized rt_mutex_waiter
  14236. + *
  14237. + * Attempt to clean up after a failed rt_mutex_wait_proxy_lock().
  14238. + *
  14239. + * Unless we acquired the lock; we're still enqueued on the wait-list and can
  14240. + * in fact still be granted ownership until we're removed. Therefore we can
  14241. + * find we are in fact the owner and must disregard the
  14242. + * rt_mutex_wait_proxy_lock() failure.
  14243. + *
  14244. + * Returns:
  14245. + * true - did the cleanup, we done.
  14246. + * false - we acquired the lock after rt_mutex_wait_proxy_lock() returned,
  14247. + * caller should disregards its return value.
  14248. + *
  14249. + * Special API call for PI-futex support
  14250. + */
  14251. +bool rt_mutex_cleanup_proxy_lock(struct rt_mutex *lock,
  14252. + struct rt_mutex_waiter *waiter)
  14253. +{
  14254. + bool cleanup = false;
  14255. - if (unlikely(ret))
  14256. + raw_spin_lock_irq(&lock->wait_lock);
  14257. + /*
  14258. + * Unless we're the owner; we're still enqueued on the wait_list.
  14259. + * So check if we became owner, if not, take us off the wait_list.
  14260. + */
  14261. + if (rt_mutex_owner(lock) != current) {
  14262. remove_waiter(lock, waiter);
  14263. + fixup_rt_mutex_waiters(lock);
  14264. + cleanup = true;
  14265. + }
  14266. /*
  14267. * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
  14268. @@ -1759,5 +2472,91 @@
  14269. raw_spin_unlock_irq(&lock->wait_lock);
  14270. + return cleanup;
  14271. +}
  14272. +
  14273. +static inline int
  14274. +ww_mutex_deadlock_injection(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
  14275. +{
  14276. +#ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
  14277. + unsigned tmp;
  14278. +
  14279. + if (ctx->deadlock_inject_countdown-- == 0) {
  14280. + tmp = ctx->deadlock_inject_interval;
  14281. + if (tmp > UINT_MAX/4)
  14282. + tmp = UINT_MAX;
  14283. + else
  14284. + tmp = tmp*2 + tmp + tmp/2;
  14285. +
  14286. + ctx->deadlock_inject_interval = tmp;
  14287. + ctx->deadlock_inject_countdown = tmp;
  14288. + ctx->contending_lock = lock;
  14289. +
  14290. + ww_mutex_unlock(lock);
  14291. +
  14292. + return -EDEADLK;
  14293. + }
  14294. +#endif
  14295. +
  14296. + return 0;
  14297. +}
  14298. +
  14299. +#ifdef CONFIG_PREEMPT_RT_FULL
  14300. +int __sched
  14301. +__ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ww_ctx)
  14302. +{
  14303. + int ret;
  14304. +
  14305. + might_sleep();
  14306. +
  14307. + mutex_acquire_nest(&lock->base.dep_map, 0, 0, &ww_ctx->dep_map, _RET_IP_);
  14308. + ret = rt_mutex_slowlock(&lock->base.lock, TASK_INTERRUPTIBLE, NULL, 0, ww_ctx);
  14309. + if (ret)
  14310. + mutex_release(&lock->base.dep_map, 1, _RET_IP_);
  14311. + else if (!ret && ww_ctx->acquired > 1)
  14312. + return ww_mutex_deadlock_injection(lock, ww_ctx);
  14313. +
  14314. + return ret;
  14315. +}
  14316. +EXPORT_SYMBOL_GPL(__ww_mutex_lock_interruptible);
  14317. +
  14318. +int __sched
  14319. +__ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ww_ctx)
  14320. +{
  14321. + int ret;
  14322. +
  14323. + might_sleep();
  14324. +
  14325. + mutex_acquire_nest(&lock->base.dep_map, 0, 0, &ww_ctx->dep_map, _RET_IP_);
  14326. + ret = rt_mutex_slowlock(&lock->base.lock, TASK_UNINTERRUPTIBLE, NULL, 0, ww_ctx);
  14327. + if (ret)
  14328. + mutex_release(&lock->base.dep_map, 1, _RET_IP_);
  14329. + else if (!ret && ww_ctx->acquired > 1)
  14330. + return ww_mutex_deadlock_injection(lock, ww_ctx);
  14331. +
  14332. return ret;
  14333. }
  14334. +EXPORT_SYMBOL_GPL(__ww_mutex_lock);
  14335. +
  14336. +void __sched ww_mutex_unlock(struct ww_mutex *lock)
  14337. +{
  14338. + int nest = !!lock->ctx;
  14339. +
  14340. + /*
  14341. + * The unlocking fastpath is the 0->1 transition from 'locked'
  14342. + * into 'unlocked' state:
  14343. + */
  14344. + if (nest) {
  14345. +#ifdef CONFIG_DEBUG_MUTEXES
  14346. + DEBUG_LOCKS_WARN_ON(!lock->ctx->acquired);
  14347. +#endif
  14348. + if (lock->ctx->acquired > 0)
  14349. + lock->ctx->acquired--;
  14350. + lock->ctx = NULL;
  14351. + }
  14352. +
  14353. + mutex_release(&lock->base.dep_map, nest, _RET_IP_);
  14354. + rt_mutex_unlock(&lock->base.lock);
  14355. +}
  14356. +EXPORT_SYMBOL(ww_mutex_unlock);
  14357. +#endif
  14358. diff -Nur linux-4.9.28.orig/kernel/locking/rtmutex_common.h linux-4.9.28/kernel/locking/rtmutex_common.h
  14359. --- linux-4.9.28.orig/kernel/locking/rtmutex_common.h 2017-05-14 14:00:37.000000000 +0200
  14360. +++ linux-4.9.28/kernel/locking/rtmutex_common.h 2017-05-19 03:37:25.186176955 +0200
  14361. @@ -27,12 +27,14 @@
  14362. struct rb_node pi_tree_entry;
  14363. struct task_struct *task;
  14364. struct rt_mutex *lock;
  14365. + bool savestate;
  14366. #ifdef CONFIG_DEBUG_RT_MUTEXES
  14367. unsigned long ip;
  14368. struct pid *deadlock_task_pid;
  14369. struct rt_mutex *deadlock_lock;
  14370. #endif
  14371. int prio;
  14372. + u64 deadline;
  14373. };
  14374. /*
  14375. @@ -98,21 +100,45 @@
  14376. /*
  14377. * PI-futex support (proxy locking functions, etc.):
  14378. */
  14379. +#define PI_WAKEUP_INPROGRESS ((struct rt_mutex_waiter *) 1)
  14380. +#define PI_REQUEUE_INPROGRESS ((struct rt_mutex_waiter *) 2)
  14381. +
  14382. extern struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock);
  14383. extern void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
  14384. struct task_struct *proxy_owner);
  14385. extern void rt_mutex_proxy_unlock(struct rt_mutex *lock,
  14386. struct task_struct *proxy_owner);
  14387. +extern void rt_mutex_init_waiter(struct rt_mutex_waiter *waiter, bool savetate);
  14388. +extern int __rt_mutex_start_proxy_lock(struct rt_mutex *lock,
  14389. + struct rt_mutex_waiter *waiter,
  14390. + struct task_struct *task);
  14391. extern int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
  14392. struct rt_mutex_waiter *waiter,
  14393. struct task_struct *task);
  14394. -extern int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
  14395. - struct hrtimer_sleeper *to,
  14396. - struct rt_mutex_waiter *waiter);
  14397. -extern int rt_mutex_timed_futex_lock(struct rt_mutex *l, struct hrtimer_sleeper *to);
  14398. -extern bool rt_mutex_futex_unlock(struct rt_mutex *lock,
  14399. - struct wake_q_head *wqh);
  14400. -extern void rt_mutex_adjust_prio(struct task_struct *task);
  14401. +extern int rt_mutex_wait_proxy_lock(struct rt_mutex *lock,
  14402. + struct hrtimer_sleeper *to,
  14403. + struct rt_mutex_waiter *waiter);
  14404. +extern bool rt_mutex_cleanup_proxy_lock(struct rt_mutex *lock,
  14405. + struct rt_mutex_waiter *waiter);
  14406. +
  14407. +extern int rt_mutex_futex_trylock(struct rt_mutex *l);
  14408. +
  14409. +extern void rt_mutex_futex_unlock(struct rt_mutex *lock);
  14410. +extern bool __rt_mutex_futex_unlock(struct rt_mutex *lock,
  14411. + struct wake_q_head *wqh,
  14412. + struct wake_q_head *wq_sleeper);
  14413. +
  14414. +extern void rt_mutex_postunlock(struct wake_q_head *wake_q,
  14415. + struct wake_q_head *wq_sleeper);
  14416. +
  14417. +/* RW semaphore special interface */
  14418. +struct ww_acquire_ctx;
  14419. +
  14420. +int __sched rt_mutex_slowlock_locked(struct rt_mutex *lock, int state,
  14421. + struct hrtimer_sleeper *timeout,
  14422. + enum rtmutex_chainwalk chwalk,
  14423. + struct ww_acquire_ctx *ww_ctx,
  14424. + struct rt_mutex_waiter *waiter);
  14425. #ifdef CONFIG_DEBUG_RT_MUTEXES
  14426. # include "rtmutex-debug.h"
  14427. diff -Nur linux-4.9.28.orig/kernel/locking/rtmutex-debug.c linux-4.9.28/kernel/locking/rtmutex-debug.c
  14428. --- linux-4.9.28.orig/kernel/locking/rtmutex-debug.c 2017-05-14 14:00:37.000000000 +0200
  14429. +++ linux-4.9.28/kernel/locking/rtmutex-debug.c 2017-05-19 03:37:25.186176955 +0200
  14430. @@ -173,12 +173,3 @@
  14431. lock->name = name;
  14432. }
  14433. -void
  14434. -rt_mutex_deadlock_account_lock(struct rt_mutex *lock, struct task_struct *task)
  14435. -{
  14436. -}
  14437. -
  14438. -void rt_mutex_deadlock_account_unlock(struct task_struct *task)
  14439. -{
  14440. -}
  14441. -
  14442. diff -Nur linux-4.9.28.orig/kernel/locking/rtmutex-debug.h linux-4.9.28/kernel/locking/rtmutex-debug.h
  14443. --- linux-4.9.28.orig/kernel/locking/rtmutex-debug.h 2017-05-14 14:00:37.000000000 +0200
  14444. +++ linux-4.9.28/kernel/locking/rtmutex-debug.h 2017-05-19 03:37:25.186176955 +0200
  14445. @@ -9,9 +9,6 @@
  14446. * This file contains macros used solely by rtmutex.c. Debug version.
  14447. */
  14448. -extern void
  14449. -rt_mutex_deadlock_account_lock(struct rt_mutex *lock, struct task_struct *task);
  14450. -extern void rt_mutex_deadlock_account_unlock(struct task_struct *task);
  14451. extern void debug_rt_mutex_init_waiter(struct rt_mutex_waiter *waiter);
  14452. extern void debug_rt_mutex_free_waiter(struct rt_mutex_waiter *waiter);
  14453. extern void debug_rt_mutex_init(struct rt_mutex *lock, const char *name);
  14454. diff -Nur linux-4.9.28.orig/kernel/locking/rtmutex.h linux-4.9.28/kernel/locking/rtmutex.h
  14455. --- linux-4.9.28.orig/kernel/locking/rtmutex.h 2017-05-14 14:00:37.000000000 +0200
  14456. +++ linux-4.9.28/kernel/locking/rtmutex.h 2017-05-19 03:37:25.186176955 +0200
  14457. @@ -11,8 +11,6 @@
  14458. */
  14459. #define rt_mutex_deadlock_check(l) (0)
  14460. -#define rt_mutex_deadlock_account_lock(m, t) do { } while (0)
  14461. -#define rt_mutex_deadlock_account_unlock(l) do { } while (0)
  14462. #define debug_rt_mutex_init_waiter(w) do { } while (0)
  14463. #define debug_rt_mutex_free_waiter(w) do { } while (0)
  14464. #define debug_rt_mutex_lock(l) do { } while (0)
  14465. diff -Nur linux-4.9.28.orig/kernel/locking/rwsem-rt.c linux-4.9.28/kernel/locking/rwsem-rt.c
  14466. --- linux-4.9.28.orig/kernel/locking/rwsem-rt.c 1970-01-01 01:00:00.000000000 +0100
  14467. +++ linux-4.9.28/kernel/locking/rwsem-rt.c 2017-05-19 03:37:25.186176955 +0200
  14468. @@ -0,0 +1,268 @@
  14469. +/*
  14470. + */
  14471. +#include <linux/rwsem.h>
  14472. +#include <linux/sched.h>
  14473. +#include <linux/export.h>
  14474. +
  14475. +#include "rtmutex_common.h"
  14476. +
  14477. +/*
  14478. + * RT-specific reader/writer semaphores
  14479. + *
  14480. + * down_write()
  14481. + * 1) Lock sem->rtmutex
  14482. + * 2) Remove the reader BIAS to force readers into the slow path
  14483. + * 3) Wait until all readers have left the critical region
  14484. + * 4) Mark it write locked
  14485. + *
  14486. + * up_write()
  14487. + * 1) Remove the write locked marker
  14488. + * 2) Set the reader BIAS so readers can use the fast path again
  14489. + * 3) Unlock sem->rtmutex to release blocked readers
  14490. + *
  14491. + * down_read()
  14492. + * 1) Try fast path acquisition (reader BIAS is set)
  14493. + * 2) Take sem->rtmutex.wait_lock which protects the writelocked flag
  14494. + * 3) If !writelocked, acquire it for read
  14495. + * 4) If writelocked, block on sem->rtmutex
  14496. + * 5) unlock sem->rtmutex, goto 1)
  14497. + *
  14498. + * up_read()
  14499. + * 1) Try fast path release (reader count != 1)
  14500. + * 2) Wake the writer waiting in down_write()#3
  14501. + *
  14502. + * down_read()#3 has the consequence, that rw semaphores on RT are not writer
  14503. + * fair, but writers, which should be avoided in RT tasks (think mmap_sem),
  14504. + * are subject to the rtmutex priority/DL inheritance mechanism.
  14505. + *
  14506. + * It's possible to make the rw semaphores writer fair by keeping a list of
  14507. + * active readers. A blocked writer would force all newly incoming readers to
  14508. + * block on the rtmutex, but the rtmutex would have to be proxy locked for one
  14509. + * reader after the other. We can't use multi-reader inheritance because there
  14510. + * is no way to support that with SCHED_DEADLINE. Implementing the one by one
  14511. + * reader boosting/handover mechanism is a major surgery for a very dubious
  14512. + * value.
  14513. + *
  14514. + * The risk of writer starvation is there, but the pathological use cases
  14515. + * which trigger it are not necessarily the typical RT workloads.
  14516. + */
  14517. +
  14518. +void __rwsem_init(struct rw_semaphore *sem, const char *name,
  14519. + struct lock_class_key *key)
  14520. +{
  14521. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  14522. + /*
  14523. + * Make sure we are not reinitializing a held semaphore:
  14524. + */
  14525. + debug_check_no_locks_freed((void *)sem, sizeof(*sem));
  14526. + lockdep_init_map(&sem->dep_map, name, key, 0);
  14527. +#endif
  14528. + atomic_set(&sem->readers, READER_BIAS);
  14529. +}
  14530. +EXPORT_SYMBOL(__rwsem_init);
  14531. +
  14532. +int __down_read_trylock(struct rw_semaphore *sem)
  14533. +{
  14534. + int r, old;
  14535. +
  14536. + /*
  14537. + * Increment reader count, if sem->readers < 0, i.e. READER_BIAS is
  14538. + * set.
  14539. + */
  14540. + for (r = atomic_read(&sem->readers); r < 0;) {
  14541. + old = atomic_cmpxchg(&sem->readers, r, r + 1);
  14542. + if (likely(old == r))
  14543. + return 1;
  14544. + r = old;
  14545. + }
  14546. + return 0;
  14547. +}
  14548. +
  14549. +void __sched __down_read(struct rw_semaphore *sem)
  14550. +{
  14551. + struct rt_mutex *m = &sem->rtmutex;
  14552. + struct rt_mutex_waiter waiter;
  14553. +
  14554. + if (__down_read_trylock(sem))
  14555. + return;
  14556. +
  14557. + might_sleep();
  14558. + raw_spin_lock_irq(&m->wait_lock);
  14559. + /*
  14560. + * Allow readers as long as the writer has not completely
  14561. + * acquired the semaphore for write.
  14562. + */
  14563. + if (atomic_read(&sem->readers) != WRITER_BIAS) {
  14564. + atomic_inc(&sem->readers);
  14565. + raw_spin_unlock_irq(&m->wait_lock);
  14566. + return;
  14567. + }
  14568. +
  14569. + /*
  14570. + * Call into the slow lock path with the rtmutex->wait_lock
  14571. + * held, so this can't result in the following race:
  14572. + *
  14573. + * Reader1 Reader2 Writer
  14574. + * down_read()
  14575. + * down_write()
  14576. + * rtmutex_lock(m)
  14577. + * swait()
  14578. + * down_read()
  14579. + * unlock(m->wait_lock)
  14580. + * up_read()
  14581. + * swake()
  14582. + * lock(m->wait_lock)
  14583. + * sem->writelocked=true
  14584. + * unlock(m->wait_lock)
  14585. + *
  14586. + * up_write()
  14587. + * sem->writelocked=false
  14588. + * rtmutex_unlock(m)
  14589. + * down_read()
  14590. + * down_write()
  14591. + * rtmutex_lock(m)
  14592. + * swait()
  14593. + * rtmutex_lock(m)
  14594. + *
  14595. + * That would put Reader1 behind the writer waiting on
  14596. + * Reader2 to call up_read() which might be unbound.
  14597. + */
  14598. + rt_mutex_init_waiter(&waiter, false);
  14599. + rt_mutex_slowlock_locked(m, TASK_UNINTERRUPTIBLE, NULL,
  14600. + RT_MUTEX_MIN_CHAINWALK, NULL,
  14601. + &waiter);
  14602. + /*
  14603. + * The slowlock() above is guaranteed to return with the rtmutex is
  14604. + * now held, so there can't be a writer active. Increment the reader
  14605. + * count and immediately drop the rtmutex again.
  14606. + */
  14607. + atomic_inc(&sem->readers);
  14608. + raw_spin_unlock_irq(&m->wait_lock);
  14609. + rt_mutex_unlock(m);
  14610. +
  14611. + debug_rt_mutex_free_waiter(&waiter);
  14612. +}
  14613. +
  14614. +void __up_read(struct rw_semaphore *sem)
  14615. +{
  14616. + struct rt_mutex *m = &sem->rtmutex;
  14617. + struct task_struct *tsk;
  14618. +
  14619. + /*
  14620. + * sem->readers can only hit 0 when a writer is waiting for the
  14621. + * active readers to leave the critical region.
  14622. + */
  14623. + if (!atomic_dec_and_test(&sem->readers))
  14624. + return;
  14625. +
  14626. + might_sleep();
  14627. + raw_spin_lock_irq(&m->wait_lock);
  14628. + /*
  14629. + * Wake the writer, i.e. the rtmutex owner. It might release the
  14630. + * rtmutex concurrently in the fast path (due to a signal), but to
  14631. + * clean up the rwsem it needs to acquire m->wait_lock. The worst
  14632. + * case which can happen is a spurious wakeup.
  14633. + */
  14634. + tsk = rt_mutex_owner(m);
  14635. + if (tsk)
  14636. + wake_up_process(tsk);
  14637. +
  14638. + raw_spin_unlock_irq(&m->wait_lock);
  14639. +}
  14640. +
  14641. +static void __up_write_unlock(struct rw_semaphore *sem, int bias,
  14642. + unsigned long flags)
  14643. +{
  14644. + struct rt_mutex *m = &sem->rtmutex;
  14645. +
  14646. + atomic_add(READER_BIAS - bias, &sem->readers);
  14647. + raw_spin_unlock_irqrestore(&m->wait_lock, flags);
  14648. + rt_mutex_unlock(m);
  14649. +}
  14650. +
  14651. +static int __sched __down_write_common(struct rw_semaphore *sem, int state)
  14652. +{
  14653. + struct rt_mutex *m = &sem->rtmutex;
  14654. + unsigned long flags;
  14655. +
  14656. + /* Take the rtmutex as a first step */
  14657. + if (rt_mutex_lock_state(m, state))
  14658. + return -EINTR;
  14659. +
  14660. + /* Force readers into slow path */
  14661. + atomic_sub(READER_BIAS, &sem->readers);
  14662. + might_sleep();
  14663. +
  14664. + set_current_state(state);
  14665. + for (;;) {
  14666. + raw_spin_lock_irqsave(&m->wait_lock, flags);
  14667. + /* Have all readers left the critical region? */
  14668. + if (!atomic_read(&sem->readers)) {
  14669. + atomic_set(&sem->readers, WRITER_BIAS);
  14670. + __set_current_state(TASK_RUNNING);
  14671. + raw_spin_unlock_irqrestore(&m->wait_lock, flags);
  14672. + return 0;
  14673. + }
  14674. +
  14675. + if (signal_pending_state(state, current)) {
  14676. + __set_current_state(TASK_RUNNING);
  14677. + __up_write_unlock(sem, 0, flags);
  14678. + return -EINTR;
  14679. + }
  14680. + raw_spin_unlock_irqrestore(&m->wait_lock, flags);
  14681. +
  14682. + if (atomic_read(&sem->readers) != 0) {
  14683. + schedule();
  14684. + set_current_state(state);
  14685. + }
  14686. + }
  14687. +}
  14688. +
  14689. +void __sched __down_write(struct rw_semaphore *sem)
  14690. +{
  14691. + __down_write_common(sem, TASK_UNINTERRUPTIBLE);
  14692. +}
  14693. +
  14694. +int __sched __down_write_killable(struct rw_semaphore *sem)
  14695. +{
  14696. + return __down_write_common(sem, TASK_KILLABLE);
  14697. +}
  14698. +
  14699. +int __down_write_trylock(struct rw_semaphore *sem)
  14700. +{
  14701. + struct rt_mutex *m = &sem->rtmutex;
  14702. + unsigned long flags;
  14703. +
  14704. + if (!rt_mutex_trylock(m))
  14705. + return 0;
  14706. +
  14707. + atomic_sub(READER_BIAS, &sem->readers);
  14708. +
  14709. + raw_spin_lock_irqsave(&m->wait_lock, flags);
  14710. + if (!atomic_read(&sem->readers)) {
  14711. + atomic_set(&sem->readers, WRITER_BIAS);
  14712. + raw_spin_unlock_irqrestore(&m->wait_lock, flags);
  14713. + return 1;
  14714. + }
  14715. + __up_write_unlock(sem, 0, flags);
  14716. + return 0;
  14717. +}
  14718. +
  14719. +void __up_write(struct rw_semaphore *sem)
  14720. +{
  14721. + struct rt_mutex *m = &sem->rtmutex;
  14722. + unsigned long flags;
  14723. +
  14724. + raw_spin_lock_irqsave(&m->wait_lock, flags);
  14725. + __up_write_unlock(sem, WRITER_BIAS, flags);
  14726. +}
  14727. +
  14728. +void __downgrade_write(struct rw_semaphore *sem)
  14729. +{
  14730. + struct rt_mutex *m = &sem->rtmutex;
  14731. + unsigned long flags;
  14732. +
  14733. + raw_spin_lock_irqsave(&m->wait_lock, flags);
  14734. + /* Release it and account current as reader */
  14735. + __up_write_unlock(sem, WRITER_BIAS - 1, flags);
  14736. +}
  14737. diff -Nur linux-4.9.28.orig/kernel/locking/spinlock.c linux-4.9.28/kernel/locking/spinlock.c
  14738. --- linux-4.9.28.orig/kernel/locking/spinlock.c 2017-05-14 14:00:37.000000000 +0200
  14739. +++ linux-4.9.28/kernel/locking/spinlock.c 2017-05-19 03:37:25.186176955 +0200
  14740. @@ -124,8 +124,11 @@
  14741. * __[spin|read|write]_lock_bh()
  14742. */
  14743. BUILD_LOCK_OPS(spin, raw_spinlock);
  14744. +
  14745. +#ifndef CONFIG_PREEMPT_RT_FULL
  14746. BUILD_LOCK_OPS(read, rwlock);
  14747. BUILD_LOCK_OPS(write, rwlock);
  14748. +#endif
  14749. #endif
  14750. @@ -209,6 +212,8 @@
  14751. EXPORT_SYMBOL(_raw_spin_unlock_bh);
  14752. #endif
  14753. +#ifndef CONFIG_PREEMPT_RT_FULL
  14754. +
  14755. #ifndef CONFIG_INLINE_READ_TRYLOCK
  14756. int __lockfunc _raw_read_trylock(rwlock_t *lock)
  14757. {
  14758. @@ -353,6 +358,8 @@
  14759. EXPORT_SYMBOL(_raw_write_unlock_bh);
  14760. #endif
  14761. +#endif /* !PREEMPT_RT_FULL */
  14762. +
  14763. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  14764. void __lockfunc _raw_spin_lock_nested(raw_spinlock_t *lock, int subclass)
  14765. diff -Nur linux-4.9.28.orig/kernel/locking/spinlock_debug.c linux-4.9.28/kernel/locking/spinlock_debug.c
  14766. --- linux-4.9.28.orig/kernel/locking/spinlock_debug.c 2017-05-14 14:00:37.000000000 +0200
  14767. +++ linux-4.9.28/kernel/locking/spinlock_debug.c 2017-05-19 03:37:25.190177110 +0200
  14768. @@ -31,6 +31,7 @@
  14769. EXPORT_SYMBOL(__raw_spin_lock_init);
  14770. +#ifndef CONFIG_PREEMPT_RT_FULL
  14771. void __rwlock_init(rwlock_t *lock, const char *name,
  14772. struct lock_class_key *key)
  14773. {
  14774. @@ -48,6 +49,7 @@
  14775. }
  14776. EXPORT_SYMBOL(__rwlock_init);
  14777. +#endif
  14778. static void spin_dump(raw_spinlock_t *lock, const char *msg)
  14779. {
  14780. @@ -159,6 +161,7 @@
  14781. arch_spin_unlock(&lock->raw_lock);
  14782. }
  14783. +#ifndef CONFIG_PREEMPT_RT_FULL
  14784. static void rwlock_bug(rwlock_t *lock, const char *msg)
  14785. {
  14786. if (!debug_locks_off())
  14787. @@ -300,3 +303,5 @@
  14788. debug_write_unlock(lock);
  14789. arch_write_unlock(&lock->raw_lock);
  14790. }
  14791. +
  14792. +#endif
  14793. diff -Nur linux-4.9.28.orig/kernel/module.c linux-4.9.28/kernel/module.c
  14794. --- linux-4.9.28.orig/kernel/module.c 2017-05-14 14:00:37.000000000 +0200
  14795. +++ linux-4.9.28/kernel/module.c 2017-05-19 03:37:25.190177110 +0200
  14796. @@ -660,16 +660,7 @@
  14797. memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
  14798. }
  14799. -/**
  14800. - * is_module_percpu_address - test whether address is from module static percpu
  14801. - * @addr: address to test
  14802. - *
  14803. - * Test whether @addr belongs to module static percpu area.
  14804. - *
  14805. - * RETURNS:
  14806. - * %true if @addr is from module static percpu area
  14807. - */
  14808. -bool is_module_percpu_address(unsigned long addr)
  14809. +bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
  14810. {
  14811. struct module *mod;
  14812. unsigned int cpu;
  14813. @@ -683,9 +674,15 @@
  14814. continue;
  14815. for_each_possible_cpu(cpu) {
  14816. void *start = per_cpu_ptr(mod->percpu, cpu);
  14817. + void *va = (void *)addr;
  14818. - if ((void *)addr >= start &&
  14819. - (void *)addr < start + mod->percpu_size) {
  14820. + if (va >= start && va < start + mod->percpu_size) {
  14821. + if (can_addr) {
  14822. + *can_addr = (unsigned long) (va - start);
  14823. + *can_addr += (unsigned long)
  14824. + per_cpu_ptr(mod->percpu,
  14825. + get_boot_cpu_id());
  14826. + }
  14827. preempt_enable();
  14828. return true;
  14829. }
  14830. @@ -696,6 +693,20 @@
  14831. return false;
  14832. }
  14833. +/**
  14834. + * is_module_percpu_address - test whether address is from module static percpu
  14835. + * @addr: address to test
  14836. + *
  14837. + * Test whether @addr belongs to module static percpu area.
  14838. + *
  14839. + * RETURNS:
  14840. + * %true if @addr is from module static percpu area
  14841. + */
  14842. +bool is_module_percpu_address(unsigned long addr)
  14843. +{
  14844. + return __is_module_percpu_address(addr, NULL);
  14845. +}
  14846. +
  14847. #else /* ... !CONFIG_SMP */
  14848. static inline void __percpu *mod_percpu(struct module *mod)
  14849. @@ -726,6 +737,11 @@
  14850. {
  14851. return false;
  14852. }
  14853. +
  14854. +bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
  14855. +{
  14856. + return false;
  14857. +}
  14858. #endif /* CONFIG_SMP */
  14859. diff -Nur linux-4.9.28.orig/kernel/panic.c linux-4.9.28/kernel/panic.c
  14860. --- linux-4.9.28.orig/kernel/panic.c 2017-05-14 14:00:37.000000000 +0200
  14861. +++ linux-4.9.28/kernel/panic.c 2017-05-19 03:37:25.190177110 +0200
  14862. @@ -482,9 +482,11 @@
  14863. static int init_oops_id(void)
  14864. {
  14865. +#ifndef CONFIG_PREEMPT_RT_FULL
  14866. if (!oops_id)
  14867. get_random_bytes(&oops_id, sizeof(oops_id));
  14868. else
  14869. +#endif
  14870. oops_id++;
  14871. return 0;
  14872. diff -Nur linux-4.9.28.orig/kernel/power/hibernate.c linux-4.9.28/kernel/power/hibernate.c
  14873. --- linux-4.9.28.orig/kernel/power/hibernate.c 2017-05-14 14:00:37.000000000 +0200
  14874. +++ linux-4.9.28/kernel/power/hibernate.c 2017-05-19 03:37:25.190177110 +0200
  14875. @@ -286,6 +286,8 @@
  14876. local_irq_disable();
  14877. + system_state = SYSTEM_SUSPEND;
  14878. +
  14879. error = syscore_suspend();
  14880. if (error) {
  14881. printk(KERN_ERR "PM: Some system devices failed to power down, "
  14882. @@ -317,6 +319,7 @@
  14883. syscore_resume();
  14884. Enable_irqs:
  14885. + system_state = SYSTEM_RUNNING;
  14886. local_irq_enable();
  14887. Enable_cpus:
  14888. @@ -446,6 +449,7 @@
  14889. goto Enable_cpus;
  14890. local_irq_disable();
  14891. + system_state = SYSTEM_SUSPEND;
  14892. error = syscore_suspend();
  14893. if (error)
  14894. @@ -479,6 +483,7 @@
  14895. syscore_resume();
  14896. Enable_irqs:
  14897. + system_state = SYSTEM_RUNNING;
  14898. local_irq_enable();
  14899. Enable_cpus:
  14900. @@ -564,6 +569,7 @@
  14901. goto Enable_cpus;
  14902. local_irq_disable();
  14903. + system_state = SYSTEM_SUSPEND;
  14904. syscore_suspend();
  14905. if (pm_wakeup_pending()) {
  14906. error = -EAGAIN;
  14907. @@ -576,6 +582,7 @@
  14908. Power_up:
  14909. syscore_resume();
  14910. + system_state = SYSTEM_RUNNING;
  14911. local_irq_enable();
  14912. Enable_cpus:
  14913. @@ -676,6 +683,10 @@
  14914. return error;
  14915. }
  14916. +#ifndef CONFIG_SUSPEND
  14917. +bool pm_in_action;
  14918. +#endif
  14919. +
  14920. /**
  14921. * hibernate - Carry out system hibernation, including saving the image.
  14922. */
  14923. @@ -689,6 +700,8 @@
  14924. return -EPERM;
  14925. }
  14926. + pm_in_action = true;
  14927. +
  14928. lock_system_sleep();
  14929. /* The snapshot device should not be opened while we're running */
  14930. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  14931. @@ -766,6 +779,7 @@
  14932. atomic_inc(&snapshot_device_available);
  14933. Unlock:
  14934. unlock_system_sleep();
  14935. + pm_in_action = false;
  14936. return error;
  14937. }
  14938. diff -Nur linux-4.9.28.orig/kernel/power/suspend.c linux-4.9.28/kernel/power/suspend.c
  14939. --- linux-4.9.28.orig/kernel/power/suspend.c 2017-05-14 14:00:37.000000000 +0200
  14940. +++ linux-4.9.28/kernel/power/suspend.c 2017-05-19 03:37:25.190177110 +0200
  14941. @@ -369,6 +369,8 @@
  14942. arch_suspend_disable_irqs();
  14943. BUG_ON(!irqs_disabled());
  14944. + system_state = SYSTEM_SUSPEND;
  14945. +
  14946. error = syscore_suspend();
  14947. if (!error) {
  14948. *wakeup = pm_wakeup_pending();
  14949. @@ -385,6 +387,8 @@
  14950. syscore_resume();
  14951. }
  14952. + system_state = SYSTEM_RUNNING;
  14953. +
  14954. arch_suspend_enable_irqs();
  14955. BUG_ON(irqs_disabled());
  14956. @@ -527,6 +531,8 @@
  14957. return error;
  14958. }
  14959. +bool pm_in_action;
  14960. +
  14961. /**
  14962. * pm_suspend - Externally visible function for suspending the system.
  14963. * @state: System sleep state to enter.
  14964. @@ -541,6 +547,8 @@
  14965. if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX)
  14966. return -EINVAL;
  14967. + pm_in_action = true;
  14968. +
  14969. error = enter_state(state);
  14970. if (error) {
  14971. suspend_stats.fail++;
  14972. @@ -548,6 +556,7 @@
  14973. } else {
  14974. suspend_stats.success++;
  14975. }
  14976. + pm_in_action = false;
  14977. return error;
  14978. }
  14979. EXPORT_SYMBOL(pm_suspend);
  14980. diff -Nur linux-4.9.28.orig/kernel/printk/printk.c linux-4.9.28/kernel/printk/printk.c
  14981. --- linux-4.9.28.orig/kernel/printk/printk.c 2017-05-14 14:00:37.000000000 +0200
  14982. +++ linux-4.9.28/kernel/printk/printk.c 2017-05-19 03:37:25.190177110 +0200
  14983. @@ -351,6 +351,65 @@
  14984. */
  14985. DEFINE_RAW_SPINLOCK(logbuf_lock);
  14986. +#ifdef CONFIG_EARLY_PRINTK
  14987. +struct console *early_console;
  14988. +
  14989. +static void early_vprintk(const char *fmt, va_list ap)
  14990. +{
  14991. + if (early_console) {
  14992. + char buf[512];
  14993. + int n = vscnprintf(buf, sizeof(buf), fmt, ap);
  14994. +
  14995. + early_console->write(early_console, buf, n);
  14996. + }
  14997. +}
  14998. +
  14999. +asmlinkage void early_printk(const char *fmt, ...)
  15000. +{
  15001. + va_list ap;
  15002. +
  15003. + va_start(ap, fmt);
  15004. + early_vprintk(fmt, ap);
  15005. + va_end(ap);
  15006. +}
  15007. +
  15008. +/*
  15009. + * This is independent of any log levels - a global
  15010. + * kill switch that turns off all of printk.
  15011. + *
  15012. + * Used by the NMI watchdog if early-printk is enabled.
  15013. + */
  15014. +static bool __read_mostly printk_killswitch;
  15015. +
  15016. +static int __init force_early_printk_setup(char *str)
  15017. +{
  15018. + printk_killswitch = true;
  15019. + return 0;
  15020. +}
  15021. +early_param("force_early_printk", force_early_printk_setup);
  15022. +
  15023. +void printk_kill(void)
  15024. +{
  15025. + printk_killswitch = true;
  15026. +}
  15027. +
  15028. +#ifdef CONFIG_PRINTK
  15029. +static int forced_early_printk(const char *fmt, va_list ap)
  15030. +{
  15031. + if (!printk_killswitch)
  15032. + return 0;
  15033. + early_vprintk(fmt, ap);
  15034. + return 1;
  15035. +}
  15036. +#endif
  15037. +
  15038. +#else
  15039. +static inline int forced_early_printk(const char *fmt, va_list ap)
  15040. +{
  15041. + return 0;
  15042. +}
  15043. +#endif
  15044. +
  15045. #ifdef CONFIG_PRINTK
  15046. DECLARE_WAIT_QUEUE_HEAD(log_wait);
  15047. /* the next printk record to read by syslog(READ) or /proc/kmsg */
  15048. @@ -1337,6 +1396,7 @@
  15049. {
  15050. char *text;
  15051. int len = 0;
  15052. + int attempts = 0;
  15053. text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
  15054. if (!text)
  15055. @@ -1348,6 +1408,14 @@
  15056. u64 seq;
  15057. u32 idx;
  15058. enum log_flags prev;
  15059. + int num_msg;
  15060. +try_again:
  15061. + attempts++;
  15062. + if (attempts > 10) {
  15063. + len = -EBUSY;
  15064. + goto out;
  15065. + }
  15066. + num_msg = 0;
  15067. /*
  15068. * Find first record that fits, including all following records,
  15069. @@ -1363,6 +1431,14 @@
  15070. prev = msg->flags;
  15071. idx = log_next(idx);
  15072. seq++;
  15073. + num_msg++;
  15074. + if (num_msg > 5) {
  15075. + num_msg = 0;
  15076. + raw_spin_unlock_irq(&logbuf_lock);
  15077. + raw_spin_lock_irq(&logbuf_lock);
  15078. + if (clear_seq < log_first_seq)
  15079. + goto try_again;
  15080. + }
  15081. }
  15082. /* move first record forward until length fits into the buffer */
  15083. @@ -1376,6 +1452,14 @@
  15084. prev = msg->flags;
  15085. idx = log_next(idx);
  15086. seq++;
  15087. + num_msg++;
  15088. + if (num_msg > 5) {
  15089. + num_msg = 0;
  15090. + raw_spin_unlock_irq(&logbuf_lock);
  15091. + raw_spin_lock_irq(&logbuf_lock);
  15092. + if (clear_seq < log_first_seq)
  15093. + goto try_again;
  15094. + }
  15095. }
  15096. /* last message fitting into this dump */
  15097. @@ -1416,6 +1500,7 @@
  15098. clear_seq = log_next_seq;
  15099. clear_idx = log_next_idx;
  15100. }
  15101. +out:
  15102. raw_spin_unlock_irq(&logbuf_lock);
  15103. kfree(text);
  15104. @@ -1569,6 +1654,12 @@
  15105. if (!console_drivers)
  15106. return;
  15107. + if (IS_ENABLED(CONFIG_PREEMPT_RT_BASE)) {
  15108. + if (in_irq() || in_nmi())
  15109. + return;
  15110. + }
  15111. +
  15112. + migrate_disable();
  15113. for_each_console(con) {
  15114. if (exclusive_console && con != exclusive_console)
  15115. continue;
  15116. @@ -1584,6 +1675,7 @@
  15117. else
  15118. con->write(con, text, len);
  15119. }
  15120. + migrate_enable();
  15121. }
  15122. /*
  15123. @@ -1781,6 +1873,13 @@
  15124. /* cpu currently holding logbuf_lock in this function */
  15125. static unsigned int logbuf_cpu = UINT_MAX;
  15126. + /*
  15127. + * Fall back to early_printk if a debugging subsystem has
  15128. + * killed printk output
  15129. + */
  15130. + if (unlikely(forced_early_printk(fmt, args)))
  15131. + return 1;
  15132. +
  15133. if (level == LOGLEVEL_SCHED) {
  15134. level = LOGLEVEL_DEFAULT;
  15135. in_sched = true;
  15136. @@ -1885,13 +1984,23 @@
  15137. /* If called from the scheduler, we can not call up(). */
  15138. if (!in_sched) {
  15139. + int may_trylock = 1;
  15140. +
  15141. lockdep_off();
  15142. +#ifdef CONFIG_PREEMPT_RT_FULL
  15143. + /*
  15144. + * we can't take a sleeping lock with IRQs or preeption disabled
  15145. + * so we can't print in these contexts
  15146. + */
  15147. + if (!(preempt_count() == 0 && !irqs_disabled()))
  15148. + may_trylock = 0;
  15149. +#endif
  15150. /*
  15151. * Try to acquire and then immediately release the console
  15152. * semaphore. The release will print out buffers and wake up
  15153. * /dev/kmsg and syslog() users.
  15154. */
  15155. - if (console_trylock())
  15156. + if (may_trylock && console_trylock())
  15157. console_unlock();
  15158. lockdep_on();
  15159. }
  15160. @@ -2014,26 +2123,6 @@
  15161. #endif /* CONFIG_PRINTK */
  15162. -#ifdef CONFIG_EARLY_PRINTK
  15163. -struct console *early_console;
  15164. -
  15165. -asmlinkage __visible void early_printk(const char *fmt, ...)
  15166. -{
  15167. - va_list ap;
  15168. - char buf[512];
  15169. - int n;
  15170. -
  15171. - if (!early_console)
  15172. - return;
  15173. -
  15174. - va_start(ap, fmt);
  15175. - n = vscnprintf(buf, sizeof(buf), fmt, ap);
  15176. - va_end(ap);
  15177. -
  15178. - early_console->write(early_console, buf, n);
  15179. -}
  15180. -#endif
  15181. -
  15182. static int __add_preferred_console(char *name, int idx, char *options,
  15183. char *brl_options)
  15184. {
  15185. @@ -2303,11 +2392,16 @@
  15186. goto out;
  15187. len = cont_print_text(text, size);
  15188. +#ifdef CONFIG_PREEMPT_RT_FULL
  15189. + raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  15190. + call_console_drivers(cont.level, NULL, 0, text, len);
  15191. +#else
  15192. raw_spin_unlock(&logbuf_lock);
  15193. stop_critical_timings();
  15194. call_console_drivers(cont.level, NULL, 0, text, len);
  15195. start_critical_timings();
  15196. local_irq_restore(flags);
  15197. +#endif
  15198. return;
  15199. out:
  15200. raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  15201. @@ -2431,13 +2525,17 @@
  15202. console_idx = log_next(console_idx);
  15203. console_seq++;
  15204. console_prev = msg->flags;
  15205. +#ifdef CONFIG_PREEMPT_RT_FULL
  15206. + raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  15207. + call_console_drivers(level, ext_text, ext_len, text, len);
  15208. +#else
  15209. raw_spin_unlock(&logbuf_lock);
  15210. stop_critical_timings(); /* don't trace print latency */
  15211. call_console_drivers(level, ext_text, ext_len, text, len);
  15212. start_critical_timings();
  15213. local_irq_restore(flags);
  15214. -
  15215. +#endif
  15216. if (do_cond_resched)
  15217. cond_resched();
  15218. }
  15219. @@ -2489,6 +2587,11 @@
  15220. {
  15221. struct console *c;
  15222. + if (IS_ENABLED(CONFIG_PREEMPT_RT_BASE)) {
  15223. + if (in_irq() || in_nmi())
  15224. + return;
  15225. + }
  15226. +
  15227. /*
  15228. * console_unblank can no longer be called in interrupt context unless
  15229. * oops_in_progress is set to 1..
  15230. diff -Nur linux-4.9.28.orig/kernel/ptrace.c linux-4.9.28/kernel/ptrace.c
  15231. --- linux-4.9.28.orig/kernel/ptrace.c 2017-05-14 14:00:37.000000000 +0200
  15232. +++ linux-4.9.28/kernel/ptrace.c 2017-05-19 03:37:25.190177110 +0200
  15233. @@ -166,7 +166,14 @@
  15234. spin_lock_irq(&task->sighand->siglock);
  15235. if (task_is_traced(task) && !__fatal_signal_pending(task)) {
  15236. - task->state = __TASK_TRACED;
  15237. + unsigned long flags;
  15238. +
  15239. + raw_spin_lock_irqsave(&task->pi_lock, flags);
  15240. + if (task->state & __TASK_TRACED)
  15241. + task->state = __TASK_TRACED;
  15242. + else
  15243. + task->saved_state = __TASK_TRACED;
  15244. + raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  15245. ret = true;
  15246. }
  15247. spin_unlock_irq(&task->sighand->siglock);
  15248. diff -Nur linux-4.9.28.orig/kernel/rcu/rcutorture.c linux-4.9.28/kernel/rcu/rcutorture.c
  15249. --- linux-4.9.28.orig/kernel/rcu/rcutorture.c 2017-05-14 14:00:37.000000000 +0200
  15250. +++ linux-4.9.28/kernel/rcu/rcutorture.c 2017-05-19 03:37:25.190177110 +0200
  15251. @@ -404,6 +404,7 @@
  15252. .name = "rcu"
  15253. };
  15254. +#ifndef CONFIG_PREEMPT_RT_FULL
  15255. /*
  15256. * Definitions for rcu_bh torture testing.
  15257. */
  15258. @@ -443,6 +444,12 @@
  15259. .name = "rcu_bh"
  15260. };
  15261. +#else
  15262. +static struct rcu_torture_ops rcu_bh_ops = {
  15263. + .ttype = INVALID_RCU_FLAVOR,
  15264. +};
  15265. +#endif
  15266. +
  15267. /*
  15268. * Don't even think about trying any of these in real life!!!
  15269. * The names includes "busted", and they really means it!
  15270. diff -Nur linux-4.9.28.orig/kernel/rcu/tree.c linux-4.9.28/kernel/rcu/tree.c
  15271. --- linux-4.9.28.orig/kernel/rcu/tree.c 2017-05-14 14:00:37.000000000 +0200
  15272. +++ linux-4.9.28/kernel/rcu/tree.c 2017-05-19 03:37:25.190177110 +0200
  15273. @@ -55,6 +55,11 @@
  15274. #include <linux/random.h>
  15275. #include <linux/trace_events.h>
  15276. #include <linux/suspend.h>
  15277. +#include <linux/delay.h>
  15278. +#include <linux/gfp.h>
  15279. +#include <linux/oom.h>
  15280. +#include <linux/smpboot.h>
  15281. +#include "../time/tick-internal.h"
  15282. #include "tree.h"
  15283. #include "rcu.h"
  15284. @@ -260,6 +265,19 @@
  15285. this_cpu_ptr(&rcu_sched_data), true);
  15286. }
  15287. +#ifdef CONFIG_PREEMPT_RT_FULL
  15288. +static void rcu_preempt_qs(void);
  15289. +
  15290. +void rcu_bh_qs(void)
  15291. +{
  15292. + unsigned long flags;
  15293. +
  15294. + /* Callers to this function, rcu_preempt_qs(), must disable irqs. */
  15295. + local_irq_save(flags);
  15296. + rcu_preempt_qs();
  15297. + local_irq_restore(flags);
  15298. +}
  15299. +#else
  15300. void rcu_bh_qs(void)
  15301. {
  15302. if (__this_cpu_read(rcu_bh_data.cpu_no_qs.s)) {
  15303. @@ -269,6 +287,7 @@
  15304. __this_cpu_write(rcu_bh_data.cpu_no_qs.b.norm, false);
  15305. }
  15306. }
  15307. +#endif
  15308. static DEFINE_PER_CPU(int, rcu_sched_qs_mask);
  15309. @@ -449,11 +468,13 @@
  15310. /*
  15311. * Return the number of RCU BH batches started thus far for debug & stats.
  15312. */
  15313. +#ifndef CONFIG_PREEMPT_RT_FULL
  15314. unsigned long rcu_batches_started_bh(void)
  15315. {
  15316. return rcu_bh_state.gpnum;
  15317. }
  15318. EXPORT_SYMBOL_GPL(rcu_batches_started_bh);
  15319. +#endif
  15320. /*
  15321. * Return the number of RCU batches completed thus far for debug & stats.
  15322. @@ -473,6 +494,7 @@
  15323. }
  15324. EXPORT_SYMBOL_GPL(rcu_batches_completed_sched);
  15325. +#ifndef CONFIG_PREEMPT_RT_FULL
  15326. /*
  15327. * Return the number of RCU BH batches completed thus far for debug & stats.
  15328. */
  15329. @@ -481,6 +503,7 @@
  15330. return rcu_bh_state.completed;
  15331. }
  15332. EXPORT_SYMBOL_GPL(rcu_batches_completed_bh);
  15333. +#endif
  15334. /*
  15335. * Return the number of RCU expedited batches completed thus far for
  15336. @@ -504,6 +527,7 @@
  15337. }
  15338. EXPORT_SYMBOL_GPL(rcu_exp_batches_completed_sched);
  15339. +#ifndef CONFIG_PREEMPT_RT_FULL
  15340. /*
  15341. * Force a quiescent state.
  15342. */
  15343. @@ -522,6 +546,13 @@
  15344. }
  15345. EXPORT_SYMBOL_GPL(rcu_bh_force_quiescent_state);
  15346. +#else
  15347. +void rcu_force_quiescent_state(void)
  15348. +{
  15349. +}
  15350. +EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
  15351. +#endif
  15352. +
  15353. /*
  15354. * Force a quiescent state for RCU-sched.
  15355. */
  15356. @@ -572,9 +603,11 @@
  15357. case RCU_FLAVOR:
  15358. rsp = rcu_state_p;
  15359. break;
  15360. +#ifndef CONFIG_PREEMPT_RT_FULL
  15361. case RCU_BH_FLAVOR:
  15362. rsp = &rcu_bh_state;
  15363. break;
  15364. +#endif
  15365. case RCU_SCHED_FLAVOR:
  15366. rsp = &rcu_sched_state;
  15367. break;
  15368. @@ -3016,18 +3049,17 @@
  15369. /*
  15370. * Do RCU core processing for the current CPU.
  15371. */
  15372. -static __latent_entropy void rcu_process_callbacks(struct softirq_action *unused)
  15373. +static __latent_entropy void rcu_process_callbacks(void)
  15374. {
  15375. struct rcu_state *rsp;
  15376. if (cpu_is_offline(smp_processor_id()))
  15377. return;
  15378. - trace_rcu_utilization(TPS("Start RCU core"));
  15379. for_each_rcu_flavor(rsp)
  15380. __rcu_process_callbacks(rsp);
  15381. - trace_rcu_utilization(TPS("End RCU core"));
  15382. }
  15383. +static DEFINE_PER_CPU(struct task_struct *, rcu_cpu_kthread_task);
  15384. /*
  15385. * Schedule RCU callback invocation. If the specified type of RCU
  15386. * does not support RCU priority boosting, just do a direct call,
  15387. @@ -3039,18 +3071,105 @@
  15388. {
  15389. if (unlikely(!READ_ONCE(rcu_scheduler_fully_active)))
  15390. return;
  15391. - if (likely(!rsp->boost)) {
  15392. - rcu_do_batch(rsp, rdp);
  15393. + rcu_do_batch(rsp, rdp);
  15394. +}
  15395. +
  15396. +static void rcu_wake_cond(struct task_struct *t, int status)
  15397. +{
  15398. + /*
  15399. + * If the thread is yielding, only wake it when this
  15400. + * is invoked from idle
  15401. + */
  15402. + if (t && (status != RCU_KTHREAD_YIELDING || is_idle_task(current)))
  15403. + wake_up_process(t);
  15404. +}
  15405. +
  15406. +/*
  15407. + * Wake up this CPU's rcuc kthread to do RCU core processing.
  15408. + */
  15409. +static void invoke_rcu_core(void)
  15410. +{
  15411. + unsigned long flags;
  15412. + struct task_struct *t;
  15413. +
  15414. + if (!cpu_online(smp_processor_id()))
  15415. return;
  15416. + local_irq_save(flags);
  15417. + __this_cpu_write(rcu_cpu_has_work, 1);
  15418. + t = __this_cpu_read(rcu_cpu_kthread_task);
  15419. + if (t != NULL && current != t)
  15420. + rcu_wake_cond(t, __this_cpu_read(rcu_cpu_kthread_status));
  15421. + local_irq_restore(flags);
  15422. +}
  15423. +
  15424. +static void rcu_cpu_kthread_park(unsigned int cpu)
  15425. +{
  15426. + per_cpu(rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
  15427. +}
  15428. +
  15429. +static int rcu_cpu_kthread_should_run(unsigned int cpu)
  15430. +{
  15431. + return __this_cpu_read(rcu_cpu_has_work);
  15432. +}
  15433. +
  15434. +/*
  15435. + * Per-CPU kernel thread that invokes RCU callbacks. This replaces the
  15436. + * RCU softirq used in flavors and configurations of RCU that do not
  15437. + * support RCU priority boosting.
  15438. + */
  15439. +static void rcu_cpu_kthread(unsigned int cpu)
  15440. +{
  15441. + unsigned int *statusp = this_cpu_ptr(&rcu_cpu_kthread_status);
  15442. + char work, *workp = this_cpu_ptr(&rcu_cpu_has_work);
  15443. + int spincnt;
  15444. +
  15445. + for (spincnt = 0; spincnt < 10; spincnt++) {
  15446. + trace_rcu_utilization(TPS("Start CPU kthread@rcu_wait"));
  15447. + local_bh_disable();
  15448. + *statusp = RCU_KTHREAD_RUNNING;
  15449. + this_cpu_inc(rcu_cpu_kthread_loops);
  15450. + local_irq_disable();
  15451. + work = *workp;
  15452. + *workp = 0;
  15453. + local_irq_enable();
  15454. + if (work)
  15455. + rcu_process_callbacks();
  15456. + local_bh_enable();
  15457. + if (*workp == 0) {
  15458. + trace_rcu_utilization(TPS("End CPU kthread@rcu_wait"));
  15459. + *statusp = RCU_KTHREAD_WAITING;
  15460. + return;
  15461. + }
  15462. }
  15463. - invoke_rcu_callbacks_kthread();
  15464. + *statusp = RCU_KTHREAD_YIELDING;
  15465. + trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield"));
  15466. + schedule_timeout_interruptible(2);
  15467. + trace_rcu_utilization(TPS("End CPU kthread@rcu_yield"));
  15468. + *statusp = RCU_KTHREAD_WAITING;
  15469. }
  15470. -static void invoke_rcu_core(void)
  15471. +static struct smp_hotplug_thread rcu_cpu_thread_spec = {
  15472. + .store = &rcu_cpu_kthread_task,
  15473. + .thread_should_run = rcu_cpu_kthread_should_run,
  15474. + .thread_fn = rcu_cpu_kthread,
  15475. + .thread_comm = "rcuc/%u",
  15476. + .setup = rcu_cpu_kthread_setup,
  15477. + .park = rcu_cpu_kthread_park,
  15478. +};
  15479. +
  15480. +/*
  15481. + * Spawn per-CPU RCU core processing kthreads.
  15482. + */
  15483. +static int __init rcu_spawn_core_kthreads(void)
  15484. {
  15485. - if (cpu_online(smp_processor_id()))
  15486. - raise_softirq(RCU_SOFTIRQ);
  15487. + int cpu;
  15488. +
  15489. + for_each_possible_cpu(cpu)
  15490. + per_cpu(rcu_cpu_has_work, cpu) = 0;
  15491. + BUG_ON(smpboot_register_percpu_thread(&rcu_cpu_thread_spec));
  15492. + return 0;
  15493. }
  15494. +early_initcall(rcu_spawn_core_kthreads);
  15495. /*
  15496. * Handle any core-RCU processing required by a call_rcu() invocation.
  15497. @@ -3195,6 +3314,7 @@
  15498. }
  15499. EXPORT_SYMBOL_GPL(call_rcu_sched);
  15500. +#ifndef CONFIG_PREEMPT_RT_FULL
  15501. /*
  15502. * Queue an RCU callback for invocation after a quicker grace period.
  15503. */
  15504. @@ -3203,6 +3323,7 @@
  15505. __call_rcu(head, func, &rcu_bh_state, -1, 0);
  15506. }
  15507. EXPORT_SYMBOL_GPL(call_rcu_bh);
  15508. +#endif
  15509. /*
  15510. * Queue an RCU callback for lazy invocation after a grace period.
  15511. @@ -3294,6 +3415,7 @@
  15512. }
  15513. EXPORT_SYMBOL_GPL(synchronize_sched);
  15514. +#ifndef CONFIG_PREEMPT_RT_FULL
  15515. /**
  15516. * synchronize_rcu_bh - wait until an rcu_bh grace period has elapsed.
  15517. *
  15518. @@ -3320,6 +3442,7 @@
  15519. wait_rcu_gp(call_rcu_bh);
  15520. }
  15521. EXPORT_SYMBOL_GPL(synchronize_rcu_bh);
  15522. +#endif
  15523. /**
  15524. * get_state_synchronize_rcu - Snapshot current RCU state
  15525. @@ -3698,6 +3821,7 @@
  15526. mutex_unlock(&rsp->barrier_mutex);
  15527. }
  15528. +#ifndef CONFIG_PREEMPT_RT_FULL
  15529. /**
  15530. * rcu_barrier_bh - Wait until all in-flight call_rcu_bh() callbacks complete.
  15531. */
  15532. @@ -3706,6 +3830,7 @@
  15533. _rcu_barrier(&rcu_bh_state);
  15534. }
  15535. EXPORT_SYMBOL_GPL(rcu_barrier_bh);
  15536. +#endif
  15537. /**
  15538. * rcu_barrier_sched - Wait for in-flight call_rcu_sched() callbacks.
  15539. @@ -4227,12 +4352,13 @@
  15540. rcu_bootup_announce();
  15541. rcu_init_geometry();
  15542. +#ifndef CONFIG_PREEMPT_RT_FULL
  15543. rcu_init_one(&rcu_bh_state);
  15544. +#endif
  15545. rcu_init_one(&rcu_sched_state);
  15546. if (dump_tree)
  15547. rcu_dump_rcu_node_tree(&rcu_sched_state);
  15548. __rcu_init_preempt();
  15549. - open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
  15550. /*
  15551. * We don't need protection against CPU-hotplug here because
  15552. diff -Nur linux-4.9.28.orig/kernel/rcu/tree.h linux-4.9.28/kernel/rcu/tree.h
  15553. --- linux-4.9.28.orig/kernel/rcu/tree.h 2017-05-14 14:00:37.000000000 +0200
  15554. +++ linux-4.9.28/kernel/rcu/tree.h 2017-05-19 03:37:25.190177110 +0200
  15555. @@ -588,18 +588,18 @@
  15556. */
  15557. extern struct rcu_state rcu_sched_state;
  15558. +#ifndef CONFIG_PREEMPT_RT_FULL
  15559. extern struct rcu_state rcu_bh_state;
  15560. +#endif
  15561. #ifdef CONFIG_PREEMPT_RCU
  15562. extern struct rcu_state rcu_preempt_state;
  15563. #endif /* #ifdef CONFIG_PREEMPT_RCU */
  15564. -#ifdef CONFIG_RCU_BOOST
  15565. DECLARE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
  15566. DECLARE_PER_CPU(int, rcu_cpu_kthread_cpu);
  15567. DECLARE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
  15568. DECLARE_PER_CPU(char, rcu_cpu_has_work);
  15569. -#endif /* #ifdef CONFIG_RCU_BOOST */
  15570. #ifndef RCU_TREE_NONCORE
  15571. @@ -619,10 +619,9 @@
  15572. static void __init __rcu_init_preempt(void);
  15573. static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags);
  15574. static void rcu_preempt_boost_start_gp(struct rcu_node *rnp);
  15575. -static void invoke_rcu_callbacks_kthread(void);
  15576. static bool rcu_is_callbacks_kthread(void);
  15577. +static void rcu_cpu_kthread_setup(unsigned int cpu);
  15578. #ifdef CONFIG_RCU_BOOST
  15579. -static void rcu_preempt_do_callbacks(void);
  15580. static int rcu_spawn_one_boost_kthread(struct rcu_state *rsp,
  15581. struct rcu_node *rnp);
  15582. #endif /* #ifdef CONFIG_RCU_BOOST */
  15583. diff -Nur linux-4.9.28.orig/kernel/rcu/tree_plugin.h linux-4.9.28/kernel/rcu/tree_plugin.h
  15584. --- linux-4.9.28.orig/kernel/rcu/tree_plugin.h 2017-05-14 14:00:37.000000000 +0200
  15585. +++ linux-4.9.28/kernel/rcu/tree_plugin.h 2017-05-19 03:37:25.190177110 +0200
  15586. @@ -24,25 +24,10 @@
  15587. * Paul E. McKenney <paulmck@linux.vnet.ibm.com>
  15588. */
  15589. -#include <linux/delay.h>
  15590. -#include <linux/gfp.h>
  15591. -#include <linux/oom.h>
  15592. -#include <linux/smpboot.h>
  15593. -#include "../time/tick-internal.h"
  15594. -
  15595. #ifdef CONFIG_RCU_BOOST
  15596. #include "../locking/rtmutex_common.h"
  15597. -/*
  15598. - * Control variables for per-CPU and per-rcu_node kthreads. These
  15599. - * handle all flavors of RCU.
  15600. - */
  15601. -static DEFINE_PER_CPU(struct task_struct *, rcu_cpu_kthread_task);
  15602. -DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
  15603. -DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
  15604. -DEFINE_PER_CPU(char, rcu_cpu_has_work);
  15605. -
  15606. #else /* #ifdef CONFIG_RCU_BOOST */
  15607. /*
  15608. @@ -55,6 +40,14 @@
  15609. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  15610. +/*
  15611. + * Control variables for per-CPU and per-rcu_node kthreads. These
  15612. + * handle all flavors of RCU.
  15613. + */
  15614. +DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
  15615. +DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
  15616. +DEFINE_PER_CPU(char, rcu_cpu_has_work);
  15617. +
  15618. #ifdef CONFIG_RCU_NOCB_CPU
  15619. static cpumask_var_t rcu_nocb_mask; /* CPUs to have callbacks offloaded. */
  15620. static bool have_rcu_nocb_mask; /* Was rcu_nocb_mask allocated? */
  15621. @@ -426,7 +419,7 @@
  15622. }
  15623. /* Hardware IRQ handlers cannot block, complain if they get here. */
  15624. - if (in_irq() || in_serving_softirq()) {
  15625. + if (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_OFFSET)) {
  15626. lockdep_rcu_suspicious(__FILE__, __LINE__,
  15627. "rcu_read_unlock() from irq or softirq with blocking in critical section!!!\n");
  15628. pr_alert("->rcu_read_unlock_special: %#x (b: %d, enq: %d nq: %d)\n",
  15629. @@ -632,15 +625,6 @@
  15630. t->rcu_read_unlock_special.b.need_qs = true;
  15631. }
  15632. -#ifdef CONFIG_RCU_BOOST
  15633. -
  15634. -static void rcu_preempt_do_callbacks(void)
  15635. -{
  15636. - rcu_do_batch(rcu_state_p, this_cpu_ptr(rcu_data_p));
  15637. -}
  15638. -
  15639. -#endif /* #ifdef CONFIG_RCU_BOOST */
  15640. -
  15641. /*
  15642. * Queue a preemptible-RCU callback for invocation after a grace period.
  15643. */
  15644. @@ -829,6 +813,19 @@
  15645. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  15646. +/*
  15647. + * If boosting, set rcuc kthreads to realtime priority.
  15648. + */
  15649. +static void rcu_cpu_kthread_setup(unsigned int cpu)
  15650. +{
  15651. +#ifdef CONFIG_RCU_BOOST
  15652. + struct sched_param sp;
  15653. +
  15654. + sp.sched_priority = kthread_prio;
  15655. + sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
  15656. +#endif /* #ifdef CONFIG_RCU_BOOST */
  15657. +}
  15658. +
  15659. #ifdef CONFIG_RCU_BOOST
  15660. #include "../locking/rtmutex_common.h"
  15661. @@ -860,16 +857,6 @@
  15662. #endif /* #else #ifdef CONFIG_RCU_TRACE */
  15663. -static void rcu_wake_cond(struct task_struct *t, int status)
  15664. -{
  15665. - /*
  15666. - * If the thread is yielding, only wake it when this
  15667. - * is invoked from idle
  15668. - */
  15669. - if (status != RCU_KTHREAD_YIELDING || is_idle_task(current))
  15670. - wake_up_process(t);
  15671. -}
  15672. -
  15673. /*
  15674. * Carry out RCU priority boosting on the task indicated by ->exp_tasks
  15675. * or ->boost_tasks, advancing the pointer to the next task in the
  15676. @@ -1013,23 +1000,6 @@
  15677. }
  15678. /*
  15679. - * Wake up the per-CPU kthread to invoke RCU callbacks.
  15680. - */
  15681. -static void invoke_rcu_callbacks_kthread(void)
  15682. -{
  15683. - unsigned long flags;
  15684. -
  15685. - local_irq_save(flags);
  15686. - __this_cpu_write(rcu_cpu_has_work, 1);
  15687. - if (__this_cpu_read(rcu_cpu_kthread_task) != NULL &&
  15688. - current != __this_cpu_read(rcu_cpu_kthread_task)) {
  15689. - rcu_wake_cond(__this_cpu_read(rcu_cpu_kthread_task),
  15690. - __this_cpu_read(rcu_cpu_kthread_status));
  15691. - }
  15692. - local_irq_restore(flags);
  15693. -}
  15694. -
  15695. -/*
  15696. * Is the current CPU running the RCU-callbacks kthread?
  15697. * Caller must have preemption disabled.
  15698. */
  15699. @@ -1083,67 +1053,6 @@
  15700. return 0;
  15701. }
  15702. -static void rcu_kthread_do_work(void)
  15703. -{
  15704. - rcu_do_batch(&rcu_sched_state, this_cpu_ptr(&rcu_sched_data));
  15705. - rcu_do_batch(&rcu_bh_state, this_cpu_ptr(&rcu_bh_data));
  15706. - rcu_preempt_do_callbacks();
  15707. -}
  15708. -
  15709. -static void rcu_cpu_kthread_setup(unsigned int cpu)
  15710. -{
  15711. - struct sched_param sp;
  15712. -
  15713. - sp.sched_priority = kthread_prio;
  15714. - sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
  15715. -}
  15716. -
  15717. -static void rcu_cpu_kthread_park(unsigned int cpu)
  15718. -{
  15719. - per_cpu(rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
  15720. -}
  15721. -
  15722. -static int rcu_cpu_kthread_should_run(unsigned int cpu)
  15723. -{
  15724. - return __this_cpu_read(rcu_cpu_has_work);
  15725. -}
  15726. -
  15727. -/*
  15728. - * Per-CPU kernel thread that invokes RCU callbacks. This replaces the
  15729. - * RCU softirq used in flavors and configurations of RCU that do not
  15730. - * support RCU priority boosting.
  15731. - */
  15732. -static void rcu_cpu_kthread(unsigned int cpu)
  15733. -{
  15734. - unsigned int *statusp = this_cpu_ptr(&rcu_cpu_kthread_status);
  15735. - char work, *workp = this_cpu_ptr(&rcu_cpu_has_work);
  15736. - int spincnt;
  15737. -
  15738. - for (spincnt = 0; spincnt < 10; spincnt++) {
  15739. - trace_rcu_utilization(TPS("Start CPU kthread@rcu_wait"));
  15740. - local_bh_disable();
  15741. - *statusp = RCU_KTHREAD_RUNNING;
  15742. - this_cpu_inc(rcu_cpu_kthread_loops);
  15743. - local_irq_disable();
  15744. - work = *workp;
  15745. - *workp = 0;
  15746. - local_irq_enable();
  15747. - if (work)
  15748. - rcu_kthread_do_work();
  15749. - local_bh_enable();
  15750. - if (*workp == 0) {
  15751. - trace_rcu_utilization(TPS("End CPU kthread@rcu_wait"));
  15752. - *statusp = RCU_KTHREAD_WAITING;
  15753. - return;
  15754. - }
  15755. - }
  15756. - *statusp = RCU_KTHREAD_YIELDING;
  15757. - trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield"));
  15758. - schedule_timeout_interruptible(2);
  15759. - trace_rcu_utilization(TPS("End CPU kthread@rcu_yield"));
  15760. - *statusp = RCU_KTHREAD_WAITING;
  15761. -}
  15762. -
  15763. /*
  15764. * Set the per-rcu_node kthread's affinity to cover all CPUs that are
  15765. * served by the rcu_node in question. The CPU hotplug lock is still
  15766. @@ -1174,26 +1083,12 @@
  15767. free_cpumask_var(cm);
  15768. }
  15769. -static struct smp_hotplug_thread rcu_cpu_thread_spec = {
  15770. - .store = &rcu_cpu_kthread_task,
  15771. - .thread_should_run = rcu_cpu_kthread_should_run,
  15772. - .thread_fn = rcu_cpu_kthread,
  15773. - .thread_comm = "rcuc/%u",
  15774. - .setup = rcu_cpu_kthread_setup,
  15775. - .park = rcu_cpu_kthread_park,
  15776. -};
  15777. -
  15778. /*
  15779. * Spawn boost kthreads -- called as soon as the scheduler is running.
  15780. */
  15781. static void __init rcu_spawn_boost_kthreads(void)
  15782. {
  15783. struct rcu_node *rnp;
  15784. - int cpu;
  15785. -
  15786. - for_each_possible_cpu(cpu)
  15787. - per_cpu(rcu_cpu_has_work, cpu) = 0;
  15788. - BUG_ON(smpboot_register_percpu_thread(&rcu_cpu_thread_spec));
  15789. rcu_for_each_leaf_node(rcu_state_p, rnp)
  15790. (void)rcu_spawn_one_boost_kthread(rcu_state_p, rnp);
  15791. }
  15792. @@ -1216,11 +1111,6 @@
  15793. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  15794. }
  15795. -static void invoke_rcu_callbacks_kthread(void)
  15796. -{
  15797. - WARN_ON_ONCE(1);
  15798. -}
  15799. -
  15800. static bool rcu_is_callbacks_kthread(void)
  15801. {
  15802. return false;
  15803. @@ -1244,7 +1134,7 @@
  15804. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  15805. -#if !defined(CONFIG_RCU_FAST_NO_HZ)
  15806. +#if !defined(CONFIG_RCU_FAST_NO_HZ) || defined(CONFIG_PREEMPT_RT_FULL)
  15807. /*
  15808. * Check to see if any future RCU-related work will need to be done
  15809. @@ -1261,7 +1151,9 @@
  15810. return IS_ENABLED(CONFIG_RCU_NOCB_CPU_ALL)
  15811. ? 0 : rcu_cpu_has_callbacks(NULL);
  15812. }
  15813. +#endif /* !defined(CONFIG_RCU_FAST_NO_HZ) || defined(CONFIG_PREEMPT_RT_FULL) */
  15814. +#if !defined(CONFIG_RCU_FAST_NO_HZ)
  15815. /*
  15816. * Because we do not have RCU_FAST_NO_HZ, don't bother cleaning up
  15817. * after it.
  15818. @@ -1357,6 +1249,8 @@
  15819. return cbs_ready;
  15820. }
  15821. +#ifndef CONFIG_PREEMPT_RT_FULL
  15822. +
  15823. /*
  15824. * Allow the CPU to enter dyntick-idle mode unless it has callbacks ready
  15825. * to invoke. If the CPU has callbacks, try to advance them. Tell the
  15826. @@ -1402,6 +1296,7 @@
  15827. *nextevt = basemono + dj * TICK_NSEC;
  15828. return 0;
  15829. }
  15830. +#endif /* #ifndef CONFIG_PREEMPT_RT_FULL */
  15831. /*
  15832. * Prepare a CPU for idle from an RCU perspective. The first major task
  15833. diff -Nur linux-4.9.28.orig/kernel/rcu/update.c linux-4.9.28/kernel/rcu/update.c
  15834. --- linux-4.9.28.orig/kernel/rcu/update.c 2017-05-14 14:00:37.000000000 +0200
  15835. +++ linux-4.9.28/kernel/rcu/update.c 2017-05-19 03:37:25.190177110 +0200
  15836. @@ -62,7 +62,7 @@
  15837. #ifndef CONFIG_TINY_RCU
  15838. module_param(rcu_expedited, int, 0);
  15839. module_param(rcu_normal, int, 0);
  15840. -static int rcu_normal_after_boot;
  15841. +static int rcu_normal_after_boot = IS_ENABLED(CONFIG_PREEMPT_RT_FULL);
  15842. module_param(rcu_normal_after_boot, int, 0);
  15843. #endif /* #ifndef CONFIG_TINY_RCU */
  15844. @@ -132,8 +132,7 @@
  15845. }
  15846. EXPORT_SYMBOL_GPL(rcu_gp_is_normal);
  15847. -static atomic_t rcu_expedited_nesting =
  15848. - ATOMIC_INIT(IS_ENABLED(CONFIG_RCU_EXPEDITE_BOOT) ? 1 : 0);
  15849. +static atomic_t rcu_expedited_nesting = ATOMIC_INIT(1);
  15850. /*
  15851. * Should normal grace-period primitives be expedited? Intended for
  15852. @@ -182,8 +181,7 @@
  15853. */
  15854. void rcu_end_inkernel_boot(void)
  15855. {
  15856. - if (IS_ENABLED(CONFIG_RCU_EXPEDITE_BOOT))
  15857. - rcu_unexpedite_gp();
  15858. + rcu_unexpedite_gp();
  15859. if (rcu_normal_after_boot)
  15860. WRITE_ONCE(rcu_normal, 1);
  15861. }
  15862. @@ -298,6 +296,7 @@
  15863. }
  15864. EXPORT_SYMBOL_GPL(rcu_read_lock_held);
  15865. +#ifndef CONFIG_PREEMPT_RT_FULL
  15866. /**
  15867. * rcu_read_lock_bh_held() - might we be in RCU-bh read-side critical section?
  15868. *
  15869. @@ -324,6 +323,7 @@
  15870. return in_softirq() || irqs_disabled();
  15871. }
  15872. EXPORT_SYMBOL_GPL(rcu_read_lock_bh_held);
  15873. +#endif
  15874. #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  15875. diff -Nur linux-4.9.28.orig/kernel/sched/completion.c linux-4.9.28/kernel/sched/completion.c
  15876. --- linux-4.9.28.orig/kernel/sched/completion.c 2017-05-14 14:00:37.000000000 +0200
  15877. +++ linux-4.9.28/kernel/sched/completion.c 2017-05-19 03:37:25.194177264 +0200
  15878. @@ -30,10 +30,10 @@
  15879. {
  15880. unsigned long flags;
  15881. - spin_lock_irqsave(&x->wait.lock, flags);
  15882. + raw_spin_lock_irqsave(&x->wait.lock, flags);
  15883. x->done++;
  15884. - __wake_up_locked(&x->wait, TASK_NORMAL, 1);
  15885. - spin_unlock_irqrestore(&x->wait.lock, flags);
  15886. + swake_up_locked(&x->wait);
  15887. + raw_spin_unlock_irqrestore(&x->wait.lock, flags);
  15888. }
  15889. EXPORT_SYMBOL(complete);
  15890. @@ -50,10 +50,10 @@
  15891. {
  15892. unsigned long flags;
  15893. - spin_lock_irqsave(&x->wait.lock, flags);
  15894. + raw_spin_lock_irqsave(&x->wait.lock, flags);
  15895. x->done += UINT_MAX/2;
  15896. - __wake_up_locked(&x->wait, TASK_NORMAL, 0);
  15897. - spin_unlock_irqrestore(&x->wait.lock, flags);
  15898. + swake_up_all_locked(&x->wait);
  15899. + raw_spin_unlock_irqrestore(&x->wait.lock, flags);
  15900. }
  15901. EXPORT_SYMBOL(complete_all);
  15902. @@ -62,20 +62,20 @@
  15903. long (*action)(long), long timeout, int state)
  15904. {
  15905. if (!x->done) {
  15906. - DECLARE_WAITQUEUE(wait, current);
  15907. + DECLARE_SWAITQUEUE(wait);
  15908. - __add_wait_queue_tail_exclusive(&x->wait, &wait);
  15909. + __prepare_to_swait(&x->wait, &wait);
  15910. do {
  15911. if (signal_pending_state(state, current)) {
  15912. timeout = -ERESTARTSYS;
  15913. break;
  15914. }
  15915. __set_current_state(state);
  15916. - spin_unlock_irq(&x->wait.lock);
  15917. + raw_spin_unlock_irq(&x->wait.lock);
  15918. timeout = action(timeout);
  15919. - spin_lock_irq(&x->wait.lock);
  15920. + raw_spin_lock_irq(&x->wait.lock);
  15921. } while (!x->done && timeout);
  15922. - __remove_wait_queue(&x->wait, &wait);
  15923. + __finish_swait(&x->wait, &wait);
  15924. if (!x->done)
  15925. return timeout;
  15926. }
  15927. @@ -89,9 +89,9 @@
  15928. {
  15929. might_sleep();
  15930. - spin_lock_irq(&x->wait.lock);
  15931. + raw_spin_lock_irq(&x->wait.lock);
  15932. timeout = do_wait_for_common(x, action, timeout, state);
  15933. - spin_unlock_irq(&x->wait.lock);
  15934. + raw_spin_unlock_irq(&x->wait.lock);
  15935. return timeout;
  15936. }
  15937. @@ -277,12 +277,12 @@
  15938. if (!READ_ONCE(x->done))
  15939. return 0;
  15940. - spin_lock_irqsave(&x->wait.lock, flags);
  15941. + raw_spin_lock_irqsave(&x->wait.lock, flags);
  15942. if (!x->done)
  15943. ret = 0;
  15944. else
  15945. x->done--;
  15946. - spin_unlock_irqrestore(&x->wait.lock, flags);
  15947. + raw_spin_unlock_irqrestore(&x->wait.lock, flags);
  15948. return ret;
  15949. }
  15950. EXPORT_SYMBOL(try_wait_for_completion);
  15951. @@ -311,7 +311,7 @@
  15952. * after it's acquired the lock.
  15953. */
  15954. smp_rmb();
  15955. - spin_unlock_wait(&x->wait.lock);
  15956. + raw_spin_unlock_wait(&x->wait.lock);
  15957. return true;
  15958. }
  15959. EXPORT_SYMBOL(completion_done);
  15960. diff -Nur linux-4.9.28.orig/kernel/sched/core.c linux-4.9.28/kernel/sched/core.c
  15961. --- linux-4.9.28.orig/kernel/sched/core.c 2017-05-14 14:00:37.000000000 +0200
  15962. +++ linux-4.9.28/kernel/sched/core.c 2017-05-19 03:37:25.194177264 +0200
  15963. @@ -129,7 +129,11 @@
  15964. * Number of tasks to iterate in a single balance run.
  15965. * Limited because this is done with IRQs disabled.
  15966. */
  15967. +#ifndef CONFIG_PREEMPT_RT_FULL
  15968. const_debug unsigned int sysctl_sched_nr_migrate = 32;
  15969. +#else
  15970. +const_debug unsigned int sysctl_sched_nr_migrate = 8;
  15971. +#endif
  15972. /*
  15973. * period over which we average the RT time consumption, measured
  15974. @@ -345,6 +349,7 @@
  15975. hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  15976. rq->hrtick_timer.function = hrtick;
  15977. + rq->hrtick_timer.irqsafe = 1;
  15978. }
  15979. #else /* CONFIG_SCHED_HRTICK */
  15980. static inline void hrtick_clear(struct rq *rq)
  15981. @@ -449,7 +454,7 @@
  15982. head->lastp = &node->next;
  15983. }
  15984. -void wake_up_q(struct wake_q_head *head)
  15985. +void __wake_up_q(struct wake_q_head *head, bool sleeper)
  15986. {
  15987. struct wake_q_node *node = head->first;
  15988. @@ -466,7 +471,10 @@
  15989. * wake_up_process() implies a wmb() to pair with the queueing
  15990. * in wake_q_add() so as not to miss wakeups.
  15991. */
  15992. - wake_up_process(task);
  15993. + if (sleeper)
  15994. + wake_up_lock_sleeper(task);
  15995. + else
  15996. + wake_up_process(task);
  15997. put_task_struct(task);
  15998. }
  15999. }
  16000. @@ -502,6 +510,38 @@
  16001. trace_sched_wake_idle_without_ipi(cpu);
  16002. }
  16003. +#ifdef CONFIG_PREEMPT_LAZY
  16004. +void resched_curr_lazy(struct rq *rq)
  16005. +{
  16006. + struct task_struct *curr = rq->curr;
  16007. + int cpu;
  16008. +
  16009. + if (!sched_feat(PREEMPT_LAZY)) {
  16010. + resched_curr(rq);
  16011. + return;
  16012. + }
  16013. +
  16014. + lockdep_assert_held(&rq->lock);
  16015. +
  16016. + if (test_tsk_need_resched(curr))
  16017. + return;
  16018. +
  16019. + if (test_tsk_need_resched_lazy(curr))
  16020. + return;
  16021. +
  16022. + set_tsk_need_resched_lazy(curr);
  16023. +
  16024. + cpu = cpu_of(rq);
  16025. + if (cpu == smp_processor_id())
  16026. + return;
  16027. +
  16028. + /* NEED_RESCHED_LAZY must be visible before we test polling */
  16029. + smp_mb();
  16030. + if (!tsk_is_polling(curr))
  16031. + smp_send_reschedule(cpu);
  16032. +}
  16033. +#endif
  16034. +
  16035. void resched_cpu(int cpu)
  16036. {
  16037. struct rq *rq = cpu_rq(cpu);
  16038. @@ -525,11 +565,14 @@
  16039. */
  16040. int get_nohz_timer_target(void)
  16041. {
  16042. - int i, cpu = smp_processor_id();
  16043. + int i, cpu;
  16044. struct sched_domain *sd;
  16045. + preempt_disable_rt();
  16046. + cpu = smp_processor_id();
  16047. +
  16048. if (!idle_cpu(cpu) && is_housekeeping_cpu(cpu))
  16049. - return cpu;
  16050. + goto preempt_en_rt;
  16051. rcu_read_lock();
  16052. for_each_domain(cpu, sd) {
  16053. @@ -548,6 +591,8 @@
  16054. cpu = housekeeping_any_cpu();
  16055. unlock:
  16056. rcu_read_unlock();
  16057. +preempt_en_rt:
  16058. + preempt_enable_rt();
  16059. return cpu;
  16060. }
  16061. /*
  16062. @@ -1100,6 +1145,11 @@
  16063. lockdep_assert_held(&p->pi_lock);
  16064. + if (__migrate_disabled(p)) {
  16065. + cpumask_copy(&p->cpus_allowed, new_mask);
  16066. + return;
  16067. + }
  16068. +
  16069. queued = task_on_rq_queued(p);
  16070. running = task_current(rq, p);
  16071. @@ -1122,6 +1172,84 @@
  16072. set_curr_task(rq, p);
  16073. }
  16074. +static DEFINE_PER_CPU(struct cpumask, sched_cpumasks);
  16075. +static DEFINE_MUTEX(sched_down_mutex);
  16076. +static cpumask_t sched_down_cpumask;
  16077. +
  16078. +void tell_sched_cpu_down_begin(int cpu)
  16079. +{
  16080. + mutex_lock(&sched_down_mutex);
  16081. + cpumask_set_cpu(cpu, &sched_down_cpumask);
  16082. + mutex_unlock(&sched_down_mutex);
  16083. +}
  16084. +
  16085. +void tell_sched_cpu_down_done(int cpu)
  16086. +{
  16087. + mutex_lock(&sched_down_mutex);
  16088. + cpumask_clear_cpu(cpu, &sched_down_cpumask);
  16089. + mutex_unlock(&sched_down_mutex);
  16090. +}
  16091. +
  16092. +/**
  16093. + * migrate_me - try to move the current task off this cpu
  16094. + *
  16095. + * Used by the pin_current_cpu() code to try to get tasks
  16096. + * to move off the current CPU as it is going down.
  16097. + * It will only move the task if the task isn't pinned to
  16098. + * the CPU (with migrate_disable, affinity or NO_SETAFFINITY)
  16099. + * and the task has to be in a RUNNING state. Otherwise the
  16100. + * movement of the task will wake it up (change its state
  16101. + * to running) when the task did not expect it.
  16102. + *
  16103. + * Returns 1 if it succeeded in moving the current task
  16104. + * 0 otherwise.
  16105. + */
  16106. +int migrate_me(void)
  16107. +{
  16108. + struct task_struct *p = current;
  16109. + struct migration_arg arg;
  16110. + struct cpumask *cpumask;
  16111. + struct cpumask *mask;
  16112. + unsigned int dest_cpu;
  16113. + struct rq_flags rf;
  16114. + struct rq *rq;
  16115. +
  16116. + /*
  16117. + * We can not migrate tasks bounded to a CPU or tasks not
  16118. + * running. The movement of the task will wake it up.
  16119. + */
  16120. + if (p->flags & PF_NO_SETAFFINITY || p->state)
  16121. + return 0;
  16122. +
  16123. + mutex_lock(&sched_down_mutex);
  16124. + rq = task_rq_lock(p, &rf);
  16125. +
  16126. + cpumask = this_cpu_ptr(&sched_cpumasks);
  16127. + mask = &p->cpus_allowed;
  16128. +
  16129. + cpumask_andnot(cpumask, mask, &sched_down_cpumask);
  16130. +
  16131. + if (!cpumask_weight(cpumask)) {
  16132. + /* It's only on this CPU? */
  16133. + task_rq_unlock(rq, p, &rf);
  16134. + mutex_unlock(&sched_down_mutex);
  16135. + return 0;
  16136. + }
  16137. +
  16138. + dest_cpu = cpumask_any_and(cpu_active_mask, cpumask);
  16139. +
  16140. + arg.task = p;
  16141. + arg.dest_cpu = dest_cpu;
  16142. +
  16143. + task_rq_unlock(rq, p, &rf);
  16144. +
  16145. + stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
  16146. + tlb_migrate_finish(p->mm);
  16147. + mutex_unlock(&sched_down_mutex);
  16148. +
  16149. + return 1;
  16150. +}
  16151. +
  16152. /*
  16153. * Change a given task's CPU affinity. Migrate the thread to a
  16154. * proper CPU and schedule it away if the CPU it's executing on
  16155. @@ -1179,7 +1307,7 @@
  16156. }
  16157. /* Can the task run on the task's current CPU? If so, we're done */
  16158. - if (cpumask_test_cpu(task_cpu(p), new_mask))
  16159. + if (cpumask_test_cpu(task_cpu(p), new_mask) || __migrate_disabled(p))
  16160. goto out;
  16161. dest_cpu = cpumask_any_and(cpu_valid_mask, new_mask);
  16162. @@ -1366,6 +1494,18 @@
  16163. return ret;
  16164. }
  16165. +static bool check_task_state(struct task_struct *p, long match_state)
  16166. +{
  16167. + bool match = false;
  16168. +
  16169. + raw_spin_lock_irq(&p->pi_lock);
  16170. + if (p->state == match_state || p->saved_state == match_state)
  16171. + match = true;
  16172. + raw_spin_unlock_irq(&p->pi_lock);
  16173. +
  16174. + return match;
  16175. +}
  16176. +
  16177. /*
  16178. * wait_task_inactive - wait for a thread to unschedule.
  16179. *
  16180. @@ -1410,7 +1550,7 @@
  16181. * is actually now running somewhere else!
  16182. */
  16183. while (task_running(rq, p)) {
  16184. - if (match_state && unlikely(p->state != match_state))
  16185. + if (match_state && !check_task_state(p, match_state))
  16186. return 0;
  16187. cpu_relax();
  16188. }
  16189. @@ -1425,7 +1565,8 @@
  16190. running = task_running(rq, p);
  16191. queued = task_on_rq_queued(p);
  16192. ncsw = 0;
  16193. - if (!match_state || p->state == match_state)
  16194. + if (!match_state || p->state == match_state ||
  16195. + p->saved_state == match_state)
  16196. ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
  16197. task_rq_unlock(rq, p, &rf);
  16198. @@ -1680,10 +1821,6 @@
  16199. {
  16200. activate_task(rq, p, en_flags);
  16201. p->on_rq = TASK_ON_RQ_QUEUED;
  16202. -
  16203. - /* if a worker is waking up, notify workqueue */
  16204. - if (p->flags & PF_WQ_WORKER)
  16205. - wq_worker_waking_up(p, cpu_of(rq));
  16206. }
  16207. /*
  16208. @@ -2018,8 +2155,27 @@
  16209. */
  16210. smp_mb__before_spinlock();
  16211. raw_spin_lock_irqsave(&p->pi_lock, flags);
  16212. - if (!(p->state & state))
  16213. + if (!(p->state & state)) {
  16214. + /*
  16215. + * The task might be running due to a spinlock sleeper
  16216. + * wakeup. Check the saved state and set it to running
  16217. + * if the wakeup condition is true.
  16218. + */
  16219. + if (!(wake_flags & WF_LOCK_SLEEPER)) {
  16220. + if (p->saved_state & state) {
  16221. + p->saved_state = TASK_RUNNING;
  16222. + success = 1;
  16223. + }
  16224. + }
  16225. goto out;
  16226. + }
  16227. +
  16228. + /*
  16229. + * If this is a regular wakeup, then we can unconditionally
  16230. + * clear the saved state of a "lock sleeper".
  16231. + */
  16232. + if (!(wake_flags & WF_LOCK_SLEEPER))
  16233. + p->saved_state = TASK_RUNNING;
  16234. trace_sched_waking(p);
  16235. @@ -2102,53 +2258,6 @@
  16236. }
  16237. /**
  16238. - * try_to_wake_up_local - try to wake up a local task with rq lock held
  16239. - * @p: the thread to be awakened
  16240. - * @cookie: context's cookie for pinning
  16241. - *
  16242. - * Put @p on the run-queue if it's not already there. The caller must
  16243. - * ensure that this_rq() is locked, @p is bound to this_rq() and not
  16244. - * the current task.
  16245. - */
  16246. -static void try_to_wake_up_local(struct task_struct *p, struct pin_cookie cookie)
  16247. -{
  16248. - struct rq *rq = task_rq(p);
  16249. -
  16250. - if (WARN_ON_ONCE(rq != this_rq()) ||
  16251. - WARN_ON_ONCE(p == current))
  16252. - return;
  16253. -
  16254. - lockdep_assert_held(&rq->lock);
  16255. -
  16256. - if (!raw_spin_trylock(&p->pi_lock)) {
  16257. - /*
  16258. - * This is OK, because current is on_cpu, which avoids it being
  16259. - * picked for load-balance and preemption/IRQs are still
  16260. - * disabled avoiding further scheduler activity on it and we've
  16261. - * not yet picked a replacement task.
  16262. - */
  16263. - lockdep_unpin_lock(&rq->lock, cookie);
  16264. - raw_spin_unlock(&rq->lock);
  16265. - raw_spin_lock(&p->pi_lock);
  16266. - raw_spin_lock(&rq->lock);
  16267. - lockdep_repin_lock(&rq->lock, cookie);
  16268. - }
  16269. -
  16270. - if (!(p->state & TASK_NORMAL))
  16271. - goto out;
  16272. -
  16273. - trace_sched_waking(p);
  16274. -
  16275. - if (!task_on_rq_queued(p))
  16276. - ttwu_activate(rq, p, ENQUEUE_WAKEUP);
  16277. -
  16278. - ttwu_do_wakeup(rq, p, 0, cookie);
  16279. - ttwu_stat(p, smp_processor_id(), 0);
  16280. -out:
  16281. - raw_spin_unlock(&p->pi_lock);
  16282. -}
  16283. -
  16284. -/**
  16285. * wake_up_process - Wake up a specific process
  16286. * @p: The process to be woken up.
  16287. *
  16288. @@ -2166,6 +2275,18 @@
  16289. }
  16290. EXPORT_SYMBOL(wake_up_process);
  16291. +/**
  16292. + * wake_up_lock_sleeper - Wake up a specific process blocked on a "sleeping lock"
  16293. + * @p: The process to be woken up.
  16294. + *
  16295. + * Same as wake_up_process() above, but wake_flags=WF_LOCK_SLEEPER to indicate
  16296. + * the nature of the wakeup.
  16297. + */
  16298. +int wake_up_lock_sleeper(struct task_struct *p)
  16299. +{
  16300. + return try_to_wake_up(p, TASK_ALL, WF_LOCK_SLEEPER);
  16301. +}
  16302. +
  16303. int wake_up_state(struct task_struct *p, unsigned int state)
  16304. {
  16305. return try_to_wake_up(p, state, 0);
  16306. @@ -2442,6 +2563,9 @@
  16307. p->on_cpu = 0;
  16308. #endif
  16309. init_task_preempt_count(p);
  16310. +#ifdef CONFIG_HAVE_PREEMPT_LAZY
  16311. + task_thread_info(p)->preempt_lazy_count = 0;
  16312. +#endif
  16313. #ifdef CONFIG_SMP
  16314. plist_node_init(&p->pushable_tasks, MAX_PRIO);
  16315. RB_CLEAR_NODE(&p->pushable_dl_tasks);
  16316. @@ -2770,21 +2894,16 @@
  16317. finish_arch_post_lock_switch();
  16318. fire_sched_in_preempt_notifiers(current);
  16319. + /*
  16320. + * We use mmdrop_delayed() here so we don't have to do the
  16321. + * full __mmdrop() when we are the last user.
  16322. + */
  16323. if (mm)
  16324. - mmdrop(mm);
  16325. + mmdrop_delayed(mm);
  16326. if (unlikely(prev_state == TASK_DEAD)) {
  16327. if (prev->sched_class->task_dead)
  16328. prev->sched_class->task_dead(prev);
  16329. - /*
  16330. - * Remove function-return probe instances associated with this
  16331. - * task and put them back on the free list.
  16332. - */
  16333. - kprobe_flush_task(prev);
  16334. -
  16335. - /* Task is done with its stack. */
  16336. - put_task_stack(prev);
  16337. -
  16338. put_task_struct(prev);
  16339. }
  16340. @@ -3252,6 +3371,77 @@
  16341. schedstat_inc(this_rq()->sched_count);
  16342. }
  16343. +#if defined(CONFIG_PREEMPT_RT_FULL) && defined(CONFIG_SMP)
  16344. +
  16345. +void migrate_disable(void)
  16346. +{
  16347. + struct task_struct *p = current;
  16348. +
  16349. + if (in_atomic() || irqs_disabled()) {
  16350. +#ifdef CONFIG_SCHED_DEBUG
  16351. + p->migrate_disable_atomic++;
  16352. +#endif
  16353. + return;
  16354. + }
  16355. +
  16356. +#ifdef CONFIG_SCHED_DEBUG
  16357. + if (unlikely(p->migrate_disable_atomic)) {
  16358. + tracing_off();
  16359. + WARN_ON_ONCE(1);
  16360. + }
  16361. +#endif
  16362. +
  16363. + if (p->migrate_disable) {
  16364. + p->migrate_disable++;
  16365. + return;
  16366. + }
  16367. +
  16368. + preempt_disable();
  16369. + preempt_lazy_disable();
  16370. + pin_current_cpu();
  16371. + p->migrate_disable = 1;
  16372. + preempt_enable();
  16373. +}
  16374. +EXPORT_SYMBOL(migrate_disable);
  16375. +
  16376. +void migrate_enable(void)
  16377. +{
  16378. + struct task_struct *p = current;
  16379. +
  16380. + if (in_atomic() || irqs_disabled()) {
  16381. +#ifdef CONFIG_SCHED_DEBUG
  16382. + p->migrate_disable_atomic--;
  16383. +#endif
  16384. + return;
  16385. + }
  16386. +
  16387. +#ifdef CONFIG_SCHED_DEBUG
  16388. + if (unlikely(p->migrate_disable_atomic)) {
  16389. + tracing_off();
  16390. + WARN_ON_ONCE(1);
  16391. + }
  16392. +#endif
  16393. + WARN_ON_ONCE(p->migrate_disable <= 0);
  16394. +
  16395. + if (p->migrate_disable > 1) {
  16396. + p->migrate_disable--;
  16397. + return;
  16398. + }
  16399. +
  16400. + preempt_disable();
  16401. + /*
  16402. + * Clearing migrate_disable causes tsk_cpus_allowed to
  16403. + * show the tasks original cpu affinity.
  16404. + */
  16405. + p->migrate_disable = 0;
  16406. +
  16407. + unpin_current_cpu();
  16408. + preempt_enable();
  16409. + preempt_lazy_enable();
  16410. +}
  16411. +EXPORT_SYMBOL(migrate_enable);
  16412. +#endif
  16413. +
  16414. /*
  16415. * Pick up the highest-prio task:
  16416. */
  16417. @@ -3368,19 +3558,6 @@
  16418. } else {
  16419. deactivate_task(rq, prev, DEQUEUE_SLEEP);
  16420. prev->on_rq = 0;
  16421. -
  16422. - /*
  16423. - * If a worker went to sleep, notify and ask workqueue
  16424. - * whether it wants to wake up a task to maintain
  16425. - * concurrency.
  16426. - */
  16427. - if (prev->flags & PF_WQ_WORKER) {
  16428. - struct task_struct *to_wakeup;
  16429. -
  16430. - to_wakeup = wq_worker_sleeping(prev);
  16431. - if (to_wakeup)
  16432. - try_to_wake_up_local(to_wakeup, cookie);
  16433. - }
  16434. }
  16435. switch_count = &prev->nvcsw;
  16436. }
  16437. @@ -3390,6 +3567,7 @@
  16438. next = pick_next_task(rq, prev, cookie);
  16439. clear_tsk_need_resched(prev);
  16440. + clear_tsk_need_resched_lazy(prev);
  16441. clear_preempt_need_resched();
  16442. rq->clock_skip_update = 0;
  16443. @@ -3437,9 +3615,20 @@
  16444. static inline void sched_submit_work(struct task_struct *tsk)
  16445. {
  16446. - if (!tsk->state || tsk_is_pi_blocked(tsk))
  16447. + if (!tsk->state)
  16448. return;
  16449. /*
  16450. + * If a worker went to sleep, notify and ask workqueue whether
  16451. + * it wants to wake up a task to maintain concurrency.
  16452. + */
  16453. + if (tsk->flags & PF_WQ_WORKER)
  16454. + wq_worker_sleeping(tsk);
  16455. +
  16456. +
  16457. + if (tsk_is_pi_blocked(tsk))
  16458. + return;
  16459. +
  16460. + /*
  16461. * If we are going to sleep and we have plugged IO queued,
  16462. * make sure to submit it to avoid deadlocks.
  16463. */
  16464. @@ -3447,6 +3636,12 @@
  16465. blk_schedule_flush_plug(tsk);
  16466. }
  16467. +static void sched_update_worker(struct task_struct *tsk)
  16468. +{
  16469. + if (tsk->flags & PF_WQ_WORKER)
  16470. + wq_worker_running(tsk);
  16471. +}
  16472. +
  16473. asmlinkage __visible void __sched schedule(void)
  16474. {
  16475. struct task_struct *tsk = current;
  16476. @@ -3457,6 +3652,7 @@
  16477. __schedule(false);
  16478. sched_preempt_enable_no_resched();
  16479. } while (need_resched());
  16480. + sched_update_worker(tsk);
  16481. }
  16482. EXPORT_SYMBOL(schedule);
  16483. @@ -3520,6 +3716,30 @@
  16484. } while (need_resched());
  16485. }
  16486. +#ifdef CONFIG_PREEMPT_LAZY
  16487. +/*
  16488. + * If TIF_NEED_RESCHED is then we allow to be scheduled away since this is
  16489. + * set by a RT task. Oterwise we try to avoid beeing scheduled out as long as
  16490. + * preempt_lazy_count counter >0.
  16491. + */
  16492. +static __always_inline int preemptible_lazy(void)
  16493. +{
  16494. + if (test_thread_flag(TIF_NEED_RESCHED))
  16495. + return 1;
  16496. + if (current_thread_info()->preempt_lazy_count)
  16497. + return 0;
  16498. + return 1;
  16499. +}
  16500. +
  16501. +#else
  16502. +
  16503. +static inline int preemptible_lazy(void)
  16504. +{
  16505. + return 1;
  16506. +}
  16507. +
  16508. +#endif
  16509. +
  16510. #ifdef CONFIG_PREEMPT
  16511. /*
  16512. * this is the entry point to schedule() from in-kernel preemption
  16513. @@ -3534,7 +3754,8 @@
  16514. */
  16515. if (likely(!preemptible()))
  16516. return;
  16517. -
  16518. + if (!preemptible_lazy())
  16519. + return;
  16520. preempt_schedule_common();
  16521. }
  16522. NOKPROBE_SYMBOL(preempt_schedule);
  16523. @@ -3561,6 +3782,9 @@
  16524. if (likely(!preemptible()))
  16525. return;
  16526. + if (!preemptible_lazy())
  16527. + return;
  16528. +
  16529. do {
  16530. /*
  16531. * Because the function tracer can trace preempt_count_sub()
  16532. @@ -3583,7 +3807,16 @@
  16533. * an infinite recursion.
  16534. */
  16535. prev_ctx = exception_enter();
  16536. + /*
  16537. + * The add/subtract must not be traced by the function
  16538. + * tracer. But we still want to account for the
  16539. + * preempt off latency tracer. Since the _notrace versions
  16540. + * of add/subtract skip the accounting for latency tracer
  16541. + * we must force it manually.
  16542. + */
  16543. + start_critical_timings();
  16544. __schedule(true);
  16545. + stop_critical_timings();
  16546. exception_exit(prev_ctx);
  16547. preempt_latency_stop(1);
  16548. @@ -3629,10 +3862,25 @@
  16549. #ifdef CONFIG_RT_MUTEXES
  16550. +static inline int __rt_effective_prio(struct task_struct *pi_task, int prio)
  16551. +{
  16552. + if (pi_task)
  16553. + prio = min(prio, pi_task->prio);
  16554. +
  16555. + return prio;
  16556. +}
  16557. +
  16558. +static inline int rt_effective_prio(struct task_struct *p, int prio)
  16559. +{
  16560. + struct task_struct *pi_task = rt_mutex_get_top_task(p);
  16561. +
  16562. + return __rt_effective_prio(pi_task, prio);
  16563. +}
  16564. +
  16565. /*
  16566. * rt_mutex_setprio - set the current priority of a task
  16567. - * @p: task
  16568. - * @prio: prio value (kernel-internal form)
  16569. + * @p: task to boost
  16570. + * @pi_task: donor task
  16571. *
  16572. * This function changes the 'effective' priority of a task. It does
  16573. * not touch ->normal_prio like __setscheduler().
  16574. @@ -3640,16 +3888,40 @@
  16575. * Used by the rt_mutex code to implement priority inheritance
  16576. * logic. Call site only calls if the priority of the task changed.
  16577. */
  16578. -void rt_mutex_setprio(struct task_struct *p, int prio)
  16579. +void rt_mutex_setprio(struct task_struct *p, struct task_struct *pi_task)
  16580. {
  16581. - int oldprio, queued, running, queue_flag = DEQUEUE_SAVE | DEQUEUE_MOVE;
  16582. + int prio, oldprio, queued, running, queue_flag = DEQUEUE_SAVE | DEQUEUE_MOVE;
  16583. const struct sched_class *prev_class;
  16584. struct rq_flags rf;
  16585. struct rq *rq;
  16586. - BUG_ON(prio > MAX_PRIO);
  16587. + /* XXX used to be waiter->prio, not waiter->task->prio */
  16588. + prio = __rt_effective_prio(pi_task, p->normal_prio);
  16589. +
  16590. + /*
  16591. + * If nothing changed; bail early.
  16592. + */
  16593. + if (p->pi_top_task == pi_task && prio == p->prio && !dl_prio(prio))
  16594. + return;
  16595. rq = __task_rq_lock(p, &rf);
  16596. + /*
  16597. + * Set under pi_lock && rq->lock, such that the value can be used under
  16598. + * either lock.
  16599. + *
  16600. + * Note that there is loads of tricky to make this pointer cache work
  16601. + * right. rt_mutex_slowunlock()+rt_mutex_postunlock() work together to
  16602. + * ensure a task is de-boosted (pi_task is set to NULL) before the
  16603. + * task is allowed to run again (and can exit). This ensures the pointer
  16604. + * points to a blocked task -- which guaratees the task is present.
  16605. + */
  16606. + p->pi_top_task = pi_task;
  16607. +
  16608. + /*
  16609. + * For FIFO/RR we only need to set prio, if that matches we're done.
  16610. + */
  16611. + if (prio == p->prio && !dl_prio(prio))
  16612. + goto out_unlock;
  16613. /*
  16614. * Idle task boosting is a nono in general. There is one
  16615. @@ -3669,7 +3941,7 @@
  16616. goto out_unlock;
  16617. }
  16618. - trace_sched_pi_setprio(p, prio);
  16619. + trace_sched_pi_setprio(p, pi_task);
  16620. oldprio = p->prio;
  16621. if (oldprio == prio)
  16622. @@ -3693,7 +3965,6 @@
  16623. * running task
  16624. */
  16625. if (dl_prio(prio)) {
  16626. - struct task_struct *pi_task = rt_mutex_get_top_task(p);
  16627. if (!dl_prio(p->normal_prio) ||
  16628. (pi_task && dl_entity_preempt(&pi_task->dl, &p->dl))) {
  16629. p->dl.dl_boosted = 1;
  16630. @@ -3730,6 +4001,11 @@
  16631. balance_callback(rq);
  16632. preempt_enable();
  16633. }
  16634. +#else
  16635. +static inline int rt_effective_prio(struct task_struct *p, int prio)
  16636. +{
  16637. + return prio;
  16638. +}
  16639. #endif
  16640. void set_user_nice(struct task_struct *p, long nice)
  16641. @@ -3974,10 +4250,9 @@
  16642. * Keep a potential priority boosting if called from
  16643. * sched_setscheduler().
  16644. */
  16645. + p->prio = normal_prio(p);
  16646. if (keep_boost)
  16647. - p->prio = rt_mutex_get_effective_prio(p, normal_prio(p));
  16648. - else
  16649. - p->prio = normal_prio(p);
  16650. + p->prio = rt_effective_prio(p, p->prio);
  16651. if (dl_prio(p->prio))
  16652. p->sched_class = &dl_sched_class;
  16653. @@ -4264,7 +4539,7 @@
  16654. * the runqueue. This will be done when the task deboost
  16655. * itself.
  16656. */
  16657. - new_effective_prio = rt_mutex_get_effective_prio(p, newprio);
  16658. + new_effective_prio = rt_effective_prio(p, newprio);
  16659. if (new_effective_prio == oldprio)
  16660. queue_flags &= ~DEQUEUE_MOVE;
  16661. }
  16662. @@ -4939,6 +5214,7 @@
  16663. }
  16664. EXPORT_SYMBOL(__cond_resched_lock);
  16665. +#ifndef CONFIG_PREEMPT_RT_FULL
  16666. int __sched __cond_resched_softirq(void)
  16667. {
  16668. BUG_ON(!in_softirq());
  16669. @@ -4952,6 +5228,7 @@
  16670. return 0;
  16671. }
  16672. EXPORT_SYMBOL(__cond_resched_softirq);
  16673. +#endif
  16674. /**
  16675. * yield - yield the current processor to other threads.
  16676. @@ -5315,7 +5592,9 @@
  16677. /* Set the preempt count _outside_ the spinlocks! */
  16678. init_idle_preempt_count(idle, cpu);
  16679. -
  16680. +#ifdef CONFIG_HAVE_PREEMPT_LAZY
  16681. + task_thread_info(idle)->preempt_lazy_count = 0;
  16682. +#endif
  16683. /*
  16684. * The idle tasks have their own, simple scheduling class:
  16685. */
  16686. @@ -5458,6 +5737,8 @@
  16687. #endif /* CONFIG_NUMA_BALANCING */
  16688. #ifdef CONFIG_HOTPLUG_CPU
  16689. +static DEFINE_PER_CPU(struct mm_struct *, idle_last_mm);
  16690. +
  16691. /*
  16692. * Ensures that the idle task is using init_mm right before its cpu goes
  16693. * offline.
  16694. @@ -5472,7 +5753,12 @@
  16695. switch_mm_irqs_off(mm, &init_mm, current);
  16696. finish_arch_post_lock_switch();
  16697. }
  16698. - mmdrop(mm);
  16699. + /*
  16700. + * Defer the cleanup to an alive cpu. On RT we can neither
  16701. + * call mmdrop() nor mmdrop_delayed() from here.
  16702. + */
  16703. + per_cpu(idle_last_mm, smp_processor_id()) = mm;
  16704. +
  16705. }
  16706. /*
  16707. @@ -7418,6 +7704,10 @@
  16708. update_max_interval();
  16709. nohz_balance_exit_idle(cpu);
  16710. hrtick_clear(rq);
  16711. + if (per_cpu(idle_last_mm, cpu)) {
  16712. + mmdrop_delayed(per_cpu(idle_last_mm, cpu));
  16713. + per_cpu(idle_last_mm, cpu) = NULL;
  16714. + }
  16715. return 0;
  16716. }
  16717. #endif
  16718. @@ -7698,7 +7988,7 @@
  16719. #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
  16720. static inline int preempt_count_equals(int preempt_offset)
  16721. {
  16722. - int nested = preempt_count() + rcu_preempt_depth();
  16723. + int nested = preempt_count() + sched_rcu_preempt_depth();
  16724. return (nested == preempt_offset);
  16725. }
  16726. diff -Nur linux-4.9.28.orig/kernel/sched/deadline.c linux-4.9.28/kernel/sched/deadline.c
  16727. --- linux-4.9.28.orig/kernel/sched/deadline.c 2017-05-14 14:00:37.000000000 +0200
  16728. +++ linux-4.9.28/kernel/sched/deadline.c 2017-05-19 03:37:25.194177264 +0200
  16729. @@ -687,6 +687,7 @@
  16730. hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  16731. timer->function = dl_task_timer;
  16732. + timer->irqsafe = 1;
  16733. }
  16734. static
  16735. diff -Nur linux-4.9.28.orig/kernel/sched/debug.c linux-4.9.28/kernel/sched/debug.c
  16736. --- linux-4.9.28.orig/kernel/sched/debug.c 2017-05-14 14:00:37.000000000 +0200
  16737. +++ linux-4.9.28/kernel/sched/debug.c 2017-05-19 03:37:25.194177264 +0200
  16738. @@ -558,6 +558,9 @@
  16739. P(rt_throttled);
  16740. PN(rt_time);
  16741. PN(rt_runtime);
  16742. +#ifdef CONFIG_SMP
  16743. + P(rt_nr_migratory);
  16744. +#endif
  16745. #undef PN
  16746. #undef P
  16747. @@ -953,6 +956,10 @@
  16748. #endif
  16749. P(policy);
  16750. P(prio);
  16751. +#ifdef CONFIG_PREEMPT_RT_FULL
  16752. + P(migrate_disable);
  16753. +#endif
  16754. + P(nr_cpus_allowed);
  16755. #undef PN_SCHEDSTAT
  16756. #undef PN
  16757. #undef __PN
  16758. diff -Nur linux-4.9.28.orig/kernel/sched/fair.c linux-4.9.28/kernel/sched/fair.c
  16759. --- linux-4.9.28.orig/kernel/sched/fair.c 2017-05-14 14:00:37.000000000 +0200
  16760. +++ linux-4.9.28/kernel/sched/fair.c 2017-05-19 03:37:25.194177264 +0200
  16761. @@ -3518,7 +3518,7 @@
  16762. ideal_runtime = sched_slice(cfs_rq, curr);
  16763. delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
  16764. if (delta_exec > ideal_runtime) {
  16765. - resched_curr(rq_of(cfs_rq));
  16766. + resched_curr_lazy(rq_of(cfs_rq));
  16767. /*
  16768. * The current task ran long enough, ensure it doesn't get
  16769. * re-elected due to buddy favours.
  16770. @@ -3542,7 +3542,7 @@
  16771. return;
  16772. if (delta > ideal_runtime)
  16773. - resched_curr(rq_of(cfs_rq));
  16774. + resched_curr_lazy(rq_of(cfs_rq));
  16775. }
  16776. static void
  16777. @@ -3684,7 +3684,7 @@
  16778. * validating it and just reschedule.
  16779. */
  16780. if (queued) {
  16781. - resched_curr(rq_of(cfs_rq));
  16782. + resched_curr_lazy(rq_of(cfs_rq));
  16783. return;
  16784. }
  16785. /*
  16786. @@ -3866,7 +3866,7 @@
  16787. * hierarchy can be throttled
  16788. */
  16789. if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr))
  16790. - resched_curr(rq_of(cfs_rq));
  16791. + resched_curr_lazy(rq_of(cfs_rq));
  16792. }
  16793. static __always_inline
  16794. @@ -4494,7 +4494,7 @@
  16795. if (delta < 0) {
  16796. if (rq->curr == p)
  16797. - resched_curr(rq);
  16798. + resched_curr_lazy(rq);
  16799. return;
  16800. }
  16801. hrtick_start(rq, delta);
  16802. @@ -5905,7 +5905,7 @@
  16803. return;
  16804. preempt:
  16805. - resched_curr(rq);
  16806. + resched_curr_lazy(rq);
  16807. /*
  16808. * Only set the backward buddy when the current task is still
  16809. * on the rq. This can happen when a wakeup gets interleaved
  16810. @@ -8631,7 +8631,7 @@
  16811. * 'current' within the tree based on its new key value.
  16812. */
  16813. swap(curr->vruntime, se->vruntime);
  16814. - resched_curr(rq);
  16815. + resched_curr_lazy(rq);
  16816. }
  16817. se->vruntime -= cfs_rq->min_vruntime;
  16818. @@ -8655,7 +8655,7 @@
  16819. */
  16820. if (rq->curr == p) {
  16821. if (p->prio > oldprio)
  16822. - resched_curr(rq);
  16823. + resched_curr_lazy(rq);
  16824. } else
  16825. check_preempt_curr(rq, p, 0);
  16826. }
  16827. diff -Nur linux-4.9.28.orig/kernel/sched/features.h linux-4.9.28/kernel/sched/features.h
  16828. --- linux-4.9.28.orig/kernel/sched/features.h 2017-05-14 14:00:37.000000000 +0200
  16829. +++ linux-4.9.28/kernel/sched/features.h 2017-05-19 03:37:25.194177264 +0200
  16830. @@ -45,11 +45,19 @@
  16831. */
  16832. SCHED_FEAT(NONTASK_CAPACITY, true)
  16833. +#ifdef CONFIG_PREEMPT_RT_FULL
  16834. +SCHED_FEAT(TTWU_QUEUE, false)
  16835. +# ifdef CONFIG_PREEMPT_LAZY
  16836. +SCHED_FEAT(PREEMPT_LAZY, true)
  16837. +# endif
  16838. +#else
  16839. +
  16840. /*
  16841. * Queue remote wakeups on the target CPU and process them
  16842. * using the scheduler IPI. Reduces rq->lock contention/bounces.
  16843. */
  16844. SCHED_FEAT(TTWU_QUEUE, true)
  16845. +#endif
  16846. #ifdef HAVE_RT_PUSH_IPI
  16847. /*
  16848. diff -Nur linux-4.9.28.orig/kernel/sched/Makefile linux-4.9.28/kernel/sched/Makefile
  16849. --- linux-4.9.28.orig/kernel/sched/Makefile 2017-05-14 14:00:37.000000000 +0200
  16850. +++ linux-4.9.28/kernel/sched/Makefile 2017-05-19 03:37:25.194177264 +0200
  16851. @@ -17,7 +17,7 @@
  16852. obj-y += core.o loadavg.o clock.o cputime.o
  16853. obj-y += idle_task.o fair.o rt.o deadline.o stop_task.o
  16854. -obj-y += wait.o swait.o completion.o idle.o
  16855. +obj-y += wait.o swait.o swork.o completion.o idle.o
  16856. obj-$(CONFIG_SMP) += cpupri.o cpudeadline.o
  16857. obj-$(CONFIG_SCHED_AUTOGROUP) += auto_group.o
  16858. obj-$(CONFIG_SCHEDSTATS) += stats.o
  16859. diff -Nur linux-4.9.28.orig/kernel/sched/rt.c linux-4.9.28/kernel/sched/rt.c
  16860. --- linux-4.9.28.orig/kernel/sched/rt.c 2017-05-14 14:00:37.000000000 +0200
  16861. +++ linux-4.9.28/kernel/sched/rt.c 2017-05-19 03:37:25.194177264 +0200
  16862. @@ -47,6 +47,7 @@
  16863. hrtimer_init(&rt_b->rt_period_timer,
  16864. CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  16865. + rt_b->rt_period_timer.irqsafe = 1;
  16866. rt_b->rt_period_timer.function = sched_rt_period_timer;
  16867. }
  16868. @@ -101,6 +102,7 @@
  16869. rt_rq->push_cpu = nr_cpu_ids;
  16870. raw_spin_lock_init(&rt_rq->push_lock);
  16871. init_irq_work(&rt_rq->push_work, push_irq_work_func);
  16872. + rt_rq->push_work.flags |= IRQ_WORK_HARD_IRQ;
  16873. #endif
  16874. #endif /* CONFIG_SMP */
  16875. /* We start is dequeued state, because no RT tasks are queued */
  16876. diff -Nur linux-4.9.28.orig/kernel/sched/sched.h linux-4.9.28/kernel/sched/sched.h
  16877. --- linux-4.9.28.orig/kernel/sched/sched.h 2017-05-14 14:00:37.000000000 +0200
  16878. +++ linux-4.9.28/kernel/sched/sched.h 2017-05-19 03:37:25.194177264 +0200
  16879. @@ -1163,6 +1163,7 @@
  16880. #define WF_SYNC 0x01 /* waker goes to sleep after wakeup */
  16881. #define WF_FORK 0x02 /* child wakeup after fork */
  16882. #define WF_MIGRATED 0x4 /* internal use, task got migrated */
  16883. +#define WF_LOCK_SLEEPER 0x08 /* wakeup spinlock "sleeper" */
  16884. /*
  16885. * To aid in avoiding the subversion of "niceness" due to uneven distribution
  16886. @@ -1346,6 +1347,15 @@
  16887. extern void resched_curr(struct rq *rq);
  16888. extern void resched_cpu(int cpu);
  16889. +#ifdef CONFIG_PREEMPT_LAZY
  16890. +extern void resched_curr_lazy(struct rq *rq);
  16891. +#else
  16892. +static inline void resched_curr_lazy(struct rq *rq)
  16893. +{
  16894. + resched_curr(rq);
  16895. +}
  16896. +#endif
  16897. +
  16898. extern struct rt_bandwidth def_rt_bandwidth;
  16899. extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
  16900. diff -Nur linux-4.9.28.orig/kernel/sched/swait.c linux-4.9.28/kernel/sched/swait.c
  16901. --- linux-4.9.28.orig/kernel/sched/swait.c 2017-05-14 14:00:37.000000000 +0200
  16902. +++ linux-4.9.28/kernel/sched/swait.c 2017-05-19 03:37:25.198177419 +0200
  16903. @@ -1,5 +1,6 @@
  16904. #include <linux/sched.h>
  16905. #include <linux/swait.h>
  16906. +#include <linux/suspend.h>
  16907. void __init_swait_queue_head(struct swait_queue_head *q, const char *name,
  16908. struct lock_class_key *key)
  16909. @@ -29,6 +30,25 @@
  16910. }
  16911. EXPORT_SYMBOL(swake_up_locked);
  16912. +void swake_up_all_locked(struct swait_queue_head *q)
  16913. +{
  16914. + struct swait_queue *curr;
  16915. + int wakes = 0;
  16916. +
  16917. + while (!list_empty(&q->task_list)) {
  16918. +
  16919. + curr = list_first_entry(&q->task_list, typeof(*curr),
  16920. + task_list);
  16921. + wake_up_process(curr->task);
  16922. + list_del_init(&curr->task_list);
  16923. + wakes++;
  16924. + }
  16925. + if (pm_in_action)
  16926. + return;
  16927. + WARN(wakes > 2, "complete_all() with %d waiters\n", wakes);
  16928. +}
  16929. +EXPORT_SYMBOL(swake_up_all_locked);
  16930. +
  16931. void swake_up(struct swait_queue_head *q)
  16932. {
  16933. unsigned long flags;
  16934. @@ -54,6 +74,7 @@
  16935. if (!swait_active(q))
  16936. return;
  16937. + WARN_ON(irqs_disabled());
  16938. raw_spin_lock_irq(&q->lock);
  16939. list_splice_init(&q->task_list, &tmp);
  16940. while (!list_empty(&tmp)) {
  16941. diff -Nur linux-4.9.28.orig/kernel/sched/swork.c linux-4.9.28/kernel/sched/swork.c
  16942. --- linux-4.9.28.orig/kernel/sched/swork.c 1970-01-01 01:00:00.000000000 +0100
  16943. +++ linux-4.9.28/kernel/sched/swork.c 2017-05-19 03:37:25.198177419 +0200
  16944. @@ -0,0 +1,173 @@
  16945. +/*
  16946. + * Copyright (C) 2014 BMW Car IT GmbH, Daniel Wagner daniel.wagner@bmw-carit.de
  16947. + *
  16948. + * Provides a framework for enqueuing callbacks from irq context
  16949. + * PREEMPT_RT_FULL safe. The callbacks are executed in kthread context.
  16950. + */
  16951. +
  16952. +#include <linux/swait.h>
  16953. +#include <linux/swork.h>
  16954. +#include <linux/kthread.h>
  16955. +#include <linux/slab.h>
  16956. +#include <linux/spinlock.h>
  16957. +#include <linux/export.h>
  16958. +
  16959. +#define SWORK_EVENT_PENDING (1 << 0)
  16960. +
  16961. +static DEFINE_MUTEX(worker_mutex);
  16962. +static struct sworker *glob_worker;
  16963. +
  16964. +struct sworker {
  16965. + struct list_head events;
  16966. + struct swait_queue_head wq;
  16967. +
  16968. + raw_spinlock_t lock;
  16969. +
  16970. + struct task_struct *task;
  16971. + int refs;
  16972. +};
  16973. +
  16974. +static bool swork_readable(struct sworker *worker)
  16975. +{
  16976. + bool r;
  16977. +
  16978. + if (kthread_should_stop())
  16979. + return true;
  16980. +
  16981. + raw_spin_lock_irq(&worker->lock);
  16982. + r = !list_empty(&worker->events);
  16983. + raw_spin_unlock_irq(&worker->lock);
  16984. +
  16985. + return r;
  16986. +}
  16987. +
  16988. +static int swork_kthread(void *arg)
  16989. +{
  16990. + struct sworker *worker = arg;
  16991. +
  16992. + for (;;) {
  16993. + swait_event_interruptible(worker->wq,
  16994. + swork_readable(worker));
  16995. + if (kthread_should_stop())
  16996. + break;
  16997. +
  16998. + raw_spin_lock_irq(&worker->lock);
  16999. + while (!list_empty(&worker->events)) {
  17000. + struct swork_event *sev;
  17001. +
  17002. + sev = list_first_entry(&worker->events,
  17003. + struct swork_event, item);
  17004. + list_del(&sev->item);
  17005. + raw_spin_unlock_irq(&worker->lock);
  17006. +
  17007. + WARN_ON_ONCE(!test_and_clear_bit(SWORK_EVENT_PENDING,
  17008. + &sev->flags));
  17009. + sev->func(sev);
  17010. + raw_spin_lock_irq(&worker->lock);
  17011. + }
  17012. + raw_spin_unlock_irq(&worker->lock);
  17013. + }
  17014. + return 0;
  17015. +}
  17016. +
  17017. +static struct sworker *swork_create(void)
  17018. +{
  17019. + struct sworker *worker;
  17020. +
  17021. + worker = kzalloc(sizeof(*worker), GFP_KERNEL);
  17022. + if (!worker)
  17023. + return ERR_PTR(-ENOMEM);
  17024. +
  17025. + INIT_LIST_HEAD(&worker->events);
  17026. + raw_spin_lock_init(&worker->lock);
  17027. + init_swait_queue_head(&worker->wq);
  17028. +
  17029. + worker->task = kthread_run(swork_kthread, worker, "kswork");
  17030. + if (IS_ERR(worker->task)) {
  17031. + kfree(worker);
  17032. + return ERR_PTR(-ENOMEM);
  17033. + }
  17034. +
  17035. + return worker;
  17036. +}
  17037. +
  17038. +static void swork_destroy(struct sworker *worker)
  17039. +{
  17040. + kthread_stop(worker->task);
  17041. +
  17042. + WARN_ON(!list_empty(&worker->events));
  17043. + kfree(worker);
  17044. +}
  17045. +
  17046. +/**
  17047. + * swork_queue - queue swork
  17048. + *
  17049. + * Returns %false if @work was already on a queue, %true otherwise.
  17050. + *
  17051. + * The work is queued and processed on a random CPU
  17052. + */
  17053. +bool swork_queue(struct swork_event *sev)
  17054. +{
  17055. + unsigned long flags;
  17056. +
  17057. + if (test_and_set_bit(SWORK_EVENT_PENDING, &sev->flags))
  17058. + return false;
  17059. +
  17060. + raw_spin_lock_irqsave(&glob_worker->lock, flags);
  17061. + list_add_tail(&sev->item, &glob_worker->events);
  17062. + raw_spin_unlock_irqrestore(&glob_worker->lock, flags);
  17063. +
  17064. + swake_up(&glob_worker->wq);
  17065. + return true;
  17066. +}
  17067. +EXPORT_SYMBOL_GPL(swork_queue);
  17068. +
  17069. +/**
  17070. + * swork_get - get an instance of the sworker
  17071. + *
  17072. + * Returns an negative error code if the initialization if the worker did not
  17073. + * work, %0 otherwise.
  17074. + *
  17075. + */
  17076. +int swork_get(void)
  17077. +{
  17078. + struct sworker *worker;
  17079. +
  17080. + mutex_lock(&worker_mutex);
  17081. + if (!glob_worker) {
  17082. + worker = swork_create();
  17083. + if (IS_ERR(worker)) {
  17084. + mutex_unlock(&worker_mutex);
  17085. + return -ENOMEM;
  17086. + }
  17087. +
  17088. + glob_worker = worker;
  17089. + }
  17090. +
  17091. + glob_worker->refs++;
  17092. + mutex_unlock(&worker_mutex);
  17093. +
  17094. + return 0;
  17095. +}
  17096. +EXPORT_SYMBOL_GPL(swork_get);
  17097. +
  17098. +/**
  17099. + * swork_put - puts an instance of the sworker
  17100. + *
  17101. + * Will destroy the sworker thread. This function must not be called until all
  17102. + * queued events have been completed.
  17103. + */
  17104. +void swork_put(void)
  17105. +{
  17106. + mutex_lock(&worker_mutex);
  17107. +
  17108. + glob_worker->refs--;
  17109. + if (glob_worker->refs > 0)
  17110. + goto out;
  17111. +
  17112. + swork_destroy(glob_worker);
  17113. + glob_worker = NULL;
  17114. +out:
  17115. + mutex_unlock(&worker_mutex);
  17116. +}
  17117. +EXPORT_SYMBOL_GPL(swork_put);
  17118. diff -Nur linux-4.9.28.orig/kernel/signal.c linux-4.9.28/kernel/signal.c
  17119. --- linux-4.9.28.orig/kernel/signal.c 2017-05-14 14:00:37.000000000 +0200
  17120. +++ linux-4.9.28/kernel/signal.c 2017-05-19 03:37:25.198177419 +0200
  17121. @@ -14,6 +14,7 @@
  17122. #include <linux/export.h>
  17123. #include <linux/init.h>
  17124. #include <linux/sched.h>
  17125. +#include <linux/sched/rt.h>
  17126. #include <linux/fs.h>
  17127. #include <linux/tty.h>
  17128. #include <linux/binfmts.h>
  17129. @@ -352,13 +353,30 @@
  17130. return false;
  17131. }
  17132. +static inline struct sigqueue *get_task_cache(struct task_struct *t)
  17133. +{
  17134. + struct sigqueue *q = t->sigqueue_cache;
  17135. +
  17136. + if (cmpxchg(&t->sigqueue_cache, q, NULL) != q)
  17137. + return NULL;
  17138. + return q;
  17139. +}
  17140. +
  17141. +static inline int put_task_cache(struct task_struct *t, struct sigqueue *q)
  17142. +{
  17143. + if (cmpxchg(&t->sigqueue_cache, NULL, q) == NULL)
  17144. + return 0;
  17145. + return 1;
  17146. +}
  17147. +
  17148. /*
  17149. * allocate a new signal queue record
  17150. * - this may be called without locks if and only if t == current, otherwise an
  17151. * appropriate lock must be held to stop the target task from exiting
  17152. */
  17153. static struct sigqueue *
  17154. -__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
  17155. +__sigqueue_do_alloc(int sig, struct task_struct *t, gfp_t flags,
  17156. + int override_rlimit, int fromslab)
  17157. {
  17158. struct sigqueue *q = NULL;
  17159. struct user_struct *user;
  17160. @@ -375,7 +393,10 @@
  17161. if (override_rlimit ||
  17162. atomic_read(&user->sigpending) <=
  17163. task_rlimit(t, RLIMIT_SIGPENDING)) {
  17164. - q = kmem_cache_alloc(sigqueue_cachep, flags);
  17165. + if (!fromslab)
  17166. + q = get_task_cache(t);
  17167. + if (!q)
  17168. + q = kmem_cache_alloc(sigqueue_cachep, flags);
  17169. } else {
  17170. print_dropped_signal(sig);
  17171. }
  17172. @@ -392,6 +413,13 @@
  17173. return q;
  17174. }
  17175. +static struct sigqueue *
  17176. +__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags,
  17177. + int override_rlimit)
  17178. +{
  17179. + return __sigqueue_do_alloc(sig, t, flags, override_rlimit, 0);
  17180. +}
  17181. +
  17182. static void __sigqueue_free(struct sigqueue *q)
  17183. {
  17184. if (q->flags & SIGQUEUE_PREALLOC)
  17185. @@ -401,6 +429,21 @@
  17186. kmem_cache_free(sigqueue_cachep, q);
  17187. }
  17188. +static void sigqueue_free_current(struct sigqueue *q)
  17189. +{
  17190. + struct user_struct *up;
  17191. +
  17192. + if (q->flags & SIGQUEUE_PREALLOC)
  17193. + return;
  17194. +
  17195. + up = q->user;
  17196. + if (rt_prio(current->normal_prio) && !put_task_cache(current, q)) {
  17197. + atomic_dec(&up->sigpending);
  17198. + free_uid(up);
  17199. + } else
  17200. + __sigqueue_free(q);
  17201. +}
  17202. +
  17203. void flush_sigqueue(struct sigpending *queue)
  17204. {
  17205. struct sigqueue *q;
  17206. @@ -414,6 +457,21 @@
  17207. }
  17208. /*
  17209. + * Called from __exit_signal. Flush tsk->pending and
  17210. + * tsk->sigqueue_cache
  17211. + */
  17212. +void flush_task_sigqueue(struct task_struct *tsk)
  17213. +{
  17214. + struct sigqueue *q;
  17215. +
  17216. + flush_sigqueue(&tsk->pending);
  17217. +
  17218. + q = get_task_cache(tsk);
  17219. + if (q)
  17220. + kmem_cache_free(sigqueue_cachep, q);
  17221. +}
  17222. +
  17223. +/*
  17224. * Flush all pending signals for this kthread.
  17225. */
  17226. void flush_signals(struct task_struct *t)
  17227. @@ -525,7 +583,7 @@
  17228. still_pending:
  17229. list_del_init(&first->list);
  17230. copy_siginfo(info, &first->info);
  17231. - __sigqueue_free(first);
  17232. + sigqueue_free_current(first);
  17233. } else {
  17234. /*
  17235. * Ok, it wasn't in the queue. This must be
  17236. @@ -560,6 +618,8 @@
  17237. {
  17238. int signr;
  17239. + WARN_ON_ONCE(tsk != current);
  17240. +
  17241. /* We only dequeue private signals from ourselves, we don't let
  17242. * signalfd steal them
  17243. */
  17244. @@ -1156,8 +1216,8 @@
  17245. * We don't want to have recursive SIGSEGV's etc, for example,
  17246. * that is why we also clear SIGNAL_UNKILLABLE.
  17247. */
  17248. -int
  17249. -force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  17250. +static int
  17251. +do_force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  17252. {
  17253. unsigned long int flags;
  17254. int ret, blocked, ignored;
  17255. @@ -1182,6 +1242,39 @@
  17256. return ret;
  17257. }
  17258. +int force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  17259. +{
  17260. +/*
  17261. + * On some archs, PREEMPT_RT has to delay sending a signal from a trap
  17262. + * since it can not enable preemption, and the signal code's spin_locks
  17263. + * turn into mutexes. Instead, it must set TIF_NOTIFY_RESUME which will
  17264. + * send the signal on exit of the trap.
  17265. + */
  17266. +#ifdef ARCH_RT_DELAYS_SIGNAL_SEND
  17267. + if (in_atomic()) {
  17268. + if (WARN_ON_ONCE(t != current))
  17269. + return 0;
  17270. + if (WARN_ON_ONCE(t->forced_info.si_signo))
  17271. + return 0;
  17272. +
  17273. + if (is_si_special(info)) {
  17274. + WARN_ON_ONCE(info != SEND_SIG_PRIV);
  17275. + t->forced_info.si_signo = sig;
  17276. + t->forced_info.si_errno = 0;
  17277. + t->forced_info.si_code = SI_KERNEL;
  17278. + t->forced_info.si_pid = 0;
  17279. + t->forced_info.si_uid = 0;
  17280. + } else {
  17281. + t->forced_info = *info;
  17282. + }
  17283. +
  17284. + set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
  17285. + return 0;
  17286. + }
  17287. +#endif
  17288. + return do_force_sig_info(sig, info, t);
  17289. +}
  17290. +
  17291. /*
  17292. * Nuke all other threads in the group.
  17293. */
  17294. @@ -1216,12 +1309,12 @@
  17295. * Disable interrupts early to avoid deadlocks.
  17296. * See rcu_read_unlock() comment header for details.
  17297. */
  17298. - local_irq_save(*flags);
  17299. + local_irq_save_nort(*flags);
  17300. rcu_read_lock();
  17301. sighand = rcu_dereference(tsk->sighand);
  17302. if (unlikely(sighand == NULL)) {
  17303. rcu_read_unlock();
  17304. - local_irq_restore(*flags);
  17305. + local_irq_restore_nort(*flags);
  17306. break;
  17307. }
  17308. /*
  17309. @@ -1242,7 +1335,7 @@
  17310. }
  17311. spin_unlock(&sighand->siglock);
  17312. rcu_read_unlock();
  17313. - local_irq_restore(*flags);
  17314. + local_irq_restore_nort(*flags);
  17315. }
  17316. return sighand;
  17317. @@ -1485,7 +1578,8 @@
  17318. */
  17319. struct sigqueue *sigqueue_alloc(void)
  17320. {
  17321. - struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
  17322. + /* Preallocated sigqueue objects always from the slabcache ! */
  17323. + struct sigqueue *q = __sigqueue_do_alloc(-1, current, GFP_KERNEL, 0, 1);
  17324. if (q)
  17325. q->flags |= SIGQUEUE_PREALLOC;
  17326. @@ -1846,15 +1940,7 @@
  17327. if (gstop_done && ptrace_reparented(current))
  17328. do_notify_parent_cldstop(current, false, why);
  17329. - /*
  17330. - * Don't want to allow preemption here, because
  17331. - * sys_ptrace() needs this task to be inactive.
  17332. - *
  17333. - * XXX: implement read_unlock_no_resched().
  17334. - */
  17335. - preempt_disable();
  17336. read_unlock(&tasklist_lock);
  17337. - preempt_enable_no_resched();
  17338. freezable_schedule();
  17339. } else {
  17340. /*
  17341. diff -Nur linux-4.9.28.orig/kernel/softirq.c linux-4.9.28/kernel/softirq.c
  17342. --- linux-4.9.28.orig/kernel/softirq.c 2017-05-14 14:00:37.000000000 +0200
  17343. +++ linux-4.9.28/kernel/softirq.c 2017-05-19 03:37:25.198177419 +0200
  17344. @@ -21,10 +21,12 @@
  17345. #include <linux/freezer.h>
  17346. #include <linux/kthread.h>
  17347. #include <linux/rcupdate.h>
  17348. +#include <linux/delay.h>
  17349. #include <linux/ftrace.h>
  17350. #include <linux/smp.h>
  17351. #include <linux/smpboot.h>
  17352. #include <linux/tick.h>
  17353. +#include <linux/locallock.h>
  17354. #include <linux/irq.h>
  17355. #define CREATE_TRACE_POINTS
  17356. @@ -56,12 +58,108 @@
  17357. static struct softirq_action softirq_vec[NR_SOFTIRQS] __cacheline_aligned_in_smp;
  17358. DEFINE_PER_CPU(struct task_struct *, ksoftirqd);
  17359. +#ifdef CONFIG_PREEMPT_RT_FULL
  17360. +#define TIMER_SOFTIRQS ((1 << TIMER_SOFTIRQ) | (1 << HRTIMER_SOFTIRQ))
  17361. +DEFINE_PER_CPU(struct task_struct *, ktimer_softirqd);
  17362. +#endif
  17363. const char * const softirq_to_name[NR_SOFTIRQS] = {
  17364. "HI", "TIMER", "NET_TX", "NET_RX", "BLOCK", "IRQ_POLL",
  17365. "TASKLET", "SCHED", "HRTIMER", "RCU"
  17366. };
  17367. +#ifdef CONFIG_NO_HZ_COMMON
  17368. +# ifdef CONFIG_PREEMPT_RT_FULL
  17369. +
  17370. +struct softirq_runner {
  17371. + struct task_struct *runner[NR_SOFTIRQS];
  17372. +};
  17373. +
  17374. +static DEFINE_PER_CPU(struct softirq_runner, softirq_runners);
  17375. +
  17376. +static inline void softirq_set_runner(unsigned int sirq)
  17377. +{
  17378. + struct softirq_runner *sr = this_cpu_ptr(&softirq_runners);
  17379. +
  17380. + sr->runner[sirq] = current;
  17381. +}
  17382. +
  17383. +static inline void softirq_clr_runner(unsigned int sirq)
  17384. +{
  17385. + struct softirq_runner *sr = this_cpu_ptr(&softirq_runners);
  17386. +
  17387. + sr->runner[sirq] = NULL;
  17388. +}
  17389. +
  17390. +/*
  17391. + * On preempt-rt a softirq running context might be blocked on a
  17392. + * lock. There might be no other runnable task on this CPU because the
  17393. + * lock owner runs on some other CPU. So we have to go into idle with
  17394. + * the pending bit set. Therefor we need to check this otherwise we
  17395. + * warn about false positives which confuses users and defeats the
  17396. + * whole purpose of this test.
  17397. + *
  17398. + * This code is called with interrupts disabled.
  17399. + */
  17400. +void softirq_check_pending_idle(void)
  17401. +{
  17402. + static int rate_limit;
  17403. + struct softirq_runner *sr = this_cpu_ptr(&softirq_runners);
  17404. + u32 warnpending;
  17405. + int i;
  17406. +
  17407. + if (rate_limit >= 10)
  17408. + return;
  17409. +
  17410. + warnpending = local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK;
  17411. + for (i = 0; i < NR_SOFTIRQS; i++) {
  17412. + struct task_struct *tsk = sr->runner[i];
  17413. +
  17414. + /*
  17415. + * The wakeup code in rtmutex.c wakes up the task
  17416. + * _before_ it sets pi_blocked_on to NULL under
  17417. + * tsk->pi_lock. So we need to check for both: state
  17418. + * and pi_blocked_on.
  17419. + */
  17420. + if (tsk) {
  17421. + raw_spin_lock(&tsk->pi_lock);
  17422. + if (tsk->pi_blocked_on || tsk->state == TASK_RUNNING) {
  17423. + /* Clear all bits pending in that task */
  17424. + warnpending &= ~(tsk->softirqs_raised);
  17425. + warnpending &= ~(1 << i);
  17426. + }
  17427. + raw_spin_unlock(&tsk->pi_lock);
  17428. + }
  17429. + }
  17430. +
  17431. + if (warnpending) {
  17432. + printk(KERN_ERR "NOHZ: local_softirq_pending %02x\n",
  17433. + warnpending);
  17434. + rate_limit++;
  17435. + }
  17436. +}
  17437. +# else
  17438. +/*
  17439. + * On !PREEMPT_RT we just printk rate limited:
  17440. + */
  17441. +void softirq_check_pending_idle(void)
  17442. +{
  17443. + static int rate_limit;
  17444. +
  17445. + if (rate_limit < 10 &&
  17446. + (local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK)) {
  17447. + printk(KERN_ERR "NOHZ: local_softirq_pending %02x\n",
  17448. + local_softirq_pending());
  17449. + rate_limit++;
  17450. + }
  17451. +}
  17452. +# endif
  17453. +
  17454. +#else /* !CONFIG_NO_HZ_COMMON */
  17455. +static inline void softirq_set_runner(unsigned int sirq) { }
  17456. +static inline void softirq_clr_runner(unsigned int sirq) { }
  17457. +#endif
  17458. +
  17459. /*
  17460. * we cannot loop indefinitely here to avoid userspace starvation,
  17461. * but we also don't want to introduce a worst case 1/HZ latency
  17462. @@ -77,6 +175,38 @@
  17463. wake_up_process(tsk);
  17464. }
  17465. +#ifdef CONFIG_PREEMPT_RT_FULL
  17466. +static void wakeup_timer_softirqd(void)
  17467. +{
  17468. + /* Interrupts are disabled: no need to stop preemption */
  17469. + struct task_struct *tsk = __this_cpu_read(ktimer_softirqd);
  17470. +
  17471. + if (tsk && tsk->state != TASK_RUNNING)
  17472. + wake_up_process(tsk);
  17473. +}
  17474. +#endif
  17475. +
  17476. +static void handle_softirq(unsigned int vec_nr)
  17477. +{
  17478. + struct softirq_action *h = softirq_vec + vec_nr;
  17479. + int prev_count;
  17480. +
  17481. + prev_count = preempt_count();
  17482. +
  17483. + kstat_incr_softirqs_this_cpu(vec_nr);
  17484. +
  17485. + trace_softirq_entry(vec_nr);
  17486. + h->action(h);
  17487. + trace_softirq_exit(vec_nr);
  17488. + if (unlikely(prev_count != preempt_count())) {
  17489. + pr_err("huh, entered softirq %u %s %p with preempt_count %08x, exited with %08x?\n",
  17490. + vec_nr, softirq_to_name[vec_nr], h->action,
  17491. + prev_count, preempt_count());
  17492. + preempt_count_set(prev_count);
  17493. + }
  17494. +}
  17495. +
  17496. +#ifndef CONFIG_PREEMPT_RT_FULL
  17497. /*
  17498. * If ksoftirqd is scheduled, we do not want to process pending softirqs
  17499. * right now. Let ksoftirqd handle this at its own rate, to get fairness.
  17500. @@ -88,6 +218,47 @@
  17501. return tsk && (tsk->state == TASK_RUNNING);
  17502. }
  17503. +static inline int ksoftirqd_softirq_pending(void)
  17504. +{
  17505. + return local_softirq_pending();
  17506. +}
  17507. +
  17508. +static void handle_pending_softirqs(u32 pending)
  17509. +{
  17510. + struct softirq_action *h = softirq_vec;
  17511. + int softirq_bit;
  17512. +
  17513. + local_irq_enable();
  17514. +
  17515. + h = softirq_vec;
  17516. +
  17517. + while ((softirq_bit = ffs(pending))) {
  17518. + unsigned int vec_nr;
  17519. +
  17520. + h += softirq_bit - 1;
  17521. + vec_nr = h - softirq_vec;
  17522. + handle_softirq(vec_nr);
  17523. +
  17524. + h++;
  17525. + pending >>= softirq_bit;
  17526. + }
  17527. +
  17528. + rcu_bh_qs();
  17529. + local_irq_disable();
  17530. +}
  17531. +
  17532. +static void run_ksoftirqd(unsigned int cpu)
  17533. +{
  17534. + local_irq_disable();
  17535. + if (ksoftirqd_softirq_pending()) {
  17536. + __do_softirq();
  17537. + local_irq_enable();
  17538. + cond_resched_rcu_qs();
  17539. + return;
  17540. + }
  17541. + local_irq_enable();
  17542. +}
  17543. +
  17544. /*
  17545. * preempt_count and SOFTIRQ_OFFSET usage:
  17546. * - preempt_count is changed by SOFTIRQ_OFFSET on entering or leaving
  17547. @@ -243,10 +414,8 @@
  17548. unsigned long end = jiffies + MAX_SOFTIRQ_TIME;
  17549. unsigned long old_flags = current->flags;
  17550. int max_restart = MAX_SOFTIRQ_RESTART;
  17551. - struct softirq_action *h;
  17552. bool in_hardirq;
  17553. __u32 pending;
  17554. - int softirq_bit;
  17555. /*
  17556. * Mask out PF_MEMALLOC s current task context is borrowed for the
  17557. @@ -265,36 +434,7 @@
  17558. /* Reset the pending bitmask before enabling irqs */
  17559. set_softirq_pending(0);
  17560. - local_irq_enable();
  17561. -
  17562. - h = softirq_vec;
  17563. -
  17564. - while ((softirq_bit = ffs(pending))) {
  17565. - unsigned int vec_nr;
  17566. - int prev_count;
  17567. -
  17568. - h += softirq_bit - 1;
  17569. -
  17570. - vec_nr = h - softirq_vec;
  17571. - prev_count = preempt_count();
  17572. -
  17573. - kstat_incr_softirqs_this_cpu(vec_nr);
  17574. -
  17575. - trace_softirq_entry(vec_nr);
  17576. - h->action(h);
  17577. - trace_softirq_exit(vec_nr);
  17578. - if (unlikely(prev_count != preempt_count())) {
  17579. - pr_err("huh, entered softirq %u %s %p with preempt_count %08x, exited with %08x?\n",
  17580. - vec_nr, softirq_to_name[vec_nr], h->action,
  17581. - prev_count, preempt_count());
  17582. - preempt_count_set(prev_count);
  17583. - }
  17584. - h++;
  17585. - pending >>= softirq_bit;
  17586. - }
  17587. -
  17588. - rcu_bh_qs();
  17589. - local_irq_disable();
  17590. + handle_pending_softirqs(pending);
  17591. pending = local_softirq_pending();
  17592. if (pending) {
  17593. @@ -331,6 +471,309 @@
  17594. }
  17595. /*
  17596. + * This function must run with irqs disabled!
  17597. + */
  17598. +void raise_softirq_irqoff(unsigned int nr)
  17599. +{
  17600. + __raise_softirq_irqoff(nr);
  17601. +
  17602. + /*
  17603. + * If we're in an interrupt or softirq, we're done
  17604. + * (this also catches softirq-disabled code). We will
  17605. + * actually run the softirq once we return from
  17606. + * the irq or softirq.
  17607. + *
  17608. + * Otherwise we wake up ksoftirqd to make sure we
  17609. + * schedule the softirq soon.
  17610. + */
  17611. + if (!in_interrupt())
  17612. + wakeup_softirqd();
  17613. +}
  17614. +
  17615. +void __raise_softirq_irqoff(unsigned int nr)
  17616. +{
  17617. + trace_softirq_raise(nr);
  17618. + or_softirq_pending(1UL << nr);
  17619. +}
  17620. +
  17621. +static inline void local_bh_disable_nort(void) { local_bh_disable(); }
  17622. +static inline void _local_bh_enable_nort(void) { _local_bh_enable(); }
  17623. +static void ksoftirqd_set_sched_params(unsigned int cpu) { }
  17624. +
  17625. +#else /* !PREEMPT_RT_FULL */
  17626. +
  17627. +/*
  17628. + * On RT we serialize softirq execution with a cpu local lock per softirq
  17629. + */
  17630. +static DEFINE_PER_CPU(struct local_irq_lock [NR_SOFTIRQS], local_softirq_locks);
  17631. +
  17632. +void __init softirq_early_init(void)
  17633. +{
  17634. + int i;
  17635. +
  17636. + for (i = 0; i < NR_SOFTIRQS; i++)
  17637. + local_irq_lock_init(local_softirq_locks[i]);
  17638. +}
  17639. +
  17640. +static void lock_softirq(int which)
  17641. +{
  17642. + local_lock(local_softirq_locks[which]);
  17643. +}
  17644. +
  17645. +static void unlock_softirq(int which)
  17646. +{
  17647. + local_unlock(local_softirq_locks[which]);
  17648. +}
  17649. +
  17650. +static void do_single_softirq(int which)
  17651. +{
  17652. + unsigned long old_flags = current->flags;
  17653. +
  17654. + current->flags &= ~PF_MEMALLOC;
  17655. + vtime_account_irq_enter(current);
  17656. + current->flags |= PF_IN_SOFTIRQ;
  17657. + lockdep_softirq_enter();
  17658. + local_irq_enable();
  17659. + handle_softirq(which);
  17660. + local_irq_disable();
  17661. + lockdep_softirq_exit();
  17662. + current->flags &= ~PF_IN_SOFTIRQ;
  17663. + vtime_account_irq_enter(current);
  17664. + tsk_restore_flags(current, old_flags, PF_MEMALLOC);
  17665. +}
  17666. +
  17667. +/*
  17668. + * Called with interrupts disabled. Process softirqs which were raised
  17669. + * in current context (or on behalf of ksoftirqd).
  17670. + */
  17671. +static void do_current_softirqs(void)
  17672. +{
  17673. + while (current->softirqs_raised) {
  17674. + int i = __ffs(current->softirqs_raised);
  17675. + unsigned int pending, mask = (1U << i);
  17676. +
  17677. + current->softirqs_raised &= ~mask;
  17678. + local_irq_enable();
  17679. +
  17680. + /*
  17681. + * If the lock is contended, we boost the owner to
  17682. + * process the softirq or leave the critical section
  17683. + * now.
  17684. + */
  17685. + lock_softirq(i);
  17686. + local_irq_disable();
  17687. + softirq_set_runner(i);
  17688. + /*
  17689. + * Check with the local_softirq_pending() bits,
  17690. + * whether we need to process this still or if someone
  17691. + * else took care of it.
  17692. + */
  17693. + pending = local_softirq_pending();
  17694. + if (pending & mask) {
  17695. + set_softirq_pending(pending & ~mask);
  17696. + do_single_softirq(i);
  17697. + }
  17698. + softirq_clr_runner(i);
  17699. + WARN_ON(current->softirq_nestcnt != 1);
  17700. + local_irq_enable();
  17701. + unlock_softirq(i);
  17702. + local_irq_disable();
  17703. + }
  17704. +}
  17705. +
  17706. +void __local_bh_disable(void)
  17707. +{
  17708. + if (++current->softirq_nestcnt == 1)
  17709. + migrate_disable();
  17710. +}
  17711. +EXPORT_SYMBOL(__local_bh_disable);
  17712. +
  17713. +void __local_bh_enable(void)
  17714. +{
  17715. + if (WARN_ON(current->softirq_nestcnt == 0))
  17716. + return;
  17717. +
  17718. + local_irq_disable();
  17719. + if (current->softirq_nestcnt == 1 && current->softirqs_raised)
  17720. + do_current_softirqs();
  17721. + local_irq_enable();
  17722. +
  17723. + if (--current->softirq_nestcnt == 0)
  17724. + migrate_enable();
  17725. +}
  17726. +EXPORT_SYMBOL(__local_bh_enable);
  17727. +
  17728. +void _local_bh_enable(void)
  17729. +{
  17730. + if (WARN_ON(current->softirq_nestcnt == 0))
  17731. + return;
  17732. + if (--current->softirq_nestcnt == 0)
  17733. + migrate_enable();
  17734. +}
  17735. +EXPORT_SYMBOL(_local_bh_enable);
  17736. +
  17737. +int in_serving_softirq(void)
  17738. +{
  17739. + return current->flags & PF_IN_SOFTIRQ;
  17740. +}
  17741. +EXPORT_SYMBOL(in_serving_softirq);
  17742. +
  17743. +/* Called with preemption disabled */
  17744. +static void run_ksoftirqd(unsigned int cpu)
  17745. +{
  17746. + local_irq_disable();
  17747. + current->softirq_nestcnt++;
  17748. +
  17749. + do_current_softirqs();
  17750. + current->softirq_nestcnt--;
  17751. + local_irq_enable();
  17752. + cond_resched_rcu_qs();
  17753. +}
  17754. +
  17755. +/*
  17756. + * Called from netif_rx_ni(). Preemption enabled, but migration
  17757. + * disabled. So the cpu can't go away under us.
  17758. + */
  17759. +void thread_do_softirq(void)
  17760. +{
  17761. + if (!in_serving_softirq() && current->softirqs_raised) {
  17762. + current->softirq_nestcnt++;
  17763. + do_current_softirqs();
  17764. + current->softirq_nestcnt--;
  17765. + }
  17766. +}
  17767. +
  17768. +static void do_raise_softirq_irqoff(unsigned int nr)
  17769. +{
  17770. + unsigned int mask;
  17771. +
  17772. + mask = 1UL << nr;
  17773. +
  17774. + trace_softirq_raise(nr);
  17775. + or_softirq_pending(mask);
  17776. +
  17777. + /*
  17778. + * If we are not in a hard interrupt and inside a bh disabled
  17779. + * region, we simply raise the flag on current. local_bh_enable()
  17780. + * will make sure that the softirq is executed. Otherwise we
  17781. + * delegate it to ksoftirqd.
  17782. + */
  17783. + if (!in_irq() && current->softirq_nestcnt)
  17784. + current->softirqs_raised |= mask;
  17785. + else if (!__this_cpu_read(ksoftirqd) || !__this_cpu_read(ktimer_softirqd))
  17786. + return;
  17787. +
  17788. + if (mask & TIMER_SOFTIRQS)
  17789. + __this_cpu_read(ktimer_softirqd)->softirqs_raised |= mask;
  17790. + else
  17791. + __this_cpu_read(ksoftirqd)->softirqs_raised |= mask;
  17792. +}
  17793. +
  17794. +static void wakeup_proper_softirq(unsigned int nr)
  17795. +{
  17796. + if ((1UL << nr) & TIMER_SOFTIRQS)
  17797. + wakeup_timer_softirqd();
  17798. + else
  17799. + wakeup_softirqd();
  17800. +}
  17801. +
  17802. +void __raise_softirq_irqoff(unsigned int nr)
  17803. +{
  17804. + do_raise_softirq_irqoff(nr);
  17805. + if (!in_irq() && !current->softirq_nestcnt)
  17806. + wakeup_proper_softirq(nr);
  17807. +}
  17808. +
  17809. +/*
  17810. + * Same as __raise_softirq_irqoff() but will process them in ksoftirqd
  17811. + */
  17812. +void __raise_softirq_irqoff_ksoft(unsigned int nr)
  17813. +{
  17814. + unsigned int mask;
  17815. +
  17816. + if (WARN_ON_ONCE(!__this_cpu_read(ksoftirqd) ||
  17817. + !__this_cpu_read(ktimer_softirqd)))
  17818. + return;
  17819. + mask = 1UL << nr;
  17820. +
  17821. + trace_softirq_raise(nr);
  17822. + or_softirq_pending(mask);
  17823. + if (mask & TIMER_SOFTIRQS)
  17824. + __this_cpu_read(ktimer_softirqd)->softirqs_raised |= mask;
  17825. + else
  17826. + __this_cpu_read(ksoftirqd)->softirqs_raised |= mask;
  17827. + wakeup_proper_softirq(nr);
  17828. +}
  17829. +
  17830. +/*
  17831. + * This function must run with irqs disabled!
  17832. + */
  17833. +void raise_softirq_irqoff(unsigned int nr)
  17834. +{
  17835. + do_raise_softirq_irqoff(nr);
  17836. +
  17837. + /*
  17838. + * If we're in an hard interrupt we let irq return code deal
  17839. + * with the wakeup of ksoftirqd.
  17840. + */
  17841. + if (in_irq())
  17842. + return;
  17843. + /*
  17844. + * If we are in thread context but outside of a bh disabled
  17845. + * region, we need to wake ksoftirqd as well.
  17846. + *
  17847. + * CHECKME: Some of the places which do that could be wrapped
  17848. + * into local_bh_disable/enable pairs. Though it's unclear
  17849. + * whether this is worth the effort. To find those places just
  17850. + * raise a WARN() if the condition is met.
  17851. + */
  17852. + if (!current->softirq_nestcnt)
  17853. + wakeup_proper_softirq(nr);
  17854. +}
  17855. +
  17856. +static inline int ksoftirqd_softirq_pending(void)
  17857. +{
  17858. + return current->softirqs_raised;
  17859. +}
  17860. +
  17861. +static inline void local_bh_disable_nort(void) { }
  17862. +static inline void _local_bh_enable_nort(void) { }
  17863. +
  17864. +static inline void ksoftirqd_set_sched_params(unsigned int cpu)
  17865. +{
  17866. + /* Take over all but timer pending softirqs when starting */
  17867. + local_irq_disable();
  17868. + current->softirqs_raised = local_softirq_pending() & ~TIMER_SOFTIRQS;
  17869. + local_irq_enable();
  17870. +}
  17871. +
  17872. +static inline void ktimer_softirqd_set_sched_params(unsigned int cpu)
  17873. +{
  17874. + struct sched_param param = { .sched_priority = 1 };
  17875. +
  17876. + sched_setscheduler(current, SCHED_FIFO, &param);
  17877. +
  17878. + /* Take over timer pending softirqs when starting */
  17879. + local_irq_disable();
  17880. + current->softirqs_raised = local_softirq_pending() & TIMER_SOFTIRQS;
  17881. + local_irq_enable();
  17882. +}
  17883. +
  17884. +static inline void ktimer_softirqd_clr_sched_params(unsigned int cpu,
  17885. + bool online)
  17886. +{
  17887. + struct sched_param param = { .sched_priority = 0 };
  17888. +
  17889. + sched_setscheduler(current, SCHED_NORMAL, &param);
  17890. +}
  17891. +
  17892. +static int ktimer_softirqd_should_run(unsigned int cpu)
  17893. +{
  17894. + return current->softirqs_raised;
  17895. +}
  17896. +
  17897. +#endif /* PREEMPT_RT_FULL */
  17898. +/*
  17899. * Enter an interrupt context.
  17900. */
  17901. void irq_enter(void)
  17902. @@ -341,9 +784,9 @@
  17903. * Prevent raise_softirq from needlessly waking up ksoftirqd
  17904. * here, as softirq will be serviced on return from interrupt.
  17905. */
  17906. - local_bh_disable();
  17907. + local_bh_disable_nort();
  17908. tick_irq_enter();
  17909. - _local_bh_enable();
  17910. + _local_bh_enable_nort();
  17911. }
  17912. __irq_enter();
  17913. @@ -351,6 +794,7 @@
  17914. static inline void invoke_softirq(void)
  17915. {
  17916. +#ifndef CONFIG_PREEMPT_RT_FULL
  17917. if (ksoftirqd_running())
  17918. return;
  17919. @@ -373,6 +817,18 @@
  17920. } else {
  17921. wakeup_softirqd();
  17922. }
  17923. +#else /* PREEMPT_RT_FULL */
  17924. + unsigned long flags;
  17925. +
  17926. + local_irq_save(flags);
  17927. + if (__this_cpu_read(ksoftirqd) &&
  17928. + __this_cpu_read(ksoftirqd)->softirqs_raised)
  17929. + wakeup_softirqd();
  17930. + if (__this_cpu_read(ktimer_softirqd) &&
  17931. + __this_cpu_read(ktimer_softirqd)->softirqs_raised)
  17932. + wakeup_timer_softirqd();
  17933. + local_irq_restore(flags);
  17934. +#endif
  17935. }
  17936. static inline void tick_irq_exit(void)
  17937. @@ -409,26 +865,6 @@
  17938. trace_hardirq_exit(); /* must be last! */
  17939. }
  17940. -/*
  17941. - * This function must run with irqs disabled!
  17942. - */
  17943. -inline void raise_softirq_irqoff(unsigned int nr)
  17944. -{
  17945. - __raise_softirq_irqoff(nr);
  17946. -
  17947. - /*
  17948. - * If we're in an interrupt or softirq, we're done
  17949. - * (this also catches softirq-disabled code). We will
  17950. - * actually run the softirq once we return from
  17951. - * the irq or softirq.
  17952. - *
  17953. - * Otherwise we wake up ksoftirqd to make sure we
  17954. - * schedule the softirq soon.
  17955. - */
  17956. - if (!in_interrupt())
  17957. - wakeup_softirqd();
  17958. -}
  17959. -
  17960. void raise_softirq(unsigned int nr)
  17961. {
  17962. unsigned long flags;
  17963. @@ -438,12 +874,6 @@
  17964. local_irq_restore(flags);
  17965. }
  17966. -void __raise_softirq_irqoff(unsigned int nr)
  17967. -{
  17968. - trace_softirq_raise(nr);
  17969. - or_softirq_pending(1UL << nr);
  17970. -}
  17971. -
  17972. void open_softirq(int nr, void (*action)(struct softirq_action *))
  17973. {
  17974. softirq_vec[nr].action = action;
  17975. @@ -460,15 +890,45 @@
  17976. static DEFINE_PER_CPU(struct tasklet_head, tasklet_vec);
  17977. static DEFINE_PER_CPU(struct tasklet_head, tasklet_hi_vec);
  17978. +static void inline
  17979. +__tasklet_common_schedule(struct tasklet_struct *t, struct tasklet_head *head, unsigned int nr)
  17980. +{
  17981. + if (tasklet_trylock(t)) {
  17982. +again:
  17983. + /* We may have been preempted before tasklet_trylock
  17984. + * and __tasklet_action may have already run.
  17985. + * So double check the sched bit while the takslet
  17986. + * is locked before adding it to the list.
  17987. + */
  17988. + if (test_bit(TASKLET_STATE_SCHED, &t->state)) {
  17989. + t->next = NULL;
  17990. + *head->tail = t;
  17991. + head->tail = &(t->next);
  17992. + raise_softirq_irqoff(nr);
  17993. + tasklet_unlock(t);
  17994. + } else {
  17995. + /* This is subtle. If we hit the corner case above
  17996. + * It is possible that we get preempted right here,
  17997. + * and another task has successfully called
  17998. + * tasklet_schedule(), then this function, and
  17999. + * failed on the trylock. Thus we must be sure
  18000. + * before releasing the tasklet lock, that the
  18001. + * SCHED_BIT is clear. Otherwise the tasklet
  18002. + * may get its SCHED_BIT set, but not added to the
  18003. + * list
  18004. + */
  18005. + if (!tasklet_tryunlock(t))
  18006. + goto again;
  18007. + }
  18008. + }
  18009. +}
  18010. +
  18011. void __tasklet_schedule(struct tasklet_struct *t)
  18012. {
  18013. unsigned long flags;
  18014. local_irq_save(flags);
  18015. - t->next = NULL;
  18016. - *__this_cpu_read(tasklet_vec.tail) = t;
  18017. - __this_cpu_write(tasklet_vec.tail, &(t->next));
  18018. - raise_softirq_irqoff(TASKLET_SOFTIRQ);
  18019. + __tasklet_common_schedule(t, this_cpu_ptr(&tasklet_vec), TASKLET_SOFTIRQ);
  18020. local_irq_restore(flags);
  18021. }
  18022. EXPORT_SYMBOL(__tasklet_schedule);
  18023. @@ -478,10 +938,7 @@
  18024. unsigned long flags;
  18025. local_irq_save(flags);
  18026. - t->next = NULL;
  18027. - *__this_cpu_read(tasklet_hi_vec.tail) = t;
  18028. - __this_cpu_write(tasklet_hi_vec.tail, &(t->next));
  18029. - raise_softirq_irqoff(HI_SOFTIRQ);
  18030. + __tasklet_common_schedule(t, this_cpu_ptr(&tasklet_hi_vec), HI_SOFTIRQ);
  18031. local_irq_restore(flags);
  18032. }
  18033. EXPORT_SYMBOL(__tasklet_hi_schedule);
  18034. @@ -490,82 +947,122 @@
  18035. {
  18036. BUG_ON(!irqs_disabled());
  18037. - t->next = __this_cpu_read(tasklet_hi_vec.head);
  18038. - __this_cpu_write(tasklet_hi_vec.head, t);
  18039. - __raise_softirq_irqoff(HI_SOFTIRQ);
  18040. + __tasklet_hi_schedule(t);
  18041. }
  18042. EXPORT_SYMBOL(__tasklet_hi_schedule_first);
  18043. -static __latent_entropy void tasklet_action(struct softirq_action *a)
  18044. +void tasklet_enable(struct tasklet_struct *t)
  18045. {
  18046. - struct tasklet_struct *list;
  18047. + if (!atomic_dec_and_test(&t->count))
  18048. + return;
  18049. + if (test_and_clear_bit(TASKLET_STATE_PENDING, &t->state))
  18050. + tasklet_schedule(t);
  18051. +}
  18052. +EXPORT_SYMBOL(tasklet_enable);
  18053. - local_irq_disable();
  18054. - list = __this_cpu_read(tasklet_vec.head);
  18055. - __this_cpu_write(tasklet_vec.head, NULL);
  18056. - __this_cpu_write(tasklet_vec.tail, this_cpu_ptr(&tasklet_vec.head));
  18057. - local_irq_enable();
  18058. +static void __tasklet_action(struct softirq_action *a,
  18059. + struct tasklet_struct *list)
  18060. +{
  18061. + int loops = 1000000;
  18062. while (list) {
  18063. struct tasklet_struct *t = list;
  18064. list = list->next;
  18065. - if (tasklet_trylock(t)) {
  18066. - if (!atomic_read(&t->count)) {
  18067. - if (!test_and_clear_bit(TASKLET_STATE_SCHED,
  18068. - &t->state))
  18069. - BUG();
  18070. - t->func(t->data);
  18071. - tasklet_unlock(t);
  18072. - continue;
  18073. - }
  18074. - tasklet_unlock(t);
  18075. + /*
  18076. + * Should always succeed - after a tasklist got on the
  18077. + * list (after getting the SCHED bit set from 0 to 1),
  18078. + * nothing but the tasklet softirq it got queued to can
  18079. + * lock it:
  18080. + */
  18081. + if (!tasklet_trylock(t)) {
  18082. + WARN_ON(1);
  18083. + continue;
  18084. }
  18085. - local_irq_disable();
  18086. t->next = NULL;
  18087. - *__this_cpu_read(tasklet_vec.tail) = t;
  18088. - __this_cpu_write(tasklet_vec.tail, &(t->next));
  18089. - __raise_softirq_irqoff(TASKLET_SOFTIRQ);
  18090. - local_irq_enable();
  18091. +
  18092. + /*
  18093. + * If we cannot handle the tasklet because it's disabled,
  18094. + * mark it as pending. tasklet_enable() will later
  18095. + * re-schedule the tasklet.
  18096. + */
  18097. + if (unlikely(atomic_read(&t->count))) {
  18098. +out_disabled:
  18099. + /* implicit unlock: */
  18100. + wmb();
  18101. + t->state = TASKLET_STATEF_PENDING;
  18102. + continue;
  18103. + }
  18104. +
  18105. + /*
  18106. + * After this point on the tasklet might be rescheduled
  18107. + * on another CPU, but it can only be added to another
  18108. + * CPU's tasklet list if we unlock the tasklet (which we
  18109. + * dont do yet).
  18110. + */
  18111. + if (!test_and_clear_bit(TASKLET_STATE_SCHED, &t->state))
  18112. + WARN_ON(1);
  18113. +
  18114. +again:
  18115. + t->func(t->data);
  18116. +
  18117. + /*
  18118. + * Try to unlock the tasklet. We must use cmpxchg, because
  18119. + * another CPU might have scheduled or disabled the tasklet.
  18120. + * We only allow the STATE_RUN -> 0 transition here.
  18121. + */
  18122. + while (!tasklet_tryunlock(t)) {
  18123. + /*
  18124. + * If it got disabled meanwhile, bail out:
  18125. + */
  18126. + if (atomic_read(&t->count))
  18127. + goto out_disabled;
  18128. + /*
  18129. + * If it got scheduled meanwhile, re-execute
  18130. + * the tasklet function:
  18131. + */
  18132. + if (test_and_clear_bit(TASKLET_STATE_SCHED, &t->state))
  18133. + goto again;
  18134. + if (!--loops) {
  18135. + printk("hm, tasklet state: %08lx\n", t->state);
  18136. + WARN_ON(1);
  18137. + tasklet_unlock(t);
  18138. + break;
  18139. + }
  18140. + }
  18141. }
  18142. }
  18143. +static void tasklet_action(struct softirq_action *a)
  18144. +{
  18145. + struct tasklet_struct *list;
  18146. +
  18147. + local_irq_disable();
  18148. +
  18149. + list = __this_cpu_read(tasklet_vec.head);
  18150. + __this_cpu_write(tasklet_vec.head, NULL);
  18151. + __this_cpu_write(tasklet_vec.tail, this_cpu_ptr(&tasklet_vec.head));
  18152. +
  18153. + local_irq_enable();
  18154. +
  18155. + __tasklet_action(a, list);
  18156. +}
  18157. +
  18158. static __latent_entropy void tasklet_hi_action(struct softirq_action *a)
  18159. {
  18160. struct tasklet_struct *list;
  18161. local_irq_disable();
  18162. +
  18163. list = __this_cpu_read(tasklet_hi_vec.head);
  18164. __this_cpu_write(tasklet_hi_vec.head, NULL);
  18165. __this_cpu_write(tasklet_hi_vec.tail, this_cpu_ptr(&tasklet_hi_vec.head));
  18166. - local_irq_enable();
  18167. - while (list) {
  18168. - struct tasklet_struct *t = list;
  18169. -
  18170. - list = list->next;
  18171. -
  18172. - if (tasklet_trylock(t)) {
  18173. - if (!atomic_read(&t->count)) {
  18174. - if (!test_and_clear_bit(TASKLET_STATE_SCHED,
  18175. - &t->state))
  18176. - BUG();
  18177. - t->func(t->data);
  18178. - tasklet_unlock(t);
  18179. - continue;
  18180. - }
  18181. - tasklet_unlock(t);
  18182. - }
  18183. + local_irq_enable();
  18184. - local_irq_disable();
  18185. - t->next = NULL;
  18186. - *__this_cpu_read(tasklet_hi_vec.tail) = t;
  18187. - __this_cpu_write(tasklet_hi_vec.tail, &(t->next));
  18188. - __raise_softirq_irqoff(HI_SOFTIRQ);
  18189. - local_irq_enable();
  18190. - }
  18191. + __tasklet_action(a, list);
  18192. }
  18193. void tasklet_init(struct tasklet_struct *t,
  18194. @@ -586,7 +1083,7 @@
  18195. while (test_and_set_bit(TASKLET_STATE_SCHED, &t->state)) {
  18196. do {
  18197. - yield();
  18198. + msleep(1);
  18199. } while (test_bit(TASKLET_STATE_SCHED, &t->state));
  18200. }
  18201. tasklet_unlock_wait(t);
  18202. @@ -660,25 +1157,26 @@
  18203. open_softirq(HI_SOFTIRQ, tasklet_hi_action);
  18204. }
  18205. -static int ksoftirqd_should_run(unsigned int cpu)
  18206. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  18207. +void tasklet_unlock_wait(struct tasklet_struct *t)
  18208. {
  18209. - return local_softirq_pending();
  18210. -}
  18211. -
  18212. -static void run_ksoftirqd(unsigned int cpu)
  18213. -{
  18214. - local_irq_disable();
  18215. - if (local_softirq_pending()) {
  18216. + while (test_bit(TASKLET_STATE_RUN, &(t)->state)) {
  18217. /*
  18218. - * We can safely run softirq on inline stack, as we are not deep
  18219. - * in the task stack here.
  18220. + * Hack for now to avoid this busy-loop:
  18221. */
  18222. - __do_softirq();
  18223. - local_irq_enable();
  18224. - cond_resched_rcu_qs();
  18225. - return;
  18226. +#ifdef CONFIG_PREEMPT_RT_FULL
  18227. + msleep(1);
  18228. +#else
  18229. + barrier();
  18230. +#endif
  18231. }
  18232. - local_irq_enable();
  18233. +}
  18234. +EXPORT_SYMBOL(tasklet_unlock_wait);
  18235. +#endif
  18236. +
  18237. +static int ksoftirqd_should_run(unsigned int cpu)
  18238. +{
  18239. + return ksoftirqd_softirq_pending();
  18240. }
  18241. #ifdef CONFIG_HOTPLUG_CPU
  18242. @@ -745,17 +1243,31 @@
  18243. static struct smp_hotplug_thread softirq_threads = {
  18244. .store = &ksoftirqd,
  18245. + .setup = ksoftirqd_set_sched_params,
  18246. .thread_should_run = ksoftirqd_should_run,
  18247. .thread_fn = run_ksoftirqd,
  18248. .thread_comm = "ksoftirqd/%u",
  18249. };
  18250. +#ifdef CONFIG_PREEMPT_RT_FULL
  18251. +static struct smp_hotplug_thread softirq_timer_threads = {
  18252. + .store = &ktimer_softirqd,
  18253. + .setup = ktimer_softirqd_set_sched_params,
  18254. + .cleanup = ktimer_softirqd_clr_sched_params,
  18255. + .thread_should_run = ktimer_softirqd_should_run,
  18256. + .thread_fn = run_ksoftirqd,
  18257. + .thread_comm = "ktimersoftd/%u",
  18258. +};
  18259. +#endif
  18260. +
  18261. static __init int spawn_ksoftirqd(void)
  18262. {
  18263. cpuhp_setup_state_nocalls(CPUHP_SOFTIRQ_DEAD, "softirq:dead", NULL,
  18264. takeover_tasklets);
  18265. BUG_ON(smpboot_register_percpu_thread(&softirq_threads));
  18266. -
  18267. +#ifdef CONFIG_PREEMPT_RT_FULL
  18268. + BUG_ON(smpboot_register_percpu_thread(&softirq_timer_threads));
  18269. +#endif
  18270. return 0;
  18271. }
  18272. early_initcall(spawn_ksoftirqd);
  18273. diff -Nur linux-4.9.28.orig/kernel/stop_machine.c linux-4.9.28/kernel/stop_machine.c
  18274. --- linux-4.9.28.orig/kernel/stop_machine.c 2017-05-14 14:00:37.000000000 +0200
  18275. +++ linux-4.9.28/kernel/stop_machine.c 2017-05-19 03:37:25.198177419 +0200
  18276. @@ -36,7 +36,7 @@
  18277. struct cpu_stopper {
  18278. struct task_struct *thread;
  18279. - spinlock_t lock;
  18280. + raw_spinlock_t lock;
  18281. bool enabled; /* is this stopper enabled? */
  18282. struct list_head works; /* list of pending works */
  18283. @@ -78,14 +78,14 @@
  18284. unsigned long flags;
  18285. bool enabled;
  18286. - spin_lock_irqsave(&stopper->lock, flags);
  18287. + raw_spin_lock_irqsave(&stopper->lock, flags);
  18288. enabled = stopper->enabled;
  18289. if (enabled)
  18290. __cpu_stop_queue_work(stopper, work);
  18291. else if (work->done)
  18292. cpu_stop_signal_done(work->done);
  18293. - spin_unlock_irqrestore(&stopper->lock, flags);
  18294. + raw_spin_unlock_irqrestore(&stopper->lock, flags);
  18295. return enabled;
  18296. }
  18297. @@ -231,8 +231,8 @@
  18298. struct cpu_stopper *stopper2 = per_cpu_ptr(&cpu_stopper, cpu2);
  18299. int err;
  18300. retry:
  18301. - spin_lock_irq(&stopper1->lock);
  18302. - spin_lock_nested(&stopper2->lock, SINGLE_DEPTH_NESTING);
  18303. + raw_spin_lock_irq(&stopper1->lock);
  18304. + raw_spin_lock_nested(&stopper2->lock, SINGLE_DEPTH_NESTING);
  18305. err = -ENOENT;
  18306. if (!stopper1->enabled || !stopper2->enabled)
  18307. @@ -255,8 +255,8 @@
  18308. __cpu_stop_queue_work(stopper1, work1);
  18309. __cpu_stop_queue_work(stopper2, work2);
  18310. unlock:
  18311. - spin_unlock(&stopper2->lock);
  18312. - spin_unlock_irq(&stopper1->lock);
  18313. + raw_spin_unlock(&stopper2->lock);
  18314. + raw_spin_unlock_irq(&stopper1->lock);
  18315. if (unlikely(err == -EDEADLK)) {
  18316. while (stop_cpus_in_progress)
  18317. @@ -448,9 +448,9 @@
  18318. unsigned long flags;
  18319. int run;
  18320. - spin_lock_irqsave(&stopper->lock, flags);
  18321. + raw_spin_lock_irqsave(&stopper->lock, flags);
  18322. run = !list_empty(&stopper->works);
  18323. - spin_unlock_irqrestore(&stopper->lock, flags);
  18324. + raw_spin_unlock_irqrestore(&stopper->lock, flags);
  18325. return run;
  18326. }
  18327. @@ -461,13 +461,13 @@
  18328. repeat:
  18329. work = NULL;
  18330. - spin_lock_irq(&stopper->lock);
  18331. + raw_spin_lock_irq(&stopper->lock);
  18332. if (!list_empty(&stopper->works)) {
  18333. work = list_first_entry(&stopper->works,
  18334. struct cpu_stop_work, list);
  18335. list_del_init(&work->list);
  18336. }
  18337. - spin_unlock_irq(&stopper->lock);
  18338. + raw_spin_unlock_irq(&stopper->lock);
  18339. if (work) {
  18340. cpu_stop_fn_t fn = work->fn;
  18341. @@ -475,6 +475,8 @@
  18342. struct cpu_stop_done *done = work->done;
  18343. int ret;
  18344. + /* XXX */
  18345. +
  18346. /* cpu stop callbacks must not sleep, make in_atomic() == T */
  18347. preempt_count_inc();
  18348. ret = fn(arg);
  18349. @@ -541,7 +543,7 @@
  18350. for_each_possible_cpu(cpu) {
  18351. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  18352. - spin_lock_init(&stopper->lock);
  18353. + raw_spin_lock_init(&stopper->lock);
  18354. INIT_LIST_HEAD(&stopper->works);
  18355. }
  18356. diff -Nur linux-4.9.28.orig/kernel/time/hrtimer.c linux-4.9.28/kernel/time/hrtimer.c
  18357. --- linux-4.9.28.orig/kernel/time/hrtimer.c 2017-05-14 14:00:37.000000000 +0200
  18358. +++ linux-4.9.28/kernel/time/hrtimer.c 2017-05-19 03:37:25.198177419 +0200
  18359. @@ -53,6 +53,7 @@
  18360. #include <asm/uaccess.h>
  18361. #include <trace/events/timer.h>
  18362. +#include <trace/events/hist.h>
  18363. #include "tick-internal.h"
  18364. @@ -695,6 +696,29 @@
  18365. retrigger_next_event(NULL);
  18366. }
  18367. +#ifdef CONFIG_PREEMPT_RT_FULL
  18368. +
  18369. +static struct swork_event clock_set_delay_work;
  18370. +
  18371. +static void run_clock_set_delay(struct swork_event *event)
  18372. +{
  18373. + clock_was_set();
  18374. +}
  18375. +
  18376. +void clock_was_set_delayed(void)
  18377. +{
  18378. + swork_queue(&clock_set_delay_work);
  18379. +}
  18380. +
  18381. +static __init int create_clock_set_delay_thread(void)
  18382. +{
  18383. + WARN_ON(swork_get());
  18384. + INIT_SWORK(&clock_set_delay_work, run_clock_set_delay);
  18385. + return 0;
  18386. +}
  18387. +early_initcall(create_clock_set_delay_thread);
  18388. +#else /* PREEMPT_RT_FULL */
  18389. +
  18390. static void clock_was_set_work(struct work_struct *work)
  18391. {
  18392. clock_was_set();
  18393. @@ -710,6 +734,7 @@
  18394. {
  18395. schedule_work(&hrtimer_work);
  18396. }
  18397. +#endif
  18398. #else
  18399. @@ -719,11 +744,8 @@
  18400. static inline void hrtimer_switch_to_hres(void) { }
  18401. static inline void
  18402. hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
  18403. -static inline int hrtimer_reprogram(struct hrtimer *timer,
  18404. - struct hrtimer_clock_base *base)
  18405. -{
  18406. - return 0;
  18407. -}
  18408. +static inline void hrtimer_reprogram(struct hrtimer *timer,
  18409. + struct hrtimer_clock_base *base) { }
  18410. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
  18411. static inline void retrigger_next_event(void *arg) { }
  18412. @@ -855,6 +877,32 @@
  18413. }
  18414. EXPORT_SYMBOL_GPL(hrtimer_forward);
  18415. +#ifdef CONFIG_PREEMPT_RT_BASE
  18416. +# define wake_up_timer_waiters(b) wake_up(&(b)->wait)
  18417. +
  18418. +/**
  18419. + * hrtimer_wait_for_timer - Wait for a running timer
  18420. + *
  18421. + * @timer: timer to wait for
  18422. + *
  18423. + * The function waits in case the timers callback function is
  18424. + * currently executed on the waitqueue of the timer base. The
  18425. + * waitqueue is woken up after the timer callback function has
  18426. + * finished execution.
  18427. + */
  18428. +void hrtimer_wait_for_timer(const struct hrtimer *timer)
  18429. +{
  18430. + struct hrtimer_clock_base *base = timer->base;
  18431. +
  18432. + if (base && base->cpu_base && !timer->irqsafe)
  18433. + wait_event(base->cpu_base->wait,
  18434. + !(hrtimer_callback_running(timer)));
  18435. +}
  18436. +
  18437. +#else
  18438. +# define wake_up_timer_waiters(b) do { } while (0)
  18439. +#endif
  18440. +
  18441. /*
  18442. * enqueue_hrtimer - internal function to (re)start a timer
  18443. *
  18444. @@ -896,6 +944,11 @@
  18445. if (!(state & HRTIMER_STATE_ENQUEUED))
  18446. return;
  18447. + if (unlikely(!list_empty(&timer->cb_entry))) {
  18448. + list_del_init(&timer->cb_entry);
  18449. + return;
  18450. + }
  18451. +
  18452. if (!timerqueue_del(&base->active, &timer->node))
  18453. cpu_base->active_bases &= ~(1 << base->index);
  18454. @@ -991,7 +1044,16 @@
  18455. new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
  18456. timer_stats_hrtimer_set_start_info(timer);
  18457. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  18458. + {
  18459. + ktime_t now = new_base->get_time();
  18460. + if (ktime_to_ns(tim) < ktime_to_ns(now))
  18461. + timer->praecox = now;
  18462. + else
  18463. + timer->praecox = ktime_set(0, 0);
  18464. + }
  18465. +#endif
  18466. leftmost = enqueue_hrtimer(timer, new_base);
  18467. if (!leftmost)
  18468. goto unlock;
  18469. @@ -1063,7 +1125,7 @@
  18470. if (ret >= 0)
  18471. return ret;
  18472. - cpu_relax();
  18473. + hrtimer_wait_for_timer(timer);
  18474. }
  18475. }
  18476. EXPORT_SYMBOL_GPL(hrtimer_cancel);
  18477. @@ -1127,6 +1189,7 @@
  18478. base = hrtimer_clockid_to_base(clock_id);
  18479. timer->base = &cpu_base->clock_base[base];
  18480. + INIT_LIST_HEAD(&timer->cb_entry);
  18481. timerqueue_init(&timer->node);
  18482. #ifdef CONFIG_TIMER_STATS
  18483. @@ -1167,6 +1230,7 @@
  18484. seq = raw_read_seqcount_begin(&cpu_base->seq);
  18485. if (timer->state != HRTIMER_STATE_INACTIVE ||
  18486. + cpu_base->running_soft == timer ||
  18487. cpu_base->running == timer)
  18488. return true;
  18489. @@ -1265,10 +1329,112 @@
  18490. cpu_base->running = NULL;
  18491. }
  18492. +#ifdef CONFIG_PREEMPT_RT_BASE
  18493. +static void hrtimer_rt_reprogram(int restart, struct hrtimer *timer,
  18494. + struct hrtimer_clock_base *base)
  18495. +{
  18496. + int leftmost;
  18497. +
  18498. + if (restart != HRTIMER_NORESTART &&
  18499. + !(timer->state & HRTIMER_STATE_ENQUEUED)) {
  18500. +
  18501. + leftmost = enqueue_hrtimer(timer, base);
  18502. + if (!leftmost)
  18503. + return;
  18504. +#ifdef CONFIG_HIGH_RES_TIMERS
  18505. + if (!hrtimer_is_hres_active(timer)) {
  18506. + /*
  18507. + * Kick to reschedule the next tick to handle the new timer
  18508. + * on dynticks target.
  18509. + */
  18510. + if (base->cpu_base->nohz_active)
  18511. + wake_up_nohz_cpu(base->cpu_base->cpu);
  18512. + } else {
  18513. +
  18514. + hrtimer_reprogram(timer, base);
  18515. + }
  18516. +#endif
  18517. + }
  18518. +}
  18519. +
  18520. +/*
  18521. + * The changes in mainline which removed the callback modes from
  18522. + * hrtimer are not yet working with -rt. The non wakeup_process()
  18523. + * based callbacks which involve sleeping locks need to be treated
  18524. + * seperately.
  18525. + */
  18526. +static void hrtimer_rt_run_pending(void)
  18527. +{
  18528. + enum hrtimer_restart (*fn)(struct hrtimer *);
  18529. + struct hrtimer_cpu_base *cpu_base;
  18530. + struct hrtimer_clock_base *base;
  18531. + struct hrtimer *timer;
  18532. + int index, restart;
  18533. +
  18534. + local_irq_disable();
  18535. + cpu_base = &per_cpu(hrtimer_bases, smp_processor_id());
  18536. +
  18537. + raw_spin_lock(&cpu_base->lock);
  18538. +
  18539. + for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
  18540. + base = &cpu_base->clock_base[index];
  18541. +
  18542. + while (!list_empty(&base->expired)) {
  18543. + timer = list_first_entry(&base->expired,
  18544. + struct hrtimer, cb_entry);
  18545. +
  18546. + /*
  18547. + * Same as the above __run_hrtimer function
  18548. + * just we run with interrupts enabled.
  18549. + */
  18550. + debug_deactivate(timer);
  18551. + cpu_base->running_soft = timer;
  18552. + raw_write_seqcount_barrier(&cpu_base->seq);
  18553. +
  18554. + __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, 0);
  18555. + timer_stats_account_hrtimer(timer);
  18556. + fn = timer->function;
  18557. +
  18558. + raw_spin_unlock_irq(&cpu_base->lock);
  18559. + restart = fn(timer);
  18560. + raw_spin_lock_irq(&cpu_base->lock);
  18561. +
  18562. + hrtimer_rt_reprogram(restart, timer, base);
  18563. + raw_write_seqcount_barrier(&cpu_base->seq);
  18564. +
  18565. + WARN_ON_ONCE(cpu_base->running_soft != timer);
  18566. + cpu_base->running_soft = NULL;
  18567. + }
  18568. + }
  18569. +
  18570. + raw_spin_unlock_irq(&cpu_base->lock);
  18571. +
  18572. + wake_up_timer_waiters(cpu_base);
  18573. +}
  18574. +
  18575. +static int hrtimer_rt_defer(struct hrtimer *timer)
  18576. +{
  18577. + if (timer->irqsafe)
  18578. + return 0;
  18579. +
  18580. + __remove_hrtimer(timer, timer->base, timer->state, 0);
  18581. + list_add_tail(&timer->cb_entry, &timer->base->expired);
  18582. + return 1;
  18583. +}
  18584. +
  18585. +#else
  18586. +
  18587. +static inline int hrtimer_rt_defer(struct hrtimer *timer) { return 0; }
  18588. +
  18589. +#endif
  18590. +
  18591. +static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer);
  18592. +
  18593. static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now)
  18594. {
  18595. struct hrtimer_clock_base *base = cpu_base->clock_base;
  18596. unsigned int active = cpu_base->active_bases;
  18597. + int raise = 0;
  18598. for (; active; base++, active >>= 1) {
  18599. struct timerqueue_node *node;
  18600. @@ -1284,6 +1450,15 @@
  18601. timer = container_of(node, struct hrtimer, node);
  18602. + trace_hrtimer_interrupt(raw_smp_processor_id(),
  18603. + ktime_to_ns(ktime_sub(ktime_to_ns(timer->praecox) ?
  18604. + timer->praecox : hrtimer_get_expires(timer),
  18605. + basenow)),
  18606. + current,
  18607. + timer->function == hrtimer_wakeup ?
  18608. + container_of(timer, struct hrtimer_sleeper,
  18609. + timer)->task : NULL);
  18610. +
  18611. /*
  18612. * The immediate goal for using the softexpires is
  18613. * minimizing wakeups, not running timers at the
  18614. @@ -1299,9 +1474,14 @@
  18615. if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer))
  18616. break;
  18617. - __run_hrtimer(cpu_base, base, timer, &basenow);
  18618. + if (!hrtimer_rt_defer(timer))
  18619. + __run_hrtimer(cpu_base, base, timer, &basenow);
  18620. + else
  18621. + raise = 1;
  18622. }
  18623. }
  18624. + if (raise)
  18625. + raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  18626. }
  18627. #ifdef CONFIG_HIGH_RES_TIMERS
  18628. @@ -1464,16 +1644,18 @@
  18629. void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
  18630. {
  18631. sl->timer.function = hrtimer_wakeup;
  18632. + sl->timer.irqsafe = 1;
  18633. sl->task = task;
  18634. }
  18635. EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
  18636. -static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
  18637. +static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode,
  18638. + unsigned long state)
  18639. {
  18640. hrtimer_init_sleeper(t, current);
  18641. do {
  18642. - set_current_state(TASK_INTERRUPTIBLE);
  18643. + set_current_state(state);
  18644. hrtimer_start_expires(&t->timer, mode);
  18645. if (likely(t->task))
  18646. @@ -1515,7 +1697,8 @@
  18647. HRTIMER_MODE_ABS);
  18648. hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
  18649. - if (do_nanosleep(&t, HRTIMER_MODE_ABS))
  18650. + /* cpu_chill() does not care about restart state. */
  18651. + if (do_nanosleep(&t, HRTIMER_MODE_ABS, TASK_INTERRUPTIBLE))
  18652. goto out;
  18653. rmtp = restart->nanosleep.rmtp;
  18654. @@ -1532,8 +1715,10 @@
  18655. return ret;
  18656. }
  18657. -long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  18658. - const enum hrtimer_mode mode, const clockid_t clockid)
  18659. +static long
  18660. +__hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  18661. + const enum hrtimer_mode mode, const clockid_t clockid,
  18662. + unsigned long state)
  18663. {
  18664. struct restart_block *restart;
  18665. struct hrtimer_sleeper t;
  18666. @@ -1546,7 +1731,7 @@
  18667. hrtimer_init_on_stack(&t.timer, clockid, mode);
  18668. hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
  18669. - if (do_nanosleep(&t, mode))
  18670. + if (do_nanosleep(&t, mode, state))
  18671. goto out;
  18672. /* Absolute timers do not update the rmtp value and restart: */
  18673. @@ -1573,6 +1758,12 @@
  18674. return ret;
  18675. }
  18676. +long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  18677. + const enum hrtimer_mode mode, const clockid_t clockid)
  18678. +{
  18679. + return __hrtimer_nanosleep(rqtp, rmtp, mode, clockid, TASK_INTERRUPTIBLE);
  18680. +}
  18681. +
  18682. SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
  18683. struct timespec __user *, rmtp)
  18684. {
  18685. @@ -1587,6 +1778,26 @@
  18686. return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
  18687. }
  18688. +#ifdef CONFIG_PREEMPT_RT_FULL
  18689. +/*
  18690. + * Sleep for 1 ms in hope whoever holds what we want will let it go.
  18691. + */
  18692. +void cpu_chill(void)
  18693. +{
  18694. + struct timespec tu = {
  18695. + .tv_nsec = NSEC_PER_MSEC,
  18696. + };
  18697. + unsigned int freeze_flag = current->flags & PF_NOFREEZE;
  18698. +
  18699. + current->flags |= PF_NOFREEZE;
  18700. + __hrtimer_nanosleep(&tu, NULL, HRTIMER_MODE_REL, CLOCK_MONOTONIC,
  18701. + TASK_UNINTERRUPTIBLE);
  18702. + if (!freeze_flag)
  18703. + current->flags &= ~PF_NOFREEZE;
  18704. +}
  18705. +EXPORT_SYMBOL(cpu_chill);
  18706. +#endif
  18707. +
  18708. /*
  18709. * Functions related to boot-time initialization:
  18710. */
  18711. @@ -1598,10 +1809,14 @@
  18712. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  18713. cpu_base->clock_base[i].cpu_base = cpu_base;
  18714. timerqueue_init_head(&cpu_base->clock_base[i].active);
  18715. + INIT_LIST_HEAD(&cpu_base->clock_base[i].expired);
  18716. }
  18717. cpu_base->cpu = cpu;
  18718. hrtimer_init_hres(cpu_base);
  18719. +#ifdef CONFIG_PREEMPT_RT_BASE
  18720. + init_waitqueue_head(&cpu_base->wait);
  18721. +#endif
  18722. return 0;
  18723. }
  18724. @@ -1671,9 +1886,26 @@
  18725. #endif /* CONFIG_HOTPLUG_CPU */
  18726. +#ifdef CONFIG_PREEMPT_RT_BASE
  18727. +
  18728. +static void run_hrtimer_softirq(struct softirq_action *h)
  18729. +{
  18730. + hrtimer_rt_run_pending();
  18731. +}
  18732. +
  18733. +static void hrtimers_open_softirq(void)
  18734. +{
  18735. + open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
  18736. +}
  18737. +
  18738. +#else
  18739. +static void hrtimers_open_softirq(void) { }
  18740. +#endif
  18741. +
  18742. void __init hrtimers_init(void)
  18743. {
  18744. hrtimers_prepare_cpu(smp_processor_id());
  18745. + hrtimers_open_softirq();
  18746. }
  18747. /**
  18748. diff -Nur linux-4.9.28.orig/kernel/time/itimer.c linux-4.9.28/kernel/time/itimer.c
  18749. --- linux-4.9.28.orig/kernel/time/itimer.c 2017-05-14 14:00:37.000000000 +0200
  18750. +++ linux-4.9.28/kernel/time/itimer.c 2017-05-19 03:37:25.198177419 +0200
  18751. @@ -213,6 +213,7 @@
  18752. /* We are sharing ->siglock with it_real_fn() */
  18753. if (hrtimer_try_to_cancel(timer) < 0) {
  18754. spin_unlock_irq(&tsk->sighand->siglock);
  18755. + hrtimer_wait_for_timer(&tsk->signal->real_timer);
  18756. goto again;
  18757. }
  18758. expires = timeval_to_ktime(value->it_value);
  18759. diff -Nur linux-4.9.28.orig/kernel/time/jiffies.c linux-4.9.28/kernel/time/jiffies.c
  18760. --- linux-4.9.28.orig/kernel/time/jiffies.c 2017-05-14 14:00:37.000000000 +0200
  18761. +++ linux-4.9.28/kernel/time/jiffies.c 2017-05-19 03:37:25.198177419 +0200
  18762. @@ -74,7 +74,8 @@
  18763. .max_cycles = 10,
  18764. };
  18765. -__cacheline_aligned_in_smp DEFINE_SEQLOCK(jiffies_lock);
  18766. +__cacheline_aligned_in_smp DEFINE_RAW_SPINLOCK(jiffies_lock);
  18767. +__cacheline_aligned_in_smp seqcount_t jiffies_seq;
  18768. #if (BITS_PER_LONG < 64)
  18769. u64 get_jiffies_64(void)
  18770. @@ -83,9 +84,9 @@
  18771. u64 ret;
  18772. do {
  18773. - seq = read_seqbegin(&jiffies_lock);
  18774. + seq = read_seqcount_begin(&jiffies_seq);
  18775. ret = jiffies_64;
  18776. - } while (read_seqretry(&jiffies_lock, seq));
  18777. + } while (read_seqcount_retry(&jiffies_seq, seq));
  18778. return ret;
  18779. }
  18780. EXPORT_SYMBOL(get_jiffies_64);
  18781. diff -Nur linux-4.9.28.orig/kernel/time/ntp.c linux-4.9.28/kernel/time/ntp.c
  18782. --- linux-4.9.28.orig/kernel/time/ntp.c 2017-05-14 14:00:37.000000000 +0200
  18783. +++ linux-4.9.28/kernel/time/ntp.c 2017-05-19 03:37:25.198177419 +0200
  18784. @@ -17,6 +17,7 @@
  18785. #include <linux/module.h>
  18786. #include <linux/rtc.h>
  18787. #include <linux/math64.h>
  18788. +#include <linux/swork.h>
  18789. #include "ntp_internal.h"
  18790. #include "timekeeping_internal.h"
  18791. @@ -568,10 +569,35 @@
  18792. &sync_cmos_work, timespec64_to_jiffies(&next));
  18793. }
  18794. +#ifdef CONFIG_PREEMPT_RT_FULL
  18795. +
  18796. +static void run_clock_set_delay(struct swork_event *event)
  18797. +{
  18798. + queue_delayed_work(system_power_efficient_wq, &sync_cmos_work, 0);
  18799. +}
  18800. +
  18801. +static struct swork_event ntp_cmos_swork;
  18802. +
  18803. +void ntp_notify_cmos_timer(void)
  18804. +{
  18805. + swork_queue(&ntp_cmos_swork);
  18806. +}
  18807. +
  18808. +static __init int create_cmos_delay_thread(void)
  18809. +{
  18810. + WARN_ON(swork_get());
  18811. + INIT_SWORK(&ntp_cmos_swork, run_clock_set_delay);
  18812. + return 0;
  18813. +}
  18814. +early_initcall(create_cmos_delay_thread);
  18815. +
  18816. +#else
  18817. +
  18818. void ntp_notify_cmos_timer(void)
  18819. {
  18820. queue_delayed_work(system_power_efficient_wq, &sync_cmos_work, 0);
  18821. }
  18822. +#endif /* CONFIG_PREEMPT_RT_FULL */
  18823. #else
  18824. void ntp_notify_cmos_timer(void) { }
  18825. diff -Nur linux-4.9.28.orig/kernel/time/posix-cpu-timers.c linux-4.9.28/kernel/time/posix-cpu-timers.c
  18826. --- linux-4.9.28.orig/kernel/time/posix-cpu-timers.c 2017-05-14 14:00:37.000000000 +0200
  18827. +++ linux-4.9.28/kernel/time/posix-cpu-timers.c 2017-05-19 03:37:25.198177419 +0200
  18828. @@ -3,6 +3,7 @@
  18829. */
  18830. #include <linux/sched.h>
  18831. +#include <linux/sched/rt.h>
  18832. #include <linux/posix-timers.h>
  18833. #include <linux/errno.h>
  18834. #include <linux/math64.h>
  18835. @@ -620,7 +621,7 @@
  18836. /*
  18837. * Disarm any old timer after extracting its expiry time.
  18838. */
  18839. - WARN_ON_ONCE(!irqs_disabled());
  18840. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  18841. ret = 0;
  18842. old_incr = timer->it.cpu.incr;
  18843. @@ -1064,7 +1065,7 @@
  18844. /*
  18845. * Now re-arm for the new expiry time.
  18846. */
  18847. - WARN_ON_ONCE(!irqs_disabled());
  18848. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  18849. arm_timer(timer);
  18850. unlock_task_sighand(p, &flags);
  18851. @@ -1153,13 +1154,13 @@
  18852. * already updated our counts. We need to check if any timers fire now.
  18853. * Interrupts are disabled.
  18854. */
  18855. -void run_posix_cpu_timers(struct task_struct *tsk)
  18856. +static void __run_posix_cpu_timers(struct task_struct *tsk)
  18857. {
  18858. LIST_HEAD(firing);
  18859. struct k_itimer *timer, *next;
  18860. unsigned long flags;
  18861. - WARN_ON_ONCE(!irqs_disabled());
  18862. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  18863. /*
  18864. * The fast path checks that there are no expired thread or thread
  18865. @@ -1213,6 +1214,190 @@
  18866. }
  18867. }
  18868. +#ifdef CONFIG_PREEMPT_RT_BASE
  18869. +#include <linux/kthread.h>
  18870. +#include <linux/cpu.h>
  18871. +DEFINE_PER_CPU(struct task_struct *, posix_timer_task);
  18872. +DEFINE_PER_CPU(struct task_struct *, posix_timer_tasklist);
  18873. +
  18874. +static int posix_cpu_timers_thread(void *data)
  18875. +{
  18876. + int cpu = (long)data;
  18877. +
  18878. + BUG_ON(per_cpu(posix_timer_task,cpu) != current);
  18879. +
  18880. + while (!kthread_should_stop()) {
  18881. + struct task_struct *tsk = NULL;
  18882. + struct task_struct *next = NULL;
  18883. +
  18884. + if (cpu_is_offline(cpu))
  18885. + goto wait_to_die;
  18886. +
  18887. + /* grab task list */
  18888. + raw_local_irq_disable();
  18889. + tsk = per_cpu(posix_timer_tasklist, cpu);
  18890. + per_cpu(posix_timer_tasklist, cpu) = NULL;
  18891. + raw_local_irq_enable();
  18892. +
  18893. + /* its possible the list is empty, just return */
  18894. + if (!tsk) {
  18895. + set_current_state(TASK_INTERRUPTIBLE);
  18896. + schedule();
  18897. + __set_current_state(TASK_RUNNING);
  18898. + continue;
  18899. + }
  18900. +
  18901. + /* Process task list */
  18902. + while (1) {
  18903. + /* save next */
  18904. + next = tsk->posix_timer_list;
  18905. +
  18906. + /* run the task timers, clear its ptr and
  18907. + * unreference it
  18908. + */
  18909. + __run_posix_cpu_timers(tsk);
  18910. + tsk->posix_timer_list = NULL;
  18911. + put_task_struct(tsk);
  18912. +
  18913. + /* check if this is the last on the list */
  18914. + if (next == tsk)
  18915. + break;
  18916. + tsk = next;
  18917. + }
  18918. + }
  18919. + return 0;
  18920. +
  18921. +wait_to_die:
  18922. + /* Wait for kthread_stop */
  18923. + set_current_state(TASK_INTERRUPTIBLE);
  18924. + while (!kthread_should_stop()) {
  18925. + schedule();
  18926. + set_current_state(TASK_INTERRUPTIBLE);
  18927. + }
  18928. + __set_current_state(TASK_RUNNING);
  18929. + return 0;
  18930. +}
  18931. +
  18932. +static inline int __fastpath_timer_check(struct task_struct *tsk)
  18933. +{
  18934. + /* tsk == current, ensure it is safe to use ->signal/sighand */
  18935. + if (unlikely(tsk->exit_state))
  18936. + return 0;
  18937. +
  18938. + if (!task_cputime_zero(&tsk->cputime_expires))
  18939. + return 1;
  18940. +
  18941. + if (!task_cputime_zero(&tsk->signal->cputime_expires))
  18942. + return 1;
  18943. +
  18944. + return 0;
  18945. +}
  18946. +
  18947. +void run_posix_cpu_timers(struct task_struct *tsk)
  18948. +{
  18949. + unsigned long cpu = smp_processor_id();
  18950. + struct task_struct *tasklist;
  18951. +
  18952. + BUG_ON(!irqs_disabled());
  18953. + if(!per_cpu(posix_timer_task, cpu))
  18954. + return;
  18955. + /* get per-cpu references */
  18956. + tasklist = per_cpu(posix_timer_tasklist, cpu);
  18957. +
  18958. + /* check to see if we're already queued */
  18959. + if (!tsk->posix_timer_list && __fastpath_timer_check(tsk)) {
  18960. + get_task_struct(tsk);
  18961. + if (tasklist) {
  18962. + tsk->posix_timer_list = tasklist;
  18963. + } else {
  18964. + /*
  18965. + * The list is terminated by a self-pointing
  18966. + * task_struct
  18967. + */
  18968. + tsk->posix_timer_list = tsk;
  18969. + }
  18970. + per_cpu(posix_timer_tasklist, cpu) = tsk;
  18971. +
  18972. + wake_up_process(per_cpu(posix_timer_task, cpu));
  18973. + }
  18974. +}
  18975. +
  18976. +/*
  18977. + * posix_cpu_thread_call - callback that gets triggered when a CPU is added.
  18978. + * Here we can start up the necessary migration thread for the new CPU.
  18979. + */
  18980. +static int posix_cpu_thread_call(struct notifier_block *nfb,
  18981. + unsigned long action, void *hcpu)
  18982. +{
  18983. + int cpu = (long)hcpu;
  18984. + struct task_struct *p;
  18985. + struct sched_param param;
  18986. +
  18987. + switch (action) {
  18988. + case CPU_UP_PREPARE:
  18989. + p = kthread_create(posix_cpu_timers_thread, hcpu,
  18990. + "posixcputmr/%d",cpu);
  18991. + if (IS_ERR(p))
  18992. + return NOTIFY_BAD;
  18993. + p->flags |= PF_NOFREEZE;
  18994. + kthread_bind(p, cpu);
  18995. + /* Must be high prio to avoid getting starved */
  18996. + param.sched_priority = MAX_RT_PRIO-1;
  18997. + sched_setscheduler(p, SCHED_FIFO, &param);
  18998. + per_cpu(posix_timer_task,cpu) = p;
  18999. + break;
  19000. + case CPU_ONLINE:
  19001. + /* Strictly unneccessary, as first user will wake it. */
  19002. + wake_up_process(per_cpu(posix_timer_task,cpu));
  19003. + break;
  19004. +#ifdef CONFIG_HOTPLUG_CPU
  19005. + case CPU_UP_CANCELED:
  19006. + /* Unbind it from offline cpu so it can run. Fall thru. */
  19007. + kthread_bind(per_cpu(posix_timer_task, cpu),
  19008. + cpumask_any(cpu_online_mask));
  19009. + kthread_stop(per_cpu(posix_timer_task,cpu));
  19010. + per_cpu(posix_timer_task,cpu) = NULL;
  19011. + break;
  19012. + case CPU_DEAD:
  19013. + kthread_stop(per_cpu(posix_timer_task,cpu));
  19014. + per_cpu(posix_timer_task,cpu) = NULL;
  19015. + break;
  19016. +#endif
  19017. + }
  19018. + return NOTIFY_OK;
  19019. +}
  19020. +
  19021. +/* Register at highest priority so that task migration (migrate_all_tasks)
  19022. + * happens before everything else.
  19023. + */
  19024. +static struct notifier_block posix_cpu_thread_notifier = {
  19025. + .notifier_call = posix_cpu_thread_call,
  19026. + .priority = 10
  19027. +};
  19028. +
  19029. +static int __init posix_cpu_thread_init(void)
  19030. +{
  19031. + void *hcpu = (void *)(long)smp_processor_id();
  19032. + /* Start one for boot CPU. */
  19033. + unsigned long cpu;
  19034. +
  19035. + /* init the per-cpu posix_timer_tasklets */
  19036. + for_each_possible_cpu(cpu)
  19037. + per_cpu(posix_timer_tasklist, cpu) = NULL;
  19038. +
  19039. + posix_cpu_thread_call(&posix_cpu_thread_notifier, CPU_UP_PREPARE, hcpu);
  19040. + posix_cpu_thread_call(&posix_cpu_thread_notifier, CPU_ONLINE, hcpu);
  19041. + register_cpu_notifier(&posix_cpu_thread_notifier);
  19042. + return 0;
  19043. +}
  19044. +early_initcall(posix_cpu_thread_init);
  19045. +#else /* CONFIG_PREEMPT_RT_BASE */
  19046. +void run_posix_cpu_timers(struct task_struct *tsk)
  19047. +{
  19048. + __run_posix_cpu_timers(tsk);
  19049. +}
  19050. +#endif /* CONFIG_PREEMPT_RT_BASE */
  19051. +
  19052. /*
  19053. * Set one of the process-wide special case CPU timers or RLIMIT_CPU.
  19054. * The tsk->sighand->siglock must be held by the caller.
  19055. diff -Nur linux-4.9.28.orig/kernel/time/posix-timers.c linux-4.9.28/kernel/time/posix-timers.c
  19056. --- linux-4.9.28.orig/kernel/time/posix-timers.c 2017-05-14 14:00:37.000000000 +0200
  19057. +++ linux-4.9.28/kernel/time/posix-timers.c 2017-05-19 03:37:25.198177419 +0200
  19058. @@ -506,6 +506,7 @@
  19059. static struct pid *good_sigevent(sigevent_t * event)
  19060. {
  19061. struct task_struct *rtn = current->group_leader;
  19062. + int sig = event->sigev_signo;
  19063. if ((event->sigev_notify & SIGEV_THREAD_ID ) &&
  19064. (!(rtn = find_task_by_vpid(event->sigev_notify_thread_id)) ||
  19065. @@ -514,7 +515,8 @@
  19066. return NULL;
  19067. if (((event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) &&
  19068. - ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX)))
  19069. + (sig <= 0 || sig > SIGRTMAX || sig_kernel_only(sig) ||
  19070. + sig_kernel_coredump(sig)))
  19071. return NULL;
  19072. return task_pid(rtn);
  19073. @@ -826,6 +828,20 @@
  19074. return overrun;
  19075. }
  19076. +/*
  19077. + * Protected by RCU!
  19078. + */
  19079. +static void timer_wait_for_callback(struct k_clock *kc, struct k_itimer *timr)
  19080. +{
  19081. +#ifdef CONFIG_PREEMPT_RT_FULL
  19082. + if (kc->timer_set == common_timer_set)
  19083. + hrtimer_wait_for_timer(&timr->it.real.timer);
  19084. + else
  19085. + /* FIXME: Whacky hack for posix-cpu-timers */
  19086. + schedule_timeout(1);
  19087. +#endif
  19088. +}
  19089. +
  19090. /* Set a POSIX.1b interval timer. */
  19091. /* timr->it_lock is taken. */
  19092. static int
  19093. @@ -903,6 +919,7 @@
  19094. if (!timr)
  19095. return -EINVAL;
  19096. + rcu_read_lock();
  19097. kc = clockid_to_kclock(timr->it_clock);
  19098. if (WARN_ON_ONCE(!kc || !kc->timer_set))
  19099. error = -EINVAL;
  19100. @@ -911,9 +928,12 @@
  19101. unlock_timer(timr, flag);
  19102. if (error == TIMER_RETRY) {
  19103. + timer_wait_for_callback(kc, timr);
  19104. rtn = NULL; // We already got the old time...
  19105. + rcu_read_unlock();
  19106. goto retry;
  19107. }
  19108. + rcu_read_unlock();
  19109. if (old_setting && !error &&
  19110. copy_to_user(old_setting, &old_spec, sizeof (old_spec)))
  19111. @@ -951,10 +971,15 @@
  19112. if (!timer)
  19113. return -EINVAL;
  19114. + rcu_read_lock();
  19115. if (timer_delete_hook(timer) == TIMER_RETRY) {
  19116. unlock_timer(timer, flags);
  19117. + timer_wait_for_callback(clockid_to_kclock(timer->it_clock),
  19118. + timer);
  19119. + rcu_read_unlock();
  19120. goto retry_delete;
  19121. }
  19122. + rcu_read_unlock();
  19123. spin_lock(&current->sighand->siglock);
  19124. list_del(&timer->list);
  19125. @@ -980,8 +1005,18 @@
  19126. retry_delete:
  19127. spin_lock_irqsave(&timer->it_lock, flags);
  19128. + /* On RT we can race with a deletion */
  19129. + if (!timer->it_signal) {
  19130. + unlock_timer(timer, flags);
  19131. + return;
  19132. + }
  19133. +
  19134. if (timer_delete_hook(timer) == TIMER_RETRY) {
  19135. + rcu_read_lock();
  19136. unlock_timer(timer, flags);
  19137. + timer_wait_for_callback(clockid_to_kclock(timer->it_clock),
  19138. + timer);
  19139. + rcu_read_unlock();
  19140. goto retry_delete;
  19141. }
  19142. list_del(&timer->list);
  19143. diff -Nur linux-4.9.28.orig/kernel/time/tick-broadcast-hrtimer.c linux-4.9.28/kernel/time/tick-broadcast-hrtimer.c
  19144. --- linux-4.9.28.orig/kernel/time/tick-broadcast-hrtimer.c 2017-05-14 14:00:37.000000000 +0200
  19145. +++ linux-4.9.28/kernel/time/tick-broadcast-hrtimer.c 2017-05-19 03:37:25.198177419 +0200
  19146. @@ -107,5 +107,6 @@
  19147. {
  19148. hrtimer_init(&bctimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  19149. bctimer.function = bc_handler;
  19150. + bctimer.irqsafe = true;
  19151. clockevents_register_device(&ce_broadcast_hrtimer);
  19152. }
  19153. diff -Nur linux-4.9.28.orig/kernel/time/tick-common.c linux-4.9.28/kernel/time/tick-common.c
  19154. --- linux-4.9.28.orig/kernel/time/tick-common.c 2017-05-14 14:00:37.000000000 +0200
  19155. +++ linux-4.9.28/kernel/time/tick-common.c 2017-05-19 03:37:25.198177419 +0200
  19156. @@ -79,13 +79,15 @@
  19157. static void tick_periodic(int cpu)
  19158. {
  19159. if (tick_do_timer_cpu == cpu) {
  19160. - write_seqlock(&jiffies_lock);
  19161. + raw_spin_lock(&jiffies_lock);
  19162. + write_seqcount_begin(&jiffies_seq);
  19163. /* Keep track of the next tick event */
  19164. tick_next_period = ktime_add(tick_next_period, tick_period);
  19165. do_timer(1);
  19166. - write_sequnlock(&jiffies_lock);
  19167. + write_seqcount_end(&jiffies_seq);
  19168. + raw_spin_unlock(&jiffies_lock);
  19169. update_wall_time();
  19170. }
  19171. @@ -157,9 +159,9 @@
  19172. ktime_t next;
  19173. do {
  19174. - seq = read_seqbegin(&jiffies_lock);
  19175. + seq = read_seqcount_begin(&jiffies_seq);
  19176. next = tick_next_period;
  19177. - } while (read_seqretry(&jiffies_lock, seq));
  19178. + } while (read_seqcount_retry(&jiffies_seq, seq));
  19179. clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
  19180. diff -Nur linux-4.9.28.orig/kernel/time/tick-sched.c linux-4.9.28/kernel/time/tick-sched.c
  19181. --- linux-4.9.28.orig/kernel/time/tick-sched.c 2017-05-14 14:00:37.000000000 +0200
  19182. +++ linux-4.9.28/kernel/time/tick-sched.c 2017-05-19 03:37:25.198177419 +0200
  19183. @@ -62,7 +62,8 @@
  19184. return;
  19185. /* Reevaluate with jiffies_lock held */
  19186. - write_seqlock(&jiffies_lock);
  19187. + raw_spin_lock(&jiffies_lock);
  19188. + write_seqcount_begin(&jiffies_seq);
  19189. delta = ktime_sub(now, last_jiffies_update);
  19190. if (delta.tv64 >= tick_period.tv64) {
  19191. @@ -85,10 +86,12 @@
  19192. /* Keep the tick_next_period variable up to date */
  19193. tick_next_period = ktime_add(last_jiffies_update, tick_period);
  19194. } else {
  19195. - write_sequnlock(&jiffies_lock);
  19196. + write_seqcount_end(&jiffies_seq);
  19197. + raw_spin_unlock(&jiffies_lock);
  19198. return;
  19199. }
  19200. - write_sequnlock(&jiffies_lock);
  19201. + write_seqcount_end(&jiffies_seq);
  19202. + raw_spin_unlock(&jiffies_lock);
  19203. update_wall_time();
  19204. }
  19205. @@ -99,12 +102,14 @@
  19206. {
  19207. ktime_t period;
  19208. - write_seqlock(&jiffies_lock);
  19209. + raw_spin_lock(&jiffies_lock);
  19210. + write_seqcount_begin(&jiffies_seq);
  19211. /* Did we start the jiffies update yet ? */
  19212. if (last_jiffies_update.tv64 == 0)
  19213. last_jiffies_update = tick_next_period;
  19214. period = last_jiffies_update;
  19215. - write_sequnlock(&jiffies_lock);
  19216. + write_seqcount_end(&jiffies_seq);
  19217. + raw_spin_unlock(&jiffies_lock);
  19218. return period;
  19219. }
  19220. @@ -215,6 +220,7 @@
  19221. static DEFINE_PER_CPU(struct irq_work, nohz_full_kick_work) = {
  19222. .func = nohz_full_kick_func,
  19223. + .flags = IRQ_WORK_HARD_IRQ,
  19224. };
  19225. /*
  19226. @@ -673,10 +679,10 @@
  19227. /* Read jiffies and the time when jiffies were updated last */
  19228. do {
  19229. - seq = read_seqbegin(&jiffies_lock);
  19230. + seq = read_seqcount_begin(&jiffies_seq);
  19231. basemono = last_jiffies_update.tv64;
  19232. basejiff = jiffies;
  19233. - } while (read_seqretry(&jiffies_lock, seq));
  19234. + } while (read_seqcount_retry(&jiffies_seq, seq));
  19235. ts->last_jiffies = basejiff;
  19236. if (rcu_needs_cpu(basemono, &next_rcu) ||
  19237. @@ -877,14 +883,7 @@
  19238. return false;
  19239. if (unlikely(local_softirq_pending() && cpu_online(cpu))) {
  19240. - static int ratelimit;
  19241. -
  19242. - if (ratelimit < 10 &&
  19243. - (local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK)) {
  19244. - pr_warn("NOHZ: local_softirq_pending %02x\n",
  19245. - (unsigned int) local_softirq_pending());
  19246. - ratelimit++;
  19247. - }
  19248. + softirq_check_pending_idle();
  19249. return false;
  19250. }
  19251. @@ -1193,6 +1192,7 @@
  19252. * Emulate tick processing via per-CPU hrtimers:
  19253. */
  19254. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  19255. + ts->sched_timer.irqsafe = 1;
  19256. ts->sched_timer.function = tick_sched_timer;
  19257. /* Get the next period (per-CPU) */
  19258. diff -Nur linux-4.9.28.orig/kernel/time/timekeeping.c linux-4.9.28/kernel/time/timekeeping.c
  19259. --- linux-4.9.28.orig/kernel/time/timekeeping.c 2017-05-14 14:00:37.000000000 +0200
  19260. +++ linux-4.9.28/kernel/time/timekeeping.c 2017-05-19 03:37:25.198177419 +0200
  19261. @@ -2328,8 +2328,10 @@
  19262. */
  19263. void xtime_update(unsigned long ticks)
  19264. {
  19265. - write_seqlock(&jiffies_lock);
  19266. + raw_spin_lock(&jiffies_lock);
  19267. + write_seqcount_begin(&jiffies_seq);
  19268. do_timer(ticks);
  19269. - write_sequnlock(&jiffies_lock);
  19270. + write_seqcount_end(&jiffies_seq);
  19271. + raw_spin_unlock(&jiffies_lock);
  19272. update_wall_time();
  19273. }
  19274. diff -Nur linux-4.9.28.orig/kernel/time/timekeeping.h linux-4.9.28/kernel/time/timekeeping.h
  19275. --- linux-4.9.28.orig/kernel/time/timekeeping.h 2017-05-14 14:00:37.000000000 +0200
  19276. +++ linux-4.9.28/kernel/time/timekeeping.h 2017-05-19 03:37:25.202177573 +0200
  19277. @@ -19,7 +19,8 @@
  19278. extern void do_timer(unsigned long ticks);
  19279. extern void update_wall_time(void);
  19280. -extern seqlock_t jiffies_lock;
  19281. +extern raw_spinlock_t jiffies_lock;
  19282. +extern seqcount_t jiffies_seq;
  19283. #define CS_NAME_LEN 32
  19284. diff -Nur linux-4.9.28.orig/kernel/time/timer.c linux-4.9.28/kernel/time/timer.c
  19285. --- linux-4.9.28.orig/kernel/time/timer.c 2017-05-14 14:00:37.000000000 +0200
  19286. +++ linux-4.9.28/kernel/time/timer.c 2017-05-19 03:37:25.202177573 +0200
  19287. @@ -193,8 +193,11 @@
  19288. #endif
  19289. struct timer_base {
  19290. - spinlock_t lock;
  19291. + raw_spinlock_t lock;
  19292. struct timer_list *running_timer;
  19293. +#ifdef CONFIG_PREEMPT_RT_FULL
  19294. + struct swait_queue_head wait_for_running_timer;
  19295. +#endif
  19296. unsigned long clk;
  19297. unsigned long next_expiry;
  19298. unsigned int cpu;
  19299. @@ -203,6 +206,8 @@
  19300. bool is_idle;
  19301. DECLARE_BITMAP(pending_map, WHEEL_SIZE);
  19302. struct hlist_head vectors[WHEEL_SIZE];
  19303. + struct hlist_head expired_lists[LVL_DEPTH];
  19304. + int expired_count;
  19305. } ____cacheline_aligned;
  19306. static DEFINE_PER_CPU(struct timer_base, timer_bases[NR_BASES]);
  19307. @@ -948,10 +953,10 @@
  19308. if (!(tf & TIMER_MIGRATING)) {
  19309. base = get_timer_base(tf);
  19310. - spin_lock_irqsave(&base->lock, *flags);
  19311. + raw_spin_lock_irqsave(&base->lock, *flags);
  19312. if (timer->flags == tf)
  19313. return base;
  19314. - spin_unlock_irqrestore(&base->lock, *flags);
  19315. + raw_spin_unlock_irqrestore(&base->lock, *flags);
  19316. }
  19317. cpu_relax();
  19318. }
  19319. @@ -1023,9 +1028,9 @@
  19320. /* See the comment in lock_timer_base() */
  19321. timer->flags |= TIMER_MIGRATING;
  19322. - spin_unlock(&base->lock);
  19323. + raw_spin_unlock(&base->lock);
  19324. base = new_base;
  19325. - spin_lock(&base->lock);
  19326. + raw_spin_lock(&base->lock);
  19327. WRITE_ONCE(timer->flags,
  19328. (timer->flags & ~TIMER_BASEMASK) | base->cpu);
  19329. }
  19330. @@ -1050,7 +1055,7 @@
  19331. }
  19332. out_unlock:
  19333. - spin_unlock_irqrestore(&base->lock, flags);
  19334. + raw_spin_unlock_irqrestore(&base->lock, flags);
  19335. return ret;
  19336. }
  19337. @@ -1144,19 +1149,46 @@
  19338. if (base != new_base) {
  19339. timer->flags |= TIMER_MIGRATING;
  19340. - spin_unlock(&base->lock);
  19341. + raw_spin_unlock(&base->lock);
  19342. base = new_base;
  19343. - spin_lock(&base->lock);
  19344. + raw_spin_lock(&base->lock);
  19345. WRITE_ONCE(timer->flags,
  19346. (timer->flags & ~TIMER_BASEMASK) | cpu);
  19347. }
  19348. debug_activate(timer, timer->expires);
  19349. internal_add_timer(base, timer);
  19350. - spin_unlock_irqrestore(&base->lock, flags);
  19351. + raw_spin_unlock_irqrestore(&base->lock, flags);
  19352. }
  19353. EXPORT_SYMBOL_GPL(add_timer_on);
  19354. +#ifdef CONFIG_PREEMPT_RT_FULL
  19355. +/*
  19356. + * Wait for a running timer
  19357. + */
  19358. +static void wait_for_running_timer(struct timer_list *timer)
  19359. +{
  19360. + struct timer_base *base;
  19361. + u32 tf = timer->flags;
  19362. +
  19363. + if (tf & TIMER_MIGRATING)
  19364. + return;
  19365. +
  19366. + base = get_timer_base(tf);
  19367. + swait_event(base->wait_for_running_timer,
  19368. + base->running_timer != timer);
  19369. +}
  19370. +
  19371. +# define wakeup_timer_waiters(b) swake_up_all(&(b)->wait_for_running_timer)
  19372. +#else
  19373. +static inline void wait_for_running_timer(struct timer_list *timer)
  19374. +{
  19375. + cpu_relax();
  19376. +}
  19377. +
  19378. +# define wakeup_timer_waiters(b) do { } while (0)
  19379. +#endif
  19380. +
  19381. /**
  19382. * del_timer - deactive a timer.
  19383. * @timer: the timer to be deactivated
  19384. @@ -1180,7 +1212,7 @@
  19385. if (timer_pending(timer)) {
  19386. base = lock_timer_base(timer, &flags);
  19387. ret = detach_if_pending(timer, base, true);
  19388. - spin_unlock_irqrestore(&base->lock, flags);
  19389. + raw_spin_unlock_irqrestore(&base->lock, flags);
  19390. }
  19391. return ret;
  19392. @@ -1208,13 +1240,13 @@
  19393. timer_stats_timer_clear_start_info(timer);
  19394. ret = detach_if_pending(timer, base, true);
  19395. }
  19396. - spin_unlock_irqrestore(&base->lock, flags);
  19397. + raw_spin_unlock_irqrestore(&base->lock, flags);
  19398. return ret;
  19399. }
  19400. EXPORT_SYMBOL(try_to_del_timer_sync);
  19401. -#ifdef CONFIG_SMP
  19402. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  19403. /**
  19404. * del_timer_sync - deactivate a timer and wait for the handler to finish.
  19405. * @timer: the timer to be deactivated
  19406. @@ -1274,7 +1306,7 @@
  19407. int ret = try_to_del_timer_sync(timer);
  19408. if (ret >= 0)
  19409. return ret;
  19410. - cpu_relax();
  19411. + wait_for_running_timer(timer);
  19412. }
  19413. }
  19414. EXPORT_SYMBOL(del_timer_sync);
  19415. @@ -1323,7 +1355,8 @@
  19416. }
  19417. }
  19418. -static void expire_timers(struct timer_base *base, struct hlist_head *head)
  19419. +static inline void __expire_timers(struct timer_base *base,
  19420. + struct hlist_head *head)
  19421. {
  19422. while (!hlist_empty(head)) {
  19423. struct timer_list *timer;
  19424. @@ -1339,33 +1372,53 @@
  19425. fn = timer->function;
  19426. data = timer->data;
  19427. - if (timer->flags & TIMER_IRQSAFE) {
  19428. - spin_unlock(&base->lock);
  19429. + if (!IS_ENABLED(CONFIG_PREEMPT_RT_FULL) &&
  19430. + timer->flags & TIMER_IRQSAFE) {
  19431. + raw_spin_unlock(&base->lock);
  19432. call_timer_fn(timer, fn, data);
  19433. - spin_lock(&base->lock);
  19434. + base->running_timer = NULL;
  19435. + raw_spin_lock(&base->lock);
  19436. } else {
  19437. - spin_unlock_irq(&base->lock);
  19438. + raw_spin_unlock_irq(&base->lock);
  19439. call_timer_fn(timer, fn, data);
  19440. - spin_lock_irq(&base->lock);
  19441. + base->running_timer = NULL;
  19442. + raw_spin_lock_irq(&base->lock);
  19443. }
  19444. }
  19445. }
  19446. -static int __collect_expired_timers(struct timer_base *base,
  19447. - struct hlist_head *heads)
  19448. +static void expire_timers(struct timer_base *base)
  19449. +{
  19450. + struct hlist_head *head;
  19451. +
  19452. + while (base->expired_count--) {
  19453. + head = base->expired_lists + base->expired_count;
  19454. + __expire_timers(base, head);
  19455. + }
  19456. + base->expired_count = 0;
  19457. +}
  19458. +
  19459. +static void __collect_expired_timers(struct timer_base *base)
  19460. {
  19461. unsigned long clk = base->clk;
  19462. struct hlist_head *vec;
  19463. - int i, levels = 0;
  19464. + int i;
  19465. unsigned int idx;
  19466. + /*
  19467. + * expire_timers() must be called at least once before we can
  19468. + * collect more timers
  19469. + */
  19470. + if (WARN_ON(base->expired_count))
  19471. + return;
  19472. +
  19473. for (i = 0; i < LVL_DEPTH; i++) {
  19474. idx = (clk & LVL_MASK) + i * LVL_SIZE;
  19475. if (__test_and_clear_bit(idx, base->pending_map)) {
  19476. vec = base->vectors + idx;
  19477. - hlist_move_list(vec, heads++);
  19478. - levels++;
  19479. + hlist_move_list(vec,
  19480. + &base->expired_lists[base->expired_count++]);
  19481. }
  19482. /* Is it time to look at the next level? */
  19483. if (clk & LVL_CLK_MASK)
  19484. @@ -1373,7 +1426,6 @@
  19485. /* Shift clock for the next level granularity */
  19486. clk >>= LVL_CLK_SHIFT;
  19487. }
  19488. - return levels;
  19489. }
  19490. #ifdef CONFIG_NO_HZ_COMMON
  19491. @@ -1515,7 +1567,7 @@
  19492. if (cpu_is_offline(smp_processor_id()))
  19493. return expires;
  19494. - spin_lock(&base->lock);
  19495. + raw_spin_lock(&base->lock);
  19496. nextevt = __next_timer_interrupt(base);
  19497. is_max_delta = (nextevt == base->clk + NEXT_TIMER_MAX_DELTA);
  19498. base->next_expiry = nextevt;
  19499. @@ -1543,7 +1595,7 @@
  19500. if ((expires - basem) > TICK_NSEC)
  19501. base->is_idle = true;
  19502. }
  19503. - spin_unlock(&base->lock);
  19504. + raw_spin_unlock(&base->lock);
  19505. return cmp_next_hrtimer_event(basem, expires);
  19506. }
  19507. @@ -1566,8 +1618,7 @@
  19508. base->is_idle = false;
  19509. }
  19510. -static int collect_expired_timers(struct timer_base *base,
  19511. - struct hlist_head *heads)
  19512. +static void collect_expired_timers(struct timer_base *base)
  19513. {
  19514. /*
  19515. * NOHZ optimization. After a long idle sleep we need to forward the
  19516. @@ -1584,20 +1635,49 @@
  19517. if (time_after(next, jiffies)) {
  19518. /* The call site will increment clock! */
  19519. base->clk = jiffies - 1;
  19520. - return 0;
  19521. + return;
  19522. }
  19523. base->clk = next;
  19524. }
  19525. - return __collect_expired_timers(base, heads);
  19526. + __collect_expired_timers(base);
  19527. }
  19528. #else
  19529. -static inline int collect_expired_timers(struct timer_base *base,
  19530. - struct hlist_head *heads)
  19531. +static inline void collect_expired_timers(struct timer_base *base)
  19532. {
  19533. - return __collect_expired_timers(base, heads);
  19534. + __collect_expired_timers(base);
  19535. }
  19536. #endif
  19537. +static int find_expired_timers(struct timer_base *base)
  19538. +{
  19539. + const unsigned long int end_clk = jiffies;
  19540. +
  19541. + while (!base->expired_count && time_after_eq(end_clk, base->clk)) {
  19542. + collect_expired_timers(base);
  19543. + base->clk++;
  19544. + }
  19545. +
  19546. + return base->expired_count;
  19547. +}
  19548. +
  19549. +/* Called from CPU tick routine to quickly collect expired timers */
  19550. +static int tick_find_expired(struct timer_base *base)
  19551. +{
  19552. + int count;
  19553. +
  19554. + raw_spin_lock(&base->lock);
  19555. +
  19556. + if (unlikely(time_after(jiffies, base->clk + HZ))) {
  19557. + /* defer to ktimersoftd; don't spend too long in irq context */
  19558. + count = -1;
  19559. + } else
  19560. + count = find_expired_timers(base);
  19561. +
  19562. + raw_spin_unlock(&base->lock);
  19563. +
  19564. + return count;
  19565. +}
  19566. +
  19567. /*
  19568. * Called from the timer interrupt handler to charge one tick to the current
  19569. * process. user_tick is 1 if the tick is user time, 0 for system.
  19570. @@ -1608,13 +1688,13 @@
  19571. /* Note: this timer irq context must be accounted for as well. */
  19572. account_process_tick(p, user_tick);
  19573. + scheduler_tick();
  19574. run_local_timers();
  19575. rcu_check_callbacks(user_tick);
  19576. -#ifdef CONFIG_IRQ_WORK
  19577. +#if defined(CONFIG_IRQ_WORK)
  19578. if (in_irq())
  19579. irq_work_tick();
  19580. #endif
  19581. - scheduler_tick();
  19582. run_posix_cpu_timers(p);
  19583. }
  19584. @@ -1624,24 +1704,13 @@
  19585. */
  19586. static inline void __run_timers(struct timer_base *base)
  19587. {
  19588. - struct hlist_head heads[LVL_DEPTH];
  19589. - int levels;
  19590. -
  19591. - if (!time_after_eq(jiffies, base->clk))
  19592. - return;
  19593. + raw_spin_lock_irq(&base->lock);
  19594. - spin_lock_irq(&base->lock);
  19595. + while (find_expired_timers(base))
  19596. + expire_timers(base);
  19597. - while (time_after_eq(jiffies, base->clk)) {
  19598. -
  19599. - levels = collect_expired_timers(base, heads);
  19600. - base->clk++;
  19601. -
  19602. - while (levels--)
  19603. - expire_timers(base, heads + levels);
  19604. - }
  19605. - base->running_timer = NULL;
  19606. - spin_unlock_irq(&base->lock);
  19607. + raw_spin_unlock_irq(&base->lock);
  19608. + wakeup_timer_waiters(base);
  19609. }
  19610. /*
  19611. @@ -1651,6 +1720,8 @@
  19612. {
  19613. struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
  19614. + irq_work_tick_soft();
  19615. +
  19616. __run_timers(base);
  19617. if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && base->nohz_active)
  19618. __run_timers(this_cpu_ptr(&timer_bases[BASE_DEF]));
  19619. @@ -1665,12 +1736,12 @@
  19620. hrtimer_run_queues();
  19621. /* Raise the softirq only if required. */
  19622. - if (time_before(jiffies, base->clk)) {
  19623. + if (time_before(jiffies, base->clk) || !tick_find_expired(base)) {
  19624. if (!IS_ENABLED(CONFIG_NO_HZ_COMMON) || !base->nohz_active)
  19625. return;
  19626. /* CPU is awake, so check the deferrable base. */
  19627. base++;
  19628. - if (time_before(jiffies, base->clk))
  19629. + if (time_before(jiffies, base->clk) || !tick_find_expired(base))
  19630. return;
  19631. }
  19632. raise_softirq(TIMER_SOFTIRQ);
  19633. @@ -1836,16 +1907,17 @@
  19634. * The caller is globally serialized and nobody else
  19635. * takes two locks at once, deadlock is not possible.
  19636. */
  19637. - spin_lock_irq(&new_base->lock);
  19638. - spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
  19639. + raw_spin_lock_irq(&new_base->lock);
  19640. + raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
  19641. BUG_ON(old_base->running_timer);
  19642. + BUG_ON(old_base->expired_count);
  19643. for (i = 0; i < WHEEL_SIZE; i++)
  19644. migrate_timer_list(new_base, old_base->vectors + i);
  19645. - spin_unlock(&old_base->lock);
  19646. - spin_unlock_irq(&new_base->lock);
  19647. + raw_spin_unlock(&old_base->lock);
  19648. + raw_spin_unlock_irq(&new_base->lock);
  19649. put_cpu_ptr(&timer_bases);
  19650. }
  19651. return 0;
  19652. @@ -1861,8 +1933,12 @@
  19653. for (i = 0; i < NR_BASES; i++) {
  19654. base = per_cpu_ptr(&timer_bases[i], cpu);
  19655. base->cpu = cpu;
  19656. - spin_lock_init(&base->lock);
  19657. + raw_spin_lock_init(&base->lock);
  19658. base->clk = jiffies;
  19659. +#ifdef CONFIG_PREEMPT_RT_FULL
  19660. + init_swait_queue_head(&base->wait_for_running_timer);
  19661. +#endif
  19662. + base->expired_count = 0;
  19663. }
  19664. }
  19665. diff -Nur linux-4.9.28.orig/kernel/trace/Kconfig linux-4.9.28/kernel/trace/Kconfig
  19666. --- linux-4.9.28.orig/kernel/trace/Kconfig 2017-05-14 14:00:37.000000000 +0200
  19667. +++ linux-4.9.28/kernel/trace/Kconfig 2017-05-19 03:37:25.202177573 +0200
  19668. @@ -182,6 +182,24 @@
  19669. enabled. This option and the preempt-off timing option can be
  19670. used together or separately.)
  19671. +config INTERRUPT_OFF_HIST
  19672. + bool "Interrupts-off Latency Histogram"
  19673. + depends on IRQSOFF_TRACER
  19674. + help
  19675. + This option generates continuously updated histograms (one per cpu)
  19676. + of the duration of time periods with interrupts disabled. The
  19677. + histograms are disabled by default. To enable them, write a non-zero
  19678. + number to
  19679. +
  19680. + /sys/kernel/debug/tracing/latency_hist/enable/preemptirqsoff
  19681. +
  19682. + If PREEMPT_OFF_HIST is also selected, additional histograms (one
  19683. + per cpu) are generated that accumulate the duration of time periods
  19684. + when both interrupts and preemption are disabled. The histogram data
  19685. + will be located in the debug file system at
  19686. +
  19687. + /sys/kernel/debug/tracing/latency_hist/irqsoff
  19688. +
  19689. config PREEMPT_TRACER
  19690. bool "Preemption-off Latency Tracer"
  19691. default n
  19692. @@ -206,6 +224,24 @@
  19693. enabled. This option and the irqs-off timing option can be
  19694. used together or separately.)
  19695. +config PREEMPT_OFF_HIST
  19696. + bool "Preemption-off Latency Histogram"
  19697. + depends on PREEMPT_TRACER
  19698. + help
  19699. + This option generates continuously updated histograms (one per cpu)
  19700. + of the duration of time periods with preemption disabled. The
  19701. + histograms are disabled by default. To enable them, write a non-zero
  19702. + number to
  19703. +
  19704. + /sys/kernel/debug/tracing/latency_hist/enable/preemptirqsoff
  19705. +
  19706. + If INTERRUPT_OFF_HIST is also selected, additional histograms (one
  19707. + per cpu) are generated that accumulate the duration of time periods
  19708. + when both interrupts and preemption are disabled. The histogram data
  19709. + will be located in the debug file system at
  19710. +
  19711. + /sys/kernel/debug/tracing/latency_hist/preemptoff
  19712. +
  19713. config SCHED_TRACER
  19714. bool "Scheduling Latency Tracer"
  19715. select GENERIC_TRACER
  19716. @@ -251,6 +287,74 @@
  19717. file. Every time a latency is greater than tracing_thresh, it will
  19718. be recorded into the ring buffer.
  19719. +config WAKEUP_LATENCY_HIST
  19720. + bool "Scheduling Latency Histogram"
  19721. + depends on SCHED_TRACER
  19722. + help
  19723. + This option generates continuously updated histograms (one per cpu)
  19724. + of the scheduling latency of the highest priority task.
  19725. + The histograms are disabled by default. To enable them, write a
  19726. + non-zero number to
  19727. +
  19728. + /sys/kernel/debug/tracing/latency_hist/enable/wakeup
  19729. +
  19730. + Two different algorithms are used, one to determine the latency of
  19731. + processes that exclusively use the highest priority of the system and
  19732. + another one to determine the latency of processes that share the
  19733. + highest system priority with other processes. The former is used to
  19734. + improve hardware and system software, the latter to optimize the
  19735. + priority design of a given system. The histogram data will be
  19736. + located in the debug file system at
  19737. +
  19738. + /sys/kernel/debug/tracing/latency_hist/wakeup
  19739. +
  19740. + and
  19741. +
  19742. + /sys/kernel/debug/tracing/latency_hist/wakeup/sharedprio
  19743. +
  19744. + If both Scheduling Latency Histogram and Missed Timer Offsets
  19745. + Histogram are selected, additional histogram data will be collected
  19746. + that contain, in addition to the wakeup latency, the timer latency, in
  19747. + case the wakeup was triggered by an expired timer. These histograms
  19748. + are available in the
  19749. +
  19750. + /sys/kernel/debug/tracing/latency_hist/timerandwakeup
  19751. +
  19752. + directory. They reflect the apparent interrupt and scheduling latency
  19753. + and are best suitable to determine the worst-case latency of a given
  19754. + system. To enable these histograms, write a non-zero number to
  19755. +
  19756. + /sys/kernel/debug/tracing/latency_hist/enable/timerandwakeup
  19757. +
  19758. +config MISSED_TIMER_OFFSETS_HIST
  19759. + depends on HIGH_RES_TIMERS
  19760. + select GENERIC_TRACER
  19761. + bool "Missed Timer Offsets Histogram"
  19762. + help
  19763. + Generate a histogram of missed timer offsets in microseconds. The
  19764. + histograms are disabled by default. To enable them, write a non-zero
  19765. + number to
  19766. +
  19767. + /sys/kernel/debug/tracing/latency_hist/enable/missed_timer_offsets
  19768. +
  19769. + The histogram data will be located in the debug file system at
  19770. +
  19771. + /sys/kernel/debug/tracing/latency_hist/missed_timer_offsets
  19772. +
  19773. + If both Scheduling Latency Histogram and Missed Timer Offsets
  19774. + Histogram are selected, additional histogram data will be collected
  19775. + that contain, in addition to the wakeup latency, the timer latency, in
  19776. + case the wakeup was triggered by an expired timer. These histograms
  19777. + are available in the
  19778. +
  19779. + /sys/kernel/debug/tracing/latency_hist/timerandwakeup
  19780. +
  19781. + directory. They reflect the apparent interrupt and scheduling latency
  19782. + and are best suitable to determine the worst-case latency of a given
  19783. + system. To enable these histograms, write a non-zero number to
  19784. +
  19785. + /sys/kernel/debug/tracing/latency_hist/enable/timerandwakeup
  19786. +
  19787. config ENABLE_DEFAULT_TRACERS
  19788. bool "Trace process context switches and events"
  19789. depends on !GENERIC_TRACER
  19790. diff -Nur linux-4.9.28.orig/kernel/trace/latency_hist.c linux-4.9.28/kernel/trace/latency_hist.c
  19791. --- linux-4.9.28.orig/kernel/trace/latency_hist.c 1970-01-01 01:00:00.000000000 +0100
  19792. +++ linux-4.9.28/kernel/trace/latency_hist.c 2017-05-19 03:37:25.202177573 +0200
  19793. @@ -0,0 +1,1178 @@
  19794. +/*
  19795. + * kernel/trace/latency_hist.c
  19796. + *
  19797. + * Add support for histograms of preemption-off latency and
  19798. + * interrupt-off latency and wakeup latency, it depends on
  19799. + * Real-Time Preemption Support.
  19800. + *
  19801. + * Copyright (C) 2005 MontaVista Software, Inc.
  19802. + * Yi Yang <yyang@ch.mvista.com>
  19803. + *
  19804. + * Converted to work with the new latency tracer.
  19805. + * Copyright (C) 2008 Red Hat, Inc.
  19806. + * Steven Rostedt <srostedt@redhat.com>
  19807. + *
  19808. + */
  19809. +#include <linux/module.h>
  19810. +#include <linux/debugfs.h>
  19811. +#include <linux/seq_file.h>
  19812. +#include <linux/percpu.h>
  19813. +#include <linux/kallsyms.h>
  19814. +#include <linux/uaccess.h>
  19815. +#include <linux/sched.h>
  19816. +#include <linux/sched/rt.h>
  19817. +#include <linux/slab.h>
  19818. +#include <linux/atomic.h>
  19819. +#include <asm/div64.h>
  19820. +
  19821. +#include "trace.h"
  19822. +#include <trace/events/sched.h>
  19823. +
  19824. +#define NSECS_PER_USECS 1000L
  19825. +
  19826. +#define CREATE_TRACE_POINTS
  19827. +#include <trace/events/hist.h>
  19828. +
  19829. +enum {
  19830. + IRQSOFF_LATENCY = 0,
  19831. + PREEMPTOFF_LATENCY,
  19832. + PREEMPTIRQSOFF_LATENCY,
  19833. + WAKEUP_LATENCY,
  19834. + WAKEUP_LATENCY_SHAREDPRIO,
  19835. + MISSED_TIMER_OFFSETS,
  19836. + TIMERANDWAKEUP_LATENCY,
  19837. + MAX_LATENCY_TYPE,
  19838. +};
  19839. +
  19840. +#define MAX_ENTRY_NUM 10240
  19841. +
  19842. +struct hist_data {
  19843. + atomic_t hist_mode; /* 0 log, 1 don't log */
  19844. + long offset; /* set it to MAX_ENTRY_NUM/2 for a bipolar scale */
  19845. + long min_lat;
  19846. + long max_lat;
  19847. + unsigned long long below_hist_bound_samples;
  19848. + unsigned long long above_hist_bound_samples;
  19849. + long long accumulate_lat;
  19850. + unsigned long long total_samples;
  19851. + unsigned long long hist_array[MAX_ENTRY_NUM];
  19852. +};
  19853. +
  19854. +struct enable_data {
  19855. + int latency_type;
  19856. + int enabled;
  19857. +};
  19858. +
  19859. +static char *latency_hist_dir_root = "latency_hist";
  19860. +
  19861. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  19862. +static DEFINE_PER_CPU(struct hist_data, irqsoff_hist);
  19863. +static char *irqsoff_hist_dir = "irqsoff";
  19864. +static DEFINE_PER_CPU(cycles_t, hist_irqsoff_start);
  19865. +static DEFINE_PER_CPU(int, hist_irqsoff_counting);
  19866. +#endif
  19867. +
  19868. +#ifdef CONFIG_PREEMPT_OFF_HIST
  19869. +static DEFINE_PER_CPU(struct hist_data, preemptoff_hist);
  19870. +static char *preemptoff_hist_dir = "preemptoff";
  19871. +static DEFINE_PER_CPU(cycles_t, hist_preemptoff_start);
  19872. +static DEFINE_PER_CPU(int, hist_preemptoff_counting);
  19873. +#endif
  19874. +
  19875. +#if defined(CONFIG_PREEMPT_OFF_HIST) && defined(CONFIG_INTERRUPT_OFF_HIST)
  19876. +static DEFINE_PER_CPU(struct hist_data, preemptirqsoff_hist);
  19877. +static char *preemptirqsoff_hist_dir = "preemptirqsoff";
  19878. +static DEFINE_PER_CPU(cycles_t, hist_preemptirqsoff_start);
  19879. +static DEFINE_PER_CPU(int, hist_preemptirqsoff_counting);
  19880. +#endif
  19881. +
  19882. +#if defined(CONFIG_PREEMPT_OFF_HIST) || defined(CONFIG_INTERRUPT_OFF_HIST)
  19883. +static notrace void probe_preemptirqsoff_hist(void *v, int reason, int start);
  19884. +static struct enable_data preemptirqsoff_enabled_data = {
  19885. + .latency_type = PREEMPTIRQSOFF_LATENCY,
  19886. + .enabled = 0,
  19887. +};
  19888. +#endif
  19889. +
  19890. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  19891. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  19892. +struct maxlatproc_data {
  19893. + char comm[FIELD_SIZEOF(struct task_struct, comm)];
  19894. + char current_comm[FIELD_SIZEOF(struct task_struct, comm)];
  19895. + int pid;
  19896. + int current_pid;
  19897. + int prio;
  19898. + int current_prio;
  19899. + long latency;
  19900. + long timeroffset;
  19901. + cycle_t timestamp;
  19902. +};
  19903. +#endif
  19904. +
  19905. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  19906. +static DEFINE_PER_CPU(struct hist_data, wakeup_latency_hist);
  19907. +static DEFINE_PER_CPU(struct hist_data, wakeup_latency_hist_sharedprio);
  19908. +static char *wakeup_latency_hist_dir = "wakeup";
  19909. +static char *wakeup_latency_hist_dir_sharedprio = "sharedprio";
  19910. +static notrace void probe_wakeup_latency_hist_start(void *v,
  19911. + struct task_struct *p);
  19912. +static notrace void probe_wakeup_latency_hist_stop(void *v,
  19913. + bool preempt, struct task_struct *prev, struct task_struct *next);
  19914. +static notrace void probe_sched_migrate_task(void *,
  19915. + struct task_struct *task, int cpu);
  19916. +static struct enable_data wakeup_latency_enabled_data = {
  19917. + .latency_type = WAKEUP_LATENCY,
  19918. + .enabled = 0,
  19919. +};
  19920. +static DEFINE_PER_CPU(struct maxlatproc_data, wakeup_maxlatproc);
  19921. +static DEFINE_PER_CPU(struct maxlatproc_data, wakeup_maxlatproc_sharedprio);
  19922. +static DEFINE_PER_CPU(struct task_struct *, wakeup_task);
  19923. +static DEFINE_PER_CPU(int, wakeup_sharedprio);
  19924. +static unsigned long wakeup_pid;
  19925. +#endif
  19926. +
  19927. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  19928. +static DEFINE_PER_CPU(struct hist_data, missed_timer_offsets);
  19929. +static char *missed_timer_offsets_dir = "missed_timer_offsets";
  19930. +static notrace void probe_hrtimer_interrupt(void *v, int cpu,
  19931. + long long offset, struct task_struct *curr, struct task_struct *task);
  19932. +static struct enable_data missed_timer_offsets_enabled_data = {
  19933. + .latency_type = MISSED_TIMER_OFFSETS,
  19934. + .enabled = 0,
  19935. +};
  19936. +static DEFINE_PER_CPU(struct maxlatproc_data, missed_timer_offsets_maxlatproc);
  19937. +static unsigned long missed_timer_offsets_pid;
  19938. +#endif
  19939. +
  19940. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  19941. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  19942. +static DEFINE_PER_CPU(struct hist_data, timerandwakeup_latency_hist);
  19943. +static char *timerandwakeup_latency_hist_dir = "timerandwakeup";
  19944. +static struct enable_data timerandwakeup_enabled_data = {
  19945. + .latency_type = TIMERANDWAKEUP_LATENCY,
  19946. + .enabled = 0,
  19947. +};
  19948. +static DEFINE_PER_CPU(struct maxlatproc_data, timerandwakeup_maxlatproc);
  19949. +#endif
  19950. +
  19951. +void notrace latency_hist(int latency_type, int cpu, long latency,
  19952. + long timeroffset, cycle_t stop,
  19953. + struct task_struct *p)
  19954. +{
  19955. + struct hist_data *my_hist;
  19956. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  19957. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  19958. + struct maxlatproc_data *mp = NULL;
  19959. +#endif
  19960. +
  19961. + if (!cpu_possible(cpu) || latency_type < 0 ||
  19962. + latency_type >= MAX_LATENCY_TYPE)
  19963. + return;
  19964. +
  19965. + switch (latency_type) {
  19966. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  19967. + case IRQSOFF_LATENCY:
  19968. + my_hist = &per_cpu(irqsoff_hist, cpu);
  19969. + break;
  19970. +#endif
  19971. +#ifdef CONFIG_PREEMPT_OFF_HIST
  19972. + case PREEMPTOFF_LATENCY:
  19973. + my_hist = &per_cpu(preemptoff_hist, cpu);
  19974. + break;
  19975. +#endif
  19976. +#if defined(CONFIG_PREEMPT_OFF_HIST) && defined(CONFIG_INTERRUPT_OFF_HIST)
  19977. + case PREEMPTIRQSOFF_LATENCY:
  19978. + my_hist = &per_cpu(preemptirqsoff_hist, cpu);
  19979. + break;
  19980. +#endif
  19981. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  19982. + case WAKEUP_LATENCY:
  19983. + my_hist = &per_cpu(wakeup_latency_hist, cpu);
  19984. + mp = &per_cpu(wakeup_maxlatproc, cpu);
  19985. + break;
  19986. + case WAKEUP_LATENCY_SHAREDPRIO:
  19987. + my_hist = &per_cpu(wakeup_latency_hist_sharedprio, cpu);
  19988. + mp = &per_cpu(wakeup_maxlatproc_sharedprio, cpu);
  19989. + break;
  19990. +#endif
  19991. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  19992. + case MISSED_TIMER_OFFSETS:
  19993. + my_hist = &per_cpu(missed_timer_offsets, cpu);
  19994. + mp = &per_cpu(missed_timer_offsets_maxlatproc, cpu);
  19995. + break;
  19996. +#endif
  19997. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  19998. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  19999. + case TIMERANDWAKEUP_LATENCY:
  20000. + my_hist = &per_cpu(timerandwakeup_latency_hist, cpu);
  20001. + mp = &per_cpu(timerandwakeup_maxlatproc, cpu);
  20002. + break;
  20003. +#endif
  20004. +
  20005. + default:
  20006. + return;
  20007. + }
  20008. +
  20009. + latency += my_hist->offset;
  20010. +
  20011. + if (atomic_read(&my_hist->hist_mode) == 0)
  20012. + return;
  20013. +
  20014. + if (latency < 0 || latency >= MAX_ENTRY_NUM) {
  20015. + if (latency < 0)
  20016. + my_hist->below_hist_bound_samples++;
  20017. + else
  20018. + my_hist->above_hist_bound_samples++;
  20019. + } else
  20020. + my_hist->hist_array[latency]++;
  20021. +
  20022. + if (unlikely(latency > my_hist->max_lat ||
  20023. + my_hist->min_lat == LONG_MAX)) {
  20024. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  20025. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  20026. + if (latency_type == WAKEUP_LATENCY ||
  20027. + latency_type == WAKEUP_LATENCY_SHAREDPRIO ||
  20028. + latency_type == MISSED_TIMER_OFFSETS ||
  20029. + latency_type == TIMERANDWAKEUP_LATENCY) {
  20030. + strncpy(mp->comm, p->comm, sizeof(mp->comm));
  20031. + strncpy(mp->current_comm, current->comm,
  20032. + sizeof(mp->current_comm));
  20033. + mp->pid = task_pid_nr(p);
  20034. + mp->current_pid = task_pid_nr(current);
  20035. + mp->prio = p->prio;
  20036. + mp->current_prio = current->prio;
  20037. + mp->latency = latency;
  20038. + mp->timeroffset = timeroffset;
  20039. + mp->timestamp = stop;
  20040. + }
  20041. +#endif
  20042. + my_hist->max_lat = latency;
  20043. + }
  20044. + if (unlikely(latency < my_hist->min_lat))
  20045. + my_hist->min_lat = latency;
  20046. + my_hist->total_samples++;
  20047. + my_hist->accumulate_lat += latency;
  20048. +}
  20049. +
  20050. +static void *l_start(struct seq_file *m, loff_t *pos)
  20051. +{
  20052. + loff_t *index_ptr = NULL;
  20053. + loff_t index = *pos;
  20054. + struct hist_data *my_hist = m->private;
  20055. +
  20056. + if (index == 0) {
  20057. + char minstr[32], avgstr[32], maxstr[32];
  20058. +
  20059. + atomic_dec(&my_hist->hist_mode);
  20060. +
  20061. + if (likely(my_hist->total_samples)) {
  20062. + long avg = (long) div64_s64(my_hist->accumulate_lat,
  20063. + my_hist->total_samples);
  20064. + snprintf(minstr, sizeof(minstr), "%ld",
  20065. + my_hist->min_lat - my_hist->offset);
  20066. + snprintf(avgstr, sizeof(avgstr), "%ld",
  20067. + avg - my_hist->offset);
  20068. + snprintf(maxstr, sizeof(maxstr), "%ld",
  20069. + my_hist->max_lat - my_hist->offset);
  20070. + } else {
  20071. + strcpy(minstr, "<undef>");
  20072. + strcpy(avgstr, minstr);
  20073. + strcpy(maxstr, minstr);
  20074. + }
  20075. +
  20076. + seq_printf(m, "#Minimum latency: %s microseconds\n"
  20077. + "#Average latency: %s microseconds\n"
  20078. + "#Maximum latency: %s microseconds\n"
  20079. + "#Total samples: %llu\n"
  20080. + "#There are %llu samples lower than %ld"
  20081. + " microseconds.\n"
  20082. + "#There are %llu samples greater or equal"
  20083. + " than %ld microseconds.\n"
  20084. + "#usecs\t%16s\n",
  20085. + minstr, avgstr, maxstr,
  20086. + my_hist->total_samples,
  20087. + my_hist->below_hist_bound_samples,
  20088. + -my_hist->offset,
  20089. + my_hist->above_hist_bound_samples,
  20090. + MAX_ENTRY_NUM - my_hist->offset,
  20091. + "samples");
  20092. + }
  20093. + if (index < MAX_ENTRY_NUM) {
  20094. + index_ptr = kmalloc(sizeof(loff_t), GFP_KERNEL);
  20095. + if (index_ptr)
  20096. + *index_ptr = index;
  20097. + }
  20098. +
  20099. + return index_ptr;
  20100. +}
  20101. +
  20102. +static void *l_next(struct seq_file *m, void *p, loff_t *pos)
  20103. +{
  20104. + loff_t *index_ptr = p;
  20105. + struct hist_data *my_hist = m->private;
  20106. +
  20107. + if (++*pos >= MAX_ENTRY_NUM) {
  20108. + atomic_inc(&my_hist->hist_mode);
  20109. + return NULL;
  20110. + }
  20111. + *index_ptr = *pos;
  20112. + return index_ptr;
  20113. +}
  20114. +
  20115. +static void l_stop(struct seq_file *m, void *p)
  20116. +{
  20117. + kfree(p);
  20118. +}
  20119. +
  20120. +static int l_show(struct seq_file *m, void *p)
  20121. +{
  20122. + int index = *(loff_t *) p;
  20123. + struct hist_data *my_hist = m->private;
  20124. +
  20125. + seq_printf(m, "%6ld\t%16llu\n", index - my_hist->offset,
  20126. + my_hist->hist_array[index]);
  20127. + return 0;
  20128. +}
  20129. +
  20130. +static const struct seq_operations latency_hist_seq_op = {
  20131. + .start = l_start,
  20132. + .next = l_next,
  20133. + .stop = l_stop,
  20134. + .show = l_show
  20135. +};
  20136. +
  20137. +static int latency_hist_open(struct inode *inode, struct file *file)
  20138. +{
  20139. + int ret;
  20140. +
  20141. + ret = seq_open(file, &latency_hist_seq_op);
  20142. + if (!ret) {
  20143. + struct seq_file *seq = file->private_data;
  20144. + seq->private = inode->i_private;
  20145. + }
  20146. + return ret;
  20147. +}
  20148. +
  20149. +static const struct file_operations latency_hist_fops = {
  20150. + .open = latency_hist_open,
  20151. + .read = seq_read,
  20152. + .llseek = seq_lseek,
  20153. + .release = seq_release,
  20154. +};
  20155. +
  20156. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  20157. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  20158. +static void clear_maxlatprocdata(struct maxlatproc_data *mp)
  20159. +{
  20160. + mp->comm[0] = mp->current_comm[0] = '\0';
  20161. + mp->prio = mp->current_prio = mp->pid = mp->current_pid =
  20162. + mp->latency = mp->timeroffset = -1;
  20163. + mp->timestamp = 0;
  20164. +}
  20165. +#endif
  20166. +
  20167. +static void hist_reset(struct hist_data *hist)
  20168. +{
  20169. + atomic_dec(&hist->hist_mode);
  20170. +
  20171. + memset(hist->hist_array, 0, sizeof(hist->hist_array));
  20172. + hist->below_hist_bound_samples = 0ULL;
  20173. + hist->above_hist_bound_samples = 0ULL;
  20174. + hist->min_lat = LONG_MAX;
  20175. + hist->max_lat = LONG_MIN;
  20176. + hist->total_samples = 0ULL;
  20177. + hist->accumulate_lat = 0LL;
  20178. +
  20179. + atomic_inc(&hist->hist_mode);
  20180. +}
  20181. +
  20182. +static ssize_t
  20183. +latency_hist_reset(struct file *file, const char __user *a,
  20184. + size_t size, loff_t *off)
  20185. +{
  20186. + int cpu;
  20187. + struct hist_data *hist = NULL;
  20188. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  20189. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  20190. + struct maxlatproc_data *mp = NULL;
  20191. +#endif
  20192. + off_t latency_type = (off_t) file->private_data;
  20193. +
  20194. + for_each_online_cpu(cpu) {
  20195. +
  20196. + switch (latency_type) {
  20197. +#ifdef CONFIG_PREEMPT_OFF_HIST
  20198. + case PREEMPTOFF_LATENCY:
  20199. + hist = &per_cpu(preemptoff_hist, cpu);
  20200. + break;
  20201. +#endif
  20202. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  20203. + case IRQSOFF_LATENCY:
  20204. + hist = &per_cpu(irqsoff_hist, cpu);
  20205. + break;
  20206. +#endif
  20207. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  20208. + case PREEMPTIRQSOFF_LATENCY:
  20209. + hist = &per_cpu(preemptirqsoff_hist, cpu);
  20210. + break;
  20211. +#endif
  20212. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  20213. + case WAKEUP_LATENCY:
  20214. + hist = &per_cpu(wakeup_latency_hist, cpu);
  20215. + mp = &per_cpu(wakeup_maxlatproc, cpu);
  20216. + break;
  20217. + case WAKEUP_LATENCY_SHAREDPRIO:
  20218. + hist = &per_cpu(wakeup_latency_hist_sharedprio, cpu);
  20219. + mp = &per_cpu(wakeup_maxlatproc_sharedprio, cpu);
  20220. + break;
  20221. +#endif
  20222. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  20223. + case MISSED_TIMER_OFFSETS:
  20224. + hist = &per_cpu(missed_timer_offsets, cpu);
  20225. + mp = &per_cpu(missed_timer_offsets_maxlatproc, cpu);
  20226. + break;
  20227. +#endif
  20228. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  20229. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  20230. + case TIMERANDWAKEUP_LATENCY:
  20231. + hist = &per_cpu(timerandwakeup_latency_hist, cpu);
  20232. + mp = &per_cpu(timerandwakeup_maxlatproc, cpu);
  20233. + break;
  20234. +#endif
  20235. + }
  20236. +
  20237. + hist_reset(hist);
  20238. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  20239. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  20240. + if (latency_type == WAKEUP_LATENCY ||
  20241. + latency_type == WAKEUP_LATENCY_SHAREDPRIO ||
  20242. + latency_type == MISSED_TIMER_OFFSETS ||
  20243. + latency_type == TIMERANDWAKEUP_LATENCY)
  20244. + clear_maxlatprocdata(mp);
  20245. +#endif
  20246. + }
  20247. +
  20248. + return size;
  20249. +}
  20250. +
  20251. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  20252. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  20253. +static ssize_t
  20254. +show_pid(struct file *file, char __user *ubuf, size_t cnt, loff_t *ppos)
  20255. +{
  20256. + char buf[64];
  20257. + int r;
  20258. + unsigned long *this_pid = file->private_data;
  20259. +
  20260. + r = snprintf(buf, sizeof(buf), "%lu\n", *this_pid);
  20261. + return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  20262. +}
  20263. +
  20264. +static ssize_t do_pid(struct file *file, const char __user *ubuf,
  20265. + size_t cnt, loff_t *ppos)
  20266. +{
  20267. + char buf[64];
  20268. + unsigned long pid;
  20269. + unsigned long *this_pid = file->private_data;
  20270. +
  20271. + if (cnt >= sizeof(buf))
  20272. + return -EINVAL;
  20273. +
  20274. + if (copy_from_user(&buf, ubuf, cnt))
  20275. + return -EFAULT;
  20276. +
  20277. + buf[cnt] = '\0';
  20278. +
  20279. + if (kstrtoul(buf, 10, &pid))
  20280. + return -EINVAL;
  20281. +
  20282. + *this_pid = pid;
  20283. +
  20284. + return cnt;
  20285. +}
  20286. +#endif
  20287. +
  20288. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  20289. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  20290. +static ssize_t
  20291. +show_maxlatproc(struct file *file, char __user *ubuf, size_t cnt, loff_t *ppos)
  20292. +{
  20293. + int r;
  20294. + struct maxlatproc_data *mp = file->private_data;
  20295. + int strmaxlen = (TASK_COMM_LEN * 2) + (8 * 8);
  20296. + unsigned long long t;
  20297. + unsigned long usecs, secs;
  20298. + char *buf;
  20299. +
  20300. + if (mp->pid == -1 || mp->current_pid == -1) {
  20301. + buf = "(none)\n";
  20302. + return simple_read_from_buffer(ubuf, cnt, ppos, buf,
  20303. + strlen(buf));
  20304. + }
  20305. +
  20306. + buf = kmalloc(strmaxlen, GFP_KERNEL);
  20307. + if (buf == NULL)
  20308. + return -ENOMEM;
  20309. +
  20310. + t = ns2usecs(mp->timestamp);
  20311. + usecs = do_div(t, USEC_PER_SEC);
  20312. + secs = (unsigned long) t;
  20313. + r = snprintf(buf, strmaxlen,
  20314. + "%d %d %ld (%ld) %s <- %d %d %s %lu.%06lu\n", mp->pid,
  20315. + MAX_RT_PRIO-1 - mp->prio, mp->latency, mp->timeroffset, mp->comm,
  20316. + mp->current_pid, MAX_RT_PRIO-1 - mp->current_prio, mp->current_comm,
  20317. + secs, usecs);
  20318. + r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  20319. + kfree(buf);
  20320. + return r;
  20321. +}
  20322. +#endif
  20323. +
  20324. +static ssize_t
  20325. +show_enable(struct file *file, char __user *ubuf, size_t cnt, loff_t *ppos)
  20326. +{
  20327. + char buf[64];
  20328. + struct enable_data *ed = file->private_data;
  20329. + int r;
  20330. +
  20331. + r = snprintf(buf, sizeof(buf), "%d\n", ed->enabled);
  20332. + return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  20333. +}
  20334. +
  20335. +static ssize_t
  20336. +do_enable(struct file *file, const char __user *ubuf, size_t cnt, loff_t *ppos)
  20337. +{
  20338. + char buf[64];
  20339. + long enable;
  20340. + struct enable_data *ed = file->private_data;
  20341. +
  20342. + if (cnt >= sizeof(buf))
  20343. + return -EINVAL;
  20344. +
  20345. + if (copy_from_user(&buf, ubuf, cnt))
  20346. + return -EFAULT;
  20347. +
  20348. + buf[cnt] = 0;
  20349. +
  20350. + if (kstrtoul(buf, 10, &enable))
  20351. + return -EINVAL;
  20352. +
  20353. + if ((enable && ed->enabled) || (!enable && !ed->enabled))
  20354. + return cnt;
  20355. +
  20356. + if (enable) {
  20357. + int ret;
  20358. +
  20359. + switch (ed->latency_type) {
  20360. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  20361. + case PREEMPTIRQSOFF_LATENCY:
  20362. + ret = register_trace_preemptirqsoff_hist(
  20363. + probe_preemptirqsoff_hist, NULL);
  20364. + if (ret) {
  20365. + pr_info("wakeup trace: Couldn't assign "
  20366. + "probe_preemptirqsoff_hist "
  20367. + "to trace_preemptirqsoff_hist\n");
  20368. + return ret;
  20369. + }
  20370. + break;
  20371. +#endif
  20372. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  20373. + case WAKEUP_LATENCY:
  20374. + ret = register_trace_sched_wakeup(
  20375. + probe_wakeup_latency_hist_start, NULL);
  20376. + if (ret) {
  20377. + pr_info("wakeup trace: Couldn't assign "
  20378. + "probe_wakeup_latency_hist_start "
  20379. + "to trace_sched_wakeup\n");
  20380. + return ret;
  20381. + }
  20382. + ret = register_trace_sched_wakeup_new(
  20383. + probe_wakeup_latency_hist_start, NULL);
  20384. + if (ret) {
  20385. + pr_info("wakeup trace: Couldn't assign "
  20386. + "probe_wakeup_latency_hist_start "
  20387. + "to trace_sched_wakeup_new\n");
  20388. + unregister_trace_sched_wakeup(
  20389. + probe_wakeup_latency_hist_start, NULL);
  20390. + return ret;
  20391. + }
  20392. + ret = register_trace_sched_switch(
  20393. + probe_wakeup_latency_hist_stop, NULL);
  20394. + if (ret) {
  20395. + pr_info("wakeup trace: Couldn't assign "
  20396. + "probe_wakeup_latency_hist_stop "
  20397. + "to trace_sched_switch\n");
  20398. + unregister_trace_sched_wakeup(
  20399. + probe_wakeup_latency_hist_start, NULL);
  20400. + unregister_trace_sched_wakeup_new(
  20401. + probe_wakeup_latency_hist_start, NULL);
  20402. + return ret;
  20403. + }
  20404. + ret = register_trace_sched_migrate_task(
  20405. + probe_sched_migrate_task, NULL);
  20406. + if (ret) {
  20407. + pr_info("wakeup trace: Couldn't assign "
  20408. + "probe_sched_migrate_task "
  20409. + "to trace_sched_migrate_task\n");
  20410. + unregister_trace_sched_wakeup(
  20411. + probe_wakeup_latency_hist_start, NULL);
  20412. + unregister_trace_sched_wakeup_new(
  20413. + probe_wakeup_latency_hist_start, NULL);
  20414. + unregister_trace_sched_switch(
  20415. + probe_wakeup_latency_hist_stop, NULL);
  20416. + return ret;
  20417. + }
  20418. + break;
  20419. +#endif
  20420. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  20421. + case MISSED_TIMER_OFFSETS:
  20422. + ret = register_trace_hrtimer_interrupt(
  20423. + probe_hrtimer_interrupt, NULL);
  20424. + if (ret) {
  20425. + pr_info("wakeup trace: Couldn't assign "
  20426. + "probe_hrtimer_interrupt "
  20427. + "to trace_hrtimer_interrupt\n");
  20428. + return ret;
  20429. + }
  20430. + break;
  20431. +#endif
  20432. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  20433. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  20434. + case TIMERANDWAKEUP_LATENCY:
  20435. + if (!wakeup_latency_enabled_data.enabled ||
  20436. + !missed_timer_offsets_enabled_data.enabled)
  20437. + return -EINVAL;
  20438. + break;
  20439. +#endif
  20440. + default:
  20441. + break;
  20442. + }
  20443. + } else {
  20444. + switch (ed->latency_type) {
  20445. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  20446. + case PREEMPTIRQSOFF_LATENCY:
  20447. + {
  20448. + int cpu;
  20449. +
  20450. + unregister_trace_preemptirqsoff_hist(
  20451. + probe_preemptirqsoff_hist, NULL);
  20452. + for_each_online_cpu(cpu) {
  20453. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  20454. + per_cpu(hist_irqsoff_counting,
  20455. + cpu) = 0;
  20456. +#endif
  20457. +#ifdef CONFIG_PREEMPT_OFF_HIST
  20458. + per_cpu(hist_preemptoff_counting,
  20459. + cpu) = 0;
  20460. +#endif
  20461. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  20462. + per_cpu(hist_preemptirqsoff_counting,
  20463. + cpu) = 0;
  20464. +#endif
  20465. + }
  20466. + }
  20467. + break;
  20468. +#endif
  20469. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  20470. + case WAKEUP_LATENCY:
  20471. + {
  20472. + int cpu;
  20473. +
  20474. + unregister_trace_sched_wakeup(
  20475. + probe_wakeup_latency_hist_start, NULL);
  20476. + unregister_trace_sched_wakeup_new(
  20477. + probe_wakeup_latency_hist_start, NULL);
  20478. + unregister_trace_sched_switch(
  20479. + probe_wakeup_latency_hist_stop, NULL);
  20480. + unregister_trace_sched_migrate_task(
  20481. + probe_sched_migrate_task, NULL);
  20482. +
  20483. + for_each_online_cpu(cpu) {
  20484. + per_cpu(wakeup_task, cpu) = NULL;
  20485. + per_cpu(wakeup_sharedprio, cpu) = 0;
  20486. + }
  20487. + }
  20488. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  20489. + timerandwakeup_enabled_data.enabled = 0;
  20490. +#endif
  20491. + break;
  20492. +#endif
  20493. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  20494. + case MISSED_TIMER_OFFSETS:
  20495. + unregister_trace_hrtimer_interrupt(
  20496. + probe_hrtimer_interrupt, NULL);
  20497. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  20498. + timerandwakeup_enabled_data.enabled = 0;
  20499. +#endif
  20500. + break;
  20501. +#endif
  20502. + default:
  20503. + break;
  20504. + }
  20505. + }
  20506. + ed->enabled = enable;
  20507. + return cnt;
  20508. +}
  20509. +
  20510. +static const struct file_operations latency_hist_reset_fops = {
  20511. + .open = tracing_open_generic,
  20512. + .write = latency_hist_reset,
  20513. +};
  20514. +
  20515. +static const struct file_operations enable_fops = {
  20516. + .open = tracing_open_generic,
  20517. + .read = show_enable,
  20518. + .write = do_enable,
  20519. +};
  20520. +
  20521. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  20522. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  20523. +static const struct file_operations pid_fops = {
  20524. + .open = tracing_open_generic,
  20525. + .read = show_pid,
  20526. + .write = do_pid,
  20527. +};
  20528. +
  20529. +static const struct file_operations maxlatproc_fops = {
  20530. + .open = tracing_open_generic,
  20531. + .read = show_maxlatproc,
  20532. +};
  20533. +#endif
  20534. +
  20535. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  20536. +static notrace void probe_preemptirqsoff_hist(void *v, int reason,
  20537. + int starthist)
  20538. +{
  20539. + int cpu = raw_smp_processor_id();
  20540. + int time_set = 0;
  20541. +
  20542. + if (starthist) {
  20543. + cycle_t uninitialized_var(start);
  20544. +
  20545. + if (!preempt_count() && !irqs_disabled())
  20546. + return;
  20547. +
  20548. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  20549. + if ((reason == IRQS_OFF || reason == TRACE_START) &&
  20550. + !per_cpu(hist_irqsoff_counting, cpu)) {
  20551. + per_cpu(hist_irqsoff_counting, cpu) = 1;
  20552. + start = ftrace_now(cpu);
  20553. + time_set++;
  20554. + per_cpu(hist_irqsoff_start, cpu) = start;
  20555. + }
  20556. +#endif
  20557. +
  20558. +#ifdef CONFIG_PREEMPT_OFF_HIST
  20559. + if ((reason == PREEMPT_OFF || reason == TRACE_START) &&
  20560. + !per_cpu(hist_preemptoff_counting, cpu)) {
  20561. + per_cpu(hist_preemptoff_counting, cpu) = 1;
  20562. + if (!(time_set++))
  20563. + start = ftrace_now(cpu);
  20564. + per_cpu(hist_preemptoff_start, cpu) = start;
  20565. + }
  20566. +#endif
  20567. +
  20568. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  20569. + if (per_cpu(hist_irqsoff_counting, cpu) &&
  20570. + per_cpu(hist_preemptoff_counting, cpu) &&
  20571. + !per_cpu(hist_preemptirqsoff_counting, cpu)) {
  20572. + per_cpu(hist_preemptirqsoff_counting, cpu) = 1;
  20573. + if (!time_set)
  20574. + start = ftrace_now(cpu);
  20575. + per_cpu(hist_preemptirqsoff_start, cpu) = start;
  20576. + }
  20577. +#endif
  20578. + } else {
  20579. + cycle_t uninitialized_var(stop);
  20580. +
  20581. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  20582. + if ((reason == IRQS_ON || reason == TRACE_STOP) &&
  20583. + per_cpu(hist_irqsoff_counting, cpu)) {
  20584. + cycle_t start = per_cpu(hist_irqsoff_start, cpu);
  20585. +
  20586. + stop = ftrace_now(cpu);
  20587. + time_set++;
  20588. + if (start) {
  20589. + long latency = ((long) (stop - start)) /
  20590. + NSECS_PER_USECS;
  20591. +
  20592. + latency_hist(IRQSOFF_LATENCY, cpu, latency, 0,
  20593. + stop, NULL);
  20594. + }
  20595. + per_cpu(hist_irqsoff_counting, cpu) = 0;
  20596. + }
  20597. +#endif
  20598. +
  20599. +#ifdef CONFIG_PREEMPT_OFF_HIST
  20600. + if ((reason == PREEMPT_ON || reason == TRACE_STOP) &&
  20601. + per_cpu(hist_preemptoff_counting, cpu)) {
  20602. + cycle_t start = per_cpu(hist_preemptoff_start, cpu);
  20603. +
  20604. + if (!(time_set++))
  20605. + stop = ftrace_now(cpu);
  20606. + if (start) {
  20607. + long latency = ((long) (stop - start)) /
  20608. + NSECS_PER_USECS;
  20609. +
  20610. + latency_hist(PREEMPTOFF_LATENCY, cpu, latency,
  20611. + 0, stop, NULL);
  20612. + }
  20613. + per_cpu(hist_preemptoff_counting, cpu) = 0;
  20614. + }
  20615. +#endif
  20616. +
  20617. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  20618. + if ((!per_cpu(hist_irqsoff_counting, cpu) ||
  20619. + !per_cpu(hist_preemptoff_counting, cpu)) &&
  20620. + per_cpu(hist_preemptirqsoff_counting, cpu)) {
  20621. + cycle_t start = per_cpu(hist_preemptirqsoff_start, cpu);
  20622. +
  20623. + if (!time_set)
  20624. + stop = ftrace_now(cpu);
  20625. + if (start) {
  20626. + long latency = ((long) (stop - start)) /
  20627. + NSECS_PER_USECS;
  20628. +
  20629. + latency_hist(PREEMPTIRQSOFF_LATENCY, cpu,
  20630. + latency, 0, stop, NULL);
  20631. + }
  20632. + per_cpu(hist_preemptirqsoff_counting, cpu) = 0;
  20633. + }
  20634. +#endif
  20635. + }
  20636. +}
  20637. +#endif
  20638. +
  20639. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  20640. +static DEFINE_RAW_SPINLOCK(wakeup_lock);
  20641. +static notrace void probe_sched_migrate_task(void *v, struct task_struct *task,
  20642. + int cpu)
  20643. +{
  20644. + int old_cpu = task_cpu(task);
  20645. +
  20646. + if (cpu != old_cpu) {
  20647. + unsigned long flags;
  20648. + struct task_struct *cpu_wakeup_task;
  20649. +
  20650. + raw_spin_lock_irqsave(&wakeup_lock, flags);
  20651. +
  20652. + cpu_wakeup_task = per_cpu(wakeup_task, old_cpu);
  20653. + if (task == cpu_wakeup_task) {
  20654. + put_task_struct(cpu_wakeup_task);
  20655. + per_cpu(wakeup_task, old_cpu) = NULL;
  20656. + cpu_wakeup_task = per_cpu(wakeup_task, cpu) = task;
  20657. + get_task_struct(cpu_wakeup_task);
  20658. + }
  20659. +
  20660. + raw_spin_unlock_irqrestore(&wakeup_lock, flags);
  20661. + }
  20662. +}
  20663. +
  20664. +static notrace void probe_wakeup_latency_hist_start(void *v,
  20665. + struct task_struct *p)
  20666. +{
  20667. + unsigned long flags;
  20668. + struct task_struct *curr = current;
  20669. + int cpu = task_cpu(p);
  20670. + struct task_struct *cpu_wakeup_task;
  20671. +
  20672. + raw_spin_lock_irqsave(&wakeup_lock, flags);
  20673. +
  20674. + cpu_wakeup_task = per_cpu(wakeup_task, cpu);
  20675. +
  20676. + if (wakeup_pid) {
  20677. + if ((cpu_wakeup_task && p->prio == cpu_wakeup_task->prio) ||
  20678. + p->prio == curr->prio)
  20679. + per_cpu(wakeup_sharedprio, cpu) = 1;
  20680. + if (likely(wakeup_pid != task_pid_nr(p)))
  20681. + goto out;
  20682. + } else {
  20683. + if (likely(!rt_task(p)) ||
  20684. + (cpu_wakeup_task && p->prio > cpu_wakeup_task->prio) ||
  20685. + p->prio > curr->prio)
  20686. + goto out;
  20687. + if ((cpu_wakeup_task && p->prio == cpu_wakeup_task->prio) ||
  20688. + p->prio == curr->prio)
  20689. + per_cpu(wakeup_sharedprio, cpu) = 1;
  20690. + }
  20691. +
  20692. + if (cpu_wakeup_task)
  20693. + put_task_struct(cpu_wakeup_task);
  20694. + cpu_wakeup_task = per_cpu(wakeup_task, cpu) = p;
  20695. + get_task_struct(cpu_wakeup_task);
  20696. + cpu_wakeup_task->preempt_timestamp_hist =
  20697. + ftrace_now(raw_smp_processor_id());
  20698. +out:
  20699. + raw_spin_unlock_irqrestore(&wakeup_lock, flags);
  20700. +}
  20701. +
  20702. +static notrace void probe_wakeup_latency_hist_stop(void *v,
  20703. + bool preempt, struct task_struct *prev, struct task_struct *next)
  20704. +{
  20705. + unsigned long flags;
  20706. + int cpu = task_cpu(next);
  20707. + long latency;
  20708. + cycle_t stop;
  20709. + struct task_struct *cpu_wakeup_task;
  20710. +
  20711. + raw_spin_lock_irqsave(&wakeup_lock, flags);
  20712. +
  20713. + cpu_wakeup_task = per_cpu(wakeup_task, cpu);
  20714. +
  20715. + if (cpu_wakeup_task == NULL)
  20716. + goto out;
  20717. +
  20718. + /* Already running? */
  20719. + if (unlikely(current == cpu_wakeup_task))
  20720. + goto out_reset;
  20721. +
  20722. + if (next != cpu_wakeup_task) {
  20723. + if (next->prio < cpu_wakeup_task->prio)
  20724. + goto out_reset;
  20725. +
  20726. + if (next->prio == cpu_wakeup_task->prio)
  20727. + per_cpu(wakeup_sharedprio, cpu) = 1;
  20728. +
  20729. + goto out;
  20730. + }
  20731. +
  20732. + if (current->prio == cpu_wakeup_task->prio)
  20733. + per_cpu(wakeup_sharedprio, cpu) = 1;
  20734. +
  20735. + /*
  20736. + * The task we are waiting for is about to be switched to.
  20737. + * Calculate latency and store it in histogram.
  20738. + */
  20739. + stop = ftrace_now(raw_smp_processor_id());
  20740. +
  20741. + latency = ((long) (stop - next->preempt_timestamp_hist)) /
  20742. + NSECS_PER_USECS;
  20743. +
  20744. + if (per_cpu(wakeup_sharedprio, cpu)) {
  20745. + latency_hist(WAKEUP_LATENCY_SHAREDPRIO, cpu, latency, 0, stop,
  20746. + next);
  20747. + per_cpu(wakeup_sharedprio, cpu) = 0;
  20748. + } else {
  20749. + latency_hist(WAKEUP_LATENCY, cpu, latency, 0, stop, next);
  20750. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  20751. + if (timerandwakeup_enabled_data.enabled) {
  20752. + latency_hist(TIMERANDWAKEUP_LATENCY, cpu,
  20753. + next->timer_offset + latency, next->timer_offset,
  20754. + stop, next);
  20755. + }
  20756. +#endif
  20757. + }
  20758. +
  20759. +out_reset:
  20760. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  20761. + next->timer_offset = 0;
  20762. +#endif
  20763. + put_task_struct(cpu_wakeup_task);
  20764. + per_cpu(wakeup_task, cpu) = NULL;
  20765. +out:
  20766. + raw_spin_unlock_irqrestore(&wakeup_lock, flags);
  20767. +}
  20768. +#endif
  20769. +
  20770. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  20771. +static notrace void probe_hrtimer_interrupt(void *v, int cpu,
  20772. + long long latency_ns, struct task_struct *curr,
  20773. + struct task_struct *task)
  20774. +{
  20775. + if (latency_ns <= 0 && task != NULL && rt_task(task) &&
  20776. + (task->prio < curr->prio ||
  20777. + (task->prio == curr->prio &&
  20778. + !cpumask_test_cpu(cpu, &task->cpus_allowed)))) {
  20779. + long latency;
  20780. + cycle_t now;
  20781. +
  20782. + if (missed_timer_offsets_pid) {
  20783. + if (likely(missed_timer_offsets_pid !=
  20784. + task_pid_nr(task)))
  20785. + return;
  20786. + }
  20787. +
  20788. + now = ftrace_now(cpu);
  20789. + latency = (long) div_s64(-latency_ns, NSECS_PER_USECS);
  20790. + latency_hist(MISSED_TIMER_OFFSETS, cpu, latency, latency, now,
  20791. + task);
  20792. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  20793. + task->timer_offset = latency;
  20794. +#endif
  20795. + }
  20796. +}
  20797. +#endif
  20798. +
  20799. +static __init int latency_hist_init(void)
  20800. +{
  20801. + struct dentry *latency_hist_root = NULL;
  20802. + struct dentry *dentry;
  20803. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  20804. + struct dentry *dentry_sharedprio;
  20805. +#endif
  20806. + struct dentry *entry;
  20807. + struct dentry *enable_root;
  20808. + int i = 0;
  20809. + struct hist_data *my_hist;
  20810. + char name[64];
  20811. + char *cpufmt = "CPU%d";
  20812. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  20813. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  20814. + char *cpufmt_maxlatproc = "max_latency-CPU%d";
  20815. + struct maxlatproc_data *mp = NULL;
  20816. +#endif
  20817. +
  20818. + dentry = tracing_init_dentry();
  20819. + latency_hist_root = debugfs_create_dir(latency_hist_dir_root, dentry);
  20820. + enable_root = debugfs_create_dir("enable", latency_hist_root);
  20821. +
  20822. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  20823. + dentry = debugfs_create_dir(irqsoff_hist_dir, latency_hist_root);
  20824. + for_each_possible_cpu(i) {
  20825. + sprintf(name, cpufmt, i);
  20826. + entry = debugfs_create_file(name, 0444, dentry,
  20827. + &per_cpu(irqsoff_hist, i), &latency_hist_fops);
  20828. + my_hist = &per_cpu(irqsoff_hist, i);
  20829. + atomic_set(&my_hist->hist_mode, 1);
  20830. + my_hist->min_lat = LONG_MAX;
  20831. + }
  20832. + entry = debugfs_create_file("reset", 0644, dentry,
  20833. + (void *)IRQSOFF_LATENCY, &latency_hist_reset_fops);
  20834. +#endif
  20835. +
  20836. +#ifdef CONFIG_PREEMPT_OFF_HIST
  20837. + dentry = debugfs_create_dir(preemptoff_hist_dir,
  20838. + latency_hist_root);
  20839. + for_each_possible_cpu(i) {
  20840. + sprintf(name, cpufmt, i);
  20841. + entry = debugfs_create_file(name, 0444, dentry,
  20842. + &per_cpu(preemptoff_hist, i), &latency_hist_fops);
  20843. + my_hist = &per_cpu(preemptoff_hist, i);
  20844. + atomic_set(&my_hist->hist_mode, 1);
  20845. + my_hist->min_lat = LONG_MAX;
  20846. + }
  20847. + entry = debugfs_create_file("reset", 0644, dentry,
  20848. + (void *)PREEMPTOFF_LATENCY, &latency_hist_reset_fops);
  20849. +#endif
  20850. +
  20851. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  20852. + dentry = debugfs_create_dir(preemptirqsoff_hist_dir,
  20853. + latency_hist_root);
  20854. + for_each_possible_cpu(i) {
  20855. + sprintf(name, cpufmt, i);
  20856. + entry = debugfs_create_file(name, 0444, dentry,
  20857. + &per_cpu(preemptirqsoff_hist, i), &latency_hist_fops);
  20858. + my_hist = &per_cpu(preemptirqsoff_hist, i);
  20859. + atomic_set(&my_hist->hist_mode, 1);
  20860. + my_hist->min_lat = LONG_MAX;
  20861. + }
  20862. + entry = debugfs_create_file("reset", 0644, dentry,
  20863. + (void *)PREEMPTIRQSOFF_LATENCY, &latency_hist_reset_fops);
  20864. +#endif
  20865. +
  20866. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  20867. + entry = debugfs_create_file("preemptirqsoff", 0644,
  20868. + enable_root, (void *)&preemptirqsoff_enabled_data,
  20869. + &enable_fops);
  20870. +#endif
  20871. +
  20872. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  20873. + dentry = debugfs_create_dir(wakeup_latency_hist_dir,
  20874. + latency_hist_root);
  20875. + dentry_sharedprio = debugfs_create_dir(
  20876. + wakeup_latency_hist_dir_sharedprio, dentry);
  20877. + for_each_possible_cpu(i) {
  20878. + sprintf(name, cpufmt, i);
  20879. +
  20880. + entry = debugfs_create_file(name, 0444, dentry,
  20881. + &per_cpu(wakeup_latency_hist, i),
  20882. + &latency_hist_fops);
  20883. + my_hist = &per_cpu(wakeup_latency_hist, i);
  20884. + atomic_set(&my_hist->hist_mode, 1);
  20885. + my_hist->min_lat = LONG_MAX;
  20886. +
  20887. + entry = debugfs_create_file(name, 0444, dentry_sharedprio,
  20888. + &per_cpu(wakeup_latency_hist_sharedprio, i),
  20889. + &latency_hist_fops);
  20890. + my_hist = &per_cpu(wakeup_latency_hist_sharedprio, i);
  20891. + atomic_set(&my_hist->hist_mode, 1);
  20892. + my_hist->min_lat = LONG_MAX;
  20893. +
  20894. + sprintf(name, cpufmt_maxlatproc, i);
  20895. +
  20896. + mp = &per_cpu(wakeup_maxlatproc, i);
  20897. + entry = debugfs_create_file(name, 0444, dentry, mp,
  20898. + &maxlatproc_fops);
  20899. + clear_maxlatprocdata(mp);
  20900. +
  20901. + mp = &per_cpu(wakeup_maxlatproc_sharedprio, i);
  20902. + entry = debugfs_create_file(name, 0444, dentry_sharedprio, mp,
  20903. + &maxlatproc_fops);
  20904. + clear_maxlatprocdata(mp);
  20905. + }
  20906. + entry = debugfs_create_file("pid", 0644, dentry,
  20907. + (void *)&wakeup_pid, &pid_fops);
  20908. + entry = debugfs_create_file("reset", 0644, dentry,
  20909. + (void *)WAKEUP_LATENCY, &latency_hist_reset_fops);
  20910. + entry = debugfs_create_file("reset", 0644, dentry_sharedprio,
  20911. + (void *)WAKEUP_LATENCY_SHAREDPRIO, &latency_hist_reset_fops);
  20912. + entry = debugfs_create_file("wakeup", 0644,
  20913. + enable_root, (void *)&wakeup_latency_enabled_data,
  20914. + &enable_fops);
  20915. +#endif
  20916. +
  20917. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  20918. + dentry = debugfs_create_dir(missed_timer_offsets_dir,
  20919. + latency_hist_root);
  20920. + for_each_possible_cpu(i) {
  20921. + sprintf(name, cpufmt, i);
  20922. + entry = debugfs_create_file(name, 0444, dentry,
  20923. + &per_cpu(missed_timer_offsets, i), &latency_hist_fops);
  20924. + my_hist = &per_cpu(missed_timer_offsets, i);
  20925. + atomic_set(&my_hist->hist_mode, 1);
  20926. + my_hist->min_lat = LONG_MAX;
  20927. +
  20928. + sprintf(name, cpufmt_maxlatproc, i);
  20929. + mp = &per_cpu(missed_timer_offsets_maxlatproc, i);
  20930. + entry = debugfs_create_file(name, 0444, dentry, mp,
  20931. + &maxlatproc_fops);
  20932. + clear_maxlatprocdata(mp);
  20933. + }
  20934. + entry = debugfs_create_file("pid", 0644, dentry,
  20935. + (void *)&missed_timer_offsets_pid, &pid_fops);
  20936. + entry = debugfs_create_file("reset", 0644, dentry,
  20937. + (void *)MISSED_TIMER_OFFSETS, &latency_hist_reset_fops);
  20938. + entry = debugfs_create_file("missed_timer_offsets", 0644,
  20939. + enable_root, (void *)&missed_timer_offsets_enabled_data,
  20940. + &enable_fops);
  20941. +#endif
  20942. +
  20943. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  20944. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  20945. + dentry = debugfs_create_dir(timerandwakeup_latency_hist_dir,
  20946. + latency_hist_root);
  20947. + for_each_possible_cpu(i) {
  20948. + sprintf(name, cpufmt, i);
  20949. + entry = debugfs_create_file(name, 0444, dentry,
  20950. + &per_cpu(timerandwakeup_latency_hist, i),
  20951. + &latency_hist_fops);
  20952. + my_hist = &per_cpu(timerandwakeup_latency_hist, i);
  20953. + atomic_set(&my_hist->hist_mode, 1);
  20954. + my_hist->min_lat = LONG_MAX;
  20955. +
  20956. + sprintf(name, cpufmt_maxlatproc, i);
  20957. + mp = &per_cpu(timerandwakeup_maxlatproc, i);
  20958. + entry = debugfs_create_file(name, 0444, dentry, mp,
  20959. + &maxlatproc_fops);
  20960. + clear_maxlatprocdata(mp);
  20961. + }
  20962. + entry = debugfs_create_file("reset", 0644, dentry,
  20963. + (void *)TIMERANDWAKEUP_LATENCY, &latency_hist_reset_fops);
  20964. + entry = debugfs_create_file("timerandwakeup", 0644,
  20965. + enable_root, (void *)&timerandwakeup_enabled_data,
  20966. + &enable_fops);
  20967. +#endif
  20968. + return 0;
  20969. +}
  20970. +
  20971. +device_initcall(latency_hist_init);
  20972. diff -Nur linux-4.9.28.orig/kernel/trace/Makefile linux-4.9.28/kernel/trace/Makefile
  20973. --- linux-4.9.28.orig/kernel/trace/Makefile 2017-05-14 14:00:37.000000000 +0200
  20974. +++ linux-4.9.28/kernel/trace/Makefile 2017-05-19 03:37:25.202177573 +0200
  20975. @@ -38,6 +38,10 @@
  20976. obj-$(CONFIG_PREEMPT_TRACER) += trace_irqsoff.o
  20977. obj-$(CONFIG_SCHED_TRACER) += trace_sched_wakeup.o
  20978. obj-$(CONFIG_HWLAT_TRACER) += trace_hwlat.o
  20979. +obj-$(CONFIG_INTERRUPT_OFF_HIST) += latency_hist.o
  20980. +obj-$(CONFIG_PREEMPT_OFF_HIST) += latency_hist.o
  20981. +obj-$(CONFIG_WAKEUP_LATENCY_HIST) += latency_hist.o
  20982. +obj-$(CONFIG_MISSED_TIMER_OFFSETS_HIST) += latency_hist.o
  20983. obj-$(CONFIG_NOP_TRACER) += trace_nop.o
  20984. obj-$(CONFIG_STACK_TRACER) += trace_stack.o
  20985. obj-$(CONFIG_MMIOTRACE) += trace_mmiotrace.o
  20986. diff -Nur linux-4.9.28.orig/kernel/trace/trace.c linux-4.9.28/kernel/trace/trace.c
  20987. --- linux-4.9.28.orig/kernel/trace/trace.c 2017-05-14 14:00:37.000000000 +0200
  20988. +++ linux-4.9.28/kernel/trace/trace.c 2017-05-19 03:37:25.202177573 +0200
  20989. @@ -1897,6 +1897,7 @@
  20990. struct task_struct *tsk = current;
  20991. entry->preempt_count = pc & 0xff;
  20992. + entry->preempt_lazy_count = preempt_lazy_count();
  20993. entry->pid = (tsk) ? tsk->pid : 0;
  20994. entry->flags =
  20995. #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
  20996. @@ -1907,8 +1908,11 @@
  20997. ((pc & NMI_MASK ) ? TRACE_FLAG_NMI : 0) |
  20998. ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
  20999. ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
  21000. - (tif_need_resched() ? TRACE_FLAG_NEED_RESCHED : 0) |
  21001. + (tif_need_resched_now() ? TRACE_FLAG_NEED_RESCHED : 0) |
  21002. + (need_resched_lazy() ? TRACE_FLAG_NEED_RESCHED_LAZY : 0) |
  21003. (test_preempt_need_resched() ? TRACE_FLAG_PREEMPT_RESCHED : 0);
  21004. +
  21005. + entry->migrate_disable = (tsk) ? __migrate_disabled(tsk) & 0xFF : 0;
  21006. }
  21007. EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
  21008. @@ -2892,14 +2896,17 @@
  21009. static void print_lat_help_header(struct seq_file *m)
  21010. {
  21011. - seq_puts(m, "# _------=> CPU# \n"
  21012. - "# / _-----=> irqs-off \n"
  21013. - "# | / _----=> need-resched \n"
  21014. - "# || / _---=> hardirq/softirq \n"
  21015. - "# ||| / _--=> preempt-depth \n"
  21016. - "# |||| / delay \n"
  21017. - "# cmd pid ||||| time | caller \n"
  21018. - "# \\ / ||||| \\ | / \n");
  21019. + seq_puts(m, "# _--------=> CPU# \n"
  21020. + "# / _-------=> irqs-off \n"
  21021. + "# | / _------=> need-resched \n"
  21022. + "# || / _-----=> need-resched_lazy \n"
  21023. + "# ||| / _----=> hardirq/softirq \n"
  21024. + "# |||| / _---=> preempt-depth \n"
  21025. + "# ||||| / _--=> preempt-lazy-depth\n"
  21026. + "# |||||| / _-=> migrate-disable \n"
  21027. + "# ||||||| / delay \n"
  21028. + "# cmd pid |||||||| time | caller \n"
  21029. + "# \\ / |||||||| \\ | / \n");
  21030. }
  21031. static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
  21032. @@ -2925,11 +2932,14 @@
  21033. print_event_info(buf, m);
  21034. seq_puts(m, "# _-----=> irqs-off\n"
  21035. "# / _----=> need-resched\n"
  21036. - "# | / _---=> hardirq/softirq\n"
  21037. - "# || / _--=> preempt-depth\n"
  21038. - "# ||| / delay\n"
  21039. - "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n"
  21040. - "# | | | |||| | |\n");
  21041. + "# |/ _-----=> need-resched_lazy\n"
  21042. + "# || / _---=> hardirq/softirq\n"
  21043. + "# ||| / _--=> preempt-depth\n"
  21044. + "# |||| / _-=> preempt-lazy-depth\n"
  21045. + "# ||||| / _-=> migrate-disable \n"
  21046. + "# |||||| / delay\n"
  21047. + "# TASK-PID CPU# ||||||| TIMESTAMP FUNCTION\n"
  21048. + "# | | | ||||||| | |\n");
  21049. }
  21050. void
  21051. diff -Nur linux-4.9.28.orig/kernel/trace/trace_events.c linux-4.9.28/kernel/trace/trace_events.c
  21052. --- linux-4.9.28.orig/kernel/trace/trace_events.c 2017-05-14 14:00:37.000000000 +0200
  21053. +++ linux-4.9.28/kernel/trace/trace_events.c 2017-05-19 03:37:25.202177573 +0200
  21054. @@ -187,6 +187,8 @@
  21055. __common_field(unsigned char, flags);
  21056. __common_field(unsigned char, preempt_count);
  21057. __common_field(int, pid);
  21058. + __common_field(unsigned short, migrate_disable);
  21059. + __common_field(unsigned short, padding);
  21060. return ret;
  21061. }
  21062. diff -Nur linux-4.9.28.orig/kernel/trace/trace.h linux-4.9.28/kernel/trace/trace.h
  21063. --- linux-4.9.28.orig/kernel/trace/trace.h 2017-05-14 14:00:37.000000000 +0200
  21064. +++ linux-4.9.28/kernel/trace/trace.h 2017-05-19 03:37:25.202177573 +0200
  21065. @@ -124,6 +124,7 @@
  21066. * NEED_RESCHED - reschedule is requested
  21067. * HARDIRQ - inside an interrupt handler
  21068. * SOFTIRQ - inside a softirq handler
  21069. + * NEED_RESCHED_LAZY - lazy reschedule is requested
  21070. */
  21071. enum trace_flag_type {
  21072. TRACE_FLAG_IRQS_OFF = 0x01,
  21073. @@ -133,6 +134,7 @@
  21074. TRACE_FLAG_SOFTIRQ = 0x10,
  21075. TRACE_FLAG_PREEMPT_RESCHED = 0x20,
  21076. TRACE_FLAG_NMI = 0x40,
  21077. + TRACE_FLAG_NEED_RESCHED_LAZY = 0x80,
  21078. };
  21079. #define TRACE_BUF_SIZE 1024
  21080. diff -Nur linux-4.9.28.orig/kernel/trace/trace_irqsoff.c linux-4.9.28/kernel/trace/trace_irqsoff.c
  21081. --- linux-4.9.28.orig/kernel/trace/trace_irqsoff.c 2017-05-14 14:00:37.000000000 +0200
  21082. +++ linux-4.9.28/kernel/trace/trace_irqsoff.c 2017-05-19 03:37:25.202177573 +0200
  21083. @@ -13,6 +13,7 @@
  21084. #include <linux/uaccess.h>
  21085. #include <linux/module.h>
  21086. #include <linux/ftrace.h>
  21087. +#include <trace/events/hist.h>
  21088. #include "trace.h"
  21089. @@ -424,11 +425,13 @@
  21090. {
  21091. if (preempt_trace() || irq_trace())
  21092. start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  21093. + trace_preemptirqsoff_hist_rcuidle(TRACE_START, 1);
  21094. }
  21095. EXPORT_SYMBOL_GPL(start_critical_timings);
  21096. void stop_critical_timings(void)
  21097. {
  21098. + trace_preemptirqsoff_hist_rcuidle(TRACE_STOP, 0);
  21099. if (preempt_trace() || irq_trace())
  21100. stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  21101. }
  21102. @@ -438,6 +441,7 @@
  21103. #ifdef CONFIG_PROVE_LOCKING
  21104. void time_hardirqs_on(unsigned long a0, unsigned long a1)
  21105. {
  21106. + trace_preemptirqsoff_hist_rcuidle(IRQS_ON, 0);
  21107. if (!preempt_trace() && irq_trace())
  21108. stop_critical_timing(a0, a1);
  21109. }
  21110. @@ -446,6 +450,7 @@
  21111. {
  21112. if (!preempt_trace() && irq_trace())
  21113. start_critical_timing(a0, a1);
  21114. + trace_preemptirqsoff_hist_rcuidle(IRQS_OFF, 1);
  21115. }
  21116. #else /* !CONFIG_PROVE_LOCKING */
  21117. @@ -471,6 +476,7 @@
  21118. */
  21119. void trace_hardirqs_on(void)
  21120. {
  21121. + trace_preemptirqsoff_hist(IRQS_ON, 0);
  21122. if (!preempt_trace() && irq_trace())
  21123. stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  21124. }
  21125. @@ -480,11 +486,13 @@
  21126. {
  21127. if (!preempt_trace() && irq_trace())
  21128. start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  21129. + trace_preemptirqsoff_hist(IRQS_OFF, 1);
  21130. }
  21131. EXPORT_SYMBOL(trace_hardirqs_off);
  21132. __visible void trace_hardirqs_on_caller(unsigned long caller_addr)
  21133. {
  21134. + trace_preemptirqsoff_hist(IRQS_ON, 0);
  21135. if (!preempt_trace() && irq_trace())
  21136. stop_critical_timing(CALLER_ADDR0, caller_addr);
  21137. }
  21138. @@ -494,6 +502,7 @@
  21139. {
  21140. if (!preempt_trace() && irq_trace())
  21141. start_critical_timing(CALLER_ADDR0, caller_addr);
  21142. + trace_preemptirqsoff_hist(IRQS_OFF, 1);
  21143. }
  21144. EXPORT_SYMBOL(trace_hardirqs_off_caller);
  21145. @@ -503,12 +512,14 @@
  21146. #ifdef CONFIG_PREEMPT_TRACER
  21147. void trace_preempt_on(unsigned long a0, unsigned long a1)
  21148. {
  21149. + trace_preemptirqsoff_hist(PREEMPT_ON, 0);
  21150. if (preempt_trace() && !irq_trace())
  21151. stop_critical_timing(a0, a1);
  21152. }
  21153. void trace_preempt_off(unsigned long a0, unsigned long a1)
  21154. {
  21155. + trace_preemptirqsoff_hist(PREEMPT_ON, 1);
  21156. if (preempt_trace() && !irq_trace())
  21157. start_critical_timing(a0, a1);
  21158. }
  21159. diff -Nur linux-4.9.28.orig/kernel/trace/trace_output.c linux-4.9.28/kernel/trace/trace_output.c
  21160. --- linux-4.9.28.orig/kernel/trace/trace_output.c 2017-05-14 14:00:37.000000000 +0200
  21161. +++ linux-4.9.28/kernel/trace/trace_output.c 2017-05-19 03:37:25.202177573 +0200
  21162. @@ -386,6 +386,7 @@
  21163. {
  21164. char hardsoft_irq;
  21165. char need_resched;
  21166. + char need_resched_lazy;
  21167. char irqs_off;
  21168. int hardirq;
  21169. int softirq;
  21170. @@ -416,6 +417,9 @@
  21171. break;
  21172. }
  21173. + need_resched_lazy =
  21174. + (entry->flags & TRACE_FLAG_NEED_RESCHED_LAZY) ? 'L' : '.';
  21175. +
  21176. hardsoft_irq =
  21177. (nmi && hardirq) ? 'Z' :
  21178. nmi ? 'z' :
  21179. @@ -424,14 +428,25 @@
  21180. softirq ? 's' :
  21181. '.' ;
  21182. - trace_seq_printf(s, "%c%c%c",
  21183. - irqs_off, need_resched, hardsoft_irq);
  21184. + trace_seq_printf(s, "%c%c%c%c",
  21185. + irqs_off, need_resched, need_resched_lazy,
  21186. + hardsoft_irq);
  21187. if (entry->preempt_count)
  21188. trace_seq_printf(s, "%x", entry->preempt_count);
  21189. else
  21190. trace_seq_putc(s, '.');
  21191. + if (entry->preempt_lazy_count)
  21192. + trace_seq_printf(s, "%x", entry->preempt_lazy_count);
  21193. + else
  21194. + trace_seq_putc(s, '.');
  21195. +
  21196. + if (entry->migrate_disable)
  21197. + trace_seq_printf(s, "%x", entry->migrate_disable);
  21198. + else
  21199. + trace_seq_putc(s, '.');
  21200. +
  21201. return !trace_seq_has_overflowed(s);
  21202. }
  21203. diff -Nur linux-4.9.28.orig/kernel/user.c linux-4.9.28/kernel/user.c
  21204. --- linux-4.9.28.orig/kernel/user.c 2017-05-14 14:00:37.000000000 +0200
  21205. +++ linux-4.9.28/kernel/user.c 2017-05-19 03:37:25.202177573 +0200
  21206. @@ -161,11 +161,11 @@
  21207. if (!up)
  21208. return;
  21209. - local_irq_save(flags);
  21210. + local_irq_save_nort(flags);
  21211. if (atomic_dec_and_lock(&up->__count, &uidhash_lock))
  21212. free_user(up, flags);
  21213. else
  21214. - local_irq_restore(flags);
  21215. + local_irq_restore_nort(flags);
  21216. }
  21217. struct user_struct *alloc_uid(kuid_t uid)
  21218. diff -Nur linux-4.9.28.orig/kernel/watchdog.c linux-4.9.28/kernel/watchdog.c
  21219. --- linux-4.9.28.orig/kernel/watchdog.c 2017-05-14 14:00:37.000000000 +0200
  21220. +++ linux-4.9.28/kernel/watchdog.c 2017-05-19 03:37:25.202177573 +0200
  21221. @@ -315,6 +315,8 @@
  21222. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  21223. +static DEFINE_RAW_SPINLOCK(watchdog_output_lock);
  21224. +
  21225. static struct perf_event_attr wd_hw_attr = {
  21226. .type = PERF_TYPE_HARDWARE,
  21227. .config = PERF_COUNT_HW_CPU_CYCLES,
  21228. @@ -348,6 +350,13 @@
  21229. /* only print hardlockups once */
  21230. if (__this_cpu_read(hard_watchdog_warn) == true)
  21231. return;
  21232. + /*
  21233. + * If early-printk is enabled then make sure we do not
  21234. + * lock up in printk() and kill console logging:
  21235. + */
  21236. + printk_kill();
  21237. +
  21238. + raw_spin_lock(&watchdog_output_lock);
  21239. pr_emerg("Watchdog detected hard LOCKUP on cpu %d", this_cpu);
  21240. print_modules();
  21241. @@ -365,6 +374,7 @@
  21242. !test_and_set_bit(0, &hardlockup_allcpu_dumped))
  21243. trigger_allbutself_cpu_backtrace();
  21244. + raw_spin_unlock(&watchdog_output_lock);
  21245. if (hardlockup_panic)
  21246. nmi_panic(regs, "Hard LOCKUP");
  21247. @@ -512,6 +522,7 @@
  21248. /* kick off the timer for the hardlockup detector */
  21249. hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  21250. hrtimer->function = watchdog_timer_fn;
  21251. + hrtimer->irqsafe = 1;
  21252. /* Enable the perf event */
  21253. watchdog_nmi_enable(cpu);
  21254. diff -Nur linux-4.9.28.orig/kernel/workqueue.c linux-4.9.28/kernel/workqueue.c
  21255. --- linux-4.9.28.orig/kernel/workqueue.c 2017-05-14 14:00:37.000000000 +0200
  21256. +++ linux-4.9.28/kernel/workqueue.c 2017-05-19 03:37:25.206177727 +0200
  21257. @@ -48,6 +48,8 @@
  21258. #include <linux/nodemask.h>
  21259. #include <linux/moduleparam.h>
  21260. #include <linux/uaccess.h>
  21261. +#include <linux/locallock.h>
  21262. +#include <linux/delay.h>
  21263. #include "workqueue_internal.h"
  21264. @@ -121,11 +123,16 @@
  21265. * cpu or grabbing pool->lock is enough for read access. If
  21266. * POOL_DISASSOCIATED is set, it's identical to L.
  21267. *
  21268. + * On RT we need the extra protection via rt_lock_idle_list() for
  21269. + * the list manipulations against read access from
  21270. + * wq_worker_sleeping(). All other places are nicely serialized via
  21271. + * pool->lock.
  21272. + *
  21273. * A: pool->attach_mutex protected.
  21274. *
  21275. * PL: wq_pool_mutex protected.
  21276. *
  21277. - * PR: wq_pool_mutex protected for writes. Sched-RCU protected for reads.
  21278. + * PR: wq_pool_mutex protected for writes. RCU protected for reads.
  21279. *
  21280. * PW: wq_pool_mutex and wq->mutex protected for writes. Either for reads.
  21281. *
  21282. @@ -134,7 +141,7 @@
  21283. *
  21284. * WQ: wq->mutex protected.
  21285. *
  21286. - * WR: wq->mutex protected for writes. Sched-RCU protected for reads.
  21287. + * WR: wq->mutex protected for writes. RCU protected for reads.
  21288. *
  21289. * MD: wq_mayday_lock protected.
  21290. */
  21291. @@ -185,7 +192,7 @@
  21292. atomic_t nr_running ____cacheline_aligned_in_smp;
  21293. /*
  21294. - * Destruction of pool is sched-RCU protected to allow dereferences
  21295. + * Destruction of pool is RCU protected to allow dereferences
  21296. * from get_work_pool().
  21297. */
  21298. struct rcu_head rcu;
  21299. @@ -214,7 +221,7 @@
  21300. /*
  21301. * Release of unbound pwq is punted to system_wq. See put_pwq()
  21302. * and pwq_unbound_release_workfn() for details. pool_workqueue
  21303. - * itself is also sched-RCU protected so that the first pwq can be
  21304. + * itself is also RCU protected so that the first pwq can be
  21305. * determined without grabbing wq->mutex.
  21306. */
  21307. struct work_struct unbound_release_work;
  21308. @@ -348,6 +355,8 @@
  21309. struct workqueue_struct *system_freezable_power_efficient_wq __read_mostly;
  21310. EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq);
  21311. +static DEFINE_LOCAL_IRQ_LOCK(pendingb_lock);
  21312. +
  21313. static int worker_thread(void *__worker);
  21314. static void workqueue_sysfs_unregister(struct workqueue_struct *wq);
  21315. @@ -355,20 +364,20 @@
  21316. #include <trace/events/workqueue.h>
  21317. #define assert_rcu_or_pool_mutex() \
  21318. - RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held() && \
  21319. + RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
  21320. !lockdep_is_held(&wq_pool_mutex), \
  21321. - "sched RCU or wq_pool_mutex should be held")
  21322. + "RCU or wq_pool_mutex should be held")
  21323. #define assert_rcu_or_wq_mutex(wq) \
  21324. - RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held() && \
  21325. + RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
  21326. !lockdep_is_held(&wq->mutex), \
  21327. - "sched RCU or wq->mutex should be held")
  21328. + "RCU or wq->mutex should be held")
  21329. #define assert_rcu_or_wq_mutex_or_pool_mutex(wq) \
  21330. - RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held() && \
  21331. + RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
  21332. !lockdep_is_held(&wq->mutex) && \
  21333. !lockdep_is_held(&wq_pool_mutex), \
  21334. - "sched RCU, wq->mutex or wq_pool_mutex should be held")
  21335. + "RCU, wq->mutex or wq_pool_mutex should be held")
  21336. #define for_each_cpu_worker_pool(pool, cpu) \
  21337. for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \
  21338. @@ -380,7 +389,7 @@
  21339. * @pool: iteration cursor
  21340. * @pi: integer used for iteration
  21341. *
  21342. - * This must be called either with wq_pool_mutex held or sched RCU read
  21343. + * This must be called either with wq_pool_mutex held or RCU read
  21344. * locked. If the pool needs to be used beyond the locking in effect, the
  21345. * caller is responsible for guaranteeing that the pool stays online.
  21346. *
  21347. @@ -412,7 +421,7 @@
  21348. * @pwq: iteration cursor
  21349. * @wq: the target workqueue
  21350. *
  21351. - * This must be called either with wq->mutex held or sched RCU read locked.
  21352. + * This must be called either with wq->mutex held or RCU read locked.
  21353. * If the pwq needs to be used beyond the locking in effect, the caller is
  21354. * responsible for guaranteeing that the pwq stays online.
  21355. *
  21356. @@ -424,6 +433,31 @@
  21357. if (({ assert_rcu_or_wq_mutex(wq); false; })) { } \
  21358. else
  21359. +#ifdef CONFIG_PREEMPT_RT_BASE
  21360. +static inline void rt_lock_idle_list(struct worker_pool *pool)
  21361. +{
  21362. + preempt_disable();
  21363. +}
  21364. +static inline void rt_unlock_idle_list(struct worker_pool *pool)
  21365. +{
  21366. + preempt_enable();
  21367. +}
  21368. +static inline void sched_lock_idle_list(struct worker_pool *pool) { }
  21369. +static inline void sched_unlock_idle_list(struct worker_pool *pool) { }
  21370. +#else
  21371. +static inline void rt_lock_idle_list(struct worker_pool *pool) { }
  21372. +static inline void rt_unlock_idle_list(struct worker_pool *pool) { }
  21373. +static inline void sched_lock_idle_list(struct worker_pool *pool)
  21374. +{
  21375. + spin_lock_irq(&pool->lock);
  21376. +}
  21377. +static inline void sched_unlock_idle_list(struct worker_pool *pool)
  21378. +{
  21379. + spin_unlock_irq(&pool->lock);
  21380. +}
  21381. +#endif
  21382. +
  21383. +
  21384. #ifdef CONFIG_DEBUG_OBJECTS_WORK
  21385. static struct debug_obj_descr work_debug_descr;
  21386. @@ -548,7 +582,7 @@
  21387. * @wq: the target workqueue
  21388. * @node: the node ID
  21389. *
  21390. - * This must be called with any of wq_pool_mutex, wq->mutex or sched RCU
  21391. + * This must be called with any of wq_pool_mutex, wq->mutex or RCU
  21392. * read locked.
  21393. * If the pwq needs to be used beyond the locking in effect, the caller is
  21394. * responsible for guaranteeing that the pwq stays online.
  21395. @@ -692,8 +726,8 @@
  21396. * @work: the work item of interest
  21397. *
  21398. * Pools are created and destroyed under wq_pool_mutex, and allows read
  21399. - * access under sched-RCU read lock. As such, this function should be
  21400. - * called under wq_pool_mutex or with preemption disabled.
  21401. + * access under RCU read lock. As such, this function should be
  21402. + * called under wq_pool_mutex or inside of a rcu_read_lock() region.
  21403. *
  21404. * All fields of the returned pool are accessible as long as the above
  21405. * mentioned locking is in effect. If the returned pool needs to be used
  21406. @@ -830,50 +864,45 @@
  21407. */
  21408. static void wake_up_worker(struct worker_pool *pool)
  21409. {
  21410. - struct worker *worker = first_idle_worker(pool);
  21411. + struct worker *worker;
  21412. +
  21413. + rt_lock_idle_list(pool);
  21414. +
  21415. + worker = first_idle_worker(pool);
  21416. if (likely(worker))
  21417. wake_up_process(worker->task);
  21418. +
  21419. + rt_unlock_idle_list(pool);
  21420. }
  21421. /**
  21422. - * wq_worker_waking_up - a worker is waking up
  21423. + * wq_worker_running - a worker is running again
  21424. * @task: task waking up
  21425. - * @cpu: CPU @task is waking up to
  21426. - *
  21427. - * This function is called during try_to_wake_up() when a worker is
  21428. - * being awoken.
  21429. *
  21430. - * CONTEXT:
  21431. - * spin_lock_irq(rq->lock)
  21432. + * This function is called when a worker returns from schedule()
  21433. */
  21434. -void wq_worker_waking_up(struct task_struct *task, int cpu)
  21435. +void wq_worker_running(struct task_struct *task)
  21436. {
  21437. struct worker *worker = kthread_data(task);
  21438. - if (!(worker->flags & WORKER_NOT_RUNNING)) {
  21439. - WARN_ON_ONCE(worker->pool->cpu != cpu);
  21440. + if (!worker->sleeping)
  21441. + return;
  21442. + if (!(worker->flags & WORKER_NOT_RUNNING))
  21443. atomic_inc(&worker->pool->nr_running);
  21444. - }
  21445. + worker->sleeping = 0;
  21446. }
  21447. /**
  21448. * wq_worker_sleeping - a worker is going to sleep
  21449. * @task: task going to sleep
  21450. *
  21451. - * This function is called during schedule() when a busy worker is
  21452. - * going to sleep. Worker on the same cpu can be woken up by
  21453. - * returning pointer to its task.
  21454. - *
  21455. - * CONTEXT:
  21456. - * spin_lock_irq(rq->lock)
  21457. - *
  21458. - * Return:
  21459. - * Worker task on @cpu to wake up, %NULL if none.
  21460. + * This function is called from schedule() when a busy worker is
  21461. + * going to sleep.
  21462. */
  21463. -struct task_struct *wq_worker_sleeping(struct task_struct *task)
  21464. +void wq_worker_sleeping(struct task_struct *task)
  21465. {
  21466. - struct worker *worker = kthread_data(task), *to_wakeup = NULL;
  21467. + struct worker *worker = kthread_data(task);
  21468. struct worker_pool *pool;
  21469. /*
  21470. @@ -882,29 +911,26 @@
  21471. * checking NOT_RUNNING.
  21472. */
  21473. if (worker->flags & WORKER_NOT_RUNNING)
  21474. - return NULL;
  21475. + return;
  21476. pool = worker->pool;
  21477. - /* this can only happen on the local cpu */
  21478. - if (WARN_ON_ONCE(pool->cpu != raw_smp_processor_id()))
  21479. - return NULL;
  21480. + if (WARN_ON_ONCE(worker->sleeping))
  21481. + return;
  21482. +
  21483. + worker->sleeping = 1;
  21484. /*
  21485. * The counterpart of the following dec_and_test, implied mb,
  21486. * worklist not empty test sequence is in insert_work().
  21487. * Please read comment there.
  21488. - *
  21489. - * NOT_RUNNING is clear. This means that we're bound to and
  21490. - * running on the local cpu w/ rq lock held and preemption
  21491. - * disabled, which in turn means that none else could be
  21492. - * manipulating idle_list, so dereferencing idle_list without pool
  21493. - * lock is safe.
  21494. */
  21495. if (atomic_dec_and_test(&pool->nr_running) &&
  21496. - !list_empty(&pool->worklist))
  21497. - to_wakeup = first_idle_worker(pool);
  21498. - return to_wakeup ? to_wakeup->task : NULL;
  21499. + !list_empty(&pool->worklist)) {
  21500. + sched_lock_idle_list(pool);
  21501. + wake_up_worker(pool);
  21502. + sched_unlock_idle_list(pool);
  21503. + }
  21504. }
  21505. /**
  21506. @@ -1098,12 +1124,14 @@
  21507. {
  21508. if (pwq) {
  21509. /*
  21510. - * As both pwqs and pools are sched-RCU protected, the
  21511. + * As both pwqs and pools are RCU protected, the
  21512. * following lock operations are safe.
  21513. */
  21514. - spin_lock_irq(&pwq->pool->lock);
  21515. + rcu_read_lock();
  21516. + local_spin_lock_irq(pendingb_lock, &pwq->pool->lock);
  21517. put_pwq(pwq);
  21518. - spin_unlock_irq(&pwq->pool->lock);
  21519. + local_spin_unlock_irq(pendingb_lock, &pwq->pool->lock);
  21520. + rcu_read_unlock();
  21521. }
  21522. }
  21523. @@ -1207,7 +1235,7 @@
  21524. struct worker_pool *pool;
  21525. struct pool_workqueue *pwq;
  21526. - local_irq_save(*flags);
  21527. + local_lock_irqsave(pendingb_lock, *flags);
  21528. /* try to steal the timer if it exists */
  21529. if (is_dwork) {
  21530. @@ -1226,6 +1254,7 @@
  21531. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
  21532. return 0;
  21533. + rcu_read_lock();
  21534. /*
  21535. * The queueing is in progress, or it is already queued. Try to
  21536. * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
  21537. @@ -1264,14 +1293,16 @@
  21538. set_work_pool_and_keep_pending(work, pool->id);
  21539. spin_unlock(&pool->lock);
  21540. + rcu_read_unlock();
  21541. return 1;
  21542. }
  21543. spin_unlock(&pool->lock);
  21544. fail:
  21545. - local_irq_restore(*flags);
  21546. + rcu_read_unlock();
  21547. + local_unlock_irqrestore(pendingb_lock, *flags);
  21548. if (work_is_canceling(work))
  21549. return -ENOENT;
  21550. - cpu_relax();
  21551. + cpu_chill();
  21552. return -EAGAIN;
  21553. }
  21554. @@ -1373,7 +1404,7 @@
  21555. * queued or lose PENDING. Grabbing PENDING and queueing should
  21556. * happen with IRQ disabled.
  21557. */
  21558. - WARN_ON_ONCE(!irqs_disabled());
  21559. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  21560. debug_work_activate(work);
  21561. @@ -1381,6 +1412,7 @@
  21562. if (unlikely(wq->flags & __WQ_DRAINING) &&
  21563. WARN_ON_ONCE(!is_chained_work(wq)))
  21564. return;
  21565. + rcu_read_lock();
  21566. retry:
  21567. if (req_cpu == WORK_CPU_UNBOUND)
  21568. cpu = wq_select_unbound_cpu(raw_smp_processor_id());
  21569. @@ -1437,10 +1469,8 @@
  21570. /* pwq determined, queue */
  21571. trace_workqueue_queue_work(req_cpu, pwq, work);
  21572. - if (WARN_ON(!list_empty(&work->entry))) {
  21573. - spin_unlock(&pwq->pool->lock);
  21574. - return;
  21575. - }
  21576. + if (WARN_ON(!list_empty(&work->entry)))
  21577. + goto out;
  21578. pwq->nr_in_flight[pwq->work_color]++;
  21579. work_flags = work_color_to_flags(pwq->work_color);
  21580. @@ -1458,7 +1488,9 @@
  21581. insert_work(pwq, work, worklist, work_flags);
  21582. +out:
  21583. spin_unlock(&pwq->pool->lock);
  21584. + rcu_read_unlock();
  21585. }
  21586. /**
  21587. @@ -1478,14 +1510,14 @@
  21588. bool ret = false;
  21589. unsigned long flags;
  21590. - local_irq_save(flags);
  21591. + local_lock_irqsave(pendingb_lock,flags);
  21592. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
  21593. __queue_work(cpu, wq, work);
  21594. ret = true;
  21595. }
  21596. - local_irq_restore(flags);
  21597. + local_unlock_irqrestore(pendingb_lock, flags);
  21598. return ret;
  21599. }
  21600. EXPORT_SYMBOL(queue_work_on);
  21601. @@ -1552,14 +1584,14 @@
  21602. unsigned long flags;
  21603. /* read the comment in __queue_work() */
  21604. - local_irq_save(flags);
  21605. + local_lock_irqsave(pendingb_lock, flags);
  21606. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
  21607. __queue_delayed_work(cpu, wq, dwork, delay);
  21608. ret = true;
  21609. }
  21610. - local_irq_restore(flags);
  21611. + local_unlock_irqrestore(pendingb_lock, flags);
  21612. return ret;
  21613. }
  21614. EXPORT_SYMBOL(queue_delayed_work_on);
  21615. @@ -1594,7 +1626,7 @@
  21616. if (likely(ret >= 0)) {
  21617. __queue_delayed_work(cpu, wq, dwork, delay);
  21618. - local_irq_restore(flags);
  21619. + local_unlock_irqrestore(pendingb_lock, flags);
  21620. }
  21621. /* -ENOENT from try_to_grab_pending() becomes %true */
  21622. @@ -1627,7 +1659,9 @@
  21623. worker->last_active = jiffies;
  21624. /* idle_list is LIFO */
  21625. + rt_lock_idle_list(pool);
  21626. list_add(&worker->entry, &pool->idle_list);
  21627. + rt_unlock_idle_list(pool);
  21628. if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
  21629. mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
  21630. @@ -1660,7 +1694,9 @@
  21631. return;
  21632. worker_clr_flags(worker, WORKER_IDLE);
  21633. pool->nr_idle--;
  21634. + rt_lock_idle_list(pool);
  21635. list_del_init(&worker->entry);
  21636. + rt_unlock_idle_list(pool);
  21637. }
  21638. static struct worker *alloc_worker(int node)
  21639. @@ -1826,7 +1862,9 @@
  21640. pool->nr_workers--;
  21641. pool->nr_idle--;
  21642. + rt_lock_idle_list(pool);
  21643. list_del_init(&worker->entry);
  21644. + rt_unlock_idle_list(pool);
  21645. worker->flags |= WORKER_DIE;
  21646. wake_up_process(worker->task);
  21647. }
  21648. @@ -2785,14 +2823,14 @@
  21649. might_sleep();
  21650. - local_irq_disable();
  21651. + rcu_read_lock();
  21652. pool = get_work_pool(work);
  21653. if (!pool) {
  21654. - local_irq_enable();
  21655. + rcu_read_unlock();
  21656. return false;
  21657. }
  21658. - spin_lock(&pool->lock);
  21659. + spin_lock_irq(&pool->lock);
  21660. /* see the comment in try_to_grab_pending() with the same code */
  21661. pwq = get_work_pwq(work);
  21662. if (pwq) {
  21663. @@ -2821,10 +2859,11 @@
  21664. else
  21665. lock_map_acquire_read(&pwq->wq->lockdep_map);
  21666. lock_map_release(&pwq->wq->lockdep_map);
  21667. -
  21668. + rcu_read_unlock();
  21669. return true;
  21670. already_gone:
  21671. spin_unlock_irq(&pool->lock);
  21672. + rcu_read_unlock();
  21673. return false;
  21674. }
  21675. @@ -2911,7 +2950,7 @@
  21676. /* tell other tasks trying to grab @work to back off */
  21677. mark_work_canceling(work);
  21678. - local_irq_restore(flags);
  21679. + local_unlock_irqrestore(pendingb_lock, flags);
  21680. flush_work(work);
  21681. clear_work_data(work);
  21682. @@ -2966,10 +3005,10 @@
  21683. */
  21684. bool flush_delayed_work(struct delayed_work *dwork)
  21685. {
  21686. - local_irq_disable();
  21687. + local_lock_irq(pendingb_lock);
  21688. if (del_timer_sync(&dwork->timer))
  21689. __queue_work(dwork->cpu, dwork->wq, &dwork->work);
  21690. - local_irq_enable();
  21691. + local_unlock_irq(pendingb_lock);
  21692. return flush_work(&dwork->work);
  21693. }
  21694. EXPORT_SYMBOL(flush_delayed_work);
  21695. @@ -2987,7 +3026,7 @@
  21696. return false;
  21697. set_work_pool_and_clear_pending(work, get_work_pool_id(work));
  21698. - local_irq_restore(flags);
  21699. + local_unlock_irqrestore(pendingb_lock, flags);
  21700. return ret;
  21701. }
  21702. @@ -3245,7 +3284,7 @@
  21703. * put_unbound_pool - put a worker_pool
  21704. * @pool: worker_pool to put
  21705. *
  21706. - * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU
  21707. + * Put @pool. If its refcnt reaches zero, it gets destroyed in RCU
  21708. * safe manner. get_unbound_pool() calls this function on its failure path
  21709. * and this function should be able to release pools which went through,
  21710. * successfully or not, init_worker_pool().
  21711. @@ -3299,8 +3338,8 @@
  21712. del_timer_sync(&pool->idle_timer);
  21713. del_timer_sync(&pool->mayday_timer);
  21714. - /* sched-RCU protected to allow dereferences from get_work_pool() */
  21715. - call_rcu_sched(&pool->rcu, rcu_free_pool);
  21716. + /* RCU protected to allow dereferences from get_work_pool() */
  21717. + call_rcu(&pool->rcu, rcu_free_pool);
  21718. }
  21719. /**
  21720. @@ -3407,14 +3446,14 @@
  21721. put_unbound_pool(pool);
  21722. mutex_unlock(&wq_pool_mutex);
  21723. - call_rcu_sched(&pwq->rcu, rcu_free_pwq);
  21724. + call_rcu(&pwq->rcu, rcu_free_pwq);
  21725. /*
  21726. * If we're the last pwq going away, @wq is already dead and no one
  21727. * is gonna access it anymore. Schedule RCU free.
  21728. */
  21729. if (is_last)
  21730. - call_rcu_sched(&wq->rcu, rcu_free_wq);
  21731. + call_rcu(&wq->rcu, rcu_free_wq);
  21732. }
  21733. /**
  21734. @@ -4064,7 +4103,7 @@
  21735. * The base ref is never dropped on per-cpu pwqs. Directly
  21736. * schedule RCU free.
  21737. */
  21738. - call_rcu_sched(&wq->rcu, rcu_free_wq);
  21739. + call_rcu(&wq->rcu, rcu_free_wq);
  21740. } else {
  21741. /*
  21742. * We're the sole accessor of @wq at this point. Directly
  21743. @@ -4157,7 +4196,8 @@
  21744. struct pool_workqueue *pwq;
  21745. bool ret;
  21746. - rcu_read_lock_sched();
  21747. + rcu_read_lock();
  21748. + preempt_disable();
  21749. if (cpu == WORK_CPU_UNBOUND)
  21750. cpu = smp_processor_id();
  21751. @@ -4168,7 +4208,8 @@
  21752. pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
  21753. ret = !list_empty(&pwq->delayed_works);
  21754. - rcu_read_unlock_sched();
  21755. + preempt_enable();
  21756. + rcu_read_unlock();
  21757. return ret;
  21758. }
  21759. @@ -4194,15 +4235,15 @@
  21760. if (work_pending(work))
  21761. ret |= WORK_BUSY_PENDING;
  21762. - local_irq_save(flags);
  21763. + rcu_read_lock();
  21764. pool = get_work_pool(work);
  21765. if (pool) {
  21766. - spin_lock(&pool->lock);
  21767. + spin_lock_irqsave(&pool->lock, flags);
  21768. if (find_worker_executing_work(pool, work))
  21769. ret |= WORK_BUSY_RUNNING;
  21770. - spin_unlock(&pool->lock);
  21771. + spin_unlock_irqrestore(&pool->lock, flags);
  21772. }
  21773. - local_irq_restore(flags);
  21774. + rcu_read_unlock();
  21775. return ret;
  21776. }
  21777. @@ -4391,7 +4432,7 @@
  21778. unsigned long flags;
  21779. int pi;
  21780. - rcu_read_lock_sched();
  21781. + rcu_read_lock();
  21782. pr_info("Showing busy workqueues and worker pools:\n");
  21783. @@ -4444,7 +4485,7 @@
  21784. spin_unlock_irqrestore(&pool->lock, flags);
  21785. }
  21786. - rcu_read_unlock_sched();
  21787. + rcu_read_unlock();
  21788. }
  21789. /*
  21790. @@ -4782,16 +4823,16 @@
  21791. * nr_active is monotonically decreasing. It's safe
  21792. * to peek without lock.
  21793. */
  21794. - rcu_read_lock_sched();
  21795. + rcu_read_lock();
  21796. for_each_pwq(pwq, wq) {
  21797. WARN_ON_ONCE(pwq->nr_active < 0);
  21798. if (pwq->nr_active) {
  21799. busy = true;
  21800. - rcu_read_unlock_sched();
  21801. + rcu_read_unlock();
  21802. goto out_unlock;
  21803. }
  21804. }
  21805. - rcu_read_unlock_sched();
  21806. + rcu_read_unlock();
  21807. }
  21808. out_unlock:
  21809. mutex_unlock(&wq_pool_mutex);
  21810. @@ -4981,7 +5022,8 @@
  21811. const char *delim = "";
  21812. int node, written = 0;
  21813. - rcu_read_lock_sched();
  21814. + get_online_cpus();
  21815. + rcu_read_lock();
  21816. for_each_node(node) {
  21817. written += scnprintf(buf + written, PAGE_SIZE - written,
  21818. "%s%d:%d", delim, node,
  21819. @@ -4989,7 +5031,8 @@
  21820. delim = " ";
  21821. }
  21822. written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
  21823. - rcu_read_unlock_sched();
  21824. + rcu_read_unlock();
  21825. + put_online_cpus();
  21826. return written;
  21827. }
  21828. diff -Nur linux-4.9.28.orig/kernel/workqueue_internal.h linux-4.9.28/kernel/workqueue_internal.h
  21829. --- linux-4.9.28.orig/kernel/workqueue_internal.h 2017-05-14 14:00:37.000000000 +0200
  21830. +++ linux-4.9.28/kernel/workqueue_internal.h 2017-05-19 03:37:25.206177727 +0200
  21831. @@ -43,6 +43,7 @@
  21832. unsigned long last_active; /* L: last active timestamp */
  21833. unsigned int flags; /* X: flags */
  21834. int id; /* I: worker id */
  21835. + int sleeping; /* None */
  21836. /*
  21837. * Opaque string set with work_set_desc(). Printed out with task
  21838. @@ -68,7 +69,7 @@
  21839. * Scheduler hooks for concurrency managed workqueue. Only to be used from
  21840. * sched/core.c and workqueue.c.
  21841. */
  21842. -void wq_worker_waking_up(struct task_struct *task, int cpu);
  21843. -struct task_struct *wq_worker_sleeping(struct task_struct *task);
  21844. +void wq_worker_running(struct task_struct *task);
  21845. +void wq_worker_sleeping(struct task_struct *task);
  21846. #endif /* _KERNEL_WORKQUEUE_INTERNAL_H */
  21847. diff -Nur linux-4.9.28.orig/lib/debugobjects.c linux-4.9.28/lib/debugobjects.c
  21848. --- linux-4.9.28.orig/lib/debugobjects.c 2017-05-14 14:00:37.000000000 +0200
  21849. +++ linux-4.9.28/lib/debugobjects.c 2017-05-19 03:37:25.206177727 +0200
  21850. @@ -308,7 +308,10 @@
  21851. struct debug_obj *obj;
  21852. unsigned long flags;
  21853. - fill_pool();
  21854. +#ifdef CONFIG_PREEMPT_RT_FULL
  21855. + if (preempt_count() == 0 && !irqs_disabled())
  21856. +#endif
  21857. + fill_pool();
  21858. db = get_bucket((unsigned long) addr);
  21859. diff -Nur linux-4.9.28.orig/lib/idr.c linux-4.9.28/lib/idr.c
  21860. --- linux-4.9.28.orig/lib/idr.c 2017-05-14 14:00:37.000000000 +0200
  21861. +++ linux-4.9.28/lib/idr.c 2017-05-19 03:37:25.206177727 +0200
  21862. @@ -30,6 +30,7 @@
  21863. #include <linux/idr.h>
  21864. #include <linux/spinlock.h>
  21865. #include <linux/percpu.h>
  21866. +#include <linux/locallock.h>
  21867. #define MAX_IDR_SHIFT (sizeof(int) * 8 - 1)
  21868. #define MAX_IDR_BIT (1U << MAX_IDR_SHIFT)
  21869. @@ -45,6 +46,37 @@
  21870. static DEFINE_PER_CPU(int, idr_preload_cnt);
  21871. static DEFINE_SPINLOCK(simple_ida_lock);
  21872. +#ifdef CONFIG_PREEMPT_RT_FULL
  21873. +static DEFINE_LOCAL_IRQ_LOCK(idr_lock);
  21874. +
  21875. +static inline void idr_preload_lock(void)
  21876. +{
  21877. + local_lock(idr_lock);
  21878. +}
  21879. +
  21880. +static inline void idr_preload_unlock(void)
  21881. +{
  21882. + local_unlock(idr_lock);
  21883. +}
  21884. +
  21885. +void idr_preload_end(void)
  21886. +{
  21887. + idr_preload_unlock();
  21888. +}
  21889. +EXPORT_SYMBOL(idr_preload_end);
  21890. +#else
  21891. +static inline void idr_preload_lock(void)
  21892. +{
  21893. + preempt_disable();
  21894. +}
  21895. +
  21896. +static inline void idr_preload_unlock(void)
  21897. +{
  21898. + preempt_enable();
  21899. +}
  21900. +#endif
  21901. +
  21902. +
  21903. /* the maximum ID which can be allocated given idr->layers */
  21904. static int idr_max(int layers)
  21905. {
  21906. @@ -115,14 +147,14 @@
  21907. * context. See idr_preload() for details.
  21908. */
  21909. if (!in_interrupt()) {
  21910. - preempt_disable();
  21911. + idr_preload_lock();
  21912. new = __this_cpu_read(idr_preload_head);
  21913. if (new) {
  21914. __this_cpu_write(idr_preload_head, new->ary[0]);
  21915. __this_cpu_dec(idr_preload_cnt);
  21916. new->ary[0] = NULL;
  21917. }
  21918. - preempt_enable();
  21919. + idr_preload_unlock();
  21920. if (new)
  21921. return new;
  21922. }
  21923. @@ -366,7 +398,6 @@
  21924. idr_mark_full(pa, id);
  21925. }
  21926. -
  21927. /**
  21928. * idr_preload - preload for idr_alloc()
  21929. * @gfp_mask: allocation mask to use for preloading
  21930. @@ -401,7 +432,7 @@
  21931. WARN_ON_ONCE(in_interrupt());
  21932. might_sleep_if(gfpflags_allow_blocking(gfp_mask));
  21933. - preempt_disable();
  21934. + idr_preload_lock();
  21935. /*
  21936. * idr_alloc() is likely to succeed w/o full idr_layer buffer and
  21937. @@ -413,9 +444,9 @@
  21938. while (__this_cpu_read(idr_preload_cnt) < MAX_IDR_FREE) {
  21939. struct idr_layer *new;
  21940. - preempt_enable();
  21941. + idr_preload_unlock();
  21942. new = kmem_cache_zalloc(idr_layer_cache, gfp_mask);
  21943. - preempt_disable();
  21944. + idr_preload_lock();
  21945. if (!new)
  21946. break;
  21947. diff -Nur linux-4.9.28.orig/lib/irq_poll.c linux-4.9.28/lib/irq_poll.c
  21948. --- linux-4.9.28.orig/lib/irq_poll.c 2017-05-14 14:00:37.000000000 +0200
  21949. +++ linux-4.9.28/lib/irq_poll.c 2017-05-19 03:37:25.206177727 +0200
  21950. @@ -36,6 +36,7 @@
  21951. list_add_tail(&iop->list, this_cpu_ptr(&blk_cpu_iopoll));
  21952. __raise_softirq_irqoff(IRQ_POLL_SOFTIRQ);
  21953. local_irq_restore(flags);
  21954. + preempt_check_resched_rt();
  21955. }
  21956. EXPORT_SYMBOL(irq_poll_sched);
  21957. @@ -71,6 +72,7 @@
  21958. local_irq_save(flags);
  21959. __irq_poll_complete(iop);
  21960. local_irq_restore(flags);
  21961. + preempt_check_resched_rt();
  21962. }
  21963. EXPORT_SYMBOL(irq_poll_complete);
  21964. @@ -95,6 +97,7 @@
  21965. }
  21966. local_irq_enable();
  21967. + preempt_check_resched_rt();
  21968. /* Even though interrupts have been re-enabled, this
  21969. * access is safe because interrupts can only add new
  21970. @@ -132,6 +135,7 @@
  21971. __raise_softirq_irqoff(IRQ_POLL_SOFTIRQ);
  21972. local_irq_enable();
  21973. + preempt_check_resched_rt();
  21974. }
  21975. /**
  21976. @@ -195,6 +199,7 @@
  21977. this_cpu_ptr(&blk_cpu_iopoll));
  21978. __raise_softirq_irqoff(IRQ_POLL_SOFTIRQ);
  21979. local_irq_enable();
  21980. + preempt_check_resched_rt();
  21981. return 0;
  21982. }
  21983. diff -Nur linux-4.9.28.orig/lib/Kconfig linux-4.9.28/lib/Kconfig
  21984. --- linux-4.9.28.orig/lib/Kconfig 2017-05-14 14:00:37.000000000 +0200
  21985. +++ linux-4.9.28/lib/Kconfig 2017-05-19 03:37:25.206177727 +0200
  21986. @@ -400,6 +400,7 @@
  21987. config CPUMASK_OFFSTACK
  21988. bool "Force CPU masks off stack" if DEBUG_PER_CPU_MAPS
  21989. + depends on !PREEMPT_RT_FULL
  21990. help
  21991. Use dynamic allocation for cpumask_var_t, instead of putting
  21992. them on the stack. This is a bit more expensive, but avoids
  21993. diff -Nur linux-4.9.28.orig/lib/locking-selftest.c linux-4.9.28/lib/locking-selftest.c
  21994. --- linux-4.9.28.orig/lib/locking-selftest.c 2017-05-14 14:00:37.000000000 +0200
  21995. +++ linux-4.9.28/lib/locking-selftest.c 2017-05-19 03:37:25.206177727 +0200
  21996. @@ -590,6 +590,8 @@
  21997. #include "locking-selftest-spin-hardirq.h"
  21998. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_spin)
  21999. +#ifndef CONFIG_PREEMPT_RT_FULL
  22000. +
  22001. #include "locking-selftest-rlock-hardirq.h"
  22002. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_rlock)
  22003. @@ -605,9 +607,12 @@
  22004. #include "locking-selftest-wlock-softirq.h"
  22005. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_wlock)
  22006. +#endif
  22007. +
  22008. #undef E1
  22009. #undef E2
  22010. +#ifndef CONFIG_PREEMPT_RT_FULL
  22011. /*
  22012. * Enabling hardirqs with a softirq-safe lock held:
  22013. */
  22014. @@ -640,6 +645,8 @@
  22015. #undef E1
  22016. #undef E2
  22017. +#endif
  22018. +
  22019. /*
  22020. * Enabling irqs with an irq-safe lock held:
  22021. */
  22022. @@ -663,6 +670,8 @@
  22023. #include "locking-selftest-spin-hardirq.h"
  22024. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_spin)
  22025. +#ifndef CONFIG_PREEMPT_RT_FULL
  22026. +
  22027. #include "locking-selftest-rlock-hardirq.h"
  22028. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_rlock)
  22029. @@ -678,6 +687,8 @@
  22030. #include "locking-selftest-wlock-softirq.h"
  22031. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock)
  22032. +#endif
  22033. +
  22034. #undef E1
  22035. #undef E2
  22036. @@ -709,6 +720,8 @@
  22037. #include "locking-selftest-spin-hardirq.h"
  22038. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_spin)
  22039. +#ifndef CONFIG_PREEMPT_RT_FULL
  22040. +
  22041. #include "locking-selftest-rlock-hardirq.h"
  22042. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_rlock)
  22043. @@ -724,6 +737,8 @@
  22044. #include "locking-selftest-wlock-softirq.h"
  22045. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock)
  22046. +#endif
  22047. +
  22048. #undef E1
  22049. #undef E2
  22050. #undef E3
  22051. @@ -757,6 +772,8 @@
  22052. #include "locking-selftest-spin-hardirq.h"
  22053. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_spin)
  22054. +#ifndef CONFIG_PREEMPT_RT_FULL
  22055. +
  22056. #include "locking-selftest-rlock-hardirq.h"
  22057. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_rlock)
  22058. @@ -772,10 +789,14 @@
  22059. #include "locking-selftest-wlock-softirq.h"
  22060. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_wlock)
  22061. +#endif
  22062. +
  22063. #undef E1
  22064. #undef E2
  22065. #undef E3
  22066. +#ifndef CONFIG_PREEMPT_RT_FULL
  22067. +
  22068. /*
  22069. * read-lock / write-lock irq inversion.
  22070. *
  22071. @@ -838,6 +859,10 @@
  22072. #undef E2
  22073. #undef E3
  22074. +#endif
  22075. +
  22076. +#ifndef CONFIG_PREEMPT_RT_FULL
  22077. +
  22078. /*
  22079. * read-lock / write-lock recursion that is actually safe.
  22080. */
  22081. @@ -876,6 +901,8 @@
  22082. #undef E2
  22083. #undef E3
  22084. +#endif
  22085. +
  22086. /*
  22087. * read-lock / write-lock recursion that is unsafe.
  22088. */
  22089. @@ -1858,6 +1885,7 @@
  22090. printk(" --------------------------------------------------------------------------\n");
  22091. +#ifndef CONFIG_PREEMPT_RT_FULL
  22092. /*
  22093. * irq-context testcases:
  22094. */
  22095. @@ -1870,6 +1898,28 @@
  22096. DO_TESTCASE_6x2("irq read-recursion", irq_read_recursion);
  22097. // DO_TESTCASE_6x2B("irq read-recursion #2", irq_read_recursion2);
  22098. +#else
  22099. + /* On -rt, we only do hardirq context test for raw spinlock */
  22100. + DO_TESTCASE_1B("hard-irqs-on + irq-safe-A", irqsafe1_hard_spin, 12);
  22101. + DO_TESTCASE_1B("hard-irqs-on + irq-safe-A", irqsafe1_hard_spin, 21);
  22102. +
  22103. + DO_TESTCASE_1B("hard-safe-A + irqs-on", irqsafe2B_hard_spin, 12);
  22104. + DO_TESTCASE_1B("hard-safe-A + irqs-on", irqsafe2B_hard_spin, 21);
  22105. +
  22106. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 123);
  22107. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 132);
  22108. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 213);
  22109. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 231);
  22110. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 312);
  22111. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 321);
  22112. +
  22113. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 123);
  22114. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 132);
  22115. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 213);
  22116. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 231);
  22117. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 312);
  22118. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 321);
  22119. +#endif
  22120. ww_tests();
  22121. diff -Nur linux-4.9.28.orig/lib/percpu_ida.c linux-4.9.28/lib/percpu_ida.c
  22122. --- linux-4.9.28.orig/lib/percpu_ida.c 2017-05-14 14:00:37.000000000 +0200
  22123. +++ linux-4.9.28/lib/percpu_ida.c 2017-05-19 03:37:25.206177727 +0200
  22124. @@ -26,6 +26,9 @@
  22125. #include <linux/string.h>
  22126. #include <linux/spinlock.h>
  22127. #include <linux/percpu_ida.h>
  22128. +#include <linux/locallock.h>
  22129. +
  22130. +static DEFINE_LOCAL_IRQ_LOCK(irq_off_lock);
  22131. struct percpu_ida_cpu {
  22132. /*
  22133. @@ -148,13 +151,13 @@
  22134. unsigned long flags;
  22135. int tag;
  22136. - local_irq_save(flags);
  22137. + local_lock_irqsave(irq_off_lock, flags);
  22138. tags = this_cpu_ptr(pool->tag_cpu);
  22139. /* Fastpath */
  22140. tag = alloc_local_tag(tags);
  22141. if (likely(tag >= 0)) {
  22142. - local_irq_restore(flags);
  22143. + local_unlock_irqrestore(irq_off_lock, flags);
  22144. return tag;
  22145. }
  22146. @@ -173,6 +176,7 @@
  22147. if (!tags->nr_free)
  22148. alloc_global_tags(pool, tags);
  22149. +
  22150. if (!tags->nr_free)
  22151. steal_tags(pool, tags);
  22152. @@ -184,7 +188,7 @@
  22153. }
  22154. spin_unlock(&pool->lock);
  22155. - local_irq_restore(flags);
  22156. + local_unlock_irqrestore(irq_off_lock, flags);
  22157. if (tag >= 0 || state == TASK_RUNNING)
  22158. break;
  22159. @@ -196,7 +200,7 @@
  22160. schedule();
  22161. - local_irq_save(flags);
  22162. + local_lock_irqsave(irq_off_lock, flags);
  22163. tags = this_cpu_ptr(pool->tag_cpu);
  22164. }
  22165. if (state != TASK_RUNNING)
  22166. @@ -221,7 +225,7 @@
  22167. BUG_ON(tag >= pool->nr_tags);
  22168. - local_irq_save(flags);
  22169. + local_lock_irqsave(irq_off_lock, flags);
  22170. tags = this_cpu_ptr(pool->tag_cpu);
  22171. spin_lock(&tags->lock);
  22172. @@ -253,7 +257,7 @@
  22173. spin_unlock(&pool->lock);
  22174. }
  22175. - local_irq_restore(flags);
  22176. + local_unlock_irqrestore(irq_off_lock, flags);
  22177. }
  22178. EXPORT_SYMBOL_GPL(percpu_ida_free);
  22179. @@ -345,7 +349,7 @@
  22180. struct percpu_ida_cpu *remote;
  22181. unsigned cpu, i, err = 0;
  22182. - local_irq_save(flags);
  22183. + local_lock_irqsave(irq_off_lock, flags);
  22184. for_each_possible_cpu(cpu) {
  22185. remote = per_cpu_ptr(pool->tag_cpu, cpu);
  22186. spin_lock(&remote->lock);
  22187. @@ -367,7 +371,7 @@
  22188. }
  22189. spin_unlock(&pool->lock);
  22190. out:
  22191. - local_irq_restore(flags);
  22192. + local_unlock_irqrestore(irq_off_lock, flags);
  22193. return err;
  22194. }
  22195. EXPORT_SYMBOL_GPL(percpu_ida_for_each_free);
  22196. diff -Nur linux-4.9.28.orig/lib/radix-tree.c linux-4.9.28/lib/radix-tree.c
  22197. --- linux-4.9.28.orig/lib/radix-tree.c 2017-05-14 14:00:37.000000000 +0200
  22198. +++ linux-4.9.28/lib/radix-tree.c 2017-05-19 03:37:25.206177727 +0200
  22199. @@ -36,7 +36,7 @@
  22200. #include <linux/bitops.h>
  22201. #include <linux/rcupdate.h>
  22202. #include <linux/preempt.h> /* in_interrupt() */
  22203. -
  22204. +#include <linux/locallock.h>
  22205. /* Number of nodes in fully populated tree of given height */
  22206. static unsigned long height_to_maxnodes[RADIX_TREE_MAX_PATH + 1] __read_mostly;
  22207. @@ -68,6 +68,7 @@
  22208. struct radix_tree_node *nodes;
  22209. };
  22210. static DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, };
  22211. +static DEFINE_LOCAL_IRQ_LOCK(radix_tree_preloads_lock);
  22212. static inline void *node_to_entry(void *ptr)
  22213. {
  22214. @@ -290,13 +291,14 @@
  22215. * succeed in getting a node here (and never reach
  22216. * kmem_cache_alloc)
  22217. */
  22218. - rtp = this_cpu_ptr(&radix_tree_preloads);
  22219. + rtp = &get_locked_var(radix_tree_preloads_lock, radix_tree_preloads);
  22220. if (rtp->nr) {
  22221. ret = rtp->nodes;
  22222. rtp->nodes = ret->private_data;
  22223. ret->private_data = NULL;
  22224. rtp->nr--;
  22225. }
  22226. + put_locked_var(radix_tree_preloads_lock, radix_tree_preloads);
  22227. /*
  22228. * Update the allocation stack trace as this is more useful
  22229. * for debugging.
  22230. @@ -357,14 +359,14 @@
  22231. */
  22232. gfp_mask &= ~__GFP_ACCOUNT;
  22233. - preempt_disable();
  22234. + local_lock(radix_tree_preloads_lock);
  22235. rtp = this_cpu_ptr(&radix_tree_preloads);
  22236. while (rtp->nr < nr) {
  22237. - preempt_enable();
  22238. + local_unlock(radix_tree_preloads_lock);
  22239. node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
  22240. if (node == NULL)
  22241. goto out;
  22242. - preempt_disable();
  22243. + local_lock(radix_tree_preloads_lock);
  22244. rtp = this_cpu_ptr(&radix_tree_preloads);
  22245. if (rtp->nr < nr) {
  22246. node->private_data = rtp->nodes;
  22247. @@ -406,7 +408,7 @@
  22248. if (gfpflags_allow_blocking(gfp_mask))
  22249. return __radix_tree_preload(gfp_mask, RADIX_TREE_PRELOAD_SIZE);
  22250. /* Preloading doesn't help anything with this gfp mask, skip it */
  22251. - preempt_disable();
  22252. + local_lock(radix_tree_preloads_lock);
  22253. return 0;
  22254. }
  22255. EXPORT_SYMBOL(radix_tree_maybe_preload);
  22256. @@ -422,7 +424,7 @@
  22257. /* Preloading doesn't help anything with this gfp mask, skip it */
  22258. if (!gfpflags_allow_blocking(gfp_mask)) {
  22259. - preempt_disable();
  22260. + local_lock(radix_tree_preloads_lock);
  22261. return 0;
  22262. }
  22263. @@ -456,6 +458,12 @@
  22264. return __radix_tree_preload(gfp_mask, nr_nodes);
  22265. }
  22266. +void radix_tree_preload_end(void)
  22267. +{
  22268. + local_unlock(radix_tree_preloads_lock);
  22269. +}
  22270. +EXPORT_SYMBOL(radix_tree_preload_end);
  22271. +
  22272. /*
  22273. * The maximum index which can be stored in a radix tree
  22274. */
  22275. diff -Nur linux-4.9.28.orig/lib/scatterlist.c linux-4.9.28/lib/scatterlist.c
  22276. --- linux-4.9.28.orig/lib/scatterlist.c 2017-05-14 14:00:37.000000000 +0200
  22277. +++ linux-4.9.28/lib/scatterlist.c 2017-05-19 03:37:25.206177727 +0200
  22278. @@ -620,7 +620,7 @@
  22279. flush_kernel_dcache_page(miter->page);
  22280. if (miter->__flags & SG_MITER_ATOMIC) {
  22281. - WARN_ON_ONCE(preemptible());
  22282. + WARN_ON_ONCE(!pagefault_disabled());
  22283. kunmap_atomic(miter->addr);
  22284. } else
  22285. kunmap(miter->page);
  22286. @@ -664,7 +664,7 @@
  22287. if (!sg_miter_skip(&miter, skip))
  22288. return false;
  22289. - local_irq_save(flags);
  22290. + local_irq_save_nort(flags);
  22291. while (sg_miter_next(&miter) && offset < buflen) {
  22292. unsigned int len;
  22293. @@ -681,7 +681,7 @@
  22294. sg_miter_stop(&miter);
  22295. - local_irq_restore(flags);
  22296. + local_irq_restore_nort(flags);
  22297. return offset;
  22298. }
  22299. EXPORT_SYMBOL(sg_copy_buffer);
  22300. diff -Nur linux-4.9.28.orig/lib/smp_processor_id.c linux-4.9.28/lib/smp_processor_id.c
  22301. --- linux-4.9.28.orig/lib/smp_processor_id.c 2017-05-14 14:00:37.000000000 +0200
  22302. +++ linux-4.9.28/lib/smp_processor_id.c 2017-05-19 03:37:25.206177727 +0200
  22303. @@ -39,8 +39,9 @@
  22304. if (!printk_ratelimit())
  22305. goto out_enable;
  22306. - printk(KERN_ERR "BUG: using %s%s() in preemptible [%08x] code: %s/%d\n",
  22307. - what1, what2, preempt_count() - 1, current->comm, current->pid);
  22308. + printk(KERN_ERR "BUG: using %s%s() in preemptible [%08x %08x] code: %s/%d\n",
  22309. + what1, what2, preempt_count() - 1, __migrate_disabled(current),
  22310. + current->comm, current->pid);
  22311. print_symbol("caller is %s\n", (long)__builtin_return_address(0));
  22312. dump_stack();
  22313. diff -Nur linux-4.9.28.orig/MAINTAINERS linux-4.9.28/MAINTAINERS
  22314. --- linux-4.9.28.orig/MAINTAINERS 2017-05-14 14:00:37.000000000 +0200
  22315. +++ linux-4.9.28/MAINTAINERS 2017-05-19 03:37:25.122174217 +0200
  22316. @@ -5196,6 +5196,23 @@
  22317. F: include/uapi/linux/fuse.h
  22318. F: Documentation/filesystems/fuse.txt
  22319. +FUTEX SUBSYSTEM
  22320. +M: Thomas Gleixner <tglx@linutronix.de>
  22321. +M: Ingo Molnar <mingo@redhat.com>
  22322. +R: Peter Zijlstra <peterz@infradead.org>
  22323. +R: Darren Hart <dvhart@infradead.org>
  22324. +L: linux-kernel@vger.kernel.org
  22325. +T: git git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git locking/core
  22326. +S: Maintained
  22327. +F: kernel/futex.c
  22328. +F: kernel/futex_compat.c
  22329. +F: include/asm-generic/futex.h
  22330. +F: include/linux/futex.h
  22331. +F: include/uapi/linux/futex.h
  22332. +F: tools/testing/selftests/futex/
  22333. +F: tools/perf/bench/futex*
  22334. +F: Documentation/*futex*
  22335. +
  22336. FUTURE DOMAIN TMC-16x0 SCSI DRIVER (16-bit)
  22337. M: Rik Faith <faith@cs.unc.edu>
  22338. L: linux-scsi@vger.kernel.org
  22339. diff -Nur linux-4.9.28.orig/mm/backing-dev.c linux-4.9.28/mm/backing-dev.c
  22340. --- linux-4.9.28.orig/mm/backing-dev.c 2017-05-14 14:00:37.000000000 +0200
  22341. +++ linux-4.9.28/mm/backing-dev.c 2017-05-19 03:37:25.206177727 +0200
  22342. @@ -457,9 +457,9 @@
  22343. {
  22344. unsigned long flags;
  22345. - local_irq_save(flags);
  22346. + local_irq_save_nort(flags);
  22347. if (!atomic_dec_and_lock(&congested->refcnt, &cgwb_lock)) {
  22348. - local_irq_restore(flags);
  22349. + local_irq_restore_nort(flags);
  22350. return;
  22351. }
  22352. diff -Nur linux-4.9.28.orig/mm/compaction.c linux-4.9.28/mm/compaction.c
  22353. --- linux-4.9.28.orig/mm/compaction.c 2017-05-14 14:00:37.000000000 +0200
  22354. +++ linux-4.9.28/mm/compaction.c 2017-05-19 03:37:25.206177727 +0200
  22355. @@ -1593,10 +1593,12 @@
  22356. block_start_pfn(cc->migrate_pfn, cc->order);
  22357. if (cc->last_migrated_pfn < current_block_start) {
  22358. - cpu = get_cpu();
  22359. + cpu = get_cpu_light();
  22360. + local_lock_irq(swapvec_lock);
  22361. lru_add_drain_cpu(cpu);
  22362. + local_unlock_irq(swapvec_lock);
  22363. drain_local_pages(zone);
  22364. - put_cpu();
  22365. + put_cpu_light();
  22366. /* No more flushing until we migrate again */
  22367. cc->last_migrated_pfn = 0;
  22368. }
  22369. diff -Nur linux-4.9.28.orig/mm/filemap.c linux-4.9.28/mm/filemap.c
  22370. --- linux-4.9.28.orig/mm/filemap.c 2017-05-14 14:00:37.000000000 +0200
  22371. +++ linux-4.9.28/mm/filemap.c 2017-05-19 03:37:25.210177882 +0200
  22372. @@ -159,9 +159,12 @@
  22373. * node->private_list is protected by
  22374. * mapping->tree_lock.
  22375. */
  22376. - if (!list_empty(&node->private_list))
  22377. - list_lru_del(&workingset_shadow_nodes,
  22378. + if (!list_empty(&node->private_list)) {
  22379. + local_lock(workingset_shadow_lock);
  22380. + list_lru_del(&__workingset_shadow_nodes,
  22381. &node->private_list);
  22382. + local_unlock(workingset_shadow_lock);
  22383. + }
  22384. }
  22385. return 0;
  22386. }
  22387. @@ -217,8 +220,10 @@
  22388. if (!dax_mapping(mapping) && !workingset_node_pages(node) &&
  22389. list_empty(&node->private_list)) {
  22390. node->private_data = mapping;
  22391. - list_lru_add(&workingset_shadow_nodes,
  22392. - &node->private_list);
  22393. + local_lock(workingset_shadow_lock);
  22394. + list_lru_add(&__workingset_shadow_nodes,
  22395. + &node->private_list);
  22396. + local_unlock(workingset_shadow_lock);
  22397. }
  22398. }
  22399. diff -Nur linux-4.9.28.orig/mm/highmem.c linux-4.9.28/mm/highmem.c
  22400. --- linux-4.9.28.orig/mm/highmem.c 2017-05-14 14:00:37.000000000 +0200
  22401. +++ linux-4.9.28/mm/highmem.c 2017-05-19 03:37:25.210177882 +0200
  22402. @@ -29,10 +29,11 @@
  22403. #include <linux/kgdb.h>
  22404. #include <asm/tlbflush.h>
  22405. -
  22406. +#ifndef CONFIG_PREEMPT_RT_FULL
  22407. #if defined(CONFIG_HIGHMEM) || defined(CONFIG_X86_32)
  22408. DEFINE_PER_CPU(int, __kmap_atomic_idx);
  22409. #endif
  22410. +#endif
  22411. /*
  22412. * Virtual_count is not a pure "count".
  22413. @@ -107,8 +108,9 @@
  22414. unsigned long totalhigh_pages __read_mostly;
  22415. EXPORT_SYMBOL(totalhigh_pages);
  22416. -
  22417. +#ifndef CONFIG_PREEMPT_RT_FULL
  22418. EXPORT_PER_CPU_SYMBOL(__kmap_atomic_idx);
  22419. +#endif
  22420. unsigned int nr_free_highpages (void)
  22421. {
  22422. diff -Nur linux-4.9.28.orig/mm/Kconfig linux-4.9.28/mm/Kconfig
  22423. --- linux-4.9.28.orig/mm/Kconfig 2017-05-14 14:00:37.000000000 +0200
  22424. +++ linux-4.9.28/mm/Kconfig 2017-05-19 03:37:25.206177727 +0200
  22425. @@ -410,7 +410,7 @@
  22426. config TRANSPARENT_HUGEPAGE
  22427. bool "Transparent Hugepage Support"
  22428. - depends on HAVE_ARCH_TRANSPARENT_HUGEPAGE
  22429. + depends on HAVE_ARCH_TRANSPARENT_HUGEPAGE && !PREEMPT_RT_FULL
  22430. select COMPACTION
  22431. select RADIX_TREE_MULTIORDER
  22432. help
  22433. diff -Nur linux-4.9.28.orig/mm/memcontrol.c linux-4.9.28/mm/memcontrol.c
  22434. --- linux-4.9.28.orig/mm/memcontrol.c 2017-05-14 14:00:37.000000000 +0200
  22435. +++ linux-4.9.28/mm/memcontrol.c 2017-05-19 03:37:25.210177882 +0200
  22436. @@ -67,6 +67,7 @@
  22437. #include <net/sock.h>
  22438. #include <net/ip.h>
  22439. #include "slab.h"
  22440. +#include <linux/locallock.h>
  22441. #include <asm/uaccess.h>
  22442. @@ -92,6 +93,8 @@
  22443. #define do_swap_account 0
  22444. #endif
  22445. +static DEFINE_LOCAL_IRQ_LOCK(event_lock);
  22446. +
  22447. /* Whether legacy memory+swap accounting is active */
  22448. static bool do_memsw_account(void)
  22449. {
  22450. @@ -1692,6 +1695,7 @@
  22451. #define FLUSHING_CACHED_CHARGE 0
  22452. };
  22453. static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
  22454. +static DEFINE_LOCAL_IRQ_LOCK(memcg_stock_ll);
  22455. static DEFINE_MUTEX(percpu_charge_mutex);
  22456. /**
  22457. @@ -1714,7 +1718,7 @@
  22458. if (nr_pages > CHARGE_BATCH)
  22459. return ret;
  22460. - local_irq_save(flags);
  22461. + local_lock_irqsave(memcg_stock_ll, flags);
  22462. stock = this_cpu_ptr(&memcg_stock);
  22463. if (memcg == stock->cached && stock->nr_pages >= nr_pages) {
  22464. @@ -1722,7 +1726,7 @@
  22465. ret = true;
  22466. }
  22467. - local_irq_restore(flags);
  22468. + local_unlock_irqrestore(memcg_stock_ll, flags);
  22469. return ret;
  22470. }
  22471. @@ -1749,13 +1753,13 @@
  22472. struct memcg_stock_pcp *stock;
  22473. unsigned long flags;
  22474. - local_irq_save(flags);
  22475. + local_lock_irqsave(memcg_stock_ll, flags);
  22476. stock = this_cpu_ptr(&memcg_stock);
  22477. drain_stock(stock);
  22478. clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
  22479. - local_irq_restore(flags);
  22480. + local_unlock_irqrestore(memcg_stock_ll, flags);
  22481. }
  22482. /*
  22483. @@ -1767,7 +1771,7 @@
  22484. struct memcg_stock_pcp *stock;
  22485. unsigned long flags;
  22486. - local_irq_save(flags);
  22487. + local_lock_irqsave(memcg_stock_ll, flags);
  22488. stock = this_cpu_ptr(&memcg_stock);
  22489. if (stock->cached != memcg) { /* reset if necessary */
  22490. @@ -1776,7 +1780,7 @@
  22491. }
  22492. stock->nr_pages += nr_pages;
  22493. - local_irq_restore(flags);
  22494. + local_unlock_irqrestore(memcg_stock_ll, flags);
  22495. }
  22496. /*
  22497. @@ -1792,7 +1796,7 @@
  22498. return;
  22499. /* Notify other cpus that system-wide "drain" is running */
  22500. get_online_cpus();
  22501. - curcpu = get_cpu();
  22502. + curcpu = get_cpu_light();
  22503. for_each_online_cpu(cpu) {
  22504. struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
  22505. struct mem_cgroup *memcg;
  22506. @@ -1809,7 +1813,7 @@
  22507. schedule_work_on(cpu, &stock->work);
  22508. }
  22509. }
  22510. - put_cpu();
  22511. + put_cpu_light();
  22512. put_online_cpus();
  22513. mutex_unlock(&percpu_charge_mutex);
  22514. }
  22515. @@ -4555,12 +4559,12 @@
  22516. ret = 0;
  22517. - local_irq_disable();
  22518. + local_lock_irq(event_lock);
  22519. mem_cgroup_charge_statistics(to, page, compound, nr_pages);
  22520. memcg_check_events(to, page);
  22521. mem_cgroup_charge_statistics(from, page, compound, -nr_pages);
  22522. memcg_check_events(from, page);
  22523. - local_irq_enable();
  22524. + local_unlock_irq(event_lock);
  22525. out_unlock:
  22526. unlock_page(page);
  22527. out:
  22528. @@ -5435,10 +5439,10 @@
  22529. commit_charge(page, memcg, lrucare);
  22530. - local_irq_disable();
  22531. + local_lock_irq(event_lock);
  22532. mem_cgroup_charge_statistics(memcg, page, compound, nr_pages);
  22533. memcg_check_events(memcg, page);
  22534. - local_irq_enable();
  22535. + local_unlock_irq(event_lock);
  22536. if (do_memsw_account() && PageSwapCache(page)) {
  22537. swp_entry_t entry = { .val = page_private(page) };
  22538. @@ -5494,14 +5498,14 @@
  22539. memcg_oom_recover(memcg);
  22540. }
  22541. - local_irq_save(flags);
  22542. + local_lock_irqsave(event_lock, flags);
  22543. __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS], nr_anon);
  22544. __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_CACHE], nr_file);
  22545. __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE], nr_huge);
  22546. __this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT], pgpgout);
  22547. __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
  22548. memcg_check_events(memcg, dummy_page);
  22549. - local_irq_restore(flags);
  22550. + local_unlock_irqrestore(event_lock, flags);
  22551. if (!mem_cgroup_is_root(memcg))
  22552. css_put_many(&memcg->css, nr_pages);
  22553. @@ -5656,10 +5660,10 @@
  22554. commit_charge(newpage, memcg, false);
  22555. - local_irq_save(flags);
  22556. + local_lock_irqsave(event_lock, flags);
  22557. mem_cgroup_charge_statistics(memcg, newpage, compound, nr_pages);
  22558. memcg_check_events(memcg, newpage);
  22559. - local_irq_restore(flags);
  22560. + local_unlock_irqrestore(event_lock, flags);
  22561. }
  22562. DEFINE_STATIC_KEY_FALSE(memcg_sockets_enabled_key);
  22563. @@ -5850,6 +5854,7 @@
  22564. {
  22565. struct mem_cgroup *memcg, *swap_memcg;
  22566. unsigned short oldid;
  22567. + unsigned long flags;
  22568. VM_BUG_ON_PAGE(PageLRU(page), page);
  22569. VM_BUG_ON_PAGE(page_count(page), page);
  22570. @@ -5890,12 +5895,16 @@
  22571. * important here to have the interrupts disabled because it is the
  22572. * only synchronisation we have for udpating the per-CPU variables.
  22573. */
  22574. + local_lock_irqsave(event_lock, flags);
  22575. +#ifndef CONFIG_PREEMPT_RT_BASE
  22576. VM_BUG_ON(!irqs_disabled());
  22577. +#endif
  22578. mem_cgroup_charge_statistics(memcg, page, false, -1);
  22579. memcg_check_events(memcg, page);
  22580. if (!mem_cgroup_is_root(memcg))
  22581. css_put(&memcg->css);
  22582. + local_unlock_irqrestore(event_lock, flags);
  22583. }
  22584. /*
  22585. diff -Nur linux-4.9.28.orig/mm/mmu_context.c linux-4.9.28/mm/mmu_context.c
  22586. --- linux-4.9.28.orig/mm/mmu_context.c 2017-05-14 14:00:37.000000000 +0200
  22587. +++ linux-4.9.28/mm/mmu_context.c 2017-05-19 03:37:25.210177882 +0200
  22588. @@ -23,6 +23,7 @@
  22589. struct task_struct *tsk = current;
  22590. task_lock(tsk);
  22591. + preempt_disable_rt();
  22592. active_mm = tsk->active_mm;
  22593. if (active_mm != mm) {
  22594. atomic_inc(&mm->mm_count);
  22595. @@ -30,6 +31,7 @@
  22596. }
  22597. tsk->mm = mm;
  22598. switch_mm(active_mm, mm, tsk);
  22599. + preempt_enable_rt();
  22600. task_unlock(tsk);
  22601. #ifdef finish_arch_post_lock_switch
  22602. finish_arch_post_lock_switch();
  22603. diff -Nur linux-4.9.28.orig/mm/page_alloc.c linux-4.9.28/mm/page_alloc.c
  22604. --- linux-4.9.28.orig/mm/page_alloc.c 2017-05-14 14:00:37.000000000 +0200
  22605. +++ linux-4.9.28/mm/page_alloc.c 2017-05-19 03:37:25.210177882 +0200
  22606. @@ -61,6 +61,7 @@
  22607. #include <linux/page_ext.h>
  22608. #include <linux/hugetlb.h>
  22609. #include <linux/sched/rt.h>
  22610. +#include <linux/locallock.h>
  22611. #include <linux/page_owner.h>
  22612. #include <linux/kthread.h>
  22613. #include <linux/memcontrol.h>
  22614. @@ -281,6 +282,18 @@
  22615. EXPORT_SYMBOL(nr_online_nodes);
  22616. #endif
  22617. +static DEFINE_LOCAL_IRQ_LOCK(pa_lock);
  22618. +
  22619. +#ifdef CONFIG_PREEMPT_RT_BASE
  22620. +# define cpu_lock_irqsave(cpu, flags) \
  22621. + local_lock_irqsave_on(pa_lock, flags, cpu)
  22622. +# define cpu_unlock_irqrestore(cpu, flags) \
  22623. + local_unlock_irqrestore_on(pa_lock, flags, cpu)
  22624. +#else
  22625. +# define cpu_lock_irqsave(cpu, flags) local_irq_save(flags)
  22626. +# define cpu_unlock_irqrestore(cpu, flags) local_irq_restore(flags)
  22627. +#endif
  22628. +
  22629. int page_group_by_mobility_disabled __read_mostly;
  22630. #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
  22631. @@ -1072,7 +1085,7 @@
  22632. #endif /* CONFIG_DEBUG_VM */
  22633. /*
  22634. - * Frees a number of pages from the PCP lists
  22635. + * Frees a number of pages which have been collected from the pcp lists.
  22636. * Assumes all pages on list are in same zone, and of same order.
  22637. * count is the number of pages to free.
  22638. *
  22639. @@ -1083,19 +1096,58 @@
  22640. * pinned" detection logic.
  22641. */
  22642. static void free_pcppages_bulk(struct zone *zone, int count,
  22643. - struct per_cpu_pages *pcp)
  22644. + struct list_head *list)
  22645. {
  22646. - int migratetype = 0;
  22647. - int batch_free = 0;
  22648. unsigned long nr_scanned;
  22649. bool isolated_pageblocks;
  22650. + unsigned long flags;
  22651. +
  22652. + spin_lock_irqsave(&zone->lock, flags);
  22653. - spin_lock(&zone->lock);
  22654. isolated_pageblocks = has_isolate_pageblock(zone);
  22655. nr_scanned = node_page_state(zone->zone_pgdat, NR_PAGES_SCANNED);
  22656. if (nr_scanned)
  22657. __mod_node_page_state(zone->zone_pgdat, NR_PAGES_SCANNED, -nr_scanned);
  22658. + while (!list_empty(list)) {
  22659. + struct page *page;
  22660. + int mt; /* migratetype of the to-be-freed page */
  22661. +
  22662. + page = list_first_entry(list, struct page, lru);
  22663. + /* must delete as __free_one_page list manipulates */
  22664. + list_del(&page->lru);
  22665. +
  22666. + mt = get_pcppage_migratetype(page);
  22667. + /* MIGRATE_ISOLATE page should not go to pcplists */
  22668. + VM_BUG_ON_PAGE(is_migrate_isolate(mt), page);
  22669. + /* Pageblock could have been isolated meanwhile */
  22670. + if (unlikely(isolated_pageblocks))
  22671. + mt = get_pageblock_migratetype(page);
  22672. +
  22673. + if (bulkfree_pcp_prepare(page))
  22674. + continue;
  22675. +
  22676. + __free_one_page(page, page_to_pfn(page), zone, 0, mt);
  22677. + trace_mm_page_pcpu_drain(page, 0, mt);
  22678. + count--;
  22679. + }
  22680. + WARN_ON(count != 0);
  22681. + spin_unlock_irqrestore(&zone->lock, flags);
  22682. +}
  22683. +
  22684. +/*
  22685. + * Moves a number of pages from the PCP lists to free list which
  22686. + * is freed outside of the locked region.
  22687. + *
  22688. + * Assumes all pages on list are in same zone, and of same order.
  22689. + * count is the number of pages to free.
  22690. + */
  22691. +static void isolate_pcp_pages(int count, struct per_cpu_pages *src,
  22692. + struct list_head *dst)
  22693. +{
  22694. + int migratetype = 0;
  22695. + int batch_free = 0;
  22696. +
  22697. while (count) {
  22698. struct page *page;
  22699. struct list_head *list;
  22700. @@ -1111,7 +1163,7 @@
  22701. batch_free++;
  22702. if (++migratetype == MIGRATE_PCPTYPES)
  22703. migratetype = 0;
  22704. - list = &pcp->lists[migratetype];
  22705. + list = &src->lists[migratetype];
  22706. } while (list_empty(list));
  22707. /* This is the only non-empty list. Free them all. */
  22708. @@ -1119,27 +1171,12 @@
  22709. batch_free = count;
  22710. do {
  22711. - int mt; /* migratetype of the to-be-freed page */
  22712. -
  22713. page = list_last_entry(list, struct page, lru);
  22714. - /* must delete as __free_one_page list manipulates */
  22715. list_del(&page->lru);
  22716. - mt = get_pcppage_migratetype(page);
  22717. - /* MIGRATE_ISOLATE page should not go to pcplists */
  22718. - VM_BUG_ON_PAGE(is_migrate_isolate(mt), page);
  22719. - /* Pageblock could have been isolated meanwhile */
  22720. - if (unlikely(isolated_pageblocks))
  22721. - mt = get_pageblock_migratetype(page);
  22722. -
  22723. - if (bulkfree_pcp_prepare(page))
  22724. - continue;
  22725. -
  22726. - __free_one_page(page, page_to_pfn(page), zone, 0, mt);
  22727. - trace_mm_page_pcpu_drain(page, 0, mt);
  22728. + list_add(&page->lru, dst);
  22729. } while (--count && --batch_free && !list_empty(list));
  22730. }
  22731. - spin_unlock(&zone->lock);
  22732. }
  22733. static void free_one_page(struct zone *zone,
  22734. @@ -1148,7 +1185,9 @@
  22735. int migratetype)
  22736. {
  22737. unsigned long nr_scanned;
  22738. - spin_lock(&zone->lock);
  22739. + unsigned long flags;
  22740. +
  22741. + spin_lock_irqsave(&zone->lock, flags);
  22742. nr_scanned = node_page_state(zone->zone_pgdat, NR_PAGES_SCANNED);
  22743. if (nr_scanned)
  22744. __mod_node_page_state(zone->zone_pgdat, NR_PAGES_SCANNED, -nr_scanned);
  22745. @@ -1158,7 +1197,7 @@
  22746. migratetype = get_pfnblock_migratetype(page, pfn);
  22747. }
  22748. __free_one_page(page, pfn, zone, order, migratetype);
  22749. - spin_unlock(&zone->lock);
  22750. + spin_unlock_irqrestore(&zone->lock, flags);
  22751. }
  22752. static void __meminit __init_single_page(struct page *page, unsigned long pfn,
  22753. @@ -1244,10 +1283,10 @@
  22754. return;
  22755. migratetype = get_pfnblock_migratetype(page, pfn);
  22756. - local_irq_save(flags);
  22757. + local_lock_irqsave(pa_lock, flags);
  22758. __count_vm_events(PGFREE, 1 << order);
  22759. free_one_page(page_zone(page), page, pfn, order, migratetype);
  22760. - local_irq_restore(flags);
  22761. + local_unlock_irqrestore(pa_lock, flags);
  22762. }
  22763. static void __init __free_pages_boot_core(struct page *page, unsigned int order)
  22764. @@ -2246,16 +2285,18 @@
  22765. void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp)
  22766. {
  22767. unsigned long flags;
  22768. + LIST_HEAD(dst);
  22769. int to_drain, batch;
  22770. - local_irq_save(flags);
  22771. + local_lock_irqsave(pa_lock, flags);
  22772. batch = READ_ONCE(pcp->batch);
  22773. to_drain = min(pcp->count, batch);
  22774. if (to_drain > 0) {
  22775. - free_pcppages_bulk(zone, to_drain, pcp);
  22776. + isolate_pcp_pages(to_drain, pcp, &dst);
  22777. pcp->count -= to_drain;
  22778. }
  22779. - local_irq_restore(flags);
  22780. + local_unlock_irqrestore(pa_lock, flags);
  22781. + free_pcppages_bulk(zone, to_drain, &dst);
  22782. }
  22783. #endif
  22784. @@ -2271,16 +2312,21 @@
  22785. unsigned long flags;
  22786. struct per_cpu_pageset *pset;
  22787. struct per_cpu_pages *pcp;
  22788. + LIST_HEAD(dst);
  22789. + int count;
  22790. - local_irq_save(flags);
  22791. + cpu_lock_irqsave(cpu, flags);
  22792. pset = per_cpu_ptr(zone->pageset, cpu);
  22793. pcp = &pset->pcp;
  22794. - if (pcp->count) {
  22795. - free_pcppages_bulk(zone, pcp->count, pcp);
  22796. + count = pcp->count;
  22797. + if (count) {
  22798. + isolate_pcp_pages(count, pcp, &dst);
  22799. pcp->count = 0;
  22800. }
  22801. - local_irq_restore(flags);
  22802. + cpu_unlock_irqrestore(cpu, flags);
  22803. + if (count)
  22804. + free_pcppages_bulk(zone, count, &dst);
  22805. }
  22806. /*
  22807. @@ -2366,8 +2412,17 @@
  22808. else
  22809. cpumask_clear_cpu(cpu, &cpus_with_pcps);
  22810. }
  22811. +#ifndef CONFIG_PREEMPT_RT_BASE
  22812. on_each_cpu_mask(&cpus_with_pcps, (smp_call_func_t) drain_local_pages,
  22813. zone, 1);
  22814. +#else
  22815. + for_each_cpu(cpu, &cpus_with_pcps) {
  22816. + if (zone)
  22817. + drain_pages_zone(cpu, zone);
  22818. + else
  22819. + drain_pages(cpu);
  22820. + }
  22821. +#endif
  22822. }
  22823. #ifdef CONFIG_HIBERNATION
  22824. @@ -2427,7 +2482,7 @@
  22825. migratetype = get_pfnblock_migratetype(page, pfn);
  22826. set_pcppage_migratetype(page, migratetype);
  22827. - local_irq_save(flags);
  22828. + local_lock_irqsave(pa_lock, flags);
  22829. __count_vm_event(PGFREE);
  22830. /*
  22831. @@ -2453,12 +2508,17 @@
  22832. pcp->count++;
  22833. if (pcp->count >= pcp->high) {
  22834. unsigned long batch = READ_ONCE(pcp->batch);
  22835. - free_pcppages_bulk(zone, batch, pcp);
  22836. + LIST_HEAD(dst);
  22837. +
  22838. + isolate_pcp_pages(batch, pcp, &dst);
  22839. pcp->count -= batch;
  22840. + local_unlock_irqrestore(pa_lock, flags);
  22841. + free_pcppages_bulk(zone, batch, &dst);
  22842. + return;
  22843. }
  22844. out:
  22845. - local_irq_restore(flags);
  22846. + local_unlock_irqrestore(pa_lock, flags);
  22847. }
  22848. /*
  22849. @@ -2600,7 +2660,7 @@
  22850. struct per_cpu_pages *pcp;
  22851. struct list_head *list;
  22852. - local_irq_save(flags);
  22853. + local_lock_irqsave(pa_lock, flags);
  22854. do {
  22855. pcp = &this_cpu_ptr(zone->pageset)->pcp;
  22856. list = &pcp->lists[migratetype];
  22857. @@ -2627,7 +2687,7 @@
  22858. * allocate greater than order-1 page units with __GFP_NOFAIL.
  22859. */
  22860. WARN_ON_ONCE((gfp_flags & __GFP_NOFAIL) && (order > 1));
  22861. - spin_lock_irqsave(&zone->lock, flags);
  22862. + local_spin_lock_irqsave(pa_lock, &zone->lock, flags);
  22863. do {
  22864. page = NULL;
  22865. @@ -2639,22 +2699,24 @@
  22866. if (!page)
  22867. page = __rmqueue(zone, order, migratetype);
  22868. } while (page && check_new_pages(page, order));
  22869. - spin_unlock(&zone->lock);
  22870. - if (!page)
  22871. + if (!page) {
  22872. + spin_unlock(&zone->lock);
  22873. goto failed;
  22874. + }
  22875. __mod_zone_freepage_state(zone, -(1 << order),
  22876. get_pcppage_migratetype(page));
  22877. + spin_unlock(&zone->lock);
  22878. }
  22879. __count_zid_vm_events(PGALLOC, page_zonenum(page), 1 << order);
  22880. zone_statistics(preferred_zone, zone, gfp_flags);
  22881. - local_irq_restore(flags);
  22882. + local_unlock_irqrestore(pa_lock, flags);
  22883. VM_BUG_ON_PAGE(bad_range(zone, page), page);
  22884. return page;
  22885. failed:
  22886. - local_irq_restore(flags);
  22887. + local_unlock_irqrestore(pa_lock, flags);
  22888. return NULL;
  22889. }
  22890. @@ -6531,7 +6593,9 @@
  22891. int cpu = (unsigned long)hcpu;
  22892. if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
  22893. + local_lock_irq_on(swapvec_lock, cpu);
  22894. lru_add_drain_cpu(cpu);
  22895. + local_unlock_irq_on(swapvec_lock, cpu);
  22896. drain_pages(cpu);
  22897. /*
  22898. @@ -6557,6 +6621,7 @@
  22899. void __init page_alloc_init(void)
  22900. {
  22901. hotcpu_notifier(page_alloc_cpu_notify, 0);
  22902. + local_irq_lock_init(pa_lock);
  22903. }
  22904. /*
  22905. @@ -7385,7 +7450,7 @@
  22906. struct per_cpu_pageset *pset;
  22907. /* avoid races with drain_pages() */
  22908. - local_irq_save(flags);
  22909. + local_lock_irqsave(pa_lock, flags);
  22910. if (zone->pageset != &boot_pageset) {
  22911. for_each_online_cpu(cpu) {
  22912. pset = per_cpu_ptr(zone->pageset, cpu);
  22913. @@ -7394,7 +7459,7 @@
  22914. free_percpu(zone->pageset);
  22915. zone->pageset = &boot_pageset;
  22916. }
  22917. - local_irq_restore(flags);
  22918. + local_unlock_irqrestore(pa_lock, flags);
  22919. }
  22920. #ifdef CONFIG_MEMORY_HOTREMOVE
  22921. diff -Nur linux-4.9.28.orig/mm/percpu.c linux-4.9.28/mm/percpu.c
  22922. --- linux-4.9.28.orig/mm/percpu.c 2017-05-14 14:00:37.000000000 +0200
  22923. +++ linux-4.9.28/mm/percpu.c 2017-05-19 03:37:25.210177882 +0200
  22924. @@ -1283,18 +1283,7 @@
  22925. }
  22926. EXPORT_SYMBOL_GPL(free_percpu);
  22927. -/**
  22928. - * is_kernel_percpu_address - test whether address is from static percpu area
  22929. - * @addr: address to test
  22930. - *
  22931. - * Test whether @addr belongs to in-kernel static percpu area. Module
  22932. - * static percpu areas are not considered. For those, use
  22933. - * is_module_percpu_address().
  22934. - *
  22935. - * RETURNS:
  22936. - * %true if @addr is from in-kernel static percpu area, %false otherwise.
  22937. - */
  22938. -bool is_kernel_percpu_address(unsigned long addr)
  22939. +bool __is_kernel_percpu_address(unsigned long addr, unsigned long *can_addr)
  22940. {
  22941. #ifdef CONFIG_SMP
  22942. const size_t static_size = __per_cpu_end - __per_cpu_start;
  22943. @@ -1303,16 +1292,39 @@
  22944. for_each_possible_cpu(cpu) {
  22945. void *start = per_cpu_ptr(base, cpu);
  22946. + void *va = (void *)addr;
  22947. - if ((void *)addr >= start && (void *)addr < start + static_size)
  22948. + if (va >= start && va < start + static_size) {
  22949. + if (can_addr) {
  22950. + *can_addr = (unsigned long) (va - start);
  22951. + *can_addr += (unsigned long)
  22952. + per_cpu_ptr(base, get_boot_cpu_id());
  22953. + }
  22954. return true;
  22955. - }
  22956. + }
  22957. + }
  22958. #endif
  22959. /* on UP, can't distinguish from other static vars, always false */
  22960. return false;
  22961. }
  22962. /**
  22963. + * is_kernel_percpu_address - test whether address is from static percpu area
  22964. + * @addr: address to test
  22965. + *
  22966. + * Test whether @addr belongs to in-kernel static percpu area. Module
  22967. + * static percpu areas are not considered. For those, use
  22968. + * is_module_percpu_address().
  22969. + *
  22970. + * RETURNS:
  22971. + * %true if @addr is from in-kernel static percpu area, %false otherwise.
  22972. + */
  22973. +bool is_kernel_percpu_address(unsigned long addr)
  22974. +{
  22975. + return __is_kernel_percpu_address(addr, NULL);
  22976. +}
  22977. +
  22978. +/**
  22979. * per_cpu_ptr_to_phys - convert translated percpu address to physical address
  22980. * @addr: the address to be converted to physical address
  22981. *
  22982. diff -Nur linux-4.9.28.orig/mm/slab.h linux-4.9.28/mm/slab.h
  22983. --- linux-4.9.28.orig/mm/slab.h 2017-05-14 14:00:37.000000000 +0200
  22984. +++ linux-4.9.28/mm/slab.h 2017-05-19 03:37:25.210177882 +0200
  22985. @@ -426,7 +426,11 @@
  22986. * The slab lists for all objects.
  22987. */
  22988. struct kmem_cache_node {
  22989. +#ifdef CONFIG_SLUB
  22990. + raw_spinlock_t list_lock;
  22991. +#else
  22992. spinlock_t list_lock;
  22993. +#endif
  22994. #ifdef CONFIG_SLAB
  22995. struct list_head slabs_partial; /* partial list first, better asm code */
  22996. diff -Nur linux-4.9.28.orig/mm/slub.c linux-4.9.28/mm/slub.c
  22997. --- linux-4.9.28.orig/mm/slub.c 2017-05-14 14:00:37.000000000 +0200
  22998. +++ linux-4.9.28/mm/slub.c 2017-05-19 03:37:25.210177882 +0200
  22999. @@ -1141,7 +1141,7 @@
  23000. unsigned long uninitialized_var(flags);
  23001. int ret = 0;
  23002. - spin_lock_irqsave(&n->list_lock, flags);
  23003. + raw_spin_lock_irqsave(&n->list_lock, flags);
  23004. slab_lock(page);
  23005. if (s->flags & SLAB_CONSISTENCY_CHECKS) {
  23006. @@ -1176,7 +1176,7 @@
  23007. bulk_cnt, cnt);
  23008. slab_unlock(page);
  23009. - spin_unlock_irqrestore(&n->list_lock, flags);
  23010. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  23011. if (!ret)
  23012. slab_fix(s, "Object at 0x%p not freed", object);
  23013. return ret;
  23014. @@ -1304,6 +1304,12 @@
  23015. #endif /* CONFIG_SLUB_DEBUG */
  23016. +struct slub_free_list {
  23017. + raw_spinlock_t lock;
  23018. + struct list_head list;
  23019. +};
  23020. +static DEFINE_PER_CPU(struct slub_free_list, slub_free_list);
  23021. +
  23022. /*
  23023. * Hooks for other subsystems that check memory allocations. In a typical
  23024. * production configuration these hooks all should produce no code at all.
  23025. @@ -1527,10 +1533,17 @@
  23026. void *start, *p;
  23027. int idx, order;
  23028. bool shuffle;
  23029. + bool enableirqs = false;
  23030. flags &= gfp_allowed_mask;
  23031. if (gfpflags_allow_blocking(flags))
  23032. + enableirqs = true;
  23033. +#ifdef CONFIG_PREEMPT_RT_FULL
  23034. + if (system_state == SYSTEM_RUNNING)
  23035. + enableirqs = true;
  23036. +#endif
  23037. + if (enableirqs)
  23038. local_irq_enable();
  23039. flags |= s->allocflags;
  23040. @@ -1605,7 +1618,7 @@
  23041. page->frozen = 1;
  23042. out:
  23043. - if (gfpflags_allow_blocking(flags))
  23044. + if (enableirqs)
  23045. local_irq_disable();
  23046. if (!page)
  23047. return NULL;
  23048. @@ -1664,6 +1677,16 @@
  23049. __free_pages(page, order);
  23050. }
  23051. +static void free_delayed(struct list_head *h)
  23052. +{
  23053. + while(!list_empty(h)) {
  23054. + struct page *page = list_first_entry(h, struct page, lru);
  23055. +
  23056. + list_del(&page->lru);
  23057. + __free_slab(page->slab_cache, page);
  23058. + }
  23059. +}
  23060. +
  23061. #define need_reserve_slab_rcu \
  23062. (sizeof(((struct page *)NULL)->lru) < sizeof(struct rcu_head))
  23063. @@ -1695,6 +1718,12 @@
  23064. }
  23065. call_rcu(head, rcu_free_slab);
  23066. + } else if (irqs_disabled()) {
  23067. + struct slub_free_list *f = this_cpu_ptr(&slub_free_list);
  23068. +
  23069. + raw_spin_lock(&f->lock);
  23070. + list_add(&page->lru, &f->list);
  23071. + raw_spin_unlock(&f->lock);
  23072. } else
  23073. __free_slab(s, page);
  23074. }
  23075. @@ -1802,7 +1831,7 @@
  23076. if (!n || !n->nr_partial)
  23077. return NULL;
  23078. - spin_lock(&n->list_lock);
  23079. + raw_spin_lock(&n->list_lock);
  23080. list_for_each_entry_safe(page, page2, &n->partial, lru) {
  23081. void *t;
  23082. @@ -1827,7 +1856,7 @@
  23083. break;
  23084. }
  23085. - spin_unlock(&n->list_lock);
  23086. + raw_spin_unlock(&n->list_lock);
  23087. return object;
  23088. }
  23089. @@ -2073,7 +2102,7 @@
  23090. * that acquire_slab() will see a slab page that
  23091. * is frozen
  23092. */
  23093. - spin_lock(&n->list_lock);
  23094. + raw_spin_lock(&n->list_lock);
  23095. }
  23096. } else {
  23097. m = M_FULL;
  23098. @@ -2084,7 +2113,7 @@
  23099. * slabs from diagnostic functions will not see
  23100. * any frozen slabs.
  23101. */
  23102. - spin_lock(&n->list_lock);
  23103. + raw_spin_lock(&n->list_lock);
  23104. }
  23105. }
  23106. @@ -2119,7 +2148,7 @@
  23107. goto redo;
  23108. if (lock)
  23109. - spin_unlock(&n->list_lock);
  23110. + raw_spin_unlock(&n->list_lock);
  23111. if (m == M_FREE) {
  23112. stat(s, DEACTIVATE_EMPTY);
  23113. @@ -2151,10 +2180,10 @@
  23114. n2 = get_node(s, page_to_nid(page));
  23115. if (n != n2) {
  23116. if (n)
  23117. - spin_unlock(&n->list_lock);
  23118. + raw_spin_unlock(&n->list_lock);
  23119. n = n2;
  23120. - spin_lock(&n->list_lock);
  23121. + raw_spin_lock(&n->list_lock);
  23122. }
  23123. do {
  23124. @@ -2183,7 +2212,7 @@
  23125. }
  23126. if (n)
  23127. - spin_unlock(&n->list_lock);
  23128. + raw_spin_unlock(&n->list_lock);
  23129. while (discard_page) {
  23130. page = discard_page;
  23131. @@ -2222,14 +2251,21 @@
  23132. pobjects = oldpage->pobjects;
  23133. pages = oldpage->pages;
  23134. if (drain && pobjects > s->cpu_partial) {
  23135. + struct slub_free_list *f;
  23136. unsigned long flags;
  23137. + LIST_HEAD(tofree);
  23138. /*
  23139. * partial array is full. Move the existing
  23140. * set to the per node partial list.
  23141. */
  23142. local_irq_save(flags);
  23143. unfreeze_partials(s, this_cpu_ptr(s->cpu_slab));
  23144. + f = this_cpu_ptr(&slub_free_list);
  23145. + raw_spin_lock(&f->lock);
  23146. + list_splice_init(&f->list, &tofree);
  23147. + raw_spin_unlock(&f->lock);
  23148. local_irq_restore(flags);
  23149. + free_delayed(&tofree);
  23150. oldpage = NULL;
  23151. pobjects = 0;
  23152. pages = 0;
  23153. @@ -2301,7 +2337,22 @@
  23154. static void flush_all(struct kmem_cache *s)
  23155. {
  23156. + LIST_HEAD(tofree);
  23157. + int cpu;
  23158. +
  23159. on_each_cpu_cond(has_cpu_slab, flush_cpu_slab, s, 1, GFP_ATOMIC);
  23160. + for_each_online_cpu(cpu) {
  23161. + struct slub_free_list *f;
  23162. +
  23163. + if (!has_cpu_slab(cpu, s))
  23164. + continue;
  23165. +
  23166. + f = &per_cpu(slub_free_list, cpu);
  23167. + raw_spin_lock_irq(&f->lock);
  23168. + list_splice_init(&f->list, &tofree);
  23169. + raw_spin_unlock_irq(&f->lock);
  23170. + free_delayed(&tofree);
  23171. + }
  23172. }
  23173. /*
  23174. @@ -2356,10 +2407,10 @@
  23175. unsigned long x = 0;
  23176. struct page *page;
  23177. - spin_lock_irqsave(&n->list_lock, flags);
  23178. + raw_spin_lock_irqsave(&n->list_lock, flags);
  23179. list_for_each_entry(page, &n->partial, lru)
  23180. x += get_count(page);
  23181. - spin_unlock_irqrestore(&n->list_lock, flags);
  23182. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  23183. return x;
  23184. }
  23185. #endif /* CONFIG_SLUB_DEBUG || CONFIG_SYSFS */
  23186. @@ -2497,8 +2548,10 @@
  23187. * already disabled (which is the case for bulk allocation).
  23188. */
  23189. static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
  23190. - unsigned long addr, struct kmem_cache_cpu *c)
  23191. + unsigned long addr, struct kmem_cache_cpu *c,
  23192. + struct list_head *to_free)
  23193. {
  23194. + struct slub_free_list *f;
  23195. void *freelist;
  23196. struct page *page;
  23197. @@ -2558,6 +2611,13 @@
  23198. VM_BUG_ON(!c->page->frozen);
  23199. c->freelist = get_freepointer(s, freelist);
  23200. c->tid = next_tid(c->tid);
  23201. +
  23202. +out:
  23203. + f = this_cpu_ptr(&slub_free_list);
  23204. + raw_spin_lock(&f->lock);
  23205. + list_splice_init(&f->list, to_free);
  23206. + raw_spin_unlock(&f->lock);
  23207. +
  23208. return freelist;
  23209. new_slab:
  23210. @@ -2589,7 +2649,7 @@
  23211. deactivate_slab(s, page, get_freepointer(s, freelist));
  23212. c->page = NULL;
  23213. c->freelist = NULL;
  23214. - return freelist;
  23215. + goto out;
  23216. }
  23217. /*
  23218. @@ -2601,6 +2661,7 @@
  23219. {
  23220. void *p;
  23221. unsigned long flags;
  23222. + LIST_HEAD(tofree);
  23223. local_irq_save(flags);
  23224. #ifdef CONFIG_PREEMPT
  23225. @@ -2612,8 +2673,9 @@
  23226. c = this_cpu_ptr(s->cpu_slab);
  23227. #endif
  23228. - p = ___slab_alloc(s, gfpflags, node, addr, c);
  23229. + p = ___slab_alloc(s, gfpflags, node, addr, c, &tofree);
  23230. local_irq_restore(flags);
  23231. + free_delayed(&tofree);
  23232. return p;
  23233. }
  23234. @@ -2799,7 +2861,7 @@
  23235. do {
  23236. if (unlikely(n)) {
  23237. - spin_unlock_irqrestore(&n->list_lock, flags);
  23238. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  23239. n = NULL;
  23240. }
  23241. prior = page->freelist;
  23242. @@ -2831,7 +2893,7 @@
  23243. * Otherwise the list_lock will synchronize with
  23244. * other processors updating the list of slabs.
  23245. */
  23246. - spin_lock_irqsave(&n->list_lock, flags);
  23247. + raw_spin_lock_irqsave(&n->list_lock, flags);
  23248. }
  23249. }
  23250. @@ -2873,7 +2935,7 @@
  23251. add_partial(n, page, DEACTIVATE_TO_TAIL);
  23252. stat(s, FREE_ADD_PARTIAL);
  23253. }
  23254. - spin_unlock_irqrestore(&n->list_lock, flags);
  23255. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  23256. return;
  23257. slab_empty:
  23258. @@ -2888,7 +2950,7 @@
  23259. remove_full(s, n, page);
  23260. }
  23261. - spin_unlock_irqrestore(&n->list_lock, flags);
  23262. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  23263. stat(s, FREE_SLAB);
  23264. discard_slab(s, page);
  23265. }
  23266. @@ -3093,6 +3155,7 @@
  23267. void **p)
  23268. {
  23269. struct kmem_cache_cpu *c;
  23270. + LIST_HEAD(to_free);
  23271. int i;
  23272. /* memcg and kmem_cache debug support */
  23273. @@ -3116,7 +3179,7 @@
  23274. * of re-populating per CPU c->freelist
  23275. */
  23276. p[i] = ___slab_alloc(s, flags, NUMA_NO_NODE,
  23277. - _RET_IP_, c);
  23278. + _RET_IP_, c, &to_free);
  23279. if (unlikely(!p[i]))
  23280. goto error;
  23281. @@ -3128,6 +3191,7 @@
  23282. }
  23283. c->tid = next_tid(c->tid);
  23284. local_irq_enable();
  23285. + free_delayed(&to_free);
  23286. /* Clear memory outside IRQ disabled fastpath loop */
  23287. if (unlikely(flags & __GFP_ZERO)) {
  23288. @@ -3275,7 +3339,7 @@
  23289. init_kmem_cache_node(struct kmem_cache_node *n)
  23290. {
  23291. n->nr_partial = 0;
  23292. - spin_lock_init(&n->list_lock);
  23293. + raw_spin_lock_init(&n->list_lock);
  23294. INIT_LIST_HEAD(&n->partial);
  23295. #ifdef CONFIG_SLUB_DEBUG
  23296. atomic_long_set(&n->nr_slabs, 0);
  23297. @@ -3619,6 +3683,10 @@
  23298. const char *text)
  23299. {
  23300. #ifdef CONFIG_SLUB_DEBUG
  23301. +#ifdef CONFIG_PREEMPT_RT_BASE
  23302. + /* XXX move out of irq-off section */
  23303. + slab_err(s, page, text, s->name);
  23304. +#else
  23305. void *addr = page_address(page);
  23306. void *p;
  23307. unsigned long *map = kzalloc(BITS_TO_LONGS(page->objects) *
  23308. @@ -3639,6 +3707,7 @@
  23309. slab_unlock(page);
  23310. kfree(map);
  23311. #endif
  23312. +#endif
  23313. }
  23314. /*
  23315. @@ -3652,7 +3721,7 @@
  23316. struct page *page, *h;
  23317. BUG_ON(irqs_disabled());
  23318. - spin_lock_irq(&n->list_lock);
  23319. + raw_spin_lock_irq(&n->list_lock);
  23320. list_for_each_entry_safe(page, h, &n->partial, lru) {
  23321. if (!page->inuse) {
  23322. remove_partial(n, page);
  23323. @@ -3662,7 +3731,7 @@
  23324. "Objects remaining in %s on __kmem_cache_shutdown()");
  23325. }
  23326. }
  23327. - spin_unlock_irq(&n->list_lock);
  23328. + raw_spin_unlock_irq(&n->list_lock);
  23329. list_for_each_entry_safe(page, h, &discard, lru)
  23330. discard_slab(s, page);
  23331. @@ -3905,7 +3974,7 @@
  23332. for (i = 0; i < SHRINK_PROMOTE_MAX; i++)
  23333. INIT_LIST_HEAD(promote + i);
  23334. - spin_lock_irqsave(&n->list_lock, flags);
  23335. + raw_spin_lock_irqsave(&n->list_lock, flags);
  23336. /*
  23337. * Build lists of slabs to discard or promote.
  23338. @@ -3936,7 +4005,7 @@
  23339. for (i = SHRINK_PROMOTE_MAX - 1; i >= 0; i--)
  23340. list_splice(promote + i, &n->partial);
  23341. - spin_unlock_irqrestore(&n->list_lock, flags);
  23342. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  23343. /* Release empty slabs */
  23344. list_for_each_entry_safe(page, t, &discard, lru)
  23345. @@ -4112,6 +4181,12 @@
  23346. {
  23347. static __initdata struct kmem_cache boot_kmem_cache,
  23348. boot_kmem_cache_node;
  23349. + int cpu;
  23350. +
  23351. + for_each_possible_cpu(cpu) {
  23352. + raw_spin_lock_init(&per_cpu(slub_free_list, cpu).lock);
  23353. + INIT_LIST_HEAD(&per_cpu(slub_free_list, cpu).list);
  23354. + }
  23355. if (debug_guardpage_minorder())
  23356. slub_max_order = 0;
  23357. @@ -4320,7 +4395,7 @@
  23358. struct page *page;
  23359. unsigned long flags;
  23360. - spin_lock_irqsave(&n->list_lock, flags);
  23361. + raw_spin_lock_irqsave(&n->list_lock, flags);
  23362. list_for_each_entry(page, &n->partial, lru) {
  23363. validate_slab_slab(s, page, map);
  23364. @@ -4342,7 +4417,7 @@
  23365. s->name, count, atomic_long_read(&n->nr_slabs));
  23366. out:
  23367. - spin_unlock_irqrestore(&n->list_lock, flags);
  23368. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  23369. return count;
  23370. }
  23371. @@ -4530,12 +4605,12 @@
  23372. if (!atomic_long_read(&n->nr_slabs))
  23373. continue;
  23374. - spin_lock_irqsave(&n->list_lock, flags);
  23375. + raw_spin_lock_irqsave(&n->list_lock, flags);
  23376. list_for_each_entry(page, &n->partial, lru)
  23377. process_slab(&t, s, page, alloc, map);
  23378. list_for_each_entry(page, &n->full, lru)
  23379. process_slab(&t, s, page, alloc, map);
  23380. - spin_unlock_irqrestore(&n->list_lock, flags);
  23381. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  23382. }
  23383. for (i = 0; i < t.count; i++) {
  23384. diff -Nur linux-4.9.28.orig/mm/swap.c linux-4.9.28/mm/swap.c
  23385. --- linux-4.9.28.orig/mm/swap.c 2017-05-14 14:00:37.000000000 +0200
  23386. +++ linux-4.9.28/mm/swap.c 2017-05-19 03:37:25.210177882 +0200
  23387. @@ -32,6 +32,7 @@
  23388. #include <linux/memcontrol.h>
  23389. #include <linux/gfp.h>
  23390. #include <linux/uio.h>
  23391. +#include <linux/locallock.h>
  23392. #include <linux/hugetlb.h>
  23393. #include <linux/page_idle.h>
  23394. @@ -50,6 +51,8 @@
  23395. #ifdef CONFIG_SMP
  23396. static DEFINE_PER_CPU(struct pagevec, activate_page_pvecs);
  23397. #endif
  23398. +static DEFINE_LOCAL_IRQ_LOCK(rotate_lock);
  23399. +DEFINE_LOCAL_IRQ_LOCK(swapvec_lock);
  23400. /*
  23401. * This path almost never happens for VM activity - pages are normally
  23402. @@ -240,11 +243,11 @@
  23403. unsigned long flags;
  23404. get_page(page);
  23405. - local_irq_save(flags);
  23406. + local_lock_irqsave(rotate_lock, flags);
  23407. pvec = this_cpu_ptr(&lru_rotate_pvecs);
  23408. if (!pagevec_add(pvec, page) || PageCompound(page))
  23409. pagevec_move_tail(pvec);
  23410. - local_irq_restore(flags);
  23411. + local_unlock_irqrestore(rotate_lock, flags);
  23412. }
  23413. }
  23414. @@ -294,12 +297,13 @@
  23415. {
  23416. page = compound_head(page);
  23417. if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
  23418. - struct pagevec *pvec = &get_cpu_var(activate_page_pvecs);
  23419. + struct pagevec *pvec = &get_locked_var(swapvec_lock,
  23420. + activate_page_pvecs);
  23421. get_page(page);
  23422. if (!pagevec_add(pvec, page) || PageCompound(page))
  23423. pagevec_lru_move_fn(pvec, __activate_page, NULL);
  23424. - put_cpu_var(activate_page_pvecs);
  23425. + put_locked_var(swapvec_lock, activate_page_pvecs);
  23426. }
  23427. }
  23428. @@ -326,7 +330,7 @@
  23429. static void __lru_cache_activate_page(struct page *page)
  23430. {
  23431. - struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
  23432. + struct pagevec *pvec = &get_locked_var(swapvec_lock, lru_add_pvec);
  23433. int i;
  23434. /*
  23435. @@ -348,7 +352,7 @@
  23436. }
  23437. }
  23438. - put_cpu_var(lru_add_pvec);
  23439. + put_locked_var(swapvec_lock, lru_add_pvec);
  23440. }
  23441. /*
  23442. @@ -390,12 +394,12 @@
  23443. static void __lru_cache_add(struct page *page)
  23444. {
  23445. - struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
  23446. + struct pagevec *pvec = &get_locked_var(swapvec_lock, lru_add_pvec);
  23447. get_page(page);
  23448. if (!pagevec_add(pvec, page) || PageCompound(page))
  23449. __pagevec_lru_add(pvec);
  23450. - put_cpu_var(lru_add_pvec);
  23451. + put_locked_var(swapvec_lock, lru_add_pvec);
  23452. }
  23453. /**
  23454. @@ -593,9 +597,15 @@
  23455. unsigned long flags;
  23456. /* No harm done if a racing interrupt already did this */
  23457. - local_irq_save(flags);
  23458. +#ifdef CONFIG_PREEMPT_RT_BASE
  23459. + local_lock_irqsave_on(rotate_lock, flags, cpu);
  23460. pagevec_move_tail(pvec);
  23461. - local_irq_restore(flags);
  23462. + local_unlock_irqrestore_on(rotate_lock, flags, cpu);
  23463. +#else
  23464. + local_lock_irqsave(rotate_lock, flags);
  23465. + pagevec_move_tail(pvec);
  23466. + local_unlock_irqrestore(rotate_lock, flags);
  23467. +#endif
  23468. }
  23469. pvec = &per_cpu(lru_deactivate_file_pvecs, cpu);
  23470. @@ -627,11 +637,12 @@
  23471. return;
  23472. if (likely(get_page_unless_zero(page))) {
  23473. - struct pagevec *pvec = &get_cpu_var(lru_deactivate_file_pvecs);
  23474. + struct pagevec *pvec = &get_locked_var(swapvec_lock,
  23475. + lru_deactivate_file_pvecs);
  23476. if (!pagevec_add(pvec, page) || PageCompound(page))
  23477. pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
  23478. - put_cpu_var(lru_deactivate_file_pvecs);
  23479. + put_locked_var(swapvec_lock, lru_deactivate_file_pvecs);
  23480. }
  23481. }
  23482. @@ -646,27 +657,31 @@
  23483. void deactivate_page(struct page *page)
  23484. {
  23485. if (PageLRU(page) && PageActive(page) && !PageUnevictable(page)) {
  23486. - struct pagevec *pvec = &get_cpu_var(lru_deactivate_pvecs);
  23487. + struct pagevec *pvec = &get_locked_var(swapvec_lock,
  23488. + lru_deactivate_pvecs);
  23489. get_page(page);
  23490. if (!pagevec_add(pvec, page) || PageCompound(page))
  23491. pagevec_lru_move_fn(pvec, lru_deactivate_fn, NULL);
  23492. - put_cpu_var(lru_deactivate_pvecs);
  23493. + put_locked_var(swapvec_lock, lru_deactivate_pvecs);
  23494. }
  23495. }
  23496. void lru_add_drain(void)
  23497. {
  23498. - lru_add_drain_cpu(get_cpu());
  23499. - put_cpu();
  23500. + lru_add_drain_cpu(local_lock_cpu(swapvec_lock));
  23501. + local_unlock_cpu(swapvec_lock);
  23502. }
  23503. -static void lru_add_drain_per_cpu(struct work_struct *dummy)
  23504. +#ifdef CONFIG_PREEMPT_RT_BASE
  23505. +static inline void remote_lru_add_drain(int cpu, struct cpumask *has_work)
  23506. {
  23507. - lru_add_drain();
  23508. + local_lock_on(swapvec_lock, cpu);
  23509. + lru_add_drain_cpu(cpu);
  23510. + local_unlock_on(swapvec_lock, cpu);
  23511. }
  23512. -static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);
  23513. +#else
  23514. /*
  23515. * lru_add_drain_wq is used to do lru_add_drain_all() from a WQ_MEM_RECLAIM
  23516. @@ -686,6 +701,22 @@
  23517. }
  23518. early_initcall(lru_init);
  23519. +static void lru_add_drain_per_cpu(struct work_struct *dummy)
  23520. +{
  23521. + lru_add_drain();
  23522. +}
  23523. +
  23524. +static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);
  23525. +static inline void remote_lru_add_drain(int cpu, struct cpumask *has_work)
  23526. +{
  23527. + struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
  23528. +
  23529. + INIT_WORK(work, lru_add_drain_per_cpu);
  23530. + queue_work_on(cpu, lru_add_drain_wq, work);
  23531. + cpumask_set_cpu(cpu, has_work);
  23532. +}
  23533. +#endif
  23534. +
  23535. void lru_add_drain_all(void)
  23536. {
  23537. static DEFINE_MUTEX(lock);
  23538. @@ -697,21 +728,18 @@
  23539. cpumask_clear(&has_work);
  23540. for_each_online_cpu(cpu) {
  23541. - struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
  23542. -
  23543. if (pagevec_count(&per_cpu(lru_add_pvec, cpu)) ||
  23544. pagevec_count(&per_cpu(lru_rotate_pvecs, cpu)) ||
  23545. pagevec_count(&per_cpu(lru_deactivate_file_pvecs, cpu)) ||
  23546. pagevec_count(&per_cpu(lru_deactivate_pvecs, cpu)) ||
  23547. - need_activate_page_drain(cpu)) {
  23548. - INIT_WORK(work, lru_add_drain_per_cpu);
  23549. - queue_work_on(cpu, lru_add_drain_wq, work);
  23550. - cpumask_set_cpu(cpu, &has_work);
  23551. - }
  23552. + need_activate_page_drain(cpu))
  23553. + remote_lru_add_drain(cpu, &has_work);
  23554. }
  23555. +#ifndef CONFIG_PREEMPT_RT_BASE
  23556. for_each_cpu(cpu, &has_work)
  23557. flush_work(&per_cpu(lru_add_drain_work, cpu));
  23558. +#endif
  23559. put_online_cpus();
  23560. mutex_unlock(&lock);
  23561. diff -Nur linux-4.9.28.orig/mm/truncate.c linux-4.9.28/mm/truncate.c
  23562. --- linux-4.9.28.orig/mm/truncate.c 2017-05-14 14:00:37.000000000 +0200
  23563. +++ linux-4.9.28/mm/truncate.c 2017-05-19 03:37:25.214178037 +0200
  23564. @@ -62,9 +62,12 @@
  23565. * protected by mapping->tree_lock.
  23566. */
  23567. if (!workingset_node_shadows(node) &&
  23568. - !list_empty(&node->private_list))
  23569. - list_lru_del(&workingset_shadow_nodes,
  23570. + !list_empty(&node->private_list)) {
  23571. + local_lock(workingset_shadow_lock);
  23572. + list_lru_del(&__workingset_shadow_nodes,
  23573. &node->private_list);
  23574. + local_unlock(workingset_shadow_lock);
  23575. + }
  23576. __radix_tree_delete_node(&mapping->page_tree, node);
  23577. unlock:
  23578. spin_unlock_irq(&mapping->tree_lock);
  23579. diff -Nur linux-4.9.28.orig/mm/vmalloc.c linux-4.9.28/mm/vmalloc.c
  23580. --- linux-4.9.28.orig/mm/vmalloc.c 2017-05-14 14:00:37.000000000 +0200
  23581. +++ linux-4.9.28/mm/vmalloc.c 2017-05-19 03:37:25.214178037 +0200
  23582. @@ -845,7 +845,7 @@
  23583. struct vmap_block *vb;
  23584. struct vmap_area *va;
  23585. unsigned long vb_idx;
  23586. - int node, err;
  23587. + int node, err, cpu;
  23588. void *vaddr;
  23589. node = numa_node_id();
  23590. @@ -888,11 +888,12 @@
  23591. BUG_ON(err);
  23592. radix_tree_preload_end();
  23593. - vbq = &get_cpu_var(vmap_block_queue);
  23594. + cpu = get_cpu_light();
  23595. + vbq = this_cpu_ptr(&vmap_block_queue);
  23596. spin_lock(&vbq->lock);
  23597. list_add_tail_rcu(&vb->free_list, &vbq->free);
  23598. spin_unlock(&vbq->lock);
  23599. - put_cpu_var(vmap_block_queue);
  23600. + put_cpu_light();
  23601. return vaddr;
  23602. }
  23603. @@ -961,6 +962,7 @@
  23604. struct vmap_block *vb;
  23605. void *vaddr = NULL;
  23606. unsigned int order;
  23607. + int cpu;
  23608. BUG_ON(offset_in_page(size));
  23609. BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
  23610. @@ -975,7 +977,8 @@
  23611. order = get_order(size);
  23612. rcu_read_lock();
  23613. - vbq = &get_cpu_var(vmap_block_queue);
  23614. + cpu = get_cpu_light();
  23615. + vbq = this_cpu_ptr(&vmap_block_queue);
  23616. list_for_each_entry_rcu(vb, &vbq->free, free_list) {
  23617. unsigned long pages_off;
  23618. @@ -998,7 +1001,7 @@
  23619. break;
  23620. }
  23621. - put_cpu_var(vmap_block_queue);
  23622. + put_cpu_light();
  23623. rcu_read_unlock();
  23624. /* Allocate new block if nothing was found */
  23625. diff -Nur linux-4.9.28.orig/mm/vmstat.c linux-4.9.28/mm/vmstat.c
  23626. --- linux-4.9.28.orig/mm/vmstat.c 2017-05-14 14:00:37.000000000 +0200
  23627. +++ linux-4.9.28/mm/vmstat.c 2017-05-19 03:37:25.214178037 +0200
  23628. @@ -245,6 +245,7 @@
  23629. long x;
  23630. long t;
  23631. + preempt_disable_rt();
  23632. x = delta + __this_cpu_read(*p);
  23633. t = __this_cpu_read(pcp->stat_threshold);
  23634. @@ -254,6 +255,7 @@
  23635. x = 0;
  23636. }
  23637. __this_cpu_write(*p, x);
  23638. + preempt_enable_rt();
  23639. }
  23640. EXPORT_SYMBOL(__mod_zone_page_state);
  23641. @@ -265,6 +267,7 @@
  23642. long x;
  23643. long t;
  23644. + preempt_disable_rt();
  23645. x = delta + __this_cpu_read(*p);
  23646. t = __this_cpu_read(pcp->stat_threshold);
  23647. @@ -274,6 +277,7 @@
  23648. x = 0;
  23649. }
  23650. __this_cpu_write(*p, x);
  23651. + preempt_enable_rt();
  23652. }
  23653. EXPORT_SYMBOL(__mod_node_page_state);
  23654. @@ -306,6 +310,7 @@
  23655. s8 __percpu *p = pcp->vm_stat_diff + item;
  23656. s8 v, t;
  23657. + preempt_disable_rt();
  23658. v = __this_cpu_inc_return(*p);
  23659. t = __this_cpu_read(pcp->stat_threshold);
  23660. if (unlikely(v > t)) {
  23661. @@ -314,6 +319,7 @@
  23662. zone_page_state_add(v + overstep, zone, item);
  23663. __this_cpu_write(*p, -overstep);
  23664. }
  23665. + preempt_enable_rt();
  23666. }
  23667. void __inc_node_state(struct pglist_data *pgdat, enum node_stat_item item)
  23668. @@ -322,6 +328,7 @@
  23669. s8 __percpu *p = pcp->vm_node_stat_diff + item;
  23670. s8 v, t;
  23671. + preempt_disable_rt();
  23672. v = __this_cpu_inc_return(*p);
  23673. t = __this_cpu_read(pcp->stat_threshold);
  23674. if (unlikely(v > t)) {
  23675. @@ -330,6 +337,7 @@
  23676. node_page_state_add(v + overstep, pgdat, item);
  23677. __this_cpu_write(*p, -overstep);
  23678. }
  23679. + preempt_enable_rt();
  23680. }
  23681. void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
  23682. @@ -350,6 +358,7 @@
  23683. s8 __percpu *p = pcp->vm_stat_diff + item;
  23684. s8 v, t;
  23685. + preempt_disable_rt();
  23686. v = __this_cpu_dec_return(*p);
  23687. t = __this_cpu_read(pcp->stat_threshold);
  23688. if (unlikely(v < - t)) {
  23689. @@ -358,6 +367,7 @@
  23690. zone_page_state_add(v - overstep, zone, item);
  23691. __this_cpu_write(*p, overstep);
  23692. }
  23693. + preempt_enable_rt();
  23694. }
  23695. void __dec_node_state(struct pglist_data *pgdat, enum node_stat_item item)
  23696. @@ -366,6 +376,7 @@
  23697. s8 __percpu *p = pcp->vm_node_stat_diff + item;
  23698. s8 v, t;
  23699. + preempt_disable_rt();
  23700. v = __this_cpu_dec_return(*p);
  23701. t = __this_cpu_read(pcp->stat_threshold);
  23702. if (unlikely(v < - t)) {
  23703. @@ -374,6 +385,7 @@
  23704. node_page_state_add(v - overstep, pgdat, item);
  23705. __this_cpu_write(*p, overstep);
  23706. }
  23707. + preempt_enable_rt();
  23708. }
  23709. void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
  23710. diff -Nur linux-4.9.28.orig/mm/workingset.c linux-4.9.28/mm/workingset.c
  23711. --- linux-4.9.28.orig/mm/workingset.c 2017-05-14 14:00:37.000000000 +0200
  23712. +++ linux-4.9.28/mm/workingset.c 2017-05-19 03:37:25.214178037 +0200
  23713. @@ -334,7 +334,8 @@
  23714. * point where they would still be useful.
  23715. */
  23716. -struct list_lru workingset_shadow_nodes;
  23717. +struct list_lru __workingset_shadow_nodes;
  23718. +DEFINE_LOCAL_IRQ_LOCK(workingset_shadow_lock);
  23719. static unsigned long count_shadow_nodes(struct shrinker *shrinker,
  23720. struct shrink_control *sc)
  23721. @@ -344,9 +345,9 @@
  23722. unsigned long pages;
  23723. /* list_lru lock nests inside IRQ-safe mapping->tree_lock */
  23724. - local_irq_disable();
  23725. - shadow_nodes = list_lru_shrink_count(&workingset_shadow_nodes, sc);
  23726. - local_irq_enable();
  23727. + local_lock_irq(workingset_shadow_lock);
  23728. + shadow_nodes = list_lru_shrink_count(&__workingset_shadow_nodes, sc);
  23729. + local_unlock_irq(workingset_shadow_lock);
  23730. if (sc->memcg) {
  23731. pages = mem_cgroup_node_nr_lru_pages(sc->memcg, sc->nid,
  23732. @@ -438,9 +439,9 @@
  23733. spin_unlock(&mapping->tree_lock);
  23734. ret = LRU_REMOVED_RETRY;
  23735. out:
  23736. - local_irq_enable();
  23737. + local_unlock_irq(workingset_shadow_lock);
  23738. cond_resched();
  23739. - local_irq_disable();
  23740. + local_lock_irq(workingset_shadow_lock);
  23741. spin_lock(lru_lock);
  23742. return ret;
  23743. }
  23744. @@ -451,10 +452,10 @@
  23745. unsigned long ret;
  23746. /* list_lru lock nests inside IRQ-safe mapping->tree_lock */
  23747. - local_irq_disable();
  23748. - ret = list_lru_shrink_walk(&workingset_shadow_nodes, sc,
  23749. + local_lock_irq(workingset_shadow_lock);
  23750. + ret = list_lru_shrink_walk(&__workingset_shadow_nodes, sc,
  23751. shadow_lru_isolate, NULL);
  23752. - local_irq_enable();
  23753. + local_unlock_irq(workingset_shadow_lock);
  23754. return ret;
  23755. }
  23756. @@ -492,7 +493,7 @@
  23757. pr_info("workingset: timestamp_bits=%d max_order=%d bucket_order=%u\n",
  23758. timestamp_bits, max_order, bucket_order);
  23759. - ret = __list_lru_init(&workingset_shadow_nodes, true, &shadow_nodes_key);
  23760. + ret = __list_lru_init(&__workingset_shadow_nodes, true, &shadow_nodes_key);
  23761. if (ret)
  23762. goto err;
  23763. ret = register_shrinker(&workingset_shadow_shrinker);
  23764. @@ -500,7 +501,7 @@
  23765. goto err_list_lru;
  23766. return 0;
  23767. err_list_lru:
  23768. - list_lru_destroy(&workingset_shadow_nodes);
  23769. + list_lru_destroy(&__workingset_shadow_nodes);
  23770. err:
  23771. return ret;
  23772. }
  23773. diff -Nur linux-4.9.28.orig/mm/zsmalloc.c linux-4.9.28/mm/zsmalloc.c
  23774. --- linux-4.9.28.orig/mm/zsmalloc.c 2017-05-14 14:00:37.000000000 +0200
  23775. +++ linux-4.9.28/mm/zsmalloc.c 2017-05-19 03:37:25.214178037 +0200
  23776. @@ -53,6 +53,7 @@
  23777. #include <linux/mount.h>
  23778. #include <linux/migrate.h>
  23779. #include <linux/pagemap.h>
  23780. +#include <linux/locallock.h>
  23781. #define ZSPAGE_MAGIC 0x58
  23782. @@ -70,9 +71,22 @@
  23783. */
  23784. #define ZS_MAX_ZSPAGE_ORDER 2
  23785. #define ZS_MAX_PAGES_PER_ZSPAGE (_AC(1, UL) << ZS_MAX_ZSPAGE_ORDER)
  23786. -
  23787. #define ZS_HANDLE_SIZE (sizeof(unsigned long))
  23788. +#ifdef CONFIG_PREEMPT_RT_FULL
  23789. +
  23790. +struct zsmalloc_handle {
  23791. + unsigned long addr;
  23792. + struct mutex lock;
  23793. +};
  23794. +
  23795. +#define ZS_HANDLE_ALLOC_SIZE (sizeof(struct zsmalloc_handle))
  23796. +
  23797. +#else
  23798. +
  23799. +#define ZS_HANDLE_ALLOC_SIZE (sizeof(unsigned long))
  23800. +#endif
  23801. +
  23802. /*
  23803. * Object location (<PFN>, <obj_idx>) is encoded as
  23804. * as single (unsigned long) handle value.
  23805. @@ -327,7 +341,7 @@
  23806. static int create_cache(struct zs_pool *pool)
  23807. {
  23808. - pool->handle_cachep = kmem_cache_create("zs_handle", ZS_HANDLE_SIZE,
  23809. + pool->handle_cachep = kmem_cache_create("zs_handle", ZS_HANDLE_ALLOC_SIZE,
  23810. 0, 0, NULL);
  23811. if (!pool->handle_cachep)
  23812. return 1;
  23813. @@ -351,10 +365,27 @@
  23814. static unsigned long cache_alloc_handle(struct zs_pool *pool, gfp_t gfp)
  23815. {
  23816. - return (unsigned long)kmem_cache_alloc(pool->handle_cachep,
  23817. - gfp & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
  23818. + void *p;
  23819. +
  23820. + p = kmem_cache_alloc(pool->handle_cachep,
  23821. + gfp & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
  23822. +#ifdef CONFIG_PREEMPT_RT_FULL
  23823. + if (p) {
  23824. + struct zsmalloc_handle *zh = p;
  23825. +
  23826. + mutex_init(&zh->lock);
  23827. + }
  23828. +#endif
  23829. + return (unsigned long)p;
  23830. }
  23831. +#ifdef CONFIG_PREEMPT_RT_FULL
  23832. +static struct zsmalloc_handle *zs_get_pure_handle(unsigned long handle)
  23833. +{
  23834. + return (void *)(handle &~((1 << OBJ_TAG_BITS) - 1));
  23835. +}
  23836. +#endif
  23837. +
  23838. static void cache_free_handle(struct zs_pool *pool, unsigned long handle)
  23839. {
  23840. kmem_cache_free(pool->handle_cachep, (void *)handle);
  23841. @@ -373,12 +404,18 @@
  23842. static void record_obj(unsigned long handle, unsigned long obj)
  23843. {
  23844. +#ifdef CONFIG_PREEMPT_RT_FULL
  23845. + struct zsmalloc_handle *zh = zs_get_pure_handle(handle);
  23846. +
  23847. + WRITE_ONCE(zh->addr, obj);
  23848. +#else
  23849. /*
  23850. * lsb of @obj represents handle lock while other bits
  23851. * represent object value the handle is pointing so
  23852. * updating shouldn't do store tearing.
  23853. */
  23854. WRITE_ONCE(*(unsigned long *)handle, obj);
  23855. +#endif
  23856. }
  23857. /* zpool driver */
  23858. @@ -467,6 +504,7 @@
  23859. /* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
  23860. static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
  23861. +static DEFINE_LOCAL_IRQ_LOCK(zs_map_area_lock);
  23862. static bool is_zspage_isolated(struct zspage *zspage)
  23863. {
  23864. @@ -902,7 +940,13 @@
  23865. static unsigned long handle_to_obj(unsigned long handle)
  23866. {
  23867. +#ifdef CONFIG_PREEMPT_RT_FULL
  23868. + struct zsmalloc_handle *zh = zs_get_pure_handle(handle);
  23869. +
  23870. + return zh->addr;
  23871. +#else
  23872. return *(unsigned long *)handle;
  23873. +#endif
  23874. }
  23875. static unsigned long obj_to_head(struct page *page, void *obj)
  23876. @@ -916,22 +960,46 @@
  23877. static inline int testpin_tag(unsigned long handle)
  23878. {
  23879. +#ifdef CONFIG_PREEMPT_RT_FULL
  23880. + struct zsmalloc_handle *zh = zs_get_pure_handle(handle);
  23881. +
  23882. + return mutex_is_locked(&zh->lock);
  23883. +#else
  23884. return bit_spin_is_locked(HANDLE_PIN_BIT, (unsigned long *)handle);
  23885. +#endif
  23886. }
  23887. static inline int trypin_tag(unsigned long handle)
  23888. {
  23889. +#ifdef CONFIG_PREEMPT_RT_FULL
  23890. + struct zsmalloc_handle *zh = zs_get_pure_handle(handle);
  23891. +
  23892. + return mutex_trylock(&zh->lock);
  23893. +#else
  23894. return bit_spin_trylock(HANDLE_PIN_BIT, (unsigned long *)handle);
  23895. +#endif
  23896. }
  23897. static void pin_tag(unsigned long handle)
  23898. {
  23899. +#ifdef CONFIG_PREEMPT_RT_FULL
  23900. + struct zsmalloc_handle *zh = zs_get_pure_handle(handle);
  23901. +
  23902. + return mutex_lock(&zh->lock);
  23903. +#else
  23904. bit_spin_lock(HANDLE_PIN_BIT, (unsigned long *)handle);
  23905. +#endif
  23906. }
  23907. static void unpin_tag(unsigned long handle)
  23908. {
  23909. +#ifdef CONFIG_PREEMPT_RT_FULL
  23910. + struct zsmalloc_handle *zh = zs_get_pure_handle(handle);
  23911. +
  23912. + return mutex_unlock(&zh->lock);
  23913. +#else
  23914. bit_spin_unlock(HANDLE_PIN_BIT, (unsigned long *)handle);
  23915. +#endif
  23916. }
  23917. static void reset_page(struct page *page)
  23918. @@ -1423,7 +1491,7 @@
  23919. class = pool->size_class[class_idx];
  23920. off = (class->size * obj_idx) & ~PAGE_MASK;
  23921. - area = &get_cpu_var(zs_map_area);
  23922. + area = &get_locked_var(zs_map_area_lock, zs_map_area);
  23923. area->vm_mm = mm;
  23924. if (off + class->size <= PAGE_SIZE) {
  23925. /* this object is contained entirely within a page */
  23926. @@ -1477,7 +1545,7 @@
  23927. __zs_unmap_object(area, pages, off, class->size);
  23928. }
  23929. - put_cpu_var(zs_map_area);
  23930. + put_locked_var(zs_map_area_lock, zs_map_area);
  23931. migrate_read_unlock(zspage);
  23932. unpin_tag(handle);
  23933. diff -Nur linux-4.9.28.orig/net/core/dev.c linux-4.9.28/net/core/dev.c
  23934. --- linux-4.9.28.orig/net/core/dev.c 2017-05-14 14:00:37.000000000 +0200
  23935. +++ linux-4.9.28/net/core/dev.c 2017-05-19 03:37:25.214178037 +0200
  23936. @@ -190,6 +190,7 @@
  23937. static DEFINE_READ_MOSTLY_HASHTABLE(napi_hash, 8);
  23938. static seqcount_t devnet_rename_seq;
  23939. +static DEFINE_MUTEX(devnet_rename_mutex);
  23940. static inline void dev_base_seq_inc(struct net *net)
  23941. {
  23942. @@ -211,14 +212,14 @@
  23943. static inline void rps_lock(struct softnet_data *sd)
  23944. {
  23945. #ifdef CONFIG_RPS
  23946. - spin_lock(&sd->input_pkt_queue.lock);
  23947. + raw_spin_lock(&sd->input_pkt_queue.raw_lock);
  23948. #endif
  23949. }
  23950. static inline void rps_unlock(struct softnet_data *sd)
  23951. {
  23952. #ifdef CONFIG_RPS
  23953. - spin_unlock(&sd->input_pkt_queue.lock);
  23954. + raw_spin_unlock(&sd->input_pkt_queue.raw_lock);
  23955. #endif
  23956. }
  23957. @@ -888,7 +889,8 @@
  23958. strcpy(name, dev->name);
  23959. rcu_read_unlock();
  23960. if (read_seqcount_retry(&devnet_rename_seq, seq)) {
  23961. - cond_resched();
  23962. + mutex_lock(&devnet_rename_mutex);
  23963. + mutex_unlock(&devnet_rename_mutex);
  23964. goto retry;
  23965. }
  23966. @@ -1157,20 +1159,17 @@
  23967. if (dev->flags & IFF_UP)
  23968. return -EBUSY;
  23969. - write_seqcount_begin(&devnet_rename_seq);
  23970. + mutex_lock(&devnet_rename_mutex);
  23971. + __raw_write_seqcount_begin(&devnet_rename_seq);
  23972. - if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
  23973. - write_seqcount_end(&devnet_rename_seq);
  23974. - return 0;
  23975. - }
  23976. + if (strncmp(newname, dev->name, IFNAMSIZ) == 0)
  23977. + goto outunlock;
  23978. memcpy(oldname, dev->name, IFNAMSIZ);
  23979. err = dev_get_valid_name(net, dev, newname);
  23980. - if (err < 0) {
  23981. - write_seqcount_end(&devnet_rename_seq);
  23982. - return err;
  23983. - }
  23984. + if (err < 0)
  23985. + goto outunlock;
  23986. if (oldname[0] && !strchr(oldname, '%'))
  23987. netdev_info(dev, "renamed from %s\n", oldname);
  23988. @@ -1183,11 +1182,12 @@
  23989. if (ret) {
  23990. memcpy(dev->name, oldname, IFNAMSIZ);
  23991. dev->name_assign_type = old_assign_type;
  23992. - write_seqcount_end(&devnet_rename_seq);
  23993. - return ret;
  23994. + err = ret;
  23995. + goto outunlock;
  23996. }
  23997. - write_seqcount_end(&devnet_rename_seq);
  23998. + __raw_write_seqcount_end(&devnet_rename_seq);
  23999. + mutex_unlock(&devnet_rename_mutex);
  24000. netdev_adjacent_rename_links(dev, oldname);
  24001. @@ -1208,7 +1208,8 @@
  24002. /* err >= 0 after dev_alloc_name() or stores the first errno */
  24003. if (err >= 0) {
  24004. err = ret;
  24005. - write_seqcount_begin(&devnet_rename_seq);
  24006. + mutex_lock(&devnet_rename_mutex);
  24007. + __raw_write_seqcount_begin(&devnet_rename_seq);
  24008. memcpy(dev->name, oldname, IFNAMSIZ);
  24009. memcpy(oldname, newname, IFNAMSIZ);
  24010. dev->name_assign_type = old_assign_type;
  24011. @@ -1221,6 +1222,11 @@
  24012. }
  24013. return err;
  24014. +
  24015. +outunlock:
  24016. + __raw_write_seqcount_end(&devnet_rename_seq);
  24017. + mutex_unlock(&devnet_rename_mutex);
  24018. + return err;
  24019. }
  24020. /**
  24021. @@ -2285,6 +2291,7 @@
  24022. sd->output_queue_tailp = &q->next_sched;
  24023. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  24024. local_irq_restore(flags);
  24025. + preempt_check_resched_rt();
  24026. }
  24027. void __netif_schedule(struct Qdisc *q)
  24028. @@ -2366,6 +2373,7 @@
  24029. __this_cpu_write(softnet_data.completion_queue, skb);
  24030. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  24031. local_irq_restore(flags);
  24032. + preempt_check_resched_rt();
  24033. }
  24034. EXPORT_SYMBOL(__dev_kfree_skb_irq);
  24035. @@ -3100,7 +3108,11 @@
  24036. * This permits qdisc->running owner to get the lock more
  24037. * often and dequeue packets faster.
  24038. */
  24039. +#ifdef CONFIG_PREEMPT_RT_FULL
  24040. + contended = true;
  24041. +#else
  24042. contended = qdisc_is_running(q);
  24043. +#endif
  24044. if (unlikely(contended))
  24045. spin_lock(&q->busylock);
  24046. @@ -3163,8 +3175,10 @@
  24047. #define skb_update_prio(skb)
  24048. #endif
  24049. +#ifndef CONFIG_PREEMPT_RT_FULL
  24050. DEFINE_PER_CPU(int, xmit_recursion);
  24051. EXPORT_SYMBOL(xmit_recursion);
  24052. +#endif
  24053. /**
  24054. * dev_loopback_xmit - loop back @skb
  24055. @@ -3398,8 +3412,7 @@
  24056. int cpu = smp_processor_id(); /* ok because BHs are off */
  24057. if (txq->xmit_lock_owner != cpu) {
  24058. - if (unlikely(__this_cpu_read(xmit_recursion) >
  24059. - XMIT_RECURSION_LIMIT))
  24060. + if (unlikely(xmit_rec_read() > XMIT_RECURSION_LIMIT))
  24061. goto recursion_alert;
  24062. skb = validate_xmit_skb(skb, dev);
  24063. @@ -3409,9 +3422,9 @@
  24064. HARD_TX_LOCK(dev, txq, cpu);
  24065. if (!netif_xmit_stopped(txq)) {
  24066. - __this_cpu_inc(xmit_recursion);
  24067. + xmit_rec_inc();
  24068. skb = dev_hard_start_xmit(skb, dev, txq, &rc);
  24069. - __this_cpu_dec(xmit_recursion);
  24070. + xmit_rec_dec();
  24071. if (dev_xmit_complete(rc)) {
  24072. HARD_TX_UNLOCK(dev, txq);
  24073. goto out;
  24074. @@ -3785,6 +3798,7 @@
  24075. rps_unlock(sd);
  24076. local_irq_restore(flags);
  24077. + preempt_check_resched_rt();
  24078. atomic_long_inc(&skb->dev->rx_dropped);
  24079. kfree_skb(skb);
  24080. @@ -3803,7 +3817,7 @@
  24081. struct rps_dev_flow voidflow, *rflow = &voidflow;
  24082. int cpu;
  24083. - preempt_disable();
  24084. + migrate_disable();
  24085. rcu_read_lock();
  24086. cpu = get_rps_cpu(skb->dev, skb, &rflow);
  24087. @@ -3813,13 +3827,13 @@
  24088. ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
  24089. rcu_read_unlock();
  24090. - preempt_enable();
  24091. + migrate_enable();
  24092. } else
  24093. #endif
  24094. {
  24095. unsigned int qtail;
  24096. - ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
  24097. - put_cpu();
  24098. + ret = enqueue_to_backlog(skb, get_cpu_light(), &qtail);
  24099. + put_cpu_light();
  24100. }
  24101. return ret;
  24102. }
  24103. @@ -3853,11 +3867,9 @@
  24104. trace_netif_rx_ni_entry(skb);
  24105. - preempt_disable();
  24106. + local_bh_disable();
  24107. err = netif_rx_internal(skb);
  24108. - if (local_softirq_pending())
  24109. - do_softirq();
  24110. - preempt_enable();
  24111. + local_bh_enable();
  24112. return err;
  24113. }
  24114. @@ -4336,7 +4348,7 @@
  24115. skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
  24116. if (skb->dev->reg_state == NETREG_UNREGISTERING) {
  24117. __skb_unlink(skb, &sd->input_pkt_queue);
  24118. - kfree_skb(skb);
  24119. + __skb_queue_tail(&sd->tofree_queue, skb);
  24120. input_queue_head_incr(sd);
  24121. }
  24122. }
  24123. @@ -4346,11 +4358,14 @@
  24124. skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
  24125. if (skb->dev->reg_state == NETREG_UNREGISTERING) {
  24126. __skb_unlink(skb, &sd->process_queue);
  24127. - kfree_skb(skb);
  24128. + __skb_queue_tail(&sd->tofree_queue, skb);
  24129. input_queue_head_incr(sd);
  24130. }
  24131. }
  24132. + if (!skb_queue_empty(&sd->tofree_queue))
  24133. + raise_softirq_irqoff(NET_RX_SOFTIRQ);
  24134. local_bh_enable();
  24135. +
  24136. }
  24137. static void flush_all_backlogs(void)
  24138. @@ -4831,6 +4846,7 @@
  24139. sd->rps_ipi_list = NULL;
  24140. local_irq_enable();
  24141. + preempt_check_resched_rt();
  24142. /* Send pending IPI's to kick RPS processing on remote cpus. */
  24143. while (remsd) {
  24144. @@ -4844,6 +4860,7 @@
  24145. } else
  24146. #endif
  24147. local_irq_enable();
  24148. + preempt_check_resched_rt();
  24149. }
  24150. static bool sd_has_rps_ipi_waiting(struct softnet_data *sd)
  24151. @@ -4873,7 +4890,9 @@
  24152. while (again) {
  24153. struct sk_buff *skb;
  24154. + local_irq_disable();
  24155. while ((skb = __skb_dequeue(&sd->process_queue))) {
  24156. + local_irq_enable();
  24157. rcu_read_lock();
  24158. __netif_receive_skb(skb);
  24159. rcu_read_unlock();
  24160. @@ -4881,9 +4900,9 @@
  24161. if (++work >= quota)
  24162. return work;
  24163. + local_irq_disable();
  24164. }
  24165. - local_irq_disable();
  24166. rps_lock(sd);
  24167. if (skb_queue_empty(&sd->input_pkt_queue)) {
  24168. /*
  24169. @@ -4921,9 +4940,11 @@
  24170. local_irq_save(flags);
  24171. ____napi_schedule(this_cpu_ptr(&softnet_data), n);
  24172. local_irq_restore(flags);
  24173. + preempt_check_resched_rt();
  24174. }
  24175. EXPORT_SYMBOL(__napi_schedule);
  24176. +#ifndef CONFIG_PREEMPT_RT_FULL
  24177. /**
  24178. * __napi_schedule_irqoff - schedule for receive
  24179. * @n: entry to schedule
  24180. @@ -4935,6 +4956,7 @@
  24181. ____napi_schedule(this_cpu_ptr(&softnet_data), n);
  24182. }
  24183. EXPORT_SYMBOL(__napi_schedule_irqoff);
  24184. +#endif
  24185. void __napi_complete(struct napi_struct *n)
  24186. {
  24187. @@ -5224,13 +5246,21 @@
  24188. struct softnet_data *sd = this_cpu_ptr(&softnet_data);
  24189. unsigned long time_limit = jiffies + 2;
  24190. int budget = netdev_budget;
  24191. + struct sk_buff_head tofree_q;
  24192. + struct sk_buff *skb;
  24193. LIST_HEAD(list);
  24194. LIST_HEAD(repoll);
  24195. + __skb_queue_head_init(&tofree_q);
  24196. +
  24197. local_irq_disable();
  24198. + skb_queue_splice_init(&sd->tofree_queue, &tofree_q);
  24199. list_splice_init(&sd->poll_list, &list);
  24200. local_irq_enable();
  24201. + while ((skb = __skb_dequeue(&tofree_q)))
  24202. + kfree_skb(skb);
  24203. +
  24204. for (;;) {
  24205. struct napi_struct *n;
  24206. @@ -5261,7 +5291,7 @@
  24207. list_splice_tail(&repoll, &list);
  24208. list_splice(&list, &sd->poll_list);
  24209. if (!list_empty(&sd->poll_list))
  24210. - __raise_softirq_irqoff(NET_RX_SOFTIRQ);
  24211. + __raise_softirq_irqoff_ksoft(NET_RX_SOFTIRQ);
  24212. net_rps_action_and_irq_enable(sd);
  24213. }
  24214. @@ -8022,16 +8052,20 @@
  24215. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  24216. local_irq_enable();
  24217. + preempt_check_resched_rt();
  24218. /* Process offline CPU's input_pkt_queue */
  24219. while ((skb = __skb_dequeue(&oldsd->process_queue))) {
  24220. netif_rx_ni(skb);
  24221. input_queue_head_incr(oldsd);
  24222. }
  24223. - while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) {
  24224. + while ((skb = __skb_dequeue(&oldsd->input_pkt_queue))) {
  24225. netif_rx_ni(skb);
  24226. input_queue_head_incr(oldsd);
  24227. }
  24228. + while ((skb = __skb_dequeue(&oldsd->tofree_queue))) {
  24229. + kfree_skb(skb);
  24230. + }
  24231. return NOTIFY_OK;
  24232. }
  24233. @@ -8336,8 +8370,9 @@
  24234. INIT_WORK(flush, flush_backlog);
  24235. - skb_queue_head_init(&sd->input_pkt_queue);
  24236. - skb_queue_head_init(&sd->process_queue);
  24237. + skb_queue_head_init_raw(&sd->input_pkt_queue);
  24238. + skb_queue_head_init_raw(&sd->process_queue);
  24239. + skb_queue_head_init_raw(&sd->tofree_queue);
  24240. INIT_LIST_HEAD(&sd->poll_list);
  24241. sd->output_queue_tailp = &sd->output_queue;
  24242. #ifdef CONFIG_RPS
  24243. diff -Nur linux-4.9.28.orig/net/core/filter.c linux-4.9.28/net/core/filter.c
  24244. --- linux-4.9.28.orig/net/core/filter.c 2017-05-14 14:00:37.000000000 +0200
  24245. +++ linux-4.9.28/net/core/filter.c 2017-05-19 03:37:25.214178037 +0200
  24246. @@ -1645,7 +1645,7 @@
  24247. {
  24248. int ret;
  24249. - if (unlikely(__this_cpu_read(xmit_recursion) > XMIT_RECURSION_LIMIT)) {
  24250. + if (unlikely(xmit_rec_read() > XMIT_RECURSION_LIMIT)) {
  24251. net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
  24252. kfree_skb(skb);
  24253. return -ENETDOWN;
  24254. @@ -1653,9 +1653,9 @@
  24255. skb->dev = dev;
  24256. - __this_cpu_inc(xmit_recursion);
  24257. + xmit_rec_inc();
  24258. ret = dev_queue_xmit(skb);
  24259. - __this_cpu_dec(xmit_recursion);
  24260. + xmit_rec_dec();
  24261. return ret;
  24262. }
  24263. diff -Nur linux-4.9.28.orig/net/core/gen_estimator.c linux-4.9.28/net/core/gen_estimator.c
  24264. --- linux-4.9.28.orig/net/core/gen_estimator.c 2017-05-14 14:00:37.000000000 +0200
  24265. +++ linux-4.9.28/net/core/gen_estimator.c 2017-05-19 03:37:25.214178037 +0200
  24266. @@ -84,7 +84,7 @@
  24267. struct gnet_stats_basic_packed *bstats;
  24268. struct gnet_stats_rate_est64 *rate_est;
  24269. spinlock_t *stats_lock;
  24270. - seqcount_t *running;
  24271. + net_seqlock_t *running;
  24272. int ewma_log;
  24273. u32 last_packets;
  24274. unsigned long avpps;
  24275. @@ -213,7 +213,7 @@
  24276. struct gnet_stats_basic_cpu __percpu *cpu_bstats,
  24277. struct gnet_stats_rate_est64 *rate_est,
  24278. spinlock_t *stats_lock,
  24279. - seqcount_t *running,
  24280. + net_seqlock_t *running,
  24281. struct nlattr *opt)
  24282. {
  24283. struct gen_estimator *est;
  24284. @@ -309,7 +309,7 @@
  24285. struct gnet_stats_basic_cpu __percpu *cpu_bstats,
  24286. struct gnet_stats_rate_est64 *rate_est,
  24287. spinlock_t *stats_lock,
  24288. - seqcount_t *running, struct nlattr *opt)
  24289. + net_seqlock_t *running, struct nlattr *opt)
  24290. {
  24291. gen_kill_estimator(bstats, rate_est);
  24292. return gen_new_estimator(bstats, cpu_bstats, rate_est, stats_lock, running, opt);
  24293. diff -Nur linux-4.9.28.orig/net/core/gen_stats.c linux-4.9.28/net/core/gen_stats.c
  24294. --- linux-4.9.28.orig/net/core/gen_stats.c 2017-05-14 14:00:37.000000000 +0200
  24295. +++ linux-4.9.28/net/core/gen_stats.c 2017-05-19 03:37:25.214178037 +0200
  24296. @@ -130,7 +130,7 @@
  24297. }
  24298. void
  24299. -__gnet_stats_copy_basic(const seqcount_t *running,
  24300. +__gnet_stats_copy_basic(net_seqlock_t *running,
  24301. struct gnet_stats_basic_packed *bstats,
  24302. struct gnet_stats_basic_cpu __percpu *cpu,
  24303. struct gnet_stats_basic_packed *b)
  24304. @@ -143,10 +143,10 @@
  24305. }
  24306. do {
  24307. if (running)
  24308. - seq = read_seqcount_begin(running);
  24309. + seq = net_seq_begin(running);
  24310. bstats->bytes = b->bytes;
  24311. bstats->packets = b->packets;
  24312. - } while (running && read_seqcount_retry(running, seq));
  24313. + } while (running && net_seq_retry(running, seq));
  24314. }
  24315. EXPORT_SYMBOL(__gnet_stats_copy_basic);
  24316. @@ -164,7 +164,7 @@
  24317. * if the room in the socket buffer was not sufficient.
  24318. */
  24319. int
  24320. -gnet_stats_copy_basic(const seqcount_t *running,
  24321. +gnet_stats_copy_basic(net_seqlock_t *running,
  24322. struct gnet_dump *d,
  24323. struct gnet_stats_basic_cpu __percpu *cpu,
  24324. struct gnet_stats_basic_packed *b)
  24325. diff -Nur linux-4.9.28.orig/net/core/skbuff.c linux-4.9.28/net/core/skbuff.c
  24326. --- linux-4.9.28.orig/net/core/skbuff.c 2017-05-14 14:00:37.000000000 +0200
  24327. +++ linux-4.9.28/net/core/skbuff.c 2017-05-19 03:37:25.218178191 +0200
  24328. @@ -64,6 +64,7 @@
  24329. #include <linux/errqueue.h>
  24330. #include <linux/prefetch.h>
  24331. #include <linux/if_vlan.h>
  24332. +#include <linux/locallock.h>
  24333. #include <net/protocol.h>
  24334. #include <net/dst.h>
  24335. @@ -360,6 +361,8 @@
  24336. static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
  24337. static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
  24338. +static DEFINE_LOCAL_IRQ_LOCK(netdev_alloc_lock);
  24339. +static DEFINE_LOCAL_IRQ_LOCK(napi_alloc_cache_lock);
  24340. static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
  24341. {
  24342. @@ -367,10 +370,10 @@
  24343. unsigned long flags;
  24344. void *data;
  24345. - local_irq_save(flags);
  24346. + local_lock_irqsave(netdev_alloc_lock, flags);
  24347. nc = this_cpu_ptr(&netdev_alloc_cache);
  24348. data = __alloc_page_frag(nc, fragsz, gfp_mask);
  24349. - local_irq_restore(flags);
  24350. + local_unlock_irqrestore(netdev_alloc_lock, flags);
  24351. return data;
  24352. }
  24353. @@ -389,9 +392,13 @@
  24354. static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
  24355. {
  24356. - struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
  24357. + struct napi_alloc_cache *nc;
  24358. + void *data;
  24359. - return __alloc_page_frag(&nc->page, fragsz, gfp_mask);
  24360. + nc = &get_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  24361. + data = __alloc_page_frag(&nc->page, fragsz, gfp_mask);
  24362. + put_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  24363. + return data;
  24364. }
  24365. void *napi_alloc_frag(unsigned int fragsz)
  24366. @@ -438,13 +445,13 @@
  24367. if (sk_memalloc_socks())
  24368. gfp_mask |= __GFP_MEMALLOC;
  24369. - local_irq_save(flags);
  24370. + local_lock_irqsave(netdev_alloc_lock, flags);
  24371. nc = this_cpu_ptr(&netdev_alloc_cache);
  24372. data = __alloc_page_frag(nc, len, gfp_mask);
  24373. pfmemalloc = nc->pfmemalloc;
  24374. - local_irq_restore(flags);
  24375. + local_unlock_irqrestore(netdev_alloc_lock, flags);
  24376. if (unlikely(!data))
  24377. return NULL;
  24378. @@ -485,9 +492,10 @@
  24379. struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
  24380. gfp_t gfp_mask)
  24381. {
  24382. - struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
  24383. + struct napi_alloc_cache *nc;
  24384. struct sk_buff *skb;
  24385. void *data;
  24386. + bool pfmemalloc;
  24387. len += NET_SKB_PAD + NET_IP_ALIGN;
  24388. @@ -505,7 +513,10 @@
  24389. if (sk_memalloc_socks())
  24390. gfp_mask |= __GFP_MEMALLOC;
  24391. + nc = &get_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  24392. data = __alloc_page_frag(&nc->page, len, gfp_mask);
  24393. + pfmemalloc = nc->page.pfmemalloc;
  24394. + put_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  24395. if (unlikely(!data))
  24396. return NULL;
  24397. @@ -516,7 +527,7 @@
  24398. }
  24399. /* use OR instead of assignment to avoid clearing of bits in mask */
  24400. - if (nc->page.pfmemalloc)
  24401. + if (pfmemalloc)
  24402. skb->pfmemalloc = 1;
  24403. skb->head_frag = 1;
  24404. @@ -760,23 +771,26 @@
  24405. void __kfree_skb_flush(void)
  24406. {
  24407. - struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
  24408. + struct napi_alloc_cache *nc;
  24409. + nc = &get_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  24410. /* flush skb_cache if containing objects */
  24411. if (nc->skb_count) {
  24412. kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count,
  24413. nc->skb_cache);
  24414. nc->skb_count = 0;
  24415. }
  24416. + put_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  24417. }
  24418. static inline void _kfree_skb_defer(struct sk_buff *skb)
  24419. {
  24420. - struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
  24421. + struct napi_alloc_cache *nc;
  24422. /* drop skb->head and call any destructors for packet */
  24423. skb_release_all(skb);
  24424. + nc = &get_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  24425. /* record skb to CPU local list */
  24426. nc->skb_cache[nc->skb_count++] = skb;
  24427. @@ -791,6 +805,7 @@
  24428. nc->skb_cache);
  24429. nc->skb_count = 0;
  24430. }
  24431. + put_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  24432. }
  24433. void __kfree_skb_defer(struct sk_buff *skb)
  24434. {
  24435. diff -Nur linux-4.9.28.orig/net/core/sock.c linux-4.9.28/net/core/sock.c
  24436. --- linux-4.9.28.orig/net/core/sock.c 2017-05-14 14:00:37.000000000 +0200
  24437. +++ linux-4.9.28/net/core/sock.c 2017-05-19 03:37:25.218178191 +0200
  24438. @@ -2499,12 +2499,11 @@
  24439. if (sk->sk_lock.owned)
  24440. __lock_sock(sk);
  24441. sk->sk_lock.owned = 1;
  24442. - spin_unlock(&sk->sk_lock.slock);
  24443. + spin_unlock_bh(&sk->sk_lock.slock);
  24444. /*
  24445. * The sk_lock has mutex_lock() semantics here:
  24446. */
  24447. mutex_acquire(&sk->sk_lock.dep_map, subclass, 0, _RET_IP_);
  24448. - local_bh_enable();
  24449. }
  24450. EXPORT_SYMBOL(lock_sock_nested);
  24451. diff -Nur linux-4.9.28.orig/net/ipv4/icmp.c linux-4.9.28/net/ipv4/icmp.c
  24452. --- linux-4.9.28.orig/net/ipv4/icmp.c 2017-05-14 14:00:37.000000000 +0200
  24453. +++ linux-4.9.28/net/ipv4/icmp.c 2017-05-19 03:37:25.218178191 +0200
  24454. @@ -69,6 +69,7 @@
  24455. #include <linux/jiffies.h>
  24456. #include <linux/kernel.h>
  24457. #include <linux/fcntl.h>
  24458. +#include <linux/sysrq.h>
  24459. #include <linux/socket.h>
  24460. #include <linux/in.h>
  24461. #include <linux/inet.h>
  24462. @@ -77,6 +78,7 @@
  24463. #include <linux/string.h>
  24464. #include <linux/netfilter_ipv4.h>
  24465. #include <linux/slab.h>
  24466. +#include <linux/locallock.h>
  24467. #include <net/snmp.h>
  24468. #include <net/ip.h>
  24469. #include <net/route.h>
  24470. @@ -204,6 +206,8 @@
  24471. *
  24472. * On SMP we have one ICMP socket per-cpu.
  24473. */
  24474. +static DEFINE_LOCAL_IRQ_LOCK(icmp_sk_lock);
  24475. +
  24476. static struct sock *icmp_sk(struct net *net)
  24477. {
  24478. return *this_cpu_ptr(net->ipv4.icmp_sk);
  24479. @@ -215,12 +219,14 @@
  24480. local_bh_disable();
  24481. + local_lock(icmp_sk_lock);
  24482. sk = icmp_sk(net);
  24483. if (unlikely(!spin_trylock(&sk->sk_lock.slock))) {
  24484. /* This can happen if the output path signals a
  24485. * dst_link_failure() for an outgoing ICMP packet.
  24486. */
  24487. + local_unlock(icmp_sk_lock);
  24488. local_bh_enable();
  24489. return NULL;
  24490. }
  24491. @@ -230,6 +236,7 @@
  24492. static inline void icmp_xmit_unlock(struct sock *sk)
  24493. {
  24494. spin_unlock_bh(&sk->sk_lock.slock);
  24495. + local_unlock(icmp_sk_lock);
  24496. }
  24497. int sysctl_icmp_msgs_per_sec __read_mostly = 1000;
  24498. @@ -358,6 +365,7 @@
  24499. struct sock *sk;
  24500. struct sk_buff *skb;
  24501. + local_lock(icmp_sk_lock);
  24502. sk = icmp_sk(dev_net((*rt)->dst.dev));
  24503. if (ip_append_data(sk, fl4, icmp_glue_bits, icmp_param,
  24504. icmp_param->data_len+icmp_param->head_len,
  24505. @@ -380,6 +388,7 @@
  24506. skb->ip_summed = CHECKSUM_NONE;
  24507. ip_push_pending_frames(sk, fl4);
  24508. }
  24509. + local_unlock(icmp_sk_lock);
  24510. }
  24511. /*
  24512. @@ -891,6 +900,30 @@
  24513. }
  24514. /*
  24515. + * 32bit and 64bit have different timestamp length, so we check for
  24516. + * the cookie at offset 20 and verify it is repeated at offset 50
  24517. + */
  24518. +#define CO_POS0 20
  24519. +#define CO_POS1 50
  24520. +#define CO_SIZE sizeof(int)
  24521. +#define ICMP_SYSRQ_SIZE 57
  24522. +
  24523. +/*
  24524. + * We got a ICMP_SYSRQ_SIZE sized ping request. Check for the cookie
  24525. + * pattern and if it matches send the next byte as a trigger to sysrq.
  24526. + */
  24527. +static void icmp_check_sysrq(struct net *net, struct sk_buff *skb)
  24528. +{
  24529. + int cookie = htonl(net->ipv4.sysctl_icmp_echo_sysrq);
  24530. + char *p = skb->data;
  24531. +
  24532. + if (!memcmp(&cookie, p + CO_POS0, CO_SIZE) &&
  24533. + !memcmp(&cookie, p + CO_POS1, CO_SIZE) &&
  24534. + p[CO_POS0 + CO_SIZE] == p[CO_POS1 + CO_SIZE])
  24535. + handle_sysrq(p[CO_POS0 + CO_SIZE]);
  24536. +}
  24537. +
  24538. +/*
  24539. * Handle ICMP_ECHO ("ping") requests.
  24540. *
  24541. * RFC 1122: 3.2.2.6 MUST have an echo server that answers ICMP echo
  24542. @@ -917,6 +950,11 @@
  24543. icmp_param.data_len = skb->len;
  24544. icmp_param.head_len = sizeof(struct icmphdr);
  24545. icmp_reply(&icmp_param, skb);
  24546. +
  24547. + if (skb->len == ICMP_SYSRQ_SIZE &&
  24548. + net->ipv4.sysctl_icmp_echo_sysrq) {
  24549. + icmp_check_sysrq(net, skb);
  24550. + }
  24551. }
  24552. /* should there be an ICMP stat for ignored echos? */
  24553. return true;
  24554. diff -Nur linux-4.9.28.orig/net/ipv4/sysctl_net_ipv4.c linux-4.9.28/net/ipv4/sysctl_net_ipv4.c
  24555. --- linux-4.9.28.orig/net/ipv4/sysctl_net_ipv4.c 2017-05-14 14:00:37.000000000 +0200
  24556. +++ linux-4.9.28/net/ipv4/sysctl_net_ipv4.c 2017-05-19 03:37:25.218178191 +0200
  24557. @@ -681,6 +681,13 @@
  24558. .proc_handler = proc_dointvec
  24559. },
  24560. {
  24561. + .procname = "icmp_echo_sysrq",
  24562. + .data = &init_net.ipv4.sysctl_icmp_echo_sysrq,
  24563. + .maxlen = sizeof(int),
  24564. + .mode = 0644,
  24565. + .proc_handler = proc_dointvec
  24566. + },
  24567. + {
  24568. .procname = "icmp_ignore_bogus_error_responses",
  24569. .data = &init_net.ipv4.sysctl_icmp_ignore_bogus_error_responses,
  24570. .maxlen = sizeof(int),
  24571. diff -Nur linux-4.9.28.orig/net/ipv4/tcp_ipv4.c linux-4.9.28/net/ipv4/tcp_ipv4.c
  24572. --- linux-4.9.28.orig/net/ipv4/tcp_ipv4.c 2017-05-14 14:00:37.000000000 +0200
  24573. +++ linux-4.9.28/net/ipv4/tcp_ipv4.c 2017-05-19 03:37:25.218178191 +0200
  24574. @@ -62,6 +62,7 @@
  24575. #include <linux/init.h>
  24576. #include <linux/times.h>
  24577. #include <linux/slab.h>
  24578. +#include <linux/locallock.h>
  24579. #include <net/net_namespace.h>
  24580. #include <net/icmp.h>
  24581. @@ -568,6 +569,7 @@
  24582. }
  24583. EXPORT_SYMBOL(tcp_v4_send_check);
  24584. +static DEFINE_LOCAL_IRQ_LOCK(tcp_sk_lock);
  24585. /*
  24586. * This routine will send an RST to the other tcp.
  24587. *
  24588. @@ -695,6 +697,8 @@
  24589. offsetof(struct inet_timewait_sock, tw_bound_dev_if));
  24590. arg.tos = ip_hdr(skb)->tos;
  24591. +
  24592. + local_lock(tcp_sk_lock);
  24593. local_bh_disable();
  24594. ip_send_unicast_reply(*this_cpu_ptr(net->ipv4.tcp_sk),
  24595. skb, &TCP_SKB_CB(skb)->header.h4.opt,
  24596. @@ -704,6 +708,7 @@
  24597. __TCP_INC_STATS(net, TCP_MIB_OUTSEGS);
  24598. __TCP_INC_STATS(net, TCP_MIB_OUTRSTS);
  24599. local_bh_enable();
  24600. + local_unlock(tcp_sk_lock);
  24601. #ifdef CONFIG_TCP_MD5SIG
  24602. out:
  24603. @@ -779,6 +784,7 @@
  24604. if (oif)
  24605. arg.bound_dev_if = oif;
  24606. arg.tos = tos;
  24607. + local_lock(tcp_sk_lock);
  24608. local_bh_disable();
  24609. ip_send_unicast_reply(*this_cpu_ptr(net->ipv4.tcp_sk),
  24610. skb, &TCP_SKB_CB(skb)->header.h4.opt,
  24611. @@ -787,6 +793,7 @@
  24612. __TCP_INC_STATS(net, TCP_MIB_OUTSEGS);
  24613. local_bh_enable();
  24614. + local_unlock(tcp_sk_lock);
  24615. }
  24616. static void tcp_v4_timewait_ack(struct sock *sk, struct sk_buff *skb)
  24617. diff -Nur linux-4.9.28.orig/net/mac80211/rx.c linux-4.9.28/net/mac80211/rx.c
  24618. --- linux-4.9.28.orig/net/mac80211/rx.c 2017-05-14 14:00:37.000000000 +0200
  24619. +++ linux-4.9.28/net/mac80211/rx.c 2017-05-19 03:37:25.218178191 +0200
  24620. @@ -4230,7 +4230,7 @@
  24621. struct ieee80211_supported_band *sband;
  24622. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  24623. - WARN_ON_ONCE(softirq_count() == 0);
  24624. + WARN_ON_ONCE_NONRT(softirq_count() == 0);
  24625. if (WARN_ON(status->band >= NUM_NL80211_BANDS))
  24626. goto drop;
  24627. diff -Nur linux-4.9.28.orig/net/netfilter/core.c linux-4.9.28/net/netfilter/core.c
  24628. --- linux-4.9.28.orig/net/netfilter/core.c 2017-05-14 14:00:37.000000000 +0200
  24629. +++ linux-4.9.28/net/netfilter/core.c 2017-05-19 03:37:25.218178191 +0200
  24630. @@ -22,12 +22,18 @@
  24631. #include <linux/proc_fs.h>
  24632. #include <linux/mutex.h>
  24633. #include <linux/slab.h>
  24634. +#include <linux/locallock.h>
  24635. #include <linux/rcupdate.h>
  24636. #include <net/net_namespace.h>
  24637. #include <net/sock.h>
  24638. #include "nf_internals.h"
  24639. +#ifdef CONFIG_PREEMPT_RT_BASE
  24640. +DEFINE_LOCAL_IRQ_LOCK(xt_write_lock);
  24641. +EXPORT_PER_CPU_SYMBOL(xt_write_lock);
  24642. +#endif
  24643. +
  24644. static DEFINE_MUTEX(afinfo_mutex);
  24645. const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO] __read_mostly;
  24646. diff -Nur linux-4.9.28.orig/net/packet/af_packet.c linux-4.9.28/net/packet/af_packet.c
  24647. --- linux-4.9.28.orig/net/packet/af_packet.c 2017-05-14 14:00:37.000000000 +0200
  24648. +++ linux-4.9.28/net/packet/af_packet.c 2017-05-19 03:37:25.218178191 +0200
  24649. @@ -63,6 +63,7 @@
  24650. #include <linux/if_packet.h>
  24651. #include <linux/wireless.h>
  24652. #include <linux/kernel.h>
  24653. +#include <linux/delay.h>
  24654. #include <linux/kmod.h>
  24655. #include <linux/slab.h>
  24656. #include <linux/vmalloc.h>
  24657. @@ -694,7 +695,7 @@
  24658. if (BLOCK_NUM_PKTS(pbd)) {
  24659. while (atomic_read(&pkc->blk_fill_in_prog)) {
  24660. /* Waiting for skb_copy_bits to finish... */
  24661. - cpu_relax();
  24662. + cpu_chill();
  24663. }
  24664. }
  24665. @@ -956,7 +957,7 @@
  24666. if (!(status & TP_STATUS_BLK_TMO)) {
  24667. while (atomic_read(&pkc->blk_fill_in_prog)) {
  24668. /* Waiting for skb_copy_bits to finish... */
  24669. - cpu_relax();
  24670. + cpu_chill();
  24671. }
  24672. }
  24673. prb_close_block(pkc, pbd, po, status);
  24674. diff -Nur linux-4.9.28.orig/net/rds/ib_rdma.c linux-4.9.28/net/rds/ib_rdma.c
  24675. --- linux-4.9.28.orig/net/rds/ib_rdma.c 2017-05-14 14:00:37.000000000 +0200
  24676. +++ linux-4.9.28/net/rds/ib_rdma.c 2017-05-19 03:37:25.218178191 +0200
  24677. @@ -34,6 +34,7 @@
  24678. #include <linux/slab.h>
  24679. #include <linux/rculist.h>
  24680. #include <linux/llist.h>
  24681. +#include <linux/delay.h>
  24682. #include "rds_single_path.h"
  24683. #include "ib_mr.h"
  24684. @@ -210,7 +211,7 @@
  24685. for_each_online_cpu(cpu) {
  24686. flag = &per_cpu(clean_list_grace, cpu);
  24687. while (test_bit(CLEAN_LIST_BUSY_BIT, flag))
  24688. - cpu_relax();
  24689. + cpu_chill();
  24690. }
  24691. }
  24692. diff -Nur linux-4.9.28.orig/net/rxrpc/security.c linux-4.9.28/net/rxrpc/security.c
  24693. --- linux-4.9.28.orig/net/rxrpc/security.c 2017-05-14 14:00:37.000000000 +0200
  24694. +++ linux-4.9.28/net/rxrpc/security.c 2017-05-19 03:37:25.218178191 +0200
  24695. @@ -19,9 +19,6 @@
  24696. #include <keys/rxrpc-type.h>
  24697. #include "ar-internal.h"
  24698. -static LIST_HEAD(rxrpc_security_methods);
  24699. -static DECLARE_RWSEM(rxrpc_security_sem);
  24700. -
  24701. static const struct rxrpc_security *rxrpc_security_types[] = {
  24702. [RXRPC_SECURITY_NONE] = &rxrpc_no_security,
  24703. #ifdef CONFIG_RXKAD
  24704. diff -Nur linux-4.9.28.orig/net/sched/sch_api.c linux-4.9.28/net/sched/sch_api.c
  24705. --- linux-4.9.28.orig/net/sched/sch_api.c 2017-05-14 14:00:37.000000000 +0200
  24706. +++ linux-4.9.28/net/sched/sch_api.c 2017-05-19 03:37:25.218178191 +0200
  24707. @@ -981,7 +981,7 @@
  24708. rcu_assign_pointer(sch->stab, stab);
  24709. }
  24710. if (tca[TCA_RATE]) {
  24711. - seqcount_t *running;
  24712. + net_seqlock_t *running;
  24713. err = -EOPNOTSUPP;
  24714. if (sch->flags & TCQ_F_MQROOT)
  24715. diff -Nur linux-4.9.28.orig/net/sched/sch_generic.c linux-4.9.28/net/sched/sch_generic.c
  24716. --- linux-4.9.28.orig/net/sched/sch_generic.c 2017-05-14 14:00:37.000000000 +0200
  24717. +++ linux-4.9.28/net/sched/sch_generic.c 2017-05-19 03:37:25.218178191 +0200
  24718. @@ -425,7 +425,11 @@
  24719. .ops = &noop_qdisc_ops,
  24720. .q.lock = __SPIN_LOCK_UNLOCKED(noop_qdisc.q.lock),
  24721. .dev_queue = &noop_netdev_queue,
  24722. +#ifdef CONFIG_PREEMPT_RT_BASE
  24723. + .running = __SEQLOCK_UNLOCKED(noop_qdisc.running),
  24724. +#else
  24725. .running = SEQCNT_ZERO(noop_qdisc.running),
  24726. +#endif
  24727. .busylock = __SPIN_LOCK_UNLOCKED(noop_qdisc.busylock),
  24728. };
  24729. EXPORT_SYMBOL(noop_qdisc);
  24730. @@ -624,9 +628,17 @@
  24731. lockdep_set_class(&sch->busylock,
  24732. dev->qdisc_tx_busylock ?: &qdisc_tx_busylock);
  24733. +#ifdef CONFIG_PREEMPT_RT_BASE
  24734. + seqlock_init(&sch->running);
  24735. + lockdep_set_class(&sch->running.seqcount,
  24736. + dev->qdisc_running_key ?: &qdisc_running_key);
  24737. + lockdep_set_class(&sch->running.lock,
  24738. + dev->qdisc_running_key ?: &qdisc_running_key);
  24739. +#else
  24740. seqcount_init(&sch->running);
  24741. lockdep_set_class(&sch->running,
  24742. dev->qdisc_running_key ?: &qdisc_running_key);
  24743. +#endif
  24744. sch->ops = ops;
  24745. sch->enqueue = ops->enqueue;
  24746. @@ -925,7 +937,7 @@
  24747. /* Wait for outstanding qdisc_run calls. */
  24748. list_for_each_entry(dev, head, close_list)
  24749. while (some_qdisc_is_busy(dev))
  24750. - yield();
  24751. + msleep(1);
  24752. }
  24753. void dev_deactivate(struct net_device *dev)
  24754. diff -Nur linux-4.9.28.orig/net/sunrpc/svc_xprt.c linux-4.9.28/net/sunrpc/svc_xprt.c
  24755. --- linux-4.9.28.orig/net/sunrpc/svc_xprt.c 2017-05-14 14:00:37.000000000 +0200
  24756. +++ linux-4.9.28/net/sunrpc/svc_xprt.c 2017-05-19 03:37:25.218178191 +0200
  24757. @@ -396,7 +396,7 @@
  24758. goto out;
  24759. }
  24760. - cpu = get_cpu();
  24761. + cpu = get_cpu_light();
  24762. pool = svc_pool_for_cpu(xprt->xpt_server, cpu);
  24763. atomic_long_inc(&pool->sp_stats.packets);
  24764. @@ -432,7 +432,7 @@
  24765. atomic_long_inc(&pool->sp_stats.threads_woken);
  24766. wake_up_process(rqstp->rq_task);
  24767. - put_cpu();
  24768. + put_cpu_light();
  24769. goto out;
  24770. }
  24771. rcu_read_unlock();
  24772. @@ -453,7 +453,7 @@
  24773. goto redo_search;
  24774. }
  24775. rqstp = NULL;
  24776. - put_cpu();
  24777. + put_cpu_light();
  24778. out:
  24779. trace_svc_xprt_do_enqueue(xprt, rqstp);
  24780. }
  24781. diff -Nur linux-4.9.28.orig/scripts/mkcompile_h linux-4.9.28/scripts/mkcompile_h
  24782. --- linux-4.9.28.orig/scripts/mkcompile_h 2017-05-14 14:00:37.000000000 +0200
  24783. +++ linux-4.9.28/scripts/mkcompile_h 2017-05-19 03:37:25.218178191 +0200
  24784. @@ -4,7 +4,8 @@
  24785. ARCH=$2
  24786. SMP=$3
  24787. PREEMPT=$4
  24788. -CC=$5
  24789. +RT=$5
  24790. +CC=$6
  24791. vecho() { [ "${quiet}" = "silent_" ] || echo "$@" ; }
  24792. @@ -57,6 +58,7 @@
  24793. CONFIG_FLAGS=""
  24794. if [ -n "$SMP" ] ; then CONFIG_FLAGS="SMP"; fi
  24795. if [ -n "$PREEMPT" ] ; then CONFIG_FLAGS="$CONFIG_FLAGS PREEMPT"; fi
  24796. +if [ -n "$RT" ] ; then CONFIG_FLAGS="$CONFIG_FLAGS RT"; fi
  24797. UTS_VERSION="$UTS_VERSION $CONFIG_FLAGS $TIMESTAMP"
  24798. # Truncate to maximum length
  24799. diff -Nur linux-4.9.28.orig/sound/core/pcm_native.c linux-4.9.28/sound/core/pcm_native.c
  24800. --- linux-4.9.28.orig/sound/core/pcm_native.c 2017-05-14 14:00:37.000000000 +0200
  24801. +++ linux-4.9.28/sound/core/pcm_native.c 2017-05-19 03:37:25.222178345 +0200
  24802. @@ -135,7 +135,7 @@
  24803. void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
  24804. {
  24805. if (!substream->pcm->nonatomic)
  24806. - local_irq_disable();
  24807. + local_irq_disable_nort();
  24808. snd_pcm_stream_lock(substream);
  24809. }
  24810. EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
  24811. @@ -150,7 +150,7 @@
  24812. {
  24813. snd_pcm_stream_unlock(substream);
  24814. if (!substream->pcm->nonatomic)
  24815. - local_irq_enable();
  24816. + local_irq_enable_nort();
  24817. }
  24818. EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
  24819. @@ -158,7 +158,7 @@
  24820. {
  24821. unsigned long flags = 0;
  24822. if (!substream->pcm->nonatomic)
  24823. - local_irq_save(flags);
  24824. + local_irq_save_nort(flags);
  24825. snd_pcm_stream_lock(substream);
  24826. return flags;
  24827. }
  24828. @@ -176,7 +176,7 @@
  24829. {
  24830. snd_pcm_stream_unlock(substream);
  24831. if (!substream->pcm->nonatomic)
  24832. - local_irq_restore(flags);
  24833. + local_irq_restore_nort(flags);
  24834. }
  24835. EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);