patch-realtime 880 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965
  1. diff -Nur linux-4.4.46.orig/arch/arm/include/asm/switch_to.h linux-4.4.46/arch/arm/include/asm/switch_to.h
  2. --- linux-4.4.46.orig/arch/arm/include/asm/switch_to.h 2017-02-01 08:31:11.000000000 +0100
  3. +++ linux-4.4.46/arch/arm/include/asm/switch_to.h 2017-02-03 17:18:05.627414322 +0100
  4. @@ -3,6 +3,13 @@
  5. #include <linux/thread_info.h>
  6. +#if defined CONFIG_PREEMPT_RT_FULL && defined CONFIG_HIGHMEM
  7. +void switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p);
  8. +#else
  9. +static inline void
  10. +switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p) { }
  11. +#endif
  12. +
  13. /*
  14. * For v7 SMP cores running a preemptible kernel we may be pre-empted
  15. * during a TLB maintenance operation, so execute an inner-shareable dsb
  16. @@ -25,6 +32,7 @@
  17. #define switch_to(prev,next,last) \
  18. do { \
  19. __complete_pending_tlbi(); \
  20. + switch_kmaps(prev, next); \
  21. last = __switch_to(prev,task_thread_info(prev), task_thread_info(next)); \
  22. } while (0)
  23. diff -Nur linux-4.4.46.orig/arch/arm/include/asm/thread_info.h linux-4.4.46/arch/arm/include/asm/thread_info.h
  24. --- linux-4.4.46.orig/arch/arm/include/asm/thread_info.h 2017-02-01 08:31:11.000000000 +0100
  25. +++ linux-4.4.46/arch/arm/include/asm/thread_info.h 2017-02-03 17:18:05.627414322 +0100
  26. @@ -49,6 +49,7 @@
  27. struct thread_info {
  28. unsigned long flags; /* low level flags */
  29. int preempt_count; /* 0 => preemptable, <0 => bug */
  30. + int preempt_lazy_count; /* 0 => preemptable, <0 => bug */
  31. mm_segment_t addr_limit; /* address limit */
  32. struct task_struct *task; /* main task structure */
  33. __u32 cpu; /* cpu */
  34. @@ -142,7 +143,8 @@
  35. #define TIF_SYSCALL_TRACE 4 /* syscall trace active */
  36. #define TIF_SYSCALL_AUDIT 5 /* syscall auditing active */
  37. #define TIF_SYSCALL_TRACEPOINT 6 /* syscall tracepoint instrumentation */
  38. -#define TIF_SECCOMP 7 /* seccomp syscall filtering active */
  39. +#define TIF_SECCOMP 8 /* seccomp syscall filtering active */
  40. +#define TIF_NEED_RESCHED_LAZY 7
  41. #define TIF_NOHZ 12 /* in adaptive nohz mode */
  42. #define TIF_USING_IWMMXT 17
  43. @@ -152,6 +154,7 @@
  44. #define _TIF_SIGPENDING (1 << TIF_SIGPENDING)
  45. #define _TIF_NEED_RESCHED (1 << TIF_NEED_RESCHED)
  46. #define _TIF_NOTIFY_RESUME (1 << TIF_NOTIFY_RESUME)
  47. +#define _TIF_NEED_RESCHED_LAZY (1 << TIF_NEED_RESCHED_LAZY)
  48. #define _TIF_UPROBE (1 << TIF_UPROBE)
  49. #define _TIF_SYSCALL_TRACE (1 << TIF_SYSCALL_TRACE)
  50. #define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
  51. @@ -167,7 +170,8 @@
  52. * Change these and you break ASM code in entry-common.S
  53. */
  54. #define _TIF_WORK_MASK (_TIF_NEED_RESCHED | _TIF_SIGPENDING | \
  55. - _TIF_NOTIFY_RESUME | _TIF_UPROBE)
  56. + _TIF_NOTIFY_RESUME | _TIF_UPROBE | \
  57. + _TIF_NEED_RESCHED_LAZY)
  58. #endif /* __KERNEL__ */
  59. #endif /* __ASM_ARM_THREAD_INFO_H */
  60. diff -Nur linux-4.4.46.orig/arch/arm/Kconfig linux-4.4.46/arch/arm/Kconfig
  61. --- linux-4.4.46.orig/arch/arm/Kconfig 2017-02-01 08:31:11.000000000 +0100
  62. +++ linux-4.4.46/arch/arm/Kconfig 2017-02-03 17:18:05.627414322 +0100
  63. @@ -33,7 +33,7 @@
  64. select HARDIRQS_SW_RESEND
  65. select HAVE_ARCH_AUDITSYSCALL if (AEABI && !OABI_COMPAT)
  66. select HAVE_ARCH_BITREVERSE if (CPU_32v7M || CPU_32v7) && !CPU_32v6
  67. - select HAVE_ARCH_JUMP_LABEL if !XIP_KERNEL && !CPU_ENDIAN_BE32
  68. + select HAVE_ARCH_JUMP_LABEL if !XIP_KERNEL && !CPU_ENDIAN_BE32 && !PREEMPT_RT_BASE
  69. select HAVE_ARCH_KGDB if !CPU_ENDIAN_BE32
  70. select HAVE_ARCH_SECCOMP_FILTER if (AEABI && !OABI_COMPAT)
  71. select HAVE_ARCH_TRACEHOOK
  72. @@ -68,6 +68,7 @@
  73. select HAVE_PERF_EVENTS
  74. select HAVE_PERF_REGS
  75. select HAVE_PERF_USER_STACK_DUMP
  76. + select HAVE_PREEMPT_LAZY
  77. select HAVE_RCU_TABLE_FREE if (SMP && ARM_LPAE)
  78. select HAVE_REGS_AND_STACK_ACCESS_API
  79. select HAVE_SYSCALL_TRACEPOINTS
  80. diff -Nur linux-4.4.46.orig/arch/arm/kernel/asm-offsets.c linux-4.4.46/arch/arm/kernel/asm-offsets.c
  81. --- linux-4.4.46.orig/arch/arm/kernel/asm-offsets.c 2017-02-01 08:31:11.000000000 +0100
  82. +++ linux-4.4.46/arch/arm/kernel/asm-offsets.c 2017-02-03 17:18:05.627414322 +0100
  83. @@ -65,6 +65,7 @@
  84. BLANK();
  85. DEFINE(TI_FLAGS, offsetof(struct thread_info, flags));
  86. DEFINE(TI_PREEMPT, offsetof(struct thread_info, preempt_count));
  87. + DEFINE(TI_PREEMPT_LAZY, offsetof(struct thread_info, preempt_lazy_count));
  88. DEFINE(TI_ADDR_LIMIT, offsetof(struct thread_info, addr_limit));
  89. DEFINE(TI_TASK, offsetof(struct thread_info, task));
  90. DEFINE(TI_CPU, offsetof(struct thread_info, cpu));
  91. diff -Nur linux-4.4.46.orig/arch/arm/kernel/entry-armv.S linux-4.4.46/arch/arm/kernel/entry-armv.S
  92. --- linux-4.4.46.orig/arch/arm/kernel/entry-armv.S 2017-02-01 08:31:11.000000000 +0100
  93. +++ linux-4.4.46/arch/arm/kernel/entry-armv.S 2017-02-03 17:18:05.627414322 +0100
  94. @@ -215,11 +215,18 @@
  95. #ifdef CONFIG_PREEMPT
  96. get_thread_info tsk
  97. ldr r8, [tsk, #TI_PREEMPT] @ get preempt count
  98. - ldr r0, [tsk, #TI_FLAGS] @ get flags
  99. teq r8, #0 @ if preempt count != 0
  100. + bne 1f @ return from exeption
  101. + ldr r0, [tsk, #TI_FLAGS] @ get flags
  102. + tst r0, #_TIF_NEED_RESCHED @ if NEED_RESCHED is set
  103. + blne svc_preempt @ preempt!
  104. +
  105. + ldr r8, [tsk, #TI_PREEMPT_LAZY] @ get preempt lazy count
  106. + teq r8, #0 @ if preempt lazy count != 0
  107. movne r0, #0 @ force flags to 0
  108. - tst r0, #_TIF_NEED_RESCHED
  109. + tst r0, #_TIF_NEED_RESCHED_LAZY
  110. blne svc_preempt
  111. +1:
  112. #endif
  113. svc_exit r5, irq = 1 @ return from exception
  114. @@ -234,8 +241,14 @@
  115. 1: bl preempt_schedule_irq @ irq en/disable is done inside
  116. ldr r0, [tsk, #TI_FLAGS] @ get new tasks TI_FLAGS
  117. tst r0, #_TIF_NEED_RESCHED
  118. + bne 1b
  119. + tst r0, #_TIF_NEED_RESCHED_LAZY
  120. reteq r8 @ go again
  121. - b 1b
  122. + ldr r0, [tsk, #TI_PREEMPT_LAZY] @ get preempt lazy count
  123. + teq r0, #0 @ if preempt lazy count != 0
  124. + beq 1b
  125. + ret r8 @ go again
  126. +
  127. #endif
  128. __und_fault:
  129. diff -Nur linux-4.4.46.orig/arch/arm/kernel/entry-common.S linux-4.4.46/arch/arm/kernel/entry-common.S
  130. --- linux-4.4.46.orig/arch/arm/kernel/entry-common.S 2017-02-01 08:31:11.000000000 +0100
  131. +++ linux-4.4.46/arch/arm/kernel/entry-common.S 2017-02-03 17:18:05.627414322 +0100
  132. @@ -36,7 +36,9 @@
  133. UNWIND(.cantunwind )
  134. disable_irq_notrace @ disable interrupts
  135. ldr r1, [tsk, #TI_FLAGS] @ re-check for syscall tracing
  136. - tst r1, #_TIF_SYSCALL_WORK | _TIF_WORK_MASK
  137. + tst r1, #((_TIF_SYSCALL_WORK | _TIF_WORK_MASK) & ~_TIF_SECCOMP)
  138. + bne fast_work_pending
  139. + tst r1, #_TIF_SECCOMP
  140. bne fast_work_pending
  141. /* perform architecture specific actions before user return */
  142. @@ -62,8 +64,11 @@
  143. str r0, [sp, #S_R0 + S_OFF]! @ save returned r0
  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 do_slower_path
  149. + tst r1, #_TIF_SECCOMP
  150. beq no_work_pending
  151. +do_slower_path:
  152. UNWIND(.fnend )
  153. ENDPROC(ret_fast_syscall)
  154. diff -Nur linux-4.4.46.orig/arch/arm/kernel/process.c linux-4.4.46/arch/arm/kernel/process.c
  155. --- linux-4.4.46.orig/arch/arm/kernel/process.c 2017-02-01 08:31:11.000000000 +0100
  156. +++ linux-4.4.46/arch/arm/kernel/process.c 2017-02-03 17:18:05.627414322 +0100
  157. @@ -319,6 +319,30 @@
  158. }
  159. #ifdef CONFIG_MMU
  160. +/*
  161. + * CONFIG_SPLIT_PTLOCK_CPUS results in a page->ptl lock. If the lock is not
  162. + * initialized by pgtable_page_ctor() then a coredump of the vector page will
  163. + * fail.
  164. + */
  165. +static int __init vectors_user_mapping_init_page(void)
  166. +{
  167. + struct page *page;
  168. + unsigned long addr = 0xffff0000;
  169. + pgd_t *pgd;
  170. + pud_t *pud;
  171. + pmd_t *pmd;
  172. +
  173. + pgd = pgd_offset_k(addr);
  174. + pud = pud_offset(pgd, addr);
  175. + pmd = pmd_offset(pud, addr);
  176. + page = pmd_page(*(pmd));
  177. +
  178. + pgtable_page_ctor(page);
  179. +
  180. + return 0;
  181. +}
  182. +late_initcall(vectors_user_mapping_init_page);
  183. +
  184. #ifdef CONFIG_KUSER_HELPERS
  185. /*
  186. * The vectors page is always readable from user space for the
  187. diff -Nur linux-4.4.46.orig/arch/arm/kernel/signal.c linux-4.4.46/arch/arm/kernel/signal.c
  188. --- linux-4.4.46.orig/arch/arm/kernel/signal.c 2017-02-01 08:31:11.000000000 +0100
  189. +++ linux-4.4.46/arch/arm/kernel/signal.c 2017-02-03 17:18:05.627414322 +0100
  190. @@ -572,7 +572,8 @@
  191. */
  192. trace_hardirqs_off();
  193. do {
  194. - if (likely(thread_flags & _TIF_NEED_RESCHED)) {
  195. + if (likely(thread_flags & (_TIF_NEED_RESCHED |
  196. + _TIF_NEED_RESCHED_LAZY))) {
  197. schedule();
  198. } else {
  199. if (unlikely(!user_mode(regs)))
  200. diff -Nur linux-4.4.46.orig/arch/arm/kernel/smp.c linux-4.4.46/arch/arm/kernel/smp.c
  201. --- linux-4.4.46.orig/arch/arm/kernel/smp.c 2017-02-01 08:31:11.000000000 +0100
  202. +++ linux-4.4.46/arch/arm/kernel/smp.c 2017-02-03 17:18:05.627414322 +0100
  203. @@ -230,8 +230,6 @@
  204. flush_cache_louis();
  205. local_flush_tlb_all();
  206. - clear_tasks_mm_cpumask(cpu);
  207. -
  208. return 0;
  209. }
  210. @@ -247,6 +245,9 @@
  211. pr_err("CPU%u: cpu didn't die\n", cpu);
  212. return;
  213. }
  214. +
  215. + clear_tasks_mm_cpumask(cpu);
  216. +
  217. pr_notice("CPU%u: shutdown\n", cpu);
  218. /*
  219. diff -Nur linux-4.4.46.orig/arch/arm/kernel/unwind.c linux-4.4.46/arch/arm/kernel/unwind.c
  220. --- linux-4.4.46.orig/arch/arm/kernel/unwind.c 2017-02-01 08:31:11.000000000 +0100
  221. +++ linux-4.4.46/arch/arm/kernel/unwind.c 2017-02-03 17:18:05.627414322 +0100
  222. @@ -93,7 +93,7 @@
  223. static const struct unwind_idx *__origin_unwind_idx;
  224. extern const struct unwind_idx __stop_unwind_idx[];
  225. -static DEFINE_SPINLOCK(unwind_lock);
  226. +static DEFINE_RAW_SPINLOCK(unwind_lock);
  227. static LIST_HEAD(unwind_tables);
  228. /* Convert a prel31 symbol to an absolute address */
  229. @@ -201,7 +201,7 @@
  230. /* module unwind tables */
  231. struct unwind_table *table;
  232. - spin_lock_irqsave(&unwind_lock, flags);
  233. + raw_spin_lock_irqsave(&unwind_lock, flags);
  234. list_for_each_entry(table, &unwind_tables, list) {
  235. if (addr >= table->begin_addr &&
  236. addr < table->end_addr) {
  237. @@ -213,7 +213,7 @@
  238. break;
  239. }
  240. }
  241. - spin_unlock_irqrestore(&unwind_lock, flags);
  242. + raw_spin_unlock_irqrestore(&unwind_lock, flags);
  243. }
  244. pr_debug("%s: idx = %p\n", __func__, idx);
  245. @@ -529,9 +529,9 @@
  246. tab->begin_addr = text_addr;
  247. tab->end_addr = text_addr + text_size;
  248. - spin_lock_irqsave(&unwind_lock, flags);
  249. + raw_spin_lock_irqsave(&unwind_lock, flags);
  250. list_add_tail(&tab->list, &unwind_tables);
  251. - spin_unlock_irqrestore(&unwind_lock, flags);
  252. + raw_spin_unlock_irqrestore(&unwind_lock, flags);
  253. return tab;
  254. }
  255. @@ -543,9 +543,9 @@
  256. if (!tab)
  257. return;
  258. - spin_lock_irqsave(&unwind_lock, flags);
  259. + raw_spin_lock_irqsave(&unwind_lock, flags);
  260. list_del(&tab->list);
  261. - spin_unlock_irqrestore(&unwind_lock, flags);
  262. + raw_spin_unlock_irqrestore(&unwind_lock, flags);
  263. kfree(tab);
  264. }
  265. diff -Nur linux-4.4.46.orig/arch/arm/kvm/arm.c linux-4.4.46/arch/arm/kvm/arm.c
  266. --- linux-4.4.46.orig/arch/arm/kvm/arm.c 2017-02-01 08:31:11.000000000 +0100
  267. +++ linux-4.4.46/arch/arm/kvm/arm.c 2017-02-03 17:18:05.627414322 +0100
  268. @@ -496,18 +496,18 @@
  269. struct kvm_vcpu *vcpu;
  270. kvm_for_each_vcpu(i, vcpu, kvm) {
  271. - wait_queue_head_t *wq = kvm_arch_vcpu_wq(vcpu);
  272. + struct swait_queue_head *wq = kvm_arch_vcpu_wq(vcpu);
  273. vcpu->arch.pause = false;
  274. - wake_up_interruptible(wq);
  275. + swake_up(wq);
  276. }
  277. }
  278. static void vcpu_sleep(struct kvm_vcpu *vcpu)
  279. {
  280. - wait_queue_head_t *wq = kvm_arch_vcpu_wq(vcpu);
  281. + struct swait_queue_head *wq = kvm_arch_vcpu_wq(vcpu);
  282. - wait_event_interruptible(*wq, ((!vcpu->arch.power_off) &&
  283. + swait_event_interruptible(*wq, ((!vcpu->arch.power_off) &&
  284. (!vcpu->arch.pause)));
  285. }
  286. @@ -566,7 +566,7 @@
  287. * involves poking the GIC, which must be done in a
  288. * non-preemptible context.
  289. */
  290. - preempt_disable();
  291. + migrate_disable();
  292. kvm_timer_flush_hwstate(vcpu);
  293. kvm_vgic_flush_hwstate(vcpu);
  294. @@ -585,7 +585,7 @@
  295. local_irq_enable();
  296. kvm_timer_sync_hwstate(vcpu);
  297. kvm_vgic_sync_hwstate(vcpu);
  298. - preempt_enable();
  299. + migrate_enable();
  300. continue;
  301. }
  302. @@ -639,7 +639,7 @@
  303. kvm_vgic_sync_hwstate(vcpu);
  304. - preempt_enable();
  305. + migrate_enable();
  306. ret = handle_exit(vcpu, run, ret);
  307. }
  308. diff -Nur linux-4.4.46.orig/arch/arm/kvm/psci.c linux-4.4.46/arch/arm/kvm/psci.c
  309. --- linux-4.4.46.orig/arch/arm/kvm/psci.c 2017-02-01 08:31:11.000000000 +0100
  310. +++ linux-4.4.46/arch/arm/kvm/psci.c 2017-02-03 17:18:05.627414322 +0100
  311. @@ -70,7 +70,7 @@
  312. {
  313. struct kvm *kvm = source_vcpu->kvm;
  314. struct kvm_vcpu *vcpu = NULL;
  315. - wait_queue_head_t *wq;
  316. + struct swait_queue_head *wq;
  317. unsigned long cpu_id;
  318. unsigned long context_id;
  319. phys_addr_t target_pc;
  320. @@ -119,7 +119,7 @@
  321. smp_mb(); /* Make sure the above is visible */
  322. wq = kvm_arch_vcpu_wq(vcpu);
  323. - wake_up_interruptible(wq);
  324. + swake_up(wq);
  325. return PSCI_RET_SUCCESS;
  326. }
  327. diff -Nur linux-4.4.46.orig/arch/arm/mach-at91/at91rm9200.c linux-4.4.46/arch/arm/mach-at91/at91rm9200.c
  328. --- linux-4.4.46.orig/arch/arm/mach-at91/at91rm9200.c 2017-02-01 08:31:11.000000000 +0100
  329. +++ linux-4.4.46/arch/arm/mach-at91/at91rm9200.c 2017-02-03 17:18:05.627414322 +0100
  330. @@ -12,7 +12,6 @@
  331. #include <linux/of_platform.h>
  332. #include <asm/mach/arch.h>
  333. -#include <asm/system_misc.h>
  334. #include "generic.h"
  335. #include "soc.h"
  336. @@ -33,7 +32,6 @@
  337. of_platform_populate(NULL, of_default_bus_match_table, NULL, soc_dev);
  338. - arm_pm_idle = at91rm9200_idle;
  339. at91rm9200_pm_init();
  340. }
  341. diff -Nur linux-4.4.46.orig/arch/arm/mach-at91/at91sam9.c linux-4.4.46/arch/arm/mach-at91/at91sam9.c
  342. --- linux-4.4.46.orig/arch/arm/mach-at91/at91sam9.c 2017-02-01 08:31:11.000000000 +0100
  343. +++ linux-4.4.46/arch/arm/mach-at91/at91sam9.c 2017-02-03 17:18:05.627414322 +0100
  344. @@ -62,8 +62,6 @@
  345. soc_dev = soc_device_to_device(soc);
  346. of_platform_populate(NULL, of_default_bus_match_table, NULL, soc_dev);
  347. -
  348. - arm_pm_idle = at91sam9_idle;
  349. }
  350. static void __init at91sam9_dt_device_init(void)
  351. diff -Nur linux-4.4.46.orig/arch/arm/mach-at91/generic.h linux-4.4.46/arch/arm/mach-at91/generic.h
  352. --- linux-4.4.46.orig/arch/arm/mach-at91/generic.h 2017-02-01 08:31:11.000000000 +0100
  353. +++ linux-4.4.46/arch/arm/mach-at91/generic.h 2017-02-03 17:18:05.627414322 +0100
  354. @@ -11,27 +11,18 @@
  355. #ifndef _AT91_GENERIC_H
  356. #define _AT91_GENERIC_H
  357. -#include <linux/of.h>
  358. -#include <linux/reboot.h>
  359. -
  360. - /* Map io */
  361. -extern void __init at91_map_io(void);
  362. -extern void __init at91_alt_map_io(void);
  363. -
  364. -/* idle */
  365. -extern void at91rm9200_idle(void);
  366. -extern void at91sam9_idle(void);
  367. -
  368. #ifdef CONFIG_PM
  369. extern void __init at91rm9200_pm_init(void);
  370. extern void __init at91sam9260_pm_init(void);
  371. extern void __init at91sam9g45_pm_init(void);
  372. extern void __init at91sam9x5_pm_init(void);
  373. +extern void __init sama5_pm_init(void);
  374. #else
  375. static inline void __init at91rm9200_pm_init(void) { }
  376. static inline void __init at91sam9260_pm_init(void) { }
  377. static inline void __init at91sam9g45_pm_init(void) { }
  378. static inline void __init at91sam9x5_pm_init(void) { }
  379. +static inline void __init sama5_pm_init(void) { }
  380. #endif
  381. #endif /* _AT91_GENERIC_H */
  382. diff -Nur linux-4.4.46.orig/arch/arm/mach-at91/Kconfig linux-4.4.46/arch/arm/mach-at91/Kconfig
  383. --- linux-4.4.46.orig/arch/arm/mach-at91/Kconfig 2017-02-01 08:31:11.000000000 +0100
  384. +++ linux-4.4.46/arch/arm/mach-at91/Kconfig 2017-02-03 17:18:05.627414322 +0100
  385. @@ -99,6 +99,7 @@
  386. config COMMON_CLK_AT91
  387. bool
  388. select COMMON_CLK
  389. + select MFD_SYSCON
  390. config HAVE_AT91_SMD
  391. bool
  392. diff -Nur linux-4.4.46.orig/arch/arm/mach-at91/pm.c linux-4.4.46/arch/arm/mach-at91/pm.c
  393. --- linux-4.4.46.orig/arch/arm/mach-at91/pm.c 2017-02-01 08:31:11.000000000 +0100
  394. +++ linux-4.4.46/arch/arm/mach-at91/pm.c 2017-02-03 17:18:05.627414322 +0100
  395. @@ -31,10 +31,13 @@
  396. #include <asm/mach/irq.h>
  397. #include <asm/fncpy.h>
  398. #include <asm/cacheflush.h>
  399. +#include <asm/system_misc.h>
  400. #include "generic.h"
  401. #include "pm.h"
  402. +static void __iomem *pmc;
  403. +
  404. /*
  405. * FIXME: this is needed to communicate between the pinctrl driver and
  406. * the PM implementation in the machine. Possibly part of the PM
  407. @@ -87,7 +90,7 @@
  408. unsigned long scsr;
  409. int i;
  410. - scsr = at91_pmc_read(AT91_PMC_SCSR);
  411. + scsr = readl(pmc + AT91_PMC_SCSR);
  412. /* USB must not be using PLLB */
  413. if ((scsr & at91_pm_data.uhp_udp_mask) != 0) {
  414. @@ -101,8 +104,7 @@
  415. if ((scsr & (AT91_PMC_PCK0 << i)) == 0)
  416. continue;
  417. -
  418. - css = at91_pmc_read(AT91_PMC_PCKR(i)) & AT91_PMC_CSS;
  419. + css = readl(pmc + AT91_PMC_PCKR(i)) & AT91_PMC_CSS;
  420. if (css != AT91_PMC_CSS_SLOW) {
  421. pr_err("AT91: PM - Suspend-to-RAM with PCK%d src %d\n", i, css);
  422. return 0;
  423. @@ -145,8 +147,8 @@
  424. flush_cache_all();
  425. outer_disable();
  426. - at91_suspend_sram_fn(at91_pmc_base, at91_ramc_base[0],
  427. - at91_ramc_base[1], pm_data);
  428. + at91_suspend_sram_fn(pmc, at91_ramc_base[0],
  429. + at91_ramc_base[1], pm_data);
  430. outer_resume();
  431. }
  432. @@ -353,6 +355,21 @@
  433. at91_pm_set_standby(standby);
  434. }
  435. +void at91rm9200_idle(void)
  436. +{
  437. + /*
  438. + * Disable the processor clock. The processor will be automatically
  439. + * re-enabled by an interrupt or by a reset.
  440. + */
  441. + writel(AT91_PMC_PCK, pmc + AT91_PMC_SCDR);
  442. +}
  443. +
  444. +void at91sam9_idle(void)
  445. +{
  446. + writel(AT91_PMC_PCK, pmc + AT91_PMC_SCDR);
  447. + cpu_do_idle();
  448. +}
  449. +
  450. static void __init at91_pm_sram_init(void)
  451. {
  452. struct gen_pool *sram_pool;
  453. @@ -399,13 +416,36 @@
  454. &at91_pm_suspend_in_sram, at91_pm_suspend_in_sram_sz);
  455. }
  456. -static void __init at91_pm_init(void)
  457. +static const struct of_device_id atmel_pmc_ids[] __initconst = {
  458. + { .compatible = "atmel,at91rm9200-pmc" },
  459. + { .compatible = "atmel,at91sam9260-pmc" },
  460. + { .compatible = "atmel,at91sam9g45-pmc" },
  461. + { .compatible = "atmel,at91sam9n12-pmc" },
  462. + { .compatible = "atmel,at91sam9x5-pmc" },
  463. + { .compatible = "atmel,sama5d3-pmc" },
  464. + { .compatible = "atmel,sama5d2-pmc" },
  465. + { /* sentinel */ },
  466. +};
  467. +
  468. +static void __init at91_pm_init(void (*pm_idle)(void))
  469. {
  470. - at91_pm_sram_init();
  471. + struct device_node *pmc_np;
  472. if (at91_cpuidle_device.dev.platform_data)
  473. platform_device_register(&at91_cpuidle_device);
  474. + pmc_np = of_find_matching_node(NULL, atmel_pmc_ids);
  475. + pmc = of_iomap(pmc_np, 0);
  476. + if (!pmc) {
  477. + pr_err("AT91: PM not supported, PMC not found\n");
  478. + return;
  479. + }
  480. +
  481. + if (pm_idle)
  482. + arm_pm_idle = pm_idle;
  483. +
  484. + at91_pm_sram_init();
  485. +
  486. if (at91_suspend_sram_fn)
  487. suspend_set_ops(&at91_pm_ops);
  488. else
  489. @@ -424,7 +464,7 @@
  490. at91_pm_data.uhp_udp_mask = AT91RM9200_PMC_UHP | AT91RM9200_PMC_UDP;
  491. at91_pm_data.memctrl = AT91_MEMCTRL_MC;
  492. - at91_pm_init();
  493. + at91_pm_init(at91rm9200_idle);
  494. }
  495. void __init at91sam9260_pm_init(void)
  496. @@ -432,7 +472,7 @@
  497. at91_dt_ramc();
  498. at91_pm_data.memctrl = AT91_MEMCTRL_SDRAMC;
  499. at91_pm_data.uhp_udp_mask = AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP;
  500. - return at91_pm_init();
  501. + at91_pm_init(at91sam9_idle);
  502. }
  503. void __init at91sam9g45_pm_init(void)
  504. @@ -440,7 +480,7 @@
  505. at91_dt_ramc();
  506. at91_pm_data.uhp_udp_mask = AT91SAM926x_PMC_UHP;
  507. at91_pm_data.memctrl = AT91_MEMCTRL_DDRSDR;
  508. - return at91_pm_init();
  509. + at91_pm_init(at91sam9_idle);
  510. }
  511. void __init at91sam9x5_pm_init(void)
  512. @@ -448,5 +488,13 @@
  513. at91_dt_ramc();
  514. at91_pm_data.uhp_udp_mask = AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP;
  515. at91_pm_data.memctrl = AT91_MEMCTRL_DDRSDR;
  516. - return at91_pm_init();
  517. + at91_pm_init(at91sam9_idle);
  518. +}
  519. +
  520. +void __init sama5_pm_init(void)
  521. +{
  522. + at91_dt_ramc();
  523. + at91_pm_data.uhp_udp_mask = AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP;
  524. + at91_pm_data.memctrl = AT91_MEMCTRL_DDRSDR;
  525. + at91_pm_init(NULL);
  526. }
  527. diff -Nur linux-4.4.46.orig/arch/arm/mach-at91/sama5.c linux-4.4.46/arch/arm/mach-at91/sama5.c
  528. --- linux-4.4.46.orig/arch/arm/mach-at91/sama5.c 2017-02-01 08:31:11.000000000 +0100
  529. +++ linux-4.4.46/arch/arm/mach-at91/sama5.c 2017-02-03 17:18:05.627414322 +0100
  530. @@ -51,7 +51,7 @@
  531. soc_dev = soc_device_to_device(soc);
  532. of_platform_populate(NULL, of_default_bus_match_table, NULL, soc_dev);
  533. - at91sam9x5_pm_init();
  534. + sama5_pm_init();
  535. }
  536. static const char *const sama5_dt_board_compat[] __initconst = {
  537. diff -Nur linux-4.4.46.orig/arch/arm/mach-exynos/platsmp.c linux-4.4.46/arch/arm/mach-exynos/platsmp.c
  538. --- linux-4.4.46.orig/arch/arm/mach-exynos/platsmp.c 2017-02-01 08:31:11.000000000 +0100
  539. +++ linux-4.4.46/arch/arm/mach-exynos/platsmp.c 2017-02-03 17:18:05.627414322 +0100
  540. @@ -230,7 +230,7 @@
  541. return (void __iomem *)(S5P_VA_SCU);
  542. }
  543. -static DEFINE_SPINLOCK(boot_lock);
  544. +static DEFINE_RAW_SPINLOCK(boot_lock);
  545. static void exynos_secondary_init(unsigned int cpu)
  546. {
  547. @@ -243,8 +243,8 @@
  548. /*
  549. * Synchronise with the boot thread.
  550. */
  551. - spin_lock(&boot_lock);
  552. - spin_unlock(&boot_lock);
  553. + raw_spin_lock(&boot_lock);
  554. + raw_spin_unlock(&boot_lock);
  555. }
  556. int exynos_set_boot_addr(u32 core_id, unsigned long boot_addr)
  557. @@ -308,7 +308,7 @@
  558. * Set synchronisation state between this boot processor
  559. * and the secondary one
  560. */
  561. - spin_lock(&boot_lock);
  562. + raw_spin_lock(&boot_lock);
  563. /*
  564. * The secondary processor is waiting to be released from
  565. @@ -335,7 +335,7 @@
  566. if (timeout == 0) {
  567. printk(KERN_ERR "cpu1 power enable failed");
  568. - spin_unlock(&boot_lock);
  569. + raw_spin_unlock(&boot_lock);
  570. return -ETIMEDOUT;
  571. }
  572. }
  573. @@ -381,7 +381,7 @@
  574. * calibrations, then wait for it to finish
  575. */
  576. fail:
  577. - spin_unlock(&boot_lock);
  578. + raw_spin_unlock(&boot_lock);
  579. return pen_release != -1 ? ret : 0;
  580. }
  581. diff -Nur linux-4.4.46.orig/arch/arm/mach-hisi/platmcpm.c linux-4.4.46/arch/arm/mach-hisi/platmcpm.c
  582. --- linux-4.4.46.orig/arch/arm/mach-hisi/platmcpm.c 2017-02-01 08:31:11.000000000 +0100
  583. +++ linux-4.4.46/arch/arm/mach-hisi/platmcpm.c 2017-02-03 17:18:05.627414322 +0100
  584. @@ -61,7 +61,7 @@
  585. static void __iomem *sysctrl, *fabric;
  586. static int hip04_cpu_table[HIP04_MAX_CLUSTERS][HIP04_MAX_CPUS_PER_CLUSTER];
  587. -static DEFINE_SPINLOCK(boot_lock);
  588. +static DEFINE_RAW_SPINLOCK(boot_lock);
  589. static u32 fabric_phys_addr;
  590. /*
  591. * [0]: bootwrapper physical address
  592. @@ -113,7 +113,7 @@
  593. if (cluster >= HIP04_MAX_CLUSTERS || cpu >= HIP04_MAX_CPUS_PER_CLUSTER)
  594. return -EINVAL;
  595. - spin_lock_irq(&boot_lock);
  596. + raw_spin_lock_irq(&boot_lock);
  597. if (hip04_cpu_table[cluster][cpu])
  598. goto out;
  599. @@ -147,7 +147,7 @@
  600. out:
  601. hip04_cpu_table[cluster][cpu]++;
  602. - spin_unlock_irq(&boot_lock);
  603. + raw_spin_unlock_irq(&boot_lock);
  604. return 0;
  605. }
  606. @@ -162,11 +162,11 @@
  607. cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  608. cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  609. - spin_lock(&boot_lock);
  610. + raw_spin_lock(&boot_lock);
  611. hip04_cpu_table[cluster][cpu]--;
  612. if (hip04_cpu_table[cluster][cpu] == 1) {
  613. /* A power_up request went ahead of us. */
  614. - spin_unlock(&boot_lock);
  615. + raw_spin_unlock(&boot_lock);
  616. return;
  617. } else if (hip04_cpu_table[cluster][cpu] > 1) {
  618. pr_err("Cluster %d CPU%d boots multiple times\n", cluster, cpu);
  619. @@ -174,7 +174,7 @@
  620. }
  621. last_man = hip04_cluster_is_down(cluster);
  622. - spin_unlock(&boot_lock);
  623. + raw_spin_unlock(&boot_lock);
  624. if (last_man) {
  625. /* Since it's Cortex A15, disable L2 prefetching. */
  626. asm volatile(
  627. @@ -203,7 +203,7 @@
  628. cpu >= HIP04_MAX_CPUS_PER_CLUSTER);
  629. count = TIMEOUT_MSEC / POLL_MSEC;
  630. - spin_lock_irq(&boot_lock);
  631. + raw_spin_lock_irq(&boot_lock);
  632. for (tries = 0; tries < count; tries++) {
  633. if (hip04_cpu_table[cluster][cpu])
  634. goto err;
  635. @@ -211,10 +211,10 @@
  636. data = readl_relaxed(sysctrl + SC_CPU_RESET_STATUS(cluster));
  637. if (data & CORE_WFI_STATUS(cpu))
  638. break;
  639. - spin_unlock_irq(&boot_lock);
  640. + raw_spin_unlock_irq(&boot_lock);
  641. /* Wait for clean L2 when the whole cluster is down. */
  642. msleep(POLL_MSEC);
  643. - spin_lock_irq(&boot_lock);
  644. + raw_spin_lock_irq(&boot_lock);
  645. }
  646. if (tries >= count)
  647. goto err;
  648. @@ -231,10 +231,10 @@
  649. goto err;
  650. if (hip04_cluster_is_down(cluster))
  651. hip04_set_snoop_filter(cluster, 0);
  652. - spin_unlock_irq(&boot_lock);
  653. + raw_spin_unlock_irq(&boot_lock);
  654. return 1;
  655. err:
  656. - spin_unlock_irq(&boot_lock);
  657. + raw_spin_unlock_irq(&boot_lock);
  658. return 0;
  659. }
  660. #endif
  661. diff -Nur linux-4.4.46.orig/arch/arm/mach-imx/Kconfig linux-4.4.46/arch/arm/mach-imx/Kconfig
  662. --- linux-4.4.46.orig/arch/arm/mach-imx/Kconfig 2017-02-01 08:31:11.000000000 +0100
  663. +++ linux-4.4.46/arch/arm/mach-imx/Kconfig 2017-02-03 17:18:05.627414322 +0100
  664. @@ -524,7 +524,7 @@
  665. bool "i.MX6 Quad/DualLite support"
  666. select ARM_ERRATA_764369 if SMP
  667. select HAVE_ARM_SCU if SMP
  668. - select HAVE_ARM_TWD if SMP
  669. + select HAVE_ARM_TWD
  670. select PCI_DOMAINS if PCI
  671. select PINCTRL_IMX6Q
  672. select SOC_IMX6
  673. diff -Nur linux-4.4.46.orig/arch/arm/mach-omap2/omap-smp.c linux-4.4.46/arch/arm/mach-omap2/omap-smp.c
  674. --- linux-4.4.46.orig/arch/arm/mach-omap2/omap-smp.c 2017-02-01 08:31:11.000000000 +0100
  675. +++ linux-4.4.46/arch/arm/mach-omap2/omap-smp.c 2017-02-03 17:18:05.631414475 +0100
  676. @@ -43,7 +43,7 @@
  677. /* SCU base address */
  678. static void __iomem *scu_base;
  679. -static DEFINE_SPINLOCK(boot_lock);
  680. +static DEFINE_RAW_SPINLOCK(boot_lock);
  681. void __iomem *omap4_get_scu_base(void)
  682. {
  683. @@ -74,8 +74,8 @@
  684. /*
  685. * Synchronise with the boot thread.
  686. */
  687. - spin_lock(&boot_lock);
  688. - spin_unlock(&boot_lock);
  689. + raw_spin_lock(&boot_lock);
  690. + raw_spin_unlock(&boot_lock);
  691. }
  692. static int omap4_boot_secondary(unsigned int cpu, struct task_struct *idle)
  693. @@ -89,7 +89,7 @@
  694. * Set synchronisation state between this boot processor
  695. * and the secondary one
  696. */
  697. - spin_lock(&boot_lock);
  698. + raw_spin_lock(&boot_lock);
  699. /*
  700. * Update the AuxCoreBoot0 with boot state for secondary core.
  701. @@ -166,7 +166,7 @@
  702. * Now the secondary core is starting up let it run its
  703. * calibrations, then wait for it to finish
  704. */
  705. - spin_unlock(&boot_lock);
  706. + raw_spin_unlock(&boot_lock);
  707. return 0;
  708. }
  709. diff -Nur linux-4.4.46.orig/arch/arm/mach-prima2/platsmp.c linux-4.4.46/arch/arm/mach-prima2/platsmp.c
  710. --- linux-4.4.46.orig/arch/arm/mach-prima2/platsmp.c 2017-02-01 08:31:11.000000000 +0100
  711. +++ linux-4.4.46/arch/arm/mach-prima2/platsmp.c 2017-02-03 17:18:05.631414475 +0100
  712. @@ -22,7 +22,7 @@
  713. static void __iomem *clk_base;
  714. -static DEFINE_SPINLOCK(boot_lock);
  715. +static DEFINE_RAW_SPINLOCK(boot_lock);
  716. static void sirfsoc_secondary_init(unsigned int cpu)
  717. {
  718. @@ -36,8 +36,8 @@
  719. /*
  720. * Synchronise with the boot thread.
  721. */
  722. - spin_lock(&boot_lock);
  723. - spin_unlock(&boot_lock);
  724. + raw_spin_lock(&boot_lock);
  725. + raw_spin_unlock(&boot_lock);
  726. }
  727. static const struct of_device_id clk_ids[] = {
  728. @@ -75,7 +75,7 @@
  729. /* make sure write buffer is drained */
  730. mb();
  731. - spin_lock(&boot_lock);
  732. + raw_spin_lock(&boot_lock);
  733. /*
  734. * The secondary processor is waiting to be released from
  735. @@ -107,7 +107,7 @@
  736. * now the secondary core is starting up let it run its
  737. * calibrations, then wait for it to finish
  738. */
  739. - spin_unlock(&boot_lock);
  740. + raw_spin_unlock(&boot_lock);
  741. return pen_release != -1 ? -ENOSYS : 0;
  742. }
  743. diff -Nur linux-4.4.46.orig/arch/arm/mach-qcom/platsmp.c linux-4.4.46/arch/arm/mach-qcom/platsmp.c
  744. --- linux-4.4.46.orig/arch/arm/mach-qcom/platsmp.c 2017-02-01 08:31:11.000000000 +0100
  745. +++ linux-4.4.46/arch/arm/mach-qcom/platsmp.c 2017-02-03 17:18:05.631414475 +0100
  746. @@ -46,7 +46,7 @@
  747. extern void secondary_startup_arm(void);
  748. -static DEFINE_SPINLOCK(boot_lock);
  749. +static DEFINE_RAW_SPINLOCK(boot_lock);
  750. #ifdef CONFIG_HOTPLUG_CPU
  751. static void qcom_cpu_die(unsigned int cpu)
  752. @@ -60,8 +60,8 @@
  753. /*
  754. * Synchronise with the boot thread.
  755. */
  756. - spin_lock(&boot_lock);
  757. - spin_unlock(&boot_lock);
  758. + raw_spin_lock(&boot_lock);
  759. + raw_spin_unlock(&boot_lock);
  760. }
  761. static int scss_release_secondary(unsigned int cpu)
  762. @@ -284,7 +284,7 @@
  763. * set synchronisation state between this boot processor
  764. * and the secondary one
  765. */
  766. - spin_lock(&boot_lock);
  767. + raw_spin_lock(&boot_lock);
  768. /*
  769. * Send the secondary CPU a soft interrupt, thereby causing
  770. @@ -297,7 +297,7 @@
  771. * now the secondary core is starting up let it run its
  772. * calibrations, then wait for it to finish
  773. */
  774. - spin_unlock(&boot_lock);
  775. + raw_spin_unlock(&boot_lock);
  776. return ret;
  777. }
  778. diff -Nur linux-4.4.46.orig/arch/arm/mach-spear/platsmp.c linux-4.4.46/arch/arm/mach-spear/platsmp.c
  779. --- linux-4.4.46.orig/arch/arm/mach-spear/platsmp.c 2017-02-01 08:31:11.000000000 +0100
  780. +++ linux-4.4.46/arch/arm/mach-spear/platsmp.c 2017-02-03 17:18:05.631414475 +0100
  781. @@ -32,7 +32,7 @@
  782. sync_cache_w(&pen_release);
  783. }
  784. -static DEFINE_SPINLOCK(boot_lock);
  785. +static DEFINE_RAW_SPINLOCK(boot_lock);
  786. static void __iomem *scu_base = IOMEM(VA_SCU_BASE);
  787. @@ -47,8 +47,8 @@
  788. /*
  789. * Synchronise with the boot thread.
  790. */
  791. - spin_lock(&boot_lock);
  792. - spin_unlock(&boot_lock);
  793. + raw_spin_lock(&boot_lock);
  794. + raw_spin_unlock(&boot_lock);
  795. }
  796. static int spear13xx_boot_secondary(unsigned int cpu, struct task_struct *idle)
  797. @@ -59,7 +59,7 @@
  798. * set synchronisation state between this boot processor
  799. * and the secondary one
  800. */
  801. - spin_lock(&boot_lock);
  802. + raw_spin_lock(&boot_lock);
  803. /*
  804. * The secondary processor is waiting to be released from
  805. @@ -84,7 +84,7 @@
  806. * now the secondary core is starting up let it run its
  807. * calibrations, then wait for it to finish
  808. */
  809. - spin_unlock(&boot_lock);
  810. + raw_spin_unlock(&boot_lock);
  811. return pen_release != -1 ? -ENOSYS : 0;
  812. }
  813. diff -Nur linux-4.4.46.orig/arch/arm/mach-sti/platsmp.c linux-4.4.46/arch/arm/mach-sti/platsmp.c
  814. --- linux-4.4.46.orig/arch/arm/mach-sti/platsmp.c 2017-02-01 08:31:11.000000000 +0100
  815. +++ linux-4.4.46/arch/arm/mach-sti/platsmp.c 2017-02-03 17:18:05.631414475 +0100
  816. @@ -35,7 +35,7 @@
  817. sync_cache_w(&pen_release);
  818. }
  819. -static DEFINE_SPINLOCK(boot_lock);
  820. +static DEFINE_RAW_SPINLOCK(boot_lock);
  821. static void sti_secondary_init(unsigned int cpu)
  822. {
  823. @@ -48,8 +48,8 @@
  824. /*
  825. * Synchronise with the boot thread.
  826. */
  827. - spin_lock(&boot_lock);
  828. - spin_unlock(&boot_lock);
  829. + raw_spin_lock(&boot_lock);
  830. + raw_spin_unlock(&boot_lock);
  831. }
  832. static int sti_boot_secondary(unsigned int cpu, struct task_struct *idle)
  833. @@ -60,7 +60,7 @@
  834. * set synchronisation state between this boot processor
  835. * and the secondary one
  836. */
  837. - spin_lock(&boot_lock);
  838. + raw_spin_lock(&boot_lock);
  839. /*
  840. * The secondary processor is waiting to be released from
  841. @@ -91,7 +91,7 @@
  842. * now the secondary core is starting up let it run its
  843. * calibrations, then wait for it to finish
  844. */
  845. - spin_unlock(&boot_lock);
  846. + raw_spin_unlock(&boot_lock);
  847. return pen_release != -1 ? -ENOSYS : 0;
  848. }
  849. diff -Nur linux-4.4.46.orig/arch/arm/mm/fault.c linux-4.4.46/arch/arm/mm/fault.c
  850. --- linux-4.4.46.orig/arch/arm/mm/fault.c 2017-02-01 08:31:11.000000000 +0100
  851. +++ linux-4.4.46/arch/arm/mm/fault.c 2017-02-03 17:18:05.631414475 +0100
  852. @@ -430,6 +430,9 @@
  853. if (addr < TASK_SIZE)
  854. return do_page_fault(addr, fsr, regs);
  855. + if (interrupts_enabled(regs))
  856. + local_irq_enable();
  857. +
  858. if (user_mode(regs))
  859. goto bad_area;
  860. @@ -497,6 +500,9 @@
  861. static int
  862. do_sect_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  863. {
  864. + if (interrupts_enabled(regs))
  865. + local_irq_enable();
  866. +
  867. do_bad_area(addr, fsr, regs);
  868. return 0;
  869. }
  870. diff -Nur linux-4.4.46.orig/arch/arm/mm/highmem.c linux-4.4.46/arch/arm/mm/highmem.c
  871. --- linux-4.4.46.orig/arch/arm/mm/highmem.c 2017-02-01 08:31:11.000000000 +0100
  872. +++ linux-4.4.46/arch/arm/mm/highmem.c 2017-02-03 17:18:05.631414475 +0100
  873. @@ -34,6 +34,11 @@
  874. return *ptep;
  875. }
  876. +static unsigned int fixmap_idx(int type)
  877. +{
  878. + return FIX_KMAP_BEGIN + type + KM_TYPE_NR * smp_processor_id();
  879. +}
  880. +
  881. void *kmap(struct page *page)
  882. {
  883. might_sleep();
  884. @@ -54,12 +59,13 @@
  885. void *kmap_atomic(struct page *page)
  886. {
  887. + pte_t pte = mk_pte(page, kmap_prot);
  888. unsigned int idx;
  889. unsigned long vaddr;
  890. void *kmap;
  891. int type;
  892. - preempt_disable();
  893. + preempt_disable_nort();
  894. pagefault_disable();
  895. if (!PageHighMem(page))
  896. return page_address(page);
  897. @@ -79,7 +85,7 @@
  898. type = kmap_atomic_idx_push();
  899. - idx = FIX_KMAP_BEGIN + type + KM_TYPE_NR * smp_processor_id();
  900. + idx = fixmap_idx(type);
  901. vaddr = __fix_to_virt(idx);
  902. #ifdef CONFIG_DEBUG_HIGHMEM
  903. /*
  904. @@ -93,7 +99,10 @@
  905. * in place, so the contained TLB flush ensures the TLB is updated
  906. * with the new mapping.
  907. */
  908. - set_fixmap_pte(idx, mk_pte(page, kmap_prot));
  909. +#ifdef CONFIG_PREEMPT_RT_FULL
  910. + current->kmap_pte[type] = pte;
  911. +#endif
  912. + set_fixmap_pte(idx, pte);
  913. return (void *)vaddr;
  914. }
  915. @@ -106,44 +115,75 @@
  916. if (kvaddr >= (void *)FIXADDR_START) {
  917. type = kmap_atomic_idx();
  918. - idx = FIX_KMAP_BEGIN + type + KM_TYPE_NR * smp_processor_id();
  919. + idx = fixmap_idx(type);
  920. if (cache_is_vivt())
  921. __cpuc_flush_dcache_area((void *)vaddr, PAGE_SIZE);
  922. +#ifdef CONFIG_PREEMPT_RT_FULL
  923. + current->kmap_pte[type] = __pte(0);
  924. +#endif
  925. #ifdef CONFIG_DEBUG_HIGHMEM
  926. BUG_ON(vaddr != __fix_to_virt(idx));
  927. - set_fixmap_pte(idx, __pte(0));
  928. #else
  929. (void) idx; /* to kill a warning */
  930. #endif
  931. + set_fixmap_pte(idx, __pte(0));
  932. kmap_atomic_idx_pop();
  933. } else if (vaddr >= PKMAP_ADDR(0) && vaddr < PKMAP_ADDR(LAST_PKMAP)) {
  934. /* this address was obtained through kmap_high_get() */
  935. kunmap_high(pte_page(pkmap_page_table[PKMAP_NR(vaddr)]));
  936. }
  937. pagefault_enable();
  938. - preempt_enable();
  939. + preempt_enable_nort();
  940. }
  941. EXPORT_SYMBOL(__kunmap_atomic);
  942. void *kmap_atomic_pfn(unsigned long pfn)
  943. {
  944. + pte_t pte = pfn_pte(pfn, kmap_prot);
  945. unsigned long vaddr;
  946. int idx, type;
  947. struct page *page = pfn_to_page(pfn);
  948. - preempt_disable();
  949. + preempt_disable_nort();
  950. pagefault_disable();
  951. if (!PageHighMem(page))
  952. return page_address(page);
  953. type = kmap_atomic_idx_push();
  954. - idx = FIX_KMAP_BEGIN + type + KM_TYPE_NR * smp_processor_id();
  955. + idx = fixmap_idx(type);
  956. vaddr = __fix_to_virt(idx);
  957. #ifdef CONFIG_DEBUG_HIGHMEM
  958. BUG_ON(!pte_none(get_fixmap_pte(vaddr)));
  959. #endif
  960. - set_fixmap_pte(idx, pfn_pte(pfn, kmap_prot));
  961. +#ifdef CONFIG_PREEMPT_RT_FULL
  962. + current->kmap_pte[type] = pte;
  963. +#endif
  964. + set_fixmap_pte(idx, pte);
  965. return (void *)vaddr;
  966. }
  967. +#if defined CONFIG_PREEMPT_RT_FULL
  968. +void switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p)
  969. +{
  970. + int i;
  971. +
  972. + /*
  973. + * Clear @prev's kmap_atomic mappings
  974. + */
  975. + for (i = 0; i < prev_p->kmap_idx; i++) {
  976. + int idx = fixmap_idx(i);
  977. +
  978. + set_fixmap_pte(idx, __pte(0));
  979. + }
  980. + /*
  981. + * Restore @next_p's kmap_atomic mappings
  982. + */
  983. + for (i = 0; i < next_p->kmap_idx; i++) {
  984. + int idx = fixmap_idx(i);
  985. +
  986. + if (!pte_none(next_p->kmap_pte[i]))
  987. + set_fixmap_pte(idx, next_p->kmap_pte[i]);
  988. + }
  989. +}
  990. +#endif
  991. diff -Nur linux-4.4.46.orig/arch/arm/plat-versatile/platsmp.c linux-4.4.46/arch/arm/plat-versatile/platsmp.c
  992. --- linux-4.4.46.orig/arch/arm/plat-versatile/platsmp.c 2017-02-01 08:31:11.000000000 +0100
  993. +++ linux-4.4.46/arch/arm/plat-versatile/platsmp.c 2017-02-03 17:18:05.631414475 +0100
  994. @@ -30,7 +30,7 @@
  995. sync_cache_w(&pen_release);
  996. }
  997. -static DEFINE_SPINLOCK(boot_lock);
  998. +static DEFINE_RAW_SPINLOCK(boot_lock);
  999. void versatile_secondary_init(unsigned int cpu)
  1000. {
  1001. @@ -43,8 +43,8 @@
  1002. /*
  1003. * Synchronise with the boot thread.
  1004. */
  1005. - spin_lock(&boot_lock);
  1006. - spin_unlock(&boot_lock);
  1007. + raw_spin_lock(&boot_lock);
  1008. + raw_spin_unlock(&boot_lock);
  1009. }
  1010. int versatile_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1011. @@ -55,7 +55,7 @@
  1012. * Set synchronisation state between this boot processor
  1013. * and the secondary one
  1014. */
  1015. - spin_lock(&boot_lock);
  1016. + raw_spin_lock(&boot_lock);
  1017. /*
  1018. * This is really belt and braces; we hold unintended secondary
  1019. @@ -85,7 +85,7 @@
  1020. * now the secondary core is starting up let it run its
  1021. * calibrations, then wait for it to finish
  1022. */
  1023. - spin_unlock(&boot_lock);
  1024. + raw_spin_unlock(&boot_lock);
  1025. return pen_release != -1 ? -ENOSYS : 0;
  1026. }
  1027. diff -Nur linux-4.4.46.orig/arch/arm64/include/asm/thread_info.h linux-4.4.46/arch/arm64/include/asm/thread_info.h
  1028. --- linux-4.4.46.orig/arch/arm64/include/asm/thread_info.h 2017-02-01 08:31:11.000000000 +0100
  1029. +++ linux-4.4.46/arch/arm64/include/asm/thread_info.h 2017-02-03 17:18:05.631414475 +0100
  1030. @@ -49,6 +49,7 @@
  1031. mm_segment_t addr_limit; /* address limit */
  1032. struct task_struct *task; /* main task structure */
  1033. int preempt_count; /* 0 => preemptable, <0 => bug */
  1034. + int preempt_lazy_count; /* 0 => preemptable, <0 => bug */
  1035. int cpu; /* cpu */
  1036. };
  1037. @@ -103,6 +104,7 @@
  1038. #define TIF_NEED_RESCHED 1
  1039. #define TIF_NOTIFY_RESUME 2 /* callback before returning to user */
  1040. #define TIF_FOREIGN_FPSTATE 3 /* CPU's FP state is not current's */
  1041. +#define TIF_NEED_RESCHED_LAZY 4
  1042. #define TIF_NOHZ 7
  1043. #define TIF_SYSCALL_TRACE 8
  1044. #define TIF_SYSCALL_AUDIT 9
  1045. @@ -118,6 +120,7 @@
  1046. #define _TIF_NEED_RESCHED (1 << TIF_NEED_RESCHED)
  1047. #define _TIF_NOTIFY_RESUME (1 << TIF_NOTIFY_RESUME)
  1048. #define _TIF_FOREIGN_FPSTATE (1 << TIF_FOREIGN_FPSTATE)
  1049. +#define _TIF_NEED_RESCHED_LAZY (1 << TIF_NEED_RESCHED_LAZY)
  1050. #define _TIF_NOHZ (1 << TIF_NOHZ)
  1051. #define _TIF_SYSCALL_TRACE (1 << TIF_SYSCALL_TRACE)
  1052. #define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
  1053. @@ -126,7 +129,8 @@
  1054. #define _TIF_32BIT (1 << TIF_32BIT)
  1055. #define _TIF_WORK_MASK (_TIF_NEED_RESCHED | _TIF_SIGPENDING | \
  1056. - _TIF_NOTIFY_RESUME | _TIF_FOREIGN_FPSTATE)
  1057. + _TIF_NOTIFY_RESUME | _TIF_FOREIGN_FPSTATE | \
  1058. + _TIF_NEED_RESCHED_LAZY)
  1059. #define _TIF_SYSCALL_WORK (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | \
  1060. _TIF_SYSCALL_TRACEPOINT | _TIF_SECCOMP | \
  1061. diff -Nur linux-4.4.46.orig/arch/arm64/Kconfig linux-4.4.46/arch/arm64/Kconfig
  1062. --- linux-4.4.46.orig/arch/arm64/Kconfig 2017-02-01 08:31:11.000000000 +0100
  1063. +++ linux-4.4.46/arch/arm64/Kconfig 2017-02-03 17:18:05.631414475 +0100
  1064. @@ -76,6 +76,7 @@
  1065. select HAVE_PERF_REGS
  1066. select HAVE_PERF_USER_STACK_DUMP
  1067. select HAVE_RCU_TABLE_FREE
  1068. + select HAVE_PREEMPT_LAZY
  1069. select HAVE_SYSCALL_TRACEPOINTS
  1070. select IOMMU_DMA if IOMMU_SUPPORT
  1071. select IRQ_DOMAIN
  1072. @@ -582,7 +583,7 @@
  1073. config XEN
  1074. bool "Xen guest support on ARM64"
  1075. - depends on ARM64 && OF
  1076. + depends on ARM64 && OF && !PREEMPT_RT_FULL
  1077. select SWIOTLB_XEN
  1078. help
  1079. Say Y if you want to run Linux in a Virtual Machine on Xen on ARM64.
  1080. diff -Nur linux-4.4.46.orig/arch/arm64/kernel/asm-offsets.c linux-4.4.46/arch/arm64/kernel/asm-offsets.c
  1081. --- linux-4.4.46.orig/arch/arm64/kernel/asm-offsets.c 2017-02-01 08:31:11.000000000 +0100
  1082. +++ linux-4.4.46/arch/arm64/kernel/asm-offsets.c 2017-02-03 17:18:05.639414785 +0100
  1083. @@ -35,6 +35,7 @@
  1084. BLANK();
  1085. DEFINE(TI_FLAGS, offsetof(struct thread_info, flags));
  1086. DEFINE(TI_PREEMPT, offsetof(struct thread_info, preempt_count));
  1087. + DEFINE(TI_PREEMPT_LAZY, offsetof(struct thread_info, preempt_lazy_count));
  1088. DEFINE(TI_ADDR_LIMIT, offsetof(struct thread_info, addr_limit));
  1089. DEFINE(TI_TASK, offsetof(struct thread_info, task));
  1090. DEFINE(TI_CPU, offsetof(struct thread_info, cpu));
  1091. diff -Nur linux-4.4.46.orig/arch/arm64/kernel/entry.S linux-4.4.46/arch/arm64/kernel/entry.S
  1092. --- linux-4.4.46.orig/arch/arm64/kernel/entry.S 2017-02-01 08:31:11.000000000 +0100
  1093. +++ linux-4.4.46/arch/arm64/kernel/entry.S 2017-02-03 17:18:05.639414785 +0100
  1094. @@ -376,11 +376,16 @@
  1095. #ifdef CONFIG_PREEMPT
  1096. get_thread_info tsk
  1097. ldr w24, [tsk, #TI_PREEMPT] // get preempt count
  1098. - cbnz w24, 1f // preempt count != 0
  1099. + cbnz w24, 2f // preempt count != 0
  1100. ldr x0, [tsk, #TI_FLAGS] // get flags
  1101. - tbz x0, #TIF_NEED_RESCHED, 1f // needs rescheduling?
  1102. - bl el1_preempt
  1103. + tbnz x0, #TIF_NEED_RESCHED, 1f // needs rescheduling?
  1104. +
  1105. + ldr w24, [tsk, #TI_PREEMPT_LAZY] // get preempt lazy count
  1106. + cbnz w24, 2f // preempt lazy count != 0
  1107. + tbz x0, #TIF_NEED_RESCHED_LAZY, 2f // needs rescheduling?
  1108. 1:
  1109. + bl el1_preempt
  1110. +2:
  1111. #endif
  1112. #ifdef CONFIG_TRACE_IRQFLAGS
  1113. bl trace_hardirqs_on
  1114. @@ -394,6 +399,7 @@
  1115. 1: bl preempt_schedule_irq // irq en/disable is done inside
  1116. ldr x0, [tsk, #TI_FLAGS] // get new tasks TI_FLAGS
  1117. tbnz x0, #TIF_NEED_RESCHED, 1b // needs rescheduling?
  1118. + tbnz x0, #TIF_NEED_RESCHED_LAZY, 1b // needs rescheduling?
  1119. ret x24
  1120. #endif
  1121. @@ -638,6 +644,7 @@
  1122. */
  1123. work_pending:
  1124. tbnz x1, #TIF_NEED_RESCHED, work_resched
  1125. + tbnz x1, #TIF_NEED_RESCHED_LAZY, work_resched
  1126. /* TIF_SIGPENDING, TIF_NOTIFY_RESUME or TIF_FOREIGN_FPSTATE case */
  1127. ldr x2, [sp, #S_PSTATE]
  1128. mov x0, sp // 'regs'
  1129. diff -Nur linux-4.4.46.orig/arch/Kconfig linux-4.4.46/arch/Kconfig
  1130. --- linux-4.4.46.orig/arch/Kconfig 2017-02-01 08:31:11.000000000 +0100
  1131. +++ linux-4.4.46/arch/Kconfig 2017-02-03 17:18:05.627414322 +0100
  1132. @@ -9,6 +9,7 @@
  1133. tristate "OProfile system profiling"
  1134. depends on PROFILING
  1135. depends on HAVE_OPROFILE
  1136. + depends on !PREEMPT_RT_FULL
  1137. select RING_BUFFER
  1138. select RING_BUFFER_ALLOW_SWAP
  1139. help
  1140. @@ -52,6 +53,7 @@
  1141. config JUMP_LABEL
  1142. bool "Optimize very unlikely/likely branches"
  1143. depends on HAVE_ARCH_JUMP_LABEL
  1144. + depends on (!INTERRUPT_OFF_HIST && !PREEMPT_OFF_HIST && !WAKEUP_LATENCY_HIST && !MISSED_TIMER_OFFSETS_HIST)
  1145. help
  1146. This option enables a transparent branch optimization that
  1147. makes certain almost-always-true or almost-always-false branch
  1148. diff -Nur linux-4.4.46.orig/arch/mips/Kconfig linux-4.4.46/arch/mips/Kconfig
  1149. --- linux-4.4.46.orig/arch/mips/Kconfig 2017-02-01 08:31:11.000000000 +0100
  1150. +++ linux-4.4.46/arch/mips/Kconfig 2017-02-03 17:18:05.639414785 +0100
  1151. @@ -2410,7 +2410,7 @@
  1152. #
  1153. config HIGHMEM
  1154. bool "High Memory Support"
  1155. - depends on 32BIT && CPU_SUPPORTS_HIGHMEM && SYS_SUPPORTS_HIGHMEM && !CPU_MIPS32_3_5_EVA
  1156. + depends on 32BIT && CPU_SUPPORTS_HIGHMEM && SYS_SUPPORTS_HIGHMEM && !CPU_MIPS32_3_5_EVA && !PREEMPT_RT_FULL
  1157. config CPU_SUPPORTS_HIGHMEM
  1158. bool
  1159. diff -Nur linux-4.4.46.orig/arch/mips/kvm/mips.c linux-4.4.46/arch/mips/kvm/mips.c
  1160. --- linux-4.4.46.orig/arch/mips/kvm/mips.c 2017-02-01 08:31:11.000000000 +0100
  1161. +++ linux-4.4.46/arch/mips/kvm/mips.c 2017-02-03 17:18:05.639414785 +0100
  1162. @@ -454,8 +454,8 @@
  1163. dvcpu->arch.wait = 0;
  1164. - if (waitqueue_active(&dvcpu->wq))
  1165. - wake_up_interruptible(&dvcpu->wq);
  1166. + if (swait_active(&dvcpu->wq))
  1167. + swake_up(&dvcpu->wq);
  1168. return 0;
  1169. }
  1170. @@ -1183,8 +1183,8 @@
  1171. kvm_mips_callbacks->queue_timer_int(vcpu);
  1172. vcpu->arch.wait = 0;
  1173. - if (waitqueue_active(&vcpu->wq))
  1174. - wake_up_interruptible(&vcpu->wq);
  1175. + if (swait_active(&vcpu->wq))
  1176. + swake_up(&vcpu->wq);
  1177. }
  1178. /* low level hrtimer wake routine */
  1179. diff -Nur linux-4.4.46.orig/arch/powerpc/include/asm/kvm_host.h linux-4.4.46/arch/powerpc/include/asm/kvm_host.h
  1180. --- linux-4.4.46.orig/arch/powerpc/include/asm/kvm_host.h 2017-02-01 08:31:11.000000000 +0100
  1181. +++ linux-4.4.46/arch/powerpc/include/asm/kvm_host.h 2017-02-03 17:18:05.639414785 +0100
  1182. @@ -286,7 +286,7 @@
  1183. struct list_head runnable_threads;
  1184. struct list_head preempt_list;
  1185. spinlock_t lock;
  1186. - wait_queue_head_t wq;
  1187. + struct swait_queue_head wq;
  1188. spinlock_t stoltb_lock; /* protects stolen_tb and preempt_tb */
  1189. u64 stolen_tb;
  1190. u64 preempt_tb;
  1191. @@ -627,7 +627,7 @@
  1192. u8 prodded;
  1193. u32 last_inst;
  1194. - wait_queue_head_t *wqp;
  1195. + struct swait_queue_head *wqp;
  1196. struct kvmppc_vcore *vcore;
  1197. int ret;
  1198. int trap;
  1199. diff -Nur linux-4.4.46.orig/arch/powerpc/include/asm/thread_info.h linux-4.4.46/arch/powerpc/include/asm/thread_info.h
  1200. --- linux-4.4.46.orig/arch/powerpc/include/asm/thread_info.h 2017-02-01 08:31:11.000000000 +0100
  1201. +++ linux-4.4.46/arch/powerpc/include/asm/thread_info.h 2017-02-03 17:18:05.639414785 +0100
  1202. @@ -42,6 +42,8 @@
  1203. int cpu; /* cpu we're on */
  1204. int preempt_count; /* 0 => preemptable,
  1205. <0 => BUG */
  1206. + int preempt_lazy_count; /* 0 => preemptable,
  1207. + <0 => BUG */
  1208. unsigned long local_flags; /* private flags for thread */
  1209. /* low level flags - has atomic operations done on it */
  1210. @@ -82,8 +84,7 @@
  1211. #define TIF_SYSCALL_TRACE 0 /* syscall trace active */
  1212. #define TIF_SIGPENDING 1 /* signal pending */
  1213. #define TIF_NEED_RESCHED 2 /* rescheduling necessary */
  1214. -#define TIF_POLLING_NRFLAG 3 /* true if poll_idle() is polling
  1215. - TIF_NEED_RESCHED */
  1216. +#define TIF_NEED_RESCHED_LAZY 3 /* lazy rescheduling necessary */
  1217. #define TIF_32BIT 4 /* 32 bit binary */
  1218. #define TIF_RESTORE_TM 5 /* need to restore TM FP/VEC/VSX */
  1219. #define TIF_SYSCALL_AUDIT 7 /* syscall auditing active */
  1220. @@ -101,6 +102,8 @@
  1221. #if defined(CONFIG_PPC64)
  1222. #define TIF_ELF2ABI 18 /* function descriptors must die! */
  1223. #endif
  1224. +#define TIF_POLLING_NRFLAG 19 /* true if poll_idle() is polling
  1225. + TIF_NEED_RESCHED */
  1226. /* as above, but as bit values */
  1227. #define _TIF_SYSCALL_TRACE (1<<TIF_SYSCALL_TRACE)
  1228. @@ -119,14 +122,16 @@
  1229. #define _TIF_SYSCALL_TRACEPOINT (1<<TIF_SYSCALL_TRACEPOINT)
  1230. #define _TIF_EMULATE_STACK_STORE (1<<TIF_EMULATE_STACK_STORE)
  1231. #define _TIF_NOHZ (1<<TIF_NOHZ)
  1232. +#define _TIF_NEED_RESCHED_LAZY (1<<TIF_NEED_RESCHED_LAZY)
  1233. #define _TIF_SYSCALL_DOTRACE (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | \
  1234. _TIF_SECCOMP | _TIF_SYSCALL_TRACEPOINT | \
  1235. _TIF_NOHZ)
  1236. #define _TIF_USER_WORK_MASK (_TIF_SIGPENDING | _TIF_NEED_RESCHED | \
  1237. _TIF_NOTIFY_RESUME | _TIF_UPROBE | \
  1238. - _TIF_RESTORE_TM)
  1239. + _TIF_RESTORE_TM | _TIF_NEED_RESCHED_LAZY)
  1240. #define _TIF_PERSYSCALL_MASK (_TIF_RESTOREALL|_TIF_NOERROR)
  1241. +#define _TIF_NEED_RESCHED_MASK (_TIF_NEED_RESCHED | _TIF_NEED_RESCHED_LAZY)
  1242. /* Bits in local_flags */
  1243. /* Don't move TLF_NAPPING without adjusting the code in entry_32.S */
  1244. diff -Nur linux-4.4.46.orig/arch/powerpc/Kconfig linux-4.4.46/arch/powerpc/Kconfig
  1245. --- linux-4.4.46.orig/arch/powerpc/Kconfig 2017-02-01 08:31:11.000000000 +0100
  1246. +++ linux-4.4.46/arch/powerpc/Kconfig 2017-02-03 17:18:05.639414785 +0100
  1247. @@ -60,10 +60,11 @@
  1248. config RWSEM_GENERIC_SPINLOCK
  1249. bool
  1250. + default y if PREEMPT_RT_FULL
  1251. config RWSEM_XCHGADD_ALGORITHM
  1252. bool
  1253. - default y
  1254. + default y if !PREEMPT_RT_FULL
  1255. config GENERIC_LOCKBREAK
  1256. bool
  1257. @@ -141,6 +142,7 @@
  1258. select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST
  1259. select GENERIC_STRNCPY_FROM_USER
  1260. select GENERIC_STRNLEN_USER
  1261. + select HAVE_PREEMPT_LAZY
  1262. select HAVE_MOD_ARCH_SPECIFIC
  1263. select MODULES_USE_ELF_RELA
  1264. select CLONE_BACKWARDS
  1265. @@ -319,7 +321,7 @@
  1266. config HIGHMEM
  1267. bool "High memory support"
  1268. - depends on PPC32
  1269. + depends on PPC32 && !PREEMPT_RT_FULL
  1270. source kernel/Kconfig.hz
  1271. source kernel/Kconfig.preempt
  1272. diff -Nur linux-4.4.46.orig/arch/powerpc/kernel/asm-offsets.c linux-4.4.46/arch/powerpc/kernel/asm-offsets.c
  1273. --- linux-4.4.46.orig/arch/powerpc/kernel/asm-offsets.c 2017-02-01 08:31:11.000000000 +0100
  1274. +++ linux-4.4.46/arch/powerpc/kernel/asm-offsets.c 2017-02-03 17:18:05.643414939 +0100
  1275. @@ -160,6 +160,7 @@
  1276. DEFINE(TI_FLAGS, offsetof(struct thread_info, flags));
  1277. DEFINE(TI_LOCAL_FLAGS, offsetof(struct thread_info, local_flags));
  1278. DEFINE(TI_PREEMPT, offsetof(struct thread_info, preempt_count));
  1279. + DEFINE(TI_PREEMPT_LAZY, offsetof(struct thread_info, preempt_lazy_count));
  1280. DEFINE(TI_TASK, offsetof(struct thread_info, task));
  1281. DEFINE(TI_CPU, offsetof(struct thread_info, cpu));
  1282. diff -Nur linux-4.4.46.orig/arch/powerpc/kernel/entry_32.S linux-4.4.46/arch/powerpc/kernel/entry_32.S
  1283. --- linux-4.4.46.orig/arch/powerpc/kernel/entry_32.S 2017-02-01 08:31:11.000000000 +0100
  1284. +++ linux-4.4.46/arch/powerpc/kernel/entry_32.S 2017-02-03 17:18:05.643414939 +0100
  1285. @@ -818,7 +818,14 @@
  1286. cmpwi 0,r0,0 /* if non-zero, just restore regs and return */
  1287. bne restore
  1288. andi. r8,r8,_TIF_NEED_RESCHED
  1289. + bne+ 1f
  1290. + lwz r0,TI_PREEMPT_LAZY(r9)
  1291. + cmpwi 0,r0,0 /* if non-zero, just restore regs and return */
  1292. + bne restore
  1293. + lwz r0,TI_FLAGS(r9)
  1294. + andi. r0,r0,_TIF_NEED_RESCHED_LAZY
  1295. beq+ restore
  1296. +1:
  1297. lwz r3,_MSR(r1)
  1298. andi. r0,r3,MSR_EE /* interrupts off? */
  1299. beq restore /* don't schedule if so */
  1300. @@ -829,11 +836,11 @@
  1301. */
  1302. bl trace_hardirqs_off
  1303. #endif
  1304. -1: bl preempt_schedule_irq
  1305. +2: bl preempt_schedule_irq
  1306. CURRENT_THREAD_INFO(r9, r1)
  1307. lwz r3,TI_FLAGS(r9)
  1308. - andi. r0,r3,_TIF_NEED_RESCHED
  1309. - bne- 1b
  1310. + andi. r0,r3,_TIF_NEED_RESCHED_MASK
  1311. + bne- 2b
  1312. #ifdef CONFIG_TRACE_IRQFLAGS
  1313. /* And now, to properly rebalance the above, we tell lockdep they
  1314. * are being turned back on, which will happen when we return
  1315. @@ -1154,7 +1161,7 @@
  1316. #endif /* !(CONFIG_4xx || CONFIG_BOOKE) */
  1317. do_work: /* r10 contains MSR_KERNEL here */
  1318. - andi. r0,r9,_TIF_NEED_RESCHED
  1319. + andi. r0,r9,_TIF_NEED_RESCHED_MASK
  1320. beq do_user_signal
  1321. do_resched: /* r10 contains MSR_KERNEL here */
  1322. @@ -1175,7 +1182,7 @@
  1323. MTMSRD(r10) /* disable interrupts */
  1324. CURRENT_THREAD_INFO(r9, r1)
  1325. lwz r9,TI_FLAGS(r9)
  1326. - andi. r0,r9,_TIF_NEED_RESCHED
  1327. + andi. r0,r9,_TIF_NEED_RESCHED_MASK
  1328. bne- do_resched
  1329. andi. r0,r9,_TIF_USER_WORK_MASK
  1330. beq restore_user
  1331. diff -Nur linux-4.4.46.orig/arch/powerpc/kernel/entry_64.S linux-4.4.46/arch/powerpc/kernel/entry_64.S
  1332. --- linux-4.4.46.orig/arch/powerpc/kernel/entry_64.S 2017-02-01 08:31:11.000000000 +0100
  1333. +++ linux-4.4.46/arch/powerpc/kernel/entry_64.S 2017-02-03 17:18:05.643414939 +0100
  1334. @@ -683,7 +683,7 @@
  1335. #else
  1336. beq restore
  1337. #endif
  1338. -1: andi. r0,r4,_TIF_NEED_RESCHED
  1339. +1: andi. r0,r4,_TIF_NEED_RESCHED_MASK
  1340. beq 2f
  1341. bl restore_interrupts
  1342. SCHEDULE_USER
  1343. @@ -745,10 +745,18 @@
  1344. #ifdef CONFIG_PREEMPT
  1345. /* Check if we need to preempt */
  1346. + lwz r8,TI_PREEMPT(r9)
  1347. + cmpwi 0,r8,0 /* if non-zero, just restore regs and return */
  1348. + bne restore
  1349. andi. r0,r4,_TIF_NEED_RESCHED
  1350. + bne+ check_count
  1351. +
  1352. + andi. r0,r4,_TIF_NEED_RESCHED_LAZY
  1353. beq+ restore
  1354. + lwz r8,TI_PREEMPT_LAZY(r9)
  1355. +
  1356. /* Check that preempt_count() == 0 and interrupts are enabled */
  1357. - lwz r8,TI_PREEMPT(r9)
  1358. +check_count:
  1359. cmpwi cr1,r8,0
  1360. ld r0,SOFTE(r1)
  1361. cmpdi r0,0
  1362. @@ -765,7 +773,7 @@
  1363. /* Re-test flags and eventually loop */
  1364. CURRENT_THREAD_INFO(r9, r1)
  1365. ld r4,TI_FLAGS(r9)
  1366. - andi. r0,r4,_TIF_NEED_RESCHED
  1367. + andi. r0,r4,_TIF_NEED_RESCHED_MASK
  1368. bne 1b
  1369. /*
  1370. diff -Nur linux-4.4.46.orig/arch/powerpc/kernel/irq.c linux-4.4.46/arch/powerpc/kernel/irq.c
  1371. --- linux-4.4.46.orig/arch/powerpc/kernel/irq.c 2017-02-01 08:31:11.000000000 +0100
  1372. +++ linux-4.4.46/arch/powerpc/kernel/irq.c 2017-02-03 17:18:05.643414939 +0100
  1373. @@ -614,6 +614,7 @@
  1374. }
  1375. }
  1376. +#ifndef CONFIG_PREEMPT_RT_FULL
  1377. void do_softirq_own_stack(void)
  1378. {
  1379. struct thread_info *curtp, *irqtp;
  1380. @@ -631,6 +632,7 @@
  1381. if (irqtp->flags)
  1382. set_bits(irqtp->flags, &curtp->flags);
  1383. }
  1384. +#endif
  1385. irq_hw_number_t virq_to_hw(unsigned int virq)
  1386. {
  1387. diff -Nur linux-4.4.46.orig/arch/powerpc/kernel/misc_32.S linux-4.4.46/arch/powerpc/kernel/misc_32.S
  1388. --- linux-4.4.46.orig/arch/powerpc/kernel/misc_32.S 2017-02-01 08:31:11.000000000 +0100
  1389. +++ linux-4.4.46/arch/powerpc/kernel/misc_32.S 2017-02-03 17:18:05.643414939 +0100
  1390. @@ -40,6 +40,7 @@
  1391. * We store the saved ksp_limit in the unused part
  1392. * of the STACK_FRAME_OVERHEAD
  1393. */
  1394. +#ifndef CONFIG_PREEMPT_RT_FULL
  1395. _GLOBAL(call_do_softirq)
  1396. mflr r0
  1397. stw r0,4(r1)
  1398. @@ -56,6 +57,7 @@
  1399. stw r10,THREAD+KSP_LIMIT(r2)
  1400. mtlr r0
  1401. blr
  1402. +#endif
  1403. /*
  1404. * void call_do_irq(struct pt_regs *regs, struct thread_info *irqtp);
  1405. diff -Nur linux-4.4.46.orig/arch/powerpc/kernel/misc_64.S linux-4.4.46/arch/powerpc/kernel/misc_64.S
  1406. --- linux-4.4.46.orig/arch/powerpc/kernel/misc_64.S 2017-02-01 08:31:11.000000000 +0100
  1407. +++ linux-4.4.46/arch/powerpc/kernel/misc_64.S 2017-02-03 17:18:05.643414939 +0100
  1408. @@ -30,6 +30,7 @@
  1409. .text
  1410. +#ifndef CONFIG_PREEMPT_RT_FULL
  1411. _GLOBAL(call_do_softirq)
  1412. mflr r0
  1413. std r0,16(r1)
  1414. @@ -40,6 +41,7 @@
  1415. ld r0,16(r1)
  1416. mtlr r0
  1417. blr
  1418. +#endif
  1419. _GLOBAL(call_do_irq)
  1420. mflr r0
  1421. diff -Nur linux-4.4.46.orig/arch/powerpc/kvm/book3s_hv.c linux-4.4.46/arch/powerpc/kvm/book3s_hv.c
  1422. --- linux-4.4.46.orig/arch/powerpc/kvm/book3s_hv.c 2017-02-01 08:31:11.000000000 +0100
  1423. +++ linux-4.4.46/arch/powerpc/kvm/book3s_hv.c 2017-02-03 17:18:05.643414939 +0100
  1424. @@ -114,11 +114,11 @@
  1425. static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
  1426. {
  1427. int cpu;
  1428. - wait_queue_head_t *wqp;
  1429. + struct swait_queue_head *wqp;
  1430. wqp = kvm_arch_vcpu_wq(vcpu);
  1431. - if (waitqueue_active(wqp)) {
  1432. - wake_up_interruptible(wqp);
  1433. + if (swait_active(wqp)) {
  1434. + swake_up(wqp);
  1435. ++vcpu->stat.halt_wakeup;
  1436. }
  1437. @@ -707,8 +707,8 @@
  1438. tvcpu->arch.prodded = 1;
  1439. smp_mb();
  1440. if (vcpu->arch.ceded) {
  1441. - if (waitqueue_active(&vcpu->wq)) {
  1442. - wake_up_interruptible(&vcpu->wq);
  1443. + if (swait_active(&vcpu->wq)) {
  1444. + swake_up(&vcpu->wq);
  1445. vcpu->stat.halt_wakeup++;
  1446. }
  1447. }
  1448. @@ -1453,7 +1453,7 @@
  1449. INIT_LIST_HEAD(&vcore->runnable_threads);
  1450. spin_lock_init(&vcore->lock);
  1451. spin_lock_init(&vcore->stoltb_lock);
  1452. - init_waitqueue_head(&vcore->wq);
  1453. + init_swait_queue_head(&vcore->wq);
  1454. vcore->preempt_tb = TB_NIL;
  1455. vcore->lpcr = kvm->arch.lpcr;
  1456. vcore->first_vcpuid = core * threads_per_subcore;
  1457. @@ -2525,10 +2525,9 @@
  1458. {
  1459. struct kvm_vcpu *vcpu;
  1460. int do_sleep = 1;
  1461. + DECLARE_SWAITQUEUE(wait);
  1462. - DEFINE_WAIT(wait);
  1463. -
  1464. - prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
  1465. + prepare_to_swait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
  1466. /*
  1467. * Check one last time for pending exceptions and ceded state after
  1468. @@ -2542,7 +2541,7 @@
  1469. }
  1470. if (!do_sleep) {
  1471. - finish_wait(&vc->wq, &wait);
  1472. + finish_swait(&vc->wq, &wait);
  1473. return;
  1474. }
  1475. @@ -2550,7 +2549,7 @@
  1476. trace_kvmppc_vcore_blocked(vc, 0);
  1477. spin_unlock(&vc->lock);
  1478. schedule();
  1479. - finish_wait(&vc->wq, &wait);
  1480. + finish_swait(&vc->wq, &wait);
  1481. spin_lock(&vc->lock);
  1482. vc->vcore_state = VCORE_INACTIVE;
  1483. trace_kvmppc_vcore_blocked(vc, 1);
  1484. @@ -2606,7 +2605,7 @@
  1485. kvmppc_start_thread(vcpu, vc);
  1486. trace_kvm_guest_enter(vcpu);
  1487. } else if (vc->vcore_state == VCORE_SLEEPING) {
  1488. - wake_up(&vc->wq);
  1489. + swake_up(&vc->wq);
  1490. }
  1491. }
  1492. diff -Nur linux-4.4.46.orig/arch/powerpc/kvm/Kconfig linux-4.4.46/arch/powerpc/kvm/Kconfig
  1493. --- linux-4.4.46.orig/arch/powerpc/kvm/Kconfig 2017-02-01 08:31:11.000000000 +0100
  1494. +++ linux-4.4.46/arch/powerpc/kvm/Kconfig 2017-02-03 17:18:05.643414939 +0100
  1495. @@ -172,6 +172,7 @@
  1496. config KVM_MPIC
  1497. bool "KVM in-kernel MPIC emulation"
  1498. depends on KVM && E500
  1499. + depends on !PREEMPT_RT_FULL
  1500. select HAVE_KVM_IRQCHIP
  1501. select HAVE_KVM_IRQFD
  1502. select HAVE_KVM_IRQ_ROUTING
  1503. diff -Nur linux-4.4.46.orig/arch/powerpc/platforms/ps3/device-init.c linux-4.4.46/arch/powerpc/platforms/ps3/device-init.c
  1504. --- linux-4.4.46.orig/arch/powerpc/platforms/ps3/device-init.c 2017-02-01 08:31:11.000000000 +0100
  1505. +++ linux-4.4.46/arch/powerpc/platforms/ps3/device-init.c 2017-02-03 17:18:05.643414939 +0100
  1506. @@ -752,7 +752,7 @@
  1507. }
  1508. pr_debug("%s:%u: notification %s issued\n", __func__, __LINE__, op);
  1509. - res = wait_event_interruptible(dev->done.wait,
  1510. + res = swait_event_interruptible(dev->done.wait,
  1511. dev->done.done || kthread_should_stop());
  1512. if (kthread_should_stop())
  1513. res = -EINTR;
  1514. diff -Nur linux-4.4.46.orig/arch/s390/include/asm/kvm_host.h linux-4.4.46/arch/s390/include/asm/kvm_host.h
  1515. --- linux-4.4.46.orig/arch/s390/include/asm/kvm_host.h 2017-02-01 08:31:11.000000000 +0100
  1516. +++ linux-4.4.46/arch/s390/include/asm/kvm_host.h 2017-02-03 17:18:05.643414939 +0100
  1517. @@ -427,7 +427,7 @@
  1518. struct kvm_s390_local_interrupt {
  1519. spinlock_t lock;
  1520. struct kvm_s390_float_interrupt *float_int;
  1521. - wait_queue_head_t *wq;
  1522. + struct swait_queue_head *wq;
  1523. atomic_t *cpuflags;
  1524. DECLARE_BITMAP(sigp_emerg_pending, KVM_MAX_VCPUS);
  1525. struct kvm_s390_irq_payload irq;
  1526. diff -Nur linux-4.4.46.orig/arch/s390/kvm/interrupt.c linux-4.4.46/arch/s390/kvm/interrupt.c
  1527. --- linux-4.4.46.orig/arch/s390/kvm/interrupt.c 2017-02-01 08:31:11.000000000 +0100
  1528. +++ linux-4.4.46/arch/s390/kvm/interrupt.c 2017-02-03 17:18:05.643414939 +0100
  1529. @@ -868,13 +868,13 @@
  1530. void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu)
  1531. {
  1532. - if (waitqueue_active(&vcpu->wq)) {
  1533. + if (swait_active(&vcpu->wq)) {
  1534. /*
  1535. * The vcpu gave up the cpu voluntarily, mark it as a good
  1536. * yield-candidate.
  1537. */
  1538. vcpu->preempted = true;
  1539. - wake_up_interruptible(&vcpu->wq);
  1540. + swake_up(&vcpu->wq);
  1541. vcpu->stat.halt_wakeup++;
  1542. }
  1543. }
  1544. diff -Nur linux-4.4.46.orig/arch/sh/kernel/irq.c linux-4.4.46/arch/sh/kernel/irq.c
  1545. --- linux-4.4.46.orig/arch/sh/kernel/irq.c 2017-02-01 08:31:11.000000000 +0100
  1546. +++ linux-4.4.46/arch/sh/kernel/irq.c 2017-02-03 17:18:05.643414939 +0100
  1547. @@ -147,6 +147,7 @@
  1548. hardirq_ctx[cpu] = NULL;
  1549. }
  1550. +#ifndef CONFIG_PREEMPT_RT_FULL
  1551. void do_softirq_own_stack(void)
  1552. {
  1553. struct thread_info *curctx;
  1554. @@ -174,6 +175,7 @@
  1555. "r5", "r6", "r7", "r8", "r9", "r15", "t", "pr"
  1556. );
  1557. }
  1558. +#endif
  1559. #else
  1560. static inline void handle_one_irq(unsigned int irq)
  1561. {
  1562. diff -Nur linux-4.4.46.orig/arch/sparc/Kconfig linux-4.4.46/arch/sparc/Kconfig
  1563. --- linux-4.4.46.orig/arch/sparc/Kconfig 2017-02-01 08:31:11.000000000 +0100
  1564. +++ linux-4.4.46/arch/sparc/Kconfig 2017-02-03 17:18:05.643414939 +0100
  1565. @@ -189,12 +189,10 @@
  1566. source kernel/Kconfig.hz
  1567. config RWSEM_GENERIC_SPINLOCK
  1568. - bool
  1569. - default y if SPARC32
  1570. + def_bool PREEMPT_RT_FULL
  1571. config RWSEM_XCHGADD_ALGORITHM
  1572. - bool
  1573. - default y if SPARC64
  1574. + def_bool !RWSEM_GENERIC_SPINLOCK && !PREEMPT_RT_FULL
  1575. config GENERIC_HWEIGHT
  1576. bool
  1577. diff -Nur linux-4.4.46.orig/arch/sparc/kernel/irq_64.c linux-4.4.46/arch/sparc/kernel/irq_64.c
  1578. --- linux-4.4.46.orig/arch/sparc/kernel/irq_64.c 2017-02-01 08:31:11.000000000 +0100
  1579. +++ linux-4.4.46/arch/sparc/kernel/irq_64.c 2017-02-03 17:18:05.643414939 +0100
  1580. @@ -854,6 +854,7 @@
  1581. set_irq_regs(old_regs);
  1582. }
  1583. +#ifndef CONFIG_PREEMPT_RT_FULL
  1584. void do_softirq_own_stack(void)
  1585. {
  1586. void *orig_sp, *sp = softirq_stack[smp_processor_id()];
  1587. @@ -868,6 +869,7 @@
  1588. __asm__ __volatile__("mov %0, %%sp"
  1589. : : "r" (orig_sp));
  1590. }
  1591. +#endif
  1592. #ifdef CONFIG_HOTPLUG_CPU
  1593. void fixup_irqs(void)
  1594. diff -Nur linux-4.4.46.orig/arch/x86/crypto/aesni-intel_glue.c linux-4.4.46/arch/x86/crypto/aesni-intel_glue.c
  1595. --- linux-4.4.46.orig/arch/x86/crypto/aesni-intel_glue.c 2017-02-01 08:31:11.000000000 +0100
  1596. +++ linux-4.4.46/arch/x86/crypto/aesni-intel_glue.c 2017-02-03 17:18:05.643414939 +0100
  1597. @@ -383,14 +383,14 @@
  1598. err = blkcipher_walk_virt(desc, &walk);
  1599. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  1600. - kernel_fpu_begin();
  1601. while ((nbytes = walk.nbytes)) {
  1602. + kernel_fpu_begin();
  1603. aesni_ecb_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  1604. - nbytes & AES_BLOCK_MASK);
  1605. + nbytes & AES_BLOCK_MASK);
  1606. + kernel_fpu_end();
  1607. nbytes &= AES_BLOCK_SIZE - 1;
  1608. err = blkcipher_walk_done(desc, &walk, nbytes);
  1609. }
  1610. - kernel_fpu_end();
  1611. return err;
  1612. }
  1613. @@ -407,14 +407,14 @@
  1614. err = blkcipher_walk_virt(desc, &walk);
  1615. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  1616. - kernel_fpu_begin();
  1617. while ((nbytes = walk.nbytes)) {
  1618. + kernel_fpu_begin();
  1619. aesni_ecb_dec(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  1620. nbytes & AES_BLOCK_MASK);
  1621. + kernel_fpu_end();
  1622. nbytes &= AES_BLOCK_SIZE - 1;
  1623. err = blkcipher_walk_done(desc, &walk, nbytes);
  1624. }
  1625. - kernel_fpu_end();
  1626. return err;
  1627. }
  1628. @@ -431,14 +431,14 @@
  1629. err = blkcipher_walk_virt(desc, &walk);
  1630. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  1631. - kernel_fpu_begin();
  1632. while ((nbytes = walk.nbytes)) {
  1633. + kernel_fpu_begin();
  1634. aesni_cbc_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  1635. nbytes & AES_BLOCK_MASK, walk.iv);
  1636. + kernel_fpu_end();
  1637. nbytes &= AES_BLOCK_SIZE - 1;
  1638. err = blkcipher_walk_done(desc, &walk, nbytes);
  1639. }
  1640. - kernel_fpu_end();
  1641. return err;
  1642. }
  1643. @@ -455,14 +455,14 @@
  1644. err = blkcipher_walk_virt(desc, &walk);
  1645. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  1646. - kernel_fpu_begin();
  1647. while ((nbytes = walk.nbytes)) {
  1648. + kernel_fpu_begin();
  1649. aesni_cbc_dec(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  1650. nbytes & AES_BLOCK_MASK, walk.iv);
  1651. + kernel_fpu_end();
  1652. nbytes &= AES_BLOCK_SIZE - 1;
  1653. err = blkcipher_walk_done(desc, &walk, nbytes);
  1654. }
  1655. - kernel_fpu_end();
  1656. return err;
  1657. }
  1658. @@ -514,18 +514,20 @@
  1659. err = blkcipher_walk_virt_block(desc, &walk, AES_BLOCK_SIZE);
  1660. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  1661. - kernel_fpu_begin();
  1662. while ((nbytes = walk.nbytes) >= AES_BLOCK_SIZE) {
  1663. + kernel_fpu_begin();
  1664. aesni_ctr_enc_tfm(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  1665. nbytes & AES_BLOCK_MASK, walk.iv);
  1666. + kernel_fpu_end();
  1667. nbytes &= AES_BLOCK_SIZE - 1;
  1668. err = blkcipher_walk_done(desc, &walk, nbytes);
  1669. }
  1670. if (walk.nbytes) {
  1671. + kernel_fpu_begin();
  1672. ctr_crypt_final(ctx, &walk);
  1673. + kernel_fpu_end();
  1674. err = blkcipher_walk_done(desc, &walk, 0);
  1675. }
  1676. - kernel_fpu_end();
  1677. return err;
  1678. }
  1679. diff -Nur linux-4.4.46.orig/arch/x86/crypto/cast5_avx_glue.c linux-4.4.46/arch/x86/crypto/cast5_avx_glue.c
  1680. --- linux-4.4.46.orig/arch/x86/crypto/cast5_avx_glue.c 2017-02-01 08:31:11.000000000 +0100
  1681. +++ linux-4.4.46/arch/x86/crypto/cast5_avx_glue.c 2017-02-03 17:18:05.647415094 +0100
  1682. @@ -59,7 +59,7 @@
  1683. static int ecb_crypt(struct blkcipher_desc *desc, struct blkcipher_walk *walk,
  1684. bool enc)
  1685. {
  1686. - bool fpu_enabled = false;
  1687. + bool fpu_enabled;
  1688. struct cast5_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  1689. const unsigned int bsize = CAST5_BLOCK_SIZE;
  1690. unsigned int nbytes;
  1691. @@ -75,7 +75,7 @@
  1692. u8 *wsrc = walk->src.virt.addr;
  1693. u8 *wdst = walk->dst.virt.addr;
  1694. - fpu_enabled = cast5_fpu_begin(fpu_enabled, nbytes);
  1695. + fpu_enabled = cast5_fpu_begin(false, nbytes);
  1696. /* Process multi-block batch */
  1697. if (nbytes >= bsize * CAST5_PARALLEL_BLOCKS) {
  1698. @@ -103,10 +103,9 @@
  1699. } while (nbytes >= bsize);
  1700. done:
  1701. + cast5_fpu_end(fpu_enabled);
  1702. err = blkcipher_walk_done(desc, walk, nbytes);
  1703. }
  1704. -
  1705. - cast5_fpu_end(fpu_enabled);
  1706. return err;
  1707. }
  1708. @@ -227,7 +226,7 @@
  1709. static int cbc_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  1710. struct scatterlist *src, unsigned int nbytes)
  1711. {
  1712. - bool fpu_enabled = false;
  1713. + bool fpu_enabled;
  1714. struct blkcipher_walk walk;
  1715. int err;
  1716. @@ -236,12 +235,11 @@
  1717. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  1718. while ((nbytes = walk.nbytes)) {
  1719. - fpu_enabled = cast5_fpu_begin(fpu_enabled, nbytes);
  1720. + fpu_enabled = cast5_fpu_begin(false, nbytes);
  1721. nbytes = __cbc_decrypt(desc, &walk);
  1722. + cast5_fpu_end(fpu_enabled);
  1723. err = blkcipher_walk_done(desc, &walk, nbytes);
  1724. }
  1725. -
  1726. - cast5_fpu_end(fpu_enabled);
  1727. return err;
  1728. }
  1729. @@ -311,7 +309,7 @@
  1730. static int ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  1731. struct scatterlist *src, unsigned int nbytes)
  1732. {
  1733. - bool fpu_enabled = false;
  1734. + bool fpu_enabled;
  1735. struct blkcipher_walk walk;
  1736. int err;
  1737. @@ -320,13 +318,12 @@
  1738. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  1739. while ((nbytes = walk.nbytes) >= CAST5_BLOCK_SIZE) {
  1740. - fpu_enabled = cast5_fpu_begin(fpu_enabled, nbytes);
  1741. + fpu_enabled = cast5_fpu_begin(false, nbytes);
  1742. nbytes = __ctr_crypt(desc, &walk);
  1743. + cast5_fpu_end(fpu_enabled);
  1744. err = blkcipher_walk_done(desc, &walk, nbytes);
  1745. }
  1746. - cast5_fpu_end(fpu_enabled);
  1747. -
  1748. if (walk.nbytes) {
  1749. ctr_crypt_final(desc, &walk);
  1750. err = blkcipher_walk_done(desc, &walk, 0);
  1751. diff -Nur linux-4.4.46.orig/arch/x86/crypto/glue_helper.c linux-4.4.46/arch/x86/crypto/glue_helper.c
  1752. --- linux-4.4.46.orig/arch/x86/crypto/glue_helper.c 2017-02-01 08:31:11.000000000 +0100
  1753. +++ linux-4.4.46/arch/x86/crypto/glue_helper.c 2017-02-03 17:18:05.647415094 +0100
  1754. @@ -39,7 +39,7 @@
  1755. void *ctx = crypto_blkcipher_ctx(desc->tfm);
  1756. const unsigned int bsize = 128 / 8;
  1757. unsigned int nbytes, i, func_bytes;
  1758. - bool fpu_enabled = false;
  1759. + bool fpu_enabled;
  1760. int err;
  1761. err = blkcipher_walk_virt(desc, walk);
  1762. @@ -49,7 +49,7 @@
  1763. u8 *wdst = walk->dst.virt.addr;
  1764. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  1765. - desc, fpu_enabled, nbytes);
  1766. + desc, false, nbytes);
  1767. for (i = 0; i < gctx->num_funcs; i++) {
  1768. func_bytes = bsize * gctx->funcs[i].num_blocks;
  1769. @@ -71,10 +71,10 @@
  1770. }
  1771. done:
  1772. + glue_fpu_end(fpu_enabled);
  1773. err = blkcipher_walk_done(desc, walk, nbytes);
  1774. }
  1775. - glue_fpu_end(fpu_enabled);
  1776. return err;
  1777. }
  1778. @@ -194,7 +194,7 @@
  1779. struct scatterlist *src, unsigned int nbytes)
  1780. {
  1781. const unsigned int bsize = 128 / 8;
  1782. - bool fpu_enabled = false;
  1783. + bool fpu_enabled;
  1784. struct blkcipher_walk walk;
  1785. int err;
  1786. @@ -203,12 +203,12 @@
  1787. while ((nbytes = walk.nbytes)) {
  1788. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  1789. - desc, fpu_enabled, nbytes);
  1790. + desc, false, nbytes);
  1791. nbytes = __glue_cbc_decrypt_128bit(gctx, desc, &walk);
  1792. + glue_fpu_end(fpu_enabled);
  1793. err = blkcipher_walk_done(desc, &walk, nbytes);
  1794. }
  1795. - glue_fpu_end(fpu_enabled);
  1796. return err;
  1797. }
  1798. EXPORT_SYMBOL_GPL(glue_cbc_decrypt_128bit);
  1799. @@ -277,7 +277,7 @@
  1800. struct scatterlist *src, unsigned int nbytes)
  1801. {
  1802. const unsigned int bsize = 128 / 8;
  1803. - bool fpu_enabled = false;
  1804. + bool fpu_enabled;
  1805. struct blkcipher_walk walk;
  1806. int err;
  1807. @@ -286,13 +286,12 @@
  1808. while ((nbytes = walk.nbytes) >= bsize) {
  1809. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  1810. - desc, fpu_enabled, nbytes);
  1811. + desc, false, nbytes);
  1812. nbytes = __glue_ctr_crypt_128bit(gctx, desc, &walk);
  1813. + glue_fpu_end(fpu_enabled);
  1814. err = blkcipher_walk_done(desc, &walk, nbytes);
  1815. }
  1816. - glue_fpu_end(fpu_enabled);
  1817. -
  1818. if (walk.nbytes) {
  1819. glue_ctr_crypt_final_128bit(
  1820. gctx->funcs[gctx->num_funcs - 1].fn_u.ctr, desc, &walk);
  1821. @@ -347,7 +346,7 @@
  1822. void *tweak_ctx, void *crypt_ctx)
  1823. {
  1824. const unsigned int bsize = 128 / 8;
  1825. - bool fpu_enabled = false;
  1826. + bool fpu_enabled;
  1827. struct blkcipher_walk walk;
  1828. int err;
  1829. @@ -360,21 +359,21 @@
  1830. /* set minimum length to bsize, for tweak_fn */
  1831. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  1832. - desc, fpu_enabled,
  1833. + desc, false,
  1834. nbytes < bsize ? bsize : nbytes);
  1835. -
  1836. /* calculate first value of T */
  1837. tweak_fn(tweak_ctx, walk.iv, walk.iv);
  1838. + glue_fpu_end(fpu_enabled);
  1839. while (nbytes) {
  1840. + fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  1841. + desc, false, nbytes);
  1842. nbytes = __glue_xts_crypt_128bit(gctx, crypt_ctx, desc, &walk);
  1843. + glue_fpu_end(fpu_enabled);
  1844. err = blkcipher_walk_done(desc, &walk, nbytes);
  1845. nbytes = walk.nbytes;
  1846. }
  1847. -
  1848. - glue_fpu_end(fpu_enabled);
  1849. -
  1850. return err;
  1851. }
  1852. EXPORT_SYMBOL_GPL(glue_xts_crypt_128bit);
  1853. diff -Nur linux-4.4.46.orig/arch/x86/entry/common.c linux-4.4.46/arch/x86/entry/common.c
  1854. --- linux-4.4.46.orig/arch/x86/entry/common.c 2017-02-01 08:31:11.000000000 +0100
  1855. +++ linux-4.4.46/arch/x86/entry/common.c 2017-02-03 17:18:05.647415094 +0100
  1856. @@ -220,7 +220,7 @@
  1857. #define EXIT_TO_USERMODE_LOOP_FLAGS \
  1858. (_TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_UPROBE | \
  1859. - _TIF_NEED_RESCHED | _TIF_USER_RETURN_NOTIFY)
  1860. + _TIF_NEED_RESCHED_MASK | _TIF_USER_RETURN_NOTIFY)
  1861. static void exit_to_usermode_loop(struct pt_regs *regs, u32 cached_flags)
  1862. {
  1863. @@ -236,9 +236,16 @@
  1864. /* We have work to do. */
  1865. local_irq_enable();
  1866. - if (cached_flags & _TIF_NEED_RESCHED)
  1867. + if (cached_flags & _TIF_NEED_RESCHED_MASK)
  1868. schedule();
  1869. +#ifdef ARCH_RT_DELAYS_SIGNAL_SEND
  1870. + if (unlikely(current->forced_info.si_signo)) {
  1871. + struct task_struct *t = current;
  1872. + force_sig_info(t->forced_info.si_signo, &t->forced_info, t);
  1873. + t->forced_info.si_signo = 0;
  1874. + }
  1875. +#endif
  1876. if (cached_flags & _TIF_UPROBE)
  1877. uprobe_notify_resume(regs);
  1878. diff -Nur linux-4.4.46.orig/arch/x86/entry/entry_32.S linux-4.4.46/arch/x86/entry/entry_32.S
  1879. --- linux-4.4.46.orig/arch/x86/entry/entry_32.S 2017-02-01 08:31:11.000000000 +0100
  1880. +++ linux-4.4.46/arch/x86/entry/entry_32.S 2017-02-03 17:18:05.647415094 +0100
  1881. @@ -278,8 +278,24 @@
  1882. ENTRY(resume_kernel)
  1883. DISABLE_INTERRUPTS(CLBR_ANY)
  1884. need_resched:
  1885. + # preempt count == 0 + NEED_RS set?
  1886. cmpl $0, PER_CPU_VAR(__preempt_count)
  1887. +#ifndef CONFIG_PREEMPT_LAZY
  1888. jnz restore_all
  1889. +#else
  1890. + jz test_int_off
  1891. +
  1892. + # atleast preempt count == 0 ?
  1893. + cmpl $_PREEMPT_ENABLED,PER_CPU_VAR(__preempt_count)
  1894. + jne restore_all
  1895. +
  1896. + cmpl $0,TI_preempt_lazy_count(%ebp) # non-zero preempt_lazy_count ?
  1897. + jnz restore_all
  1898. +
  1899. + testl $_TIF_NEED_RESCHED_LAZY, TI_flags(%ebp)
  1900. + jz restore_all
  1901. +test_int_off:
  1902. +#endif
  1903. testl $X86_EFLAGS_IF, PT_EFLAGS(%esp) # interrupts off (exception path) ?
  1904. jz restore_all
  1905. call preempt_schedule_irq
  1906. diff -Nur linux-4.4.46.orig/arch/x86/entry/entry_64.S linux-4.4.46/arch/x86/entry/entry_64.S
  1907. --- linux-4.4.46.orig/arch/x86/entry/entry_64.S 2017-02-01 08:31:11.000000000 +0100
  1908. +++ linux-4.4.46/arch/x86/entry/entry_64.S 2017-02-03 17:18:05.647415094 +0100
  1909. @@ -579,7 +579,23 @@
  1910. bt $9, EFLAGS(%rsp) /* were interrupts off? */
  1911. jnc 1f
  1912. 0: cmpl $0, PER_CPU_VAR(__preempt_count)
  1913. +#ifndef CONFIG_PREEMPT_LAZY
  1914. jnz 1f
  1915. +#else
  1916. + jz do_preempt_schedule_irq
  1917. +
  1918. + # atleast preempt count == 0 ?
  1919. + cmpl $_PREEMPT_ENABLED,PER_CPU_VAR(__preempt_count)
  1920. + jnz 1f
  1921. +
  1922. + GET_THREAD_INFO(%rcx)
  1923. + cmpl $0, TI_preempt_lazy_count(%rcx)
  1924. + jnz 1f
  1925. +
  1926. + bt $TIF_NEED_RESCHED_LAZY,TI_flags(%rcx)
  1927. + jnc 1f
  1928. +do_preempt_schedule_irq:
  1929. +#endif
  1930. call preempt_schedule_irq
  1931. jmp 0b
  1932. 1:
  1933. @@ -867,6 +883,7 @@
  1934. jmp 2b
  1935. .previous
  1936. +#ifndef CONFIG_PREEMPT_RT_FULL
  1937. /* Call softirq on interrupt stack. Interrupts are off. */
  1938. ENTRY(do_softirq_own_stack)
  1939. pushq %rbp
  1940. @@ -879,6 +896,7 @@
  1941. decl PER_CPU_VAR(irq_count)
  1942. ret
  1943. END(do_softirq_own_stack)
  1944. +#endif
  1945. #ifdef CONFIG_XEN
  1946. idtentry xen_hypervisor_callback xen_do_hypervisor_callback has_error_code=0
  1947. diff -Nur linux-4.4.46.orig/arch/x86/include/asm/preempt.h linux-4.4.46/arch/x86/include/asm/preempt.h
  1948. --- linux-4.4.46.orig/arch/x86/include/asm/preempt.h 2017-02-01 08:31:11.000000000 +0100
  1949. +++ linux-4.4.46/arch/x86/include/asm/preempt.h 2017-02-03 17:18:05.647415094 +0100
  1950. @@ -79,17 +79,46 @@
  1951. * a decrement which hits zero means we have no preempt_count and should
  1952. * reschedule.
  1953. */
  1954. -static __always_inline bool __preempt_count_dec_and_test(void)
  1955. +static __always_inline bool ____preempt_count_dec_and_test(void)
  1956. {
  1957. GEN_UNARY_RMWcc("decl", __preempt_count, __percpu_arg(0), "e");
  1958. }
  1959. +static __always_inline bool __preempt_count_dec_and_test(void)
  1960. +{
  1961. + if (____preempt_count_dec_and_test())
  1962. + return true;
  1963. +#ifdef CONFIG_PREEMPT_LAZY
  1964. + if (current_thread_info()->preempt_lazy_count)
  1965. + return false;
  1966. + return test_thread_flag(TIF_NEED_RESCHED_LAZY);
  1967. +#else
  1968. + return false;
  1969. +#endif
  1970. +}
  1971. +
  1972. /*
  1973. * Returns true when we need to resched and can (barring IRQ state).
  1974. */
  1975. static __always_inline bool should_resched(int preempt_offset)
  1976. {
  1977. +#ifdef CONFIG_PREEMPT_LAZY
  1978. + u32 tmp;
  1979. +
  1980. + tmp = raw_cpu_read_4(__preempt_count);
  1981. + if (tmp == preempt_offset)
  1982. + return true;
  1983. +
  1984. + /* preempt count == 0 ? */
  1985. + tmp &= ~PREEMPT_NEED_RESCHED;
  1986. + if (tmp)
  1987. + return false;
  1988. + if (current_thread_info()->preempt_lazy_count)
  1989. + return false;
  1990. + return test_thread_flag(TIF_NEED_RESCHED_LAZY);
  1991. +#else
  1992. return unlikely(raw_cpu_read_4(__preempt_count) == preempt_offset);
  1993. +#endif
  1994. }
  1995. #ifdef CONFIG_PREEMPT
  1996. diff -Nur linux-4.4.46.orig/arch/x86/include/asm/signal.h linux-4.4.46/arch/x86/include/asm/signal.h
  1997. --- linux-4.4.46.orig/arch/x86/include/asm/signal.h 2017-02-01 08:31:11.000000000 +0100
  1998. +++ linux-4.4.46/arch/x86/include/asm/signal.h 2017-02-03 17:18:05.647415094 +0100
  1999. @@ -23,6 +23,19 @@
  2000. unsigned long sig[_NSIG_WORDS];
  2001. } sigset_t;
  2002. +/*
  2003. + * Because some traps use the IST stack, we must keep preemption
  2004. + * disabled while calling do_trap(), but do_trap() may call
  2005. + * force_sig_info() which will grab the signal spin_locks for the
  2006. + * task, which in PREEMPT_RT_FULL are mutexes. By defining
  2007. + * ARCH_RT_DELAYS_SIGNAL_SEND the force_sig_info() will set
  2008. + * TIF_NOTIFY_RESUME and set up the signal to be sent on exit of the
  2009. + * trap.
  2010. + */
  2011. +#if defined(CONFIG_PREEMPT_RT_FULL)
  2012. +#define ARCH_RT_DELAYS_SIGNAL_SEND
  2013. +#endif
  2014. +
  2015. #ifndef CONFIG_COMPAT
  2016. typedef sigset_t compat_sigset_t;
  2017. #endif
  2018. diff -Nur linux-4.4.46.orig/arch/x86/include/asm/stackprotector.h linux-4.4.46/arch/x86/include/asm/stackprotector.h
  2019. --- linux-4.4.46.orig/arch/x86/include/asm/stackprotector.h 2017-02-01 08:31:11.000000000 +0100
  2020. +++ linux-4.4.46/arch/x86/include/asm/stackprotector.h 2017-02-03 17:18:05.647415094 +0100
  2021. @@ -59,7 +59,7 @@
  2022. */
  2023. static __always_inline void boot_init_stack_canary(void)
  2024. {
  2025. - u64 canary;
  2026. + u64 uninitialized_var(canary);
  2027. u64 tsc;
  2028. #ifdef CONFIG_X86_64
  2029. @@ -70,8 +70,15 @@
  2030. * of randomness. The TSC only matters for very early init,
  2031. * there it already has some randomness on most systems. Later
  2032. * on during the bootup the random pool has true entropy too.
  2033. + *
  2034. + * For preempt-rt we need to weaken the randomness a bit, as
  2035. + * we can't call into the random generator from atomic context
  2036. + * due to locking constraints. We just leave canary
  2037. + * uninitialized and use the TSC based randomness on top of it.
  2038. */
  2039. +#ifndef CONFIG_PREEMPT_RT_FULL
  2040. get_random_bytes(&canary, sizeof(canary));
  2041. +#endif
  2042. tsc = rdtsc();
  2043. canary += tsc + (tsc << 32UL);
  2044. diff -Nur linux-4.4.46.orig/arch/x86/include/asm/thread_info.h linux-4.4.46/arch/x86/include/asm/thread_info.h
  2045. --- linux-4.4.46.orig/arch/x86/include/asm/thread_info.h 2017-02-01 08:31:11.000000000 +0100
  2046. +++ linux-4.4.46/arch/x86/include/asm/thread_info.h 2017-02-03 17:18:05.647415094 +0100
  2047. @@ -58,6 +58,8 @@
  2048. __u32 status; /* thread synchronous flags */
  2049. __u32 cpu; /* current CPU */
  2050. mm_segment_t addr_limit;
  2051. + int preempt_lazy_count; /* 0 => lazy preemptable
  2052. + <0 => BUG */
  2053. unsigned int sig_on_uaccess_error:1;
  2054. unsigned int uaccess_err:1; /* uaccess failed */
  2055. };
  2056. @@ -95,6 +97,7 @@
  2057. #define TIF_SYSCALL_EMU 6 /* syscall emulation active */
  2058. #define TIF_SYSCALL_AUDIT 7 /* syscall auditing active */
  2059. #define TIF_SECCOMP 8 /* secure computing */
  2060. +#define TIF_NEED_RESCHED_LAZY 9 /* lazy rescheduling necessary */
  2061. #define TIF_USER_RETURN_NOTIFY 11 /* notify kernel of userspace return */
  2062. #define TIF_UPROBE 12 /* breakpointed or singlestepping */
  2063. #define TIF_NOTSC 16 /* TSC is not accessible in userland */
  2064. @@ -119,6 +122,7 @@
  2065. #define _TIF_SYSCALL_EMU (1 << TIF_SYSCALL_EMU)
  2066. #define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
  2067. #define _TIF_SECCOMP (1 << TIF_SECCOMP)
  2068. +#define _TIF_NEED_RESCHED_LAZY (1 << TIF_NEED_RESCHED_LAZY)
  2069. #define _TIF_USER_RETURN_NOTIFY (1 << TIF_USER_RETURN_NOTIFY)
  2070. #define _TIF_UPROBE (1 << TIF_UPROBE)
  2071. #define _TIF_NOTSC (1 << TIF_NOTSC)
  2072. @@ -152,6 +156,8 @@
  2073. #define _TIF_WORK_CTXSW_PREV (_TIF_WORK_CTXSW|_TIF_USER_RETURN_NOTIFY)
  2074. #define _TIF_WORK_CTXSW_NEXT (_TIF_WORK_CTXSW)
  2075. +#define _TIF_NEED_RESCHED_MASK (_TIF_NEED_RESCHED | _TIF_NEED_RESCHED_LAZY)
  2076. +
  2077. #define STACK_WARN (THREAD_SIZE/8)
  2078. /*
  2079. diff -Nur linux-4.4.46.orig/arch/x86/include/asm/uv/uv_bau.h linux-4.4.46/arch/x86/include/asm/uv/uv_bau.h
  2080. --- linux-4.4.46.orig/arch/x86/include/asm/uv/uv_bau.h 2017-02-01 08:31:11.000000000 +0100
  2081. +++ linux-4.4.46/arch/x86/include/asm/uv/uv_bau.h 2017-02-03 17:18:05.647415094 +0100
  2082. @@ -615,9 +615,9 @@
  2083. cycles_t send_message;
  2084. cycles_t period_end;
  2085. cycles_t period_time;
  2086. - spinlock_t uvhub_lock;
  2087. - spinlock_t queue_lock;
  2088. - spinlock_t disable_lock;
  2089. + raw_spinlock_t uvhub_lock;
  2090. + raw_spinlock_t queue_lock;
  2091. + raw_spinlock_t disable_lock;
  2092. /* tunables */
  2093. int max_concurr;
  2094. int max_concurr_const;
  2095. @@ -776,15 +776,15 @@
  2096. * to be lowered below the current 'v'. atomic_add_unless can only stop
  2097. * on equal.
  2098. */
  2099. -static inline int atomic_inc_unless_ge(spinlock_t *lock, atomic_t *v, int u)
  2100. +static inline int atomic_inc_unless_ge(raw_spinlock_t *lock, atomic_t *v, int u)
  2101. {
  2102. - spin_lock(lock);
  2103. + raw_spin_lock(lock);
  2104. if (atomic_read(v) >= u) {
  2105. - spin_unlock(lock);
  2106. + raw_spin_unlock(lock);
  2107. return 0;
  2108. }
  2109. atomic_inc(v);
  2110. - spin_unlock(lock);
  2111. + raw_spin_unlock(lock);
  2112. return 1;
  2113. }
  2114. diff -Nur linux-4.4.46.orig/arch/x86/include/asm/uv/uv_hub.h linux-4.4.46/arch/x86/include/asm/uv/uv_hub.h
  2115. --- linux-4.4.46.orig/arch/x86/include/asm/uv/uv_hub.h 2017-02-01 08:31:11.000000000 +0100
  2116. +++ linux-4.4.46/arch/x86/include/asm/uv/uv_hub.h 2017-02-03 17:18:05.647415094 +0100
  2117. @@ -492,7 +492,7 @@
  2118. unsigned short nr_online_cpus;
  2119. unsigned short pnode;
  2120. short memory_nid;
  2121. - spinlock_t nmi_lock; /* obsolete, see uv_hub_nmi */
  2122. + raw_spinlock_t nmi_lock; /* obsolete, see uv_hub_nmi */
  2123. unsigned long nmi_count; /* obsolete, see uv_hub_nmi */
  2124. };
  2125. extern struct uv_blade_info *uv_blade_info;
  2126. diff -Nur linux-4.4.46.orig/arch/x86/Kconfig linux-4.4.46/arch/x86/Kconfig
  2127. --- linux-4.4.46.orig/arch/x86/Kconfig 2017-02-01 08:31:11.000000000 +0100
  2128. +++ linux-4.4.46/arch/x86/Kconfig 2017-02-03 17:18:05.643414939 +0100
  2129. @@ -17,6 +17,7 @@
  2130. ### Arch settings
  2131. config X86
  2132. def_bool y
  2133. + select HAVE_PREEMPT_LAZY
  2134. select ACPI_LEGACY_TABLES_LOOKUP if ACPI
  2135. select ACPI_SYSTEM_POWER_STATES_SUPPORT if ACPI
  2136. select ANON_INODES
  2137. @@ -212,8 +213,11 @@
  2138. def_bool y
  2139. depends on ISA_DMA_API
  2140. +config RWSEM_GENERIC_SPINLOCK
  2141. + def_bool PREEMPT_RT_FULL
  2142. +
  2143. config RWSEM_XCHGADD_ALGORITHM
  2144. - def_bool y
  2145. + def_bool !RWSEM_GENERIC_SPINLOCK && !PREEMPT_RT_FULL
  2146. config GENERIC_CALIBRATE_DELAY
  2147. def_bool y
  2148. @@ -848,7 +852,7 @@
  2149. config MAXSMP
  2150. bool "Enable Maximum number of SMP Processors and NUMA Nodes"
  2151. depends on X86_64 && SMP && DEBUG_KERNEL
  2152. - select CPUMASK_OFFSTACK
  2153. + select CPUMASK_OFFSTACK if !PREEMPT_RT_FULL
  2154. ---help---
  2155. Enable maximum number of CPUS and NUMA Nodes for this architecture.
  2156. If unsure, say N.
  2157. diff -Nur linux-4.4.46.orig/arch/x86/kernel/acpi/boot.c linux-4.4.46/arch/x86/kernel/acpi/boot.c
  2158. --- linux-4.4.46.orig/arch/x86/kernel/acpi/boot.c 2017-02-01 08:31:11.000000000 +0100
  2159. +++ linux-4.4.46/arch/x86/kernel/acpi/boot.c 2017-02-03 17:18:05.655415404 +0100
  2160. @@ -87,7 +87,9 @@
  2161. * ->ioapic_mutex
  2162. * ->ioapic_lock
  2163. */
  2164. +#ifdef CONFIG_X86_IO_APIC
  2165. static DEFINE_MUTEX(acpi_ioapic_lock);
  2166. +#endif
  2167. /* --------------------------------------------------------------------------
  2168. Boot-time Configuration
  2169. diff -Nur linux-4.4.46.orig/arch/x86/kernel/apic/io_apic.c linux-4.4.46/arch/x86/kernel/apic/io_apic.c
  2170. --- linux-4.4.46.orig/arch/x86/kernel/apic/io_apic.c 2017-02-01 08:31:11.000000000 +0100
  2171. +++ linux-4.4.46/arch/x86/kernel/apic/io_apic.c 2017-02-03 17:18:05.655415404 +0100
  2172. @@ -1711,7 +1711,8 @@
  2173. static inline bool ioapic_irqd_mask(struct irq_data *data)
  2174. {
  2175. /* If we are moving the irq we need to mask it */
  2176. - if (unlikely(irqd_is_setaffinity_pending(data))) {
  2177. + if (unlikely(irqd_is_setaffinity_pending(data) &&
  2178. + !irqd_irq_inprogress(data))) {
  2179. mask_ioapic_irq(data);
  2180. return true;
  2181. }
  2182. diff -Nur linux-4.4.46.orig/arch/x86/kernel/apic/x2apic_uv_x.c linux-4.4.46/arch/x86/kernel/apic/x2apic_uv_x.c
  2183. --- linux-4.4.46.orig/arch/x86/kernel/apic/x2apic_uv_x.c 2017-02-01 08:31:11.000000000 +0100
  2184. +++ linux-4.4.46/arch/x86/kernel/apic/x2apic_uv_x.c 2017-02-03 17:18:05.655415404 +0100
  2185. @@ -947,7 +947,7 @@
  2186. uv_blade_info[blade].pnode = pnode;
  2187. uv_blade_info[blade].nr_possible_cpus = 0;
  2188. uv_blade_info[blade].nr_online_cpus = 0;
  2189. - spin_lock_init(&uv_blade_info[blade].nmi_lock);
  2190. + raw_spin_lock_init(&uv_blade_info[blade].nmi_lock);
  2191. min_pnode = min(pnode, min_pnode);
  2192. max_pnode = max(pnode, max_pnode);
  2193. blade++;
  2194. diff -Nur linux-4.4.46.orig/arch/x86/kernel/asm-offsets.c linux-4.4.46/arch/x86/kernel/asm-offsets.c
  2195. --- linux-4.4.46.orig/arch/x86/kernel/asm-offsets.c 2017-02-01 08:31:11.000000000 +0100
  2196. +++ linux-4.4.46/arch/x86/kernel/asm-offsets.c 2017-02-03 17:18:05.655415404 +0100
  2197. @@ -32,6 +32,7 @@
  2198. OFFSET(TI_flags, thread_info, flags);
  2199. OFFSET(TI_status, thread_info, status);
  2200. OFFSET(TI_addr_limit, thread_info, addr_limit);
  2201. + OFFSET(TI_preempt_lazy_count, thread_info, preempt_lazy_count);
  2202. BLANK();
  2203. OFFSET(crypto_tfm_ctx_offset, crypto_tfm, __crt_ctx);
  2204. @@ -89,4 +90,5 @@
  2205. BLANK();
  2206. DEFINE(PTREGS_SIZE, sizeof(struct pt_regs));
  2207. + DEFINE(_PREEMPT_ENABLED, PREEMPT_ENABLED);
  2208. }
  2209. diff -Nur linux-4.4.46.orig/arch/x86/kernel/cpu/mcheck/mce.c linux-4.4.46/arch/x86/kernel/cpu/mcheck/mce.c
  2210. --- linux-4.4.46.orig/arch/x86/kernel/cpu/mcheck/mce.c 2017-02-01 08:31:11.000000000 +0100
  2211. +++ linux-4.4.46/arch/x86/kernel/cpu/mcheck/mce.c 2017-02-03 17:18:05.659415557 +0100
  2212. @@ -41,6 +41,8 @@
  2213. #include <linux/debugfs.h>
  2214. #include <linux/irq_work.h>
  2215. #include <linux/export.h>
  2216. +#include <linux/jiffies.h>
  2217. +#include <linux/swork.h>
  2218. #include <asm/processor.h>
  2219. #include <asm/traps.h>
  2220. @@ -1236,7 +1238,7 @@
  2221. static unsigned long check_interval = INITIAL_CHECK_INTERVAL;
  2222. static DEFINE_PER_CPU(unsigned long, mce_next_interval); /* in jiffies */
  2223. -static DEFINE_PER_CPU(struct timer_list, mce_timer);
  2224. +static DEFINE_PER_CPU(struct hrtimer, mce_timer);
  2225. static unsigned long mce_adjust_timer_default(unsigned long interval)
  2226. {
  2227. @@ -1245,32 +1247,18 @@
  2228. static unsigned long (*mce_adjust_timer)(unsigned long interval) = mce_adjust_timer_default;
  2229. -static void __restart_timer(struct timer_list *t, unsigned long interval)
  2230. +static enum hrtimer_restart __restart_timer(struct hrtimer *timer, unsigned long interval)
  2231. {
  2232. - unsigned long when = jiffies + interval;
  2233. - unsigned long flags;
  2234. -
  2235. - local_irq_save(flags);
  2236. -
  2237. - if (timer_pending(t)) {
  2238. - if (time_before(when, t->expires))
  2239. - mod_timer_pinned(t, when);
  2240. - } else {
  2241. - t->expires = round_jiffies(when);
  2242. - add_timer_on(t, smp_processor_id());
  2243. - }
  2244. -
  2245. - local_irq_restore(flags);
  2246. + if (!interval)
  2247. + return HRTIMER_NORESTART;
  2248. + hrtimer_forward_now(timer, ns_to_ktime(jiffies_to_nsecs(interval)));
  2249. + return HRTIMER_RESTART;
  2250. }
  2251. -static void mce_timer_fn(unsigned long data)
  2252. +static enum hrtimer_restart mce_timer_fn(struct hrtimer *timer)
  2253. {
  2254. - struct timer_list *t = this_cpu_ptr(&mce_timer);
  2255. - int cpu = smp_processor_id();
  2256. unsigned long iv;
  2257. - WARN_ON(cpu != data);
  2258. -
  2259. iv = __this_cpu_read(mce_next_interval);
  2260. if (mce_available(this_cpu_ptr(&cpu_info))) {
  2261. @@ -1293,7 +1281,7 @@
  2262. done:
  2263. __this_cpu_write(mce_next_interval, iv);
  2264. - __restart_timer(t, iv);
  2265. + return __restart_timer(timer, iv);
  2266. }
  2267. /*
  2268. @@ -1301,7 +1289,7 @@
  2269. */
  2270. void mce_timer_kick(unsigned long interval)
  2271. {
  2272. - struct timer_list *t = this_cpu_ptr(&mce_timer);
  2273. + struct hrtimer *t = this_cpu_ptr(&mce_timer);
  2274. unsigned long iv = __this_cpu_read(mce_next_interval);
  2275. __restart_timer(t, interval);
  2276. @@ -1316,7 +1304,7 @@
  2277. int cpu;
  2278. for_each_online_cpu(cpu)
  2279. - del_timer_sync(&per_cpu(mce_timer, cpu));
  2280. + hrtimer_cancel(&per_cpu(mce_timer, cpu));
  2281. }
  2282. static void mce_do_trigger(struct work_struct *work)
  2283. @@ -1326,6 +1314,56 @@
  2284. static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
  2285. +static void __mce_notify_work(struct swork_event *event)
  2286. +{
  2287. + /* Not more than two messages every minute */
  2288. + static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
  2289. +
  2290. + /* wake processes polling /dev/mcelog */
  2291. + wake_up_interruptible(&mce_chrdev_wait);
  2292. +
  2293. + /*
  2294. + * There is no risk of missing notifications because
  2295. + * work_pending is always cleared before the function is
  2296. + * executed.
  2297. + */
  2298. + if (mce_helper[0] && !work_pending(&mce_trigger_work))
  2299. + schedule_work(&mce_trigger_work);
  2300. +
  2301. + if (__ratelimit(&ratelimit))
  2302. + pr_info(HW_ERR "Machine check events logged\n");
  2303. +}
  2304. +
  2305. +#ifdef CONFIG_PREEMPT_RT_FULL
  2306. +static bool notify_work_ready __read_mostly;
  2307. +static struct swork_event notify_work;
  2308. +
  2309. +static int mce_notify_work_init(void)
  2310. +{
  2311. + int err;
  2312. +
  2313. + err = swork_get();
  2314. + if (err)
  2315. + return err;
  2316. +
  2317. + INIT_SWORK(&notify_work, __mce_notify_work);
  2318. + notify_work_ready = true;
  2319. + return 0;
  2320. +}
  2321. +
  2322. +static void mce_notify_work(void)
  2323. +{
  2324. + if (notify_work_ready)
  2325. + swork_queue(&notify_work);
  2326. +}
  2327. +#else
  2328. +static void mce_notify_work(void)
  2329. +{
  2330. + __mce_notify_work(NULL);
  2331. +}
  2332. +static inline int mce_notify_work_init(void) { return 0; }
  2333. +#endif
  2334. +
  2335. /*
  2336. * Notify the user(s) about new machine check events.
  2337. * Can be called from interrupt context, but not from machine check/NMI
  2338. @@ -1333,19 +1371,8 @@
  2339. */
  2340. int mce_notify_irq(void)
  2341. {
  2342. - /* Not more than two messages every minute */
  2343. - static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
  2344. -
  2345. if (test_and_clear_bit(0, &mce_need_notify)) {
  2346. - /* wake processes polling /dev/mcelog */
  2347. - wake_up_interruptible(&mce_chrdev_wait);
  2348. -
  2349. - if (mce_helper[0])
  2350. - schedule_work(&mce_trigger_work);
  2351. -
  2352. - if (__ratelimit(&ratelimit))
  2353. - pr_info(HW_ERR "Machine check events logged\n");
  2354. -
  2355. + mce_notify_work();
  2356. return 1;
  2357. }
  2358. return 0;
  2359. @@ -1639,7 +1666,7 @@
  2360. }
  2361. }
  2362. -static void mce_start_timer(unsigned int cpu, struct timer_list *t)
  2363. +static void mce_start_timer(unsigned int cpu, struct hrtimer *t)
  2364. {
  2365. unsigned long iv = check_interval * HZ;
  2366. @@ -1648,16 +1675,17 @@
  2367. per_cpu(mce_next_interval, cpu) = iv;
  2368. - t->expires = round_jiffies(jiffies + iv);
  2369. - add_timer_on(t, cpu);
  2370. + hrtimer_start_range_ns(t, ns_to_ktime(jiffies_to_usecs(iv) * 1000ULL),
  2371. + 0, HRTIMER_MODE_REL_PINNED);
  2372. }
  2373. static void __mcheck_cpu_init_timer(void)
  2374. {
  2375. - struct timer_list *t = this_cpu_ptr(&mce_timer);
  2376. + struct hrtimer *t = this_cpu_ptr(&mce_timer);
  2377. unsigned int cpu = smp_processor_id();
  2378. - setup_timer(t, mce_timer_fn, cpu);
  2379. + hrtimer_init(t, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  2380. + t->function = mce_timer_fn;
  2381. mce_start_timer(cpu, t);
  2382. }
  2383. @@ -2376,6 +2404,8 @@
  2384. if (!mce_available(raw_cpu_ptr(&cpu_info)))
  2385. return;
  2386. + hrtimer_cancel(this_cpu_ptr(&mce_timer));
  2387. +
  2388. if (!(action & CPU_TASKS_FROZEN))
  2389. cmci_clear();
  2390. @@ -2398,6 +2428,7 @@
  2391. if (b->init)
  2392. wrmsrl(MSR_IA32_MCx_CTL(i), b->ctl);
  2393. }
  2394. + __mcheck_cpu_init_timer();
  2395. }
  2396. /* Get notified when a cpu comes on/off. Be hotplug friendly. */
  2397. @@ -2405,7 +2436,6 @@
  2398. mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
  2399. {
  2400. unsigned int cpu = (unsigned long)hcpu;
  2401. - struct timer_list *t = &per_cpu(mce_timer, cpu);
  2402. switch (action & ~CPU_TASKS_FROZEN) {
  2403. case CPU_ONLINE:
  2404. @@ -2425,11 +2455,9 @@
  2405. break;
  2406. case CPU_DOWN_PREPARE:
  2407. smp_call_function_single(cpu, mce_disable_cpu, &action, 1);
  2408. - del_timer_sync(t);
  2409. break;
  2410. case CPU_DOWN_FAILED:
  2411. smp_call_function_single(cpu, mce_reenable_cpu, &action, 1);
  2412. - mce_start_timer(cpu, t);
  2413. break;
  2414. }
  2415. @@ -2468,6 +2496,10 @@
  2416. goto err_out;
  2417. }
  2418. + err = mce_notify_work_init();
  2419. + if (err)
  2420. + goto err_out;
  2421. +
  2422. if (!zalloc_cpumask_var(&mce_device_initialized, GFP_KERNEL)) {
  2423. err = -ENOMEM;
  2424. goto err_out;
  2425. diff -Nur linux-4.4.46.orig/arch/x86/kernel/cpu/perf_event_intel_rapl.c linux-4.4.46/arch/x86/kernel/cpu/perf_event_intel_rapl.c
  2426. --- linux-4.4.46.orig/arch/x86/kernel/cpu/perf_event_intel_rapl.c 2017-02-01 08:31:11.000000000 +0100
  2427. +++ linux-4.4.46/arch/x86/kernel/cpu/perf_event_intel_rapl.c 2017-02-03 17:18:05.663415712 +0100
  2428. @@ -117,7 +117,7 @@
  2429. };
  2430. struct rapl_pmu {
  2431. - spinlock_t lock;
  2432. + raw_spinlock_t lock;
  2433. int n_active; /* number of active events */
  2434. struct list_head active_list;
  2435. struct pmu *pmu; /* pointer to rapl_pmu_class */
  2436. @@ -220,13 +220,13 @@
  2437. if (!pmu->n_active)
  2438. return HRTIMER_NORESTART;
  2439. - spin_lock_irqsave(&pmu->lock, flags);
  2440. + raw_spin_lock_irqsave(&pmu->lock, flags);
  2441. list_for_each_entry(event, &pmu->active_list, active_entry) {
  2442. rapl_event_update(event);
  2443. }
  2444. - spin_unlock_irqrestore(&pmu->lock, flags);
  2445. + raw_spin_unlock_irqrestore(&pmu->lock, flags);
  2446. hrtimer_forward_now(hrtimer, pmu->timer_interval);
  2447. @@ -263,9 +263,9 @@
  2448. struct rapl_pmu *pmu = __this_cpu_read(rapl_pmu);
  2449. unsigned long flags;
  2450. - spin_lock_irqsave(&pmu->lock, flags);
  2451. + raw_spin_lock_irqsave(&pmu->lock, flags);
  2452. __rapl_pmu_event_start(pmu, event);
  2453. - spin_unlock_irqrestore(&pmu->lock, flags);
  2454. + raw_spin_unlock_irqrestore(&pmu->lock, flags);
  2455. }
  2456. static void rapl_pmu_event_stop(struct perf_event *event, int mode)
  2457. @@ -274,7 +274,7 @@
  2458. struct hw_perf_event *hwc = &event->hw;
  2459. unsigned long flags;
  2460. - spin_lock_irqsave(&pmu->lock, flags);
  2461. + raw_spin_lock_irqsave(&pmu->lock, flags);
  2462. /* mark event as deactivated and stopped */
  2463. if (!(hwc->state & PERF_HES_STOPPED)) {
  2464. @@ -299,7 +299,7 @@
  2465. hwc->state |= PERF_HES_UPTODATE;
  2466. }
  2467. - spin_unlock_irqrestore(&pmu->lock, flags);
  2468. + raw_spin_unlock_irqrestore(&pmu->lock, flags);
  2469. }
  2470. static int rapl_pmu_event_add(struct perf_event *event, int mode)
  2471. @@ -308,14 +308,14 @@
  2472. struct hw_perf_event *hwc = &event->hw;
  2473. unsigned long flags;
  2474. - spin_lock_irqsave(&pmu->lock, flags);
  2475. + raw_spin_lock_irqsave(&pmu->lock, flags);
  2476. hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  2477. if (mode & PERF_EF_START)
  2478. __rapl_pmu_event_start(pmu, event);
  2479. - spin_unlock_irqrestore(&pmu->lock, flags);
  2480. + raw_spin_unlock_irqrestore(&pmu->lock, flags);
  2481. return 0;
  2482. }
  2483. @@ -603,7 +603,7 @@
  2484. pmu = kzalloc_node(sizeof(*pmu), GFP_KERNEL, cpu_to_node(cpu));
  2485. if (!pmu)
  2486. return -1;
  2487. - spin_lock_init(&pmu->lock);
  2488. + raw_spin_lock_init(&pmu->lock);
  2489. INIT_LIST_HEAD(&pmu->active_list);
  2490. diff -Nur linux-4.4.46.orig/arch/x86/kernel/dumpstack_32.c linux-4.4.46/arch/x86/kernel/dumpstack_32.c
  2491. --- linux-4.4.46.orig/arch/x86/kernel/dumpstack_32.c 2017-02-01 08:31:11.000000000 +0100
  2492. +++ linux-4.4.46/arch/x86/kernel/dumpstack_32.c 2017-02-03 17:18:05.663415712 +0100
  2493. @@ -42,7 +42,7 @@
  2494. unsigned long *stack, unsigned long bp,
  2495. const struct stacktrace_ops *ops, void *data)
  2496. {
  2497. - const unsigned cpu = get_cpu();
  2498. + const unsigned cpu = get_cpu_light();
  2499. int graph = 0;
  2500. u32 *prev_esp;
  2501. @@ -86,7 +86,7 @@
  2502. break;
  2503. touch_nmi_watchdog();
  2504. }
  2505. - put_cpu();
  2506. + put_cpu_light();
  2507. }
  2508. EXPORT_SYMBOL(dump_trace);
  2509. diff -Nur linux-4.4.46.orig/arch/x86/kernel/dumpstack_64.c linux-4.4.46/arch/x86/kernel/dumpstack_64.c
  2510. --- linux-4.4.46.orig/arch/x86/kernel/dumpstack_64.c 2017-02-01 08:31:11.000000000 +0100
  2511. +++ linux-4.4.46/arch/x86/kernel/dumpstack_64.c 2017-02-03 17:18:05.663415712 +0100
  2512. @@ -152,7 +152,7 @@
  2513. unsigned long *stack, unsigned long bp,
  2514. const struct stacktrace_ops *ops, void *data)
  2515. {
  2516. - const unsigned cpu = get_cpu();
  2517. + const unsigned cpu = get_cpu_light();
  2518. struct thread_info *tinfo;
  2519. unsigned long *irq_stack = (unsigned long *)per_cpu(irq_stack_ptr, cpu);
  2520. unsigned long dummy;
  2521. @@ -241,7 +241,7 @@
  2522. * This handles the process stack:
  2523. */
  2524. bp = ops->walk_stack(tinfo, stack, bp, ops, data, NULL, &graph);
  2525. - put_cpu();
  2526. + put_cpu_light();
  2527. }
  2528. EXPORT_SYMBOL(dump_trace);
  2529. @@ -255,7 +255,7 @@
  2530. int cpu;
  2531. int i;
  2532. - preempt_disable();
  2533. + migrate_disable();
  2534. cpu = smp_processor_id();
  2535. irq_stack_end = (unsigned long *)(per_cpu(irq_stack_ptr, cpu));
  2536. @@ -291,7 +291,7 @@
  2537. pr_cont(" %016lx", *stack++);
  2538. touch_nmi_watchdog();
  2539. }
  2540. - preempt_enable();
  2541. + migrate_enable();
  2542. pr_cont("\n");
  2543. show_trace_log_lvl(task, regs, sp, bp, log_lvl);
  2544. diff -Nur linux-4.4.46.orig/arch/x86/kernel/irq_32.c linux-4.4.46/arch/x86/kernel/irq_32.c
  2545. --- linux-4.4.46.orig/arch/x86/kernel/irq_32.c 2017-02-01 08:31:11.000000000 +0100
  2546. +++ linux-4.4.46/arch/x86/kernel/irq_32.c 2017-02-03 17:18:05.663415712 +0100
  2547. @@ -128,6 +128,7 @@
  2548. cpu, per_cpu(hardirq_stack, cpu), per_cpu(softirq_stack, cpu));
  2549. }
  2550. +#ifndef CONFIG_PREEMPT_RT_FULL
  2551. void do_softirq_own_stack(void)
  2552. {
  2553. struct thread_info *curstk;
  2554. @@ -146,6 +147,7 @@
  2555. call_on_stack(__do_softirq, isp);
  2556. }
  2557. +#endif
  2558. bool handle_irq(struct irq_desc *desc, struct pt_regs *regs)
  2559. {
  2560. diff -Nur linux-4.4.46.orig/arch/x86/kernel/kvm.c linux-4.4.46/arch/x86/kernel/kvm.c
  2561. --- linux-4.4.46.orig/arch/x86/kernel/kvm.c 2017-02-01 08:31:11.000000000 +0100
  2562. +++ linux-4.4.46/arch/x86/kernel/kvm.c 2017-02-03 17:18:05.663415712 +0100
  2563. @@ -36,6 +36,7 @@
  2564. #include <linux/kprobes.h>
  2565. #include <linux/debugfs.h>
  2566. #include <linux/nmi.h>
  2567. +#include <linux/swait.h>
  2568. #include <asm/timer.h>
  2569. #include <asm/cpu.h>
  2570. #include <asm/traps.h>
  2571. @@ -91,14 +92,14 @@
  2572. struct kvm_task_sleep_node {
  2573. struct hlist_node link;
  2574. - wait_queue_head_t wq;
  2575. + struct swait_queue_head wq;
  2576. u32 token;
  2577. int cpu;
  2578. bool halted;
  2579. };
  2580. static struct kvm_task_sleep_head {
  2581. - spinlock_t lock;
  2582. + raw_spinlock_t lock;
  2583. struct hlist_head list;
  2584. } async_pf_sleepers[KVM_TASK_SLEEP_HASHSIZE];
  2585. @@ -122,17 +123,17 @@
  2586. u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
  2587. struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
  2588. struct kvm_task_sleep_node n, *e;
  2589. - DEFINE_WAIT(wait);
  2590. + DECLARE_SWAITQUEUE(wait);
  2591. rcu_irq_enter();
  2592. - spin_lock(&b->lock);
  2593. + raw_spin_lock(&b->lock);
  2594. e = _find_apf_task(b, token);
  2595. if (e) {
  2596. /* dummy entry exist -> wake up was delivered ahead of PF */
  2597. hlist_del(&e->link);
  2598. kfree(e);
  2599. - spin_unlock(&b->lock);
  2600. + raw_spin_unlock(&b->lock);
  2601. rcu_irq_exit();
  2602. return;
  2603. @@ -141,13 +142,13 @@
  2604. n.token = token;
  2605. n.cpu = smp_processor_id();
  2606. n.halted = is_idle_task(current) || preempt_count() > 1;
  2607. - init_waitqueue_head(&n.wq);
  2608. + init_swait_queue_head(&n.wq);
  2609. hlist_add_head(&n.link, &b->list);
  2610. - spin_unlock(&b->lock);
  2611. + raw_spin_unlock(&b->lock);
  2612. for (;;) {
  2613. if (!n.halted)
  2614. - prepare_to_wait(&n.wq, &wait, TASK_UNINTERRUPTIBLE);
  2615. + prepare_to_swait(&n.wq, &wait, TASK_UNINTERRUPTIBLE);
  2616. if (hlist_unhashed(&n.link))
  2617. break;
  2618. @@ -166,7 +167,7 @@
  2619. }
  2620. }
  2621. if (!n.halted)
  2622. - finish_wait(&n.wq, &wait);
  2623. + finish_swait(&n.wq, &wait);
  2624. rcu_irq_exit();
  2625. return;
  2626. @@ -178,8 +179,8 @@
  2627. hlist_del_init(&n->link);
  2628. if (n->halted)
  2629. smp_send_reschedule(n->cpu);
  2630. - else if (waitqueue_active(&n->wq))
  2631. - wake_up(&n->wq);
  2632. + else if (swait_active(&n->wq))
  2633. + swake_up(&n->wq);
  2634. }
  2635. static void apf_task_wake_all(void)
  2636. @@ -189,14 +190,14 @@
  2637. for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++) {
  2638. struct hlist_node *p, *next;
  2639. struct kvm_task_sleep_head *b = &async_pf_sleepers[i];
  2640. - spin_lock(&b->lock);
  2641. + raw_spin_lock(&b->lock);
  2642. hlist_for_each_safe(p, next, &b->list) {
  2643. struct kvm_task_sleep_node *n =
  2644. hlist_entry(p, typeof(*n), link);
  2645. if (n->cpu == smp_processor_id())
  2646. apf_task_wake_one(n);
  2647. }
  2648. - spin_unlock(&b->lock);
  2649. + raw_spin_unlock(&b->lock);
  2650. }
  2651. }
  2652. @@ -212,7 +213,7 @@
  2653. }
  2654. again:
  2655. - spin_lock(&b->lock);
  2656. + raw_spin_lock(&b->lock);
  2657. n = _find_apf_task(b, token);
  2658. if (!n) {
  2659. /*
  2660. @@ -225,17 +226,17 @@
  2661. * Allocation failed! Busy wait while other cpu
  2662. * handles async PF.
  2663. */
  2664. - spin_unlock(&b->lock);
  2665. + raw_spin_unlock(&b->lock);
  2666. cpu_relax();
  2667. goto again;
  2668. }
  2669. n->token = token;
  2670. n->cpu = smp_processor_id();
  2671. - init_waitqueue_head(&n->wq);
  2672. + init_swait_queue_head(&n->wq);
  2673. hlist_add_head(&n->link, &b->list);
  2674. } else
  2675. apf_task_wake_one(n);
  2676. - spin_unlock(&b->lock);
  2677. + raw_spin_unlock(&b->lock);
  2678. return;
  2679. }
  2680. EXPORT_SYMBOL_GPL(kvm_async_pf_task_wake);
  2681. @@ -486,7 +487,7 @@
  2682. paravirt_ops_setup();
  2683. register_reboot_notifier(&kvm_pv_reboot_nb);
  2684. for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++)
  2685. - spin_lock_init(&async_pf_sleepers[i].lock);
  2686. + raw_spin_lock_init(&async_pf_sleepers[i].lock);
  2687. if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF))
  2688. x86_init.irqs.trap_init = kvm_apf_trap_init;
  2689. diff -Nur linux-4.4.46.orig/arch/x86/kernel/nmi.c linux-4.4.46/arch/x86/kernel/nmi.c
  2690. --- linux-4.4.46.orig/arch/x86/kernel/nmi.c 2017-02-01 08:31:11.000000000 +0100
  2691. +++ linux-4.4.46/arch/x86/kernel/nmi.c 2017-02-03 17:18:05.663415712 +0100
  2692. @@ -231,7 +231,7 @@
  2693. #endif
  2694. if (panic_on_unrecovered_nmi)
  2695. - panic("NMI: Not continuing");
  2696. + nmi_panic(regs, "NMI: Not continuing");
  2697. pr_emerg("Dazed and confused, but trying to continue\n");
  2698. @@ -255,8 +255,16 @@
  2699. reason, smp_processor_id());
  2700. show_regs(regs);
  2701. - if (panic_on_io_nmi)
  2702. - panic("NMI IOCK error: Not continuing");
  2703. + if (panic_on_io_nmi) {
  2704. + nmi_panic(regs, "NMI IOCK error: Not continuing");
  2705. +
  2706. + /*
  2707. + * If we end up here, it means we have received an NMI while
  2708. + * processing panic(). Simply return without delaying and
  2709. + * re-enabling NMIs.
  2710. + */
  2711. + return;
  2712. + }
  2713. /* Re-enable the IOCK line, wait for a few seconds */
  2714. reason = (reason & NMI_REASON_CLEAR_MASK) | NMI_REASON_CLEAR_IOCHK;
  2715. @@ -297,7 +305,7 @@
  2716. pr_emerg("Do you have a strange power saving mode enabled?\n");
  2717. if (unknown_nmi_panic || panic_on_unrecovered_nmi)
  2718. - panic("NMI: Not continuing");
  2719. + nmi_panic(regs, "NMI: Not continuing");
  2720. pr_emerg("Dazed and confused, but trying to continue\n");
  2721. }
  2722. diff -Nur linux-4.4.46.orig/arch/x86/kernel/process_32.c linux-4.4.46/arch/x86/kernel/process_32.c
  2723. --- linux-4.4.46.orig/arch/x86/kernel/process_32.c 2017-02-01 08:31:11.000000000 +0100
  2724. +++ linux-4.4.46/arch/x86/kernel/process_32.c 2017-02-03 17:18:05.663415712 +0100
  2725. @@ -35,6 +35,7 @@
  2726. #include <linux/uaccess.h>
  2727. #include <linux/io.h>
  2728. #include <linux/kdebug.h>
  2729. +#include <linux/highmem.h>
  2730. #include <asm/pgtable.h>
  2731. #include <asm/ldt.h>
  2732. @@ -210,6 +211,35 @@
  2733. }
  2734. EXPORT_SYMBOL_GPL(start_thread);
  2735. +#ifdef CONFIG_PREEMPT_RT_FULL
  2736. +static void switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p)
  2737. +{
  2738. + int i;
  2739. +
  2740. + /*
  2741. + * Clear @prev's kmap_atomic mappings
  2742. + */
  2743. + for (i = 0; i < prev_p->kmap_idx; i++) {
  2744. + int idx = i + KM_TYPE_NR * smp_processor_id();
  2745. + pte_t *ptep = kmap_pte - idx;
  2746. +
  2747. + kpte_clear_flush(ptep, __fix_to_virt(FIX_KMAP_BEGIN + idx));
  2748. + }
  2749. + /*
  2750. + * Restore @next_p's kmap_atomic mappings
  2751. + */
  2752. + for (i = 0; i < next_p->kmap_idx; i++) {
  2753. + int idx = i + KM_TYPE_NR * smp_processor_id();
  2754. +
  2755. + if (!pte_none(next_p->kmap_pte[i]))
  2756. + set_pte(kmap_pte - idx, next_p->kmap_pte[i]);
  2757. + }
  2758. +}
  2759. +#else
  2760. +static inline void
  2761. +switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p) { }
  2762. +#endif
  2763. +
  2764. /*
  2765. * switch_to(x,y) should switch tasks from x to y.
  2766. @@ -286,6 +316,8 @@
  2767. task_thread_info(next_p)->flags & _TIF_WORK_CTXSW_NEXT))
  2768. __switch_to_xtra(prev_p, next_p, tss);
  2769. + switch_kmaps(prev_p, next_p);
  2770. +
  2771. /*
  2772. * Leave lazy mode, flushing any hypercalls made here.
  2773. * This must be done before restoring TLS segments so
  2774. diff -Nur linux-4.4.46.orig/arch/x86/kernel/reboot.c linux-4.4.46/arch/x86/kernel/reboot.c
  2775. --- linux-4.4.46.orig/arch/x86/kernel/reboot.c 2017-02-01 08:31:11.000000000 +0100
  2776. +++ linux-4.4.46/arch/x86/kernel/reboot.c 2017-02-03 17:18:05.663415712 +0100
  2777. @@ -726,6 +726,7 @@
  2778. static nmi_shootdown_cb shootdown_callback;
  2779. static atomic_t waiting_for_crash_ipi;
  2780. +static int crash_ipi_issued;
  2781. static int crash_nmi_callback(unsigned int val, struct pt_regs *regs)
  2782. {
  2783. @@ -788,6 +789,9 @@
  2784. smp_send_nmi_allbutself();
  2785. + /* Kick CPUs looping in NMI context. */
  2786. + WRITE_ONCE(crash_ipi_issued, 1);
  2787. +
  2788. msecs = 1000; /* Wait at most a second for the other cpus to stop */
  2789. while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
  2790. mdelay(1);
  2791. @@ -796,6 +800,22 @@
  2792. /* Leave the nmi callback set */
  2793. }
  2794. +
  2795. +/* Override the weak function in kernel/panic.c */
  2796. +void nmi_panic_self_stop(struct pt_regs *regs)
  2797. +{
  2798. + while (1) {
  2799. + /*
  2800. + * Wait for the crash dumping IPI to be issued, and then
  2801. + * call its callback directly.
  2802. + */
  2803. + if (READ_ONCE(crash_ipi_issued))
  2804. + crash_nmi_callback(0, regs); /* Don't return */
  2805. +
  2806. + cpu_relax();
  2807. + }
  2808. +}
  2809. +
  2810. #else /* !CONFIG_SMP */
  2811. void nmi_shootdown_cpus(nmi_shootdown_cb callback)
  2812. {
  2813. diff -Nur linux-4.4.46.orig/arch/x86/kvm/lapic.c linux-4.4.46/arch/x86/kvm/lapic.c
  2814. --- linux-4.4.46.orig/arch/x86/kvm/lapic.c 2017-02-01 08:31:11.000000000 +0100
  2815. +++ linux-4.4.46/arch/x86/kvm/lapic.c 2017-02-03 17:18:05.663415712 +0100
  2816. @@ -1195,7 +1195,7 @@
  2817. static void apic_timer_expired(struct kvm_lapic *apic)
  2818. {
  2819. struct kvm_vcpu *vcpu = apic->vcpu;
  2820. - wait_queue_head_t *q = &vcpu->wq;
  2821. + struct swait_queue_head *q = &vcpu->wq;
  2822. struct kvm_timer *ktimer = &apic->lapic_timer;
  2823. if (atomic_read(&apic->lapic_timer.pending))
  2824. @@ -1204,8 +1204,8 @@
  2825. atomic_inc(&apic->lapic_timer.pending);
  2826. kvm_set_pending_timer(vcpu);
  2827. - if (waitqueue_active(q))
  2828. - wake_up_interruptible(q);
  2829. + if (swait_active(q))
  2830. + swake_up(q);
  2831. if (apic_lvtt_tscdeadline(apic))
  2832. ktimer->expired_tscdeadline = ktimer->tscdeadline;
  2833. @@ -1801,6 +1801,7 @@
  2834. hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
  2835. HRTIMER_MODE_ABS);
  2836. apic->lapic_timer.timer.function = apic_timer_fn;
  2837. + apic->lapic_timer.timer.irqsafe = 1;
  2838. /*
  2839. * APIC is created enabled. This will prevent kvm_lapic_set_base from
  2840. diff -Nur linux-4.4.46.orig/arch/x86/kvm/x86.c linux-4.4.46/arch/x86/kvm/x86.c
  2841. --- linux-4.4.46.orig/arch/x86/kvm/x86.c 2017-02-01 08:31:11.000000000 +0100
  2842. +++ linux-4.4.46/arch/x86/kvm/x86.c 2017-02-03 17:18:05.663415712 +0100
  2843. @@ -5809,6 +5809,13 @@
  2844. goto out;
  2845. }
  2846. +#ifdef CONFIG_PREEMPT_RT_FULL
  2847. + if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
  2848. + printk(KERN_ERR "RT requires X86_FEATURE_CONSTANT_TSC\n");
  2849. + return -EOPNOTSUPP;
  2850. + }
  2851. +#endif
  2852. +
  2853. r = kvm_mmu_module_init();
  2854. if (r)
  2855. goto out_free_percpu;
  2856. diff -Nur linux-4.4.46.orig/arch/x86/mm/highmem_32.c linux-4.4.46/arch/x86/mm/highmem_32.c
  2857. --- linux-4.4.46.orig/arch/x86/mm/highmem_32.c 2017-02-01 08:31:11.000000000 +0100
  2858. +++ linux-4.4.46/arch/x86/mm/highmem_32.c 2017-02-03 17:18:05.663415712 +0100
  2859. @@ -32,10 +32,11 @@
  2860. */
  2861. void *kmap_atomic_prot(struct page *page, pgprot_t prot)
  2862. {
  2863. + pte_t pte = mk_pte(page, prot);
  2864. unsigned long vaddr;
  2865. int idx, type;
  2866. - preempt_disable();
  2867. + preempt_disable_nort();
  2868. pagefault_disable();
  2869. if (!PageHighMem(page))
  2870. @@ -45,7 +46,10 @@
  2871. idx = type + KM_TYPE_NR*smp_processor_id();
  2872. vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
  2873. BUG_ON(!pte_none(*(kmap_pte-idx)));
  2874. - set_pte(kmap_pte-idx, mk_pte(page, prot));
  2875. +#ifdef CONFIG_PREEMPT_RT_FULL
  2876. + current->kmap_pte[type] = pte;
  2877. +#endif
  2878. + set_pte(kmap_pte-idx, pte);
  2879. arch_flush_lazy_mmu_mode();
  2880. return (void *)vaddr;
  2881. @@ -88,6 +92,9 @@
  2882. * is a bad idea also, in case the page changes cacheability
  2883. * attributes or becomes a protected page in a hypervisor.
  2884. */
  2885. +#ifdef CONFIG_PREEMPT_RT_FULL
  2886. + current->kmap_pte[type] = __pte(0);
  2887. +#endif
  2888. kpte_clear_flush(kmap_pte-idx, vaddr);
  2889. kmap_atomic_idx_pop();
  2890. arch_flush_lazy_mmu_mode();
  2891. @@ -100,7 +107,7 @@
  2892. #endif
  2893. pagefault_enable();
  2894. - preempt_enable();
  2895. + preempt_enable_nort();
  2896. }
  2897. EXPORT_SYMBOL(__kunmap_atomic);
  2898. diff -Nur linux-4.4.46.orig/arch/x86/mm/iomap_32.c linux-4.4.46/arch/x86/mm/iomap_32.c
  2899. --- linux-4.4.46.orig/arch/x86/mm/iomap_32.c 2017-02-01 08:31:11.000000000 +0100
  2900. +++ linux-4.4.46/arch/x86/mm/iomap_32.c 2017-02-03 17:18:05.663415712 +0100
  2901. @@ -56,6 +56,7 @@
  2902. void *kmap_atomic_prot_pfn(unsigned long pfn, pgprot_t prot)
  2903. {
  2904. + pte_t pte = pfn_pte(pfn, prot);
  2905. unsigned long vaddr;
  2906. int idx, type;
  2907. @@ -65,7 +66,12 @@
  2908. type = kmap_atomic_idx_push();
  2909. idx = type + KM_TYPE_NR * smp_processor_id();
  2910. vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
  2911. - set_pte(kmap_pte - idx, pfn_pte(pfn, prot));
  2912. + WARN_ON(!pte_none(*(kmap_pte - idx)));
  2913. +
  2914. +#ifdef CONFIG_PREEMPT_RT_FULL
  2915. + current->kmap_pte[type] = pte;
  2916. +#endif
  2917. + set_pte(kmap_pte - idx, pte);
  2918. arch_flush_lazy_mmu_mode();
  2919. return (void *)vaddr;
  2920. @@ -113,6 +119,9 @@
  2921. * is a bad idea also, in case the page changes cacheability
  2922. * attributes or becomes a protected page in a hypervisor.
  2923. */
  2924. +#ifdef CONFIG_PREEMPT_RT_FULL
  2925. + current->kmap_pte[type] = __pte(0);
  2926. +#endif
  2927. kpte_clear_flush(kmap_pte-idx, vaddr);
  2928. kmap_atomic_idx_pop();
  2929. }
  2930. diff -Nur linux-4.4.46.orig/arch/x86/platform/uv/tlb_uv.c linux-4.4.46/arch/x86/platform/uv/tlb_uv.c
  2931. --- linux-4.4.46.orig/arch/x86/platform/uv/tlb_uv.c 2017-02-01 08:31:11.000000000 +0100
  2932. +++ linux-4.4.46/arch/x86/platform/uv/tlb_uv.c 2017-02-03 17:18:05.663415712 +0100
  2933. @@ -714,9 +714,9 @@
  2934. quiesce_local_uvhub(hmaster);
  2935. - spin_lock(&hmaster->queue_lock);
  2936. + raw_spin_lock(&hmaster->queue_lock);
  2937. reset_with_ipi(&bau_desc->distribution, bcp);
  2938. - spin_unlock(&hmaster->queue_lock);
  2939. + raw_spin_unlock(&hmaster->queue_lock);
  2940. end_uvhub_quiesce(hmaster);
  2941. @@ -736,9 +736,9 @@
  2942. quiesce_local_uvhub(hmaster);
  2943. - spin_lock(&hmaster->queue_lock);
  2944. + raw_spin_lock(&hmaster->queue_lock);
  2945. reset_with_ipi(&bau_desc->distribution, bcp);
  2946. - spin_unlock(&hmaster->queue_lock);
  2947. + raw_spin_unlock(&hmaster->queue_lock);
  2948. end_uvhub_quiesce(hmaster);
  2949. @@ -759,7 +759,7 @@
  2950. cycles_t tm1;
  2951. hmaster = bcp->uvhub_master;
  2952. - spin_lock(&hmaster->disable_lock);
  2953. + raw_spin_lock(&hmaster->disable_lock);
  2954. if (!bcp->baudisabled) {
  2955. stat->s_bau_disabled++;
  2956. tm1 = get_cycles();
  2957. @@ -772,7 +772,7 @@
  2958. }
  2959. }
  2960. }
  2961. - spin_unlock(&hmaster->disable_lock);
  2962. + raw_spin_unlock(&hmaster->disable_lock);
  2963. }
  2964. static void count_max_concurr(int stat, struct bau_control *bcp,
  2965. @@ -835,7 +835,7 @@
  2966. */
  2967. static void uv1_throttle(struct bau_control *hmaster, struct ptc_stats *stat)
  2968. {
  2969. - spinlock_t *lock = &hmaster->uvhub_lock;
  2970. + raw_spinlock_t *lock = &hmaster->uvhub_lock;
  2971. atomic_t *v;
  2972. v = &hmaster->active_descriptor_count;
  2973. @@ -968,7 +968,7 @@
  2974. struct bau_control *hmaster;
  2975. hmaster = bcp->uvhub_master;
  2976. - spin_lock(&hmaster->disable_lock);
  2977. + raw_spin_lock(&hmaster->disable_lock);
  2978. if (bcp->baudisabled && (get_cycles() >= bcp->set_bau_on_time)) {
  2979. stat->s_bau_reenabled++;
  2980. for_each_present_cpu(tcpu) {
  2981. @@ -980,10 +980,10 @@
  2982. tbcp->period_giveups = 0;
  2983. }
  2984. }
  2985. - spin_unlock(&hmaster->disable_lock);
  2986. + raw_spin_unlock(&hmaster->disable_lock);
  2987. return 0;
  2988. }
  2989. - spin_unlock(&hmaster->disable_lock);
  2990. + raw_spin_unlock(&hmaster->disable_lock);
  2991. return -1;
  2992. }
  2993. @@ -1901,9 +1901,9 @@
  2994. bcp->cong_reps = congested_reps;
  2995. bcp->disabled_period = sec_2_cycles(disabled_period);
  2996. bcp->giveup_limit = giveup_limit;
  2997. - spin_lock_init(&bcp->queue_lock);
  2998. - spin_lock_init(&bcp->uvhub_lock);
  2999. - spin_lock_init(&bcp->disable_lock);
  3000. + raw_spin_lock_init(&bcp->queue_lock);
  3001. + raw_spin_lock_init(&bcp->uvhub_lock);
  3002. + raw_spin_lock_init(&bcp->disable_lock);
  3003. }
  3004. }
  3005. diff -Nur linux-4.4.46.orig/arch/x86/platform/uv/uv_time.c linux-4.4.46/arch/x86/platform/uv/uv_time.c
  3006. --- linux-4.4.46.orig/arch/x86/platform/uv/uv_time.c 2017-02-01 08:31:11.000000000 +0100
  3007. +++ linux-4.4.46/arch/x86/platform/uv/uv_time.c 2017-02-03 17:18:05.663415712 +0100
  3008. @@ -57,7 +57,7 @@
  3009. /* There is one of these allocated per node */
  3010. struct uv_rtc_timer_head {
  3011. - spinlock_t lock;
  3012. + raw_spinlock_t lock;
  3013. /* next cpu waiting for timer, local node relative: */
  3014. int next_cpu;
  3015. /* number of cpus on this node: */
  3016. @@ -177,7 +177,7 @@
  3017. uv_rtc_deallocate_timers();
  3018. return -ENOMEM;
  3019. }
  3020. - spin_lock_init(&head->lock);
  3021. + raw_spin_lock_init(&head->lock);
  3022. head->ncpus = uv_blade_nr_possible_cpus(bid);
  3023. head->next_cpu = -1;
  3024. blade_info[bid] = head;
  3025. @@ -231,7 +231,7 @@
  3026. unsigned long flags;
  3027. int next_cpu;
  3028. - spin_lock_irqsave(&head->lock, flags);
  3029. + raw_spin_lock_irqsave(&head->lock, flags);
  3030. next_cpu = head->next_cpu;
  3031. *t = expires;
  3032. @@ -243,12 +243,12 @@
  3033. if (uv_setup_intr(cpu, expires)) {
  3034. *t = ULLONG_MAX;
  3035. uv_rtc_find_next_timer(head, pnode);
  3036. - spin_unlock_irqrestore(&head->lock, flags);
  3037. + raw_spin_unlock_irqrestore(&head->lock, flags);
  3038. return -ETIME;
  3039. }
  3040. }
  3041. - spin_unlock_irqrestore(&head->lock, flags);
  3042. + raw_spin_unlock_irqrestore(&head->lock, flags);
  3043. return 0;
  3044. }
  3045. @@ -267,7 +267,7 @@
  3046. unsigned long flags;
  3047. int rc = 0;
  3048. - spin_lock_irqsave(&head->lock, flags);
  3049. + raw_spin_lock_irqsave(&head->lock, flags);
  3050. if ((head->next_cpu == bcpu && uv_read_rtc(NULL) >= *t) || force)
  3051. rc = 1;
  3052. @@ -279,7 +279,7 @@
  3053. uv_rtc_find_next_timer(head, pnode);
  3054. }
  3055. - spin_unlock_irqrestore(&head->lock, flags);
  3056. + raw_spin_unlock_irqrestore(&head->lock, flags);
  3057. return rc;
  3058. }
  3059. @@ -299,13 +299,18 @@
  3060. static cycle_t uv_read_rtc(struct clocksource *cs)
  3061. {
  3062. unsigned long offset;
  3063. + cycle_t cycles;
  3064. + preempt_disable();
  3065. if (uv_get_min_hub_revision_id() == 1)
  3066. offset = 0;
  3067. else
  3068. offset = (uv_blade_processor_id() * L1_CACHE_BYTES) % PAGE_SIZE;
  3069. - return (cycle_t)uv_read_local_mmr(UVH_RTC | offset);
  3070. + cycles = (cycle_t)uv_read_local_mmr(UVH_RTC | offset);
  3071. + preempt_enable();
  3072. +
  3073. + return cycles;
  3074. }
  3075. /*
  3076. diff -Nur linux-4.4.46.orig/block/blk-core.c linux-4.4.46/block/blk-core.c
  3077. --- linux-4.4.46.orig/block/blk-core.c 2017-02-01 08:31:11.000000000 +0100
  3078. +++ linux-4.4.46/block/blk-core.c 2017-02-03 17:18:05.667415866 +0100
  3079. @@ -125,6 +125,9 @@
  3080. INIT_LIST_HEAD(&rq->queuelist);
  3081. INIT_LIST_HEAD(&rq->timeout_list);
  3082. +#ifdef CONFIG_PREEMPT_RT_FULL
  3083. + INIT_WORK(&rq->work, __blk_mq_complete_request_remote_work);
  3084. +#endif
  3085. rq->cpu = -1;
  3086. rq->q = q;
  3087. rq->__sector = (sector_t) -1;
  3088. @@ -233,7 +236,7 @@
  3089. **/
  3090. void blk_start_queue(struct request_queue *q)
  3091. {
  3092. - WARN_ON(!irqs_disabled());
  3093. + WARN_ON_NONRT(!irqs_disabled());
  3094. queue_flag_clear(QUEUE_FLAG_STOPPED, q);
  3095. __blk_run_queue(q);
  3096. @@ -659,7 +662,7 @@
  3097. if (!gfpflags_allow_blocking(gfp))
  3098. return -EBUSY;
  3099. - ret = wait_event_interruptible(q->mq_freeze_wq,
  3100. + ret = swait_event_interruptible(q->mq_freeze_wq,
  3101. !atomic_read(&q->mq_freeze_depth) ||
  3102. blk_queue_dying(q));
  3103. if (blk_queue_dying(q))
  3104. @@ -679,7 +682,7 @@
  3105. struct request_queue *q =
  3106. container_of(ref, struct request_queue, q_usage_counter);
  3107. - wake_up_all(&q->mq_freeze_wq);
  3108. + swake_up_all(&q->mq_freeze_wq);
  3109. }
  3110. struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
  3111. @@ -741,7 +744,7 @@
  3112. q->bypass_depth = 1;
  3113. __set_bit(QUEUE_FLAG_BYPASS, &q->queue_flags);
  3114. - init_waitqueue_head(&q->mq_freeze_wq);
  3115. + init_swait_queue_head(&q->mq_freeze_wq);
  3116. /*
  3117. * Init percpu_ref in atomic mode so that it's faster to shutdown.
  3118. @@ -3200,7 +3203,7 @@
  3119. blk_run_queue_async(q);
  3120. else
  3121. __blk_run_queue(q);
  3122. - spin_unlock(q->queue_lock);
  3123. + spin_unlock_irq(q->queue_lock);
  3124. }
  3125. static void flush_plug_callbacks(struct blk_plug *plug, bool from_schedule)
  3126. @@ -3248,7 +3251,6 @@
  3127. void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
  3128. {
  3129. struct request_queue *q;
  3130. - unsigned long flags;
  3131. struct request *rq;
  3132. LIST_HEAD(list);
  3133. unsigned int depth;
  3134. @@ -3268,11 +3270,6 @@
  3135. q = NULL;
  3136. depth = 0;
  3137. - /*
  3138. - * Save and disable interrupts here, to avoid doing it for every
  3139. - * queue lock we have to take.
  3140. - */
  3141. - local_irq_save(flags);
  3142. while (!list_empty(&list)) {
  3143. rq = list_entry_rq(list.next);
  3144. list_del_init(&rq->queuelist);
  3145. @@ -3285,7 +3282,7 @@
  3146. queue_unplugged(q, depth, from_schedule);
  3147. q = rq->q;
  3148. depth = 0;
  3149. - spin_lock(q->queue_lock);
  3150. + spin_lock_irq(q->queue_lock);
  3151. }
  3152. /*
  3153. @@ -3312,8 +3309,6 @@
  3154. */
  3155. if (q)
  3156. queue_unplugged(q, depth, from_schedule);
  3157. -
  3158. - local_irq_restore(flags);
  3159. }
  3160. void blk_finish_plug(struct blk_plug *plug)
  3161. diff -Nur linux-4.4.46.orig/block/blk-ioc.c linux-4.4.46/block/blk-ioc.c
  3162. --- linux-4.4.46.orig/block/blk-ioc.c 2017-02-01 08:31:11.000000000 +0100
  3163. +++ linux-4.4.46/block/blk-ioc.c 2017-02-03 17:18:05.667415866 +0100
  3164. @@ -7,6 +7,7 @@
  3165. #include <linux/bio.h>
  3166. #include <linux/blkdev.h>
  3167. #include <linux/slab.h>
  3168. +#include <linux/delay.h>
  3169. #include "blk.h"
  3170. @@ -109,7 +110,7 @@
  3171. spin_unlock(q->queue_lock);
  3172. } else {
  3173. spin_unlock_irqrestore(&ioc->lock, flags);
  3174. - cpu_relax();
  3175. + cpu_chill();
  3176. spin_lock_irqsave_nested(&ioc->lock, flags, 1);
  3177. }
  3178. }
  3179. @@ -187,7 +188,7 @@
  3180. spin_unlock(icq->q->queue_lock);
  3181. } else {
  3182. spin_unlock_irqrestore(&ioc->lock, flags);
  3183. - cpu_relax();
  3184. + cpu_chill();
  3185. goto retry;
  3186. }
  3187. }
  3188. diff -Nur linux-4.4.46.orig/block/blk-iopoll.c linux-4.4.46/block/blk-iopoll.c
  3189. --- linux-4.4.46.orig/block/blk-iopoll.c 2017-02-01 08:31:11.000000000 +0100
  3190. +++ linux-4.4.46/block/blk-iopoll.c 2017-02-03 17:18:05.667415866 +0100
  3191. @@ -35,6 +35,7 @@
  3192. list_add_tail(&iop->list, this_cpu_ptr(&blk_cpu_iopoll));
  3193. __raise_softirq_irqoff(BLOCK_IOPOLL_SOFTIRQ);
  3194. local_irq_restore(flags);
  3195. + preempt_check_resched_rt();
  3196. }
  3197. EXPORT_SYMBOL(blk_iopoll_sched);
  3198. @@ -132,6 +133,7 @@
  3199. __raise_softirq_irqoff(BLOCK_IOPOLL_SOFTIRQ);
  3200. local_irq_enable();
  3201. + preempt_check_resched_rt();
  3202. }
  3203. /**
  3204. @@ -201,6 +203,7 @@
  3205. this_cpu_ptr(&blk_cpu_iopoll));
  3206. __raise_softirq_irqoff(BLOCK_IOPOLL_SOFTIRQ);
  3207. local_irq_enable();
  3208. + preempt_check_resched_rt();
  3209. }
  3210. return NOTIFY_OK;
  3211. diff -Nur linux-4.4.46.orig/block/blk-mq.c linux-4.4.46/block/blk-mq.c
  3212. --- linux-4.4.46.orig/block/blk-mq.c 2017-02-01 08:31:11.000000000 +0100
  3213. +++ linux-4.4.46/block/blk-mq.c 2017-02-03 17:18:05.667415866 +0100
  3214. @@ -92,7 +92,7 @@
  3215. static void blk_mq_freeze_queue_wait(struct request_queue *q)
  3216. {
  3217. - wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
  3218. + swait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
  3219. }
  3220. /*
  3221. @@ -130,7 +130,7 @@
  3222. WARN_ON_ONCE(freeze_depth < 0);
  3223. if (!freeze_depth) {
  3224. percpu_ref_reinit(&q->q_usage_counter);
  3225. - wake_up_all(&q->mq_freeze_wq);
  3226. + swake_up_all(&q->mq_freeze_wq);
  3227. }
  3228. }
  3229. EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
  3230. @@ -149,7 +149,7 @@
  3231. * dying, we need to ensure that processes currently waiting on
  3232. * the queue are notified as well.
  3233. */
  3234. - wake_up_all(&q->mq_freeze_wq);
  3235. + swake_up_all(&q->mq_freeze_wq);
  3236. }
  3237. bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
  3238. @@ -196,6 +196,9 @@
  3239. rq->resid_len = 0;
  3240. rq->sense = NULL;
  3241. +#ifdef CONFIG_PREEMPT_RT_FULL
  3242. + INIT_WORK(&rq->work, __blk_mq_complete_request_remote_work);
  3243. +#endif
  3244. INIT_LIST_HEAD(&rq->timeout_list);
  3245. rq->timeout = 0;
  3246. @@ -325,6 +328,17 @@
  3247. }
  3248. EXPORT_SYMBOL(blk_mq_end_request);
  3249. +#ifdef CONFIG_PREEMPT_RT_FULL
  3250. +
  3251. +void __blk_mq_complete_request_remote_work(struct work_struct *work)
  3252. +{
  3253. + struct request *rq = container_of(work, struct request, work);
  3254. +
  3255. + rq->q->softirq_done_fn(rq);
  3256. +}
  3257. +
  3258. +#else
  3259. +
  3260. static void __blk_mq_complete_request_remote(void *data)
  3261. {
  3262. struct request *rq = data;
  3263. @@ -332,6 +346,8 @@
  3264. rq->q->softirq_done_fn(rq);
  3265. }
  3266. +#endif
  3267. +
  3268. static void blk_mq_ipi_complete_request(struct request *rq)
  3269. {
  3270. struct blk_mq_ctx *ctx = rq->mq_ctx;
  3271. @@ -343,19 +359,23 @@
  3272. return;
  3273. }
  3274. - cpu = get_cpu();
  3275. + cpu = get_cpu_light();
  3276. if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
  3277. shared = cpus_share_cache(cpu, ctx->cpu);
  3278. if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
  3279. +#ifdef CONFIG_PREEMPT_RT_FULL
  3280. + schedule_work_on(ctx->cpu, &rq->work);
  3281. +#else
  3282. rq->csd.func = __blk_mq_complete_request_remote;
  3283. rq->csd.info = rq;
  3284. rq->csd.flags = 0;
  3285. smp_call_function_single_async(ctx->cpu, &rq->csd);
  3286. +#endif
  3287. } else {
  3288. rq->q->softirq_done_fn(rq);
  3289. }
  3290. - put_cpu();
  3291. + put_cpu_light();
  3292. }
  3293. static void __blk_mq_complete_request(struct request *rq)
  3294. @@ -862,14 +882,14 @@
  3295. return;
  3296. if (!async) {
  3297. - int cpu = get_cpu();
  3298. + int cpu = get_cpu_light();
  3299. if (cpumask_test_cpu(cpu, hctx->cpumask)) {
  3300. __blk_mq_run_hw_queue(hctx);
  3301. - put_cpu();
  3302. + put_cpu_light();
  3303. return;
  3304. }
  3305. - put_cpu();
  3306. + put_cpu_light();
  3307. }
  3308. kblockd_schedule_delayed_work_on(blk_mq_hctx_next_cpu(hctx),
  3309. @@ -1617,7 +1637,7 @@
  3310. {
  3311. struct blk_mq_hw_ctx *hctx = data;
  3312. - if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
  3313. + if (action == CPU_POST_DEAD)
  3314. return blk_mq_hctx_cpu_offline(hctx, cpu);
  3315. /*
  3316. diff -Nur linux-4.4.46.orig/block/blk-mq-cpu.c linux-4.4.46/block/blk-mq-cpu.c
  3317. --- linux-4.4.46.orig/block/blk-mq-cpu.c 2017-02-01 08:31:11.000000000 +0100
  3318. +++ linux-4.4.46/block/blk-mq-cpu.c 2017-02-03 17:18:05.667415866 +0100
  3319. @@ -16,7 +16,7 @@
  3320. #include "blk-mq.h"
  3321. static LIST_HEAD(blk_mq_cpu_notify_list);
  3322. -static DEFINE_RAW_SPINLOCK(blk_mq_cpu_notify_lock);
  3323. +static DEFINE_SPINLOCK(blk_mq_cpu_notify_lock);
  3324. static int blk_mq_main_cpu_notify(struct notifier_block *self,
  3325. unsigned long action, void *hcpu)
  3326. @@ -25,7 +25,10 @@
  3327. struct blk_mq_cpu_notifier *notify;
  3328. int ret = NOTIFY_OK;
  3329. - raw_spin_lock(&blk_mq_cpu_notify_lock);
  3330. + if (action != CPU_POST_DEAD)
  3331. + return NOTIFY_OK;
  3332. +
  3333. + spin_lock(&blk_mq_cpu_notify_lock);
  3334. list_for_each_entry(notify, &blk_mq_cpu_notify_list, list) {
  3335. ret = notify->notify(notify->data, action, cpu);
  3336. @@ -33,7 +36,7 @@
  3337. break;
  3338. }
  3339. - raw_spin_unlock(&blk_mq_cpu_notify_lock);
  3340. + spin_unlock(&blk_mq_cpu_notify_lock);
  3341. return ret;
  3342. }
  3343. @@ -41,16 +44,16 @@
  3344. {
  3345. BUG_ON(!notifier->notify);
  3346. - raw_spin_lock(&blk_mq_cpu_notify_lock);
  3347. + spin_lock(&blk_mq_cpu_notify_lock);
  3348. list_add_tail(&notifier->list, &blk_mq_cpu_notify_list);
  3349. - raw_spin_unlock(&blk_mq_cpu_notify_lock);
  3350. + spin_unlock(&blk_mq_cpu_notify_lock);
  3351. }
  3352. void blk_mq_unregister_cpu_notifier(struct blk_mq_cpu_notifier *notifier)
  3353. {
  3354. - raw_spin_lock(&blk_mq_cpu_notify_lock);
  3355. + spin_lock(&blk_mq_cpu_notify_lock);
  3356. list_del(&notifier->list);
  3357. - raw_spin_unlock(&blk_mq_cpu_notify_lock);
  3358. + spin_unlock(&blk_mq_cpu_notify_lock);
  3359. }
  3360. void blk_mq_init_cpu_notifier(struct blk_mq_cpu_notifier *notifier,
  3361. diff -Nur linux-4.4.46.orig/block/blk-mq.h linux-4.4.46/block/blk-mq.h
  3362. --- linux-4.4.46.orig/block/blk-mq.h 2017-02-01 08:31:11.000000000 +0100
  3363. +++ linux-4.4.46/block/blk-mq.h 2017-02-03 17:18:05.667415866 +0100
  3364. @@ -74,7 +74,10 @@
  3365. static inline struct blk_mq_ctx *__blk_mq_get_ctx(struct request_queue *q,
  3366. unsigned int cpu)
  3367. {
  3368. - return per_cpu_ptr(q->queue_ctx, cpu);
  3369. + struct blk_mq_ctx *ctx;
  3370. +
  3371. + ctx = per_cpu_ptr(q->queue_ctx, cpu);
  3372. + return ctx;
  3373. }
  3374. /*
  3375. @@ -85,12 +88,12 @@
  3376. */
  3377. static inline struct blk_mq_ctx *blk_mq_get_ctx(struct request_queue *q)
  3378. {
  3379. - return __blk_mq_get_ctx(q, get_cpu());
  3380. + return __blk_mq_get_ctx(q, get_cpu_light());
  3381. }
  3382. static inline void blk_mq_put_ctx(struct blk_mq_ctx *ctx)
  3383. {
  3384. - put_cpu();
  3385. + put_cpu_light();
  3386. }
  3387. struct blk_mq_alloc_data {
  3388. diff -Nur linux-4.4.46.orig/block/blk-softirq.c linux-4.4.46/block/blk-softirq.c
  3389. --- linux-4.4.46.orig/block/blk-softirq.c 2017-02-01 08:31:11.000000000 +0100
  3390. +++ linux-4.4.46/block/blk-softirq.c 2017-02-03 17:18:05.667415866 +0100
  3391. @@ -51,6 +51,7 @@
  3392. raise_softirq_irqoff(BLOCK_SOFTIRQ);
  3393. local_irq_restore(flags);
  3394. + preempt_check_resched_rt();
  3395. }
  3396. /*
  3397. @@ -93,6 +94,7 @@
  3398. this_cpu_ptr(&blk_cpu_done));
  3399. raise_softirq_irqoff(BLOCK_SOFTIRQ);
  3400. local_irq_enable();
  3401. + preempt_check_resched_rt();
  3402. }
  3403. return NOTIFY_OK;
  3404. @@ -150,6 +152,7 @@
  3405. goto do_local;
  3406. local_irq_restore(flags);
  3407. + preempt_check_resched_rt();
  3408. }
  3409. /**
  3410. diff -Nur linux-4.4.46.orig/block/bounce.c linux-4.4.46/block/bounce.c
  3411. --- linux-4.4.46.orig/block/bounce.c 2017-02-01 08:31:11.000000000 +0100
  3412. +++ linux-4.4.46/block/bounce.c 2017-02-03 17:18:05.667415866 +0100
  3413. @@ -55,11 +55,11 @@
  3414. unsigned long flags;
  3415. unsigned char *vto;
  3416. - local_irq_save(flags);
  3417. + local_irq_save_nort(flags);
  3418. vto = kmap_atomic(to->bv_page);
  3419. memcpy(vto + to->bv_offset, vfrom, to->bv_len);
  3420. kunmap_atomic(vto);
  3421. - local_irq_restore(flags);
  3422. + local_irq_restore_nort(flags);
  3423. }
  3424. #else /* CONFIG_HIGHMEM */
  3425. diff -Nur linux-4.4.46.orig/crypto/algapi.c linux-4.4.46/crypto/algapi.c
  3426. --- linux-4.4.46.orig/crypto/algapi.c 2017-02-01 08:31:11.000000000 +0100
  3427. +++ linux-4.4.46/crypto/algapi.c 2017-02-03 17:18:05.667415866 +0100
  3428. @@ -719,13 +719,13 @@
  3429. int crypto_register_notifier(struct notifier_block *nb)
  3430. {
  3431. - return blocking_notifier_chain_register(&crypto_chain, nb);
  3432. + return srcu_notifier_chain_register(&crypto_chain, nb);
  3433. }
  3434. EXPORT_SYMBOL_GPL(crypto_register_notifier);
  3435. int crypto_unregister_notifier(struct notifier_block *nb)
  3436. {
  3437. - return blocking_notifier_chain_unregister(&crypto_chain, nb);
  3438. + return srcu_notifier_chain_unregister(&crypto_chain, nb);
  3439. }
  3440. EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
  3441. diff -Nur linux-4.4.46.orig/crypto/api.c linux-4.4.46/crypto/api.c
  3442. --- linux-4.4.46.orig/crypto/api.c 2017-02-01 08:31:11.000000000 +0100
  3443. +++ linux-4.4.46/crypto/api.c 2017-02-03 17:18:05.671416021 +0100
  3444. @@ -31,7 +31,7 @@
  3445. DECLARE_RWSEM(crypto_alg_sem);
  3446. EXPORT_SYMBOL_GPL(crypto_alg_sem);
  3447. -BLOCKING_NOTIFIER_HEAD(crypto_chain);
  3448. +SRCU_NOTIFIER_HEAD(crypto_chain);
  3449. EXPORT_SYMBOL_GPL(crypto_chain);
  3450. static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg);
  3451. @@ -236,10 +236,10 @@
  3452. {
  3453. int ok;
  3454. - ok = blocking_notifier_call_chain(&crypto_chain, val, v);
  3455. + ok = srcu_notifier_call_chain(&crypto_chain, val, v);
  3456. if (ok == NOTIFY_DONE) {
  3457. request_module("cryptomgr");
  3458. - ok = blocking_notifier_call_chain(&crypto_chain, val, v);
  3459. + ok = srcu_notifier_call_chain(&crypto_chain, val, v);
  3460. }
  3461. return ok;
  3462. diff -Nur linux-4.4.46.orig/crypto/internal.h linux-4.4.46/crypto/internal.h
  3463. --- linux-4.4.46.orig/crypto/internal.h 2017-02-01 08:31:11.000000000 +0100
  3464. +++ linux-4.4.46/crypto/internal.h 2017-02-03 17:18:05.671416021 +0100
  3465. @@ -47,7 +47,7 @@
  3466. extern struct list_head crypto_alg_list;
  3467. extern struct rw_semaphore crypto_alg_sem;
  3468. -extern struct blocking_notifier_head crypto_chain;
  3469. +extern struct srcu_notifier_head crypto_chain;
  3470. #ifdef CONFIG_PROC_FS
  3471. void __init crypto_init_proc(void);
  3472. @@ -143,7 +143,7 @@
  3473. static inline void crypto_notify(unsigned long val, void *v)
  3474. {
  3475. - blocking_notifier_call_chain(&crypto_chain, val, v);
  3476. + srcu_notifier_call_chain(&crypto_chain, val, v);
  3477. }
  3478. #endif /* _CRYPTO_INTERNAL_H */
  3479. diff -Nur linux-4.4.46.orig/Documentation/hwlat_detector.txt linux-4.4.46/Documentation/hwlat_detector.txt
  3480. --- linux-4.4.46.orig/Documentation/hwlat_detector.txt 1970-01-01 01:00:00.000000000 +0100
  3481. +++ linux-4.4.46/Documentation/hwlat_detector.txt 2017-02-03 17:18:05.623414168 +0100
  3482. @@ -0,0 +1,64 @@
  3483. +Introduction:
  3484. +-------------
  3485. +
  3486. +The module hwlat_detector is a special purpose kernel module that is used to
  3487. +detect large system latencies induced by the behavior of certain underlying
  3488. +hardware or firmware, independent of Linux itself. The code was developed
  3489. +originally to detect SMIs (System Management Interrupts) on x86 systems,
  3490. +however there is nothing x86 specific about this patchset. It was
  3491. +originally written for use by the "RT" patch since the Real Time
  3492. +kernel is highly latency sensitive.
  3493. +
  3494. +SMIs are usually not serviced by the Linux kernel, which typically does not
  3495. +even know that they are occuring. SMIs are instead are set up by BIOS code
  3496. +and are serviced by BIOS code, usually for "critical" events such as
  3497. +management of thermal sensors and fans. Sometimes though, SMIs are used for
  3498. +other tasks and those tasks can spend an inordinate amount of time in the
  3499. +handler (sometimes measured in milliseconds). Obviously this is a problem if
  3500. +you are trying to keep event service latencies down in the microsecond range.
  3501. +
  3502. +The hardware latency detector works by hogging all of the cpus for configurable
  3503. +amounts of time (by calling stop_machine()), polling the CPU Time Stamp Counter
  3504. +for some period, then looking for gaps in the TSC data. Any gap indicates a
  3505. +time when the polling was interrupted and since the machine is stopped and
  3506. +interrupts turned off the only thing that could do that would be an SMI.
  3507. +
  3508. +Note that the SMI detector should *NEVER* be used in a production environment.
  3509. +It is intended to be run manually to determine if the hardware platform has a
  3510. +problem with long system firmware service routines.
  3511. +
  3512. +Usage:
  3513. +------
  3514. +
  3515. +Loading the module hwlat_detector passing the parameter "enabled=1" (or by
  3516. +setting the "enable" entry in "hwlat_detector" debugfs toggled on) is the only
  3517. +step required to start the hwlat_detector. It is possible to redefine the
  3518. +threshold in microseconds (us) above which latency spikes will be taken
  3519. +into account (parameter "threshold=").
  3520. +
  3521. +Example:
  3522. +
  3523. + # modprobe hwlat_detector enabled=1 threshold=100
  3524. +
  3525. +After the module is loaded, it creates a directory named "hwlat_detector" under
  3526. +the debugfs mountpoint, "/debug/hwlat_detector" for this text. It is necessary
  3527. +to have debugfs mounted, which might be on /sys/debug on your system.
  3528. +
  3529. +The /debug/hwlat_detector interface contains the following files:
  3530. +
  3531. +count - number of latency spikes observed since last reset
  3532. +enable - a global enable/disable toggle (0/1), resets count
  3533. +max - maximum hardware latency actually observed (usecs)
  3534. +sample - a pipe from which to read current raw sample data
  3535. + in the format <timestamp> <latency observed usecs>
  3536. + (can be opened O_NONBLOCK for a single sample)
  3537. +threshold - minimum latency value to be considered (usecs)
  3538. +width - time period to sample with CPUs held (usecs)
  3539. + must be less than the total window size (enforced)
  3540. +window - total period of sampling, width being inside (usecs)
  3541. +
  3542. +By default we will set width to 500,000 and window to 1,000,000, meaning that
  3543. +we will sample every 1,000,000 usecs (1s) for 500,000 usecs (0.5s). If we
  3544. +observe any latencies that exceed the threshold (initially 100 usecs),
  3545. +then we write to a global sample ring buffer of 8K samples, which is
  3546. +consumed by reading from the "sample" (pipe) debugfs file interface.
  3547. diff -Nur linux-4.4.46.orig/Documentation/kernel-parameters.txt linux-4.4.46/Documentation/kernel-parameters.txt
  3548. --- linux-4.4.46.orig/Documentation/kernel-parameters.txt 2017-02-01 08:31:11.000000000 +0100
  3549. +++ linux-4.4.46/Documentation/kernel-parameters.txt 2017-02-03 17:18:05.623414168 +0100
  3550. @@ -1636,6 +1636,15 @@
  3551. ip= [IP_PNP]
  3552. See Documentation/filesystems/nfs/nfsroot.txt.
  3553. + irqaffinity= [SMP] Set the default irq affinity mask
  3554. + Format:
  3555. + <cpu number>,...,<cpu number>
  3556. + or
  3557. + <cpu number>-<cpu number>
  3558. + (must be a positive range in ascending order)
  3559. + or a mixture
  3560. + <cpu number>,...,<cpu number>-<cpu number>
  3561. +
  3562. irqfixup [HW]
  3563. When an interrupt is not handled search all handlers
  3564. for it. Intended to get systems with badly broken
  3565. diff -Nur linux-4.4.46.orig/Documentation/sysrq.txt linux-4.4.46/Documentation/sysrq.txt
  3566. --- linux-4.4.46.orig/Documentation/sysrq.txt 2017-02-01 08:31:11.000000000 +0100
  3567. +++ linux-4.4.46/Documentation/sysrq.txt 2017-02-03 17:18:05.623414168 +0100
  3568. @@ -59,10 +59,17 @@
  3569. On other - If you know of the key combos for other architectures, please
  3570. let me know so I can add them to this section.
  3571. -On all - write a character to /proc/sysrq-trigger. e.g.:
  3572. -
  3573. +On all - write a character to /proc/sysrq-trigger, e.g.:
  3574. echo t > /proc/sysrq-trigger
  3575. +On all - Enable network SysRq by writing a cookie to icmp_echo_sysrq, e.g.
  3576. + echo 0x01020304 >/proc/sys/net/ipv4/icmp_echo_sysrq
  3577. + Send an ICMP echo request with this pattern plus the particular
  3578. + SysRq command key. Example:
  3579. + # ping -c1 -s57 -p0102030468
  3580. + will trigger the SysRq-H (help) command.
  3581. +
  3582. +
  3583. * What are the 'command' keys?
  3584. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3585. 'b' - Will immediately reboot the system without syncing or unmounting
  3586. diff -Nur linux-4.4.46.orig/Documentation/trace/histograms.txt linux-4.4.46/Documentation/trace/histograms.txt
  3587. --- linux-4.4.46.orig/Documentation/trace/histograms.txt 1970-01-01 01:00:00.000000000 +0100
  3588. +++ linux-4.4.46/Documentation/trace/histograms.txt 2017-02-03 17:18:05.623414168 +0100
  3589. @@ -0,0 +1,186 @@
  3590. + Using the Linux Kernel Latency Histograms
  3591. +
  3592. +
  3593. +This document gives a short explanation how to enable, configure and use
  3594. +latency histograms. Latency histograms are primarily relevant in the
  3595. +context of real-time enabled kernels (CONFIG_PREEMPT/CONFIG_PREEMPT_RT)
  3596. +and are used in the quality management of the Linux real-time
  3597. +capabilities.
  3598. +
  3599. +
  3600. +* Purpose of latency histograms
  3601. +
  3602. +A latency histogram continuously accumulates the frequencies of latency
  3603. +data. There are two types of histograms
  3604. +- potential sources of latencies
  3605. +- effective latencies
  3606. +
  3607. +
  3608. +* Potential sources of latencies
  3609. +
  3610. +Potential sources of latencies are code segments where interrupts,
  3611. +preemption or both are disabled (aka critical sections). To create
  3612. +histograms of potential sources of latency, the kernel stores the time
  3613. +stamp at the start of a critical section, determines the time elapsed
  3614. +when the end of the section is reached, and increments the frequency
  3615. +counter of that latency value - irrespective of whether any concurrently
  3616. +running process is affected by latency or not.
  3617. +- Configuration items (in the Kernel hacking/Tracers submenu)
  3618. + CONFIG_INTERRUPT_OFF_LATENCY
  3619. + CONFIG_PREEMPT_OFF_LATENCY
  3620. +
  3621. +
  3622. +* Effective latencies
  3623. +
  3624. +Effective latencies are actually occuring during wakeup of a process. To
  3625. +determine effective latencies, the kernel stores the time stamp when a
  3626. +process is scheduled to be woken up, and determines the duration of the
  3627. +wakeup time shortly before control is passed over to this process. Note
  3628. +that the apparent latency in user space may be somewhat longer, since the
  3629. +process may be interrupted after control is passed over to it but before
  3630. +the execution in user space takes place. Simply measuring the interval
  3631. +between enqueuing and wakeup may also not appropriate in cases when a
  3632. +process is scheduled as a result of a timer expiration. The timer may have
  3633. +missed its deadline, e.g. due to disabled interrupts, but this latency
  3634. +would not be registered. Therefore, the offsets of missed timers are
  3635. +recorded in a separate histogram. If both wakeup latency and missed timer
  3636. +offsets are configured and enabled, a third histogram may be enabled that
  3637. +records the overall latency as a sum of the timer latency, if any, and the
  3638. +wakeup latency. This histogram is called "timerandwakeup".
  3639. +- Configuration items (in the Kernel hacking/Tracers submenu)
  3640. + CONFIG_WAKEUP_LATENCY
  3641. + CONFIG_MISSED_TIMER_OFSETS
  3642. +
  3643. +
  3644. +* Usage
  3645. +
  3646. +The interface to the administration of the latency histograms is located
  3647. +in the debugfs file system. To mount it, either enter
  3648. +
  3649. +mount -t sysfs nodev /sys
  3650. +mount -t debugfs nodev /sys/kernel/debug
  3651. +
  3652. +from shell command line level, or add
  3653. +
  3654. +nodev /sys sysfs defaults 0 0
  3655. +nodev /sys/kernel/debug debugfs defaults 0 0
  3656. +
  3657. +to the file /etc/fstab. All latency histogram related files are then
  3658. +available in the directory /sys/kernel/debug/tracing/latency_hist. A
  3659. +particular histogram type is enabled by writing non-zero to the related
  3660. +variable in the /sys/kernel/debug/tracing/latency_hist/enable directory.
  3661. +Select "preemptirqsoff" for the histograms of potential sources of
  3662. +latencies and "wakeup" for histograms of effective latencies etc. The
  3663. +histogram data - one per CPU - are available in the files
  3664. +
  3665. +/sys/kernel/debug/tracing/latency_hist/preemptoff/CPUx
  3666. +/sys/kernel/debug/tracing/latency_hist/irqsoff/CPUx
  3667. +/sys/kernel/debug/tracing/latency_hist/preemptirqsoff/CPUx
  3668. +/sys/kernel/debug/tracing/latency_hist/wakeup/CPUx
  3669. +/sys/kernel/debug/tracing/latency_hist/wakeup/sharedprio/CPUx
  3670. +/sys/kernel/debug/tracing/latency_hist/missed_timer_offsets/CPUx
  3671. +/sys/kernel/debug/tracing/latency_hist/timerandwakeup/CPUx
  3672. +
  3673. +The histograms are reset by writing non-zero to the file "reset" in a
  3674. +particular latency directory. To reset all latency data, use
  3675. +
  3676. +#!/bin/sh
  3677. +
  3678. +TRACINGDIR=/sys/kernel/debug/tracing
  3679. +HISTDIR=$TRACINGDIR/latency_hist
  3680. +
  3681. +if test -d $HISTDIR
  3682. +then
  3683. + cd $HISTDIR
  3684. + for i in `find . | grep /reset$`
  3685. + do
  3686. + echo 1 >$i
  3687. + done
  3688. +fi
  3689. +
  3690. +
  3691. +* Data format
  3692. +
  3693. +Latency data are stored with a resolution of one microsecond. The
  3694. +maximum latency is 10,240 microseconds. The data are only valid, if the
  3695. +overflow register is empty. Every output line contains the latency in
  3696. +microseconds in the first row and the number of samples in the second
  3697. +row. To display only lines with a positive latency count, use, for
  3698. +example,
  3699. +
  3700. +grep -v " 0$" /sys/kernel/debug/tracing/latency_hist/preemptoff/CPU0
  3701. +
  3702. +#Minimum latency: 0 microseconds.
  3703. +#Average latency: 0 microseconds.
  3704. +#Maximum latency: 25 microseconds.
  3705. +#Total samples: 3104770694
  3706. +#There are 0 samples greater or equal than 10240 microseconds
  3707. +#usecs samples
  3708. + 0 2984486876
  3709. + 1 49843506
  3710. + 2 58219047
  3711. + 3 5348126
  3712. + 4 2187960
  3713. + 5 3388262
  3714. + 6 959289
  3715. + 7 208294
  3716. + 8 40420
  3717. + 9 4485
  3718. + 10 14918
  3719. + 11 18340
  3720. + 12 25052
  3721. + 13 19455
  3722. + 14 5602
  3723. + 15 969
  3724. + 16 47
  3725. + 17 18
  3726. + 18 14
  3727. + 19 1
  3728. + 20 3
  3729. + 21 2
  3730. + 22 5
  3731. + 23 2
  3732. + 25 1
  3733. +
  3734. +
  3735. +* Wakeup latency of a selected process
  3736. +
  3737. +To only collect wakeup latency data of a particular process, write the
  3738. +PID of the requested process to
  3739. +
  3740. +/sys/kernel/debug/tracing/latency_hist/wakeup/pid
  3741. +
  3742. +PIDs are not considered, if this variable is set to 0.
  3743. +
  3744. +
  3745. +* Details of the process with the highest wakeup latency so far
  3746. +
  3747. +Selected data of the process that suffered from the highest wakeup
  3748. +latency that occurred in a particular CPU are available in the file
  3749. +
  3750. +/sys/kernel/debug/tracing/latency_hist/wakeup/max_latency-CPUx.
  3751. +
  3752. +In addition, other relevant system data at the time when the
  3753. +latency occurred are given.
  3754. +
  3755. +The format of the data is (all in one line):
  3756. +<PID> <Priority> <Latency> (<Timeroffset>) <Command> \
  3757. +<- <PID> <Priority> <Command> <Timestamp>
  3758. +
  3759. +The value of <Timeroffset> is only relevant in the combined timer
  3760. +and wakeup latency recording. In the wakeup recording, it is
  3761. +always 0, in the missed_timer_offsets recording, it is the same
  3762. +as <Latency>.
  3763. +
  3764. +When retrospectively searching for the origin of a latency and
  3765. +tracing was not enabled, it may be helpful to know the name and
  3766. +some basic data of the task that (finally) was switching to the
  3767. +late real-tlme task. In addition to the victim's data, also the
  3768. +data of the possible culprit are therefore displayed after the
  3769. +"<-" symbol.
  3770. +
  3771. +Finally, the timestamp of the time when the latency occurred
  3772. +in <seconds>.<microseconds> after the most recent system boot
  3773. +is provided.
  3774. +
  3775. +These data are also reset when the wakeup histogram is reset.
  3776. diff -Nur linux-4.4.46.orig/drivers/acpi/acpica/acglobal.h linux-4.4.46/drivers/acpi/acpica/acglobal.h
  3777. --- linux-4.4.46.orig/drivers/acpi/acpica/acglobal.h 2017-02-01 08:31:11.000000000 +0100
  3778. +++ linux-4.4.46/drivers/acpi/acpica/acglobal.h 2017-02-03 17:18:05.671416021 +0100
  3779. @@ -116,7 +116,7 @@
  3780. * interrupt level
  3781. */
  3782. ACPI_GLOBAL(acpi_spinlock, acpi_gbl_gpe_lock); /* For GPE data structs and registers */
  3783. -ACPI_GLOBAL(acpi_spinlock, acpi_gbl_hardware_lock); /* For ACPI H/W except GPE registers */
  3784. +ACPI_GLOBAL(acpi_raw_spinlock, acpi_gbl_hardware_lock); /* For ACPI H/W except GPE registers */
  3785. ACPI_GLOBAL(acpi_spinlock, acpi_gbl_reference_count_lock);
  3786. /* Mutex for _OSI support */
  3787. diff -Nur linux-4.4.46.orig/drivers/acpi/acpica/hwregs.c linux-4.4.46/drivers/acpi/acpica/hwregs.c
  3788. --- linux-4.4.46.orig/drivers/acpi/acpica/hwregs.c 2017-02-01 08:31:11.000000000 +0100
  3789. +++ linux-4.4.46/drivers/acpi/acpica/hwregs.c 2017-02-03 17:18:05.671416021 +0100
  3790. @@ -269,14 +269,14 @@
  3791. ACPI_BITMASK_ALL_FIXED_STATUS,
  3792. ACPI_FORMAT_UINT64(acpi_gbl_xpm1a_status.address)));
  3793. - lock_flags = acpi_os_acquire_lock(acpi_gbl_hardware_lock);
  3794. + raw_spin_lock_irqsave(acpi_gbl_hardware_lock, lock_flags);
  3795. /* Clear the fixed events in PM1 A/B */
  3796. status = acpi_hw_register_write(ACPI_REGISTER_PM1_STATUS,
  3797. ACPI_BITMASK_ALL_FIXED_STATUS);
  3798. - acpi_os_release_lock(acpi_gbl_hardware_lock, lock_flags);
  3799. + raw_spin_unlock_irqrestore(acpi_gbl_hardware_lock, lock_flags);
  3800. if (ACPI_FAILURE(status)) {
  3801. goto exit;
  3802. diff -Nur linux-4.4.46.orig/drivers/acpi/acpica/hwxface.c linux-4.4.46/drivers/acpi/acpica/hwxface.c
  3803. --- linux-4.4.46.orig/drivers/acpi/acpica/hwxface.c 2017-02-01 08:31:11.000000000 +0100
  3804. +++ linux-4.4.46/drivers/acpi/acpica/hwxface.c 2017-02-03 17:18:05.671416021 +0100
  3805. @@ -374,7 +374,7 @@
  3806. return_ACPI_STATUS(AE_BAD_PARAMETER);
  3807. }
  3808. - lock_flags = acpi_os_acquire_lock(acpi_gbl_hardware_lock);
  3809. + raw_spin_lock_irqsave(acpi_gbl_hardware_lock, lock_flags);
  3810. /*
  3811. * At this point, we know that the parent register is one of the
  3812. @@ -435,7 +435,7 @@
  3813. unlock_and_exit:
  3814. - acpi_os_release_lock(acpi_gbl_hardware_lock, lock_flags);
  3815. + raw_spin_unlock_irqrestore(acpi_gbl_hardware_lock, lock_flags);
  3816. return_ACPI_STATUS(status);
  3817. }
  3818. diff -Nur linux-4.4.46.orig/drivers/acpi/acpica/utmutex.c linux-4.4.46/drivers/acpi/acpica/utmutex.c
  3819. --- linux-4.4.46.orig/drivers/acpi/acpica/utmutex.c 2017-02-01 08:31:11.000000000 +0100
  3820. +++ linux-4.4.46/drivers/acpi/acpica/utmutex.c 2017-02-03 17:18:05.671416021 +0100
  3821. @@ -88,7 +88,7 @@
  3822. return_ACPI_STATUS (status);
  3823. }
  3824. - status = acpi_os_create_lock (&acpi_gbl_hardware_lock);
  3825. + status = acpi_os_create_raw_lock (&acpi_gbl_hardware_lock);
  3826. if (ACPI_FAILURE (status)) {
  3827. return_ACPI_STATUS (status);
  3828. }
  3829. @@ -156,7 +156,7 @@
  3830. /* Delete the spinlocks */
  3831. acpi_os_delete_lock(acpi_gbl_gpe_lock);
  3832. - acpi_os_delete_lock(acpi_gbl_hardware_lock);
  3833. + acpi_os_delete_raw_lock(acpi_gbl_hardware_lock);
  3834. acpi_os_delete_lock(acpi_gbl_reference_count_lock);
  3835. /* Delete the reader/writer lock */
  3836. diff -Nur linux-4.4.46.orig/drivers/ata/libata-sff.c linux-4.4.46/drivers/ata/libata-sff.c
  3837. --- linux-4.4.46.orig/drivers/ata/libata-sff.c 2017-02-01 08:31:11.000000000 +0100
  3838. +++ linux-4.4.46/drivers/ata/libata-sff.c 2017-02-03 17:18:05.671416021 +0100
  3839. @@ -678,9 +678,9 @@
  3840. unsigned long flags;
  3841. unsigned int consumed;
  3842. - local_irq_save(flags);
  3843. + local_irq_save_nort(flags);
  3844. consumed = ata_sff_data_xfer32(dev, buf, buflen, rw);
  3845. - local_irq_restore(flags);
  3846. + local_irq_restore_nort(flags);
  3847. return consumed;
  3848. }
  3849. @@ -719,7 +719,7 @@
  3850. unsigned long flags;
  3851. /* FIXME: use a bounce buffer */
  3852. - local_irq_save(flags);
  3853. + local_irq_save_nort(flags);
  3854. buf = kmap_atomic(page);
  3855. /* do the actual data transfer */
  3856. @@ -727,7 +727,7 @@
  3857. do_write);
  3858. kunmap_atomic(buf);
  3859. - local_irq_restore(flags);
  3860. + local_irq_restore_nort(flags);
  3861. } else {
  3862. buf = page_address(page);
  3863. ap->ops->sff_data_xfer(qc->dev, buf + offset, qc->sect_size,
  3864. @@ -864,7 +864,7 @@
  3865. unsigned long flags;
  3866. /* FIXME: use bounce buffer */
  3867. - local_irq_save(flags);
  3868. + local_irq_save_nort(flags);
  3869. buf = kmap_atomic(page);
  3870. /* do the actual data transfer */
  3871. @@ -872,7 +872,7 @@
  3872. count, rw);
  3873. kunmap_atomic(buf);
  3874. - local_irq_restore(flags);
  3875. + local_irq_restore_nort(flags);
  3876. } else {
  3877. buf = page_address(page);
  3878. consumed = ap->ops->sff_data_xfer(dev, buf + offset,
  3879. diff -Nur linux-4.4.46.orig/drivers/block/zram/zram_drv.c linux-4.4.46/drivers/block/zram/zram_drv.c
  3880. --- linux-4.4.46.orig/drivers/block/zram/zram_drv.c 2017-02-01 08:31:11.000000000 +0100
  3881. +++ linux-4.4.46/drivers/block/zram/zram_drv.c 2017-02-03 17:18:05.671416021 +0100
  3882. @@ -520,6 +520,8 @@
  3883. goto out_error;
  3884. }
  3885. + zram_meta_init_table_locks(meta, disksize);
  3886. +
  3887. return meta;
  3888. out_error:
  3889. @@ -568,12 +570,12 @@
  3890. unsigned long handle;
  3891. size_t size;
  3892. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  3893. + zram_lock_table(&meta->table[index]);
  3894. handle = meta->table[index].handle;
  3895. size = zram_get_obj_size(meta, index);
  3896. if (!handle || zram_test_flag(meta, index, ZRAM_ZERO)) {
  3897. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  3898. + zram_unlock_table(&meta->table[index]);
  3899. clear_page(mem);
  3900. return 0;
  3901. }
  3902. @@ -584,7 +586,7 @@
  3903. else
  3904. ret = zcomp_decompress(zram->comp, cmem, size, mem);
  3905. zs_unmap_object(meta->mem_pool, handle);
  3906. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  3907. + zram_unlock_table(&meta->table[index]);
  3908. /* Should NEVER happen. Return bio error if it does. */
  3909. if (unlikely(ret)) {
  3910. @@ -604,14 +606,14 @@
  3911. struct zram_meta *meta = zram->meta;
  3912. page = bvec->bv_page;
  3913. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  3914. + zram_lock_table(&meta->table[index]);
  3915. if (unlikely(!meta->table[index].handle) ||
  3916. zram_test_flag(meta, index, ZRAM_ZERO)) {
  3917. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  3918. + zram_unlock_table(&meta->table[index]);
  3919. handle_zero_page(bvec);
  3920. return 0;
  3921. }
  3922. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  3923. + zram_unlock_table(&meta->table[index]);
  3924. if (is_partial_io(bvec))
  3925. /* Use a temporary buffer to decompress the page */
  3926. @@ -689,10 +691,10 @@
  3927. if (user_mem)
  3928. kunmap_atomic(user_mem);
  3929. /* Free memory associated with this sector now. */
  3930. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  3931. + zram_lock_table(&meta->table[index]);
  3932. zram_free_page(zram, index);
  3933. zram_set_flag(meta, index, ZRAM_ZERO);
  3934. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  3935. + zram_unlock_table(&meta->table[index]);
  3936. atomic64_inc(&zram->stats.zero_pages);
  3937. ret = 0;
  3938. @@ -752,12 +754,12 @@
  3939. * Free memory associated with this sector
  3940. * before overwriting unused sectors.
  3941. */
  3942. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  3943. + zram_lock_table(&meta->table[index]);
  3944. zram_free_page(zram, index);
  3945. meta->table[index].handle = handle;
  3946. zram_set_obj_size(meta, index, clen);
  3947. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  3948. + zram_unlock_table(&meta->table[index]);
  3949. /* Update stats */
  3950. atomic64_add(clen, &zram->stats.compr_data_size);
  3951. @@ -800,9 +802,9 @@
  3952. }
  3953. while (n >= PAGE_SIZE) {
  3954. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  3955. + zram_lock_table(&meta->table[index]);
  3956. zram_free_page(zram, index);
  3957. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  3958. + zram_unlock_table(&meta->table[index]);
  3959. atomic64_inc(&zram->stats.notify_free);
  3960. index++;
  3961. n -= PAGE_SIZE;
  3962. @@ -928,9 +930,9 @@
  3963. zram = bdev->bd_disk->private_data;
  3964. meta = zram->meta;
  3965. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  3966. + zram_lock_table(&meta->table[index]);
  3967. zram_free_page(zram, index);
  3968. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  3969. + zram_unlock_table(&meta->table[index]);
  3970. atomic64_inc(&zram->stats.notify_free);
  3971. }
  3972. diff -Nur linux-4.4.46.orig/drivers/block/zram/zram_drv.h linux-4.4.46/drivers/block/zram/zram_drv.h
  3973. --- linux-4.4.46.orig/drivers/block/zram/zram_drv.h 2017-02-01 08:31:11.000000000 +0100
  3974. +++ linux-4.4.46/drivers/block/zram/zram_drv.h 2017-02-03 17:18:05.671416021 +0100
  3975. @@ -72,6 +72,9 @@
  3976. struct zram_table_entry {
  3977. unsigned long handle;
  3978. unsigned long value;
  3979. +#ifdef CONFIG_PREEMPT_RT_BASE
  3980. + spinlock_t lock;
  3981. +#endif
  3982. };
  3983. struct zram_stats {
  3984. @@ -119,4 +122,42 @@
  3985. */
  3986. bool claim; /* Protected by bdev->bd_mutex */
  3987. };
  3988. +
  3989. +#ifndef CONFIG_PREEMPT_RT_BASE
  3990. +static inline void zram_lock_table(struct zram_table_entry *table)
  3991. +{
  3992. + bit_spin_lock(ZRAM_ACCESS, &table->value);
  3993. +}
  3994. +
  3995. +static inline void zram_unlock_table(struct zram_table_entry *table)
  3996. +{
  3997. + bit_spin_unlock(ZRAM_ACCESS, &table->value);
  3998. +}
  3999. +
  4000. +static inline void zram_meta_init_table_locks(struct zram_meta *meta, u64 disksize) { }
  4001. +#else /* CONFIG_PREEMPT_RT_BASE */
  4002. +static inline void zram_lock_table(struct zram_table_entry *table)
  4003. +{
  4004. + spin_lock(&table->lock);
  4005. + __set_bit(ZRAM_ACCESS, &table->value);
  4006. +}
  4007. +
  4008. +static inline void zram_unlock_table(struct zram_table_entry *table)
  4009. +{
  4010. + __clear_bit(ZRAM_ACCESS, &table->value);
  4011. + spin_unlock(&table->lock);
  4012. +}
  4013. +
  4014. +static inline void zram_meta_init_table_locks(struct zram_meta *meta, u64 disksize)
  4015. +{
  4016. + size_t num_pages = disksize >> PAGE_SHIFT;
  4017. + size_t index;
  4018. +
  4019. + for (index = 0; index < num_pages; index++) {
  4020. + spinlock_t *lock = &meta->table[index].lock;
  4021. + spin_lock_init(lock);
  4022. + }
  4023. +}
  4024. +#endif /* CONFIG_PREEMPT_RT_BASE */
  4025. +
  4026. #endif
  4027. diff -Nur linux-4.4.46.orig/drivers/char/random.c linux-4.4.46/drivers/char/random.c
  4028. --- linux-4.4.46.orig/drivers/char/random.c 2017-02-01 08:31:11.000000000 +0100
  4029. +++ linux-4.4.46/drivers/char/random.c 2017-02-03 17:18:05.671416021 +0100
  4030. @@ -799,8 +799,6 @@
  4031. } sample;
  4032. long delta, delta2, delta3;
  4033. - preempt_disable();
  4034. -
  4035. sample.jiffies = jiffies;
  4036. sample.cycles = random_get_entropy();
  4037. sample.num = num;
  4038. @@ -841,7 +839,6 @@
  4039. */
  4040. credit_entropy_bits(r, min_t(int, fls(delta>>1), 11));
  4041. }
  4042. - preempt_enable();
  4043. }
  4044. void add_input_randomness(unsigned int type, unsigned int code,
  4045. @@ -894,28 +891,27 @@
  4046. return *(ptr + f->reg_idx++);
  4047. }
  4048. -void add_interrupt_randomness(int irq, int irq_flags)
  4049. +void add_interrupt_randomness(int irq, int irq_flags, __u64 ip)
  4050. {
  4051. struct entropy_store *r;
  4052. struct fast_pool *fast_pool = this_cpu_ptr(&irq_randomness);
  4053. - struct pt_regs *regs = get_irq_regs();
  4054. unsigned long now = jiffies;
  4055. cycles_t cycles = random_get_entropy();
  4056. __u32 c_high, j_high;
  4057. - __u64 ip;
  4058. unsigned long seed;
  4059. int credit = 0;
  4060. if (cycles == 0)
  4061. - cycles = get_reg(fast_pool, regs);
  4062. + cycles = get_reg(fast_pool, NULL);
  4063. c_high = (sizeof(cycles) > 4) ? cycles >> 32 : 0;
  4064. j_high = (sizeof(now) > 4) ? now >> 32 : 0;
  4065. fast_pool->pool[0] ^= cycles ^ j_high ^ irq;
  4066. fast_pool->pool[1] ^= now ^ c_high;
  4067. - ip = regs ? instruction_pointer(regs) : _RET_IP_;
  4068. + if (!ip)
  4069. + ip = _RET_IP_;
  4070. fast_pool->pool[2] ^= ip;
  4071. fast_pool->pool[3] ^= (sizeof(ip) > 4) ? ip >> 32 :
  4072. - get_reg(fast_pool, regs);
  4073. + get_reg(fast_pool, NULL);
  4074. fast_mix(fast_pool);
  4075. add_interrupt_bench(cycles);
  4076. diff -Nur linux-4.4.46.orig/drivers/clk/at91/clk-generated.c linux-4.4.46/drivers/clk/at91/clk-generated.c
  4077. --- linux-4.4.46.orig/drivers/clk/at91/clk-generated.c 2017-02-01 08:31:11.000000000 +0100
  4078. +++ linux-4.4.46/drivers/clk/at91/clk-generated.c 2017-02-03 17:18:05.671416021 +0100
  4079. @@ -15,8 +15,8 @@
  4080. #include <linux/clkdev.h>
  4081. #include <linux/clk/at91_pmc.h>
  4082. #include <linux/of.h>
  4083. -#include <linux/of_address.h>
  4084. -#include <linux/io.h>
  4085. +#include <linux/mfd/syscon.h>
  4086. +#include <linux/regmap.h>
  4087. #include "pmc.h"
  4088. @@ -28,8 +28,9 @@
  4089. struct clk_generated {
  4090. struct clk_hw hw;
  4091. - struct at91_pmc *pmc;
  4092. + struct regmap *regmap;
  4093. struct clk_range range;
  4094. + spinlock_t *lock;
  4095. u32 id;
  4096. u32 gckdiv;
  4097. u8 parent_id;
  4098. @@ -41,49 +42,52 @@
  4099. static int clk_generated_enable(struct clk_hw *hw)
  4100. {
  4101. struct clk_generated *gck = to_clk_generated(hw);
  4102. - struct at91_pmc *pmc = gck->pmc;
  4103. - u32 tmp;
  4104. + unsigned long flags;
  4105. pr_debug("GCLK: %s, gckdiv = %d, parent id = %d\n",
  4106. __func__, gck->gckdiv, gck->parent_id);
  4107. - pmc_lock(pmc);
  4108. - pmc_write(pmc, AT91_PMC_PCR, (gck->id & AT91_PMC_PCR_PID_MASK));
  4109. - tmp = pmc_read(pmc, AT91_PMC_PCR) &
  4110. - ~(AT91_PMC_PCR_GCKDIV_MASK | AT91_PMC_PCR_GCKCSS_MASK);
  4111. - pmc_write(pmc, AT91_PMC_PCR, tmp | AT91_PMC_PCR_GCKCSS(gck->parent_id)
  4112. - | AT91_PMC_PCR_CMD
  4113. - | AT91_PMC_PCR_GCKDIV(gck->gckdiv)
  4114. - | AT91_PMC_PCR_GCKEN);
  4115. - pmc_unlock(pmc);
  4116. + spin_lock_irqsave(gck->lock, flags);
  4117. + regmap_write(gck->regmap, AT91_PMC_PCR,
  4118. + (gck->id & AT91_PMC_PCR_PID_MASK));
  4119. + regmap_update_bits(gck->regmap, AT91_PMC_PCR,
  4120. + AT91_PMC_PCR_GCKDIV_MASK | AT91_PMC_PCR_GCKCSS_MASK |
  4121. + AT91_PMC_PCR_CMD | AT91_PMC_PCR_GCKEN,
  4122. + AT91_PMC_PCR_GCKCSS(gck->parent_id) |
  4123. + AT91_PMC_PCR_CMD |
  4124. + AT91_PMC_PCR_GCKDIV(gck->gckdiv) |
  4125. + AT91_PMC_PCR_GCKEN);
  4126. + spin_unlock_irqrestore(gck->lock, flags);
  4127. return 0;
  4128. }
  4129. static void clk_generated_disable(struct clk_hw *hw)
  4130. {
  4131. struct clk_generated *gck = to_clk_generated(hw);
  4132. - struct at91_pmc *pmc = gck->pmc;
  4133. - u32 tmp;
  4134. + unsigned long flags;
  4135. - pmc_lock(pmc);
  4136. - pmc_write(pmc, AT91_PMC_PCR, (gck->id & AT91_PMC_PCR_PID_MASK));
  4137. - tmp = pmc_read(pmc, AT91_PMC_PCR) & ~AT91_PMC_PCR_GCKEN;
  4138. - pmc_write(pmc, AT91_PMC_PCR, tmp | AT91_PMC_PCR_CMD);
  4139. - pmc_unlock(pmc);
  4140. + spin_lock_irqsave(gck->lock, flags);
  4141. + regmap_write(gck->regmap, AT91_PMC_PCR,
  4142. + (gck->id & AT91_PMC_PCR_PID_MASK));
  4143. + regmap_update_bits(gck->regmap, AT91_PMC_PCR,
  4144. + AT91_PMC_PCR_CMD | AT91_PMC_PCR_GCKEN,
  4145. + AT91_PMC_PCR_CMD);
  4146. + spin_unlock_irqrestore(gck->lock, flags);
  4147. }
  4148. static int clk_generated_is_enabled(struct clk_hw *hw)
  4149. {
  4150. struct clk_generated *gck = to_clk_generated(hw);
  4151. - struct at91_pmc *pmc = gck->pmc;
  4152. - int ret;
  4153. + unsigned long flags;
  4154. + unsigned int status;
  4155. - pmc_lock(pmc);
  4156. - pmc_write(pmc, AT91_PMC_PCR, (gck->id & AT91_PMC_PCR_PID_MASK));
  4157. - ret = !!(pmc_read(pmc, AT91_PMC_PCR) & AT91_PMC_PCR_GCKEN);
  4158. - pmc_unlock(pmc);
  4159. + spin_lock_irqsave(gck->lock, flags);
  4160. + regmap_write(gck->regmap, AT91_PMC_PCR,
  4161. + (gck->id & AT91_PMC_PCR_PID_MASK));
  4162. + regmap_read(gck->regmap, AT91_PMC_PCR, &status);
  4163. + spin_unlock_irqrestore(gck->lock, flags);
  4164. - return ret;
  4165. + return status & AT91_PMC_PCR_GCKEN ? 1 : 0;
  4166. }
  4167. static unsigned long
  4168. @@ -214,13 +218,14 @@
  4169. */
  4170. static void clk_generated_startup(struct clk_generated *gck)
  4171. {
  4172. - struct at91_pmc *pmc = gck->pmc;
  4173. u32 tmp;
  4174. + unsigned long flags;
  4175. - pmc_lock(pmc);
  4176. - pmc_write(pmc, AT91_PMC_PCR, (gck->id & AT91_PMC_PCR_PID_MASK));
  4177. - tmp = pmc_read(pmc, AT91_PMC_PCR);
  4178. - pmc_unlock(pmc);
  4179. + spin_lock_irqsave(gck->lock, flags);
  4180. + regmap_write(gck->regmap, AT91_PMC_PCR,
  4181. + (gck->id & AT91_PMC_PCR_PID_MASK));
  4182. + regmap_read(gck->regmap, AT91_PMC_PCR, &tmp);
  4183. + spin_unlock_irqrestore(gck->lock, flags);
  4184. gck->parent_id = (tmp & AT91_PMC_PCR_GCKCSS_MASK)
  4185. >> AT91_PMC_PCR_GCKCSS_OFFSET;
  4186. @@ -229,8 +234,8 @@
  4187. }
  4188. static struct clk * __init
  4189. -at91_clk_register_generated(struct at91_pmc *pmc, const char *name,
  4190. - const char **parent_names, u8 num_parents,
  4191. +at91_clk_register_generated(struct regmap *regmap, spinlock_t *lock, const char
  4192. + *name, const char **parent_names, u8 num_parents,
  4193. u8 id, const struct clk_range *range)
  4194. {
  4195. struct clk_generated *gck;
  4196. @@ -249,7 +254,8 @@
  4197. gck->id = id;
  4198. gck->hw.init = &init;
  4199. - gck->pmc = pmc;
  4200. + gck->regmap = regmap;
  4201. + gck->lock = lock;
  4202. gck->range = *range;
  4203. clk = clk_register(NULL, &gck->hw);
  4204. @@ -261,8 +267,7 @@
  4205. return clk;
  4206. }
  4207. -void __init of_sama5d2_clk_generated_setup(struct device_node *np,
  4208. - struct at91_pmc *pmc)
  4209. +void __init of_sama5d2_clk_generated_setup(struct device_node *np)
  4210. {
  4211. int num;
  4212. u32 id;
  4213. @@ -272,6 +277,7 @@
  4214. const char *parent_names[GENERATED_SOURCE_MAX];
  4215. struct device_node *gcknp;
  4216. struct clk_range range = CLK_RANGE(0, 0);
  4217. + struct regmap *regmap;
  4218. num_parents = of_clk_get_parent_count(np);
  4219. if (num_parents <= 0 || num_parents > GENERATED_SOURCE_MAX)
  4220. @@ -283,6 +289,10 @@
  4221. if (!num || num > PERIPHERAL_MAX)
  4222. return;
  4223. + regmap = syscon_node_to_regmap(of_get_parent(np));
  4224. + if (IS_ERR(regmap))
  4225. + return;
  4226. +
  4227. for_each_child_of_node(np, gcknp) {
  4228. if (of_property_read_u32(gcknp, "reg", &id))
  4229. continue;
  4230. @@ -296,11 +306,14 @@
  4231. of_at91_get_clk_range(gcknp, "atmel,clk-output-range",
  4232. &range);
  4233. - clk = at91_clk_register_generated(pmc, name, parent_names,
  4234. - num_parents, id, &range);
  4235. + clk = at91_clk_register_generated(regmap, &pmc_pcr_lock, name,
  4236. + parent_names, num_parents,
  4237. + id, &range);
  4238. if (IS_ERR(clk))
  4239. continue;
  4240. of_clk_add_provider(gcknp, of_clk_src_simple_get, clk);
  4241. }
  4242. }
  4243. +CLK_OF_DECLARE(of_sama5d2_clk_generated_setup, "atmel,sama5d2-clk-generated",
  4244. + of_sama5d2_clk_generated_setup);
  4245. diff -Nur linux-4.4.46.orig/drivers/clk/at91/clk-h32mx.c linux-4.4.46/drivers/clk/at91/clk-h32mx.c
  4246. --- linux-4.4.46.orig/drivers/clk/at91/clk-h32mx.c 2017-02-01 08:31:11.000000000 +0100
  4247. +++ linux-4.4.46/drivers/clk/at91/clk-h32mx.c 2017-02-03 17:18:05.671416021 +0100
  4248. @@ -15,15 +15,9 @@
  4249. #include <linux/clk-provider.h>
  4250. #include <linux/clkdev.h>
  4251. #include <linux/clk/at91_pmc.h>
  4252. -#include <linux/delay.h>
  4253. #include <linux/of.h>
  4254. -#include <linux/of_address.h>
  4255. -#include <linux/of_irq.h>
  4256. -#include <linux/io.h>
  4257. -#include <linux/interrupt.h>
  4258. -#include <linux/irq.h>
  4259. -#include <linux/sched.h>
  4260. -#include <linux/wait.h>
  4261. +#include <linux/regmap.h>
  4262. +#include <linux/mfd/syscon.h>
  4263. #include "pmc.h"
  4264. @@ -31,7 +25,7 @@
  4265. struct clk_sama5d4_h32mx {
  4266. struct clk_hw hw;
  4267. - struct at91_pmc *pmc;
  4268. + struct regmap *regmap;
  4269. };
  4270. #define to_clk_sama5d4_h32mx(hw) container_of(hw, struct clk_sama5d4_h32mx, hw)
  4271. @@ -40,8 +34,10 @@
  4272. unsigned long parent_rate)
  4273. {
  4274. struct clk_sama5d4_h32mx *h32mxclk = to_clk_sama5d4_h32mx(hw);
  4275. + unsigned int mckr;
  4276. - if (pmc_read(h32mxclk->pmc, AT91_PMC_MCKR) & AT91_PMC_H32MXDIV)
  4277. + regmap_read(h32mxclk->regmap, AT91_PMC_MCKR, &mckr);
  4278. + if (mckr & AT91_PMC_H32MXDIV)
  4279. return parent_rate / 2;
  4280. if (parent_rate > H32MX_MAX_FREQ)
  4281. @@ -70,18 +66,16 @@
  4282. unsigned long parent_rate)
  4283. {
  4284. struct clk_sama5d4_h32mx *h32mxclk = to_clk_sama5d4_h32mx(hw);
  4285. - struct at91_pmc *pmc = h32mxclk->pmc;
  4286. - u32 tmp;
  4287. + u32 mckr = 0;
  4288. if (parent_rate != rate && (parent_rate / 2) != rate)
  4289. return -EINVAL;
  4290. - pmc_lock(pmc);
  4291. - tmp = pmc_read(pmc, AT91_PMC_MCKR) & ~AT91_PMC_H32MXDIV;
  4292. if ((parent_rate / 2) == rate)
  4293. - tmp |= AT91_PMC_H32MXDIV;
  4294. - pmc_write(pmc, AT91_PMC_MCKR, tmp);
  4295. - pmc_unlock(pmc);
  4296. + mckr = AT91_PMC_H32MXDIV;
  4297. +
  4298. + regmap_update_bits(h32mxclk->regmap, AT91_PMC_MCKR,
  4299. + AT91_PMC_H32MXDIV, mckr);
  4300. return 0;
  4301. }
  4302. @@ -92,14 +86,18 @@
  4303. .set_rate = clk_sama5d4_h32mx_set_rate,
  4304. };
  4305. -void __init of_sama5d4_clk_h32mx_setup(struct device_node *np,
  4306. - struct at91_pmc *pmc)
  4307. +static void __init of_sama5d4_clk_h32mx_setup(struct device_node *np)
  4308. {
  4309. struct clk_sama5d4_h32mx *h32mxclk;
  4310. struct clk_init_data init;
  4311. const char *parent_name;
  4312. + struct regmap *regmap;
  4313. struct clk *clk;
  4314. + regmap = syscon_node_to_regmap(of_get_parent(np));
  4315. + if (IS_ERR(regmap))
  4316. + return;
  4317. +
  4318. h32mxclk = kzalloc(sizeof(*h32mxclk), GFP_KERNEL);
  4319. if (!h32mxclk)
  4320. return;
  4321. @@ -113,7 +111,7 @@
  4322. init.flags = CLK_SET_RATE_GATE;
  4323. h32mxclk->hw.init = &init;
  4324. - h32mxclk->pmc = pmc;
  4325. + h32mxclk->regmap = regmap;
  4326. clk = clk_register(NULL, &h32mxclk->hw);
  4327. if (IS_ERR(clk)) {
  4328. @@ -123,3 +121,5 @@
  4329. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  4330. }
  4331. +CLK_OF_DECLARE(of_sama5d4_clk_h32mx_setup, "atmel,sama5d4-clk-h32mx",
  4332. + of_sama5d4_clk_h32mx_setup);
  4333. diff -Nur linux-4.4.46.orig/drivers/clk/at91/clk-main.c linux-4.4.46/drivers/clk/at91/clk-main.c
  4334. --- linux-4.4.46.orig/drivers/clk/at91/clk-main.c 2017-02-01 08:31:11.000000000 +0100
  4335. +++ linux-4.4.46/drivers/clk/at91/clk-main.c 2017-02-03 17:18:05.671416021 +0100
  4336. @@ -13,13 +13,8 @@
  4337. #include <linux/clk/at91_pmc.h>
  4338. #include <linux/delay.h>
  4339. #include <linux/of.h>
  4340. -#include <linux/of_address.h>
  4341. -#include <linux/of_irq.h>
  4342. -#include <linux/io.h>
  4343. -#include <linux/interrupt.h>
  4344. -#include <linux/irq.h>
  4345. -#include <linux/sched.h>
  4346. -#include <linux/wait.h>
  4347. +#include <linux/mfd/syscon.h>
  4348. +#include <linux/regmap.h>
  4349. #include "pmc.h"
  4350. @@ -34,18 +29,14 @@
  4351. struct clk_main_osc {
  4352. struct clk_hw hw;
  4353. - struct at91_pmc *pmc;
  4354. - unsigned int irq;
  4355. - wait_queue_head_t wait;
  4356. + struct regmap *regmap;
  4357. };
  4358. #define to_clk_main_osc(hw) container_of(hw, struct clk_main_osc, hw)
  4359. struct clk_main_rc_osc {
  4360. struct clk_hw hw;
  4361. - struct at91_pmc *pmc;
  4362. - unsigned int irq;
  4363. - wait_queue_head_t wait;
  4364. + struct regmap *regmap;
  4365. unsigned long frequency;
  4366. unsigned long accuracy;
  4367. };
  4368. @@ -54,51 +45,47 @@
  4369. struct clk_rm9200_main {
  4370. struct clk_hw hw;
  4371. - struct at91_pmc *pmc;
  4372. + struct regmap *regmap;
  4373. };
  4374. #define to_clk_rm9200_main(hw) container_of(hw, struct clk_rm9200_main, hw)
  4375. struct clk_sam9x5_main {
  4376. struct clk_hw hw;
  4377. - struct at91_pmc *pmc;
  4378. - unsigned int irq;
  4379. - wait_queue_head_t wait;
  4380. + struct regmap *regmap;
  4381. u8 parent;
  4382. };
  4383. #define to_clk_sam9x5_main(hw) container_of(hw, struct clk_sam9x5_main, hw)
  4384. -static irqreturn_t clk_main_osc_irq_handler(int irq, void *dev_id)
  4385. +static inline bool clk_main_osc_ready(struct regmap *regmap)
  4386. {
  4387. - struct clk_main_osc *osc = dev_id;
  4388. + unsigned int status;
  4389. - wake_up(&osc->wait);
  4390. - disable_irq_nosync(osc->irq);
  4391. + regmap_read(regmap, AT91_PMC_SR, &status);
  4392. - return IRQ_HANDLED;
  4393. + return status & AT91_PMC_MOSCS;
  4394. }
  4395. static int clk_main_osc_prepare(struct clk_hw *hw)
  4396. {
  4397. struct clk_main_osc *osc = to_clk_main_osc(hw);
  4398. - struct at91_pmc *pmc = osc->pmc;
  4399. + struct regmap *regmap = osc->regmap;
  4400. u32 tmp;
  4401. - tmp = pmc_read(pmc, AT91_CKGR_MOR) & ~MOR_KEY_MASK;
  4402. + regmap_read(regmap, AT91_CKGR_MOR, &tmp);
  4403. + tmp &= ~MOR_KEY_MASK;
  4404. +
  4405. if (tmp & AT91_PMC_OSCBYPASS)
  4406. return 0;
  4407. if (!(tmp & AT91_PMC_MOSCEN)) {
  4408. tmp |= AT91_PMC_MOSCEN | AT91_PMC_KEY;
  4409. - pmc_write(pmc, AT91_CKGR_MOR, tmp);
  4410. + regmap_write(regmap, AT91_CKGR_MOR, tmp);
  4411. }
  4412. - while (!(pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCS)) {
  4413. - enable_irq(osc->irq);
  4414. - wait_event(osc->wait,
  4415. - pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCS);
  4416. - }
  4417. + while (!clk_main_osc_ready(regmap))
  4418. + cpu_relax();
  4419. return 0;
  4420. }
  4421. @@ -106,9 +93,10 @@
  4422. static void clk_main_osc_unprepare(struct clk_hw *hw)
  4423. {
  4424. struct clk_main_osc *osc = to_clk_main_osc(hw);
  4425. - struct at91_pmc *pmc = osc->pmc;
  4426. - u32 tmp = pmc_read(pmc, AT91_CKGR_MOR);
  4427. + struct regmap *regmap = osc->regmap;
  4428. + u32 tmp;
  4429. + regmap_read(regmap, AT91_CKGR_MOR, &tmp);
  4430. if (tmp & AT91_PMC_OSCBYPASS)
  4431. return;
  4432. @@ -116,20 +104,22 @@
  4433. return;
  4434. tmp &= ~(AT91_PMC_KEY | AT91_PMC_MOSCEN);
  4435. - pmc_write(pmc, AT91_CKGR_MOR, tmp | AT91_PMC_KEY);
  4436. + regmap_write(regmap, AT91_CKGR_MOR, tmp | AT91_PMC_KEY);
  4437. }
  4438. static int clk_main_osc_is_prepared(struct clk_hw *hw)
  4439. {
  4440. struct clk_main_osc *osc = to_clk_main_osc(hw);
  4441. - struct at91_pmc *pmc = osc->pmc;
  4442. - u32 tmp = pmc_read(pmc, AT91_CKGR_MOR);
  4443. + struct regmap *regmap = osc->regmap;
  4444. + u32 tmp, status;
  4445. + regmap_read(regmap, AT91_CKGR_MOR, &tmp);
  4446. if (tmp & AT91_PMC_OSCBYPASS)
  4447. return 1;
  4448. - return !!((pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCS) &&
  4449. - (pmc_read(pmc, AT91_CKGR_MOR) & AT91_PMC_MOSCEN));
  4450. + regmap_read(regmap, AT91_PMC_SR, &status);
  4451. +
  4452. + return (status & AT91_PMC_MOSCS) && (tmp & AT91_PMC_MOSCEN);
  4453. }
  4454. static const struct clk_ops main_osc_ops = {
  4455. @@ -139,18 +129,16 @@
  4456. };
  4457. static struct clk * __init
  4458. -at91_clk_register_main_osc(struct at91_pmc *pmc,
  4459. - unsigned int irq,
  4460. +at91_clk_register_main_osc(struct regmap *regmap,
  4461. const char *name,
  4462. const char *parent_name,
  4463. bool bypass)
  4464. {
  4465. - int ret;
  4466. struct clk_main_osc *osc;
  4467. struct clk *clk = NULL;
  4468. struct clk_init_data init;
  4469. - if (!pmc || !irq || !name || !parent_name)
  4470. + if (!name || !parent_name)
  4471. return ERR_PTR(-EINVAL);
  4472. osc = kzalloc(sizeof(*osc), GFP_KERNEL);
  4473. @@ -164,85 +152,70 @@
  4474. init.flags = CLK_IGNORE_UNUSED;
  4475. osc->hw.init = &init;
  4476. - osc->pmc = pmc;
  4477. - osc->irq = irq;
  4478. -
  4479. - init_waitqueue_head(&osc->wait);
  4480. - irq_set_status_flags(osc->irq, IRQ_NOAUTOEN);
  4481. - ret = request_irq(osc->irq, clk_main_osc_irq_handler,
  4482. - IRQF_TRIGGER_HIGH, name, osc);
  4483. - if (ret) {
  4484. - kfree(osc);
  4485. - return ERR_PTR(ret);
  4486. - }
  4487. + osc->regmap = regmap;
  4488. if (bypass)
  4489. - pmc_write(pmc, AT91_CKGR_MOR,
  4490. - (pmc_read(pmc, AT91_CKGR_MOR) &
  4491. - ~(MOR_KEY_MASK | AT91_PMC_MOSCEN)) |
  4492. - AT91_PMC_OSCBYPASS | AT91_PMC_KEY);
  4493. + regmap_update_bits(regmap,
  4494. + AT91_CKGR_MOR, MOR_KEY_MASK |
  4495. + AT91_PMC_MOSCEN,
  4496. + AT91_PMC_OSCBYPASS | AT91_PMC_KEY);
  4497. clk = clk_register(NULL, &osc->hw);
  4498. - if (IS_ERR(clk)) {
  4499. - free_irq(irq, osc);
  4500. + if (IS_ERR(clk))
  4501. kfree(osc);
  4502. - }
  4503. return clk;
  4504. }
  4505. -void __init of_at91rm9200_clk_main_osc_setup(struct device_node *np,
  4506. - struct at91_pmc *pmc)
  4507. +static void __init of_at91rm9200_clk_main_osc_setup(struct device_node *np)
  4508. {
  4509. struct clk *clk;
  4510. - unsigned int irq;
  4511. const char *name = np->name;
  4512. const char *parent_name;
  4513. + struct regmap *regmap;
  4514. bool bypass;
  4515. of_property_read_string(np, "clock-output-names", &name);
  4516. bypass = of_property_read_bool(np, "atmel,osc-bypass");
  4517. parent_name = of_clk_get_parent_name(np, 0);
  4518. - irq = irq_of_parse_and_map(np, 0);
  4519. - if (!irq)
  4520. + regmap = syscon_node_to_regmap(of_get_parent(np));
  4521. + if (IS_ERR(regmap))
  4522. return;
  4523. - clk = at91_clk_register_main_osc(pmc, irq, name, parent_name, bypass);
  4524. + clk = at91_clk_register_main_osc(regmap, name, parent_name, bypass);
  4525. if (IS_ERR(clk))
  4526. return;
  4527. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  4528. }
  4529. +CLK_OF_DECLARE(at91rm9200_clk_main_osc, "atmel,at91rm9200-clk-main-osc",
  4530. + of_at91rm9200_clk_main_osc_setup);
  4531. -static irqreturn_t clk_main_rc_osc_irq_handler(int irq, void *dev_id)
  4532. +static bool clk_main_rc_osc_ready(struct regmap *regmap)
  4533. {
  4534. - struct clk_main_rc_osc *osc = dev_id;
  4535. + unsigned int status;
  4536. - wake_up(&osc->wait);
  4537. - disable_irq_nosync(osc->irq);
  4538. + regmap_read(regmap, AT91_PMC_SR, &status);
  4539. - return IRQ_HANDLED;
  4540. + return status & AT91_PMC_MOSCRCS;
  4541. }
  4542. static int clk_main_rc_osc_prepare(struct clk_hw *hw)
  4543. {
  4544. struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
  4545. - struct at91_pmc *pmc = osc->pmc;
  4546. - u32 tmp;
  4547. + struct regmap *regmap = osc->regmap;
  4548. + unsigned int mor;
  4549. - tmp = pmc_read(pmc, AT91_CKGR_MOR) & ~MOR_KEY_MASK;
  4550. + regmap_read(regmap, AT91_CKGR_MOR, &mor);
  4551. - if (!(tmp & AT91_PMC_MOSCRCEN)) {
  4552. - tmp |= AT91_PMC_MOSCRCEN | AT91_PMC_KEY;
  4553. - pmc_write(pmc, AT91_CKGR_MOR, tmp);
  4554. - }
  4555. + if (!(mor & AT91_PMC_MOSCRCEN))
  4556. + regmap_update_bits(regmap, AT91_CKGR_MOR,
  4557. + MOR_KEY_MASK | AT91_PMC_MOSCRCEN,
  4558. + AT91_PMC_MOSCRCEN | AT91_PMC_KEY);
  4559. - while (!(pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCRCS)) {
  4560. - enable_irq(osc->irq);
  4561. - wait_event(osc->wait,
  4562. - pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCRCS);
  4563. - }
  4564. + while (!clk_main_rc_osc_ready(regmap))
  4565. + cpu_relax();
  4566. return 0;
  4567. }
  4568. @@ -250,23 +223,28 @@
  4569. static void clk_main_rc_osc_unprepare(struct clk_hw *hw)
  4570. {
  4571. struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
  4572. - struct at91_pmc *pmc = osc->pmc;
  4573. - u32 tmp = pmc_read(pmc, AT91_CKGR_MOR);
  4574. + struct regmap *regmap = osc->regmap;
  4575. + unsigned int mor;
  4576. - if (!(tmp & AT91_PMC_MOSCRCEN))
  4577. + regmap_read(regmap, AT91_CKGR_MOR, &mor);
  4578. +
  4579. + if (!(mor & AT91_PMC_MOSCRCEN))
  4580. return;
  4581. - tmp &= ~(MOR_KEY_MASK | AT91_PMC_MOSCRCEN);
  4582. - pmc_write(pmc, AT91_CKGR_MOR, tmp | AT91_PMC_KEY);
  4583. + regmap_update_bits(regmap, AT91_CKGR_MOR,
  4584. + MOR_KEY_MASK | AT91_PMC_MOSCRCEN, AT91_PMC_KEY);
  4585. }
  4586. static int clk_main_rc_osc_is_prepared(struct clk_hw *hw)
  4587. {
  4588. struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
  4589. - struct at91_pmc *pmc = osc->pmc;
  4590. + struct regmap *regmap = osc->regmap;
  4591. + unsigned int mor, status;
  4592. +
  4593. + regmap_read(regmap, AT91_CKGR_MOR, &mor);
  4594. + regmap_read(regmap, AT91_PMC_SR, &status);
  4595. - return !!((pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCRCS) &&
  4596. - (pmc_read(pmc, AT91_CKGR_MOR) & AT91_PMC_MOSCRCEN));
  4597. + return (mor & AT91_PMC_MOSCRCEN) && (status & AT91_PMC_MOSCRCS);
  4598. }
  4599. static unsigned long clk_main_rc_osc_recalc_rate(struct clk_hw *hw,
  4600. @@ -294,17 +272,15 @@
  4601. };
  4602. static struct clk * __init
  4603. -at91_clk_register_main_rc_osc(struct at91_pmc *pmc,
  4604. - unsigned int irq,
  4605. +at91_clk_register_main_rc_osc(struct regmap *regmap,
  4606. const char *name,
  4607. u32 frequency, u32 accuracy)
  4608. {
  4609. - int ret;
  4610. struct clk_main_rc_osc *osc;
  4611. struct clk *clk = NULL;
  4612. struct clk_init_data init;
  4613. - if (!pmc || !irq || !name || !frequency)
  4614. + if (!name || !frequency)
  4615. return ERR_PTR(-EINVAL);
  4616. osc = kzalloc(sizeof(*osc), GFP_KERNEL);
  4617. @@ -318,63 +294,53 @@
  4618. init.flags = CLK_IS_ROOT | CLK_IGNORE_UNUSED;
  4619. osc->hw.init = &init;
  4620. - osc->pmc = pmc;
  4621. - osc->irq = irq;
  4622. + osc->regmap = regmap;
  4623. osc->frequency = frequency;
  4624. osc->accuracy = accuracy;
  4625. - init_waitqueue_head(&osc->wait);
  4626. - irq_set_status_flags(osc->irq, IRQ_NOAUTOEN);
  4627. - ret = request_irq(osc->irq, clk_main_rc_osc_irq_handler,
  4628. - IRQF_TRIGGER_HIGH, name, osc);
  4629. - if (ret)
  4630. - return ERR_PTR(ret);
  4631. -
  4632. clk = clk_register(NULL, &osc->hw);
  4633. - if (IS_ERR(clk)) {
  4634. - free_irq(irq, osc);
  4635. + if (IS_ERR(clk))
  4636. kfree(osc);
  4637. - }
  4638. return clk;
  4639. }
  4640. -void __init of_at91sam9x5_clk_main_rc_osc_setup(struct device_node *np,
  4641. - struct at91_pmc *pmc)
  4642. +static void __init of_at91sam9x5_clk_main_rc_osc_setup(struct device_node *np)
  4643. {
  4644. struct clk *clk;
  4645. - unsigned int irq;
  4646. u32 frequency = 0;
  4647. u32 accuracy = 0;
  4648. const char *name = np->name;
  4649. + struct regmap *regmap;
  4650. of_property_read_string(np, "clock-output-names", &name);
  4651. of_property_read_u32(np, "clock-frequency", &frequency);
  4652. of_property_read_u32(np, "clock-accuracy", &accuracy);
  4653. - irq = irq_of_parse_and_map(np, 0);
  4654. - if (!irq)
  4655. + regmap = syscon_node_to_regmap(of_get_parent(np));
  4656. + if (IS_ERR(regmap))
  4657. return;
  4658. - clk = at91_clk_register_main_rc_osc(pmc, irq, name, frequency,
  4659. - accuracy);
  4660. + clk = at91_clk_register_main_rc_osc(regmap, name, frequency, accuracy);
  4661. if (IS_ERR(clk))
  4662. return;
  4663. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  4664. }
  4665. +CLK_OF_DECLARE(at91sam9x5_clk_main_rc_osc, "atmel,at91sam9x5-clk-main-rc-osc",
  4666. + of_at91sam9x5_clk_main_rc_osc_setup);
  4667. -static int clk_main_probe_frequency(struct at91_pmc *pmc)
  4668. +static int clk_main_probe_frequency(struct regmap *regmap)
  4669. {
  4670. unsigned long prep_time, timeout;
  4671. - u32 tmp;
  4672. + unsigned int mcfr;
  4673. timeout = jiffies + usecs_to_jiffies(MAINFRDY_TIMEOUT);
  4674. do {
  4675. prep_time = jiffies;
  4676. - tmp = pmc_read(pmc, AT91_CKGR_MCFR);
  4677. - if (tmp & AT91_PMC_MAINRDY)
  4678. + regmap_read(regmap, AT91_CKGR_MCFR, &mcfr);
  4679. + if (mcfr & AT91_PMC_MAINRDY)
  4680. return 0;
  4681. usleep_range(MAINF_LOOP_MIN_WAIT, MAINF_LOOP_MAX_WAIT);
  4682. } while (time_before(prep_time, timeout));
  4683. @@ -382,34 +348,37 @@
  4684. return -ETIMEDOUT;
  4685. }
  4686. -static unsigned long clk_main_recalc_rate(struct at91_pmc *pmc,
  4687. +static unsigned long clk_main_recalc_rate(struct regmap *regmap,
  4688. unsigned long parent_rate)
  4689. {
  4690. - u32 tmp;
  4691. + unsigned int mcfr;
  4692. if (parent_rate)
  4693. return parent_rate;
  4694. pr_warn("Main crystal frequency not set, using approximate value\n");
  4695. - tmp = pmc_read(pmc, AT91_CKGR_MCFR);
  4696. - if (!(tmp & AT91_PMC_MAINRDY))
  4697. + regmap_read(regmap, AT91_CKGR_MCFR, &mcfr);
  4698. + if (!(mcfr & AT91_PMC_MAINRDY))
  4699. return 0;
  4700. - return ((tmp & AT91_PMC_MAINF) * SLOW_CLOCK_FREQ) / MAINF_DIV;
  4701. + return ((mcfr & AT91_PMC_MAINF) * SLOW_CLOCK_FREQ) / MAINF_DIV;
  4702. }
  4703. static int clk_rm9200_main_prepare(struct clk_hw *hw)
  4704. {
  4705. struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw);
  4706. - return clk_main_probe_frequency(clkmain->pmc);
  4707. + return clk_main_probe_frequency(clkmain->regmap);
  4708. }
  4709. static int clk_rm9200_main_is_prepared(struct clk_hw *hw)
  4710. {
  4711. struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw);
  4712. + unsigned int status;
  4713. +
  4714. + regmap_read(clkmain->regmap, AT91_CKGR_MCFR, &status);
  4715. - return !!(pmc_read(clkmain->pmc, AT91_CKGR_MCFR) & AT91_PMC_MAINRDY);
  4716. + return status & AT91_PMC_MAINRDY ? 1 : 0;
  4717. }
  4718. static unsigned long clk_rm9200_main_recalc_rate(struct clk_hw *hw,
  4719. @@ -417,7 +386,7 @@
  4720. {
  4721. struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw);
  4722. - return clk_main_recalc_rate(clkmain->pmc, parent_rate);
  4723. + return clk_main_recalc_rate(clkmain->regmap, parent_rate);
  4724. }
  4725. static const struct clk_ops rm9200_main_ops = {
  4726. @@ -427,7 +396,7 @@
  4727. };
  4728. static struct clk * __init
  4729. -at91_clk_register_rm9200_main(struct at91_pmc *pmc,
  4730. +at91_clk_register_rm9200_main(struct regmap *regmap,
  4731. const char *name,
  4732. const char *parent_name)
  4733. {
  4734. @@ -435,7 +404,7 @@
  4735. struct clk *clk = NULL;
  4736. struct clk_init_data init;
  4737. - if (!pmc || !name)
  4738. + if (!name)
  4739. return ERR_PTR(-EINVAL);
  4740. if (!parent_name)
  4741. @@ -452,7 +421,7 @@
  4742. init.flags = 0;
  4743. clkmain->hw.init = &init;
  4744. - clkmain->pmc = pmc;
  4745. + clkmain->regmap = regmap;
  4746. clk = clk_register(NULL, &clkmain->hw);
  4747. if (IS_ERR(clk))
  4748. @@ -461,52 +430,54 @@
  4749. return clk;
  4750. }
  4751. -void __init of_at91rm9200_clk_main_setup(struct device_node *np,
  4752. - struct at91_pmc *pmc)
  4753. +static void __init of_at91rm9200_clk_main_setup(struct device_node *np)
  4754. {
  4755. struct clk *clk;
  4756. const char *parent_name;
  4757. const char *name = np->name;
  4758. + struct regmap *regmap;
  4759. parent_name = of_clk_get_parent_name(np, 0);
  4760. of_property_read_string(np, "clock-output-names", &name);
  4761. - clk = at91_clk_register_rm9200_main(pmc, name, parent_name);
  4762. + regmap = syscon_node_to_regmap(of_get_parent(np));
  4763. + if (IS_ERR(regmap))
  4764. + return;
  4765. +
  4766. + clk = at91_clk_register_rm9200_main(regmap, name, parent_name);
  4767. if (IS_ERR(clk))
  4768. return;
  4769. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  4770. }
  4771. +CLK_OF_DECLARE(at91rm9200_clk_main, "atmel,at91rm9200-clk-main",
  4772. + of_at91rm9200_clk_main_setup);
  4773. -static irqreturn_t clk_sam9x5_main_irq_handler(int irq, void *dev_id)
  4774. +static inline bool clk_sam9x5_main_ready(struct regmap *regmap)
  4775. {
  4776. - struct clk_sam9x5_main *clkmain = dev_id;
  4777. + unsigned int status;
  4778. - wake_up(&clkmain->wait);
  4779. - disable_irq_nosync(clkmain->irq);
  4780. + regmap_read(regmap, AT91_PMC_SR, &status);
  4781. - return IRQ_HANDLED;
  4782. + return status & AT91_PMC_MOSCSELS ? 1 : 0;
  4783. }
  4784. static int clk_sam9x5_main_prepare(struct clk_hw *hw)
  4785. {
  4786. struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
  4787. - struct at91_pmc *pmc = clkmain->pmc;
  4788. + struct regmap *regmap = clkmain->regmap;
  4789. - while (!(pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCSELS)) {
  4790. - enable_irq(clkmain->irq);
  4791. - wait_event(clkmain->wait,
  4792. - pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCSELS);
  4793. - }
  4794. + while (!clk_sam9x5_main_ready(regmap))
  4795. + cpu_relax();
  4796. - return clk_main_probe_frequency(pmc);
  4797. + return clk_main_probe_frequency(regmap);
  4798. }
  4799. static int clk_sam9x5_main_is_prepared(struct clk_hw *hw)
  4800. {
  4801. struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
  4802. - return !!(pmc_read(clkmain->pmc, AT91_PMC_SR) & AT91_PMC_MOSCSELS);
  4803. + return clk_sam9x5_main_ready(clkmain->regmap);
  4804. }
  4805. static unsigned long clk_sam9x5_main_recalc_rate(struct clk_hw *hw,
  4806. @@ -514,30 +485,28 @@
  4807. {
  4808. struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
  4809. - return clk_main_recalc_rate(clkmain->pmc, parent_rate);
  4810. + return clk_main_recalc_rate(clkmain->regmap, parent_rate);
  4811. }
  4812. static int clk_sam9x5_main_set_parent(struct clk_hw *hw, u8 index)
  4813. {
  4814. struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
  4815. - struct at91_pmc *pmc = clkmain->pmc;
  4816. - u32 tmp;
  4817. + struct regmap *regmap = clkmain->regmap;
  4818. + unsigned int tmp;
  4819. if (index > 1)
  4820. return -EINVAL;
  4821. - tmp = pmc_read(pmc, AT91_CKGR_MOR) & ~MOR_KEY_MASK;
  4822. + regmap_read(regmap, AT91_CKGR_MOR, &tmp);
  4823. + tmp &= ~MOR_KEY_MASK;
  4824. if (index && !(tmp & AT91_PMC_MOSCSEL))
  4825. - pmc_write(pmc, AT91_CKGR_MOR, tmp | AT91_PMC_MOSCSEL);
  4826. + regmap_write(regmap, AT91_CKGR_MOR, tmp | AT91_PMC_MOSCSEL);
  4827. else if (!index && (tmp & AT91_PMC_MOSCSEL))
  4828. - pmc_write(pmc, AT91_CKGR_MOR, tmp & ~AT91_PMC_MOSCSEL);
  4829. + regmap_write(regmap, AT91_CKGR_MOR, tmp & ~AT91_PMC_MOSCSEL);
  4830. - while (!(pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCSELS)) {
  4831. - enable_irq(clkmain->irq);
  4832. - wait_event(clkmain->wait,
  4833. - pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCSELS);
  4834. - }
  4835. + while (!clk_sam9x5_main_ready(regmap))
  4836. + cpu_relax();
  4837. return 0;
  4838. }
  4839. @@ -545,8 +514,11 @@
  4840. static u8 clk_sam9x5_main_get_parent(struct clk_hw *hw)
  4841. {
  4842. struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
  4843. + unsigned int status;
  4844. +
  4845. + regmap_read(clkmain->regmap, AT91_CKGR_MOR, &status);
  4846. - return !!(pmc_read(clkmain->pmc, AT91_CKGR_MOR) & AT91_PMC_MOSCEN);
  4847. + return status & AT91_PMC_MOSCEN ? 1 : 0;
  4848. }
  4849. static const struct clk_ops sam9x5_main_ops = {
  4850. @@ -558,18 +530,17 @@
  4851. };
  4852. static struct clk * __init
  4853. -at91_clk_register_sam9x5_main(struct at91_pmc *pmc,
  4854. - unsigned int irq,
  4855. +at91_clk_register_sam9x5_main(struct regmap *regmap,
  4856. const char *name,
  4857. const char **parent_names,
  4858. int num_parents)
  4859. {
  4860. - int ret;
  4861. struct clk_sam9x5_main *clkmain;
  4862. struct clk *clk = NULL;
  4863. struct clk_init_data init;
  4864. + unsigned int status;
  4865. - if (!pmc || !irq || !name)
  4866. + if (!name)
  4867. return ERR_PTR(-EINVAL);
  4868. if (!parent_names || !num_parents)
  4869. @@ -586,51 +557,42 @@
  4870. init.flags = CLK_SET_PARENT_GATE;
  4871. clkmain->hw.init = &init;
  4872. - clkmain->pmc = pmc;
  4873. - clkmain->irq = irq;
  4874. - clkmain->parent = !!(pmc_read(clkmain->pmc, AT91_CKGR_MOR) &
  4875. - AT91_PMC_MOSCEN);
  4876. - init_waitqueue_head(&clkmain->wait);
  4877. - irq_set_status_flags(clkmain->irq, IRQ_NOAUTOEN);
  4878. - ret = request_irq(clkmain->irq, clk_sam9x5_main_irq_handler,
  4879. - IRQF_TRIGGER_HIGH, name, clkmain);
  4880. - if (ret)
  4881. - return ERR_PTR(ret);
  4882. + clkmain->regmap = regmap;
  4883. + regmap_read(clkmain->regmap, AT91_CKGR_MOR, &status);
  4884. + clkmain->parent = status & AT91_PMC_MOSCEN ? 1 : 0;
  4885. clk = clk_register(NULL, &clkmain->hw);
  4886. - if (IS_ERR(clk)) {
  4887. - free_irq(clkmain->irq, clkmain);
  4888. + if (IS_ERR(clk))
  4889. kfree(clkmain);
  4890. - }
  4891. return clk;
  4892. }
  4893. -void __init of_at91sam9x5_clk_main_setup(struct device_node *np,
  4894. - struct at91_pmc *pmc)
  4895. +static void __init of_at91sam9x5_clk_main_setup(struct device_node *np)
  4896. {
  4897. struct clk *clk;
  4898. const char *parent_names[2];
  4899. int num_parents;
  4900. - unsigned int irq;
  4901. const char *name = np->name;
  4902. + struct regmap *regmap;
  4903. num_parents = of_clk_get_parent_count(np);
  4904. if (num_parents <= 0 || num_parents > 2)
  4905. return;
  4906. of_clk_parent_fill(np, parent_names, num_parents);
  4907. + regmap = syscon_node_to_regmap(of_get_parent(np));
  4908. + if (IS_ERR(regmap))
  4909. + return;
  4910. of_property_read_string(np, "clock-output-names", &name);
  4911. - irq = irq_of_parse_and_map(np, 0);
  4912. - if (!irq)
  4913. - return;
  4914. -
  4915. - clk = at91_clk_register_sam9x5_main(pmc, irq, name, parent_names,
  4916. + clk = at91_clk_register_sam9x5_main(regmap, name, parent_names,
  4917. num_parents);
  4918. if (IS_ERR(clk))
  4919. return;
  4920. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  4921. }
  4922. +CLK_OF_DECLARE(at91sam9x5_clk_main, "atmel,at91sam9x5-clk-main",
  4923. + of_at91sam9x5_clk_main_setup);
  4924. diff -Nur linux-4.4.46.orig/drivers/clk/at91/clk-master.c linux-4.4.46/drivers/clk/at91/clk-master.c
  4925. --- linux-4.4.46.orig/drivers/clk/at91/clk-master.c 2017-02-01 08:31:11.000000000 +0100
  4926. +++ linux-4.4.46/drivers/clk/at91/clk-master.c 2017-02-03 17:18:05.671416021 +0100
  4927. @@ -12,13 +12,8 @@
  4928. #include <linux/clkdev.h>
  4929. #include <linux/clk/at91_pmc.h>
  4930. #include <linux/of.h>
  4931. -#include <linux/of_address.h>
  4932. -#include <linux/of_irq.h>
  4933. -#include <linux/io.h>
  4934. -#include <linux/wait.h>
  4935. -#include <linux/sched.h>
  4936. -#include <linux/interrupt.h>
  4937. -#include <linux/irq.h>
  4938. +#include <linux/mfd/syscon.h>
  4939. +#include <linux/regmap.h>
  4940. #include "pmc.h"
  4941. @@ -44,32 +39,26 @@
  4942. struct clk_master {
  4943. struct clk_hw hw;
  4944. - struct at91_pmc *pmc;
  4945. - unsigned int irq;
  4946. - wait_queue_head_t wait;
  4947. + struct regmap *regmap;
  4948. const struct clk_master_layout *layout;
  4949. const struct clk_master_characteristics *characteristics;
  4950. };
  4951. -static irqreturn_t clk_master_irq_handler(int irq, void *dev_id)
  4952. +static inline bool clk_master_ready(struct regmap *regmap)
  4953. {
  4954. - struct clk_master *master = (struct clk_master *)dev_id;
  4955. + unsigned int status;
  4956. - wake_up(&master->wait);
  4957. - disable_irq_nosync(master->irq);
  4958. + regmap_read(regmap, AT91_PMC_SR, &status);
  4959. - return IRQ_HANDLED;
  4960. + return status & AT91_PMC_MCKRDY ? 1 : 0;
  4961. }
  4962. +
  4963. static int clk_master_prepare(struct clk_hw *hw)
  4964. {
  4965. struct clk_master *master = to_clk_master(hw);
  4966. - struct at91_pmc *pmc = master->pmc;
  4967. - while (!(pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MCKRDY)) {
  4968. - enable_irq(master->irq);
  4969. - wait_event(master->wait,
  4970. - pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MCKRDY);
  4971. - }
  4972. + while (!clk_master_ready(master->regmap))
  4973. + cpu_relax();
  4974. return 0;
  4975. }
  4976. @@ -78,7 +67,7 @@
  4977. {
  4978. struct clk_master *master = to_clk_master(hw);
  4979. - return !!(pmc_read(master->pmc, AT91_PMC_SR) & AT91_PMC_MCKRDY);
  4980. + return clk_master_ready(master->regmap);
  4981. }
  4982. static unsigned long clk_master_recalc_rate(struct clk_hw *hw,
  4983. @@ -88,18 +77,16 @@
  4984. u8 div;
  4985. unsigned long rate = parent_rate;
  4986. struct clk_master *master = to_clk_master(hw);
  4987. - struct at91_pmc *pmc = master->pmc;
  4988. const struct clk_master_layout *layout = master->layout;
  4989. const struct clk_master_characteristics *characteristics =
  4990. master->characteristics;
  4991. - u32 tmp;
  4992. + unsigned int mckr;
  4993. - pmc_lock(pmc);
  4994. - tmp = pmc_read(pmc, AT91_PMC_MCKR) & layout->mask;
  4995. - pmc_unlock(pmc);
  4996. + regmap_read(master->regmap, AT91_PMC_MCKR, &mckr);
  4997. + mckr &= layout->mask;
  4998. - pres = (tmp >> layout->pres_shift) & MASTER_PRES_MASK;
  4999. - div = (tmp >> MASTER_DIV_SHIFT) & MASTER_DIV_MASK;
  5000. + pres = (mckr >> layout->pres_shift) & MASTER_PRES_MASK;
  5001. + div = (mckr >> MASTER_DIV_SHIFT) & MASTER_DIV_MASK;
  5002. if (characteristics->have_div3_pres && pres == MASTER_PRES_MAX)
  5003. rate /= 3;
  5004. @@ -119,9 +106,11 @@
  5005. static u8 clk_master_get_parent(struct clk_hw *hw)
  5006. {
  5007. struct clk_master *master = to_clk_master(hw);
  5008. - struct at91_pmc *pmc = master->pmc;
  5009. + unsigned int mckr;
  5010. - return pmc_read(pmc, AT91_PMC_MCKR) & AT91_PMC_CSS;
  5011. + regmap_read(master->regmap, AT91_PMC_MCKR, &mckr);
  5012. +
  5013. + return mckr & AT91_PMC_CSS;
  5014. }
  5015. static const struct clk_ops master_ops = {
  5016. @@ -132,18 +121,17 @@
  5017. };
  5018. static struct clk * __init
  5019. -at91_clk_register_master(struct at91_pmc *pmc, unsigned int irq,
  5020. +at91_clk_register_master(struct regmap *regmap,
  5021. const char *name, int num_parents,
  5022. const char **parent_names,
  5023. const struct clk_master_layout *layout,
  5024. const struct clk_master_characteristics *characteristics)
  5025. {
  5026. - int ret;
  5027. struct clk_master *master;
  5028. struct clk *clk = NULL;
  5029. struct clk_init_data init;
  5030. - if (!pmc || !irq || !name || !num_parents || !parent_names)
  5031. + if (!name || !num_parents || !parent_names)
  5032. return ERR_PTR(-EINVAL);
  5033. master = kzalloc(sizeof(*master), GFP_KERNEL);
  5034. @@ -159,20 +147,10 @@
  5035. master->hw.init = &init;
  5036. master->layout = layout;
  5037. master->characteristics = characteristics;
  5038. - master->pmc = pmc;
  5039. - master->irq = irq;
  5040. - init_waitqueue_head(&master->wait);
  5041. - irq_set_status_flags(master->irq, IRQ_NOAUTOEN);
  5042. - ret = request_irq(master->irq, clk_master_irq_handler,
  5043. - IRQF_TRIGGER_HIGH, "clk-master", master);
  5044. - if (ret) {
  5045. - kfree(master);
  5046. - return ERR_PTR(ret);
  5047. - }
  5048. + master->regmap = regmap;
  5049. clk = clk_register(NULL, &master->hw);
  5050. if (IS_ERR(clk)) {
  5051. - free_irq(master->irq, master);
  5052. kfree(master);
  5053. }
  5054. @@ -217,15 +195,15 @@
  5055. }
  5056. static void __init
  5057. -of_at91_clk_master_setup(struct device_node *np, struct at91_pmc *pmc,
  5058. +of_at91_clk_master_setup(struct device_node *np,
  5059. const struct clk_master_layout *layout)
  5060. {
  5061. struct clk *clk;
  5062. int num_parents;
  5063. - unsigned int irq;
  5064. const char *parent_names[MASTER_SOURCE_MAX];
  5065. const char *name = np->name;
  5066. struct clk_master_characteristics *characteristics;
  5067. + struct regmap *regmap;
  5068. num_parents = of_clk_get_parent_count(np);
  5069. if (num_parents <= 0 || num_parents > MASTER_SOURCE_MAX)
  5070. @@ -239,11 +217,11 @@
  5071. if (!characteristics)
  5072. return;
  5073. - irq = irq_of_parse_and_map(np, 0);
  5074. - if (!irq)
  5075. - goto out_free_characteristics;
  5076. + regmap = syscon_node_to_regmap(of_get_parent(np));
  5077. + if (IS_ERR(regmap))
  5078. + return;
  5079. - clk = at91_clk_register_master(pmc, irq, name, num_parents,
  5080. + clk = at91_clk_register_master(regmap, name, num_parents,
  5081. parent_names, layout,
  5082. characteristics);
  5083. if (IS_ERR(clk))
  5084. @@ -256,14 +234,16 @@
  5085. kfree(characteristics);
  5086. }
  5087. -void __init of_at91rm9200_clk_master_setup(struct device_node *np,
  5088. - struct at91_pmc *pmc)
  5089. +static void __init of_at91rm9200_clk_master_setup(struct device_node *np)
  5090. {
  5091. - of_at91_clk_master_setup(np, pmc, &at91rm9200_master_layout);
  5092. + of_at91_clk_master_setup(np, &at91rm9200_master_layout);
  5093. }
  5094. +CLK_OF_DECLARE(at91rm9200_clk_master, "atmel,at91rm9200-clk-master",
  5095. + of_at91rm9200_clk_master_setup);
  5096. -void __init of_at91sam9x5_clk_master_setup(struct device_node *np,
  5097. - struct at91_pmc *pmc)
  5098. +static void __init of_at91sam9x5_clk_master_setup(struct device_node *np)
  5099. {
  5100. - of_at91_clk_master_setup(np, pmc, &at91sam9x5_master_layout);
  5101. + of_at91_clk_master_setup(np, &at91sam9x5_master_layout);
  5102. }
  5103. +CLK_OF_DECLARE(at91sam9x5_clk_master, "atmel,at91sam9x5-clk-master",
  5104. + of_at91sam9x5_clk_master_setup);
  5105. diff -Nur linux-4.4.46.orig/drivers/clk/at91/clk-peripheral.c linux-4.4.46/drivers/clk/at91/clk-peripheral.c
  5106. --- linux-4.4.46.orig/drivers/clk/at91/clk-peripheral.c 2017-02-01 08:31:11.000000000 +0100
  5107. +++ linux-4.4.46/drivers/clk/at91/clk-peripheral.c 2017-02-03 17:18:05.671416021 +0100
  5108. @@ -12,11 +12,13 @@
  5109. #include <linux/clkdev.h>
  5110. #include <linux/clk/at91_pmc.h>
  5111. #include <linux/of.h>
  5112. -#include <linux/of_address.h>
  5113. -#include <linux/io.h>
  5114. +#include <linux/mfd/syscon.h>
  5115. +#include <linux/regmap.h>
  5116. #include "pmc.h"
  5117. +DEFINE_SPINLOCK(pmc_pcr_lock);
  5118. +
  5119. #define PERIPHERAL_MAX 64
  5120. #define PERIPHERAL_AT91RM9200 0
  5121. @@ -33,7 +35,7 @@
  5122. struct clk_peripheral {
  5123. struct clk_hw hw;
  5124. - struct at91_pmc *pmc;
  5125. + struct regmap *regmap;
  5126. u32 id;
  5127. };
  5128. @@ -41,8 +43,9 @@
  5129. struct clk_sam9x5_peripheral {
  5130. struct clk_hw hw;
  5131. - struct at91_pmc *pmc;
  5132. + struct regmap *regmap;
  5133. struct clk_range range;
  5134. + spinlock_t *lock;
  5135. u32 id;
  5136. u32 div;
  5137. bool auto_div;
  5138. @@ -54,7 +57,6 @@
  5139. static int clk_peripheral_enable(struct clk_hw *hw)
  5140. {
  5141. struct clk_peripheral *periph = to_clk_peripheral(hw);
  5142. - struct at91_pmc *pmc = periph->pmc;
  5143. int offset = AT91_PMC_PCER;
  5144. u32 id = periph->id;
  5145. @@ -62,14 +64,14 @@
  5146. return 0;
  5147. if (id > PERIPHERAL_ID_MAX)
  5148. offset = AT91_PMC_PCER1;
  5149. - pmc_write(pmc, offset, PERIPHERAL_MASK(id));
  5150. + regmap_write(periph->regmap, offset, PERIPHERAL_MASK(id));
  5151. +
  5152. return 0;
  5153. }
  5154. static void clk_peripheral_disable(struct clk_hw *hw)
  5155. {
  5156. struct clk_peripheral *periph = to_clk_peripheral(hw);
  5157. - struct at91_pmc *pmc = periph->pmc;
  5158. int offset = AT91_PMC_PCDR;
  5159. u32 id = periph->id;
  5160. @@ -77,21 +79,23 @@
  5161. return;
  5162. if (id > PERIPHERAL_ID_MAX)
  5163. offset = AT91_PMC_PCDR1;
  5164. - pmc_write(pmc, offset, PERIPHERAL_MASK(id));
  5165. + regmap_write(periph->regmap, offset, PERIPHERAL_MASK(id));
  5166. }
  5167. static int clk_peripheral_is_enabled(struct clk_hw *hw)
  5168. {
  5169. struct clk_peripheral *periph = to_clk_peripheral(hw);
  5170. - struct at91_pmc *pmc = periph->pmc;
  5171. int offset = AT91_PMC_PCSR;
  5172. + unsigned int status;
  5173. u32 id = periph->id;
  5174. if (id < PERIPHERAL_ID_MIN)
  5175. return 1;
  5176. if (id > PERIPHERAL_ID_MAX)
  5177. offset = AT91_PMC_PCSR1;
  5178. - return !!(pmc_read(pmc, offset) & PERIPHERAL_MASK(id));
  5179. + regmap_read(periph->regmap, offset, &status);
  5180. +
  5181. + return status & PERIPHERAL_MASK(id) ? 1 : 0;
  5182. }
  5183. static const struct clk_ops peripheral_ops = {
  5184. @@ -101,14 +105,14 @@
  5185. };
  5186. static struct clk * __init
  5187. -at91_clk_register_peripheral(struct at91_pmc *pmc, const char *name,
  5188. +at91_clk_register_peripheral(struct regmap *regmap, const char *name,
  5189. const char *parent_name, u32 id)
  5190. {
  5191. struct clk_peripheral *periph;
  5192. struct clk *clk = NULL;
  5193. struct clk_init_data init;
  5194. - if (!pmc || !name || !parent_name || id > PERIPHERAL_ID_MAX)
  5195. + if (!name || !parent_name || id > PERIPHERAL_ID_MAX)
  5196. return ERR_PTR(-EINVAL);
  5197. periph = kzalloc(sizeof(*periph), GFP_KERNEL);
  5198. @@ -123,7 +127,7 @@
  5199. periph->id = id;
  5200. periph->hw.init = &init;
  5201. - periph->pmc = pmc;
  5202. + periph->regmap = regmap;
  5203. clk = clk_register(NULL, &periph->hw);
  5204. if (IS_ERR(clk))
  5205. @@ -160,53 +164,58 @@
  5206. static int clk_sam9x5_peripheral_enable(struct clk_hw *hw)
  5207. {
  5208. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  5209. - struct at91_pmc *pmc = periph->pmc;
  5210. - u32 tmp;
  5211. + unsigned long flags;
  5212. if (periph->id < PERIPHERAL_ID_MIN)
  5213. return 0;
  5214. - pmc_lock(pmc);
  5215. - pmc_write(pmc, AT91_PMC_PCR, (periph->id & AT91_PMC_PCR_PID_MASK));
  5216. - tmp = pmc_read(pmc, AT91_PMC_PCR) & ~AT91_PMC_PCR_DIV_MASK;
  5217. - pmc_write(pmc, AT91_PMC_PCR, tmp | AT91_PMC_PCR_DIV(periph->div)
  5218. - | AT91_PMC_PCR_CMD
  5219. - | AT91_PMC_PCR_EN);
  5220. - pmc_unlock(pmc);
  5221. + spin_lock_irqsave(periph->lock, flags);
  5222. + regmap_write(periph->regmap, AT91_PMC_PCR,
  5223. + (periph->id & AT91_PMC_PCR_PID_MASK));
  5224. + regmap_update_bits(periph->regmap, AT91_PMC_PCR,
  5225. + AT91_PMC_PCR_DIV_MASK | AT91_PMC_PCR_CMD |
  5226. + AT91_PMC_PCR_EN,
  5227. + AT91_PMC_PCR_DIV(periph->div) |
  5228. + AT91_PMC_PCR_CMD |
  5229. + AT91_PMC_PCR_EN);
  5230. + spin_unlock_irqrestore(periph->lock, flags);
  5231. +
  5232. return 0;
  5233. }
  5234. static void clk_sam9x5_peripheral_disable(struct clk_hw *hw)
  5235. {
  5236. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  5237. - struct at91_pmc *pmc = periph->pmc;
  5238. - u32 tmp;
  5239. + unsigned long flags;
  5240. if (periph->id < PERIPHERAL_ID_MIN)
  5241. return;
  5242. - pmc_lock(pmc);
  5243. - pmc_write(pmc, AT91_PMC_PCR, (periph->id & AT91_PMC_PCR_PID_MASK));
  5244. - tmp = pmc_read(pmc, AT91_PMC_PCR) & ~AT91_PMC_PCR_EN;
  5245. - pmc_write(pmc, AT91_PMC_PCR, tmp | AT91_PMC_PCR_CMD);
  5246. - pmc_unlock(pmc);
  5247. + spin_lock_irqsave(periph->lock, flags);
  5248. + regmap_write(periph->regmap, AT91_PMC_PCR,
  5249. + (periph->id & AT91_PMC_PCR_PID_MASK));
  5250. + regmap_update_bits(periph->regmap, AT91_PMC_PCR,
  5251. + AT91_PMC_PCR_EN | AT91_PMC_PCR_CMD,
  5252. + AT91_PMC_PCR_CMD);
  5253. + spin_unlock_irqrestore(periph->lock, flags);
  5254. }
  5255. static int clk_sam9x5_peripheral_is_enabled(struct clk_hw *hw)
  5256. {
  5257. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  5258. - struct at91_pmc *pmc = periph->pmc;
  5259. - int ret;
  5260. + unsigned long flags;
  5261. + unsigned int status;
  5262. if (periph->id < PERIPHERAL_ID_MIN)
  5263. return 1;
  5264. - pmc_lock(pmc);
  5265. - pmc_write(pmc, AT91_PMC_PCR, (periph->id & AT91_PMC_PCR_PID_MASK));
  5266. - ret = !!(pmc_read(pmc, AT91_PMC_PCR) & AT91_PMC_PCR_EN);
  5267. - pmc_unlock(pmc);
  5268. + spin_lock_irqsave(periph->lock, flags);
  5269. + regmap_write(periph->regmap, AT91_PMC_PCR,
  5270. + (periph->id & AT91_PMC_PCR_PID_MASK));
  5271. + regmap_read(periph->regmap, AT91_PMC_PCR, &status);
  5272. + spin_unlock_irqrestore(periph->lock, flags);
  5273. - return ret;
  5274. + return status & AT91_PMC_PCR_EN ? 1 : 0;
  5275. }
  5276. static unsigned long
  5277. @@ -214,19 +223,20 @@
  5278. unsigned long parent_rate)
  5279. {
  5280. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  5281. - struct at91_pmc *pmc = periph->pmc;
  5282. - u32 tmp;
  5283. + unsigned long flags;
  5284. + unsigned int status;
  5285. if (periph->id < PERIPHERAL_ID_MIN)
  5286. return parent_rate;
  5287. - pmc_lock(pmc);
  5288. - pmc_write(pmc, AT91_PMC_PCR, (periph->id & AT91_PMC_PCR_PID_MASK));
  5289. - tmp = pmc_read(pmc, AT91_PMC_PCR);
  5290. - pmc_unlock(pmc);
  5291. + spin_lock_irqsave(periph->lock, flags);
  5292. + regmap_write(periph->regmap, AT91_PMC_PCR,
  5293. + (periph->id & AT91_PMC_PCR_PID_MASK));
  5294. + regmap_read(periph->regmap, AT91_PMC_PCR, &status);
  5295. + spin_unlock_irqrestore(periph->lock, flags);
  5296. - if (tmp & AT91_PMC_PCR_EN) {
  5297. - periph->div = PERIPHERAL_RSHIFT(tmp);
  5298. + if (status & AT91_PMC_PCR_EN) {
  5299. + periph->div = PERIPHERAL_RSHIFT(status);
  5300. periph->auto_div = false;
  5301. } else {
  5302. clk_sam9x5_peripheral_autodiv(periph);
  5303. @@ -318,15 +328,15 @@
  5304. };
  5305. static struct clk * __init
  5306. -at91_clk_register_sam9x5_peripheral(struct at91_pmc *pmc, const char *name,
  5307. - const char *parent_name, u32 id,
  5308. - const struct clk_range *range)
  5309. +at91_clk_register_sam9x5_peripheral(struct regmap *regmap, spinlock_t *lock,
  5310. + const char *name, const char *parent_name,
  5311. + u32 id, const struct clk_range *range)
  5312. {
  5313. struct clk_sam9x5_peripheral *periph;
  5314. struct clk *clk = NULL;
  5315. struct clk_init_data init;
  5316. - if (!pmc || !name || !parent_name)
  5317. + if (!name || !parent_name)
  5318. return ERR_PTR(-EINVAL);
  5319. periph = kzalloc(sizeof(*periph), GFP_KERNEL);
  5320. @@ -342,7 +352,8 @@
  5321. periph->id = id;
  5322. periph->hw.init = &init;
  5323. periph->div = 0;
  5324. - periph->pmc = pmc;
  5325. + periph->regmap = regmap;
  5326. + periph->lock = lock;
  5327. periph->auto_div = true;
  5328. periph->range = *range;
  5329. @@ -356,7 +367,7 @@
  5330. }
  5331. static void __init
  5332. -of_at91_clk_periph_setup(struct device_node *np, struct at91_pmc *pmc, u8 type)
  5333. +of_at91_clk_periph_setup(struct device_node *np, u8 type)
  5334. {
  5335. int num;
  5336. u32 id;
  5337. @@ -364,6 +375,7 @@
  5338. const char *parent_name;
  5339. const char *name;
  5340. struct device_node *periphclknp;
  5341. + struct regmap *regmap;
  5342. parent_name = of_clk_get_parent_name(np, 0);
  5343. if (!parent_name)
  5344. @@ -373,6 +385,10 @@
  5345. if (!num || num > PERIPHERAL_MAX)
  5346. return;
  5347. + regmap = syscon_node_to_regmap(of_get_parent(np));
  5348. + if (IS_ERR(regmap))
  5349. + return;
  5350. +
  5351. for_each_child_of_node(np, periphclknp) {
  5352. if (of_property_read_u32(periphclknp, "reg", &id))
  5353. continue;
  5354. @@ -384,7 +400,7 @@
  5355. name = periphclknp->name;
  5356. if (type == PERIPHERAL_AT91RM9200) {
  5357. - clk = at91_clk_register_peripheral(pmc, name,
  5358. + clk = at91_clk_register_peripheral(regmap, name,
  5359. parent_name, id);
  5360. } else {
  5361. struct clk_range range = CLK_RANGE(0, 0);
  5362. @@ -393,7 +409,9 @@
  5363. "atmel,clk-output-range",
  5364. &range);
  5365. - clk = at91_clk_register_sam9x5_peripheral(pmc, name,
  5366. + clk = at91_clk_register_sam9x5_peripheral(regmap,
  5367. + &pmc_pcr_lock,
  5368. + name,
  5369. parent_name,
  5370. id, &range);
  5371. }
  5372. @@ -405,14 +423,16 @@
  5373. }
  5374. }
  5375. -void __init of_at91rm9200_clk_periph_setup(struct device_node *np,
  5376. - struct at91_pmc *pmc)
  5377. +static void __init of_at91rm9200_clk_periph_setup(struct device_node *np)
  5378. {
  5379. - of_at91_clk_periph_setup(np, pmc, PERIPHERAL_AT91RM9200);
  5380. + of_at91_clk_periph_setup(np, PERIPHERAL_AT91RM9200);
  5381. }
  5382. +CLK_OF_DECLARE(at91rm9200_clk_periph, "atmel,at91rm9200-clk-peripheral",
  5383. + of_at91rm9200_clk_periph_setup);
  5384. -void __init of_at91sam9x5_clk_periph_setup(struct device_node *np,
  5385. - struct at91_pmc *pmc)
  5386. +static void __init of_at91sam9x5_clk_periph_setup(struct device_node *np)
  5387. {
  5388. - of_at91_clk_periph_setup(np, pmc, PERIPHERAL_AT91SAM9X5);
  5389. + of_at91_clk_periph_setup(np, PERIPHERAL_AT91SAM9X5);
  5390. }
  5391. +CLK_OF_DECLARE(at91sam9x5_clk_periph, "atmel,at91sam9x5-clk-peripheral",
  5392. + of_at91sam9x5_clk_periph_setup);
  5393. diff -Nur linux-4.4.46.orig/drivers/clk/at91/clk-pll.c linux-4.4.46/drivers/clk/at91/clk-pll.c
  5394. --- linux-4.4.46.orig/drivers/clk/at91/clk-pll.c 2017-02-01 08:31:11.000000000 +0100
  5395. +++ linux-4.4.46/drivers/clk/at91/clk-pll.c 2017-02-03 17:18:05.671416021 +0100
  5396. @@ -12,14 +12,8 @@
  5397. #include <linux/clkdev.h>
  5398. #include <linux/clk/at91_pmc.h>
  5399. #include <linux/of.h>
  5400. -#include <linux/of_address.h>
  5401. -#include <linux/of_irq.h>
  5402. -#include <linux/io.h>
  5403. -#include <linux/kernel.h>
  5404. -#include <linux/wait.h>
  5405. -#include <linux/sched.h>
  5406. -#include <linux/interrupt.h>
  5407. -#include <linux/irq.h>
  5408. +#include <linux/mfd/syscon.h>
  5409. +#include <linux/regmap.h>
  5410. #include "pmc.h"
  5411. @@ -58,9 +52,7 @@
  5412. struct clk_pll {
  5413. struct clk_hw hw;
  5414. - struct at91_pmc *pmc;
  5415. - unsigned int irq;
  5416. - wait_queue_head_t wait;
  5417. + struct regmap *regmap;
  5418. u8 id;
  5419. u8 div;
  5420. u8 range;
  5421. @@ -69,20 +61,19 @@
  5422. const struct clk_pll_characteristics *characteristics;
  5423. };
  5424. -static irqreturn_t clk_pll_irq_handler(int irq, void *dev_id)
  5425. +static inline bool clk_pll_ready(struct regmap *regmap, int id)
  5426. {
  5427. - struct clk_pll *pll = (struct clk_pll *)dev_id;
  5428. + unsigned int status;
  5429. - wake_up(&pll->wait);
  5430. - disable_irq_nosync(pll->irq);
  5431. + regmap_read(regmap, AT91_PMC_SR, &status);
  5432. - return IRQ_HANDLED;
  5433. + return status & PLL_STATUS_MASK(id) ? 1 : 0;
  5434. }
  5435. static int clk_pll_prepare(struct clk_hw *hw)
  5436. {
  5437. struct clk_pll *pll = to_clk_pll(hw);
  5438. - struct at91_pmc *pmc = pll->pmc;
  5439. + struct regmap *regmap = pll->regmap;
  5440. const struct clk_pll_layout *layout = pll->layout;
  5441. const struct clk_pll_characteristics *characteristics =
  5442. pll->characteristics;
  5443. @@ -90,39 +81,34 @@
  5444. u32 mask = PLL_STATUS_MASK(id);
  5445. int offset = PLL_REG(id);
  5446. u8 out = 0;
  5447. - u32 pllr, icpr;
  5448. + unsigned int pllr;
  5449. + unsigned int status;
  5450. u8 div;
  5451. u16 mul;
  5452. - pllr = pmc_read(pmc, offset);
  5453. + regmap_read(regmap, offset, &pllr);
  5454. div = PLL_DIV(pllr);
  5455. mul = PLL_MUL(pllr, layout);
  5456. - if ((pmc_read(pmc, AT91_PMC_SR) & mask) &&
  5457. + regmap_read(regmap, AT91_PMC_SR, &status);
  5458. + if ((status & mask) &&
  5459. (div == pll->div && mul == pll->mul))
  5460. return 0;
  5461. if (characteristics->out)
  5462. out = characteristics->out[pll->range];
  5463. - if (characteristics->icpll) {
  5464. - icpr = pmc_read(pmc, AT91_PMC_PLLICPR) & ~PLL_ICPR_MASK(id);
  5465. - icpr |= (characteristics->icpll[pll->range] <<
  5466. - PLL_ICPR_SHIFT(id));
  5467. - pmc_write(pmc, AT91_PMC_PLLICPR, icpr);
  5468. - }
  5469. - pllr &= ~layout->pllr_mask;
  5470. - pllr |= layout->pllr_mask &
  5471. - (pll->div | (PLL_MAX_COUNT << PLL_COUNT_SHIFT) |
  5472. - (out << PLL_OUT_SHIFT) |
  5473. - ((pll->mul & layout->mul_mask) << layout->mul_shift));
  5474. - pmc_write(pmc, offset, pllr);
  5475. -
  5476. - while (!(pmc_read(pmc, AT91_PMC_SR) & mask)) {
  5477. - enable_irq(pll->irq);
  5478. - wait_event(pll->wait,
  5479. - pmc_read(pmc, AT91_PMC_SR) & mask);
  5480. - }
  5481. + if (characteristics->icpll)
  5482. + regmap_update_bits(regmap, AT91_PMC_PLLICPR, PLL_ICPR_MASK(id),
  5483. + characteristics->icpll[pll->range] << PLL_ICPR_SHIFT(id));
  5484. +
  5485. + regmap_update_bits(regmap, offset, layout->pllr_mask,
  5486. + pll->div | (PLL_MAX_COUNT << PLL_COUNT_SHIFT) |
  5487. + (out << PLL_OUT_SHIFT) |
  5488. + ((pll->mul & layout->mul_mask) << layout->mul_shift));
  5489. +
  5490. + while (!clk_pll_ready(regmap, pll->id))
  5491. + cpu_relax();
  5492. return 0;
  5493. }
  5494. @@ -130,32 +116,35 @@
  5495. static int clk_pll_is_prepared(struct clk_hw *hw)
  5496. {
  5497. struct clk_pll *pll = to_clk_pll(hw);
  5498. - struct at91_pmc *pmc = pll->pmc;
  5499. - return !!(pmc_read(pmc, AT91_PMC_SR) &
  5500. - PLL_STATUS_MASK(pll->id));
  5501. + return clk_pll_ready(pll->regmap, pll->id);
  5502. }
  5503. static void clk_pll_unprepare(struct clk_hw *hw)
  5504. {
  5505. struct clk_pll *pll = to_clk_pll(hw);
  5506. - struct at91_pmc *pmc = pll->pmc;
  5507. - const struct clk_pll_layout *layout = pll->layout;
  5508. - int offset = PLL_REG(pll->id);
  5509. - u32 tmp = pmc_read(pmc, offset) & ~(layout->pllr_mask);
  5510. + unsigned int mask = pll->layout->pllr_mask;
  5511. - pmc_write(pmc, offset, tmp);
  5512. + regmap_update_bits(pll->regmap, PLL_REG(pll->id), mask, ~mask);
  5513. }
  5514. static unsigned long clk_pll_recalc_rate(struct clk_hw *hw,
  5515. unsigned long parent_rate)
  5516. {
  5517. struct clk_pll *pll = to_clk_pll(hw);
  5518. + unsigned int pllr;
  5519. + u16 mul;
  5520. + u8 div;
  5521. +
  5522. + regmap_read(pll->regmap, PLL_REG(pll->id), &pllr);
  5523. +
  5524. + div = PLL_DIV(pllr);
  5525. + mul = PLL_MUL(pllr, pll->layout);
  5526. - if (!pll->div || !pll->mul)
  5527. + if (!div || !mul)
  5528. return 0;
  5529. - return (parent_rate / pll->div) * (pll->mul + 1);
  5530. + return (parent_rate / div) * (mul + 1);
  5531. }
  5532. static long clk_pll_get_best_div_mul(struct clk_pll *pll, unsigned long rate,
  5533. @@ -308,7 +297,7 @@
  5534. };
  5535. static struct clk * __init
  5536. -at91_clk_register_pll(struct at91_pmc *pmc, unsigned int irq, const char *name,
  5537. +at91_clk_register_pll(struct regmap *regmap, const char *name,
  5538. const char *parent_name, u8 id,
  5539. const struct clk_pll_layout *layout,
  5540. const struct clk_pll_characteristics *characteristics)
  5541. @@ -316,9 +305,8 @@
  5542. struct clk_pll *pll;
  5543. struct clk *clk = NULL;
  5544. struct clk_init_data init;
  5545. - int ret;
  5546. int offset = PLL_REG(id);
  5547. - u32 tmp;
  5548. + unsigned int pllr;
  5549. if (id > PLL_MAX_ID)
  5550. return ERR_PTR(-EINVAL);
  5551. @@ -337,23 +325,13 @@
  5552. pll->hw.init = &init;
  5553. pll->layout = layout;
  5554. pll->characteristics = characteristics;
  5555. - pll->pmc = pmc;
  5556. - pll->irq = irq;
  5557. - tmp = pmc_read(pmc, offset) & layout->pllr_mask;
  5558. - pll->div = PLL_DIV(tmp);
  5559. - pll->mul = PLL_MUL(tmp, layout);
  5560. - init_waitqueue_head(&pll->wait);
  5561. - irq_set_status_flags(pll->irq, IRQ_NOAUTOEN);
  5562. - ret = request_irq(pll->irq, clk_pll_irq_handler, IRQF_TRIGGER_HIGH,
  5563. - id ? "clk-pllb" : "clk-plla", pll);
  5564. - if (ret) {
  5565. - kfree(pll);
  5566. - return ERR_PTR(ret);
  5567. - }
  5568. + pll->regmap = regmap;
  5569. + regmap_read(regmap, offset, &pllr);
  5570. + pll->div = PLL_DIV(pllr);
  5571. + pll->mul = PLL_MUL(pllr, layout);
  5572. clk = clk_register(NULL, &pll->hw);
  5573. if (IS_ERR(clk)) {
  5574. - free_irq(pll->irq, pll);
  5575. kfree(pll);
  5576. }
  5577. @@ -483,12 +461,12 @@
  5578. }
  5579. static void __init
  5580. -of_at91_clk_pll_setup(struct device_node *np, struct at91_pmc *pmc,
  5581. +of_at91_clk_pll_setup(struct device_node *np,
  5582. const struct clk_pll_layout *layout)
  5583. {
  5584. u32 id;
  5585. - unsigned int irq;
  5586. struct clk *clk;
  5587. + struct regmap *regmap;
  5588. const char *parent_name;
  5589. const char *name = np->name;
  5590. struct clk_pll_characteristics *characteristics;
  5591. @@ -500,15 +478,15 @@
  5592. of_property_read_string(np, "clock-output-names", &name);
  5593. - characteristics = of_at91_clk_pll_get_characteristics(np);
  5594. - if (!characteristics)
  5595. + regmap = syscon_node_to_regmap(of_get_parent(np));
  5596. + if (IS_ERR(regmap))
  5597. return;
  5598. - irq = irq_of_parse_and_map(np, 0);
  5599. - if (!irq)
  5600. + characteristics = of_at91_clk_pll_get_characteristics(np);
  5601. + if (!characteristics)
  5602. return;
  5603. - clk = at91_clk_register_pll(pmc, irq, name, parent_name, id, layout,
  5604. + clk = at91_clk_register_pll(regmap, name, parent_name, id, layout,
  5605. characteristics);
  5606. if (IS_ERR(clk))
  5607. goto out_free_characteristics;
  5608. @@ -520,26 +498,30 @@
  5609. kfree(characteristics);
  5610. }
  5611. -void __init of_at91rm9200_clk_pll_setup(struct device_node *np,
  5612. - struct at91_pmc *pmc)
  5613. +static void __init of_at91rm9200_clk_pll_setup(struct device_node *np)
  5614. {
  5615. - of_at91_clk_pll_setup(np, pmc, &at91rm9200_pll_layout);
  5616. + of_at91_clk_pll_setup(np, &at91rm9200_pll_layout);
  5617. }
  5618. +CLK_OF_DECLARE(at91rm9200_clk_pll, "atmel,at91rm9200-clk-pll",
  5619. + of_at91rm9200_clk_pll_setup);
  5620. -void __init of_at91sam9g45_clk_pll_setup(struct device_node *np,
  5621. - struct at91_pmc *pmc)
  5622. +static void __init of_at91sam9g45_clk_pll_setup(struct device_node *np)
  5623. {
  5624. - of_at91_clk_pll_setup(np, pmc, &at91sam9g45_pll_layout);
  5625. + of_at91_clk_pll_setup(np, &at91sam9g45_pll_layout);
  5626. }
  5627. +CLK_OF_DECLARE(at91sam9g45_clk_pll, "atmel,at91sam9g45-clk-pll",
  5628. + of_at91sam9g45_clk_pll_setup);
  5629. -void __init of_at91sam9g20_clk_pllb_setup(struct device_node *np,
  5630. - struct at91_pmc *pmc)
  5631. +static void __init of_at91sam9g20_clk_pllb_setup(struct device_node *np)
  5632. {
  5633. - of_at91_clk_pll_setup(np, pmc, &at91sam9g20_pllb_layout);
  5634. + of_at91_clk_pll_setup(np, &at91sam9g20_pllb_layout);
  5635. }
  5636. +CLK_OF_DECLARE(at91sam9g20_clk_pllb, "atmel,at91sam9g20-clk-pllb",
  5637. + of_at91sam9g20_clk_pllb_setup);
  5638. -void __init of_sama5d3_clk_pll_setup(struct device_node *np,
  5639. - struct at91_pmc *pmc)
  5640. +static void __init of_sama5d3_clk_pll_setup(struct device_node *np)
  5641. {
  5642. - of_at91_clk_pll_setup(np, pmc, &sama5d3_pll_layout);
  5643. + of_at91_clk_pll_setup(np, &sama5d3_pll_layout);
  5644. }
  5645. +CLK_OF_DECLARE(sama5d3_clk_pll, "atmel,sama5d3-clk-pll",
  5646. + of_sama5d3_clk_pll_setup);
  5647. diff -Nur linux-4.4.46.orig/drivers/clk/at91/clk-plldiv.c linux-4.4.46/drivers/clk/at91/clk-plldiv.c
  5648. --- linux-4.4.46.orig/drivers/clk/at91/clk-plldiv.c 2017-02-01 08:31:11.000000000 +0100
  5649. +++ linux-4.4.46/drivers/clk/at91/clk-plldiv.c 2017-02-03 17:18:05.671416021 +0100
  5650. @@ -12,8 +12,8 @@
  5651. #include <linux/clkdev.h>
  5652. #include <linux/clk/at91_pmc.h>
  5653. #include <linux/of.h>
  5654. -#include <linux/of_address.h>
  5655. -#include <linux/io.h>
  5656. +#include <linux/mfd/syscon.h>
  5657. +#include <linux/regmap.h>
  5658. #include "pmc.h"
  5659. @@ -21,16 +21,18 @@
  5660. struct clk_plldiv {
  5661. struct clk_hw hw;
  5662. - struct at91_pmc *pmc;
  5663. + struct regmap *regmap;
  5664. };
  5665. static unsigned long clk_plldiv_recalc_rate(struct clk_hw *hw,
  5666. unsigned long parent_rate)
  5667. {
  5668. struct clk_plldiv *plldiv = to_clk_plldiv(hw);
  5669. - struct at91_pmc *pmc = plldiv->pmc;
  5670. + unsigned int mckr;
  5671. - if (pmc_read(pmc, AT91_PMC_MCKR) & AT91_PMC_PLLADIV2)
  5672. + regmap_read(plldiv->regmap, AT91_PMC_MCKR, &mckr);
  5673. +
  5674. + if (mckr & AT91_PMC_PLLADIV2)
  5675. return parent_rate / 2;
  5676. return parent_rate;
  5677. @@ -57,18 +59,12 @@
  5678. unsigned long parent_rate)
  5679. {
  5680. struct clk_plldiv *plldiv = to_clk_plldiv(hw);
  5681. - struct at91_pmc *pmc = plldiv->pmc;
  5682. - u32 tmp;
  5683. - if (parent_rate != rate && (parent_rate / 2) != rate)
  5684. + if ((parent_rate != rate) && (parent_rate / 2 != rate))
  5685. return -EINVAL;
  5686. - pmc_lock(pmc);
  5687. - tmp = pmc_read(pmc, AT91_PMC_MCKR) & ~AT91_PMC_PLLADIV2;
  5688. - if ((parent_rate / 2) == rate)
  5689. - tmp |= AT91_PMC_PLLADIV2;
  5690. - pmc_write(pmc, AT91_PMC_MCKR, tmp);
  5691. - pmc_unlock(pmc);
  5692. + regmap_update_bits(plldiv->regmap, AT91_PMC_MCKR, AT91_PMC_PLLADIV2,
  5693. + parent_rate != rate ? AT91_PMC_PLLADIV2 : 0);
  5694. return 0;
  5695. }
  5696. @@ -80,7 +76,7 @@
  5697. };
  5698. static struct clk * __init
  5699. -at91_clk_register_plldiv(struct at91_pmc *pmc, const char *name,
  5700. +at91_clk_register_plldiv(struct regmap *regmap, const char *name,
  5701. const char *parent_name)
  5702. {
  5703. struct clk_plldiv *plldiv;
  5704. @@ -98,7 +94,7 @@
  5705. init.flags = CLK_SET_RATE_GATE;
  5706. plldiv->hw.init = &init;
  5707. - plldiv->pmc = pmc;
  5708. + plldiv->regmap = regmap;
  5709. clk = clk_register(NULL, &plldiv->hw);
  5710. @@ -109,27 +105,27 @@
  5711. }
  5712. static void __init
  5713. -of_at91_clk_plldiv_setup(struct device_node *np, struct at91_pmc *pmc)
  5714. +of_at91sam9x5_clk_plldiv_setup(struct device_node *np)
  5715. {
  5716. struct clk *clk;
  5717. const char *parent_name;
  5718. const char *name = np->name;
  5719. + struct regmap *regmap;
  5720. parent_name = of_clk_get_parent_name(np, 0);
  5721. of_property_read_string(np, "clock-output-names", &name);
  5722. - clk = at91_clk_register_plldiv(pmc, name, parent_name);
  5723. + regmap = syscon_node_to_regmap(of_get_parent(np));
  5724. + if (IS_ERR(regmap))
  5725. + return;
  5726. + clk = at91_clk_register_plldiv(regmap, name, parent_name);
  5727. if (IS_ERR(clk))
  5728. return;
  5729. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  5730. return;
  5731. }
  5732. -
  5733. -void __init of_at91sam9x5_clk_plldiv_setup(struct device_node *np,
  5734. - struct at91_pmc *pmc)
  5735. -{
  5736. - of_at91_clk_plldiv_setup(np, pmc);
  5737. -}
  5738. +CLK_OF_DECLARE(at91sam9x5_clk_plldiv, "atmel,at91sam9x5-clk-plldiv",
  5739. + of_at91sam9x5_clk_plldiv_setup);
  5740. diff -Nur linux-4.4.46.orig/drivers/clk/at91/clk-programmable.c linux-4.4.46/drivers/clk/at91/clk-programmable.c
  5741. --- linux-4.4.46.orig/drivers/clk/at91/clk-programmable.c 2017-02-01 08:31:11.000000000 +0100
  5742. +++ linux-4.4.46/drivers/clk/at91/clk-programmable.c 2017-02-03 17:18:05.675416176 +0100
  5743. @@ -12,10 +12,8 @@
  5744. #include <linux/clkdev.h>
  5745. #include <linux/clk/at91_pmc.h>
  5746. #include <linux/of.h>
  5747. -#include <linux/of_address.h>
  5748. -#include <linux/io.h>
  5749. -#include <linux/wait.h>
  5750. -#include <linux/sched.h>
  5751. +#include <linux/mfd/syscon.h>
  5752. +#include <linux/regmap.h>
  5753. #include "pmc.h"
  5754. @@ -24,6 +22,7 @@
  5755. #define PROG_STATUS_MASK(id) (1 << ((id) + 8))
  5756. #define PROG_PRES_MASK 0x7
  5757. +#define PROG_PRES(layout, pckr) ((pckr >> layout->pres_shift) & PROG_PRES_MASK)
  5758. #define PROG_MAX_RM9200_CSS 3
  5759. struct clk_programmable_layout {
  5760. @@ -34,7 +33,7 @@
  5761. struct clk_programmable {
  5762. struct clk_hw hw;
  5763. - struct at91_pmc *pmc;
  5764. + struct regmap *regmap;
  5765. u8 id;
  5766. const struct clk_programmable_layout *layout;
  5767. };
  5768. @@ -44,14 +43,12 @@
  5769. static unsigned long clk_programmable_recalc_rate(struct clk_hw *hw,
  5770. unsigned long parent_rate)
  5771. {
  5772. - u32 pres;
  5773. struct clk_programmable *prog = to_clk_programmable(hw);
  5774. - struct at91_pmc *pmc = prog->pmc;
  5775. - const struct clk_programmable_layout *layout = prog->layout;
  5776. + unsigned int pckr;
  5777. +
  5778. + regmap_read(prog->regmap, AT91_PMC_PCKR(prog->id), &pckr);
  5779. - pres = (pmc_read(pmc, AT91_PMC_PCKR(prog->id)) >> layout->pres_shift) &
  5780. - PROG_PRES_MASK;
  5781. - return parent_rate >> pres;
  5782. + return parent_rate >> PROG_PRES(prog->layout, pckr);
  5783. }
  5784. static int clk_programmable_determine_rate(struct clk_hw *hw,
  5785. @@ -101,36 +98,36 @@
  5786. {
  5787. struct clk_programmable *prog = to_clk_programmable(hw);
  5788. const struct clk_programmable_layout *layout = prog->layout;
  5789. - struct at91_pmc *pmc = prog->pmc;
  5790. - u32 tmp = pmc_read(pmc, AT91_PMC_PCKR(prog->id)) & ~layout->css_mask;
  5791. + unsigned int mask = layout->css_mask;
  5792. + unsigned int pckr = 0;
  5793. if (layout->have_slck_mck)
  5794. - tmp &= AT91_PMC_CSSMCK_MCK;
  5795. + mask |= AT91_PMC_CSSMCK_MCK;
  5796. if (index > layout->css_mask) {
  5797. - if (index > PROG_MAX_RM9200_CSS && layout->have_slck_mck) {
  5798. - tmp |= AT91_PMC_CSSMCK_MCK;
  5799. - return 0;
  5800. - } else {
  5801. + if (index > PROG_MAX_RM9200_CSS && !layout->have_slck_mck)
  5802. return -EINVAL;
  5803. - }
  5804. +
  5805. + pckr |= AT91_PMC_CSSMCK_MCK;
  5806. }
  5807. - pmc_write(pmc, AT91_PMC_PCKR(prog->id), tmp | index);
  5808. + regmap_update_bits(prog->regmap, AT91_PMC_PCKR(prog->id), mask, pckr);
  5809. +
  5810. return 0;
  5811. }
  5812. static u8 clk_programmable_get_parent(struct clk_hw *hw)
  5813. {
  5814. - u32 tmp;
  5815. - u8 ret;
  5816. struct clk_programmable *prog = to_clk_programmable(hw);
  5817. - struct at91_pmc *pmc = prog->pmc;
  5818. const struct clk_programmable_layout *layout = prog->layout;
  5819. + unsigned int pckr;
  5820. + u8 ret;
  5821. +
  5822. + regmap_read(prog->regmap, AT91_PMC_PCKR(prog->id), &pckr);
  5823. +
  5824. + ret = pckr & layout->css_mask;
  5825. - tmp = pmc_read(pmc, AT91_PMC_PCKR(prog->id));
  5826. - ret = tmp & layout->css_mask;
  5827. - if (layout->have_slck_mck && (tmp & AT91_PMC_CSSMCK_MCK) && !ret)
  5828. + if (layout->have_slck_mck && (pckr & AT91_PMC_CSSMCK_MCK) && !ret)
  5829. ret = PROG_MAX_RM9200_CSS + 1;
  5830. return ret;
  5831. @@ -140,26 +137,27 @@
  5832. unsigned long parent_rate)
  5833. {
  5834. struct clk_programmable *prog = to_clk_programmable(hw);
  5835. - struct at91_pmc *pmc = prog->pmc;
  5836. const struct clk_programmable_layout *layout = prog->layout;
  5837. unsigned long div = parent_rate / rate;
  5838. + unsigned int pckr;
  5839. int shift = 0;
  5840. - u32 tmp = pmc_read(pmc, AT91_PMC_PCKR(prog->id)) &
  5841. - ~(PROG_PRES_MASK << layout->pres_shift);
  5842. +
  5843. + regmap_read(prog->regmap, AT91_PMC_PCKR(prog->id), &pckr);
  5844. if (!div)
  5845. return -EINVAL;
  5846. shift = fls(div) - 1;
  5847. - if (div != (1<<shift))
  5848. + if (div != (1 << shift))
  5849. return -EINVAL;
  5850. if (shift >= PROG_PRES_MASK)
  5851. return -EINVAL;
  5852. - pmc_write(pmc, AT91_PMC_PCKR(prog->id),
  5853. - tmp | (shift << layout->pres_shift));
  5854. + regmap_update_bits(prog->regmap, AT91_PMC_PCKR(prog->id),
  5855. + PROG_PRES_MASK << layout->pres_shift,
  5856. + shift << layout->pres_shift);
  5857. return 0;
  5858. }
  5859. @@ -173,7 +171,7 @@
  5860. };
  5861. static struct clk * __init
  5862. -at91_clk_register_programmable(struct at91_pmc *pmc,
  5863. +at91_clk_register_programmable(struct regmap *regmap,
  5864. const char *name, const char **parent_names,
  5865. u8 num_parents, u8 id,
  5866. const struct clk_programmable_layout *layout)
  5867. @@ -198,7 +196,7 @@
  5868. prog->id = id;
  5869. prog->layout = layout;
  5870. prog->hw.init = &init;
  5871. - prog->pmc = pmc;
  5872. + prog->regmap = regmap;
  5873. clk = clk_register(NULL, &prog->hw);
  5874. if (IS_ERR(clk))
  5875. @@ -226,7 +224,7 @@
  5876. };
  5877. static void __init
  5878. -of_at91_clk_prog_setup(struct device_node *np, struct at91_pmc *pmc,
  5879. +of_at91_clk_prog_setup(struct device_node *np,
  5880. const struct clk_programmable_layout *layout)
  5881. {
  5882. int num;
  5883. @@ -236,6 +234,7 @@
  5884. const char *parent_names[PROG_SOURCE_MAX];
  5885. const char *name;
  5886. struct device_node *progclknp;
  5887. + struct regmap *regmap;
  5888. num_parents = of_clk_get_parent_count(np);
  5889. if (num_parents <= 0 || num_parents > PROG_SOURCE_MAX)
  5890. @@ -247,6 +246,10 @@
  5891. if (!num || num > (PROG_ID_MAX + 1))
  5892. return;
  5893. + regmap = syscon_node_to_regmap(of_get_parent(np));
  5894. + if (IS_ERR(regmap))
  5895. + return;
  5896. +
  5897. for_each_child_of_node(np, progclknp) {
  5898. if (of_property_read_u32(progclknp, "reg", &id))
  5899. continue;
  5900. @@ -254,7 +257,7 @@
  5901. if (of_property_read_string(np, "clock-output-names", &name))
  5902. name = progclknp->name;
  5903. - clk = at91_clk_register_programmable(pmc, name,
  5904. + clk = at91_clk_register_programmable(regmap, name,
  5905. parent_names, num_parents,
  5906. id, layout);
  5907. if (IS_ERR(clk))
  5908. @@ -265,20 +268,23 @@
  5909. }
  5910. -void __init of_at91rm9200_clk_prog_setup(struct device_node *np,
  5911. - struct at91_pmc *pmc)
  5912. +static void __init of_at91rm9200_clk_prog_setup(struct device_node *np)
  5913. {
  5914. - of_at91_clk_prog_setup(np, pmc, &at91rm9200_programmable_layout);
  5915. + of_at91_clk_prog_setup(np, &at91rm9200_programmable_layout);
  5916. }
  5917. +CLK_OF_DECLARE(at91rm9200_clk_prog, "atmel,at91rm9200-clk-programmable",
  5918. + of_at91rm9200_clk_prog_setup);
  5919. -void __init of_at91sam9g45_clk_prog_setup(struct device_node *np,
  5920. - struct at91_pmc *pmc)
  5921. +static void __init of_at91sam9g45_clk_prog_setup(struct device_node *np)
  5922. {
  5923. - of_at91_clk_prog_setup(np, pmc, &at91sam9g45_programmable_layout);
  5924. + of_at91_clk_prog_setup(np, &at91sam9g45_programmable_layout);
  5925. }
  5926. +CLK_OF_DECLARE(at91sam9g45_clk_prog, "atmel,at91sam9g45-clk-programmable",
  5927. + of_at91sam9g45_clk_prog_setup);
  5928. -void __init of_at91sam9x5_clk_prog_setup(struct device_node *np,
  5929. - struct at91_pmc *pmc)
  5930. +static void __init of_at91sam9x5_clk_prog_setup(struct device_node *np)
  5931. {
  5932. - of_at91_clk_prog_setup(np, pmc, &at91sam9x5_programmable_layout);
  5933. + of_at91_clk_prog_setup(np, &at91sam9x5_programmable_layout);
  5934. }
  5935. +CLK_OF_DECLARE(at91sam9x5_clk_prog, "atmel,at91sam9x5-clk-programmable",
  5936. + of_at91sam9x5_clk_prog_setup);
  5937. diff -Nur linux-4.4.46.orig/drivers/clk/at91/clk-slow.c linux-4.4.46/drivers/clk/at91/clk-slow.c
  5938. --- linux-4.4.46.orig/drivers/clk/at91/clk-slow.c 2017-02-01 08:31:11.000000000 +0100
  5939. +++ linux-4.4.46/drivers/clk/at91/clk-slow.c 2017-02-03 17:18:05.675416176 +0100
  5940. @@ -13,17 +13,11 @@
  5941. #include <linux/clk.h>
  5942. #include <linux/clk-provider.h>
  5943. #include <linux/clkdev.h>
  5944. -#include <linux/slab.h>
  5945. #include <linux/clk/at91_pmc.h>
  5946. #include <linux/delay.h>
  5947. #include <linux/of.h>
  5948. -#include <linux/of_address.h>
  5949. -#include <linux/of_irq.h>
  5950. -#include <linux/io.h>
  5951. -#include <linux/interrupt.h>
  5952. -#include <linux/irq.h>
  5953. -#include <linux/sched.h>
  5954. -#include <linux/wait.h>
  5955. +#include <linux/mfd/syscon.h>
  5956. +#include <linux/regmap.h>
  5957. #include "pmc.h"
  5958. #include "sckc.h"
  5959. @@ -59,7 +53,7 @@
  5960. struct clk_sam9260_slow {
  5961. struct clk_hw hw;
  5962. - struct at91_pmc *pmc;
  5963. + struct regmap *regmap;
  5964. };
  5965. #define to_clk_sam9260_slow(hw) container_of(hw, struct clk_sam9260_slow, hw)
  5966. @@ -393,8 +387,11 @@
  5967. static u8 clk_sam9260_slow_get_parent(struct clk_hw *hw)
  5968. {
  5969. struct clk_sam9260_slow *slowck = to_clk_sam9260_slow(hw);
  5970. + unsigned int status;
  5971. - return !!(pmc_read(slowck->pmc, AT91_PMC_SR) & AT91_PMC_OSCSEL);
  5972. + regmap_read(slowck->regmap, AT91_PMC_SR, &status);
  5973. +
  5974. + return status & AT91_PMC_OSCSEL ? 1 : 0;
  5975. }
  5976. static const struct clk_ops sam9260_slow_ops = {
  5977. @@ -402,7 +399,7 @@
  5978. };
  5979. static struct clk * __init
  5980. -at91_clk_register_sam9260_slow(struct at91_pmc *pmc,
  5981. +at91_clk_register_sam9260_slow(struct regmap *regmap,
  5982. const char *name,
  5983. const char **parent_names,
  5984. int num_parents)
  5985. @@ -411,7 +408,7 @@
  5986. struct clk *clk = NULL;
  5987. struct clk_init_data init;
  5988. - if (!pmc || !name)
  5989. + if (!name)
  5990. return ERR_PTR(-EINVAL);
  5991. if (!parent_names || !num_parents)
  5992. @@ -428,7 +425,7 @@
  5993. init.flags = 0;
  5994. slowck->hw.init = &init;
  5995. - slowck->pmc = pmc;
  5996. + slowck->regmap = regmap;
  5997. clk = clk_register(NULL, &slowck->hw);
  5998. if (IS_ERR(clk))
  5999. @@ -439,29 +436,34 @@
  6000. return clk;
  6001. }
  6002. -void __init of_at91sam9260_clk_slow_setup(struct device_node *np,
  6003. - struct at91_pmc *pmc)
  6004. +static void __init of_at91sam9260_clk_slow_setup(struct device_node *np)
  6005. {
  6006. struct clk *clk;
  6007. const char *parent_names[2];
  6008. int num_parents;
  6009. const char *name = np->name;
  6010. + struct regmap *regmap;
  6011. num_parents = of_clk_get_parent_count(np);
  6012. if (num_parents != 2)
  6013. return;
  6014. of_clk_parent_fill(np, parent_names, num_parents);
  6015. + regmap = syscon_node_to_regmap(of_get_parent(np));
  6016. + if (IS_ERR(regmap))
  6017. + return;
  6018. of_property_read_string(np, "clock-output-names", &name);
  6019. - clk = at91_clk_register_sam9260_slow(pmc, name, parent_names,
  6020. + clk = at91_clk_register_sam9260_slow(regmap, name, parent_names,
  6021. num_parents);
  6022. if (IS_ERR(clk))
  6023. return;
  6024. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  6025. }
  6026. +CLK_OF_DECLARE(at91sam9260_clk_slow, "atmel,at91sam9260-clk-slow",
  6027. + of_at91sam9260_clk_slow_setup);
  6028. /*
  6029. * FIXME: All slow clk users are not properly claiming it (get + prepare +
  6030. diff -Nur linux-4.4.46.orig/drivers/clk/at91/clk-smd.c linux-4.4.46/drivers/clk/at91/clk-smd.c
  6031. --- linux-4.4.46.orig/drivers/clk/at91/clk-smd.c 2017-02-01 08:31:11.000000000 +0100
  6032. +++ linux-4.4.46/drivers/clk/at91/clk-smd.c 2017-02-03 17:18:05.675416176 +0100
  6033. @@ -12,8 +12,8 @@
  6034. #include <linux/clkdev.h>
  6035. #include <linux/clk/at91_pmc.h>
  6036. #include <linux/of.h>
  6037. -#include <linux/of_address.h>
  6038. -#include <linux/io.h>
  6039. +#include <linux/mfd/syscon.h>
  6040. +#include <linux/regmap.h>
  6041. #include "pmc.h"
  6042. @@ -24,7 +24,7 @@
  6043. struct at91sam9x5_clk_smd {
  6044. struct clk_hw hw;
  6045. - struct at91_pmc *pmc;
  6046. + struct regmap *regmap;
  6047. };
  6048. #define to_at91sam9x5_clk_smd(hw) \
  6049. @@ -33,13 +33,13 @@
  6050. static unsigned long at91sam9x5_clk_smd_recalc_rate(struct clk_hw *hw,
  6051. unsigned long parent_rate)
  6052. {
  6053. - u32 tmp;
  6054. - u8 smddiv;
  6055. struct at91sam9x5_clk_smd *smd = to_at91sam9x5_clk_smd(hw);
  6056. - struct at91_pmc *pmc = smd->pmc;
  6057. + unsigned int smdr;
  6058. + u8 smddiv;
  6059. +
  6060. + regmap_read(smd->regmap, AT91_PMC_SMD, &smdr);
  6061. + smddiv = (smdr & AT91_PMC_SMD_DIV) >> SMD_DIV_SHIFT;
  6062. - tmp = pmc_read(pmc, AT91_PMC_SMD);
  6063. - smddiv = (tmp & AT91_PMC_SMD_DIV) >> SMD_DIV_SHIFT;
  6064. return parent_rate / (smddiv + 1);
  6065. }
  6066. @@ -67,40 +67,38 @@
  6067. static int at91sam9x5_clk_smd_set_parent(struct clk_hw *hw, u8 index)
  6068. {
  6069. - u32 tmp;
  6070. struct at91sam9x5_clk_smd *smd = to_at91sam9x5_clk_smd(hw);
  6071. - struct at91_pmc *pmc = smd->pmc;
  6072. if (index > 1)
  6073. return -EINVAL;
  6074. - tmp = pmc_read(pmc, AT91_PMC_SMD) & ~AT91_PMC_SMDS;
  6075. - if (index)
  6076. - tmp |= AT91_PMC_SMDS;
  6077. - pmc_write(pmc, AT91_PMC_SMD, tmp);
  6078. +
  6079. + regmap_update_bits(smd->regmap, AT91_PMC_SMD, AT91_PMC_SMDS,
  6080. + index ? AT91_PMC_SMDS : 0);
  6081. +
  6082. return 0;
  6083. }
  6084. static u8 at91sam9x5_clk_smd_get_parent(struct clk_hw *hw)
  6085. {
  6086. struct at91sam9x5_clk_smd *smd = to_at91sam9x5_clk_smd(hw);
  6087. - struct at91_pmc *pmc = smd->pmc;
  6088. + unsigned int smdr;
  6089. - return pmc_read(pmc, AT91_PMC_SMD) & AT91_PMC_SMDS;
  6090. + regmap_read(smd->regmap, AT91_PMC_SMD, &smdr);
  6091. +
  6092. + return smdr & AT91_PMC_SMDS;
  6093. }
  6094. static int at91sam9x5_clk_smd_set_rate(struct clk_hw *hw, unsigned long rate,
  6095. unsigned long parent_rate)
  6096. {
  6097. - u32 tmp;
  6098. struct at91sam9x5_clk_smd *smd = to_at91sam9x5_clk_smd(hw);
  6099. - struct at91_pmc *pmc = smd->pmc;
  6100. unsigned long div = parent_rate / rate;
  6101. if (parent_rate % rate || div < 1 || div > (SMD_MAX_DIV + 1))
  6102. return -EINVAL;
  6103. - tmp = pmc_read(pmc, AT91_PMC_SMD) & ~AT91_PMC_SMD_DIV;
  6104. - tmp |= (div - 1) << SMD_DIV_SHIFT;
  6105. - pmc_write(pmc, AT91_PMC_SMD, tmp);
  6106. +
  6107. + regmap_update_bits(smd->regmap, AT91_PMC_SMD, AT91_PMC_SMD_DIV,
  6108. + (div - 1) << SMD_DIV_SHIFT);
  6109. return 0;
  6110. }
  6111. @@ -114,7 +112,7 @@
  6112. };
  6113. static struct clk * __init
  6114. -at91sam9x5_clk_register_smd(struct at91_pmc *pmc, const char *name,
  6115. +at91sam9x5_clk_register_smd(struct regmap *regmap, const char *name,
  6116. const char **parent_names, u8 num_parents)
  6117. {
  6118. struct at91sam9x5_clk_smd *smd;
  6119. @@ -132,7 +130,7 @@
  6120. init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
  6121. smd->hw.init = &init;
  6122. - smd->pmc = pmc;
  6123. + smd->regmap = regmap;
  6124. clk = clk_register(NULL, &smd->hw);
  6125. if (IS_ERR(clk))
  6126. @@ -141,13 +139,13 @@
  6127. return clk;
  6128. }
  6129. -void __init of_at91sam9x5_clk_smd_setup(struct device_node *np,
  6130. - struct at91_pmc *pmc)
  6131. +static void __init of_at91sam9x5_clk_smd_setup(struct device_node *np)
  6132. {
  6133. struct clk *clk;
  6134. int num_parents;
  6135. const char *parent_names[SMD_SOURCE_MAX];
  6136. const char *name = np->name;
  6137. + struct regmap *regmap;
  6138. num_parents = of_clk_get_parent_count(np);
  6139. if (num_parents <= 0 || num_parents > SMD_SOURCE_MAX)
  6140. @@ -157,10 +155,16 @@
  6141. of_property_read_string(np, "clock-output-names", &name);
  6142. - clk = at91sam9x5_clk_register_smd(pmc, name, parent_names,
  6143. + regmap = syscon_node_to_regmap(of_get_parent(np));
  6144. + if (IS_ERR(regmap))
  6145. + return;
  6146. +
  6147. + clk = at91sam9x5_clk_register_smd(regmap, name, parent_names,
  6148. num_parents);
  6149. if (IS_ERR(clk))
  6150. return;
  6151. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  6152. }
  6153. +CLK_OF_DECLARE(at91sam9x5_clk_smd, "atmel,at91sam9x5-clk-smd",
  6154. + of_at91sam9x5_clk_smd_setup);
  6155. diff -Nur linux-4.4.46.orig/drivers/clk/at91/clk-system.c linux-4.4.46/drivers/clk/at91/clk-system.c
  6156. --- linux-4.4.46.orig/drivers/clk/at91/clk-system.c 2017-02-01 08:31:11.000000000 +0100
  6157. +++ linux-4.4.46/drivers/clk/at91/clk-system.c 2017-02-03 17:18:05.675416176 +0100
  6158. @@ -12,13 +12,8 @@
  6159. #include <linux/clkdev.h>
  6160. #include <linux/clk/at91_pmc.h>
  6161. #include <linux/of.h>
  6162. -#include <linux/of_address.h>
  6163. -#include <linux/io.h>
  6164. -#include <linux/irq.h>
  6165. -#include <linux/of_irq.h>
  6166. -#include <linux/interrupt.h>
  6167. -#include <linux/wait.h>
  6168. -#include <linux/sched.h>
  6169. +#include <linux/mfd/syscon.h>
  6170. +#include <linux/regmap.h>
  6171. #include "pmc.h"
  6172. @@ -29,9 +24,7 @@
  6173. #define to_clk_system(hw) container_of(hw, struct clk_system, hw)
  6174. struct clk_system {
  6175. struct clk_hw hw;
  6176. - struct at91_pmc *pmc;
  6177. - unsigned int irq;
  6178. - wait_queue_head_t wait;
  6179. + struct regmap *regmap;
  6180. u8 id;
  6181. };
  6182. @@ -39,58 +32,54 @@
  6183. {
  6184. return (id >= 8) && (id <= 15);
  6185. }
  6186. -static irqreturn_t clk_system_irq_handler(int irq, void *dev_id)
  6187. +
  6188. +static inline bool clk_system_ready(struct regmap *regmap, int id)
  6189. {
  6190. - struct clk_system *sys = (struct clk_system *)dev_id;
  6191. + unsigned int status;
  6192. - wake_up(&sys->wait);
  6193. - disable_irq_nosync(sys->irq);
  6194. + regmap_read(regmap, AT91_PMC_SR, &status);
  6195. - return IRQ_HANDLED;
  6196. + return status & (1 << id) ? 1 : 0;
  6197. }
  6198. static int clk_system_prepare(struct clk_hw *hw)
  6199. {
  6200. struct clk_system *sys = to_clk_system(hw);
  6201. - struct at91_pmc *pmc = sys->pmc;
  6202. - u32 mask = 1 << sys->id;
  6203. - pmc_write(pmc, AT91_PMC_SCER, mask);
  6204. + regmap_write(sys->regmap, AT91_PMC_SCER, 1 << sys->id);
  6205. if (!is_pck(sys->id))
  6206. return 0;
  6207. - while (!(pmc_read(pmc, AT91_PMC_SR) & mask)) {
  6208. - if (sys->irq) {
  6209. - enable_irq(sys->irq);
  6210. - wait_event(sys->wait,
  6211. - pmc_read(pmc, AT91_PMC_SR) & mask);
  6212. - } else
  6213. - cpu_relax();
  6214. - }
  6215. + while (!clk_system_ready(sys->regmap, sys->id))
  6216. + cpu_relax();
  6217. +
  6218. return 0;
  6219. }
  6220. static void clk_system_unprepare(struct clk_hw *hw)
  6221. {
  6222. struct clk_system *sys = to_clk_system(hw);
  6223. - struct at91_pmc *pmc = sys->pmc;
  6224. - pmc_write(pmc, AT91_PMC_SCDR, 1 << sys->id);
  6225. + regmap_write(sys->regmap, AT91_PMC_SCDR, 1 << sys->id);
  6226. }
  6227. static int clk_system_is_prepared(struct clk_hw *hw)
  6228. {
  6229. struct clk_system *sys = to_clk_system(hw);
  6230. - struct at91_pmc *pmc = sys->pmc;
  6231. + unsigned int status;
  6232. +
  6233. + regmap_read(sys->regmap, AT91_PMC_SCSR, &status);
  6234. - if (!(pmc_read(pmc, AT91_PMC_SCSR) & (1 << sys->id)))
  6235. + if (!(status & (1 << sys->id)))
  6236. return 0;
  6237. if (!is_pck(sys->id))
  6238. return 1;
  6239. - return !!(pmc_read(pmc, AT91_PMC_SR) & (1 << sys->id));
  6240. + regmap_read(sys->regmap, AT91_PMC_SR, &status);
  6241. +
  6242. + return status & (1 << sys->id) ? 1 : 0;
  6243. }
  6244. static const struct clk_ops system_ops = {
  6245. @@ -100,13 +89,12 @@
  6246. };
  6247. static struct clk * __init
  6248. -at91_clk_register_system(struct at91_pmc *pmc, const char *name,
  6249. - const char *parent_name, u8 id, int irq)
  6250. +at91_clk_register_system(struct regmap *regmap, const char *name,
  6251. + const char *parent_name, u8 id)
  6252. {
  6253. struct clk_system *sys;
  6254. struct clk *clk = NULL;
  6255. struct clk_init_data init;
  6256. - int ret;
  6257. if (!parent_name || id > SYSTEM_MAX_ID)
  6258. return ERR_PTR(-EINVAL);
  6259. @@ -123,44 +111,33 @@
  6260. sys->id = id;
  6261. sys->hw.init = &init;
  6262. - sys->pmc = pmc;
  6263. - sys->irq = irq;
  6264. - if (irq) {
  6265. - init_waitqueue_head(&sys->wait);
  6266. - irq_set_status_flags(sys->irq, IRQ_NOAUTOEN);
  6267. - ret = request_irq(sys->irq, clk_system_irq_handler,
  6268. - IRQF_TRIGGER_HIGH, name, sys);
  6269. - if (ret) {
  6270. - kfree(sys);
  6271. - return ERR_PTR(ret);
  6272. - }
  6273. - }
  6274. + sys->regmap = regmap;
  6275. clk = clk_register(NULL, &sys->hw);
  6276. - if (IS_ERR(clk)) {
  6277. - if (irq)
  6278. - free_irq(sys->irq, sys);
  6279. + if (IS_ERR(clk))
  6280. kfree(sys);
  6281. - }
  6282. return clk;
  6283. }
  6284. -static void __init
  6285. -of_at91_clk_sys_setup(struct device_node *np, struct at91_pmc *pmc)
  6286. +static void __init of_at91rm9200_clk_sys_setup(struct device_node *np)
  6287. {
  6288. int num;
  6289. - int irq = 0;
  6290. u32 id;
  6291. struct clk *clk;
  6292. const char *name;
  6293. struct device_node *sysclknp;
  6294. const char *parent_name;
  6295. + struct regmap *regmap;
  6296. num = of_get_child_count(np);
  6297. if (num > (SYSTEM_MAX_ID + 1))
  6298. return;
  6299. + regmap = syscon_node_to_regmap(of_get_parent(np));
  6300. + if (IS_ERR(regmap))
  6301. + return;
  6302. +
  6303. for_each_child_of_node(np, sysclknp) {
  6304. if (of_property_read_u32(sysclknp, "reg", &id))
  6305. continue;
  6306. @@ -168,21 +145,14 @@
  6307. if (of_property_read_string(np, "clock-output-names", &name))
  6308. name = sysclknp->name;
  6309. - if (is_pck(id))
  6310. - irq = irq_of_parse_and_map(sysclknp, 0);
  6311. -
  6312. parent_name = of_clk_get_parent_name(sysclknp, 0);
  6313. - clk = at91_clk_register_system(pmc, name, parent_name, id, irq);
  6314. + clk = at91_clk_register_system(regmap, name, parent_name, id);
  6315. if (IS_ERR(clk))
  6316. continue;
  6317. of_clk_add_provider(sysclknp, of_clk_src_simple_get, clk);
  6318. }
  6319. }
  6320. -
  6321. -void __init of_at91rm9200_clk_sys_setup(struct device_node *np,
  6322. - struct at91_pmc *pmc)
  6323. -{
  6324. - of_at91_clk_sys_setup(np, pmc);
  6325. -}
  6326. +CLK_OF_DECLARE(at91rm9200_clk_sys, "atmel,at91rm9200-clk-system",
  6327. + of_at91rm9200_clk_sys_setup);
  6328. diff -Nur linux-4.4.46.orig/drivers/clk/at91/clk-usb.c linux-4.4.46/drivers/clk/at91/clk-usb.c
  6329. --- linux-4.4.46.orig/drivers/clk/at91/clk-usb.c 2017-02-01 08:31:11.000000000 +0100
  6330. +++ linux-4.4.46/drivers/clk/at91/clk-usb.c 2017-02-03 17:18:05.675416176 +0100
  6331. @@ -12,8 +12,8 @@
  6332. #include <linux/clkdev.h>
  6333. #include <linux/clk/at91_pmc.h>
  6334. #include <linux/of.h>
  6335. -#include <linux/of_address.h>
  6336. -#include <linux/io.h>
  6337. +#include <linux/mfd/syscon.h>
  6338. +#include <linux/regmap.h>
  6339. #include "pmc.h"
  6340. @@ -27,7 +27,7 @@
  6341. struct at91sam9x5_clk_usb {
  6342. struct clk_hw hw;
  6343. - struct at91_pmc *pmc;
  6344. + struct regmap *regmap;
  6345. };
  6346. #define to_at91sam9x5_clk_usb(hw) \
  6347. @@ -35,7 +35,7 @@
  6348. struct at91rm9200_clk_usb {
  6349. struct clk_hw hw;
  6350. - struct at91_pmc *pmc;
  6351. + struct regmap *regmap;
  6352. u32 divisors[4];
  6353. };
  6354. @@ -45,13 +45,12 @@
  6355. static unsigned long at91sam9x5_clk_usb_recalc_rate(struct clk_hw *hw,
  6356. unsigned long parent_rate)
  6357. {
  6358. - u32 tmp;
  6359. - u8 usbdiv;
  6360. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  6361. - struct at91_pmc *pmc = usb->pmc;
  6362. + unsigned int usbr;
  6363. + u8 usbdiv;
  6364. - tmp = pmc_read(pmc, AT91_PMC_USB);
  6365. - usbdiv = (tmp & AT91_PMC_OHCIUSBDIV) >> SAM9X5_USB_DIV_SHIFT;
  6366. + regmap_read(usb->regmap, AT91_PMC_USB, &usbr);
  6367. + usbdiv = (usbr & AT91_PMC_OHCIUSBDIV) >> SAM9X5_USB_DIV_SHIFT;
  6368. return DIV_ROUND_CLOSEST(parent_rate, (usbdiv + 1));
  6369. }
  6370. @@ -109,33 +108,31 @@
  6371. static int at91sam9x5_clk_usb_set_parent(struct clk_hw *hw, u8 index)
  6372. {
  6373. - u32 tmp;
  6374. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  6375. - struct at91_pmc *pmc = usb->pmc;
  6376. if (index > 1)
  6377. return -EINVAL;
  6378. - tmp = pmc_read(pmc, AT91_PMC_USB) & ~AT91_PMC_USBS;
  6379. - if (index)
  6380. - tmp |= AT91_PMC_USBS;
  6381. - pmc_write(pmc, AT91_PMC_USB, tmp);
  6382. +
  6383. + regmap_update_bits(usb->regmap, AT91_PMC_USB, AT91_PMC_USBS,
  6384. + index ? AT91_PMC_USBS : 0);
  6385. +
  6386. return 0;
  6387. }
  6388. static u8 at91sam9x5_clk_usb_get_parent(struct clk_hw *hw)
  6389. {
  6390. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  6391. - struct at91_pmc *pmc = usb->pmc;
  6392. + unsigned int usbr;
  6393. - return pmc_read(pmc, AT91_PMC_USB) & AT91_PMC_USBS;
  6394. + regmap_read(usb->regmap, AT91_PMC_USB, &usbr);
  6395. +
  6396. + return usbr & AT91_PMC_USBS;
  6397. }
  6398. static int at91sam9x5_clk_usb_set_rate(struct clk_hw *hw, unsigned long rate,
  6399. unsigned long parent_rate)
  6400. {
  6401. - u32 tmp;
  6402. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  6403. - struct at91_pmc *pmc = usb->pmc;
  6404. unsigned long div;
  6405. if (!rate)
  6406. @@ -145,9 +142,8 @@
  6407. if (div > SAM9X5_USB_MAX_DIV + 1 || !div)
  6408. return -EINVAL;
  6409. - tmp = pmc_read(pmc, AT91_PMC_USB) & ~AT91_PMC_OHCIUSBDIV;
  6410. - tmp |= (div - 1) << SAM9X5_USB_DIV_SHIFT;
  6411. - pmc_write(pmc, AT91_PMC_USB, tmp);
  6412. + regmap_update_bits(usb->regmap, AT91_PMC_USB, AT91_PMC_OHCIUSBDIV,
  6413. + (div - 1) << SAM9X5_USB_DIV_SHIFT);
  6414. return 0;
  6415. }
  6416. @@ -163,28 +159,28 @@
  6417. static int at91sam9n12_clk_usb_enable(struct clk_hw *hw)
  6418. {
  6419. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  6420. - struct at91_pmc *pmc = usb->pmc;
  6421. - pmc_write(pmc, AT91_PMC_USB,
  6422. - pmc_read(pmc, AT91_PMC_USB) | AT91_PMC_USBS);
  6423. + regmap_update_bits(usb->regmap, AT91_PMC_USB, AT91_PMC_USBS,
  6424. + AT91_PMC_USBS);
  6425. +
  6426. return 0;
  6427. }
  6428. static void at91sam9n12_clk_usb_disable(struct clk_hw *hw)
  6429. {
  6430. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  6431. - struct at91_pmc *pmc = usb->pmc;
  6432. - pmc_write(pmc, AT91_PMC_USB,
  6433. - pmc_read(pmc, AT91_PMC_USB) & ~AT91_PMC_USBS);
  6434. + regmap_update_bits(usb->regmap, AT91_PMC_USB, AT91_PMC_USBS, 0);
  6435. }
  6436. static int at91sam9n12_clk_usb_is_enabled(struct clk_hw *hw)
  6437. {
  6438. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  6439. - struct at91_pmc *pmc = usb->pmc;
  6440. + unsigned int usbr;
  6441. - return !!(pmc_read(pmc, AT91_PMC_USB) & AT91_PMC_USBS);
  6442. + regmap_read(usb->regmap, AT91_PMC_USB, &usbr);
  6443. +
  6444. + return usbr & AT91_PMC_USBS;
  6445. }
  6446. static const struct clk_ops at91sam9n12_usb_ops = {
  6447. @@ -197,7 +193,7 @@
  6448. };
  6449. static struct clk * __init
  6450. -at91sam9x5_clk_register_usb(struct at91_pmc *pmc, const char *name,
  6451. +at91sam9x5_clk_register_usb(struct regmap *regmap, const char *name,
  6452. const char **parent_names, u8 num_parents)
  6453. {
  6454. struct at91sam9x5_clk_usb *usb;
  6455. @@ -216,7 +212,7 @@
  6456. CLK_SET_RATE_PARENT;
  6457. usb->hw.init = &init;
  6458. - usb->pmc = pmc;
  6459. + usb->regmap = regmap;
  6460. clk = clk_register(NULL, &usb->hw);
  6461. if (IS_ERR(clk))
  6462. @@ -226,7 +222,7 @@
  6463. }
  6464. static struct clk * __init
  6465. -at91sam9n12_clk_register_usb(struct at91_pmc *pmc, const char *name,
  6466. +at91sam9n12_clk_register_usb(struct regmap *regmap, const char *name,
  6467. const char *parent_name)
  6468. {
  6469. struct at91sam9x5_clk_usb *usb;
  6470. @@ -244,7 +240,7 @@
  6471. init.flags = CLK_SET_RATE_GATE | CLK_SET_RATE_PARENT;
  6472. usb->hw.init = &init;
  6473. - usb->pmc = pmc;
  6474. + usb->regmap = regmap;
  6475. clk = clk_register(NULL, &usb->hw);
  6476. if (IS_ERR(clk))
  6477. @@ -257,12 +253,12 @@
  6478. unsigned long parent_rate)
  6479. {
  6480. struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
  6481. - struct at91_pmc *pmc = usb->pmc;
  6482. - u32 tmp;
  6483. + unsigned int pllbr;
  6484. u8 usbdiv;
  6485. - tmp = pmc_read(pmc, AT91_CKGR_PLLBR);
  6486. - usbdiv = (tmp & AT91_PMC_USBDIV) >> RM9200_USB_DIV_SHIFT;
  6487. + regmap_read(usb->regmap, AT91_CKGR_PLLBR, &pllbr);
  6488. +
  6489. + usbdiv = (pllbr & AT91_PMC_USBDIV) >> RM9200_USB_DIV_SHIFT;
  6490. if (usb->divisors[usbdiv])
  6491. return parent_rate / usb->divisors[usbdiv];
  6492. @@ -310,10 +306,8 @@
  6493. static int at91rm9200_clk_usb_set_rate(struct clk_hw *hw, unsigned long rate,
  6494. unsigned long parent_rate)
  6495. {
  6496. - u32 tmp;
  6497. int i;
  6498. struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
  6499. - struct at91_pmc *pmc = usb->pmc;
  6500. unsigned long div;
  6501. if (!rate)
  6502. @@ -323,10 +317,10 @@
  6503. for (i = 0; i < RM9200_USB_DIV_TAB_SIZE; i++) {
  6504. if (usb->divisors[i] == div) {
  6505. - tmp = pmc_read(pmc, AT91_CKGR_PLLBR) &
  6506. - ~AT91_PMC_USBDIV;
  6507. - tmp |= i << RM9200_USB_DIV_SHIFT;
  6508. - pmc_write(pmc, AT91_CKGR_PLLBR, tmp);
  6509. + regmap_update_bits(usb->regmap, AT91_CKGR_PLLBR,
  6510. + AT91_PMC_USBDIV,
  6511. + i << RM9200_USB_DIV_SHIFT);
  6512. +
  6513. return 0;
  6514. }
  6515. }
  6516. @@ -341,7 +335,7 @@
  6517. };
  6518. static struct clk * __init
  6519. -at91rm9200_clk_register_usb(struct at91_pmc *pmc, const char *name,
  6520. +at91rm9200_clk_register_usb(struct regmap *regmap, const char *name,
  6521. const char *parent_name, const u32 *divisors)
  6522. {
  6523. struct at91rm9200_clk_usb *usb;
  6524. @@ -359,7 +353,7 @@
  6525. init.flags = CLK_SET_RATE_PARENT;
  6526. usb->hw.init = &init;
  6527. - usb->pmc = pmc;
  6528. + usb->regmap = regmap;
  6529. memcpy(usb->divisors, divisors, sizeof(usb->divisors));
  6530. clk = clk_register(NULL, &usb->hw);
  6531. @@ -369,13 +363,13 @@
  6532. return clk;
  6533. }
  6534. -void __init of_at91sam9x5_clk_usb_setup(struct device_node *np,
  6535. - struct at91_pmc *pmc)
  6536. +static void __init of_at91sam9x5_clk_usb_setup(struct device_node *np)
  6537. {
  6538. struct clk *clk;
  6539. int num_parents;
  6540. const char *parent_names[USB_SOURCE_MAX];
  6541. const char *name = np->name;
  6542. + struct regmap *regmap;
  6543. num_parents = of_clk_get_parent_count(np);
  6544. if (num_parents <= 0 || num_parents > USB_SOURCE_MAX)
  6545. @@ -385,19 +379,26 @@
  6546. of_property_read_string(np, "clock-output-names", &name);
  6547. - clk = at91sam9x5_clk_register_usb(pmc, name, parent_names, num_parents);
  6548. + regmap = syscon_node_to_regmap(of_get_parent(np));
  6549. + if (IS_ERR(regmap))
  6550. + return;
  6551. +
  6552. + clk = at91sam9x5_clk_register_usb(regmap, name, parent_names,
  6553. + num_parents);
  6554. if (IS_ERR(clk))
  6555. return;
  6556. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  6557. }
  6558. +CLK_OF_DECLARE(at91sam9x5_clk_usb, "atmel,at91sam9x5-clk-usb",
  6559. + of_at91sam9x5_clk_usb_setup);
  6560. -void __init of_at91sam9n12_clk_usb_setup(struct device_node *np,
  6561. - struct at91_pmc *pmc)
  6562. +static void __init of_at91sam9n12_clk_usb_setup(struct device_node *np)
  6563. {
  6564. struct clk *clk;
  6565. const char *parent_name;
  6566. const char *name = np->name;
  6567. + struct regmap *regmap;
  6568. parent_name = of_clk_get_parent_name(np, 0);
  6569. if (!parent_name)
  6570. @@ -405,20 +406,26 @@
  6571. of_property_read_string(np, "clock-output-names", &name);
  6572. - clk = at91sam9n12_clk_register_usb(pmc, name, parent_name);
  6573. + regmap = syscon_node_to_regmap(of_get_parent(np));
  6574. + if (IS_ERR(regmap))
  6575. + return;
  6576. +
  6577. + clk = at91sam9n12_clk_register_usb(regmap, name, parent_name);
  6578. if (IS_ERR(clk))
  6579. return;
  6580. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  6581. }
  6582. +CLK_OF_DECLARE(at91sam9n12_clk_usb, "atmel,at91sam9n12-clk-usb",
  6583. + of_at91sam9n12_clk_usb_setup);
  6584. -void __init of_at91rm9200_clk_usb_setup(struct device_node *np,
  6585. - struct at91_pmc *pmc)
  6586. +static void __init of_at91rm9200_clk_usb_setup(struct device_node *np)
  6587. {
  6588. struct clk *clk;
  6589. const char *parent_name;
  6590. const char *name = np->name;
  6591. u32 divisors[4] = {0, 0, 0, 0};
  6592. + struct regmap *regmap;
  6593. parent_name = of_clk_get_parent_name(np, 0);
  6594. if (!parent_name)
  6595. @@ -430,9 +437,15 @@
  6596. of_property_read_string(np, "clock-output-names", &name);
  6597. - clk = at91rm9200_clk_register_usb(pmc, name, parent_name, divisors);
  6598. + regmap = syscon_node_to_regmap(of_get_parent(np));
  6599. + if (IS_ERR(regmap))
  6600. + return;
  6601. +
  6602. + clk = at91rm9200_clk_register_usb(regmap, name, parent_name, divisors);
  6603. if (IS_ERR(clk))
  6604. return;
  6605. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  6606. }
  6607. +CLK_OF_DECLARE(at91rm9200_clk_usb, "atmel,at91rm9200-clk-usb",
  6608. + of_at91rm9200_clk_usb_setup);
  6609. diff -Nur linux-4.4.46.orig/drivers/clk/at91/clk-utmi.c linux-4.4.46/drivers/clk/at91/clk-utmi.c
  6610. --- linux-4.4.46.orig/drivers/clk/at91/clk-utmi.c 2017-02-01 08:31:11.000000000 +0100
  6611. +++ linux-4.4.46/drivers/clk/at91/clk-utmi.c 2017-02-03 17:18:05.675416176 +0100
  6612. @@ -11,14 +11,9 @@
  6613. #include <linux/clk-provider.h>
  6614. #include <linux/clkdev.h>
  6615. #include <linux/clk/at91_pmc.h>
  6616. -#include <linux/interrupt.h>
  6617. -#include <linux/irq.h>
  6618. #include <linux/of.h>
  6619. -#include <linux/of_address.h>
  6620. -#include <linux/of_irq.h>
  6621. -#include <linux/io.h>
  6622. -#include <linux/sched.h>
  6623. -#include <linux/wait.h>
  6624. +#include <linux/mfd/syscon.h>
  6625. +#include <linux/regmap.h>
  6626. #include "pmc.h"
  6627. @@ -26,37 +21,30 @@
  6628. struct clk_utmi {
  6629. struct clk_hw hw;
  6630. - struct at91_pmc *pmc;
  6631. - unsigned int irq;
  6632. - wait_queue_head_t wait;
  6633. + struct regmap *regmap;
  6634. };
  6635. #define to_clk_utmi(hw) container_of(hw, struct clk_utmi, hw)
  6636. -static irqreturn_t clk_utmi_irq_handler(int irq, void *dev_id)
  6637. +static inline bool clk_utmi_ready(struct regmap *regmap)
  6638. {
  6639. - struct clk_utmi *utmi = (struct clk_utmi *)dev_id;
  6640. + unsigned int status;
  6641. - wake_up(&utmi->wait);
  6642. - disable_irq_nosync(utmi->irq);
  6643. + regmap_read(regmap, AT91_PMC_SR, &status);
  6644. - return IRQ_HANDLED;
  6645. + return status & AT91_PMC_LOCKU;
  6646. }
  6647. static int clk_utmi_prepare(struct clk_hw *hw)
  6648. {
  6649. struct clk_utmi *utmi = to_clk_utmi(hw);
  6650. - struct at91_pmc *pmc = utmi->pmc;
  6651. - u32 tmp = pmc_read(pmc, AT91_CKGR_UCKR) | AT91_PMC_UPLLEN |
  6652. - AT91_PMC_UPLLCOUNT | AT91_PMC_BIASEN;
  6653. -
  6654. - pmc_write(pmc, AT91_CKGR_UCKR, tmp);
  6655. -
  6656. - while (!(pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_LOCKU)) {
  6657. - enable_irq(utmi->irq);
  6658. - wait_event(utmi->wait,
  6659. - pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_LOCKU);
  6660. - }
  6661. + unsigned int uckr = AT91_PMC_UPLLEN | AT91_PMC_UPLLCOUNT |
  6662. + AT91_PMC_BIASEN;
  6663. +
  6664. + regmap_update_bits(utmi->regmap, AT91_CKGR_UCKR, uckr, uckr);
  6665. +
  6666. + while (!clk_utmi_ready(utmi->regmap))
  6667. + cpu_relax();
  6668. return 0;
  6669. }
  6670. @@ -64,18 +52,15 @@
  6671. static int clk_utmi_is_prepared(struct clk_hw *hw)
  6672. {
  6673. struct clk_utmi *utmi = to_clk_utmi(hw);
  6674. - struct at91_pmc *pmc = utmi->pmc;
  6675. - return !!(pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_LOCKU);
  6676. + return clk_utmi_ready(utmi->regmap);
  6677. }
  6678. static void clk_utmi_unprepare(struct clk_hw *hw)
  6679. {
  6680. struct clk_utmi *utmi = to_clk_utmi(hw);
  6681. - struct at91_pmc *pmc = utmi->pmc;
  6682. - u32 tmp = pmc_read(pmc, AT91_CKGR_UCKR) & ~AT91_PMC_UPLLEN;
  6683. - pmc_write(pmc, AT91_CKGR_UCKR, tmp);
  6684. + regmap_update_bits(utmi->regmap, AT91_CKGR_UCKR, AT91_PMC_UPLLEN, 0);
  6685. }
  6686. static unsigned long clk_utmi_recalc_rate(struct clk_hw *hw,
  6687. @@ -93,10 +78,9 @@
  6688. };
  6689. static struct clk * __init
  6690. -at91_clk_register_utmi(struct at91_pmc *pmc, unsigned int irq,
  6691. +at91_clk_register_utmi(struct regmap *regmap,
  6692. const char *name, const char *parent_name)
  6693. {
  6694. - int ret;
  6695. struct clk_utmi *utmi;
  6696. struct clk *clk = NULL;
  6697. struct clk_init_data init;
  6698. @@ -112,52 +96,36 @@
  6699. init.flags = CLK_SET_RATE_GATE;
  6700. utmi->hw.init = &init;
  6701. - utmi->pmc = pmc;
  6702. - utmi->irq = irq;
  6703. - init_waitqueue_head(&utmi->wait);
  6704. - irq_set_status_flags(utmi->irq, IRQ_NOAUTOEN);
  6705. - ret = request_irq(utmi->irq, clk_utmi_irq_handler,
  6706. - IRQF_TRIGGER_HIGH, "clk-utmi", utmi);
  6707. - if (ret) {
  6708. - kfree(utmi);
  6709. - return ERR_PTR(ret);
  6710. - }
  6711. + utmi->regmap = regmap;
  6712. clk = clk_register(NULL, &utmi->hw);
  6713. - if (IS_ERR(clk)) {
  6714. - free_irq(utmi->irq, utmi);
  6715. + if (IS_ERR(clk))
  6716. kfree(utmi);
  6717. - }
  6718. return clk;
  6719. }
  6720. -static void __init
  6721. -of_at91_clk_utmi_setup(struct device_node *np, struct at91_pmc *pmc)
  6722. +static void __init of_at91sam9x5_clk_utmi_setup(struct device_node *np)
  6723. {
  6724. - unsigned int irq;
  6725. struct clk *clk;
  6726. const char *parent_name;
  6727. const char *name = np->name;
  6728. + struct regmap *regmap;
  6729. parent_name = of_clk_get_parent_name(np, 0);
  6730. of_property_read_string(np, "clock-output-names", &name);
  6731. - irq = irq_of_parse_and_map(np, 0);
  6732. - if (!irq)
  6733. + regmap = syscon_node_to_regmap(of_get_parent(np));
  6734. + if (IS_ERR(regmap))
  6735. return;
  6736. - clk = at91_clk_register_utmi(pmc, irq, name, parent_name);
  6737. + clk = at91_clk_register_utmi(regmap, name, parent_name);
  6738. if (IS_ERR(clk))
  6739. return;
  6740. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  6741. return;
  6742. }
  6743. -
  6744. -void __init of_at91sam9x5_clk_utmi_setup(struct device_node *np,
  6745. - struct at91_pmc *pmc)
  6746. -{
  6747. - of_at91_clk_utmi_setup(np, pmc);
  6748. -}
  6749. +CLK_OF_DECLARE(at91sam9x5_clk_utmi, "atmel,at91sam9x5-clk-utmi",
  6750. + of_at91sam9x5_clk_utmi_setup);
  6751. diff -Nur linux-4.4.46.orig/drivers/clk/at91/pmc.c linux-4.4.46/drivers/clk/at91/pmc.c
  6752. --- linux-4.4.46.orig/drivers/clk/at91/pmc.c 2017-02-01 08:31:11.000000000 +0100
  6753. +++ linux-4.4.46/drivers/clk/at91/pmc.c 2017-02-03 17:18:05.675416176 +0100
  6754. @@ -12,36 +12,13 @@
  6755. #include <linux/clkdev.h>
  6756. #include <linux/clk/at91_pmc.h>
  6757. #include <linux/of.h>
  6758. -#include <linux/of_address.h>
  6759. -#include <linux/io.h>
  6760. -#include <linux/interrupt.h>
  6761. -#include <linux/irq.h>
  6762. -#include <linux/irqchip/chained_irq.h>
  6763. -#include <linux/irqdomain.h>
  6764. -#include <linux/of_irq.h>
  6765. +#include <linux/mfd/syscon.h>
  6766. +#include <linux/regmap.h>
  6767. #include <asm/proc-fns.h>
  6768. #include "pmc.h"
  6769. -void __iomem *at91_pmc_base;
  6770. -EXPORT_SYMBOL_GPL(at91_pmc_base);
  6771. -
  6772. -void at91rm9200_idle(void)
  6773. -{
  6774. - /*
  6775. - * Disable the processor clock. The processor will be automatically
  6776. - * re-enabled by an interrupt or by a reset.
  6777. - */
  6778. - at91_pmc_write(AT91_PMC_SCDR, AT91_PMC_PCK);
  6779. -}
  6780. -
  6781. -void at91sam9_idle(void)
  6782. -{
  6783. - at91_pmc_write(AT91_PMC_SCDR, AT91_PMC_PCK);
  6784. - cpu_do_idle();
  6785. -}
  6786. -
  6787. int of_at91_get_clk_range(struct device_node *np, const char *propname,
  6788. struct clk_range *range)
  6789. {
  6790. @@ -64,402 +41,3 @@
  6791. return 0;
  6792. }
  6793. EXPORT_SYMBOL_GPL(of_at91_get_clk_range);
  6794. -
  6795. -static void pmc_irq_mask(struct irq_data *d)
  6796. -{
  6797. - struct at91_pmc *pmc = irq_data_get_irq_chip_data(d);
  6798. -
  6799. - pmc_write(pmc, AT91_PMC_IDR, 1 << d->hwirq);
  6800. -}
  6801. -
  6802. -static void pmc_irq_unmask(struct irq_data *d)
  6803. -{
  6804. - struct at91_pmc *pmc = irq_data_get_irq_chip_data(d);
  6805. -
  6806. - pmc_write(pmc, AT91_PMC_IER, 1 << d->hwirq);
  6807. -}
  6808. -
  6809. -static int pmc_irq_set_type(struct irq_data *d, unsigned type)
  6810. -{
  6811. - if (type != IRQ_TYPE_LEVEL_HIGH) {
  6812. - pr_warn("PMC: type not supported (support only IRQ_TYPE_LEVEL_HIGH type)\n");
  6813. - return -EINVAL;
  6814. - }
  6815. -
  6816. - return 0;
  6817. -}
  6818. -
  6819. -static void pmc_irq_suspend(struct irq_data *d)
  6820. -{
  6821. - struct at91_pmc *pmc = irq_data_get_irq_chip_data(d);
  6822. -
  6823. - pmc->imr = pmc_read(pmc, AT91_PMC_IMR);
  6824. - pmc_write(pmc, AT91_PMC_IDR, pmc->imr);
  6825. -}
  6826. -
  6827. -static void pmc_irq_resume(struct irq_data *d)
  6828. -{
  6829. - struct at91_pmc *pmc = irq_data_get_irq_chip_data(d);
  6830. -
  6831. - pmc_write(pmc, AT91_PMC_IER, pmc->imr);
  6832. -}
  6833. -
  6834. -static struct irq_chip pmc_irq = {
  6835. - .name = "PMC",
  6836. - .irq_disable = pmc_irq_mask,
  6837. - .irq_mask = pmc_irq_mask,
  6838. - .irq_unmask = pmc_irq_unmask,
  6839. - .irq_set_type = pmc_irq_set_type,
  6840. - .irq_suspend = pmc_irq_suspend,
  6841. - .irq_resume = pmc_irq_resume,
  6842. -};
  6843. -
  6844. -static struct lock_class_key pmc_lock_class;
  6845. -
  6846. -static int pmc_irq_map(struct irq_domain *h, unsigned int virq,
  6847. - irq_hw_number_t hw)
  6848. -{
  6849. - struct at91_pmc *pmc = h->host_data;
  6850. -
  6851. - irq_set_lockdep_class(virq, &pmc_lock_class);
  6852. -
  6853. - irq_set_chip_and_handler(virq, &pmc_irq,
  6854. - handle_level_irq);
  6855. - irq_set_chip_data(virq, pmc);
  6856. -
  6857. - return 0;
  6858. -}
  6859. -
  6860. -static int pmc_irq_domain_xlate(struct irq_domain *d,
  6861. - struct device_node *ctrlr,
  6862. - const u32 *intspec, unsigned int intsize,
  6863. - irq_hw_number_t *out_hwirq,
  6864. - unsigned int *out_type)
  6865. -{
  6866. - struct at91_pmc *pmc = d->host_data;
  6867. - const struct at91_pmc_caps *caps = pmc->caps;
  6868. -
  6869. - if (WARN_ON(intsize < 1))
  6870. - return -EINVAL;
  6871. -
  6872. - *out_hwirq = intspec[0];
  6873. -
  6874. - if (!(caps->available_irqs & (1 << *out_hwirq)))
  6875. - return -EINVAL;
  6876. -
  6877. - *out_type = IRQ_TYPE_LEVEL_HIGH;
  6878. -
  6879. - return 0;
  6880. -}
  6881. -
  6882. -static const struct irq_domain_ops pmc_irq_ops = {
  6883. - .map = pmc_irq_map,
  6884. - .xlate = pmc_irq_domain_xlate,
  6885. -};
  6886. -
  6887. -static irqreturn_t pmc_irq_handler(int irq, void *data)
  6888. -{
  6889. - struct at91_pmc *pmc = (struct at91_pmc *)data;
  6890. - unsigned long sr;
  6891. - int n;
  6892. -
  6893. - sr = pmc_read(pmc, AT91_PMC_SR) & pmc_read(pmc, AT91_PMC_IMR);
  6894. - if (!sr)
  6895. - return IRQ_NONE;
  6896. -
  6897. - for_each_set_bit(n, &sr, BITS_PER_LONG)
  6898. - generic_handle_irq(irq_find_mapping(pmc->irqdomain, n));
  6899. -
  6900. - return IRQ_HANDLED;
  6901. -}
  6902. -
  6903. -static const struct at91_pmc_caps at91rm9200_caps = {
  6904. - .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_LOCKB |
  6905. - AT91_PMC_MCKRDY | AT91_PMC_PCK0RDY |
  6906. - AT91_PMC_PCK1RDY | AT91_PMC_PCK2RDY |
  6907. - AT91_PMC_PCK3RDY,
  6908. -};
  6909. -
  6910. -static const struct at91_pmc_caps at91sam9260_caps = {
  6911. - .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_LOCKB |
  6912. - AT91_PMC_MCKRDY | AT91_PMC_PCK0RDY |
  6913. - AT91_PMC_PCK1RDY,
  6914. -};
  6915. -
  6916. -static const struct at91_pmc_caps at91sam9g45_caps = {
  6917. - .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_MCKRDY |
  6918. - AT91_PMC_LOCKU | AT91_PMC_PCK0RDY |
  6919. - AT91_PMC_PCK1RDY,
  6920. -};
  6921. -
  6922. -static const struct at91_pmc_caps at91sam9n12_caps = {
  6923. - .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_LOCKB |
  6924. - AT91_PMC_MCKRDY | AT91_PMC_PCK0RDY |
  6925. - AT91_PMC_PCK1RDY | AT91_PMC_MOSCSELS |
  6926. - AT91_PMC_MOSCRCS | AT91_PMC_CFDEV,
  6927. -};
  6928. -
  6929. -static const struct at91_pmc_caps at91sam9x5_caps = {
  6930. - .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_MCKRDY |
  6931. - AT91_PMC_LOCKU | AT91_PMC_PCK0RDY |
  6932. - AT91_PMC_PCK1RDY | AT91_PMC_MOSCSELS |
  6933. - AT91_PMC_MOSCRCS | AT91_PMC_CFDEV,
  6934. -};
  6935. -
  6936. -static const struct at91_pmc_caps sama5d2_caps = {
  6937. - .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_MCKRDY |
  6938. - AT91_PMC_LOCKU | AT91_PMC_PCK0RDY |
  6939. - AT91_PMC_PCK1RDY | AT91_PMC_PCK2RDY |
  6940. - AT91_PMC_MOSCSELS | AT91_PMC_MOSCRCS |
  6941. - AT91_PMC_CFDEV | AT91_PMC_GCKRDY,
  6942. -};
  6943. -
  6944. -static const struct at91_pmc_caps sama5d3_caps = {
  6945. - .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_MCKRDY |
  6946. - AT91_PMC_LOCKU | AT91_PMC_PCK0RDY |
  6947. - AT91_PMC_PCK1RDY | AT91_PMC_PCK2RDY |
  6948. - AT91_PMC_MOSCSELS | AT91_PMC_MOSCRCS |
  6949. - AT91_PMC_CFDEV,
  6950. -};
  6951. -
  6952. -static struct at91_pmc *__init at91_pmc_init(struct device_node *np,
  6953. - void __iomem *regbase, int virq,
  6954. - const struct at91_pmc_caps *caps)
  6955. -{
  6956. - struct at91_pmc *pmc;
  6957. -
  6958. - if (!regbase || !virq || !caps)
  6959. - return NULL;
  6960. -
  6961. - at91_pmc_base = regbase;
  6962. -
  6963. - pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
  6964. - if (!pmc)
  6965. - return NULL;
  6966. -
  6967. - spin_lock_init(&pmc->lock);
  6968. - pmc->regbase = regbase;
  6969. - pmc->virq = virq;
  6970. - pmc->caps = caps;
  6971. -
  6972. - pmc->irqdomain = irq_domain_add_linear(np, 32, &pmc_irq_ops, pmc);
  6973. -
  6974. - if (!pmc->irqdomain)
  6975. - goto out_free_pmc;
  6976. -
  6977. - pmc_write(pmc, AT91_PMC_IDR, 0xffffffff);
  6978. - if (request_irq(pmc->virq, pmc_irq_handler,
  6979. - IRQF_SHARED | IRQF_COND_SUSPEND, "pmc", pmc))
  6980. - goto out_remove_irqdomain;
  6981. -
  6982. - return pmc;
  6983. -
  6984. -out_remove_irqdomain:
  6985. - irq_domain_remove(pmc->irqdomain);
  6986. -out_free_pmc:
  6987. - kfree(pmc);
  6988. -
  6989. - return NULL;
  6990. -}
  6991. -
  6992. -static const struct of_device_id pmc_clk_ids[] __initconst = {
  6993. - /* Slow oscillator */
  6994. - {
  6995. - .compatible = "atmel,at91sam9260-clk-slow",
  6996. - .data = of_at91sam9260_clk_slow_setup,
  6997. - },
  6998. - /* Main clock */
  6999. - {
  7000. - .compatible = "atmel,at91rm9200-clk-main-osc",
  7001. - .data = of_at91rm9200_clk_main_osc_setup,
  7002. - },
  7003. - {
  7004. - .compatible = "atmel,at91sam9x5-clk-main-rc-osc",
  7005. - .data = of_at91sam9x5_clk_main_rc_osc_setup,
  7006. - },
  7007. - {
  7008. - .compatible = "atmel,at91rm9200-clk-main",
  7009. - .data = of_at91rm9200_clk_main_setup,
  7010. - },
  7011. - {
  7012. - .compatible = "atmel,at91sam9x5-clk-main",
  7013. - .data = of_at91sam9x5_clk_main_setup,
  7014. - },
  7015. - /* PLL clocks */
  7016. - {
  7017. - .compatible = "atmel,at91rm9200-clk-pll",
  7018. - .data = of_at91rm9200_clk_pll_setup,
  7019. - },
  7020. - {
  7021. - .compatible = "atmel,at91sam9g45-clk-pll",
  7022. - .data = of_at91sam9g45_clk_pll_setup,
  7023. - },
  7024. - {
  7025. - .compatible = "atmel,at91sam9g20-clk-pllb",
  7026. - .data = of_at91sam9g20_clk_pllb_setup,
  7027. - },
  7028. - {
  7029. - .compatible = "atmel,sama5d3-clk-pll",
  7030. - .data = of_sama5d3_clk_pll_setup,
  7031. - },
  7032. - {
  7033. - .compatible = "atmel,at91sam9x5-clk-plldiv",
  7034. - .data = of_at91sam9x5_clk_plldiv_setup,
  7035. - },
  7036. - /* Master clock */
  7037. - {
  7038. - .compatible = "atmel,at91rm9200-clk-master",
  7039. - .data = of_at91rm9200_clk_master_setup,
  7040. - },
  7041. - {
  7042. - .compatible = "atmel,at91sam9x5-clk-master",
  7043. - .data = of_at91sam9x5_clk_master_setup,
  7044. - },
  7045. - /* System clocks */
  7046. - {
  7047. - .compatible = "atmel,at91rm9200-clk-system",
  7048. - .data = of_at91rm9200_clk_sys_setup,
  7049. - },
  7050. - /* Peripheral clocks */
  7051. - {
  7052. - .compatible = "atmel,at91rm9200-clk-peripheral",
  7053. - .data = of_at91rm9200_clk_periph_setup,
  7054. - },
  7055. - {
  7056. - .compatible = "atmel,at91sam9x5-clk-peripheral",
  7057. - .data = of_at91sam9x5_clk_periph_setup,
  7058. - },
  7059. - /* Programmable clocks */
  7060. - {
  7061. - .compatible = "atmel,at91rm9200-clk-programmable",
  7062. - .data = of_at91rm9200_clk_prog_setup,
  7063. - },
  7064. - {
  7065. - .compatible = "atmel,at91sam9g45-clk-programmable",
  7066. - .data = of_at91sam9g45_clk_prog_setup,
  7067. - },
  7068. - {
  7069. - .compatible = "atmel,at91sam9x5-clk-programmable",
  7070. - .data = of_at91sam9x5_clk_prog_setup,
  7071. - },
  7072. - /* UTMI clock */
  7073. -#if defined(CONFIG_HAVE_AT91_UTMI)
  7074. - {
  7075. - .compatible = "atmel,at91sam9x5-clk-utmi",
  7076. - .data = of_at91sam9x5_clk_utmi_setup,
  7077. - },
  7078. -#endif
  7079. - /* USB clock */
  7080. -#if defined(CONFIG_HAVE_AT91_USB_CLK)
  7081. - {
  7082. - .compatible = "atmel,at91rm9200-clk-usb",
  7083. - .data = of_at91rm9200_clk_usb_setup,
  7084. - },
  7085. - {
  7086. - .compatible = "atmel,at91sam9x5-clk-usb",
  7087. - .data = of_at91sam9x5_clk_usb_setup,
  7088. - },
  7089. - {
  7090. - .compatible = "atmel,at91sam9n12-clk-usb",
  7091. - .data = of_at91sam9n12_clk_usb_setup,
  7092. - },
  7093. -#endif
  7094. - /* SMD clock */
  7095. -#if defined(CONFIG_HAVE_AT91_SMD)
  7096. - {
  7097. - .compatible = "atmel,at91sam9x5-clk-smd",
  7098. - .data = of_at91sam9x5_clk_smd_setup,
  7099. - },
  7100. -#endif
  7101. -#if defined(CONFIG_HAVE_AT91_H32MX)
  7102. - {
  7103. - .compatible = "atmel,sama5d4-clk-h32mx",
  7104. - .data = of_sama5d4_clk_h32mx_setup,
  7105. - },
  7106. -#endif
  7107. -#if defined(CONFIG_HAVE_AT91_GENERATED_CLK)
  7108. - {
  7109. - .compatible = "atmel,sama5d2-clk-generated",
  7110. - .data = of_sama5d2_clk_generated_setup,
  7111. - },
  7112. -#endif
  7113. - { /*sentinel*/ }
  7114. -};
  7115. -
  7116. -static void __init of_at91_pmc_setup(struct device_node *np,
  7117. - const struct at91_pmc_caps *caps)
  7118. -{
  7119. - struct at91_pmc *pmc;
  7120. - struct device_node *childnp;
  7121. - void (*clk_setup)(struct device_node *, struct at91_pmc *);
  7122. - const struct of_device_id *clk_id;
  7123. - void __iomem *regbase = of_iomap(np, 0);
  7124. - int virq;
  7125. -
  7126. - if (!regbase)
  7127. - return;
  7128. -
  7129. - virq = irq_of_parse_and_map(np, 0);
  7130. - if (!virq)
  7131. - return;
  7132. -
  7133. - pmc = at91_pmc_init(np, regbase, virq, caps);
  7134. - if (!pmc)
  7135. - return;
  7136. - for_each_child_of_node(np, childnp) {
  7137. - clk_id = of_match_node(pmc_clk_ids, childnp);
  7138. - if (!clk_id)
  7139. - continue;
  7140. - clk_setup = clk_id->data;
  7141. - clk_setup(childnp, pmc);
  7142. - }
  7143. -}
  7144. -
  7145. -static void __init of_at91rm9200_pmc_setup(struct device_node *np)
  7146. -{
  7147. - of_at91_pmc_setup(np, &at91rm9200_caps);
  7148. -}
  7149. -CLK_OF_DECLARE(at91rm9200_clk_pmc, "atmel,at91rm9200-pmc",
  7150. - of_at91rm9200_pmc_setup);
  7151. -
  7152. -static void __init of_at91sam9260_pmc_setup(struct device_node *np)
  7153. -{
  7154. - of_at91_pmc_setup(np, &at91sam9260_caps);
  7155. -}
  7156. -CLK_OF_DECLARE(at91sam9260_clk_pmc, "atmel,at91sam9260-pmc",
  7157. - of_at91sam9260_pmc_setup);
  7158. -
  7159. -static void __init of_at91sam9g45_pmc_setup(struct device_node *np)
  7160. -{
  7161. - of_at91_pmc_setup(np, &at91sam9g45_caps);
  7162. -}
  7163. -CLK_OF_DECLARE(at91sam9g45_clk_pmc, "atmel,at91sam9g45-pmc",
  7164. - of_at91sam9g45_pmc_setup);
  7165. -
  7166. -static void __init of_at91sam9n12_pmc_setup(struct device_node *np)
  7167. -{
  7168. - of_at91_pmc_setup(np, &at91sam9n12_caps);
  7169. -}
  7170. -CLK_OF_DECLARE(at91sam9n12_clk_pmc, "atmel,at91sam9n12-pmc",
  7171. - of_at91sam9n12_pmc_setup);
  7172. -
  7173. -static void __init of_at91sam9x5_pmc_setup(struct device_node *np)
  7174. -{
  7175. - of_at91_pmc_setup(np, &at91sam9x5_caps);
  7176. -}
  7177. -CLK_OF_DECLARE(at91sam9x5_clk_pmc, "atmel,at91sam9x5-pmc",
  7178. - of_at91sam9x5_pmc_setup);
  7179. -
  7180. -static void __init of_sama5d2_pmc_setup(struct device_node *np)
  7181. -{
  7182. - of_at91_pmc_setup(np, &sama5d2_caps);
  7183. -}
  7184. -CLK_OF_DECLARE(sama5d2_clk_pmc, "atmel,sama5d2-pmc",
  7185. - of_sama5d2_pmc_setup);
  7186. -
  7187. -static void __init of_sama5d3_pmc_setup(struct device_node *np)
  7188. -{
  7189. - of_at91_pmc_setup(np, &sama5d3_caps);
  7190. -}
  7191. -CLK_OF_DECLARE(sama5d3_clk_pmc, "atmel,sama5d3-pmc",
  7192. - of_sama5d3_pmc_setup);
  7193. diff -Nur linux-4.4.46.orig/drivers/clk/at91/pmc.h linux-4.4.46/drivers/clk/at91/pmc.h
  7194. --- linux-4.4.46.orig/drivers/clk/at91/pmc.h 2017-02-01 08:31:11.000000000 +0100
  7195. +++ linux-4.4.46/drivers/clk/at91/pmc.h 2017-02-03 17:18:05.675416176 +0100
  7196. @@ -14,8 +14,11 @@
  7197. #include <linux/io.h>
  7198. #include <linux/irqdomain.h>
  7199. +#include <linux/regmap.h>
  7200. #include <linux/spinlock.h>
  7201. +extern spinlock_t pmc_pcr_lock;
  7202. +
  7203. struct clk_range {
  7204. unsigned long min;
  7205. unsigned long max;
  7206. @@ -23,102 +26,7 @@
  7207. #define CLK_RANGE(MIN, MAX) {.min = MIN, .max = MAX,}
  7208. -struct at91_pmc_caps {
  7209. - u32 available_irqs;
  7210. -};
  7211. -
  7212. -struct at91_pmc {
  7213. - void __iomem *regbase;
  7214. - int virq;
  7215. - spinlock_t lock;
  7216. - const struct at91_pmc_caps *caps;
  7217. - struct irq_domain *irqdomain;
  7218. - u32 imr;
  7219. -};
  7220. -
  7221. -static inline void pmc_lock(struct at91_pmc *pmc)
  7222. -{
  7223. - spin_lock(&pmc->lock);
  7224. -}
  7225. -
  7226. -static inline void pmc_unlock(struct at91_pmc *pmc)
  7227. -{
  7228. - spin_unlock(&pmc->lock);
  7229. -}
  7230. -
  7231. -static inline u32 pmc_read(struct at91_pmc *pmc, int offset)
  7232. -{
  7233. - return readl(pmc->regbase + offset);
  7234. -}
  7235. -
  7236. -static inline void pmc_write(struct at91_pmc *pmc, int offset, u32 value)
  7237. -{
  7238. - writel(value, pmc->regbase + offset);
  7239. -}
  7240. -
  7241. int of_at91_get_clk_range(struct device_node *np, const char *propname,
  7242. struct clk_range *range);
  7243. -void of_at91sam9260_clk_slow_setup(struct device_node *np,
  7244. - struct at91_pmc *pmc);
  7245. -
  7246. -void of_at91rm9200_clk_main_osc_setup(struct device_node *np,
  7247. - struct at91_pmc *pmc);
  7248. -void of_at91sam9x5_clk_main_rc_osc_setup(struct device_node *np,
  7249. - struct at91_pmc *pmc);
  7250. -void of_at91rm9200_clk_main_setup(struct device_node *np,
  7251. - struct at91_pmc *pmc);
  7252. -void of_at91sam9x5_clk_main_setup(struct device_node *np,
  7253. - struct at91_pmc *pmc);
  7254. -
  7255. -void of_at91rm9200_clk_pll_setup(struct device_node *np,
  7256. - struct at91_pmc *pmc);
  7257. -void of_at91sam9g45_clk_pll_setup(struct device_node *np,
  7258. - struct at91_pmc *pmc);
  7259. -void of_at91sam9g20_clk_pllb_setup(struct device_node *np,
  7260. - struct at91_pmc *pmc);
  7261. -void of_sama5d3_clk_pll_setup(struct device_node *np,
  7262. - struct at91_pmc *pmc);
  7263. -void of_at91sam9x5_clk_plldiv_setup(struct device_node *np,
  7264. - struct at91_pmc *pmc);
  7265. -
  7266. -void of_at91rm9200_clk_master_setup(struct device_node *np,
  7267. - struct at91_pmc *pmc);
  7268. -void of_at91sam9x5_clk_master_setup(struct device_node *np,
  7269. - struct at91_pmc *pmc);
  7270. -
  7271. -void of_at91rm9200_clk_sys_setup(struct device_node *np,
  7272. - struct at91_pmc *pmc);
  7273. -
  7274. -void of_at91rm9200_clk_periph_setup(struct device_node *np,
  7275. - struct at91_pmc *pmc);
  7276. -void of_at91sam9x5_clk_periph_setup(struct device_node *np,
  7277. - struct at91_pmc *pmc);
  7278. -
  7279. -void of_at91rm9200_clk_prog_setup(struct device_node *np,
  7280. - struct at91_pmc *pmc);
  7281. -void of_at91sam9g45_clk_prog_setup(struct device_node *np,
  7282. - struct at91_pmc *pmc);
  7283. -void of_at91sam9x5_clk_prog_setup(struct device_node *np,
  7284. - struct at91_pmc *pmc);
  7285. -
  7286. -void of_at91sam9x5_clk_utmi_setup(struct device_node *np,
  7287. - struct at91_pmc *pmc);
  7288. -
  7289. -void of_at91rm9200_clk_usb_setup(struct device_node *np,
  7290. - struct at91_pmc *pmc);
  7291. -void of_at91sam9x5_clk_usb_setup(struct device_node *np,
  7292. - struct at91_pmc *pmc);
  7293. -void of_at91sam9n12_clk_usb_setup(struct device_node *np,
  7294. - struct at91_pmc *pmc);
  7295. -
  7296. -void of_at91sam9x5_clk_smd_setup(struct device_node *np,
  7297. - struct at91_pmc *pmc);
  7298. -
  7299. -void of_sama5d4_clk_h32mx_setup(struct device_node *np,
  7300. - struct at91_pmc *pmc);
  7301. -
  7302. -void of_sama5d2_clk_generated_setup(struct device_node *np,
  7303. - struct at91_pmc *pmc);
  7304. -
  7305. #endif /* __PMC_H_ */
  7306. diff -Nur linux-4.4.46.orig/drivers/clocksource/tcb_clksrc.c linux-4.4.46/drivers/clocksource/tcb_clksrc.c
  7307. --- linux-4.4.46.orig/drivers/clocksource/tcb_clksrc.c 2017-02-01 08:31:11.000000000 +0100
  7308. +++ linux-4.4.46/drivers/clocksource/tcb_clksrc.c 2017-02-03 17:18:05.675416176 +0100
  7309. @@ -23,8 +23,7 @@
  7310. * this 32 bit free-running counter. the second channel is not used.
  7311. *
  7312. * - The third channel may be used to provide a 16-bit clockevent
  7313. - * source, used in either periodic or oneshot mode. This runs
  7314. - * at 32 KiHZ, and can handle delays of up to two seconds.
  7315. + * source, used in either periodic or oneshot mode.
  7316. *
  7317. * A boot clocksource and clockevent source are also currently needed,
  7318. * unless the relevant platforms (ARM/AT91, AVR32/AT32) are changed so
  7319. @@ -74,6 +73,8 @@
  7320. struct tc_clkevt_device {
  7321. struct clock_event_device clkevt;
  7322. struct clk *clk;
  7323. + bool clk_enabled;
  7324. + u32 freq;
  7325. void __iomem *regs;
  7326. };
  7327. @@ -82,15 +83,26 @@
  7328. return container_of(clkevt, struct tc_clkevt_device, clkevt);
  7329. }
  7330. -/* For now, we always use the 32K clock ... this optimizes for NO_HZ,
  7331. - * because using one of the divided clocks would usually mean the
  7332. - * tick rate can never be less than several dozen Hz (vs 0.5 Hz).
  7333. - *
  7334. - * A divided clock could be good for high resolution timers, since
  7335. - * 30.5 usec resolution can seem "low".
  7336. - */
  7337. static u32 timer_clock;
  7338. +static void tc_clk_disable(struct clock_event_device *d)
  7339. +{
  7340. + struct tc_clkevt_device *tcd = to_tc_clkevt(d);
  7341. +
  7342. + clk_disable(tcd->clk);
  7343. + tcd->clk_enabled = false;
  7344. +}
  7345. +
  7346. +static void tc_clk_enable(struct clock_event_device *d)
  7347. +{
  7348. + struct tc_clkevt_device *tcd = to_tc_clkevt(d);
  7349. +
  7350. + if (tcd->clk_enabled)
  7351. + return;
  7352. + clk_enable(tcd->clk);
  7353. + tcd->clk_enabled = true;
  7354. +}
  7355. +
  7356. static int tc_shutdown(struct clock_event_device *d)
  7357. {
  7358. struct tc_clkevt_device *tcd = to_tc_clkevt(d);
  7359. @@ -98,8 +110,14 @@
  7360. __raw_writel(0xff, regs + ATMEL_TC_REG(2, IDR));
  7361. __raw_writel(ATMEL_TC_CLKDIS, regs + ATMEL_TC_REG(2, CCR));
  7362. + return 0;
  7363. +}
  7364. +
  7365. +static int tc_shutdown_clk_off(struct clock_event_device *d)
  7366. +{
  7367. + tc_shutdown(d);
  7368. if (!clockevent_state_detached(d))
  7369. - clk_disable(tcd->clk);
  7370. + tc_clk_disable(d);
  7371. return 0;
  7372. }
  7373. @@ -112,9 +130,9 @@
  7374. if (clockevent_state_oneshot(d) || clockevent_state_periodic(d))
  7375. tc_shutdown(d);
  7376. - clk_enable(tcd->clk);
  7377. + tc_clk_enable(d);
  7378. - /* slow clock, count up to RC, then irq and stop */
  7379. + /* count up to RC, then irq and stop */
  7380. __raw_writel(timer_clock | ATMEL_TC_CPCSTOP | ATMEL_TC_WAVE |
  7381. ATMEL_TC_WAVESEL_UP_AUTO, regs + ATMEL_TC_REG(2, CMR));
  7382. __raw_writel(ATMEL_TC_CPCS, regs + ATMEL_TC_REG(2, IER));
  7383. @@ -134,12 +152,12 @@
  7384. /* By not making the gentime core emulate periodic mode on top
  7385. * of oneshot, we get lower overhead and improved accuracy.
  7386. */
  7387. - clk_enable(tcd->clk);
  7388. + tc_clk_enable(d);
  7389. - /* slow clock, count up to RC, then irq and restart */
  7390. + /* count up to RC, then irq and restart */
  7391. __raw_writel(timer_clock | ATMEL_TC_WAVE | ATMEL_TC_WAVESEL_UP_AUTO,
  7392. regs + ATMEL_TC_REG(2, CMR));
  7393. - __raw_writel((32768 + HZ / 2) / HZ, tcaddr + ATMEL_TC_REG(2, RC));
  7394. + __raw_writel((tcd->freq + HZ / 2) / HZ, tcaddr + ATMEL_TC_REG(2, RC));
  7395. /* Enable clock and interrupts on RC compare */
  7396. __raw_writel(ATMEL_TC_CPCS, regs + ATMEL_TC_REG(2, IER));
  7397. @@ -166,9 +184,13 @@
  7398. .features = CLOCK_EVT_FEAT_PERIODIC |
  7399. CLOCK_EVT_FEAT_ONESHOT,
  7400. /* Should be lower than at91rm9200's system timer */
  7401. +#ifdef CONFIG_ATMEL_TCB_CLKSRC_USE_SLOW_CLOCK
  7402. .rating = 125,
  7403. +#else
  7404. + .rating = 200,
  7405. +#endif
  7406. .set_next_event = tc_next_event,
  7407. - .set_state_shutdown = tc_shutdown,
  7408. + .set_state_shutdown = tc_shutdown_clk_off,
  7409. .set_state_periodic = tc_set_periodic,
  7410. .set_state_oneshot = tc_set_oneshot,
  7411. },
  7412. @@ -188,8 +210,9 @@
  7413. return IRQ_NONE;
  7414. }
  7415. -static int __init setup_clkevents(struct atmel_tc *tc, int clk32k_divisor_idx)
  7416. +static int __init setup_clkevents(struct atmel_tc *tc, int divisor_idx)
  7417. {
  7418. + unsigned divisor = atmel_tc_divisors[divisor_idx];
  7419. int ret;
  7420. struct clk *t2_clk = tc->clk[2];
  7421. int irq = tc->irq[2];
  7422. @@ -210,7 +233,11 @@
  7423. clkevt.regs = tc->regs;
  7424. clkevt.clk = t2_clk;
  7425. - timer_clock = clk32k_divisor_idx;
  7426. + timer_clock = divisor_idx;
  7427. + if (!divisor)
  7428. + clkevt.freq = 32768;
  7429. + else
  7430. + clkevt.freq = clk_get_rate(t2_clk) / divisor;
  7431. clkevt.clkevt.cpumask = cpumask_of(0);
  7432. @@ -221,7 +248,7 @@
  7433. return ret;
  7434. }
  7435. - clockevents_config_and_register(&clkevt.clkevt, 32768, 1, 0xffff);
  7436. + clockevents_config_and_register(&clkevt.clkevt, clkevt.freq, 1, 0xffff);
  7437. return ret;
  7438. }
  7439. @@ -358,7 +385,11 @@
  7440. goto err_disable_t1;
  7441. /* channel 2: periodic and oneshot timer support */
  7442. +#ifdef CONFIG_ATMEL_TCB_CLKSRC_USE_SLOW_CLOCK
  7443. ret = setup_clkevents(tc, clk32k_divisor_idx);
  7444. +#else
  7445. + ret = setup_clkevents(tc, best_divisor_idx);
  7446. +#endif
  7447. if (ret)
  7448. goto err_unregister_clksrc;
  7449. diff -Nur linux-4.4.46.orig/drivers/clocksource/timer-atmel-pit.c linux-4.4.46/drivers/clocksource/timer-atmel-pit.c
  7450. --- linux-4.4.46.orig/drivers/clocksource/timer-atmel-pit.c 2017-02-01 08:31:11.000000000 +0100
  7451. +++ linux-4.4.46/drivers/clocksource/timer-atmel-pit.c 2017-02-03 17:18:05.675416176 +0100
  7452. @@ -46,6 +46,7 @@
  7453. u32 cycle;
  7454. u32 cnt;
  7455. unsigned int irq;
  7456. + bool irq_requested;
  7457. struct clk *mck;
  7458. };
  7459. @@ -96,15 +97,29 @@
  7460. /* disable irq, leaving the clocksource active */
  7461. pit_write(data->base, AT91_PIT_MR, (data->cycle - 1) | AT91_PIT_PITEN);
  7462. + if (data->irq_requested) {
  7463. + free_irq(data->irq, data);
  7464. + data->irq_requested = false;
  7465. + }
  7466. return 0;
  7467. }
  7468. +static irqreturn_t at91sam926x_pit_interrupt(int irq, void *dev_id);
  7469. /*
  7470. * Clockevent device: interrupts every 1/HZ (== pit_cycles * MCK/16)
  7471. */
  7472. static int pit_clkevt_set_periodic(struct clock_event_device *dev)
  7473. {
  7474. struct pit_data *data = clkevt_to_pit_data(dev);
  7475. + int ret;
  7476. +
  7477. + ret = request_irq(data->irq, at91sam926x_pit_interrupt,
  7478. + IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
  7479. + "at91_tick", data);
  7480. + if (ret)
  7481. + panic(pr_fmt("Unable to setup IRQ\n"));
  7482. +
  7483. + data->irq_requested = true;
  7484. /* update clocksource counter */
  7485. data->cnt += data->cycle * PIT_PICNT(pit_read(data->base, AT91_PIT_PIVR));
  7486. @@ -181,7 +196,6 @@
  7487. {
  7488. unsigned long pit_rate;
  7489. unsigned bits;
  7490. - int ret;
  7491. /*
  7492. * Use our actual MCK to figure out how many MCK/16 ticks per
  7493. @@ -206,13 +220,6 @@
  7494. data->clksrc.flags = CLOCK_SOURCE_IS_CONTINUOUS;
  7495. clocksource_register_hz(&data->clksrc, pit_rate);
  7496. - /* Set up irq handler */
  7497. - ret = request_irq(data->irq, at91sam926x_pit_interrupt,
  7498. - IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
  7499. - "at91_tick", data);
  7500. - if (ret)
  7501. - panic(pr_fmt("Unable to setup IRQ\n"));
  7502. -
  7503. /* Set up and register clockevents */
  7504. data->clkevt.name = "pit";
  7505. data->clkevt.features = CLOCK_EVT_FEAT_PERIODIC;
  7506. diff -Nur linux-4.4.46.orig/drivers/clocksource/timer-atmel-st.c linux-4.4.46/drivers/clocksource/timer-atmel-st.c
  7507. --- linux-4.4.46.orig/drivers/clocksource/timer-atmel-st.c 2017-02-01 08:31:11.000000000 +0100
  7508. +++ linux-4.4.46/drivers/clocksource/timer-atmel-st.c 2017-02-03 17:18:05.675416176 +0100
  7509. @@ -115,18 +115,29 @@
  7510. last_crtr = read_CRTR();
  7511. }
  7512. +static int atmel_st_irq;
  7513. +
  7514. static int clkevt32k_shutdown(struct clock_event_device *evt)
  7515. {
  7516. clkdev32k_disable_and_flush_irq();
  7517. irqmask = 0;
  7518. regmap_write(regmap_st, AT91_ST_IER, irqmask);
  7519. + free_irq(atmel_st_irq, regmap_st);
  7520. return 0;
  7521. }
  7522. static int clkevt32k_set_oneshot(struct clock_event_device *dev)
  7523. {
  7524. + int ret;
  7525. +
  7526. clkdev32k_disable_and_flush_irq();
  7527. + ret = request_irq(atmel_st_irq, at91rm9200_timer_interrupt,
  7528. + IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
  7529. + "at91_tick", regmap_st);
  7530. + if (ret)
  7531. + panic(pr_fmt("Unable to setup IRQ\n"));
  7532. +
  7533. /*
  7534. * ALM for oneshot irqs, set by next_event()
  7535. * before 32 seconds have passed.
  7536. @@ -139,8 +150,16 @@
  7537. static int clkevt32k_set_periodic(struct clock_event_device *dev)
  7538. {
  7539. + int ret;
  7540. +
  7541. clkdev32k_disable_and_flush_irq();
  7542. + ret = request_irq(atmel_st_irq, at91rm9200_timer_interrupt,
  7543. + IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
  7544. + "at91_tick", regmap_st);
  7545. + if (ret)
  7546. + panic(pr_fmt("Unable to setup IRQ\n"));
  7547. +
  7548. /* PIT for periodic irqs; fixed rate of 1/HZ */
  7549. irqmask = AT91_ST_PITS;
  7550. regmap_write(regmap_st, AT91_ST_PIMR, timer_latch);
  7551. @@ -198,7 +217,7 @@
  7552. {
  7553. struct clk *sclk;
  7554. unsigned int sclk_rate, val;
  7555. - int irq, ret;
  7556. + int ret;
  7557. regmap_st = syscon_node_to_regmap(node);
  7558. if (IS_ERR(regmap_st))
  7559. @@ -210,17 +229,10 @@
  7560. regmap_read(regmap_st, AT91_ST_SR, &val);
  7561. /* Get the interrupts property */
  7562. - irq = irq_of_parse_and_map(node, 0);
  7563. - if (!irq)
  7564. + atmel_st_irq = irq_of_parse_and_map(node, 0);
  7565. + if (!atmel_st_irq)
  7566. panic(pr_fmt("Unable to get IRQ from DT\n"));
  7567. - /* Make IRQs happen for the system timer */
  7568. - ret = request_irq(irq, at91rm9200_timer_interrupt,
  7569. - IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
  7570. - "at91_tick", regmap_st);
  7571. - if (ret)
  7572. - panic(pr_fmt("Unable to setup IRQ\n"));
  7573. -
  7574. sclk = of_clk_get(node, 0);
  7575. if (IS_ERR(sclk))
  7576. panic(pr_fmt("Unable to get slow clock\n"));
  7577. diff -Nur linux-4.4.46.orig/drivers/cpufreq/Kconfig.x86 linux-4.4.46/drivers/cpufreq/Kconfig.x86
  7578. --- linux-4.4.46.orig/drivers/cpufreq/Kconfig.x86 2017-02-01 08:31:11.000000000 +0100
  7579. +++ linux-4.4.46/drivers/cpufreq/Kconfig.x86 2017-02-03 17:18:05.675416176 +0100
  7580. @@ -123,7 +123,7 @@
  7581. config X86_POWERNOW_K8
  7582. tristate "AMD Opteron/Athlon64 PowerNow!"
  7583. - depends on ACPI && ACPI_PROCESSOR && X86_ACPI_CPUFREQ
  7584. + depends on ACPI && ACPI_PROCESSOR && X86_ACPI_CPUFREQ && !PREEMPT_RT_BASE
  7585. help
  7586. This adds the CPUFreq driver for K8/early Opteron/Athlon64 processors.
  7587. Support for K10 and newer processors is now in acpi-cpufreq.
  7588. diff -Nur linux-4.4.46.orig/drivers/cpuidle/coupled.c linux-4.4.46/drivers/cpuidle/coupled.c
  7589. --- linux-4.4.46.orig/drivers/cpuidle/coupled.c 2017-02-01 08:31:11.000000000 +0100
  7590. +++ linux-4.4.46/drivers/cpuidle/coupled.c 2017-02-03 17:18:05.675416176 +0100
  7591. @@ -119,7 +119,6 @@
  7592. #define CPUIDLE_COUPLED_NOT_IDLE (-1)
  7593. -static DEFINE_MUTEX(cpuidle_coupled_lock);
  7594. static DEFINE_PER_CPU(struct call_single_data, cpuidle_coupled_poke_cb);
  7595. /*
  7596. diff -Nur linux-4.4.46.orig/drivers/gpu/drm/i915/i915_gem_execbuffer.c linux-4.4.46/drivers/gpu/drm/i915/i915_gem_execbuffer.c
  7597. --- linux-4.4.46.orig/drivers/gpu/drm/i915/i915_gem_execbuffer.c 2017-02-01 08:31:11.000000000 +0100
  7598. +++ linux-4.4.46/drivers/gpu/drm/i915/i915_gem_execbuffer.c 2017-02-03 17:18:05.675416176 +0100
  7599. @@ -1264,7 +1264,9 @@
  7600. if (ret)
  7601. return ret;
  7602. +#ifndef CONFIG_PREEMPT_RT_BASE
  7603. trace_i915_gem_ring_dispatch(params->request, params->dispatch_flags);
  7604. +#endif
  7605. i915_gem_execbuffer_move_to_active(vmas, params->request);
  7606. i915_gem_execbuffer_retire_commands(params);
  7607. diff -Nur linux-4.4.46.orig/drivers/gpu/drm/i915/i915_gem_shrinker.c linux-4.4.46/drivers/gpu/drm/i915/i915_gem_shrinker.c
  7608. --- linux-4.4.46.orig/drivers/gpu/drm/i915/i915_gem_shrinker.c 2017-02-01 08:31:11.000000000 +0100
  7609. +++ linux-4.4.46/drivers/gpu/drm/i915/i915_gem_shrinker.c 2017-02-03 17:18:05.675416176 +0100
  7610. @@ -39,7 +39,7 @@
  7611. if (!mutex_is_locked(mutex))
  7612. return false;
  7613. -#if defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_MUTEX_SPIN_ON_OWNER)
  7614. +#if (defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_MUTEX_SPIN_ON_OWNER)) && !defined(CONFIG_PREEMPT_RT_BASE)
  7615. return mutex->owner == task;
  7616. #else
  7617. /* Since UP may be pre-empted, we cannot assume that we own the lock */
  7618. diff -Nur linux-4.4.46.orig/drivers/gpu/drm/i915/i915_irq.c linux-4.4.46/drivers/gpu/drm/i915/i915_irq.c
  7619. --- linux-4.4.46.orig/drivers/gpu/drm/i915/i915_irq.c 2017-02-01 08:31:11.000000000 +0100
  7620. +++ linux-4.4.46/drivers/gpu/drm/i915/i915_irq.c 2017-02-03 17:18:05.675416176 +0100
  7621. @@ -812,6 +812,7 @@
  7622. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  7623. /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
  7624. + preempt_disable_rt();
  7625. /* Get optional system timestamp before query. */
  7626. if (stime)
  7627. @@ -863,6 +864,7 @@
  7628. *etime = ktime_get();
  7629. /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
  7630. + preempt_enable_rt();
  7631. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  7632. diff -Nur linux-4.4.46.orig/drivers/gpu/drm/i915/intel_display.c linux-4.4.46/drivers/gpu/drm/i915/intel_display.c
  7633. --- linux-4.4.46.orig/drivers/gpu/drm/i915/intel_display.c 2017-02-01 08:31:11.000000000 +0100
  7634. +++ linux-4.4.46/drivers/gpu/drm/i915/intel_display.c 2017-02-03 17:18:05.679416330 +0100
  7635. @@ -11400,7 +11400,7 @@
  7636. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7637. struct intel_unpin_work *work;
  7638. - WARN_ON(!in_interrupt());
  7639. + WARN_ON_NONRT(!in_interrupt());
  7640. if (crtc == NULL)
  7641. return;
  7642. diff -Nur linux-4.4.46.orig/drivers/gpu/drm/i915/intel_sprite.c linux-4.4.46/drivers/gpu/drm/i915/intel_sprite.c
  7643. --- linux-4.4.46.orig/drivers/gpu/drm/i915/intel_sprite.c 2017-02-01 08:31:11.000000000 +0100
  7644. +++ linux-4.4.46/drivers/gpu/drm/i915/intel_sprite.c 2017-02-03 17:18:05.679416330 +0100
  7645. @@ -38,6 +38,7 @@
  7646. #include "intel_drv.h"
  7647. #include <drm/i915_drm.h>
  7648. #include "i915_drv.h"
  7649. +#include <linux/locallock.h>
  7650. static bool
  7651. format_is_yuv(uint32_t format)
  7652. @@ -64,6 +65,8 @@
  7653. 1000 * adjusted_mode->crtc_htotal);
  7654. }
  7655. +static DEFINE_LOCAL_IRQ_LOCK(pipe_update_lock);
  7656. +
  7657. /**
  7658. * intel_pipe_update_start() - start update of a set of display registers
  7659. * @crtc: the crtc of which the registers are going to be updated
  7660. @@ -96,7 +99,7 @@
  7661. min = vblank_start - usecs_to_scanlines(adjusted_mode, 100);
  7662. max = vblank_start - 1;
  7663. - local_irq_disable();
  7664. + local_lock_irq(pipe_update_lock);
  7665. if (min <= 0 || max <= 0)
  7666. return;
  7667. @@ -126,11 +129,11 @@
  7668. break;
  7669. }
  7670. - local_irq_enable();
  7671. + local_unlock_irq(pipe_update_lock);
  7672. timeout = schedule_timeout(timeout);
  7673. - local_irq_disable();
  7674. + local_lock_irq(pipe_update_lock);
  7675. }
  7676. finish_wait(wq, &wait);
  7677. @@ -164,7 +167,7 @@
  7678. trace_i915_pipe_update_end(crtc, end_vbl_count, scanline_end);
  7679. - local_irq_enable();
  7680. + local_unlock_irq(pipe_update_lock);
  7681. if (crtc->debug.start_vbl_count &&
  7682. crtc->debug.start_vbl_count != end_vbl_count) {
  7683. diff -Nur linux-4.4.46.orig/drivers/gpu/drm/radeon/radeon_display.c linux-4.4.46/drivers/gpu/drm/radeon/radeon_display.c
  7684. --- linux-4.4.46.orig/drivers/gpu/drm/radeon/radeon_display.c 2017-02-01 08:31:11.000000000 +0100
  7685. +++ linux-4.4.46/drivers/gpu/drm/radeon/radeon_display.c 2017-02-03 17:18:05.679416330 +0100
  7686. @@ -1862,6 +1862,7 @@
  7687. struct radeon_device *rdev = dev->dev_private;
  7688. /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
  7689. + preempt_disable_rt();
  7690. /* Get optional system timestamp before query. */
  7691. if (stime)
  7692. @@ -1954,6 +1955,7 @@
  7693. *etime = ktime_get();
  7694. /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
  7695. + preempt_enable_rt();
  7696. /* Decode into vertical and horizontal scanout position. */
  7697. *vpos = position & 0x1fff;
  7698. diff -Nur linux-4.4.46.orig/drivers/hv/vmbus_drv.c linux-4.4.46/drivers/hv/vmbus_drv.c
  7699. --- linux-4.4.46.orig/drivers/hv/vmbus_drv.c 2017-02-01 08:31:11.000000000 +0100
  7700. +++ linux-4.4.46/drivers/hv/vmbus_drv.c 2017-02-03 17:18:05.679416330 +0100
  7701. @@ -820,7 +820,7 @@
  7702. tasklet_schedule(&msg_dpc);
  7703. }
  7704. - add_interrupt_randomness(HYPERVISOR_CALLBACK_VECTOR, 0);
  7705. + add_interrupt_randomness(HYPERVISOR_CALLBACK_VECTOR, 0, 0);
  7706. }
  7707. diff -Nur linux-4.4.46.orig/drivers/i2c/busses/i2c-omap.c linux-4.4.46/drivers/i2c/busses/i2c-omap.c
  7708. --- linux-4.4.46.orig/drivers/i2c/busses/i2c-omap.c 2017-02-01 08:31:11.000000000 +0100
  7709. +++ linux-4.4.46/drivers/i2c/busses/i2c-omap.c 2017-02-03 17:18:05.679416330 +0100
  7710. @@ -995,15 +995,12 @@
  7711. u16 mask;
  7712. u16 stat;
  7713. - spin_lock(&omap->lock);
  7714. - mask = omap_i2c_read_reg(omap, OMAP_I2C_IE_REG);
  7715. stat = omap_i2c_read_reg(omap, OMAP_I2C_STAT_REG);
  7716. + mask = omap_i2c_read_reg(omap, OMAP_I2C_IE_REG);
  7717. if (stat & mask)
  7718. ret = IRQ_WAKE_THREAD;
  7719. - spin_unlock(&omap->lock);
  7720. -
  7721. return ret;
  7722. }
  7723. diff -Nur linux-4.4.46.orig/drivers/ide/alim15x3.c linux-4.4.46/drivers/ide/alim15x3.c
  7724. --- linux-4.4.46.orig/drivers/ide/alim15x3.c 2017-02-01 08:31:11.000000000 +0100
  7725. +++ linux-4.4.46/drivers/ide/alim15x3.c 2017-02-03 17:18:05.679416330 +0100
  7726. @@ -234,7 +234,7 @@
  7727. isa_dev = pci_get_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533, NULL);
  7728. - local_irq_save(flags);
  7729. + local_irq_save_nort(flags);
  7730. if (m5229_revision < 0xC2) {
  7731. /*
  7732. @@ -325,7 +325,7 @@
  7733. }
  7734. pci_dev_put(north);
  7735. pci_dev_put(isa_dev);
  7736. - local_irq_restore(flags);
  7737. + local_irq_restore_nort(flags);
  7738. return 0;
  7739. }
  7740. diff -Nur linux-4.4.46.orig/drivers/ide/hpt366.c linux-4.4.46/drivers/ide/hpt366.c
  7741. --- linux-4.4.46.orig/drivers/ide/hpt366.c 2017-02-01 08:31:11.000000000 +0100
  7742. +++ linux-4.4.46/drivers/ide/hpt366.c 2017-02-03 17:18:05.679416330 +0100
  7743. @@ -1241,7 +1241,7 @@
  7744. dma_old = inb(base + 2);
  7745. - local_irq_save(flags);
  7746. + local_irq_save_nort(flags);
  7747. dma_new = dma_old;
  7748. pci_read_config_byte(dev, hwif->channel ? 0x4b : 0x43, &masterdma);
  7749. @@ -1252,7 +1252,7 @@
  7750. if (dma_new != dma_old)
  7751. outb(dma_new, base + 2);
  7752. - local_irq_restore(flags);
  7753. + local_irq_restore_nort(flags);
  7754. printk(KERN_INFO " %s: BM-DMA at 0x%04lx-0x%04lx\n",
  7755. hwif->name, base, base + 7);
  7756. diff -Nur linux-4.4.46.orig/drivers/ide/ide-io.c linux-4.4.46/drivers/ide/ide-io.c
  7757. --- linux-4.4.46.orig/drivers/ide/ide-io.c 2017-02-01 08:31:11.000000000 +0100
  7758. +++ linux-4.4.46/drivers/ide/ide-io.c 2017-02-03 17:18:05.679416330 +0100
  7759. @@ -659,7 +659,7 @@
  7760. /* disable_irq_nosync ?? */
  7761. disable_irq(hwif->irq);
  7762. /* local CPU only, as if we were handling an interrupt */
  7763. - local_irq_disable();
  7764. + local_irq_disable_nort();
  7765. if (hwif->polling) {
  7766. startstop = handler(drive);
  7767. } else if (drive_is_ready(drive)) {
  7768. diff -Nur linux-4.4.46.orig/drivers/ide/ide-iops.c linux-4.4.46/drivers/ide/ide-iops.c
  7769. --- linux-4.4.46.orig/drivers/ide/ide-iops.c 2017-02-01 08:31:11.000000000 +0100
  7770. +++ linux-4.4.46/drivers/ide/ide-iops.c 2017-02-03 17:18:05.679416330 +0100
  7771. @@ -129,12 +129,12 @@
  7772. if ((stat & ATA_BUSY) == 0)
  7773. break;
  7774. - local_irq_restore(flags);
  7775. + local_irq_restore_nort(flags);
  7776. *rstat = stat;
  7777. return -EBUSY;
  7778. }
  7779. }
  7780. - local_irq_restore(flags);
  7781. + local_irq_restore_nort(flags);
  7782. }
  7783. /*
  7784. * Allow status to settle, then read it again.
  7785. diff -Nur linux-4.4.46.orig/drivers/ide/ide-io-std.c linux-4.4.46/drivers/ide/ide-io-std.c
  7786. --- linux-4.4.46.orig/drivers/ide/ide-io-std.c 2017-02-01 08:31:11.000000000 +0100
  7787. +++ linux-4.4.46/drivers/ide/ide-io-std.c 2017-02-03 17:18:05.679416330 +0100
  7788. @@ -175,7 +175,7 @@
  7789. unsigned long uninitialized_var(flags);
  7790. if ((io_32bit & 2) && !mmio) {
  7791. - local_irq_save(flags);
  7792. + local_irq_save_nort(flags);
  7793. ata_vlb_sync(io_ports->nsect_addr);
  7794. }
  7795. @@ -186,7 +186,7 @@
  7796. insl(data_addr, buf, words);
  7797. if ((io_32bit & 2) && !mmio)
  7798. - local_irq_restore(flags);
  7799. + local_irq_restore_nort(flags);
  7800. if (((len + 1) & 3) < 2)
  7801. return;
  7802. @@ -219,7 +219,7 @@
  7803. unsigned long uninitialized_var(flags);
  7804. if ((io_32bit & 2) && !mmio) {
  7805. - local_irq_save(flags);
  7806. + local_irq_save_nort(flags);
  7807. ata_vlb_sync(io_ports->nsect_addr);
  7808. }
  7809. @@ -230,7 +230,7 @@
  7810. outsl(data_addr, buf, words);
  7811. if ((io_32bit & 2) && !mmio)
  7812. - local_irq_restore(flags);
  7813. + local_irq_restore_nort(flags);
  7814. if (((len + 1) & 3) < 2)
  7815. return;
  7816. diff -Nur linux-4.4.46.orig/drivers/ide/ide-probe.c linux-4.4.46/drivers/ide/ide-probe.c
  7817. --- linux-4.4.46.orig/drivers/ide/ide-probe.c 2017-02-01 08:31:11.000000000 +0100
  7818. +++ linux-4.4.46/drivers/ide/ide-probe.c 2017-02-03 17:18:05.679416330 +0100
  7819. @@ -196,10 +196,10 @@
  7820. int bswap = 1;
  7821. /* local CPU only; some systems need this */
  7822. - local_irq_save(flags);
  7823. + local_irq_save_nort(flags);
  7824. /* read 512 bytes of id info */
  7825. hwif->tp_ops->input_data(drive, NULL, id, SECTOR_SIZE);
  7826. - local_irq_restore(flags);
  7827. + local_irq_restore_nort(flags);
  7828. drive->dev_flags |= IDE_DFLAG_ID_READ;
  7829. #ifdef DEBUG
  7830. diff -Nur linux-4.4.46.orig/drivers/ide/ide-taskfile.c linux-4.4.46/drivers/ide/ide-taskfile.c
  7831. --- linux-4.4.46.orig/drivers/ide/ide-taskfile.c 2017-02-01 08:31:11.000000000 +0100
  7832. +++ linux-4.4.46/drivers/ide/ide-taskfile.c 2017-02-03 17:18:05.683416484 +0100
  7833. @@ -250,7 +250,7 @@
  7834. page_is_high = PageHighMem(page);
  7835. if (page_is_high)
  7836. - local_irq_save(flags);
  7837. + local_irq_save_nort(flags);
  7838. buf = kmap_atomic(page) + offset;
  7839. @@ -271,7 +271,7 @@
  7840. kunmap_atomic(buf);
  7841. if (page_is_high)
  7842. - local_irq_restore(flags);
  7843. + local_irq_restore_nort(flags);
  7844. len -= nr_bytes;
  7845. }
  7846. @@ -414,7 +414,7 @@
  7847. }
  7848. if ((drive->dev_flags & IDE_DFLAG_UNMASK) == 0)
  7849. - local_irq_disable();
  7850. + local_irq_disable_nort();
  7851. ide_set_handler(drive, &task_pio_intr, WAIT_WORSTCASE);
  7852. diff -Nur linux-4.4.46.orig/drivers/infiniband/ulp/ipoib/ipoib_multicast.c linux-4.4.46/drivers/infiniband/ulp/ipoib/ipoib_multicast.c
  7853. --- linux-4.4.46.orig/drivers/infiniband/ulp/ipoib/ipoib_multicast.c 2017-02-01 08:31:11.000000000 +0100
  7854. +++ linux-4.4.46/drivers/infiniband/ulp/ipoib/ipoib_multicast.c 2017-02-03 17:18:05.683416484 +0100
  7855. @@ -862,7 +862,7 @@
  7856. ipoib_dbg_mcast(priv, "restarting multicast task\n");
  7857. - local_irq_save(flags);
  7858. + local_irq_save_nort(flags);
  7859. netif_addr_lock(dev);
  7860. spin_lock(&priv->lock);
  7861. @@ -944,7 +944,7 @@
  7862. spin_unlock(&priv->lock);
  7863. netif_addr_unlock(dev);
  7864. - local_irq_restore(flags);
  7865. + local_irq_restore_nort(flags);
  7866. /*
  7867. * make sure the in-flight joins have finished before we attempt
  7868. diff -Nur linux-4.4.46.orig/drivers/input/gameport/gameport.c linux-4.4.46/drivers/input/gameport/gameport.c
  7869. --- linux-4.4.46.orig/drivers/input/gameport/gameport.c 2017-02-01 08:31:11.000000000 +0100
  7870. +++ linux-4.4.46/drivers/input/gameport/gameport.c 2017-02-03 17:18:05.683416484 +0100
  7871. @@ -91,13 +91,13 @@
  7872. tx = ~0;
  7873. for (i = 0; i < 50; i++) {
  7874. - local_irq_save(flags);
  7875. + local_irq_save_nort(flags);
  7876. t1 = ktime_get_ns();
  7877. for (t = 0; t < 50; t++)
  7878. gameport_read(gameport);
  7879. t2 = ktime_get_ns();
  7880. t3 = ktime_get_ns();
  7881. - local_irq_restore(flags);
  7882. + local_irq_restore_nort(flags);
  7883. udelay(i * 10);
  7884. t = (t2 - t1) - (t3 - t2);
  7885. if (t < tx)
  7886. @@ -124,12 +124,12 @@
  7887. tx = 1 << 30;
  7888. for(i = 0; i < 50; i++) {
  7889. - local_irq_save(flags);
  7890. + local_irq_save_nort(flags);
  7891. GET_TIME(t1);
  7892. for (t = 0; t < 50; t++) gameport_read(gameport);
  7893. GET_TIME(t2);
  7894. GET_TIME(t3);
  7895. - local_irq_restore(flags);
  7896. + local_irq_restore_nort(flags);
  7897. udelay(i * 10);
  7898. if ((t = DELTA(t2,t1) - DELTA(t3,t2)) < tx) tx = t;
  7899. }
  7900. @@ -148,11 +148,11 @@
  7901. tx = 1 << 30;
  7902. for(i = 0; i < 50; i++) {
  7903. - local_irq_save(flags);
  7904. + local_irq_save_nort(flags);
  7905. t1 = rdtsc();
  7906. for (t = 0; t < 50; t++) gameport_read(gameport);
  7907. t2 = rdtsc();
  7908. - local_irq_restore(flags);
  7909. + local_irq_restore_nort(flags);
  7910. udelay(i * 10);
  7911. if (t2 - t1 < tx) tx = t2 - t1;
  7912. }
  7913. diff -Nur linux-4.4.46.orig/drivers/iommu/amd_iommu.c linux-4.4.46/drivers/iommu/amd_iommu.c
  7914. --- linux-4.4.46.orig/drivers/iommu/amd_iommu.c 2017-02-01 08:31:11.000000000 +0100
  7915. +++ linux-4.4.46/drivers/iommu/amd_iommu.c 2017-02-03 17:18:05.683416484 +0100
  7916. @@ -2022,10 +2022,10 @@
  7917. int ret;
  7918. /*
  7919. - * Must be called with IRQs disabled. Warn here to detect early
  7920. - * when its not.
  7921. + * Must be called with IRQs disabled on a non RT kernel. Warn here to
  7922. + * detect early when its not.
  7923. */
  7924. - WARN_ON(!irqs_disabled());
  7925. + WARN_ON_NONRT(!irqs_disabled());
  7926. /* lock domain */
  7927. spin_lock(&domain->lock);
  7928. @@ -2188,10 +2188,10 @@
  7929. struct protection_domain *domain;
  7930. /*
  7931. - * Must be called with IRQs disabled. Warn here to detect early
  7932. - * when its not.
  7933. + * Must be called with IRQs disabled on a non RT kernel. Warn here to
  7934. + * detect early when its not.
  7935. */
  7936. - WARN_ON(!irqs_disabled());
  7937. + WARN_ON_NONRT(!irqs_disabled());
  7938. if (WARN_ON(!dev_data->domain))
  7939. return;
  7940. diff -Nur linux-4.4.46.orig/drivers/leds/trigger/Kconfig linux-4.4.46/drivers/leds/trigger/Kconfig
  7941. --- linux-4.4.46.orig/drivers/leds/trigger/Kconfig 2017-02-01 08:31:11.000000000 +0100
  7942. +++ linux-4.4.46/drivers/leds/trigger/Kconfig 2017-02-03 17:18:05.683416484 +0100
  7943. @@ -61,7 +61,7 @@
  7944. config LEDS_TRIGGER_CPU
  7945. bool "LED CPU Trigger"
  7946. - depends on LEDS_TRIGGERS
  7947. + depends on LEDS_TRIGGERS && !PREEMPT_RT_BASE
  7948. help
  7949. This allows LEDs to be controlled by active CPUs. This shows
  7950. the active CPUs across an array of LEDs so you can see which
  7951. diff -Nur linux-4.4.46.orig/drivers/md/bcache/Kconfig linux-4.4.46/drivers/md/bcache/Kconfig
  7952. --- linux-4.4.46.orig/drivers/md/bcache/Kconfig 2017-02-01 08:31:11.000000000 +0100
  7953. +++ linux-4.4.46/drivers/md/bcache/Kconfig 2017-02-03 17:18:05.683416484 +0100
  7954. @@ -1,6 +1,7 @@
  7955. config BCACHE
  7956. tristate "Block device as cache"
  7957. + depends on !PREEMPT_RT_FULL
  7958. ---help---
  7959. Allows a block device to be used as cache for other devices; uses
  7960. a btree for indexing and the layout is optimized for SSDs.
  7961. diff -Nur linux-4.4.46.orig/drivers/md/dm.c linux-4.4.46/drivers/md/dm.c
  7962. --- linux-4.4.46.orig/drivers/md/dm.c 2017-02-01 08:31:11.000000000 +0100
  7963. +++ linux-4.4.46/drivers/md/dm.c 2017-02-03 17:18:05.683416484 +0100
  7964. @@ -2127,7 +2127,7 @@
  7965. /* Establish tio->ti before queuing work (map_tio_request) */
  7966. tio->ti = ti;
  7967. queue_kthread_work(&md->kworker, &tio->work);
  7968. - BUG_ON(!irqs_disabled());
  7969. + BUG_ON_NONRT(!irqs_disabled());
  7970. }
  7971. goto out;
  7972. diff -Nur linux-4.4.46.orig/drivers/md/raid5.c linux-4.4.46/drivers/md/raid5.c
  7973. --- linux-4.4.46.orig/drivers/md/raid5.c 2017-02-01 08:31:11.000000000 +0100
  7974. +++ linux-4.4.46/drivers/md/raid5.c 2017-02-03 17:18:05.683416484 +0100
  7975. @@ -1920,8 +1920,9 @@
  7976. struct raid5_percpu *percpu;
  7977. unsigned long cpu;
  7978. - cpu = get_cpu();
  7979. + cpu = get_cpu_light();
  7980. percpu = per_cpu_ptr(conf->percpu, cpu);
  7981. + spin_lock(&percpu->lock);
  7982. if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
  7983. ops_run_biofill(sh);
  7984. overlap_clear++;
  7985. @@ -1977,7 +1978,8 @@
  7986. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  7987. wake_up(&sh->raid_conf->wait_for_overlap);
  7988. }
  7989. - put_cpu();
  7990. + spin_unlock(&percpu->lock);
  7991. + put_cpu_light();
  7992. }
  7993. static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp)
  7994. @@ -6414,6 +6416,7 @@
  7995. __func__, cpu);
  7996. break;
  7997. }
  7998. + spin_lock_init(&per_cpu_ptr(conf->percpu, cpu)->lock);
  7999. }
  8000. put_online_cpus();
  8001. diff -Nur linux-4.4.46.orig/drivers/md/raid5.h linux-4.4.46/drivers/md/raid5.h
  8002. --- linux-4.4.46.orig/drivers/md/raid5.h 2017-02-01 08:31:11.000000000 +0100
  8003. +++ linux-4.4.46/drivers/md/raid5.h 2017-02-03 17:18:05.683416484 +0100
  8004. @@ -504,6 +504,7 @@
  8005. int recovery_disabled;
  8006. /* per cpu variables */
  8007. struct raid5_percpu {
  8008. + spinlock_t lock; /* Protection for -RT */
  8009. struct page *spare_page; /* Used when checking P/Q in raid6 */
  8010. struct flex_array *scribble; /* space for constructing buffer
  8011. * lists and performing address
  8012. diff -Nur linux-4.4.46.orig/drivers/media/platform/vsp1/vsp1_video.c linux-4.4.46/drivers/media/platform/vsp1/vsp1_video.c
  8013. --- linux-4.4.46.orig/drivers/media/platform/vsp1/vsp1_video.c 2017-02-01 08:31:11.000000000 +0100
  8014. +++ linux-4.4.46/drivers/media/platform/vsp1/vsp1_video.c 2017-02-03 17:18:05.683416484 +0100
  8015. @@ -520,7 +520,7 @@
  8016. bool stopped;
  8017. spin_lock_irqsave(&pipe->irqlock, flags);
  8018. - stopped = pipe->state == VSP1_PIPELINE_STOPPED,
  8019. + stopped = pipe->state == VSP1_PIPELINE_STOPPED;
  8020. spin_unlock_irqrestore(&pipe->irqlock, flags);
  8021. return stopped;
  8022. diff -Nur linux-4.4.46.orig/drivers/misc/hwlat_detector.c linux-4.4.46/drivers/misc/hwlat_detector.c
  8023. --- linux-4.4.46.orig/drivers/misc/hwlat_detector.c 1970-01-01 01:00:00.000000000 +0100
  8024. +++ linux-4.4.46/drivers/misc/hwlat_detector.c 2017-02-03 17:18:05.687416638 +0100
  8025. @@ -0,0 +1,1240 @@
  8026. +/*
  8027. + * hwlat_detector.c - A simple Hardware Latency detector.
  8028. + *
  8029. + * Use this module to detect large system latencies induced by the behavior of
  8030. + * certain underlying system hardware or firmware, independent of Linux itself.
  8031. + * The code was developed originally to detect the presence of SMIs on Intel
  8032. + * and AMD systems, although there is no dependency upon x86 herein.
  8033. + *
  8034. + * The classical example usage of this module is in detecting the presence of
  8035. + * SMIs or System Management Interrupts on Intel and AMD systems. An SMI is a
  8036. + * somewhat special form of hardware interrupt spawned from earlier CPU debug
  8037. + * modes in which the (BIOS/EFI/etc.) firmware arranges for the South Bridge
  8038. + * LPC (or other device) to generate a special interrupt under certain
  8039. + * circumstances, for example, upon expiration of a special SMI timer device,
  8040. + * due to certain external thermal readings, on certain I/O address accesses,
  8041. + * and other situations. An SMI hits a special CPU pin, triggers a special
  8042. + * SMI mode (complete with special memory map), and the OS is unaware.
  8043. + *
  8044. + * Although certain hardware-inducing latencies are necessary (for example,
  8045. + * a modern system often requires an SMI handler for correct thermal control
  8046. + * and remote management) they can wreak havoc upon any OS-level performance
  8047. + * guarantees toward low-latency, especially when the OS is not even made
  8048. + * aware of the presence of these interrupts. For this reason, we need a
  8049. + * somewhat brute force mechanism to detect these interrupts. In this case,
  8050. + * we do it by hogging all of the CPU(s) for configurable timer intervals,
  8051. + * sampling the built-in CPU timer, looking for discontiguous readings.
  8052. + *
  8053. + * WARNING: This implementation necessarily introduces latencies. Therefore,
  8054. + * you should NEVER use this module in a production environment
  8055. + * requiring any kind of low-latency performance guarantee(s).
  8056. + *
  8057. + * Copyright (C) 2008-2009 Jon Masters, Red Hat, Inc. <jcm@redhat.com>
  8058. + *
  8059. + * Includes useful feedback from Clark Williams <clark@redhat.com>
  8060. + *
  8061. + * This file is licensed under the terms of the GNU General Public
  8062. + * License version 2. This program is licensed "as is" without any
  8063. + * warranty of any kind, whether express or implied.
  8064. + */
  8065. +
  8066. +#include <linux/module.h>
  8067. +#include <linux/init.h>
  8068. +#include <linux/ring_buffer.h>
  8069. +#include <linux/time.h>
  8070. +#include <linux/hrtimer.h>
  8071. +#include <linux/kthread.h>
  8072. +#include <linux/debugfs.h>
  8073. +#include <linux/seq_file.h>
  8074. +#include <linux/uaccess.h>
  8075. +#include <linux/version.h>
  8076. +#include <linux/delay.h>
  8077. +#include <linux/slab.h>
  8078. +#include <linux/trace_clock.h>
  8079. +
  8080. +#define BUF_SIZE_DEFAULT 262144UL /* 8K*(sizeof(entry)) */
  8081. +#define BUF_FLAGS (RB_FL_OVERWRITE) /* no block on full */
  8082. +#define U64STR_SIZE 22 /* 20 digits max */
  8083. +
  8084. +#define VERSION "1.0.0"
  8085. +#define BANNER "hwlat_detector: "
  8086. +#define DRVNAME "hwlat_detector"
  8087. +#define DEFAULT_SAMPLE_WINDOW 1000000 /* 1s */
  8088. +#define DEFAULT_SAMPLE_WIDTH 500000 /* 0.5s */
  8089. +#define DEFAULT_LAT_THRESHOLD 10 /* 10us */
  8090. +
  8091. +/* Module metadata */
  8092. +
  8093. +MODULE_LICENSE("GPL");
  8094. +MODULE_AUTHOR("Jon Masters <jcm@redhat.com>");
  8095. +MODULE_DESCRIPTION("A simple hardware latency detector");
  8096. +MODULE_VERSION(VERSION);
  8097. +
  8098. +/* Module parameters */
  8099. +
  8100. +static int debug;
  8101. +static int enabled;
  8102. +static int threshold;
  8103. +
  8104. +module_param(debug, int, 0); /* enable debug */
  8105. +module_param(enabled, int, 0); /* enable detector */
  8106. +module_param(threshold, int, 0); /* latency threshold */
  8107. +
  8108. +/* Buffering and sampling */
  8109. +
  8110. +static struct ring_buffer *ring_buffer; /* sample buffer */
  8111. +static DEFINE_MUTEX(ring_buffer_mutex); /* lock changes */
  8112. +static unsigned long buf_size = BUF_SIZE_DEFAULT;
  8113. +static struct task_struct *kthread; /* sampling thread */
  8114. +
  8115. +/* DebugFS filesystem entries */
  8116. +
  8117. +static struct dentry *debug_dir; /* debugfs directory */
  8118. +static struct dentry *debug_max; /* maximum TSC delta */
  8119. +static struct dentry *debug_count; /* total detect count */
  8120. +static struct dentry *debug_sample_width; /* sample width us */
  8121. +static struct dentry *debug_sample_window; /* sample window us */
  8122. +static struct dentry *debug_sample; /* raw samples us */
  8123. +static struct dentry *debug_threshold; /* threshold us */
  8124. +static struct dentry *debug_enable; /* enable/disable */
  8125. +
  8126. +/* Individual samples and global state */
  8127. +
  8128. +struct sample; /* latency sample */
  8129. +struct data; /* Global state */
  8130. +
  8131. +/* Sampling functions */
  8132. +static int __buffer_add_sample(struct sample *sample);
  8133. +static struct sample *buffer_get_sample(struct sample *sample);
  8134. +
  8135. +/* Threading and state */
  8136. +static int kthread_fn(void *unused);
  8137. +static int start_kthread(void);
  8138. +static int stop_kthread(void);
  8139. +static void __reset_stats(void);
  8140. +static int init_stats(void);
  8141. +
  8142. +/* Debugfs interface */
  8143. +static ssize_t simple_data_read(struct file *filp, char __user *ubuf,
  8144. + size_t cnt, loff_t *ppos, const u64 *entry);
  8145. +static ssize_t simple_data_write(struct file *filp, const char __user *ubuf,
  8146. + size_t cnt, loff_t *ppos, u64 *entry);
  8147. +static int debug_sample_fopen(struct inode *inode, struct file *filp);
  8148. +static ssize_t debug_sample_fread(struct file *filp, char __user *ubuf,
  8149. + size_t cnt, loff_t *ppos);
  8150. +static int debug_sample_release(struct inode *inode, struct file *filp);
  8151. +static int debug_enable_fopen(struct inode *inode, struct file *filp);
  8152. +static ssize_t debug_enable_fread(struct file *filp, char __user *ubuf,
  8153. + size_t cnt, loff_t *ppos);
  8154. +static ssize_t debug_enable_fwrite(struct file *file,
  8155. + const char __user *user_buffer,
  8156. + size_t user_size, loff_t *offset);
  8157. +
  8158. +/* Initialization functions */
  8159. +static int init_debugfs(void);
  8160. +static void free_debugfs(void);
  8161. +static int detector_init(void);
  8162. +static void detector_exit(void);
  8163. +
  8164. +/* Individual latency samples are stored here when detected and packed into
  8165. + * the ring_buffer circular buffer, where they are overwritten when
  8166. + * more than buf_size/sizeof(sample) samples are received. */
  8167. +struct sample {
  8168. + u64 seqnum; /* unique sequence */
  8169. + u64 duration; /* ktime delta */
  8170. + u64 outer_duration; /* ktime delta (outer loop) */
  8171. + struct timespec timestamp; /* wall time */
  8172. + unsigned long lost;
  8173. +};
  8174. +
  8175. +/* keep the global state somewhere. */
  8176. +static struct data {
  8177. +
  8178. + struct mutex lock; /* protect changes */
  8179. +
  8180. + u64 count; /* total since reset */
  8181. + u64 max_sample; /* max hardware latency */
  8182. + u64 threshold; /* sample threshold level */
  8183. +
  8184. + u64 sample_window; /* total sampling window (on+off) */
  8185. + u64 sample_width; /* active sampling portion of window */
  8186. +
  8187. + atomic_t sample_open; /* whether the sample file is open */
  8188. +
  8189. + wait_queue_head_t wq; /* waitqeue for new sample values */
  8190. +
  8191. +} data;
  8192. +
  8193. +/**
  8194. + * __buffer_add_sample - add a new latency sample recording to the ring buffer
  8195. + * @sample: The new latency sample value
  8196. + *
  8197. + * This receives a new latency sample and records it in a global ring buffer.
  8198. + * No additional locking is used in this case.
  8199. + */
  8200. +static int __buffer_add_sample(struct sample *sample)
  8201. +{
  8202. + return ring_buffer_write(ring_buffer,
  8203. + sizeof(struct sample), sample);
  8204. +}
  8205. +
  8206. +/**
  8207. + * buffer_get_sample - remove a hardware latency sample from the ring buffer
  8208. + * @sample: Pre-allocated storage for the sample
  8209. + *
  8210. + * This retrieves a hardware latency sample from the global circular buffer
  8211. + */
  8212. +static struct sample *buffer_get_sample(struct sample *sample)
  8213. +{
  8214. + struct ring_buffer_event *e = NULL;
  8215. + struct sample *s = NULL;
  8216. + unsigned int cpu = 0;
  8217. +
  8218. + if (!sample)
  8219. + return NULL;
  8220. +
  8221. + mutex_lock(&ring_buffer_mutex);
  8222. + for_each_online_cpu(cpu) {
  8223. + e = ring_buffer_consume(ring_buffer, cpu, NULL, &sample->lost);
  8224. + if (e)
  8225. + break;
  8226. + }
  8227. +
  8228. + if (e) {
  8229. + s = ring_buffer_event_data(e);
  8230. + memcpy(sample, s, sizeof(struct sample));
  8231. + } else
  8232. + sample = NULL;
  8233. + mutex_unlock(&ring_buffer_mutex);
  8234. +
  8235. + return sample;
  8236. +}
  8237. +
  8238. +#ifndef CONFIG_TRACING
  8239. +#define time_type ktime_t
  8240. +#define time_get() ktime_get()
  8241. +#define time_to_us(x) ktime_to_us(x)
  8242. +#define time_sub(a, b) ktime_sub(a, b)
  8243. +#define init_time(a, b) (a).tv64 = b
  8244. +#define time_u64(a) ((a).tv64)
  8245. +#else
  8246. +#define time_type u64
  8247. +#define time_get() trace_clock_local()
  8248. +#define time_to_us(x) div_u64(x, 1000)
  8249. +#define time_sub(a, b) ((a) - (b))
  8250. +#define init_time(a, b) (a = b)
  8251. +#define time_u64(a) a
  8252. +#endif
  8253. +/**
  8254. + * get_sample - sample the CPU TSC and look for likely hardware latencies
  8255. + *
  8256. + * Used to repeatedly capture the CPU TSC (or similar), looking for potential
  8257. + * hardware-induced latency. Called with interrupts disabled and with
  8258. + * data.lock held.
  8259. + */
  8260. +static int get_sample(void)
  8261. +{
  8262. + time_type start, t1, t2, last_t2;
  8263. + s64 diff, total = 0;
  8264. + u64 sample = 0;
  8265. + u64 outer_sample = 0;
  8266. + int ret = -1;
  8267. +
  8268. + init_time(last_t2, 0);
  8269. + start = time_get(); /* start timestamp */
  8270. +
  8271. + do {
  8272. +
  8273. + t1 = time_get(); /* we'll look for a discontinuity */
  8274. + t2 = time_get();
  8275. +
  8276. + if (time_u64(last_t2)) {
  8277. + /* Check the delta from outer loop (t2 to next t1) */
  8278. + diff = time_to_us(time_sub(t1, last_t2));
  8279. + /* This shouldn't happen */
  8280. + if (diff < 0) {
  8281. + pr_err(BANNER "time running backwards\n");
  8282. + goto out;
  8283. + }
  8284. + if (diff > outer_sample)
  8285. + outer_sample = diff;
  8286. + }
  8287. + last_t2 = t2;
  8288. +
  8289. + total = time_to_us(time_sub(t2, start)); /* sample width */
  8290. +
  8291. + /* This checks the inner loop (t1 to t2) */
  8292. + diff = time_to_us(time_sub(t2, t1)); /* current diff */
  8293. +
  8294. + /* This shouldn't happen */
  8295. + if (diff < 0) {
  8296. + pr_err(BANNER "time running backwards\n");
  8297. + goto out;
  8298. + }
  8299. +
  8300. + if (diff > sample)
  8301. + sample = diff; /* only want highest value */
  8302. +
  8303. + } while (total <= data.sample_width);
  8304. +
  8305. + ret = 0;
  8306. +
  8307. + /* If we exceed the threshold value, we have found a hardware latency */
  8308. + if (sample > data.threshold || outer_sample > data.threshold) {
  8309. + struct sample s;
  8310. +
  8311. + ret = 1;
  8312. +
  8313. + data.count++;
  8314. + s.seqnum = data.count;
  8315. + s.duration = sample;
  8316. + s.outer_duration = outer_sample;
  8317. + s.timestamp = CURRENT_TIME;
  8318. + __buffer_add_sample(&s);
  8319. +
  8320. + /* Keep a running maximum ever recorded hardware latency */
  8321. + if (sample > data.max_sample)
  8322. + data.max_sample = sample;
  8323. + }
  8324. +
  8325. +out:
  8326. + return ret;
  8327. +}
  8328. +
  8329. +/*
  8330. + * kthread_fn - The CPU time sampling/hardware latency detection kernel thread
  8331. + * @unused: A required part of the kthread API.
  8332. + *
  8333. + * Used to periodically sample the CPU TSC via a call to get_sample. We
  8334. + * disable interrupts, which does (intentionally) introduce latency since we
  8335. + * need to ensure nothing else might be running (and thus pre-empting).
  8336. + * Obviously this should never be used in production environments.
  8337. + *
  8338. + * Currently this runs on which ever CPU it was scheduled on, but most
  8339. + * real-worald hardware latency situations occur across several CPUs,
  8340. + * but we might later generalize this if we find there are any actualy
  8341. + * systems with alternate SMI delivery or other hardware latencies.
  8342. + */
  8343. +static int kthread_fn(void *unused)
  8344. +{
  8345. + int ret;
  8346. + u64 interval;
  8347. +
  8348. + while (!kthread_should_stop()) {
  8349. +
  8350. + mutex_lock(&data.lock);
  8351. +
  8352. + local_irq_disable();
  8353. + ret = get_sample();
  8354. + local_irq_enable();
  8355. +
  8356. + if (ret > 0)
  8357. + wake_up(&data.wq); /* wake up reader(s) */
  8358. +
  8359. + interval = data.sample_window - data.sample_width;
  8360. + do_div(interval, USEC_PER_MSEC); /* modifies interval value */
  8361. +
  8362. + mutex_unlock(&data.lock);
  8363. +
  8364. + if (msleep_interruptible(interval))
  8365. + break;
  8366. + }
  8367. +
  8368. + return 0;
  8369. +}
  8370. +
  8371. +/**
  8372. + * start_kthread - Kick off the hardware latency sampling/detector kthread
  8373. + *
  8374. + * This starts a kernel thread that will sit and sample the CPU timestamp
  8375. + * counter (TSC or similar) and look for potential hardware latencies.
  8376. + */
  8377. +static int start_kthread(void)
  8378. +{
  8379. + kthread = kthread_run(kthread_fn, NULL,
  8380. + DRVNAME);
  8381. + if (IS_ERR(kthread)) {
  8382. + pr_err(BANNER "could not start sampling thread\n");
  8383. + enabled = 0;
  8384. + return -ENOMEM;
  8385. + }
  8386. +
  8387. + return 0;
  8388. +}
  8389. +
  8390. +/**
  8391. + * stop_kthread - Inform the hardware latency samping/detector kthread to stop
  8392. + *
  8393. + * This kicks the running hardware latency sampling/detector kernel thread and
  8394. + * tells it to stop sampling now. Use this on unload and at system shutdown.
  8395. + */
  8396. +static int stop_kthread(void)
  8397. +{
  8398. + int ret;
  8399. +
  8400. + ret = kthread_stop(kthread);
  8401. +
  8402. + return ret;
  8403. +}
  8404. +
  8405. +/**
  8406. + * __reset_stats - Reset statistics for the hardware latency detector
  8407. + *
  8408. + * We use data to store various statistics and global state. We call this
  8409. + * function in order to reset those when "enable" is toggled on or off, and
  8410. + * also at initialization. Should be called with data.lock held.
  8411. + */
  8412. +static void __reset_stats(void)
  8413. +{
  8414. + data.count = 0;
  8415. + data.max_sample = 0;
  8416. + ring_buffer_reset(ring_buffer); /* flush out old sample entries */
  8417. +}
  8418. +
  8419. +/**
  8420. + * init_stats - Setup global state statistics for the hardware latency detector
  8421. + *
  8422. + * We use data to store various statistics and global state. We also use
  8423. + * a global ring buffer (ring_buffer) to keep raw samples of detected hardware
  8424. + * induced system latencies. This function initializes these structures and
  8425. + * allocates the global ring buffer also.
  8426. + */
  8427. +static int init_stats(void)
  8428. +{
  8429. + int ret = -ENOMEM;
  8430. +
  8431. + mutex_init(&data.lock);
  8432. + init_waitqueue_head(&data.wq);
  8433. + atomic_set(&data.sample_open, 0);
  8434. +
  8435. + ring_buffer = ring_buffer_alloc(buf_size, BUF_FLAGS);
  8436. +
  8437. + if (WARN(!ring_buffer, KERN_ERR BANNER
  8438. + "failed to allocate ring buffer!\n"))
  8439. + goto out;
  8440. +
  8441. + __reset_stats();
  8442. + data.threshold = threshold ?: DEFAULT_LAT_THRESHOLD; /* threshold us */
  8443. + data.sample_window = DEFAULT_SAMPLE_WINDOW; /* window us */
  8444. + data.sample_width = DEFAULT_SAMPLE_WIDTH; /* width us */
  8445. +
  8446. + ret = 0;
  8447. +
  8448. +out:
  8449. + return ret;
  8450. +
  8451. +}
  8452. +
  8453. +/*
  8454. + * simple_data_read - Wrapper read function for global state debugfs entries
  8455. + * @filp: The active open file structure for the debugfs "file"
  8456. + * @ubuf: The userspace provided buffer to read value into
  8457. + * @cnt: The maximum number of bytes to read
  8458. + * @ppos: The current "file" position
  8459. + * @entry: The entry to read from
  8460. + *
  8461. + * This function provides a generic read implementation for the global state
  8462. + * "data" structure debugfs filesystem entries. It would be nice to use
  8463. + * simple_attr_read directly, but we need to make sure that the data.lock
  8464. + * is held during the actual read.
  8465. + */
  8466. +static ssize_t simple_data_read(struct file *filp, char __user *ubuf,
  8467. + size_t cnt, loff_t *ppos, const u64 *entry)
  8468. +{
  8469. + char buf[U64STR_SIZE];
  8470. + u64 val = 0;
  8471. + int len = 0;
  8472. +
  8473. + memset(buf, 0, sizeof(buf));
  8474. +
  8475. + if (!entry)
  8476. + return -EFAULT;
  8477. +
  8478. + mutex_lock(&data.lock);
  8479. + val = *entry;
  8480. + mutex_unlock(&data.lock);
  8481. +
  8482. + len = snprintf(buf, sizeof(buf), "%llu\n", (unsigned long long)val);
  8483. +
  8484. + return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
  8485. +
  8486. +}
  8487. +
  8488. +/*
  8489. + * simple_data_write - Wrapper write function for global state debugfs entries
  8490. + * @filp: The active open file structure for the debugfs "file"
  8491. + * @ubuf: The userspace provided buffer to write value from
  8492. + * @cnt: The maximum number of bytes to write
  8493. + * @ppos: The current "file" position
  8494. + * @entry: The entry to write to
  8495. + *
  8496. + * This function provides a generic write implementation for the global state
  8497. + * "data" structure debugfs filesystem entries. It would be nice to use
  8498. + * simple_attr_write directly, but we need to make sure that the data.lock
  8499. + * is held during the actual write.
  8500. + */
  8501. +static ssize_t simple_data_write(struct file *filp, const char __user *ubuf,
  8502. + size_t cnt, loff_t *ppos, u64 *entry)
  8503. +{
  8504. + char buf[U64STR_SIZE];
  8505. + int csize = min(cnt, sizeof(buf));
  8506. + u64 val = 0;
  8507. + int err = 0;
  8508. +
  8509. + memset(buf, '\0', sizeof(buf));
  8510. + if (copy_from_user(buf, ubuf, csize))
  8511. + return -EFAULT;
  8512. +
  8513. + buf[U64STR_SIZE-1] = '\0'; /* just in case */
  8514. + err = kstrtoull(buf, 10, &val);
  8515. + if (err)
  8516. + return -EINVAL;
  8517. +
  8518. + mutex_lock(&data.lock);
  8519. + *entry = val;
  8520. + mutex_unlock(&data.lock);
  8521. +
  8522. + return csize;
  8523. +}
  8524. +
  8525. +/**
  8526. + * debug_count_fopen - Open function for "count" debugfs entry
  8527. + * @inode: The in-kernel inode representation of the debugfs "file"
  8528. + * @filp: The active open file structure for the debugfs "file"
  8529. + *
  8530. + * This function provides an open implementation for the "count" debugfs
  8531. + * interface to the hardware latency detector.
  8532. + */
  8533. +static int debug_count_fopen(struct inode *inode, struct file *filp)
  8534. +{
  8535. + return 0;
  8536. +}
  8537. +
  8538. +/**
  8539. + * debug_count_fread - Read function for "count" debugfs entry
  8540. + * @filp: The active open file structure for the debugfs "file"
  8541. + * @ubuf: The userspace provided buffer to read value into
  8542. + * @cnt: The maximum number of bytes to read
  8543. + * @ppos: The current "file" position
  8544. + *
  8545. + * This function provides a read implementation for the "count" debugfs
  8546. + * interface to the hardware latency detector. Can be used to read the
  8547. + * number of latency readings exceeding the configured threshold since
  8548. + * the detector was last reset (e.g. by writing a zero into "count").
  8549. + */
  8550. +static ssize_t debug_count_fread(struct file *filp, char __user *ubuf,
  8551. + size_t cnt, loff_t *ppos)
  8552. +{
  8553. + return simple_data_read(filp, ubuf, cnt, ppos, &data.count);
  8554. +}
  8555. +
  8556. +/**
  8557. + * debug_count_fwrite - Write function for "count" debugfs entry
  8558. + * @filp: The active open file structure for the debugfs "file"
  8559. + * @ubuf: The user buffer that contains the value to write
  8560. + * @cnt: The maximum number of bytes to write to "file"
  8561. + * @ppos: The current position in the debugfs "file"
  8562. + *
  8563. + * This function provides a write implementation for the "count" debugfs
  8564. + * interface to the hardware latency detector. Can be used to write a
  8565. + * desired value, especially to zero the total count.
  8566. + */
  8567. +static ssize_t debug_count_fwrite(struct file *filp,
  8568. + const char __user *ubuf,
  8569. + size_t cnt,
  8570. + loff_t *ppos)
  8571. +{
  8572. + return simple_data_write(filp, ubuf, cnt, ppos, &data.count);
  8573. +}
  8574. +
  8575. +/**
  8576. + * debug_enable_fopen - Dummy open function for "enable" debugfs interface
  8577. + * @inode: The in-kernel inode representation of the debugfs "file"
  8578. + * @filp: The active open file structure for the debugfs "file"
  8579. + *
  8580. + * This function provides an open implementation for the "enable" debugfs
  8581. + * interface to the hardware latency detector.
  8582. + */
  8583. +static int debug_enable_fopen(struct inode *inode, struct file *filp)
  8584. +{
  8585. + return 0;
  8586. +}
  8587. +
  8588. +/**
  8589. + * debug_enable_fread - Read function for "enable" debugfs interface
  8590. + * @filp: The active open file structure for the debugfs "file"
  8591. + * @ubuf: The userspace provided buffer to read value into
  8592. + * @cnt: The maximum number of bytes to read
  8593. + * @ppos: The current "file" position
  8594. + *
  8595. + * This function provides a read implementation for the "enable" debugfs
  8596. + * interface to the hardware latency detector. Can be used to determine
  8597. + * whether the detector is currently enabled ("0\n" or "1\n" returned).
  8598. + */
  8599. +static ssize_t debug_enable_fread(struct file *filp, char __user *ubuf,
  8600. + size_t cnt, loff_t *ppos)
  8601. +{
  8602. + char buf[4];
  8603. +
  8604. + if ((cnt < sizeof(buf)) || (*ppos))
  8605. + return 0;
  8606. +
  8607. + buf[0] = enabled ? '1' : '0';
  8608. + buf[1] = '\n';
  8609. + buf[2] = '\0';
  8610. + if (copy_to_user(ubuf, buf, strlen(buf)))
  8611. + return -EFAULT;
  8612. + return *ppos = strlen(buf);
  8613. +}
  8614. +
  8615. +/**
  8616. + * debug_enable_fwrite - Write function for "enable" debugfs interface
  8617. + * @filp: The active open file structure for the debugfs "file"
  8618. + * @ubuf: The user buffer that contains the value to write
  8619. + * @cnt: The maximum number of bytes to write to "file"
  8620. + * @ppos: The current position in the debugfs "file"
  8621. + *
  8622. + * This function provides a write implementation for the "enable" debugfs
  8623. + * interface to the hardware latency detector. Can be used to enable or
  8624. + * disable the detector, which will have the side-effect of possibly
  8625. + * also resetting the global stats and kicking off the measuring
  8626. + * kthread (on an enable) or the converse (upon a disable).
  8627. + */
  8628. +static ssize_t debug_enable_fwrite(struct file *filp,
  8629. + const char __user *ubuf,
  8630. + size_t cnt,
  8631. + loff_t *ppos)
  8632. +{
  8633. + char buf[4];
  8634. + int csize = min(cnt, sizeof(buf));
  8635. + long val = 0;
  8636. + int err = 0;
  8637. +
  8638. + memset(buf, '\0', sizeof(buf));
  8639. + if (copy_from_user(buf, ubuf, csize))
  8640. + return -EFAULT;
  8641. +
  8642. + buf[sizeof(buf)-1] = '\0'; /* just in case */
  8643. + err = kstrtoul(buf, 10, &val);
  8644. + if (err)
  8645. + return -EINVAL;
  8646. +
  8647. + if (val) {
  8648. + if (enabled)
  8649. + goto unlock;
  8650. + enabled = 1;
  8651. + __reset_stats();
  8652. + if (start_kthread())
  8653. + return -EFAULT;
  8654. + } else {
  8655. + if (!enabled)
  8656. + goto unlock;
  8657. + enabled = 0;
  8658. + err = stop_kthread();
  8659. + if (err) {
  8660. + pr_err(BANNER "cannot stop kthread\n");
  8661. + return -EFAULT;
  8662. + }
  8663. + wake_up(&data.wq); /* reader(s) should return */
  8664. + }
  8665. +unlock:
  8666. + return csize;
  8667. +}
  8668. +
  8669. +/**
  8670. + * debug_max_fopen - Open function for "max" debugfs entry
  8671. + * @inode: The in-kernel inode representation of the debugfs "file"
  8672. + * @filp: The active open file structure for the debugfs "file"
  8673. + *
  8674. + * This function provides an open implementation for the "max" debugfs
  8675. + * interface to the hardware latency detector.
  8676. + */
  8677. +static int debug_max_fopen(struct inode *inode, struct file *filp)
  8678. +{
  8679. + return 0;
  8680. +}
  8681. +
  8682. +/**
  8683. + * debug_max_fread - Read function for "max" debugfs entry
  8684. + * @filp: The active open file structure for the debugfs "file"
  8685. + * @ubuf: The userspace provided buffer to read value into
  8686. + * @cnt: The maximum number of bytes to read
  8687. + * @ppos: The current "file" position
  8688. + *
  8689. + * This function provides a read implementation for the "max" debugfs
  8690. + * interface to the hardware latency detector. Can be used to determine
  8691. + * the maximum latency value observed since it was last reset.
  8692. + */
  8693. +static ssize_t debug_max_fread(struct file *filp, char __user *ubuf,
  8694. + size_t cnt, loff_t *ppos)
  8695. +{
  8696. + return simple_data_read(filp, ubuf, cnt, ppos, &data.max_sample);
  8697. +}
  8698. +
  8699. +/**
  8700. + * debug_max_fwrite - Write function for "max" debugfs entry
  8701. + * @filp: The active open file structure for the debugfs "file"
  8702. + * @ubuf: The user buffer that contains the value to write
  8703. + * @cnt: The maximum number of bytes to write to "file"
  8704. + * @ppos: The current position in the debugfs "file"
  8705. + *
  8706. + * This function provides a write implementation for the "max" debugfs
  8707. + * interface to the hardware latency detector. Can be used to reset the
  8708. + * maximum or set it to some other desired value - if, then, subsequent
  8709. + * measurements exceed this value, the maximum will be updated.
  8710. + */
  8711. +static ssize_t debug_max_fwrite(struct file *filp,
  8712. + const char __user *ubuf,
  8713. + size_t cnt,
  8714. + loff_t *ppos)
  8715. +{
  8716. + return simple_data_write(filp, ubuf, cnt, ppos, &data.max_sample);
  8717. +}
  8718. +
  8719. +
  8720. +/**
  8721. + * debug_sample_fopen - An open function for "sample" debugfs interface
  8722. + * @inode: The in-kernel inode representation of this debugfs "file"
  8723. + * @filp: The active open file structure for the debugfs "file"
  8724. + *
  8725. + * This function handles opening the "sample" file within the hardware
  8726. + * latency detector debugfs directory interface. This file is used to read
  8727. + * raw samples from the global ring_buffer and allows the user to see a
  8728. + * running latency history. Can be opened blocking or non-blocking,
  8729. + * affecting whether it behaves as a buffer read pipe, or does not.
  8730. + * Implements simple locking to prevent multiple simultaneous use.
  8731. + */
  8732. +static int debug_sample_fopen(struct inode *inode, struct file *filp)
  8733. +{
  8734. + if (!atomic_add_unless(&data.sample_open, 1, 1))
  8735. + return -EBUSY;
  8736. + else
  8737. + return 0;
  8738. +}
  8739. +
  8740. +/**
  8741. + * debug_sample_fread - A read function for "sample" debugfs interface
  8742. + * @filp: The active open file structure for the debugfs "file"
  8743. + * @ubuf: The user buffer that will contain the samples read
  8744. + * @cnt: The maximum bytes to read from the debugfs "file"
  8745. + * @ppos: The current position in the debugfs "file"
  8746. + *
  8747. + * This function handles reading from the "sample" file within the hardware
  8748. + * latency detector debugfs directory interface. This file is used to read
  8749. + * raw samples from the global ring_buffer and allows the user to see a
  8750. + * running latency history. By default this will block pending a new
  8751. + * value written into the sample buffer, unless there are already a
  8752. + * number of value(s) waiting in the buffer, or the sample file was
  8753. + * previously opened in a non-blocking mode of operation.
  8754. + */
  8755. +static ssize_t debug_sample_fread(struct file *filp, char __user *ubuf,
  8756. + size_t cnt, loff_t *ppos)
  8757. +{
  8758. + int len = 0;
  8759. + char buf[64];
  8760. + struct sample *sample = NULL;
  8761. +
  8762. + if (!enabled)
  8763. + return 0;
  8764. +
  8765. + sample = kzalloc(sizeof(struct sample), GFP_KERNEL);
  8766. + if (!sample)
  8767. + return -ENOMEM;
  8768. +
  8769. + while (!buffer_get_sample(sample)) {
  8770. +
  8771. + DEFINE_WAIT(wait);
  8772. +
  8773. + if (filp->f_flags & O_NONBLOCK) {
  8774. + len = -EAGAIN;
  8775. + goto out;
  8776. + }
  8777. +
  8778. + prepare_to_wait(&data.wq, &wait, TASK_INTERRUPTIBLE);
  8779. + schedule();
  8780. + finish_wait(&data.wq, &wait);
  8781. +
  8782. + if (signal_pending(current)) {
  8783. + len = -EINTR;
  8784. + goto out;
  8785. + }
  8786. +
  8787. + if (!enabled) { /* enable was toggled */
  8788. + len = 0;
  8789. + goto out;
  8790. + }
  8791. + }
  8792. +
  8793. + len = snprintf(buf, sizeof(buf), "%010lu.%010lu\t%llu\t%llu\n",
  8794. + sample->timestamp.tv_sec,
  8795. + sample->timestamp.tv_nsec,
  8796. + sample->duration,
  8797. + sample->outer_duration);
  8798. +
  8799. +
  8800. + /* handling partial reads is more trouble than it's worth */
  8801. + if (len > cnt)
  8802. + goto out;
  8803. +
  8804. + if (copy_to_user(ubuf, buf, len))
  8805. + len = -EFAULT;
  8806. +
  8807. +out:
  8808. + kfree(sample);
  8809. + return len;
  8810. +}
  8811. +
  8812. +/**
  8813. + * debug_sample_release - Release function for "sample" debugfs interface
  8814. + * @inode: The in-kernel inode represenation of the debugfs "file"
  8815. + * @filp: The active open file structure for the debugfs "file"
  8816. + *
  8817. + * This function completes the close of the debugfs interface "sample" file.
  8818. + * Frees the sample_open "lock" so that other users may open the interface.
  8819. + */
  8820. +static int debug_sample_release(struct inode *inode, struct file *filp)
  8821. +{
  8822. + atomic_dec(&data.sample_open);
  8823. +
  8824. + return 0;
  8825. +}
  8826. +
  8827. +/**
  8828. + * debug_threshold_fopen - Open function for "threshold" debugfs entry
  8829. + * @inode: The in-kernel inode representation of the debugfs "file"
  8830. + * @filp: The active open file structure for the debugfs "file"
  8831. + *
  8832. + * This function provides an open implementation for the "threshold" debugfs
  8833. + * interface to the hardware latency detector.
  8834. + */
  8835. +static int debug_threshold_fopen(struct inode *inode, struct file *filp)
  8836. +{
  8837. + return 0;
  8838. +}
  8839. +
  8840. +/**
  8841. + * debug_threshold_fread - Read function for "threshold" debugfs entry
  8842. + * @filp: The active open file structure for the debugfs "file"
  8843. + * @ubuf: The userspace provided buffer to read value into
  8844. + * @cnt: The maximum number of bytes to read
  8845. + * @ppos: The current "file" position
  8846. + *
  8847. + * This function provides a read implementation for the "threshold" debugfs
  8848. + * interface to the hardware latency detector. It can be used to determine
  8849. + * the current threshold level at which a latency will be recorded in the
  8850. + * global ring buffer, typically on the order of 10us.
  8851. + */
  8852. +static ssize_t debug_threshold_fread(struct file *filp, char __user *ubuf,
  8853. + size_t cnt, loff_t *ppos)
  8854. +{
  8855. + return simple_data_read(filp, ubuf, cnt, ppos, &data.threshold);
  8856. +}
  8857. +
  8858. +/**
  8859. + * debug_threshold_fwrite - Write function for "threshold" debugfs entry
  8860. + * @filp: The active open file structure for the debugfs "file"
  8861. + * @ubuf: The user buffer that contains the value to write
  8862. + * @cnt: The maximum number of bytes to write to "file"
  8863. + * @ppos: The current position in the debugfs "file"
  8864. + *
  8865. + * This function provides a write implementation for the "threshold" debugfs
  8866. + * interface to the hardware latency detector. It can be used to configure
  8867. + * the threshold level at which any subsequently detected latencies will
  8868. + * be recorded into the global ring buffer.
  8869. + */
  8870. +static ssize_t debug_threshold_fwrite(struct file *filp,
  8871. + const char __user *ubuf,
  8872. + size_t cnt,
  8873. + loff_t *ppos)
  8874. +{
  8875. + int ret;
  8876. +
  8877. + ret = simple_data_write(filp, ubuf, cnt, ppos, &data.threshold);
  8878. +
  8879. + if (enabled)
  8880. + wake_up_process(kthread);
  8881. +
  8882. + return ret;
  8883. +}
  8884. +
  8885. +/**
  8886. + * debug_width_fopen - Open function for "width" debugfs entry
  8887. + * @inode: The in-kernel inode representation of the debugfs "file"
  8888. + * @filp: The active open file structure for the debugfs "file"
  8889. + *
  8890. + * This function provides an open implementation for the "width" debugfs
  8891. + * interface to the hardware latency detector.
  8892. + */
  8893. +static int debug_width_fopen(struct inode *inode, struct file *filp)
  8894. +{
  8895. + return 0;
  8896. +}
  8897. +
  8898. +/**
  8899. + * debug_width_fread - Read function for "width" debugfs entry
  8900. + * @filp: The active open file structure for the debugfs "file"
  8901. + * @ubuf: The userspace provided buffer to read value into
  8902. + * @cnt: The maximum number of bytes to read
  8903. + * @ppos: The current "file" position
  8904. + *
  8905. + * This function provides a read implementation for the "width" debugfs
  8906. + * interface to the hardware latency detector. It can be used to determine
  8907. + * for how many us of the total window us we will actively sample for any
  8908. + * hardware-induced latecy periods. Obviously, it is not possible to
  8909. + * sample constantly and have the system respond to a sample reader, or,
  8910. + * worse, without having the system appear to have gone out to lunch.
  8911. + */
  8912. +static ssize_t debug_width_fread(struct file *filp, char __user *ubuf,
  8913. + size_t cnt, loff_t *ppos)
  8914. +{
  8915. + return simple_data_read(filp, ubuf, cnt, ppos, &data.sample_width);
  8916. +}
  8917. +
  8918. +/**
  8919. + * debug_width_fwrite - Write function for "width" debugfs entry
  8920. + * @filp: The active open file structure for the debugfs "file"
  8921. + * @ubuf: The user buffer that contains the value to write
  8922. + * @cnt: The maximum number of bytes to write to "file"
  8923. + * @ppos: The current position in the debugfs "file"
  8924. + *
  8925. + * This function provides a write implementation for the "width" debugfs
  8926. + * interface to the hardware latency detector. It can be used to configure
  8927. + * for how many us of the total window us we will actively sample for any
  8928. + * hardware-induced latency periods. Obviously, it is not possible to
  8929. + * sample constantly and have the system respond to a sample reader, or,
  8930. + * worse, without having the system appear to have gone out to lunch. It
  8931. + * is enforced that width is less that the total window size.
  8932. + */
  8933. +static ssize_t debug_width_fwrite(struct file *filp,
  8934. + const char __user *ubuf,
  8935. + size_t cnt,
  8936. + loff_t *ppos)
  8937. +{
  8938. + char buf[U64STR_SIZE];
  8939. + int csize = min(cnt, sizeof(buf));
  8940. + u64 val = 0;
  8941. + int err = 0;
  8942. +
  8943. + memset(buf, '\0', sizeof(buf));
  8944. + if (copy_from_user(buf, ubuf, csize))
  8945. + return -EFAULT;
  8946. +
  8947. + buf[U64STR_SIZE-1] = '\0'; /* just in case */
  8948. + err = kstrtoull(buf, 10, &val);
  8949. + if (err)
  8950. + return -EINVAL;
  8951. +
  8952. + mutex_lock(&data.lock);
  8953. + if (val < data.sample_window)
  8954. + data.sample_width = val;
  8955. + else {
  8956. + mutex_unlock(&data.lock);
  8957. + return -EINVAL;
  8958. + }
  8959. + mutex_unlock(&data.lock);
  8960. +
  8961. + if (enabled)
  8962. + wake_up_process(kthread);
  8963. +
  8964. + return csize;
  8965. +}
  8966. +
  8967. +/**
  8968. + * debug_window_fopen - Open function for "window" debugfs entry
  8969. + * @inode: The in-kernel inode representation of the debugfs "file"
  8970. + * @filp: The active open file structure for the debugfs "file"
  8971. + *
  8972. + * This function provides an open implementation for the "window" debugfs
  8973. + * interface to the hardware latency detector. The window is the total time
  8974. + * in us that will be considered one sample period. Conceptually, windows
  8975. + * occur back-to-back and contain a sample width period during which
  8976. + * actual sampling occurs.
  8977. + */
  8978. +static int debug_window_fopen(struct inode *inode, struct file *filp)
  8979. +{
  8980. + return 0;
  8981. +}
  8982. +
  8983. +/**
  8984. + * debug_window_fread - Read function for "window" debugfs entry
  8985. + * @filp: The active open file structure for the debugfs "file"
  8986. + * @ubuf: The userspace provided buffer to read value into
  8987. + * @cnt: The maximum number of bytes to read
  8988. + * @ppos: The current "file" position
  8989. + *
  8990. + * This function provides a read implementation for the "window" debugfs
  8991. + * interface to the hardware latency detector. The window is the total time
  8992. + * in us that will be considered one sample period. Conceptually, windows
  8993. + * occur back-to-back and contain a sample width period during which
  8994. + * actual sampling occurs. Can be used to read the total window size.
  8995. + */
  8996. +static ssize_t debug_window_fread(struct file *filp, char __user *ubuf,
  8997. + size_t cnt, loff_t *ppos)
  8998. +{
  8999. + return simple_data_read(filp, ubuf, cnt, ppos, &data.sample_window);
  9000. +}
  9001. +
  9002. +/**
  9003. + * debug_window_fwrite - Write function for "window" debugfs entry
  9004. + * @filp: The active open file structure for the debugfs "file"
  9005. + * @ubuf: The user buffer that contains the value to write
  9006. + * @cnt: The maximum number of bytes to write to "file"
  9007. + * @ppos: The current position in the debugfs "file"
  9008. + *
  9009. + * This function provides a write implementation for the "window" debufds
  9010. + * interface to the hardware latency detetector. The window is the total time
  9011. + * in us that will be considered one sample period. Conceptually, windows
  9012. + * occur back-to-back and contain a sample width period during which
  9013. + * actual sampling occurs. Can be used to write a new total window size. It
  9014. + * is enfoced that any value written must be greater than the sample width
  9015. + * size, or an error results.
  9016. + */
  9017. +static ssize_t debug_window_fwrite(struct file *filp,
  9018. + const char __user *ubuf,
  9019. + size_t cnt,
  9020. + loff_t *ppos)
  9021. +{
  9022. + char buf[U64STR_SIZE];
  9023. + int csize = min(cnt, sizeof(buf));
  9024. + u64 val = 0;
  9025. + int err = 0;
  9026. +
  9027. + memset(buf, '\0', sizeof(buf));
  9028. + if (copy_from_user(buf, ubuf, csize))
  9029. + return -EFAULT;
  9030. +
  9031. + buf[U64STR_SIZE-1] = '\0'; /* just in case */
  9032. + err = kstrtoull(buf, 10, &val);
  9033. + if (err)
  9034. + return -EINVAL;
  9035. +
  9036. + mutex_lock(&data.lock);
  9037. + if (data.sample_width < val)
  9038. + data.sample_window = val;
  9039. + else {
  9040. + mutex_unlock(&data.lock);
  9041. + return -EINVAL;
  9042. + }
  9043. + mutex_unlock(&data.lock);
  9044. +
  9045. + return csize;
  9046. +}
  9047. +
  9048. +/*
  9049. + * Function pointers for the "count" debugfs file operations
  9050. + */
  9051. +static const struct file_operations count_fops = {
  9052. + .open = debug_count_fopen,
  9053. + .read = debug_count_fread,
  9054. + .write = debug_count_fwrite,
  9055. + .owner = THIS_MODULE,
  9056. +};
  9057. +
  9058. +/*
  9059. + * Function pointers for the "enable" debugfs file operations
  9060. + */
  9061. +static const struct file_operations enable_fops = {
  9062. + .open = debug_enable_fopen,
  9063. + .read = debug_enable_fread,
  9064. + .write = debug_enable_fwrite,
  9065. + .owner = THIS_MODULE,
  9066. +};
  9067. +
  9068. +/*
  9069. + * Function pointers for the "max" debugfs file operations
  9070. + */
  9071. +static const struct file_operations max_fops = {
  9072. + .open = debug_max_fopen,
  9073. + .read = debug_max_fread,
  9074. + .write = debug_max_fwrite,
  9075. + .owner = THIS_MODULE,
  9076. +};
  9077. +
  9078. +/*
  9079. + * Function pointers for the "sample" debugfs file operations
  9080. + */
  9081. +static const struct file_operations sample_fops = {
  9082. + .open = debug_sample_fopen,
  9083. + .read = debug_sample_fread,
  9084. + .release = debug_sample_release,
  9085. + .owner = THIS_MODULE,
  9086. +};
  9087. +
  9088. +/*
  9089. + * Function pointers for the "threshold" debugfs file operations
  9090. + */
  9091. +static const struct file_operations threshold_fops = {
  9092. + .open = debug_threshold_fopen,
  9093. + .read = debug_threshold_fread,
  9094. + .write = debug_threshold_fwrite,
  9095. + .owner = THIS_MODULE,
  9096. +};
  9097. +
  9098. +/*
  9099. + * Function pointers for the "width" debugfs file operations
  9100. + */
  9101. +static const struct file_operations width_fops = {
  9102. + .open = debug_width_fopen,
  9103. + .read = debug_width_fread,
  9104. + .write = debug_width_fwrite,
  9105. + .owner = THIS_MODULE,
  9106. +};
  9107. +
  9108. +/*
  9109. + * Function pointers for the "window" debugfs file operations
  9110. + */
  9111. +static const struct file_operations window_fops = {
  9112. + .open = debug_window_fopen,
  9113. + .read = debug_window_fread,
  9114. + .write = debug_window_fwrite,
  9115. + .owner = THIS_MODULE,
  9116. +};
  9117. +
  9118. +/**
  9119. + * init_debugfs - A function to initialize the debugfs interface files
  9120. + *
  9121. + * This function creates entries in debugfs for "hwlat_detector", including
  9122. + * files to read values from the detector, current samples, and the
  9123. + * maximum sample that has been captured since the hardware latency
  9124. + * dectector was started.
  9125. + */
  9126. +static int init_debugfs(void)
  9127. +{
  9128. + int ret = -ENOMEM;
  9129. +
  9130. + debug_dir = debugfs_create_dir(DRVNAME, NULL);
  9131. + if (!debug_dir)
  9132. + goto err_debug_dir;
  9133. +
  9134. + debug_sample = debugfs_create_file("sample", 0444,
  9135. + debug_dir, NULL,
  9136. + &sample_fops);
  9137. + if (!debug_sample)
  9138. + goto err_sample;
  9139. +
  9140. + debug_count = debugfs_create_file("count", 0444,
  9141. + debug_dir, NULL,
  9142. + &count_fops);
  9143. + if (!debug_count)
  9144. + goto err_count;
  9145. +
  9146. + debug_max = debugfs_create_file("max", 0444,
  9147. + debug_dir, NULL,
  9148. + &max_fops);
  9149. + if (!debug_max)
  9150. + goto err_max;
  9151. +
  9152. + debug_sample_window = debugfs_create_file("window", 0644,
  9153. + debug_dir, NULL,
  9154. + &window_fops);
  9155. + if (!debug_sample_window)
  9156. + goto err_window;
  9157. +
  9158. + debug_sample_width = debugfs_create_file("width", 0644,
  9159. + debug_dir, NULL,
  9160. + &width_fops);
  9161. + if (!debug_sample_width)
  9162. + goto err_width;
  9163. +
  9164. + debug_threshold = debugfs_create_file("threshold", 0644,
  9165. + debug_dir, NULL,
  9166. + &threshold_fops);
  9167. + if (!debug_threshold)
  9168. + goto err_threshold;
  9169. +
  9170. + debug_enable = debugfs_create_file("enable", 0644,
  9171. + debug_dir, &enabled,
  9172. + &enable_fops);
  9173. + if (!debug_enable)
  9174. + goto err_enable;
  9175. +
  9176. + else {
  9177. + ret = 0;
  9178. + goto out;
  9179. + }
  9180. +
  9181. +err_enable:
  9182. + debugfs_remove(debug_threshold);
  9183. +err_threshold:
  9184. + debugfs_remove(debug_sample_width);
  9185. +err_width:
  9186. + debugfs_remove(debug_sample_window);
  9187. +err_window:
  9188. + debugfs_remove(debug_max);
  9189. +err_max:
  9190. + debugfs_remove(debug_count);
  9191. +err_count:
  9192. + debugfs_remove(debug_sample);
  9193. +err_sample:
  9194. + debugfs_remove(debug_dir);
  9195. +err_debug_dir:
  9196. +out:
  9197. + return ret;
  9198. +}
  9199. +
  9200. +/**
  9201. + * free_debugfs - A function to cleanup the debugfs file interface
  9202. + */
  9203. +static void free_debugfs(void)
  9204. +{
  9205. + /* could also use a debugfs_remove_recursive */
  9206. + debugfs_remove(debug_enable);
  9207. + debugfs_remove(debug_threshold);
  9208. + debugfs_remove(debug_sample_width);
  9209. + debugfs_remove(debug_sample_window);
  9210. + debugfs_remove(debug_max);
  9211. + debugfs_remove(debug_count);
  9212. + debugfs_remove(debug_sample);
  9213. + debugfs_remove(debug_dir);
  9214. +}
  9215. +
  9216. +/**
  9217. + * detector_init - Standard module initialization code
  9218. + */
  9219. +static int detector_init(void)
  9220. +{
  9221. + int ret = -ENOMEM;
  9222. +
  9223. + pr_info(BANNER "version %s\n", VERSION);
  9224. +
  9225. + ret = init_stats();
  9226. + if (ret)
  9227. + goto out;
  9228. +
  9229. + ret = init_debugfs();
  9230. + if (ret)
  9231. + goto err_stats;
  9232. +
  9233. + if (enabled)
  9234. + ret = start_kthread();
  9235. +
  9236. + goto out;
  9237. +
  9238. +err_stats:
  9239. + ring_buffer_free(ring_buffer);
  9240. +out:
  9241. + return ret;
  9242. +
  9243. +}
  9244. +
  9245. +/**
  9246. + * detector_exit - Standard module cleanup code
  9247. + */
  9248. +static void detector_exit(void)
  9249. +{
  9250. + int err;
  9251. +
  9252. + if (enabled) {
  9253. + enabled = 0;
  9254. + err = stop_kthread();
  9255. + if (err)
  9256. + pr_err(BANNER "cannot stop kthread\n");
  9257. + }
  9258. +
  9259. + free_debugfs();
  9260. + ring_buffer_free(ring_buffer); /* free up the ring buffer */
  9261. +
  9262. +}
  9263. +
  9264. +module_init(detector_init);
  9265. +module_exit(detector_exit);
  9266. diff -Nur linux-4.4.46.orig/drivers/misc/Kconfig linux-4.4.46/drivers/misc/Kconfig
  9267. --- linux-4.4.46.orig/drivers/misc/Kconfig 2017-02-01 08:31:11.000000000 +0100
  9268. +++ linux-4.4.46/drivers/misc/Kconfig 2017-02-03 17:18:05.683416484 +0100
  9269. @@ -54,6 +54,7 @@
  9270. config ATMEL_TCLIB
  9271. bool "Atmel AT32/AT91 Timer/Counter Library"
  9272. depends on (AVR32 || ARCH_AT91)
  9273. + default y if PREEMPT_RT_FULL
  9274. help
  9275. Select this if you want a library to allocate the Timer/Counter
  9276. blocks found on many Atmel processors. This facilitates using
  9277. @@ -69,8 +70,7 @@
  9278. are combined to make a single 32-bit timer.
  9279. When GENERIC_CLOCKEVENTS is defined, the third timer channel
  9280. - may be used as a clock event device supporting oneshot mode
  9281. - (delays of up to two seconds) based on the 32 KiHz clock.
  9282. + may be used as a clock event device supporting oneshot mode.
  9283. config ATMEL_TCB_CLKSRC_BLOCK
  9284. int
  9285. @@ -84,6 +84,15 @@
  9286. TC can be used for other purposes, such as PWM generation and
  9287. interval timing.
  9288. +config ATMEL_TCB_CLKSRC_USE_SLOW_CLOCK
  9289. + bool "TC Block use 32 KiHz clock"
  9290. + depends on ATMEL_TCB_CLKSRC
  9291. + default y if !PREEMPT_RT_FULL
  9292. + help
  9293. + Select this to use 32 KiHz base clock rate as TC block clock
  9294. + source for clock events.
  9295. +
  9296. +
  9297. config DUMMY_IRQ
  9298. tristate "Dummy IRQ handler"
  9299. default n
  9300. @@ -113,6 +122,35 @@
  9301. for information on the specific driver level and support statement
  9302. for your IBM server.
  9303. +config HWLAT_DETECTOR
  9304. + tristate "Testing module to detect hardware-induced latencies"
  9305. + depends on DEBUG_FS
  9306. + depends on RING_BUFFER
  9307. + default m
  9308. + ---help---
  9309. + A simple hardware latency detector. Use this module to detect
  9310. + large latencies introduced by the behavior of the underlying
  9311. + system firmware external to Linux. We do this using periodic
  9312. + use of stop_machine to grab all available CPUs and measure
  9313. + for unexplainable gaps in the CPU timestamp counter(s). By
  9314. + default, the module is not enabled until the "enable" file
  9315. + within the "hwlat_detector" debugfs directory is toggled.
  9316. +
  9317. + This module is often used to detect SMI (System Management
  9318. + Interrupts) on x86 systems, though is not x86 specific. To
  9319. + this end, we default to using a sample window of 1 second,
  9320. + during which we will sample for 0.5 seconds. If an SMI or
  9321. + similar event occurs during that time, it is recorded
  9322. + into an 8K samples global ring buffer until retreived.
  9323. +
  9324. + WARNING: This software should never be enabled (it can be built
  9325. + but should not be turned on after it is loaded) in a production
  9326. + environment where high latencies are a concern since the
  9327. + sampling mechanism actually introduces latencies for
  9328. + regular tasks while the CPU(s) are being held.
  9329. +
  9330. + If unsure, say N
  9331. +
  9332. config PHANTOM
  9333. tristate "Sensable PHANToM (PCI)"
  9334. depends on PCI
  9335. diff -Nur linux-4.4.46.orig/drivers/misc/Makefile linux-4.4.46/drivers/misc/Makefile
  9336. --- linux-4.4.46.orig/drivers/misc/Makefile 2017-02-01 08:31:11.000000000 +0100
  9337. +++ linux-4.4.46/drivers/misc/Makefile 2017-02-03 17:18:05.683416484 +0100
  9338. @@ -39,6 +39,7 @@
  9339. obj-$(CONFIG_HMC6352) += hmc6352.o
  9340. obj-y += eeprom/
  9341. obj-y += cb710/
  9342. +obj-$(CONFIG_HWLAT_DETECTOR) += hwlat_detector.o
  9343. obj-$(CONFIG_SPEAR13XX_PCIE_GADGET) += spear13xx_pcie_gadget.o
  9344. obj-$(CONFIG_VMWARE_BALLOON) += vmw_balloon.o
  9345. obj-$(CONFIG_ARM_CHARLCD) += arm-charlcd.o
  9346. diff -Nur linux-4.4.46.orig/drivers/mmc/host/mmci.c linux-4.4.46/drivers/mmc/host/mmci.c
  9347. --- linux-4.4.46.orig/drivers/mmc/host/mmci.c 2017-02-01 08:31:11.000000000 +0100
  9348. +++ linux-4.4.46/drivers/mmc/host/mmci.c 2017-02-03 17:18:05.687416638 +0100
  9349. @@ -1155,15 +1155,12 @@
  9350. struct sg_mapping_iter *sg_miter = &host->sg_miter;
  9351. struct variant_data *variant = host->variant;
  9352. void __iomem *base = host->base;
  9353. - unsigned long flags;
  9354. u32 status;
  9355. status = readl(base + MMCISTATUS);
  9356. dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
  9357. - local_irq_save(flags);
  9358. -
  9359. do {
  9360. unsigned int remain, len;
  9361. char *buffer;
  9362. @@ -1203,8 +1200,6 @@
  9363. sg_miter_stop(sg_miter);
  9364. - local_irq_restore(flags);
  9365. -
  9366. /*
  9367. * If we have less than the fifo 'half-full' threshold to transfer,
  9368. * trigger a PIO interrupt as soon as any data is available.
  9369. diff -Nur linux-4.4.46.orig/drivers/net/ethernet/3com/3c59x.c linux-4.4.46/drivers/net/ethernet/3com/3c59x.c
  9370. --- linux-4.4.46.orig/drivers/net/ethernet/3com/3c59x.c 2017-02-01 08:31:11.000000000 +0100
  9371. +++ linux-4.4.46/drivers/net/ethernet/3com/3c59x.c 2017-02-03 17:18:05.687416638 +0100
  9372. @@ -842,9 +842,9 @@
  9373. {
  9374. struct vortex_private *vp = netdev_priv(dev);
  9375. unsigned long flags;
  9376. - local_irq_save(flags);
  9377. + local_irq_save_nort(flags);
  9378. (vp->full_bus_master_rx ? boomerang_interrupt:vortex_interrupt)(dev->irq,dev);
  9379. - local_irq_restore(flags);
  9380. + local_irq_restore_nort(flags);
  9381. }
  9382. #endif
  9383. @@ -1916,12 +1916,12 @@
  9384. * Block interrupts because vortex_interrupt does a bare spin_lock()
  9385. */
  9386. unsigned long flags;
  9387. - local_irq_save(flags);
  9388. + local_irq_save_nort(flags);
  9389. if (vp->full_bus_master_tx)
  9390. boomerang_interrupt(dev->irq, dev);
  9391. else
  9392. vortex_interrupt(dev->irq, dev);
  9393. - local_irq_restore(flags);
  9394. + local_irq_restore_nort(flags);
  9395. }
  9396. }
  9397. diff -Nur linux-4.4.46.orig/drivers/net/ethernet/atheros/atl1c/atl1c_main.c linux-4.4.46/drivers/net/ethernet/atheros/atl1c/atl1c_main.c
  9398. --- linux-4.4.46.orig/drivers/net/ethernet/atheros/atl1c/atl1c_main.c 2017-02-01 08:31:11.000000000 +0100
  9399. +++ linux-4.4.46/drivers/net/ethernet/atheros/atl1c/atl1c_main.c 2017-02-03 17:18:05.687416638 +0100
  9400. @@ -2221,11 +2221,7 @@
  9401. }
  9402. tpd_req = atl1c_cal_tpd_req(skb);
  9403. - if (!spin_trylock_irqsave(&adapter->tx_lock, flags)) {
  9404. - if (netif_msg_pktdata(adapter))
  9405. - dev_info(&adapter->pdev->dev, "tx locked\n");
  9406. - return NETDEV_TX_LOCKED;
  9407. - }
  9408. + spin_lock_irqsave(&adapter->tx_lock, flags);
  9409. if (atl1c_tpd_avail(adapter, type) < tpd_req) {
  9410. /* no enough descriptor, just stop queue */
  9411. diff -Nur linux-4.4.46.orig/drivers/net/ethernet/atheros/atl1e/atl1e_main.c linux-4.4.46/drivers/net/ethernet/atheros/atl1e/atl1e_main.c
  9412. --- linux-4.4.46.orig/drivers/net/ethernet/atheros/atl1e/atl1e_main.c 2017-02-01 08:31:11.000000000 +0100
  9413. +++ linux-4.4.46/drivers/net/ethernet/atheros/atl1e/atl1e_main.c 2017-02-03 17:18:05.687416638 +0100
  9414. @@ -1880,8 +1880,7 @@
  9415. return NETDEV_TX_OK;
  9416. }
  9417. tpd_req = atl1e_cal_tdp_req(skb);
  9418. - if (!spin_trylock_irqsave(&adapter->tx_lock, flags))
  9419. - return NETDEV_TX_LOCKED;
  9420. + spin_lock_irqsave(&adapter->tx_lock, flags);
  9421. if (atl1e_tpd_avail(adapter) < tpd_req) {
  9422. /* no enough descriptor, just stop queue */
  9423. diff -Nur linux-4.4.46.orig/drivers/net/ethernet/chelsio/cxgb/sge.c linux-4.4.46/drivers/net/ethernet/chelsio/cxgb/sge.c
  9424. --- linux-4.4.46.orig/drivers/net/ethernet/chelsio/cxgb/sge.c 2017-02-01 08:31:11.000000000 +0100
  9425. +++ linux-4.4.46/drivers/net/ethernet/chelsio/cxgb/sge.c 2017-02-03 17:18:05.691416794 +0100
  9426. @@ -1664,8 +1664,7 @@
  9427. struct cmdQ *q = &sge->cmdQ[qid];
  9428. unsigned int credits, pidx, genbit, count, use_sched_skb = 0;
  9429. - if (!spin_trylock(&q->lock))
  9430. - return NETDEV_TX_LOCKED;
  9431. + spin_lock(&q->lock);
  9432. reclaim_completed_tx(sge, q);
  9433. diff -Nur linux-4.4.46.orig/drivers/net/ethernet/neterion/s2io.c linux-4.4.46/drivers/net/ethernet/neterion/s2io.c
  9434. --- linux-4.4.46.orig/drivers/net/ethernet/neterion/s2io.c 2017-02-01 08:31:11.000000000 +0100
  9435. +++ linux-4.4.46/drivers/net/ethernet/neterion/s2io.c 2017-02-03 17:18:05.691416794 +0100
  9436. @@ -4084,12 +4084,7 @@
  9437. [skb->priority & (MAX_TX_FIFOS - 1)];
  9438. fifo = &mac_control->fifos[queue];
  9439. - if (do_spin_lock)
  9440. - spin_lock_irqsave(&fifo->tx_lock, flags);
  9441. - else {
  9442. - if (unlikely(!spin_trylock_irqsave(&fifo->tx_lock, flags)))
  9443. - return NETDEV_TX_LOCKED;
  9444. - }
  9445. + spin_lock_irqsave(&fifo->tx_lock, flags);
  9446. if (sp->config.multiq) {
  9447. if (__netif_subqueue_stopped(dev, fifo->fifo_no)) {
  9448. diff -Nur linux-4.4.46.orig/drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c linux-4.4.46/drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c
  9449. --- linux-4.4.46.orig/drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c 2017-02-01 08:31:11.000000000 +0100
  9450. +++ linux-4.4.46/drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c 2017-02-03 17:18:05.691416794 +0100
  9451. @@ -2137,10 +2137,8 @@
  9452. struct pch_gbe_tx_ring *tx_ring = adapter->tx_ring;
  9453. unsigned long flags;
  9454. - if (!spin_trylock_irqsave(&tx_ring->tx_lock, flags)) {
  9455. - /* Collision - tell upper layer to requeue */
  9456. - return NETDEV_TX_LOCKED;
  9457. - }
  9458. + spin_lock_irqsave(&tx_ring->tx_lock, flags);
  9459. +
  9460. if (unlikely(!PCH_GBE_DESC_UNUSED(tx_ring))) {
  9461. netif_stop_queue(netdev);
  9462. spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
  9463. diff -Nur linux-4.4.46.orig/drivers/net/ethernet/realtek/8139too.c linux-4.4.46/drivers/net/ethernet/realtek/8139too.c
  9464. --- linux-4.4.46.orig/drivers/net/ethernet/realtek/8139too.c 2017-02-01 08:31:11.000000000 +0100
  9465. +++ linux-4.4.46/drivers/net/ethernet/realtek/8139too.c 2017-02-03 17:18:05.691416794 +0100
  9466. @@ -2229,7 +2229,7 @@
  9467. struct rtl8139_private *tp = netdev_priv(dev);
  9468. const int irq = tp->pci_dev->irq;
  9469. - disable_irq(irq);
  9470. + disable_irq_nosync(irq);
  9471. rtl8139_interrupt(irq, dev);
  9472. enable_irq(irq);
  9473. }
  9474. diff -Nur linux-4.4.46.orig/drivers/net/ethernet/tehuti/tehuti.c linux-4.4.46/drivers/net/ethernet/tehuti/tehuti.c
  9475. --- linux-4.4.46.orig/drivers/net/ethernet/tehuti/tehuti.c 2017-02-01 08:31:11.000000000 +0100
  9476. +++ linux-4.4.46/drivers/net/ethernet/tehuti/tehuti.c 2017-02-03 17:18:05.691416794 +0100
  9477. @@ -1629,13 +1629,8 @@
  9478. unsigned long flags;
  9479. ENTER;
  9480. - local_irq_save(flags);
  9481. - if (!spin_trylock(&priv->tx_lock)) {
  9482. - local_irq_restore(flags);
  9483. - DBG("%s[%s]: TX locked, returning NETDEV_TX_LOCKED\n",
  9484. - BDX_DRV_NAME, ndev->name);
  9485. - return NETDEV_TX_LOCKED;
  9486. - }
  9487. +
  9488. + spin_lock_irqsave(&priv->tx_lock, flags);
  9489. /* build tx descriptor */
  9490. BDX_ASSERT(f->m.wptr >= f->m.memsz); /* started with valid wptr */
  9491. diff -Nur linux-4.4.46.orig/drivers/net/rionet.c linux-4.4.46/drivers/net/rionet.c
  9492. --- linux-4.4.46.orig/drivers/net/rionet.c 2017-02-01 08:31:11.000000000 +0100
  9493. +++ linux-4.4.46/drivers/net/rionet.c 2017-02-03 17:18:05.691416794 +0100
  9494. @@ -174,11 +174,7 @@
  9495. unsigned long flags;
  9496. int add_num = 1;
  9497. - local_irq_save(flags);
  9498. - if (!spin_trylock(&rnet->tx_lock)) {
  9499. - local_irq_restore(flags);
  9500. - return NETDEV_TX_LOCKED;
  9501. - }
  9502. + spin_lock_irqsave(&rnet->tx_lock, flags);
  9503. if (is_multicast_ether_addr(eth->h_dest))
  9504. add_num = nets[rnet->mport->id].nact;
  9505. diff -Nur linux-4.4.46.orig/drivers/net/wireless/orinoco/orinoco_usb.c linux-4.4.46/drivers/net/wireless/orinoco/orinoco_usb.c
  9506. --- linux-4.4.46.orig/drivers/net/wireless/orinoco/orinoco_usb.c 2017-02-01 08:31:11.000000000 +0100
  9507. +++ linux-4.4.46/drivers/net/wireless/orinoco/orinoco_usb.c 2017-02-03 17:18:05.695416949 +0100
  9508. @@ -697,7 +697,7 @@
  9509. while (!ctx->done.done && msecs--)
  9510. udelay(1000);
  9511. } else {
  9512. - wait_event_interruptible(ctx->done.wait,
  9513. + swait_event_interruptible(ctx->done.wait,
  9514. ctx->done.done);
  9515. }
  9516. break;
  9517. diff -Nur linux-4.4.46.orig/drivers/pci/access.c linux-4.4.46/drivers/pci/access.c
  9518. --- linux-4.4.46.orig/drivers/pci/access.c 2017-02-01 08:31:11.000000000 +0100
  9519. +++ linux-4.4.46/drivers/pci/access.c 2017-02-03 17:18:05.695416949 +0100
  9520. @@ -561,7 +561,7 @@
  9521. WARN_ON(!dev->block_cfg_access);
  9522. dev->block_cfg_access = 0;
  9523. - wake_up_all(&pci_cfg_wait);
  9524. + wake_up_all_locked(&pci_cfg_wait);
  9525. raw_spin_unlock_irqrestore(&pci_lock, flags);
  9526. }
  9527. EXPORT_SYMBOL_GPL(pci_cfg_access_unlock);
  9528. diff -Nur linux-4.4.46.orig/drivers/scsi/fcoe/fcoe.c linux-4.4.46/drivers/scsi/fcoe/fcoe.c
  9529. --- linux-4.4.46.orig/drivers/scsi/fcoe/fcoe.c 2017-02-01 08:31:11.000000000 +0100
  9530. +++ linux-4.4.46/drivers/scsi/fcoe/fcoe.c 2017-02-03 17:18:05.695416949 +0100
  9531. @@ -1286,7 +1286,7 @@
  9532. struct sk_buff *skb;
  9533. #ifdef CONFIG_SMP
  9534. struct fcoe_percpu_s *p0;
  9535. - unsigned targ_cpu = get_cpu();
  9536. + unsigned targ_cpu = get_cpu_light();
  9537. #endif /* CONFIG_SMP */
  9538. FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
  9539. @@ -1342,7 +1342,7 @@
  9540. kfree_skb(skb);
  9541. spin_unlock_bh(&p->fcoe_rx_list.lock);
  9542. }
  9543. - put_cpu();
  9544. + put_cpu_light();
  9545. #else
  9546. /*
  9547. * This a non-SMP scenario where the singular Rx thread is
  9548. @@ -1566,11 +1566,11 @@
  9549. static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
  9550. {
  9551. struct fcoe_percpu_s *fps;
  9552. - int rc;
  9553. + int rc, cpu = get_cpu_light();
  9554. - fps = &get_cpu_var(fcoe_percpu);
  9555. + fps = &per_cpu(fcoe_percpu, cpu);
  9556. rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
  9557. - put_cpu_var(fcoe_percpu);
  9558. + put_cpu_light();
  9559. return rc;
  9560. }
  9561. @@ -1766,11 +1766,11 @@
  9562. return 0;
  9563. }
  9564. - stats = per_cpu_ptr(lport->stats, get_cpu());
  9565. + stats = per_cpu_ptr(lport->stats, get_cpu_light());
  9566. stats->InvalidCRCCount++;
  9567. if (stats->InvalidCRCCount < 5)
  9568. printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
  9569. - put_cpu();
  9570. + put_cpu_light();
  9571. return -EINVAL;
  9572. }
  9573. @@ -1814,7 +1814,7 @@
  9574. */
  9575. hp = (struct fcoe_hdr *) skb_network_header(skb);
  9576. - stats = per_cpu_ptr(lport->stats, get_cpu());
  9577. + stats = per_cpu_ptr(lport->stats, get_cpu_light());
  9578. if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
  9579. if (stats->ErrorFrames < 5)
  9580. printk(KERN_WARNING "fcoe: FCoE version "
  9581. @@ -1846,13 +1846,13 @@
  9582. goto drop;
  9583. if (!fcoe_filter_frames(lport, fp)) {
  9584. - put_cpu();
  9585. + put_cpu_light();
  9586. fc_exch_recv(lport, fp);
  9587. return;
  9588. }
  9589. drop:
  9590. stats->ErrorFrames++;
  9591. - put_cpu();
  9592. + put_cpu_light();
  9593. kfree_skb(skb);
  9594. }
  9595. diff -Nur linux-4.4.46.orig/drivers/scsi/fcoe/fcoe_ctlr.c linux-4.4.46/drivers/scsi/fcoe/fcoe_ctlr.c
  9596. --- linux-4.4.46.orig/drivers/scsi/fcoe/fcoe_ctlr.c 2017-02-01 08:31:11.000000000 +0100
  9597. +++ linux-4.4.46/drivers/scsi/fcoe/fcoe_ctlr.c 2017-02-03 17:18:05.695416949 +0100
  9598. @@ -831,7 +831,7 @@
  9599. INIT_LIST_HEAD(&del_list);
  9600. - stats = per_cpu_ptr(fip->lp->stats, get_cpu());
  9601. + stats = per_cpu_ptr(fip->lp->stats, get_cpu_light());
  9602. list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
  9603. deadline = fcf->time + fcf->fka_period + fcf->fka_period / 2;
  9604. @@ -867,7 +867,7 @@
  9605. sel_time = fcf->time;
  9606. }
  9607. }
  9608. - put_cpu();
  9609. + put_cpu_light();
  9610. list_for_each_entry_safe(fcf, next, &del_list, list) {
  9611. /* Removes fcf from current list */
  9612. diff -Nur linux-4.4.46.orig/drivers/scsi/libfc/fc_exch.c linux-4.4.46/drivers/scsi/libfc/fc_exch.c
  9613. --- linux-4.4.46.orig/drivers/scsi/libfc/fc_exch.c 2017-02-01 08:31:11.000000000 +0100
  9614. +++ linux-4.4.46/drivers/scsi/libfc/fc_exch.c 2017-02-03 17:18:05.695416949 +0100
  9615. @@ -814,10 +814,10 @@
  9616. }
  9617. memset(ep, 0, sizeof(*ep));
  9618. - cpu = get_cpu();
  9619. + cpu = get_cpu_light();
  9620. pool = per_cpu_ptr(mp->pool, cpu);
  9621. spin_lock_bh(&pool->lock);
  9622. - put_cpu();
  9623. + put_cpu_light();
  9624. /* peek cache of free slot */
  9625. if (pool->left != FC_XID_UNKNOWN) {
  9626. diff -Nur linux-4.4.46.orig/drivers/scsi/libsas/sas_ata.c linux-4.4.46/drivers/scsi/libsas/sas_ata.c
  9627. --- linux-4.4.46.orig/drivers/scsi/libsas/sas_ata.c 2017-02-01 08:31:11.000000000 +0100
  9628. +++ linux-4.4.46/drivers/scsi/libsas/sas_ata.c 2017-02-03 17:18:05.695416949 +0100
  9629. @@ -190,7 +190,7 @@
  9630. /* TODO: audit callers to ensure they are ready for qc_issue to
  9631. * unconditionally re-enable interrupts
  9632. */
  9633. - local_irq_save(flags);
  9634. + local_irq_save_nort(flags);
  9635. spin_unlock(ap->lock);
  9636. /* If the device fell off, no sense in issuing commands */
  9637. @@ -255,7 +255,7 @@
  9638. out:
  9639. spin_lock(ap->lock);
  9640. - local_irq_restore(flags);
  9641. + local_irq_restore_nort(flags);
  9642. return ret;
  9643. }
  9644. diff -Nur linux-4.4.46.orig/drivers/scsi/qla2xxx/qla_inline.h linux-4.4.46/drivers/scsi/qla2xxx/qla_inline.h
  9645. --- linux-4.4.46.orig/drivers/scsi/qla2xxx/qla_inline.h 2017-02-01 08:31:11.000000000 +0100
  9646. +++ linux-4.4.46/drivers/scsi/qla2xxx/qla_inline.h 2017-02-03 17:18:05.695416949 +0100
  9647. @@ -59,12 +59,12 @@
  9648. {
  9649. unsigned long flags;
  9650. struct qla_hw_data *ha = rsp->hw;
  9651. - local_irq_save(flags);
  9652. + local_irq_save_nort(flags);
  9653. if (IS_P3P_TYPE(ha))
  9654. qla82xx_poll(0, rsp);
  9655. else
  9656. ha->isp_ops->intr_handler(0, rsp);
  9657. - local_irq_restore(flags);
  9658. + local_irq_restore_nort(flags);
  9659. }
  9660. static inline uint8_t *
  9661. diff -Nur linux-4.4.46.orig/drivers/thermal/x86_pkg_temp_thermal.c linux-4.4.46/drivers/thermal/x86_pkg_temp_thermal.c
  9662. --- linux-4.4.46.orig/drivers/thermal/x86_pkg_temp_thermal.c 2017-02-01 08:31:11.000000000 +0100
  9663. +++ linux-4.4.46/drivers/thermal/x86_pkg_temp_thermal.c 2017-02-03 17:18:05.695416949 +0100
  9664. @@ -29,6 +29,7 @@
  9665. #include <linux/pm.h>
  9666. #include <linux/thermal.h>
  9667. #include <linux/debugfs.h>
  9668. +#include <linux/swork.h>
  9669. #include <asm/cpu_device_id.h>
  9670. #include <asm/mce.h>
  9671. @@ -352,7 +353,7 @@
  9672. }
  9673. }
  9674. -static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  9675. +static void platform_thermal_notify_work(struct swork_event *event)
  9676. {
  9677. unsigned long flags;
  9678. int cpu = smp_processor_id();
  9679. @@ -369,7 +370,7 @@
  9680. pkg_work_scheduled[phy_id]) {
  9681. disable_pkg_thres_interrupt();
  9682. spin_unlock_irqrestore(&pkg_work_lock, flags);
  9683. - return -EINVAL;
  9684. + return;
  9685. }
  9686. pkg_work_scheduled[phy_id] = 1;
  9687. spin_unlock_irqrestore(&pkg_work_lock, flags);
  9688. @@ -378,9 +379,48 @@
  9689. schedule_delayed_work_on(cpu,
  9690. &per_cpu(pkg_temp_thermal_threshold_work, cpu),
  9691. msecs_to_jiffies(notify_delay_ms));
  9692. +}
  9693. +
  9694. +#ifdef CONFIG_PREEMPT_RT_FULL
  9695. +static struct swork_event notify_work;
  9696. +
  9697. +static int thermal_notify_work_init(void)
  9698. +{
  9699. + int err;
  9700. +
  9701. + err = swork_get();
  9702. + if (err)
  9703. + return err;
  9704. +
  9705. + INIT_SWORK(&notify_work, platform_thermal_notify_work);
  9706. return 0;
  9707. }
  9708. +static void thermal_notify_work_cleanup(void)
  9709. +{
  9710. + swork_put();
  9711. +}
  9712. +
  9713. +static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  9714. +{
  9715. + swork_queue(&notify_work);
  9716. + return 0;
  9717. +}
  9718. +
  9719. +#else /* !CONFIG_PREEMPT_RT_FULL */
  9720. +
  9721. +static int thermal_notify_work_init(void) { return 0; }
  9722. +
  9723. +static void thermal_notify_work_cleanup(void) { }
  9724. +
  9725. +static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  9726. +{
  9727. + platform_thermal_notify_work(NULL);
  9728. +
  9729. + return 0;
  9730. +}
  9731. +#endif /* CONFIG_PREEMPT_RT_FULL */
  9732. +
  9733. static int find_siblings_cpu(int cpu)
  9734. {
  9735. int i;
  9736. @@ -584,6 +624,9 @@
  9737. if (!x86_match_cpu(pkg_temp_thermal_ids))
  9738. return -ENODEV;
  9739. + if (!thermal_notify_work_init())
  9740. + return -ENODEV;
  9741. +
  9742. spin_lock_init(&pkg_work_lock);
  9743. platform_thermal_package_notify =
  9744. pkg_temp_thermal_platform_thermal_notify;
  9745. @@ -608,7 +651,7 @@
  9746. kfree(pkg_work_scheduled);
  9747. platform_thermal_package_notify = NULL;
  9748. platform_thermal_package_rate_control = NULL;
  9749. -
  9750. + thermal_notify_work_cleanup();
  9751. return -ENODEV;
  9752. }
  9753. @@ -633,6 +676,7 @@
  9754. mutex_unlock(&phy_dev_list_mutex);
  9755. platform_thermal_package_notify = NULL;
  9756. platform_thermal_package_rate_control = NULL;
  9757. + thermal_notify_work_cleanup();
  9758. for_each_online_cpu(i)
  9759. cancel_delayed_work_sync(
  9760. &per_cpu(pkg_temp_thermal_threshold_work, i));
  9761. diff -Nur linux-4.4.46.orig/drivers/tty/serial/8250/8250_core.c linux-4.4.46/drivers/tty/serial/8250/8250_core.c
  9762. --- linux-4.4.46.orig/drivers/tty/serial/8250/8250_core.c 2017-02-01 08:31:11.000000000 +0100
  9763. +++ linux-4.4.46/drivers/tty/serial/8250/8250_core.c 2017-02-03 17:18:05.695416949 +0100
  9764. @@ -58,7 +58,16 @@
  9765. static unsigned int skip_txen_test; /* force skip of txen test at init time */
  9766. -#define PASS_LIMIT 512
  9767. +/*
  9768. + * On -rt we can have a more delays, and legitimately
  9769. + * so - so don't drop work spuriously and spam the
  9770. + * syslog:
  9771. + */
  9772. +#ifdef CONFIG_PREEMPT_RT_FULL
  9773. +# define PASS_LIMIT 1000000
  9774. +#else
  9775. +# define PASS_LIMIT 512
  9776. +#endif
  9777. #include <asm/serial.h>
  9778. /*
  9779. diff -Nur linux-4.4.46.orig/drivers/tty/serial/8250/8250_port.c linux-4.4.46/drivers/tty/serial/8250/8250_port.c
  9780. --- linux-4.4.46.orig/drivers/tty/serial/8250/8250_port.c 2017-02-01 08:31:11.000000000 +0100
  9781. +++ linux-4.4.46/drivers/tty/serial/8250/8250_port.c 2017-02-03 17:18:05.695416949 +0100
  9782. @@ -35,6 +35,7 @@
  9783. #include <linux/nmi.h>
  9784. #include <linux/mutex.h>
  9785. #include <linux/slab.h>
  9786. +#include <linux/kdb.h>
  9787. #include <linux/uaccess.h>
  9788. #include <linux/pm_runtime.h>
  9789. @@ -2843,9 +2844,9 @@
  9790. serial8250_rpm_get(up);
  9791. - if (port->sysrq)
  9792. + if (port->sysrq || oops_in_progress)
  9793. locked = 0;
  9794. - else if (oops_in_progress)
  9795. + else if (in_kdb_printk())
  9796. locked = spin_trylock_irqsave(&port->lock, flags);
  9797. else
  9798. spin_lock_irqsave(&port->lock, flags);
  9799. diff -Nur linux-4.4.46.orig/drivers/tty/serial/amba-pl011.c linux-4.4.46/drivers/tty/serial/amba-pl011.c
  9800. --- linux-4.4.46.orig/drivers/tty/serial/amba-pl011.c 2017-02-01 08:31:11.000000000 +0100
  9801. +++ linux-4.4.46/drivers/tty/serial/amba-pl011.c 2017-02-03 17:18:05.695416949 +0100
  9802. @@ -2067,13 +2067,19 @@
  9803. clk_enable(uap->clk);
  9804. - local_irq_save(flags);
  9805. + /*
  9806. + * local_irq_save(flags);
  9807. + *
  9808. + * This local_irq_save() is nonsense. If we come in via sysrq
  9809. + * handling then interrupts are already disabled. Aside of
  9810. + * that the port.sysrq check is racy on SMP regardless.
  9811. + */
  9812. if (uap->port.sysrq)
  9813. locked = 0;
  9814. else if (oops_in_progress)
  9815. - locked = spin_trylock(&uap->port.lock);
  9816. + locked = spin_trylock_irqsave(&uap->port.lock, flags);
  9817. else
  9818. - spin_lock(&uap->port.lock);
  9819. + spin_lock_irqsave(&uap->port.lock, flags);
  9820. /*
  9821. * First save the CR then disable the interrupts
  9822. @@ -2098,8 +2104,7 @@
  9823. writew(old_cr, uap->port.membase + UART011_CR);
  9824. if (locked)
  9825. - spin_unlock(&uap->port.lock);
  9826. - local_irq_restore(flags);
  9827. + spin_unlock_irqrestore(&uap->port.lock, flags);
  9828. clk_disable(uap->clk);
  9829. }
  9830. diff -Nur linux-4.4.46.orig/drivers/tty/serial/omap-serial.c linux-4.4.46/drivers/tty/serial/omap-serial.c
  9831. --- linux-4.4.46.orig/drivers/tty/serial/omap-serial.c 2017-02-01 08:31:11.000000000 +0100
  9832. +++ linux-4.4.46/drivers/tty/serial/omap-serial.c 2017-02-03 17:18:05.695416949 +0100
  9833. @@ -1257,13 +1257,10 @@
  9834. pm_runtime_get_sync(up->dev);
  9835. - local_irq_save(flags);
  9836. - if (up->port.sysrq)
  9837. - locked = 0;
  9838. - else if (oops_in_progress)
  9839. - locked = spin_trylock(&up->port.lock);
  9840. + if (up->port.sysrq || oops_in_progress)
  9841. + locked = spin_trylock_irqsave(&up->port.lock, flags);
  9842. else
  9843. - spin_lock(&up->port.lock);
  9844. + spin_lock_irqsave(&up->port.lock, flags);
  9845. /*
  9846. * First save the IER then disable the interrupts
  9847. @@ -1292,8 +1289,7 @@
  9848. pm_runtime_mark_last_busy(up->dev);
  9849. pm_runtime_put_autosuspend(up->dev);
  9850. if (locked)
  9851. - spin_unlock(&up->port.lock);
  9852. - local_irq_restore(flags);
  9853. + spin_unlock_irqrestore(&up->port.lock, flags);
  9854. }
  9855. static int __init
  9856. diff -Nur linux-4.4.46.orig/drivers/usb/core/hcd.c linux-4.4.46/drivers/usb/core/hcd.c
  9857. --- linux-4.4.46.orig/drivers/usb/core/hcd.c 2017-02-01 08:31:11.000000000 +0100
  9858. +++ linux-4.4.46/drivers/usb/core/hcd.c 2017-02-03 17:18:10.895617822 +0100
  9859. @@ -1735,9 +1735,9 @@
  9860. * and no one may trigger the above deadlock situation when
  9861. * running complete() in tasklet.
  9862. */
  9863. - local_irq_save(flags);
  9864. + local_irq_save_nort(flags);
  9865. urb->complete(urb);
  9866. - local_irq_restore(flags);
  9867. + local_irq_restore_nort(flags);
  9868. usb_anchor_resume_wakeups(anchor);
  9869. atomic_dec(&urb->use_count);
  9870. diff -Nur linux-4.4.46.orig/drivers/usb/gadget/function/f_fs.c linux-4.4.46/drivers/usb/gadget/function/f_fs.c
  9871. --- linux-4.4.46.orig/drivers/usb/gadget/function/f_fs.c 2017-02-01 08:31:11.000000000 +0100
  9872. +++ linux-4.4.46/drivers/usb/gadget/function/f_fs.c 2017-02-03 17:18:10.895617822 +0100
  9873. @@ -1404,7 +1404,7 @@
  9874. pr_info("%s(): freeing\n", __func__);
  9875. ffs_data_clear(ffs);
  9876. BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
  9877. - waitqueue_active(&ffs->ep0req_completion.wait));
  9878. + swait_active(&ffs->ep0req_completion.wait));
  9879. kfree(ffs->dev_name);
  9880. kfree(ffs);
  9881. }
  9882. diff -Nur linux-4.4.46.orig/drivers/usb/gadget/legacy/inode.c linux-4.4.46/drivers/usb/gadget/legacy/inode.c
  9883. --- linux-4.4.46.orig/drivers/usb/gadget/legacy/inode.c 2017-02-01 08:31:11.000000000 +0100
  9884. +++ linux-4.4.46/drivers/usb/gadget/legacy/inode.c 2017-02-03 17:18:10.895617822 +0100
  9885. @@ -345,7 +345,7 @@
  9886. spin_unlock_irq (&epdata->dev->lock);
  9887. if (likely (value == 0)) {
  9888. - value = wait_event_interruptible (done.wait, done.done);
  9889. + value = swait_event_interruptible (done.wait, done.done);
  9890. if (value != 0) {
  9891. spin_lock_irq (&epdata->dev->lock);
  9892. if (likely (epdata->ep != NULL)) {
  9893. @@ -354,7 +354,7 @@
  9894. usb_ep_dequeue (epdata->ep, epdata->req);
  9895. spin_unlock_irq (&epdata->dev->lock);
  9896. - wait_event (done.wait, done.done);
  9897. + swait_event (done.wait, done.done);
  9898. if (epdata->status == -ECONNRESET)
  9899. epdata->status = -EINTR;
  9900. } else {
  9901. diff -Nur linux-4.4.46.orig/drivers/usb/gadget/udc/atmel_usba_udc.c linux-4.4.46/drivers/usb/gadget/udc/atmel_usba_udc.c
  9902. --- linux-4.4.46.orig/drivers/usb/gadget/udc/atmel_usba_udc.c 2017-02-01 08:31:11.000000000 +0100
  9903. +++ linux-4.4.46/drivers/usb/gadget/udc/atmel_usba_udc.c 2017-02-03 17:18:10.895617822 +0100
  9904. @@ -17,7 +17,9 @@
  9905. #include <linux/device.h>
  9906. #include <linux/dma-mapping.h>
  9907. #include <linux/list.h>
  9908. +#include <linux/mfd/syscon.h>
  9909. #include <linux/platform_device.h>
  9910. +#include <linux/regmap.h>
  9911. #include <linux/usb/ch9.h>
  9912. #include <linux/usb/gadget.h>
  9913. #include <linux/usb/atmel_usba_udc.h>
  9914. @@ -1888,20 +1890,15 @@
  9915. #ifdef CONFIG_OF
  9916. static void at91sam9rl_toggle_bias(struct usba_udc *udc, int is_on)
  9917. {
  9918. - unsigned int uckr = at91_pmc_read(AT91_CKGR_UCKR);
  9919. -
  9920. - if (is_on)
  9921. - at91_pmc_write(AT91_CKGR_UCKR, uckr | AT91_PMC_BIASEN);
  9922. - else
  9923. - at91_pmc_write(AT91_CKGR_UCKR, uckr & ~(AT91_PMC_BIASEN));
  9924. + regmap_update_bits(udc->pmc, AT91_CKGR_UCKR, AT91_PMC_BIASEN,
  9925. + is_on ? AT91_PMC_BIASEN : 0);
  9926. }
  9927. static void at91sam9g45_pulse_bias(struct usba_udc *udc)
  9928. {
  9929. - unsigned int uckr = at91_pmc_read(AT91_CKGR_UCKR);
  9930. -
  9931. - at91_pmc_write(AT91_CKGR_UCKR, uckr & ~(AT91_PMC_BIASEN));
  9932. - at91_pmc_write(AT91_CKGR_UCKR, uckr | AT91_PMC_BIASEN);
  9933. + regmap_update_bits(udc->pmc, AT91_CKGR_UCKR, AT91_PMC_BIASEN, 0);
  9934. + regmap_update_bits(udc->pmc, AT91_CKGR_UCKR, AT91_PMC_BIASEN,
  9935. + AT91_PMC_BIASEN);
  9936. }
  9937. static const struct usba_udc_errata at91sam9rl_errata = {
  9938. @@ -1938,6 +1935,9 @@
  9939. return ERR_PTR(-EINVAL);
  9940. udc->errata = match->data;
  9941. + udc->pmc = syscon_regmap_lookup_by_compatible("atmel,at91sam9g45-pmc");
  9942. + if (udc->errata && IS_ERR(udc->pmc))
  9943. + return ERR_CAST(udc->pmc);
  9944. udc->num_ep = 0;
  9945. diff -Nur linux-4.4.46.orig/drivers/usb/gadget/udc/atmel_usba_udc.h linux-4.4.46/drivers/usb/gadget/udc/atmel_usba_udc.h
  9946. --- linux-4.4.46.orig/drivers/usb/gadget/udc/atmel_usba_udc.h 2017-02-01 08:31:11.000000000 +0100
  9947. +++ linux-4.4.46/drivers/usb/gadget/udc/atmel_usba_udc.h 2017-02-03 17:18:10.895617822 +0100
  9948. @@ -354,6 +354,8 @@
  9949. struct dentry *debugfs_root;
  9950. struct dentry *debugfs_regs;
  9951. #endif
  9952. +
  9953. + struct regmap *pmc;
  9954. };
  9955. static inline struct usba_ep *to_usba_ep(struct usb_ep *ep)
  9956. diff -Nur linux-4.4.46.orig/fs/aio.c linux-4.4.46/fs/aio.c
  9957. --- linux-4.4.46.orig/fs/aio.c 2017-02-01 08:31:11.000000000 +0100
  9958. +++ linux-4.4.46/fs/aio.c 2017-02-03 17:18:10.899617976 +0100
  9959. @@ -40,6 +40,7 @@
  9960. #include <linux/ramfs.h>
  9961. #include <linux/percpu-refcount.h>
  9962. #include <linux/mount.h>
  9963. +#include <linux/swork.h>
  9964. #include <asm/kmap_types.h>
  9965. #include <asm/uaccess.h>
  9966. @@ -115,7 +116,7 @@
  9967. struct page **ring_pages;
  9968. long nr_pages;
  9969. - struct work_struct free_work;
  9970. + struct swork_event free_work;
  9971. /*
  9972. * signals when all in-flight requests are done
  9973. @@ -258,6 +259,7 @@
  9974. .mount = aio_mount,
  9975. .kill_sb = kill_anon_super,
  9976. };
  9977. + BUG_ON(swork_get());
  9978. aio_mnt = kern_mount(&aio_fs);
  9979. if (IS_ERR(aio_mnt))
  9980. panic("Failed to create aio fs mount.");
  9981. @@ -573,9 +575,9 @@
  9982. return cancel(&kiocb->common);
  9983. }
  9984. -static void free_ioctx(struct work_struct *work)
  9985. +static void free_ioctx(struct swork_event *sev)
  9986. {
  9987. - struct kioctx *ctx = container_of(work, struct kioctx, free_work);
  9988. + struct kioctx *ctx = container_of(sev, struct kioctx, free_work);
  9989. pr_debug("freeing %p\n", ctx);
  9990. @@ -594,8 +596,8 @@
  9991. if (ctx->rq_wait && atomic_dec_and_test(&ctx->rq_wait->count))
  9992. complete(&ctx->rq_wait->comp);
  9993. - INIT_WORK(&ctx->free_work, free_ioctx);
  9994. - schedule_work(&ctx->free_work);
  9995. + INIT_SWORK(&ctx->free_work, free_ioctx);
  9996. + swork_queue(&ctx->free_work);
  9997. }
  9998. /*
  9999. @@ -603,9 +605,9 @@
  10000. * and ctx->users has dropped to 0, so we know no more kiocbs can be submitted -
  10001. * now it's safe to cancel any that need to be.
  10002. */
  10003. -static void free_ioctx_users(struct percpu_ref *ref)
  10004. +static void free_ioctx_users_work(struct swork_event *sev)
  10005. {
  10006. - struct kioctx *ctx = container_of(ref, struct kioctx, users);
  10007. + struct kioctx *ctx = container_of(sev, struct kioctx, free_work);
  10008. struct aio_kiocb *req;
  10009. spin_lock_irq(&ctx->ctx_lock);
  10010. @@ -624,6 +626,14 @@
  10011. percpu_ref_put(&ctx->reqs);
  10012. }
  10013. +static void free_ioctx_users(struct percpu_ref *ref)
  10014. +{
  10015. + struct kioctx *ctx = container_of(ref, struct kioctx, users);
  10016. +
  10017. + INIT_SWORK(&ctx->free_work, free_ioctx_users_work);
  10018. + swork_queue(&ctx->free_work);
  10019. +}
  10020. +
  10021. static int ioctx_add_table(struct kioctx *ctx, struct mm_struct *mm)
  10022. {
  10023. unsigned i, new_nr;
  10024. diff -Nur linux-4.4.46.orig/fs/autofs4/autofs_i.h linux-4.4.46/fs/autofs4/autofs_i.h
  10025. --- linux-4.4.46.orig/fs/autofs4/autofs_i.h 2017-02-01 08:31:11.000000000 +0100
  10026. +++ linux-4.4.46/fs/autofs4/autofs_i.h 2017-02-03 17:18:10.899617976 +0100
  10027. @@ -34,6 +34,7 @@
  10028. #include <linux/sched.h>
  10029. #include <linux/mount.h>
  10030. #include <linux/namei.h>
  10031. +#include <linux/delay.h>
  10032. #include <asm/current.h>
  10033. #include <asm/uaccess.h>
  10034. diff -Nur linux-4.4.46.orig/fs/autofs4/expire.c linux-4.4.46/fs/autofs4/expire.c
  10035. --- linux-4.4.46.orig/fs/autofs4/expire.c 2017-02-01 08:31:11.000000000 +0100
  10036. +++ linux-4.4.46/fs/autofs4/expire.c 2017-02-03 17:18:10.899617976 +0100
  10037. @@ -150,7 +150,7 @@
  10038. parent = p->d_parent;
  10039. if (!spin_trylock(&parent->d_lock)) {
  10040. spin_unlock(&p->d_lock);
  10041. - cpu_relax();
  10042. + cpu_chill();
  10043. goto relock;
  10044. }
  10045. spin_unlock(&p->d_lock);
  10046. diff -Nur linux-4.4.46.orig/fs/buffer.c linux-4.4.46/fs/buffer.c
  10047. --- linux-4.4.46.orig/fs/buffer.c 2017-02-01 08:31:11.000000000 +0100
  10048. +++ linux-4.4.46/fs/buffer.c 2017-02-03 17:18:10.899617976 +0100
  10049. @@ -305,8 +305,7 @@
  10050. * decide that the page is now completely done.
  10051. */
  10052. first = page_buffers(page);
  10053. - local_irq_save(flags);
  10054. - bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  10055. + flags = bh_uptodate_lock_irqsave(first);
  10056. clear_buffer_async_read(bh);
  10057. unlock_buffer(bh);
  10058. tmp = bh;
  10059. @@ -319,8 +318,7 @@
  10060. }
  10061. tmp = tmp->b_this_page;
  10062. } while (tmp != bh);
  10063. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  10064. - local_irq_restore(flags);
  10065. + bh_uptodate_unlock_irqrestore(first, flags);
  10066. /*
  10067. * If none of the buffers had errors and they are all
  10068. @@ -332,9 +330,7 @@
  10069. return;
  10070. still_busy:
  10071. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  10072. - local_irq_restore(flags);
  10073. - return;
  10074. + bh_uptodate_unlock_irqrestore(first, flags);
  10075. }
  10076. /*
  10077. @@ -362,8 +358,7 @@
  10078. }
  10079. first = page_buffers(page);
  10080. - local_irq_save(flags);
  10081. - bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  10082. + flags = bh_uptodate_lock_irqsave(first);
  10083. clear_buffer_async_write(bh);
  10084. unlock_buffer(bh);
  10085. @@ -375,15 +370,12 @@
  10086. }
  10087. tmp = tmp->b_this_page;
  10088. }
  10089. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  10090. - local_irq_restore(flags);
  10091. + bh_uptodate_unlock_irqrestore(first, flags);
  10092. end_page_writeback(page);
  10093. return;
  10094. still_busy:
  10095. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  10096. - local_irq_restore(flags);
  10097. - return;
  10098. + bh_uptodate_unlock_irqrestore(first, flags);
  10099. }
  10100. EXPORT_SYMBOL(end_buffer_async_write);
  10101. @@ -3325,6 +3317,7 @@
  10102. struct buffer_head *ret = kmem_cache_zalloc(bh_cachep, gfp_flags);
  10103. if (ret) {
  10104. INIT_LIST_HEAD(&ret->b_assoc_buffers);
  10105. + buffer_head_init_locks(ret);
  10106. preempt_disable();
  10107. __this_cpu_inc(bh_accounting.nr);
  10108. recalc_bh_state();
  10109. diff -Nur linux-4.4.46.orig/fs/dcache.c linux-4.4.46/fs/dcache.c
  10110. --- linux-4.4.46.orig/fs/dcache.c 2017-02-01 08:31:11.000000000 +0100
  10111. +++ linux-4.4.46/fs/dcache.c 2017-02-03 17:18:10.899617976 +0100
  10112. @@ -19,6 +19,7 @@
  10113. #include <linux/mm.h>
  10114. #include <linux/fs.h>
  10115. #include <linux/fsnotify.h>
  10116. +#include <linux/delay.h>
  10117. #include <linux/slab.h>
  10118. #include <linux/init.h>
  10119. #include <linux/hash.h>
  10120. @@ -747,6 +748,8 @@
  10121. */
  10122. void dput(struct dentry *dentry)
  10123. {
  10124. + struct dentry *parent;
  10125. +
  10126. if (unlikely(!dentry))
  10127. return;
  10128. @@ -783,9 +786,18 @@
  10129. return;
  10130. kill_it:
  10131. - dentry = dentry_kill(dentry);
  10132. - if (dentry) {
  10133. - cond_resched();
  10134. + parent = dentry_kill(dentry);
  10135. + if (parent) {
  10136. + int r;
  10137. +
  10138. + if (parent == dentry) {
  10139. + /* the task with the highest priority won't schedule */
  10140. + r = cond_resched();
  10141. + if (!r)
  10142. + cpu_chill();
  10143. + } else {
  10144. + dentry = parent;
  10145. + }
  10146. goto repeat;
  10147. }
  10148. }
  10149. @@ -2397,7 +2409,7 @@
  10150. if (dentry->d_lockref.count == 1) {
  10151. if (!spin_trylock(&inode->i_lock)) {
  10152. spin_unlock(&dentry->d_lock);
  10153. - cpu_relax();
  10154. + cpu_chill();
  10155. goto again;
  10156. }
  10157. dentry->d_flags &= ~DCACHE_CANT_MOUNT;
  10158. diff -Nur linux-4.4.46.orig/fs/eventpoll.c linux-4.4.46/fs/eventpoll.c
  10159. --- linux-4.4.46.orig/fs/eventpoll.c 2017-02-01 08:31:11.000000000 +0100
  10160. +++ linux-4.4.46/fs/eventpoll.c 2017-02-03 17:18:10.899617976 +0100
  10161. @@ -505,12 +505,12 @@
  10162. */
  10163. static void ep_poll_safewake(wait_queue_head_t *wq)
  10164. {
  10165. - int this_cpu = get_cpu();
  10166. + int this_cpu = get_cpu_light();
  10167. ep_call_nested(&poll_safewake_ncalls, EP_MAX_NESTS,
  10168. ep_poll_wakeup_proc, NULL, wq, (void *) (long) this_cpu);
  10169. - put_cpu();
  10170. + put_cpu_light();
  10171. }
  10172. static void ep_remove_wait_queue(struct eppoll_entry *pwq)
  10173. diff -Nur linux-4.4.46.orig/fs/exec.c linux-4.4.46/fs/exec.c
  10174. --- linux-4.4.46.orig/fs/exec.c 2017-02-01 08:31:11.000000000 +0100
  10175. +++ linux-4.4.46/fs/exec.c 2017-02-03 17:18:10.899617976 +0100
  10176. @@ -866,12 +866,14 @@
  10177. }
  10178. }
  10179. task_lock(tsk);
  10180. + preempt_disable_rt();
  10181. active_mm = tsk->active_mm;
  10182. tsk->mm = mm;
  10183. tsk->active_mm = mm;
  10184. activate_mm(active_mm, mm);
  10185. tsk->mm->vmacache_seqnum = 0;
  10186. vmacache_flush(tsk);
  10187. + preempt_enable_rt();
  10188. task_unlock(tsk);
  10189. if (old_mm) {
  10190. up_read(&old_mm->mmap_sem);
  10191. diff -Nur linux-4.4.46.orig/fs/f2fs/f2fs.h linux-4.4.46/fs/f2fs/f2fs.h
  10192. --- linux-4.4.46.orig/fs/f2fs/f2fs.h 2017-02-01 08:31:11.000000000 +0100
  10193. +++ linux-4.4.46/fs/f2fs/f2fs.h 2017-02-03 17:18:10.899617976 +0100
  10194. @@ -24,7 +24,6 @@
  10195. #ifdef CONFIG_F2FS_CHECK_FS
  10196. #define f2fs_bug_on(sbi, condition) BUG_ON(condition)
  10197. -#define f2fs_down_write(x, y) down_write_nest_lock(x, y)
  10198. #else
  10199. #define f2fs_bug_on(sbi, condition) \
  10200. do { \
  10201. @@ -33,7 +32,6 @@
  10202. set_sbi_flag(sbi, SBI_NEED_FSCK); \
  10203. } \
  10204. } while (0)
  10205. -#define f2fs_down_write(x, y) down_write(x)
  10206. #endif
  10207. /*
  10208. @@ -959,7 +957,7 @@
  10209. static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
  10210. {
  10211. - f2fs_down_write(&sbi->cp_rwsem, &sbi->cp_mutex);
  10212. + down_write(&sbi->cp_rwsem);
  10213. }
  10214. static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
  10215. diff -Nur linux-4.4.46.orig/fs/jbd2/checkpoint.c linux-4.4.46/fs/jbd2/checkpoint.c
  10216. --- linux-4.4.46.orig/fs/jbd2/checkpoint.c 2017-02-01 08:31:11.000000000 +0100
  10217. +++ linux-4.4.46/fs/jbd2/checkpoint.c 2017-02-03 17:18:10.899617976 +0100
  10218. @@ -116,6 +116,8 @@
  10219. nblocks = jbd2_space_needed(journal);
  10220. while (jbd2_log_space_left(journal) < nblocks) {
  10221. write_unlock(&journal->j_state_lock);
  10222. + if (current->plug)
  10223. + io_schedule();
  10224. mutex_lock(&journal->j_checkpoint_mutex);
  10225. /*
  10226. diff -Nur linux-4.4.46.orig/fs/namespace.c linux-4.4.46/fs/namespace.c
  10227. --- linux-4.4.46.orig/fs/namespace.c 2017-02-01 08:31:11.000000000 +0100
  10228. +++ linux-4.4.46/fs/namespace.c 2017-02-03 17:18:10.899617976 +0100
  10229. @@ -14,6 +14,7 @@
  10230. #include <linux/mnt_namespace.h>
  10231. #include <linux/user_namespace.h>
  10232. #include <linux/namei.h>
  10233. +#include <linux/delay.h>
  10234. #include <linux/security.h>
  10235. #include <linux/idr.h>
  10236. #include <linux/init.h> /* init_rootfs */
  10237. @@ -353,8 +354,11 @@
  10238. * incremented count after it has set MNT_WRITE_HOLD.
  10239. */
  10240. smp_mb();
  10241. - while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD)
  10242. - cpu_relax();
  10243. + while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD) {
  10244. + preempt_enable();
  10245. + cpu_chill();
  10246. + preempt_disable();
  10247. + }
  10248. /*
  10249. * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
  10250. * be set to match its requirements. So we must not load that until
  10251. diff -Nur linux-4.4.46.orig/fs/ntfs/aops.c linux-4.4.46/fs/ntfs/aops.c
  10252. --- linux-4.4.46.orig/fs/ntfs/aops.c 2017-02-01 08:31:11.000000000 +0100
  10253. +++ linux-4.4.46/fs/ntfs/aops.c 2017-02-03 17:18:10.899617976 +0100
  10254. @@ -107,8 +107,7 @@
  10255. "0x%llx.", (unsigned long long)bh->b_blocknr);
  10256. }
  10257. first = page_buffers(page);
  10258. - local_irq_save(flags);
  10259. - bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  10260. + flags = bh_uptodate_lock_irqsave(first);
  10261. clear_buffer_async_read(bh);
  10262. unlock_buffer(bh);
  10263. tmp = bh;
  10264. @@ -123,8 +122,7 @@
  10265. }
  10266. tmp = tmp->b_this_page;
  10267. } while (tmp != bh);
  10268. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  10269. - local_irq_restore(flags);
  10270. + bh_uptodate_unlock_irqrestore(first, flags);
  10271. /*
  10272. * If none of the buffers had errors then we can set the page uptodate,
  10273. * but we first have to perform the post read mst fixups, if the
  10274. @@ -145,13 +143,13 @@
  10275. recs = PAGE_CACHE_SIZE / rec_size;
  10276. /* Should have been verified before we got here... */
  10277. BUG_ON(!recs);
  10278. - local_irq_save(flags);
  10279. + local_irq_save_nort(flags);
  10280. kaddr = kmap_atomic(page);
  10281. for (i = 0; i < recs; i++)
  10282. post_read_mst_fixup((NTFS_RECORD*)(kaddr +
  10283. i * rec_size), rec_size);
  10284. kunmap_atomic(kaddr);
  10285. - local_irq_restore(flags);
  10286. + local_irq_restore_nort(flags);
  10287. flush_dcache_page(page);
  10288. if (likely(page_uptodate && !PageError(page)))
  10289. SetPageUptodate(page);
  10290. @@ -159,9 +157,7 @@
  10291. unlock_page(page);
  10292. return;
  10293. still_busy:
  10294. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  10295. - local_irq_restore(flags);
  10296. - return;
  10297. + bh_uptodate_unlock_irqrestore(first, flags);
  10298. }
  10299. /**
  10300. diff -Nur linux-4.4.46.orig/fs/timerfd.c linux-4.4.46/fs/timerfd.c
  10301. --- linux-4.4.46.orig/fs/timerfd.c 2017-02-01 08:31:11.000000000 +0100
  10302. +++ linux-4.4.46/fs/timerfd.c 2017-02-03 17:18:10.899617976 +0100
  10303. @@ -450,7 +450,10 @@
  10304. break;
  10305. }
  10306. spin_unlock_irq(&ctx->wqh.lock);
  10307. - cpu_relax();
  10308. + if (isalarm(ctx))
  10309. + hrtimer_wait_for_timer(&ctx->t.alarm.timer);
  10310. + else
  10311. + hrtimer_wait_for_timer(&ctx->t.tmr);
  10312. }
  10313. /*
  10314. diff -Nur linux-4.4.46.orig/include/acpi/platform/aclinux.h linux-4.4.46/include/acpi/platform/aclinux.h
  10315. --- linux-4.4.46.orig/include/acpi/platform/aclinux.h 2017-02-01 08:31:11.000000000 +0100
  10316. +++ linux-4.4.46/include/acpi/platform/aclinux.h 2017-02-03 17:18:10.899617976 +0100
  10317. @@ -127,6 +127,7 @@
  10318. #define acpi_cache_t struct kmem_cache
  10319. #define acpi_spinlock spinlock_t *
  10320. +#define acpi_raw_spinlock raw_spinlock_t *
  10321. #define acpi_cpu_flags unsigned long
  10322. /* Use native linux version of acpi_os_allocate_zeroed */
  10323. @@ -145,6 +146,20 @@
  10324. #define ACPI_USE_ALTERNATE_PROTOTYPE_acpi_os_get_thread_id
  10325. #define ACPI_USE_ALTERNATE_PROTOTYPE_acpi_os_create_lock
  10326. +#define acpi_os_create_raw_lock(__handle) \
  10327. +({ \
  10328. + raw_spinlock_t *lock = ACPI_ALLOCATE(sizeof(*lock)); \
  10329. + \
  10330. + if (lock) { \
  10331. + *(__handle) = lock; \
  10332. + raw_spin_lock_init(*(__handle)); \
  10333. + } \
  10334. + lock ? AE_OK : AE_NO_MEMORY; \
  10335. + })
  10336. +
  10337. +#define acpi_os_delete_raw_lock(__handle) kfree(__handle)
  10338. +
  10339. +
  10340. /*
  10341. * OSL interfaces used by debugger/disassembler
  10342. */
  10343. diff -Nur linux-4.4.46.orig/include/asm-generic/bug.h linux-4.4.46/include/asm-generic/bug.h
  10344. --- linux-4.4.46.orig/include/asm-generic/bug.h 2017-02-01 08:31:11.000000000 +0100
  10345. +++ linux-4.4.46/include/asm-generic/bug.h 2017-02-03 17:18:10.903618130 +0100
  10346. @@ -206,6 +206,20 @@
  10347. # define WARN_ON_SMP(x) ({0;})
  10348. #endif
  10349. +#ifdef CONFIG_PREEMPT_RT_BASE
  10350. +# define BUG_ON_RT(c) BUG_ON(c)
  10351. +# define BUG_ON_NONRT(c) do { } while (0)
  10352. +# define WARN_ON_RT(condition) WARN_ON(condition)
  10353. +# define WARN_ON_NONRT(condition) do { } while (0)
  10354. +# define WARN_ON_ONCE_NONRT(condition) do { } while (0)
  10355. +#else
  10356. +# define BUG_ON_RT(c) do { } while (0)
  10357. +# define BUG_ON_NONRT(c) BUG_ON(c)
  10358. +# define WARN_ON_RT(condition) do { } while (0)
  10359. +# define WARN_ON_NONRT(condition) WARN_ON(condition)
  10360. +# define WARN_ON_ONCE_NONRT(condition) WARN_ON_ONCE(condition)
  10361. +#endif
  10362. +
  10363. #endif /* __ASSEMBLY__ */
  10364. #endif
  10365. diff -Nur linux-4.4.46.orig/include/asm-generic/preempt.h linux-4.4.46/include/asm-generic/preempt.h
  10366. --- linux-4.4.46.orig/include/asm-generic/preempt.h 2017-02-01 08:31:11.000000000 +0100
  10367. +++ linux-4.4.46/include/asm-generic/preempt.h 2017-02-03 17:18:10.903618130 +0100
  10368. @@ -7,10 +7,10 @@
  10369. static __always_inline int preempt_count(void)
  10370. {
  10371. - return current_thread_info()->preempt_count;
  10372. + return READ_ONCE(current_thread_info()->preempt_count);
  10373. }
  10374. -static __always_inline int *preempt_count_ptr(void)
  10375. +static __always_inline volatile int *preempt_count_ptr(void)
  10376. {
  10377. return &current_thread_info()->preempt_count;
  10378. }
  10379. diff -Nur linux-4.4.46.orig/include/linux/blkdev.h linux-4.4.46/include/linux/blkdev.h
  10380. --- linux-4.4.46.orig/include/linux/blkdev.h 2017-02-01 08:31:11.000000000 +0100
  10381. +++ linux-4.4.46/include/linux/blkdev.h 2017-02-03 17:18:10.903618130 +0100
  10382. @@ -89,6 +89,7 @@
  10383. struct list_head queuelist;
  10384. union {
  10385. struct call_single_data csd;
  10386. + struct work_struct work;
  10387. unsigned long fifo_time;
  10388. };
  10389. @@ -455,7 +456,7 @@
  10390. struct throtl_data *td;
  10391. #endif
  10392. struct rcu_head rcu_head;
  10393. - wait_queue_head_t mq_freeze_wq;
  10394. + struct swait_queue_head mq_freeze_wq;
  10395. struct percpu_ref q_usage_counter;
  10396. struct list_head all_q_node;
  10397. diff -Nur linux-4.4.46.orig/include/linux/blk-mq.h linux-4.4.46/include/linux/blk-mq.h
  10398. --- linux-4.4.46.orig/include/linux/blk-mq.h 2017-02-01 08:31:11.000000000 +0100
  10399. +++ linux-4.4.46/include/linux/blk-mq.h 2017-02-03 17:18:10.903618130 +0100
  10400. @@ -212,6 +212,7 @@
  10401. struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *, const int ctx_index);
  10402. struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_tag_set *, unsigned int, int);
  10403. +void __blk_mq_complete_request_remote_work(struct work_struct *work);
  10404. int blk_mq_request_started(struct request *rq);
  10405. void blk_mq_start_request(struct request *rq);
  10406. diff -Nur linux-4.4.46.orig/include/linux/bottom_half.h linux-4.4.46/include/linux/bottom_half.h
  10407. --- linux-4.4.46.orig/include/linux/bottom_half.h 2017-02-01 08:31:11.000000000 +0100
  10408. +++ linux-4.4.46/include/linux/bottom_half.h 2017-02-03 17:18:10.903618130 +0100
  10409. @@ -3,6 +3,39 @@
  10410. #include <linux/preempt.h>
  10411. +#ifdef CONFIG_PREEMPT_RT_FULL
  10412. +
  10413. +extern void __local_bh_disable(void);
  10414. +extern void _local_bh_enable(void);
  10415. +extern void __local_bh_enable(void);
  10416. +
  10417. +static inline void local_bh_disable(void)
  10418. +{
  10419. + __local_bh_disable();
  10420. +}
  10421. +
  10422. +static inline void __local_bh_disable_ip(unsigned long ip, unsigned int cnt)
  10423. +{
  10424. + __local_bh_disable();
  10425. +}
  10426. +
  10427. +static inline void local_bh_enable(void)
  10428. +{
  10429. + __local_bh_enable();
  10430. +}
  10431. +
  10432. +static inline void __local_bh_enable_ip(unsigned long ip, unsigned int cnt)
  10433. +{
  10434. + __local_bh_enable();
  10435. +}
  10436. +
  10437. +static inline void local_bh_enable_ip(unsigned long ip)
  10438. +{
  10439. + __local_bh_enable();
  10440. +}
  10441. +
  10442. +#else
  10443. +
  10444. #ifdef CONFIG_TRACE_IRQFLAGS
  10445. extern void __local_bh_disable_ip(unsigned long ip, unsigned int cnt);
  10446. #else
  10447. @@ -30,5 +63,6 @@
  10448. {
  10449. __local_bh_enable_ip(_THIS_IP_, SOFTIRQ_DISABLE_OFFSET);
  10450. }
  10451. +#endif
  10452. #endif /* _LINUX_BH_H */
  10453. diff -Nur linux-4.4.46.orig/include/linux/buffer_head.h linux-4.4.46/include/linux/buffer_head.h
  10454. --- linux-4.4.46.orig/include/linux/buffer_head.h 2017-02-01 08:31:11.000000000 +0100
  10455. +++ linux-4.4.46/include/linux/buffer_head.h 2017-02-03 17:18:10.903618130 +0100
  10456. @@ -75,8 +75,50 @@
  10457. struct address_space *b_assoc_map; /* mapping this buffer is
  10458. associated with */
  10459. atomic_t b_count; /* users using this buffer_head */
  10460. +#ifdef CONFIG_PREEMPT_RT_BASE
  10461. + spinlock_t b_uptodate_lock;
  10462. +#if IS_ENABLED(CONFIG_JBD2)
  10463. + spinlock_t b_state_lock;
  10464. + spinlock_t b_journal_head_lock;
  10465. +#endif
  10466. +#endif
  10467. };
  10468. +static inline unsigned long bh_uptodate_lock_irqsave(struct buffer_head *bh)
  10469. +{
  10470. + unsigned long flags;
  10471. +
  10472. +#ifndef CONFIG_PREEMPT_RT_BASE
  10473. + local_irq_save(flags);
  10474. + bit_spin_lock(BH_Uptodate_Lock, &bh->b_state);
  10475. +#else
  10476. + spin_lock_irqsave(&bh->b_uptodate_lock, flags);
  10477. +#endif
  10478. + return flags;
  10479. +}
  10480. +
  10481. +static inline void
  10482. +bh_uptodate_unlock_irqrestore(struct buffer_head *bh, unsigned long flags)
  10483. +{
  10484. +#ifndef CONFIG_PREEMPT_RT_BASE
  10485. + bit_spin_unlock(BH_Uptodate_Lock, &bh->b_state);
  10486. + local_irq_restore(flags);
  10487. +#else
  10488. + spin_unlock_irqrestore(&bh->b_uptodate_lock, flags);
  10489. +#endif
  10490. +}
  10491. +
  10492. +static inline void buffer_head_init_locks(struct buffer_head *bh)
  10493. +{
  10494. +#ifdef CONFIG_PREEMPT_RT_BASE
  10495. + spin_lock_init(&bh->b_uptodate_lock);
  10496. +#if IS_ENABLED(CONFIG_JBD2)
  10497. + spin_lock_init(&bh->b_state_lock);
  10498. + spin_lock_init(&bh->b_journal_head_lock);
  10499. +#endif
  10500. +#endif
  10501. +}
  10502. +
  10503. /*
  10504. * macro tricks to expand the set_buffer_foo(), clear_buffer_foo()
  10505. * and buffer_foo() functions.
  10506. diff -Nur linux-4.4.46.orig/include/linux/cgroup-defs.h linux-4.4.46/include/linux/cgroup-defs.h
  10507. --- linux-4.4.46.orig/include/linux/cgroup-defs.h 2017-02-01 08:31:11.000000000 +0100
  10508. +++ linux-4.4.46/include/linux/cgroup-defs.h 2017-02-03 17:18:10.903618130 +0100
  10509. @@ -16,6 +16,7 @@
  10510. #include <linux/percpu-refcount.h>
  10511. #include <linux/percpu-rwsem.h>
  10512. #include <linux/workqueue.h>
  10513. +#include <linux/swork.h>
  10514. #ifdef CONFIG_CGROUPS
  10515. @@ -142,6 +143,7 @@
  10516. /* percpu_ref killing and RCU release */
  10517. struct rcu_head rcu_head;
  10518. struct work_struct destroy_work;
  10519. + struct swork_event destroy_swork;
  10520. };
  10521. /*
  10522. diff -Nur linux-4.4.46.orig/include/linux/clk/at91_pmc.h linux-4.4.46/include/linux/clk/at91_pmc.h
  10523. --- linux-4.4.46.orig/include/linux/clk/at91_pmc.h 2017-02-01 08:31:11.000000000 +0100
  10524. +++ linux-4.4.46/include/linux/clk/at91_pmc.h 2017-02-03 17:18:10.903618130 +0100
  10525. @@ -16,18 +16,6 @@
  10526. #ifndef AT91_PMC_H
  10527. #define AT91_PMC_H
  10528. -#ifndef __ASSEMBLY__
  10529. -extern void __iomem *at91_pmc_base;
  10530. -
  10531. -#define at91_pmc_read(field) \
  10532. - readl_relaxed(at91_pmc_base + field)
  10533. -
  10534. -#define at91_pmc_write(field, value) \
  10535. - writel_relaxed(value, at91_pmc_base + field)
  10536. -#else
  10537. -.extern at91_pmc_base
  10538. -#endif
  10539. -
  10540. #define AT91_PMC_SCER 0x00 /* System Clock Enable Register */
  10541. #define AT91_PMC_SCDR 0x04 /* System Clock Disable Register */
  10542. diff -Nur linux-4.4.46.orig/include/linux/completion.h linux-4.4.46/include/linux/completion.h
  10543. --- linux-4.4.46.orig/include/linux/completion.h 2017-02-01 08:31:11.000000000 +0100
  10544. +++ linux-4.4.46/include/linux/completion.h 2017-02-03 17:18:10.903618130 +0100
  10545. @@ -7,8 +7,7 @@
  10546. * Atomic wait-for-completion handler data structures.
  10547. * See kernel/sched/completion.c for details.
  10548. */
  10549. -
  10550. -#include <linux/wait.h>
  10551. +#include <linux/swait.h>
  10552. /*
  10553. * struct completion - structure used to maintain state for a "completion"
  10554. @@ -24,11 +23,11 @@
  10555. */
  10556. struct completion {
  10557. unsigned int done;
  10558. - wait_queue_head_t wait;
  10559. + struct swait_queue_head wait;
  10560. };
  10561. #define COMPLETION_INITIALIZER(work) \
  10562. - { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
  10563. + { 0, __SWAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
  10564. #define COMPLETION_INITIALIZER_ONSTACK(work) \
  10565. ({ init_completion(&work); work; })
  10566. @@ -73,7 +72,7 @@
  10567. static inline void init_completion(struct completion *x)
  10568. {
  10569. x->done = 0;
  10570. - init_waitqueue_head(&x->wait);
  10571. + init_swait_queue_head(&x->wait);
  10572. }
  10573. /**
  10574. diff -Nur linux-4.4.46.orig/include/linux/cpu.h linux-4.4.46/include/linux/cpu.h
  10575. --- linux-4.4.46.orig/include/linux/cpu.h 2017-02-01 08:31:11.000000000 +0100
  10576. +++ linux-4.4.46/include/linux/cpu.h 2017-02-03 17:18:10.903618130 +0100
  10577. @@ -224,6 +224,8 @@
  10578. extern void put_online_cpus(void);
  10579. extern void cpu_hotplug_disable(void);
  10580. extern void cpu_hotplug_enable(void);
  10581. +extern void pin_current_cpu(void);
  10582. +extern void unpin_current_cpu(void);
  10583. #define hotcpu_notifier(fn, pri) cpu_notifier(fn, pri)
  10584. #define __hotcpu_notifier(fn, pri) __cpu_notifier(fn, pri)
  10585. #define register_hotcpu_notifier(nb) register_cpu_notifier(nb)
  10586. @@ -241,6 +243,8 @@
  10587. #define put_online_cpus() do { } while (0)
  10588. #define cpu_hotplug_disable() do { } while (0)
  10589. #define cpu_hotplug_enable() do { } while (0)
  10590. +static inline void pin_current_cpu(void) { }
  10591. +static inline void unpin_current_cpu(void) { }
  10592. #define hotcpu_notifier(fn, pri) do { (void)(fn); } while (0)
  10593. #define __hotcpu_notifier(fn, pri) do { (void)(fn); } while (0)
  10594. /* These aren't inline functions due to a GCC bug. */
  10595. diff -Nur linux-4.4.46.orig/include/linux/delay.h linux-4.4.46/include/linux/delay.h
  10596. --- linux-4.4.46.orig/include/linux/delay.h 2017-02-01 08:31:11.000000000 +0100
  10597. +++ linux-4.4.46/include/linux/delay.h 2017-02-03 17:18:10.903618130 +0100
  10598. @@ -52,4 +52,10 @@
  10599. msleep(seconds * 1000);
  10600. }
  10601. +#ifdef CONFIG_PREEMPT_RT_FULL
  10602. +extern void cpu_chill(void);
  10603. +#else
  10604. +# define cpu_chill() cpu_relax()
  10605. +#endif
  10606. +
  10607. #endif /* defined(_LINUX_DELAY_H) */
  10608. diff -Nur linux-4.4.46.orig/include/linux/ftrace.h linux-4.4.46/include/linux/ftrace.h
  10609. --- linux-4.4.46.orig/include/linux/ftrace.h 2017-02-01 08:31:11.000000000 +0100
  10610. +++ linux-4.4.46/include/linux/ftrace.h 2017-02-03 17:18:10.903618130 +0100
  10611. @@ -694,6 +694,18 @@
  10612. #define CALLER_ADDR5 ((unsigned long)ftrace_return_address(5))
  10613. #define CALLER_ADDR6 ((unsigned long)ftrace_return_address(6))
  10614. +static inline unsigned long get_lock_parent_ip(void)
  10615. +{
  10616. + unsigned long addr = CALLER_ADDR0;
  10617. +
  10618. + if (!in_lock_functions(addr))
  10619. + return addr;
  10620. + addr = CALLER_ADDR1;
  10621. + if (!in_lock_functions(addr))
  10622. + return addr;
  10623. + return CALLER_ADDR2;
  10624. +}
  10625. +
  10626. #ifdef CONFIG_IRQSOFF_TRACER
  10627. extern void time_hardirqs_on(unsigned long a0, unsigned long a1);
  10628. extern void time_hardirqs_off(unsigned long a0, unsigned long a1);
  10629. diff -Nur linux-4.4.46.orig/include/linux/highmem.h linux-4.4.46/include/linux/highmem.h
  10630. --- linux-4.4.46.orig/include/linux/highmem.h 2017-02-01 08:31:11.000000000 +0100
  10631. +++ linux-4.4.46/include/linux/highmem.h 2017-02-03 17:18:10.903618130 +0100
  10632. @@ -7,6 +7,7 @@
  10633. #include <linux/mm.h>
  10634. #include <linux/uaccess.h>
  10635. #include <linux/hardirq.h>
  10636. +#include <linux/sched.h>
  10637. #include <asm/cacheflush.h>
  10638. @@ -65,7 +66,7 @@
  10639. static inline void *kmap_atomic(struct page *page)
  10640. {
  10641. - preempt_disable();
  10642. + preempt_disable_nort();
  10643. pagefault_disable();
  10644. return page_address(page);
  10645. }
  10646. @@ -74,7 +75,7 @@
  10647. static inline void __kunmap_atomic(void *addr)
  10648. {
  10649. pagefault_enable();
  10650. - preempt_enable();
  10651. + preempt_enable_nort();
  10652. }
  10653. #define kmap_atomic_pfn(pfn) kmap_atomic(pfn_to_page(pfn))
  10654. @@ -86,32 +87,51 @@
  10655. #if defined(CONFIG_HIGHMEM) || defined(CONFIG_X86_32)
  10656. +#ifndef CONFIG_PREEMPT_RT_FULL
  10657. DECLARE_PER_CPU(int, __kmap_atomic_idx);
  10658. +#endif
  10659. static inline int kmap_atomic_idx_push(void)
  10660. {
  10661. +#ifndef CONFIG_PREEMPT_RT_FULL
  10662. int idx = __this_cpu_inc_return(__kmap_atomic_idx) - 1;
  10663. -#ifdef CONFIG_DEBUG_HIGHMEM
  10664. +# ifdef CONFIG_DEBUG_HIGHMEM
  10665. WARN_ON_ONCE(in_irq() && !irqs_disabled());
  10666. BUG_ON(idx >= KM_TYPE_NR);
  10667. -#endif
  10668. +# endif
  10669. return idx;
  10670. +#else
  10671. + current->kmap_idx++;
  10672. + BUG_ON(current->kmap_idx > KM_TYPE_NR);
  10673. + return current->kmap_idx - 1;
  10674. +#endif
  10675. }
  10676. static inline int kmap_atomic_idx(void)
  10677. {
  10678. +#ifndef CONFIG_PREEMPT_RT_FULL
  10679. return __this_cpu_read(__kmap_atomic_idx) - 1;
  10680. +#else
  10681. + return current->kmap_idx - 1;
  10682. +#endif
  10683. }
  10684. static inline void kmap_atomic_idx_pop(void)
  10685. {
  10686. -#ifdef CONFIG_DEBUG_HIGHMEM
  10687. +#ifndef CONFIG_PREEMPT_RT_FULL
  10688. +# ifdef CONFIG_DEBUG_HIGHMEM
  10689. int idx = __this_cpu_dec_return(__kmap_atomic_idx);
  10690. BUG_ON(idx < 0);
  10691. -#else
  10692. +# else
  10693. __this_cpu_dec(__kmap_atomic_idx);
  10694. +# endif
  10695. +#else
  10696. + current->kmap_idx--;
  10697. +# ifdef CONFIG_DEBUG_HIGHMEM
  10698. + BUG_ON(current->kmap_idx < 0);
  10699. +# endif
  10700. #endif
  10701. }
  10702. diff -Nur linux-4.4.46.orig/include/linux/hrtimer.h linux-4.4.46/include/linux/hrtimer.h
  10703. --- linux-4.4.46.orig/include/linux/hrtimer.h 2017-02-01 08:31:11.000000000 +0100
  10704. +++ linux-4.4.46/include/linux/hrtimer.h 2017-02-03 17:18:10.903618130 +0100
  10705. @@ -87,6 +87,9 @@
  10706. * @function: timer expiry callback function
  10707. * @base: pointer to the timer base (per cpu and per clock)
  10708. * @state: state information (See bit values above)
  10709. + * @cb_entry: list entry to defer timers from hardirq context
  10710. + * @irqsafe: timer can run in hardirq context
  10711. + * @praecox: timer expiry time if expired at the time of programming
  10712. * @is_rel: Set if the timer was armed relative
  10713. * @start_pid: timer statistics field to store the pid of the task which
  10714. * started the timer
  10715. @@ -103,6 +106,11 @@
  10716. enum hrtimer_restart (*function)(struct hrtimer *);
  10717. struct hrtimer_clock_base *base;
  10718. u8 state;
  10719. + struct list_head cb_entry;
  10720. + int irqsafe;
  10721. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  10722. + ktime_t praecox;
  10723. +#endif
  10724. u8 is_rel;
  10725. #ifdef CONFIG_TIMER_STATS
  10726. int start_pid;
  10727. @@ -123,11 +131,7 @@
  10728. struct task_struct *task;
  10729. };
  10730. -#ifdef CONFIG_64BIT
  10731. # define HRTIMER_CLOCK_BASE_ALIGN 64
  10732. -#else
  10733. -# define HRTIMER_CLOCK_BASE_ALIGN 32
  10734. -#endif
  10735. /**
  10736. * struct hrtimer_clock_base - the timer base for a specific clock
  10737. @@ -136,6 +140,7 @@
  10738. * timer to a base on another cpu.
  10739. * @clockid: clock id for per_cpu support
  10740. * @active: red black tree root node for the active timers
  10741. + * @expired: list head for deferred timers.
  10742. * @get_time: function to retrieve the current time of the clock
  10743. * @offset: offset of this clock to the monotonic base
  10744. */
  10745. @@ -144,6 +149,7 @@
  10746. int index;
  10747. clockid_t clockid;
  10748. struct timerqueue_head active;
  10749. + struct list_head expired;
  10750. ktime_t (*get_time)(void);
  10751. ktime_t offset;
  10752. } __attribute__((__aligned__(HRTIMER_CLOCK_BASE_ALIGN)));
  10753. @@ -187,6 +193,7 @@
  10754. raw_spinlock_t lock;
  10755. seqcount_t seq;
  10756. struct hrtimer *running;
  10757. + struct hrtimer *running_soft;
  10758. unsigned int cpu;
  10759. unsigned int active_bases;
  10760. unsigned int clock_was_set_seq;
  10761. @@ -203,6 +210,9 @@
  10762. unsigned int nr_hangs;
  10763. unsigned int max_hang_time;
  10764. #endif
  10765. +#ifdef CONFIG_PREEMPT_RT_BASE
  10766. + wait_queue_head_t wait;
  10767. +#endif
  10768. struct hrtimer_clock_base clock_base[HRTIMER_MAX_CLOCK_BASES];
  10769. } ____cacheline_aligned;
  10770. @@ -412,6 +422,13 @@
  10771. hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
  10772. }
  10773. +/* Softirq preemption could deadlock timer removal */
  10774. +#ifdef CONFIG_PREEMPT_RT_BASE
  10775. + extern void hrtimer_wait_for_timer(const struct hrtimer *timer);
  10776. +#else
  10777. +# define hrtimer_wait_for_timer(timer) do { cpu_relax(); } while (0)
  10778. +#endif
  10779. +
  10780. /* Query timers: */
  10781. extern ktime_t __hrtimer_get_remaining(const struct hrtimer *timer, bool adjust);
  10782. @@ -436,7 +453,7 @@
  10783. * Helper function to check, whether the timer is running the callback
  10784. * function
  10785. */
  10786. -static inline int hrtimer_callback_running(struct hrtimer *timer)
  10787. +static inline int hrtimer_callback_running(const struct hrtimer *timer)
  10788. {
  10789. return timer->base->cpu_base->running == timer;
  10790. }
  10791. diff -Nur linux-4.4.46.orig/include/linux/idr.h linux-4.4.46/include/linux/idr.h
  10792. --- linux-4.4.46.orig/include/linux/idr.h 2017-02-01 08:31:11.000000000 +0100
  10793. +++ linux-4.4.46/include/linux/idr.h 2017-02-03 17:18:10.903618130 +0100
  10794. @@ -95,10 +95,14 @@
  10795. * Each idr_preload() should be matched with an invocation of this
  10796. * function. See idr_preload() for details.
  10797. */
  10798. +#ifdef CONFIG_PREEMPT_RT_FULL
  10799. +void idr_preload_end(void);
  10800. +#else
  10801. static inline void idr_preload_end(void)
  10802. {
  10803. preempt_enable();
  10804. }
  10805. +#endif
  10806. /**
  10807. * idr_find - return pointer for given id
  10808. diff -Nur linux-4.4.46.orig/include/linux/init_task.h linux-4.4.46/include/linux/init_task.h
  10809. --- linux-4.4.46.orig/include/linux/init_task.h 2017-02-01 08:31:11.000000000 +0100
  10810. +++ linux-4.4.46/include/linux/init_task.h 2017-02-03 17:18:10.903618130 +0100
  10811. @@ -148,9 +148,15 @@
  10812. # define INIT_PERF_EVENTS(tsk)
  10813. #endif
  10814. +#ifdef CONFIG_PREEMPT_RT_BASE
  10815. +# define INIT_TIMER_LIST .posix_timer_list = NULL,
  10816. +#else
  10817. +# define INIT_TIMER_LIST
  10818. +#endif
  10819. +
  10820. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  10821. # define INIT_VTIME(tsk) \
  10822. - .vtime_seqlock = __SEQLOCK_UNLOCKED(tsk.vtime_seqlock), \
  10823. + .vtime_seqcount = SEQCNT_ZERO(tsk.vtime_seqcount), \
  10824. .vtime_snap = 0, \
  10825. .vtime_snap_whence = VTIME_SYS,
  10826. #else
  10827. @@ -239,6 +245,7 @@
  10828. .cpu_timers = INIT_CPU_TIMERS(tsk.cpu_timers), \
  10829. .pi_lock = __RAW_SPIN_LOCK_UNLOCKED(tsk.pi_lock), \
  10830. .timer_slack_ns = 50000, /* 50 usec default slack */ \
  10831. + INIT_TIMER_LIST \
  10832. .pids = { \
  10833. [PIDTYPE_PID] = INIT_PID_LINK(PIDTYPE_PID), \
  10834. [PIDTYPE_PGID] = INIT_PID_LINK(PIDTYPE_PGID), \
  10835. diff -Nur linux-4.4.46.orig/include/linux/interrupt.h linux-4.4.46/include/linux/interrupt.h
  10836. --- linux-4.4.46.orig/include/linux/interrupt.h 2017-02-01 08:31:11.000000000 +0100
  10837. +++ linux-4.4.46/include/linux/interrupt.h 2017-02-03 17:18:10.903618130 +0100
  10838. @@ -61,6 +61,7 @@
  10839. * interrupt handler after suspending interrupts. For system
  10840. * wakeup devices users need to implement wakeup detection in
  10841. * their interrupt handlers.
  10842. + * IRQF_NO_SOFTIRQ_CALL - Do not process softirqs in the irq thread context (RT)
  10843. */
  10844. #define IRQF_SHARED 0x00000080
  10845. #define IRQF_PROBE_SHARED 0x00000100
  10846. @@ -74,6 +75,7 @@
  10847. #define IRQF_NO_THREAD 0x00010000
  10848. #define IRQF_EARLY_RESUME 0x00020000
  10849. #define IRQF_COND_SUSPEND 0x00040000
  10850. +#define IRQF_NO_SOFTIRQ_CALL 0x00080000
  10851. #define IRQF_TIMER (__IRQF_TIMER | IRQF_NO_SUSPEND | IRQF_NO_THREAD)
  10852. @@ -186,7 +188,7 @@
  10853. #ifdef CONFIG_LOCKDEP
  10854. # define local_irq_enable_in_hardirq() do { } while (0)
  10855. #else
  10856. -# define local_irq_enable_in_hardirq() local_irq_enable()
  10857. +# define local_irq_enable_in_hardirq() local_irq_enable_nort()
  10858. #endif
  10859. extern void disable_irq_nosync(unsigned int irq);
  10860. @@ -206,6 +208,7 @@
  10861. * @irq: Interrupt to which notification applies
  10862. * @kref: Reference count, for internal use
  10863. * @work: Work item, for internal use
  10864. + * @list: List item for deferred callbacks
  10865. * @notify: Function to be called on change. This will be
  10866. * called in process context.
  10867. * @release: Function to be called on release. This will be
  10868. @@ -217,6 +220,7 @@
  10869. unsigned int irq;
  10870. struct kref kref;
  10871. struct work_struct work;
  10872. + struct list_head list;
  10873. void (*notify)(struct irq_affinity_notify *, const cpumask_t *mask);
  10874. void (*release)(struct kref *ref);
  10875. };
  10876. @@ -379,9 +383,13 @@
  10877. bool state);
  10878. #ifdef CONFIG_IRQ_FORCED_THREADING
  10879. +# ifndef CONFIG_PREEMPT_RT_BASE
  10880. extern bool force_irqthreads;
  10881. +# else
  10882. +# define force_irqthreads (true)
  10883. +# endif
  10884. #else
  10885. -#define force_irqthreads (0)
  10886. +#define force_irqthreads (false)
  10887. #endif
  10888. #ifndef __ARCH_SET_SOFTIRQ_PENDING
  10889. @@ -438,9 +446,10 @@
  10890. void (*action)(struct softirq_action *);
  10891. };
  10892. +#ifndef CONFIG_PREEMPT_RT_FULL
  10893. asmlinkage void do_softirq(void);
  10894. asmlinkage void __do_softirq(void);
  10895. -
  10896. +static inline void thread_do_softirq(void) { do_softirq(); }
  10897. #ifdef __ARCH_HAS_DO_SOFTIRQ
  10898. void do_softirq_own_stack(void);
  10899. #else
  10900. @@ -449,13 +458,25 @@
  10901. __do_softirq();
  10902. }
  10903. #endif
  10904. +#else
  10905. +extern void thread_do_softirq(void);
  10906. +#endif
  10907. extern void open_softirq(int nr, void (*action)(struct softirq_action *));
  10908. extern void softirq_init(void);
  10909. extern void __raise_softirq_irqoff(unsigned int nr);
  10910. +#ifdef CONFIG_PREEMPT_RT_FULL
  10911. +extern void __raise_softirq_irqoff_ksoft(unsigned int nr);
  10912. +#else
  10913. +static inline void __raise_softirq_irqoff_ksoft(unsigned int nr)
  10914. +{
  10915. + __raise_softirq_irqoff(nr);
  10916. +}
  10917. +#endif
  10918. extern void raise_softirq_irqoff(unsigned int nr);
  10919. extern void raise_softirq(unsigned int nr);
  10920. +extern void softirq_check_pending_idle(void);
  10921. DECLARE_PER_CPU(struct task_struct *, ksoftirqd);
  10922. @@ -477,8 +498,9 @@
  10923. to be executed on some cpu at least once after this.
  10924. * If the tasklet is already scheduled, but its execution is still not
  10925. started, it will be executed only once.
  10926. - * If this tasklet is already running on another CPU (or schedule is called
  10927. - from tasklet itself), it is rescheduled for later.
  10928. + * If this tasklet is already running on another CPU, it is rescheduled
  10929. + for later.
  10930. + * Schedule must not be called from the tasklet itself (a lockup occurs)
  10931. * Tasklet is strictly serialized wrt itself, but not
  10932. wrt another tasklets. If client needs some intertask synchronization,
  10933. he makes it with spinlocks.
  10934. @@ -503,27 +525,36 @@
  10935. enum
  10936. {
  10937. TASKLET_STATE_SCHED, /* Tasklet is scheduled for execution */
  10938. - TASKLET_STATE_RUN /* Tasklet is running (SMP only) */
  10939. + TASKLET_STATE_RUN, /* Tasklet is running (SMP only) */
  10940. + TASKLET_STATE_PENDING /* Tasklet is pending */
  10941. };
  10942. -#ifdef CONFIG_SMP
  10943. +#define TASKLET_STATEF_SCHED (1 << TASKLET_STATE_SCHED)
  10944. +#define TASKLET_STATEF_RUN (1 << TASKLET_STATE_RUN)
  10945. +#define TASKLET_STATEF_PENDING (1 << TASKLET_STATE_PENDING)
  10946. +
  10947. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  10948. static inline int tasklet_trylock(struct tasklet_struct *t)
  10949. {
  10950. return !test_and_set_bit(TASKLET_STATE_RUN, &(t)->state);
  10951. }
  10952. +static inline int tasklet_tryunlock(struct tasklet_struct *t)
  10953. +{
  10954. + return cmpxchg(&t->state, TASKLET_STATEF_RUN, 0) == TASKLET_STATEF_RUN;
  10955. +}
  10956. +
  10957. static inline void tasklet_unlock(struct tasklet_struct *t)
  10958. {
  10959. smp_mb__before_atomic();
  10960. clear_bit(TASKLET_STATE_RUN, &(t)->state);
  10961. }
  10962. -static inline void tasklet_unlock_wait(struct tasklet_struct *t)
  10963. -{
  10964. - while (test_bit(TASKLET_STATE_RUN, &(t)->state)) { barrier(); }
  10965. -}
  10966. +extern void tasklet_unlock_wait(struct tasklet_struct *t);
  10967. +
  10968. #else
  10969. #define tasklet_trylock(t) 1
  10970. +#define tasklet_tryunlock(t) 1
  10971. #define tasklet_unlock_wait(t) do { } while (0)
  10972. #define tasklet_unlock(t) do { } while (0)
  10973. #endif
  10974. @@ -572,12 +603,7 @@
  10975. smp_mb();
  10976. }
  10977. -static inline void tasklet_enable(struct tasklet_struct *t)
  10978. -{
  10979. - smp_mb__before_atomic();
  10980. - atomic_dec(&t->count);
  10981. -}
  10982. -
  10983. +extern void tasklet_enable(struct tasklet_struct *t);
  10984. extern void tasklet_kill(struct tasklet_struct *t);
  10985. extern void tasklet_kill_immediate(struct tasklet_struct *t, unsigned int cpu);
  10986. extern void tasklet_init(struct tasklet_struct *t,
  10987. @@ -608,6 +634,12 @@
  10988. tasklet_kill(&ttimer->tasklet);
  10989. }
  10990. +#ifdef CONFIG_PREEMPT_RT_FULL
  10991. +extern void softirq_early_init(void);
  10992. +#else
  10993. +static inline void softirq_early_init(void) { }
  10994. +#endif
  10995. +
  10996. /*
  10997. * Autoprobing for irqs:
  10998. *
  10999. diff -Nur linux-4.4.46.orig/include/linux/irqdesc.h linux-4.4.46/include/linux/irqdesc.h
  11000. --- linux-4.4.46.orig/include/linux/irqdesc.h 2017-02-01 08:31:11.000000000 +0100
  11001. +++ linux-4.4.46/include/linux/irqdesc.h 2017-02-03 17:18:10.903618130 +0100
  11002. @@ -61,6 +61,7 @@
  11003. unsigned int irqs_unhandled;
  11004. atomic_t threads_handled;
  11005. int threads_handled_last;
  11006. + u64 random_ip;
  11007. raw_spinlock_t lock;
  11008. struct cpumask *percpu_enabled;
  11009. #ifdef CONFIG_SMP
  11010. diff -Nur linux-4.4.46.orig/include/linux/irqflags.h linux-4.4.46/include/linux/irqflags.h
  11011. --- linux-4.4.46.orig/include/linux/irqflags.h 2017-02-01 08:31:11.000000000 +0100
  11012. +++ linux-4.4.46/include/linux/irqflags.h 2017-02-03 17:18:10.903618130 +0100
  11013. @@ -25,8 +25,6 @@
  11014. # define trace_softirqs_enabled(p) ((p)->softirqs_enabled)
  11015. # define trace_hardirq_enter() do { current->hardirq_context++; } while (0)
  11016. # define trace_hardirq_exit() do { current->hardirq_context--; } while (0)
  11017. -# define lockdep_softirq_enter() do { current->softirq_context++; } while (0)
  11018. -# define lockdep_softirq_exit() do { current->softirq_context--; } while (0)
  11019. # define INIT_TRACE_IRQFLAGS .softirqs_enabled = 1,
  11020. #else
  11021. # define trace_hardirqs_on() do { } while (0)
  11022. @@ -39,9 +37,15 @@
  11023. # define trace_softirqs_enabled(p) 0
  11024. # define trace_hardirq_enter() do { } while (0)
  11025. # define trace_hardirq_exit() do { } while (0)
  11026. +# define INIT_TRACE_IRQFLAGS
  11027. +#endif
  11028. +
  11029. +#if defined(CONFIG_TRACE_IRQFLAGS) && !defined(CONFIG_PREEMPT_RT_FULL)
  11030. +# define lockdep_softirq_enter() do { current->softirq_context++; } while (0)
  11031. +# define lockdep_softirq_exit() do { current->softirq_context--; } while (0)
  11032. +#else
  11033. # define lockdep_softirq_enter() do { } while (0)
  11034. # define lockdep_softirq_exit() do { } while (0)
  11035. -# define INIT_TRACE_IRQFLAGS
  11036. #endif
  11037. #if defined(CONFIG_IRQSOFF_TRACER) || \
  11038. @@ -148,4 +152,23 @@
  11039. #define irqs_disabled_flags(flags) raw_irqs_disabled_flags(flags)
  11040. +/*
  11041. + * local_irq* variants depending on RT/!RT
  11042. + */
  11043. +#ifdef CONFIG_PREEMPT_RT_FULL
  11044. +# define local_irq_disable_nort() do { } while (0)
  11045. +# define local_irq_enable_nort() do { } while (0)
  11046. +# define local_irq_save_nort(flags) local_save_flags(flags)
  11047. +# define local_irq_restore_nort(flags) (void)(flags)
  11048. +# define local_irq_disable_rt() local_irq_disable()
  11049. +# define local_irq_enable_rt() local_irq_enable()
  11050. +#else
  11051. +# define local_irq_disable_nort() local_irq_disable()
  11052. +# define local_irq_enable_nort() local_irq_enable()
  11053. +# define local_irq_save_nort(flags) local_irq_save(flags)
  11054. +# define local_irq_restore_nort(flags) local_irq_restore(flags)
  11055. +# define local_irq_disable_rt() do { } while (0)
  11056. +# define local_irq_enable_rt() do { } while (0)
  11057. +#endif
  11058. +
  11059. #endif
  11060. diff -Nur linux-4.4.46.orig/include/linux/irq.h linux-4.4.46/include/linux/irq.h
  11061. --- linux-4.4.46.orig/include/linux/irq.h 2017-02-01 08:31:11.000000000 +0100
  11062. +++ linux-4.4.46/include/linux/irq.h 2017-02-03 17:18:10.903618130 +0100
  11063. @@ -72,6 +72,7 @@
  11064. * IRQ_IS_POLLED - Always polled by another interrupt. Exclude
  11065. * it from the spurious interrupt detection
  11066. * mechanism and from core side polling.
  11067. + * IRQ_NO_SOFTIRQ_CALL - No softirq processing in the irq thread context (RT)
  11068. * IRQ_DISABLE_UNLAZY - Disable lazy irq disable
  11069. */
  11070. enum {
  11071. @@ -99,13 +100,14 @@
  11072. IRQ_PER_CPU_DEVID = (1 << 17),
  11073. IRQ_IS_POLLED = (1 << 18),
  11074. IRQ_DISABLE_UNLAZY = (1 << 19),
  11075. + IRQ_NO_SOFTIRQ_CALL = (1 << 20),
  11076. };
  11077. #define IRQF_MODIFY_MASK \
  11078. (IRQ_TYPE_SENSE_MASK | IRQ_NOPROBE | IRQ_NOREQUEST | \
  11079. IRQ_NOAUTOEN | IRQ_MOVE_PCNTXT | IRQ_LEVEL | IRQ_NO_BALANCING | \
  11080. IRQ_PER_CPU | IRQ_NESTED_THREAD | IRQ_NOTHREAD | IRQ_PER_CPU_DEVID | \
  11081. - IRQ_IS_POLLED | IRQ_DISABLE_UNLAZY)
  11082. + IRQ_IS_POLLED | IRQ_DISABLE_UNLAZY | IRQ_NO_SOFTIRQ_CALL)
  11083. #define IRQ_NO_BALANCING_MASK (IRQ_PER_CPU | IRQ_NO_BALANCING)
  11084. diff -Nur linux-4.4.46.orig/include/linux/irq_work.h linux-4.4.46/include/linux/irq_work.h
  11085. --- linux-4.4.46.orig/include/linux/irq_work.h 2017-02-01 08:31:11.000000000 +0100
  11086. +++ linux-4.4.46/include/linux/irq_work.h 2017-02-03 17:18:10.903618130 +0100
  11087. @@ -16,6 +16,7 @@
  11088. #define IRQ_WORK_BUSY 2UL
  11089. #define IRQ_WORK_FLAGS 3UL
  11090. #define IRQ_WORK_LAZY 4UL /* Doesn't want IPI, wait for tick */
  11091. +#define IRQ_WORK_HARD_IRQ 8UL /* Run hard IRQ context, even on RT */
  11092. struct irq_work {
  11093. unsigned long flags;
  11094. @@ -51,4 +52,10 @@
  11095. static inline void irq_work_run(void) { }
  11096. #endif
  11097. +#if defined(CONFIG_IRQ_WORK) && defined(CONFIG_PREEMPT_RT_FULL)
  11098. +void irq_work_tick_soft(void);
  11099. +#else
  11100. +static inline void irq_work_tick_soft(void) { }
  11101. +#endif
  11102. +
  11103. #endif /* _LINUX_IRQ_WORK_H */
  11104. diff -Nur linux-4.4.46.orig/include/linux/jbd2.h linux-4.4.46/include/linux/jbd2.h
  11105. --- linux-4.4.46.orig/include/linux/jbd2.h 2017-02-01 08:31:11.000000000 +0100
  11106. +++ linux-4.4.46/include/linux/jbd2.h 2017-02-03 17:18:10.907618284 +0100
  11107. @@ -352,32 +352,56 @@
  11108. static inline void jbd_lock_bh_state(struct buffer_head *bh)
  11109. {
  11110. +#ifndef CONFIG_PREEMPT_RT_BASE
  11111. bit_spin_lock(BH_State, &bh->b_state);
  11112. +#else
  11113. + spin_lock(&bh->b_state_lock);
  11114. +#endif
  11115. }
  11116. static inline int jbd_trylock_bh_state(struct buffer_head *bh)
  11117. {
  11118. +#ifndef CONFIG_PREEMPT_RT_BASE
  11119. return bit_spin_trylock(BH_State, &bh->b_state);
  11120. +#else
  11121. + return spin_trylock(&bh->b_state_lock);
  11122. +#endif
  11123. }
  11124. static inline int jbd_is_locked_bh_state(struct buffer_head *bh)
  11125. {
  11126. +#ifndef CONFIG_PREEMPT_RT_BASE
  11127. return bit_spin_is_locked(BH_State, &bh->b_state);
  11128. +#else
  11129. + return spin_is_locked(&bh->b_state_lock);
  11130. +#endif
  11131. }
  11132. static inline void jbd_unlock_bh_state(struct buffer_head *bh)
  11133. {
  11134. +#ifndef CONFIG_PREEMPT_RT_BASE
  11135. bit_spin_unlock(BH_State, &bh->b_state);
  11136. +#else
  11137. + spin_unlock(&bh->b_state_lock);
  11138. +#endif
  11139. }
  11140. static inline void jbd_lock_bh_journal_head(struct buffer_head *bh)
  11141. {
  11142. +#ifndef CONFIG_PREEMPT_RT_BASE
  11143. bit_spin_lock(BH_JournalHead, &bh->b_state);
  11144. +#else
  11145. + spin_lock(&bh->b_journal_head_lock);
  11146. +#endif
  11147. }
  11148. static inline void jbd_unlock_bh_journal_head(struct buffer_head *bh)
  11149. {
  11150. +#ifndef CONFIG_PREEMPT_RT_BASE
  11151. bit_spin_unlock(BH_JournalHead, &bh->b_state);
  11152. +#else
  11153. + spin_unlock(&bh->b_journal_head_lock);
  11154. +#endif
  11155. }
  11156. #define J_ASSERT(assert) BUG_ON(!(assert))
  11157. diff -Nur linux-4.4.46.orig/include/linux/kdb.h linux-4.4.46/include/linux/kdb.h
  11158. --- linux-4.4.46.orig/include/linux/kdb.h 2017-02-01 08:31:11.000000000 +0100
  11159. +++ linux-4.4.46/include/linux/kdb.h 2017-02-03 17:18:10.907618284 +0100
  11160. @@ -167,6 +167,7 @@
  11161. extern __printf(1, 2) int kdb_printf(const char *, ...);
  11162. typedef __printf(1, 2) int (*kdb_printf_t)(const char *, ...);
  11163. +#define in_kdb_printk() (kdb_trap_printk)
  11164. extern void kdb_init(int level);
  11165. /* Access to kdb specific polling devices */
  11166. @@ -201,6 +202,7 @@
  11167. extern int kdb_unregister(char *);
  11168. #else /* ! CONFIG_KGDB_KDB */
  11169. static inline __printf(1, 2) int kdb_printf(const char *fmt, ...) { return 0; }
  11170. +#define in_kdb_printk() (0)
  11171. static inline void kdb_init(int level) {}
  11172. static inline int kdb_register(char *cmd, kdb_func_t func, char *usage,
  11173. char *help, short minlen) { return 0; }
  11174. diff -Nur linux-4.4.46.orig/include/linux/kernel.h linux-4.4.46/include/linux/kernel.h
  11175. --- linux-4.4.46.orig/include/linux/kernel.h 2017-02-01 08:31:11.000000000 +0100
  11176. +++ linux-4.4.46/include/linux/kernel.h 2017-02-03 17:18:10.907618284 +0100
  11177. @@ -188,6 +188,9 @@
  11178. */
  11179. # define might_sleep() \
  11180. do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
  11181. +
  11182. +# define might_sleep_no_state_check() \
  11183. + do { ___might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
  11184. # define sched_annotate_sleep() (current->task_state_change = 0)
  11185. #else
  11186. static inline void ___might_sleep(const char *file, int line,
  11187. @@ -195,6 +198,7 @@
  11188. static inline void __might_sleep(const char *file, int line,
  11189. int preempt_offset) { }
  11190. # define might_sleep() do { might_resched(); } while (0)
  11191. +# define might_sleep_no_state_check() do { might_resched(); } while (0)
  11192. # define sched_annotate_sleep() do { } while (0)
  11193. #endif
  11194. @@ -255,6 +259,7 @@
  11195. __printf(1, 2)
  11196. void panic(const char *fmt, ...)
  11197. __noreturn __cold;
  11198. +void nmi_panic(struct pt_regs *regs, const char *msg);
  11199. extern void oops_enter(void);
  11200. extern void oops_exit(void);
  11201. void print_oops_end_marker(void);
  11202. @@ -448,6 +453,14 @@
  11203. extern bool crash_kexec_post_notifiers;
  11204. /*
  11205. + * panic_cpu is used for synchronizing panic() and crash_kexec() execution. It
  11206. + * holds a CPU number which is executing panic() currently. A value of
  11207. + * PANIC_CPU_INVALID means no CPU has entered panic() or crash_kexec().
  11208. + */
  11209. +extern atomic_t panic_cpu;
  11210. +#define PANIC_CPU_INVALID -1
  11211. +
  11212. +/*
  11213. * Only to be used by arch init code. If the user over-wrote the default
  11214. * CONFIG_PANIC_TIMEOUT, honor it.
  11215. */
  11216. @@ -475,6 +488,7 @@
  11217. SYSTEM_HALT,
  11218. SYSTEM_POWER_OFF,
  11219. SYSTEM_RESTART,
  11220. + SYSTEM_SUSPEND,
  11221. } system_state;
  11222. #define TAINT_PROPRIETARY_MODULE 0
  11223. diff -Nur linux-4.4.46.orig/include/linux/kvm_host.h linux-4.4.46/include/linux/kvm_host.h
  11224. --- linux-4.4.46.orig/include/linux/kvm_host.h 2017-02-01 08:31:11.000000000 +0100
  11225. +++ linux-4.4.46/include/linux/kvm_host.h 2017-02-03 17:18:10.907618284 +0100
  11226. @@ -25,6 +25,7 @@
  11227. #include <linux/irqflags.h>
  11228. #include <linux/context_tracking.h>
  11229. #include <linux/irqbypass.h>
  11230. +#include <linux/swait.h>
  11231. #include <asm/signal.h>
  11232. #include <linux/kvm.h>
  11233. @@ -243,7 +244,7 @@
  11234. int fpu_active;
  11235. int guest_fpu_loaded, guest_xcr0_loaded;
  11236. unsigned char fpu_counter;
  11237. - wait_queue_head_t wq;
  11238. + struct swait_queue_head wq;
  11239. struct pid *pid;
  11240. int sigset_active;
  11241. sigset_t sigset;
  11242. @@ -794,7 +795,7 @@
  11243. }
  11244. #endif
  11245. -static inline wait_queue_head_t *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu)
  11246. +static inline struct swait_queue_head *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu)
  11247. {
  11248. #ifdef __KVM_HAVE_ARCH_WQP
  11249. return vcpu->arch.wqp;
  11250. diff -Nur linux-4.4.46.orig/include/linux/lglock.h linux-4.4.46/include/linux/lglock.h
  11251. --- linux-4.4.46.orig/include/linux/lglock.h 2017-02-01 08:31:11.000000000 +0100
  11252. +++ linux-4.4.46/include/linux/lglock.h 2017-02-03 17:18:10.907618284 +0100
  11253. @@ -34,13 +34,30 @@
  11254. #endif
  11255. struct lglock {
  11256. +#ifdef CONFIG_PREEMPT_RT_FULL
  11257. + struct rt_mutex __percpu *lock;
  11258. +#else
  11259. arch_spinlock_t __percpu *lock;
  11260. +#endif
  11261. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  11262. struct lock_class_key lock_key;
  11263. struct lockdep_map lock_dep_map;
  11264. #endif
  11265. };
  11266. +#ifdef CONFIG_PREEMPT_RT_FULL
  11267. +# define DEFINE_LGLOCK(name) \
  11268. + static DEFINE_PER_CPU(struct rt_mutex, name ## _lock) \
  11269. + = __RT_MUTEX_INITIALIZER( name ## _lock); \
  11270. + struct lglock name = { .lock = &name ## _lock }
  11271. +
  11272. +# define DEFINE_STATIC_LGLOCK(name) \
  11273. + static DEFINE_PER_CPU(struct rt_mutex, name ## _lock) \
  11274. + = __RT_MUTEX_INITIALIZER( name ## _lock); \
  11275. + static struct lglock name = { .lock = &name ## _lock }
  11276. +
  11277. +#else
  11278. +
  11279. #define DEFINE_LGLOCK(name) \
  11280. static DEFINE_PER_CPU(arch_spinlock_t, name ## _lock) \
  11281. = __ARCH_SPIN_LOCK_UNLOCKED; \
  11282. @@ -50,6 +67,7 @@
  11283. static DEFINE_PER_CPU(arch_spinlock_t, name ## _lock) \
  11284. = __ARCH_SPIN_LOCK_UNLOCKED; \
  11285. static struct lglock name = { .lock = &name ## _lock }
  11286. +#endif
  11287. void lg_lock_init(struct lglock *lg, char *name);
  11288. @@ -64,6 +82,12 @@
  11289. void lg_global_lock(struct lglock *lg);
  11290. void lg_global_unlock(struct lglock *lg);
  11291. +#ifndef CONFIG_PREEMPT_RT_FULL
  11292. +#define lg_global_trylock_relax(name) lg_global_lock(name)
  11293. +#else
  11294. +void lg_global_trylock_relax(struct lglock *lg);
  11295. +#endif
  11296. +
  11297. #else
  11298. /* When !CONFIG_SMP, map lglock to spinlock */
  11299. #define lglock spinlock
  11300. diff -Nur linux-4.4.46.orig/include/linux/list_bl.h linux-4.4.46/include/linux/list_bl.h
  11301. --- linux-4.4.46.orig/include/linux/list_bl.h 2017-02-01 08:31:11.000000000 +0100
  11302. +++ linux-4.4.46/include/linux/list_bl.h 2017-02-03 17:18:10.907618284 +0100
  11303. @@ -2,6 +2,7 @@
  11304. #define _LINUX_LIST_BL_H
  11305. #include <linux/list.h>
  11306. +#include <linux/spinlock.h>
  11307. #include <linux/bit_spinlock.h>
  11308. /*
  11309. @@ -32,13 +33,24 @@
  11310. struct hlist_bl_head {
  11311. struct hlist_bl_node *first;
  11312. +#ifdef CONFIG_PREEMPT_RT_BASE
  11313. + raw_spinlock_t lock;
  11314. +#endif
  11315. };
  11316. struct hlist_bl_node {
  11317. struct hlist_bl_node *next, **pprev;
  11318. };
  11319. -#define INIT_HLIST_BL_HEAD(ptr) \
  11320. - ((ptr)->first = NULL)
  11321. +
  11322. +#ifdef CONFIG_PREEMPT_RT_BASE
  11323. +#define INIT_HLIST_BL_HEAD(h) \
  11324. +do { \
  11325. + (h)->first = NULL; \
  11326. + raw_spin_lock_init(&(h)->lock); \
  11327. +} while (0)
  11328. +#else
  11329. +#define INIT_HLIST_BL_HEAD(h) (h)->first = NULL
  11330. +#endif
  11331. static inline void INIT_HLIST_BL_NODE(struct hlist_bl_node *h)
  11332. {
  11333. @@ -118,12 +130,26 @@
  11334. static inline void hlist_bl_lock(struct hlist_bl_head *b)
  11335. {
  11336. +#ifndef CONFIG_PREEMPT_RT_BASE
  11337. bit_spin_lock(0, (unsigned long *)b);
  11338. +#else
  11339. + raw_spin_lock(&b->lock);
  11340. +#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  11341. + __set_bit(0, (unsigned long *)b);
  11342. +#endif
  11343. +#endif
  11344. }
  11345. static inline void hlist_bl_unlock(struct hlist_bl_head *b)
  11346. {
  11347. +#ifndef CONFIG_PREEMPT_RT_BASE
  11348. __bit_spin_unlock(0, (unsigned long *)b);
  11349. +#else
  11350. +#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  11351. + __clear_bit(0, (unsigned long *)b);
  11352. +#endif
  11353. + raw_spin_unlock(&b->lock);
  11354. +#endif
  11355. }
  11356. static inline bool hlist_bl_is_locked(struct hlist_bl_head *b)
  11357. diff -Nur linux-4.4.46.orig/include/linux/locallock.h linux-4.4.46/include/linux/locallock.h
  11358. --- linux-4.4.46.orig/include/linux/locallock.h 1970-01-01 01:00:00.000000000 +0100
  11359. +++ linux-4.4.46/include/linux/locallock.h 2017-02-03 17:18:10.907618284 +0100
  11360. @@ -0,0 +1,276 @@
  11361. +#ifndef _LINUX_LOCALLOCK_H
  11362. +#define _LINUX_LOCALLOCK_H
  11363. +
  11364. +#include <linux/percpu.h>
  11365. +#include <linux/spinlock.h>
  11366. +
  11367. +#ifdef CONFIG_PREEMPT_RT_BASE
  11368. +
  11369. +#ifdef CONFIG_DEBUG_SPINLOCK
  11370. +# define LL_WARN(cond) WARN_ON(cond)
  11371. +#else
  11372. +# define LL_WARN(cond) do { } while (0)
  11373. +#endif
  11374. +
  11375. +/*
  11376. + * per cpu lock based substitute for local_irq_*()
  11377. + */
  11378. +struct local_irq_lock {
  11379. + spinlock_t lock;
  11380. + struct task_struct *owner;
  11381. + int nestcnt;
  11382. + unsigned long flags;
  11383. +};
  11384. +
  11385. +#define DEFINE_LOCAL_IRQ_LOCK(lvar) \
  11386. + DEFINE_PER_CPU(struct local_irq_lock, lvar) = { \
  11387. + .lock = __SPIN_LOCK_UNLOCKED((lvar).lock) }
  11388. +
  11389. +#define DECLARE_LOCAL_IRQ_LOCK(lvar) \
  11390. + DECLARE_PER_CPU(struct local_irq_lock, lvar)
  11391. +
  11392. +#define local_irq_lock_init(lvar) \
  11393. + do { \
  11394. + int __cpu; \
  11395. + for_each_possible_cpu(__cpu) \
  11396. + spin_lock_init(&per_cpu(lvar, __cpu).lock); \
  11397. + } while (0)
  11398. +
  11399. +/*
  11400. + * spin_lock|trylock|unlock_local flavour that does not migrate disable
  11401. + * used for __local_lock|trylock|unlock where get_local_var/put_local_var
  11402. + * already takes care of the migrate_disable/enable
  11403. + * for CONFIG_PREEMPT_BASE map to the normal spin_* calls.
  11404. + */
  11405. +#ifdef CONFIG_PREEMPT_RT_FULL
  11406. +# define spin_lock_local(lock) rt_spin_lock__no_mg(lock)
  11407. +# define spin_trylock_local(lock) rt_spin_trylock__no_mg(lock)
  11408. +# define spin_unlock_local(lock) rt_spin_unlock__no_mg(lock)
  11409. +#else
  11410. +# define spin_lock_local(lock) spin_lock(lock)
  11411. +# define spin_trylock_local(lock) spin_trylock(lock)
  11412. +# define spin_unlock_local(lock) spin_unlock(lock)
  11413. +#endif
  11414. +
  11415. +static inline void __local_lock(struct local_irq_lock *lv)
  11416. +{
  11417. + if (lv->owner != current) {
  11418. + spin_lock_local(&lv->lock);
  11419. + LL_WARN(lv->owner);
  11420. + LL_WARN(lv->nestcnt);
  11421. + lv->owner = current;
  11422. + }
  11423. + lv->nestcnt++;
  11424. +}
  11425. +
  11426. +#define local_lock(lvar) \
  11427. + do { __local_lock(&get_local_var(lvar)); } while (0)
  11428. +
  11429. +#define local_lock_on(lvar, cpu) \
  11430. + do { __local_lock(&per_cpu(lvar, cpu)); } while (0)
  11431. +
  11432. +static inline int __local_trylock(struct local_irq_lock *lv)
  11433. +{
  11434. + if (lv->owner != current && spin_trylock_local(&lv->lock)) {
  11435. + LL_WARN(lv->owner);
  11436. + LL_WARN(lv->nestcnt);
  11437. + lv->owner = current;
  11438. + lv->nestcnt = 1;
  11439. + return 1;
  11440. + }
  11441. + return 0;
  11442. +}
  11443. +
  11444. +#define local_trylock(lvar) \
  11445. + ({ \
  11446. + int __locked; \
  11447. + __locked = __local_trylock(&get_local_var(lvar)); \
  11448. + if (!__locked) \
  11449. + put_local_var(lvar); \
  11450. + __locked; \
  11451. + })
  11452. +
  11453. +static inline void __local_unlock(struct local_irq_lock *lv)
  11454. +{
  11455. + LL_WARN(lv->nestcnt == 0);
  11456. + LL_WARN(lv->owner != current);
  11457. + if (--lv->nestcnt)
  11458. + return;
  11459. +
  11460. + lv->owner = NULL;
  11461. + spin_unlock_local(&lv->lock);
  11462. +}
  11463. +
  11464. +#define local_unlock(lvar) \
  11465. + do { \
  11466. + __local_unlock(this_cpu_ptr(&lvar)); \
  11467. + put_local_var(lvar); \
  11468. + } while (0)
  11469. +
  11470. +#define local_unlock_on(lvar, cpu) \
  11471. + do { __local_unlock(&per_cpu(lvar, cpu)); } while (0)
  11472. +
  11473. +static inline void __local_lock_irq(struct local_irq_lock *lv)
  11474. +{
  11475. + spin_lock_irqsave(&lv->lock, lv->flags);
  11476. + LL_WARN(lv->owner);
  11477. + LL_WARN(lv->nestcnt);
  11478. + lv->owner = current;
  11479. + lv->nestcnt = 1;
  11480. +}
  11481. +
  11482. +#define local_lock_irq(lvar) \
  11483. + do { __local_lock_irq(&get_local_var(lvar)); } while (0)
  11484. +
  11485. +#define local_lock_irq_on(lvar, cpu) \
  11486. + do { __local_lock_irq(&per_cpu(lvar, cpu)); } while (0)
  11487. +
  11488. +static inline void __local_unlock_irq(struct local_irq_lock *lv)
  11489. +{
  11490. + LL_WARN(!lv->nestcnt);
  11491. + LL_WARN(lv->owner != current);
  11492. + lv->owner = NULL;
  11493. + lv->nestcnt = 0;
  11494. + spin_unlock_irq(&lv->lock);
  11495. +}
  11496. +
  11497. +#define local_unlock_irq(lvar) \
  11498. + do { \
  11499. + __local_unlock_irq(this_cpu_ptr(&lvar)); \
  11500. + put_local_var(lvar); \
  11501. + } while (0)
  11502. +
  11503. +#define local_unlock_irq_on(lvar, cpu) \
  11504. + do { \
  11505. + __local_unlock_irq(&per_cpu(lvar, cpu)); \
  11506. + } while (0)
  11507. +
  11508. +static inline int __local_lock_irqsave(struct local_irq_lock *lv)
  11509. +{
  11510. + if (lv->owner != current) {
  11511. + __local_lock_irq(lv);
  11512. + return 0;
  11513. + } else {
  11514. + lv->nestcnt++;
  11515. + return 1;
  11516. + }
  11517. +}
  11518. +
  11519. +#define local_lock_irqsave(lvar, _flags) \
  11520. + do { \
  11521. + if (__local_lock_irqsave(&get_local_var(lvar))) \
  11522. + put_local_var(lvar); \
  11523. + _flags = __this_cpu_read(lvar.flags); \
  11524. + } while (0)
  11525. +
  11526. +#define local_lock_irqsave_on(lvar, _flags, cpu) \
  11527. + do { \
  11528. + __local_lock_irqsave(&per_cpu(lvar, cpu)); \
  11529. + _flags = per_cpu(lvar, cpu).flags; \
  11530. + } while (0)
  11531. +
  11532. +static inline int __local_unlock_irqrestore(struct local_irq_lock *lv,
  11533. + unsigned long flags)
  11534. +{
  11535. + LL_WARN(!lv->nestcnt);
  11536. + LL_WARN(lv->owner != current);
  11537. + if (--lv->nestcnt)
  11538. + return 0;
  11539. +
  11540. + lv->owner = NULL;
  11541. + spin_unlock_irqrestore(&lv->lock, lv->flags);
  11542. + return 1;
  11543. +}
  11544. +
  11545. +#define local_unlock_irqrestore(lvar, flags) \
  11546. + do { \
  11547. + if (__local_unlock_irqrestore(this_cpu_ptr(&lvar), flags)) \
  11548. + put_local_var(lvar); \
  11549. + } while (0)
  11550. +
  11551. +#define local_unlock_irqrestore_on(lvar, flags, cpu) \
  11552. + do { \
  11553. + __local_unlock_irqrestore(&per_cpu(lvar, cpu), flags); \
  11554. + } while (0)
  11555. +
  11556. +#define local_spin_trylock_irq(lvar, lock) \
  11557. + ({ \
  11558. + int __locked; \
  11559. + local_lock_irq(lvar); \
  11560. + __locked = spin_trylock(lock); \
  11561. + if (!__locked) \
  11562. + local_unlock_irq(lvar); \
  11563. + __locked; \
  11564. + })
  11565. +
  11566. +#define local_spin_lock_irq(lvar, lock) \
  11567. + do { \
  11568. + local_lock_irq(lvar); \
  11569. + spin_lock(lock); \
  11570. + } while (0)
  11571. +
  11572. +#define local_spin_unlock_irq(lvar, lock) \
  11573. + do { \
  11574. + spin_unlock(lock); \
  11575. + local_unlock_irq(lvar); \
  11576. + } while (0)
  11577. +
  11578. +#define local_spin_lock_irqsave(lvar, lock, flags) \
  11579. + do { \
  11580. + local_lock_irqsave(lvar, flags); \
  11581. + spin_lock(lock); \
  11582. + } while (0)
  11583. +
  11584. +#define local_spin_unlock_irqrestore(lvar, lock, flags) \
  11585. + do { \
  11586. + spin_unlock(lock); \
  11587. + local_unlock_irqrestore(lvar, flags); \
  11588. + } while (0)
  11589. +
  11590. +#define get_locked_var(lvar, var) \
  11591. + (*({ \
  11592. + local_lock(lvar); \
  11593. + this_cpu_ptr(&var); \
  11594. + }))
  11595. +
  11596. +#define put_locked_var(lvar, var) local_unlock(lvar);
  11597. +
  11598. +#define local_lock_cpu(lvar) \
  11599. + ({ \
  11600. + local_lock(lvar); \
  11601. + smp_processor_id(); \
  11602. + })
  11603. +
  11604. +#define local_unlock_cpu(lvar) local_unlock(lvar)
  11605. +
  11606. +#else /* PREEMPT_RT_BASE */
  11607. +
  11608. +#define DEFINE_LOCAL_IRQ_LOCK(lvar) __typeof__(const int) lvar
  11609. +#define DECLARE_LOCAL_IRQ_LOCK(lvar) extern __typeof__(const int) lvar
  11610. +
  11611. +static inline void local_irq_lock_init(int lvar) { }
  11612. +
  11613. +#define local_lock(lvar) preempt_disable()
  11614. +#define local_unlock(lvar) preempt_enable()
  11615. +#define local_lock_irq(lvar) local_irq_disable()
  11616. +#define local_unlock_irq(lvar) local_irq_enable()
  11617. +#define local_lock_irqsave(lvar, flags) local_irq_save(flags)
  11618. +#define local_unlock_irqrestore(lvar, flags) local_irq_restore(flags)
  11619. +
  11620. +#define local_spin_trylock_irq(lvar, lock) spin_trylock_irq(lock)
  11621. +#define local_spin_lock_irq(lvar, lock) spin_lock_irq(lock)
  11622. +#define local_spin_unlock_irq(lvar, lock) spin_unlock_irq(lock)
  11623. +#define local_spin_lock_irqsave(lvar, lock, flags) \
  11624. + spin_lock_irqsave(lock, flags)
  11625. +#define local_spin_unlock_irqrestore(lvar, lock, flags) \
  11626. + spin_unlock_irqrestore(lock, flags)
  11627. +
  11628. +#define get_locked_var(lvar, var) get_cpu_var(var)
  11629. +#define put_locked_var(lvar, var) put_cpu_var(var)
  11630. +
  11631. +#define local_lock_cpu(lvar) get_cpu()
  11632. +#define local_unlock_cpu(lvar) put_cpu()
  11633. +
  11634. +#endif
  11635. +
  11636. +#endif
  11637. diff -Nur linux-4.4.46.orig/include/linux/mm_types.h linux-4.4.46/include/linux/mm_types.h
  11638. --- linux-4.4.46.orig/include/linux/mm_types.h 2017-02-01 08:31:11.000000000 +0100
  11639. +++ linux-4.4.46/include/linux/mm_types.h 2017-02-03 17:18:10.907618284 +0100
  11640. @@ -11,6 +11,7 @@
  11641. #include <linux/completion.h>
  11642. #include <linux/cpumask.h>
  11643. #include <linux/uprobes.h>
  11644. +#include <linux/rcupdate.h>
  11645. #include <linux/page-flags-layout.h>
  11646. #include <asm/page.h>
  11647. #include <asm/mmu.h>
  11648. @@ -505,6 +506,9 @@
  11649. bool tlb_flush_pending;
  11650. #endif
  11651. struct uprobes_state uprobes_state;
  11652. +#ifdef CONFIG_PREEMPT_RT_BASE
  11653. + struct rcu_head delayed_drop;
  11654. +#endif
  11655. #ifdef CONFIG_X86_INTEL_MPX
  11656. /* address of the bounds directory */
  11657. void __user *bd_addr;
  11658. diff -Nur linux-4.4.46.orig/include/linux/mutex.h linux-4.4.46/include/linux/mutex.h
  11659. --- linux-4.4.46.orig/include/linux/mutex.h 2017-02-01 08:31:11.000000000 +0100
  11660. +++ linux-4.4.46/include/linux/mutex.h 2017-02-03 17:18:10.907618284 +0100
  11661. @@ -19,6 +19,17 @@
  11662. #include <asm/processor.h>
  11663. #include <linux/osq_lock.h>
  11664. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  11665. +# define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
  11666. + , .dep_map = { .name = #lockname }
  11667. +#else
  11668. +# define __DEP_MAP_MUTEX_INITIALIZER(lockname)
  11669. +#endif
  11670. +
  11671. +#ifdef CONFIG_PREEMPT_RT_FULL
  11672. +# include <linux/mutex_rt.h>
  11673. +#else
  11674. +
  11675. /*
  11676. * Simple, straightforward mutexes with strict semantics:
  11677. *
  11678. @@ -99,13 +110,6 @@
  11679. static inline void mutex_destroy(struct mutex *lock) {}
  11680. #endif
  11681. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  11682. -# define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
  11683. - , .dep_map = { .name = #lockname }
  11684. -#else
  11685. -# define __DEP_MAP_MUTEX_INITIALIZER(lockname)
  11686. -#endif
  11687. -
  11688. #define __MUTEX_INITIALIZER(lockname) \
  11689. { .count = ATOMIC_INIT(1) \
  11690. , .wait_lock = __SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
  11691. @@ -173,6 +177,8 @@
  11692. extern int mutex_trylock(struct mutex *lock);
  11693. extern void mutex_unlock(struct mutex *lock);
  11694. +#endif /* !PREEMPT_RT_FULL */
  11695. +
  11696. extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);
  11697. #endif /* __LINUX_MUTEX_H */
  11698. diff -Nur linux-4.4.46.orig/include/linux/mutex_rt.h linux-4.4.46/include/linux/mutex_rt.h
  11699. --- linux-4.4.46.orig/include/linux/mutex_rt.h 1970-01-01 01:00:00.000000000 +0100
  11700. +++ linux-4.4.46/include/linux/mutex_rt.h 2017-02-03 17:18:10.907618284 +0100
  11701. @@ -0,0 +1,84 @@
  11702. +#ifndef __LINUX_MUTEX_RT_H
  11703. +#define __LINUX_MUTEX_RT_H
  11704. +
  11705. +#ifndef __LINUX_MUTEX_H
  11706. +#error "Please include mutex.h"
  11707. +#endif
  11708. +
  11709. +#include <linux/rtmutex.h>
  11710. +
  11711. +/* FIXME: Just for __lockfunc */
  11712. +#include <linux/spinlock.h>
  11713. +
  11714. +struct mutex {
  11715. + struct rt_mutex lock;
  11716. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  11717. + struct lockdep_map dep_map;
  11718. +#endif
  11719. +};
  11720. +
  11721. +#define __MUTEX_INITIALIZER(mutexname) \
  11722. + { \
  11723. + .lock = __RT_MUTEX_INITIALIZER(mutexname.lock) \
  11724. + __DEP_MAP_MUTEX_INITIALIZER(mutexname) \
  11725. + }
  11726. +
  11727. +#define DEFINE_MUTEX(mutexname) \
  11728. + struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
  11729. +
  11730. +extern void __mutex_do_init(struct mutex *lock, const char *name, struct lock_class_key *key);
  11731. +extern void __lockfunc _mutex_lock(struct mutex *lock);
  11732. +extern int __lockfunc _mutex_lock_interruptible(struct mutex *lock);
  11733. +extern int __lockfunc _mutex_lock_killable(struct mutex *lock);
  11734. +extern void __lockfunc _mutex_lock_nested(struct mutex *lock, int subclass);
  11735. +extern void __lockfunc _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest_lock);
  11736. +extern int __lockfunc _mutex_lock_interruptible_nested(struct mutex *lock, int subclass);
  11737. +extern int __lockfunc _mutex_lock_killable_nested(struct mutex *lock, int subclass);
  11738. +extern int __lockfunc _mutex_trylock(struct mutex *lock);
  11739. +extern void __lockfunc _mutex_unlock(struct mutex *lock);
  11740. +
  11741. +#define mutex_is_locked(l) rt_mutex_is_locked(&(l)->lock)
  11742. +#define mutex_lock(l) _mutex_lock(l)
  11743. +#define mutex_lock_interruptible(l) _mutex_lock_interruptible(l)
  11744. +#define mutex_lock_killable(l) _mutex_lock_killable(l)
  11745. +#define mutex_trylock(l) _mutex_trylock(l)
  11746. +#define mutex_unlock(l) _mutex_unlock(l)
  11747. +#define mutex_destroy(l) rt_mutex_destroy(&(l)->lock)
  11748. +
  11749. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  11750. +# define mutex_lock_nested(l, s) _mutex_lock_nested(l, s)
  11751. +# define mutex_lock_interruptible_nested(l, s) \
  11752. + _mutex_lock_interruptible_nested(l, s)
  11753. +# define mutex_lock_killable_nested(l, s) \
  11754. + _mutex_lock_killable_nested(l, s)
  11755. +
  11756. +# define mutex_lock_nest_lock(lock, nest_lock) \
  11757. +do { \
  11758. + typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
  11759. + _mutex_lock_nest_lock(lock, &(nest_lock)->dep_map); \
  11760. +} while (0)
  11761. +
  11762. +#else
  11763. +# define mutex_lock_nested(l, s) _mutex_lock(l)
  11764. +# define mutex_lock_interruptible_nested(l, s) \
  11765. + _mutex_lock_interruptible(l)
  11766. +# define mutex_lock_killable_nested(l, s) \
  11767. + _mutex_lock_killable(l)
  11768. +# define mutex_lock_nest_lock(lock, nest_lock) mutex_lock(lock)
  11769. +#endif
  11770. +
  11771. +# define mutex_init(mutex) \
  11772. +do { \
  11773. + static struct lock_class_key __key; \
  11774. + \
  11775. + rt_mutex_init(&(mutex)->lock); \
  11776. + __mutex_do_init((mutex), #mutex, &__key); \
  11777. +} while (0)
  11778. +
  11779. +# define __mutex_init(mutex, name, key) \
  11780. +do { \
  11781. + rt_mutex_init(&(mutex)->lock); \
  11782. + __mutex_do_init((mutex), name, key); \
  11783. +} while (0)
  11784. +
  11785. +#endif
  11786. diff -Nur linux-4.4.46.orig/include/linux/netdevice.h linux-4.4.46/include/linux/netdevice.h
  11787. --- linux-4.4.46.orig/include/linux/netdevice.h 2017-02-01 08:31:11.000000000 +0100
  11788. +++ linux-4.4.46/include/linux/netdevice.h 2017-02-03 17:18:10.907618284 +0100
  11789. @@ -2286,11 +2286,20 @@
  11790. void synchronize_net(void);
  11791. int init_dummy_netdev(struct net_device *dev);
  11792. +#ifdef CONFIG_PREEMPT_RT_FULL
  11793. +static inline int dev_recursion_level(void)
  11794. +{
  11795. + return current->xmit_recursion;
  11796. +}
  11797. +
  11798. +#else
  11799. +
  11800. DECLARE_PER_CPU(int, xmit_recursion);
  11801. static inline int dev_recursion_level(void)
  11802. {
  11803. return this_cpu_read(xmit_recursion);
  11804. }
  11805. +#endif
  11806. struct net_device *dev_get_by_index(struct net *net, int ifindex);
  11807. struct net_device *__dev_get_by_index(struct net *net, int ifindex);
  11808. @@ -2606,6 +2615,7 @@
  11809. unsigned int dropped;
  11810. struct sk_buff_head input_pkt_queue;
  11811. struct napi_struct backlog;
  11812. + struct sk_buff_head tofree_queue;
  11813. };
  11814. diff -Nur linux-4.4.46.orig/include/linux/netfilter/x_tables.h linux-4.4.46/include/linux/netfilter/x_tables.h
  11815. --- linux-4.4.46.orig/include/linux/netfilter/x_tables.h 2017-02-01 08:31:11.000000000 +0100
  11816. +++ linux-4.4.46/include/linux/netfilter/x_tables.h 2017-02-03 17:18:10.907618284 +0100
  11817. @@ -4,6 +4,7 @@
  11818. #include <linux/netdevice.h>
  11819. #include <linux/static_key.h>
  11820. +#include <linux/locallock.h>
  11821. #include <uapi/linux/netfilter/x_tables.h>
  11822. /**
  11823. @@ -289,6 +290,8 @@
  11824. */
  11825. DECLARE_PER_CPU(seqcount_t, xt_recseq);
  11826. +DECLARE_LOCAL_IRQ_LOCK(xt_write_lock);
  11827. +
  11828. /* xt_tee_enabled - true if x_tables needs to handle reentrancy
  11829. *
  11830. * Enabled if current ip(6)tables ruleset has at least one -j TEE rule.
  11831. @@ -309,6 +312,9 @@
  11832. {
  11833. unsigned int addend;
  11834. + /* RT protection */
  11835. + local_lock(xt_write_lock);
  11836. +
  11837. /*
  11838. * Low order bit of sequence is set if we already
  11839. * called xt_write_recseq_begin().
  11840. @@ -339,6 +345,7 @@
  11841. /* this is kind of a write_seqcount_end(), but addend is 0 or 1 */
  11842. smp_wmb();
  11843. __this_cpu_add(xt_recseq.sequence, addend);
  11844. + local_unlock(xt_write_lock);
  11845. }
  11846. /*
  11847. diff -Nur linux-4.4.46.orig/include/linux/notifier.h linux-4.4.46/include/linux/notifier.h
  11848. --- linux-4.4.46.orig/include/linux/notifier.h 2017-02-01 08:31:11.000000000 +0100
  11849. +++ linux-4.4.46/include/linux/notifier.h 2017-02-03 17:18:10.907618284 +0100
  11850. @@ -6,7 +6,7 @@
  11851. *
  11852. * Alan Cox <Alan.Cox@linux.org>
  11853. */
  11854. -
  11855. +
  11856. #ifndef _LINUX_NOTIFIER_H
  11857. #define _LINUX_NOTIFIER_H
  11858. #include <linux/errno.h>
  11859. @@ -42,9 +42,7 @@
  11860. * in srcu_notifier_call_chain(): no cache bounces and no memory barriers.
  11861. * As compensation, srcu_notifier_chain_unregister() is rather expensive.
  11862. * SRCU notifier chains should be used when the chain will be called very
  11863. - * often but notifier_blocks will seldom be removed. Also, SRCU notifier
  11864. - * chains are slightly more difficult to use because they require special
  11865. - * runtime initialization.
  11866. + * often but notifier_blocks will seldom be removed.
  11867. */
  11868. typedef int (*notifier_fn_t)(struct notifier_block *nb,
  11869. @@ -88,7 +86,7 @@
  11870. (name)->head = NULL; \
  11871. } while (0)
  11872. -/* srcu_notifier_heads must be initialized and cleaned up dynamically */
  11873. +/* srcu_notifier_heads must be cleaned up dynamically */
  11874. extern void srcu_init_notifier_head(struct srcu_notifier_head *nh);
  11875. #define srcu_cleanup_notifier_head(name) \
  11876. cleanup_srcu_struct(&(name)->srcu);
  11877. @@ -101,7 +99,13 @@
  11878. .head = NULL }
  11879. #define RAW_NOTIFIER_INIT(name) { \
  11880. .head = NULL }
  11881. -/* srcu_notifier_heads cannot be initialized statically */
  11882. +
  11883. +#define SRCU_NOTIFIER_INIT(name, pcpu) \
  11884. + { \
  11885. + .mutex = __MUTEX_INITIALIZER(name.mutex), \
  11886. + .head = NULL, \
  11887. + .srcu = __SRCU_STRUCT_INIT(name.srcu, pcpu), \
  11888. + }
  11889. #define ATOMIC_NOTIFIER_HEAD(name) \
  11890. struct atomic_notifier_head name = \
  11891. @@ -113,6 +117,18 @@
  11892. struct raw_notifier_head name = \
  11893. RAW_NOTIFIER_INIT(name)
  11894. +#define _SRCU_NOTIFIER_HEAD(name, mod) \
  11895. + static DEFINE_PER_CPU(struct srcu_struct_array, \
  11896. + name##_head_srcu_array); \
  11897. + mod struct srcu_notifier_head name = \
  11898. + SRCU_NOTIFIER_INIT(name, name##_head_srcu_array)
  11899. +
  11900. +#define SRCU_NOTIFIER_HEAD(name) \
  11901. + _SRCU_NOTIFIER_HEAD(name, )
  11902. +
  11903. +#define SRCU_NOTIFIER_HEAD_STATIC(name) \
  11904. + _SRCU_NOTIFIER_HEAD(name, static)
  11905. +
  11906. #ifdef __KERNEL__
  11907. extern int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
  11908. @@ -182,12 +198,12 @@
  11909. /*
  11910. * Declared notifiers so far. I can imagine quite a few more chains
  11911. - * over time (eg laptop power reset chains, reboot chain (to clean
  11912. + * over time (eg laptop power reset chains, reboot chain (to clean
  11913. * device units up), device [un]mount chain, module load/unload chain,
  11914. - * low memory chain, screenblank chain (for plug in modular screenblankers)
  11915. + * low memory chain, screenblank chain (for plug in modular screenblankers)
  11916. * VC switch chains (for loadable kernel svgalib VC switch helpers) etc...
  11917. */
  11918. -
  11919. +
  11920. /* CPU notfiers are defined in include/linux/cpu.h. */
  11921. /* netdevice notifiers are defined in include/linux/netdevice.h */
  11922. diff -Nur linux-4.4.46.orig/include/linux/percpu.h linux-4.4.46/include/linux/percpu.h
  11923. --- linux-4.4.46.orig/include/linux/percpu.h 2017-02-01 08:31:11.000000000 +0100
  11924. +++ linux-4.4.46/include/linux/percpu.h 2017-02-03 17:18:10.907618284 +0100
  11925. @@ -24,6 +24,35 @@
  11926. PERCPU_MODULE_RESERVE)
  11927. #endif
  11928. +#ifdef CONFIG_PREEMPT_RT_FULL
  11929. +
  11930. +#define get_local_var(var) (*({ \
  11931. + migrate_disable(); \
  11932. + this_cpu_ptr(&var); }))
  11933. +
  11934. +#define put_local_var(var) do { \
  11935. + (void)&(var); \
  11936. + migrate_enable(); \
  11937. +} while (0)
  11938. +
  11939. +# define get_local_ptr(var) ({ \
  11940. + migrate_disable(); \
  11941. + this_cpu_ptr(var); })
  11942. +
  11943. +# define put_local_ptr(var) do { \
  11944. + (void)(var); \
  11945. + migrate_enable(); \
  11946. +} while (0)
  11947. +
  11948. +#else
  11949. +
  11950. +#define get_local_var(var) get_cpu_var(var)
  11951. +#define put_local_var(var) put_cpu_var(var)
  11952. +#define get_local_ptr(var) get_cpu_ptr(var)
  11953. +#define put_local_ptr(var) put_cpu_ptr(var)
  11954. +
  11955. +#endif
  11956. +
  11957. /* minimum unit size, also is the maximum supported allocation size */
  11958. #define PCPU_MIN_UNIT_SIZE PFN_ALIGN(32 << 10)
  11959. diff -Nur linux-4.4.46.orig/include/linux/pid.h linux-4.4.46/include/linux/pid.h
  11960. --- linux-4.4.46.orig/include/linux/pid.h 2017-02-01 08:31:11.000000000 +0100
  11961. +++ linux-4.4.46/include/linux/pid.h 2017-02-03 17:18:10.907618284 +0100
  11962. @@ -2,6 +2,7 @@
  11963. #define _LINUX_PID_H
  11964. #include <linux/rcupdate.h>
  11965. +#include <linux/atomic.h>
  11966. enum pid_type
  11967. {
  11968. diff -Nur linux-4.4.46.orig/include/linux/preempt.h linux-4.4.46/include/linux/preempt.h
  11969. --- linux-4.4.46.orig/include/linux/preempt.h 2017-02-01 08:31:11.000000000 +0100
  11970. +++ linux-4.4.46/include/linux/preempt.h 2017-02-03 17:18:10.907618284 +0100
  11971. @@ -50,7 +50,11 @@
  11972. #define HARDIRQ_OFFSET (1UL << HARDIRQ_SHIFT)
  11973. #define NMI_OFFSET (1UL << NMI_SHIFT)
  11974. -#define SOFTIRQ_DISABLE_OFFSET (2 * SOFTIRQ_OFFSET)
  11975. +#ifndef CONFIG_PREEMPT_RT_FULL
  11976. +# define SOFTIRQ_DISABLE_OFFSET (2 * SOFTIRQ_OFFSET)
  11977. +#else
  11978. +# define SOFTIRQ_DISABLE_OFFSET (0)
  11979. +#endif
  11980. /* We use the MSB mostly because its available */
  11981. #define PREEMPT_NEED_RESCHED 0x80000000
  11982. @@ -59,9 +63,15 @@
  11983. #include <asm/preempt.h>
  11984. #define hardirq_count() (preempt_count() & HARDIRQ_MASK)
  11985. -#define softirq_count() (preempt_count() & SOFTIRQ_MASK)
  11986. #define irq_count() (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_MASK \
  11987. | NMI_MASK))
  11988. +#ifndef CONFIG_PREEMPT_RT_FULL
  11989. +# define softirq_count() (preempt_count() & SOFTIRQ_MASK)
  11990. +# define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET)
  11991. +#else
  11992. +# define softirq_count() (0UL)
  11993. +extern int in_serving_softirq(void);
  11994. +#endif
  11995. /*
  11996. * Are we doing bottom half or hardware interrupt processing?
  11997. @@ -72,7 +82,6 @@
  11998. #define in_irq() (hardirq_count())
  11999. #define in_softirq() (softirq_count())
  12000. #define in_interrupt() (irq_count())
  12001. -#define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET)
  12002. /*
  12003. * Are we in NMI context?
  12004. @@ -91,7 +100,11 @@
  12005. /*
  12006. * The preempt_count offset after spin_lock()
  12007. */
  12008. +#if !defined(CONFIG_PREEMPT_RT_FULL)
  12009. #define PREEMPT_LOCK_OFFSET PREEMPT_DISABLE_OFFSET
  12010. +#else
  12011. +#define PREEMPT_LOCK_OFFSET 0
  12012. +#endif
  12013. /*
  12014. * The preempt_count offset needed for things like:
  12015. @@ -140,6 +153,20 @@
  12016. #define preempt_count_inc() preempt_count_add(1)
  12017. #define preempt_count_dec() preempt_count_sub(1)
  12018. +#ifdef CONFIG_PREEMPT_LAZY
  12019. +#define add_preempt_lazy_count(val) do { preempt_lazy_count() += (val); } while (0)
  12020. +#define sub_preempt_lazy_count(val) do { preempt_lazy_count() -= (val); } while (0)
  12021. +#define inc_preempt_lazy_count() add_preempt_lazy_count(1)
  12022. +#define dec_preempt_lazy_count() sub_preempt_lazy_count(1)
  12023. +#define preempt_lazy_count() (current_thread_info()->preempt_lazy_count)
  12024. +#else
  12025. +#define add_preempt_lazy_count(val) do { } while (0)
  12026. +#define sub_preempt_lazy_count(val) do { } while (0)
  12027. +#define inc_preempt_lazy_count() do { } while (0)
  12028. +#define dec_preempt_lazy_count() do { } while (0)
  12029. +#define preempt_lazy_count() (0)
  12030. +#endif
  12031. +
  12032. #ifdef CONFIG_PREEMPT_COUNT
  12033. #define preempt_disable() \
  12034. @@ -148,13 +175,25 @@
  12035. barrier(); \
  12036. } while (0)
  12037. +#define preempt_lazy_disable() \
  12038. +do { \
  12039. + inc_preempt_lazy_count(); \
  12040. + barrier(); \
  12041. +} while (0)
  12042. +
  12043. #define sched_preempt_enable_no_resched() \
  12044. do { \
  12045. barrier(); \
  12046. preempt_count_dec(); \
  12047. } while (0)
  12048. -#define preempt_enable_no_resched() sched_preempt_enable_no_resched()
  12049. +#ifdef CONFIG_PREEMPT_RT_BASE
  12050. +# define preempt_enable_no_resched() sched_preempt_enable_no_resched()
  12051. +# define preempt_check_resched_rt() preempt_check_resched()
  12052. +#else
  12053. +# define preempt_enable_no_resched() preempt_enable()
  12054. +# define preempt_check_resched_rt() barrier();
  12055. +#endif
  12056. #define preemptible() (preempt_count() == 0 && !irqs_disabled())
  12057. @@ -179,6 +218,13 @@
  12058. __preempt_schedule(); \
  12059. } while (0)
  12060. +#define preempt_lazy_enable() \
  12061. +do { \
  12062. + dec_preempt_lazy_count(); \
  12063. + barrier(); \
  12064. + preempt_check_resched(); \
  12065. +} while (0)
  12066. +
  12067. #else /* !CONFIG_PREEMPT */
  12068. #define preempt_enable() \
  12069. do { \
  12070. @@ -224,6 +270,7 @@
  12071. #define preempt_disable_notrace() barrier()
  12072. #define preempt_enable_no_resched_notrace() barrier()
  12073. #define preempt_enable_notrace() barrier()
  12074. +#define preempt_check_resched_rt() barrier()
  12075. #define preemptible() 0
  12076. #endif /* CONFIG_PREEMPT_COUNT */
  12077. @@ -244,10 +291,31 @@
  12078. } while (0)
  12079. #define preempt_fold_need_resched() \
  12080. do { \
  12081. - if (tif_need_resched()) \
  12082. + if (tif_need_resched_now()) \
  12083. set_preempt_need_resched(); \
  12084. } while (0)
  12085. +#ifdef CONFIG_PREEMPT_RT_FULL
  12086. +# define preempt_disable_rt() preempt_disable()
  12087. +# define preempt_enable_rt() preempt_enable()
  12088. +# define preempt_disable_nort() barrier()
  12089. +# define preempt_enable_nort() barrier()
  12090. +# ifdef CONFIG_SMP
  12091. + extern void migrate_disable(void);
  12092. + extern void migrate_enable(void);
  12093. +# else /* CONFIG_SMP */
  12094. +# define migrate_disable() barrier()
  12095. +# define migrate_enable() barrier()
  12096. +# endif /* CONFIG_SMP */
  12097. +#else
  12098. +# define preempt_disable_rt() barrier()
  12099. +# define preempt_enable_rt() barrier()
  12100. +# define preempt_disable_nort() preempt_disable()
  12101. +# define preempt_enable_nort() preempt_enable()
  12102. +# define migrate_disable() preempt_disable()
  12103. +# define migrate_enable() preempt_enable()
  12104. +#endif
  12105. +
  12106. #ifdef CONFIG_PREEMPT_NOTIFIERS
  12107. struct preempt_notifier;
  12108. diff -Nur linux-4.4.46.orig/include/linux/printk.h linux-4.4.46/include/linux/printk.h
  12109. --- linux-4.4.46.orig/include/linux/printk.h 2017-02-01 08:31:11.000000000 +0100
  12110. +++ linux-4.4.46/include/linux/printk.h 2017-02-03 17:18:10.907618284 +0100
  12111. @@ -117,9 +117,11 @@
  12112. #ifdef CONFIG_EARLY_PRINTK
  12113. extern asmlinkage __printf(1, 2)
  12114. void early_printk(const char *fmt, ...);
  12115. +extern void printk_kill(void);
  12116. #else
  12117. static inline __printf(1, 2) __cold
  12118. void early_printk(const char *s, ...) { }
  12119. +static inline void printk_kill(void) { }
  12120. #endif
  12121. typedef __printf(1, 0) int (*printk_func_t)(const char *fmt, va_list args);
  12122. diff -Nur linux-4.4.46.orig/include/linux/radix-tree.h linux-4.4.46/include/linux/radix-tree.h
  12123. --- linux-4.4.46.orig/include/linux/radix-tree.h 2017-02-01 08:31:11.000000000 +0100
  12124. +++ linux-4.4.46/include/linux/radix-tree.h 2017-02-03 17:18:10.907618284 +0100
  12125. @@ -277,8 +277,13 @@
  12126. unsigned int radix_tree_gang_lookup_slot(struct radix_tree_root *root,
  12127. void ***results, unsigned long *indices,
  12128. unsigned long first_index, unsigned int max_items);
  12129. +#ifndef CONFIG_PREEMPT_RT_FULL
  12130. int radix_tree_preload(gfp_t gfp_mask);
  12131. int radix_tree_maybe_preload(gfp_t gfp_mask);
  12132. +#else
  12133. +static inline int radix_tree_preload(gfp_t gm) { return 0; }
  12134. +static inline int radix_tree_maybe_preload(gfp_t gfp_mask) { return 0; }
  12135. +#endif
  12136. void radix_tree_init(void);
  12137. void *radix_tree_tag_set(struct radix_tree_root *root,
  12138. unsigned long index, unsigned int tag);
  12139. @@ -303,7 +308,7 @@
  12140. static inline void radix_tree_preload_end(void)
  12141. {
  12142. - preempt_enable();
  12143. + preempt_enable_nort();
  12144. }
  12145. /**
  12146. diff -Nur linux-4.4.46.orig/include/linux/random.h linux-4.4.46/include/linux/random.h
  12147. --- linux-4.4.46.orig/include/linux/random.h 2017-02-01 08:31:11.000000000 +0100
  12148. +++ linux-4.4.46/include/linux/random.h 2017-02-03 17:18:10.907618284 +0100
  12149. @@ -20,7 +20,7 @@
  12150. extern void add_device_randomness(const void *, unsigned int);
  12151. extern void add_input_randomness(unsigned int type, unsigned int code,
  12152. unsigned int value);
  12153. -extern void add_interrupt_randomness(int irq, int irq_flags);
  12154. +extern void add_interrupt_randomness(int irq, int irq_flags, __u64 ip);
  12155. extern void get_random_bytes(void *buf, int nbytes);
  12156. extern int add_random_ready_callback(struct random_ready_callback *rdy);
  12157. diff -Nur linux-4.4.46.orig/include/linux/rbtree.h linux-4.4.46/include/linux/rbtree.h
  12158. --- linux-4.4.46.orig/include/linux/rbtree.h 2017-02-01 08:31:11.000000000 +0100
  12159. +++ linux-4.4.46/include/linux/rbtree.h 2017-02-03 17:18:10.907618284 +0100
  12160. @@ -31,7 +31,6 @@
  12161. #include <linux/kernel.h>
  12162. #include <linux/stddef.h>
  12163. -#include <linux/rcupdate.h>
  12164. struct rb_node {
  12165. unsigned long __rb_parent_color;
  12166. @@ -86,14 +85,8 @@
  12167. *rb_link = node;
  12168. }
  12169. -static inline void rb_link_node_rcu(struct rb_node *node, struct rb_node *parent,
  12170. - struct rb_node **rb_link)
  12171. -{
  12172. - node->__rb_parent_color = (unsigned long)parent;
  12173. - node->rb_left = node->rb_right = NULL;
  12174. -
  12175. - rcu_assign_pointer(*rb_link, node);
  12176. -}
  12177. +void rb_link_node_rcu(struct rb_node *node, struct rb_node *parent,
  12178. + struct rb_node **rb_link);
  12179. #define rb_entry_safe(ptr, type, member) \
  12180. ({ typeof(ptr) ____ptr = (ptr); \
  12181. diff -Nur linux-4.4.46.orig/include/linux/rcupdate.h linux-4.4.46/include/linux/rcupdate.h
  12182. --- linux-4.4.46.orig/include/linux/rcupdate.h 2017-02-01 08:31:11.000000000 +0100
  12183. +++ linux-4.4.46/include/linux/rcupdate.h 2017-02-03 17:18:10.907618284 +0100
  12184. @@ -169,6 +169,9 @@
  12185. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  12186. +#ifdef CONFIG_PREEMPT_RT_FULL
  12187. +#define call_rcu_bh call_rcu
  12188. +#else
  12189. /**
  12190. * call_rcu_bh() - Queue an RCU for invocation after a quicker grace period.
  12191. * @head: structure to be used for queueing the RCU updates.
  12192. @@ -192,6 +195,7 @@
  12193. */
  12194. void call_rcu_bh(struct rcu_head *head,
  12195. rcu_callback_t func);
  12196. +#endif
  12197. /**
  12198. * call_rcu_sched() - Queue an RCU for invocation after sched grace period.
  12199. @@ -292,6 +296,11 @@
  12200. * types of kernel builds, the rcu_read_lock() nesting depth is unknowable.
  12201. */
  12202. #define rcu_preempt_depth() (current->rcu_read_lock_nesting)
  12203. +#ifndef CONFIG_PREEMPT_RT_FULL
  12204. +#define sched_rcu_preempt_depth() rcu_preempt_depth()
  12205. +#else
  12206. +static inline int sched_rcu_preempt_depth(void) { return 0; }
  12207. +#endif
  12208. #else /* #ifdef CONFIG_PREEMPT_RCU */
  12209. @@ -317,6 +326,8 @@
  12210. return 0;
  12211. }
  12212. +#define sched_rcu_preempt_depth() rcu_preempt_depth()
  12213. +
  12214. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  12215. /* Internal to kernel */
  12216. @@ -489,7 +500,14 @@
  12217. int debug_lockdep_rcu_enabled(void);
  12218. int rcu_read_lock_held(void);
  12219. +#ifdef CONFIG_PREEMPT_RT_FULL
  12220. +static inline int rcu_read_lock_bh_held(void)
  12221. +{
  12222. + return rcu_read_lock_held();
  12223. +}
  12224. +#else
  12225. int rcu_read_lock_bh_held(void);
  12226. +#endif
  12227. /**
  12228. * rcu_read_lock_sched_held() - might we be in RCU-sched read-side critical section?
  12229. @@ -937,10 +955,14 @@
  12230. static inline void rcu_read_lock_bh(void)
  12231. {
  12232. local_bh_disable();
  12233. +#ifdef CONFIG_PREEMPT_RT_FULL
  12234. + rcu_read_lock();
  12235. +#else
  12236. __acquire(RCU_BH);
  12237. rcu_lock_acquire(&rcu_bh_lock_map);
  12238. RCU_LOCKDEP_WARN(!rcu_is_watching(),
  12239. "rcu_read_lock_bh() used illegally while idle");
  12240. +#endif
  12241. }
  12242. /*
  12243. @@ -950,10 +972,14 @@
  12244. */
  12245. static inline void rcu_read_unlock_bh(void)
  12246. {
  12247. +#ifdef CONFIG_PREEMPT_RT_FULL
  12248. + rcu_read_unlock();
  12249. +#else
  12250. RCU_LOCKDEP_WARN(!rcu_is_watching(),
  12251. "rcu_read_unlock_bh() used illegally while idle");
  12252. rcu_lock_release(&rcu_bh_lock_map);
  12253. __release(RCU_BH);
  12254. +#endif
  12255. local_bh_enable();
  12256. }
  12257. diff -Nur linux-4.4.46.orig/include/linux/rcutree.h linux-4.4.46/include/linux/rcutree.h
  12258. --- linux-4.4.46.orig/include/linux/rcutree.h 2017-02-01 08:31:11.000000000 +0100
  12259. +++ linux-4.4.46/include/linux/rcutree.h 2017-02-03 17:18:10.907618284 +0100
  12260. @@ -44,7 +44,11 @@
  12261. rcu_note_context_switch();
  12262. }
  12263. +#ifdef CONFIG_PREEMPT_RT_FULL
  12264. +# define synchronize_rcu_bh synchronize_rcu
  12265. +#else
  12266. void synchronize_rcu_bh(void);
  12267. +#endif
  12268. void synchronize_sched_expedited(void);
  12269. void synchronize_rcu_expedited(void);
  12270. @@ -72,7 +76,11 @@
  12271. }
  12272. void rcu_barrier(void);
  12273. +#ifdef CONFIG_PREEMPT_RT_FULL
  12274. +# define rcu_barrier_bh rcu_barrier
  12275. +#else
  12276. void rcu_barrier_bh(void);
  12277. +#endif
  12278. void rcu_barrier_sched(void);
  12279. unsigned long get_state_synchronize_rcu(void);
  12280. void cond_synchronize_rcu(unsigned long oldstate);
  12281. @@ -85,12 +93,10 @@
  12282. unsigned long rcu_batches_started_bh(void);
  12283. unsigned long rcu_batches_started_sched(void);
  12284. unsigned long rcu_batches_completed(void);
  12285. -unsigned long rcu_batches_completed_bh(void);
  12286. unsigned long rcu_batches_completed_sched(void);
  12287. void show_rcu_gp_kthreads(void);
  12288. void rcu_force_quiescent_state(void);
  12289. -void rcu_bh_force_quiescent_state(void);
  12290. void rcu_sched_force_quiescent_state(void);
  12291. void rcu_idle_enter(void);
  12292. @@ -105,6 +111,14 @@
  12293. bool rcu_is_watching(void);
  12294. +#ifndef CONFIG_PREEMPT_RT_FULL
  12295. +void rcu_bh_force_quiescent_state(void);
  12296. +unsigned long rcu_batches_completed_bh(void);
  12297. +#else
  12298. +# define rcu_bh_force_quiescent_state rcu_force_quiescent_state
  12299. +# define rcu_batches_completed_bh rcu_batches_completed
  12300. +#endif
  12301. +
  12302. void rcu_all_qs(void);
  12303. #endif /* __LINUX_RCUTREE_H */
  12304. diff -Nur linux-4.4.46.orig/include/linux/rtmutex.h linux-4.4.46/include/linux/rtmutex.h
  12305. --- linux-4.4.46.orig/include/linux/rtmutex.h 2017-02-01 08:31:11.000000000 +0100
  12306. +++ linux-4.4.46/include/linux/rtmutex.h 2017-02-03 17:18:10.911618440 +0100
  12307. @@ -13,11 +13,15 @@
  12308. #define __LINUX_RT_MUTEX_H
  12309. #include <linux/linkage.h>
  12310. +#include <linux/spinlock_types_raw.h>
  12311. #include <linux/rbtree.h>
  12312. -#include <linux/spinlock_types.h>
  12313. extern int max_lock_depth; /* for sysctl */
  12314. +#ifdef CONFIG_DEBUG_MUTEXES
  12315. +#include <linux/debug_locks.h>
  12316. +#endif
  12317. +
  12318. /**
  12319. * The rt_mutex structure
  12320. *
  12321. @@ -31,8 +35,8 @@
  12322. struct rb_root waiters;
  12323. struct rb_node *waiters_leftmost;
  12324. struct task_struct *owner;
  12325. -#ifdef CONFIG_DEBUG_RT_MUTEXES
  12326. int save_state;
  12327. +#ifdef CONFIG_DEBUG_RT_MUTEXES
  12328. const char *name, *file;
  12329. int line;
  12330. void *magic;
  12331. @@ -55,22 +59,33 @@
  12332. # define rt_mutex_debug_check_no_locks_held(task) do { } while (0)
  12333. #endif
  12334. +# define rt_mutex_init(mutex) \
  12335. + do { \
  12336. + raw_spin_lock_init(&(mutex)->wait_lock); \
  12337. + __rt_mutex_init(mutex, #mutex); \
  12338. + } while (0)
  12339. +
  12340. #ifdef CONFIG_DEBUG_RT_MUTEXES
  12341. # define __DEBUG_RT_MUTEX_INITIALIZER(mutexname) \
  12342. , .name = #mutexname, .file = __FILE__, .line = __LINE__
  12343. -# define rt_mutex_init(mutex) __rt_mutex_init(mutex, __func__)
  12344. extern void rt_mutex_debug_task_free(struct task_struct *tsk);
  12345. #else
  12346. # define __DEBUG_RT_MUTEX_INITIALIZER(mutexname)
  12347. -# define rt_mutex_init(mutex) __rt_mutex_init(mutex, NULL)
  12348. # define rt_mutex_debug_task_free(t) do { } while (0)
  12349. #endif
  12350. -#define __RT_MUTEX_INITIALIZER(mutexname) \
  12351. - { .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(mutexname.wait_lock) \
  12352. +#define __RT_MUTEX_INITIALIZER_PLAIN(mutexname) \
  12353. + .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(mutexname.wait_lock) \
  12354. , .waiters = RB_ROOT \
  12355. , .owner = NULL \
  12356. - __DEBUG_RT_MUTEX_INITIALIZER(mutexname)}
  12357. + __DEBUG_RT_MUTEX_INITIALIZER(mutexname)
  12358. +
  12359. +#define __RT_MUTEX_INITIALIZER(mutexname) \
  12360. + { __RT_MUTEX_INITIALIZER_PLAIN(mutexname) }
  12361. +
  12362. +#define __RT_MUTEX_INITIALIZER_SAVE_STATE(mutexname) \
  12363. + { __RT_MUTEX_INITIALIZER_PLAIN(mutexname) \
  12364. + , .save_state = 1 }
  12365. #define DEFINE_RT_MUTEX(mutexname) \
  12366. struct rt_mutex mutexname = __RT_MUTEX_INITIALIZER(mutexname)
  12367. @@ -91,6 +106,7 @@
  12368. extern void rt_mutex_lock(struct rt_mutex *lock);
  12369. extern int rt_mutex_lock_interruptible(struct rt_mutex *lock);
  12370. +extern int rt_mutex_lock_killable(struct rt_mutex *lock);
  12371. extern int rt_mutex_timed_lock(struct rt_mutex *lock,
  12372. struct hrtimer_sleeper *timeout);
  12373. diff -Nur linux-4.4.46.orig/include/linux/rwlock_rt.h linux-4.4.46/include/linux/rwlock_rt.h
  12374. --- linux-4.4.46.orig/include/linux/rwlock_rt.h 1970-01-01 01:00:00.000000000 +0100
  12375. +++ linux-4.4.46/include/linux/rwlock_rt.h 2017-02-03 17:18:10.911618440 +0100
  12376. @@ -0,0 +1,99 @@
  12377. +#ifndef __LINUX_RWLOCK_RT_H
  12378. +#define __LINUX_RWLOCK_RT_H
  12379. +
  12380. +#ifndef __LINUX_SPINLOCK_H
  12381. +#error Do not include directly. Use spinlock.h
  12382. +#endif
  12383. +
  12384. +#define rwlock_init(rwl) \
  12385. +do { \
  12386. + static struct lock_class_key __key; \
  12387. + \
  12388. + rt_mutex_init(&(rwl)->lock); \
  12389. + __rt_rwlock_init(rwl, #rwl, &__key); \
  12390. +} while (0)
  12391. +
  12392. +extern void __lockfunc rt_write_lock(rwlock_t *rwlock);
  12393. +extern void __lockfunc rt_read_lock(rwlock_t *rwlock);
  12394. +extern int __lockfunc rt_write_trylock(rwlock_t *rwlock);
  12395. +extern int __lockfunc rt_write_trylock_irqsave(rwlock_t *trylock, unsigned long *flags);
  12396. +extern int __lockfunc rt_read_trylock(rwlock_t *rwlock);
  12397. +extern void __lockfunc rt_write_unlock(rwlock_t *rwlock);
  12398. +extern void __lockfunc rt_read_unlock(rwlock_t *rwlock);
  12399. +extern unsigned long __lockfunc rt_write_lock_irqsave(rwlock_t *rwlock);
  12400. +extern unsigned long __lockfunc rt_read_lock_irqsave(rwlock_t *rwlock);
  12401. +extern void __rt_rwlock_init(rwlock_t *rwlock, char *name, struct lock_class_key *key);
  12402. +
  12403. +#define read_trylock(lock) __cond_lock(lock, rt_read_trylock(lock))
  12404. +#define write_trylock(lock) __cond_lock(lock, rt_write_trylock(lock))
  12405. +
  12406. +#define write_trylock_irqsave(lock, flags) \
  12407. + __cond_lock(lock, rt_write_trylock_irqsave(lock, &flags))
  12408. +
  12409. +#define read_lock_irqsave(lock, flags) \
  12410. + do { \
  12411. + typecheck(unsigned long, flags); \
  12412. + flags = rt_read_lock_irqsave(lock); \
  12413. + } while (0)
  12414. +
  12415. +#define write_lock_irqsave(lock, flags) \
  12416. + do { \
  12417. + typecheck(unsigned long, flags); \
  12418. + flags = rt_write_lock_irqsave(lock); \
  12419. + } while (0)
  12420. +
  12421. +#define read_lock(lock) rt_read_lock(lock)
  12422. +
  12423. +#define read_lock_bh(lock) \
  12424. + do { \
  12425. + local_bh_disable(); \
  12426. + rt_read_lock(lock); \
  12427. + } while (0)
  12428. +
  12429. +#define read_lock_irq(lock) read_lock(lock)
  12430. +
  12431. +#define write_lock(lock) rt_write_lock(lock)
  12432. +
  12433. +#define write_lock_bh(lock) \
  12434. + do { \
  12435. + local_bh_disable(); \
  12436. + rt_write_lock(lock); \
  12437. + } while (0)
  12438. +
  12439. +#define write_lock_irq(lock) write_lock(lock)
  12440. +
  12441. +#define read_unlock(lock) rt_read_unlock(lock)
  12442. +
  12443. +#define read_unlock_bh(lock) \
  12444. + do { \
  12445. + rt_read_unlock(lock); \
  12446. + local_bh_enable(); \
  12447. + } while (0)
  12448. +
  12449. +#define read_unlock_irq(lock) read_unlock(lock)
  12450. +
  12451. +#define write_unlock(lock) rt_write_unlock(lock)
  12452. +
  12453. +#define write_unlock_bh(lock) \
  12454. + do { \
  12455. + rt_write_unlock(lock); \
  12456. + local_bh_enable(); \
  12457. + } while (0)
  12458. +
  12459. +#define write_unlock_irq(lock) write_unlock(lock)
  12460. +
  12461. +#define read_unlock_irqrestore(lock, flags) \
  12462. + do { \
  12463. + typecheck(unsigned long, flags); \
  12464. + (void) flags; \
  12465. + rt_read_unlock(lock); \
  12466. + } while (0)
  12467. +
  12468. +#define write_unlock_irqrestore(lock, flags) \
  12469. + do { \
  12470. + typecheck(unsigned long, flags); \
  12471. + (void) flags; \
  12472. + rt_write_unlock(lock); \
  12473. + } while (0)
  12474. +
  12475. +#endif
  12476. diff -Nur linux-4.4.46.orig/include/linux/rwlock_types.h linux-4.4.46/include/linux/rwlock_types.h
  12477. --- linux-4.4.46.orig/include/linux/rwlock_types.h 2017-02-01 08:31:11.000000000 +0100
  12478. +++ linux-4.4.46/include/linux/rwlock_types.h 2017-02-03 17:18:10.911618440 +0100
  12479. @@ -1,6 +1,10 @@
  12480. #ifndef __LINUX_RWLOCK_TYPES_H
  12481. #define __LINUX_RWLOCK_TYPES_H
  12482. +#if !defined(__LINUX_SPINLOCK_TYPES_H)
  12483. +# error "Do not include directly, include spinlock_types.h"
  12484. +#endif
  12485. +
  12486. /*
  12487. * include/linux/rwlock_types.h - generic rwlock type definitions
  12488. * and initializers
  12489. @@ -43,6 +47,7 @@
  12490. RW_DEP_MAP_INIT(lockname) }
  12491. #endif
  12492. -#define DEFINE_RWLOCK(x) rwlock_t x = __RW_LOCK_UNLOCKED(x)
  12493. +#define DEFINE_RWLOCK(name) \
  12494. + rwlock_t name __cacheline_aligned_in_smp = __RW_LOCK_UNLOCKED(name)
  12495. #endif /* __LINUX_RWLOCK_TYPES_H */
  12496. diff -Nur linux-4.4.46.orig/include/linux/rwlock_types_rt.h linux-4.4.46/include/linux/rwlock_types_rt.h
  12497. --- linux-4.4.46.orig/include/linux/rwlock_types_rt.h 1970-01-01 01:00:00.000000000 +0100
  12498. +++ linux-4.4.46/include/linux/rwlock_types_rt.h 2017-02-03 17:18:10.911618440 +0100
  12499. @@ -0,0 +1,33 @@
  12500. +#ifndef __LINUX_RWLOCK_TYPES_RT_H
  12501. +#define __LINUX_RWLOCK_TYPES_RT_H
  12502. +
  12503. +#ifndef __LINUX_SPINLOCK_TYPES_H
  12504. +#error "Do not include directly. Include spinlock_types.h instead"
  12505. +#endif
  12506. +
  12507. +/*
  12508. + * rwlocks - rtmutex which allows single reader recursion
  12509. + */
  12510. +typedef struct {
  12511. + struct rt_mutex lock;
  12512. + int read_depth;
  12513. + unsigned int break_lock;
  12514. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12515. + struct lockdep_map dep_map;
  12516. +#endif
  12517. +} rwlock_t;
  12518. +
  12519. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12520. +# define RW_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }
  12521. +#else
  12522. +# define RW_DEP_MAP_INIT(lockname)
  12523. +#endif
  12524. +
  12525. +#define __RW_LOCK_UNLOCKED(name) \
  12526. + { .lock = __RT_MUTEX_INITIALIZER_SAVE_STATE(name.lock), \
  12527. + RW_DEP_MAP_INIT(name) }
  12528. +
  12529. +#define DEFINE_RWLOCK(name) \
  12530. + rwlock_t name __cacheline_aligned_in_smp = __RW_LOCK_UNLOCKED(name)
  12531. +
  12532. +#endif
  12533. diff -Nur linux-4.4.46.orig/include/linux/rwsem.h linux-4.4.46/include/linux/rwsem.h
  12534. --- linux-4.4.46.orig/include/linux/rwsem.h 2017-02-01 08:31:11.000000000 +0100
  12535. +++ linux-4.4.46/include/linux/rwsem.h 2017-02-03 17:18:10.911618440 +0100
  12536. @@ -18,6 +18,10 @@
  12537. #include <linux/osq_lock.h>
  12538. #endif
  12539. +#ifdef CONFIG_PREEMPT_RT_FULL
  12540. +#include <linux/rwsem_rt.h>
  12541. +#else /* PREEMPT_RT_FULL */
  12542. +
  12543. struct rw_semaphore;
  12544. #ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
  12545. @@ -177,4 +181,6 @@
  12546. # define up_read_non_owner(sem) up_read(sem)
  12547. #endif
  12548. +#endif /* !PREEMPT_RT_FULL */
  12549. +
  12550. #endif /* _LINUX_RWSEM_H */
  12551. diff -Nur linux-4.4.46.orig/include/linux/rwsem_rt.h linux-4.4.46/include/linux/rwsem_rt.h
  12552. --- linux-4.4.46.orig/include/linux/rwsem_rt.h 1970-01-01 01:00:00.000000000 +0100
  12553. +++ linux-4.4.46/include/linux/rwsem_rt.h 2017-02-03 17:18:10.911618440 +0100
  12554. @@ -0,0 +1,152 @@
  12555. +#ifndef _LINUX_RWSEM_RT_H
  12556. +#define _LINUX_RWSEM_RT_H
  12557. +
  12558. +#ifndef _LINUX_RWSEM_H
  12559. +#error "Include rwsem.h"
  12560. +#endif
  12561. +
  12562. +/*
  12563. + * RW-semaphores are a spinlock plus a reader-depth count.
  12564. + *
  12565. + * Note that the semantics are different from the usual
  12566. + * Linux rw-sems, in PREEMPT_RT mode we do not allow
  12567. + * multiple readers to hold the lock at once, we only allow
  12568. + * a read-lock owner to read-lock recursively. This is
  12569. + * better for latency, makes the implementation inherently
  12570. + * fair and makes it simpler as well.
  12571. + */
  12572. +
  12573. +#include <linux/rtmutex.h>
  12574. +
  12575. +struct rw_semaphore {
  12576. + struct rt_mutex lock;
  12577. + int read_depth;
  12578. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12579. + struct lockdep_map dep_map;
  12580. +#endif
  12581. +};
  12582. +
  12583. +#define __RWSEM_INITIALIZER(name) \
  12584. + { .lock = __RT_MUTEX_INITIALIZER(name.lock), \
  12585. + RW_DEP_MAP_INIT(name) }
  12586. +
  12587. +#define DECLARE_RWSEM(lockname) \
  12588. + struct rw_semaphore lockname = __RWSEM_INITIALIZER(lockname)
  12589. +
  12590. +extern void __rt_rwsem_init(struct rw_semaphore *rwsem, const char *name,
  12591. + struct lock_class_key *key);
  12592. +
  12593. +#define __rt_init_rwsem(sem, name, key) \
  12594. + do { \
  12595. + rt_mutex_init(&(sem)->lock); \
  12596. + __rt_rwsem_init((sem), (name), (key));\
  12597. + } while (0)
  12598. +
  12599. +#define __init_rwsem(sem, name, key) __rt_init_rwsem(sem, name, key)
  12600. +
  12601. +# define rt_init_rwsem(sem) \
  12602. +do { \
  12603. + static struct lock_class_key __key; \
  12604. + \
  12605. + __rt_init_rwsem((sem), #sem, &__key); \
  12606. +} while (0)
  12607. +
  12608. +extern void rt_down_write(struct rw_semaphore *rwsem);
  12609. +extern void rt_down_read_nested(struct rw_semaphore *rwsem, int subclass);
  12610. +extern void rt_down_write_nested(struct rw_semaphore *rwsem, int subclass);
  12611. +extern void rt_down_write_nested_lock(struct rw_semaphore *rwsem,
  12612. + struct lockdep_map *nest);
  12613. +extern void rt__down_read(struct rw_semaphore *rwsem);
  12614. +extern void rt_down_read(struct rw_semaphore *rwsem);
  12615. +extern int rt_down_write_trylock(struct rw_semaphore *rwsem);
  12616. +extern int rt__down_read_trylock(struct rw_semaphore *rwsem);
  12617. +extern int rt_down_read_trylock(struct rw_semaphore *rwsem);
  12618. +extern void __rt_up_read(struct rw_semaphore *rwsem);
  12619. +extern void rt_up_read(struct rw_semaphore *rwsem);
  12620. +extern void rt_up_write(struct rw_semaphore *rwsem);
  12621. +extern void rt_downgrade_write(struct rw_semaphore *rwsem);
  12622. +
  12623. +#define init_rwsem(sem) rt_init_rwsem(sem)
  12624. +#define rwsem_is_locked(s) rt_mutex_is_locked(&(s)->lock)
  12625. +
  12626. +static inline int rwsem_is_contended(struct rw_semaphore *sem)
  12627. +{
  12628. + /* rt_mutex_has_waiters() */
  12629. + return !RB_EMPTY_ROOT(&sem->lock.waiters);
  12630. +}
  12631. +
  12632. +static inline void __down_read(struct rw_semaphore *sem)
  12633. +{
  12634. + rt__down_read(sem);
  12635. +}
  12636. +
  12637. +static inline void down_read(struct rw_semaphore *sem)
  12638. +{
  12639. + rt_down_read(sem);
  12640. +}
  12641. +
  12642. +static inline int __down_read_trylock(struct rw_semaphore *sem)
  12643. +{
  12644. + return rt__down_read_trylock(sem);
  12645. +}
  12646. +
  12647. +static inline int down_read_trylock(struct rw_semaphore *sem)
  12648. +{
  12649. + return rt_down_read_trylock(sem);
  12650. +}
  12651. +
  12652. +static inline void down_write(struct rw_semaphore *sem)
  12653. +{
  12654. + rt_down_write(sem);
  12655. +}
  12656. +
  12657. +static inline int down_write_trylock(struct rw_semaphore *sem)
  12658. +{
  12659. + return rt_down_write_trylock(sem);
  12660. +}
  12661. +
  12662. +static inline void __up_read(struct rw_semaphore *sem)
  12663. +{
  12664. + __rt_up_read(sem);
  12665. +}
  12666. +
  12667. +static inline void up_read(struct rw_semaphore *sem)
  12668. +{
  12669. + rt_up_read(sem);
  12670. +}
  12671. +
  12672. +static inline void up_write(struct rw_semaphore *sem)
  12673. +{
  12674. + rt_up_write(sem);
  12675. +}
  12676. +
  12677. +static inline void downgrade_write(struct rw_semaphore *sem)
  12678. +{
  12679. + rt_downgrade_write(sem);
  12680. +}
  12681. +
  12682. +static inline void down_read_nested(struct rw_semaphore *sem, int subclass)
  12683. +{
  12684. + return rt_down_read_nested(sem, subclass);
  12685. +}
  12686. +
  12687. +static inline void down_write_nested(struct rw_semaphore *sem, int subclass)
  12688. +{
  12689. + rt_down_write_nested(sem, subclass);
  12690. +}
  12691. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12692. +static inline void down_write_nest_lock(struct rw_semaphore *sem,
  12693. + struct rw_semaphore *nest_lock)
  12694. +{
  12695. + rt_down_write_nested_lock(sem, &nest_lock->dep_map);
  12696. +}
  12697. +
  12698. +#else
  12699. +
  12700. +static inline void down_write_nest_lock(struct rw_semaphore *sem,
  12701. + struct rw_semaphore *nest_lock)
  12702. +{
  12703. + rt_down_write_nested_lock(sem, NULL);
  12704. +}
  12705. +#endif
  12706. +#endif
  12707. diff -Nur linux-4.4.46.orig/include/linux/sched.h linux-4.4.46/include/linux/sched.h
  12708. --- linux-4.4.46.orig/include/linux/sched.h 2017-02-01 08:31:11.000000000 +0100
  12709. +++ linux-4.4.46/include/linux/sched.h 2017-02-03 17:18:10.911618440 +0100
  12710. @@ -26,6 +26,7 @@
  12711. #include <linux/nodemask.h>
  12712. #include <linux/mm_types.h>
  12713. #include <linux/preempt.h>
  12714. +#include <asm/kmap_types.h>
  12715. #include <asm/page.h>
  12716. #include <asm/ptrace.h>
  12717. @@ -182,8 +183,6 @@
  12718. static inline void update_cpu_load_nohz(void) { }
  12719. #endif
  12720. -extern unsigned long get_parent_ip(unsigned long addr);
  12721. -
  12722. extern void dump_cpu_task(int cpu);
  12723. struct seq_file;
  12724. @@ -242,10 +241,7 @@
  12725. TASK_UNINTERRUPTIBLE | __TASK_STOPPED | \
  12726. __TASK_TRACED | EXIT_ZOMBIE | EXIT_DEAD)
  12727. -#define task_is_traced(task) ((task->state & __TASK_TRACED) != 0)
  12728. #define task_is_stopped(task) ((task->state & __TASK_STOPPED) != 0)
  12729. -#define task_is_stopped_or_traced(task) \
  12730. - ((task->state & (__TASK_STOPPED | __TASK_TRACED)) != 0)
  12731. #define task_contributes_to_load(task) \
  12732. ((task->state & TASK_UNINTERRUPTIBLE) != 0 && \
  12733. (task->flags & PF_FROZEN) == 0 && \
  12734. @@ -311,6 +307,11 @@
  12735. #endif
  12736. +#define __set_current_state_no_track(state_value) \
  12737. + do { current->state = (state_value); } while (0)
  12738. +#define set_current_state_no_track(state_value) \
  12739. + set_mb(current->state, (state_value))
  12740. +
  12741. /* Task command name length */
  12742. #define TASK_COMM_LEN 16
  12743. @@ -970,8 +971,18 @@
  12744. struct wake_q_head name = { WAKE_Q_TAIL, &name.first }
  12745. extern void wake_q_add(struct wake_q_head *head,
  12746. - struct task_struct *task);
  12747. -extern void wake_up_q(struct wake_q_head *head);
  12748. + struct task_struct *task);
  12749. +extern void __wake_up_q(struct wake_q_head *head, bool sleeper);
  12750. +
  12751. +static inline void wake_up_q(struct wake_q_head *head)
  12752. +{
  12753. + __wake_up_q(head, false);
  12754. +}
  12755. +
  12756. +static inline void wake_up_q_sleeper(struct wake_q_head *head)
  12757. +{
  12758. + __wake_up_q(head, true);
  12759. +}
  12760. /*
  12761. * sched-domains (multiprocessor balancing) declarations:
  12762. @@ -1379,6 +1390,7 @@
  12763. struct task_struct {
  12764. volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
  12765. + volatile long saved_state; /* saved state for "spinlock sleepers" */
  12766. void *stack;
  12767. atomic_t usage;
  12768. unsigned int flags; /* per process flags, defined below */
  12769. @@ -1415,6 +1427,12 @@
  12770. #endif
  12771. unsigned int policy;
  12772. +#ifdef CONFIG_PREEMPT_RT_FULL
  12773. + int migrate_disable;
  12774. +# ifdef CONFIG_SCHED_DEBUG
  12775. + int migrate_disable_atomic;
  12776. +# endif
  12777. +#endif
  12778. int nr_cpus_allowed;
  12779. cpumask_t cpus_allowed;
  12780. @@ -1522,11 +1540,14 @@
  12781. cputime_t gtime;
  12782. struct prev_cputime prev_cputime;
  12783. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  12784. - seqlock_t vtime_seqlock;
  12785. + seqcount_t vtime_seqcount;
  12786. unsigned long long vtime_snap;
  12787. enum {
  12788. - VTIME_SLEEPING = 0,
  12789. + /* Task is sleeping or running in a CPU with VTIME inactive */
  12790. + VTIME_INACTIVE = 0,
  12791. + /* Task runs in userspace in a CPU with VTIME active */
  12792. VTIME_USER,
  12793. + /* Task runs in kernelspace in a CPU with VTIME active */
  12794. VTIME_SYS,
  12795. } vtime_snap_whence;
  12796. #endif
  12797. @@ -1538,6 +1559,9 @@
  12798. struct task_cputime cputime_expires;
  12799. struct list_head cpu_timers[3];
  12800. +#ifdef CONFIG_PREEMPT_RT_BASE
  12801. + struct task_struct *posix_timer_list;
  12802. +#endif
  12803. /* process credentials */
  12804. const struct cred __rcu *ptracer_cred; /* Tracer's credentials at attach */
  12805. @@ -1569,10 +1593,15 @@
  12806. /* signal handlers */
  12807. struct signal_struct *signal;
  12808. struct sighand_struct *sighand;
  12809. + struct sigqueue *sigqueue_cache;
  12810. sigset_t blocked, real_blocked;
  12811. sigset_t saved_sigmask; /* restored if set_restore_sigmask() was used */
  12812. struct sigpending pending;
  12813. +#ifdef CONFIG_PREEMPT_RT_FULL
  12814. + /* TODO: move me into ->restart_block ? */
  12815. + struct siginfo forced_info;
  12816. +#endif
  12817. unsigned long sas_ss_sp;
  12818. size_t sas_ss_size;
  12819. @@ -1796,6 +1825,12 @@
  12820. unsigned long trace;
  12821. /* bitmask and counter of trace recursion */
  12822. unsigned long trace_recursion;
  12823. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  12824. + u64 preempt_timestamp_hist;
  12825. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  12826. + long timer_offset;
  12827. +#endif
  12828. +#endif
  12829. #endif /* CONFIG_TRACING */
  12830. #ifdef CONFIG_MEMCG
  12831. struct mem_cgroup *memcg_in_oom;
  12832. @@ -1812,9 +1847,23 @@
  12833. unsigned int sequential_io;
  12834. unsigned int sequential_io_avg;
  12835. #endif
  12836. +#ifdef CONFIG_PREEMPT_RT_BASE
  12837. + struct rcu_head put_rcu;
  12838. + int softirq_nestcnt;
  12839. + unsigned int softirqs_raised;
  12840. +#endif
  12841. +#ifdef CONFIG_PREEMPT_RT_FULL
  12842. +# if defined CONFIG_HIGHMEM || defined CONFIG_X86_32
  12843. + int kmap_idx;
  12844. + pte_t kmap_pte[KM_TYPE_NR];
  12845. +# endif
  12846. +#endif
  12847. #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
  12848. unsigned long task_state_change;
  12849. #endif
  12850. +#ifdef CONFIG_PREEMPT_RT_FULL
  12851. + int xmit_recursion;
  12852. +#endif
  12853. int pagefault_disabled;
  12854. /* CPU-specific state of this task */
  12855. struct thread_struct thread;
  12856. @@ -1832,9 +1881,6 @@
  12857. # define arch_task_struct_size (sizeof(struct task_struct))
  12858. #endif
  12859. -/* Future-safe accessor for struct task_struct's cpus_allowed. */
  12860. -#define tsk_cpus_allowed(tsk) (&(tsk)->cpus_allowed)
  12861. -
  12862. #define TNF_MIGRATED 0x01
  12863. #define TNF_NO_GROUP 0x02
  12864. #define TNF_SHARED 0x04
  12865. @@ -2024,6 +2070,15 @@
  12866. extern void free_task(struct task_struct *tsk);
  12867. #define get_task_struct(tsk) do { atomic_inc(&(tsk)->usage); } while(0)
  12868. +#ifdef CONFIG_PREEMPT_RT_BASE
  12869. +extern void __put_task_struct_cb(struct rcu_head *rhp);
  12870. +
  12871. +static inline void put_task_struct(struct task_struct *t)
  12872. +{
  12873. + if (atomic_dec_and_test(&t->usage))
  12874. + call_rcu(&t->put_rcu, __put_task_struct_cb);
  12875. +}
  12876. +#else
  12877. extern void __put_task_struct(struct task_struct *t);
  12878. static inline void put_task_struct(struct task_struct *t)
  12879. @@ -2031,6 +2086,7 @@
  12880. if (atomic_dec_and_test(&t->usage))
  12881. __put_task_struct(t);
  12882. }
  12883. +#endif
  12884. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  12885. extern void task_cputime(struct task_struct *t,
  12886. @@ -2069,6 +2125,7 @@
  12887. /*
  12888. * Per process flags
  12889. */
  12890. +#define PF_IN_SOFTIRQ 0x00000001 /* Task is serving softirq */
  12891. #define PF_EXITING 0x00000004 /* getting shut down */
  12892. #define PF_EXITPIDONE 0x00000008 /* pi exit done on shut down */
  12893. #define PF_VCPU 0x00000010 /* I'm a virtual CPU */
  12894. @@ -2233,6 +2290,10 @@
  12895. extern int set_cpus_allowed_ptr(struct task_struct *p,
  12896. const struct cpumask *new_mask);
  12897. +int migrate_me(void);
  12898. +void tell_sched_cpu_down_begin(int cpu);
  12899. +void tell_sched_cpu_down_done(int cpu);
  12900. +
  12901. #else
  12902. static inline void do_set_cpus_allowed(struct task_struct *p,
  12903. const struct cpumask *new_mask)
  12904. @@ -2245,6 +2306,9 @@
  12905. return -EINVAL;
  12906. return 0;
  12907. }
  12908. +static inline int migrate_me(void) { return 0; }
  12909. +static inline void tell_sched_cpu_down_begin(int cpu) { }
  12910. +static inline void tell_sched_cpu_down_done(int cpu) { }
  12911. #endif
  12912. #ifdef CONFIG_NO_HZ_COMMON
  12913. @@ -2454,6 +2518,7 @@
  12914. extern int wake_up_state(struct task_struct *tsk, unsigned int state);
  12915. extern int wake_up_process(struct task_struct *tsk);
  12916. +extern int wake_up_lock_sleeper(struct task_struct * tsk);
  12917. extern void wake_up_new_task(struct task_struct *tsk);
  12918. #ifdef CONFIG_SMP
  12919. extern void kick_process(struct task_struct *tsk);
  12920. @@ -2577,12 +2642,24 @@
  12921. /* mmdrop drops the mm and the page tables */
  12922. extern void __mmdrop(struct mm_struct *);
  12923. +
  12924. static inline void mmdrop(struct mm_struct * mm)
  12925. {
  12926. if (unlikely(atomic_dec_and_test(&mm->mm_count)))
  12927. __mmdrop(mm);
  12928. }
  12929. +#ifdef CONFIG_PREEMPT_RT_BASE
  12930. +extern void __mmdrop_delayed(struct rcu_head *rhp);
  12931. +static inline void mmdrop_delayed(struct mm_struct *mm)
  12932. +{
  12933. + if (atomic_dec_and_test(&mm->mm_count))
  12934. + call_rcu(&mm->delayed_drop, __mmdrop_delayed);
  12935. +}
  12936. +#else
  12937. +# define mmdrop_delayed(mm) mmdrop(mm)
  12938. +#endif
  12939. +
  12940. /* mmput gets rid of the mappings and all user-space */
  12941. extern void mmput(struct mm_struct *);
  12942. /* Grab a reference to a task's mm, if it is not already going away */
  12943. @@ -2892,6 +2969,43 @@
  12944. return unlikely(test_tsk_thread_flag(tsk,TIF_NEED_RESCHED));
  12945. }
  12946. +#ifdef CONFIG_PREEMPT_LAZY
  12947. +static inline void set_tsk_need_resched_lazy(struct task_struct *tsk)
  12948. +{
  12949. + set_tsk_thread_flag(tsk,TIF_NEED_RESCHED_LAZY);
  12950. +}
  12951. +
  12952. +static inline void clear_tsk_need_resched_lazy(struct task_struct *tsk)
  12953. +{
  12954. + clear_tsk_thread_flag(tsk,TIF_NEED_RESCHED_LAZY);
  12955. +}
  12956. +
  12957. +static inline int test_tsk_need_resched_lazy(struct task_struct *tsk)
  12958. +{
  12959. + return unlikely(test_tsk_thread_flag(tsk,TIF_NEED_RESCHED_LAZY));
  12960. +}
  12961. +
  12962. +static inline int need_resched_lazy(void)
  12963. +{
  12964. + return test_thread_flag(TIF_NEED_RESCHED_LAZY);
  12965. +}
  12966. +
  12967. +static inline int need_resched_now(void)
  12968. +{
  12969. + return test_thread_flag(TIF_NEED_RESCHED);
  12970. +}
  12971. +
  12972. +#else
  12973. +static inline void clear_tsk_need_resched_lazy(struct task_struct *tsk) { }
  12974. +static inline int need_resched_lazy(void) { return 0; }
  12975. +
  12976. +static inline int need_resched_now(void)
  12977. +{
  12978. + return test_thread_flag(TIF_NEED_RESCHED);
  12979. +}
  12980. +
  12981. +#endif
  12982. +
  12983. static inline int restart_syscall(void)
  12984. {
  12985. set_tsk_thread_flag(current, TIF_SIGPENDING);
  12986. @@ -2923,6 +3037,51 @@
  12987. return (state & TASK_INTERRUPTIBLE) || __fatal_signal_pending(p);
  12988. }
  12989. +static inline bool __task_is_stopped_or_traced(struct task_struct *task)
  12990. +{
  12991. + if (task->state & (__TASK_STOPPED | __TASK_TRACED))
  12992. + return true;
  12993. +#ifdef CONFIG_PREEMPT_RT_FULL
  12994. + if (task->saved_state & (__TASK_STOPPED | __TASK_TRACED))
  12995. + return true;
  12996. +#endif
  12997. + return false;
  12998. +}
  12999. +
  13000. +static inline bool task_is_stopped_or_traced(struct task_struct *task)
  13001. +{
  13002. + bool traced_stopped;
  13003. +
  13004. +#ifdef CONFIG_PREEMPT_RT_FULL
  13005. + unsigned long flags;
  13006. +
  13007. + raw_spin_lock_irqsave(&task->pi_lock, flags);
  13008. + traced_stopped = __task_is_stopped_or_traced(task);
  13009. + raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  13010. +#else
  13011. + traced_stopped = __task_is_stopped_or_traced(task);
  13012. +#endif
  13013. + return traced_stopped;
  13014. +}
  13015. +
  13016. +static inline bool task_is_traced(struct task_struct *task)
  13017. +{
  13018. + bool traced = false;
  13019. +
  13020. + if (task->state & __TASK_TRACED)
  13021. + return true;
  13022. +#ifdef CONFIG_PREEMPT_RT_FULL
  13023. + /* in case the task is sleeping on tasklist_lock */
  13024. + raw_spin_lock_irq(&task->pi_lock);
  13025. + if (task->state & __TASK_TRACED)
  13026. + traced = true;
  13027. + else if (task->saved_state & __TASK_TRACED)
  13028. + traced = true;
  13029. + raw_spin_unlock_irq(&task->pi_lock);
  13030. +#endif
  13031. + return traced;
  13032. +}
  13033. +
  13034. /*
  13035. * cond_resched() and cond_resched_lock(): latency reduction via
  13036. * explicit rescheduling in places that are safe. The return
  13037. @@ -2944,12 +3103,16 @@
  13038. __cond_resched_lock(lock); \
  13039. })
  13040. +#ifndef CONFIG_PREEMPT_RT_FULL
  13041. extern int __cond_resched_softirq(void);
  13042. #define cond_resched_softirq() ({ \
  13043. ___might_sleep(__FILE__, __LINE__, SOFTIRQ_DISABLE_OFFSET); \
  13044. __cond_resched_softirq(); \
  13045. })
  13046. +#else
  13047. +# define cond_resched_softirq() cond_resched()
  13048. +#endif
  13049. static inline void cond_resched_rcu(void)
  13050. {
  13051. @@ -3111,6 +3274,31 @@
  13052. #endif /* CONFIG_SMP */
  13053. +static inline int __migrate_disabled(struct task_struct *p)
  13054. +{
  13055. +#ifdef CONFIG_PREEMPT_RT_FULL
  13056. + return p->migrate_disable;
  13057. +#else
  13058. + return 0;
  13059. +#endif
  13060. +}
  13061. +
  13062. +/* Future-safe accessor for struct task_struct's cpus_allowed. */
  13063. +static inline const struct cpumask *tsk_cpus_allowed(struct task_struct *p)
  13064. +{
  13065. + if (__migrate_disabled(p))
  13066. + return cpumask_of(task_cpu(p));
  13067. +
  13068. + return &p->cpus_allowed;
  13069. +}
  13070. +
  13071. +static inline int tsk_nr_cpus_allowed(struct task_struct *p)
  13072. +{
  13073. + if (__migrate_disabled(p))
  13074. + return 1;
  13075. + return p->nr_cpus_allowed;
  13076. +}
  13077. +
  13078. extern long sched_setaffinity(pid_t pid, const struct cpumask *new_mask);
  13079. extern long sched_getaffinity(pid_t pid, struct cpumask *mask);
  13080. diff -Nur linux-4.4.46.orig/include/linux/seqlock.h linux-4.4.46/include/linux/seqlock.h
  13081. --- linux-4.4.46.orig/include/linux/seqlock.h 2017-02-01 08:31:11.000000000 +0100
  13082. +++ linux-4.4.46/include/linux/seqlock.h 2017-02-03 17:18:10.911618440 +0100
  13083. @@ -220,20 +220,30 @@
  13084. return __read_seqcount_retry(s, start);
  13085. }
  13086. -
  13087. -
  13088. -static inline void raw_write_seqcount_begin(seqcount_t *s)
  13089. +static inline void __raw_write_seqcount_begin(seqcount_t *s)
  13090. {
  13091. s->sequence++;
  13092. smp_wmb();
  13093. }
  13094. -static inline void raw_write_seqcount_end(seqcount_t *s)
  13095. +static inline void raw_write_seqcount_begin(seqcount_t *s)
  13096. +{
  13097. + preempt_disable_rt();
  13098. + __raw_write_seqcount_begin(s);
  13099. +}
  13100. +
  13101. +static inline void __raw_write_seqcount_end(seqcount_t *s)
  13102. {
  13103. smp_wmb();
  13104. s->sequence++;
  13105. }
  13106. +static inline void raw_write_seqcount_end(seqcount_t *s)
  13107. +{
  13108. + __raw_write_seqcount_end(s);
  13109. + preempt_enable_rt();
  13110. +}
  13111. +
  13112. /**
  13113. * raw_write_seqcount_barrier - do a seq write barrier
  13114. * @s: pointer to seqcount_t
  13115. @@ -425,10 +435,32 @@
  13116. /*
  13117. * Read side functions for starting and finalizing a read side section.
  13118. */
  13119. +#ifndef CONFIG_PREEMPT_RT_FULL
  13120. static inline unsigned read_seqbegin(const seqlock_t *sl)
  13121. {
  13122. return read_seqcount_begin(&sl->seqcount);
  13123. }
  13124. +#else
  13125. +/*
  13126. + * Starvation safe read side for RT
  13127. + */
  13128. +static inline unsigned read_seqbegin(seqlock_t *sl)
  13129. +{
  13130. + unsigned ret;
  13131. +
  13132. +repeat:
  13133. + ret = ACCESS_ONCE(sl->seqcount.sequence);
  13134. + if (unlikely(ret & 1)) {
  13135. + /*
  13136. + * Take the lock and let the writer proceed (i.e. evtl
  13137. + * boost it), otherwise we could loop here forever.
  13138. + */
  13139. + spin_unlock_wait(&sl->lock);
  13140. + goto repeat;
  13141. + }
  13142. + return ret;
  13143. +}
  13144. +#endif
  13145. static inline unsigned read_seqretry(const seqlock_t *sl, unsigned start)
  13146. {
  13147. @@ -443,36 +475,36 @@
  13148. static inline void write_seqlock(seqlock_t *sl)
  13149. {
  13150. spin_lock(&sl->lock);
  13151. - write_seqcount_begin(&sl->seqcount);
  13152. + __raw_write_seqcount_begin(&sl->seqcount);
  13153. }
  13154. static inline void write_sequnlock(seqlock_t *sl)
  13155. {
  13156. - write_seqcount_end(&sl->seqcount);
  13157. + __raw_write_seqcount_end(&sl->seqcount);
  13158. spin_unlock(&sl->lock);
  13159. }
  13160. static inline void write_seqlock_bh(seqlock_t *sl)
  13161. {
  13162. spin_lock_bh(&sl->lock);
  13163. - write_seqcount_begin(&sl->seqcount);
  13164. + __raw_write_seqcount_begin(&sl->seqcount);
  13165. }
  13166. static inline void write_sequnlock_bh(seqlock_t *sl)
  13167. {
  13168. - write_seqcount_end(&sl->seqcount);
  13169. + __raw_write_seqcount_end(&sl->seqcount);
  13170. spin_unlock_bh(&sl->lock);
  13171. }
  13172. static inline void write_seqlock_irq(seqlock_t *sl)
  13173. {
  13174. spin_lock_irq(&sl->lock);
  13175. - write_seqcount_begin(&sl->seqcount);
  13176. + __raw_write_seqcount_begin(&sl->seqcount);
  13177. }
  13178. static inline void write_sequnlock_irq(seqlock_t *sl)
  13179. {
  13180. - write_seqcount_end(&sl->seqcount);
  13181. + __raw_write_seqcount_end(&sl->seqcount);
  13182. spin_unlock_irq(&sl->lock);
  13183. }
  13184. @@ -481,7 +513,7 @@
  13185. unsigned long flags;
  13186. spin_lock_irqsave(&sl->lock, flags);
  13187. - write_seqcount_begin(&sl->seqcount);
  13188. + __raw_write_seqcount_begin(&sl->seqcount);
  13189. return flags;
  13190. }
  13191. @@ -491,7 +523,7 @@
  13192. static inline void
  13193. write_sequnlock_irqrestore(seqlock_t *sl, unsigned long flags)
  13194. {
  13195. - write_seqcount_end(&sl->seqcount);
  13196. + __raw_write_seqcount_end(&sl->seqcount);
  13197. spin_unlock_irqrestore(&sl->lock, flags);
  13198. }
  13199. diff -Nur linux-4.4.46.orig/include/linux/signal.h linux-4.4.46/include/linux/signal.h
  13200. --- linux-4.4.46.orig/include/linux/signal.h 2017-02-01 08:31:11.000000000 +0100
  13201. +++ linux-4.4.46/include/linux/signal.h 2017-02-03 17:18:10.911618440 +0100
  13202. @@ -233,6 +233,7 @@
  13203. }
  13204. extern void flush_sigqueue(struct sigpending *queue);
  13205. +extern void flush_task_sigqueue(struct task_struct *tsk);
  13206. /* Test if 'sig' is valid signal. Use this instead of testing _NSIG directly */
  13207. static inline int valid_signal(unsigned long sig)
  13208. diff -Nur linux-4.4.46.orig/include/linux/skbuff.h linux-4.4.46/include/linux/skbuff.h
  13209. --- linux-4.4.46.orig/include/linux/skbuff.h 2017-02-01 08:31:11.000000000 +0100
  13210. +++ linux-4.4.46/include/linux/skbuff.h 2017-02-03 17:18:10.911618440 +0100
  13211. @@ -203,6 +203,7 @@
  13212. __u32 qlen;
  13213. spinlock_t lock;
  13214. + raw_spinlock_t raw_lock;
  13215. };
  13216. struct sk_buff;
  13217. @@ -1465,6 +1466,12 @@
  13218. __skb_queue_head_init(list);
  13219. }
  13220. +static inline void skb_queue_head_init_raw(struct sk_buff_head *list)
  13221. +{
  13222. + raw_spin_lock_init(&list->raw_lock);
  13223. + __skb_queue_head_init(list);
  13224. +}
  13225. +
  13226. static inline void skb_queue_head_init_class(struct sk_buff_head *list,
  13227. struct lock_class_key *class)
  13228. {
  13229. diff -Nur linux-4.4.46.orig/include/linux/smp.h linux-4.4.46/include/linux/smp.h
  13230. --- linux-4.4.46.orig/include/linux/smp.h 2017-02-01 08:31:11.000000000 +0100
  13231. +++ linux-4.4.46/include/linux/smp.h 2017-02-03 17:18:10.911618440 +0100
  13232. @@ -185,6 +185,9 @@
  13233. #define get_cpu() ({ preempt_disable(); smp_processor_id(); })
  13234. #define put_cpu() preempt_enable()
  13235. +#define get_cpu_light() ({ migrate_disable(); smp_processor_id(); })
  13236. +#define put_cpu_light() migrate_enable()
  13237. +
  13238. /*
  13239. * Callback to arch code if there's nosmp or maxcpus=0 on the
  13240. * boot command line:
  13241. diff -Nur linux-4.4.46.orig/include/linux/spinlock_api_smp.h linux-4.4.46/include/linux/spinlock_api_smp.h
  13242. --- linux-4.4.46.orig/include/linux/spinlock_api_smp.h 2017-02-01 08:31:11.000000000 +0100
  13243. +++ linux-4.4.46/include/linux/spinlock_api_smp.h 2017-02-03 17:18:10.911618440 +0100
  13244. @@ -189,6 +189,8 @@
  13245. return 0;
  13246. }
  13247. -#include <linux/rwlock_api_smp.h>
  13248. +#ifndef CONFIG_PREEMPT_RT_FULL
  13249. +# include <linux/rwlock_api_smp.h>
  13250. +#endif
  13251. #endif /* __LINUX_SPINLOCK_API_SMP_H */
  13252. diff -Nur linux-4.4.46.orig/include/linux/spinlock.h linux-4.4.46/include/linux/spinlock.h
  13253. --- linux-4.4.46.orig/include/linux/spinlock.h 2017-02-01 08:31:11.000000000 +0100
  13254. +++ linux-4.4.46/include/linux/spinlock.h 2017-02-03 17:18:10.911618440 +0100
  13255. @@ -271,7 +271,11 @@
  13256. #define raw_spin_can_lock(lock) (!raw_spin_is_locked(lock))
  13257. /* Include rwlock functions */
  13258. -#include <linux/rwlock.h>
  13259. +#ifdef CONFIG_PREEMPT_RT_FULL
  13260. +# include <linux/rwlock_rt.h>
  13261. +#else
  13262. +# include <linux/rwlock.h>
  13263. +#endif
  13264. /*
  13265. * Pull the _spin_*()/_read_*()/_write_*() functions/declarations:
  13266. @@ -282,6 +286,10 @@
  13267. # include <linux/spinlock_api_up.h>
  13268. #endif
  13269. +#ifdef CONFIG_PREEMPT_RT_FULL
  13270. +# include <linux/spinlock_rt.h>
  13271. +#else /* PREEMPT_RT_FULL */
  13272. +
  13273. /*
  13274. * Map the spin_lock functions to the raw variants for PREEMPT_RT=n
  13275. */
  13276. @@ -347,6 +355,12 @@
  13277. raw_spin_unlock(&lock->rlock);
  13278. }
  13279. +static __always_inline int spin_unlock_no_deboost(spinlock_t *lock)
  13280. +{
  13281. + raw_spin_unlock(&lock->rlock);
  13282. + return 0;
  13283. +}
  13284. +
  13285. static __always_inline void spin_unlock_bh(spinlock_t *lock)
  13286. {
  13287. raw_spin_unlock_bh(&lock->rlock);
  13288. @@ -416,4 +430,6 @@
  13289. #define atomic_dec_and_lock(atomic, lock) \
  13290. __cond_lock(lock, _atomic_dec_and_lock(atomic, lock))
  13291. +#endif /* !PREEMPT_RT_FULL */
  13292. +
  13293. #endif /* __LINUX_SPINLOCK_H */
  13294. diff -Nur linux-4.4.46.orig/include/linux/spinlock_rt.h linux-4.4.46/include/linux/spinlock_rt.h
  13295. --- linux-4.4.46.orig/include/linux/spinlock_rt.h 1970-01-01 01:00:00.000000000 +0100
  13296. +++ linux-4.4.46/include/linux/spinlock_rt.h 2017-02-03 17:18:10.911618440 +0100
  13297. @@ -0,0 +1,165 @@
  13298. +#ifndef __LINUX_SPINLOCK_RT_H
  13299. +#define __LINUX_SPINLOCK_RT_H
  13300. +
  13301. +#ifndef __LINUX_SPINLOCK_H
  13302. +#error Do not include directly. Use spinlock.h
  13303. +#endif
  13304. +
  13305. +#include <linux/bug.h>
  13306. +
  13307. +extern void
  13308. +__rt_spin_lock_init(spinlock_t *lock, char *name, struct lock_class_key *key);
  13309. +
  13310. +#define spin_lock_init(slock) \
  13311. +do { \
  13312. + static struct lock_class_key __key; \
  13313. + \
  13314. + rt_mutex_init(&(slock)->lock); \
  13315. + __rt_spin_lock_init(slock, #slock, &__key); \
  13316. +} while (0)
  13317. +
  13318. +void __lockfunc rt_spin_lock__no_mg(spinlock_t *lock);
  13319. +void __lockfunc rt_spin_unlock__no_mg(spinlock_t *lock);
  13320. +int __lockfunc rt_spin_trylock__no_mg(spinlock_t *lock);
  13321. +
  13322. +extern void __lockfunc rt_spin_lock(spinlock_t *lock);
  13323. +extern unsigned long __lockfunc rt_spin_lock_trace_flags(spinlock_t *lock);
  13324. +extern void __lockfunc rt_spin_lock_nested(spinlock_t *lock, int subclass);
  13325. +extern void __lockfunc rt_spin_unlock(spinlock_t *lock);
  13326. +extern int __lockfunc rt_spin_unlock_no_deboost(spinlock_t *lock);
  13327. +extern void __lockfunc rt_spin_unlock_wait(spinlock_t *lock);
  13328. +extern int __lockfunc rt_spin_trylock_irqsave(spinlock_t *lock, unsigned long *flags);
  13329. +extern int __lockfunc rt_spin_trylock_bh(spinlock_t *lock);
  13330. +extern int __lockfunc rt_spin_trylock(spinlock_t *lock);
  13331. +extern int atomic_dec_and_spin_lock(atomic_t *atomic, spinlock_t *lock);
  13332. +
  13333. +/*
  13334. + * lockdep-less calls, for derived types like rwlock:
  13335. + * (for trylock they can use rt_mutex_trylock() directly.
  13336. + */
  13337. +extern void __lockfunc __rt_spin_lock__no_mg(struct rt_mutex *lock);
  13338. +extern void __lockfunc __rt_spin_lock(struct rt_mutex *lock);
  13339. +extern void __lockfunc __rt_spin_unlock(struct rt_mutex *lock);
  13340. +extern int __lockfunc __rt_spin_trylock(struct rt_mutex *lock);
  13341. +
  13342. +#define spin_lock(lock) rt_spin_lock(lock)
  13343. +
  13344. +#define spin_lock_bh(lock) \
  13345. + do { \
  13346. + local_bh_disable(); \
  13347. + rt_spin_lock(lock); \
  13348. + } while (0)
  13349. +
  13350. +#define spin_lock_irq(lock) spin_lock(lock)
  13351. +
  13352. +#define spin_do_trylock(lock) __cond_lock(lock, rt_spin_trylock(lock))
  13353. +
  13354. +#define spin_trylock(lock) \
  13355. +({ \
  13356. + int __locked; \
  13357. + __locked = spin_do_trylock(lock); \
  13358. + __locked; \
  13359. +})
  13360. +
  13361. +#ifdef CONFIG_LOCKDEP
  13362. +# define spin_lock_nested(lock, subclass) \
  13363. + do { \
  13364. + rt_spin_lock_nested(lock, subclass); \
  13365. + } while (0)
  13366. +
  13367. +#define spin_lock_bh_nested(lock, subclass) \
  13368. + do { \
  13369. + local_bh_disable(); \
  13370. + rt_spin_lock_nested(lock, subclass); \
  13371. + } while (0)
  13372. +
  13373. +# define spin_lock_irqsave_nested(lock, flags, subclass) \
  13374. + do { \
  13375. + typecheck(unsigned long, flags); \
  13376. + flags = 0; \
  13377. + rt_spin_lock_nested(lock, subclass); \
  13378. + } while (0)
  13379. +#else
  13380. +# define spin_lock_nested(lock, subclass) spin_lock(lock)
  13381. +# define spin_lock_bh_nested(lock, subclass) spin_lock_bh(lock)
  13382. +
  13383. +# define spin_lock_irqsave_nested(lock, flags, subclass) \
  13384. + do { \
  13385. + typecheck(unsigned long, flags); \
  13386. + flags = 0; \
  13387. + spin_lock(lock); \
  13388. + } while (0)
  13389. +#endif
  13390. +
  13391. +#define spin_lock_irqsave(lock, flags) \
  13392. + do { \
  13393. + typecheck(unsigned long, flags); \
  13394. + flags = 0; \
  13395. + spin_lock(lock); \
  13396. + } while (0)
  13397. +
  13398. +static inline unsigned long spin_lock_trace_flags(spinlock_t *lock)
  13399. +{
  13400. + unsigned long flags = 0;
  13401. +#ifdef CONFIG_TRACE_IRQFLAGS
  13402. + flags = rt_spin_lock_trace_flags(lock);
  13403. +#else
  13404. + spin_lock(lock); /* lock_local */
  13405. +#endif
  13406. + return flags;
  13407. +}
  13408. +
  13409. +/* FIXME: we need rt_spin_lock_nest_lock */
  13410. +#define spin_lock_nest_lock(lock, nest_lock) spin_lock_nested(lock, 0)
  13411. +
  13412. +#define spin_unlock(lock) rt_spin_unlock(lock)
  13413. +#define spin_unlock_no_deboost(lock) rt_spin_unlock_no_deboost(lock)
  13414. +
  13415. +#define spin_unlock_bh(lock) \
  13416. + do { \
  13417. + rt_spin_unlock(lock); \
  13418. + local_bh_enable(); \
  13419. + } while (0)
  13420. +
  13421. +#define spin_unlock_irq(lock) spin_unlock(lock)
  13422. +
  13423. +#define spin_unlock_irqrestore(lock, flags) \
  13424. + do { \
  13425. + typecheck(unsigned long, flags); \
  13426. + (void) flags; \
  13427. + spin_unlock(lock); \
  13428. + } while (0)
  13429. +
  13430. +#define spin_trylock_bh(lock) __cond_lock(lock, rt_spin_trylock_bh(lock))
  13431. +#define spin_trylock_irq(lock) spin_trylock(lock)
  13432. +
  13433. +#define spin_trylock_irqsave(lock, flags) \
  13434. + rt_spin_trylock_irqsave(lock, &(flags))
  13435. +
  13436. +#define spin_unlock_wait(lock) rt_spin_unlock_wait(lock)
  13437. +
  13438. +#ifdef CONFIG_GENERIC_LOCKBREAK
  13439. +# define spin_is_contended(lock) ((lock)->break_lock)
  13440. +#else
  13441. +# define spin_is_contended(lock) (((void)(lock), 0))
  13442. +#endif
  13443. +
  13444. +static inline int spin_can_lock(spinlock_t *lock)
  13445. +{
  13446. + return !rt_mutex_is_locked(&lock->lock);
  13447. +}
  13448. +
  13449. +static inline int spin_is_locked(spinlock_t *lock)
  13450. +{
  13451. + return rt_mutex_is_locked(&lock->lock);
  13452. +}
  13453. +
  13454. +static inline void assert_spin_locked(spinlock_t *lock)
  13455. +{
  13456. + BUG_ON(!spin_is_locked(lock));
  13457. +}
  13458. +
  13459. +#define atomic_dec_and_lock(atomic, lock) \
  13460. + atomic_dec_and_spin_lock(atomic, lock)
  13461. +
  13462. +#endif
  13463. diff -Nur linux-4.4.46.orig/include/linux/spinlock_types.h linux-4.4.46/include/linux/spinlock_types.h
  13464. --- linux-4.4.46.orig/include/linux/spinlock_types.h 2017-02-01 08:31:11.000000000 +0100
  13465. +++ linux-4.4.46/include/linux/spinlock_types.h 2017-02-03 17:18:10.911618440 +0100
  13466. @@ -9,80 +9,15 @@
  13467. * Released under the General Public License (GPL).
  13468. */
  13469. -#if defined(CONFIG_SMP)
  13470. -# include <asm/spinlock_types.h>
  13471. -#else
  13472. -# include <linux/spinlock_types_up.h>
  13473. -#endif
  13474. -
  13475. -#include <linux/lockdep.h>
  13476. -
  13477. -typedef struct raw_spinlock {
  13478. - arch_spinlock_t raw_lock;
  13479. -#ifdef CONFIG_GENERIC_LOCKBREAK
  13480. - unsigned int break_lock;
  13481. -#endif
  13482. -#ifdef CONFIG_DEBUG_SPINLOCK
  13483. - unsigned int magic, owner_cpu;
  13484. - void *owner;
  13485. -#endif
  13486. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  13487. - struct lockdep_map dep_map;
  13488. -#endif
  13489. -} raw_spinlock_t;
  13490. -
  13491. -#define SPINLOCK_MAGIC 0xdead4ead
  13492. -
  13493. -#define SPINLOCK_OWNER_INIT ((void *)-1L)
  13494. -
  13495. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  13496. -# define SPIN_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }
  13497. -#else
  13498. -# define SPIN_DEP_MAP_INIT(lockname)
  13499. -#endif
  13500. +#include <linux/spinlock_types_raw.h>
  13501. -#ifdef CONFIG_DEBUG_SPINLOCK
  13502. -# define SPIN_DEBUG_INIT(lockname) \
  13503. - .magic = SPINLOCK_MAGIC, \
  13504. - .owner_cpu = -1, \
  13505. - .owner = SPINLOCK_OWNER_INIT,
  13506. +#ifndef CONFIG_PREEMPT_RT_FULL
  13507. +# include <linux/spinlock_types_nort.h>
  13508. +# include <linux/rwlock_types.h>
  13509. #else
  13510. -# define SPIN_DEBUG_INIT(lockname)
  13511. +# include <linux/rtmutex.h>
  13512. +# include <linux/spinlock_types_rt.h>
  13513. +# include <linux/rwlock_types_rt.h>
  13514. #endif
  13515. -#define __RAW_SPIN_LOCK_INITIALIZER(lockname) \
  13516. - { \
  13517. - .raw_lock = __ARCH_SPIN_LOCK_UNLOCKED, \
  13518. - SPIN_DEBUG_INIT(lockname) \
  13519. - SPIN_DEP_MAP_INIT(lockname) }
  13520. -
  13521. -#define __RAW_SPIN_LOCK_UNLOCKED(lockname) \
  13522. - (raw_spinlock_t) __RAW_SPIN_LOCK_INITIALIZER(lockname)
  13523. -
  13524. -#define DEFINE_RAW_SPINLOCK(x) raw_spinlock_t x = __RAW_SPIN_LOCK_UNLOCKED(x)
  13525. -
  13526. -typedef struct spinlock {
  13527. - union {
  13528. - struct raw_spinlock rlock;
  13529. -
  13530. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  13531. -# define LOCK_PADSIZE (offsetof(struct raw_spinlock, dep_map))
  13532. - struct {
  13533. - u8 __padding[LOCK_PADSIZE];
  13534. - struct lockdep_map dep_map;
  13535. - };
  13536. -#endif
  13537. - };
  13538. -} spinlock_t;
  13539. -
  13540. -#define __SPIN_LOCK_INITIALIZER(lockname) \
  13541. - { { .rlock = __RAW_SPIN_LOCK_INITIALIZER(lockname) } }
  13542. -
  13543. -#define __SPIN_LOCK_UNLOCKED(lockname) \
  13544. - (spinlock_t ) __SPIN_LOCK_INITIALIZER(lockname)
  13545. -
  13546. -#define DEFINE_SPINLOCK(x) spinlock_t x = __SPIN_LOCK_UNLOCKED(x)
  13547. -
  13548. -#include <linux/rwlock_types.h>
  13549. -
  13550. #endif /* __LINUX_SPINLOCK_TYPES_H */
  13551. diff -Nur linux-4.4.46.orig/include/linux/spinlock_types_nort.h linux-4.4.46/include/linux/spinlock_types_nort.h
  13552. --- linux-4.4.46.orig/include/linux/spinlock_types_nort.h 1970-01-01 01:00:00.000000000 +0100
  13553. +++ linux-4.4.46/include/linux/spinlock_types_nort.h 2017-02-03 17:18:10.911618440 +0100
  13554. @@ -0,0 +1,33 @@
  13555. +#ifndef __LINUX_SPINLOCK_TYPES_NORT_H
  13556. +#define __LINUX_SPINLOCK_TYPES_NORT_H
  13557. +
  13558. +#ifndef __LINUX_SPINLOCK_TYPES_H
  13559. +#error "Do not include directly. Include spinlock_types.h instead"
  13560. +#endif
  13561. +
  13562. +/*
  13563. + * The non RT version maps spinlocks to raw_spinlocks
  13564. + */
  13565. +typedef struct spinlock {
  13566. + union {
  13567. + struct raw_spinlock rlock;
  13568. +
  13569. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  13570. +# define LOCK_PADSIZE (offsetof(struct raw_spinlock, dep_map))
  13571. + struct {
  13572. + u8 __padding[LOCK_PADSIZE];
  13573. + struct lockdep_map dep_map;
  13574. + };
  13575. +#endif
  13576. + };
  13577. +} spinlock_t;
  13578. +
  13579. +#define __SPIN_LOCK_INITIALIZER(lockname) \
  13580. + { { .rlock = __RAW_SPIN_LOCK_INITIALIZER(lockname) } }
  13581. +
  13582. +#define __SPIN_LOCK_UNLOCKED(lockname) \
  13583. + (spinlock_t ) __SPIN_LOCK_INITIALIZER(lockname)
  13584. +
  13585. +#define DEFINE_SPINLOCK(x) spinlock_t x = __SPIN_LOCK_UNLOCKED(x)
  13586. +
  13587. +#endif
  13588. diff -Nur linux-4.4.46.orig/include/linux/spinlock_types_raw.h linux-4.4.46/include/linux/spinlock_types_raw.h
  13589. --- linux-4.4.46.orig/include/linux/spinlock_types_raw.h 1970-01-01 01:00:00.000000000 +0100
  13590. +++ linux-4.4.46/include/linux/spinlock_types_raw.h 2017-02-03 17:18:10.911618440 +0100
  13591. @@ -0,0 +1,56 @@
  13592. +#ifndef __LINUX_SPINLOCK_TYPES_RAW_H
  13593. +#define __LINUX_SPINLOCK_TYPES_RAW_H
  13594. +
  13595. +#if defined(CONFIG_SMP)
  13596. +# include <asm/spinlock_types.h>
  13597. +#else
  13598. +# include <linux/spinlock_types_up.h>
  13599. +#endif
  13600. +
  13601. +#include <linux/lockdep.h>
  13602. +
  13603. +typedef struct raw_spinlock {
  13604. + arch_spinlock_t raw_lock;
  13605. +#ifdef CONFIG_GENERIC_LOCKBREAK
  13606. + unsigned int break_lock;
  13607. +#endif
  13608. +#ifdef CONFIG_DEBUG_SPINLOCK
  13609. + unsigned int magic, owner_cpu;
  13610. + void *owner;
  13611. +#endif
  13612. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  13613. + struct lockdep_map dep_map;
  13614. +#endif
  13615. +} raw_spinlock_t;
  13616. +
  13617. +#define SPINLOCK_MAGIC 0xdead4ead
  13618. +
  13619. +#define SPINLOCK_OWNER_INIT ((void *)-1L)
  13620. +
  13621. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  13622. +# define SPIN_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }
  13623. +#else
  13624. +# define SPIN_DEP_MAP_INIT(lockname)
  13625. +#endif
  13626. +
  13627. +#ifdef CONFIG_DEBUG_SPINLOCK
  13628. +# define SPIN_DEBUG_INIT(lockname) \
  13629. + .magic = SPINLOCK_MAGIC, \
  13630. + .owner_cpu = -1, \
  13631. + .owner = SPINLOCK_OWNER_INIT,
  13632. +#else
  13633. +# define SPIN_DEBUG_INIT(lockname)
  13634. +#endif
  13635. +
  13636. +#define __RAW_SPIN_LOCK_INITIALIZER(lockname) \
  13637. + { \
  13638. + .raw_lock = __ARCH_SPIN_LOCK_UNLOCKED, \
  13639. + SPIN_DEBUG_INIT(lockname) \
  13640. + SPIN_DEP_MAP_INIT(lockname) }
  13641. +
  13642. +#define __RAW_SPIN_LOCK_UNLOCKED(lockname) \
  13643. + (raw_spinlock_t) __RAW_SPIN_LOCK_INITIALIZER(lockname)
  13644. +
  13645. +#define DEFINE_RAW_SPINLOCK(x) raw_spinlock_t x = __RAW_SPIN_LOCK_UNLOCKED(x)
  13646. +
  13647. +#endif
  13648. diff -Nur linux-4.4.46.orig/include/linux/spinlock_types_rt.h linux-4.4.46/include/linux/spinlock_types_rt.h
  13649. --- linux-4.4.46.orig/include/linux/spinlock_types_rt.h 1970-01-01 01:00:00.000000000 +0100
  13650. +++ linux-4.4.46/include/linux/spinlock_types_rt.h 2017-02-03 17:18:10.911618440 +0100
  13651. @@ -0,0 +1,51 @@
  13652. +#ifndef __LINUX_SPINLOCK_TYPES_RT_H
  13653. +#define __LINUX_SPINLOCK_TYPES_RT_H
  13654. +
  13655. +#ifndef __LINUX_SPINLOCK_TYPES_H
  13656. +#error "Do not include directly. Include spinlock_types.h instead"
  13657. +#endif
  13658. +
  13659. +#include <linux/cache.h>
  13660. +
  13661. +/*
  13662. + * PREEMPT_RT: spinlocks - an RT mutex plus lock-break field:
  13663. + */
  13664. +typedef struct spinlock {
  13665. + struct rt_mutex lock;
  13666. + unsigned int break_lock;
  13667. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  13668. + struct lockdep_map dep_map;
  13669. +#endif
  13670. +} spinlock_t;
  13671. +
  13672. +#ifdef CONFIG_DEBUG_RT_MUTEXES
  13673. +# define __RT_SPIN_INITIALIZER(name) \
  13674. + { \
  13675. + .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock), \
  13676. + .save_state = 1, \
  13677. + .file = __FILE__, \
  13678. + .line = __LINE__ , \
  13679. + }
  13680. +#else
  13681. +# define __RT_SPIN_INITIALIZER(name) \
  13682. + { \
  13683. + .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock), \
  13684. + .save_state = 1, \
  13685. + }
  13686. +#endif
  13687. +
  13688. +/*
  13689. +.wait_list = PLIST_HEAD_INIT_RAW((name).lock.wait_list, (name).lock.wait_lock)
  13690. +*/
  13691. +
  13692. +#define __SPIN_LOCK_UNLOCKED(name) \
  13693. + { .lock = __RT_SPIN_INITIALIZER(name.lock), \
  13694. + SPIN_DEP_MAP_INIT(name) }
  13695. +
  13696. +#define __DEFINE_SPINLOCK(name) \
  13697. + spinlock_t name = __SPIN_LOCK_UNLOCKED(name)
  13698. +
  13699. +#define DEFINE_SPINLOCK(name) \
  13700. + spinlock_t name __cacheline_aligned_in_smp = __SPIN_LOCK_UNLOCKED(name)
  13701. +
  13702. +#endif
  13703. diff -Nur linux-4.4.46.orig/include/linux/srcu.h linux-4.4.46/include/linux/srcu.h
  13704. --- linux-4.4.46.orig/include/linux/srcu.h 2017-02-01 08:31:11.000000000 +0100
  13705. +++ linux-4.4.46/include/linux/srcu.h 2017-02-03 17:18:10.911618440 +0100
  13706. @@ -84,10 +84,10 @@
  13707. void process_srcu(struct work_struct *work);
  13708. -#define __SRCU_STRUCT_INIT(name) \
  13709. +#define __SRCU_STRUCT_INIT(name, pcpu_name) \
  13710. { \
  13711. .completed = -300, \
  13712. - .per_cpu_ref = &name##_srcu_array, \
  13713. + .per_cpu_ref = &pcpu_name, \
  13714. .queue_lock = __SPIN_LOCK_UNLOCKED(name.queue_lock), \
  13715. .running = false, \
  13716. .batch_queue = RCU_BATCH_INIT(name.batch_queue), \
  13717. @@ -104,7 +104,7 @@
  13718. */
  13719. #define __DEFINE_SRCU(name, is_static) \
  13720. static DEFINE_PER_CPU(struct srcu_struct_array, name##_srcu_array);\
  13721. - is_static struct srcu_struct name = __SRCU_STRUCT_INIT(name)
  13722. + is_static struct srcu_struct name = __SRCU_STRUCT_INIT(name, name##_srcu_array)
  13723. #define DEFINE_SRCU(name) __DEFINE_SRCU(name, /* not static */)
  13724. #define DEFINE_STATIC_SRCU(name) __DEFINE_SRCU(name, static)
  13725. diff -Nur linux-4.4.46.orig/include/linux/suspend.h linux-4.4.46/include/linux/suspend.h
  13726. --- linux-4.4.46.orig/include/linux/suspend.h 2017-02-01 08:31:11.000000000 +0100
  13727. +++ linux-4.4.46/include/linux/suspend.h 2017-02-03 17:18:10.911618440 +0100
  13728. @@ -194,6 +194,12 @@
  13729. void (*end)(void);
  13730. };
  13731. +#if defined(CONFIG_SUSPEND) || defined(CONFIG_HIBERNATION)
  13732. +extern bool pm_in_action;
  13733. +#else
  13734. +# define pm_in_action false
  13735. +#endif
  13736. +
  13737. #ifdef CONFIG_SUSPEND
  13738. /**
  13739. * suspend_set_ops - set platform dependent suspend operations
  13740. diff -Nur linux-4.4.46.orig/include/linux/swait.h linux-4.4.46/include/linux/swait.h
  13741. --- linux-4.4.46.orig/include/linux/swait.h 1970-01-01 01:00:00.000000000 +0100
  13742. +++ linux-4.4.46/include/linux/swait.h 2017-02-03 17:18:10.915618595 +0100
  13743. @@ -0,0 +1,173 @@
  13744. +#ifndef _LINUX_SWAIT_H
  13745. +#define _LINUX_SWAIT_H
  13746. +
  13747. +#include <linux/list.h>
  13748. +#include <linux/stddef.h>
  13749. +#include <linux/spinlock.h>
  13750. +#include <asm/current.h>
  13751. +
  13752. +/*
  13753. + * Simple wait queues
  13754. + *
  13755. + * While these are very similar to the other/complex wait queues (wait.h) the
  13756. + * most important difference is that the simple waitqueue allows for
  13757. + * deterministic behaviour -- IOW it has strictly bounded IRQ and lock hold
  13758. + * times.
  13759. + *
  13760. + * In order to make this so, we had to drop a fair number of features of the
  13761. + * other waitqueue code; notably:
  13762. + *
  13763. + * - mixing INTERRUPTIBLE and UNINTERRUPTIBLE sleeps on the same waitqueue;
  13764. + * all wakeups are TASK_NORMAL in order to avoid O(n) lookups for the right
  13765. + * sleeper state.
  13766. + *
  13767. + * - the exclusive mode; because this requires preserving the list order
  13768. + * and this is hard.
  13769. + *
  13770. + * - custom wake functions; because you cannot give any guarantees about
  13771. + * random code.
  13772. + *
  13773. + * As a side effect of this; the data structures are slimmer.
  13774. + *
  13775. + * One would recommend using this wait queue where possible.
  13776. + */
  13777. +
  13778. +struct task_struct;
  13779. +
  13780. +struct swait_queue_head {
  13781. + raw_spinlock_t lock;
  13782. + struct list_head task_list;
  13783. +};
  13784. +
  13785. +struct swait_queue {
  13786. + struct task_struct *task;
  13787. + struct list_head task_list;
  13788. +};
  13789. +
  13790. +#define __SWAITQUEUE_INITIALIZER(name) { \
  13791. + .task = current, \
  13792. + .task_list = LIST_HEAD_INIT((name).task_list), \
  13793. +}
  13794. +
  13795. +#define DECLARE_SWAITQUEUE(name) \
  13796. + struct swait_queue name = __SWAITQUEUE_INITIALIZER(name)
  13797. +
  13798. +#define __SWAIT_QUEUE_HEAD_INITIALIZER(name) { \
  13799. + .lock = __RAW_SPIN_LOCK_UNLOCKED(name.lock), \
  13800. + .task_list = LIST_HEAD_INIT((name).task_list), \
  13801. +}
  13802. +
  13803. +#define DECLARE_SWAIT_QUEUE_HEAD(name) \
  13804. + struct swait_queue_head name = __SWAIT_QUEUE_HEAD_INITIALIZER(name)
  13805. +
  13806. +extern void __init_swait_queue_head(struct swait_queue_head *q, const char *name,
  13807. + struct lock_class_key *key);
  13808. +
  13809. +#define init_swait_queue_head(q) \
  13810. + do { \
  13811. + static struct lock_class_key __key; \
  13812. + __init_swait_queue_head((q), #q, &__key); \
  13813. + } while (0)
  13814. +
  13815. +#ifdef CONFIG_LOCKDEP
  13816. +# define __SWAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
  13817. + ({ init_swait_queue_head(&name); name; })
  13818. +# define DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(name) \
  13819. + struct swait_queue_head name = __SWAIT_QUEUE_HEAD_INIT_ONSTACK(name)
  13820. +#else
  13821. +# define DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(name) \
  13822. + DECLARE_SWAIT_QUEUE_HEAD(name)
  13823. +#endif
  13824. +
  13825. +static inline int swait_active(struct swait_queue_head *q)
  13826. +{
  13827. + return !list_empty(&q->task_list);
  13828. +}
  13829. +
  13830. +extern void swake_up(struct swait_queue_head *q);
  13831. +extern void swake_up_all(struct swait_queue_head *q);
  13832. +extern void swake_up_locked(struct swait_queue_head *q);
  13833. +extern void swake_up_all_locked(struct swait_queue_head *q);
  13834. +
  13835. +extern void __prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait);
  13836. +extern void prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait, int state);
  13837. +extern long prepare_to_swait_event(struct swait_queue_head *q, struct swait_queue *wait, int state);
  13838. +
  13839. +extern void __finish_swait(struct swait_queue_head *q, struct swait_queue *wait);
  13840. +extern void finish_swait(struct swait_queue_head *q, struct swait_queue *wait);
  13841. +
  13842. +/* as per ___wait_event() but for swait, therefore "exclusive == 0" */
  13843. +#define ___swait_event(wq, condition, state, ret, cmd) \
  13844. +({ \
  13845. + struct swait_queue __wait; \
  13846. + long __ret = ret; \
  13847. + \
  13848. + INIT_LIST_HEAD(&__wait.task_list); \
  13849. + for (;;) { \
  13850. + long __int = prepare_to_swait_event(&wq, &__wait, state);\
  13851. + \
  13852. + if (condition) \
  13853. + break; \
  13854. + \
  13855. + if (___wait_is_interruptible(state) && __int) { \
  13856. + __ret = __int; \
  13857. + break; \
  13858. + } \
  13859. + \
  13860. + cmd; \
  13861. + } \
  13862. + finish_swait(&wq, &__wait); \
  13863. + __ret; \
  13864. +})
  13865. +
  13866. +#define __swait_event(wq, condition) \
  13867. + (void)___swait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, \
  13868. + schedule())
  13869. +
  13870. +#define swait_event(wq, condition) \
  13871. +do { \
  13872. + if (condition) \
  13873. + break; \
  13874. + __swait_event(wq, condition); \
  13875. +} while (0)
  13876. +
  13877. +#define __swait_event_timeout(wq, condition, timeout) \
  13878. + ___swait_event(wq, ___wait_cond_timeout(condition), \
  13879. + TASK_UNINTERRUPTIBLE, timeout, \
  13880. + __ret = schedule_timeout(__ret))
  13881. +
  13882. +#define swait_event_timeout(wq, condition, timeout) \
  13883. +({ \
  13884. + long __ret = timeout; \
  13885. + if (!___wait_cond_timeout(condition)) \
  13886. + __ret = __swait_event_timeout(wq, condition, timeout); \
  13887. + __ret; \
  13888. +})
  13889. +
  13890. +#define __swait_event_interruptible(wq, condition) \
  13891. + ___swait_event(wq, condition, TASK_INTERRUPTIBLE, 0, \
  13892. + schedule())
  13893. +
  13894. +#define swait_event_interruptible(wq, condition) \
  13895. +({ \
  13896. + int __ret = 0; \
  13897. + if (!(condition)) \
  13898. + __ret = __swait_event_interruptible(wq, condition); \
  13899. + __ret; \
  13900. +})
  13901. +
  13902. +#define __swait_event_interruptible_timeout(wq, condition, timeout) \
  13903. + ___swait_event(wq, ___wait_cond_timeout(condition), \
  13904. + TASK_INTERRUPTIBLE, timeout, \
  13905. + __ret = schedule_timeout(__ret))
  13906. +
  13907. +#define swait_event_interruptible_timeout(wq, condition, timeout) \
  13908. +({ \
  13909. + long __ret = timeout; \
  13910. + if (!___wait_cond_timeout(condition)) \
  13911. + __ret = __swait_event_interruptible_timeout(wq, \
  13912. + condition, timeout); \
  13913. + __ret; \
  13914. +})
  13915. +
  13916. +#endif /* _LINUX_SWAIT_H */
  13917. diff -Nur linux-4.4.46.orig/include/linux/swap.h linux-4.4.46/include/linux/swap.h
  13918. --- linux-4.4.46.orig/include/linux/swap.h 2017-02-01 08:31:11.000000000 +0100
  13919. +++ linux-4.4.46/include/linux/swap.h 2017-02-03 17:18:10.915618595 +0100
  13920. @@ -11,6 +11,7 @@
  13921. #include <linux/fs.h>
  13922. #include <linux/atomic.h>
  13923. #include <linux/page-flags.h>
  13924. +#include <linux/locallock.h>
  13925. #include <asm/page.h>
  13926. struct notifier_block;
  13927. @@ -252,7 +253,8 @@
  13928. void *workingset_eviction(struct address_space *mapping, struct page *page);
  13929. bool workingset_refault(void *shadow);
  13930. void workingset_activation(struct page *page);
  13931. -extern struct list_lru workingset_shadow_nodes;
  13932. +extern struct list_lru __workingset_shadow_nodes;
  13933. +DECLARE_LOCAL_IRQ_LOCK(workingset_shadow_lock);
  13934. static inline unsigned int workingset_node_pages(struct radix_tree_node *node)
  13935. {
  13936. @@ -298,6 +300,7 @@
  13937. /* linux/mm/swap.c */
  13938. +DECLARE_LOCAL_IRQ_LOCK(swapvec_lock);
  13939. extern void lru_cache_add(struct page *);
  13940. extern void lru_cache_add_anon(struct page *page);
  13941. extern void lru_cache_add_file(struct page *page);
  13942. diff -Nur linux-4.4.46.orig/include/linux/swork.h linux-4.4.46/include/linux/swork.h
  13943. --- linux-4.4.46.orig/include/linux/swork.h 1970-01-01 01:00:00.000000000 +0100
  13944. +++ linux-4.4.46/include/linux/swork.h 2017-02-03 17:18:10.915618595 +0100
  13945. @@ -0,0 +1,24 @@
  13946. +#ifndef _LINUX_SWORK_H
  13947. +#define _LINUX_SWORK_H
  13948. +
  13949. +#include <linux/list.h>
  13950. +
  13951. +struct swork_event {
  13952. + struct list_head item;
  13953. + unsigned long flags;
  13954. + void (*func)(struct swork_event *);
  13955. +};
  13956. +
  13957. +static inline void INIT_SWORK(struct swork_event *event,
  13958. + void (*func)(struct swork_event *))
  13959. +{
  13960. + event->flags = 0;
  13961. + event->func = func;
  13962. +}
  13963. +
  13964. +bool swork_queue(struct swork_event *sev);
  13965. +
  13966. +int swork_get(void);
  13967. +void swork_put(void);
  13968. +
  13969. +#endif /* _LINUX_SWORK_H */
  13970. diff -Nur linux-4.4.46.orig/include/linux/thread_info.h linux-4.4.46/include/linux/thread_info.h
  13971. --- linux-4.4.46.orig/include/linux/thread_info.h 2017-02-01 08:31:11.000000000 +0100
  13972. +++ linux-4.4.46/include/linux/thread_info.h 2017-02-03 17:18:10.915618595 +0100
  13973. @@ -102,7 +102,17 @@
  13974. #define test_thread_flag(flag) \
  13975. test_ti_thread_flag(current_thread_info(), flag)
  13976. -#define tif_need_resched() test_thread_flag(TIF_NEED_RESCHED)
  13977. +#ifdef CONFIG_PREEMPT_LAZY
  13978. +#define tif_need_resched() (test_thread_flag(TIF_NEED_RESCHED) || \
  13979. + test_thread_flag(TIF_NEED_RESCHED_LAZY))
  13980. +#define tif_need_resched_now() (test_thread_flag(TIF_NEED_RESCHED))
  13981. +#define tif_need_resched_lazy() test_thread_flag(TIF_NEED_RESCHED_LAZY))
  13982. +
  13983. +#else
  13984. +#define tif_need_resched() test_thread_flag(TIF_NEED_RESCHED)
  13985. +#define tif_need_resched_now() test_thread_flag(TIF_NEED_RESCHED)
  13986. +#define tif_need_resched_lazy() 0
  13987. +#endif
  13988. #if defined TIF_RESTORE_SIGMASK && !defined HAVE_SET_RESTORE_SIGMASK
  13989. /*
  13990. diff -Nur linux-4.4.46.orig/include/linux/timer.h linux-4.4.46/include/linux/timer.h
  13991. --- linux-4.4.46.orig/include/linux/timer.h 2017-02-01 08:31:11.000000000 +0100
  13992. +++ linux-4.4.46/include/linux/timer.h 2017-02-03 17:18:10.915618595 +0100
  13993. @@ -225,7 +225,7 @@
  13994. extern int try_to_del_timer_sync(struct timer_list *timer);
  13995. -#ifdef CONFIG_SMP
  13996. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  13997. extern int del_timer_sync(struct timer_list *timer);
  13998. #else
  13999. # define del_timer_sync(t) del_timer(t)
  14000. diff -Nur linux-4.4.46.orig/include/linux/trace_events.h linux-4.4.46/include/linux/trace_events.h
  14001. --- linux-4.4.46.orig/include/linux/trace_events.h 2017-02-01 08:31:11.000000000 +0100
  14002. +++ linux-4.4.46/include/linux/trace_events.h 2017-02-03 17:18:10.915618595 +0100
  14003. @@ -66,6 +66,9 @@
  14004. unsigned char flags;
  14005. unsigned char preempt_count;
  14006. int pid;
  14007. + unsigned short migrate_disable;
  14008. + unsigned short padding;
  14009. + unsigned char preempt_lazy_count;
  14010. };
  14011. #define TRACE_EVENT_TYPE_MAX \
  14012. diff -Nur linux-4.4.46.orig/include/linux/uaccess.h linux-4.4.46/include/linux/uaccess.h
  14013. --- linux-4.4.46.orig/include/linux/uaccess.h 2017-02-01 08:31:11.000000000 +0100
  14014. +++ linux-4.4.46/include/linux/uaccess.h 2017-02-03 17:18:10.915618595 +0100
  14015. @@ -24,6 +24,7 @@
  14016. */
  14017. static inline void pagefault_disable(void)
  14018. {
  14019. + migrate_disable();
  14020. pagefault_disabled_inc();
  14021. /*
  14022. * make sure to have issued the store before a pagefault
  14023. @@ -40,6 +41,7 @@
  14024. */
  14025. barrier();
  14026. pagefault_disabled_dec();
  14027. + migrate_enable();
  14028. }
  14029. /*
  14030. diff -Nur linux-4.4.46.orig/include/linux/uprobes.h linux-4.4.46/include/linux/uprobes.h
  14031. --- linux-4.4.46.orig/include/linux/uprobes.h 2017-02-01 08:31:11.000000000 +0100
  14032. +++ linux-4.4.46/include/linux/uprobes.h 2017-02-03 17:18:10.915618595 +0100
  14033. @@ -27,6 +27,7 @@
  14034. #include <linux/errno.h>
  14035. #include <linux/rbtree.h>
  14036. #include <linux/types.h>
  14037. +#include <linux/wait.h>
  14038. struct vm_area_struct;
  14039. struct mm_struct;
  14040. diff -Nur linux-4.4.46.orig/include/linux/vmstat.h linux-4.4.46/include/linux/vmstat.h
  14041. --- linux-4.4.46.orig/include/linux/vmstat.h 2017-02-01 08:31:11.000000000 +0100
  14042. +++ linux-4.4.46/include/linux/vmstat.h 2017-02-03 17:18:10.915618595 +0100
  14043. @@ -33,7 +33,9 @@
  14044. */
  14045. static inline void __count_vm_event(enum vm_event_item item)
  14046. {
  14047. + preempt_disable_rt();
  14048. raw_cpu_inc(vm_event_states.event[item]);
  14049. + preempt_enable_rt();
  14050. }
  14051. static inline void count_vm_event(enum vm_event_item item)
  14052. @@ -43,7 +45,9 @@
  14053. static inline void __count_vm_events(enum vm_event_item item, long delta)
  14054. {
  14055. + preempt_disable_rt();
  14056. raw_cpu_add(vm_event_states.event[item], delta);
  14057. + preempt_enable_rt();
  14058. }
  14059. static inline void count_vm_events(enum vm_event_item item, long delta)
  14060. diff -Nur linux-4.4.46.orig/include/linux/wait.h linux-4.4.46/include/linux/wait.h
  14061. --- linux-4.4.46.orig/include/linux/wait.h 2017-02-01 08:31:11.000000000 +0100
  14062. +++ linux-4.4.46/include/linux/wait.h 2017-02-03 17:18:10.915618595 +0100
  14063. @@ -8,6 +8,7 @@
  14064. #include <linux/spinlock.h>
  14065. #include <asm/current.h>
  14066. #include <uapi/linux/wait.h>
  14067. +#include <linux/atomic.h>
  14068. typedef struct __wait_queue wait_queue_t;
  14069. typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key);
  14070. diff -Nur linux-4.4.46.orig/include/net/dst.h linux-4.4.46/include/net/dst.h
  14071. --- linux-4.4.46.orig/include/net/dst.h 2017-02-01 08:31:11.000000000 +0100
  14072. +++ linux-4.4.46/include/net/dst.h 2017-02-03 17:18:10.915618595 +0100
  14073. @@ -437,7 +437,7 @@
  14074. static inline int dst_neigh_output(struct dst_entry *dst, struct neighbour *n,
  14075. struct sk_buff *skb)
  14076. {
  14077. - const struct hh_cache *hh;
  14078. + struct hh_cache *hh;
  14079. if (dst->pending_confirm) {
  14080. unsigned long now = jiffies;
  14081. diff -Nur linux-4.4.46.orig/include/net/neighbour.h linux-4.4.46/include/net/neighbour.h
  14082. --- linux-4.4.46.orig/include/net/neighbour.h 2017-02-01 08:31:11.000000000 +0100
  14083. +++ linux-4.4.46/include/net/neighbour.h 2017-02-03 17:18:10.915618595 +0100
  14084. @@ -446,7 +446,7 @@
  14085. }
  14086. #endif
  14087. -static inline int neigh_hh_output(const struct hh_cache *hh, struct sk_buff *skb)
  14088. +static inline int neigh_hh_output(struct hh_cache *hh, struct sk_buff *skb)
  14089. {
  14090. unsigned int seq;
  14091. int hh_len;
  14092. @@ -501,7 +501,7 @@
  14093. #define NEIGH_CB(skb) ((struct neighbour_cb *)(skb)->cb)
  14094. -static inline void neigh_ha_snapshot(char *dst, const struct neighbour *n,
  14095. +static inline void neigh_ha_snapshot(char *dst, struct neighbour *n,
  14096. const struct net_device *dev)
  14097. {
  14098. unsigned int seq;
  14099. diff -Nur linux-4.4.46.orig/include/net/netns/ipv4.h linux-4.4.46/include/net/netns/ipv4.h
  14100. --- linux-4.4.46.orig/include/net/netns/ipv4.h 2017-02-01 08:31:11.000000000 +0100
  14101. +++ linux-4.4.46/include/net/netns/ipv4.h 2017-02-03 17:18:10.915618595 +0100
  14102. @@ -70,6 +70,7 @@
  14103. int sysctl_icmp_echo_ignore_all;
  14104. int sysctl_icmp_echo_ignore_broadcasts;
  14105. + int sysctl_icmp_echo_sysrq;
  14106. int sysctl_icmp_ignore_bogus_error_responses;
  14107. int sysctl_icmp_ratelimit;
  14108. int sysctl_icmp_ratemask;
  14109. diff -Nur linux-4.4.46.orig/include/trace/events/hist.h linux-4.4.46/include/trace/events/hist.h
  14110. --- linux-4.4.46.orig/include/trace/events/hist.h 1970-01-01 01:00:00.000000000 +0100
  14111. +++ linux-4.4.46/include/trace/events/hist.h 2017-02-03 17:18:10.915618595 +0100
  14112. @@ -0,0 +1,73 @@
  14113. +#undef TRACE_SYSTEM
  14114. +#define TRACE_SYSTEM hist
  14115. +
  14116. +#if !defined(_TRACE_HIST_H) || defined(TRACE_HEADER_MULTI_READ)
  14117. +#define _TRACE_HIST_H
  14118. +
  14119. +#include "latency_hist.h"
  14120. +#include <linux/tracepoint.h>
  14121. +
  14122. +#if !defined(CONFIG_PREEMPT_OFF_HIST) && !defined(CONFIG_INTERRUPT_OFF_HIST)
  14123. +#define trace_preemptirqsoff_hist(a, b)
  14124. +#define trace_preemptirqsoff_hist_rcuidle(a, b)
  14125. +#else
  14126. +TRACE_EVENT(preemptirqsoff_hist,
  14127. +
  14128. + TP_PROTO(int reason, int starthist),
  14129. +
  14130. + TP_ARGS(reason, starthist),
  14131. +
  14132. + TP_STRUCT__entry(
  14133. + __field(int, reason)
  14134. + __field(int, starthist)
  14135. + ),
  14136. +
  14137. + TP_fast_assign(
  14138. + __entry->reason = reason;
  14139. + __entry->starthist = starthist;
  14140. + ),
  14141. +
  14142. + TP_printk("reason=%s starthist=%s", getaction(__entry->reason),
  14143. + __entry->starthist ? "start" : "stop")
  14144. +);
  14145. +#endif
  14146. +
  14147. +#ifndef CONFIG_MISSED_TIMER_OFFSETS_HIST
  14148. +#define trace_hrtimer_interrupt(a, b, c, d)
  14149. +#else
  14150. +TRACE_EVENT(hrtimer_interrupt,
  14151. +
  14152. + TP_PROTO(int cpu, long long offset, struct task_struct *curr,
  14153. + struct task_struct *task),
  14154. +
  14155. + TP_ARGS(cpu, offset, curr, task),
  14156. +
  14157. + TP_STRUCT__entry(
  14158. + __field(int, cpu)
  14159. + __field(long long, offset)
  14160. + __array(char, ccomm, TASK_COMM_LEN)
  14161. + __field(int, cprio)
  14162. + __array(char, tcomm, TASK_COMM_LEN)
  14163. + __field(int, tprio)
  14164. + ),
  14165. +
  14166. + TP_fast_assign(
  14167. + __entry->cpu = cpu;
  14168. + __entry->offset = offset;
  14169. + memcpy(__entry->ccomm, curr->comm, TASK_COMM_LEN);
  14170. + __entry->cprio = curr->prio;
  14171. + memcpy(__entry->tcomm, task != NULL ? task->comm : "<none>",
  14172. + task != NULL ? TASK_COMM_LEN : 7);
  14173. + __entry->tprio = task != NULL ? task->prio : -1;
  14174. + ),
  14175. +
  14176. + TP_printk("cpu=%d offset=%lld curr=%s[%d] thread=%s[%d]",
  14177. + __entry->cpu, __entry->offset, __entry->ccomm,
  14178. + __entry->cprio, __entry->tcomm, __entry->tprio)
  14179. +);
  14180. +#endif
  14181. +
  14182. +#endif /* _TRACE_HIST_H */
  14183. +
  14184. +/* This part must be outside protection */
  14185. +#include <trace/define_trace.h>
  14186. diff -Nur linux-4.4.46.orig/include/trace/events/latency_hist.h linux-4.4.46/include/trace/events/latency_hist.h
  14187. --- linux-4.4.46.orig/include/trace/events/latency_hist.h 1970-01-01 01:00:00.000000000 +0100
  14188. +++ linux-4.4.46/include/trace/events/latency_hist.h 2017-02-03 17:18:10.915618595 +0100
  14189. @@ -0,0 +1,29 @@
  14190. +#ifndef _LATENCY_HIST_H
  14191. +#define _LATENCY_HIST_H
  14192. +
  14193. +enum hist_action {
  14194. + IRQS_ON,
  14195. + PREEMPT_ON,
  14196. + TRACE_STOP,
  14197. + IRQS_OFF,
  14198. + PREEMPT_OFF,
  14199. + TRACE_START,
  14200. +};
  14201. +
  14202. +static char *actions[] = {
  14203. + "IRQS_ON",
  14204. + "PREEMPT_ON",
  14205. + "TRACE_STOP",
  14206. + "IRQS_OFF",
  14207. + "PREEMPT_OFF",
  14208. + "TRACE_START",
  14209. +};
  14210. +
  14211. +static inline char *getaction(int action)
  14212. +{
  14213. + if (action >= 0 && action <= sizeof(actions)/sizeof(actions[0]))
  14214. + return actions[action];
  14215. + return "unknown";
  14216. +}
  14217. +
  14218. +#endif /* _LATENCY_HIST_H */
  14219. diff -Nur linux-4.4.46.orig/include/trace/events/writeback.h linux-4.4.46/include/trace/events/writeback.h
  14220. --- linux-4.4.46.orig/include/trace/events/writeback.h 2017-02-01 08:31:11.000000000 +0100
  14221. +++ linux-4.4.46/include/trace/events/writeback.h 2017-02-03 17:18:10.915618595 +0100
  14222. @@ -134,58 +134,28 @@
  14223. #ifdef CREATE_TRACE_POINTS
  14224. #ifdef CONFIG_CGROUP_WRITEBACK
  14225. -static inline size_t __trace_wb_cgroup_size(struct bdi_writeback *wb)
  14226. +static inline unsigned int __trace_wb_assign_cgroup(struct bdi_writeback *wb)
  14227. {
  14228. - return kernfs_path_len(wb->memcg_css->cgroup->kn) + 1;
  14229. + return wb->memcg_css->cgroup->kn->ino;
  14230. }
  14231. -static inline void __trace_wb_assign_cgroup(char *buf, struct bdi_writeback *wb)
  14232. -{
  14233. - struct cgroup *cgrp = wb->memcg_css->cgroup;
  14234. - char *path;
  14235. -
  14236. - path = cgroup_path(cgrp, buf, kernfs_path_len(cgrp->kn) + 1);
  14237. - WARN_ON_ONCE(path != buf);
  14238. -}
  14239. -
  14240. -static inline size_t __trace_wbc_cgroup_size(struct writeback_control *wbc)
  14241. -{
  14242. - if (wbc->wb)
  14243. - return __trace_wb_cgroup_size(wbc->wb);
  14244. - else
  14245. - return 2;
  14246. -}
  14247. -
  14248. -static inline void __trace_wbc_assign_cgroup(char *buf,
  14249. - struct writeback_control *wbc)
  14250. +static inline unsigned int __trace_wbc_assign_cgroup(struct writeback_control *wbc)
  14251. {
  14252. if (wbc->wb)
  14253. - __trace_wb_assign_cgroup(buf, wbc->wb);
  14254. + return __trace_wb_assign_cgroup(wbc->wb);
  14255. else
  14256. - strcpy(buf, "/");
  14257. + return -1U;
  14258. }
  14259. -
  14260. #else /* CONFIG_CGROUP_WRITEBACK */
  14261. -static inline size_t __trace_wb_cgroup_size(struct bdi_writeback *wb)
  14262. -{
  14263. - return 2;
  14264. -}
  14265. -
  14266. -static inline void __trace_wb_assign_cgroup(char *buf, struct bdi_writeback *wb)
  14267. -{
  14268. - strcpy(buf, "/");
  14269. -}
  14270. -
  14271. -static inline size_t __trace_wbc_cgroup_size(struct writeback_control *wbc)
  14272. +static inline unsigned int __trace_wb_assign_cgroup(struct bdi_writeback *wb)
  14273. {
  14274. - return 2;
  14275. + return -1U;
  14276. }
  14277. -static inline void __trace_wbc_assign_cgroup(char *buf,
  14278. - struct writeback_control *wbc)
  14279. +static inline unsigned int __trace_wbc_assign_cgroup(struct writeback_control *wbc)
  14280. {
  14281. - strcpy(buf, "/");
  14282. + return -1U;
  14283. }
  14284. #endif /* CONFIG_CGROUP_WRITEBACK */
  14285. @@ -201,7 +171,7 @@
  14286. __array(char, name, 32)
  14287. __field(unsigned long, ino)
  14288. __field(int, sync_mode)
  14289. - __dynamic_array(char, cgroup, __trace_wbc_cgroup_size(wbc))
  14290. + __field(unsigned int, cgroup_ino)
  14291. ),
  14292. TP_fast_assign(
  14293. @@ -209,14 +179,14 @@
  14294. dev_name(inode_to_bdi(inode)->dev), 32);
  14295. __entry->ino = inode->i_ino;
  14296. __entry->sync_mode = wbc->sync_mode;
  14297. - __trace_wbc_assign_cgroup(__get_str(cgroup), wbc);
  14298. + __entry->cgroup_ino = __trace_wbc_assign_cgroup(wbc);
  14299. ),
  14300. - TP_printk("bdi %s: ino=%lu sync_mode=%d cgroup=%s",
  14301. + TP_printk("bdi %s: ino=%lu sync_mode=%d cgroup_ino=%u",
  14302. __entry->name,
  14303. __entry->ino,
  14304. __entry->sync_mode,
  14305. - __get_str(cgroup)
  14306. + __entry->cgroup_ino
  14307. )
  14308. );
  14309. @@ -246,7 +216,7 @@
  14310. __field(int, range_cyclic)
  14311. __field(int, for_background)
  14312. __field(int, reason)
  14313. - __dynamic_array(char, cgroup, __trace_wb_cgroup_size(wb))
  14314. + __field(unsigned int, cgroup_ino)
  14315. ),
  14316. TP_fast_assign(
  14317. strncpy(__entry->name,
  14318. @@ -258,10 +228,10 @@
  14319. __entry->range_cyclic = work->range_cyclic;
  14320. __entry->for_background = work->for_background;
  14321. __entry->reason = work->reason;
  14322. - __trace_wb_assign_cgroup(__get_str(cgroup), wb);
  14323. + __entry->cgroup_ino = __trace_wb_assign_cgroup(wb);
  14324. ),
  14325. TP_printk("bdi %s: sb_dev %d:%d nr_pages=%ld sync_mode=%d "
  14326. - "kupdate=%d range_cyclic=%d background=%d reason=%s cgroup=%s",
  14327. + "kupdate=%d range_cyclic=%d background=%d reason=%s cgroup_ino=%u",
  14328. __entry->name,
  14329. MAJOR(__entry->sb_dev), MINOR(__entry->sb_dev),
  14330. __entry->nr_pages,
  14331. @@ -270,7 +240,7 @@
  14332. __entry->range_cyclic,
  14333. __entry->for_background,
  14334. __print_symbolic(__entry->reason, WB_WORK_REASON),
  14335. - __get_str(cgroup)
  14336. + __entry->cgroup_ino
  14337. )
  14338. );
  14339. #define DEFINE_WRITEBACK_WORK_EVENT(name) \
  14340. @@ -300,15 +270,15 @@
  14341. TP_ARGS(wb),
  14342. TP_STRUCT__entry(
  14343. __array(char, name, 32)
  14344. - __dynamic_array(char, cgroup, __trace_wb_cgroup_size(wb))
  14345. + __field(unsigned int, cgroup_ino)
  14346. ),
  14347. TP_fast_assign(
  14348. strncpy(__entry->name, dev_name(wb->bdi->dev), 32);
  14349. - __trace_wb_assign_cgroup(__get_str(cgroup), wb);
  14350. + __entry->cgroup_ino = __trace_wb_assign_cgroup(wb);
  14351. ),
  14352. - TP_printk("bdi %s: cgroup=%s",
  14353. + TP_printk("bdi %s: cgroup_ino=%u",
  14354. __entry->name,
  14355. - __get_str(cgroup)
  14356. + __entry->cgroup_ino
  14357. )
  14358. );
  14359. #define DEFINE_WRITEBACK_EVENT(name) \
  14360. @@ -347,7 +317,7 @@
  14361. __field(int, range_cyclic)
  14362. __field(long, range_start)
  14363. __field(long, range_end)
  14364. - __dynamic_array(char, cgroup, __trace_wbc_cgroup_size(wbc))
  14365. + __field(unsigned int, cgroup_ino)
  14366. ),
  14367. TP_fast_assign(
  14368. @@ -361,12 +331,12 @@
  14369. __entry->range_cyclic = wbc->range_cyclic;
  14370. __entry->range_start = (long)wbc->range_start;
  14371. __entry->range_end = (long)wbc->range_end;
  14372. - __trace_wbc_assign_cgroup(__get_str(cgroup), wbc);
  14373. + __entry->cgroup_ino = __trace_wbc_assign_cgroup(wbc);
  14374. ),
  14375. TP_printk("bdi %s: towrt=%ld skip=%ld mode=%d kupd=%d "
  14376. "bgrd=%d reclm=%d cyclic=%d "
  14377. - "start=0x%lx end=0x%lx cgroup=%s",
  14378. + "start=0x%lx end=0x%lx cgroup_ino=%u",
  14379. __entry->name,
  14380. __entry->nr_to_write,
  14381. __entry->pages_skipped,
  14382. @@ -377,7 +347,7 @@
  14383. __entry->range_cyclic,
  14384. __entry->range_start,
  14385. __entry->range_end,
  14386. - __get_str(cgroup)
  14387. + __entry->cgroup_ino
  14388. )
  14389. )
  14390. @@ -398,7 +368,7 @@
  14391. __field(long, age)
  14392. __field(int, moved)
  14393. __field(int, reason)
  14394. - __dynamic_array(char, cgroup, __trace_wb_cgroup_size(wb))
  14395. + __field(unsigned int, cgroup_ino)
  14396. ),
  14397. TP_fast_assign(
  14398. unsigned long *older_than_this = work->older_than_this;
  14399. @@ -408,15 +378,15 @@
  14400. (jiffies - *older_than_this) * 1000 / HZ : -1;
  14401. __entry->moved = moved;
  14402. __entry->reason = work->reason;
  14403. - __trace_wb_assign_cgroup(__get_str(cgroup), wb);
  14404. + __entry->cgroup_ino = __trace_wb_assign_cgroup(wb);
  14405. ),
  14406. - TP_printk("bdi %s: older=%lu age=%ld enqueue=%d reason=%s cgroup=%s",
  14407. + TP_printk("bdi %s: older=%lu age=%ld enqueue=%d reason=%s cgroup_ino=%u",
  14408. __entry->name,
  14409. __entry->older, /* older_than_this in jiffies */
  14410. __entry->age, /* older_than_this in relative milliseconds */
  14411. __entry->moved,
  14412. __print_symbolic(__entry->reason, WB_WORK_REASON),
  14413. - __get_str(cgroup)
  14414. + __entry->cgroup_ino
  14415. )
  14416. );
  14417. @@ -484,7 +454,7 @@
  14418. __field(unsigned long, dirty_ratelimit)
  14419. __field(unsigned long, task_ratelimit)
  14420. __field(unsigned long, balanced_dirty_ratelimit)
  14421. - __dynamic_array(char, cgroup, __trace_wb_cgroup_size(wb))
  14422. + __field(unsigned int, cgroup_ino)
  14423. ),
  14424. TP_fast_assign(
  14425. @@ -496,13 +466,13 @@
  14426. __entry->task_ratelimit = KBps(task_ratelimit);
  14427. __entry->balanced_dirty_ratelimit =
  14428. KBps(wb->balanced_dirty_ratelimit);
  14429. - __trace_wb_assign_cgroup(__get_str(cgroup), wb);
  14430. + __entry->cgroup_ino = __trace_wb_assign_cgroup(wb);
  14431. ),
  14432. TP_printk("bdi %s: "
  14433. "write_bw=%lu awrite_bw=%lu dirty_rate=%lu "
  14434. "dirty_ratelimit=%lu task_ratelimit=%lu "
  14435. - "balanced_dirty_ratelimit=%lu cgroup=%s",
  14436. + "balanced_dirty_ratelimit=%lu cgroup_ino=%u",
  14437. __entry->bdi,
  14438. __entry->write_bw, /* write bandwidth */
  14439. __entry->avg_write_bw, /* avg write bandwidth */
  14440. @@ -510,7 +480,7 @@
  14441. __entry->dirty_ratelimit, /* base ratelimit */
  14442. __entry->task_ratelimit, /* ratelimit with position control */
  14443. __entry->balanced_dirty_ratelimit, /* the balanced ratelimit */
  14444. - __get_str(cgroup)
  14445. + __entry->cgroup_ino
  14446. )
  14447. );
  14448. @@ -548,7 +518,7 @@
  14449. __field( long, pause)
  14450. __field(unsigned long, period)
  14451. __field( long, think)
  14452. - __dynamic_array(char, cgroup, __trace_wb_cgroup_size(wb))
  14453. + __field(unsigned int, cgroup_ino)
  14454. ),
  14455. TP_fast_assign(
  14456. @@ -571,7 +541,7 @@
  14457. __entry->period = period * 1000 / HZ;
  14458. __entry->pause = pause * 1000 / HZ;
  14459. __entry->paused = (jiffies - start_time) * 1000 / HZ;
  14460. - __trace_wb_assign_cgroup(__get_str(cgroup), wb);
  14461. + __entry->cgroup_ino = __trace_wb_assign_cgroup(wb);
  14462. ),
  14463. @@ -580,7 +550,7 @@
  14464. "bdi_setpoint=%lu bdi_dirty=%lu "
  14465. "dirty_ratelimit=%lu task_ratelimit=%lu "
  14466. "dirtied=%u dirtied_pause=%u "
  14467. - "paused=%lu pause=%ld period=%lu think=%ld cgroup=%s",
  14468. + "paused=%lu pause=%ld period=%lu think=%ld cgroup_ino=%u",
  14469. __entry->bdi,
  14470. __entry->limit,
  14471. __entry->setpoint,
  14472. @@ -595,7 +565,7 @@
  14473. __entry->pause, /* ms */
  14474. __entry->period, /* ms */
  14475. __entry->think, /* ms */
  14476. - __get_str(cgroup)
  14477. + __entry->cgroup_ino
  14478. )
  14479. );
  14480. @@ -609,8 +579,7 @@
  14481. __field(unsigned long, ino)
  14482. __field(unsigned long, state)
  14483. __field(unsigned long, dirtied_when)
  14484. - __dynamic_array(char, cgroup,
  14485. - __trace_wb_cgroup_size(inode_to_wb(inode)))
  14486. + __field(unsigned int, cgroup_ino)
  14487. ),
  14488. TP_fast_assign(
  14489. @@ -619,16 +588,16 @@
  14490. __entry->ino = inode->i_ino;
  14491. __entry->state = inode->i_state;
  14492. __entry->dirtied_when = inode->dirtied_when;
  14493. - __trace_wb_assign_cgroup(__get_str(cgroup), inode_to_wb(inode));
  14494. + __entry->cgroup_ino = __trace_wb_assign_cgroup(inode_to_wb(inode));
  14495. ),
  14496. - TP_printk("bdi %s: ino=%lu state=%s dirtied_when=%lu age=%lu cgroup=%s",
  14497. + TP_printk("bdi %s: ino=%lu state=%s dirtied_when=%lu age=%lu cgroup_ino=%u",
  14498. __entry->name,
  14499. __entry->ino,
  14500. show_inode_state(__entry->state),
  14501. __entry->dirtied_when,
  14502. (jiffies - __entry->dirtied_when) / HZ,
  14503. - __get_str(cgroup)
  14504. + __entry->cgroup_ino
  14505. )
  14506. );
  14507. @@ -684,7 +653,7 @@
  14508. __field(unsigned long, writeback_index)
  14509. __field(long, nr_to_write)
  14510. __field(unsigned long, wrote)
  14511. - __dynamic_array(char, cgroup, __trace_wbc_cgroup_size(wbc))
  14512. + __field(unsigned int, cgroup_ino)
  14513. ),
  14514. TP_fast_assign(
  14515. @@ -696,11 +665,11 @@
  14516. __entry->writeback_index = inode->i_mapping->writeback_index;
  14517. __entry->nr_to_write = nr_to_write;
  14518. __entry->wrote = nr_to_write - wbc->nr_to_write;
  14519. - __trace_wbc_assign_cgroup(__get_str(cgroup), wbc);
  14520. + __entry->cgroup_ino = __trace_wbc_assign_cgroup(wbc);
  14521. ),
  14522. TP_printk("bdi %s: ino=%lu state=%s dirtied_when=%lu age=%lu "
  14523. - "index=%lu to_write=%ld wrote=%lu cgroup=%s",
  14524. + "index=%lu to_write=%ld wrote=%lu cgroup_ino=%u",
  14525. __entry->name,
  14526. __entry->ino,
  14527. show_inode_state(__entry->state),
  14528. @@ -709,7 +678,7 @@
  14529. __entry->writeback_index,
  14530. __entry->nr_to_write,
  14531. __entry->wrote,
  14532. - __get_str(cgroup)
  14533. + __entry->cgroup_ino
  14534. )
  14535. );
  14536. diff -Nur linux-4.4.46.orig/init/Kconfig linux-4.4.46/init/Kconfig
  14537. --- linux-4.4.46.orig/init/Kconfig 2017-02-01 08:31:11.000000000 +0100
  14538. +++ linux-4.4.46/init/Kconfig 2017-02-03 17:18:10.915618595 +0100
  14539. @@ -498,7 +498,7 @@
  14540. config RCU_EXPERT
  14541. bool "Make expert-level adjustments to RCU configuration"
  14542. - default n
  14543. + default y if PREEMPT_RT_FULL
  14544. help
  14545. This option needs to be enabled if you wish to make
  14546. expert-level adjustments to RCU configuration. By default,
  14547. @@ -614,7 +614,7 @@
  14548. config RCU_FAST_NO_HZ
  14549. bool "Accelerate last non-dyntick-idle CPU's grace periods"
  14550. - depends on NO_HZ_COMMON && SMP && RCU_EXPERT
  14551. + depends on NO_HZ_COMMON && SMP && RCU_EXPERT && !PREEMPT_RT_FULL
  14552. default n
  14553. help
  14554. This option permits CPUs to enter dynticks-idle state even if
  14555. @@ -641,7 +641,7 @@
  14556. config RCU_BOOST
  14557. bool "Enable RCU priority boosting"
  14558. depends on RT_MUTEXES && PREEMPT_RCU && RCU_EXPERT
  14559. - default n
  14560. + default y if PREEMPT_RT_FULL
  14561. help
  14562. This option boosts the priority of preempted RCU readers that
  14563. block the current preemptible RCU grace period for too long.
  14564. @@ -1106,6 +1106,7 @@
  14565. config RT_GROUP_SCHED
  14566. bool "Group scheduling for SCHED_RR/FIFO"
  14567. depends on CGROUP_SCHED
  14568. + depends on !PREEMPT_RT_FULL
  14569. default n
  14570. help
  14571. This feature lets you explicitly allocate real CPU bandwidth
  14572. @@ -1719,6 +1720,7 @@
  14573. config SLAB
  14574. bool "SLAB"
  14575. + depends on !PREEMPT_RT_FULL
  14576. help
  14577. The regular slab allocator that is established and known to work
  14578. well in all environments. It organizes cache hot objects in
  14579. @@ -1737,6 +1739,7 @@
  14580. config SLOB
  14581. depends on EXPERT
  14582. bool "SLOB (Simple Allocator)"
  14583. + depends on !PREEMPT_RT_FULL
  14584. help
  14585. SLOB replaces the stock allocator with a drastically simpler
  14586. allocator. SLOB is generally more space efficient but
  14587. @@ -1746,7 +1749,7 @@
  14588. config SLUB_CPU_PARTIAL
  14589. default y
  14590. - depends on SLUB && SMP
  14591. + depends on SLUB && SMP && !PREEMPT_RT_FULL
  14592. bool "SLUB per cpu partial cache"
  14593. help
  14594. Per cpu partial caches accellerate objects allocation and freeing
  14595. diff -Nur linux-4.4.46.orig/init/main.c linux-4.4.46/init/main.c
  14596. --- linux-4.4.46.orig/init/main.c 2017-02-01 08:31:11.000000000 +0100
  14597. +++ linux-4.4.46/init/main.c 2017-02-03 17:18:10.915618595 +0100
  14598. @@ -530,6 +530,7 @@
  14599. setup_command_line(command_line);
  14600. setup_nr_cpu_ids();
  14601. setup_per_cpu_areas();
  14602. + softirq_early_init();
  14603. smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */
  14604. build_all_zonelists(NULL, NULL);
  14605. diff -Nur linux-4.4.46.orig/init/Makefile linux-4.4.46/init/Makefile
  14606. --- linux-4.4.46.orig/init/Makefile 2017-02-01 08:31:11.000000000 +0100
  14607. +++ linux-4.4.46/init/Makefile 2017-02-03 17:18:10.915618595 +0100
  14608. @@ -33,4 +33,4 @@
  14609. include/generated/compile.h: FORCE
  14610. @$($(quiet)chk_compile.h)
  14611. $(Q)$(CONFIG_SHELL) $(srctree)/scripts/mkcompile_h $@ \
  14612. - "$(UTS_MACHINE)" "$(CONFIG_SMP)" "$(CONFIG_PREEMPT)" "$(CC) $(KBUILD_CFLAGS)"
  14613. + "$(UTS_MACHINE)" "$(CONFIG_SMP)" "$(CONFIG_PREEMPT)" "$(CONFIG_PREEMPT_RT_FULL)" "$(CC) $(KBUILD_CFLAGS)"
  14614. diff -Nur linux-4.4.46.orig/ipc/msg.c linux-4.4.46/ipc/msg.c
  14615. --- linux-4.4.46.orig/ipc/msg.c 2017-02-01 08:31:11.000000000 +0100
  14616. +++ linux-4.4.46/ipc/msg.c 2017-02-03 17:18:10.919618749 +0100
  14617. @@ -183,20 +183,14 @@
  14618. }
  14619. }
  14620. -static void expunge_all(struct msg_queue *msq, int res)
  14621. +static void expunge_all(struct msg_queue *msq, int res,
  14622. + struct wake_q_head *wake_q)
  14623. {
  14624. struct msg_receiver *msr, *t;
  14625. list_for_each_entry_safe(msr, t, &msq->q_receivers, r_list) {
  14626. - msr->r_msg = NULL; /* initialize expunge ordering */
  14627. - wake_up_process(msr->r_tsk);
  14628. - /*
  14629. - * Ensure that the wakeup is visible before setting r_msg as
  14630. - * the receiving end depends on it: either spinning on a nil,
  14631. - * or dealing with -EAGAIN cases. See lockless receive part 1
  14632. - * and 2 in do_msgrcv().
  14633. - */
  14634. - smp_wmb(); /* barrier (B) */
  14635. +
  14636. + wake_q_add(wake_q, msr->r_tsk);
  14637. msr->r_msg = ERR_PTR(res);
  14638. }
  14639. }
  14640. @@ -213,11 +207,13 @@
  14641. {
  14642. struct msg_msg *msg, *t;
  14643. struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
  14644. + WAKE_Q(wake_q);
  14645. - expunge_all(msq, -EIDRM);
  14646. + expunge_all(msq, -EIDRM, &wake_q);
  14647. ss_wakeup(&msq->q_senders, 1);
  14648. msg_rmid(ns, msq);
  14649. ipc_unlock_object(&msq->q_perm);
  14650. + wake_up_q(&wake_q);
  14651. rcu_read_unlock();
  14652. list_for_each_entry_safe(msg, t, &msq->q_messages, m_list) {
  14653. @@ -342,6 +338,7 @@
  14654. struct kern_ipc_perm *ipcp;
  14655. struct msqid64_ds uninitialized_var(msqid64);
  14656. struct msg_queue *msq;
  14657. + WAKE_Q(wake_q);
  14658. int err;
  14659. if (cmd == IPC_SET) {
  14660. @@ -389,7 +386,7 @@
  14661. /* sleeping receivers might be excluded by
  14662. * stricter permissions.
  14663. */
  14664. - expunge_all(msq, -EAGAIN);
  14665. + expunge_all(msq, -EAGAIN, &wake_q);
  14666. /* sleeping senders might be able to send
  14667. * due to a larger queue size.
  14668. */
  14669. @@ -402,6 +399,7 @@
  14670. out_unlock0:
  14671. ipc_unlock_object(&msq->q_perm);
  14672. + wake_up_q(&wake_q);
  14673. out_unlock1:
  14674. rcu_read_unlock();
  14675. out_up:
  14676. @@ -566,7 +564,8 @@
  14677. return 0;
  14678. }
  14679. -static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
  14680. +static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg,
  14681. + struct wake_q_head *wake_q)
  14682. {
  14683. struct msg_receiver *msr, *t;
  14684. @@ -577,27 +576,13 @@
  14685. list_del(&msr->r_list);
  14686. if (msr->r_maxsize < msg->m_ts) {
  14687. - /* initialize pipelined send ordering */
  14688. - msr->r_msg = NULL;
  14689. - wake_up_process(msr->r_tsk);
  14690. - /* barrier (B) see barrier comment below */
  14691. - smp_wmb();
  14692. + wake_q_add(wake_q, msr->r_tsk);
  14693. msr->r_msg = ERR_PTR(-E2BIG);
  14694. } else {
  14695. - msr->r_msg = NULL;
  14696. msq->q_lrpid = task_pid_vnr(msr->r_tsk);
  14697. msq->q_rtime = get_seconds();
  14698. - wake_up_process(msr->r_tsk);
  14699. - /*
  14700. - * Ensure that the wakeup is visible before
  14701. - * setting r_msg, as the receiving can otherwise
  14702. - * exit - once r_msg is set, the receiver can
  14703. - * continue. See lockless receive part 1 and 2
  14704. - * in do_msgrcv(). Barrier (B).
  14705. - */
  14706. - smp_wmb();
  14707. + wake_q_add(wake_q, msr->r_tsk);
  14708. msr->r_msg = msg;
  14709. -
  14710. return 1;
  14711. }
  14712. }
  14713. @@ -613,6 +598,7 @@
  14714. struct msg_msg *msg;
  14715. int err;
  14716. struct ipc_namespace *ns;
  14717. + WAKE_Q(wake_q);
  14718. ns = current->nsproxy->ipc_ns;
  14719. @@ -698,7 +684,7 @@
  14720. msq->q_lspid = task_tgid_vnr(current);
  14721. msq->q_stime = get_seconds();
  14722. - if (!pipelined_send(msq, msg)) {
  14723. + if (!pipelined_send(msq, msg, &wake_q)) {
  14724. /* no one is waiting for this message, enqueue it */
  14725. list_add_tail(&msg->m_list, &msq->q_messages);
  14726. msq->q_cbytes += msgsz;
  14727. @@ -712,6 +698,7 @@
  14728. out_unlock0:
  14729. ipc_unlock_object(&msq->q_perm);
  14730. + wake_up_q(&wake_q);
  14731. out_unlock1:
  14732. rcu_read_unlock();
  14733. if (msg != NULL)
  14734. @@ -932,57 +919,25 @@
  14735. rcu_read_lock();
  14736. /* Lockless receive, part 2:
  14737. - * Wait until pipelined_send or expunge_all are outside of
  14738. - * wake_up_process(). There is a race with exit(), see
  14739. - * ipc/mqueue.c for the details. The correct serialization
  14740. - * ensures that a receiver cannot continue without the wakeup
  14741. - * being visibible _before_ setting r_msg:
  14742. + * The work in pipelined_send() and expunge_all():
  14743. + * - Set pointer to message
  14744. + * - Queue the receiver task for later wakeup
  14745. + * - Wake up the process after the lock is dropped.
  14746. *
  14747. - * CPU 0 CPU 1
  14748. - * <loop receiver>
  14749. - * smp_rmb(); (A) <-- pair -. <waker thread>
  14750. - * <load ->r_msg> | msr->r_msg = NULL;
  14751. - * | wake_up_process();
  14752. - * <continue> `------> smp_wmb(); (B)
  14753. - * msr->r_msg = msg;
  14754. - *
  14755. - * Where (A) orders the message value read and where (B) orders
  14756. - * the write to the r_msg -- done in both pipelined_send and
  14757. - * expunge_all.
  14758. + * Should the process wake up before this wakeup (due to a
  14759. + * signal) it will either see the message and continue …
  14760. */
  14761. - for (;;) {
  14762. - /*
  14763. - * Pairs with writer barrier in pipelined_send
  14764. - * or expunge_all.
  14765. - */
  14766. - smp_rmb(); /* barrier (A) */
  14767. - msg = (struct msg_msg *)msr_d.r_msg;
  14768. - if (msg)
  14769. - break;
  14770. - /*
  14771. - * The cpu_relax() call is a compiler barrier
  14772. - * which forces everything in this loop to be
  14773. - * re-loaded.
  14774. - */
  14775. - cpu_relax();
  14776. - }
  14777. -
  14778. - /* Lockless receive, part 3:
  14779. - * If there is a message or an error then accept it without
  14780. - * locking.
  14781. - */
  14782. + msg = (struct msg_msg *)msr_d.r_msg;
  14783. if (msg != ERR_PTR(-EAGAIN))
  14784. goto out_unlock1;
  14785. - /* Lockless receive, part 3:
  14786. - * Acquire the queue spinlock.
  14787. - */
  14788. + /*
  14789. + * … or see -EAGAIN, acquire the lock to check the message
  14790. + * again.
  14791. + */
  14792. ipc_lock_object(&msq->q_perm);
  14793. - /* Lockless receive, part 4:
  14794. - * Repeat test after acquiring the spinlock.
  14795. - */
  14796. msg = (struct msg_msg *)msr_d.r_msg;
  14797. if (msg != ERR_PTR(-EAGAIN))
  14798. goto out_unlock0;
  14799. diff -Nur linux-4.4.46.orig/ipc/sem.c linux-4.4.46/ipc/sem.c
  14800. --- linux-4.4.46.orig/ipc/sem.c 2017-02-01 08:31:11.000000000 +0100
  14801. +++ linux-4.4.46/ipc/sem.c 2017-02-03 17:18:10.919618749 +0100
  14802. @@ -708,6 +708,13 @@
  14803. static void wake_up_sem_queue_prepare(struct list_head *pt,
  14804. struct sem_queue *q, int error)
  14805. {
  14806. +#ifdef CONFIG_PREEMPT_RT_BASE
  14807. + struct task_struct *p = q->sleeper;
  14808. + get_task_struct(p);
  14809. + q->status = error;
  14810. + wake_up_process(p);
  14811. + put_task_struct(p);
  14812. +#else
  14813. if (list_empty(pt)) {
  14814. /*
  14815. * Hold preempt off so that we don't get preempted and have the
  14816. @@ -719,6 +726,7 @@
  14817. q->pid = error;
  14818. list_add_tail(&q->list, pt);
  14819. +#endif
  14820. }
  14821. /**
  14822. @@ -732,6 +740,7 @@
  14823. */
  14824. static void wake_up_sem_queue_do(struct list_head *pt)
  14825. {
  14826. +#ifndef CONFIG_PREEMPT_RT_BASE
  14827. struct sem_queue *q, *t;
  14828. int did_something;
  14829. @@ -744,6 +753,7 @@
  14830. }
  14831. if (did_something)
  14832. preempt_enable();
  14833. +#endif
  14834. }
  14835. static void unlink_queue(struct sem_array *sma, struct sem_queue *q)
  14836. diff -Nur linux-4.4.46.orig/kernel/cgroup.c linux-4.4.46/kernel/cgroup.c
  14837. --- linux-4.4.46.orig/kernel/cgroup.c 2017-02-01 08:31:11.000000000 +0100
  14838. +++ linux-4.4.46/kernel/cgroup.c 2017-02-03 17:18:10.919618749 +0100
  14839. @@ -4740,10 +4740,10 @@
  14840. queue_work(cgroup_destroy_wq, &css->destroy_work);
  14841. }
  14842. -static void css_release_work_fn(struct work_struct *work)
  14843. +static void css_release_work_fn(struct swork_event *sev)
  14844. {
  14845. struct cgroup_subsys_state *css =
  14846. - container_of(work, struct cgroup_subsys_state, destroy_work);
  14847. + container_of(sev, struct cgroup_subsys_state, destroy_swork);
  14848. struct cgroup_subsys *ss = css->ss;
  14849. struct cgroup *cgrp = css->cgroup;
  14850. @@ -4782,8 +4782,8 @@
  14851. struct cgroup_subsys_state *css =
  14852. container_of(ref, struct cgroup_subsys_state, refcnt);
  14853. - INIT_WORK(&css->destroy_work, css_release_work_fn);
  14854. - queue_work(cgroup_destroy_wq, &css->destroy_work);
  14855. + INIT_SWORK(&css->destroy_swork, css_release_work_fn);
  14856. + swork_queue(&css->destroy_swork);
  14857. }
  14858. static void init_and_link_css(struct cgroup_subsys_state *css,
  14859. @@ -5400,6 +5400,7 @@
  14860. */
  14861. cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
  14862. BUG_ON(!cgroup_destroy_wq);
  14863. + BUG_ON(swork_get());
  14864. /*
  14865. * Used to destroy pidlists and separate to serve as flush domain.
  14866. diff -Nur linux-4.4.46.orig/kernel/cpu.c linux-4.4.46/kernel/cpu.c
  14867. --- linux-4.4.46.orig/kernel/cpu.c 2017-02-01 08:31:11.000000000 +0100
  14868. +++ linux-4.4.46/kernel/cpu.c 2017-02-03 17:18:10.919618749 +0100
  14869. @@ -75,8 +75,8 @@
  14870. #endif
  14871. } cpu_hotplug = {
  14872. .active_writer = NULL,
  14873. - .wq = __WAIT_QUEUE_HEAD_INITIALIZER(cpu_hotplug.wq),
  14874. .lock = __MUTEX_INITIALIZER(cpu_hotplug.lock),
  14875. + .wq = __WAIT_QUEUE_HEAD_INITIALIZER(cpu_hotplug.wq),
  14876. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  14877. .dep_map = {.name = "cpu_hotplug.lock" },
  14878. #endif
  14879. @@ -89,6 +89,289 @@
  14880. #define cpuhp_lock_acquire() lock_map_acquire(&cpu_hotplug.dep_map)
  14881. #define cpuhp_lock_release() lock_map_release(&cpu_hotplug.dep_map)
  14882. +/**
  14883. + * hotplug_pcp - per cpu hotplug descriptor
  14884. + * @unplug: set when pin_current_cpu() needs to sync tasks
  14885. + * @sync_tsk: the task that waits for tasks to finish pinned sections
  14886. + * @refcount: counter of tasks in pinned sections
  14887. + * @grab_lock: set when the tasks entering pinned sections should wait
  14888. + * @synced: notifier for @sync_tsk to tell cpu_down it's finished
  14889. + * @mutex: the mutex to make tasks wait (used when @grab_lock is true)
  14890. + * @mutex_init: zero if the mutex hasn't been initialized yet.
  14891. + *
  14892. + * Although @unplug and @sync_tsk may point to the same task, the @unplug
  14893. + * is used as a flag and still exists after @sync_tsk has exited and
  14894. + * @sync_tsk set to NULL.
  14895. + */
  14896. +struct hotplug_pcp {
  14897. + struct task_struct *unplug;
  14898. + struct task_struct *sync_tsk;
  14899. + int refcount;
  14900. + int grab_lock;
  14901. + struct completion synced;
  14902. + struct completion unplug_wait;
  14903. +#ifdef CONFIG_PREEMPT_RT_FULL
  14904. + /*
  14905. + * Note, on PREEMPT_RT, the hotplug lock must save the state of
  14906. + * the task, otherwise the mutex will cause the task to fail
  14907. + * to sleep when required. (Because it's called from migrate_disable())
  14908. + *
  14909. + * The spinlock_t on PREEMPT_RT is a mutex that saves the task's
  14910. + * state.
  14911. + */
  14912. + spinlock_t lock;
  14913. +#else
  14914. + struct mutex mutex;
  14915. +#endif
  14916. + int mutex_init;
  14917. +};
  14918. +
  14919. +#ifdef CONFIG_PREEMPT_RT_FULL
  14920. +# define hotplug_lock(hp) rt_spin_lock__no_mg(&(hp)->lock)
  14921. +# define hotplug_unlock(hp) rt_spin_unlock__no_mg(&(hp)->lock)
  14922. +#else
  14923. +# define hotplug_lock(hp) mutex_lock(&(hp)->mutex)
  14924. +# define hotplug_unlock(hp) mutex_unlock(&(hp)->mutex)
  14925. +#endif
  14926. +
  14927. +static DEFINE_PER_CPU(struct hotplug_pcp, hotplug_pcp);
  14928. +
  14929. +/**
  14930. + * pin_current_cpu - Prevent the current cpu from being unplugged
  14931. + *
  14932. + * Lightweight version of get_online_cpus() to prevent cpu from being
  14933. + * unplugged when code runs in a migration disabled region.
  14934. + *
  14935. + * Must be called with preemption disabled (preempt_count = 1)!
  14936. + */
  14937. +void pin_current_cpu(void)
  14938. +{
  14939. + struct hotplug_pcp *hp;
  14940. + int force = 0;
  14941. +
  14942. +retry:
  14943. + hp = this_cpu_ptr(&hotplug_pcp);
  14944. +
  14945. + if (!hp->unplug || hp->refcount || force || preempt_count() > 1 ||
  14946. + hp->unplug == current) {
  14947. + hp->refcount++;
  14948. + return;
  14949. + }
  14950. + if (hp->grab_lock) {
  14951. + preempt_enable();
  14952. + hotplug_lock(hp);
  14953. + hotplug_unlock(hp);
  14954. + } else {
  14955. + preempt_enable();
  14956. + /*
  14957. + * Try to push this task off of this CPU.
  14958. + */
  14959. + if (!migrate_me()) {
  14960. + preempt_disable();
  14961. + hp = this_cpu_ptr(&hotplug_pcp);
  14962. + if (!hp->grab_lock) {
  14963. + /*
  14964. + * Just let it continue it's already pinned
  14965. + * or about to sleep.
  14966. + */
  14967. + force = 1;
  14968. + goto retry;
  14969. + }
  14970. + preempt_enable();
  14971. + }
  14972. + }
  14973. + preempt_disable();
  14974. + goto retry;
  14975. +}
  14976. +
  14977. +/**
  14978. + * unpin_current_cpu - Allow unplug of current cpu
  14979. + *
  14980. + * Must be called with preemption or interrupts disabled!
  14981. + */
  14982. +void unpin_current_cpu(void)
  14983. +{
  14984. + struct hotplug_pcp *hp = this_cpu_ptr(&hotplug_pcp);
  14985. +
  14986. + WARN_ON(hp->refcount <= 0);
  14987. +
  14988. + /* This is safe. sync_unplug_thread is pinned to this cpu */
  14989. + if (!--hp->refcount && hp->unplug && hp->unplug != current)
  14990. + wake_up_process(hp->unplug);
  14991. +}
  14992. +
  14993. +static void wait_for_pinned_cpus(struct hotplug_pcp *hp)
  14994. +{
  14995. + set_current_state(TASK_UNINTERRUPTIBLE);
  14996. + while (hp->refcount) {
  14997. + schedule_preempt_disabled();
  14998. + set_current_state(TASK_UNINTERRUPTIBLE);
  14999. + }
  15000. +}
  15001. +
  15002. +static int sync_unplug_thread(void *data)
  15003. +{
  15004. + struct hotplug_pcp *hp = data;
  15005. +
  15006. + wait_for_completion(&hp->unplug_wait);
  15007. + preempt_disable();
  15008. + hp->unplug = current;
  15009. + wait_for_pinned_cpus(hp);
  15010. +
  15011. + /*
  15012. + * This thread will synchronize the cpu_down() with threads
  15013. + * that have pinned the CPU. When the pinned CPU count reaches
  15014. + * zero, we inform the cpu_down code to continue to the next step.
  15015. + */
  15016. + set_current_state(TASK_UNINTERRUPTIBLE);
  15017. + preempt_enable();
  15018. + complete(&hp->synced);
  15019. +
  15020. + /*
  15021. + * If all succeeds, the next step will need tasks to wait till
  15022. + * the CPU is offline before continuing. To do this, the grab_lock
  15023. + * is set and tasks going into pin_current_cpu() will block on the
  15024. + * mutex. But we still need to wait for those that are already in
  15025. + * pinned CPU sections. If the cpu_down() failed, the kthread_should_stop()
  15026. + * will kick this thread out.
  15027. + */
  15028. + while (!hp->grab_lock && !kthread_should_stop()) {
  15029. + schedule();
  15030. + set_current_state(TASK_UNINTERRUPTIBLE);
  15031. + }
  15032. +
  15033. + /* Make sure grab_lock is seen before we see a stale completion */
  15034. + smp_mb();
  15035. +
  15036. + /*
  15037. + * Now just before cpu_down() enters stop machine, we need to make
  15038. + * sure all tasks that are in pinned CPU sections are out, and new
  15039. + * tasks will now grab the lock, keeping them from entering pinned
  15040. + * CPU sections.
  15041. + */
  15042. + if (!kthread_should_stop()) {
  15043. + preempt_disable();
  15044. + wait_for_pinned_cpus(hp);
  15045. + preempt_enable();
  15046. + complete(&hp->synced);
  15047. + }
  15048. +
  15049. + set_current_state(TASK_UNINTERRUPTIBLE);
  15050. + while (!kthread_should_stop()) {
  15051. + schedule();
  15052. + set_current_state(TASK_UNINTERRUPTIBLE);
  15053. + }
  15054. + set_current_state(TASK_RUNNING);
  15055. +
  15056. + /*
  15057. + * Force this thread off this CPU as it's going down and
  15058. + * we don't want any more work on this CPU.
  15059. + */
  15060. + current->flags &= ~PF_NO_SETAFFINITY;
  15061. + set_cpus_allowed_ptr(current, cpu_present_mask);
  15062. + migrate_me();
  15063. + return 0;
  15064. +}
  15065. +
  15066. +static void __cpu_unplug_sync(struct hotplug_pcp *hp)
  15067. +{
  15068. + wake_up_process(hp->sync_tsk);
  15069. + wait_for_completion(&hp->synced);
  15070. +}
  15071. +
  15072. +static void __cpu_unplug_wait(unsigned int cpu)
  15073. +{
  15074. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  15075. +
  15076. + complete(&hp->unplug_wait);
  15077. + wait_for_completion(&hp->synced);
  15078. +}
  15079. +
  15080. +/*
  15081. + * Start the sync_unplug_thread on the target cpu and wait for it to
  15082. + * complete.
  15083. + */
  15084. +static int cpu_unplug_begin(unsigned int cpu)
  15085. +{
  15086. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  15087. + int err;
  15088. +
  15089. + /* Protected by cpu_hotplug.lock */
  15090. + if (!hp->mutex_init) {
  15091. +#ifdef CONFIG_PREEMPT_RT_FULL
  15092. + spin_lock_init(&hp->lock);
  15093. +#else
  15094. + mutex_init(&hp->mutex);
  15095. +#endif
  15096. + hp->mutex_init = 1;
  15097. + }
  15098. +
  15099. + /* Inform the scheduler to migrate tasks off this CPU */
  15100. + tell_sched_cpu_down_begin(cpu);
  15101. +
  15102. + init_completion(&hp->synced);
  15103. + init_completion(&hp->unplug_wait);
  15104. +
  15105. + hp->sync_tsk = kthread_create(sync_unplug_thread, hp, "sync_unplug/%d", cpu);
  15106. + if (IS_ERR(hp->sync_tsk)) {
  15107. + err = PTR_ERR(hp->sync_tsk);
  15108. + hp->sync_tsk = NULL;
  15109. + return err;
  15110. + }
  15111. + kthread_bind(hp->sync_tsk, cpu);
  15112. +
  15113. + /*
  15114. + * Wait for tasks to get out of the pinned sections,
  15115. + * it's still OK if new tasks enter. Some CPU notifiers will
  15116. + * wait for tasks that are going to enter these sections and
  15117. + * we must not have them block.
  15118. + */
  15119. + wake_up_process(hp->sync_tsk);
  15120. + return 0;
  15121. +}
  15122. +
  15123. +static void cpu_unplug_sync(unsigned int cpu)
  15124. +{
  15125. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  15126. +
  15127. + init_completion(&hp->synced);
  15128. + /* The completion needs to be initialzied before setting grab_lock */
  15129. + smp_wmb();
  15130. +
  15131. + /* Grab the mutex before setting grab_lock */
  15132. + hotplug_lock(hp);
  15133. + hp->grab_lock = 1;
  15134. +
  15135. + /*
  15136. + * The CPU notifiers have been completed.
  15137. + * Wait for tasks to get out of pinned CPU sections and have new
  15138. + * tasks block until the CPU is completely down.
  15139. + */
  15140. + __cpu_unplug_sync(hp);
  15141. +
  15142. + /* All done with the sync thread */
  15143. + kthread_stop(hp->sync_tsk);
  15144. + hp->sync_tsk = NULL;
  15145. +}
  15146. +
  15147. +static void cpu_unplug_done(unsigned int cpu)
  15148. +{
  15149. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  15150. +
  15151. + hp->unplug = NULL;
  15152. + /* Let all tasks know cpu unplug is finished before cleaning up */
  15153. + smp_wmb();
  15154. +
  15155. + if (hp->sync_tsk)
  15156. + kthread_stop(hp->sync_tsk);
  15157. +
  15158. + if (hp->grab_lock) {
  15159. + hotplug_unlock(hp);
  15160. + /* protected by cpu_hotplug.lock */
  15161. + hp->grab_lock = 0;
  15162. + }
  15163. + tell_sched_cpu_down_done(cpu);
  15164. +}
  15165. void get_online_cpus(void)
  15166. {
  15167. @@ -338,13 +621,15 @@
  15168. /* Requires cpu_add_remove_lock to be held */
  15169. static int _cpu_down(unsigned int cpu, int tasks_frozen)
  15170. {
  15171. - int err, nr_calls = 0;
  15172. + int mycpu, err, nr_calls = 0;
  15173. void *hcpu = (void *)(long)cpu;
  15174. unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
  15175. struct take_cpu_down_param tcd_param = {
  15176. .mod = mod,
  15177. .hcpu = hcpu,
  15178. };
  15179. + cpumask_var_t cpumask;
  15180. + cpumask_var_t cpumask_org;
  15181. if (num_online_cpus() == 1)
  15182. return -EBUSY;
  15183. @@ -352,7 +637,34 @@
  15184. if (!cpu_online(cpu))
  15185. return -EINVAL;
  15186. + /* Move the downtaker off the unplug cpu */
  15187. + if (!alloc_cpumask_var(&cpumask, GFP_KERNEL))
  15188. + return -ENOMEM;
  15189. + if (!alloc_cpumask_var(&cpumask_org, GFP_KERNEL)) {
  15190. + free_cpumask_var(cpumask);
  15191. + return -ENOMEM;
  15192. + }
  15193. +
  15194. + cpumask_copy(cpumask_org, tsk_cpus_allowed(current));
  15195. + cpumask_andnot(cpumask, cpu_online_mask, cpumask_of(cpu));
  15196. + set_cpus_allowed_ptr(current, cpumask);
  15197. + free_cpumask_var(cpumask);
  15198. + migrate_disable();
  15199. + mycpu = smp_processor_id();
  15200. + if (mycpu == cpu) {
  15201. + printk(KERN_ERR "Yuck! Still on unplug CPU\n!");
  15202. + migrate_enable();
  15203. + err = -EBUSY;
  15204. + goto restore_cpus;
  15205. + }
  15206. + migrate_enable();
  15207. +
  15208. cpu_hotplug_begin();
  15209. + err = cpu_unplug_begin(cpu);
  15210. + if (err) {
  15211. + printk("cpu_unplug_begin(%d) failed\n", cpu);
  15212. + goto out_cancel;
  15213. + }
  15214. err = __cpu_notify(CPU_DOWN_PREPARE | mod, hcpu, -1, &nr_calls);
  15215. if (err) {
  15216. @@ -378,8 +690,12 @@
  15217. else
  15218. synchronize_rcu();
  15219. + __cpu_unplug_wait(cpu);
  15220. smpboot_park_threads(cpu);
  15221. + /* Notifiers are done. Don't let any more tasks pin this CPU. */
  15222. + cpu_unplug_sync(cpu);
  15223. +
  15224. /*
  15225. * Prevent irq alloc/free while the dying cpu reorganizes the
  15226. * interrupt affinities.
  15227. @@ -424,9 +740,14 @@
  15228. check_for_tasks(cpu);
  15229. out_release:
  15230. + cpu_unplug_done(cpu);
  15231. +out_cancel:
  15232. cpu_hotplug_done();
  15233. if (!err)
  15234. cpu_notify_nofail(CPU_POST_DEAD | mod, hcpu);
  15235. +restore_cpus:
  15236. + set_cpus_allowed_ptr(current, cpumask_org);
  15237. + free_cpumask_var(cpumask_org);
  15238. return err;
  15239. }
  15240. diff -Nur linux-4.4.46.orig/kernel/debug/kdb/kdb_io.c linux-4.4.46/kernel/debug/kdb/kdb_io.c
  15241. --- linux-4.4.46.orig/kernel/debug/kdb/kdb_io.c 2017-02-01 08:31:11.000000000 +0100
  15242. +++ linux-4.4.46/kernel/debug/kdb/kdb_io.c 2017-02-03 17:18:10.919618749 +0100
  15243. @@ -554,7 +554,6 @@
  15244. int linecount;
  15245. int colcount;
  15246. int logging, saved_loglevel = 0;
  15247. - int saved_trap_printk;
  15248. int got_printf_lock = 0;
  15249. int retlen = 0;
  15250. int fnd, len;
  15251. @@ -565,8 +564,6 @@
  15252. unsigned long uninitialized_var(flags);
  15253. preempt_disable();
  15254. - saved_trap_printk = kdb_trap_printk;
  15255. - kdb_trap_printk = 0;
  15256. /* Serialize kdb_printf if multiple cpus try to write at once.
  15257. * But if any cpu goes recursive in kdb, just print the output,
  15258. @@ -855,7 +852,6 @@
  15259. } else {
  15260. __release(kdb_printf_lock);
  15261. }
  15262. - kdb_trap_printk = saved_trap_printk;
  15263. preempt_enable();
  15264. return retlen;
  15265. }
  15266. @@ -865,9 +861,11 @@
  15267. va_list ap;
  15268. int r;
  15269. + kdb_trap_printk++;
  15270. va_start(ap, fmt);
  15271. r = vkdb_printf(KDB_MSGSRC_INTERNAL, fmt, ap);
  15272. va_end(ap);
  15273. + kdb_trap_printk--;
  15274. return r;
  15275. }
  15276. diff -Nur linux-4.4.46.orig/kernel/events/core.c linux-4.4.46/kernel/events/core.c
  15277. --- linux-4.4.46.orig/kernel/events/core.c 2017-02-01 08:31:11.000000000 +0100
  15278. +++ linux-4.4.46/kernel/events/core.c 2017-02-03 17:18:10.919618749 +0100
  15279. @@ -802,6 +802,7 @@
  15280. raw_spin_lock_init(&cpuctx->hrtimer_lock);
  15281. hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
  15282. timer->function = perf_mux_hrtimer_handler;
  15283. + timer->irqsafe = 1;
  15284. }
  15285. static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx)
  15286. @@ -7240,6 +7241,7 @@
  15287. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  15288. hwc->hrtimer.function = perf_swevent_hrtimer;
  15289. + hwc->hrtimer.irqsafe = 1;
  15290. /*
  15291. * Since hrtimers have a fixed rate, we can do a static freq->period
  15292. diff -Nur linux-4.4.46.orig/kernel/exit.c linux-4.4.46/kernel/exit.c
  15293. --- linux-4.4.46.orig/kernel/exit.c 2017-02-01 08:31:11.000000000 +0100
  15294. +++ linux-4.4.46/kernel/exit.c 2017-02-03 17:18:10.923618903 +0100
  15295. @@ -144,7 +144,7 @@
  15296. * Do this under ->siglock, we can race with another thread
  15297. * doing sigqueue_free() if we have SIGQUEUE_PREALLOC signals.
  15298. */
  15299. - flush_sigqueue(&tsk->pending);
  15300. + flush_task_sigqueue(tsk);
  15301. tsk->sighand = NULL;
  15302. spin_unlock(&sighand->siglock);
  15303. diff -Nur linux-4.4.46.orig/kernel/fork.c linux-4.4.46/kernel/fork.c
  15304. --- linux-4.4.46.orig/kernel/fork.c 2017-02-01 08:31:11.000000000 +0100
  15305. +++ linux-4.4.46/kernel/fork.c 2017-02-03 17:18:10.923618903 +0100
  15306. @@ -108,7 +108,7 @@
  15307. DEFINE_PER_CPU(unsigned long, process_counts) = 0;
  15308. -__cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */
  15309. +DEFINE_RWLOCK(tasklist_lock); /* outer */
  15310. #ifdef CONFIG_PROVE_RCU
  15311. int lockdep_tasklist_lock_is_held(void)
  15312. @@ -244,7 +244,9 @@
  15313. if (atomic_dec_and_test(&sig->sigcnt))
  15314. free_signal_struct(sig);
  15315. }
  15316. -
  15317. +#ifdef CONFIG_PREEMPT_RT_BASE
  15318. +static
  15319. +#endif
  15320. void __put_task_struct(struct task_struct *tsk)
  15321. {
  15322. WARN_ON(!tsk->exit_state);
  15323. @@ -261,7 +263,18 @@
  15324. if (!profile_handoff_task(tsk))
  15325. free_task(tsk);
  15326. }
  15327. +#ifndef CONFIG_PREEMPT_RT_BASE
  15328. EXPORT_SYMBOL_GPL(__put_task_struct);
  15329. +#else
  15330. +void __put_task_struct_cb(struct rcu_head *rhp)
  15331. +{
  15332. + struct task_struct *tsk = container_of(rhp, struct task_struct, put_rcu);
  15333. +
  15334. + __put_task_struct(tsk);
  15335. +
  15336. +}
  15337. +EXPORT_SYMBOL_GPL(__put_task_struct_cb);
  15338. +#endif
  15339. void __init __weak arch_task_cache_init(void) { }
  15340. @@ -692,6 +705,19 @@
  15341. }
  15342. EXPORT_SYMBOL_GPL(__mmdrop);
  15343. +#ifdef CONFIG_PREEMPT_RT_BASE
  15344. +/*
  15345. + * RCU callback for delayed mm drop. Not strictly rcu, but we don't
  15346. + * want another facility to make this work.
  15347. + */
  15348. +void __mmdrop_delayed(struct rcu_head *rhp)
  15349. +{
  15350. + struct mm_struct *mm = container_of(rhp, struct mm_struct, delayed_drop);
  15351. +
  15352. + __mmdrop(mm);
  15353. +}
  15354. +#endif
  15355. +
  15356. /*
  15357. * Decrement the use count and release all resources for an mm.
  15358. */
  15359. @@ -1242,6 +1268,9 @@
  15360. */
  15361. static void posix_cpu_timers_init(struct task_struct *tsk)
  15362. {
  15363. +#ifdef CONFIG_PREEMPT_RT_BASE
  15364. + tsk->posix_timer_list = NULL;
  15365. +#endif
  15366. tsk->cputime_expires.prof_exp = 0;
  15367. tsk->cputime_expires.virt_exp = 0;
  15368. tsk->cputime_expires.sched_exp = 0;
  15369. @@ -1367,15 +1396,16 @@
  15370. spin_lock_init(&p->alloc_lock);
  15371. init_sigpending(&p->pending);
  15372. + p->sigqueue_cache = NULL;
  15373. p->utime = p->stime = p->gtime = 0;
  15374. p->utimescaled = p->stimescaled = 0;
  15375. prev_cputime_init(&p->prev_cputime);
  15376. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  15377. - seqlock_init(&p->vtime_seqlock);
  15378. + seqcount_init(&p->vtime_seqcount);
  15379. p->vtime_snap = 0;
  15380. - p->vtime_snap_whence = VTIME_SLEEPING;
  15381. + p->vtime_snap_whence = VTIME_INACTIVE;
  15382. #endif
  15383. #if defined(SPLIT_RSS_COUNTING)
  15384. diff -Nur linux-4.4.46.orig/kernel/futex.c linux-4.4.46/kernel/futex.c
  15385. --- linux-4.4.46.orig/kernel/futex.c 2017-02-01 08:31:11.000000000 +0100
  15386. +++ linux-4.4.46/kernel/futex.c 2017-02-03 17:18:10.923618903 +0100
  15387. @@ -815,7 +815,9 @@
  15388. * task still owns the PI-state:
  15389. */
  15390. if (head->next != next) {
  15391. + raw_spin_unlock_irq(&curr->pi_lock);
  15392. spin_unlock(&hb->lock);
  15393. + raw_spin_lock_irq(&curr->pi_lock);
  15394. continue;
  15395. }
  15396. @@ -1210,6 +1212,7 @@
  15397. struct futex_pi_state *pi_state = this->pi_state;
  15398. u32 uninitialized_var(curval), newval;
  15399. WAKE_Q(wake_q);
  15400. + WAKE_Q(wake_sleeper_q);
  15401. bool deboost;
  15402. int ret = 0;
  15403. @@ -1223,7 +1226,7 @@
  15404. if (pi_state->owner != current)
  15405. return -EINVAL;
  15406. - raw_spin_lock(&pi_state->pi_mutex.wait_lock);
  15407. + raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
  15408. new_owner = rt_mutex_next_owner(&pi_state->pi_mutex);
  15409. /*
  15410. @@ -1259,24 +1262,25 @@
  15411. ret = -EINVAL;
  15412. }
  15413. if (ret) {
  15414. - raw_spin_unlock(&pi_state->pi_mutex.wait_lock);
  15415. + raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
  15416. return ret;
  15417. }
  15418. - raw_spin_lock_irq(&pi_state->owner->pi_lock);
  15419. + raw_spin_lock(&pi_state->owner->pi_lock);
  15420. WARN_ON(list_empty(&pi_state->list));
  15421. list_del_init(&pi_state->list);
  15422. - raw_spin_unlock_irq(&pi_state->owner->pi_lock);
  15423. + raw_spin_unlock(&pi_state->owner->pi_lock);
  15424. - raw_spin_lock_irq(&new_owner->pi_lock);
  15425. + raw_spin_lock(&new_owner->pi_lock);
  15426. WARN_ON(!list_empty(&pi_state->list));
  15427. list_add(&pi_state->list, &new_owner->pi_state_list);
  15428. pi_state->owner = new_owner;
  15429. - raw_spin_unlock_irq(&new_owner->pi_lock);
  15430. + raw_spin_unlock(&new_owner->pi_lock);
  15431. - raw_spin_unlock(&pi_state->pi_mutex.wait_lock);
  15432. + raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
  15433. - deboost = rt_mutex_futex_unlock(&pi_state->pi_mutex, &wake_q);
  15434. + deboost = rt_mutex_futex_unlock(&pi_state->pi_mutex, &wake_q,
  15435. + &wake_sleeper_q);
  15436. /*
  15437. * First unlock HB so the waiter does not spin on it once he got woken
  15438. @@ -1284,8 +1288,9 @@
  15439. * deboost first (and lose our higher priority), then the task might get
  15440. * scheduled away before the wake up can take place.
  15441. */
  15442. - spin_unlock(&hb->lock);
  15443. + deboost |= spin_unlock_no_deboost(&hb->lock);
  15444. wake_up_q(&wake_q);
  15445. + wake_up_q_sleeper(&wake_sleeper_q);
  15446. if (deboost)
  15447. rt_mutex_adjust_prio(current);
  15448. @@ -1822,6 +1827,16 @@
  15449. requeue_pi_wake_futex(this, &key2, hb2);
  15450. drop_count++;
  15451. continue;
  15452. + } else if (ret == -EAGAIN) {
  15453. + /*
  15454. + * Waiter was woken by timeout or
  15455. + * signal and has set pi_blocked_on to
  15456. + * PI_WAKEUP_INPROGRESS before we
  15457. + * tried to enqueue it on the rtmutex.
  15458. + */
  15459. + this->pi_state = NULL;
  15460. + free_pi_state(pi_state);
  15461. + continue;
  15462. } else if (ret) {
  15463. /* -EDEADLK */
  15464. this->pi_state = NULL;
  15465. @@ -2139,11 +2154,11 @@
  15466. * we returned due to timeout or signal without taking the
  15467. * rt_mutex. Too late.
  15468. */
  15469. - raw_spin_lock(&q->pi_state->pi_mutex.wait_lock);
  15470. + raw_spin_lock_irq(&q->pi_state->pi_mutex.wait_lock);
  15471. owner = rt_mutex_owner(&q->pi_state->pi_mutex);
  15472. if (!owner)
  15473. owner = rt_mutex_next_owner(&q->pi_state->pi_mutex);
  15474. - raw_spin_unlock(&q->pi_state->pi_mutex.wait_lock);
  15475. + raw_spin_unlock_irq(&q->pi_state->pi_mutex.wait_lock);
  15476. ret = fixup_pi_state_owner(uaddr, q, owner);
  15477. goto out;
  15478. }
  15479. @@ -2691,7 +2706,7 @@
  15480. struct hrtimer_sleeper timeout, *to = NULL;
  15481. struct rt_mutex_waiter rt_waiter;
  15482. struct rt_mutex *pi_mutex = NULL;
  15483. - struct futex_hash_bucket *hb;
  15484. + struct futex_hash_bucket *hb, *hb2;
  15485. union futex_key key2 = FUTEX_KEY_INIT;
  15486. struct futex_q q = futex_q_init;
  15487. int res, ret;
  15488. @@ -2716,10 +2731,7 @@
  15489. * The waiter is allocated on our stack, manipulated by the requeue
  15490. * code while we sleep on uaddr.
  15491. */
  15492. - debug_rt_mutex_init_waiter(&rt_waiter);
  15493. - RB_CLEAR_NODE(&rt_waiter.pi_tree_entry);
  15494. - RB_CLEAR_NODE(&rt_waiter.tree_entry);
  15495. - rt_waiter.task = NULL;
  15496. + rt_mutex_init_waiter(&rt_waiter, false);
  15497. ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE);
  15498. if (unlikely(ret != 0))
  15499. @@ -2750,20 +2762,55 @@
  15500. /* Queue the futex_q, drop the hb lock, wait for wakeup. */
  15501. futex_wait_queue_me(hb, &q, to);
  15502. - spin_lock(&hb->lock);
  15503. - ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
  15504. - spin_unlock(&hb->lock);
  15505. - if (ret)
  15506. - goto out_put_keys;
  15507. + /*
  15508. + * On RT we must avoid races with requeue and trying to block
  15509. + * on two mutexes (hb->lock and uaddr2's rtmutex) by
  15510. + * serializing access to pi_blocked_on with pi_lock.
  15511. + */
  15512. + raw_spin_lock_irq(&current->pi_lock);
  15513. + if (current->pi_blocked_on) {
  15514. + /*
  15515. + * We have been requeued or are in the process of
  15516. + * being requeued.
  15517. + */
  15518. + raw_spin_unlock_irq(&current->pi_lock);
  15519. + } else {
  15520. + /*
  15521. + * Setting pi_blocked_on to PI_WAKEUP_INPROGRESS
  15522. + * prevents a concurrent requeue from moving us to the
  15523. + * uaddr2 rtmutex. After that we can safely acquire
  15524. + * (and possibly block on) hb->lock.
  15525. + */
  15526. + current->pi_blocked_on = PI_WAKEUP_INPROGRESS;
  15527. + raw_spin_unlock_irq(&current->pi_lock);
  15528. +
  15529. + spin_lock(&hb->lock);
  15530. +
  15531. + /*
  15532. + * Clean up pi_blocked_on. We might leak it otherwise
  15533. + * when we succeeded with the hb->lock in the fast
  15534. + * path.
  15535. + */
  15536. + raw_spin_lock_irq(&current->pi_lock);
  15537. + current->pi_blocked_on = NULL;
  15538. + raw_spin_unlock_irq(&current->pi_lock);
  15539. +
  15540. + ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
  15541. + spin_unlock(&hb->lock);
  15542. + if (ret)
  15543. + goto out_put_keys;
  15544. + }
  15545. /*
  15546. - * In order for us to be here, we know our q.key == key2, and since
  15547. - * we took the hb->lock above, we also know that futex_requeue() has
  15548. - * completed and we no longer have to concern ourselves with a wakeup
  15549. - * race with the atomic proxy lock acquisition by the requeue code. The
  15550. - * futex_requeue dropped our key1 reference and incremented our key2
  15551. - * reference count.
  15552. + * In order to be here, we have either been requeued, are in
  15553. + * the process of being requeued, or requeue successfully
  15554. + * acquired uaddr2 on our behalf. If pi_blocked_on was
  15555. + * non-null above, we may be racing with a requeue. Do not
  15556. + * rely on q->lock_ptr to be hb2->lock until after blocking on
  15557. + * hb->lock or hb2->lock. The futex_requeue dropped our key1
  15558. + * reference and incremented our key2 reference count.
  15559. */
  15560. + hb2 = hash_futex(&key2);
  15561. /* Check if the requeue code acquired the second futex for us. */
  15562. if (!q.rt_waiter) {
  15563. @@ -2772,14 +2819,15 @@
  15564. * did a lock-steal - fix up the PI-state in that case.
  15565. */
  15566. if (q.pi_state && (q.pi_state->owner != current)) {
  15567. - spin_lock(q.lock_ptr);
  15568. + spin_lock(&hb2->lock);
  15569. + BUG_ON(&hb2->lock != q.lock_ptr);
  15570. ret = fixup_pi_state_owner(uaddr2, &q, current);
  15571. /*
  15572. * Drop the reference to the pi state which
  15573. * the requeue_pi() code acquired for us.
  15574. */
  15575. free_pi_state(q.pi_state);
  15576. - spin_unlock(q.lock_ptr);
  15577. + spin_unlock(&hb2->lock);
  15578. }
  15579. } else {
  15580. /*
  15581. @@ -2792,7 +2840,8 @@
  15582. ret = rt_mutex_finish_proxy_lock(pi_mutex, to, &rt_waiter);
  15583. debug_rt_mutex_free_waiter(&rt_waiter);
  15584. - spin_lock(q.lock_ptr);
  15585. + spin_lock(&hb2->lock);
  15586. + BUG_ON(&hb2->lock != q.lock_ptr);
  15587. /*
  15588. * Fixup the pi_state owner and possibly acquire the lock if we
  15589. * haven't already.
  15590. diff -Nur linux-4.4.46.orig/kernel/irq/handle.c linux-4.4.46/kernel/irq/handle.c
  15591. --- linux-4.4.46.orig/kernel/irq/handle.c 2017-02-01 08:31:11.000000000 +0100
  15592. +++ linux-4.4.46/kernel/irq/handle.c 2017-02-03 17:18:10.923618903 +0100
  15593. @@ -134,6 +134,8 @@
  15594. irqreturn_t handle_irq_event_percpu(struct irq_desc *desc)
  15595. {
  15596. + struct pt_regs *regs = get_irq_regs();
  15597. + u64 ip = regs ? instruction_pointer(regs) : 0;
  15598. irqreturn_t retval = IRQ_NONE;
  15599. unsigned int flags = 0, irq = desc->irq_data.irq;
  15600. struct irqaction *action = desc->action;
  15601. @@ -176,7 +178,11 @@
  15602. action = action->next;
  15603. }
  15604. - add_interrupt_randomness(irq, flags);
  15605. +#ifdef CONFIG_PREEMPT_RT_FULL
  15606. + desc->random_ip = ip;
  15607. +#else
  15608. + add_interrupt_randomness(irq, flags, ip);
  15609. +#endif
  15610. if (!noirqdebug)
  15611. note_interrupt(desc, retval);
  15612. diff -Nur linux-4.4.46.orig/kernel/irq/irqdesc.c linux-4.4.46/kernel/irq/irqdesc.c
  15613. --- linux-4.4.46.orig/kernel/irq/irqdesc.c 2017-02-01 08:31:11.000000000 +0100
  15614. +++ linux-4.4.46/kernel/irq/irqdesc.c 2017-02-03 17:18:10.923618903 +0100
  15615. @@ -24,10 +24,27 @@
  15616. static struct lock_class_key irq_desc_lock_class;
  15617. #if defined(CONFIG_SMP)
  15618. +static int __init irq_affinity_setup(char *str)
  15619. +{
  15620. + zalloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
  15621. + cpulist_parse(str, irq_default_affinity);
  15622. + /*
  15623. + * Set at least the boot cpu. We don't want to end up with
  15624. + * bugreports caused by random comandline masks
  15625. + */
  15626. + cpumask_set_cpu(smp_processor_id(), irq_default_affinity);
  15627. + return 1;
  15628. +}
  15629. +__setup("irqaffinity=", irq_affinity_setup);
  15630. +
  15631. static void __init init_irq_default_affinity(void)
  15632. {
  15633. - alloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
  15634. - cpumask_setall(irq_default_affinity);
  15635. +#ifdef CONFIG_CPUMASK_OFFSTACK
  15636. + if (!irq_default_affinity)
  15637. + zalloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
  15638. +#endif
  15639. + if (cpumask_empty(irq_default_affinity))
  15640. + cpumask_setall(irq_default_affinity);
  15641. }
  15642. #else
  15643. static void __init init_irq_default_affinity(void)
  15644. diff -Nur linux-4.4.46.orig/kernel/irq/manage.c linux-4.4.46/kernel/irq/manage.c
  15645. --- linux-4.4.46.orig/kernel/irq/manage.c 2017-02-01 08:31:11.000000000 +0100
  15646. +++ linux-4.4.46/kernel/irq/manage.c 2017-02-03 17:18:10.923618903 +0100
  15647. @@ -22,6 +22,7 @@
  15648. #include "internals.h"
  15649. #ifdef CONFIG_IRQ_FORCED_THREADING
  15650. +# ifndef CONFIG_PREEMPT_RT_BASE
  15651. __read_mostly bool force_irqthreads;
  15652. static int __init setup_forced_irqthreads(char *arg)
  15653. @@ -30,6 +31,7 @@
  15654. return 0;
  15655. }
  15656. early_param("threadirqs", setup_forced_irqthreads);
  15657. +# endif
  15658. #endif
  15659. static void __synchronize_hardirq(struct irq_desc *desc)
  15660. @@ -181,6 +183,62 @@
  15661. irq_get_pending(struct cpumask *mask, struct irq_desc *desc) { }
  15662. #endif
  15663. +#ifdef CONFIG_PREEMPT_RT_FULL
  15664. +static void _irq_affinity_notify(struct irq_affinity_notify *notify);
  15665. +static struct task_struct *set_affinity_helper;
  15666. +static LIST_HEAD(affinity_list);
  15667. +static DEFINE_RAW_SPINLOCK(affinity_list_lock);
  15668. +
  15669. +static int set_affinity_thread(void *unused)
  15670. +{
  15671. + while (1) {
  15672. + struct irq_affinity_notify *notify;
  15673. + int empty;
  15674. +
  15675. + set_current_state(TASK_INTERRUPTIBLE);
  15676. +
  15677. + raw_spin_lock_irq(&affinity_list_lock);
  15678. + empty = list_empty(&affinity_list);
  15679. + raw_spin_unlock_irq(&affinity_list_lock);
  15680. +
  15681. + if (empty)
  15682. + schedule();
  15683. + if (kthread_should_stop())
  15684. + break;
  15685. + set_current_state(TASK_RUNNING);
  15686. +try_next:
  15687. + notify = NULL;
  15688. +
  15689. + raw_spin_lock_irq(&affinity_list_lock);
  15690. + if (!list_empty(&affinity_list)) {
  15691. + notify = list_first_entry(&affinity_list,
  15692. + struct irq_affinity_notify, list);
  15693. + list_del_init(&notify->list);
  15694. + }
  15695. + raw_spin_unlock_irq(&affinity_list_lock);
  15696. +
  15697. + if (!notify)
  15698. + continue;
  15699. + _irq_affinity_notify(notify);
  15700. + goto try_next;
  15701. + }
  15702. + return 0;
  15703. +}
  15704. +
  15705. +static void init_helper_thread(void)
  15706. +{
  15707. + if (set_affinity_helper)
  15708. + return;
  15709. + set_affinity_helper = kthread_run(set_affinity_thread, NULL,
  15710. + "affinity-cb");
  15711. + WARN_ON(IS_ERR(set_affinity_helper));
  15712. +}
  15713. +#else
  15714. +
  15715. +static inline void init_helper_thread(void) { }
  15716. +
  15717. +#endif
  15718. +
  15719. int irq_do_set_affinity(struct irq_data *data, const struct cpumask *mask,
  15720. bool force)
  15721. {
  15722. @@ -220,7 +278,17 @@
  15723. if (desc->affinity_notify) {
  15724. kref_get(&desc->affinity_notify->kref);
  15725. +
  15726. +#ifdef CONFIG_PREEMPT_RT_FULL
  15727. + raw_spin_lock(&affinity_list_lock);
  15728. + if (list_empty(&desc->affinity_notify->list))
  15729. + list_add_tail(&affinity_list,
  15730. + &desc->affinity_notify->list);
  15731. + raw_spin_unlock(&affinity_list_lock);
  15732. + wake_up_process(set_affinity_helper);
  15733. +#else
  15734. schedule_work(&desc->affinity_notify->work);
  15735. +#endif
  15736. }
  15737. irqd_set(data, IRQD_AFFINITY_SET);
  15738. @@ -258,10 +326,8 @@
  15739. }
  15740. EXPORT_SYMBOL_GPL(irq_set_affinity_hint);
  15741. -static void irq_affinity_notify(struct work_struct *work)
  15742. +static void _irq_affinity_notify(struct irq_affinity_notify *notify)
  15743. {
  15744. - struct irq_affinity_notify *notify =
  15745. - container_of(work, struct irq_affinity_notify, work);
  15746. struct irq_desc *desc = irq_to_desc(notify->irq);
  15747. cpumask_var_t cpumask;
  15748. unsigned long flags;
  15749. @@ -283,6 +349,13 @@
  15750. kref_put(&notify->kref, notify->release);
  15751. }
  15752. +static void irq_affinity_notify(struct work_struct *work)
  15753. +{
  15754. + struct irq_affinity_notify *notify =
  15755. + container_of(work, struct irq_affinity_notify, work);
  15756. + _irq_affinity_notify(notify);
  15757. +}
  15758. +
  15759. /**
  15760. * irq_set_affinity_notifier - control notification of IRQ affinity changes
  15761. * @irq: Interrupt for which to enable/disable notification
  15762. @@ -312,6 +385,8 @@
  15763. notify->irq = irq;
  15764. kref_init(&notify->kref);
  15765. INIT_WORK(&notify->work, irq_affinity_notify);
  15766. + INIT_LIST_HEAD(&notify->list);
  15767. + init_helper_thread();
  15768. }
  15769. raw_spin_lock_irqsave(&desc->lock, flags);
  15770. @@ -865,7 +940,15 @@
  15771. local_bh_disable();
  15772. ret = action->thread_fn(action->irq, action->dev_id);
  15773. irq_finalize_oneshot(desc, action);
  15774. - local_bh_enable();
  15775. + /*
  15776. + * Interrupts which have real time requirements can be set up
  15777. + * to avoid softirq processing in the thread handler. This is
  15778. + * safe as these interrupts do not raise soft interrupts.
  15779. + */
  15780. + if (irq_settings_no_softirq_call(desc))
  15781. + _local_bh_enable();
  15782. + else
  15783. + local_bh_enable();
  15784. return ret;
  15785. }
  15786. @@ -962,6 +1045,12 @@
  15787. if (action_ret == IRQ_WAKE_THREAD)
  15788. irq_wake_secondary(desc, action);
  15789. +#ifdef CONFIG_PREEMPT_RT_FULL
  15790. + migrate_disable();
  15791. + add_interrupt_randomness(action->irq, 0,
  15792. + desc->random_ip ^ (unsigned long) action);
  15793. + migrate_enable();
  15794. +#endif
  15795. wake_threads_waitq(desc);
  15796. }
  15797. @@ -1315,6 +1404,9 @@
  15798. irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
  15799. }
  15800. + if (new->flags & IRQF_NO_SOFTIRQ_CALL)
  15801. + irq_settings_set_no_softirq_call(desc);
  15802. +
  15803. /* Set default affinity mask once everything is setup */
  15804. setup_affinity(desc, mask);
  15805. @@ -1968,7 +2060,7 @@
  15806. * This call sets the internal irqchip state of an interrupt,
  15807. * depending on the value of @which.
  15808. *
  15809. - * This function should be called with preemption disabled if the
  15810. + * This function should be called with migration disabled if the
  15811. * interrupt controller has per-cpu registers.
  15812. */
  15813. int irq_set_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
  15814. diff -Nur linux-4.4.46.orig/kernel/irq/settings.h linux-4.4.46/kernel/irq/settings.h
  15815. --- linux-4.4.46.orig/kernel/irq/settings.h 2017-02-01 08:31:11.000000000 +0100
  15816. +++ linux-4.4.46/kernel/irq/settings.h 2017-02-03 17:18:10.923618903 +0100
  15817. @@ -16,6 +16,7 @@
  15818. _IRQ_PER_CPU_DEVID = IRQ_PER_CPU_DEVID,
  15819. _IRQ_IS_POLLED = IRQ_IS_POLLED,
  15820. _IRQ_DISABLE_UNLAZY = IRQ_DISABLE_UNLAZY,
  15821. + _IRQ_NO_SOFTIRQ_CALL = IRQ_NO_SOFTIRQ_CALL,
  15822. _IRQF_MODIFY_MASK = IRQF_MODIFY_MASK,
  15823. };
  15824. @@ -30,6 +31,7 @@
  15825. #define IRQ_PER_CPU_DEVID GOT_YOU_MORON
  15826. #define IRQ_IS_POLLED GOT_YOU_MORON
  15827. #define IRQ_DISABLE_UNLAZY GOT_YOU_MORON
  15828. +#define IRQ_NO_SOFTIRQ_CALL GOT_YOU_MORON
  15829. #undef IRQF_MODIFY_MASK
  15830. #define IRQF_MODIFY_MASK GOT_YOU_MORON
  15831. @@ -40,6 +42,16 @@
  15832. desc->status_use_accessors |= (set & _IRQF_MODIFY_MASK);
  15833. }
  15834. +static inline bool irq_settings_no_softirq_call(struct irq_desc *desc)
  15835. +{
  15836. + return desc->status_use_accessors & _IRQ_NO_SOFTIRQ_CALL;
  15837. +}
  15838. +
  15839. +static inline void irq_settings_set_no_softirq_call(struct irq_desc *desc)
  15840. +{
  15841. + desc->status_use_accessors |= _IRQ_NO_SOFTIRQ_CALL;
  15842. +}
  15843. +
  15844. static inline bool irq_settings_is_per_cpu(struct irq_desc *desc)
  15845. {
  15846. return desc->status_use_accessors & _IRQ_PER_CPU;
  15847. diff -Nur linux-4.4.46.orig/kernel/irq/spurious.c linux-4.4.46/kernel/irq/spurious.c
  15848. --- linux-4.4.46.orig/kernel/irq/spurious.c 2017-02-01 08:31:11.000000000 +0100
  15849. +++ linux-4.4.46/kernel/irq/spurious.c 2017-02-03 17:18:10.923618903 +0100
  15850. @@ -444,6 +444,10 @@
  15851. static int __init irqfixup_setup(char *str)
  15852. {
  15853. +#ifdef CONFIG_PREEMPT_RT_BASE
  15854. + pr_warn("irqfixup boot option not supported w/ CONFIG_PREEMPT_RT_BASE\n");
  15855. + return 1;
  15856. +#endif
  15857. irqfixup = 1;
  15858. printk(KERN_WARNING "Misrouted IRQ fixup support enabled.\n");
  15859. printk(KERN_WARNING "This may impact system performance.\n");
  15860. @@ -456,6 +460,10 @@
  15861. static int __init irqpoll_setup(char *str)
  15862. {
  15863. +#ifdef CONFIG_PREEMPT_RT_BASE
  15864. + pr_warn("irqpoll boot option not supported w/ CONFIG_PREEMPT_RT_BASE\n");
  15865. + return 1;
  15866. +#endif
  15867. irqfixup = 2;
  15868. printk(KERN_WARNING "Misrouted IRQ fixup and polling support "
  15869. "enabled\n");
  15870. diff -Nur linux-4.4.46.orig/kernel/irq_work.c linux-4.4.46/kernel/irq_work.c
  15871. --- linux-4.4.46.orig/kernel/irq_work.c 2017-02-01 08:31:11.000000000 +0100
  15872. +++ linux-4.4.46/kernel/irq_work.c 2017-02-03 17:18:10.923618903 +0100
  15873. @@ -17,6 +17,7 @@
  15874. #include <linux/cpu.h>
  15875. #include <linux/notifier.h>
  15876. #include <linux/smp.h>
  15877. +#include <linux/interrupt.h>
  15878. #include <asm/processor.h>
  15879. @@ -65,6 +66,8 @@
  15880. */
  15881. bool irq_work_queue_on(struct irq_work *work, int cpu)
  15882. {
  15883. + struct llist_head *list;
  15884. +
  15885. /* All work should have been flushed before going offline */
  15886. WARN_ON_ONCE(cpu_is_offline(cpu));
  15887. @@ -75,7 +78,12 @@
  15888. if (!irq_work_claim(work))
  15889. return false;
  15890. - if (llist_add(&work->llnode, &per_cpu(raised_list, cpu)))
  15891. + if (IS_ENABLED(CONFIG_PREEMPT_RT_FULL) && !(work->flags & IRQ_WORK_HARD_IRQ))
  15892. + list = &per_cpu(lazy_list, cpu);
  15893. + else
  15894. + list = &per_cpu(raised_list, cpu);
  15895. +
  15896. + if (llist_add(&work->llnode, list))
  15897. arch_send_call_function_single_ipi(cpu);
  15898. return true;
  15899. @@ -86,6 +94,9 @@
  15900. /* Enqueue the irq work @work on the current CPU */
  15901. bool irq_work_queue(struct irq_work *work)
  15902. {
  15903. + struct llist_head *list;
  15904. + bool lazy_work, realtime = IS_ENABLED(CONFIG_PREEMPT_RT_FULL);
  15905. +
  15906. /* Only queue if not already pending */
  15907. if (!irq_work_claim(work))
  15908. return false;
  15909. @@ -93,13 +104,15 @@
  15910. /* Queue the entry and raise the IPI if needed. */
  15911. preempt_disable();
  15912. - /* If the work is "lazy", handle it from next tick if any */
  15913. - if (work->flags & IRQ_WORK_LAZY) {
  15914. - if (llist_add(&work->llnode, this_cpu_ptr(&lazy_list)) &&
  15915. - tick_nohz_tick_stopped())
  15916. - arch_irq_work_raise();
  15917. - } else {
  15918. - if (llist_add(&work->llnode, this_cpu_ptr(&raised_list)))
  15919. + lazy_work = work->flags & IRQ_WORK_LAZY;
  15920. +
  15921. + if (lazy_work || (realtime && !(work->flags & IRQ_WORK_HARD_IRQ)))
  15922. + list = this_cpu_ptr(&lazy_list);
  15923. + else
  15924. + list = this_cpu_ptr(&raised_list);
  15925. +
  15926. + if (llist_add(&work->llnode, list)) {
  15927. + if (!lazy_work || tick_nohz_tick_stopped())
  15928. arch_irq_work_raise();
  15929. }
  15930. @@ -116,9 +129,8 @@
  15931. raised = this_cpu_ptr(&raised_list);
  15932. lazy = this_cpu_ptr(&lazy_list);
  15933. - if (llist_empty(raised) || arch_irq_work_has_interrupt())
  15934. - if (llist_empty(lazy))
  15935. - return false;
  15936. + if (llist_empty(raised) && llist_empty(lazy))
  15937. + return false;
  15938. /* All work should have been flushed before going offline */
  15939. WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
  15940. @@ -132,7 +144,7 @@
  15941. struct irq_work *work;
  15942. struct llist_node *llnode;
  15943. - BUG_ON(!irqs_disabled());
  15944. + BUG_ON_NONRT(!irqs_disabled());
  15945. if (llist_empty(list))
  15946. return;
  15947. @@ -169,7 +181,16 @@
  15948. void irq_work_run(void)
  15949. {
  15950. irq_work_run_list(this_cpu_ptr(&raised_list));
  15951. - irq_work_run_list(this_cpu_ptr(&lazy_list));
  15952. + if (IS_ENABLED(CONFIG_PREEMPT_RT_FULL)) {
  15953. + /*
  15954. + * NOTE: we raise softirq via IPI for safety,
  15955. + * and execute in irq_work_tick() to move the
  15956. + * overhead from hard to soft irq context.
  15957. + */
  15958. + if (!llist_empty(this_cpu_ptr(&lazy_list)))
  15959. + raise_softirq(TIMER_SOFTIRQ);
  15960. + } else
  15961. + irq_work_run_list(this_cpu_ptr(&lazy_list));
  15962. }
  15963. EXPORT_SYMBOL_GPL(irq_work_run);
  15964. @@ -179,8 +200,17 @@
  15965. if (!llist_empty(raised) && !arch_irq_work_has_interrupt())
  15966. irq_work_run_list(raised);
  15967. +
  15968. + if (!IS_ENABLED(CONFIG_PREEMPT_RT_FULL))
  15969. + irq_work_run_list(this_cpu_ptr(&lazy_list));
  15970. +}
  15971. +
  15972. +#if defined(CONFIG_IRQ_WORK) && defined(CONFIG_PREEMPT_RT_FULL)
  15973. +void irq_work_tick_soft(void)
  15974. +{
  15975. irq_work_run_list(this_cpu_ptr(&lazy_list));
  15976. }
  15977. +#endif
  15978. /*
  15979. * Synchronize against the irq_work @entry, ensures the entry is not
  15980. diff -Nur linux-4.4.46.orig/kernel/Kconfig.locks linux-4.4.46/kernel/Kconfig.locks
  15981. --- linux-4.4.46.orig/kernel/Kconfig.locks 2017-02-01 08:31:11.000000000 +0100
  15982. +++ linux-4.4.46/kernel/Kconfig.locks 2017-02-03 17:18:10.919618749 +0100
  15983. @@ -225,11 +225,11 @@
  15984. config MUTEX_SPIN_ON_OWNER
  15985. def_bool y
  15986. - depends on SMP && !DEBUG_MUTEXES && ARCH_SUPPORTS_ATOMIC_RMW
  15987. + depends on SMP && !DEBUG_MUTEXES && ARCH_SUPPORTS_ATOMIC_RMW && !PREEMPT_RT_FULL
  15988. config RWSEM_SPIN_ON_OWNER
  15989. def_bool y
  15990. - depends on SMP && RWSEM_XCHGADD_ALGORITHM && ARCH_SUPPORTS_ATOMIC_RMW
  15991. + depends on SMP && RWSEM_XCHGADD_ALGORITHM && ARCH_SUPPORTS_ATOMIC_RMW && !PREEMPT_RT_FULL
  15992. config LOCK_SPIN_ON_OWNER
  15993. def_bool y
  15994. diff -Nur linux-4.4.46.orig/kernel/Kconfig.preempt linux-4.4.46/kernel/Kconfig.preempt
  15995. --- linux-4.4.46.orig/kernel/Kconfig.preempt 2017-02-01 08:31:11.000000000 +0100
  15996. +++ linux-4.4.46/kernel/Kconfig.preempt 2017-02-03 17:18:10.919618749 +0100
  15997. @@ -1,3 +1,16 @@
  15998. +config PREEMPT
  15999. + bool
  16000. + select PREEMPT_COUNT
  16001. +
  16002. +config PREEMPT_RT_BASE
  16003. + bool
  16004. + select PREEMPT
  16005. +
  16006. +config HAVE_PREEMPT_LAZY
  16007. + bool
  16008. +
  16009. +config PREEMPT_LAZY
  16010. + def_bool y if HAVE_PREEMPT_LAZY && PREEMPT_RT_FULL
  16011. choice
  16012. prompt "Preemption Model"
  16013. @@ -33,9 +46,9 @@
  16014. Select this if you are building a kernel for a desktop system.
  16015. -config PREEMPT
  16016. +config PREEMPT__LL
  16017. bool "Preemptible Kernel (Low-Latency Desktop)"
  16018. - select PREEMPT_COUNT
  16019. + select PREEMPT
  16020. select UNINLINE_SPIN_UNLOCK if !ARCH_INLINE_SPIN_UNLOCK
  16021. help
  16022. This option reduces the latency of the kernel by making
  16023. @@ -52,6 +65,22 @@
  16024. embedded system with latency requirements in the milliseconds
  16025. range.
  16026. +config PREEMPT_RTB
  16027. + bool "Preemptible Kernel (Basic RT)"
  16028. + select PREEMPT_RT_BASE
  16029. + help
  16030. + This option is basically the same as (Low-Latency Desktop) but
  16031. + enables changes which are preliminary for the full preemptible
  16032. + RT kernel.
  16033. +
  16034. +config PREEMPT_RT_FULL
  16035. + bool "Fully Preemptible Kernel (RT)"
  16036. + depends on IRQ_FORCED_THREADING
  16037. + select PREEMPT_RT_BASE
  16038. + select PREEMPT_RCU
  16039. + help
  16040. + All and everything
  16041. +
  16042. endchoice
  16043. config PREEMPT_COUNT
  16044. diff -Nur linux-4.4.46.orig/kernel/ksysfs.c linux-4.4.46/kernel/ksysfs.c
  16045. --- linux-4.4.46.orig/kernel/ksysfs.c 2017-02-01 08:31:11.000000000 +0100
  16046. +++ linux-4.4.46/kernel/ksysfs.c 2017-02-03 17:18:10.923618903 +0100
  16047. @@ -136,6 +136,15 @@
  16048. #endif /* CONFIG_KEXEC_CORE */
  16049. +#if defined(CONFIG_PREEMPT_RT_FULL)
  16050. +static ssize_t realtime_show(struct kobject *kobj,
  16051. + struct kobj_attribute *attr, char *buf)
  16052. +{
  16053. + return sprintf(buf, "%d\n", 1);
  16054. +}
  16055. +KERNEL_ATTR_RO(realtime);
  16056. +#endif
  16057. +
  16058. /* whether file capabilities are enabled */
  16059. static ssize_t fscaps_show(struct kobject *kobj,
  16060. struct kobj_attribute *attr, char *buf)
  16061. @@ -203,6 +212,9 @@
  16062. &vmcoreinfo_attr.attr,
  16063. #endif
  16064. &rcu_expedited_attr.attr,
  16065. +#ifdef CONFIG_PREEMPT_RT_FULL
  16066. + &realtime_attr.attr,
  16067. +#endif
  16068. NULL
  16069. };
  16070. diff -Nur linux-4.4.46.orig/kernel/locking/lglock.c linux-4.4.46/kernel/locking/lglock.c
  16071. --- linux-4.4.46.orig/kernel/locking/lglock.c 2017-02-01 08:31:11.000000000 +0100
  16072. +++ linux-4.4.46/kernel/locking/lglock.c 2017-02-03 17:18:10.923618903 +0100
  16073. @@ -4,6 +4,15 @@
  16074. #include <linux/cpu.h>
  16075. #include <linux/string.h>
  16076. +#ifndef CONFIG_PREEMPT_RT_FULL
  16077. +# define lg_lock_ptr arch_spinlock_t
  16078. +# define lg_do_lock(l) arch_spin_lock(l)
  16079. +# define lg_do_unlock(l) arch_spin_unlock(l)
  16080. +#else
  16081. +# define lg_lock_ptr struct rt_mutex
  16082. +# define lg_do_lock(l) __rt_spin_lock__no_mg(l)
  16083. +# define lg_do_unlock(l) __rt_spin_unlock(l)
  16084. +#endif
  16085. /*
  16086. * Note there is no uninit, so lglocks cannot be defined in
  16087. * modules (but it's fine to use them from there)
  16088. @@ -12,51 +21,60 @@
  16089. void lg_lock_init(struct lglock *lg, char *name)
  16090. {
  16091. +#ifdef CONFIG_PREEMPT_RT_FULL
  16092. + int i;
  16093. +
  16094. + for_each_possible_cpu(i) {
  16095. + struct rt_mutex *lock = per_cpu_ptr(lg->lock, i);
  16096. +
  16097. + rt_mutex_init(lock);
  16098. + }
  16099. +#endif
  16100. LOCKDEP_INIT_MAP(&lg->lock_dep_map, name, &lg->lock_key, 0);
  16101. }
  16102. EXPORT_SYMBOL(lg_lock_init);
  16103. void lg_local_lock(struct lglock *lg)
  16104. {
  16105. - arch_spinlock_t *lock;
  16106. + lg_lock_ptr *lock;
  16107. - preempt_disable();
  16108. + migrate_disable();
  16109. lock_acquire_shared(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  16110. lock = this_cpu_ptr(lg->lock);
  16111. - arch_spin_lock(lock);
  16112. + lg_do_lock(lock);
  16113. }
  16114. EXPORT_SYMBOL(lg_local_lock);
  16115. void lg_local_unlock(struct lglock *lg)
  16116. {
  16117. - arch_spinlock_t *lock;
  16118. + lg_lock_ptr *lock;
  16119. lock_release(&lg->lock_dep_map, 1, _RET_IP_);
  16120. lock = this_cpu_ptr(lg->lock);
  16121. - arch_spin_unlock(lock);
  16122. - preempt_enable();
  16123. + lg_do_unlock(lock);
  16124. + migrate_enable();
  16125. }
  16126. EXPORT_SYMBOL(lg_local_unlock);
  16127. void lg_local_lock_cpu(struct lglock *lg, int cpu)
  16128. {
  16129. - arch_spinlock_t *lock;
  16130. + lg_lock_ptr *lock;
  16131. - preempt_disable();
  16132. + preempt_disable_nort();
  16133. lock_acquire_shared(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  16134. lock = per_cpu_ptr(lg->lock, cpu);
  16135. - arch_spin_lock(lock);
  16136. + lg_do_lock(lock);
  16137. }
  16138. EXPORT_SYMBOL(lg_local_lock_cpu);
  16139. void lg_local_unlock_cpu(struct lglock *lg, int cpu)
  16140. {
  16141. - arch_spinlock_t *lock;
  16142. + lg_lock_ptr *lock;
  16143. lock_release(&lg->lock_dep_map, 1, _RET_IP_);
  16144. lock = per_cpu_ptr(lg->lock, cpu);
  16145. - arch_spin_unlock(lock);
  16146. - preempt_enable();
  16147. + lg_do_unlock(lock);
  16148. + preempt_enable_nort();
  16149. }
  16150. EXPORT_SYMBOL(lg_local_unlock_cpu);
  16151. @@ -68,30 +86,30 @@
  16152. if (cpu2 < cpu1)
  16153. swap(cpu1, cpu2);
  16154. - preempt_disable();
  16155. + preempt_disable_nort();
  16156. lock_acquire_shared(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  16157. - arch_spin_lock(per_cpu_ptr(lg->lock, cpu1));
  16158. - arch_spin_lock(per_cpu_ptr(lg->lock, cpu2));
  16159. + lg_do_lock(per_cpu_ptr(lg->lock, cpu1));
  16160. + lg_do_lock(per_cpu_ptr(lg->lock, cpu2));
  16161. }
  16162. void lg_double_unlock(struct lglock *lg, int cpu1, int cpu2)
  16163. {
  16164. lock_release(&lg->lock_dep_map, 1, _RET_IP_);
  16165. - arch_spin_unlock(per_cpu_ptr(lg->lock, cpu1));
  16166. - arch_spin_unlock(per_cpu_ptr(lg->lock, cpu2));
  16167. - preempt_enable();
  16168. + lg_do_unlock(per_cpu_ptr(lg->lock, cpu1));
  16169. + lg_do_unlock(per_cpu_ptr(lg->lock, cpu2));
  16170. + preempt_enable_nort();
  16171. }
  16172. void lg_global_lock(struct lglock *lg)
  16173. {
  16174. int i;
  16175. - preempt_disable();
  16176. + preempt_disable_nort();
  16177. lock_acquire_exclusive(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  16178. for_each_possible_cpu(i) {
  16179. - arch_spinlock_t *lock;
  16180. + lg_lock_ptr *lock;
  16181. lock = per_cpu_ptr(lg->lock, i);
  16182. - arch_spin_lock(lock);
  16183. + lg_do_lock(lock);
  16184. }
  16185. }
  16186. EXPORT_SYMBOL(lg_global_lock);
  16187. @@ -102,10 +120,35 @@
  16188. lock_release(&lg->lock_dep_map, 1, _RET_IP_);
  16189. for_each_possible_cpu(i) {
  16190. - arch_spinlock_t *lock;
  16191. + lg_lock_ptr *lock;
  16192. lock = per_cpu_ptr(lg->lock, i);
  16193. - arch_spin_unlock(lock);
  16194. + lg_do_unlock(lock);
  16195. }
  16196. - preempt_enable();
  16197. + preempt_enable_nort();
  16198. }
  16199. EXPORT_SYMBOL(lg_global_unlock);
  16200. +
  16201. +#ifdef CONFIG_PREEMPT_RT_FULL
  16202. +/*
  16203. + * HACK: If you use this, you get to keep the pieces.
  16204. + * Used in queue_stop_cpus_work() when stop machinery
  16205. + * is called from inactive CPU, so we can't schedule.
  16206. + */
  16207. +# define lg_do_trylock_relax(l) \
  16208. + do { \
  16209. + while (!__rt_spin_trylock(l)) \
  16210. + cpu_relax(); \
  16211. + } while (0)
  16212. +
  16213. +void lg_global_trylock_relax(struct lglock *lg)
  16214. +{
  16215. + int i;
  16216. +
  16217. + lock_acquire_exclusive(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  16218. + for_each_possible_cpu(i) {
  16219. + lg_lock_ptr *lock;
  16220. + lock = per_cpu_ptr(lg->lock, i);
  16221. + lg_do_trylock_relax(lock);
  16222. + }
  16223. +}
  16224. +#endif
  16225. diff -Nur linux-4.4.46.orig/kernel/locking/lockdep.c linux-4.4.46/kernel/locking/lockdep.c
  16226. --- linux-4.4.46.orig/kernel/locking/lockdep.c 2017-02-01 08:31:11.000000000 +0100
  16227. +++ linux-4.4.46/kernel/locking/lockdep.c 2017-02-03 17:18:10.923618903 +0100
  16228. @@ -3525,6 +3525,7 @@
  16229. }
  16230. }
  16231. +#ifndef CONFIG_PREEMPT_RT_FULL
  16232. /*
  16233. * We dont accurately track softirq state in e.g.
  16234. * hardirq contexts (such as on 4KSTACKS), so only
  16235. @@ -3539,6 +3540,7 @@
  16236. DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
  16237. }
  16238. }
  16239. +#endif
  16240. if (!debug_locks)
  16241. print_irqtrace_events(current);
  16242. diff -Nur linux-4.4.46.orig/kernel/locking/locktorture.c linux-4.4.46/kernel/locking/locktorture.c
  16243. --- linux-4.4.46.orig/kernel/locking/locktorture.c 2017-02-01 08:31:11.000000000 +0100
  16244. +++ linux-4.4.46/kernel/locking/locktorture.c 2017-02-03 17:18:10.923618903 +0100
  16245. @@ -26,7 +26,6 @@
  16246. #include <linux/kthread.h>
  16247. #include <linux/sched/rt.h>
  16248. #include <linux/spinlock.h>
  16249. -#include <linux/rwlock.h>
  16250. #include <linux/mutex.h>
  16251. #include <linux/rwsem.h>
  16252. #include <linux/smp.h>
  16253. diff -Nur linux-4.4.46.orig/kernel/locking/Makefile linux-4.4.46/kernel/locking/Makefile
  16254. --- linux-4.4.46.orig/kernel/locking/Makefile 2017-02-01 08:31:11.000000000 +0100
  16255. +++ linux-4.4.46/kernel/locking/Makefile 2017-02-03 17:18:10.923618903 +0100
  16256. @@ -1,5 +1,5 @@
  16257. -obj-y += mutex.o semaphore.o rwsem.o percpu-rwsem.o
  16258. +obj-y += semaphore.o percpu-rwsem.o
  16259. ifdef CONFIG_FUNCTION_TRACER
  16260. CFLAGS_REMOVE_lockdep.o = $(CC_FLAGS_FTRACE)
  16261. @@ -8,7 +8,11 @@
  16262. CFLAGS_REMOVE_rtmutex-debug.o = $(CC_FLAGS_FTRACE)
  16263. endif
  16264. +ifneq ($(CONFIG_PREEMPT_RT_FULL),y)
  16265. +obj-y += mutex.o
  16266. obj-$(CONFIG_DEBUG_MUTEXES) += mutex-debug.o
  16267. +obj-y += rwsem.o
  16268. +endif
  16269. obj-$(CONFIG_LOCKDEP) += lockdep.o
  16270. ifeq ($(CONFIG_PROC_FS),y)
  16271. obj-$(CONFIG_LOCKDEP) += lockdep_proc.o
  16272. @@ -22,7 +26,10 @@
  16273. obj-$(CONFIG_DEBUG_RT_MUTEXES) += rtmutex-debug.o
  16274. obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock.o
  16275. obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock_debug.o
  16276. +ifneq ($(CONFIG_PREEMPT_RT_FULL),y)
  16277. obj-$(CONFIG_RWSEM_GENERIC_SPINLOCK) += rwsem-spinlock.o
  16278. obj-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem-xadd.o
  16279. +endif
  16280. +obj-$(CONFIG_PREEMPT_RT_FULL) += rt.o
  16281. obj-$(CONFIG_QUEUED_RWLOCKS) += qrwlock.o
  16282. obj-$(CONFIG_LOCK_TORTURE_TEST) += locktorture.o
  16283. diff -Nur linux-4.4.46.orig/kernel/locking/rt.c linux-4.4.46/kernel/locking/rt.c
  16284. --- linux-4.4.46.orig/kernel/locking/rt.c 1970-01-01 01:00:00.000000000 +0100
  16285. +++ linux-4.4.46/kernel/locking/rt.c 2017-02-03 17:18:10.923618903 +0100
  16286. @@ -0,0 +1,474 @@
  16287. +/*
  16288. + * kernel/rt.c
  16289. + *
  16290. + * Real-Time Preemption Support
  16291. + *
  16292. + * started by Ingo Molnar:
  16293. + *
  16294. + * Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  16295. + * Copyright (C) 2006, Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  16296. + *
  16297. + * historic credit for proving that Linux spinlocks can be implemented via
  16298. + * RT-aware mutexes goes to many people: The Pmutex project (Dirk Grambow
  16299. + * and others) who prototyped it on 2.4 and did lots of comparative
  16300. + * research and analysis; TimeSys, for proving that you can implement a
  16301. + * fully preemptible kernel via the use of IRQ threading and mutexes;
  16302. + * Bill Huey for persuasively arguing on lkml that the mutex model is the
  16303. + * right one; and to MontaVista, who ported pmutexes to 2.6.
  16304. + *
  16305. + * This code is a from-scratch implementation and is not based on pmutexes,
  16306. + * but the idea of converting spinlocks to mutexes is used here too.
  16307. + *
  16308. + * lock debugging, locking tree, deadlock detection:
  16309. + *
  16310. + * Copyright (C) 2004, LynuxWorks, Inc., Igor Manyilov, Bill Huey
  16311. + * Released under the General Public License (GPL).
  16312. + *
  16313. + * Includes portions of the generic R/W semaphore implementation from:
  16314. + *
  16315. + * Copyright (c) 2001 David Howells (dhowells@redhat.com).
  16316. + * - Derived partially from idea by Andrea Arcangeli <andrea@suse.de>
  16317. + * - Derived also from comments by Linus
  16318. + *
  16319. + * Pending ownership of locks and ownership stealing:
  16320. + *
  16321. + * Copyright (C) 2005, Kihon Technologies Inc., Steven Rostedt
  16322. + *
  16323. + * (also by Steven Rostedt)
  16324. + * - Converted single pi_lock to individual task locks.
  16325. + *
  16326. + * By Esben Nielsen:
  16327. + * Doing priority inheritance with help of the scheduler.
  16328. + *
  16329. + * Copyright (C) 2006, Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  16330. + * - major rework based on Esben Nielsens initial patch
  16331. + * - replaced thread_info references by task_struct refs
  16332. + * - removed task->pending_owner dependency
  16333. + * - BKL drop/reacquire for semaphore style locks to avoid deadlocks
  16334. + * in the scheduler return path as discussed with Steven Rostedt
  16335. + *
  16336. + * Copyright (C) 2006, Kihon Technologies Inc.
  16337. + * Steven Rostedt <rostedt@goodmis.org>
  16338. + * - debugged and patched Thomas Gleixner's rework.
  16339. + * - added back the cmpxchg to the rework.
  16340. + * - turned atomic require back on for SMP.
  16341. + */
  16342. +
  16343. +#include <linux/spinlock.h>
  16344. +#include <linux/rtmutex.h>
  16345. +#include <linux/sched.h>
  16346. +#include <linux/delay.h>
  16347. +#include <linux/module.h>
  16348. +#include <linux/kallsyms.h>
  16349. +#include <linux/syscalls.h>
  16350. +#include <linux/interrupt.h>
  16351. +#include <linux/plist.h>
  16352. +#include <linux/fs.h>
  16353. +#include <linux/futex.h>
  16354. +#include <linux/hrtimer.h>
  16355. +
  16356. +#include "rtmutex_common.h"
  16357. +
  16358. +/*
  16359. + * struct mutex functions
  16360. + */
  16361. +void __mutex_do_init(struct mutex *mutex, const char *name,
  16362. + struct lock_class_key *key)
  16363. +{
  16364. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  16365. + /*
  16366. + * Make sure we are not reinitializing a held lock:
  16367. + */
  16368. + debug_check_no_locks_freed((void *)mutex, sizeof(*mutex));
  16369. + lockdep_init_map(&mutex->dep_map, name, key, 0);
  16370. +#endif
  16371. + mutex->lock.save_state = 0;
  16372. +}
  16373. +EXPORT_SYMBOL(__mutex_do_init);
  16374. +
  16375. +void __lockfunc _mutex_lock(struct mutex *lock)
  16376. +{
  16377. + mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  16378. + rt_mutex_lock(&lock->lock);
  16379. +}
  16380. +EXPORT_SYMBOL(_mutex_lock);
  16381. +
  16382. +int __lockfunc _mutex_lock_interruptible(struct mutex *lock)
  16383. +{
  16384. + int ret;
  16385. +
  16386. + mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  16387. + ret = rt_mutex_lock_interruptible(&lock->lock);
  16388. + if (ret)
  16389. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  16390. + return ret;
  16391. +}
  16392. +EXPORT_SYMBOL(_mutex_lock_interruptible);
  16393. +
  16394. +int __lockfunc _mutex_lock_killable(struct mutex *lock)
  16395. +{
  16396. + int ret;
  16397. +
  16398. + mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  16399. + ret = rt_mutex_lock_killable(&lock->lock);
  16400. + if (ret)
  16401. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  16402. + return ret;
  16403. +}
  16404. +EXPORT_SYMBOL(_mutex_lock_killable);
  16405. +
  16406. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  16407. +void __lockfunc _mutex_lock_nested(struct mutex *lock, int subclass)
  16408. +{
  16409. + mutex_acquire_nest(&lock->dep_map, subclass, 0, NULL, _RET_IP_);
  16410. + rt_mutex_lock(&lock->lock);
  16411. +}
  16412. +EXPORT_SYMBOL(_mutex_lock_nested);
  16413. +
  16414. +void __lockfunc _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest)
  16415. +{
  16416. + mutex_acquire_nest(&lock->dep_map, 0, 0, nest, _RET_IP_);
  16417. + rt_mutex_lock(&lock->lock);
  16418. +}
  16419. +EXPORT_SYMBOL(_mutex_lock_nest_lock);
  16420. +
  16421. +int __lockfunc _mutex_lock_interruptible_nested(struct mutex *lock, int subclass)
  16422. +{
  16423. + int ret;
  16424. +
  16425. + mutex_acquire_nest(&lock->dep_map, subclass, 0, NULL, _RET_IP_);
  16426. + ret = rt_mutex_lock_interruptible(&lock->lock);
  16427. + if (ret)
  16428. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  16429. + return ret;
  16430. +}
  16431. +EXPORT_SYMBOL(_mutex_lock_interruptible_nested);
  16432. +
  16433. +int __lockfunc _mutex_lock_killable_nested(struct mutex *lock, int subclass)
  16434. +{
  16435. + int ret;
  16436. +
  16437. + mutex_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
  16438. + ret = rt_mutex_lock_killable(&lock->lock);
  16439. + if (ret)
  16440. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  16441. + return ret;
  16442. +}
  16443. +EXPORT_SYMBOL(_mutex_lock_killable_nested);
  16444. +#endif
  16445. +
  16446. +int __lockfunc _mutex_trylock(struct mutex *lock)
  16447. +{
  16448. + int ret = rt_mutex_trylock(&lock->lock);
  16449. +
  16450. + if (ret)
  16451. + mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  16452. +
  16453. + return ret;
  16454. +}
  16455. +EXPORT_SYMBOL(_mutex_trylock);
  16456. +
  16457. +void __lockfunc _mutex_unlock(struct mutex *lock)
  16458. +{
  16459. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  16460. + rt_mutex_unlock(&lock->lock);
  16461. +}
  16462. +EXPORT_SYMBOL(_mutex_unlock);
  16463. +
  16464. +/*
  16465. + * rwlock_t functions
  16466. + */
  16467. +int __lockfunc rt_write_trylock(rwlock_t *rwlock)
  16468. +{
  16469. + int ret;
  16470. +
  16471. + migrate_disable();
  16472. + ret = rt_mutex_trylock(&rwlock->lock);
  16473. + if (ret)
  16474. + rwlock_acquire(&rwlock->dep_map, 0, 1, _RET_IP_);
  16475. + else
  16476. + migrate_enable();
  16477. +
  16478. + return ret;
  16479. +}
  16480. +EXPORT_SYMBOL(rt_write_trylock);
  16481. +
  16482. +int __lockfunc rt_write_trylock_irqsave(rwlock_t *rwlock, unsigned long *flags)
  16483. +{
  16484. + int ret;
  16485. +
  16486. + *flags = 0;
  16487. + ret = rt_write_trylock(rwlock);
  16488. + return ret;
  16489. +}
  16490. +EXPORT_SYMBOL(rt_write_trylock_irqsave);
  16491. +
  16492. +int __lockfunc rt_read_trylock(rwlock_t *rwlock)
  16493. +{
  16494. + struct rt_mutex *lock = &rwlock->lock;
  16495. + int ret = 1;
  16496. +
  16497. + /*
  16498. + * recursive read locks succeed when current owns the lock,
  16499. + * but not when read_depth == 0 which means that the lock is
  16500. + * write locked.
  16501. + */
  16502. + if (rt_mutex_owner(lock) != current) {
  16503. + migrate_disable();
  16504. + ret = rt_mutex_trylock(lock);
  16505. + if (ret)
  16506. + rwlock_acquire(&rwlock->dep_map, 0, 1, _RET_IP_);
  16507. + else
  16508. + migrate_enable();
  16509. +
  16510. + } else if (!rwlock->read_depth) {
  16511. + ret = 0;
  16512. + }
  16513. +
  16514. + if (ret)
  16515. + rwlock->read_depth++;
  16516. +
  16517. + return ret;
  16518. +}
  16519. +EXPORT_SYMBOL(rt_read_trylock);
  16520. +
  16521. +void __lockfunc rt_write_lock(rwlock_t *rwlock)
  16522. +{
  16523. + rwlock_acquire(&rwlock->dep_map, 0, 0, _RET_IP_);
  16524. + __rt_spin_lock(&rwlock->lock);
  16525. +}
  16526. +EXPORT_SYMBOL(rt_write_lock);
  16527. +
  16528. +void __lockfunc rt_read_lock(rwlock_t *rwlock)
  16529. +{
  16530. + struct rt_mutex *lock = &rwlock->lock;
  16531. +
  16532. +
  16533. + /*
  16534. + * recursive read locks succeed when current owns the lock
  16535. + */
  16536. + if (rt_mutex_owner(lock) != current) {
  16537. + rwlock_acquire(&rwlock->dep_map, 0, 0, _RET_IP_);
  16538. + __rt_spin_lock(lock);
  16539. + }
  16540. + rwlock->read_depth++;
  16541. +}
  16542. +
  16543. +EXPORT_SYMBOL(rt_read_lock);
  16544. +
  16545. +void __lockfunc rt_write_unlock(rwlock_t *rwlock)
  16546. +{
  16547. + /* NOTE: we always pass in '1' for nested, for simplicity */
  16548. + rwlock_release(&rwlock->dep_map, 1, _RET_IP_);
  16549. + __rt_spin_unlock(&rwlock->lock);
  16550. + migrate_enable();
  16551. +}
  16552. +EXPORT_SYMBOL(rt_write_unlock);
  16553. +
  16554. +void __lockfunc rt_read_unlock(rwlock_t *rwlock)
  16555. +{
  16556. + /* Release the lock only when read_depth is down to 0 */
  16557. + if (--rwlock->read_depth == 0) {
  16558. + rwlock_release(&rwlock->dep_map, 1, _RET_IP_);
  16559. + __rt_spin_unlock(&rwlock->lock);
  16560. + migrate_enable();
  16561. + }
  16562. +}
  16563. +EXPORT_SYMBOL(rt_read_unlock);
  16564. +
  16565. +unsigned long __lockfunc rt_write_lock_irqsave(rwlock_t *rwlock)
  16566. +{
  16567. + rt_write_lock(rwlock);
  16568. +
  16569. + return 0;
  16570. +}
  16571. +EXPORT_SYMBOL(rt_write_lock_irqsave);
  16572. +
  16573. +unsigned long __lockfunc rt_read_lock_irqsave(rwlock_t *rwlock)
  16574. +{
  16575. + rt_read_lock(rwlock);
  16576. +
  16577. + return 0;
  16578. +}
  16579. +EXPORT_SYMBOL(rt_read_lock_irqsave);
  16580. +
  16581. +void __rt_rwlock_init(rwlock_t *rwlock, char *name, struct lock_class_key *key)
  16582. +{
  16583. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  16584. + /*
  16585. + * Make sure we are not reinitializing a held lock:
  16586. + */
  16587. + debug_check_no_locks_freed((void *)rwlock, sizeof(*rwlock));
  16588. + lockdep_init_map(&rwlock->dep_map, name, key, 0);
  16589. +#endif
  16590. + rwlock->lock.save_state = 1;
  16591. + rwlock->read_depth = 0;
  16592. +}
  16593. +EXPORT_SYMBOL(__rt_rwlock_init);
  16594. +
  16595. +/*
  16596. + * rw_semaphores
  16597. + */
  16598. +
  16599. +void rt_up_write(struct rw_semaphore *rwsem)
  16600. +{
  16601. + rwsem_release(&rwsem->dep_map, 1, _RET_IP_);
  16602. + rt_mutex_unlock(&rwsem->lock);
  16603. +}
  16604. +EXPORT_SYMBOL(rt_up_write);
  16605. +
  16606. +void __rt_up_read(struct rw_semaphore *rwsem)
  16607. +{
  16608. + if (--rwsem->read_depth == 0)
  16609. + rt_mutex_unlock(&rwsem->lock);
  16610. +}
  16611. +
  16612. +void rt_up_read(struct rw_semaphore *rwsem)
  16613. +{
  16614. + rwsem_release(&rwsem->dep_map, 1, _RET_IP_);
  16615. + __rt_up_read(rwsem);
  16616. +}
  16617. +EXPORT_SYMBOL(rt_up_read);
  16618. +
  16619. +/*
  16620. + * downgrade a write lock into a read lock
  16621. + * - just wake up any readers at the front of the queue
  16622. + */
  16623. +void rt_downgrade_write(struct rw_semaphore *rwsem)
  16624. +{
  16625. + BUG_ON(rt_mutex_owner(&rwsem->lock) != current);
  16626. + rwsem->read_depth = 1;
  16627. +}
  16628. +EXPORT_SYMBOL(rt_downgrade_write);
  16629. +
  16630. +int rt_down_write_trylock(struct rw_semaphore *rwsem)
  16631. +{
  16632. + int ret = rt_mutex_trylock(&rwsem->lock);
  16633. +
  16634. + if (ret)
  16635. + rwsem_acquire(&rwsem->dep_map, 0, 1, _RET_IP_);
  16636. + return ret;
  16637. +}
  16638. +EXPORT_SYMBOL(rt_down_write_trylock);
  16639. +
  16640. +void rt_down_write(struct rw_semaphore *rwsem)
  16641. +{
  16642. + rwsem_acquire(&rwsem->dep_map, 0, 0, _RET_IP_);
  16643. + rt_mutex_lock(&rwsem->lock);
  16644. +}
  16645. +EXPORT_SYMBOL(rt_down_write);
  16646. +
  16647. +void rt_down_write_nested(struct rw_semaphore *rwsem, int subclass)
  16648. +{
  16649. + rwsem_acquire(&rwsem->dep_map, subclass, 0, _RET_IP_);
  16650. + rt_mutex_lock(&rwsem->lock);
  16651. +}
  16652. +EXPORT_SYMBOL(rt_down_write_nested);
  16653. +
  16654. +void rt_down_write_nested_lock(struct rw_semaphore *rwsem,
  16655. + struct lockdep_map *nest)
  16656. +{
  16657. + rwsem_acquire_nest(&rwsem->dep_map, 0, 0, nest, _RET_IP_);
  16658. + rt_mutex_lock(&rwsem->lock);
  16659. +}
  16660. +EXPORT_SYMBOL(rt_down_write_nested_lock);
  16661. +
  16662. +int rt__down_read_trylock(struct rw_semaphore *rwsem)
  16663. +{
  16664. + struct rt_mutex *lock = &rwsem->lock;
  16665. + int ret = 1;
  16666. +
  16667. + /*
  16668. + * recursive read locks succeed when current owns the rwsem,
  16669. + * but not when read_depth == 0 which means that the rwsem is
  16670. + * write locked.
  16671. + */
  16672. + if (rt_mutex_owner(lock) != current)
  16673. + ret = rt_mutex_trylock(&rwsem->lock);
  16674. + else if (!rwsem->read_depth)
  16675. + ret = 0;
  16676. +
  16677. + if (ret)
  16678. + rwsem->read_depth++;
  16679. + return ret;
  16680. +
  16681. +}
  16682. +
  16683. +int rt_down_read_trylock(struct rw_semaphore *rwsem)
  16684. +{
  16685. + int ret;
  16686. +
  16687. + ret = rt__down_read_trylock(rwsem);
  16688. + if (ret)
  16689. + rwsem_acquire(&rwsem->dep_map, 0, 1, _RET_IP_);
  16690. +
  16691. + return ret;
  16692. +}
  16693. +EXPORT_SYMBOL(rt_down_read_trylock);
  16694. +
  16695. +void rt__down_read(struct rw_semaphore *rwsem)
  16696. +{
  16697. + struct rt_mutex *lock = &rwsem->lock;
  16698. +
  16699. + if (rt_mutex_owner(lock) != current)
  16700. + rt_mutex_lock(&rwsem->lock);
  16701. + rwsem->read_depth++;
  16702. +}
  16703. +EXPORT_SYMBOL(rt__down_read);
  16704. +
  16705. +static void __rt_down_read(struct rw_semaphore *rwsem, int subclass)
  16706. +{
  16707. + rwsem_acquire_read(&rwsem->dep_map, subclass, 0, _RET_IP_);
  16708. + rt__down_read(rwsem);
  16709. +}
  16710. +
  16711. +void rt_down_read(struct rw_semaphore *rwsem)
  16712. +{
  16713. + __rt_down_read(rwsem, 0);
  16714. +}
  16715. +EXPORT_SYMBOL(rt_down_read);
  16716. +
  16717. +void rt_down_read_nested(struct rw_semaphore *rwsem, int subclass)
  16718. +{
  16719. + __rt_down_read(rwsem, subclass);
  16720. +}
  16721. +EXPORT_SYMBOL(rt_down_read_nested);
  16722. +
  16723. +void __rt_rwsem_init(struct rw_semaphore *rwsem, const char *name,
  16724. + struct lock_class_key *key)
  16725. +{
  16726. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  16727. + /*
  16728. + * Make sure we are not reinitializing a held lock:
  16729. + */
  16730. + debug_check_no_locks_freed((void *)rwsem, sizeof(*rwsem));
  16731. + lockdep_init_map(&rwsem->dep_map, name, key, 0);
  16732. +#endif
  16733. + rwsem->read_depth = 0;
  16734. + rwsem->lock.save_state = 0;
  16735. +}
  16736. +EXPORT_SYMBOL(__rt_rwsem_init);
  16737. +
  16738. +/**
  16739. + * atomic_dec_and_mutex_lock - return holding mutex if we dec to 0
  16740. + * @cnt: the atomic which we are to dec
  16741. + * @lock: the mutex to return holding if we dec to 0
  16742. + *
  16743. + * return true and hold lock if we dec to 0, return false otherwise
  16744. + */
  16745. +int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock)
  16746. +{
  16747. + /* dec if we can't possibly hit 0 */
  16748. + if (atomic_add_unless(cnt, -1, 1))
  16749. + return 0;
  16750. + /* we might hit 0, so take the lock */
  16751. + mutex_lock(lock);
  16752. + if (!atomic_dec_and_test(cnt)) {
  16753. + /* when we actually did the dec, we didn't hit 0 */
  16754. + mutex_unlock(lock);
  16755. + return 0;
  16756. + }
  16757. + /* we hit 0, and we hold the lock */
  16758. + return 1;
  16759. +}
  16760. +EXPORT_SYMBOL(atomic_dec_and_mutex_lock);
  16761. diff -Nur linux-4.4.46.orig/kernel/locking/rtmutex.c linux-4.4.46/kernel/locking/rtmutex.c
  16762. --- linux-4.4.46.orig/kernel/locking/rtmutex.c 2017-02-01 08:31:11.000000000 +0100
  16763. +++ linux-4.4.46/kernel/locking/rtmutex.c 2017-02-03 17:18:10.927619058 +0100
  16764. @@ -7,6 +7,11 @@
  16765. * Copyright (C) 2005-2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  16766. * Copyright (C) 2005 Kihon Technologies Inc., Steven Rostedt
  16767. * Copyright (C) 2006 Esben Nielsen
  16768. + * Adaptive Spinlocks:
  16769. + * Copyright (C) 2008 Novell, Inc., Gregory Haskins, Sven Dietrich,
  16770. + * and Peter Morreale,
  16771. + * Adaptive Spinlocks simplification:
  16772. + * Copyright (C) 2008 Red Hat, Inc., Steven Rostedt <srostedt@redhat.com>
  16773. *
  16774. * See Documentation/locking/rt-mutex-design.txt for details.
  16775. */
  16776. @@ -16,6 +21,7 @@
  16777. #include <linux/sched/rt.h>
  16778. #include <linux/sched/deadline.h>
  16779. #include <linux/timer.h>
  16780. +#include <linux/ww_mutex.h>
  16781. #include "rtmutex_common.h"
  16782. @@ -133,6 +139,12 @@
  16783. WRITE_ONCE(*p, owner & ~RT_MUTEX_HAS_WAITERS);
  16784. }
  16785. +static int rt_mutex_real_waiter(struct rt_mutex_waiter *waiter)
  16786. +{
  16787. + return waiter && waiter != PI_WAKEUP_INPROGRESS &&
  16788. + waiter != PI_REQUEUE_INPROGRESS;
  16789. +}
  16790. +
  16791. /*
  16792. * We can speed up the acquire/release, if there's no debugging state to be
  16793. * set up.
  16794. @@ -163,13 +175,14 @@
  16795. * 2) Drop lock->wait_lock
  16796. * 3) Try to unlock the lock with cmpxchg
  16797. */
  16798. -static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
  16799. +static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
  16800. + unsigned long flags)
  16801. __releases(lock->wait_lock)
  16802. {
  16803. struct task_struct *owner = rt_mutex_owner(lock);
  16804. clear_rt_mutex_waiters(lock);
  16805. - raw_spin_unlock(&lock->wait_lock);
  16806. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  16807. /*
  16808. * If a new waiter comes in between the unlock and the cmpxchg
  16809. * we have two situations:
  16810. @@ -211,11 +224,12 @@
  16811. /*
  16812. * Simple slow path only version: lock->owner is protected by lock->wait_lock.
  16813. */
  16814. -static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
  16815. +static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
  16816. + unsigned long flags)
  16817. __releases(lock->wait_lock)
  16818. {
  16819. lock->owner = NULL;
  16820. - raw_spin_unlock(&lock->wait_lock);
  16821. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  16822. return true;
  16823. }
  16824. #endif
  16825. @@ -412,6 +426,14 @@
  16826. return debug_rt_mutex_detect_deadlock(waiter, chwalk);
  16827. }
  16828. +static void rt_mutex_wake_waiter(struct rt_mutex_waiter *waiter)
  16829. +{
  16830. + if (waiter->savestate)
  16831. + wake_up_lock_sleeper(waiter->task);
  16832. + else
  16833. + wake_up_process(waiter->task);
  16834. +}
  16835. +
  16836. /*
  16837. * Max number of times we'll walk the boosting chain:
  16838. */
  16839. @@ -419,7 +441,8 @@
  16840. static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
  16841. {
  16842. - return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
  16843. + return rt_mutex_real_waiter(p->pi_blocked_on) ?
  16844. + p->pi_blocked_on->lock : NULL;
  16845. }
  16846. /*
  16847. @@ -497,7 +520,6 @@
  16848. int ret = 0, depth = 0;
  16849. struct rt_mutex *lock;
  16850. bool detect_deadlock;
  16851. - unsigned long flags;
  16852. bool requeue = true;
  16853. detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
  16854. @@ -540,7 +562,7 @@
  16855. /*
  16856. * [1] Task cannot go away as we did a get_task() before !
  16857. */
  16858. - raw_spin_lock_irqsave(&task->pi_lock, flags);
  16859. + raw_spin_lock_irq(&task->pi_lock);
  16860. /*
  16861. * [2] Get the waiter on which @task is blocked on.
  16862. @@ -556,7 +578,7 @@
  16863. * reached or the state of the chain has changed while we
  16864. * dropped the locks.
  16865. */
  16866. - if (!waiter)
  16867. + if (!rt_mutex_real_waiter(waiter))
  16868. goto out_unlock_pi;
  16869. /*
  16870. @@ -624,7 +646,7 @@
  16871. * operations.
  16872. */
  16873. if (!raw_spin_trylock(&lock->wait_lock)) {
  16874. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  16875. + raw_spin_unlock_irq(&task->pi_lock);
  16876. cpu_relax();
  16877. goto retry;
  16878. }
  16879. @@ -655,7 +677,7 @@
  16880. /*
  16881. * No requeue[7] here. Just release @task [8]
  16882. */
  16883. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  16884. + raw_spin_unlock(&task->pi_lock);
  16885. put_task_struct(task);
  16886. /*
  16887. @@ -663,14 +685,14 @@
  16888. * If there is no owner of the lock, end of chain.
  16889. */
  16890. if (!rt_mutex_owner(lock)) {
  16891. - raw_spin_unlock(&lock->wait_lock);
  16892. + raw_spin_unlock_irq(&lock->wait_lock);
  16893. return 0;
  16894. }
  16895. /* [10] Grab the next task, i.e. owner of @lock */
  16896. task = rt_mutex_owner(lock);
  16897. get_task_struct(task);
  16898. - raw_spin_lock_irqsave(&task->pi_lock, flags);
  16899. + raw_spin_lock(&task->pi_lock);
  16900. /*
  16901. * No requeue [11] here. We just do deadlock detection.
  16902. @@ -685,8 +707,8 @@
  16903. top_waiter = rt_mutex_top_waiter(lock);
  16904. /* [13] Drop locks */
  16905. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  16906. - raw_spin_unlock(&lock->wait_lock);
  16907. + raw_spin_unlock(&task->pi_lock);
  16908. + raw_spin_unlock_irq(&lock->wait_lock);
  16909. /* If owner is not blocked, end of chain. */
  16910. if (!next_lock)
  16911. @@ -707,7 +729,7 @@
  16912. rt_mutex_enqueue(lock, waiter);
  16913. /* [8] Release the task */
  16914. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  16915. + raw_spin_unlock(&task->pi_lock);
  16916. put_task_struct(task);
  16917. /*
  16918. @@ -718,21 +740,24 @@
  16919. * follow here. This is the end of the chain we are walking.
  16920. */
  16921. if (!rt_mutex_owner(lock)) {
  16922. + struct rt_mutex_waiter *lock_top_waiter;
  16923. +
  16924. /*
  16925. * If the requeue [7] above changed the top waiter,
  16926. * then we need to wake the new top waiter up to try
  16927. * to get the lock.
  16928. */
  16929. - if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
  16930. - wake_up_process(rt_mutex_top_waiter(lock)->task);
  16931. - raw_spin_unlock(&lock->wait_lock);
  16932. + lock_top_waiter = rt_mutex_top_waiter(lock);
  16933. + if (prerequeue_top_waiter != lock_top_waiter)
  16934. + rt_mutex_wake_waiter(lock_top_waiter);
  16935. + raw_spin_unlock_irq(&lock->wait_lock);
  16936. return 0;
  16937. }
  16938. /* [10] Grab the next task, i.e. the owner of @lock */
  16939. task = rt_mutex_owner(lock);
  16940. get_task_struct(task);
  16941. - raw_spin_lock_irqsave(&task->pi_lock, flags);
  16942. + raw_spin_lock(&task->pi_lock);
  16943. /* [11] requeue the pi waiters if necessary */
  16944. if (waiter == rt_mutex_top_waiter(lock)) {
  16945. @@ -786,8 +811,8 @@
  16946. top_waiter = rt_mutex_top_waiter(lock);
  16947. /* [13] Drop the locks */
  16948. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  16949. - raw_spin_unlock(&lock->wait_lock);
  16950. + raw_spin_unlock(&task->pi_lock);
  16951. + raw_spin_unlock_irq(&lock->wait_lock);
  16952. /*
  16953. * Make the actual exit decisions [12], based on the stored
  16954. @@ -810,28 +835,46 @@
  16955. goto again;
  16956. out_unlock_pi:
  16957. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  16958. + raw_spin_unlock_irq(&task->pi_lock);
  16959. out_put_task:
  16960. put_task_struct(task);
  16961. return ret;
  16962. }
  16963. +
  16964. +#define STEAL_NORMAL 0
  16965. +#define STEAL_LATERAL 1
  16966. +
  16967. +/*
  16968. + * Note that RT tasks are excluded from lateral-steals to prevent the
  16969. + * introduction of an unbounded latency
  16970. + */
  16971. +static inline int lock_is_stealable(struct task_struct *task,
  16972. + struct task_struct *pendowner, int mode)
  16973. +{
  16974. + if (mode == STEAL_NORMAL || rt_task(task)) {
  16975. + if (task->prio >= pendowner->prio)
  16976. + return 0;
  16977. + } else if (task->prio > pendowner->prio)
  16978. + return 0;
  16979. + return 1;
  16980. +}
  16981. +
  16982. /*
  16983. * Try to take an rt-mutex
  16984. *
  16985. - * Must be called with lock->wait_lock held.
  16986. + * Must be called with lock->wait_lock held and interrupts disabled
  16987. *
  16988. * @lock: The lock to be acquired.
  16989. * @task: The task which wants to acquire the lock
  16990. * @waiter: The waiter that is queued to the lock's wait tree if the
  16991. * callsite called task_blocked_on_lock(), otherwise NULL
  16992. */
  16993. -static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  16994. - struct rt_mutex_waiter *waiter)
  16995. +static int __try_to_take_rt_mutex(struct rt_mutex *lock,
  16996. + struct task_struct *task,
  16997. + struct rt_mutex_waiter *waiter, int mode)
  16998. {
  16999. - unsigned long flags;
  17000. -
  17001. /*
  17002. * Before testing whether we can acquire @lock, we set the
  17003. * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
  17004. @@ -867,8 +910,10 @@
  17005. * If waiter is not the highest priority waiter of
  17006. * @lock, give up.
  17007. */
  17008. - if (waiter != rt_mutex_top_waiter(lock))
  17009. + if (waiter != rt_mutex_top_waiter(lock)) {
  17010. + /* XXX lock_is_stealable() ? */
  17011. return 0;
  17012. + }
  17013. /*
  17014. * We can acquire the lock. Remove the waiter from the
  17015. @@ -886,14 +931,10 @@
  17016. * not need to be dequeued.
  17017. */
  17018. if (rt_mutex_has_waiters(lock)) {
  17019. - /*
  17020. - * If @task->prio is greater than or equal to
  17021. - * the top waiter priority (kernel view),
  17022. - * @task lost.
  17023. - */
  17024. - if (task->prio >= rt_mutex_top_waiter(lock)->prio)
  17025. - return 0;
  17026. + struct task_struct *pown = rt_mutex_top_waiter(lock)->task;
  17027. + if (task != pown && !lock_is_stealable(task, pown, mode))
  17028. + return 0;
  17029. /*
  17030. * The current top waiter stays enqueued. We
  17031. * don't have to change anything in the lock
  17032. @@ -916,7 +957,7 @@
  17033. * case, but conditionals are more expensive than a redundant
  17034. * store.
  17035. */
  17036. - raw_spin_lock_irqsave(&task->pi_lock, flags);
  17037. + raw_spin_lock(&task->pi_lock);
  17038. task->pi_blocked_on = NULL;
  17039. /*
  17040. * Finish the lock acquisition. @task is the new owner. If
  17041. @@ -925,7 +966,7 @@
  17042. */
  17043. if (rt_mutex_has_waiters(lock))
  17044. rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
  17045. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  17046. + raw_spin_unlock(&task->pi_lock);
  17047. takeit:
  17048. /* We got the lock. */
  17049. @@ -942,12 +983,444 @@
  17050. return 1;
  17051. }
  17052. +#ifdef CONFIG_PREEMPT_RT_FULL
  17053. +/*
  17054. + * preemptible spin_lock functions:
  17055. + */
  17056. +static inline void rt_spin_lock_fastlock(struct rt_mutex *lock,
  17057. + void (*slowfn)(struct rt_mutex *lock,
  17058. + bool mg_off),
  17059. + bool do_mig_dis)
  17060. +{
  17061. + might_sleep_no_state_check();
  17062. +
  17063. + if (do_mig_dis)
  17064. + migrate_disable();
  17065. +
  17066. + if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
  17067. + rt_mutex_deadlock_account_lock(lock, current);
  17068. + else
  17069. + slowfn(lock, do_mig_dis);
  17070. +}
  17071. +
  17072. +static inline int rt_spin_lock_fastunlock(struct rt_mutex *lock,
  17073. + int (*slowfn)(struct rt_mutex *lock))
  17074. +{
  17075. + if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
  17076. + rt_mutex_deadlock_account_unlock(current);
  17077. + return 0;
  17078. + }
  17079. + return slowfn(lock);
  17080. +}
  17081. +#ifdef CONFIG_SMP
  17082. +/*
  17083. + * Note that owner is a speculative pointer and dereferencing relies
  17084. + * on rcu_read_lock() and the check against the lock owner.
  17085. + */
  17086. +static int adaptive_wait(struct rt_mutex *lock,
  17087. + struct task_struct *owner)
  17088. +{
  17089. + int res = 0;
  17090. +
  17091. + rcu_read_lock();
  17092. + for (;;) {
  17093. + if (owner != rt_mutex_owner(lock))
  17094. + break;
  17095. + /*
  17096. + * Ensure that owner->on_cpu is dereferenced _after_
  17097. + * checking the above to be valid.
  17098. + */
  17099. + barrier();
  17100. + if (!owner->on_cpu) {
  17101. + res = 1;
  17102. + break;
  17103. + }
  17104. + cpu_relax();
  17105. + }
  17106. + rcu_read_unlock();
  17107. + return res;
  17108. +}
  17109. +#else
  17110. +static int adaptive_wait(struct rt_mutex *lock,
  17111. + struct task_struct *orig_owner)
  17112. +{
  17113. + return 1;
  17114. +}
  17115. +#endif
  17116. +
  17117. +static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  17118. + struct rt_mutex_waiter *waiter,
  17119. + struct task_struct *task,
  17120. + enum rtmutex_chainwalk chwalk);
  17121. +/*
  17122. + * Slow path lock function spin_lock style: this variant is very
  17123. + * careful not to miss any non-lock wakeups.
  17124. + *
  17125. + * We store the current state under p->pi_lock in p->saved_state and
  17126. + * the try_to_wake_up() code handles this accordingly.
  17127. + */
  17128. +static void noinline __sched rt_spin_lock_slowlock(struct rt_mutex *lock,
  17129. + bool mg_off)
  17130. +{
  17131. + struct task_struct *lock_owner, *self = current;
  17132. + struct rt_mutex_waiter waiter, *top_waiter;
  17133. + unsigned long flags;
  17134. + int ret;
  17135. +
  17136. + rt_mutex_init_waiter(&waiter, true);
  17137. +
  17138. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  17139. +
  17140. + if (__try_to_take_rt_mutex(lock, self, NULL, STEAL_LATERAL)) {
  17141. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  17142. + return;
  17143. + }
  17144. +
  17145. + BUG_ON(rt_mutex_owner(lock) == self);
  17146. +
  17147. + /*
  17148. + * We save whatever state the task is in and we'll restore it
  17149. + * after acquiring the lock taking real wakeups into account
  17150. + * as well. We are serialized via pi_lock against wakeups. See
  17151. + * try_to_wake_up().
  17152. + */
  17153. + raw_spin_lock(&self->pi_lock);
  17154. + self->saved_state = self->state;
  17155. + __set_current_state_no_track(TASK_UNINTERRUPTIBLE);
  17156. + raw_spin_unlock(&self->pi_lock);
  17157. +
  17158. + ret = task_blocks_on_rt_mutex(lock, &waiter, self, RT_MUTEX_MIN_CHAINWALK);
  17159. + BUG_ON(ret);
  17160. +
  17161. + for (;;) {
  17162. + /* Try to acquire the lock again. */
  17163. + if (__try_to_take_rt_mutex(lock, self, &waiter, STEAL_LATERAL))
  17164. + break;
  17165. +
  17166. + top_waiter = rt_mutex_top_waiter(lock);
  17167. + lock_owner = rt_mutex_owner(lock);
  17168. +
  17169. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  17170. +
  17171. + debug_rt_mutex_print_deadlock(&waiter);
  17172. +
  17173. + if (top_waiter != &waiter || adaptive_wait(lock, lock_owner)) {
  17174. + if (mg_off)
  17175. + migrate_enable();
  17176. + schedule();
  17177. + if (mg_off)
  17178. + migrate_disable();
  17179. + }
  17180. +
  17181. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  17182. +
  17183. + raw_spin_lock(&self->pi_lock);
  17184. + __set_current_state_no_track(TASK_UNINTERRUPTIBLE);
  17185. + raw_spin_unlock(&self->pi_lock);
  17186. + }
  17187. +
  17188. + /*
  17189. + * Restore the task state to current->saved_state. We set it
  17190. + * to the original state above and the try_to_wake_up() code
  17191. + * has possibly updated it when a real (non-rtmutex) wakeup
  17192. + * happened while we were blocked. Clear saved_state so
  17193. + * try_to_wakeup() does not get confused.
  17194. + */
  17195. + raw_spin_lock(&self->pi_lock);
  17196. + __set_current_state_no_track(self->saved_state);
  17197. + self->saved_state = TASK_RUNNING;
  17198. + raw_spin_unlock(&self->pi_lock);
  17199. +
  17200. + /*
  17201. + * try_to_take_rt_mutex() sets the waiter bit
  17202. + * unconditionally. We might have to fix that up:
  17203. + */
  17204. + fixup_rt_mutex_waiters(lock);
  17205. +
  17206. + BUG_ON(rt_mutex_has_waiters(lock) && &waiter == rt_mutex_top_waiter(lock));
  17207. + BUG_ON(!RB_EMPTY_NODE(&waiter.tree_entry));
  17208. +
  17209. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  17210. +
  17211. + debug_rt_mutex_free_waiter(&waiter);
  17212. +}
  17213. +
  17214. +static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
  17215. + struct wake_q_head *wake_sleeper_q,
  17216. + struct rt_mutex *lock);
  17217. +/*
  17218. + * Slow path to release a rt_mutex spin_lock style
  17219. + */
  17220. +static int noinline __sched rt_spin_lock_slowunlock(struct rt_mutex *lock)
  17221. +{
  17222. + unsigned long flags;
  17223. + WAKE_Q(wake_q);
  17224. + WAKE_Q(wake_sleeper_q);
  17225. +
  17226. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  17227. +
  17228. + debug_rt_mutex_unlock(lock);
  17229. +
  17230. + rt_mutex_deadlock_account_unlock(current);
  17231. +
  17232. + if (!rt_mutex_has_waiters(lock)) {
  17233. + lock->owner = NULL;
  17234. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  17235. + return 0;
  17236. + }
  17237. +
  17238. + mark_wakeup_next_waiter(&wake_q, &wake_sleeper_q, lock);
  17239. +
  17240. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  17241. + wake_up_q(&wake_q);
  17242. + wake_up_q_sleeper(&wake_sleeper_q);
  17243. +
  17244. + /* Undo pi boosting.when necessary */
  17245. + rt_mutex_adjust_prio(current);
  17246. + return 0;
  17247. +}
  17248. +
  17249. +static int noinline __sched rt_spin_lock_slowunlock_no_deboost(struct rt_mutex *lock)
  17250. +{
  17251. + unsigned long flags;
  17252. + WAKE_Q(wake_q);
  17253. + WAKE_Q(wake_sleeper_q);
  17254. +
  17255. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  17256. +
  17257. + debug_rt_mutex_unlock(lock);
  17258. +
  17259. + rt_mutex_deadlock_account_unlock(current);
  17260. +
  17261. + if (!rt_mutex_has_waiters(lock)) {
  17262. + lock->owner = NULL;
  17263. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  17264. + return 0;
  17265. + }
  17266. +
  17267. + mark_wakeup_next_waiter(&wake_q, &wake_sleeper_q, lock);
  17268. +
  17269. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  17270. + wake_up_q(&wake_q);
  17271. + wake_up_q_sleeper(&wake_sleeper_q);
  17272. + return 1;
  17273. +}
  17274. +
  17275. +void __lockfunc rt_spin_lock__no_mg(spinlock_t *lock)
  17276. +{
  17277. + rt_spin_lock_fastlock(&lock->lock, rt_spin_lock_slowlock, false);
  17278. + spin_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  17279. +}
  17280. +EXPORT_SYMBOL(rt_spin_lock__no_mg);
  17281. +
  17282. +void __lockfunc rt_spin_lock(spinlock_t *lock)
  17283. +{
  17284. + rt_spin_lock_fastlock(&lock->lock, rt_spin_lock_slowlock, true);
  17285. + spin_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  17286. +}
  17287. +EXPORT_SYMBOL(rt_spin_lock);
  17288. +
  17289. +void __lockfunc __rt_spin_lock(struct rt_mutex *lock)
  17290. +{
  17291. + rt_spin_lock_fastlock(lock, rt_spin_lock_slowlock, true);
  17292. +}
  17293. +EXPORT_SYMBOL(__rt_spin_lock);
  17294. +
  17295. +void __lockfunc __rt_spin_lock__no_mg(struct rt_mutex *lock)
  17296. +{
  17297. + rt_spin_lock_fastlock(lock, rt_spin_lock_slowlock, false);
  17298. +}
  17299. +EXPORT_SYMBOL(__rt_spin_lock__no_mg);
  17300. +
  17301. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  17302. +void __lockfunc rt_spin_lock_nested(spinlock_t *lock, int subclass)
  17303. +{
  17304. + spin_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
  17305. + rt_spin_lock_fastlock(&lock->lock, rt_spin_lock_slowlock, true);
  17306. +}
  17307. +EXPORT_SYMBOL(rt_spin_lock_nested);
  17308. +#endif
  17309. +
  17310. +void __lockfunc rt_spin_unlock__no_mg(spinlock_t *lock)
  17311. +{
  17312. + /* NOTE: we always pass in '1' for nested, for simplicity */
  17313. + spin_release(&lock->dep_map, 1, _RET_IP_);
  17314. + rt_spin_lock_fastunlock(&lock->lock, rt_spin_lock_slowunlock);
  17315. +}
  17316. +EXPORT_SYMBOL(rt_spin_unlock__no_mg);
  17317. +
  17318. +void __lockfunc rt_spin_unlock(spinlock_t *lock)
  17319. +{
  17320. + /* NOTE: we always pass in '1' for nested, for simplicity */
  17321. + spin_release(&lock->dep_map, 1, _RET_IP_);
  17322. + rt_spin_lock_fastunlock(&lock->lock, rt_spin_lock_slowunlock);
  17323. + migrate_enable();
  17324. +}
  17325. +EXPORT_SYMBOL(rt_spin_unlock);
  17326. +
  17327. +int __lockfunc rt_spin_unlock_no_deboost(spinlock_t *lock)
  17328. +{
  17329. + int ret;
  17330. +
  17331. + /* NOTE: we always pass in '1' for nested, for simplicity */
  17332. + spin_release(&lock->dep_map, 1, _RET_IP_);
  17333. + ret = rt_spin_lock_fastunlock(&lock->lock, rt_spin_lock_slowunlock_no_deboost);
  17334. + migrate_enable();
  17335. + return ret;
  17336. +}
  17337. +
  17338. +void __lockfunc __rt_spin_unlock(struct rt_mutex *lock)
  17339. +{
  17340. + rt_spin_lock_fastunlock(lock, rt_spin_lock_slowunlock);
  17341. +}
  17342. +EXPORT_SYMBOL(__rt_spin_unlock);
  17343. +
  17344. +/*
  17345. + * Wait for the lock to get unlocked: instead of polling for an unlock
  17346. + * (like raw spinlocks do), we lock and unlock, to force the kernel to
  17347. + * schedule if there's contention:
  17348. + */
  17349. +void __lockfunc rt_spin_unlock_wait(spinlock_t *lock)
  17350. +{
  17351. + spin_lock(lock);
  17352. + spin_unlock(lock);
  17353. +}
  17354. +EXPORT_SYMBOL(rt_spin_unlock_wait);
  17355. +
  17356. +int __lockfunc __rt_spin_trylock(struct rt_mutex *lock)
  17357. +{
  17358. + return rt_mutex_trylock(lock);
  17359. +}
  17360. +
  17361. +int __lockfunc rt_spin_trylock__no_mg(spinlock_t *lock)
  17362. +{
  17363. + int ret;
  17364. +
  17365. + ret = rt_mutex_trylock(&lock->lock);
  17366. + if (ret)
  17367. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  17368. + return ret;
  17369. +}
  17370. +EXPORT_SYMBOL(rt_spin_trylock__no_mg);
  17371. +
  17372. +int __lockfunc rt_spin_trylock(spinlock_t *lock)
  17373. +{
  17374. + int ret;
  17375. +
  17376. + migrate_disable();
  17377. + ret = rt_mutex_trylock(&lock->lock);
  17378. + if (ret)
  17379. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  17380. + else
  17381. + migrate_enable();
  17382. + return ret;
  17383. +}
  17384. +EXPORT_SYMBOL(rt_spin_trylock);
  17385. +
  17386. +int __lockfunc rt_spin_trylock_bh(spinlock_t *lock)
  17387. +{
  17388. + int ret;
  17389. +
  17390. + local_bh_disable();
  17391. + ret = rt_mutex_trylock(&lock->lock);
  17392. + if (ret) {
  17393. + migrate_disable();
  17394. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  17395. + } else
  17396. + local_bh_enable();
  17397. + return ret;
  17398. +}
  17399. +EXPORT_SYMBOL(rt_spin_trylock_bh);
  17400. +
  17401. +int __lockfunc rt_spin_trylock_irqsave(spinlock_t *lock, unsigned long *flags)
  17402. +{
  17403. + int ret;
  17404. +
  17405. + *flags = 0;
  17406. + ret = rt_mutex_trylock(&lock->lock);
  17407. + if (ret) {
  17408. + migrate_disable();
  17409. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  17410. + }
  17411. + return ret;
  17412. +}
  17413. +EXPORT_SYMBOL(rt_spin_trylock_irqsave);
  17414. +
  17415. +int atomic_dec_and_spin_lock(atomic_t *atomic, spinlock_t *lock)
  17416. +{
  17417. + /* Subtract 1 from counter unless that drops it to 0 (ie. it was 1) */
  17418. + if (atomic_add_unless(atomic, -1, 1))
  17419. + return 0;
  17420. + rt_spin_lock(lock);
  17421. + if (atomic_dec_and_test(atomic))
  17422. + return 1;
  17423. + rt_spin_unlock(lock);
  17424. + return 0;
  17425. +}
  17426. +EXPORT_SYMBOL(atomic_dec_and_spin_lock);
  17427. +
  17428. + void
  17429. +__rt_spin_lock_init(spinlock_t *lock, char *name, struct lock_class_key *key)
  17430. +{
  17431. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  17432. + /*
  17433. + * Make sure we are not reinitializing a held lock:
  17434. + */
  17435. + debug_check_no_locks_freed((void *)lock, sizeof(*lock));
  17436. + lockdep_init_map(&lock->dep_map, name, key, 0);
  17437. +#endif
  17438. +}
  17439. +EXPORT_SYMBOL(__rt_spin_lock_init);
  17440. +
  17441. +#endif /* PREEMPT_RT_FULL */
  17442. +
  17443. +#ifdef CONFIG_PREEMPT_RT_FULL
  17444. + static inline int __sched
  17445. +__mutex_lock_check_stamp(struct rt_mutex *lock, struct ww_acquire_ctx *ctx)
  17446. +{
  17447. + struct ww_mutex *ww = container_of(lock, struct ww_mutex, base.lock);
  17448. + struct ww_acquire_ctx *hold_ctx = ACCESS_ONCE(ww->ctx);
  17449. +
  17450. + if (!hold_ctx)
  17451. + return 0;
  17452. +
  17453. + if (unlikely(ctx == hold_ctx))
  17454. + return -EALREADY;
  17455. +
  17456. + if (ctx->stamp - hold_ctx->stamp <= LONG_MAX &&
  17457. + (ctx->stamp != hold_ctx->stamp || ctx > hold_ctx)) {
  17458. +#ifdef CONFIG_DEBUG_MUTEXES
  17459. + DEBUG_LOCKS_WARN_ON(ctx->contending_lock);
  17460. + ctx->contending_lock = ww;
  17461. +#endif
  17462. + return -EDEADLK;
  17463. + }
  17464. +
  17465. + return 0;
  17466. +}
  17467. +#else
  17468. + static inline int __sched
  17469. +__mutex_lock_check_stamp(struct rt_mutex *lock, struct ww_acquire_ctx *ctx)
  17470. +{
  17471. + BUG();
  17472. + return 0;
  17473. +}
  17474. +
  17475. +#endif
  17476. +
  17477. +static inline int
  17478. +try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  17479. + struct rt_mutex_waiter *waiter)
  17480. +{
  17481. + return __try_to_take_rt_mutex(lock, task, waiter, STEAL_NORMAL);
  17482. +}
  17483. +
  17484. /*
  17485. * Task blocks on lock.
  17486. *
  17487. * Prepare waiter and propagate pi chain
  17488. *
  17489. - * This must be called with lock->wait_lock held.
  17490. + * This must be called with lock->wait_lock held and interrupts disabled
  17491. */
  17492. static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  17493. struct rt_mutex_waiter *waiter,
  17494. @@ -958,7 +1431,6 @@
  17495. struct rt_mutex_waiter *top_waiter = waiter;
  17496. struct rt_mutex *next_lock;
  17497. int chain_walk = 0, res;
  17498. - unsigned long flags;
  17499. /*
  17500. * Early deadlock detection. We really don't want the task to
  17501. @@ -972,7 +1444,24 @@
  17502. if (owner == task)
  17503. return -EDEADLK;
  17504. - raw_spin_lock_irqsave(&task->pi_lock, flags);
  17505. + raw_spin_lock(&task->pi_lock);
  17506. +
  17507. + /*
  17508. + * In the case of futex requeue PI, this will be a proxy
  17509. + * lock. The task will wake unaware that it is enqueueed on
  17510. + * this lock. Avoid blocking on two locks and corrupting
  17511. + * pi_blocked_on via the PI_WAKEUP_INPROGRESS
  17512. + * flag. futex_wait_requeue_pi() sets this when it wakes up
  17513. + * before requeue (due to a signal or timeout). Do not enqueue
  17514. + * the task if PI_WAKEUP_INPROGRESS is set.
  17515. + */
  17516. + if (task != current && task->pi_blocked_on == PI_WAKEUP_INPROGRESS) {
  17517. + raw_spin_unlock(&task->pi_lock);
  17518. + return -EAGAIN;
  17519. + }
  17520. +
  17521. + BUG_ON(rt_mutex_real_waiter(task->pi_blocked_on));
  17522. +
  17523. __rt_mutex_adjust_prio(task);
  17524. waiter->task = task;
  17525. waiter->lock = lock;
  17526. @@ -985,18 +1474,18 @@
  17527. task->pi_blocked_on = waiter;
  17528. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  17529. + raw_spin_unlock(&task->pi_lock);
  17530. if (!owner)
  17531. return 0;
  17532. - raw_spin_lock_irqsave(&owner->pi_lock, flags);
  17533. + raw_spin_lock(&owner->pi_lock);
  17534. if (waiter == rt_mutex_top_waiter(lock)) {
  17535. rt_mutex_dequeue_pi(owner, top_waiter);
  17536. rt_mutex_enqueue_pi(owner, waiter);
  17537. __rt_mutex_adjust_prio(owner);
  17538. - if (owner->pi_blocked_on)
  17539. + if (rt_mutex_real_waiter(owner->pi_blocked_on))
  17540. chain_walk = 1;
  17541. } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
  17542. chain_walk = 1;
  17543. @@ -1005,7 +1494,7 @@
  17544. /* Store the lock on which owner is blocked or NULL */
  17545. next_lock = task_blocked_on_lock(owner);
  17546. - raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
  17547. + raw_spin_unlock(&owner->pi_lock);
  17548. /*
  17549. * Even if full deadlock detection is on, if the owner is not
  17550. * blocked itself, we can avoid finding this out in the chain
  17551. @@ -1021,12 +1510,12 @@
  17552. */
  17553. get_task_struct(owner);
  17554. - raw_spin_unlock(&lock->wait_lock);
  17555. + raw_spin_unlock_irq(&lock->wait_lock);
  17556. res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
  17557. next_lock, waiter, task);
  17558. - raw_spin_lock(&lock->wait_lock);
  17559. + raw_spin_lock_irq(&lock->wait_lock);
  17560. return res;
  17561. }
  17562. @@ -1035,15 +1524,15 @@
  17563. * Remove the top waiter from the current tasks pi waiter tree and
  17564. * queue it up.
  17565. *
  17566. - * Called with lock->wait_lock held.
  17567. + * Called with lock->wait_lock held and interrupts disabled.
  17568. */
  17569. static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
  17570. + struct wake_q_head *wake_sleeper_q,
  17571. struct rt_mutex *lock)
  17572. {
  17573. struct rt_mutex_waiter *waiter;
  17574. - unsigned long flags;
  17575. - raw_spin_lock_irqsave(&current->pi_lock, flags);
  17576. + raw_spin_lock(&current->pi_lock);
  17577. waiter = rt_mutex_top_waiter(lock);
  17578. @@ -1065,15 +1554,18 @@
  17579. */
  17580. lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
  17581. - raw_spin_unlock_irqrestore(&current->pi_lock, flags);
  17582. + raw_spin_unlock(&current->pi_lock);
  17583. - wake_q_add(wake_q, waiter->task);
  17584. + if (waiter->savestate)
  17585. + wake_q_add(wake_sleeper_q, waiter->task);
  17586. + else
  17587. + wake_q_add(wake_q, waiter->task);
  17588. }
  17589. /*
  17590. * Remove a waiter from a lock and give up
  17591. *
  17592. - * Must be called with lock->wait_lock held and
  17593. + * Must be called with lock->wait_lock held and interrupts disabled. I must
  17594. * have just failed to try_to_take_rt_mutex().
  17595. */
  17596. static void remove_waiter(struct rt_mutex *lock,
  17597. @@ -1081,13 +1573,12 @@
  17598. {
  17599. bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
  17600. struct task_struct *owner = rt_mutex_owner(lock);
  17601. - struct rt_mutex *next_lock;
  17602. - unsigned long flags;
  17603. + struct rt_mutex *next_lock = NULL;
  17604. - raw_spin_lock_irqsave(&current->pi_lock, flags);
  17605. + raw_spin_lock(&current->pi_lock);
  17606. rt_mutex_dequeue(lock, waiter);
  17607. current->pi_blocked_on = NULL;
  17608. - raw_spin_unlock_irqrestore(&current->pi_lock, flags);
  17609. + raw_spin_unlock(&current->pi_lock);
  17610. /*
  17611. * Only update priority if the waiter was the highest priority
  17612. @@ -1096,7 +1587,7 @@
  17613. if (!owner || !is_top_waiter)
  17614. return;
  17615. - raw_spin_lock_irqsave(&owner->pi_lock, flags);
  17616. + raw_spin_lock(&owner->pi_lock);
  17617. rt_mutex_dequeue_pi(owner, waiter);
  17618. @@ -1106,9 +1597,10 @@
  17619. __rt_mutex_adjust_prio(owner);
  17620. /* Store the lock on which owner is blocked or NULL */
  17621. - next_lock = task_blocked_on_lock(owner);
  17622. + if (rt_mutex_real_waiter(owner->pi_blocked_on))
  17623. + next_lock = task_blocked_on_lock(owner);
  17624. - raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
  17625. + raw_spin_unlock(&owner->pi_lock);
  17626. /*
  17627. * Don't walk the chain, if the owner task is not blocked
  17628. @@ -1120,12 +1612,12 @@
  17629. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  17630. get_task_struct(owner);
  17631. - raw_spin_unlock(&lock->wait_lock);
  17632. + raw_spin_unlock_irq(&lock->wait_lock);
  17633. rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
  17634. next_lock, NULL, current);
  17635. - raw_spin_lock(&lock->wait_lock);
  17636. + raw_spin_lock_irq(&lock->wait_lock);
  17637. }
  17638. /*
  17639. @@ -1142,17 +1634,17 @@
  17640. raw_spin_lock_irqsave(&task->pi_lock, flags);
  17641. waiter = task->pi_blocked_on;
  17642. - if (!waiter || (waiter->prio == task->prio &&
  17643. + if (!rt_mutex_real_waiter(waiter) || (waiter->prio == task->prio &&
  17644. !dl_prio(task->prio))) {
  17645. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  17646. return;
  17647. }
  17648. next_lock = waiter->lock;
  17649. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  17650. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  17651. get_task_struct(task);
  17652. + raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  17653. rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
  17654. next_lock, NULL, task);
  17655. }
  17656. @@ -1161,16 +1653,17 @@
  17657. * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
  17658. * @lock: the rt_mutex to take
  17659. * @state: the state the task should block in (TASK_INTERRUPTIBLE
  17660. - * or TASK_UNINTERRUPTIBLE)
  17661. + * or TASK_UNINTERRUPTIBLE)
  17662. * @timeout: the pre-initialized and started timer, or NULL for none
  17663. * @waiter: the pre-initialized rt_mutex_waiter
  17664. *
  17665. - * lock->wait_lock must be held by the caller.
  17666. + * Must be called with lock->wait_lock held and interrupts disabled
  17667. */
  17668. static int __sched
  17669. __rt_mutex_slowlock(struct rt_mutex *lock, int state,
  17670. struct hrtimer_sleeper *timeout,
  17671. - struct rt_mutex_waiter *waiter)
  17672. + struct rt_mutex_waiter *waiter,
  17673. + struct ww_acquire_ctx *ww_ctx)
  17674. {
  17675. int ret = 0;
  17676. @@ -1193,13 +1686,19 @@
  17677. break;
  17678. }
  17679. - raw_spin_unlock(&lock->wait_lock);
  17680. + if (ww_ctx && ww_ctx->acquired > 0) {
  17681. + ret = __mutex_lock_check_stamp(lock, ww_ctx);
  17682. + if (ret)
  17683. + break;
  17684. + }
  17685. +
  17686. + raw_spin_unlock_irq(&lock->wait_lock);
  17687. debug_rt_mutex_print_deadlock(waiter);
  17688. schedule();
  17689. - raw_spin_lock(&lock->wait_lock);
  17690. + raw_spin_lock_irq(&lock->wait_lock);
  17691. set_current_state(state);
  17692. }
  17693. @@ -1227,26 +1726,112 @@
  17694. }
  17695. }
  17696. +static __always_inline void ww_mutex_lock_acquired(struct ww_mutex *ww,
  17697. + struct ww_acquire_ctx *ww_ctx)
  17698. +{
  17699. +#ifdef CONFIG_DEBUG_MUTEXES
  17700. + /*
  17701. + * If this WARN_ON triggers, you used ww_mutex_lock to acquire,
  17702. + * but released with a normal mutex_unlock in this call.
  17703. + *
  17704. + * This should never happen, always use ww_mutex_unlock.
  17705. + */
  17706. + DEBUG_LOCKS_WARN_ON(ww->ctx);
  17707. +
  17708. + /*
  17709. + * Not quite done after calling ww_acquire_done() ?
  17710. + */
  17711. + DEBUG_LOCKS_WARN_ON(ww_ctx->done_acquire);
  17712. +
  17713. + if (ww_ctx->contending_lock) {
  17714. + /*
  17715. + * After -EDEADLK you tried to
  17716. + * acquire a different ww_mutex? Bad!
  17717. + */
  17718. + DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock != ww);
  17719. +
  17720. + /*
  17721. + * You called ww_mutex_lock after receiving -EDEADLK,
  17722. + * but 'forgot' to unlock everything else first?
  17723. + */
  17724. + DEBUG_LOCKS_WARN_ON(ww_ctx->acquired > 0);
  17725. + ww_ctx->contending_lock = NULL;
  17726. + }
  17727. +
  17728. + /*
  17729. + * Naughty, using a different class will lead to undefined behavior!
  17730. + */
  17731. + DEBUG_LOCKS_WARN_ON(ww_ctx->ww_class != ww->ww_class);
  17732. +#endif
  17733. + ww_ctx->acquired++;
  17734. +}
  17735. +
  17736. +#ifdef CONFIG_PREEMPT_RT_FULL
  17737. +static void ww_mutex_account_lock(struct rt_mutex *lock,
  17738. + struct ww_acquire_ctx *ww_ctx)
  17739. +{
  17740. + struct ww_mutex *ww = container_of(lock, struct ww_mutex, base.lock);
  17741. + struct rt_mutex_waiter *waiter, *n;
  17742. +
  17743. + /*
  17744. + * This branch gets optimized out for the common case,
  17745. + * and is only important for ww_mutex_lock.
  17746. + */
  17747. + ww_mutex_lock_acquired(ww, ww_ctx);
  17748. + ww->ctx = ww_ctx;
  17749. +
  17750. + /*
  17751. + * Give any possible sleeping processes the chance to wake up,
  17752. + * so they can recheck if they have to back off.
  17753. + */
  17754. + rbtree_postorder_for_each_entry_safe(waiter, n, &lock->waiters,
  17755. + tree_entry) {
  17756. + /* XXX debug rt mutex waiter wakeup */
  17757. +
  17758. + BUG_ON(waiter->lock != lock);
  17759. + rt_mutex_wake_waiter(waiter);
  17760. + }
  17761. +}
  17762. +
  17763. +#else
  17764. +
  17765. +static void ww_mutex_account_lock(struct rt_mutex *lock,
  17766. + struct ww_acquire_ctx *ww_ctx)
  17767. +{
  17768. + BUG();
  17769. +}
  17770. +#endif
  17771. +
  17772. /*
  17773. * Slow path lock function:
  17774. */
  17775. static int __sched
  17776. rt_mutex_slowlock(struct rt_mutex *lock, int state,
  17777. struct hrtimer_sleeper *timeout,
  17778. - enum rtmutex_chainwalk chwalk)
  17779. + enum rtmutex_chainwalk chwalk,
  17780. + struct ww_acquire_ctx *ww_ctx)
  17781. {
  17782. struct rt_mutex_waiter waiter;
  17783. + unsigned long flags;
  17784. int ret = 0;
  17785. - debug_rt_mutex_init_waiter(&waiter);
  17786. - RB_CLEAR_NODE(&waiter.pi_tree_entry);
  17787. - RB_CLEAR_NODE(&waiter.tree_entry);
  17788. + rt_mutex_init_waiter(&waiter, false);
  17789. - raw_spin_lock(&lock->wait_lock);
  17790. + /*
  17791. + * Technically we could use raw_spin_[un]lock_irq() here, but this can
  17792. + * be called in early boot if the cmpxchg() fast path is disabled
  17793. + * (debug, no architecture support). In this case we will acquire the
  17794. + * rtmutex with lock->wait_lock held. But we cannot unconditionally
  17795. + * enable interrupts in that early boot case. So we need to use the
  17796. + * irqsave/restore variants.
  17797. + */
  17798. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  17799. /* Try to acquire the lock again: */
  17800. if (try_to_take_rt_mutex(lock, current, NULL)) {
  17801. - raw_spin_unlock(&lock->wait_lock);
  17802. + if (ww_ctx)
  17803. + ww_mutex_account_lock(lock, ww_ctx);
  17804. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  17805. return 0;
  17806. }
  17807. @@ -1260,13 +1845,23 @@
  17808. if (likely(!ret))
  17809. /* sleep on the mutex */
  17810. - ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
  17811. + ret = __rt_mutex_slowlock(lock, state, timeout, &waiter,
  17812. + ww_ctx);
  17813. + else if (ww_ctx) {
  17814. + /* ww_mutex received EDEADLK, let it become EALREADY */
  17815. + ret = __mutex_lock_check_stamp(lock, ww_ctx);
  17816. + BUG_ON(!ret);
  17817. + }
  17818. if (unlikely(ret)) {
  17819. __set_current_state(TASK_RUNNING);
  17820. if (rt_mutex_has_waiters(lock))
  17821. remove_waiter(lock, &waiter);
  17822. - rt_mutex_handle_deadlock(ret, chwalk, &waiter);
  17823. + /* ww_mutex want to report EDEADLK/EALREADY, let them */
  17824. + if (!ww_ctx)
  17825. + rt_mutex_handle_deadlock(ret, chwalk, &waiter);
  17826. + } else if (ww_ctx) {
  17827. + ww_mutex_account_lock(lock, ww_ctx);
  17828. }
  17829. /*
  17830. @@ -1275,7 +1870,7 @@
  17831. */
  17832. fixup_rt_mutex_waiters(lock);
  17833. - raw_spin_unlock(&lock->wait_lock);
  17834. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  17835. /* Remove pending timer: */
  17836. if (unlikely(timeout))
  17837. @@ -1291,6 +1886,7 @@
  17838. */
  17839. static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
  17840. {
  17841. + unsigned long flags;
  17842. int ret;
  17843. /*
  17844. @@ -1302,10 +1898,10 @@
  17845. return 0;
  17846. /*
  17847. - * The mutex has currently no owner. Lock the wait lock and
  17848. - * try to acquire the lock.
  17849. + * The mutex has currently no owner. Lock the wait lock and try to
  17850. + * acquire the lock. We use irqsave here to support early boot calls.
  17851. */
  17852. - raw_spin_lock(&lock->wait_lock);
  17853. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  17854. ret = try_to_take_rt_mutex(lock, current, NULL);
  17855. @@ -1315,7 +1911,7 @@
  17856. */
  17857. fixup_rt_mutex_waiters(lock);
  17858. - raw_spin_unlock(&lock->wait_lock);
  17859. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  17860. return ret;
  17861. }
  17862. @@ -1325,9 +1921,13 @@
  17863. * Return whether the current task needs to undo a potential priority boosting.
  17864. */
  17865. static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
  17866. - struct wake_q_head *wake_q)
  17867. + struct wake_q_head *wake_q,
  17868. + struct wake_q_head *wake_sleeper_q)
  17869. {
  17870. - raw_spin_lock(&lock->wait_lock);
  17871. + unsigned long flags;
  17872. +
  17873. + /* irqsave required to support early boot calls */
  17874. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  17875. debug_rt_mutex_unlock(lock);
  17876. @@ -1366,10 +1966,10 @@
  17877. */
  17878. while (!rt_mutex_has_waiters(lock)) {
  17879. /* Drops lock->wait_lock ! */
  17880. - if (unlock_rt_mutex_safe(lock) == true)
  17881. + if (unlock_rt_mutex_safe(lock, flags) == true)
  17882. return false;
  17883. /* Relock the rtmutex and try again */
  17884. - raw_spin_lock(&lock->wait_lock);
  17885. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  17886. }
  17887. /*
  17888. @@ -1378,9 +1978,9 @@
  17889. *
  17890. * Queue the next waiter for wakeup once we release the wait_lock.
  17891. */
  17892. - mark_wakeup_next_waiter(wake_q, lock);
  17893. + mark_wakeup_next_waiter(wake_q, wake_sleeper_q, lock);
  17894. - raw_spin_unlock(&lock->wait_lock);
  17895. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  17896. /* check PI boosting */
  17897. return true;
  17898. @@ -1394,31 +1994,36 @@
  17899. */
  17900. static inline int
  17901. rt_mutex_fastlock(struct rt_mutex *lock, int state,
  17902. + struct ww_acquire_ctx *ww_ctx,
  17903. int (*slowfn)(struct rt_mutex *lock, int state,
  17904. struct hrtimer_sleeper *timeout,
  17905. - enum rtmutex_chainwalk chwalk))
  17906. + enum rtmutex_chainwalk chwalk,
  17907. + struct ww_acquire_ctx *ww_ctx))
  17908. {
  17909. if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
  17910. rt_mutex_deadlock_account_lock(lock, current);
  17911. return 0;
  17912. } else
  17913. - return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
  17914. + return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK,
  17915. + ww_ctx);
  17916. }
  17917. static inline int
  17918. rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
  17919. struct hrtimer_sleeper *timeout,
  17920. enum rtmutex_chainwalk chwalk,
  17921. + struct ww_acquire_ctx *ww_ctx,
  17922. int (*slowfn)(struct rt_mutex *lock, int state,
  17923. struct hrtimer_sleeper *timeout,
  17924. - enum rtmutex_chainwalk chwalk))
  17925. + enum rtmutex_chainwalk chwalk,
  17926. + struct ww_acquire_ctx *ww_ctx))
  17927. {
  17928. if (chwalk == RT_MUTEX_MIN_CHAINWALK &&
  17929. likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
  17930. rt_mutex_deadlock_account_lock(lock, current);
  17931. return 0;
  17932. } else
  17933. - return slowfn(lock, state, timeout, chwalk);
  17934. + return slowfn(lock, state, timeout, chwalk, ww_ctx);
  17935. }
  17936. static inline int
  17937. @@ -1435,17 +2040,20 @@
  17938. static inline void
  17939. rt_mutex_fastunlock(struct rt_mutex *lock,
  17940. bool (*slowfn)(struct rt_mutex *lock,
  17941. - struct wake_q_head *wqh))
  17942. + struct wake_q_head *wqh,
  17943. + struct wake_q_head *wq_sleeper))
  17944. {
  17945. WAKE_Q(wake_q);
  17946. + WAKE_Q(wake_sleeper_q);
  17947. if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
  17948. rt_mutex_deadlock_account_unlock(current);
  17949. } else {
  17950. - bool deboost = slowfn(lock, &wake_q);
  17951. + bool deboost = slowfn(lock, &wake_q, &wake_sleeper_q);
  17952. wake_up_q(&wake_q);
  17953. + wake_up_q_sleeper(&wake_sleeper_q);
  17954. /* Undo pi boosting if necessary: */
  17955. if (deboost)
  17956. @@ -1462,7 +2070,7 @@
  17957. {
  17958. might_sleep();
  17959. - rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
  17960. + rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, NULL, rt_mutex_slowlock);
  17961. }
  17962. EXPORT_SYMBOL_GPL(rt_mutex_lock);
  17963. @@ -1479,7 +2087,7 @@
  17964. {
  17965. might_sleep();
  17966. - return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
  17967. + return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, NULL, rt_mutex_slowlock);
  17968. }
  17969. EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
  17970. @@ -1492,11 +2100,30 @@
  17971. might_sleep();
  17972. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  17973. - RT_MUTEX_FULL_CHAINWALK,
  17974. + RT_MUTEX_FULL_CHAINWALK, NULL,
  17975. rt_mutex_slowlock);
  17976. }
  17977. /**
  17978. + * rt_mutex_lock_killable - lock a rt_mutex killable
  17979. + *
  17980. + * @lock: the rt_mutex to be locked
  17981. + * @detect_deadlock: deadlock detection on/off
  17982. + *
  17983. + * Returns:
  17984. + * 0 on success
  17985. + * -EINTR when interrupted by a signal
  17986. + * -EDEADLK when the lock would deadlock (when deadlock detection is on)
  17987. + */
  17988. +int __sched rt_mutex_lock_killable(struct rt_mutex *lock)
  17989. +{
  17990. + might_sleep();
  17991. +
  17992. + return rt_mutex_fastlock(lock, TASK_KILLABLE, NULL, rt_mutex_slowlock);
  17993. +}
  17994. +EXPORT_SYMBOL_GPL(rt_mutex_lock_killable);
  17995. +
  17996. +/**
  17997. * rt_mutex_timed_lock - lock a rt_mutex interruptible
  17998. * the timeout structure is provided
  17999. * by the caller
  18000. @@ -1516,6 +2143,7 @@
  18001. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  18002. RT_MUTEX_MIN_CHAINWALK,
  18003. + NULL,
  18004. rt_mutex_slowlock);
  18005. }
  18006. EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
  18007. @@ -1533,7 +2161,11 @@
  18008. */
  18009. int __sched rt_mutex_trylock(struct rt_mutex *lock)
  18010. {
  18011. +#ifdef CONFIG_PREEMPT_RT_FULL
  18012. + if (WARN_ON_ONCE(in_irq() || in_nmi()))
  18013. +#else
  18014. if (WARN_ON(in_irq() || in_nmi() || in_serving_softirq()))
  18015. +#endif
  18016. return 0;
  18017. return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
  18018. @@ -1559,13 +2191,14 @@
  18019. * required or not.
  18020. */
  18021. bool __sched rt_mutex_futex_unlock(struct rt_mutex *lock,
  18022. - struct wake_q_head *wqh)
  18023. + struct wake_q_head *wqh,
  18024. + struct wake_q_head *wq_sleeper)
  18025. {
  18026. if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
  18027. rt_mutex_deadlock_account_unlock(current);
  18028. return false;
  18029. }
  18030. - return rt_mutex_slowunlock(lock, wqh);
  18031. + return rt_mutex_slowunlock(lock, wqh, wq_sleeper);
  18032. }
  18033. /**
  18034. @@ -1598,13 +2231,12 @@
  18035. void __rt_mutex_init(struct rt_mutex *lock, const char *name)
  18036. {
  18037. lock->owner = NULL;
  18038. - raw_spin_lock_init(&lock->wait_lock);
  18039. lock->waiters = RB_ROOT;
  18040. lock->waiters_leftmost = NULL;
  18041. debug_rt_mutex_init(lock, name);
  18042. }
  18043. -EXPORT_SYMBOL_GPL(__rt_mutex_init);
  18044. +EXPORT_SYMBOL(__rt_mutex_init);
  18045. /**
  18046. * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
  18047. @@ -1619,7 +2251,7 @@
  18048. void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
  18049. struct task_struct *proxy_owner)
  18050. {
  18051. - __rt_mutex_init(lock, NULL);
  18052. + rt_mutex_init(lock);
  18053. debug_rt_mutex_proxy_lock(lock, proxy_owner);
  18054. rt_mutex_set_owner(lock, proxy_owner);
  18055. rt_mutex_deadlock_account_lock(lock, proxy_owner);
  18056. @@ -1660,13 +2292,42 @@
  18057. {
  18058. int ret;
  18059. - raw_spin_lock(&lock->wait_lock);
  18060. + raw_spin_lock_irq(&lock->wait_lock);
  18061. if (try_to_take_rt_mutex(lock, task, NULL)) {
  18062. - raw_spin_unlock(&lock->wait_lock);
  18063. + raw_spin_unlock_irq(&lock->wait_lock);
  18064. return 1;
  18065. }
  18066. +#ifdef CONFIG_PREEMPT_RT_FULL
  18067. + /*
  18068. + * In PREEMPT_RT there's an added race.
  18069. + * If the task, that we are about to requeue, times out,
  18070. + * it can set the PI_WAKEUP_INPROGRESS. This tells the requeue
  18071. + * to skip this task. But right after the task sets
  18072. + * its pi_blocked_on to PI_WAKEUP_INPROGRESS it can then
  18073. + * block on the spin_lock(&hb->lock), which in RT is an rtmutex.
  18074. + * This will replace the PI_WAKEUP_INPROGRESS with the actual
  18075. + * lock that it blocks on. We *must not* place this task
  18076. + * on this proxy lock in that case.
  18077. + *
  18078. + * To prevent this race, we first take the task's pi_lock
  18079. + * and check if it has updated its pi_blocked_on. If it has,
  18080. + * we assume that it woke up and we return -EAGAIN.
  18081. + * Otherwise, we set the task's pi_blocked_on to
  18082. + * PI_REQUEUE_INPROGRESS, so that if the task is waking up
  18083. + * it will know that we are in the process of requeuing it.
  18084. + */
  18085. + raw_spin_lock(&task->pi_lock);
  18086. + if (task->pi_blocked_on) {
  18087. + raw_spin_unlock(&task->pi_lock);
  18088. + raw_spin_unlock_irq(&lock->wait_lock);
  18089. + return -EAGAIN;
  18090. + }
  18091. + task->pi_blocked_on = PI_REQUEUE_INPROGRESS;
  18092. + raw_spin_unlock(&task->pi_lock);
  18093. +#endif
  18094. +
  18095. /* We enforce deadlock detection for futexes */
  18096. ret = task_blocks_on_rt_mutex(lock, waiter, task,
  18097. RT_MUTEX_FULL_CHAINWALK);
  18098. @@ -1681,10 +2342,10 @@
  18099. ret = 0;
  18100. }
  18101. - if (unlikely(ret))
  18102. + if (ret && rt_mutex_has_waiters(lock))
  18103. remove_waiter(lock, waiter);
  18104. - raw_spin_unlock(&lock->wait_lock);
  18105. + raw_spin_unlock_irq(&lock->wait_lock);
  18106. debug_rt_mutex_print_deadlock(waiter);
  18107. @@ -1732,12 +2393,12 @@
  18108. {
  18109. int ret;
  18110. - raw_spin_lock(&lock->wait_lock);
  18111. + raw_spin_lock_irq(&lock->wait_lock);
  18112. set_current_state(TASK_INTERRUPTIBLE);
  18113. /* sleep on the mutex */
  18114. - ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
  18115. + ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter, NULL);
  18116. if (unlikely(ret))
  18117. remove_waiter(lock, waiter);
  18118. @@ -1748,7 +2409,93 @@
  18119. */
  18120. fixup_rt_mutex_waiters(lock);
  18121. - raw_spin_unlock(&lock->wait_lock);
  18122. + raw_spin_unlock_irq(&lock->wait_lock);
  18123. +
  18124. + return ret;
  18125. +}
  18126. +
  18127. +static inline int
  18128. +ww_mutex_deadlock_injection(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
  18129. +{
  18130. +#ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
  18131. + unsigned tmp;
  18132. +
  18133. + if (ctx->deadlock_inject_countdown-- == 0) {
  18134. + tmp = ctx->deadlock_inject_interval;
  18135. + if (tmp > UINT_MAX/4)
  18136. + tmp = UINT_MAX;
  18137. + else
  18138. + tmp = tmp*2 + tmp + tmp/2;
  18139. +
  18140. + ctx->deadlock_inject_interval = tmp;
  18141. + ctx->deadlock_inject_countdown = tmp;
  18142. + ctx->contending_lock = lock;
  18143. +
  18144. + ww_mutex_unlock(lock);
  18145. +
  18146. + return -EDEADLK;
  18147. + }
  18148. +#endif
  18149. +
  18150. + return 0;
  18151. +}
  18152. +
  18153. +#ifdef CONFIG_PREEMPT_RT_FULL
  18154. +int __sched
  18155. +__ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ww_ctx)
  18156. +{
  18157. + int ret;
  18158. +
  18159. + might_sleep();
  18160. +
  18161. + mutex_acquire_nest(&lock->base.dep_map, 0, 0, &ww_ctx->dep_map, _RET_IP_);
  18162. + ret = rt_mutex_slowlock(&lock->base.lock, TASK_INTERRUPTIBLE, NULL, 0, ww_ctx);
  18163. + if (ret)
  18164. + mutex_release(&lock->base.dep_map, 1, _RET_IP_);
  18165. + else if (!ret && ww_ctx->acquired > 1)
  18166. + return ww_mutex_deadlock_injection(lock, ww_ctx);
  18167. return ret;
  18168. }
  18169. +EXPORT_SYMBOL_GPL(__ww_mutex_lock_interruptible);
  18170. +
  18171. +int __sched
  18172. +__ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ww_ctx)
  18173. +{
  18174. + int ret;
  18175. +
  18176. + might_sleep();
  18177. +
  18178. + mutex_acquire_nest(&lock->base.dep_map, 0, 0, &ww_ctx->dep_map, _RET_IP_);
  18179. + ret = rt_mutex_slowlock(&lock->base.lock, TASK_UNINTERRUPTIBLE, NULL, 0, ww_ctx);
  18180. + if (ret)
  18181. + mutex_release(&lock->base.dep_map, 1, _RET_IP_);
  18182. + else if (!ret && ww_ctx->acquired > 1)
  18183. + return ww_mutex_deadlock_injection(lock, ww_ctx);
  18184. +
  18185. + return ret;
  18186. +}
  18187. +EXPORT_SYMBOL_GPL(__ww_mutex_lock);
  18188. +
  18189. +void __sched ww_mutex_unlock(struct ww_mutex *lock)
  18190. +{
  18191. + int nest = !!lock->ctx;
  18192. +
  18193. + /*
  18194. + * The unlocking fastpath is the 0->1 transition from 'locked'
  18195. + * into 'unlocked' state:
  18196. + */
  18197. + if (nest) {
  18198. +#ifdef CONFIG_DEBUG_MUTEXES
  18199. + DEBUG_LOCKS_WARN_ON(!lock->ctx->acquired);
  18200. +#endif
  18201. + if (lock->ctx->acquired > 0)
  18202. + lock->ctx->acquired--;
  18203. + lock->ctx = NULL;
  18204. + }
  18205. +
  18206. + mutex_release(&lock->base.dep_map, nest, _RET_IP_);
  18207. + rt_mutex_unlock(&lock->base.lock);
  18208. +}
  18209. +EXPORT_SYMBOL(ww_mutex_unlock);
  18210. +#endif
  18211. diff -Nur linux-4.4.46.orig/kernel/locking/rtmutex_common.h linux-4.4.46/kernel/locking/rtmutex_common.h
  18212. --- linux-4.4.46.orig/kernel/locking/rtmutex_common.h 2017-02-01 08:31:11.000000000 +0100
  18213. +++ linux-4.4.46/kernel/locking/rtmutex_common.h 2017-02-03 17:18:10.927619058 +0100
  18214. @@ -27,6 +27,7 @@
  18215. struct rb_node pi_tree_entry;
  18216. struct task_struct *task;
  18217. struct rt_mutex *lock;
  18218. + bool savestate;
  18219. #ifdef CONFIG_DEBUG_RT_MUTEXES
  18220. unsigned long ip;
  18221. struct pid *deadlock_task_pid;
  18222. @@ -98,6 +99,9 @@
  18223. /*
  18224. * PI-futex support (proxy locking functions, etc.):
  18225. */
  18226. +#define PI_WAKEUP_INPROGRESS ((struct rt_mutex_waiter *) 1)
  18227. +#define PI_REQUEUE_INPROGRESS ((struct rt_mutex_waiter *) 2)
  18228. +
  18229. extern struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock);
  18230. extern void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
  18231. struct task_struct *proxy_owner);
  18232. @@ -111,7 +115,8 @@
  18233. struct rt_mutex_waiter *waiter);
  18234. extern int rt_mutex_timed_futex_lock(struct rt_mutex *l, struct hrtimer_sleeper *to);
  18235. extern bool rt_mutex_futex_unlock(struct rt_mutex *lock,
  18236. - struct wake_q_head *wqh);
  18237. + struct wake_q_head *wqh,
  18238. + struct wake_q_head *wq_sleeper);
  18239. extern void rt_mutex_adjust_prio(struct task_struct *task);
  18240. #ifdef CONFIG_DEBUG_RT_MUTEXES
  18241. @@ -120,4 +125,14 @@
  18242. # include "rtmutex.h"
  18243. #endif
  18244. +static inline void
  18245. +rt_mutex_init_waiter(struct rt_mutex_waiter *waiter, bool savestate)
  18246. +{
  18247. + debug_rt_mutex_init_waiter(waiter);
  18248. + waiter->task = NULL;
  18249. + waiter->savestate = savestate;
  18250. + RB_CLEAR_NODE(&waiter->pi_tree_entry);
  18251. + RB_CLEAR_NODE(&waiter->tree_entry);
  18252. +}
  18253. +
  18254. #endif
  18255. diff -Nur linux-4.4.46.orig/kernel/locking/spinlock.c linux-4.4.46/kernel/locking/spinlock.c
  18256. --- linux-4.4.46.orig/kernel/locking/spinlock.c 2017-02-01 08:31:11.000000000 +0100
  18257. +++ linux-4.4.46/kernel/locking/spinlock.c 2017-02-03 17:18:10.927619058 +0100
  18258. @@ -124,8 +124,11 @@
  18259. * __[spin|read|write]_lock_bh()
  18260. */
  18261. BUILD_LOCK_OPS(spin, raw_spinlock);
  18262. +
  18263. +#ifndef CONFIG_PREEMPT_RT_FULL
  18264. BUILD_LOCK_OPS(read, rwlock);
  18265. BUILD_LOCK_OPS(write, rwlock);
  18266. +#endif
  18267. #endif
  18268. @@ -209,6 +212,8 @@
  18269. EXPORT_SYMBOL(_raw_spin_unlock_bh);
  18270. #endif
  18271. +#ifndef CONFIG_PREEMPT_RT_FULL
  18272. +
  18273. #ifndef CONFIG_INLINE_READ_TRYLOCK
  18274. int __lockfunc _raw_read_trylock(rwlock_t *lock)
  18275. {
  18276. @@ -353,6 +358,8 @@
  18277. EXPORT_SYMBOL(_raw_write_unlock_bh);
  18278. #endif
  18279. +#endif /* !PREEMPT_RT_FULL */
  18280. +
  18281. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  18282. void __lockfunc _raw_spin_lock_nested(raw_spinlock_t *lock, int subclass)
  18283. diff -Nur linux-4.4.46.orig/kernel/locking/spinlock_debug.c linux-4.4.46/kernel/locking/spinlock_debug.c
  18284. --- linux-4.4.46.orig/kernel/locking/spinlock_debug.c 2017-02-01 08:31:11.000000000 +0100
  18285. +++ linux-4.4.46/kernel/locking/spinlock_debug.c 2017-02-03 17:18:10.927619058 +0100
  18286. @@ -31,6 +31,7 @@
  18287. EXPORT_SYMBOL(__raw_spin_lock_init);
  18288. +#ifndef CONFIG_PREEMPT_RT_FULL
  18289. void __rwlock_init(rwlock_t *lock, const char *name,
  18290. struct lock_class_key *key)
  18291. {
  18292. @@ -48,6 +49,7 @@
  18293. }
  18294. EXPORT_SYMBOL(__rwlock_init);
  18295. +#endif
  18296. static void spin_dump(raw_spinlock_t *lock, const char *msg)
  18297. {
  18298. @@ -159,6 +161,7 @@
  18299. arch_spin_unlock(&lock->raw_lock);
  18300. }
  18301. +#ifndef CONFIG_PREEMPT_RT_FULL
  18302. static void rwlock_bug(rwlock_t *lock, const char *msg)
  18303. {
  18304. if (!debug_locks_off())
  18305. @@ -300,3 +303,5 @@
  18306. debug_write_unlock(lock);
  18307. arch_write_unlock(&lock->raw_lock);
  18308. }
  18309. +
  18310. +#endif
  18311. diff -Nur linux-4.4.46.orig/kernel/panic.c linux-4.4.46/kernel/panic.c
  18312. --- linux-4.4.46.orig/kernel/panic.c 2017-02-01 08:31:11.000000000 +0100
  18313. +++ linux-4.4.46/kernel/panic.c 2017-02-03 17:18:10.927619058 +0100
  18314. @@ -61,6 +61,37 @@
  18315. cpu_relax();
  18316. }
  18317. +/*
  18318. + * Stop ourselves in NMI context if another CPU has already panicked. Arch code
  18319. + * may override this to prepare for crash dumping, e.g. save regs info.
  18320. + */
  18321. +void __weak nmi_panic_self_stop(struct pt_regs *regs)
  18322. +{
  18323. + panic_smp_self_stop();
  18324. +}
  18325. +
  18326. +atomic_t panic_cpu = ATOMIC_INIT(PANIC_CPU_INVALID);
  18327. +
  18328. +/*
  18329. + * A variant of panic() called from NMI context. We return if we've already
  18330. + * panicked on this CPU. If another CPU already panicked, loop in
  18331. + * nmi_panic_self_stop() which can provide architecture dependent code such
  18332. + * as saving register state for crash dump.
  18333. + */
  18334. +void nmi_panic(struct pt_regs *regs, const char *msg)
  18335. +{
  18336. + int old_cpu, cpu;
  18337. +
  18338. + cpu = raw_smp_processor_id();
  18339. + old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, cpu);
  18340. +
  18341. + if (old_cpu == PANIC_CPU_INVALID)
  18342. + panic("%s", msg);
  18343. + else if (old_cpu != cpu)
  18344. + nmi_panic_self_stop(regs);
  18345. +}
  18346. +EXPORT_SYMBOL(nmi_panic);
  18347. +
  18348. /**
  18349. * panic - halt the system
  18350. * @fmt: The text string to print
  18351. @@ -71,17 +102,17 @@
  18352. */
  18353. void panic(const char *fmt, ...)
  18354. {
  18355. - static DEFINE_SPINLOCK(panic_lock);
  18356. static char buf[1024];
  18357. va_list args;
  18358. long i, i_next = 0;
  18359. int state = 0;
  18360. + int old_cpu, this_cpu;
  18361. /*
  18362. * Disable local interrupts. This will prevent panic_smp_self_stop
  18363. * from deadlocking the first cpu that invokes the panic, since
  18364. * there is nothing to prevent an interrupt handler (that runs
  18365. - * after the panic_lock is acquired) from invoking panic again.
  18366. + * after setting panic_cpu) from invoking panic() again.
  18367. */
  18368. local_irq_disable();
  18369. @@ -94,8 +125,16 @@
  18370. * multiple parallel invocations of panic, all other CPUs either
  18371. * stop themself or will wait until they are stopped by the 1st CPU
  18372. * with smp_send_stop().
  18373. + *
  18374. + * `old_cpu == PANIC_CPU_INVALID' means this is the 1st CPU which
  18375. + * comes here, so go ahead.
  18376. + * `old_cpu == this_cpu' means we came from nmi_panic() which sets
  18377. + * panic_cpu to this CPU. In this case, this is also the 1st CPU.
  18378. */
  18379. - if (!spin_trylock(&panic_lock))
  18380. + this_cpu = raw_smp_processor_id();
  18381. + old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, this_cpu);
  18382. +
  18383. + if (old_cpu != PANIC_CPU_INVALID && old_cpu != this_cpu)
  18384. panic_smp_self_stop();
  18385. console_verbose();
  18386. @@ -400,9 +439,11 @@
  18387. static int init_oops_id(void)
  18388. {
  18389. +#ifndef CONFIG_PREEMPT_RT_FULL
  18390. if (!oops_id)
  18391. get_random_bytes(&oops_id, sizeof(oops_id));
  18392. else
  18393. +#endif
  18394. oops_id++;
  18395. return 0;
  18396. diff -Nur linux-4.4.46.orig/kernel/power/hibernate.c linux-4.4.46/kernel/power/hibernate.c
  18397. --- linux-4.4.46.orig/kernel/power/hibernate.c 2017-02-01 08:31:11.000000000 +0100
  18398. +++ linux-4.4.46/kernel/power/hibernate.c 2017-02-03 17:18:10.927619058 +0100
  18399. @@ -285,6 +285,8 @@
  18400. local_irq_disable();
  18401. + system_state = SYSTEM_SUSPEND;
  18402. +
  18403. error = syscore_suspend();
  18404. if (error) {
  18405. printk(KERN_ERR "PM: Some system devices failed to power down, "
  18406. @@ -314,6 +316,7 @@
  18407. syscore_resume();
  18408. Enable_irqs:
  18409. + system_state = SYSTEM_RUNNING;
  18410. local_irq_enable();
  18411. Enable_cpus:
  18412. @@ -438,6 +441,7 @@
  18413. goto Enable_cpus;
  18414. local_irq_disable();
  18415. + system_state = SYSTEM_SUSPEND;
  18416. error = syscore_suspend();
  18417. if (error)
  18418. @@ -471,6 +475,7 @@
  18419. syscore_resume();
  18420. Enable_irqs:
  18421. + system_state = SYSTEM_RUNNING;
  18422. local_irq_enable();
  18423. Enable_cpus:
  18424. @@ -556,6 +561,7 @@
  18425. goto Enable_cpus;
  18426. local_irq_disable();
  18427. + system_state = SYSTEM_SUSPEND;
  18428. syscore_suspend();
  18429. if (pm_wakeup_pending()) {
  18430. error = -EAGAIN;
  18431. @@ -568,6 +574,7 @@
  18432. Power_up:
  18433. syscore_resume();
  18434. + system_state = SYSTEM_RUNNING;
  18435. local_irq_enable();
  18436. Enable_cpus:
  18437. @@ -642,6 +649,10 @@
  18438. cpu_relax();
  18439. }
  18440. +#ifndef CONFIG_SUSPEND
  18441. +bool pm_in_action;
  18442. +#endif
  18443. +
  18444. /**
  18445. * hibernate - Carry out system hibernation, including saving the image.
  18446. */
  18447. @@ -654,6 +665,8 @@
  18448. return -EPERM;
  18449. }
  18450. + pm_in_action = true;
  18451. +
  18452. lock_system_sleep();
  18453. /* The snapshot device should not be opened while we're running */
  18454. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  18455. @@ -719,6 +732,7 @@
  18456. atomic_inc(&snapshot_device_available);
  18457. Unlock:
  18458. unlock_system_sleep();
  18459. + pm_in_action = false;
  18460. return error;
  18461. }
  18462. diff -Nur linux-4.4.46.orig/kernel/power/suspend.c linux-4.4.46/kernel/power/suspend.c
  18463. --- linux-4.4.46.orig/kernel/power/suspend.c 2017-02-01 08:31:11.000000000 +0100
  18464. +++ linux-4.4.46/kernel/power/suspend.c 2017-02-03 17:18:10.927619058 +0100
  18465. @@ -359,6 +359,8 @@
  18466. arch_suspend_disable_irqs();
  18467. BUG_ON(!irqs_disabled());
  18468. + system_state = SYSTEM_SUSPEND;
  18469. +
  18470. error = syscore_suspend();
  18471. if (!error) {
  18472. *wakeup = pm_wakeup_pending();
  18473. @@ -375,6 +377,8 @@
  18474. syscore_resume();
  18475. }
  18476. + system_state = SYSTEM_RUNNING;
  18477. +
  18478. arch_suspend_enable_irqs();
  18479. BUG_ON(irqs_disabled());
  18480. @@ -518,6 +522,8 @@
  18481. return error;
  18482. }
  18483. +bool pm_in_action;
  18484. +
  18485. /**
  18486. * pm_suspend - Externally visible function for suspending the system.
  18487. * @state: System sleep state to enter.
  18488. @@ -532,6 +538,8 @@
  18489. if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX)
  18490. return -EINVAL;
  18491. + pm_in_action = true;
  18492. +
  18493. error = enter_state(state);
  18494. if (error) {
  18495. suspend_stats.fail++;
  18496. @@ -539,6 +547,7 @@
  18497. } else {
  18498. suspend_stats.success++;
  18499. }
  18500. + pm_in_action = false;
  18501. return error;
  18502. }
  18503. EXPORT_SYMBOL(pm_suspend);
  18504. diff -Nur linux-4.4.46.orig/kernel/printk/printk.c linux-4.4.46/kernel/printk/printk.c
  18505. --- linux-4.4.46.orig/kernel/printk/printk.c 2017-02-01 08:31:11.000000000 +0100
  18506. +++ linux-4.4.46/kernel/printk/printk.c 2017-02-03 17:18:10.927619058 +0100
  18507. @@ -241,6 +241,65 @@
  18508. */
  18509. static DEFINE_RAW_SPINLOCK(logbuf_lock);
  18510. +#ifdef CONFIG_EARLY_PRINTK
  18511. +struct console *early_console;
  18512. +
  18513. +static void early_vprintk(const char *fmt, va_list ap)
  18514. +{
  18515. + if (early_console) {
  18516. + char buf[512];
  18517. + int n = vscnprintf(buf, sizeof(buf), fmt, ap);
  18518. +
  18519. + early_console->write(early_console, buf, n);
  18520. + }
  18521. +}
  18522. +
  18523. +asmlinkage void early_printk(const char *fmt, ...)
  18524. +{
  18525. + va_list ap;
  18526. +
  18527. + va_start(ap, fmt);
  18528. + early_vprintk(fmt, ap);
  18529. + va_end(ap);
  18530. +}
  18531. +
  18532. +/*
  18533. + * This is independent of any log levels - a global
  18534. + * kill switch that turns off all of printk.
  18535. + *
  18536. + * Used by the NMI watchdog if early-printk is enabled.
  18537. + */
  18538. +static bool __read_mostly printk_killswitch;
  18539. +
  18540. +static int __init force_early_printk_setup(char *str)
  18541. +{
  18542. + printk_killswitch = true;
  18543. + return 0;
  18544. +}
  18545. +early_param("force_early_printk", force_early_printk_setup);
  18546. +
  18547. +void printk_kill(void)
  18548. +{
  18549. + printk_killswitch = true;
  18550. +}
  18551. +
  18552. +#ifdef CONFIG_PRINTK
  18553. +static int forced_early_printk(const char *fmt, va_list ap)
  18554. +{
  18555. + if (!printk_killswitch)
  18556. + return 0;
  18557. + early_vprintk(fmt, ap);
  18558. + return 1;
  18559. +}
  18560. +#endif
  18561. +
  18562. +#else
  18563. +static inline int forced_early_printk(const char *fmt, va_list ap)
  18564. +{
  18565. + return 0;
  18566. +}
  18567. +#endif
  18568. +
  18569. #ifdef CONFIG_PRINTK
  18570. DECLARE_WAIT_QUEUE_HEAD(log_wait);
  18571. /* the next printk record to read by syslog(READ) or /proc/kmsg */
  18572. @@ -1203,6 +1262,7 @@
  18573. {
  18574. char *text;
  18575. int len = 0;
  18576. + int attempts = 0;
  18577. text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
  18578. if (!text)
  18579. @@ -1214,7 +1274,14 @@
  18580. u64 seq;
  18581. u32 idx;
  18582. enum log_flags prev;
  18583. -
  18584. + int num_msg;
  18585. +try_again:
  18586. + attempts++;
  18587. + if (attempts > 10) {
  18588. + len = -EBUSY;
  18589. + goto out;
  18590. + }
  18591. + num_msg = 0;
  18592. if (clear_seq < log_first_seq) {
  18593. /* messages are gone, move to first available one */
  18594. clear_seq = log_first_seq;
  18595. @@ -1235,6 +1302,14 @@
  18596. prev = msg->flags;
  18597. idx = log_next(idx);
  18598. seq++;
  18599. + num_msg++;
  18600. + if (num_msg > 5) {
  18601. + num_msg = 0;
  18602. + raw_spin_unlock_irq(&logbuf_lock);
  18603. + raw_spin_lock_irq(&logbuf_lock);
  18604. + if (clear_seq < log_first_seq)
  18605. + goto try_again;
  18606. + }
  18607. }
  18608. /* move first record forward until length fits into the buffer */
  18609. @@ -1248,6 +1323,14 @@
  18610. prev = msg->flags;
  18611. idx = log_next(idx);
  18612. seq++;
  18613. + num_msg++;
  18614. + if (num_msg > 5) {
  18615. + num_msg = 0;
  18616. + raw_spin_unlock_irq(&logbuf_lock);
  18617. + raw_spin_lock_irq(&logbuf_lock);
  18618. + if (clear_seq < log_first_seq)
  18619. + goto try_again;
  18620. + }
  18621. }
  18622. /* last message fitting into this dump */
  18623. @@ -1288,6 +1371,7 @@
  18624. clear_seq = log_next_seq;
  18625. clear_idx = log_next_idx;
  18626. }
  18627. +out:
  18628. raw_spin_unlock_irq(&logbuf_lock);
  18629. kfree(text);
  18630. @@ -1443,6 +1527,12 @@
  18631. if (!console_drivers)
  18632. return;
  18633. + if (IS_ENABLED(CONFIG_PREEMPT_RT_BASE)) {
  18634. + if (in_irq() || in_nmi())
  18635. + return;
  18636. + }
  18637. +
  18638. + migrate_disable();
  18639. for_each_console(con) {
  18640. if (exclusive_console && con != exclusive_console)
  18641. continue;
  18642. @@ -1458,6 +1548,7 @@
  18643. else
  18644. con->write(con, text, len);
  18645. }
  18646. + migrate_enable();
  18647. }
  18648. /*
  18649. @@ -1518,6 +1609,15 @@
  18650. static int console_trylock_for_printk(void)
  18651. {
  18652. unsigned int cpu = smp_processor_id();
  18653. +#ifdef CONFIG_PREEMPT_RT_FULL
  18654. + int lock = !early_boot_irqs_disabled && (preempt_count() == 0) &&
  18655. + !irqs_disabled();
  18656. +#else
  18657. + int lock = 1;
  18658. +#endif
  18659. +
  18660. + if (!lock)
  18661. + return 0;
  18662. if (!console_trylock())
  18663. return 0;
  18664. @@ -1672,6 +1772,13 @@
  18665. /* cpu currently holding logbuf_lock in this function */
  18666. static unsigned int logbuf_cpu = UINT_MAX;
  18667. + /*
  18668. + * Fall back to early_printk if a debugging subsystem has
  18669. + * killed printk output
  18670. + */
  18671. + if (unlikely(forced_early_printk(fmt, args)))
  18672. + return 1;
  18673. +
  18674. if (level == LOGLEVEL_SCHED) {
  18675. level = LOGLEVEL_DEFAULT;
  18676. in_sched = true;
  18677. @@ -1813,8 +1920,7 @@
  18678. * console_sem which would prevent anyone from printing to
  18679. * console
  18680. */
  18681. - preempt_disable();
  18682. -
  18683. + migrate_disable();
  18684. /*
  18685. * Try to acquire and then immediately release the console
  18686. * semaphore. The release will print out buffers and wake up
  18687. @@ -1822,7 +1928,7 @@
  18688. */
  18689. if (console_trylock_for_printk())
  18690. console_unlock();
  18691. - preempt_enable();
  18692. + migrate_enable();
  18693. lockdep_on();
  18694. }
  18695. @@ -1961,26 +2067,6 @@
  18696. #endif /* CONFIG_PRINTK */
  18697. -#ifdef CONFIG_EARLY_PRINTK
  18698. -struct console *early_console;
  18699. -
  18700. -asmlinkage __visible void early_printk(const char *fmt, ...)
  18701. -{
  18702. - va_list ap;
  18703. - char buf[512];
  18704. - int n;
  18705. -
  18706. - if (!early_console)
  18707. - return;
  18708. -
  18709. - va_start(ap, fmt);
  18710. - n = vscnprintf(buf, sizeof(buf), fmt, ap);
  18711. - va_end(ap);
  18712. -
  18713. - early_console->write(early_console, buf, n);
  18714. -}
  18715. -#endif
  18716. -
  18717. static int __add_preferred_console(char *name, int idx, char *options,
  18718. char *brl_options)
  18719. {
  18720. @@ -2202,11 +2288,16 @@
  18721. goto out;
  18722. len = cont_print_text(text, size);
  18723. +#ifdef CONFIG_PREEMPT_RT_FULL
  18724. + raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  18725. + call_console_drivers(cont.level, NULL, 0, text, len);
  18726. +#else
  18727. raw_spin_unlock(&logbuf_lock);
  18728. stop_critical_timings();
  18729. call_console_drivers(cont.level, NULL, 0, text, len);
  18730. start_critical_timings();
  18731. local_irq_restore(flags);
  18732. +#endif
  18733. return;
  18734. out:
  18735. raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  18736. @@ -2316,13 +2407,17 @@
  18737. console_idx = log_next(console_idx);
  18738. console_seq++;
  18739. console_prev = msg->flags;
  18740. +#ifdef CONFIG_PREEMPT_RT_FULL
  18741. + raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  18742. + call_console_drivers(level, ext_text, ext_len, text, len);
  18743. +#else
  18744. raw_spin_unlock(&logbuf_lock);
  18745. stop_critical_timings(); /* don't trace print latency */
  18746. call_console_drivers(level, ext_text, ext_len, text, len);
  18747. start_critical_timings();
  18748. local_irq_restore(flags);
  18749. -
  18750. +#endif
  18751. if (do_cond_resched)
  18752. cond_resched();
  18753. }
  18754. @@ -2374,6 +2469,11 @@
  18755. {
  18756. struct console *c;
  18757. + if (IS_ENABLED(CONFIG_PREEMPT_RT_BASE)) {
  18758. + if (in_irq() || in_nmi())
  18759. + return;
  18760. + }
  18761. +
  18762. /*
  18763. * console_unblank can no longer be called in interrupt context unless
  18764. * oops_in_progress is set to 1..
  18765. diff -Nur linux-4.4.46.orig/kernel/ptrace.c linux-4.4.46/kernel/ptrace.c
  18766. --- linux-4.4.46.orig/kernel/ptrace.c 2017-02-01 08:31:11.000000000 +0100
  18767. +++ linux-4.4.46/kernel/ptrace.c 2017-02-03 17:18:10.927619058 +0100
  18768. @@ -136,7 +136,14 @@
  18769. spin_lock_irq(&task->sighand->siglock);
  18770. if (task_is_traced(task) && !__fatal_signal_pending(task)) {
  18771. - task->state = __TASK_TRACED;
  18772. + unsigned long flags;
  18773. +
  18774. + raw_spin_lock_irqsave(&task->pi_lock, flags);
  18775. + if (task->state & __TASK_TRACED)
  18776. + task->state = __TASK_TRACED;
  18777. + else
  18778. + task->saved_state = __TASK_TRACED;
  18779. + raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  18780. ret = true;
  18781. }
  18782. spin_unlock_irq(&task->sighand->siglock);
  18783. diff -Nur linux-4.4.46.orig/kernel/rcu/rcutorture.c linux-4.4.46/kernel/rcu/rcutorture.c
  18784. --- linux-4.4.46.orig/kernel/rcu/rcutorture.c 2017-02-01 08:31:11.000000000 +0100
  18785. +++ linux-4.4.46/kernel/rcu/rcutorture.c 2017-02-03 17:18:10.927619058 +0100
  18786. @@ -390,6 +390,7 @@
  18787. .name = "rcu"
  18788. };
  18789. +#ifndef CONFIG_PREEMPT_RT_FULL
  18790. /*
  18791. * Definitions for rcu_bh torture testing.
  18792. */
  18793. @@ -429,6 +430,12 @@
  18794. .name = "rcu_bh"
  18795. };
  18796. +#else
  18797. +static struct rcu_torture_ops rcu_bh_ops = {
  18798. + .ttype = INVALID_RCU_FLAVOR,
  18799. +};
  18800. +#endif
  18801. +
  18802. /*
  18803. * Don't even think about trying any of these in real life!!!
  18804. * The names includes "busted", and they really means it!
  18805. diff -Nur linux-4.4.46.orig/kernel/rcu/tree.c linux-4.4.46/kernel/rcu/tree.c
  18806. --- linux-4.4.46.orig/kernel/rcu/tree.c 2017-02-01 08:31:11.000000000 +0100
  18807. +++ linux-4.4.46/kernel/rcu/tree.c 2017-02-03 17:18:10.931619212 +0100
  18808. @@ -56,6 +56,11 @@
  18809. #include <linux/random.h>
  18810. #include <linux/trace_events.h>
  18811. #include <linux/suspend.h>
  18812. +#include <linux/delay.h>
  18813. +#include <linux/gfp.h>
  18814. +#include <linux/oom.h>
  18815. +#include <linux/smpboot.h>
  18816. +#include "../time/tick-internal.h"
  18817. #include "tree.h"
  18818. #include "rcu.h"
  18819. @@ -266,6 +271,19 @@
  18820. }
  18821. }
  18822. +#ifdef CONFIG_PREEMPT_RT_FULL
  18823. +static void rcu_preempt_qs(void);
  18824. +
  18825. +void rcu_bh_qs(void)
  18826. +{
  18827. + unsigned long flags;
  18828. +
  18829. + /* Callers to this function, rcu_preempt_qs(), must disable irqs. */
  18830. + local_irq_save(flags);
  18831. + rcu_preempt_qs();
  18832. + local_irq_restore(flags);
  18833. +}
  18834. +#else
  18835. void rcu_bh_qs(void)
  18836. {
  18837. if (__this_cpu_read(rcu_bh_data.cpu_no_qs.s)) {
  18838. @@ -275,6 +293,7 @@
  18839. __this_cpu_write(rcu_bh_data.cpu_no_qs.b.norm, false);
  18840. }
  18841. }
  18842. +#endif
  18843. static DEFINE_PER_CPU(int, rcu_sched_qs_mask);
  18844. @@ -435,11 +454,13 @@
  18845. /*
  18846. * Return the number of RCU BH batches started thus far for debug & stats.
  18847. */
  18848. +#ifndef CONFIG_PREEMPT_RT_FULL
  18849. unsigned long rcu_batches_started_bh(void)
  18850. {
  18851. return rcu_bh_state.gpnum;
  18852. }
  18853. EXPORT_SYMBOL_GPL(rcu_batches_started_bh);
  18854. +#endif
  18855. /*
  18856. * Return the number of RCU batches completed thus far for debug & stats.
  18857. @@ -459,6 +480,7 @@
  18858. }
  18859. EXPORT_SYMBOL_GPL(rcu_batches_completed_sched);
  18860. +#ifndef CONFIG_PREEMPT_RT_FULL
  18861. /*
  18862. * Return the number of RCU BH batches completed thus far for debug & stats.
  18863. */
  18864. @@ -486,6 +508,13 @@
  18865. }
  18866. EXPORT_SYMBOL_GPL(rcu_bh_force_quiescent_state);
  18867. +#else
  18868. +void rcu_force_quiescent_state(void)
  18869. +{
  18870. +}
  18871. +EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
  18872. +#endif
  18873. +
  18874. /*
  18875. * Force a quiescent state for RCU-sched.
  18876. */
  18877. @@ -536,9 +565,11 @@
  18878. case RCU_FLAVOR:
  18879. rsp = rcu_state_p;
  18880. break;
  18881. +#ifndef CONFIG_PREEMPT_RT_FULL
  18882. case RCU_BH_FLAVOR:
  18883. rsp = &rcu_bh_state;
  18884. break;
  18885. +#endif
  18886. case RCU_SCHED_FLAVOR:
  18887. rsp = &rcu_sched_state;
  18888. break;
  18889. @@ -1590,7 +1621,6 @@
  18890. int needmore;
  18891. struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
  18892. - rcu_nocb_gp_cleanup(rsp, rnp);
  18893. rnp->need_future_gp[c & 0x1] = 0;
  18894. needmore = rnp->need_future_gp[(c + 1) & 0x1];
  18895. trace_rcu_future_gp(rnp, rdp, c,
  18896. @@ -1611,7 +1641,7 @@
  18897. !READ_ONCE(rsp->gp_flags) ||
  18898. !rsp->gp_kthread)
  18899. return;
  18900. - wake_up(&rsp->gp_wq);
  18901. + swake_up(&rsp->gp_wq);
  18902. }
  18903. /*
  18904. @@ -1991,6 +2021,7 @@
  18905. int nocb = 0;
  18906. struct rcu_data *rdp;
  18907. struct rcu_node *rnp = rcu_get_root(rsp);
  18908. + struct swait_queue_head *sq;
  18909. WRITE_ONCE(rsp->gp_activity, jiffies);
  18910. raw_spin_lock_irq(&rnp->lock);
  18911. @@ -2029,7 +2060,9 @@
  18912. needgp = __note_gp_changes(rsp, rnp, rdp) || needgp;
  18913. /* smp_mb() provided by prior unlock-lock pair. */
  18914. nocb += rcu_future_gp_cleanup(rsp, rnp);
  18915. + sq = rcu_nocb_gp_get(rnp);
  18916. raw_spin_unlock_irq(&rnp->lock);
  18917. + rcu_nocb_gp_cleanup(sq);
  18918. cond_resched_rcu_qs();
  18919. WRITE_ONCE(rsp->gp_activity, jiffies);
  18920. rcu_gp_slow(rsp, gp_cleanup_delay);
  18921. @@ -2076,7 +2109,7 @@
  18922. READ_ONCE(rsp->gpnum),
  18923. TPS("reqwait"));
  18924. rsp->gp_state = RCU_GP_WAIT_GPS;
  18925. - wait_event_interruptible(rsp->gp_wq,
  18926. + swait_event_interruptible(rsp->gp_wq,
  18927. READ_ONCE(rsp->gp_flags) &
  18928. RCU_GP_FLAG_INIT);
  18929. rsp->gp_state = RCU_GP_DONE_GPS;
  18930. @@ -2106,7 +2139,7 @@
  18931. READ_ONCE(rsp->gpnum),
  18932. TPS("fqswait"));
  18933. rsp->gp_state = RCU_GP_WAIT_FQS;
  18934. - ret = wait_event_interruptible_timeout(rsp->gp_wq,
  18935. + ret = swait_event_interruptible_timeout(rsp->gp_wq,
  18936. rcu_gp_fqs_check_wake(rsp, &gf), j);
  18937. rsp->gp_state = RCU_GP_DOING_FQS;
  18938. /* Locking provides needed memory barriers. */
  18939. @@ -2230,7 +2263,7 @@
  18940. WARN_ON_ONCE(!rcu_gp_in_progress(rsp));
  18941. WRITE_ONCE(rsp->gp_flags, READ_ONCE(rsp->gp_flags) | RCU_GP_FLAG_FQS);
  18942. raw_spin_unlock_irqrestore(&rcu_get_root(rsp)->lock, flags);
  18943. - rcu_gp_kthread_wake(rsp);
  18944. + swake_up(&rsp->gp_wq); /* Memory barrier implied by swake_up() path. */
  18945. }
  18946. /*
  18947. @@ -2891,7 +2924,7 @@
  18948. }
  18949. WRITE_ONCE(rsp->gp_flags, READ_ONCE(rsp->gp_flags) | RCU_GP_FLAG_FQS);
  18950. raw_spin_unlock_irqrestore(&rnp_old->lock, flags);
  18951. - rcu_gp_kthread_wake(rsp);
  18952. + swake_up(&rsp->gp_wq); /* Memory barrier implied by swake_up() path. */
  18953. }
  18954. /*
  18955. @@ -2934,18 +2967,17 @@
  18956. /*
  18957. * Do RCU core processing for the current CPU.
  18958. */
  18959. -static void rcu_process_callbacks(struct softirq_action *unused)
  18960. +static void rcu_process_callbacks(void)
  18961. {
  18962. struct rcu_state *rsp;
  18963. if (cpu_is_offline(smp_processor_id()))
  18964. return;
  18965. - trace_rcu_utilization(TPS("Start RCU core"));
  18966. for_each_rcu_flavor(rsp)
  18967. __rcu_process_callbacks(rsp);
  18968. - trace_rcu_utilization(TPS("End RCU core"));
  18969. }
  18970. +static DEFINE_PER_CPU(struct task_struct *, rcu_cpu_kthread_task);
  18971. /*
  18972. * Schedule RCU callback invocation. If the specified type of RCU
  18973. * does not support RCU priority boosting, just do a direct call,
  18974. @@ -2957,18 +2989,105 @@
  18975. {
  18976. if (unlikely(!READ_ONCE(rcu_scheduler_fully_active)))
  18977. return;
  18978. - if (likely(!rsp->boost)) {
  18979. - rcu_do_batch(rsp, rdp);
  18980. + rcu_do_batch(rsp, rdp);
  18981. +}
  18982. +
  18983. +static void rcu_wake_cond(struct task_struct *t, int status)
  18984. +{
  18985. + /*
  18986. + * If the thread is yielding, only wake it when this
  18987. + * is invoked from idle
  18988. + */
  18989. + if (t && (status != RCU_KTHREAD_YIELDING || is_idle_task(current)))
  18990. + wake_up_process(t);
  18991. +}
  18992. +
  18993. +/*
  18994. + * Wake up this CPU's rcuc kthread to do RCU core processing.
  18995. + */
  18996. +static void invoke_rcu_core(void)
  18997. +{
  18998. + unsigned long flags;
  18999. + struct task_struct *t;
  19000. +
  19001. + if (!cpu_online(smp_processor_id()))
  19002. return;
  19003. + local_irq_save(flags);
  19004. + __this_cpu_write(rcu_cpu_has_work, 1);
  19005. + t = __this_cpu_read(rcu_cpu_kthread_task);
  19006. + if (t != NULL && current != t)
  19007. + rcu_wake_cond(t, __this_cpu_read(rcu_cpu_kthread_status));
  19008. + local_irq_restore(flags);
  19009. +}
  19010. +
  19011. +static void rcu_cpu_kthread_park(unsigned int cpu)
  19012. +{
  19013. + per_cpu(rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
  19014. +}
  19015. +
  19016. +static int rcu_cpu_kthread_should_run(unsigned int cpu)
  19017. +{
  19018. + return __this_cpu_read(rcu_cpu_has_work);
  19019. +}
  19020. +
  19021. +/*
  19022. + * Per-CPU kernel thread that invokes RCU callbacks. This replaces the
  19023. + * RCU softirq used in flavors and configurations of RCU that do not
  19024. + * support RCU priority boosting.
  19025. + */
  19026. +static void rcu_cpu_kthread(unsigned int cpu)
  19027. +{
  19028. + unsigned int *statusp = this_cpu_ptr(&rcu_cpu_kthread_status);
  19029. + char work, *workp = this_cpu_ptr(&rcu_cpu_has_work);
  19030. + int spincnt;
  19031. +
  19032. + for (spincnt = 0; spincnt < 10; spincnt++) {
  19033. + trace_rcu_utilization(TPS("Start CPU kthread@rcu_wait"));
  19034. + local_bh_disable();
  19035. + *statusp = RCU_KTHREAD_RUNNING;
  19036. + this_cpu_inc(rcu_cpu_kthread_loops);
  19037. + local_irq_disable();
  19038. + work = *workp;
  19039. + *workp = 0;
  19040. + local_irq_enable();
  19041. + if (work)
  19042. + rcu_process_callbacks();
  19043. + local_bh_enable();
  19044. + if (*workp == 0) {
  19045. + trace_rcu_utilization(TPS("End CPU kthread@rcu_wait"));
  19046. + *statusp = RCU_KTHREAD_WAITING;
  19047. + return;
  19048. + }
  19049. }
  19050. - invoke_rcu_callbacks_kthread();
  19051. + *statusp = RCU_KTHREAD_YIELDING;
  19052. + trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield"));
  19053. + schedule_timeout_interruptible(2);
  19054. + trace_rcu_utilization(TPS("End CPU kthread@rcu_yield"));
  19055. + *statusp = RCU_KTHREAD_WAITING;
  19056. }
  19057. -static void invoke_rcu_core(void)
  19058. +static struct smp_hotplug_thread rcu_cpu_thread_spec = {
  19059. + .store = &rcu_cpu_kthread_task,
  19060. + .thread_should_run = rcu_cpu_kthread_should_run,
  19061. + .thread_fn = rcu_cpu_kthread,
  19062. + .thread_comm = "rcuc/%u",
  19063. + .setup = rcu_cpu_kthread_setup,
  19064. + .park = rcu_cpu_kthread_park,
  19065. +};
  19066. +
  19067. +/*
  19068. + * Spawn per-CPU RCU core processing kthreads.
  19069. + */
  19070. +static int __init rcu_spawn_core_kthreads(void)
  19071. {
  19072. - if (cpu_online(smp_processor_id()))
  19073. - raise_softirq(RCU_SOFTIRQ);
  19074. + int cpu;
  19075. +
  19076. + for_each_possible_cpu(cpu)
  19077. + per_cpu(rcu_cpu_has_work, cpu) = 0;
  19078. + BUG_ON(smpboot_register_percpu_thread(&rcu_cpu_thread_spec));
  19079. + return 0;
  19080. }
  19081. +early_initcall(rcu_spawn_core_kthreads);
  19082. /*
  19083. * Handle any core-RCU processing required by a call_rcu() invocation.
  19084. @@ -3114,6 +3233,7 @@
  19085. }
  19086. EXPORT_SYMBOL_GPL(call_rcu_sched);
  19087. +#ifndef CONFIG_PREEMPT_RT_FULL
  19088. /*
  19089. * Queue an RCU callback for invocation after a quicker grace period.
  19090. */
  19091. @@ -3122,6 +3242,7 @@
  19092. __call_rcu(head, func, &rcu_bh_state, -1, 0);
  19093. }
  19094. EXPORT_SYMBOL_GPL(call_rcu_bh);
  19095. +#endif
  19096. /*
  19097. * Queue an RCU callback for lazy invocation after a grace period.
  19098. @@ -3213,6 +3334,7 @@
  19099. }
  19100. EXPORT_SYMBOL_GPL(synchronize_sched);
  19101. +#ifndef CONFIG_PREEMPT_RT_FULL
  19102. /**
  19103. * synchronize_rcu_bh - wait until an rcu_bh grace period has elapsed.
  19104. *
  19105. @@ -3239,6 +3361,7 @@
  19106. wait_rcu_gp(call_rcu_bh);
  19107. }
  19108. EXPORT_SYMBOL_GPL(synchronize_rcu_bh);
  19109. +#endif
  19110. /**
  19111. * get_state_synchronize_rcu - Snapshot current RCU state
  19112. @@ -3524,7 +3647,7 @@
  19113. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  19114. if (wake) {
  19115. smp_mb(); /* EGP done before wake_up(). */
  19116. - wake_up(&rsp->expedited_wq);
  19117. + swake_up(&rsp->expedited_wq);
  19118. }
  19119. break;
  19120. }
  19121. @@ -3781,7 +3904,7 @@
  19122. jiffies_start = jiffies;
  19123. for (;;) {
  19124. - ret = wait_event_interruptible_timeout(
  19125. + ret = swait_event_timeout(
  19126. rsp->expedited_wq,
  19127. sync_rcu_preempt_exp_done(rnp_root),
  19128. jiffies_stall);
  19129. @@ -3789,7 +3912,7 @@
  19130. return;
  19131. if (ret < 0) {
  19132. /* Hit a signal, disable CPU stall warnings. */
  19133. - wait_event(rsp->expedited_wq,
  19134. + swait_event(rsp->expedited_wq,
  19135. sync_rcu_preempt_exp_done(rnp_root));
  19136. return;
  19137. }
  19138. @@ -4101,6 +4224,7 @@
  19139. mutex_unlock(&rsp->barrier_mutex);
  19140. }
  19141. +#ifndef CONFIG_PREEMPT_RT_FULL
  19142. /**
  19143. * rcu_barrier_bh - Wait until all in-flight call_rcu_bh() callbacks complete.
  19144. */
  19145. @@ -4109,6 +4233,7 @@
  19146. _rcu_barrier(&rcu_bh_state);
  19147. }
  19148. EXPORT_SYMBOL_GPL(rcu_barrier_bh);
  19149. +#endif
  19150. /**
  19151. * rcu_barrier_sched - Wait for in-flight call_rcu_sched() callbacks.
  19152. @@ -4455,8 +4580,8 @@
  19153. }
  19154. }
  19155. - init_waitqueue_head(&rsp->gp_wq);
  19156. - init_waitqueue_head(&rsp->expedited_wq);
  19157. + init_swait_queue_head(&rsp->gp_wq);
  19158. + init_swait_queue_head(&rsp->expedited_wq);
  19159. rnp = rsp->level[rcu_num_lvls - 1];
  19160. for_each_possible_cpu(i) {
  19161. while (i > rnp->grphi)
  19162. @@ -4576,12 +4701,13 @@
  19163. rcu_bootup_announce();
  19164. rcu_init_geometry();
  19165. +#ifndef CONFIG_PREEMPT_RT_FULL
  19166. rcu_init_one(&rcu_bh_state, &rcu_bh_data);
  19167. +#endif
  19168. rcu_init_one(&rcu_sched_state, &rcu_sched_data);
  19169. if (dump_tree)
  19170. rcu_dump_rcu_node_tree(&rcu_sched_state);
  19171. __rcu_init_preempt();
  19172. - open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
  19173. /*
  19174. * We don't need protection against CPU-hotplug here because
  19175. diff -Nur linux-4.4.46.orig/kernel/rcu/tree.h linux-4.4.46/kernel/rcu/tree.h
  19176. --- linux-4.4.46.orig/kernel/rcu/tree.h 2017-02-01 08:31:11.000000000 +0100
  19177. +++ linux-4.4.46/kernel/rcu/tree.h 2017-02-03 17:18:10.931619212 +0100
  19178. @@ -27,6 +27,7 @@
  19179. #include <linux/threads.h>
  19180. #include <linux/cpumask.h>
  19181. #include <linux/seqlock.h>
  19182. +#include <linux/swait.h>
  19183. #include <linux/stop_machine.h>
  19184. /*
  19185. @@ -241,7 +242,7 @@
  19186. /* Refused to boost: not sure why, though. */
  19187. /* This can happen due to race conditions. */
  19188. #ifdef CONFIG_RCU_NOCB_CPU
  19189. - wait_queue_head_t nocb_gp_wq[2];
  19190. + struct swait_queue_head nocb_gp_wq[2];
  19191. /* Place for rcu_nocb_kthread() to wait GP. */
  19192. #endif /* #ifdef CONFIG_RCU_NOCB_CPU */
  19193. int need_future_gp[2];
  19194. @@ -393,7 +394,7 @@
  19195. atomic_long_t nocb_q_count_lazy; /* invocation (all stages). */
  19196. struct rcu_head *nocb_follower_head; /* CBs ready to invoke. */
  19197. struct rcu_head **nocb_follower_tail;
  19198. - wait_queue_head_t nocb_wq; /* For nocb kthreads to sleep on. */
  19199. + struct swait_queue_head nocb_wq; /* For nocb kthreads to sleep on. */
  19200. struct task_struct *nocb_kthread;
  19201. int nocb_defer_wakeup; /* Defer wakeup of nocb_kthread. */
  19202. @@ -472,7 +473,7 @@
  19203. unsigned long gpnum; /* Current gp number. */
  19204. unsigned long completed; /* # of last completed gp. */
  19205. struct task_struct *gp_kthread; /* Task for grace periods. */
  19206. - wait_queue_head_t gp_wq; /* Where GP task waits. */
  19207. + struct swait_queue_head gp_wq; /* Where GP task waits. */
  19208. short gp_flags; /* Commands for GP task. */
  19209. short gp_state; /* GP kthread sleep state. */
  19210. @@ -504,7 +505,7 @@
  19211. atomic_long_t expedited_workdone3; /* # done by others #3. */
  19212. atomic_long_t expedited_normal; /* # fallbacks to normal. */
  19213. atomic_t expedited_need_qs; /* # CPUs left to check in. */
  19214. - wait_queue_head_t expedited_wq; /* Wait for check-ins. */
  19215. + struct swait_queue_head expedited_wq; /* Wait for check-ins. */
  19216. int ncpus_snap; /* # CPUs seen last time. */
  19217. unsigned long jiffies_force_qs; /* Time at which to invoke */
  19218. @@ -556,18 +557,18 @@
  19219. */
  19220. extern struct rcu_state rcu_sched_state;
  19221. +#ifndef CONFIG_PREEMPT_RT_FULL
  19222. extern struct rcu_state rcu_bh_state;
  19223. +#endif
  19224. #ifdef CONFIG_PREEMPT_RCU
  19225. extern struct rcu_state rcu_preempt_state;
  19226. #endif /* #ifdef CONFIG_PREEMPT_RCU */
  19227. -#ifdef CONFIG_RCU_BOOST
  19228. DECLARE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
  19229. DECLARE_PER_CPU(int, rcu_cpu_kthread_cpu);
  19230. DECLARE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
  19231. DECLARE_PER_CPU(char, rcu_cpu_has_work);
  19232. -#endif /* #ifdef CONFIG_RCU_BOOST */
  19233. #ifndef RCU_TREE_NONCORE
  19234. @@ -587,10 +588,9 @@
  19235. static void __init __rcu_init_preempt(void);
  19236. static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags);
  19237. static void rcu_preempt_boost_start_gp(struct rcu_node *rnp);
  19238. -static void invoke_rcu_callbacks_kthread(void);
  19239. static bool rcu_is_callbacks_kthread(void);
  19240. +static void rcu_cpu_kthread_setup(unsigned int cpu);
  19241. #ifdef CONFIG_RCU_BOOST
  19242. -static void rcu_preempt_do_callbacks(void);
  19243. static int rcu_spawn_one_boost_kthread(struct rcu_state *rsp,
  19244. struct rcu_node *rnp);
  19245. #endif /* #ifdef CONFIG_RCU_BOOST */
  19246. @@ -607,7 +607,8 @@
  19247. static void increment_cpu_stall_ticks(void);
  19248. static bool rcu_nocb_cpu_needs_barrier(struct rcu_state *rsp, int cpu);
  19249. static void rcu_nocb_gp_set(struct rcu_node *rnp, int nrq);
  19250. -static void rcu_nocb_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp);
  19251. +static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp);
  19252. +static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq);
  19253. static void rcu_init_one_nocb(struct rcu_node *rnp);
  19254. static bool __call_rcu_nocb(struct rcu_data *rdp, struct rcu_head *rhp,
  19255. bool lazy, unsigned long flags);
  19256. diff -Nur linux-4.4.46.orig/kernel/rcu/tree_plugin.h linux-4.4.46/kernel/rcu/tree_plugin.h
  19257. --- linux-4.4.46.orig/kernel/rcu/tree_plugin.h 2017-02-01 08:31:11.000000000 +0100
  19258. +++ linux-4.4.46/kernel/rcu/tree_plugin.h 2017-02-03 17:18:10.931619212 +0100
  19259. @@ -24,25 +24,10 @@
  19260. * Paul E. McKenney <paulmck@linux.vnet.ibm.com>
  19261. */
  19262. -#include <linux/delay.h>
  19263. -#include <linux/gfp.h>
  19264. -#include <linux/oom.h>
  19265. -#include <linux/smpboot.h>
  19266. -#include "../time/tick-internal.h"
  19267. -
  19268. #ifdef CONFIG_RCU_BOOST
  19269. #include "../locking/rtmutex_common.h"
  19270. -/*
  19271. - * Control variables for per-CPU and per-rcu_node kthreads. These
  19272. - * handle all flavors of RCU.
  19273. - */
  19274. -static DEFINE_PER_CPU(struct task_struct *, rcu_cpu_kthread_task);
  19275. -DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
  19276. -DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
  19277. -DEFINE_PER_CPU(char, rcu_cpu_has_work);
  19278. -
  19279. #else /* #ifdef CONFIG_RCU_BOOST */
  19280. /*
  19281. @@ -55,6 +40,14 @@
  19282. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  19283. +/*
  19284. + * Control variables for per-CPU and per-rcu_node kthreads. These
  19285. + * handle all flavors of RCU.
  19286. + */
  19287. +DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
  19288. +DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
  19289. +DEFINE_PER_CPU(char, rcu_cpu_has_work);
  19290. +
  19291. #ifdef CONFIG_RCU_NOCB_CPU
  19292. static cpumask_var_t rcu_nocb_mask; /* CPUs to have callbacks offloaded. */
  19293. static bool have_rcu_nocb_mask; /* Was rcu_nocb_mask allocated? */
  19294. @@ -432,7 +425,7 @@
  19295. }
  19296. /* Hardware IRQ handlers cannot block, complain if they get here. */
  19297. - if (in_irq() || in_serving_softirq()) {
  19298. + if (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_OFFSET)) {
  19299. lockdep_rcu_suspicious(__FILE__, __LINE__,
  19300. "rcu_read_unlock() from irq or softirq with blocking in critical section!!!\n");
  19301. pr_alert("->rcu_read_unlock_special: %#x (b: %d, enq: %d nq: %d)\n",
  19302. @@ -645,15 +638,6 @@
  19303. t->rcu_read_unlock_special.b.need_qs = true;
  19304. }
  19305. -#ifdef CONFIG_RCU_BOOST
  19306. -
  19307. -static void rcu_preempt_do_callbacks(void)
  19308. -{
  19309. - rcu_do_batch(rcu_state_p, this_cpu_ptr(rcu_data_p));
  19310. -}
  19311. -
  19312. -#endif /* #ifdef CONFIG_RCU_BOOST */
  19313. -
  19314. /*
  19315. * Queue a preemptible-RCU callback for invocation after a grace period.
  19316. */
  19317. @@ -930,6 +914,19 @@
  19318. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  19319. +/*
  19320. + * If boosting, set rcuc kthreads to realtime priority.
  19321. + */
  19322. +static void rcu_cpu_kthread_setup(unsigned int cpu)
  19323. +{
  19324. +#ifdef CONFIG_RCU_BOOST
  19325. + struct sched_param sp;
  19326. +
  19327. + sp.sched_priority = kthread_prio;
  19328. + sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
  19329. +#endif /* #ifdef CONFIG_RCU_BOOST */
  19330. +}
  19331. +
  19332. #ifdef CONFIG_RCU_BOOST
  19333. #include "../locking/rtmutex_common.h"
  19334. @@ -961,16 +958,6 @@
  19335. #endif /* #else #ifdef CONFIG_RCU_TRACE */
  19336. -static void rcu_wake_cond(struct task_struct *t, int status)
  19337. -{
  19338. - /*
  19339. - * If the thread is yielding, only wake it when this
  19340. - * is invoked from idle
  19341. - */
  19342. - if (status != RCU_KTHREAD_YIELDING || is_idle_task(current))
  19343. - wake_up_process(t);
  19344. -}
  19345. -
  19346. /*
  19347. * Carry out RCU priority boosting on the task indicated by ->exp_tasks
  19348. * or ->boost_tasks, advancing the pointer to the next task in the
  19349. @@ -1115,23 +1102,6 @@
  19350. }
  19351. /*
  19352. - * Wake up the per-CPU kthread to invoke RCU callbacks.
  19353. - */
  19354. -static void invoke_rcu_callbacks_kthread(void)
  19355. -{
  19356. - unsigned long flags;
  19357. -
  19358. - local_irq_save(flags);
  19359. - __this_cpu_write(rcu_cpu_has_work, 1);
  19360. - if (__this_cpu_read(rcu_cpu_kthread_task) != NULL &&
  19361. - current != __this_cpu_read(rcu_cpu_kthread_task)) {
  19362. - rcu_wake_cond(__this_cpu_read(rcu_cpu_kthread_task),
  19363. - __this_cpu_read(rcu_cpu_kthread_status));
  19364. - }
  19365. - local_irq_restore(flags);
  19366. -}
  19367. -
  19368. -/*
  19369. * Is the current CPU running the RCU-callbacks kthread?
  19370. * Caller must have preemption disabled.
  19371. */
  19372. @@ -1186,67 +1156,6 @@
  19373. return 0;
  19374. }
  19375. -static void rcu_kthread_do_work(void)
  19376. -{
  19377. - rcu_do_batch(&rcu_sched_state, this_cpu_ptr(&rcu_sched_data));
  19378. - rcu_do_batch(&rcu_bh_state, this_cpu_ptr(&rcu_bh_data));
  19379. - rcu_preempt_do_callbacks();
  19380. -}
  19381. -
  19382. -static void rcu_cpu_kthread_setup(unsigned int cpu)
  19383. -{
  19384. - struct sched_param sp;
  19385. -
  19386. - sp.sched_priority = kthread_prio;
  19387. - sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
  19388. -}
  19389. -
  19390. -static void rcu_cpu_kthread_park(unsigned int cpu)
  19391. -{
  19392. - per_cpu(rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
  19393. -}
  19394. -
  19395. -static int rcu_cpu_kthread_should_run(unsigned int cpu)
  19396. -{
  19397. - return __this_cpu_read(rcu_cpu_has_work);
  19398. -}
  19399. -
  19400. -/*
  19401. - * Per-CPU kernel thread that invokes RCU callbacks. This replaces the
  19402. - * RCU softirq used in flavors and configurations of RCU that do not
  19403. - * support RCU priority boosting.
  19404. - */
  19405. -static void rcu_cpu_kthread(unsigned int cpu)
  19406. -{
  19407. - unsigned int *statusp = this_cpu_ptr(&rcu_cpu_kthread_status);
  19408. - char work, *workp = this_cpu_ptr(&rcu_cpu_has_work);
  19409. - int spincnt;
  19410. -
  19411. - for (spincnt = 0; spincnt < 10; spincnt++) {
  19412. - trace_rcu_utilization(TPS("Start CPU kthread@rcu_wait"));
  19413. - local_bh_disable();
  19414. - *statusp = RCU_KTHREAD_RUNNING;
  19415. - this_cpu_inc(rcu_cpu_kthread_loops);
  19416. - local_irq_disable();
  19417. - work = *workp;
  19418. - *workp = 0;
  19419. - local_irq_enable();
  19420. - if (work)
  19421. - rcu_kthread_do_work();
  19422. - local_bh_enable();
  19423. - if (*workp == 0) {
  19424. - trace_rcu_utilization(TPS("End CPU kthread@rcu_wait"));
  19425. - *statusp = RCU_KTHREAD_WAITING;
  19426. - return;
  19427. - }
  19428. - }
  19429. - *statusp = RCU_KTHREAD_YIELDING;
  19430. - trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield"));
  19431. - schedule_timeout_interruptible(2);
  19432. - trace_rcu_utilization(TPS("End CPU kthread@rcu_yield"));
  19433. - *statusp = RCU_KTHREAD_WAITING;
  19434. -}
  19435. -
  19436. /*
  19437. * Set the per-rcu_node kthread's affinity to cover all CPUs that are
  19438. * served by the rcu_node in question. The CPU hotplug lock is still
  19439. @@ -1276,26 +1185,12 @@
  19440. free_cpumask_var(cm);
  19441. }
  19442. -static struct smp_hotplug_thread rcu_cpu_thread_spec = {
  19443. - .store = &rcu_cpu_kthread_task,
  19444. - .thread_should_run = rcu_cpu_kthread_should_run,
  19445. - .thread_fn = rcu_cpu_kthread,
  19446. - .thread_comm = "rcuc/%u",
  19447. - .setup = rcu_cpu_kthread_setup,
  19448. - .park = rcu_cpu_kthread_park,
  19449. -};
  19450. -
  19451. /*
  19452. * Spawn boost kthreads -- called as soon as the scheduler is running.
  19453. */
  19454. static void __init rcu_spawn_boost_kthreads(void)
  19455. {
  19456. struct rcu_node *rnp;
  19457. - int cpu;
  19458. -
  19459. - for_each_possible_cpu(cpu)
  19460. - per_cpu(rcu_cpu_has_work, cpu) = 0;
  19461. - BUG_ON(smpboot_register_percpu_thread(&rcu_cpu_thread_spec));
  19462. rcu_for_each_leaf_node(rcu_state_p, rnp)
  19463. (void)rcu_spawn_one_boost_kthread(rcu_state_p, rnp);
  19464. }
  19465. @@ -1318,11 +1213,6 @@
  19466. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  19467. }
  19468. -static void invoke_rcu_callbacks_kthread(void)
  19469. -{
  19470. - WARN_ON_ONCE(1);
  19471. -}
  19472. -
  19473. static bool rcu_is_callbacks_kthread(void)
  19474. {
  19475. return false;
  19476. @@ -1346,7 +1236,7 @@
  19477. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  19478. -#if !defined(CONFIG_RCU_FAST_NO_HZ)
  19479. +#if !defined(CONFIG_RCU_FAST_NO_HZ) || defined(CONFIG_PREEMPT_RT_FULL)
  19480. /*
  19481. * Check to see if any future RCU-related work will need to be done
  19482. @@ -1363,7 +1253,9 @@
  19483. return IS_ENABLED(CONFIG_RCU_NOCB_CPU_ALL)
  19484. ? 0 : rcu_cpu_has_callbacks(NULL);
  19485. }
  19486. +#endif /* !defined(CONFIG_RCU_FAST_NO_HZ) || defined(CONFIG_PREEMPT_RT_FULL) */
  19487. +#if !defined(CONFIG_RCU_FAST_NO_HZ)
  19488. /*
  19489. * Because we do not have RCU_FAST_NO_HZ, don't bother cleaning up
  19490. * after it.
  19491. @@ -1459,6 +1351,8 @@
  19492. return cbs_ready;
  19493. }
  19494. +#ifndef CONFIG_PREEMPT_RT_FULL
  19495. +
  19496. /*
  19497. * Allow the CPU to enter dyntick-idle mode unless it has callbacks ready
  19498. * to invoke. If the CPU has callbacks, try to advance them. Tell the
  19499. @@ -1504,6 +1398,7 @@
  19500. *nextevt = basemono + dj * TICK_NSEC;
  19501. return 0;
  19502. }
  19503. +#endif /* #ifndef CONFIG_PREEMPT_RT_FULL */
  19504. /*
  19505. * Prepare a CPU for idle from an RCU perspective. The first major task
  19506. @@ -1822,9 +1717,9 @@
  19507. * Wake up any no-CBs CPUs' kthreads that were waiting on the just-ended
  19508. * grace period.
  19509. */
  19510. -static void rcu_nocb_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp)
  19511. +static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq)
  19512. {
  19513. - wake_up_all(&rnp->nocb_gp_wq[rnp->completed & 0x1]);
  19514. + swake_up_all(sq);
  19515. }
  19516. /*
  19517. @@ -1840,10 +1735,15 @@
  19518. rnp->need_future_gp[(rnp->completed + 1) & 0x1] += nrq;
  19519. }
  19520. +static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp)
  19521. +{
  19522. + return &rnp->nocb_gp_wq[rnp->completed & 0x1];
  19523. +}
  19524. +
  19525. static void rcu_init_one_nocb(struct rcu_node *rnp)
  19526. {
  19527. - init_waitqueue_head(&rnp->nocb_gp_wq[0]);
  19528. - init_waitqueue_head(&rnp->nocb_gp_wq[1]);
  19529. + init_swait_queue_head(&rnp->nocb_gp_wq[0]);
  19530. + init_swait_queue_head(&rnp->nocb_gp_wq[1]);
  19531. }
  19532. #ifndef CONFIG_RCU_NOCB_CPU_ALL
  19533. @@ -1868,7 +1768,7 @@
  19534. if (READ_ONCE(rdp_leader->nocb_leader_sleep) || force) {
  19535. /* Prior smp_mb__after_atomic() orders against prior enqueue. */
  19536. WRITE_ONCE(rdp_leader->nocb_leader_sleep, false);
  19537. - wake_up(&rdp_leader->nocb_wq);
  19538. + swake_up(&rdp_leader->nocb_wq);
  19539. }
  19540. }
  19541. @@ -2081,7 +1981,7 @@
  19542. */
  19543. trace_rcu_future_gp(rnp, rdp, c, TPS("StartWait"));
  19544. for (;;) {
  19545. - wait_event_interruptible(
  19546. + swait_event_interruptible(
  19547. rnp->nocb_gp_wq[c & 0x1],
  19548. (d = ULONG_CMP_GE(READ_ONCE(rnp->completed), c)));
  19549. if (likely(d))
  19550. @@ -2109,7 +2009,7 @@
  19551. /* Wait for callbacks to appear. */
  19552. if (!rcu_nocb_poll) {
  19553. trace_rcu_nocb_wake(my_rdp->rsp->name, my_rdp->cpu, "Sleep");
  19554. - wait_event_interruptible(my_rdp->nocb_wq,
  19555. + swait_event_interruptible(my_rdp->nocb_wq,
  19556. !READ_ONCE(my_rdp->nocb_leader_sleep));
  19557. /* Memory barrier handled by smp_mb() calls below and repoll. */
  19558. } else if (firsttime) {
  19559. @@ -2184,7 +2084,7 @@
  19560. * List was empty, wake up the follower.
  19561. * Memory barriers supplied by atomic_long_add().
  19562. */
  19563. - wake_up(&rdp->nocb_wq);
  19564. + swake_up(&rdp->nocb_wq);
  19565. }
  19566. }
  19567. @@ -2205,7 +2105,7 @@
  19568. if (!rcu_nocb_poll) {
  19569. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  19570. "FollowerSleep");
  19571. - wait_event_interruptible(rdp->nocb_wq,
  19572. + swait_event_interruptible(rdp->nocb_wq,
  19573. READ_ONCE(rdp->nocb_follower_head));
  19574. } else if (firsttime) {
  19575. /* Don't drown trace log with "Poll"! */
  19576. @@ -2365,7 +2265,7 @@
  19577. static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
  19578. {
  19579. rdp->nocb_tail = &rdp->nocb_head;
  19580. - init_waitqueue_head(&rdp->nocb_wq);
  19581. + init_swait_queue_head(&rdp->nocb_wq);
  19582. rdp->nocb_follower_tail = &rdp->nocb_follower_head;
  19583. }
  19584. @@ -2515,7 +2415,7 @@
  19585. return false;
  19586. }
  19587. -static void rcu_nocb_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp)
  19588. +static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq)
  19589. {
  19590. }
  19591. @@ -2523,6 +2423,11 @@
  19592. {
  19593. }
  19594. +static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp)
  19595. +{
  19596. + return NULL;
  19597. +}
  19598. +
  19599. static void rcu_init_one_nocb(struct rcu_node *rnp)
  19600. {
  19601. }
  19602. diff -Nur linux-4.4.46.orig/kernel/rcu/update.c linux-4.4.46/kernel/rcu/update.c
  19603. --- linux-4.4.46.orig/kernel/rcu/update.c 2017-02-01 08:31:11.000000000 +0100
  19604. +++ linux-4.4.46/kernel/rcu/update.c 2017-02-03 17:18:10.931619212 +0100
  19605. @@ -276,6 +276,7 @@
  19606. }
  19607. EXPORT_SYMBOL_GPL(rcu_read_lock_held);
  19608. +#ifndef CONFIG_PREEMPT_RT_FULL
  19609. /**
  19610. * rcu_read_lock_bh_held() - might we be in RCU-bh read-side critical section?
  19611. *
  19612. @@ -302,6 +303,7 @@
  19613. return in_softirq() || irqs_disabled();
  19614. }
  19615. EXPORT_SYMBOL_GPL(rcu_read_lock_bh_held);
  19616. +#endif
  19617. #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  19618. diff -Nur linux-4.4.46.orig/kernel/relay.c linux-4.4.46/kernel/relay.c
  19619. --- linux-4.4.46.orig/kernel/relay.c 2017-02-01 08:31:11.000000000 +0100
  19620. +++ linux-4.4.46/kernel/relay.c 2017-02-03 17:18:10.931619212 +0100
  19621. @@ -336,6 +336,10 @@
  19622. {
  19623. struct rchan_buf *buf = (struct rchan_buf *)data;
  19624. wake_up_interruptible(&buf->read_wait);
  19625. + /*
  19626. + * Stupid polling for now:
  19627. + */
  19628. + mod_timer(&buf->timer, jiffies + 1);
  19629. }
  19630. /**
  19631. @@ -353,6 +357,7 @@
  19632. init_waitqueue_head(&buf->read_wait);
  19633. kref_init(&buf->kref);
  19634. setup_timer(&buf->timer, wakeup_readers, (unsigned long)buf);
  19635. + mod_timer(&buf->timer, jiffies + 1);
  19636. } else
  19637. del_timer_sync(&buf->timer);
  19638. @@ -736,15 +741,6 @@
  19639. else
  19640. buf->early_bytes += buf->chan->subbuf_size -
  19641. buf->padding[old_subbuf];
  19642. - smp_mb();
  19643. - if (waitqueue_active(&buf->read_wait))
  19644. - /*
  19645. - * Calling wake_up_interruptible() from here
  19646. - * will deadlock if we happen to be logging
  19647. - * from the scheduler (trying to re-grab
  19648. - * rq->lock), so defer it.
  19649. - */
  19650. - mod_timer(&buf->timer, jiffies + 1);
  19651. }
  19652. old = buf->data;
  19653. diff -Nur linux-4.4.46.orig/kernel/sched/completion.c linux-4.4.46/kernel/sched/completion.c
  19654. --- linux-4.4.46.orig/kernel/sched/completion.c 2017-02-01 08:31:11.000000000 +0100
  19655. +++ linux-4.4.46/kernel/sched/completion.c 2017-02-03 17:18:10.931619212 +0100
  19656. @@ -30,10 +30,10 @@
  19657. {
  19658. unsigned long flags;
  19659. - spin_lock_irqsave(&x->wait.lock, flags);
  19660. + raw_spin_lock_irqsave(&x->wait.lock, flags);
  19661. x->done++;
  19662. - __wake_up_locked(&x->wait, TASK_NORMAL, 1);
  19663. - spin_unlock_irqrestore(&x->wait.lock, flags);
  19664. + swake_up_locked(&x->wait);
  19665. + raw_spin_unlock_irqrestore(&x->wait.lock, flags);
  19666. }
  19667. EXPORT_SYMBOL(complete);
  19668. @@ -50,10 +50,10 @@
  19669. {
  19670. unsigned long flags;
  19671. - spin_lock_irqsave(&x->wait.lock, flags);
  19672. + raw_spin_lock_irqsave(&x->wait.lock, flags);
  19673. x->done += UINT_MAX/2;
  19674. - __wake_up_locked(&x->wait, TASK_NORMAL, 0);
  19675. - spin_unlock_irqrestore(&x->wait.lock, flags);
  19676. + swake_up_all_locked(&x->wait);
  19677. + raw_spin_unlock_irqrestore(&x->wait.lock, flags);
  19678. }
  19679. EXPORT_SYMBOL(complete_all);
  19680. @@ -62,20 +62,20 @@
  19681. long (*action)(long), long timeout, int state)
  19682. {
  19683. if (!x->done) {
  19684. - DECLARE_WAITQUEUE(wait, current);
  19685. + DECLARE_SWAITQUEUE(wait);
  19686. - __add_wait_queue_tail_exclusive(&x->wait, &wait);
  19687. + __prepare_to_swait(&x->wait, &wait);
  19688. do {
  19689. if (signal_pending_state(state, current)) {
  19690. timeout = -ERESTARTSYS;
  19691. break;
  19692. }
  19693. __set_current_state(state);
  19694. - spin_unlock_irq(&x->wait.lock);
  19695. + raw_spin_unlock_irq(&x->wait.lock);
  19696. timeout = action(timeout);
  19697. - spin_lock_irq(&x->wait.lock);
  19698. + raw_spin_lock_irq(&x->wait.lock);
  19699. } while (!x->done && timeout);
  19700. - __remove_wait_queue(&x->wait, &wait);
  19701. + __finish_swait(&x->wait, &wait);
  19702. if (!x->done)
  19703. return timeout;
  19704. }
  19705. @@ -89,9 +89,9 @@
  19706. {
  19707. might_sleep();
  19708. - spin_lock_irq(&x->wait.lock);
  19709. + raw_spin_lock_irq(&x->wait.lock);
  19710. timeout = do_wait_for_common(x, action, timeout, state);
  19711. - spin_unlock_irq(&x->wait.lock);
  19712. + raw_spin_unlock_irq(&x->wait.lock);
  19713. return timeout;
  19714. }
  19715. @@ -277,12 +277,12 @@
  19716. if (!READ_ONCE(x->done))
  19717. return 0;
  19718. - spin_lock_irqsave(&x->wait.lock, flags);
  19719. + raw_spin_lock_irqsave(&x->wait.lock, flags);
  19720. if (!x->done)
  19721. ret = 0;
  19722. else
  19723. x->done--;
  19724. - spin_unlock_irqrestore(&x->wait.lock, flags);
  19725. + raw_spin_unlock_irqrestore(&x->wait.lock, flags);
  19726. return ret;
  19727. }
  19728. EXPORT_SYMBOL(try_wait_for_completion);
  19729. @@ -311,7 +311,7 @@
  19730. * after it's acquired the lock.
  19731. */
  19732. smp_rmb();
  19733. - spin_unlock_wait(&x->wait.lock);
  19734. + raw_spin_unlock_wait(&x->wait.lock);
  19735. return true;
  19736. }
  19737. EXPORT_SYMBOL(completion_done);
  19738. diff -Nur linux-4.4.46.orig/kernel/sched/core.c linux-4.4.46/kernel/sched/core.c
  19739. --- linux-4.4.46.orig/kernel/sched/core.c 2017-02-01 08:31:11.000000000 +0100
  19740. +++ linux-4.4.46/kernel/sched/core.c 2017-02-03 17:18:10.931619212 +0100
  19741. @@ -260,7 +260,11 @@
  19742. * Number of tasks to iterate in a single balance run.
  19743. * Limited because this is done with IRQs disabled.
  19744. */
  19745. +#ifndef CONFIG_PREEMPT_RT_FULL
  19746. const_debug unsigned int sysctl_sched_nr_migrate = 32;
  19747. +#else
  19748. +const_debug unsigned int sysctl_sched_nr_migrate = 8;
  19749. +#endif
  19750. /*
  19751. * period over which we average the RT time consumption, measured
  19752. @@ -438,6 +442,7 @@
  19753. hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  19754. rq->hrtick_timer.function = hrtick;
  19755. + rq->hrtick_timer.irqsafe = 1;
  19756. }
  19757. #else /* CONFIG_SCHED_HRTICK */
  19758. static inline void hrtick_clear(struct rq *rq)
  19759. @@ -542,7 +547,7 @@
  19760. head->lastp = &node->next;
  19761. }
  19762. -void wake_up_q(struct wake_q_head *head)
  19763. +void __wake_up_q(struct wake_q_head *head, bool sleeper)
  19764. {
  19765. struct wake_q_node *node = head->first;
  19766. @@ -559,7 +564,10 @@
  19767. * wake_up_process() implies a wmb() to pair with the queueing
  19768. * in wake_q_add() so as not to miss wakeups.
  19769. */
  19770. - wake_up_process(task);
  19771. + if (sleeper)
  19772. + wake_up_lock_sleeper(task);
  19773. + else
  19774. + wake_up_process(task);
  19775. put_task_struct(task);
  19776. }
  19777. }
  19778. @@ -595,6 +603,38 @@
  19779. trace_sched_wake_idle_without_ipi(cpu);
  19780. }
  19781. +#ifdef CONFIG_PREEMPT_LAZY
  19782. +void resched_curr_lazy(struct rq *rq)
  19783. +{
  19784. + struct task_struct *curr = rq->curr;
  19785. + int cpu;
  19786. +
  19787. + if (!sched_feat(PREEMPT_LAZY)) {
  19788. + resched_curr(rq);
  19789. + return;
  19790. + }
  19791. +
  19792. + lockdep_assert_held(&rq->lock);
  19793. +
  19794. + if (test_tsk_need_resched(curr))
  19795. + return;
  19796. +
  19797. + if (test_tsk_need_resched_lazy(curr))
  19798. + return;
  19799. +
  19800. + set_tsk_need_resched_lazy(curr);
  19801. +
  19802. + cpu = cpu_of(rq);
  19803. + if (cpu == smp_processor_id())
  19804. + return;
  19805. +
  19806. + /* NEED_RESCHED_LAZY must be visible before we test polling */
  19807. + smp_mb();
  19808. + if (!tsk_is_polling(curr))
  19809. + smp_send_reschedule(cpu);
  19810. +}
  19811. +#endif
  19812. +
  19813. void resched_cpu(int cpu)
  19814. {
  19815. struct rq *rq = cpu_rq(cpu);
  19816. @@ -618,11 +658,14 @@
  19817. */
  19818. int get_nohz_timer_target(void)
  19819. {
  19820. - int i, cpu = smp_processor_id();
  19821. + int i, cpu;
  19822. struct sched_domain *sd;
  19823. + preempt_disable_rt();
  19824. + cpu = smp_processor_id();
  19825. +
  19826. if (!idle_cpu(cpu) && is_housekeeping_cpu(cpu))
  19827. - return cpu;
  19828. + goto preempt_en_rt;
  19829. rcu_read_lock();
  19830. for_each_domain(cpu, sd) {
  19831. @@ -641,6 +684,8 @@
  19832. cpu = housekeeping_any_cpu();
  19833. unlock:
  19834. rcu_read_unlock();
  19835. +preempt_en_rt:
  19836. + preempt_enable_rt();
  19837. return cpu;
  19838. }
  19839. /*
  19840. @@ -1174,6 +1219,11 @@
  19841. lockdep_assert_held(&p->pi_lock);
  19842. + if (__migrate_disabled(p)) {
  19843. + cpumask_copy(&p->cpus_allowed, new_mask);
  19844. + return;
  19845. + }
  19846. +
  19847. queued = task_on_rq_queued(p);
  19848. running = task_current(rq, p);
  19849. @@ -1196,6 +1246,84 @@
  19850. enqueue_task(rq, p, ENQUEUE_RESTORE);
  19851. }
  19852. +static DEFINE_PER_CPU(struct cpumask, sched_cpumasks);
  19853. +static DEFINE_MUTEX(sched_down_mutex);
  19854. +static cpumask_t sched_down_cpumask;
  19855. +
  19856. +void tell_sched_cpu_down_begin(int cpu)
  19857. +{
  19858. + mutex_lock(&sched_down_mutex);
  19859. + cpumask_set_cpu(cpu, &sched_down_cpumask);
  19860. + mutex_unlock(&sched_down_mutex);
  19861. +}
  19862. +
  19863. +void tell_sched_cpu_down_done(int cpu)
  19864. +{
  19865. + mutex_lock(&sched_down_mutex);
  19866. + cpumask_clear_cpu(cpu, &sched_down_cpumask);
  19867. + mutex_unlock(&sched_down_mutex);
  19868. +}
  19869. +
  19870. +/**
  19871. + * migrate_me - try to move the current task off this cpu
  19872. + *
  19873. + * Used by the pin_current_cpu() code to try to get tasks
  19874. + * to move off the current CPU as it is going down.
  19875. + * It will only move the task if the task isn't pinned to
  19876. + * the CPU (with migrate_disable, affinity or NO_SETAFFINITY)
  19877. + * and the task has to be in a RUNNING state. Otherwise the
  19878. + * movement of the task will wake it up (change its state
  19879. + * to running) when the task did not expect it.
  19880. + *
  19881. + * Returns 1 if it succeeded in moving the current task
  19882. + * 0 otherwise.
  19883. + */
  19884. +int migrate_me(void)
  19885. +{
  19886. + struct task_struct *p = current;
  19887. + struct migration_arg arg;
  19888. + struct cpumask *cpumask;
  19889. + struct cpumask *mask;
  19890. + unsigned long flags;
  19891. + unsigned int dest_cpu;
  19892. + struct rq *rq;
  19893. +
  19894. + /*
  19895. + * We can not migrate tasks bounded to a CPU or tasks not
  19896. + * running. The movement of the task will wake it up.
  19897. + */
  19898. + if (p->flags & PF_NO_SETAFFINITY || p->state)
  19899. + return 0;
  19900. +
  19901. + mutex_lock(&sched_down_mutex);
  19902. + rq = task_rq_lock(p, &flags);
  19903. +
  19904. + cpumask = this_cpu_ptr(&sched_cpumasks);
  19905. + mask = &p->cpus_allowed;
  19906. +
  19907. + cpumask_andnot(cpumask, mask, &sched_down_cpumask);
  19908. +
  19909. + if (!cpumask_weight(cpumask)) {
  19910. + /* It's only on this CPU? */
  19911. + task_rq_unlock(rq, p, &flags);
  19912. + mutex_unlock(&sched_down_mutex);
  19913. + return 0;
  19914. + }
  19915. +
  19916. + dest_cpu = cpumask_any_and(cpu_active_mask, cpumask);
  19917. +
  19918. + arg.task = p;
  19919. + arg.dest_cpu = dest_cpu;
  19920. +
  19921. + task_rq_unlock(rq, p, &flags);
  19922. +
  19923. + stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
  19924. + tlb_migrate_finish(p->mm);
  19925. + mutex_unlock(&sched_down_mutex);
  19926. +
  19927. + return 1;
  19928. +}
  19929. +
  19930. /*
  19931. * Change a given task's CPU affinity. Migrate the thread to a
  19932. * proper CPU and schedule it away if the CPU it's executing on
  19933. @@ -1235,7 +1363,7 @@
  19934. do_set_cpus_allowed(p, new_mask);
  19935. /* Can the task run on the task's current CPU? If so, we're done */
  19936. - if (cpumask_test_cpu(task_cpu(p), new_mask))
  19937. + if (cpumask_test_cpu(task_cpu(p), new_mask) || __migrate_disabled(p))
  19938. goto out;
  19939. dest_cpu = cpumask_any_and(cpu_active_mask, new_mask);
  19940. @@ -1411,6 +1539,18 @@
  19941. return ret;
  19942. }
  19943. +static bool check_task_state(struct task_struct *p, long match_state)
  19944. +{
  19945. + bool match = false;
  19946. +
  19947. + raw_spin_lock_irq(&p->pi_lock);
  19948. + if (p->state == match_state || p->saved_state == match_state)
  19949. + match = true;
  19950. + raw_spin_unlock_irq(&p->pi_lock);
  19951. +
  19952. + return match;
  19953. +}
  19954. +
  19955. /*
  19956. * wait_task_inactive - wait for a thread to unschedule.
  19957. *
  19958. @@ -1455,7 +1595,7 @@
  19959. * is actually now running somewhere else!
  19960. */
  19961. while (task_running(rq, p)) {
  19962. - if (match_state && unlikely(p->state != match_state))
  19963. + if (match_state && !check_task_state(p, match_state))
  19964. return 0;
  19965. cpu_relax();
  19966. }
  19967. @@ -1470,7 +1610,8 @@
  19968. running = task_running(rq, p);
  19969. queued = task_on_rq_queued(p);
  19970. ncsw = 0;
  19971. - if (!match_state || p->state == match_state)
  19972. + if (!match_state || p->state == match_state ||
  19973. + p->saved_state == match_state)
  19974. ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
  19975. task_rq_unlock(rq, p, &flags);
  19976. @@ -1627,7 +1768,7 @@
  19977. {
  19978. lockdep_assert_held(&p->pi_lock);
  19979. - if (p->nr_cpus_allowed > 1)
  19980. + if (tsk_nr_cpus_allowed(p) > 1)
  19981. cpu = p->sched_class->select_task_rq(p, cpu, sd_flags, wake_flags);
  19982. /*
  19983. @@ -1707,10 +1848,6 @@
  19984. {
  19985. activate_task(rq, p, en_flags);
  19986. p->on_rq = TASK_ON_RQ_QUEUED;
  19987. -
  19988. - /* if a worker is waking up, notify workqueue */
  19989. - if (p->flags & PF_WQ_WORKER)
  19990. - wq_worker_waking_up(p, cpu_of(rq));
  19991. }
  19992. /*
  19993. @@ -1937,8 +2074,27 @@
  19994. */
  19995. smp_mb__before_spinlock();
  19996. raw_spin_lock_irqsave(&p->pi_lock, flags);
  19997. - if (!(p->state & state))
  19998. + if (!(p->state & state)) {
  19999. + /*
  20000. + * The task might be running due to a spinlock sleeper
  20001. + * wakeup. Check the saved state and set it to running
  20002. + * if the wakeup condition is true.
  20003. + */
  20004. + if (!(wake_flags & WF_LOCK_SLEEPER)) {
  20005. + if (p->saved_state & state) {
  20006. + p->saved_state = TASK_RUNNING;
  20007. + success = 1;
  20008. + }
  20009. + }
  20010. goto out;
  20011. + }
  20012. +
  20013. + /*
  20014. + * If this is a regular wakeup, then we can unconditionally
  20015. + * clear the saved state of a "lock sleeper".
  20016. + */
  20017. + if (!(wake_flags & WF_LOCK_SLEEPER))
  20018. + p->saved_state = TASK_RUNNING;
  20019. trace_sched_waking(p);
  20020. @@ -2030,52 +2186,6 @@
  20021. }
  20022. /**
  20023. - * try_to_wake_up_local - try to wake up a local task with rq lock held
  20024. - * @p: the thread to be awakened
  20025. - *
  20026. - * Put @p on the run-queue if it's not already there. The caller must
  20027. - * ensure that this_rq() is locked, @p is bound to this_rq() and not
  20028. - * the current task.
  20029. - */
  20030. -static void try_to_wake_up_local(struct task_struct *p)
  20031. -{
  20032. - struct rq *rq = task_rq(p);
  20033. -
  20034. - if (WARN_ON_ONCE(rq != this_rq()) ||
  20035. - WARN_ON_ONCE(p == current))
  20036. - return;
  20037. -
  20038. - lockdep_assert_held(&rq->lock);
  20039. -
  20040. - if (!raw_spin_trylock(&p->pi_lock)) {
  20041. - /*
  20042. - * This is OK, because current is on_cpu, which avoids it being
  20043. - * picked for load-balance and preemption/IRQs are still
  20044. - * disabled avoiding further scheduler activity on it and we've
  20045. - * not yet picked a replacement task.
  20046. - */
  20047. - lockdep_unpin_lock(&rq->lock);
  20048. - raw_spin_unlock(&rq->lock);
  20049. - raw_spin_lock(&p->pi_lock);
  20050. - raw_spin_lock(&rq->lock);
  20051. - lockdep_pin_lock(&rq->lock);
  20052. - }
  20053. -
  20054. - if (!(p->state & TASK_NORMAL))
  20055. - goto out;
  20056. -
  20057. - trace_sched_waking(p);
  20058. -
  20059. - if (!task_on_rq_queued(p))
  20060. - ttwu_activate(rq, p, ENQUEUE_WAKEUP);
  20061. -
  20062. - ttwu_do_wakeup(rq, p, 0);
  20063. - ttwu_stat(p, smp_processor_id(), 0);
  20064. -out:
  20065. - raw_spin_unlock(&p->pi_lock);
  20066. -}
  20067. -
  20068. -/**
  20069. * wake_up_process - Wake up a specific process
  20070. * @p: The process to be woken up.
  20071. *
  20072. @@ -2093,6 +2203,18 @@
  20073. }
  20074. EXPORT_SYMBOL(wake_up_process);
  20075. +/**
  20076. + * wake_up_lock_sleeper - Wake up a specific process blocked on a "sleeping lock"
  20077. + * @p: The process to be woken up.
  20078. + *
  20079. + * Same as wake_up_process() above, but wake_flags=WF_LOCK_SLEEPER to indicate
  20080. + * the nature of the wakeup.
  20081. + */
  20082. +int wake_up_lock_sleeper(struct task_struct *p)
  20083. +{
  20084. + return try_to_wake_up(p, TASK_ALL, WF_LOCK_SLEEPER);
  20085. +}
  20086. +
  20087. int wake_up_state(struct task_struct *p, unsigned int state)
  20088. {
  20089. return try_to_wake_up(p, state, 0);
  20090. @@ -2279,6 +2401,9 @@
  20091. p->on_cpu = 0;
  20092. #endif
  20093. init_task_preempt_count(p);
  20094. +#ifdef CONFIG_HAVE_PREEMPT_LAZY
  20095. + task_thread_info(p)->preempt_lazy_count = 0;
  20096. +#endif
  20097. #ifdef CONFIG_SMP
  20098. plist_node_init(&p->pushable_tasks, MAX_PRIO);
  20099. RB_CLEAR_NODE(&p->pushable_dl_tasks);
  20100. @@ -2603,8 +2728,12 @@
  20101. finish_arch_post_lock_switch();
  20102. fire_sched_in_preempt_notifiers(current);
  20103. + /*
  20104. + * We use mmdrop_delayed() here so we don't have to do the
  20105. + * full __mmdrop() when we are the last user.
  20106. + */
  20107. if (mm)
  20108. - mmdrop(mm);
  20109. + mmdrop_delayed(mm);
  20110. if (unlikely(prev_state == TASK_DEAD)) {
  20111. if (prev->sched_class->task_dead)
  20112. prev->sched_class->task_dead(prev);
  20113. @@ -2935,16 +3064,6 @@
  20114. }
  20115. #endif
  20116. -notrace unsigned long get_parent_ip(unsigned long addr)
  20117. -{
  20118. - if (in_lock_functions(addr)) {
  20119. - addr = CALLER_ADDR2;
  20120. - if (in_lock_functions(addr))
  20121. - addr = CALLER_ADDR3;
  20122. - }
  20123. - return addr;
  20124. -}
  20125. -
  20126. #if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
  20127. defined(CONFIG_PREEMPT_TRACER))
  20128. @@ -2966,7 +3085,7 @@
  20129. PREEMPT_MASK - 10);
  20130. #endif
  20131. if (preempt_count() == val) {
  20132. - unsigned long ip = get_parent_ip(CALLER_ADDR1);
  20133. + unsigned long ip = get_lock_parent_ip();
  20134. #ifdef CONFIG_DEBUG_PREEMPT
  20135. current->preempt_disable_ip = ip;
  20136. #endif
  20137. @@ -2993,7 +3112,7 @@
  20138. #endif
  20139. if (preempt_count() == val)
  20140. - trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
  20141. + trace_preempt_on(CALLER_ADDR0, get_lock_parent_ip());
  20142. __preempt_count_sub(val);
  20143. }
  20144. EXPORT_SYMBOL(preempt_count_sub);
  20145. @@ -3048,6 +3167,77 @@
  20146. schedstat_inc(this_rq(), sched_count);
  20147. }
  20148. +#if defined(CONFIG_PREEMPT_RT_FULL) && defined(CONFIG_SMP)
  20149. +
  20150. +void migrate_disable(void)
  20151. +{
  20152. + struct task_struct *p = current;
  20153. +
  20154. + if (in_atomic() || irqs_disabled()) {
  20155. +#ifdef CONFIG_SCHED_DEBUG
  20156. + p->migrate_disable_atomic++;
  20157. +#endif
  20158. + return;
  20159. + }
  20160. +
  20161. +#ifdef CONFIG_SCHED_DEBUG
  20162. + if (unlikely(p->migrate_disable_atomic)) {
  20163. + tracing_off();
  20164. + WARN_ON_ONCE(1);
  20165. + }
  20166. +#endif
  20167. +
  20168. + if (p->migrate_disable) {
  20169. + p->migrate_disable++;
  20170. + return;
  20171. + }
  20172. +
  20173. + preempt_disable();
  20174. + preempt_lazy_disable();
  20175. + pin_current_cpu();
  20176. + p->migrate_disable = 1;
  20177. + preempt_enable();
  20178. +}
  20179. +EXPORT_SYMBOL(migrate_disable);
  20180. +
  20181. +void migrate_enable(void)
  20182. +{
  20183. + struct task_struct *p = current;
  20184. +
  20185. + if (in_atomic() || irqs_disabled()) {
  20186. +#ifdef CONFIG_SCHED_DEBUG
  20187. + p->migrate_disable_atomic--;
  20188. +#endif
  20189. + return;
  20190. + }
  20191. +
  20192. +#ifdef CONFIG_SCHED_DEBUG
  20193. + if (unlikely(p->migrate_disable_atomic)) {
  20194. + tracing_off();
  20195. + WARN_ON_ONCE(1);
  20196. + }
  20197. +#endif
  20198. + WARN_ON_ONCE(p->migrate_disable <= 0);
  20199. +
  20200. + if (p->migrate_disable > 1) {
  20201. + p->migrate_disable--;
  20202. + return;
  20203. + }
  20204. +
  20205. + preempt_disable();
  20206. + /*
  20207. + * Clearing migrate_disable causes tsk_cpus_allowed to
  20208. + * show the tasks original cpu affinity.
  20209. + */
  20210. + p->migrate_disable = 0;
  20211. +
  20212. + unpin_current_cpu();
  20213. + preempt_enable();
  20214. + preempt_lazy_enable();
  20215. +}
  20216. +EXPORT_SYMBOL(migrate_enable);
  20217. +#endif
  20218. +
  20219. /*
  20220. * Pick up the highest-prio task:
  20221. */
  20222. @@ -3172,19 +3362,6 @@
  20223. } else {
  20224. deactivate_task(rq, prev, DEQUEUE_SLEEP);
  20225. prev->on_rq = 0;
  20226. -
  20227. - /*
  20228. - * If a worker went to sleep, notify and ask workqueue
  20229. - * whether it wants to wake up a task to maintain
  20230. - * concurrency.
  20231. - */
  20232. - if (prev->flags & PF_WQ_WORKER) {
  20233. - struct task_struct *to_wakeup;
  20234. -
  20235. - to_wakeup = wq_worker_sleeping(prev, cpu);
  20236. - if (to_wakeup)
  20237. - try_to_wake_up_local(to_wakeup);
  20238. - }
  20239. }
  20240. switch_count = &prev->nvcsw;
  20241. }
  20242. @@ -3194,6 +3371,7 @@
  20243. next = pick_next_task(rq, prev);
  20244. clear_tsk_need_resched(prev);
  20245. + clear_tsk_need_resched_lazy(prev);
  20246. clear_preempt_need_resched();
  20247. rq->clock_skip_update = 0;
  20248. @@ -3215,9 +3393,20 @@
  20249. static inline void sched_submit_work(struct task_struct *tsk)
  20250. {
  20251. - if (!tsk->state || tsk_is_pi_blocked(tsk))
  20252. + if (!tsk->state)
  20253. return;
  20254. /*
  20255. + * If a worker went to sleep, notify and ask workqueue whether
  20256. + * it wants to wake up a task to maintain concurrency.
  20257. + */
  20258. + if (tsk->flags & PF_WQ_WORKER)
  20259. + wq_worker_sleeping(tsk);
  20260. +
  20261. +
  20262. + if (tsk_is_pi_blocked(tsk))
  20263. + return;
  20264. +
  20265. + /*
  20266. * If we are going to sleep and we have plugged IO queued,
  20267. * make sure to submit it to avoid deadlocks.
  20268. */
  20269. @@ -3225,6 +3414,12 @@
  20270. blk_schedule_flush_plug(tsk);
  20271. }
  20272. +static void sched_update_worker(struct task_struct *tsk)
  20273. +{
  20274. + if (tsk->flags & PF_WQ_WORKER)
  20275. + wq_worker_running(tsk);
  20276. +}
  20277. +
  20278. asmlinkage __visible void __sched schedule(void)
  20279. {
  20280. struct task_struct *tsk = current;
  20281. @@ -3235,6 +3430,7 @@
  20282. __schedule(false);
  20283. sched_preempt_enable_no_resched();
  20284. } while (need_resched());
  20285. + sched_update_worker(tsk);
  20286. }
  20287. EXPORT_SYMBOL(schedule);
  20288. @@ -3283,6 +3479,30 @@
  20289. } while (need_resched());
  20290. }
  20291. +#ifdef CONFIG_PREEMPT_LAZY
  20292. +/*
  20293. + * If TIF_NEED_RESCHED is then we allow to be scheduled away since this is
  20294. + * set by a RT task. Oterwise we try to avoid beeing scheduled out as long as
  20295. + * preempt_lazy_count counter >0.
  20296. + */
  20297. +static __always_inline int preemptible_lazy(void)
  20298. +{
  20299. + if (test_thread_flag(TIF_NEED_RESCHED))
  20300. + return 1;
  20301. + if (current_thread_info()->preempt_lazy_count)
  20302. + return 0;
  20303. + return 1;
  20304. +}
  20305. +
  20306. +#else
  20307. +
  20308. +static inline int preemptible_lazy(void)
  20309. +{
  20310. + return 1;
  20311. +}
  20312. +
  20313. +#endif
  20314. +
  20315. #ifdef CONFIG_PREEMPT
  20316. /*
  20317. * this is the entry point to schedule() from in-kernel preemption
  20318. @@ -3297,6 +3517,8 @@
  20319. */
  20320. if (likely(!preemptible()))
  20321. return;
  20322. + if (!preemptible_lazy())
  20323. + return;
  20324. preempt_schedule_common();
  20325. }
  20326. @@ -3323,6 +3545,8 @@
  20327. if (likely(!preemptible()))
  20328. return;
  20329. + if (!preemptible_lazy())
  20330. + return;
  20331. do {
  20332. preempt_disable_notrace();
  20333. @@ -3332,7 +3556,16 @@
  20334. * an infinite recursion.
  20335. */
  20336. prev_ctx = exception_enter();
  20337. + /*
  20338. + * The add/subtract must not be traced by the function
  20339. + * tracer. But we still want to account for the
  20340. + * preempt off latency tracer. Since the _notrace versions
  20341. + * of add/subtract skip the accounting for latency tracer
  20342. + * we must force it manually.
  20343. + */
  20344. + start_critical_timings();
  20345. __schedule(true);
  20346. + stop_critical_timings();
  20347. exception_exit(prev_ctx);
  20348. preempt_enable_no_resched_notrace();
  20349. @@ -4676,6 +4909,7 @@
  20350. }
  20351. EXPORT_SYMBOL(__cond_resched_lock);
  20352. +#ifndef CONFIG_PREEMPT_RT_FULL
  20353. int __sched __cond_resched_softirq(void)
  20354. {
  20355. BUG_ON(!in_softirq());
  20356. @@ -4689,6 +4923,7 @@
  20357. return 0;
  20358. }
  20359. EXPORT_SYMBOL(__cond_resched_softirq);
  20360. +#endif
  20361. /**
  20362. * yield - yield the current processor to other threads.
  20363. @@ -5055,7 +5290,9 @@
  20364. /* Set the preempt count _outside_ the spinlocks! */
  20365. init_idle_preempt_count(idle, cpu);
  20366. -
  20367. +#ifdef CONFIG_HAVE_PREEMPT_LAZY
  20368. + task_thread_info(idle)->preempt_lazy_count = 0;
  20369. +#endif
  20370. /*
  20371. * The idle tasks have their own, simple scheduling class:
  20372. */
  20373. @@ -5196,6 +5433,8 @@
  20374. #endif /* CONFIG_NUMA_BALANCING */
  20375. #ifdef CONFIG_HOTPLUG_CPU
  20376. +static DEFINE_PER_CPU(struct mm_struct *, idle_last_mm);
  20377. +
  20378. /*
  20379. * Ensures that the idle task is using init_mm right before its cpu goes
  20380. * offline.
  20381. @@ -5210,7 +5449,11 @@
  20382. switch_mm(mm, &init_mm, current);
  20383. finish_arch_post_lock_switch();
  20384. }
  20385. - mmdrop(mm);
  20386. + /*
  20387. + * Defer the cleanup to an alive cpu. On RT we can neither
  20388. + * call mmdrop() nor mmdrop_delayed() from here.
  20389. + */
  20390. + per_cpu(idle_last_mm, smp_processor_id()) = mm;
  20391. }
  20392. /*
  20393. @@ -5583,6 +5826,10 @@
  20394. case CPU_DEAD:
  20395. calc_load_migrate(rq);
  20396. + if (per_cpu(idle_last_mm, cpu)) {
  20397. + mmdrop(per_cpu(idle_last_mm, cpu));
  20398. + per_cpu(idle_last_mm, cpu) = NULL;
  20399. + }
  20400. break;
  20401. #endif
  20402. }
  20403. @@ -7566,7 +7813,7 @@
  20404. #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
  20405. static inline int preempt_count_equals(int preempt_offset)
  20406. {
  20407. - int nested = preempt_count() + rcu_preempt_depth();
  20408. + int nested = preempt_count() + sched_rcu_preempt_depth();
  20409. return (nested == preempt_offset);
  20410. }
  20411. diff -Nur linux-4.4.46.orig/kernel/sched/cpudeadline.c linux-4.4.46/kernel/sched/cpudeadline.c
  20412. --- linux-4.4.46.orig/kernel/sched/cpudeadline.c 2017-02-01 08:31:11.000000000 +0100
  20413. +++ linux-4.4.46/kernel/sched/cpudeadline.c 2017-02-03 17:18:10.931619212 +0100
  20414. @@ -103,10 +103,10 @@
  20415. const struct sched_dl_entity *dl_se = &p->dl;
  20416. if (later_mask &&
  20417. - cpumask_and(later_mask, cp->free_cpus, &p->cpus_allowed)) {
  20418. + cpumask_and(later_mask, cp->free_cpus, tsk_cpus_allowed(p))) {
  20419. best_cpu = cpumask_any(later_mask);
  20420. goto out;
  20421. - } else if (cpumask_test_cpu(cpudl_maximum(cp), &p->cpus_allowed) &&
  20422. + } else if (cpumask_test_cpu(cpudl_maximum(cp), tsk_cpus_allowed(p)) &&
  20423. dl_time_before(dl_se->deadline, cp->elements[0].dl)) {
  20424. best_cpu = cpudl_maximum(cp);
  20425. if (later_mask)
  20426. diff -Nur linux-4.4.46.orig/kernel/sched/cpupri.c linux-4.4.46/kernel/sched/cpupri.c
  20427. --- linux-4.4.46.orig/kernel/sched/cpupri.c 2017-02-01 08:31:11.000000000 +0100
  20428. +++ linux-4.4.46/kernel/sched/cpupri.c 2017-02-03 17:18:10.931619212 +0100
  20429. @@ -103,11 +103,11 @@
  20430. if (skip)
  20431. continue;
  20432. - if (cpumask_any_and(&p->cpus_allowed, vec->mask) >= nr_cpu_ids)
  20433. + if (cpumask_any_and(tsk_cpus_allowed(p), vec->mask) >= nr_cpu_ids)
  20434. continue;
  20435. if (lowest_mask) {
  20436. - cpumask_and(lowest_mask, &p->cpus_allowed, vec->mask);
  20437. + cpumask_and(lowest_mask, tsk_cpus_allowed(p), vec->mask);
  20438. /*
  20439. * We have to ensure that we have at least one bit
  20440. diff -Nur linux-4.4.46.orig/kernel/sched/cputime.c linux-4.4.46/kernel/sched/cputime.c
  20441. --- linux-4.4.46.orig/kernel/sched/cputime.c 2017-02-01 08:31:11.000000000 +0100
  20442. +++ linux-4.4.46/kernel/sched/cputime.c 2017-02-03 17:18:10.931619212 +0100
  20443. @@ -685,7 +685,7 @@
  20444. {
  20445. unsigned long long delta = vtime_delta(tsk);
  20446. - WARN_ON_ONCE(tsk->vtime_snap_whence == VTIME_SLEEPING);
  20447. + WARN_ON_ONCE(tsk->vtime_snap_whence == VTIME_INACTIVE);
  20448. tsk->vtime_snap += delta;
  20449. /* CHECKME: always safe to convert nsecs to cputime? */
  20450. @@ -701,37 +701,37 @@
  20451. void vtime_account_system(struct task_struct *tsk)
  20452. {
  20453. - write_seqlock(&tsk->vtime_seqlock);
  20454. + write_seqcount_begin(&tsk->vtime_seqcount);
  20455. __vtime_account_system(tsk);
  20456. - write_sequnlock(&tsk->vtime_seqlock);
  20457. + write_seqcount_end(&tsk->vtime_seqcount);
  20458. }
  20459. void vtime_gen_account_irq_exit(struct task_struct *tsk)
  20460. {
  20461. - write_seqlock(&tsk->vtime_seqlock);
  20462. + write_seqcount_begin(&tsk->vtime_seqcount);
  20463. __vtime_account_system(tsk);
  20464. if (context_tracking_in_user())
  20465. tsk->vtime_snap_whence = VTIME_USER;
  20466. - write_sequnlock(&tsk->vtime_seqlock);
  20467. + write_seqcount_end(&tsk->vtime_seqcount);
  20468. }
  20469. void vtime_account_user(struct task_struct *tsk)
  20470. {
  20471. cputime_t delta_cpu;
  20472. - write_seqlock(&tsk->vtime_seqlock);
  20473. + write_seqcount_begin(&tsk->vtime_seqcount);
  20474. delta_cpu = get_vtime_delta(tsk);
  20475. tsk->vtime_snap_whence = VTIME_SYS;
  20476. account_user_time(tsk, delta_cpu, cputime_to_scaled(delta_cpu));
  20477. - write_sequnlock(&tsk->vtime_seqlock);
  20478. + write_seqcount_end(&tsk->vtime_seqcount);
  20479. }
  20480. void vtime_user_enter(struct task_struct *tsk)
  20481. {
  20482. - write_seqlock(&tsk->vtime_seqlock);
  20483. + write_seqcount_begin(&tsk->vtime_seqcount);
  20484. __vtime_account_system(tsk);
  20485. tsk->vtime_snap_whence = VTIME_USER;
  20486. - write_sequnlock(&tsk->vtime_seqlock);
  20487. + write_seqcount_end(&tsk->vtime_seqcount);
  20488. }
  20489. void vtime_guest_enter(struct task_struct *tsk)
  20490. @@ -743,19 +743,19 @@
  20491. * synchronization against the reader (task_gtime())
  20492. * that can thus safely catch up with a tickless delta.
  20493. */
  20494. - write_seqlock(&tsk->vtime_seqlock);
  20495. + write_seqcount_begin(&tsk->vtime_seqcount);
  20496. __vtime_account_system(tsk);
  20497. current->flags |= PF_VCPU;
  20498. - write_sequnlock(&tsk->vtime_seqlock);
  20499. + write_seqcount_end(&tsk->vtime_seqcount);
  20500. }
  20501. EXPORT_SYMBOL_GPL(vtime_guest_enter);
  20502. void vtime_guest_exit(struct task_struct *tsk)
  20503. {
  20504. - write_seqlock(&tsk->vtime_seqlock);
  20505. + write_seqcount_begin(&tsk->vtime_seqcount);
  20506. __vtime_account_system(tsk);
  20507. current->flags &= ~PF_VCPU;
  20508. - write_sequnlock(&tsk->vtime_seqlock);
  20509. + write_seqcount_end(&tsk->vtime_seqcount);
  20510. }
  20511. EXPORT_SYMBOL_GPL(vtime_guest_exit);
  20512. @@ -768,24 +768,26 @@
  20513. void arch_vtime_task_switch(struct task_struct *prev)
  20514. {
  20515. - write_seqlock(&prev->vtime_seqlock);
  20516. - prev->vtime_snap_whence = VTIME_SLEEPING;
  20517. - write_sequnlock(&prev->vtime_seqlock);
  20518. + write_seqcount_begin(&prev->vtime_seqcount);
  20519. + prev->vtime_snap_whence = VTIME_INACTIVE;
  20520. + write_seqcount_end(&prev->vtime_seqcount);
  20521. - write_seqlock(&current->vtime_seqlock);
  20522. + write_seqcount_begin(&current->vtime_seqcount);
  20523. current->vtime_snap_whence = VTIME_SYS;
  20524. current->vtime_snap = sched_clock_cpu(smp_processor_id());
  20525. - write_sequnlock(&current->vtime_seqlock);
  20526. + write_seqcount_end(&current->vtime_seqcount);
  20527. }
  20528. void vtime_init_idle(struct task_struct *t, int cpu)
  20529. {
  20530. unsigned long flags;
  20531. - write_seqlock_irqsave(&t->vtime_seqlock, flags);
  20532. + local_irq_save(flags);
  20533. + write_seqcount_begin(&t->vtime_seqcount);
  20534. t->vtime_snap_whence = VTIME_SYS;
  20535. t->vtime_snap = sched_clock_cpu(cpu);
  20536. - write_sequnlock_irqrestore(&t->vtime_seqlock, flags);
  20537. + write_seqcount_end(&t->vtime_seqcount);
  20538. + local_irq_restore(flags);
  20539. }
  20540. cputime_t task_gtime(struct task_struct *t)
  20541. @@ -797,13 +799,13 @@
  20542. return t->gtime;
  20543. do {
  20544. - seq = read_seqbegin(&t->vtime_seqlock);
  20545. + seq = read_seqcount_begin(&t->vtime_seqcount);
  20546. gtime = t->gtime;
  20547. if (t->flags & PF_VCPU)
  20548. gtime += vtime_delta(t);
  20549. - } while (read_seqretry(&t->vtime_seqlock, seq));
  20550. + } while (read_seqcount_retry(&t->vtime_seqcount, seq));
  20551. return gtime;
  20552. }
  20553. @@ -826,7 +828,7 @@
  20554. *udelta = 0;
  20555. *sdelta = 0;
  20556. - seq = read_seqbegin(&t->vtime_seqlock);
  20557. + seq = read_seqcount_begin(&t->vtime_seqcount);
  20558. if (u_dst)
  20559. *u_dst = *u_src;
  20560. @@ -834,7 +836,7 @@
  20561. *s_dst = *s_src;
  20562. /* Task is sleeping, nothing to add */
  20563. - if (t->vtime_snap_whence == VTIME_SLEEPING ||
  20564. + if (t->vtime_snap_whence == VTIME_INACTIVE ||
  20565. is_idle_task(t))
  20566. continue;
  20567. @@ -850,7 +852,7 @@
  20568. if (t->vtime_snap_whence == VTIME_SYS)
  20569. *sdelta = delta;
  20570. }
  20571. - } while (read_seqretry(&t->vtime_seqlock, seq));
  20572. + } while (read_seqcount_retry(&t->vtime_seqcount, seq));
  20573. }
  20574. diff -Nur linux-4.4.46.orig/kernel/sched/deadline.c linux-4.4.46/kernel/sched/deadline.c
  20575. --- linux-4.4.46.orig/kernel/sched/deadline.c 2017-02-01 08:31:11.000000000 +0100
  20576. +++ linux-4.4.46/kernel/sched/deadline.c 2017-02-03 17:18:10.935619367 +0100
  20577. @@ -134,7 +134,7 @@
  20578. {
  20579. struct task_struct *p = dl_task_of(dl_se);
  20580. - if (p->nr_cpus_allowed > 1)
  20581. + if (tsk_nr_cpus_allowed(p) > 1)
  20582. dl_rq->dl_nr_migratory++;
  20583. update_dl_migration(dl_rq);
  20584. @@ -144,7 +144,7 @@
  20585. {
  20586. struct task_struct *p = dl_task_of(dl_se);
  20587. - if (p->nr_cpus_allowed > 1)
  20588. + if (tsk_nr_cpus_allowed(p) > 1)
  20589. dl_rq->dl_nr_migratory--;
  20590. update_dl_migration(dl_rq);
  20591. @@ -697,6 +697,7 @@
  20592. hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  20593. timer->function = dl_task_timer;
  20594. + timer->irqsafe = 1;
  20595. }
  20596. static
  20597. @@ -989,7 +990,7 @@
  20598. enqueue_dl_entity(&p->dl, pi_se, flags);
  20599. - if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
  20600. + if (!task_current(rq, p) && tsk_nr_cpus_allowed(p) > 1)
  20601. enqueue_pushable_dl_task(rq, p);
  20602. }
  20603. @@ -1067,9 +1068,9 @@
  20604. * try to make it stay here, it might be important.
  20605. */
  20606. if (unlikely(dl_task(curr)) &&
  20607. - (curr->nr_cpus_allowed < 2 ||
  20608. + (tsk_nr_cpus_allowed(curr) < 2 ||
  20609. !dl_entity_preempt(&p->dl, &curr->dl)) &&
  20610. - (p->nr_cpus_allowed > 1)) {
  20611. + (tsk_nr_cpus_allowed(p) > 1)) {
  20612. int target = find_later_rq(p);
  20613. if (target != -1 &&
  20614. @@ -1090,7 +1091,7 @@
  20615. * Current can't be migrated, useless to reschedule,
  20616. * let's hope p can move out.
  20617. */
  20618. - if (rq->curr->nr_cpus_allowed == 1 ||
  20619. + if (tsk_nr_cpus_allowed(rq->curr) == 1 ||
  20620. cpudl_find(&rq->rd->cpudl, rq->curr, NULL) == -1)
  20621. return;
  20622. @@ -1098,7 +1099,7 @@
  20623. * p is migratable, so let's not schedule it and
  20624. * see if it is pushed or pulled somewhere else.
  20625. */
  20626. - if (p->nr_cpus_allowed != 1 &&
  20627. + if (tsk_nr_cpus_allowed(p) != 1 &&
  20628. cpudl_find(&rq->rd->cpudl, p, NULL) != -1)
  20629. return;
  20630. @@ -1212,7 +1213,7 @@
  20631. {
  20632. update_curr_dl(rq);
  20633. - if (on_dl_rq(&p->dl) && p->nr_cpus_allowed > 1)
  20634. + if (on_dl_rq(&p->dl) && tsk_nr_cpus_allowed(p) > 1)
  20635. enqueue_pushable_dl_task(rq, p);
  20636. }
  20637. @@ -1335,7 +1336,7 @@
  20638. if (unlikely(!later_mask))
  20639. return -1;
  20640. - if (task->nr_cpus_allowed == 1)
  20641. + if (tsk_nr_cpus_allowed(task) == 1)
  20642. return -1;
  20643. /*
  20644. @@ -1441,7 +1442,7 @@
  20645. if (double_lock_balance(rq, later_rq)) {
  20646. if (unlikely(task_rq(task) != rq ||
  20647. !cpumask_test_cpu(later_rq->cpu,
  20648. - &task->cpus_allowed) ||
  20649. + tsk_cpus_allowed(task)) ||
  20650. task_running(rq, task) ||
  20651. !task_on_rq_queued(task))) {
  20652. double_unlock_balance(rq, later_rq);
  20653. @@ -1480,7 +1481,7 @@
  20654. BUG_ON(rq->cpu != task_cpu(p));
  20655. BUG_ON(task_current(rq, p));
  20656. - BUG_ON(p->nr_cpus_allowed <= 1);
  20657. + BUG_ON(tsk_nr_cpus_allowed(p) <= 1);
  20658. BUG_ON(!task_on_rq_queued(p));
  20659. BUG_ON(!dl_task(p));
  20660. @@ -1519,7 +1520,7 @@
  20661. */
  20662. if (dl_task(rq->curr) &&
  20663. dl_time_before(next_task->dl.deadline, rq->curr->dl.deadline) &&
  20664. - rq->curr->nr_cpus_allowed > 1) {
  20665. + tsk_nr_cpus_allowed(rq->curr) > 1) {
  20666. resched_curr(rq);
  20667. return 0;
  20668. }
  20669. @@ -1666,9 +1667,9 @@
  20670. {
  20671. if (!task_running(rq, p) &&
  20672. !test_tsk_need_resched(rq->curr) &&
  20673. - p->nr_cpus_allowed > 1 &&
  20674. + tsk_nr_cpus_allowed(p) > 1 &&
  20675. dl_task(rq->curr) &&
  20676. - (rq->curr->nr_cpus_allowed < 2 ||
  20677. + (tsk_nr_cpus_allowed(rq->curr) < 2 ||
  20678. !dl_entity_preempt(&p->dl, &rq->curr->dl))) {
  20679. push_dl_tasks(rq);
  20680. }
  20681. @@ -1769,7 +1770,7 @@
  20682. {
  20683. if (task_on_rq_queued(p) && rq->curr != p) {
  20684. #ifdef CONFIG_SMP
  20685. - if (p->nr_cpus_allowed > 1 && rq->dl.overloaded)
  20686. + if (tsk_nr_cpus_allowed(p) > 1 && rq->dl.overloaded)
  20687. queue_push_tasks(rq);
  20688. #else
  20689. if (dl_task(rq->curr))
  20690. diff -Nur linux-4.4.46.orig/kernel/sched/debug.c linux-4.4.46/kernel/sched/debug.c
  20691. --- linux-4.4.46.orig/kernel/sched/debug.c 2017-02-01 08:31:11.000000000 +0100
  20692. +++ linux-4.4.46/kernel/sched/debug.c 2017-02-03 17:18:10.935619367 +0100
  20693. @@ -251,6 +251,9 @@
  20694. P(rt_throttled);
  20695. PN(rt_time);
  20696. PN(rt_runtime);
  20697. +#ifdef CONFIG_SMP
  20698. + P(rt_nr_migratory);
  20699. +#endif
  20700. #undef PN
  20701. #undef P
  20702. @@ -635,6 +638,10 @@
  20703. #endif
  20704. P(policy);
  20705. P(prio);
  20706. +#ifdef CONFIG_PREEMPT_RT_FULL
  20707. + P(migrate_disable);
  20708. +#endif
  20709. + P(nr_cpus_allowed);
  20710. #undef PN
  20711. #undef __PN
  20712. #undef P
  20713. diff -Nur linux-4.4.46.orig/kernel/sched/fair.c linux-4.4.46/kernel/sched/fair.c
  20714. --- linux-4.4.46.orig/kernel/sched/fair.c 2017-02-01 08:31:11.000000000 +0100
  20715. +++ linux-4.4.46/kernel/sched/fair.c 2017-02-03 17:18:10.935619367 +0100
  20716. @@ -3166,7 +3166,7 @@
  20717. ideal_runtime = sched_slice(cfs_rq, curr);
  20718. delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
  20719. if (delta_exec > ideal_runtime) {
  20720. - resched_curr(rq_of(cfs_rq));
  20721. + resched_curr_lazy(rq_of(cfs_rq));
  20722. /*
  20723. * The current task ran long enough, ensure it doesn't get
  20724. * re-elected due to buddy favours.
  20725. @@ -3190,7 +3190,7 @@
  20726. return;
  20727. if (delta > ideal_runtime)
  20728. - resched_curr(rq_of(cfs_rq));
  20729. + resched_curr_lazy(rq_of(cfs_rq));
  20730. }
  20731. static void
  20732. @@ -3330,7 +3330,7 @@
  20733. * validating it and just reschedule.
  20734. */
  20735. if (queued) {
  20736. - resched_curr(rq_of(cfs_rq));
  20737. + resched_curr_lazy(rq_of(cfs_rq));
  20738. return;
  20739. }
  20740. /*
  20741. @@ -3512,7 +3512,7 @@
  20742. * hierarchy can be throttled
  20743. */
  20744. if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr))
  20745. - resched_curr(rq_of(cfs_rq));
  20746. + resched_curr_lazy(rq_of(cfs_rq));
  20747. }
  20748. static __always_inline
  20749. @@ -4124,7 +4124,7 @@
  20750. if (delta < 0) {
  20751. if (rq->curr == p)
  20752. - resched_curr(rq);
  20753. + resched_curr_lazy(rq);
  20754. return;
  20755. }
  20756. hrtick_start(rq, delta);
  20757. @@ -5213,7 +5213,7 @@
  20758. return;
  20759. preempt:
  20760. - resched_curr(rq);
  20761. + resched_curr_lazy(rq);
  20762. /*
  20763. * Only set the backward buddy when the current task is still
  20764. * on the rq. This can happen when a wakeup gets interleaved
  20765. @@ -7964,7 +7964,7 @@
  20766. * 'current' within the tree based on its new key value.
  20767. */
  20768. swap(curr->vruntime, se->vruntime);
  20769. - resched_curr(rq);
  20770. + resched_curr_lazy(rq);
  20771. }
  20772. se->vruntime -= cfs_rq->min_vruntime;
  20773. @@ -7989,7 +7989,7 @@
  20774. */
  20775. if (rq->curr == p) {
  20776. if (p->prio > oldprio)
  20777. - resched_curr(rq);
  20778. + resched_curr_lazy(rq);
  20779. } else
  20780. check_preempt_curr(rq, p, 0);
  20781. }
  20782. diff -Nur linux-4.4.46.orig/kernel/sched/features.h linux-4.4.46/kernel/sched/features.h
  20783. --- linux-4.4.46.orig/kernel/sched/features.h 2017-02-01 08:31:11.000000000 +0100
  20784. +++ linux-4.4.46/kernel/sched/features.h 2017-02-03 17:18:10.935619367 +0100
  20785. @@ -45,11 +45,19 @@
  20786. */
  20787. SCHED_FEAT(NONTASK_CAPACITY, true)
  20788. +#ifdef CONFIG_PREEMPT_RT_FULL
  20789. +SCHED_FEAT(TTWU_QUEUE, false)
  20790. +# ifdef CONFIG_PREEMPT_LAZY
  20791. +SCHED_FEAT(PREEMPT_LAZY, true)
  20792. +# endif
  20793. +#else
  20794. +
  20795. /*
  20796. * Queue remote wakeups on the target CPU and process them
  20797. * using the scheduler IPI. Reduces rq->lock contention/bounces.
  20798. */
  20799. SCHED_FEAT(TTWU_QUEUE, true)
  20800. +#endif
  20801. #ifdef HAVE_RT_PUSH_IPI
  20802. /*
  20803. diff -Nur linux-4.4.46.orig/kernel/sched/Makefile linux-4.4.46/kernel/sched/Makefile
  20804. --- linux-4.4.46.orig/kernel/sched/Makefile 2017-02-01 08:31:11.000000000 +0100
  20805. +++ linux-4.4.46/kernel/sched/Makefile 2017-02-03 17:18:10.931619212 +0100
  20806. @@ -13,7 +13,7 @@
  20807. obj-y += core.o loadavg.o clock.o cputime.o
  20808. obj-y += idle_task.o fair.o rt.o deadline.o stop_task.o
  20809. -obj-y += wait.o completion.o idle.o
  20810. +obj-y += wait.o swait.o swork.o completion.o idle.o
  20811. obj-$(CONFIG_SMP) += cpupri.o cpudeadline.o
  20812. obj-$(CONFIG_SCHED_AUTOGROUP) += auto_group.o
  20813. obj-$(CONFIG_SCHEDSTATS) += stats.o
  20814. diff -Nur linux-4.4.46.orig/kernel/sched/rt.c linux-4.4.46/kernel/sched/rt.c
  20815. --- linux-4.4.46.orig/kernel/sched/rt.c 2017-02-01 08:31:11.000000000 +0100
  20816. +++ linux-4.4.46/kernel/sched/rt.c 2017-02-03 17:18:10.935619367 +0100
  20817. @@ -47,6 +47,7 @@
  20818. hrtimer_init(&rt_b->rt_period_timer,
  20819. CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  20820. + rt_b->rt_period_timer.irqsafe = 1;
  20821. rt_b->rt_period_timer.function = sched_rt_period_timer;
  20822. }
  20823. @@ -93,6 +94,7 @@
  20824. rt_rq->push_cpu = nr_cpu_ids;
  20825. raw_spin_lock_init(&rt_rq->push_lock);
  20826. init_irq_work(&rt_rq->push_work, push_irq_work_func);
  20827. + rt_rq->push_work.flags |= IRQ_WORK_HARD_IRQ;
  20828. #endif
  20829. #endif /* CONFIG_SMP */
  20830. /* We start is dequeued state, because no RT tasks are queued */
  20831. @@ -326,7 +328,7 @@
  20832. rt_rq = &rq_of_rt_rq(rt_rq)->rt;
  20833. rt_rq->rt_nr_total++;
  20834. - if (p->nr_cpus_allowed > 1)
  20835. + if (tsk_nr_cpus_allowed(p) > 1)
  20836. rt_rq->rt_nr_migratory++;
  20837. update_rt_migration(rt_rq);
  20838. @@ -343,7 +345,7 @@
  20839. rt_rq = &rq_of_rt_rq(rt_rq)->rt;
  20840. rt_rq->rt_nr_total--;
  20841. - if (p->nr_cpus_allowed > 1)
  20842. + if (tsk_nr_cpus_allowed(p) > 1)
  20843. rt_rq->rt_nr_migratory--;
  20844. update_rt_migration(rt_rq);
  20845. @@ -1262,7 +1264,7 @@
  20846. enqueue_rt_entity(rt_se, flags & ENQUEUE_HEAD);
  20847. - if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
  20848. + if (!task_current(rq, p) && tsk_nr_cpus_allowed(p) > 1)
  20849. enqueue_pushable_task(rq, p);
  20850. }
  20851. @@ -1351,7 +1353,7 @@
  20852. * will have to sort it out.
  20853. */
  20854. if (curr && unlikely(rt_task(curr)) &&
  20855. - (curr->nr_cpus_allowed < 2 ||
  20856. + (tsk_nr_cpus_allowed(curr) < 2 ||
  20857. curr->prio <= p->prio)) {
  20858. int target = find_lowest_rq(p);
  20859. @@ -1375,7 +1377,7 @@
  20860. * Current can't be migrated, useless to reschedule,
  20861. * let's hope p can move out.
  20862. */
  20863. - if (rq->curr->nr_cpus_allowed == 1 ||
  20864. + if (tsk_nr_cpus_allowed(rq->curr) == 1 ||
  20865. !cpupri_find(&rq->rd->cpupri, rq->curr, NULL))
  20866. return;
  20867. @@ -1383,7 +1385,7 @@
  20868. * p is migratable, so let's not schedule it and
  20869. * see if it is pushed or pulled somewhere else.
  20870. */
  20871. - if (p->nr_cpus_allowed != 1
  20872. + if (tsk_nr_cpus_allowed(p) != 1
  20873. && cpupri_find(&rq->rd->cpupri, p, NULL))
  20874. return;
  20875. @@ -1517,7 +1519,7 @@
  20876. * The previous task needs to be made eligible for pushing
  20877. * if it is still active
  20878. */
  20879. - if (on_rt_rq(&p->rt) && p->nr_cpus_allowed > 1)
  20880. + if (on_rt_rq(&p->rt) && tsk_nr_cpus_allowed(p) > 1)
  20881. enqueue_pushable_task(rq, p);
  20882. }
  20883. @@ -1567,7 +1569,7 @@
  20884. if (unlikely(!lowest_mask))
  20885. return -1;
  20886. - if (task->nr_cpus_allowed == 1)
  20887. + if (tsk_nr_cpus_allowed(task) == 1)
  20888. return -1; /* No other targets possible */
  20889. if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask))
  20890. @@ -1699,7 +1701,7 @@
  20891. BUG_ON(rq->cpu != task_cpu(p));
  20892. BUG_ON(task_current(rq, p));
  20893. - BUG_ON(p->nr_cpus_allowed <= 1);
  20894. + BUG_ON(tsk_nr_cpus_allowed(p) <= 1);
  20895. BUG_ON(!task_on_rq_queued(p));
  20896. BUG_ON(!rt_task(p));
  20897. @@ -2059,9 +2061,9 @@
  20898. {
  20899. if (!task_running(rq, p) &&
  20900. !test_tsk_need_resched(rq->curr) &&
  20901. - p->nr_cpus_allowed > 1 &&
  20902. + tsk_nr_cpus_allowed(p) > 1 &&
  20903. (dl_task(rq->curr) || rt_task(rq->curr)) &&
  20904. - (rq->curr->nr_cpus_allowed < 2 ||
  20905. + (tsk_nr_cpus_allowed(rq->curr) < 2 ||
  20906. rq->curr->prio <= p->prio))
  20907. push_rt_tasks(rq);
  20908. }
  20909. @@ -2134,7 +2136,7 @@
  20910. */
  20911. if (task_on_rq_queued(p) && rq->curr != p) {
  20912. #ifdef CONFIG_SMP
  20913. - if (p->nr_cpus_allowed > 1 && rq->rt.overloaded)
  20914. + if (tsk_nr_cpus_allowed(p) > 1 && rq->rt.overloaded)
  20915. queue_push_tasks(rq);
  20916. #else
  20917. if (p->prio < rq->curr->prio)
  20918. diff -Nur linux-4.4.46.orig/kernel/sched/sched.h linux-4.4.46/kernel/sched/sched.h
  20919. --- linux-4.4.46.orig/kernel/sched/sched.h 2017-02-01 08:31:11.000000000 +0100
  20920. +++ linux-4.4.46/kernel/sched/sched.h 2017-02-03 17:18:10.935619367 +0100
  20921. @@ -1100,6 +1100,7 @@
  20922. #define WF_SYNC 0x01 /* waker goes to sleep after wakeup */
  20923. #define WF_FORK 0x02 /* child wakeup after fork */
  20924. #define WF_MIGRATED 0x4 /* internal use, task got migrated */
  20925. +#define WF_LOCK_SLEEPER 0x08 /* wakeup spinlock "sleeper" */
  20926. /*
  20927. * To aid in avoiding the subversion of "niceness" due to uneven distribution
  20928. @@ -1299,6 +1300,15 @@
  20929. extern void resched_curr(struct rq *rq);
  20930. extern void resched_cpu(int cpu);
  20931. +#ifdef CONFIG_PREEMPT_LAZY
  20932. +extern void resched_curr_lazy(struct rq *rq);
  20933. +#else
  20934. +static inline void resched_curr_lazy(struct rq *rq)
  20935. +{
  20936. + resched_curr(rq);
  20937. +}
  20938. +#endif
  20939. +
  20940. extern struct rt_bandwidth def_rt_bandwidth;
  20941. extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
  20942. diff -Nur linux-4.4.46.orig/kernel/sched/swait.c linux-4.4.46/kernel/sched/swait.c
  20943. --- linux-4.4.46.orig/kernel/sched/swait.c 1970-01-01 01:00:00.000000000 +0100
  20944. +++ linux-4.4.46/kernel/sched/swait.c 2017-02-03 17:18:10.935619367 +0100
  20945. @@ -0,0 +1,143 @@
  20946. +#include <linux/sched.h>
  20947. +#include <linux/swait.h>
  20948. +#include <linux/suspend.h>
  20949. +
  20950. +void __init_swait_queue_head(struct swait_queue_head *q, const char *name,
  20951. + struct lock_class_key *key)
  20952. +{
  20953. + raw_spin_lock_init(&q->lock);
  20954. + lockdep_set_class_and_name(&q->lock, key, name);
  20955. + INIT_LIST_HEAD(&q->task_list);
  20956. +}
  20957. +EXPORT_SYMBOL(__init_swait_queue_head);
  20958. +
  20959. +/*
  20960. + * The thing about the wake_up_state() return value; I think we can ignore it.
  20961. + *
  20962. + * If for some reason it would return 0, that means the previously waiting
  20963. + * task is already running, so it will observe condition true (or has already).
  20964. + */
  20965. +void swake_up_locked(struct swait_queue_head *q)
  20966. +{
  20967. + struct swait_queue *curr;
  20968. +
  20969. + if (list_empty(&q->task_list))
  20970. + return;
  20971. +
  20972. + curr = list_first_entry(&q->task_list, typeof(*curr), task_list);
  20973. + wake_up_process(curr->task);
  20974. + list_del_init(&curr->task_list);
  20975. +}
  20976. +EXPORT_SYMBOL(swake_up_locked);
  20977. +
  20978. +void swake_up_all_locked(struct swait_queue_head *q)
  20979. +{
  20980. + struct swait_queue *curr;
  20981. + int wakes = 0;
  20982. +
  20983. + while (!list_empty(&q->task_list)) {
  20984. +
  20985. + curr = list_first_entry(&q->task_list, typeof(*curr),
  20986. + task_list);
  20987. + wake_up_process(curr->task);
  20988. + list_del_init(&curr->task_list);
  20989. + wakes++;
  20990. + }
  20991. + if (pm_in_action)
  20992. + return;
  20993. + WARN(wakes > 2, "complate_all() with %d waiters\n", wakes);
  20994. +}
  20995. +EXPORT_SYMBOL(swake_up_all_locked);
  20996. +
  20997. +void swake_up(struct swait_queue_head *q)
  20998. +{
  20999. + unsigned long flags;
  21000. +
  21001. + if (!swait_active(q))
  21002. + return;
  21003. +
  21004. + raw_spin_lock_irqsave(&q->lock, flags);
  21005. + swake_up_locked(q);
  21006. + raw_spin_unlock_irqrestore(&q->lock, flags);
  21007. +}
  21008. +EXPORT_SYMBOL(swake_up);
  21009. +
  21010. +/*
  21011. + * Does not allow usage from IRQ disabled, since we must be able to
  21012. + * release IRQs to guarantee bounded hold time.
  21013. + */
  21014. +void swake_up_all(struct swait_queue_head *q)
  21015. +{
  21016. + struct swait_queue *curr;
  21017. + LIST_HEAD(tmp);
  21018. +
  21019. + if (!swait_active(q))
  21020. + return;
  21021. +
  21022. + raw_spin_lock_irq(&q->lock);
  21023. + list_splice_init(&q->task_list, &tmp);
  21024. + while (!list_empty(&tmp)) {
  21025. + curr = list_first_entry(&tmp, typeof(*curr), task_list);
  21026. +
  21027. + wake_up_state(curr->task, TASK_NORMAL);
  21028. + list_del_init(&curr->task_list);
  21029. +
  21030. + if (list_empty(&tmp))
  21031. + break;
  21032. +
  21033. + raw_spin_unlock_irq(&q->lock);
  21034. + raw_spin_lock_irq(&q->lock);
  21035. + }
  21036. + raw_spin_unlock_irq(&q->lock);
  21037. +}
  21038. +EXPORT_SYMBOL(swake_up_all);
  21039. +
  21040. +void __prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait)
  21041. +{
  21042. + wait->task = current;
  21043. + if (list_empty(&wait->task_list))
  21044. + list_add(&wait->task_list, &q->task_list);
  21045. +}
  21046. +
  21047. +void prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait, int state)
  21048. +{
  21049. + unsigned long flags;
  21050. +
  21051. + raw_spin_lock_irqsave(&q->lock, flags);
  21052. + __prepare_to_swait(q, wait);
  21053. + set_current_state(state);
  21054. + raw_spin_unlock_irqrestore(&q->lock, flags);
  21055. +}
  21056. +EXPORT_SYMBOL(prepare_to_swait);
  21057. +
  21058. +long prepare_to_swait_event(struct swait_queue_head *q, struct swait_queue *wait, int state)
  21059. +{
  21060. + if (signal_pending_state(state, current))
  21061. + return -ERESTARTSYS;
  21062. +
  21063. + prepare_to_swait(q, wait, state);
  21064. +
  21065. + return 0;
  21066. +}
  21067. +EXPORT_SYMBOL(prepare_to_swait_event);
  21068. +
  21069. +void __finish_swait(struct swait_queue_head *q, struct swait_queue *wait)
  21070. +{
  21071. + __set_current_state(TASK_RUNNING);
  21072. + if (!list_empty(&wait->task_list))
  21073. + list_del_init(&wait->task_list);
  21074. +}
  21075. +
  21076. +void finish_swait(struct swait_queue_head *q, struct swait_queue *wait)
  21077. +{
  21078. + unsigned long flags;
  21079. +
  21080. + __set_current_state(TASK_RUNNING);
  21081. +
  21082. + if (!list_empty_careful(&wait->task_list)) {
  21083. + raw_spin_lock_irqsave(&q->lock, flags);
  21084. + list_del_init(&wait->task_list);
  21085. + raw_spin_unlock_irqrestore(&q->lock, flags);
  21086. + }
  21087. +}
  21088. +EXPORT_SYMBOL(finish_swait);
  21089. diff -Nur linux-4.4.46.orig/kernel/sched/swork.c linux-4.4.46/kernel/sched/swork.c
  21090. --- linux-4.4.46.orig/kernel/sched/swork.c 1970-01-01 01:00:00.000000000 +0100
  21091. +++ linux-4.4.46/kernel/sched/swork.c 2017-02-03 17:18:10.935619367 +0100
  21092. @@ -0,0 +1,173 @@
  21093. +/*
  21094. + * Copyright (C) 2014 BMW Car IT GmbH, Daniel Wagner daniel.wagner@bmw-carit.de
  21095. + *
  21096. + * Provides a framework for enqueuing callbacks from irq context
  21097. + * PREEMPT_RT_FULL safe. The callbacks are executed in kthread context.
  21098. + */
  21099. +
  21100. +#include <linux/swait.h>
  21101. +#include <linux/swork.h>
  21102. +#include <linux/kthread.h>
  21103. +#include <linux/slab.h>
  21104. +#include <linux/spinlock.h>
  21105. +#include <linux/export.h>
  21106. +
  21107. +#define SWORK_EVENT_PENDING (1 << 0)
  21108. +
  21109. +static DEFINE_MUTEX(worker_mutex);
  21110. +static struct sworker *glob_worker;
  21111. +
  21112. +struct sworker {
  21113. + struct list_head events;
  21114. + struct swait_queue_head wq;
  21115. +
  21116. + raw_spinlock_t lock;
  21117. +
  21118. + struct task_struct *task;
  21119. + int refs;
  21120. +};
  21121. +
  21122. +static bool swork_readable(struct sworker *worker)
  21123. +{
  21124. + bool r;
  21125. +
  21126. + if (kthread_should_stop())
  21127. + return true;
  21128. +
  21129. + raw_spin_lock_irq(&worker->lock);
  21130. + r = !list_empty(&worker->events);
  21131. + raw_spin_unlock_irq(&worker->lock);
  21132. +
  21133. + return r;
  21134. +}
  21135. +
  21136. +static int swork_kthread(void *arg)
  21137. +{
  21138. + struct sworker *worker = arg;
  21139. +
  21140. + for (;;) {
  21141. + swait_event_interruptible(worker->wq,
  21142. + swork_readable(worker));
  21143. + if (kthread_should_stop())
  21144. + break;
  21145. +
  21146. + raw_spin_lock_irq(&worker->lock);
  21147. + while (!list_empty(&worker->events)) {
  21148. + struct swork_event *sev;
  21149. +
  21150. + sev = list_first_entry(&worker->events,
  21151. + struct swork_event, item);
  21152. + list_del(&sev->item);
  21153. + raw_spin_unlock_irq(&worker->lock);
  21154. +
  21155. + WARN_ON_ONCE(!test_and_clear_bit(SWORK_EVENT_PENDING,
  21156. + &sev->flags));
  21157. + sev->func(sev);
  21158. + raw_spin_lock_irq(&worker->lock);
  21159. + }
  21160. + raw_spin_unlock_irq(&worker->lock);
  21161. + }
  21162. + return 0;
  21163. +}
  21164. +
  21165. +static struct sworker *swork_create(void)
  21166. +{
  21167. + struct sworker *worker;
  21168. +
  21169. + worker = kzalloc(sizeof(*worker), GFP_KERNEL);
  21170. + if (!worker)
  21171. + return ERR_PTR(-ENOMEM);
  21172. +
  21173. + INIT_LIST_HEAD(&worker->events);
  21174. + raw_spin_lock_init(&worker->lock);
  21175. + init_swait_queue_head(&worker->wq);
  21176. +
  21177. + worker->task = kthread_run(swork_kthread, worker, "kswork");
  21178. + if (IS_ERR(worker->task)) {
  21179. + kfree(worker);
  21180. + return ERR_PTR(-ENOMEM);
  21181. + }
  21182. +
  21183. + return worker;
  21184. +}
  21185. +
  21186. +static void swork_destroy(struct sworker *worker)
  21187. +{
  21188. + kthread_stop(worker->task);
  21189. +
  21190. + WARN_ON(!list_empty(&worker->events));
  21191. + kfree(worker);
  21192. +}
  21193. +
  21194. +/**
  21195. + * swork_queue - queue swork
  21196. + *
  21197. + * Returns %false if @work was already on a queue, %true otherwise.
  21198. + *
  21199. + * The work is queued and processed on a random CPU
  21200. + */
  21201. +bool swork_queue(struct swork_event *sev)
  21202. +{
  21203. + unsigned long flags;
  21204. +
  21205. + if (test_and_set_bit(SWORK_EVENT_PENDING, &sev->flags))
  21206. + return false;
  21207. +
  21208. + raw_spin_lock_irqsave(&glob_worker->lock, flags);
  21209. + list_add_tail(&sev->item, &glob_worker->events);
  21210. + raw_spin_unlock_irqrestore(&glob_worker->lock, flags);
  21211. +
  21212. + swake_up(&glob_worker->wq);
  21213. + return true;
  21214. +}
  21215. +EXPORT_SYMBOL_GPL(swork_queue);
  21216. +
  21217. +/**
  21218. + * swork_get - get an instance of the sworker
  21219. + *
  21220. + * Returns an negative error code if the initialization if the worker did not
  21221. + * work, %0 otherwise.
  21222. + *
  21223. + */
  21224. +int swork_get(void)
  21225. +{
  21226. + struct sworker *worker;
  21227. +
  21228. + mutex_lock(&worker_mutex);
  21229. + if (!glob_worker) {
  21230. + worker = swork_create();
  21231. + if (IS_ERR(worker)) {
  21232. + mutex_unlock(&worker_mutex);
  21233. + return -ENOMEM;
  21234. + }
  21235. +
  21236. + glob_worker = worker;
  21237. + }
  21238. +
  21239. + glob_worker->refs++;
  21240. + mutex_unlock(&worker_mutex);
  21241. +
  21242. + return 0;
  21243. +}
  21244. +EXPORT_SYMBOL_GPL(swork_get);
  21245. +
  21246. +/**
  21247. + * swork_put - puts an instance of the sworker
  21248. + *
  21249. + * Will destroy the sworker thread. This function must not be called until all
  21250. + * queued events have been completed.
  21251. + */
  21252. +void swork_put(void)
  21253. +{
  21254. + mutex_lock(&worker_mutex);
  21255. +
  21256. + glob_worker->refs--;
  21257. + if (glob_worker->refs > 0)
  21258. + goto out;
  21259. +
  21260. + swork_destroy(glob_worker);
  21261. + glob_worker = NULL;
  21262. +out:
  21263. + mutex_unlock(&worker_mutex);
  21264. +}
  21265. +EXPORT_SYMBOL_GPL(swork_put);
  21266. diff -Nur linux-4.4.46.orig/kernel/signal.c linux-4.4.46/kernel/signal.c
  21267. --- linux-4.4.46.orig/kernel/signal.c 2017-02-01 08:31:11.000000000 +0100
  21268. +++ linux-4.4.46/kernel/signal.c 2017-02-03 17:18:10.935619367 +0100
  21269. @@ -14,6 +14,7 @@
  21270. #include <linux/export.h>
  21271. #include <linux/init.h>
  21272. #include <linux/sched.h>
  21273. +#include <linux/sched/rt.h>
  21274. #include <linux/fs.h>
  21275. #include <linux/tty.h>
  21276. #include <linux/binfmts.h>
  21277. @@ -352,13 +353,30 @@
  21278. return false;
  21279. }
  21280. +static inline struct sigqueue *get_task_cache(struct task_struct *t)
  21281. +{
  21282. + struct sigqueue *q = t->sigqueue_cache;
  21283. +
  21284. + if (cmpxchg(&t->sigqueue_cache, q, NULL) != q)
  21285. + return NULL;
  21286. + return q;
  21287. +}
  21288. +
  21289. +static inline int put_task_cache(struct task_struct *t, struct sigqueue *q)
  21290. +{
  21291. + if (cmpxchg(&t->sigqueue_cache, NULL, q) == NULL)
  21292. + return 0;
  21293. + return 1;
  21294. +}
  21295. +
  21296. /*
  21297. * allocate a new signal queue record
  21298. * - this may be called without locks if and only if t == current, otherwise an
  21299. * appropriate lock must be held to stop the target task from exiting
  21300. */
  21301. static struct sigqueue *
  21302. -__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
  21303. +__sigqueue_do_alloc(int sig, struct task_struct *t, gfp_t flags,
  21304. + int override_rlimit, int fromslab)
  21305. {
  21306. struct sigqueue *q = NULL;
  21307. struct user_struct *user;
  21308. @@ -375,7 +393,10 @@
  21309. if (override_rlimit ||
  21310. atomic_read(&user->sigpending) <=
  21311. task_rlimit(t, RLIMIT_SIGPENDING)) {
  21312. - q = kmem_cache_alloc(sigqueue_cachep, flags);
  21313. + if (!fromslab)
  21314. + q = get_task_cache(t);
  21315. + if (!q)
  21316. + q = kmem_cache_alloc(sigqueue_cachep, flags);
  21317. } else {
  21318. print_dropped_signal(sig);
  21319. }
  21320. @@ -392,6 +413,13 @@
  21321. return q;
  21322. }
  21323. +static struct sigqueue *
  21324. +__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags,
  21325. + int override_rlimit)
  21326. +{
  21327. + return __sigqueue_do_alloc(sig, t, flags, override_rlimit, 0);
  21328. +}
  21329. +
  21330. static void __sigqueue_free(struct sigqueue *q)
  21331. {
  21332. if (q->flags & SIGQUEUE_PREALLOC)
  21333. @@ -401,6 +429,21 @@
  21334. kmem_cache_free(sigqueue_cachep, q);
  21335. }
  21336. +static void sigqueue_free_current(struct sigqueue *q)
  21337. +{
  21338. + struct user_struct *up;
  21339. +
  21340. + if (q->flags & SIGQUEUE_PREALLOC)
  21341. + return;
  21342. +
  21343. + up = q->user;
  21344. + if (rt_prio(current->normal_prio) && !put_task_cache(current, q)) {
  21345. + atomic_dec(&up->sigpending);
  21346. + free_uid(up);
  21347. + } else
  21348. + __sigqueue_free(q);
  21349. +}
  21350. +
  21351. void flush_sigqueue(struct sigpending *queue)
  21352. {
  21353. struct sigqueue *q;
  21354. @@ -414,6 +457,21 @@
  21355. }
  21356. /*
  21357. + * Called from __exit_signal. Flush tsk->pending and
  21358. + * tsk->sigqueue_cache
  21359. + */
  21360. +void flush_task_sigqueue(struct task_struct *tsk)
  21361. +{
  21362. + struct sigqueue *q;
  21363. +
  21364. + flush_sigqueue(&tsk->pending);
  21365. +
  21366. + q = get_task_cache(tsk);
  21367. + if (q)
  21368. + kmem_cache_free(sigqueue_cachep, q);
  21369. +}
  21370. +
  21371. +/*
  21372. * Flush all pending signals for this kthread.
  21373. */
  21374. void flush_signals(struct task_struct *t)
  21375. @@ -525,7 +583,7 @@
  21376. still_pending:
  21377. list_del_init(&first->list);
  21378. copy_siginfo(info, &first->info);
  21379. - __sigqueue_free(first);
  21380. + sigqueue_free_current(first);
  21381. } else {
  21382. /*
  21383. * Ok, it wasn't in the queue. This must be
  21384. @@ -560,6 +618,8 @@
  21385. {
  21386. int signr;
  21387. + WARN_ON_ONCE(tsk != current);
  21388. +
  21389. /* We only dequeue private signals from ourselves, we don't let
  21390. * signalfd steal them
  21391. */
  21392. @@ -1156,8 +1216,8 @@
  21393. * We don't want to have recursive SIGSEGV's etc, for example,
  21394. * that is why we also clear SIGNAL_UNKILLABLE.
  21395. */
  21396. -int
  21397. -force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  21398. +static int
  21399. +do_force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  21400. {
  21401. unsigned long int flags;
  21402. int ret, blocked, ignored;
  21403. @@ -1182,6 +1242,39 @@
  21404. return ret;
  21405. }
  21406. +int force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  21407. +{
  21408. +/*
  21409. + * On some archs, PREEMPT_RT has to delay sending a signal from a trap
  21410. + * since it can not enable preemption, and the signal code's spin_locks
  21411. + * turn into mutexes. Instead, it must set TIF_NOTIFY_RESUME which will
  21412. + * send the signal on exit of the trap.
  21413. + */
  21414. +#ifdef ARCH_RT_DELAYS_SIGNAL_SEND
  21415. + if (in_atomic()) {
  21416. + if (WARN_ON_ONCE(t != current))
  21417. + return 0;
  21418. + if (WARN_ON_ONCE(t->forced_info.si_signo))
  21419. + return 0;
  21420. +
  21421. + if (is_si_special(info)) {
  21422. + WARN_ON_ONCE(info != SEND_SIG_PRIV);
  21423. + t->forced_info.si_signo = sig;
  21424. + t->forced_info.si_errno = 0;
  21425. + t->forced_info.si_code = SI_KERNEL;
  21426. + t->forced_info.si_pid = 0;
  21427. + t->forced_info.si_uid = 0;
  21428. + } else {
  21429. + t->forced_info = *info;
  21430. + }
  21431. +
  21432. + set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
  21433. + return 0;
  21434. + }
  21435. +#endif
  21436. + return do_force_sig_info(sig, info, t);
  21437. +}
  21438. +
  21439. /*
  21440. * Nuke all other threads in the group.
  21441. */
  21442. @@ -1216,12 +1309,12 @@
  21443. * Disable interrupts early to avoid deadlocks.
  21444. * See rcu_read_unlock() comment header for details.
  21445. */
  21446. - local_irq_save(*flags);
  21447. + local_irq_save_nort(*flags);
  21448. rcu_read_lock();
  21449. sighand = rcu_dereference(tsk->sighand);
  21450. if (unlikely(sighand == NULL)) {
  21451. rcu_read_unlock();
  21452. - local_irq_restore(*flags);
  21453. + local_irq_restore_nort(*flags);
  21454. break;
  21455. }
  21456. /*
  21457. @@ -1242,7 +1335,7 @@
  21458. }
  21459. spin_unlock(&sighand->siglock);
  21460. rcu_read_unlock();
  21461. - local_irq_restore(*flags);
  21462. + local_irq_restore_nort(*flags);
  21463. }
  21464. return sighand;
  21465. @@ -1485,7 +1578,8 @@
  21466. */
  21467. struct sigqueue *sigqueue_alloc(void)
  21468. {
  21469. - struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
  21470. + /* Preallocated sigqueue objects always from the slabcache ! */
  21471. + struct sigqueue *q = __sigqueue_do_alloc(-1, current, GFP_KERNEL, 0, 1);
  21472. if (q)
  21473. q->flags |= SIGQUEUE_PREALLOC;
  21474. @@ -1846,15 +1940,7 @@
  21475. if (gstop_done && ptrace_reparented(current))
  21476. do_notify_parent_cldstop(current, false, why);
  21477. - /*
  21478. - * Don't want to allow preemption here, because
  21479. - * sys_ptrace() needs this task to be inactive.
  21480. - *
  21481. - * XXX: implement read_unlock_no_resched().
  21482. - */
  21483. - preempt_disable();
  21484. read_unlock(&tasklist_lock);
  21485. - preempt_enable_no_resched();
  21486. freezable_schedule();
  21487. } else {
  21488. /*
  21489. diff -Nur linux-4.4.46.orig/kernel/softirq.c linux-4.4.46/kernel/softirq.c
  21490. --- linux-4.4.46.orig/kernel/softirq.c 2017-02-01 08:31:11.000000000 +0100
  21491. +++ linux-4.4.46/kernel/softirq.c 2017-02-03 17:18:10.935619367 +0100
  21492. @@ -21,10 +21,12 @@
  21493. #include <linux/freezer.h>
  21494. #include <linux/kthread.h>
  21495. #include <linux/rcupdate.h>
  21496. +#include <linux/delay.h>
  21497. #include <linux/ftrace.h>
  21498. #include <linux/smp.h>
  21499. #include <linux/smpboot.h>
  21500. #include <linux/tick.h>
  21501. +#include <linux/locallock.h>
  21502. #include <linux/irq.h>
  21503. #define CREATE_TRACE_POINTS
  21504. @@ -56,12 +58,108 @@
  21505. static struct softirq_action softirq_vec[NR_SOFTIRQS] __cacheline_aligned_in_smp;
  21506. DEFINE_PER_CPU(struct task_struct *, ksoftirqd);
  21507. +#ifdef CONFIG_PREEMPT_RT_FULL
  21508. +#define TIMER_SOFTIRQS ((1 << TIMER_SOFTIRQ) | (1 << HRTIMER_SOFTIRQ))
  21509. +DEFINE_PER_CPU(struct task_struct *, ktimer_softirqd);
  21510. +#endif
  21511. const char * const softirq_to_name[NR_SOFTIRQS] = {
  21512. "HI", "TIMER", "NET_TX", "NET_RX", "BLOCK", "BLOCK_IOPOLL",
  21513. "TASKLET", "SCHED", "HRTIMER", "RCU"
  21514. };
  21515. +#ifdef CONFIG_NO_HZ_COMMON
  21516. +# ifdef CONFIG_PREEMPT_RT_FULL
  21517. +
  21518. +struct softirq_runner {
  21519. + struct task_struct *runner[NR_SOFTIRQS];
  21520. +};
  21521. +
  21522. +static DEFINE_PER_CPU(struct softirq_runner, softirq_runners);
  21523. +
  21524. +static inline void softirq_set_runner(unsigned int sirq)
  21525. +{
  21526. + struct softirq_runner *sr = this_cpu_ptr(&softirq_runners);
  21527. +
  21528. + sr->runner[sirq] = current;
  21529. +}
  21530. +
  21531. +static inline void softirq_clr_runner(unsigned int sirq)
  21532. +{
  21533. + struct softirq_runner *sr = this_cpu_ptr(&softirq_runners);
  21534. +
  21535. + sr->runner[sirq] = NULL;
  21536. +}
  21537. +
  21538. +/*
  21539. + * On preempt-rt a softirq running context might be blocked on a
  21540. + * lock. There might be no other runnable task on this CPU because the
  21541. + * lock owner runs on some other CPU. So we have to go into idle with
  21542. + * the pending bit set. Therefor we need to check this otherwise we
  21543. + * warn about false positives which confuses users and defeats the
  21544. + * whole purpose of this test.
  21545. + *
  21546. + * This code is called with interrupts disabled.
  21547. + */
  21548. +void softirq_check_pending_idle(void)
  21549. +{
  21550. + static int rate_limit;
  21551. + struct softirq_runner *sr = this_cpu_ptr(&softirq_runners);
  21552. + u32 warnpending;
  21553. + int i;
  21554. +
  21555. + if (rate_limit >= 10)
  21556. + return;
  21557. +
  21558. + warnpending = local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK;
  21559. + for (i = 0; i < NR_SOFTIRQS; i++) {
  21560. + struct task_struct *tsk = sr->runner[i];
  21561. +
  21562. + /*
  21563. + * The wakeup code in rtmutex.c wakes up the task
  21564. + * _before_ it sets pi_blocked_on to NULL under
  21565. + * tsk->pi_lock. So we need to check for both: state
  21566. + * and pi_blocked_on.
  21567. + */
  21568. + if (tsk) {
  21569. + raw_spin_lock(&tsk->pi_lock);
  21570. + if (tsk->pi_blocked_on || tsk->state == TASK_RUNNING) {
  21571. + /* Clear all bits pending in that task */
  21572. + warnpending &= ~(tsk->softirqs_raised);
  21573. + warnpending &= ~(1 << i);
  21574. + }
  21575. + raw_spin_unlock(&tsk->pi_lock);
  21576. + }
  21577. + }
  21578. +
  21579. + if (warnpending) {
  21580. + printk(KERN_ERR "NOHZ: local_softirq_pending %02x\n",
  21581. + warnpending);
  21582. + rate_limit++;
  21583. + }
  21584. +}
  21585. +# else
  21586. +/*
  21587. + * On !PREEMPT_RT we just printk rate limited:
  21588. + */
  21589. +void softirq_check_pending_idle(void)
  21590. +{
  21591. + static int rate_limit;
  21592. +
  21593. + if (rate_limit < 10 &&
  21594. + (local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK)) {
  21595. + printk(KERN_ERR "NOHZ: local_softirq_pending %02x\n",
  21596. + local_softirq_pending());
  21597. + rate_limit++;
  21598. + }
  21599. +}
  21600. +# endif
  21601. +
  21602. +#else /* !CONFIG_NO_HZ_COMMON */
  21603. +static inline void softirq_set_runner(unsigned int sirq) { }
  21604. +static inline void softirq_clr_runner(unsigned int sirq) { }
  21605. +#endif
  21606. +
  21607. /*
  21608. * we cannot loop indefinitely here to avoid userspace starvation,
  21609. * but we also don't want to introduce a worst case 1/HZ latency
  21610. @@ -77,6 +175,79 @@
  21611. wake_up_process(tsk);
  21612. }
  21613. +#ifdef CONFIG_PREEMPT_RT_FULL
  21614. +static void wakeup_timer_softirqd(void)
  21615. +{
  21616. + /* Interrupts are disabled: no need to stop preemption */
  21617. + struct task_struct *tsk = __this_cpu_read(ktimer_softirqd);
  21618. +
  21619. + if (tsk && tsk->state != TASK_RUNNING)
  21620. + wake_up_process(tsk);
  21621. +}
  21622. +#endif
  21623. +
  21624. +static void handle_softirq(unsigned int vec_nr)
  21625. +{
  21626. + struct softirq_action *h = softirq_vec + vec_nr;
  21627. + int prev_count;
  21628. +
  21629. + prev_count = preempt_count();
  21630. +
  21631. + kstat_incr_softirqs_this_cpu(vec_nr);
  21632. +
  21633. + trace_softirq_entry(vec_nr);
  21634. + h->action(h);
  21635. + trace_softirq_exit(vec_nr);
  21636. + if (unlikely(prev_count != preempt_count())) {
  21637. + pr_err("huh, entered softirq %u %s %p with preempt_count %08x, exited with %08x?\n",
  21638. + vec_nr, softirq_to_name[vec_nr], h->action,
  21639. + prev_count, preempt_count());
  21640. + preempt_count_set(prev_count);
  21641. + }
  21642. +}
  21643. +
  21644. +#ifndef CONFIG_PREEMPT_RT_FULL
  21645. +static inline int ksoftirqd_softirq_pending(void)
  21646. +{
  21647. + return local_softirq_pending();
  21648. +}
  21649. +
  21650. +static void handle_pending_softirqs(u32 pending)
  21651. +{
  21652. + struct softirq_action *h = softirq_vec;
  21653. + int softirq_bit;
  21654. +
  21655. + local_irq_enable();
  21656. +
  21657. + h = softirq_vec;
  21658. +
  21659. + while ((softirq_bit = ffs(pending))) {
  21660. + unsigned int vec_nr;
  21661. +
  21662. + h += softirq_bit - 1;
  21663. + vec_nr = h - softirq_vec;
  21664. + handle_softirq(vec_nr);
  21665. +
  21666. + h++;
  21667. + pending >>= softirq_bit;
  21668. + }
  21669. +
  21670. + rcu_bh_qs();
  21671. + local_irq_disable();
  21672. +}
  21673. +
  21674. +static void run_ksoftirqd(unsigned int cpu)
  21675. +{
  21676. + local_irq_disable();
  21677. + if (ksoftirqd_softirq_pending()) {
  21678. + __do_softirq();
  21679. + local_irq_enable();
  21680. + cond_resched_rcu_qs();
  21681. + return;
  21682. + }
  21683. + local_irq_enable();
  21684. +}
  21685. +
  21686. /*
  21687. * preempt_count and SOFTIRQ_OFFSET usage:
  21688. * - preempt_count is changed by SOFTIRQ_OFFSET on entering or leaving
  21689. @@ -116,9 +287,9 @@
  21690. if (preempt_count() == cnt) {
  21691. #ifdef CONFIG_DEBUG_PREEMPT
  21692. - current->preempt_disable_ip = get_parent_ip(CALLER_ADDR1);
  21693. + current->preempt_disable_ip = get_lock_parent_ip();
  21694. #endif
  21695. - trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
  21696. + trace_preempt_off(CALLER_ADDR0, get_lock_parent_ip());
  21697. }
  21698. }
  21699. EXPORT_SYMBOL(__local_bh_disable_ip);
  21700. @@ -232,10 +403,8 @@
  21701. unsigned long end = jiffies + MAX_SOFTIRQ_TIME;
  21702. unsigned long old_flags = current->flags;
  21703. int max_restart = MAX_SOFTIRQ_RESTART;
  21704. - struct softirq_action *h;
  21705. bool in_hardirq;
  21706. __u32 pending;
  21707. - int softirq_bit;
  21708. /*
  21709. * Mask out PF_MEMALLOC s current task context is borrowed for the
  21710. @@ -254,36 +423,7 @@
  21711. /* Reset the pending bitmask before enabling irqs */
  21712. set_softirq_pending(0);
  21713. - local_irq_enable();
  21714. -
  21715. - h = softirq_vec;
  21716. -
  21717. - while ((softirq_bit = ffs(pending))) {
  21718. - unsigned int vec_nr;
  21719. - int prev_count;
  21720. -
  21721. - h += softirq_bit - 1;
  21722. -
  21723. - vec_nr = h - softirq_vec;
  21724. - prev_count = preempt_count();
  21725. -
  21726. - kstat_incr_softirqs_this_cpu(vec_nr);
  21727. -
  21728. - trace_softirq_entry(vec_nr);
  21729. - h->action(h);
  21730. - trace_softirq_exit(vec_nr);
  21731. - if (unlikely(prev_count != preempt_count())) {
  21732. - pr_err("huh, entered softirq %u %s %p with preempt_count %08x, exited with %08x?\n",
  21733. - vec_nr, softirq_to_name[vec_nr], h->action,
  21734. - prev_count, preempt_count());
  21735. - preempt_count_set(prev_count);
  21736. - }
  21737. - h++;
  21738. - pending >>= softirq_bit;
  21739. - }
  21740. -
  21741. - rcu_bh_qs();
  21742. - local_irq_disable();
  21743. + handle_pending_softirqs(pending);
  21744. pending = local_softirq_pending();
  21745. if (pending) {
  21746. @@ -320,6 +460,310 @@
  21747. }
  21748. /*
  21749. + * This function must run with irqs disabled!
  21750. + */
  21751. +void raise_softirq_irqoff(unsigned int nr)
  21752. +{
  21753. + __raise_softirq_irqoff(nr);
  21754. +
  21755. + /*
  21756. + * If we're in an interrupt or softirq, we're done
  21757. + * (this also catches softirq-disabled code). We will
  21758. + * actually run the softirq once we return from
  21759. + * the irq or softirq.
  21760. + *
  21761. + * Otherwise we wake up ksoftirqd to make sure we
  21762. + * schedule the softirq soon.
  21763. + */
  21764. + if (!in_interrupt())
  21765. + wakeup_softirqd();
  21766. +}
  21767. +
  21768. +void __raise_softirq_irqoff(unsigned int nr)
  21769. +{
  21770. + trace_softirq_raise(nr);
  21771. + or_softirq_pending(1UL << nr);
  21772. +}
  21773. +
  21774. +static inline void local_bh_disable_nort(void) { local_bh_disable(); }
  21775. +static inline void _local_bh_enable_nort(void) { _local_bh_enable(); }
  21776. +static void ksoftirqd_set_sched_params(unsigned int cpu) { }
  21777. +
  21778. +#else /* !PREEMPT_RT_FULL */
  21779. +
  21780. +/*
  21781. + * On RT we serialize softirq execution with a cpu local lock per softirq
  21782. + */
  21783. +static DEFINE_PER_CPU(struct local_irq_lock [NR_SOFTIRQS], local_softirq_locks);
  21784. +
  21785. +void __init softirq_early_init(void)
  21786. +{
  21787. + int i;
  21788. +
  21789. + for (i = 0; i < NR_SOFTIRQS; i++)
  21790. + local_irq_lock_init(local_softirq_locks[i]);
  21791. +}
  21792. +
  21793. +static void lock_softirq(int which)
  21794. +{
  21795. + local_lock(local_softirq_locks[which]);
  21796. +}
  21797. +
  21798. +static void unlock_softirq(int which)
  21799. +{
  21800. + local_unlock(local_softirq_locks[which]);
  21801. +}
  21802. +
  21803. +static void do_single_softirq(int which)
  21804. +{
  21805. + unsigned long old_flags = current->flags;
  21806. +
  21807. + current->flags &= ~PF_MEMALLOC;
  21808. + vtime_account_irq_enter(current);
  21809. + current->flags |= PF_IN_SOFTIRQ;
  21810. + lockdep_softirq_enter();
  21811. + local_irq_enable();
  21812. + handle_softirq(which);
  21813. + local_irq_disable();
  21814. + lockdep_softirq_exit();
  21815. + current->flags &= ~PF_IN_SOFTIRQ;
  21816. + vtime_account_irq_enter(current);
  21817. + tsk_restore_flags(current, old_flags, PF_MEMALLOC);
  21818. +}
  21819. +
  21820. +/*
  21821. + * Called with interrupts disabled. Process softirqs which were raised
  21822. + * in current context (or on behalf of ksoftirqd).
  21823. + */
  21824. +static void do_current_softirqs(void)
  21825. +{
  21826. + while (current->softirqs_raised) {
  21827. + int i = __ffs(current->softirqs_raised);
  21828. + unsigned int pending, mask = (1U << i);
  21829. +
  21830. + current->softirqs_raised &= ~mask;
  21831. + local_irq_enable();
  21832. +
  21833. + /*
  21834. + * If the lock is contended, we boost the owner to
  21835. + * process the softirq or leave the critical section
  21836. + * now.
  21837. + */
  21838. + lock_softirq(i);
  21839. + local_irq_disable();
  21840. + softirq_set_runner(i);
  21841. + /*
  21842. + * Check with the local_softirq_pending() bits,
  21843. + * whether we need to process this still or if someone
  21844. + * else took care of it.
  21845. + */
  21846. + pending = local_softirq_pending();
  21847. + if (pending & mask) {
  21848. + set_softirq_pending(pending & ~mask);
  21849. + do_single_softirq(i);
  21850. + }
  21851. + softirq_clr_runner(i);
  21852. + WARN_ON(current->softirq_nestcnt != 1);
  21853. + local_irq_enable();
  21854. + unlock_softirq(i);
  21855. + local_irq_disable();
  21856. + }
  21857. +}
  21858. +
  21859. +void __local_bh_disable(void)
  21860. +{
  21861. + if (++current->softirq_nestcnt == 1)
  21862. + migrate_disable();
  21863. +}
  21864. +EXPORT_SYMBOL(__local_bh_disable);
  21865. +
  21866. +void __local_bh_enable(void)
  21867. +{
  21868. + if (WARN_ON(current->softirq_nestcnt == 0))
  21869. + return;
  21870. +
  21871. + local_irq_disable();
  21872. + if (current->softirq_nestcnt == 1 && current->softirqs_raised)
  21873. + do_current_softirqs();
  21874. + local_irq_enable();
  21875. +
  21876. + if (--current->softirq_nestcnt == 0)
  21877. + migrate_enable();
  21878. +}
  21879. +EXPORT_SYMBOL(__local_bh_enable);
  21880. +
  21881. +void _local_bh_enable(void)
  21882. +{
  21883. + if (WARN_ON(current->softirq_nestcnt == 0))
  21884. + return;
  21885. + if (--current->softirq_nestcnt == 0)
  21886. + migrate_enable();
  21887. +}
  21888. +EXPORT_SYMBOL(_local_bh_enable);
  21889. +
  21890. +int in_serving_softirq(void)
  21891. +{
  21892. + return current->flags & PF_IN_SOFTIRQ;
  21893. +}
  21894. +EXPORT_SYMBOL(in_serving_softirq);
  21895. +
  21896. +/* Called with preemption disabled */
  21897. +static void run_ksoftirqd(unsigned int cpu)
  21898. +{
  21899. + local_irq_disable();
  21900. + current->softirq_nestcnt++;
  21901. +
  21902. + do_current_softirqs();
  21903. + current->softirq_nestcnt--;
  21904. + local_irq_enable();
  21905. + cond_resched_rcu_qs();
  21906. +}
  21907. +
  21908. +/*
  21909. + * Called from netif_rx_ni(). Preemption enabled, but migration
  21910. + * disabled. So the cpu can't go away under us.
  21911. + */
  21912. +void thread_do_softirq(void)
  21913. +{
  21914. + if (!in_serving_softirq() && current->softirqs_raised) {
  21915. + current->softirq_nestcnt++;
  21916. + do_current_softirqs();
  21917. + current->softirq_nestcnt--;
  21918. + }
  21919. +}
  21920. +
  21921. +static void do_raise_softirq_irqoff(unsigned int nr)
  21922. +{
  21923. + unsigned int mask;
  21924. +
  21925. + mask = 1UL << nr;
  21926. +
  21927. + trace_softirq_raise(nr);
  21928. + or_softirq_pending(mask);
  21929. +
  21930. + /*
  21931. + * If we are not in a hard interrupt and inside a bh disabled
  21932. + * region, we simply raise the flag on current. local_bh_enable()
  21933. + * will make sure that the softirq is executed. Otherwise we
  21934. + * delegate it to ksoftirqd.
  21935. + */
  21936. + if (!in_irq() && current->softirq_nestcnt)
  21937. + current->softirqs_raised |= mask;
  21938. + else if (!__this_cpu_read(ksoftirqd) || !__this_cpu_read(ktimer_softirqd))
  21939. + return;
  21940. +
  21941. + if (mask & TIMER_SOFTIRQS)
  21942. + __this_cpu_read(ktimer_softirqd)->softirqs_raised |= mask;
  21943. + else
  21944. + __this_cpu_read(ksoftirqd)->softirqs_raised |= mask;
  21945. +}
  21946. +
  21947. +static void wakeup_proper_softirq(unsigned int nr)
  21948. +{
  21949. + if ((1UL << nr) & TIMER_SOFTIRQS)
  21950. + wakeup_timer_softirqd();
  21951. + else
  21952. + wakeup_softirqd();
  21953. +}
  21954. +
  21955. +
  21956. +void __raise_softirq_irqoff(unsigned int nr)
  21957. +{
  21958. + do_raise_softirq_irqoff(nr);
  21959. + if (!in_irq() && !current->softirq_nestcnt)
  21960. + wakeup_proper_softirq(nr);
  21961. +}
  21962. +
  21963. +/*
  21964. + * Same as __raise_softirq_irqoff() but will process them in ksoftirqd
  21965. + */
  21966. +void __raise_softirq_irqoff_ksoft(unsigned int nr)
  21967. +{
  21968. + unsigned int mask;
  21969. +
  21970. + if (WARN_ON_ONCE(!__this_cpu_read(ksoftirqd) ||
  21971. + !__this_cpu_read(ktimer_softirqd)))
  21972. + return;
  21973. + mask = 1UL << nr;
  21974. +
  21975. + trace_softirq_raise(nr);
  21976. + or_softirq_pending(mask);
  21977. + if (mask & TIMER_SOFTIRQS)
  21978. + __this_cpu_read(ktimer_softirqd)->softirqs_raised |= mask;
  21979. + else
  21980. + __this_cpu_read(ksoftirqd)->softirqs_raised |= mask;
  21981. + wakeup_proper_softirq(nr);
  21982. +}
  21983. +
  21984. +/*
  21985. + * This function must run with irqs disabled!
  21986. + */
  21987. +void raise_softirq_irqoff(unsigned int nr)
  21988. +{
  21989. + do_raise_softirq_irqoff(nr);
  21990. +
  21991. + /*
  21992. + * If we're in an hard interrupt we let irq return code deal
  21993. + * with the wakeup of ksoftirqd.
  21994. + */
  21995. + if (in_irq())
  21996. + return;
  21997. + /*
  21998. + * If we are in thread context but outside of a bh disabled
  21999. + * region, we need to wake ksoftirqd as well.
  22000. + *
  22001. + * CHECKME: Some of the places which do that could be wrapped
  22002. + * into local_bh_disable/enable pairs. Though it's unclear
  22003. + * whether this is worth the effort. To find those places just
  22004. + * raise a WARN() if the condition is met.
  22005. + */
  22006. + if (!current->softirq_nestcnt)
  22007. + wakeup_proper_softirq(nr);
  22008. +}
  22009. +
  22010. +static inline int ksoftirqd_softirq_pending(void)
  22011. +{
  22012. + return current->softirqs_raised;
  22013. +}
  22014. +
  22015. +static inline void local_bh_disable_nort(void) { }
  22016. +static inline void _local_bh_enable_nort(void) { }
  22017. +
  22018. +static inline void ksoftirqd_set_sched_params(unsigned int cpu)
  22019. +{
  22020. + /* Take over all but timer pending softirqs when starting */
  22021. + local_irq_disable();
  22022. + current->softirqs_raised = local_softirq_pending() & ~TIMER_SOFTIRQS;
  22023. + local_irq_enable();
  22024. +}
  22025. +
  22026. +static inline void ktimer_softirqd_set_sched_params(unsigned int cpu)
  22027. +{
  22028. + struct sched_param param = { .sched_priority = 1 };
  22029. +
  22030. + sched_setscheduler(current, SCHED_FIFO, &param);
  22031. +
  22032. + /* Take over timer pending softirqs when starting */
  22033. + local_irq_disable();
  22034. + current->softirqs_raised = local_softirq_pending() & TIMER_SOFTIRQS;
  22035. + local_irq_enable();
  22036. +}
  22037. +
  22038. +static inline void ktimer_softirqd_clr_sched_params(unsigned int cpu,
  22039. + bool online)
  22040. +{
  22041. + struct sched_param param = { .sched_priority = 0 };
  22042. +
  22043. + sched_setscheduler(current, SCHED_NORMAL, &param);
  22044. +}
  22045. +
  22046. +static int ktimer_softirqd_should_run(unsigned int cpu)
  22047. +{
  22048. + return current->softirqs_raised;
  22049. +}
  22050. +
  22051. +#endif /* PREEMPT_RT_FULL */
  22052. +/*
  22053. * Enter an interrupt context.
  22054. */
  22055. void irq_enter(void)
  22056. @@ -330,9 +774,9 @@
  22057. * Prevent raise_softirq from needlessly waking up ksoftirqd
  22058. * here, as softirq will be serviced on return from interrupt.
  22059. */
  22060. - local_bh_disable();
  22061. + local_bh_disable_nort();
  22062. tick_irq_enter();
  22063. - _local_bh_enable();
  22064. + _local_bh_enable_nort();
  22065. }
  22066. __irq_enter();
  22067. @@ -340,6 +784,7 @@
  22068. static inline void invoke_softirq(void)
  22069. {
  22070. +#ifndef CONFIG_PREEMPT_RT_FULL
  22071. if (!force_irqthreads) {
  22072. #ifdef CONFIG_HAVE_IRQ_EXIT_ON_IRQ_STACK
  22073. /*
  22074. @@ -359,6 +804,18 @@
  22075. } else {
  22076. wakeup_softirqd();
  22077. }
  22078. +#else /* PREEMPT_RT_FULL */
  22079. + unsigned long flags;
  22080. +
  22081. + local_irq_save(flags);
  22082. + if (__this_cpu_read(ksoftirqd) &&
  22083. + __this_cpu_read(ksoftirqd)->softirqs_raised)
  22084. + wakeup_softirqd();
  22085. + if (__this_cpu_read(ktimer_softirqd) &&
  22086. + __this_cpu_read(ktimer_softirqd)->softirqs_raised)
  22087. + wakeup_timer_softirqd();
  22088. + local_irq_restore(flags);
  22089. +#endif
  22090. }
  22091. static inline void tick_irq_exit(void)
  22092. @@ -395,26 +852,6 @@
  22093. trace_hardirq_exit(); /* must be last! */
  22094. }
  22095. -/*
  22096. - * This function must run with irqs disabled!
  22097. - */
  22098. -inline void raise_softirq_irqoff(unsigned int nr)
  22099. -{
  22100. - __raise_softirq_irqoff(nr);
  22101. -
  22102. - /*
  22103. - * If we're in an interrupt or softirq, we're done
  22104. - * (this also catches softirq-disabled code). We will
  22105. - * actually run the softirq once we return from
  22106. - * the irq or softirq.
  22107. - *
  22108. - * Otherwise we wake up ksoftirqd to make sure we
  22109. - * schedule the softirq soon.
  22110. - */
  22111. - if (!in_interrupt())
  22112. - wakeup_softirqd();
  22113. -}
  22114. -
  22115. void raise_softirq(unsigned int nr)
  22116. {
  22117. unsigned long flags;
  22118. @@ -424,12 +861,6 @@
  22119. local_irq_restore(flags);
  22120. }
  22121. -void __raise_softirq_irqoff(unsigned int nr)
  22122. -{
  22123. - trace_softirq_raise(nr);
  22124. - or_softirq_pending(1UL << nr);
  22125. -}
  22126. -
  22127. void open_softirq(int nr, void (*action)(struct softirq_action *))
  22128. {
  22129. softirq_vec[nr].action = action;
  22130. @@ -446,15 +877,45 @@
  22131. static DEFINE_PER_CPU(struct tasklet_head, tasklet_vec);
  22132. static DEFINE_PER_CPU(struct tasklet_head, tasklet_hi_vec);
  22133. +static void inline
  22134. +__tasklet_common_schedule(struct tasklet_struct *t, struct tasklet_head *head, unsigned int nr)
  22135. +{
  22136. + if (tasklet_trylock(t)) {
  22137. +again:
  22138. + /* We may have been preempted before tasklet_trylock
  22139. + * and __tasklet_action may have already run.
  22140. + * So double check the sched bit while the takslet
  22141. + * is locked before adding it to the list.
  22142. + */
  22143. + if (test_bit(TASKLET_STATE_SCHED, &t->state)) {
  22144. + t->next = NULL;
  22145. + *head->tail = t;
  22146. + head->tail = &(t->next);
  22147. + raise_softirq_irqoff(nr);
  22148. + tasklet_unlock(t);
  22149. + } else {
  22150. + /* This is subtle. If we hit the corner case above
  22151. + * It is possible that we get preempted right here,
  22152. + * and another task has successfully called
  22153. + * tasklet_schedule(), then this function, and
  22154. + * failed on the trylock. Thus we must be sure
  22155. + * before releasing the tasklet lock, that the
  22156. + * SCHED_BIT is clear. Otherwise the tasklet
  22157. + * may get its SCHED_BIT set, but not added to the
  22158. + * list
  22159. + */
  22160. + if (!tasklet_tryunlock(t))
  22161. + goto again;
  22162. + }
  22163. + }
  22164. +}
  22165. +
  22166. void __tasklet_schedule(struct tasklet_struct *t)
  22167. {
  22168. unsigned long flags;
  22169. local_irq_save(flags);
  22170. - t->next = NULL;
  22171. - *__this_cpu_read(tasklet_vec.tail) = t;
  22172. - __this_cpu_write(tasklet_vec.tail, &(t->next));
  22173. - raise_softirq_irqoff(TASKLET_SOFTIRQ);
  22174. + __tasklet_common_schedule(t, this_cpu_ptr(&tasklet_vec), TASKLET_SOFTIRQ);
  22175. local_irq_restore(flags);
  22176. }
  22177. EXPORT_SYMBOL(__tasklet_schedule);
  22178. @@ -464,10 +925,7 @@
  22179. unsigned long flags;
  22180. local_irq_save(flags);
  22181. - t->next = NULL;
  22182. - *__this_cpu_read(tasklet_hi_vec.tail) = t;
  22183. - __this_cpu_write(tasklet_hi_vec.tail, &(t->next));
  22184. - raise_softirq_irqoff(HI_SOFTIRQ);
  22185. + __tasklet_common_schedule(t, this_cpu_ptr(&tasklet_hi_vec), HI_SOFTIRQ);
  22186. local_irq_restore(flags);
  22187. }
  22188. EXPORT_SYMBOL(__tasklet_hi_schedule);
  22189. @@ -476,82 +934,122 @@
  22190. {
  22191. BUG_ON(!irqs_disabled());
  22192. - t->next = __this_cpu_read(tasklet_hi_vec.head);
  22193. - __this_cpu_write(tasklet_hi_vec.head, t);
  22194. - __raise_softirq_irqoff(HI_SOFTIRQ);
  22195. + __tasklet_hi_schedule(t);
  22196. }
  22197. EXPORT_SYMBOL(__tasklet_hi_schedule_first);
  22198. -static void tasklet_action(struct softirq_action *a)
  22199. +void tasklet_enable(struct tasklet_struct *t)
  22200. {
  22201. - struct tasklet_struct *list;
  22202. + if (!atomic_dec_and_test(&t->count))
  22203. + return;
  22204. + if (test_and_clear_bit(TASKLET_STATE_PENDING, &t->state))
  22205. + tasklet_schedule(t);
  22206. +}
  22207. +EXPORT_SYMBOL(tasklet_enable);
  22208. - local_irq_disable();
  22209. - list = __this_cpu_read(tasklet_vec.head);
  22210. - __this_cpu_write(tasklet_vec.head, NULL);
  22211. - __this_cpu_write(tasklet_vec.tail, this_cpu_ptr(&tasklet_vec.head));
  22212. - local_irq_enable();
  22213. +static void __tasklet_action(struct softirq_action *a,
  22214. + struct tasklet_struct *list)
  22215. +{
  22216. + int loops = 1000000;
  22217. while (list) {
  22218. struct tasklet_struct *t = list;
  22219. list = list->next;
  22220. - if (tasklet_trylock(t)) {
  22221. - if (!atomic_read(&t->count)) {
  22222. - if (!test_and_clear_bit(TASKLET_STATE_SCHED,
  22223. - &t->state))
  22224. - BUG();
  22225. - t->func(t->data);
  22226. - tasklet_unlock(t);
  22227. - continue;
  22228. - }
  22229. - tasklet_unlock(t);
  22230. + /*
  22231. + * Should always succeed - after a tasklist got on the
  22232. + * list (after getting the SCHED bit set from 0 to 1),
  22233. + * nothing but the tasklet softirq it got queued to can
  22234. + * lock it:
  22235. + */
  22236. + if (!tasklet_trylock(t)) {
  22237. + WARN_ON(1);
  22238. + continue;
  22239. }
  22240. - local_irq_disable();
  22241. t->next = NULL;
  22242. - *__this_cpu_read(tasklet_vec.tail) = t;
  22243. - __this_cpu_write(tasklet_vec.tail, &(t->next));
  22244. - __raise_softirq_irqoff(TASKLET_SOFTIRQ);
  22245. - local_irq_enable();
  22246. +
  22247. + /*
  22248. + * If we cannot handle the tasklet because it's disabled,
  22249. + * mark it as pending. tasklet_enable() will later
  22250. + * re-schedule the tasklet.
  22251. + */
  22252. + if (unlikely(atomic_read(&t->count))) {
  22253. +out_disabled:
  22254. + /* implicit unlock: */
  22255. + wmb();
  22256. + t->state = TASKLET_STATEF_PENDING;
  22257. + continue;
  22258. + }
  22259. +
  22260. + /*
  22261. + * After this point on the tasklet might be rescheduled
  22262. + * on another CPU, but it can only be added to another
  22263. + * CPU's tasklet list if we unlock the tasklet (which we
  22264. + * dont do yet).
  22265. + */
  22266. + if (!test_and_clear_bit(TASKLET_STATE_SCHED, &t->state))
  22267. + WARN_ON(1);
  22268. +
  22269. +again:
  22270. + t->func(t->data);
  22271. +
  22272. + /*
  22273. + * Try to unlock the tasklet. We must use cmpxchg, because
  22274. + * another CPU might have scheduled or disabled the tasklet.
  22275. + * We only allow the STATE_RUN -> 0 transition here.
  22276. + */
  22277. + while (!tasklet_tryunlock(t)) {
  22278. + /*
  22279. + * If it got disabled meanwhile, bail out:
  22280. + */
  22281. + if (atomic_read(&t->count))
  22282. + goto out_disabled;
  22283. + /*
  22284. + * If it got scheduled meanwhile, re-execute
  22285. + * the tasklet function:
  22286. + */
  22287. + if (test_and_clear_bit(TASKLET_STATE_SCHED, &t->state))
  22288. + goto again;
  22289. + if (!--loops) {
  22290. + printk("hm, tasklet state: %08lx\n", t->state);
  22291. + WARN_ON(1);
  22292. + tasklet_unlock(t);
  22293. + break;
  22294. + }
  22295. + }
  22296. }
  22297. }
  22298. +static void tasklet_action(struct softirq_action *a)
  22299. +{
  22300. + struct tasklet_struct *list;
  22301. +
  22302. + local_irq_disable();
  22303. +
  22304. + list = __this_cpu_read(tasklet_vec.head);
  22305. + __this_cpu_write(tasklet_vec.head, NULL);
  22306. + __this_cpu_write(tasklet_vec.tail, this_cpu_ptr(&tasklet_vec.head));
  22307. +
  22308. + local_irq_enable();
  22309. +
  22310. + __tasklet_action(a, list);
  22311. +}
  22312. +
  22313. static void tasklet_hi_action(struct softirq_action *a)
  22314. {
  22315. struct tasklet_struct *list;
  22316. local_irq_disable();
  22317. +
  22318. list = __this_cpu_read(tasklet_hi_vec.head);
  22319. __this_cpu_write(tasklet_hi_vec.head, NULL);
  22320. __this_cpu_write(tasklet_hi_vec.tail, this_cpu_ptr(&tasklet_hi_vec.head));
  22321. - local_irq_enable();
  22322. - while (list) {
  22323. - struct tasklet_struct *t = list;
  22324. -
  22325. - list = list->next;
  22326. -
  22327. - if (tasklet_trylock(t)) {
  22328. - if (!atomic_read(&t->count)) {
  22329. - if (!test_and_clear_bit(TASKLET_STATE_SCHED,
  22330. - &t->state))
  22331. - BUG();
  22332. - t->func(t->data);
  22333. - tasklet_unlock(t);
  22334. - continue;
  22335. - }
  22336. - tasklet_unlock(t);
  22337. - }
  22338. + local_irq_enable();
  22339. - local_irq_disable();
  22340. - t->next = NULL;
  22341. - *__this_cpu_read(tasklet_hi_vec.tail) = t;
  22342. - __this_cpu_write(tasklet_hi_vec.tail, &(t->next));
  22343. - __raise_softirq_irqoff(HI_SOFTIRQ);
  22344. - local_irq_enable();
  22345. - }
  22346. + __tasklet_action(a, list);
  22347. }
  22348. void tasklet_init(struct tasklet_struct *t,
  22349. @@ -572,7 +1070,7 @@
  22350. while (test_and_set_bit(TASKLET_STATE_SCHED, &t->state)) {
  22351. do {
  22352. - yield();
  22353. + msleep(1);
  22354. } while (test_bit(TASKLET_STATE_SCHED, &t->state));
  22355. }
  22356. tasklet_unlock_wait(t);
  22357. @@ -646,25 +1144,26 @@
  22358. open_softirq(HI_SOFTIRQ, tasklet_hi_action);
  22359. }
  22360. -static int ksoftirqd_should_run(unsigned int cpu)
  22361. -{
  22362. - return local_softirq_pending();
  22363. -}
  22364. -
  22365. -static void run_ksoftirqd(unsigned int cpu)
  22366. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  22367. +void tasklet_unlock_wait(struct tasklet_struct *t)
  22368. {
  22369. - local_irq_disable();
  22370. - if (local_softirq_pending()) {
  22371. + while (test_bit(TASKLET_STATE_RUN, &(t)->state)) {
  22372. /*
  22373. - * We can safely run softirq on inline stack, as we are not deep
  22374. - * in the task stack here.
  22375. + * Hack for now to avoid this busy-loop:
  22376. */
  22377. - __do_softirq();
  22378. - local_irq_enable();
  22379. - cond_resched_rcu_qs();
  22380. - return;
  22381. +#ifdef CONFIG_PREEMPT_RT_FULL
  22382. + msleep(1);
  22383. +#else
  22384. + barrier();
  22385. +#endif
  22386. }
  22387. - local_irq_enable();
  22388. +}
  22389. +EXPORT_SYMBOL(tasklet_unlock_wait);
  22390. +#endif
  22391. +
  22392. +static int ksoftirqd_should_run(unsigned int cpu)
  22393. +{
  22394. + return ksoftirqd_softirq_pending();
  22395. }
  22396. #ifdef CONFIG_HOTPLUG_CPU
  22397. @@ -746,16 +1245,31 @@
  22398. static struct smp_hotplug_thread softirq_threads = {
  22399. .store = &ksoftirqd,
  22400. + .setup = ksoftirqd_set_sched_params,
  22401. .thread_should_run = ksoftirqd_should_run,
  22402. .thread_fn = run_ksoftirqd,
  22403. .thread_comm = "ksoftirqd/%u",
  22404. };
  22405. +#ifdef CONFIG_PREEMPT_RT_FULL
  22406. +static struct smp_hotplug_thread softirq_timer_threads = {
  22407. + .store = &ktimer_softirqd,
  22408. + .setup = ktimer_softirqd_set_sched_params,
  22409. + .cleanup = ktimer_softirqd_clr_sched_params,
  22410. + .thread_should_run = ktimer_softirqd_should_run,
  22411. + .thread_fn = run_ksoftirqd,
  22412. + .thread_comm = "ktimersoftd/%u",
  22413. +};
  22414. +#endif
  22415. +
  22416. static __init int spawn_ksoftirqd(void)
  22417. {
  22418. register_cpu_notifier(&cpu_nfb);
  22419. BUG_ON(smpboot_register_percpu_thread(&softirq_threads));
  22420. +#ifdef CONFIG_PREEMPT_RT_FULL
  22421. + BUG_ON(smpboot_register_percpu_thread(&softirq_timer_threads));
  22422. +#endif
  22423. return 0;
  22424. }
  22425. diff -Nur linux-4.4.46.orig/kernel/stop_machine.c linux-4.4.46/kernel/stop_machine.c
  22426. --- linux-4.4.46.orig/kernel/stop_machine.c 2017-02-01 08:31:11.000000000 +0100
  22427. +++ linux-4.4.46/kernel/stop_machine.c 2017-02-03 17:18:10.935619367 +0100
  22428. @@ -37,7 +37,7 @@
  22429. struct cpu_stopper {
  22430. struct task_struct *thread;
  22431. - spinlock_t lock;
  22432. + raw_spinlock_t lock;
  22433. bool enabled; /* is this stopper enabled? */
  22434. struct list_head works; /* list of pending works */
  22435. @@ -86,12 +86,12 @@
  22436. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  22437. unsigned long flags;
  22438. - spin_lock_irqsave(&stopper->lock, flags);
  22439. + raw_spin_lock_irqsave(&stopper->lock, flags);
  22440. if (stopper->enabled)
  22441. __cpu_stop_queue_work(stopper, work);
  22442. else
  22443. cpu_stop_signal_done(work->done, false);
  22444. - spin_unlock_irqrestore(&stopper->lock, flags);
  22445. + raw_spin_unlock_irqrestore(&stopper->lock, flags);
  22446. }
  22447. /**
  22448. @@ -224,8 +224,8 @@
  22449. int err;
  22450. lg_double_lock(&stop_cpus_lock, cpu1, cpu2);
  22451. - spin_lock_irq(&stopper1->lock);
  22452. - spin_lock_nested(&stopper2->lock, SINGLE_DEPTH_NESTING);
  22453. + raw_spin_lock_irq(&stopper1->lock);
  22454. + raw_spin_lock_nested(&stopper2->lock, SINGLE_DEPTH_NESTING);
  22455. err = -ENOENT;
  22456. if (!stopper1->enabled || !stopper2->enabled)
  22457. @@ -235,8 +235,8 @@
  22458. __cpu_stop_queue_work(stopper1, work1);
  22459. __cpu_stop_queue_work(stopper2, work2);
  22460. unlock:
  22461. - spin_unlock(&stopper2->lock);
  22462. - spin_unlock_irq(&stopper1->lock);
  22463. + raw_spin_unlock(&stopper2->lock);
  22464. + raw_spin_unlock_irq(&stopper1->lock);
  22465. lg_double_unlock(&stop_cpus_lock, cpu1, cpu2);
  22466. return err;
  22467. @@ -258,7 +258,7 @@
  22468. struct cpu_stop_work work1, work2;
  22469. struct multi_stop_data msdata;
  22470. - preempt_disable();
  22471. + preempt_disable_nort();
  22472. msdata = (struct multi_stop_data){
  22473. .fn = fn,
  22474. .data = arg,
  22475. @@ -278,11 +278,11 @@
  22476. if (cpu1 > cpu2)
  22477. swap(cpu1, cpu2);
  22478. if (cpu_stop_queue_two_works(cpu1, &work1, cpu2, &work2)) {
  22479. - preempt_enable();
  22480. + preempt_enable_nort();
  22481. return -ENOENT;
  22482. }
  22483. - preempt_enable();
  22484. + preempt_enable_nort();
  22485. wait_for_completion(&done.completion);
  22486. @@ -315,17 +315,20 @@
  22487. static void queue_stop_cpus_work(const struct cpumask *cpumask,
  22488. cpu_stop_fn_t fn, void *arg,
  22489. - struct cpu_stop_done *done)
  22490. + struct cpu_stop_done *done, bool inactive)
  22491. {
  22492. struct cpu_stop_work *work;
  22493. unsigned int cpu;
  22494. /*
  22495. - * Disable preemption while queueing to avoid getting
  22496. - * preempted by a stopper which might wait for other stoppers
  22497. - * to enter @fn which can lead to deadlock.
  22498. + * Make sure that all work is queued on all cpus before
  22499. + * any of the cpus can execute it.
  22500. */
  22501. - lg_global_lock(&stop_cpus_lock);
  22502. + if (!inactive)
  22503. + lg_global_lock(&stop_cpus_lock);
  22504. + else
  22505. + lg_global_trylock_relax(&stop_cpus_lock);
  22506. +
  22507. for_each_cpu(cpu, cpumask) {
  22508. work = &per_cpu(cpu_stopper.stop_work, cpu);
  22509. work->fn = fn;
  22510. @@ -342,7 +345,7 @@
  22511. struct cpu_stop_done done;
  22512. cpu_stop_init_done(&done, cpumask_weight(cpumask));
  22513. - queue_stop_cpus_work(cpumask, fn, arg, &done);
  22514. + queue_stop_cpus_work(cpumask, fn, arg, &done, false);
  22515. wait_for_completion(&done.completion);
  22516. return done.executed ? done.ret : -ENOENT;
  22517. }
  22518. @@ -422,9 +425,9 @@
  22519. unsigned long flags;
  22520. int run;
  22521. - spin_lock_irqsave(&stopper->lock, flags);
  22522. + raw_spin_lock_irqsave(&stopper->lock, flags);
  22523. run = !list_empty(&stopper->works);
  22524. - spin_unlock_irqrestore(&stopper->lock, flags);
  22525. + raw_spin_unlock_irqrestore(&stopper->lock, flags);
  22526. return run;
  22527. }
  22528. @@ -436,13 +439,13 @@
  22529. repeat:
  22530. work = NULL;
  22531. - spin_lock_irq(&stopper->lock);
  22532. + raw_spin_lock_irq(&stopper->lock);
  22533. if (!list_empty(&stopper->works)) {
  22534. work = list_first_entry(&stopper->works,
  22535. struct cpu_stop_work, list);
  22536. list_del_init(&work->list);
  22537. }
  22538. - spin_unlock_irq(&stopper->lock);
  22539. + raw_spin_unlock_irq(&stopper->lock);
  22540. if (work) {
  22541. cpu_stop_fn_t fn = work->fn;
  22542. @@ -450,6 +453,16 @@
  22543. struct cpu_stop_done *done = work->done;
  22544. char ksym_buf[KSYM_NAME_LEN] __maybe_unused;
  22545. + /*
  22546. + * Wait until the stopper finished scheduling on all
  22547. + * cpus
  22548. + */
  22549. + lg_global_lock(&stop_cpus_lock);
  22550. + /*
  22551. + * Let other cpu threads continue as well
  22552. + */
  22553. + lg_global_unlock(&stop_cpus_lock);
  22554. +
  22555. /* cpu stop callbacks are not allowed to sleep */
  22556. preempt_disable();
  22557. @@ -520,10 +533,12 @@
  22558. for_each_possible_cpu(cpu) {
  22559. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  22560. - spin_lock_init(&stopper->lock);
  22561. + raw_spin_lock_init(&stopper->lock);
  22562. INIT_LIST_HEAD(&stopper->works);
  22563. }
  22564. + lg_lock_init(&stop_cpus_lock, "stop_cpus_lock");
  22565. +
  22566. BUG_ON(smpboot_register_percpu_thread(&cpu_stop_threads));
  22567. stop_machine_unpark(raw_smp_processor_id());
  22568. stop_machine_initialized = true;
  22569. @@ -620,7 +635,7 @@
  22570. set_state(&msdata, MULTI_STOP_PREPARE);
  22571. cpu_stop_init_done(&done, num_active_cpus());
  22572. queue_stop_cpus_work(cpu_active_mask, multi_cpu_stop, &msdata,
  22573. - &done);
  22574. + &done, true);
  22575. ret = multi_cpu_stop(&msdata);
  22576. /* Busy wait for completion. */
  22577. diff -Nur linux-4.4.46.orig/kernel/time/hrtimer.c linux-4.4.46/kernel/time/hrtimer.c
  22578. --- linux-4.4.46.orig/kernel/time/hrtimer.c 2017-02-01 08:31:11.000000000 +0100
  22579. +++ linux-4.4.46/kernel/time/hrtimer.c 2017-02-03 17:18:10.939619522 +0100
  22580. @@ -48,11 +48,13 @@
  22581. #include <linux/sched/rt.h>
  22582. #include <linux/sched/deadline.h>
  22583. #include <linux/timer.h>
  22584. +#include <linux/kthread.h>
  22585. #include <linux/freezer.h>
  22586. #include <asm/uaccess.h>
  22587. #include <trace/events/timer.h>
  22588. +#include <trace/events/hist.h>
  22589. #include "tick-internal.h"
  22590. @@ -717,6 +719,44 @@
  22591. static DECLARE_WORK(hrtimer_work, clock_was_set_work);
  22592. +#ifdef CONFIG_PREEMPT_RT_FULL
  22593. +/*
  22594. + * RT can not call schedule_work from real interrupt context.
  22595. + * Need to make a thread to do the real work.
  22596. + */
  22597. +static struct task_struct *clock_set_delay_thread;
  22598. +static bool do_clock_set_delay;
  22599. +
  22600. +static int run_clock_set_delay(void *ignore)
  22601. +{
  22602. + while (!kthread_should_stop()) {
  22603. + set_current_state(TASK_INTERRUPTIBLE);
  22604. + if (do_clock_set_delay) {
  22605. + do_clock_set_delay = false;
  22606. + schedule_work(&hrtimer_work);
  22607. + }
  22608. + schedule();
  22609. + }
  22610. + __set_current_state(TASK_RUNNING);
  22611. + return 0;
  22612. +}
  22613. +
  22614. +void clock_was_set_delayed(void)
  22615. +{
  22616. + do_clock_set_delay = true;
  22617. + /* Make visible before waking up process */
  22618. + smp_wmb();
  22619. + wake_up_process(clock_set_delay_thread);
  22620. +}
  22621. +
  22622. +static __init int create_clock_set_delay_thread(void)
  22623. +{
  22624. + clock_set_delay_thread = kthread_run(run_clock_set_delay, NULL, "kclksetdelayd");
  22625. + BUG_ON(!clock_set_delay_thread);
  22626. + return 0;
  22627. +}
  22628. +early_initcall(create_clock_set_delay_thread);
  22629. +#else /* PREEMPT_RT_FULL */
  22630. /*
  22631. * Called from timekeeping and resume code to reprogramm the hrtimer
  22632. * interrupt device on all cpus.
  22633. @@ -725,6 +765,7 @@
  22634. {
  22635. schedule_work(&hrtimer_work);
  22636. }
  22637. +#endif
  22638. #else
  22639. @@ -734,11 +775,8 @@
  22640. static inline void hrtimer_switch_to_hres(void) { }
  22641. static inline void
  22642. hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
  22643. -static inline int hrtimer_reprogram(struct hrtimer *timer,
  22644. - struct hrtimer_clock_base *base)
  22645. -{
  22646. - return 0;
  22647. -}
  22648. +static inline void hrtimer_reprogram(struct hrtimer *timer,
  22649. + struct hrtimer_clock_base *base) { }
  22650. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
  22651. static inline void retrigger_next_event(void *arg) { }
  22652. @@ -870,6 +908,32 @@
  22653. }
  22654. EXPORT_SYMBOL_GPL(hrtimer_forward);
  22655. +#ifdef CONFIG_PREEMPT_RT_BASE
  22656. +# define wake_up_timer_waiters(b) wake_up(&(b)->wait)
  22657. +
  22658. +/**
  22659. + * hrtimer_wait_for_timer - Wait for a running timer
  22660. + *
  22661. + * @timer: timer to wait for
  22662. + *
  22663. + * The function waits in case the timers callback function is
  22664. + * currently executed on the waitqueue of the timer base. The
  22665. + * waitqueue is woken up after the timer callback function has
  22666. + * finished execution.
  22667. + */
  22668. +void hrtimer_wait_for_timer(const struct hrtimer *timer)
  22669. +{
  22670. + struct hrtimer_clock_base *base = timer->base;
  22671. +
  22672. + if (base && base->cpu_base && !timer->irqsafe)
  22673. + wait_event(base->cpu_base->wait,
  22674. + !(hrtimer_callback_running(timer)));
  22675. +}
  22676. +
  22677. +#else
  22678. +# define wake_up_timer_waiters(b) do { } while (0)
  22679. +#endif
  22680. +
  22681. /*
  22682. * enqueue_hrtimer - internal function to (re)start a timer
  22683. *
  22684. @@ -911,6 +975,11 @@
  22685. if (!(state & HRTIMER_STATE_ENQUEUED))
  22686. return;
  22687. + if (unlikely(!list_empty(&timer->cb_entry))) {
  22688. + list_del_init(&timer->cb_entry);
  22689. + return;
  22690. + }
  22691. +
  22692. if (!timerqueue_del(&base->active, &timer->node))
  22693. cpu_base->active_bases &= ~(1 << base->index);
  22694. @@ -1006,7 +1075,16 @@
  22695. new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
  22696. timer_stats_hrtimer_set_start_info(timer);
  22697. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  22698. + {
  22699. + ktime_t now = new_base->get_time();
  22700. + if (ktime_to_ns(tim) < ktime_to_ns(now))
  22701. + timer->praecox = now;
  22702. + else
  22703. + timer->praecox = ktime_set(0, 0);
  22704. + }
  22705. +#endif
  22706. leftmost = enqueue_hrtimer(timer, new_base);
  22707. if (!leftmost)
  22708. goto unlock;
  22709. @@ -1078,7 +1156,7 @@
  22710. if (ret >= 0)
  22711. return ret;
  22712. - cpu_relax();
  22713. + hrtimer_wait_for_timer(timer);
  22714. }
  22715. }
  22716. EXPORT_SYMBOL_GPL(hrtimer_cancel);
  22717. @@ -1142,6 +1220,7 @@
  22718. base = hrtimer_clockid_to_base(clock_id);
  22719. timer->base = &cpu_base->clock_base[base];
  22720. + INIT_LIST_HEAD(&timer->cb_entry);
  22721. timerqueue_init(&timer->node);
  22722. #ifdef CONFIG_TIMER_STATS
  22723. @@ -1182,6 +1261,7 @@
  22724. seq = raw_read_seqcount_begin(&cpu_base->seq);
  22725. if (timer->state != HRTIMER_STATE_INACTIVE ||
  22726. + cpu_base->running_soft == timer ||
  22727. cpu_base->running == timer)
  22728. return true;
  22729. @@ -1280,10 +1360,112 @@
  22730. cpu_base->running = NULL;
  22731. }
  22732. +#ifdef CONFIG_PREEMPT_RT_BASE
  22733. +static void hrtimer_rt_reprogram(int restart, struct hrtimer *timer,
  22734. + struct hrtimer_clock_base *base)
  22735. +{
  22736. + int leftmost;
  22737. +
  22738. + if (restart != HRTIMER_NORESTART &&
  22739. + !(timer->state & HRTIMER_STATE_ENQUEUED)) {
  22740. +
  22741. + leftmost = enqueue_hrtimer(timer, base);
  22742. + if (!leftmost)
  22743. + return;
  22744. +#ifdef CONFIG_HIGH_RES_TIMERS
  22745. + if (!hrtimer_is_hres_active(timer)) {
  22746. + /*
  22747. + * Kick to reschedule the next tick to handle the new timer
  22748. + * on dynticks target.
  22749. + */
  22750. + if (base->cpu_base->nohz_active)
  22751. + wake_up_nohz_cpu(base->cpu_base->cpu);
  22752. + } else {
  22753. +
  22754. + hrtimer_reprogram(timer, base);
  22755. + }
  22756. +#endif
  22757. + }
  22758. +}
  22759. +
  22760. +/*
  22761. + * The changes in mainline which removed the callback modes from
  22762. + * hrtimer are not yet working with -rt. The non wakeup_process()
  22763. + * based callbacks which involve sleeping locks need to be treated
  22764. + * seperately.
  22765. + */
  22766. +static void hrtimer_rt_run_pending(void)
  22767. +{
  22768. + enum hrtimer_restart (*fn)(struct hrtimer *);
  22769. + struct hrtimer_cpu_base *cpu_base;
  22770. + struct hrtimer_clock_base *base;
  22771. + struct hrtimer *timer;
  22772. + int index, restart;
  22773. +
  22774. + local_irq_disable();
  22775. + cpu_base = &per_cpu(hrtimer_bases, smp_processor_id());
  22776. +
  22777. + raw_spin_lock(&cpu_base->lock);
  22778. +
  22779. + for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
  22780. + base = &cpu_base->clock_base[index];
  22781. +
  22782. + while (!list_empty(&base->expired)) {
  22783. + timer = list_first_entry(&base->expired,
  22784. + struct hrtimer, cb_entry);
  22785. +
  22786. + /*
  22787. + * Same as the above __run_hrtimer function
  22788. + * just we run with interrupts enabled.
  22789. + */
  22790. + debug_deactivate(timer);
  22791. + cpu_base->running_soft = timer;
  22792. + raw_write_seqcount_barrier(&cpu_base->seq);
  22793. +
  22794. + __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, 0);
  22795. + timer_stats_account_hrtimer(timer);
  22796. + fn = timer->function;
  22797. +
  22798. + raw_spin_unlock_irq(&cpu_base->lock);
  22799. + restart = fn(timer);
  22800. + raw_spin_lock_irq(&cpu_base->lock);
  22801. +
  22802. + hrtimer_rt_reprogram(restart, timer, base);
  22803. + raw_write_seqcount_barrier(&cpu_base->seq);
  22804. +
  22805. + WARN_ON_ONCE(cpu_base->running_soft != timer);
  22806. + cpu_base->running_soft = NULL;
  22807. + }
  22808. + }
  22809. +
  22810. + raw_spin_unlock_irq(&cpu_base->lock);
  22811. +
  22812. + wake_up_timer_waiters(cpu_base);
  22813. +}
  22814. +
  22815. +static int hrtimer_rt_defer(struct hrtimer *timer)
  22816. +{
  22817. + if (timer->irqsafe)
  22818. + return 0;
  22819. +
  22820. + __remove_hrtimer(timer, timer->base, timer->state, 0);
  22821. + list_add_tail(&timer->cb_entry, &timer->base->expired);
  22822. + return 1;
  22823. +}
  22824. +
  22825. +#else
  22826. +
  22827. +static inline int hrtimer_rt_defer(struct hrtimer *timer) { return 0; }
  22828. +
  22829. +#endif
  22830. +
  22831. +static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer);
  22832. +
  22833. static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now)
  22834. {
  22835. struct hrtimer_clock_base *base = cpu_base->clock_base;
  22836. unsigned int active = cpu_base->active_bases;
  22837. + int raise = 0;
  22838. for (; active; base++, active >>= 1) {
  22839. struct timerqueue_node *node;
  22840. @@ -1299,6 +1481,15 @@
  22841. timer = container_of(node, struct hrtimer, node);
  22842. + trace_hrtimer_interrupt(raw_smp_processor_id(),
  22843. + ktime_to_ns(ktime_sub(ktime_to_ns(timer->praecox) ?
  22844. + timer->praecox : hrtimer_get_expires(timer),
  22845. + basenow)),
  22846. + current,
  22847. + timer->function == hrtimer_wakeup ?
  22848. + container_of(timer, struct hrtimer_sleeper,
  22849. + timer)->task : NULL);
  22850. +
  22851. /*
  22852. * The immediate goal for using the softexpires is
  22853. * minimizing wakeups, not running timers at the
  22854. @@ -1314,9 +1505,14 @@
  22855. if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer))
  22856. break;
  22857. - __run_hrtimer(cpu_base, base, timer, &basenow);
  22858. + if (!hrtimer_rt_defer(timer))
  22859. + __run_hrtimer(cpu_base, base, timer, &basenow);
  22860. + else
  22861. + raise = 1;
  22862. }
  22863. }
  22864. + if (raise)
  22865. + raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  22866. }
  22867. #ifdef CONFIG_HIGH_RES_TIMERS
  22868. @@ -1479,16 +1675,18 @@
  22869. void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
  22870. {
  22871. sl->timer.function = hrtimer_wakeup;
  22872. + sl->timer.irqsafe = 1;
  22873. sl->task = task;
  22874. }
  22875. EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
  22876. -static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
  22877. +static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode,
  22878. + unsigned long state)
  22879. {
  22880. hrtimer_init_sleeper(t, current);
  22881. do {
  22882. - set_current_state(TASK_INTERRUPTIBLE);
  22883. + set_current_state(state);
  22884. hrtimer_start_expires(&t->timer, mode);
  22885. if (likely(t->task))
  22886. @@ -1530,7 +1728,8 @@
  22887. HRTIMER_MODE_ABS);
  22888. hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
  22889. - if (do_nanosleep(&t, HRTIMER_MODE_ABS))
  22890. + /* cpu_chill() does not care about restart state. */
  22891. + if (do_nanosleep(&t, HRTIMER_MODE_ABS, TASK_INTERRUPTIBLE))
  22892. goto out;
  22893. rmtp = restart->nanosleep.rmtp;
  22894. @@ -1547,8 +1746,10 @@
  22895. return ret;
  22896. }
  22897. -long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  22898. - const enum hrtimer_mode mode, const clockid_t clockid)
  22899. +static long
  22900. +__hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  22901. + const enum hrtimer_mode mode, const clockid_t clockid,
  22902. + unsigned long state)
  22903. {
  22904. struct restart_block *restart;
  22905. struct hrtimer_sleeper t;
  22906. @@ -1561,7 +1762,7 @@
  22907. hrtimer_init_on_stack(&t.timer, clockid, mode);
  22908. hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
  22909. - if (do_nanosleep(&t, mode))
  22910. + if (do_nanosleep(&t, mode, state))
  22911. goto out;
  22912. /* Absolute timers do not update the rmtp value and restart: */
  22913. @@ -1588,6 +1789,12 @@
  22914. return ret;
  22915. }
  22916. +long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  22917. + const enum hrtimer_mode mode, const clockid_t clockid)
  22918. +{
  22919. + return __hrtimer_nanosleep(rqtp, rmtp, mode, clockid, TASK_INTERRUPTIBLE);
  22920. +}
  22921. +
  22922. SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
  22923. struct timespec __user *, rmtp)
  22924. {
  22925. @@ -1602,6 +1809,26 @@
  22926. return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
  22927. }
  22928. +#ifdef CONFIG_PREEMPT_RT_FULL
  22929. +/*
  22930. + * Sleep for 1 ms in hope whoever holds what we want will let it go.
  22931. + */
  22932. +void cpu_chill(void)
  22933. +{
  22934. + struct timespec tu = {
  22935. + .tv_nsec = NSEC_PER_MSEC,
  22936. + };
  22937. + unsigned int freeze_flag = current->flags & PF_NOFREEZE;
  22938. +
  22939. + current->flags |= PF_NOFREEZE;
  22940. + __hrtimer_nanosleep(&tu, NULL, HRTIMER_MODE_REL, CLOCK_MONOTONIC,
  22941. + TASK_UNINTERRUPTIBLE);
  22942. + if (!freeze_flag)
  22943. + current->flags &= ~PF_NOFREEZE;
  22944. +}
  22945. +EXPORT_SYMBOL(cpu_chill);
  22946. +#endif
  22947. +
  22948. /*
  22949. * Functions related to boot-time initialization:
  22950. */
  22951. @@ -1613,10 +1840,14 @@
  22952. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  22953. cpu_base->clock_base[i].cpu_base = cpu_base;
  22954. timerqueue_init_head(&cpu_base->clock_base[i].active);
  22955. + INIT_LIST_HEAD(&cpu_base->clock_base[i].expired);
  22956. }
  22957. cpu_base->cpu = cpu;
  22958. hrtimer_init_hres(cpu_base);
  22959. +#ifdef CONFIG_PREEMPT_RT_BASE
  22960. + init_waitqueue_head(&cpu_base->wait);
  22961. +#endif
  22962. }
  22963. #ifdef CONFIG_HOTPLUG_CPU
  22964. @@ -1714,11 +1945,21 @@
  22965. .notifier_call = hrtimer_cpu_notify,
  22966. };
  22967. +#ifdef CONFIG_PREEMPT_RT_BASE
  22968. +static void run_hrtimer_softirq(struct softirq_action *h)
  22969. +{
  22970. + hrtimer_rt_run_pending();
  22971. +}
  22972. +#endif
  22973. +
  22974. void __init hrtimers_init(void)
  22975. {
  22976. hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
  22977. (void *)(long)smp_processor_id());
  22978. register_cpu_notifier(&hrtimers_nb);
  22979. +#ifdef CONFIG_PREEMPT_RT_BASE
  22980. + open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
  22981. +#endif
  22982. }
  22983. /**
  22984. diff -Nur linux-4.4.46.orig/kernel/time/itimer.c linux-4.4.46/kernel/time/itimer.c
  22985. --- linux-4.4.46.orig/kernel/time/itimer.c 2017-02-01 08:31:11.000000000 +0100
  22986. +++ linux-4.4.46/kernel/time/itimer.c 2017-02-03 17:18:10.939619522 +0100
  22987. @@ -213,6 +213,7 @@
  22988. /* We are sharing ->siglock with it_real_fn() */
  22989. if (hrtimer_try_to_cancel(timer) < 0) {
  22990. spin_unlock_irq(&tsk->sighand->siglock);
  22991. + hrtimer_wait_for_timer(&tsk->signal->real_timer);
  22992. goto again;
  22993. }
  22994. expires = timeval_to_ktime(value->it_value);
  22995. diff -Nur linux-4.4.46.orig/kernel/time/jiffies.c linux-4.4.46/kernel/time/jiffies.c
  22996. --- linux-4.4.46.orig/kernel/time/jiffies.c 2017-02-01 08:31:11.000000000 +0100
  22997. +++ linux-4.4.46/kernel/time/jiffies.c 2017-02-03 17:18:10.939619522 +0100
  22998. @@ -74,7 +74,8 @@
  22999. .max_cycles = 10,
  23000. };
  23001. -__cacheline_aligned_in_smp DEFINE_SEQLOCK(jiffies_lock);
  23002. +__cacheline_aligned_in_smp DEFINE_RAW_SPINLOCK(jiffies_lock);
  23003. +__cacheline_aligned_in_smp seqcount_t jiffies_seq;
  23004. #if (BITS_PER_LONG < 64)
  23005. u64 get_jiffies_64(void)
  23006. @@ -83,9 +84,9 @@
  23007. u64 ret;
  23008. do {
  23009. - seq = read_seqbegin(&jiffies_lock);
  23010. + seq = read_seqcount_begin(&jiffies_seq);
  23011. ret = jiffies_64;
  23012. - } while (read_seqretry(&jiffies_lock, seq));
  23013. + } while (read_seqcount_retry(&jiffies_seq, seq));
  23014. return ret;
  23015. }
  23016. EXPORT_SYMBOL(get_jiffies_64);
  23017. diff -Nur linux-4.4.46.orig/kernel/time/ntp.c linux-4.4.46/kernel/time/ntp.c
  23018. --- linux-4.4.46.orig/kernel/time/ntp.c 2017-02-01 08:31:11.000000000 +0100
  23019. +++ linux-4.4.46/kernel/time/ntp.c 2017-02-03 17:18:10.939619522 +0100
  23020. @@ -10,6 +10,7 @@
  23021. #include <linux/workqueue.h>
  23022. #include <linux/hrtimer.h>
  23023. #include <linux/jiffies.h>
  23024. +#include <linux/kthread.h>
  23025. #include <linux/math64.h>
  23026. #include <linux/timex.h>
  23027. #include <linux/time.h>
  23028. @@ -562,10 +563,52 @@
  23029. &sync_cmos_work, timespec64_to_jiffies(&next));
  23030. }
  23031. +#ifdef CONFIG_PREEMPT_RT_FULL
  23032. +/*
  23033. + * RT can not call schedule_delayed_work from real interrupt context.
  23034. + * Need to make a thread to do the real work.
  23035. + */
  23036. +static struct task_struct *cmos_delay_thread;
  23037. +static bool do_cmos_delay;
  23038. +
  23039. +static int run_cmos_delay(void *ignore)
  23040. +{
  23041. + while (!kthread_should_stop()) {
  23042. + set_current_state(TASK_INTERRUPTIBLE);
  23043. + if (do_cmos_delay) {
  23044. + do_cmos_delay = false;
  23045. + queue_delayed_work(system_power_efficient_wq,
  23046. + &sync_cmos_work, 0);
  23047. + }
  23048. + schedule();
  23049. + }
  23050. + __set_current_state(TASK_RUNNING);
  23051. + return 0;
  23052. +}
  23053. +
  23054. +void ntp_notify_cmos_timer(void)
  23055. +{
  23056. + do_cmos_delay = true;
  23057. + /* Make visible before waking up process */
  23058. + smp_wmb();
  23059. + wake_up_process(cmos_delay_thread);
  23060. +}
  23061. +
  23062. +static __init int create_cmos_delay_thread(void)
  23063. +{
  23064. + cmos_delay_thread = kthread_run(run_cmos_delay, NULL, "kcmosdelayd");
  23065. + BUG_ON(!cmos_delay_thread);
  23066. + return 0;
  23067. +}
  23068. +early_initcall(create_cmos_delay_thread);
  23069. +
  23070. +#else
  23071. +
  23072. void ntp_notify_cmos_timer(void)
  23073. {
  23074. queue_delayed_work(system_power_efficient_wq, &sync_cmos_work, 0);
  23075. }
  23076. +#endif /* CONFIG_PREEMPT_RT_FULL */
  23077. #else
  23078. void ntp_notify_cmos_timer(void) { }
  23079. diff -Nur linux-4.4.46.orig/kernel/time/posix-cpu-timers.c linux-4.4.46/kernel/time/posix-cpu-timers.c
  23080. --- linux-4.4.46.orig/kernel/time/posix-cpu-timers.c 2017-02-01 08:31:11.000000000 +0100
  23081. +++ linux-4.4.46/kernel/time/posix-cpu-timers.c 2017-02-03 17:18:10.939619522 +0100
  23082. @@ -3,6 +3,7 @@
  23083. */
  23084. #include <linux/sched.h>
  23085. +#include <linux/sched/rt.h>
  23086. #include <linux/posix-timers.h>
  23087. #include <linux/errno.h>
  23088. #include <linux/math64.h>
  23089. @@ -650,7 +651,7 @@
  23090. /*
  23091. * Disarm any old timer after extracting its expiry time.
  23092. */
  23093. - WARN_ON_ONCE(!irqs_disabled());
  23094. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  23095. ret = 0;
  23096. old_incr = timer->it.cpu.incr;
  23097. @@ -1092,7 +1093,7 @@
  23098. /*
  23099. * Now re-arm for the new expiry time.
  23100. */
  23101. - WARN_ON_ONCE(!irqs_disabled());
  23102. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  23103. arm_timer(timer);
  23104. unlock_task_sighand(p, &flags);
  23105. @@ -1183,13 +1184,13 @@
  23106. * already updated our counts. We need to check if any timers fire now.
  23107. * Interrupts are disabled.
  23108. */
  23109. -void run_posix_cpu_timers(struct task_struct *tsk)
  23110. +static void __run_posix_cpu_timers(struct task_struct *tsk)
  23111. {
  23112. LIST_HEAD(firing);
  23113. struct k_itimer *timer, *next;
  23114. unsigned long flags;
  23115. - WARN_ON_ONCE(!irqs_disabled());
  23116. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  23117. /*
  23118. * The fast path checks that there are no expired thread or thread
  23119. @@ -1243,6 +1244,190 @@
  23120. }
  23121. }
  23122. +#ifdef CONFIG_PREEMPT_RT_BASE
  23123. +#include <linux/kthread.h>
  23124. +#include <linux/cpu.h>
  23125. +DEFINE_PER_CPU(struct task_struct *, posix_timer_task);
  23126. +DEFINE_PER_CPU(struct task_struct *, posix_timer_tasklist);
  23127. +
  23128. +static int posix_cpu_timers_thread(void *data)
  23129. +{
  23130. + int cpu = (long)data;
  23131. +
  23132. + BUG_ON(per_cpu(posix_timer_task,cpu) != current);
  23133. +
  23134. + while (!kthread_should_stop()) {
  23135. + struct task_struct *tsk = NULL;
  23136. + struct task_struct *next = NULL;
  23137. +
  23138. + if (cpu_is_offline(cpu))
  23139. + goto wait_to_die;
  23140. +
  23141. + /* grab task list */
  23142. + raw_local_irq_disable();
  23143. + tsk = per_cpu(posix_timer_tasklist, cpu);
  23144. + per_cpu(posix_timer_tasklist, cpu) = NULL;
  23145. + raw_local_irq_enable();
  23146. +
  23147. + /* its possible the list is empty, just return */
  23148. + if (!tsk) {
  23149. + set_current_state(TASK_INTERRUPTIBLE);
  23150. + schedule();
  23151. + __set_current_state(TASK_RUNNING);
  23152. + continue;
  23153. + }
  23154. +
  23155. + /* Process task list */
  23156. + while (1) {
  23157. + /* save next */
  23158. + next = tsk->posix_timer_list;
  23159. +
  23160. + /* run the task timers, clear its ptr and
  23161. + * unreference it
  23162. + */
  23163. + __run_posix_cpu_timers(tsk);
  23164. + tsk->posix_timer_list = NULL;
  23165. + put_task_struct(tsk);
  23166. +
  23167. + /* check if this is the last on the list */
  23168. + if (next == tsk)
  23169. + break;
  23170. + tsk = next;
  23171. + }
  23172. + }
  23173. + return 0;
  23174. +
  23175. +wait_to_die:
  23176. + /* Wait for kthread_stop */
  23177. + set_current_state(TASK_INTERRUPTIBLE);
  23178. + while (!kthread_should_stop()) {
  23179. + schedule();
  23180. + set_current_state(TASK_INTERRUPTIBLE);
  23181. + }
  23182. + __set_current_state(TASK_RUNNING);
  23183. + return 0;
  23184. +}
  23185. +
  23186. +static inline int __fastpath_timer_check(struct task_struct *tsk)
  23187. +{
  23188. + /* tsk == current, ensure it is safe to use ->signal/sighand */
  23189. + if (unlikely(tsk->exit_state))
  23190. + return 0;
  23191. +
  23192. + if (!task_cputime_zero(&tsk->cputime_expires))
  23193. + return 1;
  23194. +
  23195. + if (!task_cputime_zero(&tsk->signal->cputime_expires))
  23196. + return 1;
  23197. +
  23198. + return 0;
  23199. +}
  23200. +
  23201. +void run_posix_cpu_timers(struct task_struct *tsk)
  23202. +{
  23203. + unsigned long cpu = smp_processor_id();
  23204. + struct task_struct *tasklist;
  23205. +
  23206. + BUG_ON(!irqs_disabled());
  23207. + if(!per_cpu(posix_timer_task, cpu))
  23208. + return;
  23209. + /* get per-cpu references */
  23210. + tasklist = per_cpu(posix_timer_tasklist, cpu);
  23211. +
  23212. + /* check to see if we're already queued */
  23213. + if (!tsk->posix_timer_list && __fastpath_timer_check(tsk)) {
  23214. + get_task_struct(tsk);
  23215. + if (tasklist) {
  23216. + tsk->posix_timer_list = tasklist;
  23217. + } else {
  23218. + /*
  23219. + * The list is terminated by a self-pointing
  23220. + * task_struct
  23221. + */
  23222. + tsk->posix_timer_list = tsk;
  23223. + }
  23224. + per_cpu(posix_timer_tasklist, cpu) = tsk;
  23225. +
  23226. + wake_up_process(per_cpu(posix_timer_task, cpu));
  23227. + }
  23228. +}
  23229. +
  23230. +/*
  23231. + * posix_cpu_thread_call - callback that gets triggered when a CPU is added.
  23232. + * Here we can start up the necessary migration thread for the new CPU.
  23233. + */
  23234. +static int posix_cpu_thread_call(struct notifier_block *nfb,
  23235. + unsigned long action, void *hcpu)
  23236. +{
  23237. + int cpu = (long)hcpu;
  23238. + struct task_struct *p;
  23239. + struct sched_param param;
  23240. +
  23241. + switch (action) {
  23242. + case CPU_UP_PREPARE:
  23243. + p = kthread_create(posix_cpu_timers_thread, hcpu,
  23244. + "posixcputmr/%d",cpu);
  23245. + if (IS_ERR(p))
  23246. + return NOTIFY_BAD;
  23247. + p->flags |= PF_NOFREEZE;
  23248. + kthread_bind(p, cpu);
  23249. + /* Must be high prio to avoid getting starved */
  23250. + param.sched_priority = MAX_RT_PRIO-1;
  23251. + sched_setscheduler(p, SCHED_FIFO, &param);
  23252. + per_cpu(posix_timer_task,cpu) = p;
  23253. + break;
  23254. + case CPU_ONLINE:
  23255. + /* Strictly unneccessary, as first user will wake it. */
  23256. + wake_up_process(per_cpu(posix_timer_task,cpu));
  23257. + break;
  23258. +#ifdef CONFIG_HOTPLUG_CPU
  23259. + case CPU_UP_CANCELED:
  23260. + /* Unbind it from offline cpu so it can run. Fall thru. */
  23261. + kthread_bind(per_cpu(posix_timer_task, cpu),
  23262. + cpumask_any(cpu_online_mask));
  23263. + kthread_stop(per_cpu(posix_timer_task,cpu));
  23264. + per_cpu(posix_timer_task,cpu) = NULL;
  23265. + break;
  23266. + case CPU_DEAD:
  23267. + kthread_stop(per_cpu(posix_timer_task,cpu));
  23268. + per_cpu(posix_timer_task,cpu) = NULL;
  23269. + break;
  23270. +#endif
  23271. + }
  23272. + return NOTIFY_OK;
  23273. +}
  23274. +
  23275. +/* Register at highest priority so that task migration (migrate_all_tasks)
  23276. + * happens before everything else.
  23277. + */
  23278. +static struct notifier_block posix_cpu_thread_notifier = {
  23279. + .notifier_call = posix_cpu_thread_call,
  23280. + .priority = 10
  23281. +};
  23282. +
  23283. +static int __init posix_cpu_thread_init(void)
  23284. +{
  23285. + void *hcpu = (void *)(long)smp_processor_id();
  23286. + /* Start one for boot CPU. */
  23287. + unsigned long cpu;
  23288. +
  23289. + /* init the per-cpu posix_timer_tasklets */
  23290. + for_each_possible_cpu(cpu)
  23291. + per_cpu(posix_timer_tasklist, cpu) = NULL;
  23292. +
  23293. + posix_cpu_thread_call(&posix_cpu_thread_notifier, CPU_UP_PREPARE, hcpu);
  23294. + posix_cpu_thread_call(&posix_cpu_thread_notifier, CPU_ONLINE, hcpu);
  23295. + register_cpu_notifier(&posix_cpu_thread_notifier);
  23296. + return 0;
  23297. +}
  23298. +early_initcall(posix_cpu_thread_init);
  23299. +#else /* CONFIG_PREEMPT_RT_BASE */
  23300. +void run_posix_cpu_timers(struct task_struct *tsk)
  23301. +{
  23302. + __run_posix_cpu_timers(tsk);
  23303. +}
  23304. +#endif /* CONFIG_PREEMPT_RT_BASE */
  23305. +
  23306. /*
  23307. * Set one of the process-wide special case CPU timers or RLIMIT_CPU.
  23308. * The tsk->sighand->siglock must be held by the caller.
  23309. diff -Nur linux-4.4.46.orig/kernel/time/posix-timers.c linux-4.4.46/kernel/time/posix-timers.c
  23310. --- linux-4.4.46.orig/kernel/time/posix-timers.c 2017-02-01 08:31:11.000000000 +0100
  23311. +++ linux-4.4.46/kernel/time/posix-timers.c 2017-02-03 17:18:10.939619522 +0100
  23312. @@ -506,6 +506,7 @@
  23313. static struct pid *good_sigevent(sigevent_t * event)
  23314. {
  23315. struct task_struct *rtn = current->group_leader;
  23316. + int sig = event->sigev_signo;
  23317. if ((event->sigev_notify & SIGEV_THREAD_ID ) &&
  23318. (!(rtn = find_task_by_vpid(event->sigev_notify_thread_id)) ||
  23319. @@ -514,7 +515,8 @@
  23320. return NULL;
  23321. if (((event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) &&
  23322. - ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX)))
  23323. + (sig <= 0 || sig > SIGRTMAX || sig_kernel_only(sig) ||
  23324. + sig_kernel_coredump(sig)))
  23325. return NULL;
  23326. return task_pid(rtn);
  23327. @@ -826,6 +828,20 @@
  23328. return overrun;
  23329. }
  23330. +/*
  23331. + * Protected by RCU!
  23332. + */
  23333. +static void timer_wait_for_callback(struct k_clock *kc, struct k_itimer *timr)
  23334. +{
  23335. +#ifdef CONFIG_PREEMPT_RT_FULL
  23336. + if (kc->timer_set == common_timer_set)
  23337. + hrtimer_wait_for_timer(&timr->it.real.timer);
  23338. + else
  23339. + /* FIXME: Whacky hack for posix-cpu-timers */
  23340. + schedule_timeout(1);
  23341. +#endif
  23342. +}
  23343. +
  23344. /* Set a POSIX.1b interval timer. */
  23345. /* timr->it_lock is taken. */
  23346. static int
  23347. @@ -903,6 +919,7 @@
  23348. if (!timr)
  23349. return -EINVAL;
  23350. + rcu_read_lock();
  23351. kc = clockid_to_kclock(timr->it_clock);
  23352. if (WARN_ON_ONCE(!kc || !kc->timer_set))
  23353. error = -EINVAL;
  23354. @@ -911,9 +928,12 @@
  23355. unlock_timer(timr, flag);
  23356. if (error == TIMER_RETRY) {
  23357. + timer_wait_for_callback(kc, timr);
  23358. rtn = NULL; // We already got the old time...
  23359. + rcu_read_unlock();
  23360. goto retry;
  23361. }
  23362. + rcu_read_unlock();
  23363. if (old_setting && !error &&
  23364. copy_to_user(old_setting, &old_spec, sizeof (old_spec)))
  23365. @@ -951,10 +971,15 @@
  23366. if (!timer)
  23367. return -EINVAL;
  23368. + rcu_read_lock();
  23369. if (timer_delete_hook(timer) == TIMER_RETRY) {
  23370. unlock_timer(timer, flags);
  23371. + timer_wait_for_callback(clockid_to_kclock(timer->it_clock),
  23372. + timer);
  23373. + rcu_read_unlock();
  23374. goto retry_delete;
  23375. }
  23376. + rcu_read_unlock();
  23377. spin_lock(&current->sighand->siglock);
  23378. list_del(&timer->list);
  23379. @@ -980,8 +1005,18 @@
  23380. retry_delete:
  23381. spin_lock_irqsave(&timer->it_lock, flags);
  23382. + /* On RT we can race with a deletion */
  23383. + if (!timer->it_signal) {
  23384. + unlock_timer(timer, flags);
  23385. + return;
  23386. + }
  23387. +
  23388. if (timer_delete_hook(timer) == TIMER_RETRY) {
  23389. + rcu_read_lock();
  23390. unlock_timer(timer, flags);
  23391. + timer_wait_for_callback(clockid_to_kclock(timer->it_clock),
  23392. + timer);
  23393. + rcu_read_unlock();
  23394. goto retry_delete;
  23395. }
  23396. list_del(&timer->list);
  23397. diff -Nur linux-4.4.46.orig/kernel/time/tick-broadcast-hrtimer.c linux-4.4.46/kernel/time/tick-broadcast-hrtimer.c
  23398. --- linux-4.4.46.orig/kernel/time/tick-broadcast-hrtimer.c 2017-02-01 08:31:11.000000000 +0100
  23399. +++ linux-4.4.46/kernel/time/tick-broadcast-hrtimer.c 2017-02-03 17:18:10.939619522 +0100
  23400. @@ -106,5 +106,6 @@
  23401. {
  23402. hrtimer_init(&bctimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  23403. bctimer.function = bc_handler;
  23404. + bctimer.irqsafe = true;
  23405. clockevents_register_device(&ce_broadcast_hrtimer);
  23406. }
  23407. diff -Nur linux-4.4.46.orig/kernel/time/tick-common.c linux-4.4.46/kernel/time/tick-common.c
  23408. --- linux-4.4.46.orig/kernel/time/tick-common.c 2017-02-01 08:31:11.000000000 +0100
  23409. +++ linux-4.4.46/kernel/time/tick-common.c 2017-02-03 17:18:10.939619522 +0100
  23410. @@ -79,13 +79,15 @@
  23411. static void tick_periodic(int cpu)
  23412. {
  23413. if (tick_do_timer_cpu == cpu) {
  23414. - write_seqlock(&jiffies_lock);
  23415. + raw_spin_lock(&jiffies_lock);
  23416. + write_seqcount_begin(&jiffies_seq);
  23417. /* Keep track of the next tick event */
  23418. tick_next_period = ktime_add(tick_next_period, tick_period);
  23419. do_timer(1);
  23420. - write_sequnlock(&jiffies_lock);
  23421. + write_seqcount_end(&jiffies_seq);
  23422. + raw_spin_unlock(&jiffies_lock);
  23423. update_wall_time();
  23424. }
  23425. @@ -157,9 +159,9 @@
  23426. ktime_t next;
  23427. do {
  23428. - seq = read_seqbegin(&jiffies_lock);
  23429. + seq = read_seqcount_begin(&jiffies_seq);
  23430. next = tick_next_period;
  23431. - } while (read_seqretry(&jiffies_lock, seq));
  23432. + } while (read_seqcount_retry(&jiffies_seq, seq));
  23433. clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
  23434. diff -Nur linux-4.4.46.orig/kernel/time/tick-sched.c linux-4.4.46/kernel/time/tick-sched.c
  23435. --- linux-4.4.46.orig/kernel/time/tick-sched.c 2017-02-01 08:31:11.000000000 +0100
  23436. +++ linux-4.4.46/kernel/time/tick-sched.c 2017-02-03 17:18:10.939619522 +0100
  23437. @@ -62,7 +62,8 @@
  23438. return;
  23439. /* Reevalute with jiffies_lock held */
  23440. - write_seqlock(&jiffies_lock);
  23441. + raw_spin_lock(&jiffies_lock);
  23442. + write_seqcount_begin(&jiffies_seq);
  23443. delta = ktime_sub(now, last_jiffies_update);
  23444. if (delta.tv64 >= tick_period.tv64) {
  23445. @@ -85,10 +86,12 @@
  23446. /* Keep the tick_next_period variable up to date */
  23447. tick_next_period = ktime_add(last_jiffies_update, tick_period);
  23448. } else {
  23449. - write_sequnlock(&jiffies_lock);
  23450. + write_seqcount_end(&jiffies_seq);
  23451. + raw_spin_unlock(&jiffies_lock);
  23452. return;
  23453. }
  23454. - write_sequnlock(&jiffies_lock);
  23455. + write_seqcount_end(&jiffies_seq);
  23456. + raw_spin_unlock(&jiffies_lock);
  23457. update_wall_time();
  23458. }
  23459. @@ -99,12 +102,14 @@
  23460. {
  23461. ktime_t period;
  23462. - write_seqlock(&jiffies_lock);
  23463. + raw_spin_lock(&jiffies_lock);
  23464. + write_seqcount_begin(&jiffies_seq);
  23465. /* Did we start the jiffies update yet ? */
  23466. if (last_jiffies_update.tv64 == 0)
  23467. last_jiffies_update = tick_next_period;
  23468. period = last_jiffies_update;
  23469. - write_sequnlock(&jiffies_lock);
  23470. + write_seqcount_end(&jiffies_seq);
  23471. + raw_spin_unlock(&jiffies_lock);
  23472. return period;
  23473. }
  23474. @@ -176,6 +181,11 @@
  23475. return false;
  23476. }
  23477. + if (!arch_irq_work_has_interrupt()) {
  23478. + trace_tick_stop(0, "missing irq work interrupt\n");
  23479. + return false;
  23480. + }
  23481. +
  23482. /* sched_clock_tick() needs us? */
  23483. #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
  23484. /*
  23485. @@ -204,6 +214,7 @@
  23486. static DEFINE_PER_CPU(struct irq_work, nohz_full_kick_work) = {
  23487. .func = nohz_full_kick_work_func,
  23488. + .flags = IRQ_WORK_HARD_IRQ,
  23489. };
  23490. /*
  23491. @@ -578,10 +589,10 @@
  23492. /* Read jiffies and the time when jiffies were updated last */
  23493. do {
  23494. - seq = read_seqbegin(&jiffies_lock);
  23495. + seq = read_seqcount_begin(&jiffies_seq);
  23496. basemono = last_jiffies_update.tv64;
  23497. basejiff = jiffies;
  23498. - } while (read_seqretry(&jiffies_lock, seq));
  23499. + } while (read_seqcount_retry(&jiffies_seq, seq));
  23500. ts->last_jiffies = basejiff;
  23501. if (rcu_needs_cpu(basemono, &next_rcu) ||
  23502. @@ -753,14 +764,7 @@
  23503. return false;
  23504. if (unlikely(local_softirq_pending() && cpu_online(cpu))) {
  23505. - static int ratelimit;
  23506. -
  23507. - if (ratelimit < 10 &&
  23508. - (local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK)) {
  23509. - pr_warn("NOHZ: local_softirq_pending %02x\n",
  23510. - (unsigned int) local_softirq_pending());
  23511. - ratelimit++;
  23512. - }
  23513. + softirq_check_pending_idle();
  23514. return false;
  23515. }
  23516. @@ -1100,6 +1104,7 @@
  23517. * Emulate tick processing via per-CPU hrtimers:
  23518. */
  23519. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  23520. + ts->sched_timer.irqsafe = 1;
  23521. ts->sched_timer.function = tick_sched_timer;
  23522. /* Get the next period (per cpu) */
  23523. diff -Nur linux-4.4.46.orig/kernel/time/timekeeping.c linux-4.4.46/kernel/time/timekeeping.c
  23524. --- linux-4.4.46.orig/kernel/time/timekeeping.c 2017-02-01 08:31:11.000000000 +0100
  23525. +++ linux-4.4.46/kernel/time/timekeeping.c 2017-02-03 17:18:10.939619522 +0100
  23526. @@ -2070,8 +2070,10 @@
  23527. */
  23528. void xtime_update(unsigned long ticks)
  23529. {
  23530. - write_seqlock(&jiffies_lock);
  23531. + raw_spin_lock(&jiffies_lock);
  23532. + write_seqcount_begin(&jiffies_seq);
  23533. do_timer(ticks);
  23534. - write_sequnlock(&jiffies_lock);
  23535. + write_seqcount_end(&jiffies_seq);
  23536. + raw_spin_unlock(&jiffies_lock);
  23537. update_wall_time();
  23538. }
  23539. diff -Nur linux-4.4.46.orig/kernel/time/timekeeping.h linux-4.4.46/kernel/time/timekeeping.h
  23540. --- linux-4.4.46.orig/kernel/time/timekeeping.h 2017-02-01 08:31:11.000000000 +0100
  23541. +++ linux-4.4.46/kernel/time/timekeeping.h 2017-02-03 17:18:10.939619522 +0100
  23542. @@ -19,7 +19,8 @@
  23543. extern void do_timer(unsigned long ticks);
  23544. extern void update_wall_time(void);
  23545. -extern seqlock_t jiffies_lock;
  23546. +extern raw_spinlock_t jiffies_lock;
  23547. +extern seqcount_t jiffies_seq;
  23548. #define CS_NAME_LEN 32
  23549. diff -Nur linux-4.4.46.orig/kernel/time/timer.c linux-4.4.46/kernel/time/timer.c
  23550. --- linux-4.4.46.orig/kernel/time/timer.c 2017-02-01 08:31:11.000000000 +0100
  23551. +++ linux-4.4.46/kernel/time/timer.c 2017-02-03 17:18:10.939619522 +0100
  23552. @@ -80,6 +80,9 @@
  23553. struct tvec_base {
  23554. spinlock_t lock;
  23555. struct timer_list *running_timer;
  23556. +#ifdef CONFIG_PREEMPT_RT_FULL
  23557. + wait_queue_head_t wait_for_running_timer;
  23558. +#endif
  23559. unsigned long timer_jiffies;
  23560. unsigned long next_timer;
  23561. unsigned long active_timers;
  23562. @@ -777,6 +780,39 @@
  23563. cpu_relax();
  23564. }
  23565. }
  23566. +#ifdef CONFIG_PREEMPT_RT_FULL
  23567. +static inline struct tvec_base *switch_timer_base(struct timer_list *timer,
  23568. + struct tvec_base *old,
  23569. + struct tvec_base *new)
  23570. +{
  23571. + /*
  23572. + * We cannot do the below because we might be preempted and
  23573. + * then the preempter would see NULL and loop forever.
  23574. + */
  23575. + if (spin_trylock(&new->lock)) {
  23576. + WRITE_ONCE(timer->flags,
  23577. + (timer->flags & ~TIMER_BASEMASK) | new->cpu);
  23578. + spin_unlock(&old->lock);
  23579. + return new;
  23580. + }
  23581. + return old;
  23582. +}
  23583. +
  23584. +#else
  23585. +static inline struct tvec_base *switch_timer_base(struct timer_list *timer,
  23586. + struct tvec_base *old,
  23587. + struct tvec_base *new)
  23588. +{
  23589. + /* See the comment in lock_timer_base() */
  23590. + timer->flags |= TIMER_MIGRATING;
  23591. +
  23592. + spin_unlock(&old->lock);
  23593. + spin_lock(&new->lock);
  23594. + WRITE_ONCE(timer->flags,
  23595. + (timer->flags & ~TIMER_BASEMASK) | new->cpu);
  23596. + return new;
  23597. +}
  23598. +#endif
  23599. static inline int
  23600. __mod_timer(struct timer_list *timer, unsigned long expires,
  23601. @@ -807,16 +843,8 @@
  23602. * handler yet has not finished. This also guarantees that
  23603. * the timer is serialized wrt itself.
  23604. */
  23605. - if (likely(base->running_timer != timer)) {
  23606. - /* See the comment in lock_timer_base() */
  23607. - timer->flags |= TIMER_MIGRATING;
  23608. -
  23609. - spin_unlock(&base->lock);
  23610. - base = new_base;
  23611. - spin_lock(&base->lock);
  23612. - WRITE_ONCE(timer->flags,
  23613. - (timer->flags & ~TIMER_BASEMASK) | base->cpu);
  23614. - }
  23615. + if (likely(base->running_timer != timer))
  23616. + base = switch_timer_base(timer, base, new_base);
  23617. }
  23618. timer->expires = expires;
  23619. @@ -1006,6 +1034,33 @@
  23620. }
  23621. EXPORT_SYMBOL_GPL(add_timer_on);
  23622. +#ifdef CONFIG_PREEMPT_RT_FULL
  23623. +/*
  23624. + * Wait for a running timer
  23625. + */
  23626. +static void wait_for_running_timer(struct timer_list *timer)
  23627. +{
  23628. + struct tvec_base *base;
  23629. + u32 tf = timer->flags;
  23630. +
  23631. + if (tf & TIMER_MIGRATING)
  23632. + return;
  23633. +
  23634. + base = per_cpu_ptr(&tvec_bases, tf & TIMER_CPUMASK);
  23635. + wait_event(base->wait_for_running_timer,
  23636. + base->running_timer != timer);
  23637. +}
  23638. +
  23639. +# define wakeup_timer_waiters(b) wake_up_all(&(b)->wait_for_running_timer)
  23640. +#else
  23641. +static inline void wait_for_running_timer(struct timer_list *timer)
  23642. +{
  23643. + cpu_relax();
  23644. +}
  23645. +
  23646. +# define wakeup_timer_waiters(b) do { } while (0)
  23647. +#endif
  23648. +
  23649. /**
  23650. * del_timer - deactive a timer.
  23651. * @timer: the timer to be deactivated
  23652. @@ -1063,7 +1118,7 @@
  23653. }
  23654. EXPORT_SYMBOL(try_to_del_timer_sync);
  23655. -#ifdef CONFIG_SMP
  23656. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  23657. /**
  23658. * del_timer_sync - deactivate a timer and wait for the handler to finish.
  23659. * @timer: the timer to be deactivated
  23660. @@ -1123,7 +1178,7 @@
  23661. int ret = try_to_del_timer_sync(timer);
  23662. if (ret >= 0)
  23663. return ret;
  23664. - cpu_relax();
  23665. + wait_for_running_timer(timer);
  23666. }
  23667. }
  23668. EXPORT_SYMBOL(del_timer_sync);
  23669. @@ -1248,16 +1303,18 @@
  23670. if (irqsafe) {
  23671. spin_unlock(&base->lock);
  23672. call_timer_fn(timer, fn, data);
  23673. + base->running_timer = NULL;
  23674. spin_lock(&base->lock);
  23675. } else {
  23676. spin_unlock_irq(&base->lock);
  23677. call_timer_fn(timer, fn, data);
  23678. + base->running_timer = NULL;
  23679. spin_lock_irq(&base->lock);
  23680. }
  23681. }
  23682. }
  23683. - base->running_timer = NULL;
  23684. spin_unlock_irq(&base->lock);
  23685. + wakeup_timer_waiters(base);
  23686. }
  23687. #ifdef CONFIG_NO_HZ_COMMON
  23688. @@ -1390,6 +1447,14 @@
  23689. if (cpu_is_offline(smp_processor_id()))
  23690. return expires;
  23691. +#ifdef CONFIG_PREEMPT_RT_FULL
  23692. + /*
  23693. + * On PREEMPT_RT we cannot sleep here. As a result we can't take
  23694. + * the base lock to check when the next timer is pending and so
  23695. + * we assume the next jiffy.
  23696. + */
  23697. + return basem + TICK_NSEC;
  23698. +#endif
  23699. spin_lock(&base->lock);
  23700. if (base->active_timers) {
  23701. if (time_before_eq(base->next_timer, base->timer_jiffies))
  23702. @@ -1416,13 +1481,13 @@
  23703. /* Note: this timer irq context must be accounted for as well. */
  23704. account_process_tick(p, user_tick);
  23705. + scheduler_tick();
  23706. run_local_timers();
  23707. rcu_check_callbacks(user_tick);
  23708. -#ifdef CONFIG_IRQ_WORK
  23709. +#if defined(CONFIG_IRQ_WORK)
  23710. if (in_irq())
  23711. irq_work_tick();
  23712. #endif
  23713. - scheduler_tick();
  23714. run_posix_cpu_timers(p);
  23715. }
  23716. @@ -1433,6 +1498,8 @@
  23717. {
  23718. struct tvec_base *base = this_cpu_ptr(&tvec_bases);
  23719. + irq_work_tick_soft();
  23720. +
  23721. if (time_after_eq(jiffies, base->timer_jiffies))
  23722. __run_timers(base);
  23723. }
  23724. @@ -1589,7 +1656,7 @@
  23725. BUG_ON(cpu_online(cpu));
  23726. old_base = per_cpu_ptr(&tvec_bases, cpu);
  23727. - new_base = get_cpu_ptr(&tvec_bases);
  23728. + new_base = get_local_ptr(&tvec_bases);
  23729. /*
  23730. * The caller is globally serialized and nobody else
  23731. * takes two locks at once, deadlock is not possible.
  23732. @@ -1613,7 +1680,7 @@
  23733. spin_unlock(&old_base->lock);
  23734. spin_unlock_irq(&new_base->lock);
  23735. - put_cpu_ptr(&tvec_bases);
  23736. + put_local_ptr(&tvec_bases);
  23737. }
  23738. static int timer_cpu_notify(struct notifier_block *self,
  23739. @@ -1645,6 +1712,9 @@
  23740. base->cpu = cpu;
  23741. spin_lock_init(&base->lock);
  23742. +#ifdef CONFIG_PREEMPT_RT_FULL
  23743. + init_waitqueue_head(&base->wait_for_running_timer);
  23744. +#endif
  23745. base->timer_jiffies = jiffies;
  23746. base->next_timer = base->timer_jiffies;
  23747. diff -Nur linux-4.4.46.orig/kernel/trace/Kconfig linux-4.4.46/kernel/trace/Kconfig
  23748. --- linux-4.4.46.orig/kernel/trace/Kconfig 2017-02-01 08:31:11.000000000 +0100
  23749. +++ linux-4.4.46/kernel/trace/Kconfig 2017-02-03 17:18:10.939619522 +0100
  23750. @@ -187,6 +187,24 @@
  23751. enabled. This option and the preempt-off timing option can be
  23752. used together or separately.)
  23753. +config INTERRUPT_OFF_HIST
  23754. + bool "Interrupts-off Latency Histogram"
  23755. + depends on IRQSOFF_TRACER
  23756. + help
  23757. + This option generates continuously updated histograms (one per cpu)
  23758. + of the duration of time periods with interrupts disabled. The
  23759. + histograms are disabled by default. To enable them, write a non-zero
  23760. + number to
  23761. +
  23762. + /sys/kernel/debug/tracing/latency_hist/enable/preemptirqsoff
  23763. +
  23764. + If PREEMPT_OFF_HIST is also selected, additional histograms (one
  23765. + per cpu) are generated that accumulate the duration of time periods
  23766. + when both interrupts and preemption are disabled. The histogram data
  23767. + will be located in the debug file system at
  23768. +
  23769. + /sys/kernel/debug/tracing/latency_hist/irqsoff
  23770. +
  23771. config PREEMPT_TRACER
  23772. bool "Preemption-off Latency Tracer"
  23773. default n
  23774. @@ -211,6 +229,24 @@
  23775. enabled. This option and the irqs-off timing option can be
  23776. used together or separately.)
  23777. +config PREEMPT_OFF_HIST
  23778. + bool "Preemption-off Latency Histogram"
  23779. + depends on PREEMPT_TRACER
  23780. + help
  23781. + This option generates continuously updated histograms (one per cpu)
  23782. + of the duration of time periods with preemption disabled. The
  23783. + histograms are disabled by default. To enable them, write a non-zero
  23784. + number to
  23785. +
  23786. + /sys/kernel/debug/tracing/latency_hist/enable/preemptirqsoff
  23787. +
  23788. + If INTERRUPT_OFF_HIST is also selected, additional histograms (one
  23789. + per cpu) are generated that accumulate the duration of time periods
  23790. + when both interrupts and preemption are disabled. The histogram data
  23791. + will be located in the debug file system at
  23792. +
  23793. + /sys/kernel/debug/tracing/latency_hist/preemptoff
  23794. +
  23795. config SCHED_TRACER
  23796. bool "Scheduling Latency Tracer"
  23797. select GENERIC_TRACER
  23798. @@ -221,6 +257,74 @@
  23799. This tracer tracks the latency of the highest priority task
  23800. to be scheduled in, starting from the point it has woken up.
  23801. +config WAKEUP_LATENCY_HIST
  23802. + bool "Scheduling Latency Histogram"
  23803. + depends on SCHED_TRACER
  23804. + help
  23805. + This option generates continuously updated histograms (one per cpu)
  23806. + of the scheduling latency of the highest priority task.
  23807. + The histograms are disabled by default. To enable them, write a
  23808. + non-zero number to
  23809. +
  23810. + /sys/kernel/debug/tracing/latency_hist/enable/wakeup
  23811. +
  23812. + Two different algorithms are used, one to determine the latency of
  23813. + processes that exclusively use the highest priority of the system and
  23814. + another one to determine the latency of processes that share the
  23815. + highest system priority with other processes. The former is used to
  23816. + improve hardware and system software, the latter to optimize the
  23817. + priority design of a given system. The histogram data will be
  23818. + located in the debug file system at
  23819. +
  23820. + /sys/kernel/debug/tracing/latency_hist/wakeup
  23821. +
  23822. + and
  23823. +
  23824. + /sys/kernel/debug/tracing/latency_hist/wakeup/sharedprio
  23825. +
  23826. + If both Scheduling Latency Histogram and Missed Timer Offsets
  23827. + Histogram are selected, additional histogram data will be collected
  23828. + that contain, in addition to the wakeup latency, the timer latency, in
  23829. + case the wakeup was triggered by an expired timer. These histograms
  23830. + are available in the
  23831. +
  23832. + /sys/kernel/debug/tracing/latency_hist/timerandwakeup
  23833. +
  23834. + directory. They reflect the apparent interrupt and scheduling latency
  23835. + and are best suitable to determine the worst-case latency of a given
  23836. + system. To enable these histograms, write a non-zero number to
  23837. +
  23838. + /sys/kernel/debug/tracing/latency_hist/enable/timerandwakeup
  23839. +
  23840. +config MISSED_TIMER_OFFSETS_HIST
  23841. + depends on HIGH_RES_TIMERS
  23842. + select GENERIC_TRACER
  23843. + bool "Missed Timer Offsets Histogram"
  23844. + help
  23845. + Generate a histogram of missed timer offsets in microseconds. The
  23846. + histograms are disabled by default. To enable them, write a non-zero
  23847. + number to
  23848. +
  23849. + /sys/kernel/debug/tracing/latency_hist/enable/missed_timer_offsets
  23850. +
  23851. + The histogram data will be located in the debug file system at
  23852. +
  23853. + /sys/kernel/debug/tracing/latency_hist/missed_timer_offsets
  23854. +
  23855. + If both Scheduling Latency Histogram and Missed Timer Offsets
  23856. + Histogram are selected, additional histogram data will be collected
  23857. + that contain, in addition to the wakeup latency, the timer latency, in
  23858. + case the wakeup was triggered by an expired timer. These histograms
  23859. + are available in the
  23860. +
  23861. + /sys/kernel/debug/tracing/latency_hist/timerandwakeup
  23862. +
  23863. + directory. They reflect the apparent interrupt and scheduling latency
  23864. + and are best suitable to determine the worst-case latency of a given
  23865. + system. To enable these histograms, write a non-zero number to
  23866. +
  23867. + /sys/kernel/debug/tracing/latency_hist/enable/timerandwakeup
  23868. +
  23869. config ENABLE_DEFAULT_TRACERS
  23870. bool "Trace process context switches and events"
  23871. depends on !GENERIC_TRACER
  23872. diff -Nur linux-4.4.46.orig/kernel/trace/latency_hist.c linux-4.4.46/kernel/trace/latency_hist.c
  23873. --- linux-4.4.46.orig/kernel/trace/latency_hist.c 1970-01-01 01:00:00.000000000 +0100
  23874. +++ linux-4.4.46/kernel/trace/latency_hist.c 2017-02-03 17:18:10.939619522 +0100
  23875. @@ -0,0 +1,1178 @@
  23876. +/*
  23877. + * kernel/trace/latency_hist.c
  23878. + *
  23879. + * Add support for histograms of preemption-off latency and
  23880. + * interrupt-off latency and wakeup latency, it depends on
  23881. + * Real-Time Preemption Support.
  23882. + *
  23883. + * Copyright (C) 2005 MontaVista Software, Inc.
  23884. + * Yi Yang <yyang@ch.mvista.com>
  23885. + *
  23886. + * Converted to work with the new latency tracer.
  23887. + * Copyright (C) 2008 Red Hat, Inc.
  23888. + * Steven Rostedt <srostedt@redhat.com>
  23889. + *
  23890. + */
  23891. +#include <linux/module.h>
  23892. +#include <linux/debugfs.h>
  23893. +#include <linux/seq_file.h>
  23894. +#include <linux/percpu.h>
  23895. +#include <linux/kallsyms.h>
  23896. +#include <linux/uaccess.h>
  23897. +#include <linux/sched.h>
  23898. +#include <linux/sched/rt.h>
  23899. +#include <linux/slab.h>
  23900. +#include <linux/atomic.h>
  23901. +#include <asm/div64.h>
  23902. +
  23903. +#include "trace.h"
  23904. +#include <trace/events/sched.h>
  23905. +
  23906. +#define NSECS_PER_USECS 1000L
  23907. +
  23908. +#define CREATE_TRACE_POINTS
  23909. +#include <trace/events/hist.h>
  23910. +
  23911. +enum {
  23912. + IRQSOFF_LATENCY = 0,
  23913. + PREEMPTOFF_LATENCY,
  23914. + PREEMPTIRQSOFF_LATENCY,
  23915. + WAKEUP_LATENCY,
  23916. + WAKEUP_LATENCY_SHAREDPRIO,
  23917. + MISSED_TIMER_OFFSETS,
  23918. + TIMERANDWAKEUP_LATENCY,
  23919. + MAX_LATENCY_TYPE,
  23920. +};
  23921. +
  23922. +#define MAX_ENTRY_NUM 10240
  23923. +
  23924. +struct hist_data {
  23925. + atomic_t hist_mode; /* 0 log, 1 don't log */
  23926. + long offset; /* set it to MAX_ENTRY_NUM/2 for a bipolar scale */
  23927. + long min_lat;
  23928. + long max_lat;
  23929. + unsigned long long below_hist_bound_samples;
  23930. + unsigned long long above_hist_bound_samples;
  23931. + long long accumulate_lat;
  23932. + unsigned long long total_samples;
  23933. + unsigned long long hist_array[MAX_ENTRY_NUM];
  23934. +};
  23935. +
  23936. +struct enable_data {
  23937. + int latency_type;
  23938. + int enabled;
  23939. +};
  23940. +
  23941. +static char *latency_hist_dir_root = "latency_hist";
  23942. +
  23943. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  23944. +static DEFINE_PER_CPU(struct hist_data, irqsoff_hist);
  23945. +static char *irqsoff_hist_dir = "irqsoff";
  23946. +static DEFINE_PER_CPU(cycles_t, hist_irqsoff_start);
  23947. +static DEFINE_PER_CPU(int, hist_irqsoff_counting);
  23948. +#endif
  23949. +
  23950. +#ifdef CONFIG_PREEMPT_OFF_HIST
  23951. +static DEFINE_PER_CPU(struct hist_data, preemptoff_hist);
  23952. +static char *preemptoff_hist_dir = "preemptoff";
  23953. +static DEFINE_PER_CPU(cycles_t, hist_preemptoff_start);
  23954. +static DEFINE_PER_CPU(int, hist_preemptoff_counting);
  23955. +#endif
  23956. +
  23957. +#if defined(CONFIG_PREEMPT_OFF_HIST) && defined(CONFIG_INTERRUPT_OFF_HIST)
  23958. +static DEFINE_PER_CPU(struct hist_data, preemptirqsoff_hist);
  23959. +static char *preemptirqsoff_hist_dir = "preemptirqsoff";
  23960. +static DEFINE_PER_CPU(cycles_t, hist_preemptirqsoff_start);
  23961. +static DEFINE_PER_CPU(int, hist_preemptirqsoff_counting);
  23962. +#endif
  23963. +
  23964. +#if defined(CONFIG_PREEMPT_OFF_HIST) || defined(CONFIG_INTERRUPT_OFF_HIST)
  23965. +static notrace void probe_preemptirqsoff_hist(void *v, int reason, int start);
  23966. +static struct enable_data preemptirqsoff_enabled_data = {
  23967. + .latency_type = PREEMPTIRQSOFF_LATENCY,
  23968. + .enabled = 0,
  23969. +};
  23970. +#endif
  23971. +
  23972. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  23973. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  23974. +struct maxlatproc_data {
  23975. + char comm[FIELD_SIZEOF(struct task_struct, comm)];
  23976. + char current_comm[FIELD_SIZEOF(struct task_struct, comm)];
  23977. + int pid;
  23978. + int current_pid;
  23979. + int prio;
  23980. + int current_prio;
  23981. + long latency;
  23982. + long timeroffset;
  23983. + cycle_t timestamp;
  23984. +};
  23985. +#endif
  23986. +
  23987. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  23988. +static DEFINE_PER_CPU(struct hist_data, wakeup_latency_hist);
  23989. +static DEFINE_PER_CPU(struct hist_data, wakeup_latency_hist_sharedprio);
  23990. +static char *wakeup_latency_hist_dir = "wakeup";
  23991. +static char *wakeup_latency_hist_dir_sharedprio = "sharedprio";
  23992. +static notrace void probe_wakeup_latency_hist_start(void *v,
  23993. + struct task_struct *p);
  23994. +static notrace void probe_wakeup_latency_hist_stop(void *v,
  23995. + bool preempt, struct task_struct *prev, struct task_struct *next);
  23996. +static notrace void probe_sched_migrate_task(void *,
  23997. + struct task_struct *task, int cpu);
  23998. +static struct enable_data wakeup_latency_enabled_data = {
  23999. + .latency_type = WAKEUP_LATENCY,
  24000. + .enabled = 0,
  24001. +};
  24002. +static DEFINE_PER_CPU(struct maxlatproc_data, wakeup_maxlatproc);
  24003. +static DEFINE_PER_CPU(struct maxlatproc_data, wakeup_maxlatproc_sharedprio);
  24004. +static DEFINE_PER_CPU(struct task_struct *, wakeup_task);
  24005. +static DEFINE_PER_CPU(int, wakeup_sharedprio);
  24006. +static unsigned long wakeup_pid;
  24007. +#endif
  24008. +
  24009. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  24010. +static DEFINE_PER_CPU(struct hist_data, missed_timer_offsets);
  24011. +static char *missed_timer_offsets_dir = "missed_timer_offsets";
  24012. +static notrace void probe_hrtimer_interrupt(void *v, int cpu,
  24013. + long long offset, struct task_struct *curr, struct task_struct *task);
  24014. +static struct enable_data missed_timer_offsets_enabled_data = {
  24015. + .latency_type = MISSED_TIMER_OFFSETS,
  24016. + .enabled = 0,
  24017. +};
  24018. +static DEFINE_PER_CPU(struct maxlatproc_data, missed_timer_offsets_maxlatproc);
  24019. +static unsigned long missed_timer_offsets_pid;
  24020. +#endif
  24021. +
  24022. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  24023. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24024. +static DEFINE_PER_CPU(struct hist_data, timerandwakeup_latency_hist);
  24025. +static char *timerandwakeup_latency_hist_dir = "timerandwakeup";
  24026. +static struct enable_data timerandwakeup_enabled_data = {
  24027. + .latency_type = TIMERANDWAKEUP_LATENCY,
  24028. + .enabled = 0,
  24029. +};
  24030. +static DEFINE_PER_CPU(struct maxlatproc_data, timerandwakeup_maxlatproc);
  24031. +#endif
  24032. +
  24033. +void notrace latency_hist(int latency_type, int cpu, long latency,
  24034. + long timeroffset, cycle_t stop,
  24035. + struct task_struct *p)
  24036. +{
  24037. + struct hist_data *my_hist;
  24038. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  24039. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24040. + struct maxlatproc_data *mp = NULL;
  24041. +#endif
  24042. +
  24043. + if (!cpu_possible(cpu) || latency_type < 0 ||
  24044. + latency_type >= MAX_LATENCY_TYPE)
  24045. + return;
  24046. +
  24047. + switch (latency_type) {
  24048. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  24049. + case IRQSOFF_LATENCY:
  24050. + my_hist = &per_cpu(irqsoff_hist, cpu);
  24051. + break;
  24052. +#endif
  24053. +#ifdef CONFIG_PREEMPT_OFF_HIST
  24054. + case PREEMPTOFF_LATENCY:
  24055. + my_hist = &per_cpu(preemptoff_hist, cpu);
  24056. + break;
  24057. +#endif
  24058. +#if defined(CONFIG_PREEMPT_OFF_HIST) && defined(CONFIG_INTERRUPT_OFF_HIST)
  24059. + case PREEMPTIRQSOFF_LATENCY:
  24060. + my_hist = &per_cpu(preemptirqsoff_hist, cpu);
  24061. + break;
  24062. +#endif
  24063. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  24064. + case WAKEUP_LATENCY:
  24065. + my_hist = &per_cpu(wakeup_latency_hist, cpu);
  24066. + mp = &per_cpu(wakeup_maxlatproc, cpu);
  24067. + break;
  24068. + case WAKEUP_LATENCY_SHAREDPRIO:
  24069. + my_hist = &per_cpu(wakeup_latency_hist_sharedprio, cpu);
  24070. + mp = &per_cpu(wakeup_maxlatproc_sharedprio, cpu);
  24071. + break;
  24072. +#endif
  24073. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  24074. + case MISSED_TIMER_OFFSETS:
  24075. + my_hist = &per_cpu(missed_timer_offsets, cpu);
  24076. + mp = &per_cpu(missed_timer_offsets_maxlatproc, cpu);
  24077. + break;
  24078. +#endif
  24079. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  24080. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24081. + case TIMERANDWAKEUP_LATENCY:
  24082. + my_hist = &per_cpu(timerandwakeup_latency_hist, cpu);
  24083. + mp = &per_cpu(timerandwakeup_maxlatproc, cpu);
  24084. + break;
  24085. +#endif
  24086. +
  24087. + default:
  24088. + return;
  24089. + }
  24090. +
  24091. + latency += my_hist->offset;
  24092. +
  24093. + if (atomic_read(&my_hist->hist_mode) == 0)
  24094. + return;
  24095. +
  24096. + if (latency < 0 || latency >= MAX_ENTRY_NUM) {
  24097. + if (latency < 0)
  24098. + my_hist->below_hist_bound_samples++;
  24099. + else
  24100. + my_hist->above_hist_bound_samples++;
  24101. + } else
  24102. + my_hist->hist_array[latency]++;
  24103. +
  24104. + if (unlikely(latency > my_hist->max_lat ||
  24105. + my_hist->min_lat == LONG_MAX)) {
  24106. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  24107. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24108. + if (latency_type == WAKEUP_LATENCY ||
  24109. + latency_type == WAKEUP_LATENCY_SHAREDPRIO ||
  24110. + latency_type == MISSED_TIMER_OFFSETS ||
  24111. + latency_type == TIMERANDWAKEUP_LATENCY) {
  24112. + strncpy(mp->comm, p->comm, sizeof(mp->comm));
  24113. + strncpy(mp->current_comm, current->comm,
  24114. + sizeof(mp->current_comm));
  24115. + mp->pid = task_pid_nr(p);
  24116. + mp->current_pid = task_pid_nr(current);
  24117. + mp->prio = p->prio;
  24118. + mp->current_prio = current->prio;
  24119. + mp->latency = latency;
  24120. + mp->timeroffset = timeroffset;
  24121. + mp->timestamp = stop;
  24122. + }
  24123. +#endif
  24124. + my_hist->max_lat = latency;
  24125. + }
  24126. + if (unlikely(latency < my_hist->min_lat))
  24127. + my_hist->min_lat = latency;
  24128. + my_hist->total_samples++;
  24129. + my_hist->accumulate_lat += latency;
  24130. +}
  24131. +
  24132. +static void *l_start(struct seq_file *m, loff_t *pos)
  24133. +{
  24134. + loff_t *index_ptr = NULL;
  24135. + loff_t index = *pos;
  24136. + struct hist_data *my_hist = m->private;
  24137. +
  24138. + if (index == 0) {
  24139. + char minstr[32], avgstr[32], maxstr[32];
  24140. +
  24141. + atomic_dec(&my_hist->hist_mode);
  24142. +
  24143. + if (likely(my_hist->total_samples)) {
  24144. + long avg = (long) div64_s64(my_hist->accumulate_lat,
  24145. + my_hist->total_samples);
  24146. + snprintf(minstr, sizeof(minstr), "%ld",
  24147. + my_hist->min_lat - my_hist->offset);
  24148. + snprintf(avgstr, sizeof(avgstr), "%ld",
  24149. + avg - my_hist->offset);
  24150. + snprintf(maxstr, sizeof(maxstr), "%ld",
  24151. + my_hist->max_lat - my_hist->offset);
  24152. + } else {
  24153. + strcpy(minstr, "<undef>");
  24154. + strcpy(avgstr, minstr);
  24155. + strcpy(maxstr, minstr);
  24156. + }
  24157. +
  24158. + seq_printf(m, "#Minimum latency: %s microseconds\n"
  24159. + "#Average latency: %s microseconds\n"
  24160. + "#Maximum latency: %s microseconds\n"
  24161. + "#Total samples: %llu\n"
  24162. + "#There are %llu samples lower than %ld"
  24163. + " microseconds.\n"
  24164. + "#There are %llu samples greater or equal"
  24165. + " than %ld microseconds.\n"
  24166. + "#usecs\t%16s\n",
  24167. + minstr, avgstr, maxstr,
  24168. + my_hist->total_samples,
  24169. + my_hist->below_hist_bound_samples,
  24170. + -my_hist->offset,
  24171. + my_hist->above_hist_bound_samples,
  24172. + MAX_ENTRY_NUM - my_hist->offset,
  24173. + "samples");
  24174. + }
  24175. + if (index < MAX_ENTRY_NUM) {
  24176. + index_ptr = kmalloc(sizeof(loff_t), GFP_KERNEL);
  24177. + if (index_ptr)
  24178. + *index_ptr = index;
  24179. + }
  24180. +
  24181. + return index_ptr;
  24182. +}
  24183. +
  24184. +static void *l_next(struct seq_file *m, void *p, loff_t *pos)
  24185. +{
  24186. + loff_t *index_ptr = p;
  24187. + struct hist_data *my_hist = m->private;
  24188. +
  24189. + if (++*pos >= MAX_ENTRY_NUM) {
  24190. + atomic_inc(&my_hist->hist_mode);
  24191. + return NULL;
  24192. + }
  24193. + *index_ptr = *pos;
  24194. + return index_ptr;
  24195. +}
  24196. +
  24197. +static void l_stop(struct seq_file *m, void *p)
  24198. +{
  24199. + kfree(p);
  24200. +}
  24201. +
  24202. +static int l_show(struct seq_file *m, void *p)
  24203. +{
  24204. + int index = *(loff_t *) p;
  24205. + struct hist_data *my_hist = m->private;
  24206. +
  24207. + seq_printf(m, "%6ld\t%16llu\n", index - my_hist->offset,
  24208. + my_hist->hist_array[index]);
  24209. + return 0;
  24210. +}
  24211. +
  24212. +static const struct seq_operations latency_hist_seq_op = {
  24213. + .start = l_start,
  24214. + .next = l_next,
  24215. + .stop = l_stop,
  24216. + .show = l_show
  24217. +};
  24218. +
  24219. +static int latency_hist_open(struct inode *inode, struct file *file)
  24220. +{
  24221. + int ret;
  24222. +
  24223. + ret = seq_open(file, &latency_hist_seq_op);
  24224. + if (!ret) {
  24225. + struct seq_file *seq = file->private_data;
  24226. + seq->private = inode->i_private;
  24227. + }
  24228. + return ret;
  24229. +}
  24230. +
  24231. +static const struct file_operations latency_hist_fops = {
  24232. + .open = latency_hist_open,
  24233. + .read = seq_read,
  24234. + .llseek = seq_lseek,
  24235. + .release = seq_release,
  24236. +};
  24237. +
  24238. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  24239. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24240. +static void clear_maxlatprocdata(struct maxlatproc_data *mp)
  24241. +{
  24242. + mp->comm[0] = mp->current_comm[0] = '\0';
  24243. + mp->prio = mp->current_prio = mp->pid = mp->current_pid =
  24244. + mp->latency = mp->timeroffset = -1;
  24245. + mp->timestamp = 0;
  24246. +}
  24247. +#endif
  24248. +
  24249. +static void hist_reset(struct hist_data *hist)
  24250. +{
  24251. + atomic_dec(&hist->hist_mode);
  24252. +
  24253. + memset(hist->hist_array, 0, sizeof(hist->hist_array));
  24254. + hist->below_hist_bound_samples = 0ULL;
  24255. + hist->above_hist_bound_samples = 0ULL;
  24256. + hist->min_lat = LONG_MAX;
  24257. + hist->max_lat = LONG_MIN;
  24258. + hist->total_samples = 0ULL;
  24259. + hist->accumulate_lat = 0LL;
  24260. +
  24261. + atomic_inc(&hist->hist_mode);
  24262. +}
  24263. +
  24264. +static ssize_t
  24265. +latency_hist_reset(struct file *file, const char __user *a,
  24266. + size_t size, loff_t *off)
  24267. +{
  24268. + int cpu;
  24269. + struct hist_data *hist = NULL;
  24270. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  24271. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24272. + struct maxlatproc_data *mp = NULL;
  24273. +#endif
  24274. + off_t latency_type = (off_t) file->private_data;
  24275. +
  24276. + for_each_online_cpu(cpu) {
  24277. +
  24278. + switch (latency_type) {
  24279. +#ifdef CONFIG_PREEMPT_OFF_HIST
  24280. + case PREEMPTOFF_LATENCY:
  24281. + hist = &per_cpu(preemptoff_hist, cpu);
  24282. + break;
  24283. +#endif
  24284. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  24285. + case IRQSOFF_LATENCY:
  24286. + hist = &per_cpu(irqsoff_hist, cpu);
  24287. + break;
  24288. +#endif
  24289. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  24290. + case PREEMPTIRQSOFF_LATENCY:
  24291. + hist = &per_cpu(preemptirqsoff_hist, cpu);
  24292. + break;
  24293. +#endif
  24294. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  24295. + case WAKEUP_LATENCY:
  24296. + hist = &per_cpu(wakeup_latency_hist, cpu);
  24297. + mp = &per_cpu(wakeup_maxlatproc, cpu);
  24298. + break;
  24299. + case WAKEUP_LATENCY_SHAREDPRIO:
  24300. + hist = &per_cpu(wakeup_latency_hist_sharedprio, cpu);
  24301. + mp = &per_cpu(wakeup_maxlatproc_sharedprio, cpu);
  24302. + break;
  24303. +#endif
  24304. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  24305. + case MISSED_TIMER_OFFSETS:
  24306. + hist = &per_cpu(missed_timer_offsets, cpu);
  24307. + mp = &per_cpu(missed_timer_offsets_maxlatproc, cpu);
  24308. + break;
  24309. +#endif
  24310. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  24311. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24312. + case TIMERANDWAKEUP_LATENCY:
  24313. + hist = &per_cpu(timerandwakeup_latency_hist, cpu);
  24314. + mp = &per_cpu(timerandwakeup_maxlatproc, cpu);
  24315. + break;
  24316. +#endif
  24317. + }
  24318. +
  24319. + hist_reset(hist);
  24320. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  24321. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24322. + if (latency_type == WAKEUP_LATENCY ||
  24323. + latency_type == WAKEUP_LATENCY_SHAREDPRIO ||
  24324. + latency_type == MISSED_TIMER_OFFSETS ||
  24325. + latency_type == TIMERANDWAKEUP_LATENCY)
  24326. + clear_maxlatprocdata(mp);
  24327. +#endif
  24328. + }
  24329. +
  24330. + return size;
  24331. +}
  24332. +
  24333. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  24334. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24335. +static ssize_t
  24336. +show_pid(struct file *file, char __user *ubuf, size_t cnt, loff_t *ppos)
  24337. +{
  24338. + char buf[64];
  24339. + int r;
  24340. + unsigned long *this_pid = file->private_data;
  24341. +
  24342. + r = snprintf(buf, sizeof(buf), "%lu\n", *this_pid);
  24343. + return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  24344. +}
  24345. +
  24346. +static ssize_t do_pid(struct file *file, const char __user *ubuf,
  24347. + size_t cnt, loff_t *ppos)
  24348. +{
  24349. + char buf[64];
  24350. + unsigned long pid;
  24351. + unsigned long *this_pid = file->private_data;
  24352. +
  24353. + if (cnt >= sizeof(buf))
  24354. + return -EINVAL;
  24355. +
  24356. + if (copy_from_user(&buf, ubuf, cnt))
  24357. + return -EFAULT;
  24358. +
  24359. + buf[cnt] = '\0';
  24360. +
  24361. + if (kstrtoul(buf, 10, &pid))
  24362. + return -EINVAL;
  24363. +
  24364. + *this_pid = pid;
  24365. +
  24366. + return cnt;
  24367. +}
  24368. +#endif
  24369. +
  24370. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  24371. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24372. +static ssize_t
  24373. +show_maxlatproc(struct file *file, char __user *ubuf, size_t cnt, loff_t *ppos)
  24374. +{
  24375. + int r;
  24376. + struct maxlatproc_data *mp = file->private_data;
  24377. + int strmaxlen = (TASK_COMM_LEN * 2) + (8 * 8);
  24378. + unsigned long long t;
  24379. + unsigned long usecs, secs;
  24380. + char *buf;
  24381. +
  24382. + if (mp->pid == -1 || mp->current_pid == -1) {
  24383. + buf = "(none)\n";
  24384. + return simple_read_from_buffer(ubuf, cnt, ppos, buf,
  24385. + strlen(buf));
  24386. + }
  24387. +
  24388. + buf = kmalloc(strmaxlen, GFP_KERNEL);
  24389. + if (buf == NULL)
  24390. + return -ENOMEM;
  24391. +
  24392. + t = ns2usecs(mp->timestamp);
  24393. + usecs = do_div(t, USEC_PER_SEC);
  24394. + secs = (unsigned long) t;
  24395. + r = snprintf(buf, strmaxlen,
  24396. + "%d %d %ld (%ld) %s <- %d %d %s %lu.%06lu\n", mp->pid,
  24397. + MAX_RT_PRIO-1 - mp->prio, mp->latency, mp->timeroffset, mp->comm,
  24398. + mp->current_pid, MAX_RT_PRIO-1 - mp->current_prio, mp->current_comm,
  24399. + secs, usecs);
  24400. + r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  24401. + kfree(buf);
  24402. + return r;
  24403. +}
  24404. +#endif
  24405. +
  24406. +static ssize_t
  24407. +show_enable(struct file *file, char __user *ubuf, size_t cnt, loff_t *ppos)
  24408. +{
  24409. + char buf[64];
  24410. + struct enable_data *ed = file->private_data;
  24411. + int r;
  24412. +
  24413. + r = snprintf(buf, sizeof(buf), "%d\n", ed->enabled);
  24414. + return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  24415. +}
  24416. +
  24417. +static ssize_t
  24418. +do_enable(struct file *file, const char __user *ubuf, size_t cnt, loff_t *ppos)
  24419. +{
  24420. + char buf[64];
  24421. + long enable;
  24422. + struct enable_data *ed = file->private_data;
  24423. +
  24424. + if (cnt >= sizeof(buf))
  24425. + return -EINVAL;
  24426. +
  24427. + if (copy_from_user(&buf, ubuf, cnt))
  24428. + return -EFAULT;
  24429. +
  24430. + buf[cnt] = 0;
  24431. +
  24432. + if (kstrtoul(buf, 10, &enable))
  24433. + return -EINVAL;
  24434. +
  24435. + if ((enable && ed->enabled) || (!enable && !ed->enabled))
  24436. + return cnt;
  24437. +
  24438. + if (enable) {
  24439. + int ret;
  24440. +
  24441. + switch (ed->latency_type) {
  24442. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  24443. + case PREEMPTIRQSOFF_LATENCY:
  24444. + ret = register_trace_preemptirqsoff_hist(
  24445. + probe_preemptirqsoff_hist, NULL);
  24446. + if (ret) {
  24447. + pr_info("wakeup trace: Couldn't assign "
  24448. + "probe_preemptirqsoff_hist "
  24449. + "to trace_preemptirqsoff_hist\n");
  24450. + return ret;
  24451. + }
  24452. + break;
  24453. +#endif
  24454. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  24455. + case WAKEUP_LATENCY:
  24456. + ret = register_trace_sched_wakeup(
  24457. + probe_wakeup_latency_hist_start, NULL);
  24458. + if (ret) {
  24459. + pr_info("wakeup trace: Couldn't assign "
  24460. + "probe_wakeup_latency_hist_start "
  24461. + "to trace_sched_wakeup\n");
  24462. + return ret;
  24463. + }
  24464. + ret = register_trace_sched_wakeup_new(
  24465. + probe_wakeup_latency_hist_start, NULL);
  24466. + if (ret) {
  24467. + pr_info("wakeup trace: Couldn't assign "
  24468. + "probe_wakeup_latency_hist_start "
  24469. + "to trace_sched_wakeup_new\n");
  24470. + unregister_trace_sched_wakeup(
  24471. + probe_wakeup_latency_hist_start, NULL);
  24472. + return ret;
  24473. + }
  24474. + ret = register_trace_sched_switch(
  24475. + probe_wakeup_latency_hist_stop, NULL);
  24476. + if (ret) {
  24477. + pr_info("wakeup trace: Couldn't assign "
  24478. + "probe_wakeup_latency_hist_stop "
  24479. + "to trace_sched_switch\n");
  24480. + unregister_trace_sched_wakeup(
  24481. + probe_wakeup_latency_hist_start, NULL);
  24482. + unregister_trace_sched_wakeup_new(
  24483. + probe_wakeup_latency_hist_start, NULL);
  24484. + return ret;
  24485. + }
  24486. + ret = register_trace_sched_migrate_task(
  24487. + probe_sched_migrate_task, NULL);
  24488. + if (ret) {
  24489. + pr_info("wakeup trace: Couldn't assign "
  24490. + "probe_sched_migrate_task "
  24491. + "to trace_sched_migrate_task\n");
  24492. + unregister_trace_sched_wakeup(
  24493. + probe_wakeup_latency_hist_start, NULL);
  24494. + unregister_trace_sched_wakeup_new(
  24495. + probe_wakeup_latency_hist_start, NULL);
  24496. + unregister_trace_sched_switch(
  24497. + probe_wakeup_latency_hist_stop, NULL);
  24498. + return ret;
  24499. + }
  24500. + break;
  24501. +#endif
  24502. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  24503. + case MISSED_TIMER_OFFSETS:
  24504. + ret = register_trace_hrtimer_interrupt(
  24505. + probe_hrtimer_interrupt, NULL);
  24506. + if (ret) {
  24507. + pr_info("wakeup trace: Couldn't assign "
  24508. + "probe_hrtimer_interrupt "
  24509. + "to trace_hrtimer_interrupt\n");
  24510. + return ret;
  24511. + }
  24512. + break;
  24513. +#endif
  24514. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  24515. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24516. + case TIMERANDWAKEUP_LATENCY:
  24517. + if (!wakeup_latency_enabled_data.enabled ||
  24518. + !missed_timer_offsets_enabled_data.enabled)
  24519. + return -EINVAL;
  24520. + break;
  24521. +#endif
  24522. + default:
  24523. + break;
  24524. + }
  24525. + } else {
  24526. + switch (ed->latency_type) {
  24527. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  24528. + case PREEMPTIRQSOFF_LATENCY:
  24529. + {
  24530. + int cpu;
  24531. +
  24532. + unregister_trace_preemptirqsoff_hist(
  24533. + probe_preemptirqsoff_hist, NULL);
  24534. + for_each_online_cpu(cpu) {
  24535. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  24536. + per_cpu(hist_irqsoff_counting,
  24537. + cpu) = 0;
  24538. +#endif
  24539. +#ifdef CONFIG_PREEMPT_OFF_HIST
  24540. + per_cpu(hist_preemptoff_counting,
  24541. + cpu) = 0;
  24542. +#endif
  24543. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  24544. + per_cpu(hist_preemptirqsoff_counting,
  24545. + cpu) = 0;
  24546. +#endif
  24547. + }
  24548. + }
  24549. + break;
  24550. +#endif
  24551. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  24552. + case WAKEUP_LATENCY:
  24553. + {
  24554. + int cpu;
  24555. +
  24556. + unregister_trace_sched_wakeup(
  24557. + probe_wakeup_latency_hist_start, NULL);
  24558. + unregister_trace_sched_wakeup_new(
  24559. + probe_wakeup_latency_hist_start, NULL);
  24560. + unregister_trace_sched_switch(
  24561. + probe_wakeup_latency_hist_stop, NULL);
  24562. + unregister_trace_sched_migrate_task(
  24563. + probe_sched_migrate_task, NULL);
  24564. +
  24565. + for_each_online_cpu(cpu) {
  24566. + per_cpu(wakeup_task, cpu) = NULL;
  24567. + per_cpu(wakeup_sharedprio, cpu) = 0;
  24568. + }
  24569. + }
  24570. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  24571. + timerandwakeup_enabled_data.enabled = 0;
  24572. +#endif
  24573. + break;
  24574. +#endif
  24575. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  24576. + case MISSED_TIMER_OFFSETS:
  24577. + unregister_trace_hrtimer_interrupt(
  24578. + probe_hrtimer_interrupt, NULL);
  24579. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  24580. + timerandwakeup_enabled_data.enabled = 0;
  24581. +#endif
  24582. + break;
  24583. +#endif
  24584. + default:
  24585. + break;
  24586. + }
  24587. + }
  24588. + ed->enabled = enable;
  24589. + return cnt;
  24590. +}
  24591. +
  24592. +static const struct file_operations latency_hist_reset_fops = {
  24593. + .open = tracing_open_generic,
  24594. + .write = latency_hist_reset,
  24595. +};
  24596. +
  24597. +static const struct file_operations enable_fops = {
  24598. + .open = tracing_open_generic,
  24599. + .read = show_enable,
  24600. + .write = do_enable,
  24601. +};
  24602. +
  24603. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  24604. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24605. +static const struct file_operations pid_fops = {
  24606. + .open = tracing_open_generic,
  24607. + .read = show_pid,
  24608. + .write = do_pid,
  24609. +};
  24610. +
  24611. +static const struct file_operations maxlatproc_fops = {
  24612. + .open = tracing_open_generic,
  24613. + .read = show_maxlatproc,
  24614. +};
  24615. +#endif
  24616. +
  24617. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  24618. +static notrace void probe_preemptirqsoff_hist(void *v, int reason,
  24619. + int starthist)
  24620. +{
  24621. + int cpu = raw_smp_processor_id();
  24622. + int time_set = 0;
  24623. +
  24624. + if (starthist) {
  24625. + cycle_t uninitialized_var(start);
  24626. +
  24627. + if (!preempt_count() && !irqs_disabled())
  24628. + return;
  24629. +
  24630. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  24631. + if ((reason == IRQS_OFF || reason == TRACE_START) &&
  24632. + !per_cpu(hist_irqsoff_counting, cpu)) {
  24633. + per_cpu(hist_irqsoff_counting, cpu) = 1;
  24634. + start = ftrace_now(cpu);
  24635. + time_set++;
  24636. + per_cpu(hist_irqsoff_start, cpu) = start;
  24637. + }
  24638. +#endif
  24639. +
  24640. +#ifdef CONFIG_PREEMPT_OFF_HIST
  24641. + if ((reason == PREEMPT_OFF || reason == TRACE_START) &&
  24642. + !per_cpu(hist_preemptoff_counting, cpu)) {
  24643. + per_cpu(hist_preemptoff_counting, cpu) = 1;
  24644. + if (!(time_set++))
  24645. + start = ftrace_now(cpu);
  24646. + per_cpu(hist_preemptoff_start, cpu) = start;
  24647. + }
  24648. +#endif
  24649. +
  24650. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  24651. + if (per_cpu(hist_irqsoff_counting, cpu) &&
  24652. + per_cpu(hist_preemptoff_counting, cpu) &&
  24653. + !per_cpu(hist_preemptirqsoff_counting, cpu)) {
  24654. + per_cpu(hist_preemptirqsoff_counting, cpu) = 1;
  24655. + if (!time_set)
  24656. + start = ftrace_now(cpu);
  24657. + per_cpu(hist_preemptirqsoff_start, cpu) = start;
  24658. + }
  24659. +#endif
  24660. + } else {
  24661. + cycle_t uninitialized_var(stop);
  24662. +
  24663. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  24664. + if ((reason == IRQS_ON || reason == TRACE_STOP) &&
  24665. + per_cpu(hist_irqsoff_counting, cpu)) {
  24666. + cycle_t start = per_cpu(hist_irqsoff_start, cpu);
  24667. +
  24668. + stop = ftrace_now(cpu);
  24669. + time_set++;
  24670. + if (start) {
  24671. + long latency = ((long) (stop - start)) /
  24672. + NSECS_PER_USECS;
  24673. +
  24674. + latency_hist(IRQSOFF_LATENCY, cpu, latency, 0,
  24675. + stop, NULL);
  24676. + }
  24677. + per_cpu(hist_irqsoff_counting, cpu) = 0;
  24678. + }
  24679. +#endif
  24680. +
  24681. +#ifdef CONFIG_PREEMPT_OFF_HIST
  24682. + if ((reason == PREEMPT_ON || reason == TRACE_STOP) &&
  24683. + per_cpu(hist_preemptoff_counting, cpu)) {
  24684. + cycle_t start = per_cpu(hist_preemptoff_start, cpu);
  24685. +
  24686. + if (!(time_set++))
  24687. + stop = ftrace_now(cpu);
  24688. + if (start) {
  24689. + long latency = ((long) (stop - start)) /
  24690. + NSECS_PER_USECS;
  24691. +
  24692. + latency_hist(PREEMPTOFF_LATENCY, cpu, latency,
  24693. + 0, stop, NULL);
  24694. + }
  24695. + per_cpu(hist_preemptoff_counting, cpu) = 0;
  24696. + }
  24697. +#endif
  24698. +
  24699. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  24700. + if ((!per_cpu(hist_irqsoff_counting, cpu) ||
  24701. + !per_cpu(hist_preemptoff_counting, cpu)) &&
  24702. + per_cpu(hist_preemptirqsoff_counting, cpu)) {
  24703. + cycle_t start = per_cpu(hist_preemptirqsoff_start, cpu);
  24704. +
  24705. + if (!time_set)
  24706. + stop = ftrace_now(cpu);
  24707. + if (start) {
  24708. + long latency = ((long) (stop - start)) /
  24709. + NSECS_PER_USECS;
  24710. +
  24711. + latency_hist(PREEMPTIRQSOFF_LATENCY, cpu,
  24712. + latency, 0, stop, NULL);
  24713. + }
  24714. + per_cpu(hist_preemptirqsoff_counting, cpu) = 0;
  24715. + }
  24716. +#endif
  24717. + }
  24718. +}
  24719. +#endif
  24720. +
  24721. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  24722. +static DEFINE_RAW_SPINLOCK(wakeup_lock);
  24723. +static notrace void probe_sched_migrate_task(void *v, struct task_struct *task,
  24724. + int cpu)
  24725. +{
  24726. + int old_cpu = task_cpu(task);
  24727. +
  24728. + if (cpu != old_cpu) {
  24729. + unsigned long flags;
  24730. + struct task_struct *cpu_wakeup_task;
  24731. +
  24732. + raw_spin_lock_irqsave(&wakeup_lock, flags);
  24733. +
  24734. + cpu_wakeup_task = per_cpu(wakeup_task, old_cpu);
  24735. + if (task == cpu_wakeup_task) {
  24736. + put_task_struct(cpu_wakeup_task);
  24737. + per_cpu(wakeup_task, old_cpu) = NULL;
  24738. + cpu_wakeup_task = per_cpu(wakeup_task, cpu) = task;
  24739. + get_task_struct(cpu_wakeup_task);
  24740. + }
  24741. +
  24742. + raw_spin_unlock_irqrestore(&wakeup_lock, flags);
  24743. + }
  24744. +}
  24745. +
  24746. +static notrace void probe_wakeup_latency_hist_start(void *v,
  24747. + struct task_struct *p)
  24748. +{
  24749. + unsigned long flags;
  24750. + struct task_struct *curr = current;
  24751. + int cpu = task_cpu(p);
  24752. + struct task_struct *cpu_wakeup_task;
  24753. +
  24754. + raw_spin_lock_irqsave(&wakeup_lock, flags);
  24755. +
  24756. + cpu_wakeup_task = per_cpu(wakeup_task, cpu);
  24757. +
  24758. + if (wakeup_pid) {
  24759. + if ((cpu_wakeup_task && p->prio == cpu_wakeup_task->prio) ||
  24760. + p->prio == curr->prio)
  24761. + per_cpu(wakeup_sharedprio, cpu) = 1;
  24762. + if (likely(wakeup_pid != task_pid_nr(p)))
  24763. + goto out;
  24764. + } else {
  24765. + if (likely(!rt_task(p)) ||
  24766. + (cpu_wakeup_task && p->prio > cpu_wakeup_task->prio) ||
  24767. + p->prio > curr->prio)
  24768. + goto out;
  24769. + if ((cpu_wakeup_task && p->prio == cpu_wakeup_task->prio) ||
  24770. + p->prio == curr->prio)
  24771. + per_cpu(wakeup_sharedprio, cpu) = 1;
  24772. + }
  24773. +
  24774. + if (cpu_wakeup_task)
  24775. + put_task_struct(cpu_wakeup_task);
  24776. + cpu_wakeup_task = per_cpu(wakeup_task, cpu) = p;
  24777. + get_task_struct(cpu_wakeup_task);
  24778. + cpu_wakeup_task->preempt_timestamp_hist =
  24779. + ftrace_now(raw_smp_processor_id());
  24780. +out:
  24781. + raw_spin_unlock_irqrestore(&wakeup_lock, flags);
  24782. +}
  24783. +
  24784. +static notrace void probe_wakeup_latency_hist_stop(void *v,
  24785. + bool preempt, struct task_struct *prev, struct task_struct *next)
  24786. +{
  24787. + unsigned long flags;
  24788. + int cpu = task_cpu(next);
  24789. + long latency;
  24790. + cycle_t stop;
  24791. + struct task_struct *cpu_wakeup_task;
  24792. +
  24793. + raw_spin_lock_irqsave(&wakeup_lock, flags);
  24794. +
  24795. + cpu_wakeup_task = per_cpu(wakeup_task, cpu);
  24796. +
  24797. + if (cpu_wakeup_task == NULL)
  24798. + goto out;
  24799. +
  24800. + /* Already running? */
  24801. + if (unlikely(current == cpu_wakeup_task))
  24802. + goto out_reset;
  24803. +
  24804. + if (next != cpu_wakeup_task) {
  24805. + if (next->prio < cpu_wakeup_task->prio)
  24806. + goto out_reset;
  24807. +
  24808. + if (next->prio == cpu_wakeup_task->prio)
  24809. + per_cpu(wakeup_sharedprio, cpu) = 1;
  24810. +
  24811. + goto out;
  24812. + }
  24813. +
  24814. + if (current->prio == cpu_wakeup_task->prio)
  24815. + per_cpu(wakeup_sharedprio, cpu) = 1;
  24816. +
  24817. + /*
  24818. + * The task we are waiting for is about to be switched to.
  24819. + * Calculate latency and store it in histogram.
  24820. + */
  24821. + stop = ftrace_now(raw_smp_processor_id());
  24822. +
  24823. + latency = ((long) (stop - next->preempt_timestamp_hist)) /
  24824. + NSECS_PER_USECS;
  24825. +
  24826. + if (per_cpu(wakeup_sharedprio, cpu)) {
  24827. + latency_hist(WAKEUP_LATENCY_SHAREDPRIO, cpu, latency, 0, stop,
  24828. + next);
  24829. + per_cpu(wakeup_sharedprio, cpu) = 0;
  24830. + } else {
  24831. + latency_hist(WAKEUP_LATENCY, cpu, latency, 0, stop, next);
  24832. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  24833. + if (timerandwakeup_enabled_data.enabled) {
  24834. + latency_hist(TIMERANDWAKEUP_LATENCY, cpu,
  24835. + next->timer_offset + latency, next->timer_offset,
  24836. + stop, next);
  24837. + }
  24838. +#endif
  24839. + }
  24840. +
  24841. +out_reset:
  24842. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  24843. + next->timer_offset = 0;
  24844. +#endif
  24845. + put_task_struct(cpu_wakeup_task);
  24846. + per_cpu(wakeup_task, cpu) = NULL;
  24847. +out:
  24848. + raw_spin_unlock_irqrestore(&wakeup_lock, flags);
  24849. +}
  24850. +#endif
  24851. +
  24852. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  24853. +static notrace void probe_hrtimer_interrupt(void *v, int cpu,
  24854. + long long latency_ns, struct task_struct *curr,
  24855. + struct task_struct *task)
  24856. +{
  24857. + if (latency_ns <= 0 && task != NULL && rt_task(task) &&
  24858. + (task->prio < curr->prio ||
  24859. + (task->prio == curr->prio &&
  24860. + !cpumask_test_cpu(cpu, &task->cpus_allowed)))) {
  24861. + long latency;
  24862. + cycle_t now;
  24863. +
  24864. + if (missed_timer_offsets_pid) {
  24865. + if (likely(missed_timer_offsets_pid !=
  24866. + task_pid_nr(task)))
  24867. + return;
  24868. + }
  24869. +
  24870. + now = ftrace_now(cpu);
  24871. + latency = (long) div_s64(-latency_ns, NSECS_PER_USECS);
  24872. + latency_hist(MISSED_TIMER_OFFSETS, cpu, latency, latency, now,
  24873. + task);
  24874. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  24875. + task->timer_offset = latency;
  24876. +#endif
  24877. + }
  24878. +}
  24879. +#endif
  24880. +
  24881. +static __init int latency_hist_init(void)
  24882. +{
  24883. + struct dentry *latency_hist_root = NULL;
  24884. + struct dentry *dentry;
  24885. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  24886. + struct dentry *dentry_sharedprio;
  24887. +#endif
  24888. + struct dentry *entry;
  24889. + struct dentry *enable_root;
  24890. + int i = 0;
  24891. + struct hist_data *my_hist;
  24892. + char name[64];
  24893. + char *cpufmt = "CPU%d";
  24894. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  24895. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24896. + char *cpufmt_maxlatproc = "max_latency-CPU%d";
  24897. + struct maxlatproc_data *mp = NULL;
  24898. +#endif
  24899. +
  24900. + dentry = tracing_init_dentry();
  24901. + latency_hist_root = debugfs_create_dir(latency_hist_dir_root, dentry);
  24902. + enable_root = debugfs_create_dir("enable", latency_hist_root);
  24903. +
  24904. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  24905. + dentry = debugfs_create_dir(irqsoff_hist_dir, latency_hist_root);
  24906. + for_each_possible_cpu(i) {
  24907. + sprintf(name, cpufmt, i);
  24908. + entry = debugfs_create_file(name, 0444, dentry,
  24909. + &per_cpu(irqsoff_hist, i), &latency_hist_fops);
  24910. + my_hist = &per_cpu(irqsoff_hist, i);
  24911. + atomic_set(&my_hist->hist_mode, 1);
  24912. + my_hist->min_lat = LONG_MAX;
  24913. + }
  24914. + entry = debugfs_create_file("reset", 0644, dentry,
  24915. + (void *)IRQSOFF_LATENCY, &latency_hist_reset_fops);
  24916. +#endif
  24917. +
  24918. +#ifdef CONFIG_PREEMPT_OFF_HIST
  24919. + dentry = debugfs_create_dir(preemptoff_hist_dir,
  24920. + latency_hist_root);
  24921. + for_each_possible_cpu(i) {
  24922. + sprintf(name, cpufmt, i);
  24923. + entry = debugfs_create_file(name, 0444, dentry,
  24924. + &per_cpu(preemptoff_hist, i), &latency_hist_fops);
  24925. + my_hist = &per_cpu(preemptoff_hist, i);
  24926. + atomic_set(&my_hist->hist_mode, 1);
  24927. + my_hist->min_lat = LONG_MAX;
  24928. + }
  24929. + entry = debugfs_create_file("reset", 0644, dentry,
  24930. + (void *)PREEMPTOFF_LATENCY, &latency_hist_reset_fops);
  24931. +#endif
  24932. +
  24933. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  24934. + dentry = debugfs_create_dir(preemptirqsoff_hist_dir,
  24935. + latency_hist_root);
  24936. + for_each_possible_cpu(i) {
  24937. + sprintf(name, cpufmt, i);
  24938. + entry = debugfs_create_file(name, 0444, dentry,
  24939. + &per_cpu(preemptirqsoff_hist, i), &latency_hist_fops);
  24940. + my_hist = &per_cpu(preemptirqsoff_hist, i);
  24941. + atomic_set(&my_hist->hist_mode, 1);
  24942. + my_hist->min_lat = LONG_MAX;
  24943. + }
  24944. + entry = debugfs_create_file("reset", 0644, dentry,
  24945. + (void *)PREEMPTIRQSOFF_LATENCY, &latency_hist_reset_fops);
  24946. +#endif
  24947. +
  24948. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  24949. + entry = debugfs_create_file("preemptirqsoff", 0644,
  24950. + enable_root, (void *)&preemptirqsoff_enabled_data,
  24951. + &enable_fops);
  24952. +#endif
  24953. +
  24954. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  24955. + dentry = debugfs_create_dir(wakeup_latency_hist_dir,
  24956. + latency_hist_root);
  24957. + dentry_sharedprio = debugfs_create_dir(
  24958. + wakeup_latency_hist_dir_sharedprio, dentry);
  24959. + for_each_possible_cpu(i) {
  24960. + sprintf(name, cpufmt, i);
  24961. +
  24962. + entry = debugfs_create_file(name, 0444, dentry,
  24963. + &per_cpu(wakeup_latency_hist, i),
  24964. + &latency_hist_fops);
  24965. + my_hist = &per_cpu(wakeup_latency_hist, i);
  24966. + atomic_set(&my_hist->hist_mode, 1);
  24967. + my_hist->min_lat = LONG_MAX;
  24968. +
  24969. + entry = debugfs_create_file(name, 0444, dentry_sharedprio,
  24970. + &per_cpu(wakeup_latency_hist_sharedprio, i),
  24971. + &latency_hist_fops);
  24972. + my_hist = &per_cpu(wakeup_latency_hist_sharedprio, i);
  24973. + atomic_set(&my_hist->hist_mode, 1);
  24974. + my_hist->min_lat = LONG_MAX;
  24975. +
  24976. + sprintf(name, cpufmt_maxlatproc, i);
  24977. +
  24978. + mp = &per_cpu(wakeup_maxlatproc, i);
  24979. + entry = debugfs_create_file(name, 0444, dentry, mp,
  24980. + &maxlatproc_fops);
  24981. + clear_maxlatprocdata(mp);
  24982. +
  24983. + mp = &per_cpu(wakeup_maxlatproc_sharedprio, i);
  24984. + entry = debugfs_create_file(name, 0444, dentry_sharedprio, mp,
  24985. + &maxlatproc_fops);
  24986. + clear_maxlatprocdata(mp);
  24987. + }
  24988. + entry = debugfs_create_file("pid", 0644, dentry,
  24989. + (void *)&wakeup_pid, &pid_fops);
  24990. + entry = debugfs_create_file("reset", 0644, dentry,
  24991. + (void *)WAKEUP_LATENCY, &latency_hist_reset_fops);
  24992. + entry = debugfs_create_file("reset", 0644, dentry_sharedprio,
  24993. + (void *)WAKEUP_LATENCY_SHAREDPRIO, &latency_hist_reset_fops);
  24994. + entry = debugfs_create_file("wakeup", 0644,
  24995. + enable_root, (void *)&wakeup_latency_enabled_data,
  24996. + &enable_fops);
  24997. +#endif
  24998. +
  24999. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  25000. + dentry = debugfs_create_dir(missed_timer_offsets_dir,
  25001. + latency_hist_root);
  25002. + for_each_possible_cpu(i) {
  25003. + sprintf(name, cpufmt, i);
  25004. + entry = debugfs_create_file(name, 0444, dentry,
  25005. + &per_cpu(missed_timer_offsets, i), &latency_hist_fops);
  25006. + my_hist = &per_cpu(missed_timer_offsets, i);
  25007. + atomic_set(&my_hist->hist_mode, 1);
  25008. + my_hist->min_lat = LONG_MAX;
  25009. +
  25010. + sprintf(name, cpufmt_maxlatproc, i);
  25011. + mp = &per_cpu(missed_timer_offsets_maxlatproc, i);
  25012. + entry = debugfs_create_file(name, 0444, dentry, mp,
  25013. + &maxlatproc_fops);
  25014. + clear_maxlatprocdata(mp);
  25015. + }
  25016. + entry = debugfs_create_file("pid", 0644, dentry,
  25017. + (void *)&missed_timer_offsets_pid, &pid_fops);
  25018. + entry = debugfs_create_file("reset", 0644, dentry,
  25019. + (void *)MISSED_TIMER_OFFSETS, &latency_hist_reset_fops);
  25020. + entry = debugfs_create_file("missed_timer_offsets", 0644,
  25021. + enable_root, (void *)&missed_timer_offsets_enabled_data,
  25022. + &enable_fops);
  25023. +#endif
  25024. +
  25025. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  25026. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  25027. + dentry = debugfs_create_dir(timerandwakeup_latency_hist_dir,
  25028. + latency_hist_root);
  25029. + for_each_possible_cpu(i) {
  25030. + sprintf(name, cpufmt, i);
  25031. + entry = debugfs_create_file(name, 0444, dentry,
  25032. + &per_cpu(timerandwakeup_latency_hist, i),
  25033. + &latency_hist_fops);
  25034. + my_hist = &per_cpu(timerandwakeup_latency_hist, i);
  25035. + atomic_set(&my_hist->hist_mode, 1);
  25036. + my_hist->min_lat = LONG_MAX;
  25037. +
  25038. + sprintf(name, cpufmt_maxlatproc, i);
  25039. + mp = &per_cpu(timerandwakeup_maxlatproc, i);
  25040. + entry = debugfs_create_file(name, 0444, dentry, mp,
  25041. + &maxlatproc_fops);
  25042. + clear_maxlatprocdata(mp);
  25043. + }
  25044. + entry = debugfs_create_file("reset", 0644, dentry,
  25045. + (void *)TIMERANDWAKEUP_LATENCY, &latency_hist_reset_fops);
  25046. + entry = debugfs_create_file("timerandwakeup", 0644,
  25047. + enable_root, (void *)&timerandwakeup_enabled_data,
  25048. + &enable_fops);
  25049. +#endif
  25050. + return 0;
  25051. +}
  25052. +
  25053. +device_initcall(latency_hist_init);
  25054. diff -Nur linux-4.4.46.orig/kernel/trace/Makefile linux-4.4.46/kernel/trace/Makefile
  25055. --- linux-4.4.46.orig/kernel/trace/Makefile 2017-02-01 08:31:11.000000000 +0100
  25056. +++ linux-4.4.46/kernel/trace/Makefile 2017-02-03 17:18:10.939619522 +0100
  25057. @@ -40,6 +40,10 @@
  25058. obj-$(CONFIG_IRQSOFF_TRACER) += trace_irqsoff.o
  25059. obj-$(CONFIG_PREEMPT_TRACER) += trace_irqsoff.o
  25060. obj-$(CONFIG_SCHED_TRACER) += trace_sched_wakeup.o
  25061. +obj-$(CONFIG_INTERRUPT_OFF_HIST) += latency_hist.o
  25062. +obj-$(CONFIG_PREEMPT_OFF_HIST) += latency_hist.o
  25063. +obj-$(CONFIG_WAKEUP_LATENCY_HIST) += latency_hist.o
  25064. +obj-$(CONFIG_MISSED_TIMER_OFFSETS_HIST) += latency_hist.o
  25065. obj-$(CONFIG_NOP_TRACER) += trace_nop.o
  25066. obj-$(CONFIG_STACK_TRACER) += trace_stack.o
  25067. obj-$(CONFIG_MMIOTRACE) += trace_mmiotrace.o
  25068. diff -Nur linux-4.4.46.orig/kernel/trace/trace.c linux-4.4.46/kernel/trace/trace.c
  25069. --- linux-4.4.46.orig/kernel/trace/trace.c 2017-02-01 08:31:11.000000000 +0100
  25070. +++ linux-4.4.46/kernel/trace/trace.c 2017-02-03 17:18:10.943619676 +0100
  25071. @@ -1652,6 +1652,7 @@
  25072. struct task_struct *tsk = current;
  25073. entry->preempt_count = pc & 0xff;
  25074. + entry->preempt_lazy_count = preempt_lazy_count();
  25075. entry->pid = (tsk) ? tsk->pid : 0;
  25076. entry->flags =
  25077. #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
  25078. @@ -1661,8 +1662,11 @@
  25079. #endif
  25080. ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
  25081. ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
  25082. - (tif_need_resched() ? TRACE_FLAG_NEED_RESCHED : 0) |
  25083. + (tif_need_resched_now() ? TRACE_FLAG_NEED_RESCHED : 0) |
  25084. + (need_resched_lazy() ? TRACE_FLAG_NEED_RESCHED_LAZY : 0) |
  25085. (test_preempt_need_resched() ? TRACE_FLAG_PREEMPT_RESCHED : 0);
  25086. +
  25087. + entry->migrate_disable = (tsk) ? __migrate_disabled(tsk) & 0xFF : 0;
  25088. }
  25089. EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
  25090. @@ -2555,14 +2559,17 @@
  25091. static void print_lat_help_header(struct seq_file *m)
  25092. {
  25093. - seq_puts(m, "# _------=> CPU# \n"
  25094. - "# / _-----=> irqs-off \n"
  25095. - "# | / _----=> need-resched \n"
  25096. - "# || / _---=> hardirq/softirq \n"
  25097. - "# ||| / _--=> preempt-depth \n"
  25098. - "# |||| / delay \n"
  25099. - "# cmd pid ||||| time | caller \n"
  25100. - "# \\ / ||||| \\ | / \n");
  25101. + seq_puts(m, "# _--------=> CPU# \n"
  25102. + "# / _-------=> irqs-off \n"
  25103. + "# | / _------=> need-resched \n"
  25104. + "# || / _-----=> need-resched_lazy \n"
  25105. + "# ||| / _----=> hardirq/softirq \n"
  25106. + "# |||| / _---=> preempt-depth \n"
  25107. + "# ||||| / _--=> preempt-lazy-depth\n"
  25108. + "# |||||| / _-=> migrate-disable \n"
  25109. + "# ||||||| / delay \n"
  25110. + "# cmd pid |||||||| time | caller \n"
  25111. + "# \\ / |||||||| \\ | / \n");
  25112. }
  25113. static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
  25114. @@ -2588,11 +2595,14 @@
  25115. print_event_info(buf, m);
  25116. seq_puts(m, "# _-----=> irqs-off\n"
  25117. "# / _----=> need-resched\n"
  25118. - "# | / _---=> hardirq/softirq\n"
  25119. - "# || / _--=> preempt-depth\n"
  25120. - "# ||| / delay\n"
  25121. - "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n"
  25122. - "# | | | |||| | |\n");
  25123. + "# |/ _-----=> need-resched_lazy\n"
  25124. + "# || / _---=> hardirq/softirq\n"
  25125. + "# ||| / _--=> preempt-depth\n"
  25126. + "# |||| / _-=> preempt-lazy-depth\n"
  25127. + "# ||||| / _-=> migrate-disable \n"
  25128. + "# |||||| / delay\n"
  25129. + "# TASK-PID CPU# ||||||| TIMESTAMP FUNCTION\n"
  25130. + "# | | | ||||||| | |\n");
  25131. }
  25132. void
  25133. diff -Nur linux-4.4.46.orig/kernel/trace/trace_events.c linux-4.4.46/kernel/trace/trace_events.c
  25134. --- linux-4.4.46.orig/kernel/trace/trace_events.c 2017-02-01 08:31:11.000000000 +0100
  25135. +++ linux-4.4.46/kernel/trace/trace_events.c 2017-02-03 17:18:10.943619676 +0100
  25136. @@ -188,6 +188,8 @@
  25137. __common_field(unsigned char, flags);
  25138. __common_field(unsigned char, preempt_count);
  25139. __common_field(int, pid);
  25140. + __common_field(unsigned short, migrate_disable);
  25141. + __common_field(unsigned short, padding);
  25142. return ret;
  25143. }
  25144. @@ -244,6 +246,14 @@
  25145. local_save_flags(fbuffer->flags);
  25146. fbuffer->pc = preempt_count();
  25147. + /*
  25148. + * If CONFIG_PREEMPT is enabled, then the tracepoint itself disables
  25149. + * preemption (adding one to the preempt_count). Since we are
  25150. + * interested in the preempt_count at the time the tracepoint was
  25151. + * hit, we need to subtract one to offset the increment.
  25152. + */
  25153. + if (IS_ENABLED(CONFIG_PREEMPT))
  25154. + fbuffer->pc--;
  25155. fbuffer->trace_file = trace_file;
  25156. fbuffer->event =
  25157. diff -Nur linux-4.4.46.orig/kernel/trace/trace.h linux-4.4.46/kernel/trace/trace.h
  25158. --- linux-4.4.46.orig/kernel/trace/trace.h 2017-02-01 08:31:11.000000000 +0100
  25159. +++ linux-4.4.46/kernel/trace/trace.h 2017-02-03 17:18:10.943619676 +0100
  25160. @@ -117,6 +117,7 @@
  25161. * NEED_RESCHED - reschedule is requested
  25162. * HARDIRQ - inside an interrupt handler
  25163. * SOFTIRQ - inside a softirq handler
  25164. + * NEED_RESCHED_LAZY - lazy reschedule is requested
  25165. */
  25166. enum trace_flag_type {
  25167. TRACE_FLAG_IRQS_OFF = 0x01,
  25168. @@ -125,6 +126,7 @@
  25169. TRACE_FLAG_HARDIRQ = 0x08,
  25170. TRACE_FLAG_SOFTIRQ = 0x10,
  25171. TRACE_FLAG_PREEMPT_RESCHED = 0x20,
  25172. + TRACE_FLAG_NEED_RESCHED_LAZY = 0x40,
  25173. };
  25174. #define TRACE_BUF_SIZE 1024
  25175. diff -Nur linux-4.4.46.orig/kernel/trace/trace_irqsoff.c linux-4.4.46/kernel/trace/trace_irqsoff.c
  25176. --- linux-4.4.46.orig/kernel/trace/trace_irqsoff.c 2017-02-01 08:31:11.000000000 +0100
  25177. +++ linux-4.4.46/kernel/trace/trace_irqsoff.c 2017-02-03 17:18:10.943619676 +0100
  25178. @@ -13,6 +13,7 @@
  25179. #include <linux/uaccess.h>
  25180. #include <linux/module.h>
  25181. #include <linux/ftrace.h>
  25182. +#include <trace/events/hist.h>
  25183. #include "trace.h"
  25184. @@ -424,11 +425,13 @@
  25185. {
  25186. if (preempt_trace() || irq_trace())
  25187. start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  25188. + trace_preemptirqsoff_hist_rcuidle(TRACE_START, 1);
  25189. }
  25190. EXPORT_SYMBOL_GPL(start_critical_timings);
  25191. void stop_critical_timings(void)
  25192. {
  25193. + trace_preemptirqsoff_hist_rcuidle(TRACE_STOP, 0);
  25194. if (preempt_trace() || irq_trace())
  25195. stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  25196. }
  25197. @@ -438,6 +441,7 @@
  25198. #ifdef CONFIG_PROVE_LOCKING
  25199. void time_hardirqs_on(unsigned long a0, unsigned long a1)
  25200. {
  25201. + trace_preemptirqsoff_hist_rcuidle(IRQS_ON, 0);
  25202. if (!preempt_trace() && irq_trace())
  25203. stop_critical_timing(a0, a1);
  25204. }
  25205. @@ -446,6 +450,7 @@
  25206. {
  25207. if (!preempt_trace() && irq_trace())
  25208. start_critical_timing(a0, a1);
  25209. + trace_preemptirqsoff_hist_rcuidle(IRQS_OFF, 1);
  25210. }
  25211. #else /* !CONFIG_PROVE_LOCKING */
  25212. @@ -471,6 +476,7 @@
  25213. */
  25214. void trace_hardirqs_on(void)
  25215. {
  25216. + trace_preemptirqsoff_hist(IRQS_ON, 0);
  25217. if (!preempt_trace() && irq_trace())
  25218. stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  25219. }
  25220. @@ -480,11 +486,13 @@
  25221. {
  25222. if (!preempt_trace() && irq_trace())
  25223. start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  25224. + trace_preemptirqsoff_hist(IRQS_OFF, 1);
  25225. }
  25226. EXPORT_SYMBOL(trace_hardirqs_off);
  25227. __visible void trace_hardirqs_on_caller(unsigned long caller_addr)
  25228. {
  25229. + trace_preemptirqsoff_hist(IRQS_ON, 0);
  25230. if (!preempt_trace() && irq_trace())
  25231. stop_critical_timing(CALLER_ADDR0, caller_addr);
  25232. }
  25233. @@ -494,6 +502,7 @@
  25234. {
  25235. if (!preempt_trace() && irq_trace())
  25236. start_critical_timing(CALLER_ADDR0, caller_addr);
  25237. + trace_preemptirqsoff_hist(IRQS_OFF, 1);
  25238. }
  25239. EXPORT_SYMBOL(trace_hardirqs_off_caller);
  25240. @@ -503,12 +512,14 @@
  25241. #ifdef CONFIG_PREEMPT_TRACER
  25242. void trace_preempt_on(unsigned long a0, unsigned long a1)
  25243. {
  25244. + trace_preemptirqsoff_hist(PREEMPT_ON, 0);
  25245. if (preempt_trace() && !irq_trace())
  25246. stop_critical_timing(a0, a1);
  25247. }
  25248. void trace_preempt_off(unsigned long a0, unsigned long a1)
  25249. {
  25250. + trace_preemptirqsoff_hist(PREEMPT_ON, 1);
  25251. if (preempt_trace() && !irq_trace())
  25252. start_critical_timing(a0, a1);
  25253. }
  25254. diff -Nur linux-4.4.46.orig/kernel/trace/trace_output.c linux-4.4.46/kernel/trace/trace_output.c
  25255. --- linux-4.4.46.orig/kernel/trace/trace_output.c 2017-02-01 08:31:11.000000000 +0100
  25256. +++ linux-4.4.46/kernel/trace/trace_output.c 2017-02-03 17:18:10.943619676 +0100
  25257. @@ -386,6 +386,7 @@
  25258. {
  25259. char hardsoft_irq;
  25260. char need_resched;
  25261. + char need_resched_lazy;
  25262. char irqs_off;
  25263. int hardirq;
  25264. int softirq;
  25265. @@ -413,6 +414,8 @@
  25266. need_resched = '.';
  25267. break;
  25268. }
  25269. + need_resched_lazy =
  25270. + (entry->flags & TRACE_FLAG_NEED_RESCHED_LAZY) ? 'L' : '.';
  25271. hardsoft_irq =
  25272. (hardirq && softirq) ? 'H' :
  25273. @@ -420,14 +423,25 @@
  25274. softirq ? 's' :
  25275. '.';
  25276. - trace_seq_printf(s, "%c%c%c",
  25277. - irqs_off, need_resched, hardsoft_irq);
  25278. + trace_seq_printf(s, "%c%c%c%c",
  25279. + irqs_off, need_resched, need_resched_lazy,
  25280. + hardsoft_irq);
  25281. if (entry->preempt_count)
  25282. trace_seq_printf(s, "%x", entry->preempt_count);
  25283. else
  25284. trace_seq_putc(s, '.');
  25285. + if (entry->preempt_lazy_count)
  25286. + trace_seq_printf(s, "%x", entry->preempt_lazy_count);
  25287. + else
  25288. + trace_seq_putc(s, '.');
  25289. +
  25290. + if (entry->migrate_disable)
  25291. + trace_seq_printf(s, "%x", entry->migrate_disable);
  25292. + else
  25293. + trace_seq_putc(s, '.');
  25294. +
  25295. return !trace_seq_has_overflowed(s);
  25296. }
  25297. diff -Nur linux-4.4.46.orig/kernel/user.c linux-4.4.46/kernel/user.c
  25298. --- linux-4.4.46.orig/kernel/user.c 2017-02-01 08:31:11.000000000 +0100
  25299. +++ linux-4.4.46/kernel/user.c 2017-02-03 17:18:10.943619676 +0100
  25300. @@ -161,11 +161,11 @@
  25301. if (!up)
  25302. return;
  25303. - local_irq_save(flags);
  25304. + local_irq_save_nort(flags);
  25305. if (atomic_dec_and_lock(&up->__count, &uidhash_lock))
  25306. free_user(up, flags);
  25307. else
  25308. - local_irq_restore(flags);
  25309. + local_irq_restore_nort(flags);
  25310. }
  25311. struct user_struct *alloc_uid(kuid_t uid)
  25312. diff -Nur linux-4.4.46.orig/kernel/watchdog.c linux-4.4.46/kernel/watchdog.c
  25313. --- linux-4.4.46.orig/kernel/watchdog.c 2017-02-01 08:31:11.000000000 +0100
  25314. +++ linux-4.4.46/kernel/watchdog.c 2017-02-03 17:18:10.943619676 +0100
  25315. @@ -299,6 +299,8 @@
  25316. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  25317. +static DEFINE_RAW_SPINLOCK(watchdog_output_lock);
  25318. +
  25319. static struct perf_event_attr wd_hw_attr = {
  25320. .type = PERF_TYPE_HARDWARE,
  25321. .config = PERF_COUNT_HW_CPU_CYCLES,
  25322. @@ -332,6 +334,13 @@
  25323. /* only print hardlockups once */
  25324. if (__this_cpu_read(hard_watchdog_warn) == true)
  25325. return;
  25326. + /*
  25327. + * If early-printk is enabled then make sure we do not
  25328. + * lock up in printk() and kill console logging:
  25329. + */
  25330. + printk_kill();
  25331. +
  25332. + raw_spin_lock(&watchdog_output_lock);
  25333. pr_emerg("Watchdog detected hard LOCKUP on cpu %d", this_cpu);
  25334. print_modules();
  25335. @@ -349,8 +358,9 @@
  25336. !test_and_set_bit(0, &hardlockup_allcpu_dumped))
  25337. trigger_allbutself_cpu_backtrace();
  25338. + raw_spin_unlock(&watchdog_output_lock);
  25339. if (hardlockup_panic)
  25340. - panic("Hard LOCKUP");
  25341. + nmi_panic(regs, "Hard LOCKUP");
  25342. __this_cpu_write(hard_watchdog_warn, true);
  25343. return;
  25344. @@ -496,6 +506,7 @@
  25345. /* kick off the timer for the hardlockup detector */
  25346. hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  25347. hrtimer->function = watchdog_timer_fn;
  25348. + hrtimer->irqsafe = 1;
  25349. /* Enable the perf event */
  25350. watchdog_nmi_enable(cpu);
  25351. diff -Nur linux-4.4.46.orig/kernel/workqueue.c linux-4.4.46/kernel/workqueue.c
  25352. --- linux-4.4.46.orig/kernel/workqueue.c 2017-02-01 08:31:11.000000000 +0100
  25353. +++ linux-4.4.46/kernel/workqueue.c 2017-02-03 17:18:10.943619676 +0100
  25354. @@ -48,6 +48,8 @@
  25355. #include <linux/nodemask.h>
  25356. #include <linux/moduleparam.h>
  25357. #include <linux/uaccess.h>
  25358. +#include <linux/locallock.h>
  25359. +#include <linux/delay.h>
  25360. #include "workqueue_internal.h"
  25361. @@ -121,11 +123,16 @@
  25362. * cpu or grabbing pool->lock is enough for read access. If
  25363. * POOL_DISASSOCIATED is set, it's identical to L.
  25364. *
  25365. + * On RT we need the extra protection via rt_lock_idle_list() for
  25366. + * the list manipulations against read access from
  25367. + * wq_worker_sleeping(). All other places are nicely serialized via
  25368. + * pool->lock.
  25369. + *
  25370. * A: pool->attach_mutex protected.
  25371. *
  25372. * PL: wq_pool_mutex protected.
  25373. *
  25374. - * PR: wq_pool_mutex protected for writes. Sched-RCU protected for reads.
  25375. + * PR: wq_pool_mutex protected for writes. RCU protected for reads.
  25376. *
  25377. * PW: wq_pool_mutex and wq->mutex protected for writes. Either for reads.
  25378. *
  25379. @@ -134,7 +141,7 @@
  25380. *
  25381. * WQ: wq->mutex protected.
  25382. *
  25383. - * WR: wq->mutex protected for writes. Sched-RCU protected for reads.
  25384. + * WR: wq->mutex protected for writes. RCU protected for reads.
  25385. *
  25386. * MD: wq_mayday_lock protected.
  25387. */
  25388. @@ -183,7 +190,7 @@
  25389. atomic_t nr_running ____cacheline_aligned_in_smp;
  25390. /*
  25391. - * Destruction of pool is sched-RCU protected to allow dereferences
  25392. + * Destruction of pool is RCU protected to allow dereferences
  25393. * from get_work_pool().
  25394. */
  25395. struct rcu_head rcu;
  25396. @@ -212,7 +219,7 @@
  25397. /*
  25398. * Release of unbound pwq is punted to system_wq. See put_pwq()
  25399. * and pwq_unbound_release_workfn() for details. pool_workqueue
  25400. - * itself is also sched-RCU protected so that the first pwq can be
  25401. + * itself is also RCU protected so that the first pwq can be
  25402. * determined without grabbing wq->mutex.
  25403. */
  25404. struct work_struct unbound_release_work;
  25405. @@ -331,6 +338,8 @@
  25406. struct workqueue_struct *system_freezable_power_efficient_wq __read_mostly;
  25407. EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq);
  25408. +static DEFINE_LOCAL_IRQ_LOCK(pendingb_lock);
  25409. +
  25410. static int worker_thread(void *__worker);
  25411. static void workqueue_sysfs_unregister(struct workqueue_struct *wq);
  25412. @@ -338,20 +347,20 @@
  25413. #include <trace/events/workqueue.h>
  25414. #define assert_rcu_or_pool_mutex() \
  25415. - RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held() && \
  25416. + RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
  25417. !lockdep_is_held(&wq_pool_mutex), \
  25418. - "sched RCU or wq_pool_mutex should be held")
  25419. + "RCU or wq_pool_mutex should be held")
  25420. #define assert_rcu_or_wq_mutex(wq) \
  25421. - RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held() && \
  25422. + RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
  25423. !lockdep_is_held(&wq->mutex), \
  25424. - "sched RCU or wq->mutex should be held")
  25425. + "RCU or wq->mutex should be held")
  25426. #define assert_rcu_or_wq_mutex_or_pool_mutex(wq) \
  25427. - RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held() && \
  25428. + RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
  25429. !lockdep_is_held(&wq->mutex) && \
  25430. !lockdep_is_held(&wq_pool_mutex), \
  25431. - "sched RCU, wq->mutex or wq_pool_mutex should be held")
  25432. + "RCU, wq->mutex or wq_pool_mutex should be held")
  25433. #define for_each_cpu_worker_pool(pool, cpu) \
  25434. for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \
  25435. @@ -363,7 +372,7 @@
  25436. * @pool: iteration cursor
  25437. * @pi: integer used for iteration
  25438. *
  25439. - * This must be called either with wq_pool_mutex held or sched RCU read
  25440. + * This must be called either with wq_pool_mutex held or RCU read
  25441. * locked. If the pool needs to be used beyond the locking in effect, the
  25442. * caller is responsible for guaranteeing that the pool stays online.
  25443. *
  25444. @@ -395,7 +404,7 @@
  25445. * @pwq: iteration cursor
  25446. * @wq: the target workqueue
  25447. *
  25448. - * This must be called either with wq->mutex held or sched RCU read locked.
  25449. + * This must be called either with wq->mutex held or RCU read locked.
  25450. * If the pwq needs to be used beyond the locking in effect, the caller is
  25451. * responsible for guaranteeing that the pwq stays online.
  25452. *
  25453. @@ -407,6 +416,31 @@
  25454. if (({ assert_rcu_or_wq_mutex(wq); false; })) { } \
  25455. else
  25456. +#ifdef CONFIG_PREEMPT_RT_BASE
  25457. +static inline void rt_lock_idle_list(struct worker_pool *pool)
  25458. +{
  25459. + preempt_disable();
  25460. +}
  25461. +static inline void rt_unlock_idle_list(struct worker_pool *pool)
  25462. +{
  25463. + preempt_enable();
  25464. +}
  25465. +static inline void sched_lock_idle_list(struct worker_pool *pool) { }
  25466. +static inline void sched_unlock_idle_list(struct worker_pool *pool) { }
  25467. +#else
  25468. +static inline void rt_lock_idle_list(struct worker_pool *pool) { }
  25469. +static inline void rt_unlock_idle_list(struct worker_pool *pool) { }
  25470. +static inline void sched_lock_idle_list(struct worker_pool *pool)
  25471. +{
  25472. + spin_lock_irq(&pool->lock);
  25473. +}
  25474. +static inline void sched_unlock_idle_list(struct worker_pool *pool)
  25475. +{
  25476. + spin_unlock_irq(&pool->lock);
  25477. +}
  25478. +#endif
  25479. +
  25480. +
  25481. #ifdef CONFIG_DEBUG_OBJECTS_WORK
  25482. static struct debug_obj_descr work_debug_descr;
  25483. @@ -557,7 +591,7 @@
  25484. * @wq: the target workqueue
  25485. * @node: the node ID
  25486. *
  25487. - * This must be called with any of wq_pool_mutex, wq->mutex or sched RCU
  25488. + * This must be called with any of wq_pool_mutex, wq->mutex or RCU
  25489. * read locked.
  25490. * If the pwq needs to be used beyond the locking in effect, the caller is
  25491. * responsible for guaranteeing that the pwq stays online.
  25492. @@ -701,8 +735,8 @@
  25493. * @work: the work item of interest
  25494. *
  25495. * Pools are created and destroyed under wq_pool_mutex, and allows read
  25496. - * access under sched-RCU read lock. As such, this function should be
  25497. - * called under wq_pool_mutex or with preemption disabled.
  25498. + * access under RCU read lock. As such, this function should be
  25499. + * called under wq_pool_mutex or inside of a rcu_read_lock() region.
  25500. *
  25501. * All fields of the returned pool are accessible as long as the above
  25502. * mentioned locking is in effect. If the returned pool needs to be used
  25503. @@ -839,51 +873,44 @@
  25504. */
  25505. static void wake_up_worker(struct worker_pool *pool)
  25506. {
  25507. - struct worker *worker = first_idle_worker(pool);
  25508. + struct worker *worker;
  25509. +
  25510. + rt_lock_idle_list(pool);
  25511. +
  25512. + worker = first_idle_worker(pool);
  25513. if (likely(worker))
  25514. wake_up_process(worker->task);
  25515. +
  25516. + rt_unlock_idle_list(pool);
  25517. }
  25518. /**
  25519. - * wq_worker_waking_up - a worker is waking up
  25520. - * @task: task waking up
  25521. - * @cpu: CPU @task is waking up to
  25522. + * wq_worker_running - a worker is running again
  25523. + * @task: task returning from sleep
  25524. *
  25525. - * This function is called during try_to_wake_up() when a worker is
  25526. - * being awoken.
  25527. - *
  25528. - * CONTEXT:
  25529. - * spin_lock_irq(rq->lock)
  25530. + * This function is called when a worker returns from schedule()
  25531. */
  25532. -void wq_worker_waking_up(struct task_struct *task, int cpu)
  25533. +void wq_worker_running(struct task_struct *task)
  25534. {
  25535. struct worker *worker = kthread_data(task);
  25536. - if (!(worker->flags & WORKER_NOT_RUNNING)) {
  25537. - WARN_ON_ONCE(worker->pool->cpu != cpu);
  25538. + if (!worker->sleeping)
  25539. + return;
  25540. + if (!(worker->flags & WORKER_NOT_RUNNING))
  25541. atomic_inc(&worker->pool->nr_running);
  25542. - }
  25543. + worker->sleeping = 0;
  25544. }
  25545. /**
  25546. * wq_worker_sleeping - a worker is going to sleep
  25547. * @task: task going to sleep
  25548. - * @cpu: CPU in question, must be the current CPU number
  25549. - *
  25550. - * This function is called during schedule() when a busy worker is
  25551. - * going to sleep. Worker on the same cpu can be woken up by
  25552. - * returning pointer to its task.
  25553. - *
  25554. - * CONTEXT:
  25555. - * spin_lock_irq(rq->lock)
  25556. - *
  25557. - * Return:
  25558. - * Worker task on @cpu to wake up, %NULL if none.
  25559. + * This function is called from schedule() when a busy worker is
  25560. + * going to sleep.
  25561. */
  25562. -struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
  25563. +void wq_worker_sleeping(struct task_struct *task)
  25564. {
  25565. - struct worker *worker = kthread_data(task), *to_wakeup = NULL;
  25566. + struct worker *worker = kthread_data(task);
  25567. struct worker_pool *pool;
  25568. /*
  25569. @@ -892,29 +919,26 @@
  25570. * checking NOT_RUNNING.
  25571. */
  25572. if (worker->flags & WORKER_NOT_RUNNING)
  25573. - return NULL;
  25574. + return;
  25575. pool = worker->pool;
  25576. - /* this can only happen on the local cpu */
  25577. - if (WARN_ON_ONCE(cpu != raw_smp_processor_id() || pool->cpu != cpu))
  25578. - return NULL;
  25579. + if (WARN_ON_ONCE(worker->sleeping))
  25580. + return;
  25581. +
  25582. + worker->sleeping = 1;
  25583. /*
  25584. * The counterpart of the following dec_and_test, implied mb,
  25585. * worklist not empty test sequence is in insert_work().
  25586. * Please read comment there.
  25587. - *
  25588. - * NOT_RUNNING is clear. This means that we're bound to and
  25589. - * running on the local cpu w/ rq lock held and preemption
  25590. - * disabled, which in turn means that none else could be
  25591. - * manipulating idle_list, so dereferencing idle_list without pool
  25592. - * lock is safe.
  25593. */
  25594. if (atomic_dec_and_test(&pool->nr_running) &&
  25595. - !list_empty(&pool->worklist))
  25596. - to_wakeup = first_idle_worker(pool);
  25597. - return to_wakeup ? to_wakeup->task : NULL;
  25598. + !list_empty(&pool->worklist)) {
  25599. + sched_lock_idle_list(pool);
  25600. + wake_up_worker(pool);
  25601. + sched_unlock_idle_list(pool);
  25602. + }
  25603. }
  25604. /**
  25605. @@ -1108,12 +1132,12 @@
  25606. {
  25607. if (pwq) {
  25608. /*
  25609. - * As both pwqs and pools are sched-RCU protected, the
  25610. + * As both pwqs and pools are RCU protected, the
  25611. * following lock operations are safe.
  25612. */
  25613. - spin_lock_irq(&pwq->pool->lock);
  25614. + local_spin_lock_irq(pendingb_lock, &pwq->pool->lock);
  25615. put_pwq(pwq);
  25616. - spin_unlock_irq(&pwq->pool->lock);
  25617. + local_spin_unlock_irq(pendingb_lock, &pwq->pool->lock);
  25618. }
  25619. }
  25620. @@ -1215,7 +1239,7 @@
  25621. struct worker_pool *pool;
  25622. struct pool_workqueue *pwq;
  25623. - local_irq_save(*flags);
  25624. + local_lock_irqsave(pendingb_lock, *flags);
  25625. /* try to steal the timer if it exists */
  25626. if (is_dwork) {
  25627. @@ -1234,6 +1258,7 @@
  25628. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
  25629. return 0;
  25630. + rcu_read_lock();
  25631. /*
  25632. * The queueing is in progress, or it is already queued. Try to
  25633. * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
  25634. @@ -1272,14 +1297,16 @@
  25635. set_work_pool_and_keep_pending(work, pool->id);
  25636. spin_unlock(&pool->lock);
  25637. + rcu_read_unlock();
  25638. return 1;
  25639. }
  25640. spin_unlock(&pool->lock);
  25641. fail:
  25642. - local_irq_restore(*flags);
  25643. + rcu_read_unlock();
  25644. + local_unlock_irqrestore(pendingb_lock, *flags);
  25645. if (work_is_canceling(work))
  25646. return -ENOENT;
  25647. - cpu_relax();
  25648. + cpu_chill();
  25649. return -EAGAIN;
  25650. }
  25651. @@ -1348,7 +1375,7 @@
  25652. * queued or lose PENDING. Grabbing PENDING and queueing should
  25653. * happen with IRQ disabled.
  25654. */
  25655. - WARN_ON_ONCE(!irqs_disabled());
  25656. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  25657. debug_work_activate(work);
  25658. @@ -1356,6 +1383,8 @@
  25659. if (unlikely(wq->flags & __WQ_DRAINING) &&
  25660. WARN_ON_ONCE(!is_chained_work(wq)))
  25661. return;
  25662. +
  25663. + rcu_read_lock();
  25664. retry:
  25665. if (req_cpu == WORK_CPU_UNBOUND)
  25666. cpu = raw_smp_processor_id();
  25667. @@ -1412,10 +1441,8 @@
  25668. /* pwq determined, queue */
  25669. trace_workqueue_queue_work(req_cpu, pwq, work);
  25670. - if (WARN_ON(!list_empty(&work->entry))) {
  25671. - spin_unlock(&pwq->pool->lock);
  25672. - return;
  25673. - }
  25674. + if (WARN_ON(!list_empty(&work->entry)))
  25675. + goto out;
  25676. pwq->nr_in_flight[pwq->work_color]++;
  25677. work_flags = work_color_to_flags(pwq->work_color);
  25678. @@ -1431,7 +1458,9 @@
  25679. insert_work(pwq, work, worklist, work_flags);
  25680. +out:
  25681. spin_unlock(&pwq->pool->lock);
  25682. + rcu_read_unlock();
  25683. }
  25684. /**
  25685. @@ -1451,14 +1480,14 @@
  25686. bool ret = false;
  25687. unsigned long flags;
  25688. - local_irq_save(flags);
  25689. + local_lock_irqsave(pendingb_lock,flags);
  25690. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
  25691. __queue_work(cpu, wq, work);
  25692. ret = true;
  25693. }
  25694. - local_irq_restore(flags);
  25695. + local_unlock_irqrestore(pendingb_lock, flags);
  25696. return ret;
  25697. }
  25698. EXPORT_SYMBOL(queue_work_on);
  25699. @@ -1525,14 +1554,14 @@
  25700. unsigned long flags;
  25701. /* read the comment in __queue_work() */
  25702. - local_irq_save(flags);
  25703. + local_lock_irqsave(pendingb_lock, flags);
  25704. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
  25705. __queue_delayed_work(cpu, wq, dwork, delay);
  25706. ret = true;
  25707. }
  25708. - local_irq_restore(flags);
  25709. + local_unlock_irqrestore(pendingb_lock, flags);
  25710. return ret;
  25711. }
  25712. EXPORT_SYMBOL(queue_delayed_work_on);
  25713. @@ -1567,7 +1596,7 @@
  25714. if (likely(ret >= 0)) {
  25715. __queue_delayed_work(cpu, wq, dwork, delay);
  25716. - local_irq_restore(flags);
  25717. + local_unlock_irqrestore(pendingb_lock, flags);
  25718. }
  25719. /* -ENOENT from try_to_grab_pending() becomes %true */
  25720. @@ -1600,7 +1629,9 @@
  25721. worker->last_active = jiffies;
  25722. /* idle_list is LIFO */
  25723. + rt_lock_idle_list(pool);
  25724. list_add(&worker->entry, &pool->idle_list);
  25725. + rt_unlock_idle_list(pool);
  25726. if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
  25727. mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
  25728. @@ -1633,7 +1664,9 @@
  25729. return;
  25730. worker_clr_flags(worker, WORKER_IDLE);
  25731. pool->nr_idle--;
  25732. + rt_lock_idle_list(pool);
  25733. list_del_init(&worker->entry);
  25734. + rt_unlock_idle_list(pool);
  25735. }
  25736. static struct worker *alloc_worker(int node)
  25737. @@ -1799,7 +1832,9 @@
  25738. pool->nr_workers--;
  25739. pool->nr_idle--;
  25740. + rt_lock_idle_list(pool);
  25741. list_del_init(&worker->entry);
  25742. + rt_unlock_idle_list(pool);
  25743. worker->flags |= WORKER_DIE;
  25744. wake_up_process(worker->task);
  25745. }
  25746. @@ -2716,14 +2751,14 @@
  25747. might_sleep();
  25748. - local_irq_disable();
  25749. + rcu_read_lock();
  25750. pool = get_work_pool(work);
  25751. if (!pool) {
  25752. - local_irq_enable();
  25753. + rcu_read_unlock();
  25754. return false;
  25755. }
  25756. - spin_lock(&pool->lock);
  25757. + spin_lock_irq(&pool->lock);
  25758. /* see the comment in try_to_grab_pending() with the same code */
  25759. pwq = get_work_pwq(work);
  25760. if (pwq) {
  25761. @@ -2750,10 +2785,11 @@
  25762. else
  25763. lock_map_acquire_read(&pwq->wq->lockdep_map);
  25764. lock_map_release(&pwq->wq->lockdep_map);
  25765. -
  25766. + rcu_read_unlock();
  25767. return true;
  25768. already_gone:
  25769. spin_unlock_irq(&pool->lock);
  25770. + rcu_read_unlock();
  25771. return false;
  25772. }
  25773. @@ -2840,7 +2876,7 @@
  25774. /* tell other tasks trying to grab @work to back off */
  25775. mark_work_canceling(work);
  25776. - local_irq_restore(flags);
  25777. + local_unlock_irqrestore(pendingb_lock, flags);
  25778. flush_work(work);
  25779. clear_work_data(work);
  25780. @@ -2895,10 +2931,10 @@
  25781. */
  25782. bool flush_delayed_work(struct delayed_work *dwork)
  25783. {
  25784. - local_irq_disable();
  25785. + local_lock_irq(pendingb_lock);
  25786. if (del_timer_sync(&dwork->timer))
  25787. __queue_work(dwork->cpu, dwork->wq, &dwork->work);
  25788. - local_irq_enable();
  25789. + local_unlock_irq(pendingb_lock);
  25790. return flush_work(&dwork->work);
  25791. }
  25792. EXPORT_SYMBOL(flush_delayed_work);
  25793. @@ -2933,7 +2969,7 @@
  25794. set_work_pool_and_clear_pending(&dwork->work,
  25795. get_work_pool_id(&dwork->work));
  25796. - local_irq_restore(flags);
  25797. + local_unlock_irqrestore(pendingb_lock, flags);
  25798. return ret;
  25799. }
  25800. EXPORT_SYMBOL(cancel_delayed_work);
  25801. @@ -3161,7 +3197,7 @@
  25802. * put_unbound_pool - put a worker_pool
  25803. * @pool: worker_pool to put
  25804. *
  25805. - * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU
  25806. + * Put @pool. If its refcnt reaches zero, it gets destroyed in RCU
  25807. * safe manner. get_unbound_pool() calls this function on its failure path
  25808. * and this function should be able to release pools which went through,
  25809. * successfully or not, init_worker_pool().
  25810. @@ -3215,8 +3251,8 @@
  25811. del_timer_sync(&pool->idle_timer);
  25812. del_timer_sync(&pool->mayday_timer);
  25813. - /* sched-RCU protected to allow dereferences from get_work_pool() */
  25814. - call_rcu_sched(&pool->rcu, rcu_free_pool);
  25815. + /* RCU protected to allow dereferences from get_work_pool() */
  25816. + call_rcu(&pool->rcu, rcu_free_pool);
  25817. }
  25818. /**
  25819. @@ -3323,14 +3359,14 @@
  25820. put_unbound_pool(pool);
  25821. mutex_unlock(&wq_pool_mutex);
  25822. - call_rcu_sched(&pwq->rcu, rcu_free_pwq);
  25823. + call_rcu(&pwq->rcu, rcu_free_pwq);
  25824. /*
  25825. * If we're the last pwq going away, @wq is already dead and no one
  25826. * is gonna access it anymore. Schedule RCU free.
  25827. */
  25828. if (is_last)
  25829. - call_rcu_sched(&wq->rcu, rcu_free_wq);
  25830. + call_rcu(&wq->rcu, rcu_free_wq);
  25831. }
  25832. /**
  25833. @@ -3983,7 +4019,7 @@
  25834. * The base ref is never dropped on per-cpu pwqs. Directly
  25835. * schedule RCU free.
  25836. */
  25837. - call_rcu_sched(&wq->rcu, rcu_free_wq);
  25838. + call_rcu(&wq->rcu, rcu_free_wq);
  25839. } else {
  25840. /*
  25841. * We're the sole accessor of @wq at this point. Directly
  25842. @@ -4076,7 +4112,8 @@
  25843. struct pool_workqueue *pwq;
  25844. bool ret;
  25845. - rcu_read_lock_sched();
  25846. + rcu_read_lock();
  25847. + preempt_disable();
  25848. if (cpu == WORK_CPU_UNBOUND)
  25849. cpu = smp_processor_id();
  25850. @@ -4087,7 +4124,8 @@
  25851. pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
  25852. ret = !list_empty(&pwq->delayed_works);
  25853. - rcu_read_unlock_sched();
  25854. + preempt_enable();
  25855. + rcu_read_unlock();
  25856. return ret;
  25857. }
  25858. @@ -4113,15 +4151,15 @@
  25859. if (work_pending(work))
  25860. ret |= WORK_BUSY_PENDING;
  25861. - local_irq_save(flags);
  25862. + rcu_read_lock();
  25863. pool = get_work_pool(work);
  25864. if (pool) {
  25865. - spin_lock(&pool->lock);
  25866. + spin_lock_irqsave(&pool->lock, flags);
  25867. if (find_worker_executing_work(pool, work))
  25868. ret |= WORK_BUSY_RUNNING;
  25869. - spin_unlock(&pool->lock);
  25870. + spin_unlock_irqrestore(&pool->lock, flags);
  25871. }
  25872. - local_irq_restore(flags);
  25873. + rcu_read_unlock();
  25874. return ret;
  25875. }
  25876. @@ -4310,7 +4348,7 @@
  25877. unsigned long flags;
  25878. int pi;
  25879. - rcu_read_lock_sched();
  25880. + rcu_read_lock();
  25881. pr_info("Showing busy workqueues and worker pools:\n");
  25882. @@ -4361,7 +4399,7 @@
  25883. spin_unlock_irqrestore(&pool->lock, flags);
  25884. }
  25885. - rcu_read_unlock_sched();
  25886. + rcu_read_unlock();
  25887. }
  25888. /*
  25889. @@ -4722,16 +4760,16 @@
  25890. * nr_active is monotonically decreasing. It's safe
  25891. * to peek without lock.
  25892. */
  25893. - rcu_read_lock_sched();
  25894. + rcu_read_lock();
  25895. for_each_pwq(pwq, wq) {
  25896. WARN_ON_ONCE(pwq->nr_active < 0);
  25897. if (pwq->nr_active) {
  25898. busy = true;
  25899. - rcu_read_unlock_sched();
  25900. + rcu_read_unlock();
  25901. goto out_unlock;
  25902. }
  25903. }
  25904. - rcu_read_unlock_sched();
  25905. + rcu_read_unlock();
  25906. }
  25907. out_unlock:
  25908. mutex_unlock(&wq_pool_mutex);
  25909. @@ -4921,7 +4959,8 @@
  25910. const char *delim = "";
  25911. int node, written = 0;
  25912. - rcu_read_lock_sched();
  25913. + get_online_cpus();
  25914. + rcu_read_lock();
  25915. for_each_node(node) {
  25916. written += scnprintf(buf + written, PAGE_SIZE - written,
  25917. "%s%d:%d", delim, node,
  25918. @@ -4929,7 +4968,8 @@
  25919. delim = " ";
  25920. }
  25921. written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
  25922. - rcu_read_unlock_sched();
  25923. + rcu_read_unlock();
  25924. + put_online_cpus();
  25925. return written;
  25926. }
  25927. diff -Nur linux-4.4.46.orig/kernel/workqueue_internal.h linux-4.4.46/kernel/workqueue_internal.h
  25928. --- linux-4.4.46.orig/kernel/workqueue_internal.h 2017-02-01 08:31:11.000000000 +0100
  25929. +++ linux-4.4.46/kernel/workqueue_internal.h 2017-02-03 17:18:10.943619676 +0100
  25930. @@ -43,6 +43,7 @@
  25931. unsigned long last_active; /* L: last active timestamp */
  25932. unsigned int flags; /* X: flags */
  25933. int id; /* I: worker id */
  25934. + int sleeping; /* None */
  25935. /*
  25936. * Opaque string set with work_set_desc(). Printed out with task
  25937. @@ -68,7 +69,7 @@
  25938. * Scheduler hooks for concurrency managed workqueue. Only to be used from
  25939. * sched/core.c and workqueue.c.
  25940. */
  25941. -void wq_worker_waking_up(struct task_struct *task, int cpu);
  25942. -struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu);
  25943. +void wq_worker_running(struct task_struct *task);
  25944. +void wq_worker_sleeping(struct task_struct *task);
  25945. #endif /* _KERNEL_WORKQUEUE_INTERNAL_H */
  25946. diff -Nur linux-4.4.46.orig/lib/debugobjects.c linux-4.4.46/lib/debugobjects.c
  25947. --- linux-4.4.46.orig/lib/debugobjects.c 2017-02-01 08:31:11.000000000 +0100
  25948. +++ linux-4.4.46/lib/debugobjects.c 2017-02-03 17:18:10.943619676 +0100
  25949. @@ -309,7 +309,10 @@
  25950. struct debug_obj *obj;
  25951. unsigned long flags;
  25952. - fill_pool();
  25953. +#ifdef CONFIG_PREEMPT_RT_FULL
  25954. + if (preempt_count() == 0 && !irqs_disabled())
  25955. +#endif
  25956. + fill_pool();
  25957. db = get_bucket((unsigned long) addr);
  25958. diff -Nur linux-4.4.46.orig/lib/idr.c linux-4.4.46/lib/idr.c
  25959. --- linux-4.4.46.orig/lib/idr.c 2017-02-01 08:31:11.000000000 +0100
  25960. +++ linux-4.4.46/lib/idr.c 2017-02-03 17:18:10.943619676 +0100
  25961. @@ -30,6 +30,7 @@
  25962. #include <linux/idr.h>
  25963. #include <linux/spinlock.h>
  25964. #include <linux/percpu.h>
  25965. +#include <linux/locallock.h>
  25966. #define MAX_IDR_SHIFT (sizeof(int) * 8 - 1)
  25967. #define MAX_IDR_BIT (1U << MAX_IDR_SHIFT)
  25968. @@ -45,6 +46,37 @@
  25969. static DEFINE_PER_CPU(int, idr_preload_cnt);
  25970. static DEFINE_SPINLOCK(simple_ida_lock);
  25971. +#ifdef CONFIG_PREEMPT_RT_FULL
  25972. +static DEFINE_LOCAL_IRQ_LOCK(idr_lock);
  25973. +
  25974. +static inline void idr_preload_lock(void)
  25975. +{
  25976. + local_lock(idr_lock);
  25977. +}
  25978. +
  25979. +static inline void idr_preload_unlock(void)
  25980. +{
  25981. + local_unlock(idr_lock);
  25982. +}
  25983. +
  25984. +void idr_preload_end(void)
  25985. +{
  25986. + idr_preload_unlock();
  25987. +}
  25988. +EXPORT_SYMBOL(idr_preload_end);
  25989. +#else
  25990. +static inline void idr_preload_lock(void)
  25991. +{
  25992. + preempt_disable();
  25993. +}
  25994. +
  25995. +static inline void idr_preload_unlock(void)
  25996. +{
  25997. + preempt_enable();
  25998. +}
  25999. +#endif
  26000. +
  26001. +
  26002. /* the maximum ID which can be allocated given idr->layers */
  26003. static int idr_max(int layers)
  26004. {
  26005. @@ -115,14 +147,14 @@
  26006. * context. See idr_preload() for details.
  26007. */
  26008. if (!in_interrupt()) {
  26009. - preempt_disable();
  26010. + idr_preload_lock();
  26011. new = __this_cpu_read(idr_preload_head);
  26012. if (new) {
  26013. __this_cpu_write(idr_preload_head, new->ary[0]);
  26014. __this_cpu_dec(idr_preload_cnt);
  26015. new->ary[0] = NULL;
  26016. }
  26017. - preempt_enable();
  26018. + idr_preload_unlock();
  26019. if (new)
  26020. return new;
  26021. }
  26022. @@ -366,7 +398,6 @@
  26023. idr_mark_full(pa, id);
  26024. }
  26025. -
  26026. /**
  26027. * idr_preload - preload for idr_alloc()
  26028. * @gfp_mask: allocation mask to use for preloading
  26029. @@ -401,7 +432,7 @@
  26030. WARN_ON_ONCE(in_interrupt());
  26031. might_sleep_if(gfpflags_allow_blocking(gfp_mask));
  26032. - preempt_disable();
  26033. + idr_preload_lock();
  26034. /*
  26035. * idr_alloc() is likely to succeed w/o full idr_layer buffer and
  26036. @@ -413,9 +444,9 @@
  26037. while (__this_cpu_read(idr_preload_cnt) < MAX_IDR_FREE) {
  26038. struct idr_layer *new;
  26039. - preempt_enable();
  26040. + idr_preload_unlock();
  26041. new = kmem_cache_zalloc(idr_layer_cache, gfp_mask);
  26042. - preempt_disable();
  26043. + idr_preload_lock();
  26044. if (!new)
  26045. break;
  26046. diff -Nur linux-4.4.46.orig/lib/Kconfig linux-4.4.46/lib/Kconfig
  26047. --- linux-4.4.46.orig/lib/Kconfig 2017-02-01 08:31:11.000000000 +0100
  26048. +++ linux-4.4.46/lib/Kconfig 2017-02-03 17:18:10.943619676 +0100
  26049. @@ -397,6 +397,7 @@
  26050. config CPUMASK_OFFSTACK
  26051. bool "Force CPU masks off stack" if DEBUG_PER_CPU_MAPS
  26052. + depends on !PREEMPT_RT_FULL
  26053. help
  26054. Use dynamic allocation for cpumask_var_t, instead of putting
  26055. them on the stack. This is a bit more expensive, but avoids
  26056. diff -Nur linux-4.4.46.orig/lib/locking-selftest.c linux-4.4.46/lib/locking-selftest.c
  26057. --- linux-4.4.46.orig/lib/locking-selftest.c 2017-02-01 08:31:11.000000000 +0100
  26058. +++ linux-4.4.46/lib/locking-selftest.c 2017-02-03 17:18:10.947619830 +0100
  26059. @@ -590,6 +590,8 @@
  26060. #include "locking-selftest-spin-hardirq.h"
  26061. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_spin)
  26062. +#ifndef CONFIG_PREEMPT_RT_FULL
  26063. +
  26064. #include "locking-selftest-rlock-hardirq.h"
  26065. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_rlock)
  26066. @@ -605,9 +607,12 @@
  26067. #include "locking-selftest-wlock-softirq.h"
  26068. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_wlock)
  26069. +#endif
  26070. +
  26071. #undef E1
  26072. #undef E2
  26073. +#ifndef CONFIG_PREEMPT_RT_FULL
  26074. /*
  26075. * Enabling hardirqs with a softirq-safe lock held:
  26076. */
  26077. @@ -640,6 +645,8 @@
  26078. #undef E1
  26079. #undef E2
  26080. +#endif
  26081. +
  26082. /*
  26083. * Enabling irqs with an irq-safe lock held:
  26084. */
  26085. @@ -663,6 +670,8 @@
  26086. #include "locking-selftest-spin-hardirq.h"
  26087. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_spin)
  26088. +#ifndef CONFIG_PREEMPT_RT_FULL
  26089. +
  26090. #include "locking-selftest-rlock-hardirq.h"
  26091. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_rlock)
  26092. @@ -678,6 +687,8 @@
  26093. #include "locking-selftest-wlock-softirq.h"
  26094. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock)
  26095. +#endif
  26096. +
  26097. #undef E1
  26098. #undef E2
  26099. @@ -709,6 +720,8 @@
  26100. #include "locking-selftest-spin-hardirq.h"
  26101. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_spin)
  26102. +#ifndef CONFIG_PREEMPT_RT_FULL
  26103. +
  26104. #include "locking-selftest-rlock-hardirq.h"
  26105. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_rlock)
  26106. @@ -724,6 +737,8 @@
  26107. #include "locking-selftest-wlock-softirq.h"
  26108. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock)
  26109. +#endif
  26110. +
  26111. #undef E1
  26112. #undef E2
  26113. #undef E3
  26114. @@ -757,6 +772,8 @@
  26115. #include "locking-selftest-spin-hardirq.h"
  26116. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_spin)
  26117. +#ifndef CONFIG_PREEMPT_RT_FULL
  26118. +
  26119. #include "locking-selftest-rlock-hardirq.h"
  26120. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_rlock)
  26121. @@ -772,10 +789,14 @@
  26122. #include "locking-selftest-wlock-softirq.h"
  26123. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_wlock)
  26124. +#endif
  26125. +
  26126. #undef E1
  26127. #undef E2
  26128. #undef E3
  26129. +#ifndef CONFIG_PREEMPT_RT_FULL
  26130. +
  26131. /*
  26132. * read-lock / write-lock irq inversion.
  26133. *
  26134. @@ -838,6 +859,10 @@
  26135. #undef E2
  26136. #undef E3
  26137. +#endif
  26138. +
  26139. +#ifndef CONFIG_PREEMPT_RT_FULL
  26140. +
  26141. /*
  26142. * read-lock / write-lock recursion that is actually safe.
  26143. */
  26144. @@ -876,6 +901,8 @@
  26145. #undef E2
  26146. #undef E3
  26147. +#endif
  26148. +
  26149. /*
  26150. * read-lock / write-lock recursion that is unsafe.
  26151. */
  26152. @@ -1858,6 +1885,7 @@
  26153. printk(" --------------------------------------------------------------------------\n");
  26154. +#ifndef CONFIG_PREEMPT_RT_FULL
  26155. /*
  26156. * irq-context testcases:
  26157. */
  26158. @@ -1870,6 +1898,28 @@
  26159. DO_TESTCASE_6x2("irq read-recursion", irq_read_recursion);
  26160. // DO_TESTCASE_6x2B("irq read-recursion #2", irq_read_recursion2);
  26161. +#else
  26162. + /* On -rt, we only do hardirq context test for raw spinlock */
  26163. + DO_TESTCASE_1B("hard-irqs-on + irq-safe-A", irqsafe1_hard_spin, 12);
  26164. + DO_TESTCASE_1B("hard-irqs-on + irq-safe-A", irqsafe1_hard_spin, 21);
  26165. +
  26166. + DO_TESTCASE_1B("hard-safe-A + irqs-on", irqsafe2B_hard_spin, 12);
  26167. + DO_TESTCASE_1B("hard-safe-A + irqs-on", irqsafe2B_hard_spin, 21);
  26168. +
  26169. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 123);
  26170. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 132);
  26171. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 213);
  26172. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 231);
  26173. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 312);
  26174. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 321);
  26175. +
  26176. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 123);
  26177. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 132);
  26178. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 213);
  26179. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 231);
  26180. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 312);
  26181. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 321);
  26182. +#endif
  26183. ww_tests();
  26184. diff -Nur linux-4.4.46.orig/lib/percpu_ida.c linux-4.4.46/lib/percpu_ida.c
  26185. --- linux-4.4.46.orig/lib/percpu_ida.c 2017-02-01 08:31:11.000000000 +0100
  26186. +++ linux-4.4.46/lib/percpu_ida.c 2017-02-03 17:18:10.947619830 +0100
  26187. @@ -26,6 +26,9 @@
  26188. #include <linux/string.h>
  26189. #include <linux/spinlock.h>
  26190. #include <linux/percpu_ida.h>
  26191. +#include <linux/locallock.h>
  26192. +
  26193. +static DEFINE_LOCAL_IRQ_LOCK(irq_off_lock);
  26194. struct percpu_ida_cpu {
  26195. /*
  26196. @@ -148,13 +151,13 @@
  26197. unsigned long flags;
  26198. int tag;
  26199. - local_irq_save(flags);
  26200. + local_lock_irqsave(irq_off_lock, flags);
  26201. tags = this_cpu_ptr(pool->tag_cpu);
  26202. /* Fastpath */
  26203. tag = alloc_local_tag(tags);
  26204. if (likely(tag >= 0)) {
  26205. - local_irq_restore(flags);
  26206. + local_unlock_irqrestore(irq_off_lock, flags);
  26207. return tag;
  26208. }
  26209. @@ -173,6 +176,7 @@
  26210. if (!tags->nr_free)
  26211. alloc_global_tags(pool, tags);
  26212. +
  26213. if (!tags->nr_free)
  26214. steal_tags(pool, tags);
  26215. @@ -184,7 +188,7 @@
  26216. }
  26217. spin_unlock(&pool->lock);
  26218. - local_irq_restore(flags);
  26219. + local_unlock_irqrestore(irq_off_lock, flags);
  26220. if (tag >= 0 || state == TASK_RUNNING)
  26221. break;
  26222. @@ -196,7 +200,7 @@
  26223. schedule();
  26224. - local_irq_save(flags);
  26225. + local_lock_irqsave(irq_off_lock, flags);
  26226. tags = this_cpu_ptr(pool->tag_cpu);
  26227. }
  26228. if (state != TASK_RUNNING)
  26229. @@ -221,7 +225,7 @@
  26230. BUG_ON(tag >= pool->nr_tags);
  26231. - local_irq_save(flags);
  26232. + local_lock_irqsave(irq_off_lock, flags);
  26233. tags = this_cpu_ptr(pool->tag_cpu);
  26234. spin_lock(&tags->lock);
  26235. @@ -253,7 +257,7 @@
  26236. spin_unlock(&pool->lock);
  26237. }
  26238. - local_irq_restore(flags);
  26239. + local_unlock_irqrestore(irq_off_lock, flags);
  26240. }
  26241. EXPORT_SYMBOL_GPL(percpu_ida_free);
  26242. @@ -345,7 +349,7 @@
  26243. struct percpu_ida_cpu *remote;
  26244. unsigned cpu, i, err = 0;
  26245. - local_irq_save(flags);
  26246. + local_lock_irqsave(irq_off_lock, flags);
  26247. for_each_possible_cpu(cpu) {
  26248. remote = per_cpu_ptr(pool->tag_cpu, cpu);
  26249. spin_lock(&remote->lock);
  26250. @@ -367,7 +371,7 @@
  26251. }
  26252. spin_unlock(&pool->lock);
  26253. out:
  26254. - local_irq_restore(flags);
  26255. + local_unlock_irqrestore(irq_off_lock, flags);
  26256. return err;
  26257. }
  26258. EXPORT_SYMBOL_GPL(percpu_ida_for_each_free);
  26259. diff -Nur linux-4.4.46.orig/lib/radix-tree.c linux-4.4.46/lib/radix-tree.c
  26260. --- linux-4.4.46.orig/lib/radix-tree.c 2017-02-01 08:31:11.000000000 +0100
  26261. +++ linux-4.4.46/lib/radix-tree.c 2017-02-03 17:18:10.947619830 +0100
  26262. @@ -196,13 +196,14 @@
  26263. * succeed in getting a node here (and never reach
  26264. * kmem_cache_alloc)
  26265. */
  26266. - rtp = this_cpu_ptr(&radix_tree_preloads);
  26267. + rtp = &get_cpu_var(radix_tree_preloads);
  26268. if (rtp->nr) {
  26269. ret = rtp->nodes;
  26270. rtp->nodes = ret->private_data;
  26271. ret->private_data = NULL;
  26272. rtp->nr--;
  26273. }
  26274. + put_cpu_var(radix_tree_preloads);
  26275. /*
  26276. * Update the allocation stack trace as this is more useful
  26277. * for debugging.
  26278. @@ -242,6 +243,7 @@
  26279. call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
  26280. }
  26281. +#ifndef CONFIG_PREEMPT_RT_FULL
  26282. /*
  26283. * Load up this CPU's radix_tree_node buffer with sufficient objects to
  26284. * ensure that the addition of a single element in the tree cannot fail. On
  26285. @@ -310,6 +312,7 @@
  26286. return 0;
  26287. }
  26288. EXPORT_SYMBOL(radix_tree_maybe_preload);
  26289. +#endif
  26290. /*
  26291. * Return the maximum key which can be store into a
  26292. diff -Nur linux-4.4.46.orig/lib/rbtree.c linux-4.4.46/lib/rbtree.c
  26293. --- linux-4.4.46.orig/lib/rbtree.c 2017-02-01 08:31:11.000000000 +0100
  26294. +++ linux-4.4.46/lib/rbtree.c 2017-02-03 17:18:10.947619830 +0100
  26295. @@ -23,6 +23,7 @@
  26296. #include <linux/rbtree_augmented.h>
  26297. #include <linux/export.h>
  26298. +#include <linux/rcupdate.h>
  26299. /*
  26300. * red-black trees properties: http://en.wikipedia.org/wiki/Rbtree
  26301. @@ -590,3 +591,13 @@
  26302. return rb_left_deepest_node(root->rb_node);
  26303. }
  26304. EXPORT_SYMBOL(rb_first_postorder);
  26305. +
  26306. +void rb_link_node_rcu(struct rb_node *node, struct rb_node *parent,
  26307. + struct rb_node **rb_link)
  26308. +{
  26309. + node->__rb_parent_color = (unsigned long)parent;
  26310. + node->rb_left = node->rb_right = NULL;
  26311. +
  26312. + rcu_assign_pointer(*rb_link, node);
  26313. +}
  26314. +EXPORT_SYMBOL(rb_link_node_rcu);
  26315. diff -Nur linux-4.4.46.orig/lib/scatterlist.c linux-4.4.46/lib/scatterlist.c
  26316. --- linux-4.4.46.orig/lib/scatterlist.c 2017-02-01 08:31:11.000000000 +0100
  26317. +++ linux-4.4.46/lib/scatterlist.c 2017-02-03 17:18:10.947619830 +0100
  26318. @@ -620,7 +620,7 @@
  26319. flush_kernel_dcache_page(miter->page);
  26320. if (miter->__flags & SG_MITER_ATOMIC) {
  26321. - WARN_ON_ONCE(preemptible());
  26322. + WARN_ON_ONCE(!pagefault_disabled());
  26323. kunmap_atomic(miter->addr);
  26324. } else
  26325. kunmap(miter->page);
  26326. @@ -664,7 +664,7 @@
  26327. if (!sg_miter_skip(&miter, skip))
  26328. return false;
  26329. - local_irq_save(flags);
  26330. + local_irq_save_nort(flags);
  26331. while (sg_miter_next(&miter) && offset < buflen) {
  26332. unsigned int len;
  26333. @@ -681,7 +681,7 @@
  26334. sg_miter_stop(&miter);
  26335. - local_irq_restore(flags);
  26336. + local_irq_restore_nort(flags);
  26337. return offset;
  26338. }
  26339. EXPORT_SYMBOL(sg_copy_buffer);
  26340. diff -Nur linux-4.4.46.orig/lib/smp_processor_id.c linux-4.4.46/lib/smp_processor_id.c
  26341. --- linux-4.4.46.orig/lib/smp_processor_id.c 2017-02-01 08:31:11.000000000 +0100
  26342. +++ linux-4.4.46/lib/smp_processor_id.c 2017-02-03 17:18:10.947619830 +0100
  26343. @@ -39,8 +39,9 @@
  26344. if (!printk_ratelimit())
  26345. goto out_enable;
  26346. - printk(KERN_ERR "BUG: using %s%s() in preemptible [%08x] code: %s/%d\n",
  26347. - what1, what2, preempt_count() - 1, current->comm, current->pid);
  26348. + printk(KERN_ERR "BUG: using %s%s() in preemptible [%08x %08x] code: %s/%d\n",
  26349. + what1, what2, preempt_count() - 1, __migrate_disabled(current),
  26350. + current->comm, current->pid);
  26351. print_symbol("caller is %s\n", (long)__builtin_return_address(0));
  26352. dump_stack();
  26353. diff -Nur linux-4.4.46.orig/Makefile linux-4.4.46/Makefile
  26354. --- linux-4.4.46.orig/Makefile 2017-02-01 08:31:11.000000000 +0100
  26355. +++ linux-4.4.46/Makefile 2017-02-03 17:18:05.627414322 +0100
  26356. @@ -785,6 +785,9 @@
  26357. # Prohibit date/time macros, which would make the build non-deterministic
  26358. KBUILD_CFLAGS += $(call cc-option,-Werror=date-time)
  26359. +# enforce correct pointer usage
  26360. +KBUILD_CFLAGS += $(call cc-option,-Werror=incompatible-pointer-types)
  26361. +
  26362. # use the deterministic mode of AR if available
  26363. KBUILD_ARFLAGS := $(call ar-option,D)
  26364. diff -Nur linux-4.4.46.orig/mm/backing-dev.c linux-4.4.46/mm/backing-dev.c
  26365. --- linux-4.4.46.orig/mm/backing-dev.c 2017-02-01 08:31:11.000000000 +0100
  26366. +++ linux-4.4.46/mm/backing-dev.c 2017-02-03 17:18:10.947619830 +0100
  26367. @@ -457,9 +457,9 @@
  26368. {
  26369. unsigned long flags;
  26370. - local_irq_save(flags);
  26371. + local_irq_save_nort(flags);
  26372. if (!atomic_dec_and_lock(&congested->refcnt, &cgwb_lock)) {
  26373. - local_irq_restore(flags);
  26374. + local_irq_restore_nort(flags);
  26375. return;
  26376. }
  26377. diff -Nur linux-4.4.46.orig/mm/compaction.c linux-4.4.46/mm/compaction.c
  26378. --- linux-4.4.46.orig/mm/compaction.c 2017-02-01 08:31:11.000000000 +0100
  26379. +++ linux-4.4.46/mm/compaction.c 2017-02-03 17:18:10.947619830 +0100
  26380. @@ -1430,10 +1430,12 @@
  26381. cc->migrate_pfn & ~((1UL << cc->order) - 1);
  26382. if (cc->last_migrated_pfn < current_block_start) {
  26383. - cpu = get_cpu();
  26384. + cpu = get_cpu_light();
  26385. + local_lock_irq(swapvec_lock);
  26386. lru_add_drain_cpu(cpu);
  26387. + local_unlock_irq(swapvec_lock);
  26388. drain_local_pages(zone);
  26389. - put_cpu();
  26390. + put_cpu_light();
  26391. /* No more flushing until we migrate again */
  26392. cc->last_migrated_pfn = 0;
  26393. }
  26394. diff -Nur linux-4.4.46.orig/mm/filemap.c linux-4.4.46/mm/filemap.c
  26395. --- linux-4.4.46.orig/mm/filemap.c 2017-02-01 08:31:11.000000000 +0100
  26396. +++ linux-4.4.46/mm/filemap.c 2017-02-03 17:18:10.947619830 +0100
  26397. @@ -144,9 +144,12 @@
  26398. * node->private_list is protected by
  26399. * mapping->tree_lock.
  26400. */
  26401. - if (!list_empty(&node->private_list))
  26402. - list_lru_del(&workingset_shadow_nodes,
  26403. + if (!list_empty(&node->private_list)) {
  26404. + local_lock(workingset_shadow_lock);
  26405. + list_lru_del(&__workingset_shadow_nodes,
  26406. &node->private_list);
  26407. + local_unlock(workingset_shadow_lock);
  26408. + }
  26409. }
  26410. return 0;
  26411. }
  26412. @@ -218,7 +221,9 @@
  26413. if (!workingset_node_pages(node) &&
  26414. list_empty(&node->private_list)) {
  26415. node->private_data = mapping;
  26416. - list_lru_add(&workingset_shadow_nodes, &node->private_list);
  26417. + local_lock(workingset_shadow_lock);
  26418. + list_lru_add(&__workingset_shadow_nodes, &node->private_list);
  26419. + local_unlock(workingset_shadow_lock);
  26420. }
  26421. }
  26422. diff -Nur linux-4.4.46.orig/mm/highmem.c linux-4.4.46/mm/highmem.c
  26423. --- linux-4.4.46.orig/mm/highmem.c 2017-02-01 08:31:11.000000000 +0100
  26424. +++ linux-4.4.46/mm/highmem.c 2017-02-03 17:18:10.947619830 +0100
  26425. @@ -29,10 +29,11 @@
  26426. #include <linux/kgdb.h>
  26427. #include <asm/tlbflush.h>
  26428. -
  26429. +#ifndef CONFIG_PREEMPT_RT_FULL
  26430. #if defined(CONFIG_HIGHMEM) || defined(CONFIG_X86_32)
  26431. DEFINE_PER_CPU(int, __kmap_atomic_idx);
  26432. #endif
  26433. +#endif
  26434. /*
  26435. * Virtual_count is not a pure "count".
  26436. @@ -107,8 +108,9 @@
  26437. unsigned long totalhigh_pages __read_mostly;
  26438. EXPORT_SYMBOL(totalhigh_pages);
  26439. -
  26440. +#ifndef CONFIG_PREEMPT_RT_FULL
  26441. EXPORT_PER_CPU_SYMBOL(__kmap_atomic_idx);
  26442. +#endif
  26443. unsigned int nr_free_highpages (void)
  26444. {
  26445. diff -Nur linux-4.4.46.orig/mm/Kconfig linux-4.4.46/mm/Kconfig
  26446. --- linux-4.4.46.orig/mm/Kconfig 2017-02-01 08:31:11.000000000 +0100
  26447. +++ linux-4.4.46/mm/Kconfig 2017-02-03 17:18:10.947619830 +0100
  26448. @@ -392,7 +392,7 @@
  26449. config TRANSPARENT_HUGEPAGE
  26450. bool "Transparent Hugepage Support"
  26451. - depends on HAVE_ARCH_TRANSPARENT_HUGEPAGE
  26452. + depends on HAVE_ARCH_TRANSPARENT_HUGEPAGE && !PREEMPT_RT_FULL
  26453. select COMPACTION
  26454. help
  26455. Transparent Hugepages allows the kernel to use huge pages and
  26456. diff -Nur linux-4.4.46.orig/mm/memcontrol.c linux-4.4.46/mm/memcontrol.c
  26457. --- linux-4.4.46.orig/mm/memcontrol.c 2017-02-01 08:31:11.000000000 +0100
  26458. +++ linux-4.4.46/mm/memcontrol.c 2017-02-03 17:18:10.947619830 +0100
  26459. @@ -67,6 +67,8 @@
  26460. #include <net/sock.h>
  26461. #include <net/ip.h>
  26462. #include <net/tcp_memcontrol.h>
  26463. +#include <linux/locallock.h>
  26464. +
  26465. #include "slab.h"
  26466. #include <asm/uaccess.h>
  26467. @@ -87,6 +89,7 @@
  26468. #define do_swap_account 0
  26469. #endif
  26470. +static DEFINE_LOCAL_IRQ_LOCK(event_lock);
  26471. static const char * const mem_cgroup_stat_names[] = {
  26472. "cache",
  26473. "rss",
  26474. @@ -1922,14 +1925,17 @@
  26475. */
  26476. static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
  26477. {
  26478. - struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
  26479. + struct memcg_stock_pcp *stock;
  26480. + int cpu = get_cpu_light();
  26481. +
  26482. + stock = &per_cpu(memcg_stock, cpu);
  26483. if (stock->cached != memcg) { /* reset if necessary */
  26484. drain_stock(stock);
  26485. stock->cached = memcg;
  26486. }
  26487. stock->nr_pages += nr_pages;
  26488. - put_cpu_var(memcg_stock);
  26489. + put_cpu_light();
  26490. }
  26491. /*
  26492. @@ -1945,7 +1951,7 @@
  26493. return;
  26494. /* Notify other cpus that system-wide "drain" is running */
  26495. get_online_cpus();
  26496. - curcpu = get_cpu();
  26497. + curcpu = get_cpu_light();
  26498. for_each_online_cpu(cpu) {
  26499. struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
  26500. struct mem_cgroup *memcg;
  26501. @@ -1962,7 +1968,7 @@
  26502. schedule_work_on(cpu, &stock->work);
  26503. }
  26504. }
  26505. - put_cpu();
  26506. + put_cpu_light();
  26507. put_online_cpus();
  26508. mutex_unlock(&percpu_charge_mutex);
  26509. }
  26510. @@ -4709,12 +4715,12 @@
  26511. ret = 0;
  26512. - local_irq_disable();
  26513. + local_lock_irq(event_lock);
  26514. mem_cgroup_charge_statistics(to, page, nr_pages);
  26515. memcg_check_events(to, page);
  26516. mem_cgroup_charge_statistics(from, page, -nr_pages);
  26517. memcg_check_events(from, page);
  26518. - local_irq_enable();
  26519. + local_unlock_irq(event_lock);
  26520. out_unlock:
  26521. unlock_page(page);
  26522. out:
  26523. @@ -5504,10 +5510,10 @@
  26524. VM_BUG_ON_PAGE(!PageTransHuge(page), page);
  26525. }
  26526. - local_irq_disable();
  26527. + local_lock_irq(event_lock);
  26528. mem_cgroup_charge_statistics(memcg, page, nr_pages);
  26529. memcg_check_events(memcg, page);
  26530. - local_irq_enable();
  26531. + local_unlock_irq(event_lock);
  26532. if (do_swap_account && PageSwapCache(page)) {
  26533. swp_entry_t entry = { .val = page_private(page) };
  26534. @@ -5563,14 +5569,14 @@
  26535. memcg_oom_recover(memcg);
  26536. }
  26537. - local_irq_save(flags);
  26538. + local_lock_irqsave(event_lock, flags);
  26539. __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS], nr_anon);
  26540. __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_CACHE], nr_file);
  26541. __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE], nr_huge);
  26542. __this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT], pgpgout);
  26543. __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
  26544. memcg_check_events(memcg, dummy_page);
  26545. - local_irq_restore(flags);
  26546. + local_unlock_irqrestore(event_lock, flags);
  26547. if (!mem_cgroup_is_root(memcg))
  26548. css_put_many(&memcg->css, nr_pages);
  26549. @@ -5762,6 +5768,7 @@
  26550. {
  26551. struct mem_cgroup *memcg, *swap_memcg;
  26552. unsigned short oldid;
  26553. + unsigned long flags;
  26554. VM_BUG_ON_PAGE(PageLRU(page), page);
  26555. VM_BUG_ON_PAGE(page_count(page), page);
  26556. @@ -5802,12 +5809,16 @@
  26557. * important here to have the interrupts disabled because it is the
  26558. * only synchronisation we have for udpating the per-CPU variables.
  26559. */
  26560. + local_lock_irqsave(event_lock, flags);
  26561. +#ifndef CONFIG_PREEMPT_RT_BASE
  26562. VM_BUG_ON(!irqs_disabled());
  26563. +#endif
  26564. mem_cgroup_charge_statistics(memcg, page, -1);
  26565. memcg_check_events(memcg, page);
  26566. if (!mem_cgroup_is_root(memcg))
  26567. css_put(&memcg->css);
  26568. + local_unlock_irqrestore(event_lock, flags);
  26569. }
  26570. /**
  26571. diff -Nur linux-4.4.46.orig/mm/mmu_context.c linux-4.4.46/mm/mmu_context.c
  26572. --- linux-4.4.46.orig/mm/mmu_context.c 2017-02-01 08:31:11.000000000 +0100
  26573. +++ linux-4.4.46/mm/mmu_context.c 2017-02-03 17:18:10.947619830 +0100
  26574. @@ -23,6 +23,7 @@
  26575. struct task_struct *tsk = current;
  26576. task_lock(tsk);
  26577. + preempt_disable_rt();
  26578. active_mm = tsk->active_mm;
  26579. if (active_mm != mm) {
  26580. atomic_inc(&mm->mm_count);
  26581. @@ -30,6 +31,7 @@
  26582. }
  26583. tsk->mm = mm;
  26584. switch_mm(active_mm, mm, tsk);
  26585. + preempt_enable_rt();
  26586. task_unlock(tsk);
  26587. #ifdef finish_arch_post_lock_switch
  26588. finish_arch_post_lock_switch();
  26589. diff -Nur linux-4.4.46.orig/mm/page_alloc.c linux-4.4.46/mm/page_alloc.c
  26590. --- linux-4.4.46.orig/mm/page_alloc.c 2017-02-01 08:31:11.000000000 +0100
  26591. +++ linux-4.4.46/mm/page_alloc.c 2017-02-03 17:18:10.951619984 +0100
  26592. @@ -60,6 +60,7 @@
  26593. #include <linux/page_ext.h>
  26594. #include <linux/hugetlb.h>
  26595. #include <linux/sched/rt.h>
  26596. +#include <linux/locallock.h>
  26597. #include <linux/page_owner.h>
  26598. #include <linux/kthread.h>
  26599. @@ -264,6 +265,18 @@
  26600. EXPORT_SYMBOL(nr_online_nodes);
  26601. #endif
  26602. +static DEFINE_LOCAL_IRQ_LOCK(pa_lock);
  26603. +
  26604. +#ifdef CONFIG_PREEMPT_RT_BASE
  26605. +# define cpu_lock_irqsave(cpu, flags) \
  26606. + local_lock_irqsave_on(pa_lock, flags, cpu)
  26607. +# define cpu_unlock_irqrestore(cpu, flags) \
  26608. + local_unlock_irqrestore_on(pa_lock, flags, cpu)
  26609. +#else
  26610. +# define cpu_lock_irqsave(cpu, flags) local_irq_save(flags)
  26611. +# define cpu_unlock_irqrestore(cpu, flags) local_irq_restore(flags)
  26612. +#endif
  26613. +
  26614. int page_group_by_mobility_disabled __read_mostly;
  26615. #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
  26616. @@ -786,7 +799,7 @@
  26617. }
  26618. /*
  26619. - * Frees a number of pages from the PCP lists
  26620. + * Frees a number of pages which have been collected from the pcp lists.
  26621. * Assumes all pages on list are in same zone, and of same order.
  26622. * count is the number of pages to free.
  26623. *
  26624. @@ -797,18 +810,53 @@
  26625. * pinned" detection logic.
  26626. */
  26627. static void free_pcppages_bulk(struct zone *zone, int count,
  26628. - struct per_cpu_pages *pcp)
  26629. + struct list_head *list)
  26630. {
  26631. - int migratetype = 0;
  26632. - int batch_free = 0;
  26633. int to_free = count;
  26634. unsigned long nr_scanned;
  26635. + unsigned long flags;
  26636. +
  26637. + spin_lock_irqsave(&zone->lock, flags);
  26638. - spin_lock(&zone->lock);
  26639. nr_scanned = zone_page_state(zone, NR_PAGES_SCANNED);
  26640. if (nr_scanned)
  26641. __mod_zone_page_state(zone, NR_PAGES_SCANNED, -nr_scanned);
  26642. + while (!list_empty(list)) {
  26643. + struct page *page = list_first_entry(list, struct page, lru);
  26644. + int mt; /* migratetype of the to-be-freed page */
  26645. +
  26646. + /* must delete as __free_one_page list manipulates */
  26647. + list_del(&page->lru);
  26648. +
  26649. + mt = get_pcppage_migratetype(page);
  26650. + /* MIGRATE_ISOLATE page should not go to pcplists */
  26651. + VM_BUG_ON_PAGE(is_migrate_isolate(mt), page);
  26652. + /* Pageblock could have been isolated meanwhile */
  26653. + if (unlikely(has_isolate_pageblock(zone)))
  26654. + mt = get_pageblock_migratetype(page);
  26655. +
  26656. + __free_one_page(page, page_to_pfn(page), zone, 0, mt);
  26657. + trace_mm_page_pcpu_drain(page, 0, mt);
  26658. + to_free--;
  26659. + }
  26660. + WARN_ON(to_free != 0);
  26661. + spin_unlock_irqrestore(&zone->lock, flags);
  26662. +}
  26663. +
  26664. +/*
  26665. + * Moves a number of pages from the PCP lists to free list which
  26666. + * is freed outside of the locked region.
  26667. + *
  26668. + * Assumes all pages on list are in same zone, and of same order.
  26669. + * count is the number of pages to free.
  26670. + */
  26671. +static void isolate_pcp_pages(int to_free, struct per_cpu_pages *src,
  26672. + struct list_head *dst)
  26673. +{
  26674. + int migratetype = 0;
  26675. + int batch_free = 0;
  26676. +
  26677. while (to_free) {
  26678. struct page *page;
  26679. struct list_head *list;
  26680. @@ -824,7 +872,7 @@
  26681. batch_free++;
  26682. if (++migratetype == MIGRATE_PCPTYPES)
  26683. migratetype = 0;
  26684. - list = &pcp->lists[migratetype];
  26685. + list = &src->lists[migratetype];
  26686. } while (list_empty(list));
  26687. /* This is the only non-empty list. Free them all. */
  26688. @@ -832,24 +880,12 @@
  26689. batch_free = to_free;
  26690. do {
  26691. - int mt; /* migratetype of the to-be-freed page */
  26692. -
  26693. - page = list_entry(list->prev, struct page, lru);
  26694. - /* must delete as __free_one_page list manipulates */
  26695. + page = list_last_entry(list, struct page, lru);
  26696. list_del(&page->lru);
  26697. - mt = get_pcppage_migratetype(page);
  26698. - /* MIGRATE_ISOLATE page should not go to pcplists */
  26699. - VM_BUG_ON_PAGE(is_migrate_isolate(mt), page);
  26700. - /* Pageblock could have been isolated meanwhile */
  26701. - if (unlikely(has_isolate_pageblock(zone)))
  26702. - mt = get_pageblock_migratetype(page);
  26703. -
  26704. - __free_one_page(page, page_to_pfn(page), zone, 0, mt);
  26705. - trace_mm_page_pcpu_drain(page, 0, mt);
  26706. + list_add(&page->lru, dst);
  26707. } while (--to_free && --batch_free && !list_empty(list));
  26708. }
  26709. - spin_unlock(&zone->lock);
  26710. }
  26711. static void free_one_page(struct zone *zone,
  26712. @@ -858,7 +894,9 @@
  26713. int migratetype)
  26714. {
  26715. unsigned long nr_scanned;
  26716. - spin_lock(&zone->lock);
  26717. + unsigned long flags;
  26718. +
  26719. + spin_lock_irqsave(&zone->lock, flags);
  26720. nr_scanned = zone_page_state(zone, NR_PAGES_SCANNED);
  26721. if (nr_scanned)
  26722. __mod_zone_page_state(zone, NR_PAGES_SCANNED, -nr_scanned);
  26723. @@ -868,7 +906,7 @@
  26724. migratetype = get_pfnblock_migratetype(page, pfn);
  26725. }
  26726. __free_one_page(page, pfn, zone, order, migratetype);
  26727. - spin_unlock(&zone->lock);
  26728. + spin_unlock_irqrestore(&zone->lock, flags);
  26729. }
  26730. static int free_tail_pages_check(struct page *head_page, struct page *page)
  26731. @@ -1019,10 +1057,10 @@
  26732. return;
  26733. migratetype = get_pfnblock_migratetype(page, pfn);
  26734. - local_irq_save(flags);
  26735. + local_lock_irqsave(pa_lock, flags);
  26736. __count_vm_events(PGFREE, 1 << order);
  26737. free_one_page(page_zone(page), page, pfn, order, migratetype);
  26738. - local_irq_restore(flags);
  26739. + local_unlock_irqrestore(pa_lock, flags);
  26740. }
  26741. static void __init __free_pages_boot_core(struct page *page,
  26742. @@ -1879,16 +1917,18 @@
  26743. void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp)
  26744. {
  26745. unsigned long flags;
  26746. + LIST_HEAD(dst);
  26747. int to_drain, batch;
  26748. - local_irq_save(flags);
  26749. + local_lock_irqsave(pa_lock, flags);
  26750. batch = READ_ONCE(pcp->batch);
  26751. to_drain = min(pcp->count, batch);
  26752. if (to_drain > 0) {
  26753. - free_pcppages_bulk(zone, to_drain, pcp);
  26754. + isolate_pcp_pages(to_drain, pcp, &dst);
  26755. pcp->count -= to_drain;
  26756. }
  26757. - local_irq_restore(flags);
  26758. + local_unlock_irqrestore(pa_lock, flags);
  26759. + free_pcppages_bulk(zone, to_drain, &dst);
  26760. }
  26761. #endif
  26762. @@ -1904,16 +1944,21 @@
  26763. unsigned long flags;
  26764. struct per_cpu_pageset *pset;
  26765. struct per_cpu_pages *pcp;
  26766. + LIST_HEAD(dst);
  26767. + int count;
  26768. - local_irq_save(flags);
  26769. + cpu_lock_irqsave(cpu, flags);
  26770. pset = per_cpu_ptr(zone->pageset, cpu);
  26771. pcp = &pset->pcp;
  26772. - if (pcp->count) {
  26773. - free_pcppages_bulk(zone, pcp->count, pcp);
  26774. + count = pcp->count;
  26775. + if (count) {
  26776. + isolate_pcp_pages(count, pcp, &dst);
  26777. pcp->count = 0;
  26778. }
  26779. - local_irq_restore(flags);
  26780. + cpu_unlock_irqrestore(cpu, flags);
  26781. + if (count)
  26782. + free_pcppages_bulk(zone, count, &dst);
  26783. }
  26784. /*
  26785. @@ -1999,8 +2044,17 @@
  26786. else
  26787. cpumask_clear_cpu(cpu, &cpus_with_pcps);
  26788. }
  26789. +#ifndef CONFIG_PREEMPT_RT_BASE
  26790. on_each_cpu_mask(&cpus_with_pcps, (smp_call_func_t) drain_local_pages,
  26791. zone, 1);
  26792. +#else
  26793. + for_each_cpu(cpu, &cpus_with_pcps) {
  26794. + if (zone)
  26795. + drain_pages_zone(cpu, zone);
  26796. + else
  26797. + drain_pages(cpu);
  26798. + }
  26799. +#endif
  26800. }
  26801. #ifdef CONFIG_HIBERNATION
  26802. @@ -2056,7 +2110,7 @@
  26803. migratetype = get_pfnblock_migratetype(page, pfn);
  26804. set_pcppage_migratetype(page, migratetype);
  26805. - local_irq_save(flags);
  26806. + local_lock_irqsave(pa_lock, flags);
  26807. __count_vm_event(PGFREE);
  26808. /*
  26809. @@ -2082,12 +2136,17 @@
  26810. pcp->count++;
  26811. if (pcp->count >= pcp->high) {
  26812. unsigned long batch = READ_ONCE(pcp->batch);
  26813. - free_pcppages_bulk(zone, batch, pcp);
  26814. + LIST_HEAD(dst);
  26815. +
  26816. + isolate_pcp_pages(batch, pcp, &dst);
  26817. pcp->count -= batch;
  26818. + local_unlock_irqrestore(pa_lock, flags);
  26819. + free_pcppages_bulk(zone, batch, &dst);
  26820. + return;
  26821. }
  26822. out:
  26823. - local_irq_restore(flags);
  26824. + local_unlock_irqrestore(pa_lock, flags);
  26825. }
  26826. /*
  26827. @@ -2222,7 +2281,7 @@
  26828. struct per_cpu_pages *pcp;
  26829. struct list_head *list;
  26830. - local_irq_save(flags);
  26831. + local_lock_irqsave(pa_lock, flags);
  26832. pcp = &this_cpu_ptr(zone->pageset)->pcp;
  26833. list = &pcp->lists[migratetype];
  26834. if (list_empty(list)) {
  26835. @@ -2254,7 +2313,7 @@
  26836. */
  26837. WARN_ON_ONCE(order > 1);
  26838. }
  26839. - spin_lock_irqsave(&zone->lock, flags);
  26840. + local_spin_lock_irqsave(pa_lock, &zone->lock, flags);
  26841. page = NULL;
  26842. if (alloc_flags & ALLOC_HARDER) {
  26843. @@ -2264,11 +2323,13 @@
  26844. }
  26845. if (!page)
  26846. page = __rmqueue(zone, order, migratetype, gfp_flags);
  26847. - spin_unlock(&zone->lock);
  26848. - if (!page)
  26849. + if (!page) {
  26850. + spin_unlock(&zone->lock);
  26851. goto failed;
  26852. + }
  26853. __mod_zone_freepage_state(zone, -(1 << order),
  26854. get_pcppage_migratetype(page));
  26855. + spin_unlock(&zone->lock);
  26856. }
  26857. __mod_zone_page_state(zone, NR_ALLOC_BATCH, -(1 << order));
  26858. @@ -2278,13 +2339,13 @@
  26859. __count_zone_vm_events(PGALLOC, zone, 1 << order);
  26860. zone_statistics(preferred_zone, zone, gfp_flags);
  26861. - local_irq_restore(flags);
  26862. + local_unlock_irqrestore(pa_lock, flags);
  26863. VM_BUG_ON_PAGE(bad_range(zone, page), page);
  26864. return page;
  26865. failed:
  26866. - local_irq_restore(flags);
  26867. + local_unlock_irqrestore(pa_lock, flags);
  26868. return NULL;
  26869. }
  26870. @@ -5953,6 +6014,7 @@
  26871. void __init page_alloc_init(void)
  26872. {
  26873. hotcpu_notifier(page_alloc_cpu_notify, 0);
  26874. + local_irq_lock_init(pa_lock);
  26875. }
  26876. /*
  26877. @@ -6847,7 +6909,7 @@
  26878. struct per_cpu_pageset *pset;
  26879. /* avoid races with drain_pages() */
  26880. - local_irq_save(flags);
  26881. + local_lock_irqsave(pa_lock, flags);
  26882. if (zone->pageset != &boot_pageset) {
  26883. for_each_online_cpu(cpu) {
  26884. pset = per_cpu_ptr(zone->pageset, cpu);
  26885. @@ -6856,7 +6918,7 @@
  26886. free_percpu(zone->pageset);
  26887. zone->pageset = &boot_pageset;
  26888. }
  26889. - local_irq_restore(flags);
  26890. + local_unlock_irqrestore(pa_lock, flags);
  26891. }
  26892. #ifdef CONFIG_MEMORY_HOTREMOVE
  26893. diff -Nur linux-4.4.46.orig/mm/slab.h linux-4.4.46/mm/slab.h
  26894. --- linux-4.4.46.orig/mm/slab.h 2017-02-01 08:31:11.000000000 +0100
  26895. +++ linux-4.4.46/mm/slab.h 2017-02-03 17:18:10.951619984 +0100
  26896. @@ -324,7 +324,11 @@
  26897. * The slab lists for all objects.
  26898. */
  26899. struct kmem_cache_node {
  26900. +#ifdef CONFIG_SLUB
  26901. + raw_spinlock_t list_lock;
  26902. +#else
  26903. spinlock_t list_lock;
  26904. +#endif
  26905. #ifdef CONFIG_SLAB
  26906. struct list_head slabs_partial; /* partial list first, better asm code */
  26907. diff -Nur linux-4.4.46.orig/mm/slub.c linux-4.4.46/mm/slub.c
  26908. --- linux-4.4.46.orig/mm/slub.c 2017-02-01 08:31:11.000000000 +0100
  26909. +++ linux-4.4.46/mm/slub.c 2017-02-03 17:18:10.951619984 +0100
  26910. @@ -1075,7 +1075,7 @@
  26911. void *object = head;
  26912. int cnt = 0;
  26913. - spin_lock_irqsave(&n->list_lock, *flags);
  26914. + raw_spin_lock_irqsave(&n->list_lock, *flags);
  26915. slab_lock(page);
  26916. if (!check_slab(s, page))
  26917. @@ -1136,7 +1136,7 @@
  26918. fail:
  26919. slab_unlock(page);
  26920. - spin_unlock_irqrestore(&n->list_lock, *flags);
  26921. + raw_spin_unlock_irqrestore(&n->list_lock, *flags);
  26922. slab_fix(s, "Object at 0x%p not freed", object);
  26923. return NULL;
  26924. }
  26925. @@ -1263,6 +1263,12 @@
  26926. #endif /* CONFIG_SLUB_DEBUG */
  26927. +struct slub_free_list {
  26928. + raw_spinlock_t lock;
  26929. + struct list_head list;
  26930. +};
  26931. +static DEFINE_PER_CPU(struct slub_free_list, slub_free_list);
  26932. +
  26933. /*
  26934. * Hooks for other subsystems that check memory allocations. In a typical
  26935. * production configuration these hooks all should produce no code at all.
  26936. @@ -1399,10 +1405,17 @@
  26937. gfp_t alloc_gfp;
  26938. void *start, *p;
  26939. int idx, order;
  26940. + bool enableirqs = false;
  26941. flags &= gfp_allowed_mask;
  26942. if (gfpflags_allow_blocking(flags))
  26943. + enableirqs = true;
  26944. +#ifdef CONFIG_PREEMPT_RT_FULL
  26945. + if (system_state == SYSTEM_RUNNING)
  26946. + enableirqs = true;
  26947. +#endif
  26948. + if (enableirqs)
  26949. local_irq_enable();
  26950. flags |= s->allocflags;
  26951. @@ -1473,7 +1486,7 @@
  26952. page->frozen = 1;
  26953. out:
  26954. - if (gfpflags_allow_blocking(flags))
  26955. + if (enableirqs)
  26956. local_irq_disable();
  26957. if (!page)
  26958. return NULL;
  26959. @@ -1529,6 +1542,16 @@
  26960. __free_kmem_pages(page, order);
  26961. }
  26962. +static void free_delayed(struct list_head *h)
  26963. +{
  26964. + while(!list_empty(h)) {
  26965. + struct page *page = list_first_entry(h, struct page, lru);
  26966. +
  26967. + list_del(&page->lru);
  26968. + __free_slab(page->slab_cache, page);
  26969. + }
  26970. +}
  26971. +
  26972. #define need_reserve_slab_rcu \
  26973. (sizeof(((struct page *)NULL)->lru) < sizeof(struct rcu_head))
  26974. @@ -1560,6 +1583,12 @@
  26975. }
  26976. call_rcu(head, rcu_free_slab);
  26977. + } else if (irqs_disabled()) {
  26978. + struct slub_free_list *f = this_cpu_ptr(&slub_free_list);
  26979. +
  26980. + raw_spin_lock(&f->lock);
  26981. + list_add(&page->lru, &f->list);
  26982. + raw_spin_unlock(&f->lock);
  26983. } else
  26984. __free_slab(s, page);
  26985. }
  26986. @@ -1673,7 +1702,7 @@
  26987. if (!n || !n->nr_partial)
  26988. return NULL;
  26989. - spin_lock(&n->list_lock);
  26990. + raw_spin_lock(&n->list_lock);
  26991. list_for_each_entry_safe(page, page2, &n->partial, lru) {
  26992. void *t;
  26993. @@ -1698,7 +1727,7 @@
  26994. break;
  26995. }
  26996. - spin_unlock(&n->list_lock);
  26997. + raw_spin_unlock(&n->list_lock);
  26998. return object;
  26999. }
  27000. @@ -1944,7 +1973,7 @@
  27001. * that acquire_slab() will see a slab page that
  27002. * is frozen
  27003. */
  27004. - spin_lock(&n->list_lock);
  27005. + raw_spin_lock(&n->list_lock);
  27006. }
  27007. } else {
  27008. m = M_FULL;
  27009. @@ -1955,7 +1984,7 @@
  27010. * slabs from diagnostic functions will not see
  27011. * any frozen slabs.
  27012. */
  27013. - spin_lock(&n->list_lock);
  27014. + raw_spin_lock(&n->list_lock);
  27015. }
  27016. }
  27017. @@ -1990,7 +2019,7 @@
  27018. goto redo;
  27019. if (lock)
  27020. - spin_unlock(&n->list_lock);
  27021. + raw_spin_unlock(&n->list_lock);
  27022. if (m == M_FREE) {
  27023. stat(s, DEACTIVATE_EMPTY);
  27024. @@ -2022,10 +2051,10 @@
  27025. n2 = get_node(s, page_to_nid(page));
  27026. if (n != n2) {
  27027. if (n)
  27028. - spin_unlock(&n->list_lock);
  27029. + raw_spin_unlock(&n->list_lock);
  27030. n = n2;
  27031. - spin_lock(&n->list_lock);
  27032. + raw_spin_lock(&n->list_lock);
  27033. }
  27034. do {
  27035. @@ -2054,7 +2083,7 @@
  27036. }
  27037. if (n)
  27038. - spin_unlock(&n->list_lock);
  27039. + raw_spin_unlock(&n->list_lock);
  27040. while (discard_page) {
  27041. page = discard_page;
  27042. @@ -2093,14 +2122,21 @@
  27043. pobjects = oldpage->pobjects;
  27044. pages = oldpage->pages;
  27045. if (drain && pobjects > s->cpu_partial) {
  27046. + struct slub_free_list *f;
  27047. unsigned long flags;
  27048. + LIST_HEAD(tofree);
  27049. /*
  27050. * partial array is full. Move the existing
  27051. * set to the per node partial list.
  27052. */
  27053. local_irq_save(flags);
  27054. unfreeze_partials(s, this_cpu_ptr(s->cpu_slab));
  27055. + f = this_cpu_ptr(&slub_free_list);
  27056. + raw_spin_lock(&f->lock);
  27057. + list_splice_init(&f->list, &tofree);
  27058. + raw_spin_unlock(&f->lock);
  27059. local_irq_restore(flags);
  27060. + free_delayed(&tofree);
  27061. oldpage = NULL;
  27062. pobjects = 0;
  27063. pages = 0;
  27064. @@ -2172,7 +2208,22 @@
  27065. static void flush_all(struct kmem_cache *s)
  27066. {
  27067. + LIST_HEAD(tofree);
  27068. + int cpu;
  27069. +
  27070. on_each_cpu_cond(has_cpu_slab, flush_cpu_slab, s, 1, GFP_ATOMIC);
  27071. + for_each_online_cpu(cpu) {
  27072. + struct slub_free_list *f;
  27073. +
  27074. + if (!has_cpu_slab(cpu, s))
  27075. + continue;
  27076. +
  27077. + f = &per_cpu(slub_free_list, cpu);
  27078. + raw_spin_lock_irq(&f->lock);
  27079. + list_splice_init(&f->list, &tofree);
  27080. + raw_spin_unlock_irq(&f->lock);
  27081. + free_delayed(&tofree);
  27082. + }
  27083. }
  27084. /*
  27085. @@ -2208,10 +2259,10 @@
  27086. unsigned long x = 0;
  27087. struct page *page;
  27088. - spin_lock_irqsave(&n->list_lock, flags);
  27089. + raw_spin_lock_irqsave(&n->list_lock, flags);
  27090. list_for_each_entry(page, &n->partial, lru)
  27091. x += get_count(page);
  27092. - spin_unlock_irqrestore(&n->list_lock, flags);
  27093. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  27094. return x;
  27095. }
  27096. #endif /* CONFIG_SLUB_DEBUG || CONFIG_SYSFS */
  27097. @@ -2349,8 +2400,10 @@
  27098. * already disabled (which is the case for bulk allocation).
  27099. */
  27100. static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
  27101. - unsigned long addr, struct kmem_cache_cpu *c)
  27102. + unsigned long addr, struct kmem_cache_cpu *c,
  27103. + struct list_head *to_free)
  27104. {
  27105. + struct slub_free_list *f;
  27106. void *freelist;
  27107. struct page *page;
  27108. @@ -2410,6 +2463,13 @@
  27109. VM_BUG_ON(!c->page->frozen);
  27110. c->freelist = get_freepointer(s, freelist);
  27111. c->tid = next_tid(c->tid);
  27112. +
  27113. +out:
  27114. + f = this_cpu_ptr(&slub_free_list);
  27115. + raw_spin_lock(&f->lock);
  27116. + list_splice_init(&f->list, to_free);
  27117. + raw_spin_unlock(&f->lock);
  27118. +
  27119. return freelist;
  27120. new_slab:
  27121. @@ -2441,7 +2501,7 @@
  27122. deactivate_slab(s, page, get_freepointer(s, freelist));
  27123. c->page = NULL;
  27124. c->freelist = NULL;
  27125. - return freelist;
  27126. + goto out;
  27127. }
  27128. /*
  27129. @@ -2453,6 +2513,7 @@
  27130. {
  27131. void *p;
  27132. unsigned long flags;
  27133. + LIST_HEAD(tofree);
  27134. local_irq_save(flags);
  27135. #ifdef CONFIG_PREEMPT
  27136. @@ -2464,8 +2525,9 @@
  27137. c = this_cpu_ptr(s->cpu_slab);
  27138. #endif
  27139. - p = ___slab_alloc(s, gfpflags, node, addr, c);
  27140. + p = ___slab_alloc(s, gfpflags, node, addr, c, &tofree);
  27141. local_irq_restore(flags);
  27142. + free_delayed(&tofree);
  27143. return p;
  27144. }
  27145. @@ -2652,7 +2714,7 @@
  27146. do {
  27147. if (unlikely(n)) {
  27148. - spin_unlock_irqrestore(&n->list_lock, flags);
  27149. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  27150. n = NULL;
  27151. }
  27152. prior = page->freelist;
  27153. @@ -2684,7 +2746,7 @@
  27154. * Otherwise the list_lock will synchronize with
  27155. * other processors updating the list of slabs.
  27156. */
  27157. - spin_lock_irqsave(&n->list_lock, flags);
  27158. + raw_spin_lock_irqsave(&n->list_lock, flags);
  27159. }
  27160. }
  27161. @@ -2726,7 +2788,7 @@
  27162. add_partial(n, page, DEACTIVATE_TO_TAIL);
  27163. stat(s, FREE_ADD_PARTIAL);
  27164. }
  27165. - spin_unlock_irqrestore(&n->list_lock, flags);
  27166. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  27167. return;
  27168. slab_empty:
  27169. @@ -2741,7 +2803,7 @@
  27170. remove_full(s, n, page);
  27171. }
  27172. - spin_unlock_irqrestore(&n->list_lock, flags);
  27173. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  27174. stat(s, FREE_SLAB);
  27175. discard_slab(s, page);
  27176. }
  27177. @@ -2913,6 +2975,7 @@
  27178. void **p)
  27179. {
  27180. struct kmem_cache_cpu *c;
  27181. + LIST_HEAD(to_free);
  27182. int i;
  27183. /* memcg and kmem_cache debug support */
  27184. @@ -2936,7 +2999,7 @@
  27185. * of re-populating per CPU c->freelist
  27186. */
  27187. p[i] = ___slab_alloc(s, flags, NUMA_NO_NODE,
  27188. - _RET_IP_, c);
  27189. + _RET_IP_, c, &to_free);
  27190. if (unlikely(!p[i]))
  27191. goto error;
  27192. @@ -2948,6 +3011,7 @@
  27193. }
  27194. c->tid = next_tid(c->tid);
  27195. local_irq_enable();
  27196. + free_delayed(&to_free);
  27197. /* Clear memory outside IRQ disabled fastpath loop */
  27198. if (unlikely(flags & __GFP_ZERO)) {
  27199. @@ -3095,7 +3159,7 @@
  27200. init_kmem_cache_node(struct kmem_cache_node *n)
  27201. {
  27202. n->nr_partial = 0;
  27203. - spin_lock_init(&n->list_lock);
  27204. + raw_spin_lock_init(&n->list_lock);
  27205. INIT_LIST_HEAD(&n->partial);
  27206. #ifdef CONFIG_SLUB_DEBUG
  27207. atomic_long_set(&n->nr_slabs, 0);
  27208. @@ -3677,7 +3741,7 @@
  27209. for (i = 0; i < SHRINK_PROMOTE_MAX; i++)
  27210. INIT_LIST_HEAD(promote + i);
  27211. - spin_lock_irqsave(&n->list_lock, flags);
  27212. + raw_spin_lock_irqsave(&n->list_lock, flags);
  27213. /*
  27214. * Build lists of slabs to discard or promote.
  27215. @@ -3708,7 +3772,7 @@
  27216. for (i = SHRINK_PROMOTE_MAX - 1; i >= 0; i--)
  27217. list_splice(promote + i, &n->partial);
  27218. - spin_unlock_irqrestore(&n->list_lock, flags);
  27219. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  27220. /* Release empty slabs */
  27221. list_for_each_entry_safe(page, t, &discard, lru)
  27222. @@ -3884,6 +3948,12 @@
  27223. {
  27224. static __initdata struct kmem_cache boot_kmem_cache,
  27225. boot_kmem_cache_node;
  27226. + int cpu;
  27227. +
  27228. + for_each_possible_cpu(cpu) {
  27229. + raw_spin_lock_init(&per_cpu(slub_free_list, cpu).lock);
  27230. + INIT_LIST_HEAD(&per_cpu(slub_free_list, cpu).list);
  27231. + }
  27232. if (debug_guardpage_minorder())
  27233. slub_max_order = 0;
  27234. @@ -4127,7 +4197,7 @@
  27235. struct page *page;
  27236. unsigned long flags;
  27237. - spin_lock_irqsave(&n->list_lock, flags);
  27238. + raw_spin_lock_irqsave(&n->list_lock, flags);
  27239. list_for_each_entry(page, &n->partial, lru) {
  27240. validate_slab_slab(s, page, map);
  27241. @@ -4149,7 +4219,7 @@
  27242. s->name, count, atomic_long_read(&n->nr_slabs));
  27243. out:
  27244. - spin_unlock_irqrestore(&n->list_lock, flags);
  27245. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  27246. return count;
  27247. }
  27248. @@ -4337,12 +4407,12 @@
  27249. if (!atomic_long_read(&n->nr_slabs))
  27250. continue;
  27251. - spin_lock_irqsave(&n->list_lock, flags);
  27252. + raw_spin_lock_irqsave(&n->list_lock, flags);
  27253. list_for_each_entry(page, &n->partial, lru)
  27254. process_slab(&t, s, page, alloc, map);
  27255. list_for_each_entry(page, &n->full, lru)
  27256. process_slab(&t, s, page, alloc, map);
  27257. - spin_unlock_irqrestore(&n->list_lock, flags);
  27258. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  27259. }
  27260. for (i = 0; i < t.count; i++) {
  27261. diff -Nur linux-4.4.46.orig/mm/swap.c linux-4.4.46/mm/swap.c
  27262. --- linux-4.4.46.orig/mm/swap.c 2017-02-01 08:31:11.000000000 +0100
  27263. +++ linux-4.4.46/mm/swap.c 2017-02-03 17:18:10.951619984 +0100
  27264. @@ -31,6 +31,7 @@
  27265. #include <linux/memcontrol.h>
  27266. #include <linux/gfp.h>
  27267. #include <linux/uio.h>
  27268. +#include <linux/locallock.h>
  27269. #include <linux/hugetlb.h>
  27270. #include <linux/page_idle.h>
  27271. @@ -46,6 +47,9 @@
  27272. static DEFINE_PER_CPU(struct pagevec, lru_rotate_pvecs);
  27273. static DEFINE_PER_CPU(struct pagevec, lru_deactivate_file_pvecs);
  27274. +static DEFINE_LOCAL_IRQ_LOCK(rotate_lock);
  27275. +DEFINE_LOCAL_IRQ_LOCK(swapvec_lock);
  27276. +
  27277. /*
  27278. * This path almost never happens for VM activity - pages are normally
  27279. * freed via pagevecs. But it gets used by networking.
  27280. @@ -481,11 +485,11 @@
  27281. unsigned long flags;
  27282. page_cache_get(page);
  27283. - local_irq_save(flags);
  27284. + local_lock_irqsave(rotate_lock, flags);
  27285. pvec = this_cpu_ptr(&lru_rotate_pvecs);
  27286. if (!pagevec_add(pvec, page))
  27287. pagevec_move_tail(pvec);
  27288. - local_irq_restore(flags);
  27289. + local_unlock_irqrestore(rotate_lock, flags);
  27290. }
  27291. }
  27292. @@ -536,12 +540,13 @@
  27293. void activate_page(struct page *page)
  27294. {
  27295. if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
  27296. - struct pagevec *pvec = &get_cpu_var(activate_page_pvecs);
  27297. + struct pagevec *pvec = &get_locked_var(swapvec_lock,
  27298. + activate_page_pvecs);
  27299. page_cache_get(page);
  27300. if (!pagevec_add(pvec, page))
  27301. pagevec_lru_move_fn(pvec, __activate_page, NULL);
  27302. - put_cpu_var(activate_page_pvecs);
  27303. + put_locked_var(swapvec_lock, activate_page_pvecs);
  27304. }
  27305. }
  27306. @@ -567,7 +572,7 @@
  27307. static void __lru_cache_activate_page(struct page *page)
  27308. {
  27309. - struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
  27310. + struct pagevec *pvec = &get_locked_var(swapvec_lock, lru_add_pvec);
  27311. int i;
  27312. /*
  27313. @@ -589,7 +594,7 @@
  27314. }
  27315. }
  27316. - put_cpu_var(lru_add_pvec);
  27317. + put_locked_var(swapvec_lock, lru_add_pvec);
  27318. }
  27319. /*
  27320. @@ -630,13 +635,13 @@
  27321. static void __lru_cache_add(struct page *page)
  27322. {
  27323. - struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
  27324. + struct pagevec *pvec = &get_locked_var(swapvec_lock, lru_add_pvec);
  27325. page_cache_get(page);
  27326. if (!pagevec_space(pvec))
  27327. __pagevec_lru_add(pvec);
  27328. pagevec_add(pvec, page);
  27329. - put_cpu_var(lru_add_pvec);
  27330. + put_locked_var(swapvec_lock, lru_add_pvec);
  27331. }
  27332. /**
  27333. @@ -816,9 +821,15 @@
  27334. unsigned long flags;
  27335. /* No harm done if a racing interrupt already did this */
  27336. - local_irq_save(flags);
  27337. +#ifdef CONFIG_PREEMPT_RT_BASE
  27338. + local_lock_irqsave_on(rotate_lock, flags, cpu);
  27339. + pagevec_move_tail(pvec);
  27340. + local_unlock_irqrestore_on(rotate_lock, flags, cpu);
  27341. +#else
  27342. + local_lock_irqsave(rotate_lock, flags);
  27343. pagevec_move_tail(pvec);
  27344. - local_irq_restore(flags);
  27345. + local_unlock_irqrestore(rotate_lock, flags);
  27346. +#endif
  27347. }
  27348. pvec = &per_cpu(lru_deactivate_file_pvecs, cpu);
  27349. @@ -846,26 +857,47 @@
  27350. return;
  27351. if (likely(get_page_unless_zero(page))) {
  27352. - struct pagevec *pvec = &get_cpu_var(lru_deactivate_file_pvecs);
  27353. + struct pagevec *pvec = &get_locked_var(swapvec_lock,
  27354. + lru_deactivate_file_pvecs);
  27355. if (!pagevec_add(pvec, page))
  27356. pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
  27357. - put_cpu_var(lru_deactivate_file_pvecs);
  27358. + put_locked_var(swapvec_lock, lru_deactivate_file_pvecs);
  27359. }
  27360. }
  27361. void lru_add_drain(void)
  27362. {
  27363. - lru_add_drain_cpu(get_cpu());
  27364. - put_cpu();
  27365. + lru_add_drain_cpu(local_lock_cpu(swapvec_lock));
  27366. + local_unlock_cpu(swapvec_lock);
  27367. }
  27368. +
  27369. +#ifdef CONFIG_PREEMPT_RT_BASE
  27370. +static inline void remote_lru_add_drain(int cpu, struct cpumask *has_work)
  27371. +{
  27372. + local_lock_on(swapvec_lock, cpu);
  27373. + lru_add_drain_cpu(cpu);
  27374. + local_unlock_on(swapvec_lock, cpu);
  27375. +}
  27376. +
  27377. +#else
  27378. +
  27379. static void lru_add_drain_per_cpu(struct work_struct *dummy)
  27380. {
  27381. lru_add_drain();
  27382. }
  27383. static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);
  27384. +static inline void remote_lru_add_drain(int cpu, struct cpumask *has_work)
  27385. +{
  27386. + struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
  27387. +
  27388. + INIT_WORK(work, lru_add_drain_per_cpu);
  27389. + schedule_work_on(cpu, work);
  27390. + cpumask_set_cpu(cpu, has_work);
  27391. +}
  27392. +#endif
  27393. void lru_add_drain_all(void)
  27394. {
  27395. @@ -878,20 +910,17 @@
  27396. cpumask_clear(&has_work);
  27397. for_each_online_cpu(cpu) {
  27398. - struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
  27399. -
  27400. if (pagevec_count(&per_cpu(lru_add_pvec, cpu)) ||
  27401. pagevec_count(&per_cpu(lru_rotate_pvecs, cpu)) ||
  27402. pagevec_count(&per_cpu(lru_deactivate_file_pvecs, cpu)) ||
  27403. - need_activate_page_drain(cpu)) {
  27404. - INIT_WORK(work, lru_add_drain_per_cpu);
  27405. - schedule_work_on(cpu, work);
  27406. - cpumask_set_cpu(cpu, &has_work);
  27407. - }
  27408. + need_activate_page_drain(cpu))
  27409. + remote_lru_add_drain(cpu, &has_work);
  27410. }
  27411. +#ifndef CONFIG_PREEMPT_RT_BASE
  27412. for_each_cpu(cpu, &has_work)
  27413. flush_work(&per_cpu(lru_add_drain_work, cpu));
  27414. +#endif
  27415. put_online_cpus();
  27416. mutex_unlock(&lock);
  27417. diff -Nur linux-4.4.46.orig/mm/truncate.c linux-4.4.46/mm/truncate.c
  27418. --- linux-4.4.46.orig/mm/truncate.c 2017-02-01 08:31:11.000000000 +0100
  27419. +++ linux-4.4.46/mm/truncate.c 2017-02-03 17:18:10.951619984 +0100
  27420. @@ -56,8 +56,11 @@
  27421. * protected by mapping->tree_lock.
  27422. */
  27423. if (!workingset_node_shadows(node) &&
  27424. - !list_empty(&node->private_list))
  27425. - list_lru_del(&workingset_shadow_nodes, &node->private_list);
  27426. + !list_empty(&node->private_list)) {
  27427. + local_lock(workingset_shadow_lock);
  27428. + list_lru_del(&__workingset_shadow_nodes, &node->private_list);
  27429. + local_unlock(workingset_shadow_lock);
  27430. + }
  27431. __radix_tree_delete_node(&mapping->page_tree, node);
  27432. unlock:
  27433. spin_unlock_irq(&mapping->tree_lock);
  27434. diff -Nur linux-4.4.46.orig/mm/vmalloc.c linux-4.4.46/mm/vmalloc.c
  27435. --- linux-4.4.46.orig/mm/vmalloc.c 2017-02-01 08:31:11.000000000 +0100
  27436. +++ linux-4.4.46/mm/vmalloc.c 2017-02-03 17:18:10.951619984 +0100
  27437. @@ -821,7 +821,7 @@
  27438. struct vmap_block *vb;
  27439. struct vmap_area *va;
  27440. unsigned long vb_idx;
  27441. - int node, err;
  27442. + int node, err, cpu;
  27443. void *vaddr;
  27444. node = numa_node_id();
  27445. @@ -864,11 +864,12 @@
  27446. BUG_ON(err);
  27447. radix_tree_preload_end();
  27448. - vbq = &get_cpu_var(vmap_block_queue);
  27449. + cpu = get_cpu_light();
  27450. + vbq = this_cpu_ptr(&vmap_block_queue);
  27451. spin_lock(&vbq->lock);
  27452. list_add_tail_rcu(&vb->free_list, &vbq->free);
  27453. spin_unlock(&vbq->lock);
  27454. - put_cpu_var(vmap_block_queue);
  27455. + put_cpu_light();
  27456. return vaddr;
  27457. }
  27458. @@ -937,6 +938,7 @@
  27459. struct vmap_block *vb;
  27460. void *vaddr = NULL;
  27461. unsigned int order;
  27462. + int cpu;
  27463. BUG_ON(offset_in_page(size));
  27464. BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
  27465. @@ -951,7 +953,8 @@
  27466. order = get_order(size);
  27467. rcu_read_lock();
  27468. - vbq = &get_cpu_var(vmap_block_queue);
  27469. + cpu = get_cpu_light();
  27470. + vbq = this_cpu_ptr(&vmap_block_queue);
  27471. list_for_each_entry_rcu(vb, &vbq->free, free_list) {
  27472. unsigned long pages_off;
  27473. @@ -974,7 +977,7 @@
  27474. break;
  27475. }
  27476. - put_cpu_var(vmap_block_queue);
  27477. + put_cpu_light();
  27478. rcu_read_unlock();
  27479. /* Allocate new block if nothing was found */
  27480. diff -Nur linux-4.4.46.orig/mm/vmstat.c linux-4.4.46/mm/vmstat.c
  27481. --- linux-4.4.46.orig/mm/vmstat.c 2017-02-01 08:31:11.000000000 +0100
  27482. +++ linux-4.4.46/mm/vmstat.c 2017-02-03 17:18:10.951619984 +0100
  27483. @@ -226,6 +226,7 @@
  27484. long x;
  27485. long t;
  27486. + preempt_disable_rt();
  27487. x = delta + __this_cpu_read(*p);
  27488. t = __this_cpu_read(pcp->stat_threshold);
  27489. @@ -235,6 +236,7 @@
  27490. x = 0;
  27491. }
  27492. __this_cpu_write(*p, x);
  27493. + preempt_enable_rt();
  27494. }
  27495. EXPORT_SYMBOL(__mod_zone_page_state);
  27496. @@ -267,6 +269,7 @@
  27497. s8 __percpu *p = pcp->vm_stat_diff + item;
  27498. s8 v, t;
  27499. + preempt_disable_rt();
  27500. v = __this_cpu_inc_return(*p);
  27501. t = __this_cpu_read(pcp->stat_threshold);
  27502. if (unlikely(v > t)) {
  27503. @@ -275,6 +278,7 @@
  27504. zone_page_state_add(v + overstep, zone, item);
  27505. __this_cpu_write(*p, -overstep);
  27506. }
  27507. + preempt_enable_rt();
  27508. }
  27509. void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
  27510. @@ -289,6 +293,7 @@
  27511. s8 __percpu *p = pcp->vm_stat_diff + item;
  27512. s8 v, t;
  27513. + preempt_disable_rt();
  27514. v = __this_cpu_dec_return(*p);
  27515. t = __this_cpu_read(pcp->stat_threshold);
  27516. if (unlikely(v < - t)) {
  27517. @@ -297,6 +302,7 @@
  27518. zone_page_state_add(v - overstep, zone, item);
  27519. __this_cpu_write(*p, overstep);
  27520. }
  27521. + preempt_enable_rt();
  27522. }
  27523. void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
  27524. diff -Nur linux-4.4.46.orig/mm/workingset.c linux-4.4.46/mm/workingset.c
  27525. --- linux-4.4.46.orig/mm/workingset.c 2017-02-01 08:31:11.000000000 +0100
  27526. +++ linux-4.4.46/mm/workingset.c 2017-02-03 17:18:10.951619984 +0100
  27527. @@ -264,7 +264,8 @@
  27528. * point where they would still be useful.
  27529. */
  27530. -struct list_lru workingset_shadow_nodes;
  27531. +struct list_lru __workingset_shadow_nodes;
  27532. +DEFINE_LOCAL_IRQ_LOCK(workingset_shadow_lock);
  27533. static unsigned long count_shadow_nodes(struct shrinker *shrinker,
  27534. struct shrink_control *sc)
  27535. @@ -274,9 +275,9 @@
  27536. unsigned long pages;
  27537. /* list_lru lock nests inside IRQ-safe mapping->tree_lock */
  27538. - local_irq_disable();
  27539. - shadow_nodes = list_lru_shrink_count(&workingset_shadow_nodes, sc);
  27540. - local_irq_enable();
  27541. + local_lock_irq(workingset_shadow_lock);
  27542. + shadow_nodes = list_lru_shrink_count(&__workingset_shadow_nodes, sc);
  27543. + local_unlock_irq(workingset_shadow_lock);
  27544. pages = node_present_pages(sc->nid);
  27545. /*
  27546. @@ -361,9 +362,9 @@
  27547. spin_unlock(&mapping->tree_lock);
  27548. ret = LRU_REMOVED_RETRY;
  27549. out:
  27550. - local_irq_enable();
  27551. + local_unlock_irq(workingset_shadow_lock);
  27552. cond_resched();
  27553. - local_irq_disable();
  27554. + local_lock_irq(workingset_shadow_lock);
  27555. spin_lock(lru_lock);
  27556. return ret;
  27557. }
  27558. @@ -374,10 +375,10 @@
  27559. unsigned long ret;
  27560. /* list_lru lock nests inside IRQ-safe mapping->tree_lock */
  27561. - local_irq_disable();
  27562. - ret = list_lru_shrink_walk(&workingset_shadow_nodes, sc,
  27563. + local_lock_irq(workingset_shadow_lock);
  27564. + ret = list_lru_shrink_walk(&__workingset_shadow_nodes, sc,
  27565. shadow_lru_isolate, NULL);
  27566. - local_irq_enable();
  27567. + local_unlock_irq(workingset_shadow_lock);
  27568. return ret;
  27569. }
  27570. @@ -398,7 +399,7 @@
  27571. {
  27572. int ret;
  27573. - ret = list_lru_init_key(&workingset_shadow_nodes, &shadow_nodes_key);
  27574. + ret = list_lru_init_key(&__workingset_shadow_nodes, &shadow_nodes_key);
  27575. if (ret)
  27576. goto err;
  27577. ret = register_shrinker(&workingset_shadow_shrinker);
  27578. @@ -406,7 +407,7 @@
  27579. goto err_list_lru;
  27580. return 0;
  27581. err_list_lru:
  27582. - list_lru_destroy(&workingset_shadow_nodes);
  27583. + list_lru_destroy(&__workingset_shadow_nodes);
  27584. err:
  27585. return ret;
  27586. }
  27587. diff -Nur linux-4.4.46.orig/mm/zsmalloc.c linux-4.4.46/mm/zsmalloc.c
  27588. --- linux-4.4.46.orig/mm/zsmalloc.c 2017-02-01 08:31:11.000000000 +0100
  27589. +++ linux-4.4.46/mm/zsmalloc.c 2017-02-03 17:18:10.951619984 +0100
  27590. @@ -64,6 +64,7 @@
  27591. #include <linux/debugfs.h>
  27592. #include <linux/zsmalloc.h>
  27593. #include <linux/zpool.h>
  27594. +#include <linux/locallock.h>
  27595. /*
  27596. * This must be power of 2 and greater than of equal to sizeof(link_free).
  27597. @@ -403,6 +404,7 @@
  27598. /* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
  27599. static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
  27600. +static DEFINE_LOCAL_IRQ_LOCK(zs_map_area_lock);
  27601. static int is_first_page(struct page *page)
  27602. {
  27603. @@ -1289,7 +1291,7 @@
  27604. class = pool->size_class[class_idx];
  27605. off = obj_idx_to_offset(page, obj_idx, class->size);
  27606. - area = &get_cpu_var(zs_map_area);
  27607. + area = &get_locked_var(zs_map_area_lock, zs_map_area);
  27608. area->vm_mm = mm;
  27609. if (off + class->size <= PAGE_SIZE) {
  27610. /* this object is contained entirely within a page */
  27611. @@ -1342,7 +1344,7 @@
  27612. __zs_unmap_object(area, pages, off, class->size);
  27613. }
  27614. - put_cpu_var(zs_map_area);
  27615. + put_locked_var(zs_map_area_lock, zs_map_area);
  27616. unpin_tag(handle);
  27617. }
  27618. EXPORT_SYMBOL_GPL(zs_unmap_object);
  27619. diff -Nur linux-4.4.46.orig/net/core/dev.c linux-4.4.46/net/core/dev.c
  27620. --- linux-4.4.46.orig/net/core/dev.c 2017-02-01 08:31:11.000000000 +0100
  27621. +++ linux-4.4.46/net/core/dev.c 2017-02-03 17:18:10.955620139 +0100
  27622. @@ -186,6 +186,7 @@
  27623. static DEFINE_HASHTABLE(napi_hash, 8);
  27624. static seqcount_t devnet_rename_seq;
  27625. +static DEFINE_MUTEX(devnet_rename_mutex);
  27626. static inline void dev_base_seq_inc(struct net *net)
  27627. {
  27628. @@ -207,14 +208,14 @@
  27629. static inline void rps_lock(struct softnet_data *sd)
  27630. {
  27631. #ifdef CONFIG_RPS
  27632. - spin_lock(&sd->input_pkt_queue.lock);
  27633. + raw_spin_lock(&sd->input_pkt_queue.raw_lock);
  27634. #endif
  27635. }
  27636. static inline void rps_unlock(struct softnet_data *sd)
  27637. {
  27638. #ifdef CONFIG_RPS
  27639. - spin_unlock(&sd->input_pkt_queue.lock);
  27640. + raw_spin_unlock(&sd->input_pkt_queue.raw_lock);
  27641. #endif
  27642. }
  27643. @@ -884,7 +885,8 @@
  27644. strcpy(name, dev->name);
  27645. rcu_read_unlock();
  27646. if (read_seqcount_retry(&devnet_rename_seq, seq)) {
  27647. - cond_resched();
  27648. + mutex_lock(&devnet_rename_mutex);
  27649. + mutex_unlock(&devnet_rename_mutex);
  27650. goto retry;
  27651. }
  27652. @@ -1153,20 +1155,17 @@
  27653. if (dev->flags & IFF_UP)
  27654. return -EBUSY;
  27655. - write_seqcount_begin(&devnet_rename_seq);
  27656. + mutex_lock(&devnet_rename_mutex);
  27657. + __raw_write_seqcount_begin(&devnet_rename_seq);
  27658. - if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
  27659. - write_seqcount_end(&devnet_rename_seq);
  27660. - return 0;
  27661. - }
  27662. + if (strncmp(newname, dev->name, IFNAMSIZ) == 0)
  27663. + goto outunlock;
  27664. memcpy(oldname, dev->name, IFNAMSIZ);
  27665. err = dev_get_valid_name(net, dev, newname);
  27666. - if (err < 0) {
  27667. - write_seqcount_end(&devnet_rename_seq);
  27668. - return err;
  27669. - }
  27670. + if (err < 0)
  27671. + goto outunlock;
  27672. if (oldname[0] && !strchr(oldname, '%'))
  27673. netdev_info(dev, "renamed from %s\n", oldname);
  27674. @@ -1179,11 +1178,12 @@
  27675. if (ret) {
  27676. memcpy(dev->name, oldname, IFNAMSIZ);
  27677. dev->name_assign_type = old_assign_type;
  27678. - write_seqcount_end(&devnet_rename_seq);
  27679. - return ret;
  27680. + err = ret;
  27681. + goto outunlock;
  27682. }
  27683. - write_seqcount_end(&devnet_rename_seq);
  27684. + __raw_write_seqcount_end(&devnet_rename_seq);
  27685. + mutex_unlock(&devnet_rename_mutex);
  27686. netdev_adjacent_rename_links(dev, oldname);
  27687. @@ -1204,7 +1204,8 @@
  27688. /* err >= 0 after dev_alloc_name() or stores the first errno */
  27689. if (err >= 0) {
  27690. err = ret;
  27691. - write_seqcount_begin(&devnet_rename_seq);
  27692. + mutex_lock(&devnet_rename_mutex);
  27693. + __raw_write_seqcount_begin(&devnet_rename_seq);
  27694. memcpy(dev->name, oldname, IFNAMSIZ);
  27695. memcpy(oldname, newname, IFNAMSIZ);
  27696. dev->name_assign_type = old_assign_type;
  27697. @@ -1217,6 +1218,11 @@
  27698. }
  27699. return err;
  27700. +
  27701. +outunlock:
  27702. + __raw_write_seqcount_end(&devnet_rename_seq);
  27703. + mutex_unlock(&devnet_rename_mutex);
  27704. + return err;
  27705. }
  27706. /**
  27707. @@ -2246,6 +2252,7 @@
  27708. sd->output_queue_tailp = &q->next_sched;
  27709. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  27710. local_irq_restore(flags);
  27711. + preempt_check_resched_rt();
  27712. }
  27713. void __netif_schedule(struct Qdisc *q)
  27714. @@ -2327,6 +2334,7 @@
  27715. __this_cpu_write(softnet_data.completion_queue, skb);
  27716. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  27717. local_irq_restore(flags);
  27718. + preempt_check_resched_rt();
  27719. }
  27720. EXPORT_SYMBOL(__dev_kfree_skb_irq);
  27721. @@ -2884,7 +2892,11 @@
  27722. * This permits __QDISC___STATE_RUNNING owner to get the lock more
  27723. * often and dequeue packets faster.
  27724. */
  27725. +#ifdef CONFIG_PREEMPT_RT_FULL
  27726. + contended = true;
  27727. +#else
  27728. contended = qdisc_is_running(q);
  27729. +#endif
  27730. if (unlikely(contended))
  27731. spin_lock(&q->busylock);
  27732. @@ -2944,9 +2956,44 @@
  27733. #define skb_update_prio(skb)
  27734. #endif
  27735. +#ifdef CONFIG_PREEMPT_RT_FULL
  27736. +
  27737. +static inline int xmit_rec_read(void)
  27738. +{
  27739. + return current->xmit_recursion;
  27740. +}
  27741. +
  27742. +static inline void xmit_rec_inc(void)
  27743. +{
  27744. + current->xmit_recursion++;
  27745. +}
  27746. +
  27747. +static inline void xmit_rec_dec(void)
  27748. +{
  27749. + current->xmit_recursion--;
  27750. +}
  27751. +
  27752. +#else
  27753. +
  27754. DEFINE_PER_CPU(int, xmit_recursion);
  27755. EXPORT_SYMBOL(xmit_recursion);
  27756. +static inline int xmit_rec_read(void)
  27757. +{
  27758. + return __this_cpu_read(xmit_recursion);
  27759. +}
  27760. +
  27761. +static inline void xmit_rec_inc(void)
  27762. +{
  27763. + __this_cpu_inc(xmit_recursion);
  27764. +}
  27765. +
  27766. +static inline void xmit_rec_dec(void)
  27767. +{
  27768. + __this_cpu_dec(xmit_recursion);
  27769. +}
  27770. +#endif
  27771. +
  27772. #define RECURSION_LIMIT 10
  27773. /**
  27774. @@ -3139,7 +3186,7 @@
  27775. if (txq->xmit_lock_owner != cpu) {
  27776. - if (__this_cpu_read(xmit_recursion) > RECURSION_LIMIT)
  27777. + if (xmit_rec_read() > RECURSION_LIMIT)
  27778. goto recursion_alert;
  27779. skb = validate_xmit_skb(skb, dev);
  27780. @@ -3149,9 +3196,9 @@
  27781. HARD_TX_LOCK(dev, txq, cpu);
  27782. if (!netif_xmit_stopped(txq)) {
  27783. - __this_cpu_inc(xmit_recursion);
  27784. + xmit_rec_inc();
  27785. skb = dev_hard_start_xmit(skb, dev, txq, &rc);
  27786. - __this_cpu_dec(xmit_recursion);
  27787. + xmit_rec_dec();
  27788. if (dev_xmit_complete(rc)) {
  27789. HARD_TX_UNLOCK(dev, txq);
  27790. goto out;
  27791. @@ -3525,6 +3572,7 @@
  27792. rps_unlock(sd);
  27793. local_irq_restore(flags);
  27794. + preempt_check_resched_rt();
  27795. atomic_long_inc(&skb->dev->rx_dropped);
  27796. kfree_skb(skb);
  27797. @@ -3543,7 +3591,7 @@
  27798. struct rps_dev_flow voidflow, *rflow = &voidflow;
  27799. int cpu;
  27800. - preempt_disable();
  27801. + migrate_disable();
  27802. rcu_read_lock();
  27803. cpu = get_rps_cpu(skb->dev, skb, &rflow);
  27804. @@ -3553,13 +3601,13 @@
  27805. ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
  27806. rcu_read_unlock();
  27807. - preempt_enable();
  27808. + migrate_enable();
  27809. } else
  27810. #endif
  27811. {
  27812. unsigned int qtail;
  27813. - ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
  27814. - put_cpu();
  27815. + ret = enqueue_to_backlog(skb, get_cpu_light(), &qtail);
  27816. + put_cpu_light();
  27817. }
  27818. return ret;
  27819. }
  27820. @@ -3593,16 +3641,44 @@
  27821. trace_netif_rx_ni_entry(skb);
  27822. - preempt_disable();
  27823. + local_bh_disable();
  27824. err = netif_rx_internal(skb);
  27825. - if (local_softirq_pending())
  27826. - do_softirq();
  27827. - preempt_enable();
  27828. + local_bh_enable();
  27829. return err;
  27830. }
  27831. EXPORT_SYMBOL(netif_rx_ni);
  27832. +#ifdef CONFIG_PREEMPT_RT_FULL
  27833. +/*
  27834. + * RT runs ksoftirqd as a real time thread and the root_lock is a
  27835. + * "sleeping spinlock". If the trylock fails then we can go into an
  27836. + * infinite loop when ksoftirqd preempted the task which actually
  27837. + * holds the lock, because we requeue q and raise NET_TX softirq
  27838. + * causing ksoftirqd to loop forever.
  27839. + *
  27840. + * It's safe to use spin_lock on RT here as softirqs run in thread
  27841. + * context and cannot deadlock against the thread which is holding
  27842. + * root_lock.
  27843. + *
  27844. + * On !RT the trylock might fail, but there we bail out from the
  27845. + * softirq loop after 10 attempts which we can't do on RT. And the
  27846. + * task holding root_lock cannot be preempted, so the only downside of
  27847. + * that trylock is that we need 10 loops to decide that we should have
  27848. + * given up in the first one :)
  27849. + */
  27850. +static inline int take_root_lock(spinlock_t *lock)
  27851. +{
  27852. + spin_lock(lock);
  27853. + return 1;
  27854. +}
  27855. +#else
  27856. +static inline int take_root_lock(spinlock_t *lock)
  27857. +{
  27858. + return spin_trylock(lock);
  27859. +}
  27860. +#endif
  27861. +
  27862. static void net_tx_action(struct softirq_action *h)
  27863. {
  27864. struct softnet_data *sd = this_cpu_ptr(&softnet_data);
  27865. @@ -3644,7 +3720,7 @@
  27866. head = head->next_sched;
  27867. root_lock = qdisc_lock(q);
  27868. - if (spin_trylock(root_lock)) {
  27869. + if (take_root_lock(root_lock)) {
  27870. smp_mb__before_atomic();
  27871. clear_bit(__QDISC_STATE_SCHED,
  27872. &q->state);
  27873. @@ -4066,7 +4142,7 @@
  27874. skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
  27875. if (skb->dev == dev) {
  27876. __skb_unlink(skb, &sd->input_pkt_queue);
  27877. - kfree_skb(skb);
  27878. + __skb_queue_tail(&sd->tofree_queue, skb);
  27879. input_queue_head_incr(sd);
  27880. }
  27881. }
  27882. @@ -4075,10 +4151,13 @@
  27883. skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
  27884. if (skb->dev == dev) {
  27885. __skb_unlink(skb, &sd->process_queue);
  27886. - kfree_skb(skb);
  27887. + __skb_queue_tail(&sd->tofree_queue, skb);
  27888. input_queue_head_incr(sd);
  27889. }
  27890. }
  27891. +
  27892. + if (!skb_queue_empty(&sd->tofree_queue))
  27893. + raise_softirq_irqoff(NET_RX_SOFTIRQ);
  27894. }
  27895. static int napi_gro_complete(struct sk_buff *skb)
  27896. @@ -4535,6 +4614,7 @@
  27897. sd->rps_ipi_list = NULL;
  27898. local_irq_enable();
  27899. + preempt_check_resched_rt();
  27900. /* Send pending IPI's to kick RPS processing on remote cpus. */
  27901. while (remsd) {
  27902. @@ -4548,6 +4628,7 @@
  27903. } else
  27904. #endif
  27905. local_irq_enable();
  27906. + preempt_check_resched_rt();
  27907. }
  27908. static bool sd_has_rps_ipi_waiting(struct softnet_data *sd)
  27909. @@ -4629,6 +4710,7 @@
  27910. local_irq_save(flags);
  27911. ____napi_schedule(this_cpu_ptr(&softnet_data), n);
  27912. local_irq_restore(flags);
  27913. + preempt_check_resched_rt();
  27914. }
  27915. EXPORT_SYMBOL(__napi_schedule);
  27916. @@ -4905,7 +4987,7 @@
  27917. list_splice_tail(&repoll, &list);
  27918. list_splice(&list, &sd->poll_list);
  27919. if (!list_empty(&sd->poll_list))
  27920. - __raise_softirq_irqoff(NET_RX_SOFTIRQ);
  27921. + __raise_softirq_irqoff_ksoft(NET_RX_SOFTIRQ);
  27922. net_rps_action_and_irq_enable(sd);
  27923. }
  27924. @@ -7244,7 +7326,7 @@
  27925. void synchronize_net(void)
  27926. {
  27927. might_sleep();
  27928. - if (rtnl_is_locked())
  27929. + if (rtnl_is_locked() && !IS_ENABLED(CONFIG_PREEMPT_RT_FULL))
  27930. synchronize_rcu_expedited();
  27931. else
  27932. synchronize_rcu();
  27933. @@ -7485,16 +7567,20 @@
  27934. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  27935. local_irq_enable();
  27936. + preempt_check_resched_rt();
  27937. /* Process offline CPU's input_pkt_queue */
  27938. while ((skb = __skb_dequeue(&oldsd->process_queue))) {
  27939. netif_rx_ni(skb);
  27940. input_queue_head_incr(oldsd);
  27941. }
  27942. - while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) {
  27943. + while ((skb = __skb_dequeue(&oldsd->input_pkt_queue))) {
  27944. netif_rx_ni(skb);
  27945. input_queue_head_incr(oldsd);
  27946. }
  27947. + while ((skb = __skb_dequeue(&oldsd->tofree_queue))) {
  27948. + kfree_skb(skb);
  27949. + }
  27950. return NOTIFY_OK;
  27951. }
  27952. @@ -7796,8 +7882,9 @@
  27953. for_each_possible_cpu(i) {
  27954. struct softnet_data *sd = &per_cpu(softnet_data, i);
  27955. - skb_queue_head_init(&sd->input_pkt_queue);
  27956. - skb_queue_head_init(&sd->process_queue);
  27957. + skb_queue_head_init_raw(&sd->input_pkt_queue);
  27958. + skb_queue_head_init_raw(&sd->process_queue);
  27959. + skb_queue_head_init_raw(&sd->tofree_queue);
  27960. INIT_LIST_HEAD(&sd->poll_list);
  27961. sd->output_queue_tailp = &sd->output_queue;
  27962. #ifdef CONFIG_RPS
  27963. diff -Nur linux-4.4.46.orig/net/core/skbuff.c linux-4.4.46/net/core/skbuff.c
  27964. --- linux-4.4.46.orig/net/core/skbuff.c 2017-02-01 08:31:11.000000000 +0100
  27965. +++ linux-4.4.46/net/core/skbuff.c 2017-02-03 17:18:10.955620139 +0100
  27966. @@ -63,6 +63,7 @@
  27967. #include <linux/errqueue.h>
  27968. #include <linux/prefetch.h>
  27969. #include <linux/if_vlan.h>
  27970. +#include <linux/locallock.h>
  27971. #include <net/protocol.h>
  27972. #include <net/dst.h>
  27973. @@ -351,6 +352,8 @@
  27974. static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
  27975. static DEFINE_PER_CPU(struct page_frag_cache, napi_alloc_cache);
  27976. +static DEFINE_LOCAL_IRQ_LOCK(netdev_alloc_lock);
  27977. +static DEFINE_LOCAL_IRQ_LOCK(napi_alloc_cache_lock);
  27978. static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
  27979. {
  27980. @@ -358,10 +361,10 @@
  27981. unsigned long flags;
  27982. void *data;
  27983. - local_irq_save(flags);
  27984. + local_lock_irqsave(netdev_alloc_lock, flags);
  27985. nc = this_cpu_ptr(&netdev_alloc_cache);
  27986. data = __alloc_page_frag(nc, fragsz, gfp_mask);
  27987. - local_irq_restore(flags);
  27988. + local_unlock_irqrestore(netdev_alloc_lock, flags);
  27989. return data;
  27990. }
  27991. @@ -380,9 +383,13 @@
  27992. static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
  27993. {
  27994. - struct page_frag_cache *nc = this_cpu_ptr(&napi_alloc_cache);
  27995. + struct page_frag_cache *nc;
  27996. + void *data;
  27997. - return __alloc_page_frag(nc, fragsz, gfp_mask);
  27998. + nc = &get_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  27999. + data = __alloc_page_frag(nc, fragsz, gfp_mask);
  28000. + put_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  28001. + return data;
  28002. }
  28003. void *napi_alloc_frag(unsigned int fragsz)
  28004. @@ -429,13 +436,13 @@
  28005. if (sk_memalloc_socks())
  28006. gfp_mask |= __GFP_MEMALLOC;
  28007. - local_irq_save(flags);
  28008. + local_lock_irqsave(netdev_alloc_lock, flags);
  28009. nc = this_cpu_ptr(&netdev_alloc_cache);
  28010. data = __alloc_page_frag(nc, len, gfp_mask);
  28011. pfmemalloc = nc->pfmemalloc;
  28012. - local_irq_restore(flags);
  28013. + local_unlock_irqrestore(netdev_alloc_lock, flags);
  28014. if (unlikely(!data))
  28015. return NULL;
  28016. @@ -476,9 +483,10 @@
  28017. struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
  28018. gfp_t gfp_mask)
  28019. {
  28020. - struct page_frag_cache *nc = this_cpu_ptr(&napi_alloc_cache);
  28021. + struct page_frag_cache *nc;
  28022. struct sk_buff *skb;
  28023. void *data;
  28024. + bool pfmemalloc;
  28025. len += NET_SKB_PAD + NET_IP_ALIGN;
  28026. @@ -496,7 +504,11 @@
  28027. if (sk_memalloc_socks())
  28028. gfp_mask |= __GFP_MEMALLOC;
  28029. + nc = &get_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  28030. data = __alloc_page_frag(nc, len, gfp_mask);
  28031. + pfmemalloc = nc->pfmemalloc;
  28032. + put_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  28033. +
  28034. if (unlikely(!data))
  28035. return NULL;
  28036. @@ -507,7 +519,7 @@
  28037. }
  28038. /* use OR instead of assignment to avoid clearing of bits in mask */
  28039. - if (nc->pfmemalloc)
  28040. + if (pfmemalloc)
  28041. skb->pfmemalloc = 1;
  28042. skb->head_frag = 1;
  28043. diff -Nur linux-4.4.46.orig/net/core/sock.c linux-4.4.46/net/core/sock.c
  28044. --- linux-4.4.46.orig/net/core/sock.c 2017-02-01 08:31:11.000000000 +0100
  28045. +++ linux-4.4.46/net/core/sock.c 2017-02-03 17:18:10.955620139 +0100
  28046. @@ -2436,12 +2436,11 @@
  28047. if (sk->sk_lock.owned)
  28048. __lock_sock(sk);
  28049. sk->sk_lock.owned = 1;
  28050. - spin_unlock(&sk->sk_lock.slock);
  28051. + spin_unlock_bh(&sk->sk_lock.slock);
  28052. /*
  28053. * The sk_lock has mutex_lock() semantics here:
  28054. */
  28055. mutex_acquire(&sk->sk_lock.dep_map, subclass, 0, _RET_IP_);
  28056. - local_bh_enable();
  28057. }
  28058. EXPORT_SYMBOL(lock_sock_nested);
  28059. diff -Nur linux-4.4.46.orig/net/ipv4/icmp.c linux-4.4.46/net/ipv4/icmp.c
  28060. --- linux-4.4.46.orig/net/ipv4/icmp.c 2017-02-01 08:31:11.000000000 +0100
  28061. +++ linux-4.4.46/net/ipv4/icmp.c 2017-02-03 17:18:10.955620139 +0100
  28062. @@ -69,6 +69,7 @@
  28063. #include <linux/jiffies.h>
  28064. #include <linux/kernel.h>
  28065. #include <linux/fcntl.h>
  28066. +#include <linux/sysrq.h>
  28067. #include <linux/socket.h>
  28068. #include <linux/in.h>
  28069. #include <linux/inet.h>
  28070. @@ -77,6 +78,7 @@
  28071. #include <linux/string.h>
  28072. #include <linux/netfilter_ipv4.h>
  28073. #include <linux/slab.h>
  28074. +#include <linux/locallock.h>
  28075. #include <net/snmp.h>
  28076. #include <net/ip.h>
  28077. #include <net/route.h>
  28078. @@ -204,6 +206,8 @@
  28079. *
  28080. * On SMP we have one ICMP socket per-cpu.
  28081. */
  28082. +static DEFINE_LOCAL_IRQ_LOCK(icmp_sk_lock);
  28083. +
  28084. static struct sock *icmp_sk(struct net *net)
  28085. {
  28086. return *this_cpu_ptr(net->ipv4.icmp_sk);
  28087. @@ -215,12 +219,14 @@
  28088. local_bh_disable();
  28089. + local_lock(icmp_sk_lock);
  28090. sk = icmp_sk(net);
  28091. if (unlikely(!spin_trylock(&sk->sk_lock.slock))) {
  28092. /* This can happen if the output path signals a
  28093. * dst_link_failure() for an outgoing ICMP packet.
  28094. */
  28095. + local_unlock(icmp_sk_lock);
  28096. local_bh_enable();
  28097. return NULL;
  28098. }
  28099. @@ -230,6 +236,7 @@
  28100. static inline void icmp_xmit_unlock(struct sock *sk)
  28101. {
  28102. spin_unlock_bh(&sk->sk_lock.slock);
  28103. + local_unlock(icmp_sk_lock);
  28104. }
  28105. int sysctl_icmp_msgs_per_sec __read_mostly = 1000;
  28106. @@ -358,6 +365,7 @@
  28107. struct sock *sk;
  28108. struct sk_buff *skb;
  28109. + local_lock(icmp_sk_lock);
  28110. sk = icmp_sk(dev_net((*rt)->dst.dev));
  28111. if (ip_append_data(sk, fl4, icmp_glue_bits, icmp_param,
  28112. icmp_param->data_len+icmp_param->head_len,
  28113. @@ -380,6 +388,7 @@
  28114. skb->ip_summed = CHECKSUM_NONE;
  28115. ip_push_pending_frames(sk, fl4);
  28116. }
  28117. + local_unlock(icmp_sk_lock);
  28118. }
  28119. /*
  28120. @@ -891,6 +900,30 @@
  28121. }
  28122. /*
  28123. + * 32bit and 64bit have different timestamp length, so we check for
  28124. + * the cookie at offset 20 and verify it is repeated at offset 50
  28125. + */
  28126. +#define CO_POS0 20
  28127. +#define CO_POS1 50
  28128. +#define CO_SIZE sizeof(int)
  28129. +#define ICMP_SYSRQ_SIZE 57
  28130. +
  28131. +/*
  28132. + * We got a ICMP_SYSRQ_SIZE sized ping request. Check for the cookie
  28133. + * pattern and if it matches send the next byte as a trigger to sysrq.
  28134. + */
  28135. +static void icmp_check_sysrq(struct net *net, struct sk_buff *skb)
  28136. +{
  28137. + int cookie = htonl(net->ipv4.sysctl_icmp_echo_sysrq);
  28138. + char *p = skb->data;
  28139. +
  28140. + if (!memcmp(&cookie, p + CO_POS0, CO_SIZE) &&
  28141. + !memcmp(&cookie, p + CO_POS1, CO_SIZE) &&
  28142. + p[CO_POS0 + CO_SIZE] == p[CO_POS1 + CO_SIZE])
  28143. + handle_sysrq(p[CO_POS0 + CO_SIZE]);
  28144. +}
  28145. +
  28146. +/*
  28147. * Handle ICMP_ECHO ("ping") requests.
  28148. *
  28149. * RFC 1122: 3.2.2.6 MUST have an echo server that answers ICMP echo
  28150. @@ -917,6 +950,11 @@
  28151. icmp_param.data_len = skb->len;
  28152. icmp_param.head_len = sizeof(struct icmphdr);
  28153. icmp_reply(&icmp_param, skb);
  28154. +
  28155. + if (skb->len == ICMP_SYSRQ_SIZE &&
  28156. + net->ipv4.sysctl_icmp_echo_sysrq) {
  28157. + icmp_check_sysrq(net, skb);
  28158. + }
  28159. }
  28160. /* should there be an ICMP stat for ignored echos? */
  28161. return true;
  28162. diff -Nur linux-4.4.46.orig/net/ipv4/sysctl_net_ipv4.c linux-4.4.46/net/ipv4/sysctl_net_ipv4.c
  28163. --- linux-4.4.46.orig/net/ipv4/sysctl_net_ipv4.c 2017-02-01 08:31:11.000000000 +0100
  28164. +++ linux-4.4.46/net/ipv4/sysctl_net_ipv4.c 2017-02-03 17:18:10.955620139 +0100
  28165. @@ -818,6 +818,13 @@
  28166. .proc_handler = proc_dointvec
  28167. },
  28168. {
  28169. + .procname = "icmp_echo_sysrq",
  28170. + .data = &init_net.ipv4.sysctl_icmp_echo_sysrq,
  28171. + .maxlen = sizeof(int),
  28172. + .mode = 0644,
  28173. + .proc_handler = proc_dointvec
  28174. + },
  28175. + {
  28176. .procname = "icmp_ignore_bogus_error_responses",
  28177. .data = &init_net.ipv4.sysctl_icmp_ignore_bogus_error_responses,
  28178. .maxlen = sizeof(int),
  28179. diff -Nur linux-4.4.46.orig/net/ipv4/tcp_ipv4.c linux-4.4.46/net/ipv4/tcp_ipv4.c
  28180. --- linux-4.4.46.orig/net/ipv4/tcp_ipv4.c 2017-02-01 08:31:11.000000000 +0100
  28181. +++ linux-4.4.46/net/ipv4/tcp_ipv4.c 2017-02-03 17:18:10.955620139 +0100
  28182. @@ -62,6 +62,7 @@
  28183. #include <linux/init.h>
  28184. #include <linux/times.h>
  28185. #include <linux/slab.h>
  28186. +#include <linux/locallock.h>
  28187. #include <net/net_namespace.h>
  28188. #include <net/icmp.h>
  28189. @@ -566,6 +567,7 @@
  28190. }
  28191. EXPORT_SYMBOL(tcp_v4_send_check);
  28192. +static DEFINE_LOCAL_IRQ_LOCK(tcp_sk_lock);
  28193. /*
  28194. * This routine will send an RST to the other tcp.
  28195. *
  28196. @@ -687,10 +689,13 @@
  28197. arg.bound_dev_if = sk->sk_bound_dev_if;
  28198. arg.tos = ip_hdr(skb)->tos;
  28199. +
  28200. + local_lock(tcp_sk_lock);
  28201. ip_send_unicast_reply(*this_cpu_ptr(net->ipv4.tcp_sk),
  28202. skb, &TCP_SKB_CB(skb)->header.h4.opt,
  28203. ip_hdr(skb)->saddr, ip_hdr(skb)->daddr,
  28204. &arg, arg.iov[0].iov_len);
  28205. + local_unlock(tcp_sk_lock);
  28206. TCP_INC_STATS_BH(net, TCP_MIB_OUTSEGS);
  28207. TCP_INC_STATS_BH(net, TCP_MIB_OUTRSTS);
  28208. @@ -772,10 +777,12 @@
  28209. if (oif)
  28210. arg.bound_dev_if = oif;
  28211. arg.tos = tos;
  28212. + local_lock(tcp_sk_lock);
  28213. ip_send_unicast_reply(*this_cpu_ptr(net->ipv4.tcp_sk),
  28214. skb, &TCP_SKB_CB(skb)->header.h4.opt,
  28215. ip_hdr(skb)->saddr, ip_hdr(skb)->daddr,
  28216. &arg, arg.iov[0].iov_len);
  28217. + local_unlock(tcp_sk_lock);
  28218. TCP_INC_STATS_BH(net, TCP_MIB_OUTSEGS);
  28219. }
  28220. diff -Nur linux-4.4.46.orig/net/mac80211/rx.c linux-4.4.46/net/mac80211/rx.c
  28221. --- linux-4.4.46.orig/net/mac80211/rx.c 2017-02-01 08:31:11.000000000 +0100
  28222. +++ linux-4.4.46/net/mac80211/rx.c 2017-02-03 17:18:10.955620139 +0100
  28223. @@ -3580,7 +3580,7 @@
  28224. struct ieee80211_supported_band *sband;
  28225. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  28226. - WARN_ON_ONCE(softirq_count() == 0);
  28227. + WARN_ON_ONCE_NONRT(softirq_count() == 0);
  28228. if (WARN_ON(status->band >= IEEE80211_NUM_BANDS))
  28229. goto drop;
  28230. diff -Nur linux-4.4.46.orig/net/netfilter/core.c linux-4.4.46/net/netfilter/core.c
  28231. --- linux-4.4.46.orig/net/netfilter/core.c 2017-02-01 08:31:11.000000000 +0100
  28232. +++ linux-4.4.46/net/netfilter/core.c 2017-02-03 17:18:10.955620139 +0100
  28233. @@ -22,11 +22,17 @@
  28234. #include <linux/proc_fs.h>
  28235. #include <linux/mutex.h>
  28236. #include <linux/slab.h>
  28237. +#include <linux/locallock.h>
  28238. #include <net/net_namespace.h>
  28239. #include <net/sock.h>
  28240. #include "nf_internals.h"
  28241. +#ifdef CONFIG_PREEMPT_RT_BASE
  28242. +DEFINE_LOCAL_IRQ_LOCK(xt_write_lock);
  28243. +EXPORT_PER_CPU_SYMBOL(xt_write_lock);
  28244. +#endif
  28245. +
  28246. static DEFINE_MUTEX(afinfo_mutex);
  28247. const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO] __read_mostly;
  28248. diff -Nur linux-4.4.46.orig/net/packet/af_packet.c linux-4.4.46/net/packet/af_packet.c
  28249. --- linux-4.4.46.orig/net/packet/af_packet.c 2017-02-01 08:31:11.000000000 +0100
  28250. +++ linux-4.4.46/net/packet/af_packet.c 2017-02-03 17:18:10.959620294 +0100
  28251. @@ -63,6 +63,7 @@
  28252. #include <linux/if_packet.h>
  28253. #include <linux/wireless.h>
  28254. #include <linux/kernel.h>
  28255. +#include <linux/delay.h>
  28256. #include <linux/kmod.h>
  28257. #include <linux/slab.h>
  28258. #include <linux/vmalloc.h>
  28259. @@ -694,7 +695,7 @@
  28260. if (BLOCK_NUM_PKTS(pbd)) {
  28261. while (atomic_read(&pkc->blk_fill_in_prog)) {
  28262. /* Waiting for skb_copy_bits to finish... */
  28263. - cpu_relax();
  28264. + cpu_chill();
  28265. }
  28266. }
  28267. @@ -956,7 +957,7 @@
  28268. if (!(status & TP_STATUS_BLK_TMO)) {
  28269. while (atomic_read(&pkc->blk_fill_in_prog)) {
  28270. /* Waiting for skb_copy_bits to finish... */
  28271. - cpu_relax();
  28272. + cpu_chill();
  28273. }
  28274. }
  28275. prb_close_block(pkc, pbd, po, status);
  28276. diff -Nur linux-4.4.46.orig/net/rds/ib_rdma.c linux-4.4.46/net/rds/ib_rdma.c
  28277. --- linux-4.4.46.orig/net/rds/ib_rdma.c 2017-02-01 08:31:11.000000000 +0100
  28278. +++ linux-4.4.46/net/rds/ib_rdma.c 2017-02-03 17:18:10.959620294 +0100
  28279. @@ -34,6 +34,7 @@
  28280. #include <linux/slab.h>
  28281. #include <linux/rculist.h>
  28282. #include <linux/llist.h>
  28283. +#include <linux/delay.h>
  28284. #include "rds.h"
  28285. #include "ib.h"
  28286. @@ -313,7 +314,7 @@
  28287. for_each_online_cpu(cpu) {
  28288. flag = &per_cpu(clean_list_grace, cpu);
  28289. while (test_bit(CLEAN_LIST_BUSY_BIT, flag))
  28290. - cpu_relax();
  28291. + cpu_chill();
  28292. }
  28293. }
  28294. diff -Nur linux-4.4.46.orig/net/sched/sch_generic.c linux-4.4.46/net/sched/sch_generic.c
  28295. --- linux-4.4.46.orig/net/sched/sch_generic.c 2017-02-01 08:31:11.000000000 +0100
  28296. +++ linux-4.4.46/net/sched/sch_generic.c 2017-02-03 17:18:10.959620294 +0100
  28297. @@ -893,7 +893,7 @@
  28298. /* Wait for outstanding qdisc_run calls. */
  28299. list_for_each_entry(dev, head, close_list)
  28300. while (some_qdisc_is_busy(dev))
  28301. - yield();
  28302. + msleep(1);
  28303. }
  28304. void dev_deactivate(struct net_device *dev)
  28305. diff -Nur linux-4.4.46.orig/net/sunrpc/svc_xprt.c linux-4.4.46/net/sunrpc/svc_xprt.c
  28306. --- linux-4.4.46.orig/net/sunrpc/svc_xprt.c 2017-02-01 08:31:11.000000000 +0100
  28307. +++ linux-4.4.46/net/sunrpc/svc_xprt.c 2017-02-03 17:18:10.959620294 +0100
  28308. @@ -340,7 +340,7 @@
  28309. goto out;
  28310. }
  28311. - cpu = get_cpu();
  28312. + cpu = get_cpu_light();
  28313. pool = svc_pool_for_cpu(xprt->xpt_server, cpu);
  28314. atomic_long_inc(&pool->sp_stats.packets);
  28315. @@ -376,7 +376,7 @@
  28316. atomic_long_inc(&pool->sp_stats.threads_woken);
  28317. wake_up_process(rqstp->rq_task);
  28318. - put_cpu();
  28319. + put_cpu_light();
  28320. goto out;
  28321. }
  28322. rcu_read_unlock();
  28323. @@ -397,7 +397,7 @@
  28324. goto redo_search;
  28325. }
  28326. rqstp = NULL;
  28327. - put_cpu();
  28328. + put_cpu_light();
  28329. out:
  28330. trace_svc_xprt_do_enqueue(xprt, rqstp);
  28331. }
  28332. diff -Nur linux-4.4.46.orig/scripts/mkcompile_h linux-4.4.46/scripts/mkcompile_h
  28333. --- linux-4.4.46.orig/scripts/mkcompile_h 2017-02-01 08:31:11.000000000 +0100
  28334. +++ linux-4.4.46/scripts/mkcompile_h 2017-02-03 17:18:10.959620294 +0100
  28335. @@ -4,7 +4,8 @@
  28336. ARCH=$2
  28337. SMP=$3
  28338. PREEMPT=$4
  28339. -CC=$5
  28340. +RT=$5
  28341. +CC=$6
  28342. vecho() { [ "${quiet}" = "silent_" ] || echo "$@" ; }
  28343. @@ -57,6 +58,7 @@
  28344. CONFIG_FLAGS=""
  28345. if [ -n "$SMP" ] ; then CONFIG_FLAGS="SMP"; fi
  28346. if [ -n "$PREEMPT" ] ; then CONFIG_FLAGS="$CONFIG_FLAGS PREEMPT"; fi
  28347. +if [ -n "$RT" ] ; then CONFIG_FLAGS="$CONFIG_FLAGS RT"; fi
  28348. UTS_VERSION="$UTS_VERSION $CONFIG_FLAGS $TIMESTAMP"
  28349. # Truncate to maximum length
  28350. diff -Nur linux-4.4.46.orig/sound/core/pcm_native.c linux-4.4.46/sound/core/pcm_native.c
  28351. --- linux-4.4.46.orig/sound/core/pcm_native.c 2017-02-01 08:31:11.000000000 +0100
  28352. +++ linux-4.4.46/sound/core/pcm_native.c 2017-02-03 17:18:10.959620294 +0100
  28353. @@ -135,7 +135,7 @@
  28354. void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
  28355. {
  28356. if (!substream->pcm->nonatomic)
  28357. - local_irq_disable();
  28358. + local_irq_disable_nort();
  28359. snd_pcm_stream_lock(substream);
  28360. }
  28361. EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
  28362. @@ -150,7 +150,7 @@
  28363. {
  28364. snd_pcm_stream_unlock(substream);
  28365. if (!substream->pcm->nonatomic)
  28366. - local_irq_enable();
  28367. + local_irq_enable_nort();
  28368. }
  28369. EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
  28370. @@ -158,7 +158,7 @@
  28371. {
  28372. unsigned long flags = 0;
  28373. if (!substream->pcm->nonatomic)
  28374. - local_irq_save(flags);
  28375. + local_irq_save_nort(flags);
  28376. snd_pcm_stream_lock(substream);
  28377. return flags;
  28378. }
  28379. @@ -176,7 +176,7 @@
  28380. {
  28381. snd_pcm_stream_unlock(substream);
  28382. if (!substream->pcm->nonatomic)
  28383. - local_irq_restore(flags);
  28384. + local_irq_restore_nort(flags);
  28385. }
  28386. EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
  28387. diff -Nur linux-4.4.46.orig/virt/kvm/async_pf.c linux-4.4.46/virt/kvm/async_pf.c
  28388. --- linux-4.4.46.orig/virt/kvm/async_pf.c 2017-02-01 08:31:11.000000000 +0100
  28389. +++ linux-4.4.46/virt/kvm/async_pf.c 2017-02-03 17:18:10.959620294 +0100
  28390. @@ -98,8 +98,8 @@
  28391. * This memory barrier pairs with prepare_to_wait's set_current_state()
  28392. */
  28393. smp_mb();
  28394. - if (waitqueue_active(&vcpu->wq))
  28395. - wake_up_interruptible(&vcpu->wq);
  28396. + if (swait_active(&vcpu->wq))
  28397. + swake_up(&vcpu->wq);
  28398. mmput(mm);
  28399. kvm_put_kvm(vcpu->kvm);
  28400. diff -Nur linux-4.4.46.orig/virt/kvm/kvm_main.c linux-4.4.46/virt/kvm/kvm_main.c
  28401. --- linux-4.4.46.orig/virt/kvm/kvm_main.c 2017-02-01 08:31:11.000000000 +0100
  28402. +++ linux-4.4.46/virt/kvm/kvm_main.c 2017-02-03 17:18:10.959620294 +0100
  28403. @@ -228,8 +228,7 @@
  28404. vcpu->kvm = kvm;
  28405. vcpu->vcpu_id = id;
  28406. vcpu->pid = NULL;
  28407. - vcpu->halt_poll_ns = 0;
  28408. - init_waitqueue_head(&vcpu->wq);
  28409. + init_swait_queue_head(&vcpu->wq);
  28410. kvm_async_pf_vcpu_init(vcpu);
  28411. vcpu->pre_pcpu = -1;
  28412. @@ -2005,7 +2004,7 @@
  28413. void kvm_vcpu_block(struct kvm_vcpu *vcpu)
  28414. {
  28415. ktime_t start, cur;
  28416. - DEFINE_WAIT(wait);
  28417. + DECLARE_SWAITQUEUE(wait);
  28418. bool waited = false;
  28419. u64 block_ns;
  28420. @@ -2030,7 +2029,7 @@
  28421. kvm_arch_vcpu_blocking(vcpu);
  28422. for (;;) {
  28423. - prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
  28424. + prepare_to_swait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
  28425. if (kvm_vcpu_check_block(vcpu) < 0)
  28426. break;
  28427. @@ -2039,7 +2038,7 @@
  28428. schedule();
  28429. }
  28430. - finish_wait(&vcpu->wq, &wait);
  28431. + finish_swait(&vcpu->wq, &wait);
  28432. cur = ktime_get();
  28433. kvm_arch_vcpu_unblocking(vcpu);
  28434. @@ -2071,11 +2070,11 @@
  28435. {
  28436. int me;
  28437. int cpu = vcpu->cpu;
  28438. - wait_queue_head_t *wqp;
  28439. + struct swait_queue_head *wqp;
  28440. wqp = kvm_arch_vcpu_wq(vcpu);
  28441. - if (waitqueue_active(wqp)) {
  28442. - wake_up_interruptible(wqp);
  28443. + if (swait_active(wqp)) {
  28444. + swake_up(wqp);
  28445. ++vcpu->stat.halt_wakeup;
  28446. }
  28447. @@ -2176,7 +2175,7 @@
  28448. continue;
  28449. if (vcpu == me)
  28450. continue;
  28451. - if (waitqueue_active(&vcpu->wq) && !kvm_arch_vcpu_runnable(vcpu))
  28452. + if (swait_active(&vcpu->wq) && !kvm_arch_vcpu_runnable(vcpu))
  28453. continue;
  28454. if (!kvm_vcpu_eligible_for_directed_yield(vcpu))
  28455. continue;