patch-realtime 881 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987
  1. diff -Nur linux-4.4.28.orig/arch/arm/include/asm/switch_to.h linux-4.4.28/arch/arm/include/asm/switch_to.h
  2. --- linux-4.4.28.orig/arch/arm/include/asm/switch_to.h 2016-10-28 09:53:25.000000000 +0200
  3. +++ linux-4.4.28/arch/arm/include/asm/switch_to.h 2016-11-01 21:26:56.728210258 +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.28.orig/arch/arm/include/asm/thread_info.h linux-4.4.28/arch/arm/include/asm/thread_info.h
  24. --- linux-4.4.28.orig/arch/arm/include/asm/thread_info.h 2016-10-28 09:53:25.000000000 +0200
  25. +++ linux-4.4.28/arch/arm/include/asm/thread_info.h 2016-11-01 21:26:56.748211548 +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.28.orig/arch/arm/Kconfig linux-4.4.28/arch/arm/Kconfig
  61. --- linux-4.4.28.orig/arch/arm/Kconfig 2016-10-28 09:53:25.000000000 +0200
  62. +++ linux-4.4.28/arch/arm/Kconfig 2016-11-01 21:26:56.696208202 +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.28.orig/arch/arm/kernel/asm-offsets.c linux-4.4.28/arch/arm/kernel/asm-offsets.c
  81. --- linux-4.4.28.orig/arch/arm/kernel/asm-offsets.c 2016-10-28 09:53:25.000000000 +0200
  82. +++ linux-4.4.28/arch/arm/kernel/asm-offsets.c 2016-11-01 21:26:56.760212322 +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.28.orig/arch/arm/kernel/entry-armv.S linux-4.4.28/arch/arm/kernel/entry-armv.S
  92. --- linux-4.4.28.orig/arch/arm/kernel/entry-armv.S 2016-10-28 09:53:25.000000000 +0200
  93. +++ linux-4.4.28/arch/arm/kernel/entry-armv.S 2016-11-01 21:26:56.764212578 +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.28.orig/arch/arm/kernel/entry-common.S linux-4.4.28/arch/arm/kernel/entry-common.S
  130. --- linux-4.4.28.orig/arch/arm/kernel/entry-common.S 2016-10-28 09:53:25.000000000 +0200
  131. +++ linux-4.4.28/arch/arm/kernel/entry-common.S 2016-11-01 21:26:56.764212578 +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.28.orig/arch/arm/kernel/process.c linux-4.4.28/arch/arm/kernel/process.c
  155. --- linux-4.4.28.orig/arch/arm/kernel/process.c 2016-10-28 09:53:25.000000000 +0200
  156. +++ linux-4.4.28/arch/arm/kernel/process.c 2016-11-01 21:26:56.772213095 +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.28.orig/arch/arm/kernel/signal.c linux-4.4.28/arch/arm/kernel/signal.c
  188. --- linux-4.4.28.orig/arch/arm/kernel/signal.c 2016-10-28 09:53:25.000000000 +0200
  189. +++ linux-4.4.28/arch/arm/kernel/signal.c 2016-11-01 21:26:56.772213095 +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.28.orig/arch/arm/kernel/smp.c linux-4.4.28/arch/arm/kernel/smp.c
  201. --- linux-4.4.28.orig/arch/arm/kernel/smp.c 2016-10-28 09:53:25.000000000 +0200
  202. +++ linux-4.4.28/arch/arm/kernel/smp.c 2016-11-01 21:26:56.772213095 +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.28.orig/arch/arm/kernel/unwind.c linux-4.4.28/arch/arm/kernel/unwind.c
  220. --- linux-4.4.28.orig/arch/arm/kernel/unwind.c 2016-10-28 09:53:25.000000000 +0200
  221. +++ linux-4.4.28/arch/arm/kernel/unwind.c 2016-11-01 21:26:56.776213352 +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.28.orig/arch/arm/kvm/arm.c linux-4.4.28/arch/arm/kvm/arm.c
  266. --- linux-4.4.28.orig/arch/arm/kvm/arm.c 2016-10-28 09:53:25.000000000 +0200
  267. +++ linux-4.4.28/arch/arm/kvm/arm.c 2016-11-01 21:26:56.776213352 +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.28.orig/arch/arm/kvm/psci.c linux-4.4.28/arch/arm/kvm/psci.c
  309. --- linux-4.4.28.orig/arch/arm/kvm/psci.c 2016-10-28 09:53:25.000000000 +0200
  310. +++ linux-4.4.28/arch/arm/kvm/psci.c 2016-11-01 21:26:56.780213616 +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.28.orig/arch/arm/mach-at91/at91rm9200.c linux-4.4.28/arch/arm/mach-at91/at91rm9200.c
  328. --- linux-4.4.28.orig/arch/arm/mach-at91/at91rm9200.c 2016-10-28 09:53:25.000000000 +0200
  329. +++ linux-4.4.28/arch/arm/mach-at91/at91rm9200.c 2016-11-01 21:26:56.784213869 +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.28.orig/arch/arm/mach-at91/at91sam9.c linux-4.4.28/arch/arm/mach-at91/at91sam9.c
  342. --- linux-4.4.28.orig/arch/arm/mach-at91/at91sam9.c 2016-10-28 09:53:25.000000000 +0200
  343. +++ linux-4.4.28/arch/arm/mach-at91/at91sam9.c 2016-11-01 21:26:56.784213869 +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.28.orig/arch/arm/mach-at91/generic.h linux-4.4.28/arch/arm/mach-at91/generic.h
  352. --- linux-4.4.28.orig/arch/arm/mach-at91/generic.h 2016-10-28 09:53:25.000000000 +0200
  353. +++ linux-4.4.28/arch/arm/mach-at91/generic.h 2016-11-01 21:26:56.784213869 +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.28.orig/arch/arm/mach-at91/Kconfig linux-4.4.28/arch/arm/mach-at91/Kconfig
  383. --- linux-4.4.28.orig/arch/arm/mach-at91/Kconfig 2016-10-28 09:53:25.000000000 +0200
  384. +++ linux-4.4.28/arch/arm/mach-at91/Kconfig 2016-11-01 21:26:56.784213869 +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.28.orig/arch/arm/mach-at91/pm.c linux-4.4.28/arch/arm/mach-at91/pm.c
  393. --- linux-4.4.28.orig/arch/arm/mach-at91/pm.c 2016-10-28 09:53:25.000000000 +0200
  394. +++ linux-4.4.28/arch/arm/mach-at91/pm.c 2016-11-01 21:26:56.788214125 +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.28.orig/arch/arm/mach-at91/sama5.c linux-4.4.28/arch/arm/mach-at91/sama5.c
  528. --- linux-4.4.28.orig/arch/arm/mach-at91/sama5.c 2016-10-28 09:53:25.000000000 +0200
  529. +++ linux-4.4.28/arch/arm/mach-at91/sama5.c 2016-11-01 21:26:56.788214125 +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.28.orig/arch/arm/mach-exynos/platsmp.c linux-4.4.28/arch/arm/mach-exynos/platsmp.c
  538. --- linux-4.4.28.orig/arch/arm/mach-exynos/platsmp.c 2016-10-28 09:53:25.000000000 +0200
  539. +++ linux-4.4.28/arch/arm/mach-exynos/platsmp.c 2016-11-01 21:26:56.796214642 +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.28.orig/arch/arm/mach-hisi/platmcpm.c linux-4.4.28/arch/arm/mach-hisi/platmcpm.c
  582. --- linux-4.4.28.orig/arch/arm/mach-hisi/platmcpm.c 2016-10-28 09:53:25.000000000 +0200
  583. +++ linux-4.4.28/arch/arm/mach-hisi/platmcpm.c 2016-11-01 21:26:56.800214899 +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.28.orig/arch/arm/mach-imx/Kconfig linux-4.4.28/arch/arm/mach-imx/Kconfig
  662. --- linux-4.4.28.orig/arch/arm/mach-imx/Kconfig 2016-10-28 09:53:25.000000000 +0200
  663. +++ linux-4.4.28/arch/arm/mach-imx/Kconfig 2016-11-01 21:26:56.804215163 +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.28.orig/arch/arm/mach-omap2/omap-smp.c linux-4.4.28/arch/arm/mach-omap2/omap-smp.c
  674. --- linux-4.4.28.orig/arch/arm/mach-omap2/omap-smp.c 2016-10-28 09:53:25.000000000 +0200
  675. +++ linux-4.4.28/arch/arm/mach-omap2/omap-smp.c 2016-11-01 21:26:56.836217219 +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.28.orig/arch/arm/mach-prima2/platsmp.c linux-4.4.28/arch/arm/mach-prima2/platsmp.c
  710. --- linux-4.4.28.orig/arch/arm/mach-prima2/platsmp.c 2016-10-28 09:53:25.000000000 +0200
  711. +++ linux-4.4.28/arch/arm/mach-prima2/platsmp.c 2016-11-01 21:26:56.864219031 +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.28.orig/arch/arm/mach-qcom/platsmp.c linux-4.4.28/arch/arm/mach-qcom/platsmp.c
  744. --- linux-4.4.28.orig/arch/arm/mach-qcom/platsmp.c 2016-10-28 09:53:25.000000000 +0200
  745. +++ linux-4.4.28/arch/arm/mach-qcom/platsmp.c 2016-11-01 21:26:56.872219540 +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.28.orig/arch/arm/mach-spear/platsmp.c linux-4.4.28/arch/arm/mach-spear/platsmp.c
  779. --- linux-4.4.28.orig/arch/arm/mach-spear/platsmp.c 2016-10-28 09:53:25.000000000 +0200
  780. +++ linux-4.4.28/arch/arm/mach-spear/platsmp.c 2016-11-01 21:26:56.896221093 +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.28.orig/arch/arm/mach-sti/platsmp.c linux-4.4.28/arch/arm/mach-sti/platsmp.c
  814. --- linux-4.4.28.orig/arch/arm/mach-sti/platsmp.c 2016-10-28 09:53:25.000000000 +0200
  815. +++ linux-4.4.28/arch/arm/mach-sti/platsmp.c 2016-11-01 21:26:56.896221093 +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.28.orig/arch/arm/mm/fault.c linux-4.4.28/arch/arm/mm/fault.c
  850. --- linux-4.4.28.orig/arch/arm/mm/fault.c 2016-10-28 09:53:25.000000000 +0200
  851. +++ linux-4.4.28/arch/arm/mm/fault.c 2016-11-01 21:26:56.900221345 +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.28.orig/arch/arm/mm/highmem.c linux-4.4.28/arch/arm/mm/highmem.c
  871. --- linux-4.4.28.orig/arch/arm/mm/highmem.c 2016-10-28 09:53:25.000000000 +0200
  872. +++ linux-4.4.28/arch/arm/mm/highmem.c 2016-11-01 21:26:56.904221609 +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.28.orig/arch/arm/plat-versatile/platsmp.c linux-4.4.28/arch/arm/plat-versatile/platsmp.c
  992. --- linux-4.4.28.orig/arch/arm/plat-versatile/platsmp.c 2016-10-28 09:53:25.000000000 +0200
  993. +++ linux-4.4.28/arch/arm/plat-versatile/platsmp.c 2016-11-01 21:26:56.936223672 +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.28.orig/arch/arm64/include/asm/thread_info.h linux-4.4.28/arch/arm64/include/asm/thread_info.h
  1028. --- linux-4.4.28.orig/arch/arm64/include/asm/thread_info.h 2016-10-28 09:53:25.000000000 +0200
  1029. +++ linux-4.4.28/arch/arm64/include/asm/thread_info.h 2016-11-01 21:26:56.948224439 +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.28.orig/arch/arm64/Kconfig linux-4.4.28/arch/arm64/Kconfig
  1062. --- linux-4.4.28.orig/arch/arm64/Kconfig 2016-10-28 09:53:25.000000000 +0200
  1063. +++ linux-4.4.28/arch/arm64/Kconfig 2016-11-01 21:26:56.944224187 +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.28.orig/arch/arm64/kernel/asm-offsets.c linux-4.4.28/arch/arm64/kernel/asm-offsets.c
  1081. --- linux-4.4.28.orig/arch/arm64/kernel/asm-offsets.c 2016-10-28 09:53:25.000000000 +0200
  1082. +++ linux-4.4.28/arch/arm64/kernel/asm-offsets.c 2016-11-01 21:26:56.976226250 +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.28.orig/arch/arm64/kernel/entry.S linux-4.4.28/arch/arm64/kernel/entry.S
  1092. --- linux-4.4.28.orig/arch/arm64/kernel/entry.S 2016-10-28 09:53:25.000000000 +0200
  1093. +++ linux-4.4.28/arch/arm64/kernel/entry.S 2016-11-01 21:26:56.976226250 +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.28.orig/arch/Kconfig linux-4.4.28/arch/Kconfig
  1130. --- linux-4.4.28.orig/arch/Kconfig 2016-10-28 09:53:25.000000000 +0200
  1131. +++ linux-4.4.28/arch/Kconfig 2016-11-01 21:26:56.688207681 +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.28.orig/arch/mips/Kconfig linux-4.4.28/arch/mips/Kconfig
  1149. --- linux-4.4.28.orig/arch/mips/Kconfig 2016-10-28 09:53:25.000000000 +0200
  1150. +++ linux-4.4.28/arch/mips/Kconfig 2016-11-01 21:26:57.008228310 +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.28.orig/arch/mips/kvm/mips.c linux-4.4.28/arch/mips/kvm/mips.c
  1160. --- linux-4.4.28.orig/arch/mips/kvm/mips.c 2016-10-28 09:53:25.000000000 +0200
  1161. +++ linux-4.4.28/arch/mips/kvm/mips.c 2016-11-01 21:26:57.044230629 +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.28.orig/arch/powerpc/include/asm/kvm_host.h linux-4.4.28/arch/powerpc/include/asm/kvm_host.h
  1180. --- linux-4.4.28.orig/arch/powerpc/include/asm/kvm_host.h 2016-10-28 09:53:25.000000000 +0200
  1181. +++ linux-4.4.28/arch/powerpc/include/asm/kvm_host.h 2016-11-01 21:26:57.116235270 +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. @@ -626,7 +626,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.28.orig/arch/powerpc/include/asm/thread_info.h linux-4.4.28/arch/powerpc/include/asm/thread_info.h
  1200. --- linux-4.4.28.orig/arch/powerpc/include/asm/thread_info.h 2016-10-28 09:53:25.000000000 +0200
  1201. +++ linux-4.4.28/arch/powerpc/include/asm/thread_info.h 2016-11-01 21:26:57.124235787 +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.28.orig/arch/powerpc/Kconfig linux-4.4.28/arch/powerpc/Kconfig
  1245. --- linux-4.4.28.orig/arch/powerpc/Kconfig 2016-10-28 09:53:25.000000000 +0200
  1246. +++ linux-4.4.28/arch/powerpc/Kconfig 2016-11-01 21:26:57.072232440 +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.28.orig/arch/powerpc/kernel/asm-offsets.c linux-4.4.28/arch/powerpc/kernel/asm-offsets.c
  1273. --- linux-4.4.28.orig/arch/powerpc/kernel/asm-offsets.c 2016-10-28 09:53:25.000000000 +0200
  1274. +++ linux-4.4.28/arch/powerpc/kernel/asm-offsets.c 2016-11-01 21:26:57.136236560 +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.28.orig/arch/powerpc/kernel/entry_32.S linux-4.4.28/arch/powerpc/kernel/entry_32.S
  1283. --- linux-4.4.28.orig/arch/powerpc/kernel/entry_32.S 2016-10-28 09:53:25.000000000 +0200
  1284. +++ linux-4.4.28/arch/powerpc/kernel/entry_32.S 2016-11-01 21:26:57.144237081 +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.28.orig/arch/powerpc/kernel/entry_64.S linux-4.4.28/arch/powerpc/kernel/entry_64.S
  1332. --- linux-4.4.28.orig/arch/powerpc/kernel/entry_64.S 2016-10-28 09:53:25.000000000 +0200
  1333. +++ linux-4.4.28/arch/powerpc/kernel/entry_64.S 2016-11-01 21:26:57.144237081 +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.28.orig/arch/powerpc/kernel/irq.c linux-4.4.28/arch/powerpc/kernel/irq.c
  1371. --- linux-4.4.28.orig/arch/powerpc/kernel/irq.c 2016-10-28 09:53:25.000000000 +0200
  1372. +++ linux-4.4.28/arch/powerpc/kernel/irq.c 2016-11-01 21:26:57.152237590 +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.28.orig/arch/powerpc/kernel/misc_32.S linux-4.4.28/arch/powerpc/kernel/misc_32.S
  1388. --- linux-4.4.28.orig/arch/powerpc/kernel/misc_32.S 2016-10-28 09:53:25.000000000 +0200
  1389. +++ linux-4.4.28/arch/powerpc/kernel/misc_32.S 2016-11-01 21:26:57.156237855 +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.28.orig/arch/powerpc/kernel/misc_64.S linux-4.4.28/arch/powerpc/kernel/misc_64.S
  1406. --- linux-4.4.28.orig/arch/powerpc/kernel/misc_64.S 2016-10-28 09:53:25.000000000 +0200
  1407. +++ linux-4.4.28/arch/powerpc/kernel/misc_64.S 2016-11-01 21:26:57.160238107 +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.28.orig/arch/powerpc/kvm/book3s_hv.c linux-4.4.28/arch/powerpc/kvm/book3s_hv.c
  1422. --- linux-4.4.28.orig/arch/powerpc/kvm/book3s_hv.c 2016-10-28 09:53:25.000000000 +0200
  1423. +++ linux-4.4.28/arch/powerpc/kvm/book3s_hv.c 2016-11-01 21:26:57.184239655 +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. @@ -1447,7 +1447,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. @@ -2519,10 +2519,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. @@ -2536,7 +2535,7 @@
  1469. }
  1470. if (!do_sleep) {
  1471. - finish_wait(&vc->wq, &wait);
  1472. + finish_swait(&vc->wq, &wait);
  1473. return;
  1474. }
  1475. @@ -2544,7 +2543,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. @@ -2600,7 +2599,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.28.orig/arch/powerpc/kvm/Kconfig linux-4.4.28/arch/powerpc/kvm/Kconfig
  1493. --- linux-4.4.28.orig/arch/powerpc/kvm/Kconfig 2016-10-28 09:53:25.000000000 +0200
  1494. +++ linux-4.4.28/arch/powerpc/kvm/Kconfig 2016-11-01 21:26:57.168238628 +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.28.orig/arch/powerpc/platforms/ps3/device-init.c linux-4.4.28/arch/powerpc/platforms/ps3/device-init.c
  1504. --- linux-4.4.28.orig/arch/powerpc/platforms/ps3/device-init.c 2016-10-28 09:53:25.000000000 +0200
  1505. +++ linux-4.4.28/arch/powerpc/platforms/ps3/device-init.c 2016-11-01 21:26:57.192240175 +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.28.orig/arch/s390/include/asm/kvm_host.h linux-4.4.28/arch/s390/include/asm/kvm_host.h
  1515. --- linux-4.4.28.orig/arch/s390/include/asm/kvm_host.h 2016-10-28 09:53:25.000000000 +0200
  1516. +++ linux-4.4.28/arch/s390/include/asm/kvm_host.h 2016-11-01 21:26:57.204240949 +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.28.orig/arch/s390/kvm/interrupt.c linux-4.4.28/arch/s390/kvm/interrupt.c
  1527. --- linux-4.4.28.orig/arch/s390/kvm/interrupt.c 2016-10-28 09:53:25.000000000 +0200
  1528. +++ linux-4.4.28/arch/s390/kvm/interrupt.c 2016-11-01 21:26:57.224242231 +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.28.orig/arch/sh/kernel/irq.c linux-4.4.28/arch/sh/kernel/irq.c
  1545. --- linux-4.4.28.orig/arch/sh/kernel/irq.c 2016-10-28 09:53:25.000000000 +0200
  1546. +++ linux-4.4.28/arch/sh/kernel/irq.c 2016-11-01 21:26:57.284246099 +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.28.orig/arch/sparc/Kconfig linux-4.4.28/arch/sparc/Kconfig
  1563. --- linux-4.4.28.orig/arch/sparc/Kconfig 2016-10-28 09:53:25.000000000 +0200
  1564. +++ linux-4.4.28/arch/sparc/Kconfig 2016-11-01 21:26:57.460257444 +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.28.orig/arch/sparc/kernel/irq_64.c linux-4.4.28/arch/sparc/kernel/irq_64.c
  1578. --- linux-4.4.28.orig/arch/sparc/kernel/irq_64.c 2016-10-28 09:53:25.000000000 +0200
  1579. +++ linux-4.4.28/arch/sparc/kernel/irq_64.c 2016-11-01 21:26:57.488259255 +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.28.orig/arch/x86/crypto/aesni-intel_glue.c linux-4.4.28/arch/x86/crypto/aesni-intel_glue.c
  1595. --- linux-4.4.28.orig/arch/x86/crypto/aesni-intel_glue.c 2016-10-28 09:53:25.000000000 +0200
  1596. +++ linux-4.4.28/arch/x86/crypto/aesni-intel_glue.c 2016-11-01 21:26:57.524261570 +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.28.orig/arch/x86/crypto/cast5_avx_glue.c linux-4.4.28/arch/x86/crypto/cast5_avx_glue.c
  1680. --- linux-4.4.28.orig/arch/x86/crypto/cast5_avx_glue.c 2016-10-28 09:53:25.000000000 +0200
  1681. +++ linux-4.4.28/arch/x86/crypto/cast5_avx_glue.c 2016-11-01 21:26:57.528261834 +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.28.orig/arch/x86/crypto/glue_helper.c linux-4.4.28/arch/x86/crypto/glue_helper.c
  1752. --- linux-4.4.28.orig/arch/x86/crypto/glue_helper.c 2016-10-28 09:53:25.000000000 +0200
  1753. +++ linux-4.4.28/arch/x86/crypto/glue_helper.c 2016-11-01 21:26:57.536262350 +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.28.orig/arch/x86/entry/common.c linux-4.4.28/arch/x86/entry/common.c
  1854. --- linux-4.4.28.orig/arch/x86/entry/common.c 2016-10-28 09:53:25.000000000 +0200
  1855. +++ linux-4.4.28/arch/x86/entry/common.c 2016-11-01 21:26:57.544262865 +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.28.orig/arch/x86/entry/entry_32.S linux-4.4.28/arch/x86/entry/entry_32.S
  1879. --- linux-4.4.28.orig/arch/x86/entry/entry_32.S 2016-10-28 09:53:25.000000000 +0200
  1880. +++ linux-4.4.28/arch/x86/entry/entry_32.S 2016-11-01 21:26:57.548263117 +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.28.orig/arch/x86/entry/entry_64.S linux-4.4.28/arch/x86/entry/entry_64.S
  1907. --- linux-4.4.28.orig/arch/x86/entry/entry_64.S 2016-10-28 09:53:25.000000000 +0200
  1908. +++ linux-4.4.28/arch/x86/entry/entry_64.S 2016-11-01 21:26:57.548263117 +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.28.orig/arch/x86/include/asm/preempt.h linux-4.4.28/arch/x86/include/asm/preempt.h
  1948. --- linux-4.4.28.orig/arch/x86/include/asm/preempt.h 2016-10-28 09:53:25.000000000 +0200
  1949. +++ linux-4.4.28/arch/x86/include/asm/preempt.h 2016-11-01 21:26:57.556263632 +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.28.orig/arch/x86/include/asm/signal.h linux-4.4.28/arch/x86/include/asm/signal.h
  1997. --- linux-4.4.28.orig/arch/x86/include/asm/signal.h 2016-10-28 09:53:25.000000000 +0200
  1998. +++ linux-4.4.28/arch/x86/include/asm/signal.h 2016-11-01 21:26:57.556263632 +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.28.orig/arch/x86/include/asm/stackprotector.h linux-4.4.28/arch/x86/include/asm/stackprotector.h
  2019. --- linux-4.4.28.orig/arch/x86/include/asm/stackprotector.h 2016-10-28 09:53:25.000000000 +0200
  2020. +++ linux-4.4.28/arch/x86/include/asm/stackprotector.h 2016-11-01 21:26:57.560263897 +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.28.orig/arch/x86/include/asm/thread_info.h linux-4.4.28/arch/x86/include/asm/thread_info.h
  2045. --- linux-4.4.28.orig/arch/x86/include/asm/thread_info.h 2016-10-28 09:53:25.000000000 +0200
  2046. +++ linux-4.4.28/arch/x86/include/asm/thread_info.h 2016-11-01 21:26:57.560263897 +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.28.orig/arch/x86/include/asm/uv/uv_bau.h linux-4.4.28/arch/x86/include/asm/uv/uv_bau.h
  2080. --- linux-4.4.28.orig/arch/x86/include/asm/uv/uv_bau.h 2016-10-28 09:53:25.000000000 +0200
  2081. +++ linux-4.4.28/arch/x86/include/asm/uv/uv_bau.h 2016-11-01 21:26:57.568264412 +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.28.orig/arch/x86/include/asm/uv/uv_hub.h linux-4.4.28/arch/x86/include/asm/uv/uv_hub.h
  2115. --- linux-4.4.28.orig/arch/x86/include/asm/uv/uv_hub.h 2016-10-28 09:53:25.000000000 +0200
  2116. +++ linux-4.4.28/arch/x86/include/asm/uv/uv_hub.h 2016-11-01 21:26:57.568264412 +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.28.orig/arch/x86/Kconfig linux-4.4.28/arch/x86/Kconfig
  2127. --- linux-4.4.28.orig/arch/x86/Kconfig 2016-10-28 09:53:25.000000000 +0200
  2128. +++ linux-4.4.28/arch/x86/Kconfig 2016-11-01 21:26:57.508260538 +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.28.orig/arch/x86/kernel/acpi/boot.c linux-4.4.28/arch/x86/kernel/acpi/boot.c
  2158. --- linux-4.4.28.orig/arch/x86/kernel/acpi/boot.c 2016-10-28 09:53:25.000000000 +0200
  2159. +++ linux-4.4.28/arch/x86/kernel/acpi/boot.c 2016-11-01 21:26:57.580265179 +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.28.orig/arch/x86/kernel/apic/io_apic.c linux-4.4.28/arch/x86/kernel/apic/io_apic.c
  2170. --- linux-4.4.28.orig/arch/x86/kernel/apic/io_apic.c 2016-10-28 09:53:25.000000000 +0200
  2171. +++ linux-4.4.28/arch/x86/kernel/apic/io_apic.c 2016-11-01 21:26:57.600266475 +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.28.orig/arch/x86/kernel/apic/x2apic_uv_x.c linux-4.4.28/arch/x86/kernel/apic/x2apic_uv_x.c
  2183. --- linux-4.4.28.orig/arch/x86/kernel/apic/x2apic_uv_x.c 2016-10-28 09:53:25.000000000 +0200
  2184. +++ linux-4.4.28/arch/x86/kernel/apic/x2apic_uv_x.c 2016-11-01 21:26:57.612267242 +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.28.orig/arch/x86/kernel/asm-offsets.c linux-4.4.28/arch/x86/kernel/asm-offsets.c
  2195. --- linux-4.4.28.orig/arch/x86/kernel/asm-offsets.c 2016-10-28 09:53:25.000000000 +0200
  2196. +++ linux-4.4.28/arch/x86/kernel/asm-offsets.c 2016-11-01 21:26:57.612267242 +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.28.orig/arch/x86/kernel/cpu/mcheck/mce.c linux-4.4.28/arch/x86/kernel/cpu/mcheck/mce.c
  2210. --- linux-4.4.28.orig/arch/x86/kernel/cpu/mcheck/mce.c 2016-10-28 09:53:25.000000000 +0200
  2211. +++ linux-4.4.28/arch/x86/kernel/cpu/mcheck/mce.c 2016-11-01 21:26:57.624268022 +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.28.orig/arch/x86/kernel/cpu/perf_event_intel_rapl.c linux-4.4.28/arch/x86/kernel/cpu/perf_event_intel_rapl.c
  2426. --- linux-4.4.28.orig/arch/x86/kernel/cpu/perf_event_intel_rapl.c 2016-10-28 09:53:25.000000000 +0200
  2427. +++ linux-4.4.28/arch/x86/kernel/cpu/perf_event_intel_rapl.c 2016-11-01 21:26:57.636268789 +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.28.orig/arch/x86/kernel/dumpstack_32.c linux-4.4.28/arch/x86/kernel/dumpstack_32.c
  2491. --- linux-4.4.28.orig/arch/x86/kernel/dumpstack_32.c 2016-10-28 09:53:25.000000000 +0200
  2492. +++ linux-4.4.28/arch/x86/kernel/dumpstack_32.c 2016-11-01 21:26:57.648269569 +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.28.orig/arch/x86/kernel/dumpstack_64.c linux-4.4.28/arch/x86/kernel/dumpstack_64.c
  2510. --- linux-4.4.28.orig/arch/x86/kernel/dumpstack_64.c 2016-10-28 09:53:25.000000000 +0200
  2511. +++ linux-4.4.28/arch/x86/kernel/dumpstack_64.c 2016-11-01 21:26:57.648269569 +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.28.orig/arch/x86/kernel/irq_32.c linux-4.4.28/arch/x86/kernel/irq_32.c
  2545. --- linux-4.4.28.orig/arch/x86/kernel/irq_32.c 2016-10-28 09:53:25.000000000 +0200
  2546. +++ linux-4.4.28/arch/x86/kernel/irq_32.c 2016-11-01 21:26:57.652269821 +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.28.orig/arch/x86/kernel/kvm.c linux-4.4.28/arch/x86/kernel/kvm.c
  2561. --- linux-4.4.28.orig/arch/x86/kernel/kvm.c 2016-10-28 09:53:25.000000000 +0200
  2562. +++ linux-4.4.28/arch/x86/kernel/kvm.c 2016-11-01 21:26:57.652269821 +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.28.orig/arch/x86/kernel/nmi.c linux-4.4.28/arch/x86/kernel/nmi.c
  2690. --- linux-4.4.28.orig/arch/x86/kernel/nmi.c 2016-10-28 09:53:25.000000000 +0200
  2691. +++ linux-4.4.28/arch/x86/kernel/nmi.c 2016-11-01 21:26:57.660270336 +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.28.orig/arch/x86/kernel/process_32.c linux-4.4.28/arch/x86/kernel/process_32.c
  2723. --- linux-4.4.28.orig/arch/x86/kernel/process_32.c 2016-10-28 09:53:25.000000000 +0200
  2724. +++ linux-4.4.28/arch/x86/kernel/process_32.c 2016-11-01 21:26:57.664270598 +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.28.orig/arch/x86/kernel/reboot.c linux-4.4.28/arch/x86/kernel/reboot.c
  2775. --- linux-4.4.28.orig/arch/x86/kernel/reboot.c 2016-10-28 09:53:25.000000000 +0200
  2776. +++ linux-4.4.28/arch/x86/kernel/reboot.c 2016-11-01 21:26:57.664270598 +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.28.orig/arch/x86/kvm/lapic.c linux-4.4.28/arch/x86/kvm/lapic.c
  2814. --- linux-4.4.28.orig/arch/x86/kvm/lapic.c 2016-10-28 09:53:25.000000000 +0200
  2815. +++ linux-4.4.28/arch/x86/kvm/lapic.c 2016-11-01 21:26:57.672271115 +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.28.orig/arch/x86/kvm/x86.c linux-4.4.28/arch/x86/kvm/x86.c
  2841. --- linux-4.4.28.orig/arch/x86/kvm/x86.c 2016-10-28 09:53:25.000000000 +0200
  2842. +++ linux-4.4.28/arch/x86/kvm/x86.c 2016-11-01 21:26:57.732274981 +0100
  2843. @@ -5792,6 +5792,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.28.orig/arch/x86/mm/highmem_32.c linux-4.4.28/arch/x86/mm/highmem_32.c
  2857. --- linux-4.4.28.orig/arch/x86/mm/highmem_32.c 2016-10-28 09:53:25.000000000 +0200
  2858. +++ linux-4.4.28/arch/x86/mm/highmem_32.c 2016-11-01 21:26:57.740275502 +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.28.orig/arch/x86/mm/iomap_32.c linux-4.4.28/arch/x86/mm/iomap_32.c
  2899. --- linux-4.4.28.orig/arch/x86/mm/iomap_32.c 2016-10-28 09:53:25.000000000 +0200
  2900. +++ linux-4.4.28/arch/x86/mm/iomap_32.c 2016-11-01 21:26:57.740275502 +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.28.orig/arch/x86/platform/uv/tlb_uv.c linux-4.4.28/arch/x86/platform/uv/tlb_uv.c
  2931. --- linux-4.4.28.orig/arch/x86/platform/uv/tlb_uv.c 2016-10-28 09:53:25.000000000 +0200
  2932. +++ linux-4.4.28/arch/x86/platform/uv/tlb_uv.c 2016-11-01 21:26:57.768277302 +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.28.orig/arch/x86/platform/uv/uv_time.c linux-4.4.28/arch/x86/platform/uv/uv_time.c
  3006. --- linux-4.4.28.orig/arch/x86/platform/uv/uv_time.c 2016-10-28 09:53:25.000000000 +0200
  3007. +++ linux-4.4.28/arch/x86/platform/uv/uv_time.c 2016-11-01 21:26:57.768277302 +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.28.orig/block/blk-core.c linux-4.4.28/block/blk-core.c
  3077. --- linux-4.4.28.orig/block/blk-core.c 2016-10-28 09:53:25.000000000 +0200
  3078. +++ linux-4.4.28/block/blk-core.c 2016-11-01 21:26:57.788278596 +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.28.orig/block/blk-ioc.c linux-4.4.28/block/blk-ioc.c
  3162. --- linux-4.4.28.orig/block/blk-ioc.c 2016-10-28 09:53:25.000000000 +0200
  3163. +++ linux-4.4.28/block/blk-ioc.c 2016-11-01 21:26:57.800279369 +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.28.orig/block/blk-iopoll.c linux-4.4.28/block/blk-iopoll.c
  3189. --- linux-4.4.28.orig/block/blk-iopoll.c 2016-10-28 09:53:25.000000000 +0200
  3190. +++ linux-4.4.28/block/blk-iopoll.c 2016-11-01 21:26:57.800279369 +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.28.orig/block/blk-mq.c linux-4.4.28/block/blk-mq.c
  3212. --- linux-4.4.28.orig/block/blk-mq.c 2016-10-28 09:53:25.000000000 +0200
  3213. +++ linux-4.4.28/block/blk-mq.c 2016-11-01 21:26:57.812280143 +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. @@ -864,14 +884,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. @@ -1619,7 +1639,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.28.orig/block/blk-mq-cpu.c linux-4.4.28/block/blk-mq-cpu.c
  3317. --- linux-4.4.28.orig/block/blk-mq-cpu.c 2016-10-28 09:53:25.000000000 +0200
  3318. +++ linux-4.4.28/block/blk-mq-cpu.c 2016-11-01 21:26:57.800279369 +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.28.orig/block/blk-mq.h linux-4.4.28/block/blk-mq.h
  3362. --- linux-4.4.28.orig/block/blk-mq.h 2016-10-28 09:53:25.000000000 +0200
  3363. +++ linux-4.4.28/block/blk-mq.h 2016-11-01 21:26:57.812280143 +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.28.orig/block/blk-softirq.c linux-4.4.28/block/blk-softirq.c
  3389. --- linux-4.4.28.orig/block/blk-softirq.c 2016-10-28 09:53:25.000000000 +0200
  3390. +++ linux-4.4.28/block/blk-softirq.c 2016-11-01 21:26:57.812280143 +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.28.orig/block/bounce.c linux-4.4.28/block/bounce.c
  3411. --- linux-4.4.28.orig/block/bounce.c 2016-10-28 09:53:25.000000000 +0200
  3412. +++ linux-4.4.28/block/bounce.c 2016-11-01 21:26:57.812280143 +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.28.orig/crypto/algapi.c linux-4.4.28/crypto/algapi.c
  3426. --- linux-4.4.28.orig/crypto/algapi.c 2016-10-28 09:53:25.000000000 +0200
  3427. +++ linux-4.4.28/crypto/algapi.c 2016-11-01 21:26:57.824280917 +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.28.orig/crypto/api.c linux-4.4.28/crypto/api.c
  3442. --- linux-4.4.28.orig/crypto/api.c 2016-10-28 09:53:25.000000000 +0200
  3443. +++ linux-4.4.28/crypto/api.c 2016-11-01 21:26:57.828281169 +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.28.orig/crypto/internal.h linux-4.4.28/crypto/internal.h
  3463. --- linux-4.4.28.orig/crypto/internal.h 2016-10-28 09:53:25.000000000 +0200
  3464. +++ linux-4.4.28/crypto/internal.h 2016-11-01 21:26:57.832281426 +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.28.orig/Documentation/hwlat_detector.txt linux-4.4.28/Documentation/hwlat_detector.txt
  3480. --- linux-4.4.28.orig/Documentation/hwlat_detector.txt 1970-01-01 01:00:00.000000000 +0100
  3481. +++ linux-4.4.28/Documentation/hwlat_detector.txt 2016-11-01 21:26:56.600202005 +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.28.orig/Documentation/kernel-parameters.txt linux-4.4.28/Documentation/kernel-parameters.txt
  3548. --- linux-4.4.28.orig/Documentation/kernel-parameters.txt 2016-10-28 09:53:25.000000000 +0200
  3549. +++ linux-4.4.28/Documentation/kernel-parameters.txt 2016-11-01 21:26:56.640204584 +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.28.orig/Documentation/sysrq.txt linux-4.4.28/Documentation/sysrq.txt
  3566. --- linux-4.4.28.orig/Documentation/sysrq.txt 2016-10-28 09:53:25.000000000 +0200
  3567. +++ linux-4.4.28/Documentation/sysrq.txt 2016-11-01 21:26:56.652205360 +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.28.orig/Documentation/trace/histograms.txt linux-4.4.28/Documentation/trace/histograms.txt
  3587. --- linux-4.4.28.orig/Documentation/trace/histograms.txt 1970-01-01 01:00:00.000000000 +0100
  3588. +++ linux-4.4.28/Documentation/trace/histograms.txt 2016-11-01 21:26:56.672206655 +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.28.orig/drivers/acpi/acpica/acglobal.h linux-4.4.28/drivers/acpi/acpica/acglobal.h
  3777. --- linux-4.4.28.orig/drivers/acpi/acpica/acglobal.h 2016-10-28 09:53:25.000000000 +0200
  3778. +++ linux-4.4.28/drivers/acpi/acpica/acglobal.h 2016-11-01 21:26:57.844282199 +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.28.orig/drivers/acpi/acpica/hwregs.c linux-4.4.28/drivers/acpi/acpica/hwregs.c
  3788. --- linux-4.4.28.orig/drivers/acpi/acpica/hwregs.c 2016-10-28 09:53:25.000000000 +0200
  3789. +++ linux-4.4.28/drivers/acpi/acpica/hwregs.c 2016-11-01 21:26:57.852282717 +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.28.orig/drivers/acpi/acpica/hwxface.c linux-4.4.28/drivers/acpi/acpica/hwxface.c
  3803. --- linux-4.4.28.orig/drivers/acpi/acpica/hwxface.c 2016-10-28 09:53:25.000000000 +0200
  3804. +++ linux-4.4.28/drivers/acpi/acpica/hwxface.c 2016-11-01 21:26:57.852282717 +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.28.orig/drivers/acpi/acpica/utmutex.c linux-4.4.28/drivers/acpi/acpica/utmutex.c
  3819. --- linux-4.4.28.orig/drivers/acpi/acpica/utmutex.c 2016-10-28 09:53:25.000000000 +0200
  3820. +++ linux-4.4.28/drivers/acpi/acpica/utmutex.c 2016-11-01 21:26:57.864283490 +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.28.orig/drivers/ata/libata-sff.c linux-4.4.28/drivers/ata/libata-sff.c
  3837. --- linux-4.4.28.orig/drivers/ata/libata-sff.c 2016-10-28 09:53:25.000000000 +0200
  3838. +++ linux-4.4.28/drivers/ata/libata-sff.c 2016-11-01 21:26:57.884284784 +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.28.orig/drivers/block/zram/zram_drv.c linux-4.4.28/drivers/block/zram/zram_drv.c
  3880. --- linux-4.4.28.orig/drivers/block/zram/zram_drv.c 2016-10-28 09:53:25.000000000 +0200
  3881. +++ linux-4.4.28/drivers/block/zram/zram_drv.c 2016-11-01 21:26:57.892285294 +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.28.orig/drivers/block/zram/zram_drv.h linux-4.4.28/drivers/block/zram/zram_drv.h
  3973. --- linux-4.4.28.orig/drivers/block/zram/zram_drv.h 2016-10-28 09:53:25.000000000 +0200
  3974. +++ linux-4.4.28/drivers/block/zram/zram_drv.h 2016-11-01 21:26:57.892285294 +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.28.orig/drivers/char/random.c linux-4.4.28/drivers/char/random.c
  4028. --- linux-4.4.28.orig/drivers/char/random.c 2016-10-28 09:53:25.000000000 +0200
  4029. +++ linux-4.4.28/drivers/char/random.c 2016-11-01 21:26:57.900285811 +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.28.orig/drivers/clk/at91/clk-generated.c linux-4.4.28/drivers/clk/at91/clk-generated.c
  4077. --- linux-4.4.28.orig/drivers/clk/at91/clk-generated.c 2016-10-28 09:53:25.000000000 +0200
  4078. +++ linux-4.4.28/drivers/clk/at91/clk-generated.c 2016-11-01 21:26:57.916286841 +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.28.orig/drivers/clk/at91/clk-h32mx.c linux-4.4.28/drivers/clk/at91/clk-h32mx.c
  4246. --- linux-4.4.28.orig/drivers/clk/at91/clk-h32mx.c 2016-10-28 09:53:25.000000000 +0200
  4247. +++ linux-4.4.28/drivers/clk/at91/clk-h32mx.c 2016-11-01 21:26:57.916286841 +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.28.orig/drivers/clk/at91/clk-main.c linux-4.4.28/drivers/clk/at91/clk-main.c
  4334. --- linux-4.4.28.orig/drivers/clk/at91/clk-main.c 2016-10-28 09:53:25.000000000 +0200
  4335. +++ linux-4.4.28/drivers/clk/at91/clk-main.c 2016-11-01 21:26:57.920287105 +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.28.orig/drivers/clk/at91/clk-master.c linux-4.4.28/drivers/clk/at91/clk-master.c
  4925. --- linux-4.4.28.orig/drivers/clk/at91/clk-master.c 2016-10-28 09:53:25.000000000 +0200
  4926. +++ linux-4.4.28/drivers/clk/at91/clk-master.c 2016-11-01 21:26:57.920287105 +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.28.orig/drivers/clk/at91/clk-peripheral.c linux-4.4.28/drivers/clk/at91/clk-peripheral.c
  5106. --- linux-4.4.28.orig/drivers/clk/at91/clk-peripheral.c 2016-10-28 09:53:25.000000000 +0200
  5107. +++ linux-4.4.28/drivers/clk/at91/clk-peripheral.c 2016-11-01 21:26:57.920287105 +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.28.orig/drivers/clk/at91/clk-pll.c linux-4.4.28/drivers/clk/at91/clk-pll.c
  5394. --- linux-4.4.28.orig/drivers/clk/at91/clk-pll.c 2016-10-28 09:53:25.000000000 +0200
  5395. +++ linux-4.4.28/drivers/clk/at91/clk-pll.c 2016-11-01 21:26:57.924287358 +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.28.orig/drivers/clk/at91/clk-plldiv.c linux-4.4.28/drivers/clk/at91/clk-plldiv.c
  5648. --- linux-4.4.28.orig/drivers/clk/at91/clk-plldiv.c 2016-10-28 09:53:25.000000000 +0200
  5649. +++ linux-4.4.28/drivers/clk/at91/clk-plldiv.c 2016-11-01 21:26:57.924287358 +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.28.orig/drivers/clk/at91/clk-programmable.c linux-4.4.28/drivers/clk/at91/clk-programmable.c
  5741. --- linux-4.4.28.orig/drivers/clk/at91/clk-programmable.c 2016-10-28 09:53:25.000000000 +0200
  5742. +++ linux-4.4.28/drivers/clk/at91/clk-programmable.c 2016-11-01 21:26:57.936288132 +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.28.orig/drivers/clk/at91/clk-slow.c linux-4.4.28/drivers/clk/at91/clk-slow.c
  5938. --- linux-4.4.28.orig/drivers/clk/at91/clk-slow.c 2016-10-28 09:53:25.000000000 +0200
  5939. +++ linux-4.4.28/drivers/clk/at91/clk-slow.c 2016-11-01 21:26:57.940288388 +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.28.orig/drivers/clk/at91/clk-smd.c linux-4.4.28/drivers/clk/at91/clk-smd.c
  6031. --- linux-4.4.28.orig/drivers/clk/at91/clk-smd.c 2016-10-28 09:53:25.000000000 +0200
  6032. +++ linux-4.4.28/drivers/clk/at91/clk-smd.c 2016-11-01 21:26:57.940288388 +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.28.orig/drivers/clk/at91/clk-system.c linux-4.4.28/drivers/clk/at91/clk-system.c
  6156. --- linux-4.4.28.orig/drivers/clk/at91/clk-system.c 2016-10-28 09:53:25.000000000 +0200
  6157. +++ linux-4.4.28/drivers/clk/at91/clk-system.c 2016-11-01 21:26:57.940288388 +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.28.orig/drivers/clk/at91/clk-usb.c linux-4.4.28/drivers/clk/at91/clk-usb.c
  6329. --- linux-4.4.28.orig/drivers/clk/at91/clk-usb.c 2016-10-28 09:53:25.000000000 +0200
  6330. +++ linux-4.4.28/drivers/clk/at91/clk-usb.c 2016-11-01 21:26:57.940288388 +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.28.orig/drivers/clk/at91/clk-utmi.c linux-4.4.28/drivers/clk/at91/clk-utmi.c
  6610. --- linux-4.4.28.orig/drivers/clk/at91/clk-utmi.c 2016-10-28 09:53:25.000000000 +0200
  6611. +++ linux-4.4.28/drivers/clk/at91/clk-utmi.c 2016-11-01 21:26:57.940288388 +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.28.orig/drivers/clk/at91/pmc.c linux-4.4.28/drivers/clk/at91/pmc.c
  6752. --- linux-4.4.28.orig/drivers/clk/at91/pmc.c 2016-10-28 09:53:25.000000000 +0200
  6753. +++ linux-4.4.28/drivers/clk/at91/pmc.c 2016-11-01 21:26:57.940288388 +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.28.orig/drivers/clk/at91/pmc.h linux-4.4.28/drivers/clk/at91/pmc.h
  7194. --- linux-4.4.28.orig/drivers/clk/at91/pmc.h 2016-10-28 09:53:25.000000000 +0200
  7195. +++ linux-4.4.28/drivers/clk/at91/pmc.h 2016-11-01 21:26:57.944288652 +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.28.orig/drivers/clocksource/tcb_clksrc.c linux-4.4.28/drivers/clocksource/tcb_clksrc.c
  7307. --- linux-4.4.28.orig/drivers/clocksource/tcb_clksrc.c 2016-10-28 09:53:25.000000000 +0200
  7308. +++ linux-4.4.28/drivers/clocksource/tcb_clksrc.c 2016-11-01 21:26:57.948288905 +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.28.orig/drivers/clocksource/timer-atmel-pit.c linux-4.4.28/drivers/clocksource/timer-atmel-pit.c
  7450. --- linux-4.4.28.orig/drivers/clocksource/timer-atmel-pit.c 2016-10-28 09:53:25.000000000 +0200
  7451. +++ linux-4.4.28/drivers/clocksource/timer-atmel-pit.c 2016-11-01 21:26:57.952289162 +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.28.orig/drivers/clocksource/timer-atmel-st.c linux-4.4.28/drivers/clocksource/timer-atmel-st.c
  7507. --- linux-4.4.28.orig/drivers/clocksource/timer-atmel-st.c 2016-10-28 09:53:25.000000000 +0200
  7508. +++ linux-4.4.28/drivers/clocksource/timer-atmel-st.c 2016-11-01 21:26:57.952289162 +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.28.orig/drivers/cpufreq/Kconfig.x86 linux-4.4.28/drivers/cpufreq/Kconfig.x86
  7578. --- linux-4.4.28.orig/drivers/cpufreq/Kconfig.x86 2016-10-28 09:53:25.000000000 +0200
  7579. +++ linux-4.4.28/drivers/cpufreq/Kconfig.x86 2016-11-01 21:26:57.956289426 +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.28.orig/drivers/cpuidle/coupled.c linux-4.4.28/drivers/cpuidle/coupled.c
  7589. --- linux-4.4.28.orig/drivers/cpuidle/coupled.c 2016-10-28 09:53:25.000000000 +0200
  7590. +++ linux-4.4.28/drivers/cpuidle/coupled.c 2016-11-01 21:26:57.960289679 +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.28.orig/drivers/gpu/drm/i915/i915_gem_execbuffer.c linux-4.4.28/drivers/gpu/drm/i915/i915_gem_execbuffer.c
  7597. --- linux-4.4.28.orig/drivers/gpu/drm/i915/i915_gem_execbuffer.c 2016-10-28 09:53:25.000000000 +0200
  7598. +++ linux-4.4.28/drivers/gpu/drm/i915/i915_gem_execbuffer.c 2016-11-01 21:26:57.980290973 +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.28.orig/drivers/gpu/drm/i915/i915_gem_shrinker.c linux-4.4.28/drivers/gpu/drm/i915/i915_gem_shrinker.c
  7608. --- linux-4.4.28.orig/drivers/gpu/drm/i915/i915_gem_shrinker.c 2016-10-28 09:53:25.000000000 +0200
  7609. +++ linux-4.4.28/drivers/gpu/drm/i915/i915_gem_shrinker.c 2016-11-01 21:26:57.984291226 +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.28.orig/drivers/gpu/drm/i915/i915_irq.c linux-4.4.28/drivers/gpu/drm/i915/i915_irq.c
  7619. --- linux-4.4.28.orig/drivers/gpu/drm/i915/i915_irq.c 2016-10-28 09:53:25.000000000 +0200
  7620. +++ linux-4.4.28/drivers/gpu/drm/i915/i915_irq.c 2016-11-01 21:26:58.000292256 +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.28.orig/drivers/gpu/drm/i915/intel_display.c linux-4.4.28/drivers/gpu/drm/i915/intel_display.c
  7633. --- linux-4.4.28.orig/drivers/gpu/drm/i915/intel_display.c 2016-10-28 09:53:25.000000000 +0200
  7634. +++ linux-4.4.28/drivers/gpu/drm/i915/intel_display.c 2016-11-01 21:26:58.040294841 +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.28.orig/drivers/gpu/drm/i915/intel_sprite.c linux-4.4.28/drivers/gpu/drm/i915/intel_sprite.c
  7643. --- linux-4.4.28.orig/drivers/gpu/drm/i915/intel_sprite.c 2016-10-28 09:53:25.000000000 +0200
  7644. +++ linux-4.4.28/drivers/gpu/drm/i915/intel_sprite.c 2016-11-01 21:26:58.056295867 +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.28.orig/drivers/gpu/drm/radeon/radeon_display.c linux-4.4.28/drivers/gpu/drm/radeon/radeon_display.c
  7684. --- linux-4.4.28.orig/drivers/gpu/drm/radeon/radeon_display.c 2016-10-28 09:53:25.000000000 +0200
  7685. +++ linux-4.4.28/drivers/gpu/drm/radeon/radeon_display.c 2016-11-01 21:26:58.068296641 +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.28.orig/drivers/hv/vmbus_drv.c linux-4.4.28/drivers/hv/vmbus_drv.c
  7699. --- linux-4.4.28.orig/drivers/hv/vmbus_drv.c 2016-10-28 09:53:25.000000000 +0200
  7700. +++ linux-4.4.28/drivers/hv/vmbus_drv.c 2016-11-01 21:26:58.080297414 +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.28.orig/drivers/i2c/busses/i2c-omap.c linux-4.4.28/drivers/i2c/busses/i2c-omap.c
  7708. --- linux-4.4.28.orig/drivers/i2c/busses/i2c-omap.c 2016-10-28 09:53:25.000000000 +0200
  7709. +++ linux-4.4.28/drivers/i2c/busses/i2c-omap.c 2016-11-01 21:26:58.088297935 +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.28.orig/drivers/ide/alim15x3.c linux-4.4.28/drivers/ide/alim15x3.c
  7724. --- linux-4.4.28.orig/drivers/ide/alim15x3.c 2016-10-28 09:53:25.000000000 +0200
  7725. +++ linux-4.4.28/drivers/ide/alim15x3.c 2016-11-01 21:26:58.096298444 +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.28.orig/drivers/ide/hpt366.c linux-4.4.28/drivers/ide/hpt366.c
  7741. --- linux-4.4.28.orig/drivers/ide/hpt366.c 2016-10-28 09:53:25.000000000 +0200
  7742. +++ linux-4.4.28/drivers/ide/hpt366.c 2016-11-01 21:26:58.100298709 +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.28.orig/drivers/ide/ide-io.c linux-4.4.28/drivers/ide/ide-io.c
  7757. --- linux-4.4.28.orig/drivers/ide/ide-io.c 2016-10-28 09:53:25.000000000 +0200
  7758. +++ linux-4.4.28/drivers/ide/ide-io.c 2016-11-01 21:26:58.108299218 +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.28.orig/drivers/ide/ide-iops.c linux-4.4.28/drivers/ide/ide-iops.c
  7769. --- linux-4.4.28.orig/drivers/ide/ide-iops.c 2016-10-28 09:53:25.000000000 +0200
  7770. +++ linux-4.4.28/drivers/ide/ide-iops.c 2016-11-01 21:26:58.108299218 +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.28.orig/drivers/ide/ide-io-std.c linux-4.4.28/drivers/ide/ide-io-std.c
  7786. --- linux-4.4.28.orig/drivers/ide/ide-io-std.c 2016-10-28 09:53:25.000000000 +0200
  7787. +++ linux-4.4.28/drivers/ide/ide-io-std.c 2016-11-01 21:26:58.104298962 +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.28.orig/drivers/ide/ide-probe.c linux-4.4.28/drivers/ide/ide-probe.c
  7817. --- linux-4.4.28.orig/drivers/ide/ide-probe.c 2016-10-28 09:53:25.000000000 +0200
  7818. +++ linux-4.4.28/drivers/ide/ide-probe.c 2016-11-01 21:26:58.116299735 +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.28.orig/drivers/ide/ide-taskfile.c linux-4.4.28/drivers/ide/ide-taskfile.c
  7831. --- linux-4.4.28.orig/drivers/ide/ide-taskfile.c 2016-10-28 09:53:25.000000000 +0200
  7832. +++ linux-4.4.28/drivers/ide/ide-taskfile.c 2016-11-01 21:26:58.116299735 +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.28.orig/drivers/infiniband/ulp/ipoib/ipoib_multicast.c linux-4.4.28/drivers/infiniband/ulp/ipoib/ipoib_multicast.c
  7853. --- linux-4.4.28.orig/drivers/infiniband/ulp/ipoib/ipoib_multicast.c 2016-10-28 09:53:25.000000000 +0200
  7854. +++ linux-4.4.28/drivers/infiniband/ulp/ipoib/ipoib_multicast.c 2016-11-01 21:26:58.124300256 +0100
  7855. @@ -857,7 +857,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. @@ -939,7 +939,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.28.orig/drivers/input/gameport/gameport.c linux-4.4.28/drivers/input/gameport/gameport.c
  7869. --- linux-4.4.28.orig/drivers/input/gameport/gameport.c 2016-10-28 09:53:25.000000000 +0200
  7870. +++ linux-4.4.28/drivers/input/gameport/gameport.c 2016-11-01 21:26:58.132300765 +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.28.orig/drivers/iommu/amd_iommu.c linux-4.4.28/drivers/iommu/amd_iommu.c
  7914. --- linux-4.4.28.orig/drivers/iommu/amd_iommu.c 2016-10-28 09:53:25.000000000 +0200
  7915. +++ linux-4.4.28/drivers/iommu/amd_iommu.c 2016-11-01 21:26:58.156302312 +0100
  7916. @@ -2019,10 +2019,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. @@ -2185,10 +2185,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.28.orig/drivers/leds/trigger/Kconfig linux-4.4.28/drivers/leds/trigger/Kconfig
  7941. --- linux-4.4.28.orig/drivers/leds/trigger/Kconfig 2016-10-28 09:53:25.000000000 +0200
  7942. +++ linux-4.4.28/drivers/leds/trigger/Kconfig 2016-11-01 21:26:58.168303087 +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.28.orig/drivers/md/bcache/Kconfig linux-4.4.28/drivers/md/bcache/Kconfig
  7952. --- linux-4.4.28.orig/drivers/md/bcache/Kconfig 2016-10-28 09:53:25.000000000 +0200
  7953. +++ linux-4.4.28/drivers/md/bcache/Kconfig 2016-11-01 21:26:58.172303343 +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.28.orig/drivers/md/dm.c linux-4.4.28/drivers/md/dm.c
  7962. --- linux-4.4.28.orig/drivers/md/dm.c 2016-10-28 09:53:25.000000000 +0200
  7963. +++ linux-4.4.28/drivers/md/dm.c 2016-11-01 21:26:58.196304892 +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.28.orig/drivers/md/raid5.c linux-4.4.28/drivers/md/raid5.c
  7973. --- linux-4.4.28.orig/drivers/md/raid5.c 2016-10-28 09:53:25.000000000 +0200
  7974. +++ linux-4.4.28/drivers/md/raid5.c 2016-11-01 21:26:58.224306697 +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.28.orig/drivers/md/raid5.h linux-4.4.28/drivers/md/raid5.h
  8002. --- linux-4.4.28.orig/drivers/md/raid5.h 2016-10-28 09:53:25.000000000 +0200
  8003. +++ linux-4.4.28/drivers/md/raid5.h 2016-11-01 21:26:58.236307469 +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.28.orig/drivers/media/platform/vsp1/vsp1_video.c linux-4.4.28/drivers/media/platform/vsp1/vsp1_video.c
  8013. --- linux-4.4.28.orig/drivers/media/platform/vsp1/vsp1_video.c 2016-10-28 09:53:25.000000000 +0200
  8014. +++ linux-4.4.28/drivers/media/platform/vsp1/vsp1_video.c 2016-11-01 21:26:58.244307986 +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.28.orig/drivers/misc/hwlat_detector.c linux-4.4.28/drivers/misc/hwlat_detector.c
  8023. --- linux-4.4.28.orig/drivers/misc/hwlat_detector.c 1970-01-01 01:00:00.000000000 +0100
  8024. +++ linux-4.4.28/drivers/misc/hwlat_detector.c 2016-11-01 21:26:58.256308759 +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.28.orig/drivers/misc/Kconfig linux-4.4.28/drivers/misc/Kconfig
  9267. --- linux-4.4.28.orig/drivers/misc/Kconfig 2016-10-28 09:53:25.000000000 +0200
  9268. +++ linux-4.4.28/drivers/misc/Kconfig 2016-11-01 21:26:58.256308759 +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.28.orig/drivers/misc/Makefile linux-4.4.28/drivers/misc/Makefile
  9336. --- linux-4.4.28.orig/drivers/misc/Makefile 2016-10-28 09:53:25.000000000 +0200
  9337. +++ linux-4.4.28/drivers/misc/Makefile 2016-11-01 21:26:58.256308759 +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.28.orig/drivers/mmc/host/mmci.c linux-4.4.28/drivers/mmc/host/mmci.c
  9347. --- linux-4.4.28.orig/drivers/mmc/host/mmci.c 2016-10-28 09:53:25.000000000 +0200
  9348. +++ linux-4.4.28/drivers/mmc/host/mmci.c 2016-11-01 21:26:58.268309533 +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.28.orig/drivers/net/ethernet/3com/3c59x.c linux-4.4.28/drivers/net/ethernet/3com/3c59x.c
  9370. --- linux-4.4.28.orig/drivers/net/ethernet/3com/3c59x.c 2016-10-28 09:53:25.000000000 +0200
  9371. +++ linux-4.4.28/drivers/net/ethernet/3com/3c59x.c 2016-11-01 21:26:58.300311601 +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.28.orig/drivers/net/ethernet/atheros/atl1c/atl1c_main.c linux-4.4.28/drivers/net/ethernet/atheros/atl1c/atl1c_main.c
  9398. --- linux-4.4.28.orig/drivers/net/ethernet/atheros/atl1c/atl1c_main.c 2016-10-28 09:53:25.000000000 +0200
  9399. +++ linux-4.4.28/drivers/net/ethernet/atheros/atl1c/atl1c_main.c 2016-11-01 21:26:58.324313148 +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.28.orig/drivers/net/ethernet/atheros/atl1e/atl1e_main.c linux-4.4.28/drivers/net/ethernet/atheros/atl1e/atl1e_main.c
  9412. --- linux-4.4.28.orig/drivers/net/ethernet/atheros/atl1e/atl1e_main.c 2016-10-28 09:53:25.000000000 +0200
  9413. +++ linux-4.4.28/drivers/net/ethernet/atheros/atl1e/atl1e_main.c 2016-11-01 21:26:58.336313921 +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.28.orig/drivers/net/ethernet/chelsio/cxgb/sge.c linux-4.4.28/drivers/net/ethernet/chelsio/cxgb/sge.c
  9424. --- linux-4.4.28.orig/drivers/net/ethernet/chelsio/cxgb/sge.c 2016-10-28 09:53:25.000000000 +0200
  9425. +++ linux-4.4.28/drivers/net/ethernet/chelsio/cxgb/sge.c 2016-11-01 21:26:58.348314695 +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.28.orig/drivers/net/ethernet/neterion/s2io.c linux-4.4.28/drivers/net/ethernet/neterion/s2io.c
  9434. --- linux-4.4.28.orig/drivers/net/ethernet/neterion/s2io.c 2016-10-28 09:53:25.000000000 +0200
  9435. +++ linux-4.4.28/drivers/net/ethernet/neterion/s2io.c 2016-11-01 21:26:58.372316242 +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.28.orig/drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c linux-4.4.28/drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c
  9449. --- linux-4.4.28.orig/drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c 2016-10-28 09:53:25.000000000 +0200
  9450. +++ linux-4.4.28/drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c 2016-11-01 21:26:58.396317789 +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.28.orig/drivers/net/ethernet/realtek/8139too.c linux-4.4.28/drivers/net/ethernet/realtek/8139too.c
  9464. --- linux-4.4.28.orig/drivers/net/ethernet/realtek/8139too.c 2016-10-28 09:53:25.000000000 +0200
  9465. +++ linux-4.4.28/drivers/net/ethernet/realtek/8139too.c 2016-11-01 21:26:58.416319072 +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.28.orig/drivers/net/ethernet/tehuti/tehuti.c linux-4.4.28/drivers/net/ethernet/tehuti/tehuti.c
  9475. --- linux-4.4.28.orig/drivers/net/ethernet/tehuti/tehuti.c 2016-10-28 09:53:25.000000000 +0200
  9476. +++ linux-4.4.28/drivers/net/ethernet/tehuti/tehuti.c 2016-11-01 21:26:58.436320363 +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.28.orig/drivers/net/rionet.c linux-4.4.28/drivers/net/rionet.c
  9492. --- linux-4.4.28.orig/drivers/net/rionet.c 2016-10-28 09:53:25.000000000 +0200
  9493. +++ linux-4.4.28/drivers/net/rionet.c 2016-11-01 21:26:58.448321136 +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.28.orig/drivers/net/wireless/orinoco/orinoco_usb.c linux-4.4.28/drivers/net/wireless/orinoco/orinoco_usb.c
  9506. --- linux-4.4.28.orig/drivers/net/wireless/orinoco/orinoco_usb.c 2016-10-28 09:53:25.000000000 +0200
  9507. +++ linux-4.4.28/drivers/net/wireless/orinoco/orinoco_usb.c 2016-11-01 21:26:58.460321910 +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.28.orig/drivers/pci/access.c linux-4.4.28/drivers/pci/access.c
  9518. --- linux-4.4.28.orig/drivers/pci/access.c 2016-10-28 09:53:25.000000000 +0200
  9519. +++ linux-4.4.28/drivers/pci/access.c 2016-11-01 21:26:58.472322683 +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.28.orig/drivers/scsi/fcoe/fcoe.c linux-4.4.28/drivers/scsi/fcoe/fcoe.c
  9529. --- linux-4.4.28.orig/drivers/scsi/fcoe/fcoe.c 2016-10-28 09:53:25.000000000 +0200
  9530. +++ linux-4.4.28/drivers/scsi/fcoe/fcoe.c 2016-11-01 21:26:58.496324231 +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.28.orig/drivers/scsi/fcoe/fcoe_ctlr.c linux-4.4.28/drivers/scsi/fcoe/fcoe_ctlr.c
  9596. --- linux-4.4.28.orig/drivers/scsi/fcoe/fcoe_ctlr.c 2016-10-28 09:53:25.000000000 +0200
  9597. +++ linux-4.4.28/drivers/scsi/fcoe/fcoe_ctlr.c 2016-11-01 21:26:58.516325525 +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.28.orig/drivers/scsi/libfc/fc_exch.c linux-4.4.28/drivers/scsi/libfc/fc_exch.c
  9613. --- linux-4.4.28.orig/drivers/scsi/libfc/fc_exch.c 2016-10-28 09:53:25.000000000 +0200
  9614. +++ linux-4.4.28/drivers/scsi/libfc/fc_exch.c 2016-11-01 21:26:58.536326808 +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.28.orig/drivers/scsi/libsas/sas_ata.c linux-4.4.28/drivers/scsi/libsas/sas_ata.c
  9627. --- linux-4.4.28.orig/drivers/scsi/libsas/sas_ata.c 2016-10-28 09:53:25.000000000 +0200
  9628. +++ linux-4.4.28/drivers/scsi/libsas/sas_ata.c 2016-11-01 21:26:58.548327581 +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.28.orig/drivers/scsi/qla2xxx/qla_inline.h linux-4.4.28/drivers/scsi/qla2xxx/qla_inline.h
  9645. --- linux-4.4.28.orig/drivers/scsi/qla2xxx/qla_inline.h 2016-10-28 09:53:25.000000000 +0200
  9646. +++ linux-4.4.28/drivers/scsi/qla2xxx/qla_inline.h 2016-11-01 21:26:58.560328355 +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.28.orig/drivers/thermal/x86_pkg_temp_thermal.c linux-4.4.28/drivers/thermal/x86_pkg_temp_thermal.c
  9662. --- linux-4.4.28.orig/drivers/thermal/x86_pkg_temp_thermal.c 2016-10-28 09:53:25.000000000 +0200
  9663. +++ linux-4.4.28/drivers/thermal/x86_pkg_temp_thermal.c 2016-11-01 21:26:58.572329128 +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.28.orig/drivers/tty/serial/8250/8250_core.c linux-4.4.28/drivers/tty/serial/8250/8250_core.c
  9762. --- linux-4.4.28.orig/drivers/tty/serial/8250/8250_core.c 2016-10-28 09:53:25.000000000 +0200
  9763. +++ linux-4.4.28/drivers/tty/serial/8250/8250_core.c 2016-11-01 21:26:58.576329393 +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.28.orig/drivers/tty/serial/8250/8250_port.c linux-4.4.28/drivers/tty/serial/8250/8250_port.c
  9780. --- linux-4.4.28.orig/drivers/tty/serial/8250/8250_port.c 2016-10-28 09:53:25.000000000 +0200
  9781. +++ linux-4.4.28/drivers/tty/serial/8250/8250_port.c 2016-11-01 21:26:58.596330676 +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.28.orig/drivers/tty/serial/amba-pl011.c linux-4.4.28/drivers/tty/serial/amba-pl011.c
  9800. --- linux-4.4.28.orig/drivers/tty/serial/amba-pl011.c 2016-10-28 09:53:25.000000000 +0200
  9801. +++ linux-4.4.28/drivers/tty/serial/amba-pl011.c 2016-11-01 21:26:58.608331449 +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.28.orig/drivers/tty/serial/omap-serial.c linux-4.4.28/drivers/tty/serial/omap-serial.c
  9831. --- linux-4.4.28.orig/drivers/tty/serial/omap-serial.c 2016-10-28 09:53:25.000000000 +0200
  9832. +++ linux-4.4.28/drivers/tty/serial/omap-serial.c 2016-11-01 21:26:58.620332223 +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.28.orig/drivers/tty/serial/sc16is7xx.c linux-4.4.28/drivers/tty/serial/sc16is7xx.c
  9857. --- linux-4.4.28.orig/drivers/tty/serial/sc16is7xx.c 2016-10-28 09:53:25.000000000 +0200
  9858. +++ linux-4.4.28/drivers/tty/serial/sc16is7xx.c 2016-11-01 21:26:58.628332740 +0100
  9859. @@ -1230,7 +1230,7 @@
  9860. /* Setup interrupt */
  9861. ret = devm_request_irq(dev, irq, sc16is7xx_irq,
  9862. - IRQF_ONESHOT | flags, dev_name(dev), s);
  9863. + flags, dev_name(dev), s);
  9864. if (!ret)
  9865. return 0;
  9866. diff -Nur linux-4.4.28.orig/drivers/usb/core/hcd.c linux-4.4.28/drivers/usb/core/hcd.c
  9867. --- linux-4.4.28.orig/drivers/usb/core/hcd.c 2016-10-28 09:53:25.000000000 +0200
  9868. +++ linux-4.4.28/drivers/usb/core/hcd.c 2016-11-01 21:26:58.648334034 +0100
  9869. @@ -1735,9 +1735,9 @@
  9870. * and no one may trigger the above deadlock situation when
  9871. * running complete() in tasklet.
  9872. */
  9873. - local_irq_save(flags);
  9874. + local_irq_save_nort(flags);
  9875. urb->complete(urb);
  9876. - local_irq_restore(flags);
  9877. + local_irq_restore_nort(flags);
  9878. usb_anchor_resume_wakeups(anchor);
  9879. atomic_dec(&urb->use_count);
  9880. diff -Nur linux-4.4.28.orig/drivers/usb/gadget/function/f_fs.c linux-4.4.28/drivers/usb/gadget/function/f_fs.c
  9881. --- linux-4.4.28.orig/drivers/usb/gadget/function/f_fs.c 2016-10-28 09:53:25.000000000 +0200
  9882. +++ linux-4.4.28/drivers/usb/gadget/function/f_fs.c 2016-11-01 21:26:58.680336091 +0100
  9883. @@ -1404,7 +1404,7 @@
  9884. pr_info("%s(): freeing\n", __func__);
  9885. ffs_data_clear(ffs);
  9886. BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
  9887. - waitqueue_active(&ffs->ep0req_completion.wait));
  9888. + swait_active(&ffs->ep0req_completion.wait));
  9889. kfree(ffs->dev_name);
  9890. kfree(ffs);
  9891. }
  9892. diff -Nur linux-4.4.28.orig/drivers/usb/gadget/legacy/inode.c linux-4.4.28/drivers/usb/gadget/legacy/inode.c
  9893. --- linux-4.4.28.orig/drivers/usb/gadget/legacy/inode.c 2016-10-28 09:53:25.000000000 +0200
  9894. +++ linux-4.4.28/drivers/usb/gadget/legacy/inode.c 2016-11-01 21:26:58.692336864 +0100
  9895. @@ -345,7 +345,7 @@
  9896. spin_unlock_irq (&epdata->dev->lock);
  9897. if (likely (value == 0)) {
  9898. - value = wait_event_interruptible (done.wait, done.done);
  9899. + value = swait_event_interruptible (done.wait, done.done);
  9900. if (value != 0) {
  9901. spin_lock_irq (&epdata->dev->lock);
  9902. if (likely (epdata->ep != NULL)) {
  9903. @@ -354,7 +354,7 @@
  9904. usb_ep_dequeue (epdata->ep, epdata->req);
  9905. spin_unlock_irq (&epdata->dev->lock);
  9906. - wait_event (done.wait, done.done);
  9907. + swait_event (done.wait, done.done);
  9908. if (epdata->status == -ECONNRESET)
  9909. epdata->status = -EINTR;
  9910. } else {
  9911. diff -Nur linux-4.4.28.orig/drivers/usb/gadget/udc/atmel_usba_udc.c linux-4.4.28/drivers/usb/gadget/udc/atmel_usba_udc.c
  9912. --- linux-4.4.28.orig/drivers/usb/gadget/udc/atmel_usba_udc.c 2016-10-28 09:53:25.000000000 +0200
  9913. +++ linux-4.4.28/drivers/usb/gadget/udc/atmel_usba_udc.c 2016-11-01 21:26:58.704337638 +0100
  9914. @@ -17,7 +17,9 @@
  9915. #include <linux/device.h>
  9916. #include <linux/dma-mapping.h>
  9917. #include <linux/list.h>
  9918. +#include <linux/mfd/syscon.h>
  9919. #include <linux/platform_device.h>
  9920. +#include <linux/regmap.h>
  9921. #include <linux/usb/ch9.h>
  9922. #include <linux/usb/gadget.h>
  9923. #include <linux/usb/atmel_usba_udc.h>
  9924. @@ -1888,20 +1890,15 @@
  9925. #ifdef CONFIG_OF
  9926. static void at91sam9rl_toggle_bias(struct usba_udc *udc, int is_on)
  9927. {
  9928. - unsigned int uckr = at91_pmc_read(AT91_CKGR_UCKR);
  9929. -
  9930. - if (is_on)
  9931. - at91_pmc_write(AT91_CKGR_UCKR, uckr | AT91_PMC_BIASEN);
  9932. - else
  9933. - at91_pmc_write(AT91_CKGR_UCKR, uckr & ~(AT91_PMC_BIASEN));
  9934. + regmap_update_bits(udc->pmc, AT91_CKGR_UCKR, AT91_PMC_BIASEN,
  9935. + is_on ? AT91_PMC_BIASEN : 0);
  9936. }
  9937. static void at91sam9g45_pulse_bias(struct usba_udc *udc)
  9938. {
  9939. - unsigned int uckr = at91_pmc_read(AT91_CKGR_UCKR);
  9940. -
  9941. - at91_pmc_write(AT91_CKGR_UCKR, uckr & ~(AT91_PMC_BIASEN));
  9942. - at91_pmc_write(AT91_CKGR_UCKR, uckr | AT91_PMC_BIASEN);
  9943. + regmap_update_bits(udc->pmc, AT91_CKGR_UCKR, AT91_PMC_BIASEN, 0);
  9944. + regmap_update_bits(udc->pmc, AT91_CKGR_UCKR, AT91_PMC_BIASEN,
  9945. + AT91_PMC_BIASEN);
  9946. }
  9947. static const struct usba_udc_errata at91sam9rl_errata = {
  9948. @@ -1938,6 +1935,9 @@
  9949. return ERR_PTR(-EINVAL);
  9950. udc->errata = match->data;
  9951. + udc->pmc = syscon_regmap_lookup_by_compatible("atmel,at91sam9g45-pmc");
  9952. + if (udc->errata && IS_ERR(udc->pmc))
  9953. + return ERR_CAST(udc->pmc);
  9954. udc->num_ep = 0;
  9955. diff -Nur linux-4.4.28.orig/drivers/usb/gadget/udc/atmel_usba_udc.h linux-4.4.28/drivers/usb/gadget/udc/atmel_usba_udc.h
  9956. --- linux-4.4.28.orig/drivers/usb/gadget/udc/atmel_usba_udc.h 2016-10-28 09:53:25.000000000 +0200
  9957. +++ linux-4.4.28/drivers/usb/gadget/udc/atmel_usba_udc.h 2016-11-01 21:26:58.704337638 +0100
  9958. @@ -354,6 +354,8 @@
  9959. struct dentry *debugfs_root;
  9960. struct dentry *debugfs_regs;
  9961. #endif
  9962. +
  9963. + struct regmap *pmc;
  9964. };
  9965. static inline struct usba_ep *to_usba_ep(struct usb_ep *ep)
  9966. diff -Nur linux-4.4.28.orig/fs/aio.c linux-4.4.28/fs/aio.c
  9967. --- linux-4.4.28.orig/fs/aio.c 2016-10-28 09:53:25.000000000 +0200
  9968. +++ linux-4.4.28/fs/aio.c 2016-11-01 21:26:58.716338411 +0100
  9969. @@ -40,6 +40,7 @@
  9970. #include <linux/ramfs.h>
  9971. #include <linux/percpu-refcount.h>
  9972. #include <linux/mount.h>
  9973. +#include <linux/swork.h>
  9974. #include <asm/kmap_types.h>
  9975. #include <asm/uaccess.h>
  9976. @@ -115,7 +116,7 @@
  9977. struct page **ring_pages;
  9978. long nr_pages;
  9979. - struct work_struct free_work;
  9980. + struct swork_event free_work;
  9981. /*
  9982. * signals when all in-flight requests are done
  9983. @@ -258,6 +259,7 @@
  9984. .mount = aio_mount,
  9985. .kill_sb = kill_anon_super,
  9986. };
  9987. + BUG_ON(swork_get());
  9988. aio_mnt = kern_mount(&aio_fs);
  9989. if (IS_ERR(aio_mnt))
  9990. panic("Failed to create aio fs mount.");
  9991. @@ -573,9 +575,9 @@
  9992. return cancel(&kiocb->common);
  9993. }
  9994. -static void free_ioctx(struct work_struct *work)
  9995. +static void free_ioctx(struct swork_event *sev)
  9996. {
  9997. - struct kioctx *ctx = container_of(work, struct kioctx, free_work);
  9998. + struct kioctx *ctx = container_of(sev, struct kioctx, free_work);
  9999. pr_debug("freeing %p\n", ctx);
  10000. @@ -594,8 +596,8 @@
  10001. if (ctx->rq_wait && atomic_dec_and_test(&ctx->rq_wait->count))
  10002. complete(&ctx->rq_wait->comp);
  10003. - INIT_WORK(&ctx->free_work, free_ioctx);
  10004. - schedule_work(&ctx->free_work);
  10005. + INIT_SWORK(&ctx->free_work, free_ioctx);
  10006. + swork_queue(&ctx->free_work);
  10007. }
  10008. /*
  10009. @@ -603,9 +605,9 @@
  10010. * and ctx->users has dropped to 0, so we know no more kiocbs can be submitted -
  10011. * now it's safe to cancel any that need to be.
  10012. */
  10013. -static void free_ioctx_users(struct percpu_ref *ref)
  10014. +static void free_ioctx_users_work(struct swork_event *sev)
  10015. {
  10016. - struct kioctx *ctx = container_of(ref, struct kioctx, users);
  10017. + struct kioctx *ctx = container_of(sev, struct kioctx, free_work);
  10018. struct aio_kiocb *req;
  10019. spin_lock_irq(&ctx->ctx_lock);
  10020. @@ -624,6 +626,14 @@
  10021. percpu_ref_put(&ctx->reqs);
  10022. }
  10023. +static void free_ioctx_users(struct percpu_ref *ref)
  10024. +{
  10025. + struct kioctx *ctx = container_of(ref, struct kioctx, users);
  10026. +
  10027. + INIT_SWORK(&ctx->free_work, free_ioctx_users_work);
  10028. + swork_queue(&ctx->free_work);
  10029. +}
  10030. +
  10031. static int ioctx_add_table(struct kioctx *ctx, struct mm_struct *mm)
  10032. {
  10033. unsigned i, new_nr;
  10034. diff -Nur linux-4.4.28.orig/fs/autofs4/autofs_i.h linux-4.4.28/fs/autofs4/autofs_i.h
  10035. --- linux-4.4.28.orig/fs/autofs4/autofs_i.h 2016-10-28 09:53:25.000000000 +0200
  10036. +++ linux-4.4.28/fs/autofs4/autofs_i.h 2016-11-01 21:26:58.716338411 +0100
  10037. @@ -34,6 +34,7 @@
  10038. #include <linux/sched.h>
  10039. #include <linux/mount.h>
  10040. #include <linux/namei.h>
  10041. +#include <linux/delay.h>
  10042. #include <asm/current.h>
  10043. #include <asm/uaccess.h>
  10044. diff -Nur linux-4.4.28.orig/fs/autofs4/expire.c linux-4.4.28/fs/autofs4/expire.c
  10045. --- linux-4.4.28.orig/fs/autofs4/expire.c 2016-10-28 09:53:25.000000000 +0200
  10046. +++ linux-4.4.28/fs/autofs4/expire.c 2016-11-01 21:26:58.716338411 +0100
  10047. @@ -150,7 +150,7 @@
  10048. parent = p->d_parent;
  10049. if (!spin_trylock(&parent->d_lock)) {
  10050. spin_unlock(&p->d_lock);
  10051. - cpu_relax();
  10052. + cpu_chill();
  10053. goto relock;
  10054. }
  10055. spin_unlock(&p->d_lock);
  10056. diff -Nur linux-4.4.28.orig/fs/buffer.c linux-4.4.28/fs/buffer.c
  10057. --- linux-4.4.28.orig/fs/buffer.c 2016-10-28 09:53:25.000000000 +0200
  10058. +++ linux-4.4.28/fs/buffer.c 2016-11-01 21:26:58.716338411 +0100
  10059. @@ -305,8 +305,7 @@
  10060. * decide that the page is now completely done.
  10061. */
  10062. first = page_buffers(page);
  10063. - local_irq_save(flags);
  10064. - bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  10065. + flags = bh_uptodate_lock_irqsave(first);
  10066. clear_buffer_async_read(bh);
  10067. unlock_buffer(bh);
  10068. tmp = bh;
  10069. @@ -319,8 +318,7 @@
  10070. }
  10071. tmp = tmp->b_this_page;
  10072. } while (tmp != bh);
  10073. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  10074. - local_irq_restore(flags);
  10075. + bh_uptodate_unlock_irqrestore(first, flags);
  10076. /*
  10077. * If none of the buffers had errors and they are all
  10078. @@ -332,9 +330,7 @@
  10079. return;
  10080. still_busy:
  10081. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  10082. - local_irq_restore(flags);
  10083. - return;
  10084. + bh_uptodate_unlock_irqrestore(first, flags);
  10085. }
  10086. /*
  10087. @@ -362,8 +358,7 @@
  10088. }
  10089. first = page_buffers(page);
  10090. - local_irq_save(flags);
  10091. - bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  10092. + flags = bh_uptodate_lock_irqsave(first);
  10093. clear_buffer_async_write(bh);
  10094. unlock_buffer(bh);
  10095. @@ -375,15 +370,12 @@
  10096. }
  10097. tmp = tmp->b_this_page;
  10098. }
  10099. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  10100. - local_irq_restore(flags);
  10101. + bh_uptodate_unlock_irqrestore(first, flags);
  10102. end_page_writeback(page);
  10103. return;
  10104. still_busy:
  10105. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  10106. - local_irq_restore(flags);
  10107. - return;
  10108. + bh_uptodate_unlock_irqrestore(first, flags);
  10109. }
  10110. EXPORT_SYMBOL(end_buffer_async_write);
  10111. @@ -3325,6 +3317,7 @@
  10112. struct buffer_head *ret = kmem_cache_zalloc(bh_cachep, gfp_flags);
  10113. if (ret) {
  10114. INIT_LIST_HEAD(&ret->b_assoc_buffers);
  10115. + buffer_head_init_locks(ret);
  10116. preempt_disable();
  10117. __this_cpu_inc(bh_accounting.nr);
  10118. recalc_bh_state();
  10119. diff -Nur linux-4.4.28.orig/fs/dcache.c linux-4.4.28/fs/dcache.c
  10120. --- linux-4.4.28.orig/fs/dcache.c 2016-10-28 09:53:25.000000000 +0200
  10121. +++ linux-4.4.28/fs/dcache.c 2016-11-01 21:26:58.720338675 +0100
  10122. @@ -19,6 +19,7 @@
  10123. #include <linux/mm.h>
  10124. #include <linux/fs.h>
  10125. #include <linux/fsnotify.h>
  10126. +#include <linux/delay.h>
  10127. #include <linux/slab.h>
  10128. #include <linux/init.h>
  10129. #include <linux/hash.h>
  10130. @@ -747,6 +748,8 @@
  10131. */
  10132. void dput(struct dentry *dentry)
  10133. {
  10134. + struct dentry *parent;
  10135. +
  10136. if (unlikely(!dentry))
  10137. return;
  10138. @@ -783,9 +786,18 @@
  10139. return;
  10140. kill_it:
  10141. - dentry = dentry_kill(dentry);
  10142. - if (dentry) {
  10143. - cond_resched();
  10144. + parent = dentry_kill(dentry);
  10145. + if (parent) {
  10146. + int r;
  10147. +
  10148. + if (parent == dentry) {
  10149. + /* the task with the highest priority won't schedule */
  10150. + r = cond_resched();
  10151. + if (!r)
  10152. + cpu_chill();
  10153. + } else {
  10154. + dentry = parent;
  10155. + }
  10156. goto repeat;
  10157. }
  10158. }
  10159. @@ -2394,7 +2406,7 @@
  10160. if (dentry->d_lockref.count == 1) {
  10161. if (!spin_trylock(&inode->i_lock)) {
  10162. spin_unlock(&dentry->d_lock);
  10163. - cpu_relax();
  10164. + cpu_chill();
  10165. goto again;
  10166. }
  10167. dentry->d_flags &= ~DCACHE_CANT_MOUNT;
  10168. diff -Nur linux-4.4.28.orig/fs/eventpoll.c linux-4.4.28/fs/eventpoll.c
  10169. --- linux-4.4.28.orig/fs/eventpoll.c 2016-10-28 09:53:25.000000000 +0200
  10170. +++ linux-4.4.28/fs/eventpoll.c 2016-11-01 21:26:58.720338675 +0100
  10171. @@ -505,12 +505,12 @@
  10172. */
  10173. static void ep_poll_safewake(wait_queue_head_t *wq)
  10174. {
  10175. - int this_cpu = get_cpu();
  10176. + int this_cpu = get_cpu_light();
  10177. ep_call_nested(&poll_safewake_ncalls, EP_MAX_NESTS,
  10178. ep_poll_wakeup_proc, NULL, wq, (void *) (long) this_cpu);
  10179. - put_cpu();
  10180. + put_cpu_light();
  10181. }
  10182. static void ep_remove_wait_queue(struct eppoll_entry *pwq)
  10183. diff -Nur linux-4.4.28.orig/fs/exec.c linux-4.4.28/fs/exec.c
  10184. --- linux-4.4.28.orig/fs/exec.c 2016-10-28 09:53:25.000000000 +0200
  10185. +++ linux-4.4.28/fs/exec.c 2016-11-01 21:26:58.720338675 +0100
  10186. @@ -865,12 +865,14 @@
  10187. }
  10188. }
  10189. task_lock(tsk);
  10190. + preempt_disable_rt();
  10191. active_mm = tsk->active_mm;
  10192. tsk->mm = mm;
  10193. tsk->active_mm = mm;
  10194. activate_mm(active_mm, mm);
  10195. tsk->mm->vmacache_seqnum = 0;
  10196. vmacache_flush(tsk);
  10197. + preempt_enable_rt();
  10198. task_unlock(tsk);
  10199. if (old_mm) {
  10200. up_read(&old_mm->mmap_sem);
  10201. diff -Nur linux-4.4.28.orig/fs/f2fs/f2fs.h linux-4.4.28/fs/f2fs/f2fs.h
  10202. --- linux-4.4.28.orig/fs/f2fs/f2fs.h 2016-10-28 09:53:25.000000000 +0200
  10203. +++ linux-4.4.28/fs/f2fs/f2fs.h 2016-11-01 21:26:58.720338675 +0100
  10204. @@ -24,7 +24,6 @@
  10205. #ifdef CONFIG_F2FS_CHECK_FS
  10206. #define f2fs_bug_on(sbi, condition) BUG_ON(condition)
  10207. -#define f2fs_down_write(x, y) down_write_nest_lock(x, y)
  10208. #else
  10209. #define f2fs_bug_on(sbi, condition) \
  10210. do { \
  10211. @@ -33,7 +32,6 @@
  10212. set_sbi_flag(sbi, SBI_NEED_FSCK); \
  10213. } \
  10214. } while (0)
  10215. -#define f2fs_down_write(x, y) down_write(x)
  10216. #endif
  10217. /*
  10218. @@ -959,7 +957,7 @@
  10219. static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
  10220. {
  10221. - f2fs_down_write(&sbi->cp_rwsem, &sbi->cp_mutex);
  10222. + down_write(&sbi->cp_rwsem);
  10223. }
  10224. static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
  10225. diff -Nur linux-4.4.28.orig/fs/jbd2/checkpoint.c linux-4.4.28/fs/jbd2/checkpoint.c
  10226. --- linux-4.4.28.orig/fs/jbd2/checkpoint.c 2016-10-28 09:53:25.000000000 +0200
  10227. +++ linux-4.4.28/fs/jbd2/checkpoint.c 2016-11-01 21:26:58.720338675 +0100
  10228. @@ -116,6 +116,8 @@
  10229. nblocks = jbd2_space_needed(journal);
  10230. while (jbd2_log_space_left(journal) < nblocks) {
  10231. write_unlock(&journal->j_state_lock);
  10232. + if (current->plug)
  10233. + io_schedule();
  10234. mutex_lock(&journal->j_checkpoint_mutex);
  10235. /*
  10236. diff -Nur linux-4.4.28.orig/fs/namespace.c linux-4.4.28/fs/namespace.c
  10237. --- linux-4.4.28.orig/fs/namespace.c 2016-10-28 09:53:25.000000000 +0200
  10238. +++ linux-4.4.28/fs/namespace.c 2016-11-01 21:26:58.724338928 +0100
  10239. @@ -14,6 +14,7 @@
  10240. #include <linux/mnt_namespace.h>
  10241. #include <linux/user_namespace.h>
  10242. #include <linux/namei.h>
  10243. +#include <linux/delay.h>
  10244. #include <linux/security.h>
  10245. #include <linux/idr.h>
  10246. #include <linux/init.h> /* init_rootfs */
  10247. @@ -353,8 +354,11 @@
  10248. * incremented count after it has set MNT_WRITE_HOLD.
  10249. */
  10250. smp_mb();
  10251. - while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD)
  10252. - cpu_relax();
  10253. + while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD) {
  10254. + preempt_enable();
  10255. + cpu_chill();
  10256. + preempt_disable();
  10257. + }
  10258. /*
  10259. * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
  10260. * be set to match its requirements. So we must not load that until
  10261. diff -Nur linux-4.4.28.orig/fs/ntfs/aops.c linux-4.4.28/fs/ntfs/aops.c
  10262. --- linux-4.4.28.orig/fs/ntfs/aops.c 2016-10-28 09:53:25.000000000 +0200
  10263. +++ linux-4.4.28/fs/ntfs/aops.c 2016-11-01 21:26:58.724338928 +0100
  10264. @@ -107,8 +107,7 @@
  10265. "0x%llx.", (unsigned long long)bh->b_blocknr);
  10266. }
  10267. first = page_buffers(page);
  10268. - local_irq_save(flags);
  10269. - bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  10270. + flags = bh_uptodate_lock_irqsave(first);
  10271. clear_buffer_async_read(bh);
  10272. unlock_buffer(bh);
  10273. tmp = bh;
  10274. @@ -123,8 +122,7 @@
  10275. }
  10276. tmp = tmp->b_this_page;
  10277. } while (tmp != bh);
  10278. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  10279. - local_irq_restore(flags);
  10280. + bh_uptodate_unlock_irqrestore(first, flags);
  10281. /*
  10282. * If none of the buffers had errors then we can set the page uptodate,
  10283. * but we first have to perform the post read mst fixups, if the
  10284. @@ -145,13 +143,13 @@
  10285. recs = PAGE_CACHE_SIZE / rec_size;
  10286. /* Should have been verified before we got here... */
  10287. BUG_ON(!recs);
  10288. - local_irq_save(flags);
  10289. + local_irq_save_nort(flags);
  10290. kaddr = kmap_atomic(page);
  10291. for (i = 0; i < recs; i++)
  10292. post_read_mst_fixup((NTFS_RECORD*)(kaddr +
  10293. i * rec_size), rec_size);
  10294. kunmap_atomic(kaddr);
  10295. - local_irq_restore(flags);
  10296. + local_irq_restore_nort(flags);
  10297. flush_dcache_page(page);
  10298. if (likely(page_uptodate && !PageError(page)))
  10299. SetPageUptodate(page);
  10300. @@ -159,9 +157,7 @@
  10301. unlock_page(page);
  10302. return;
  10303. still_busy:
  10304. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  10305. - local_irq_restore(flags);
  10306. - return;
  10307. + bh_uptodate_unlock_irqrestore(first, flags);
  10308. }
  10309. /**
  10310. diff -Nur linux-4.4.28.orig/fs/timerfd.c linux-4.4.28/fs/timerfd.c
  10311. --- linux-4.4.28.orig/fs/timerfd.c 2016-10-28 09:53:25.000000000 +0200
  10312. +++ linux-4.4.28/fs/timerfd.c 2016-11-01 21:26:58.724338928 +0100
  10313. @@ -450,7 +450,10 @@
  10314. break;
  10315. }
  10316. spin_unlock_irq(&ctx->wqh.lock);
  10317. - cpu_relax();
  10318. + if (isalarm(ctx))
  10319. + hrtimer_wait_for_timer(&ctx->t.alarm.timer);
  10320. + else
  10321. + hrtimer_wait_for_timer(&ctx->t.tmr);
  10322. }
  10323. /*
  10324. diff -Nur linux-4.4.28.orig/include/acpi/platform/aclinux.h linux-4.4.28/include/acpi/platform/aclinux.h
  10325. --- linux-4.4.28.orig/include/acpi/platform/aclinux.h 2016-10-28 09:53:25.000000000 +0200
  10326. +++ linux-4.4.28/include/acpi/platform/aclinux.h 2016-11-01 21:26:58.724338928 +0100
  10327. @@ -127,6 +127,7 @@
  10328. #define acpi_cache_t struct kmem_cache
  10329. #define acpi_spinlock spinlock_t *
  10330. +#define acpi_raw_spinlock raw_spinlock_t *
  10331. #define acpi_cpu_flags unsigned long
  10332. /* Use native linux version of acpi_os_allocate_zeroed */
  10333. @@ -145,6 +146,20 @@
  10334. #define ACPI_USE_ALTERNATE_PROTOTYPE_acpi_os_get_thread_id
  10335. #define ACPI_USE_ALTERNATE_PROTOTYPE_acpi_os_create_lock
  10336. +#define acpi_os_create_raw_lock(__handle) \
  10337. +({ \
  10338. + raw_spinlock_t *lock = ACPI_ALLOCATE(sizeof(*lock)); \
  10339. + \
  10340. + if (lock) { \
  10341. + *(__handle) = lock; \
  10342. + raw_spin_lock_init(*(__handle)); \
  10343. + } \
  10344. + lock ? AE_OK : AE_NO_MEMORY; \
  10345. + })
  10346. +
  10347. +#define acpi_os_delete_raw_lock(__handle) kfree(__handle)
  10348. +
  10349. +
  10350. /*
  10351. * OSL interfaces used by debugger/disassembler
  10352. */
  10353. diff -Nur linux-4.4.28.orig/include/asm-generic/bug.h linux-4.4.28/include/asm-generic/bug.h
  10354. --- linux-4.4.28.orig/include/asm-generic/bug.h 2016-10-28 09:53:25.000000000 +0200
  10355. +++ linux-4.4.28/include/asm-generic/bug.h 2016-11-01 21:26:58.724338928 +0100
  10356. @@ -206,6 +206,20 @@
  10357. # define WARN_ON_SMP(x) ({0;})
  10358. #endif
  10359. +#ifdef CONFIG_PREEMPT_RT_BASE
  10360. +# define BUG_ON_RT(c) BUG_ON(c)
  10361. +# define BUG_ON_NONRT(c) do { } while (0)
  10362. +# define WARN_ON_RT(condition) WARN_ON(condition)
  10363. +# define WARN_ON_NONRT(condition) do { } while (0)
  10364. +# define WARN_ON_ONCE_NONRT(condition) do { } while (0)
  10365. +#else
  10366. +# define BUG_ON_RT(c) do { } while (0)
  10367. +# define BUG_ON_NONRT(c) BUG_ON(c)
  10368. +# define WARN_ON_RT(condition) do { } while (0)
  10369. +# define WARN_ON_NONRT(condition) WARN_ON(condition)
  10370. +# define WARN_ON_ONCE_NONRT(condition) WARN_ON_ONCE(condition)
  10371. +#endif
  10372. +
  10373. #endif /* __ASSEMBLY__ */
  10374. #endif
  10375. diff -Nur linux-4.4.28.orig/include/asm-generic/preempt.h linux-4.4.28/include/asm-generic/preempt.h
  10376. --- linux-4.4.28.orig/include/asm-generic/preempt.h 2016-10-28 09:53:25.000000000 +0200
  10377. +++ linux-4.4.28/include/asm-generic/preempt.h 2016-11-01 21:26:58.724338928 +0100
  10378. @@ -7,10 +7,10 @@
  10379. static __always_inline int preempt_count(void)
  10380. {
  10381. - return current_thread_info()->preempt_count;
  10382. + return READ_ONCE(current_thread_info()->preempt_count);
  10383. }
  10384. -static __always_inline int *preempt_count_ptr(void)
  10385. +static __always_inline volatile int *preempt_count_ptr(void)
  10386. {
  10387. return &current_thread_info()->preempt_count;
  10388. }
  10389. diff -Nur linux-4.4.28.orig/include/linux/blkdev.h linux-4.4.28/include/linux/blkdev.h
  10390. --- linux-4.4.28.orig/include/linux/blkdev.h 2016-10-28 09:53:25.000000000 +0200
  10391. +++ linux-4.4.28/include/linux/blkdev.h 2016-11-01 21:26:58.724338928 +0100
  10392. @@ -89,6 +89,7 @@
  10393. struct list_head queuelist;
  10394. union {
  10395. struct call_single_data csd;
  10396. + struct work_struct work;
  10397. unsigned long fifo_time;
  10398. };
  10399. @@ -455,7 +456,7 @@
  10400. struct throtl_data *td;
  10401. #endif
  10402. struct rcu_head rcu_head;
  10403. - wait_queue_head_t mq_freeze_wq;
  10404. + struct swait_queue_head mq_freeze_wq;
  10405. struct percpu_ref q_usage_counter;
  10406. struct list_head all_q_node;
  10407. diff -Nur linux-4.4.28.orig/include/linux/blk-mq.h linux-4.4.28/include/linux/blk-mq.h
  10408. --- linux-4.4.28.orig/include/linux/blk-mq.h 2016-10-28 09:53:25.000000000 +0200
  10409. +++ linux-4.4.28/include/linux/blk-mq.h 2016-11-01 21:26:58.724338928 +0100
  10410. @@ -212,6 +212,7 @@
  10411. struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *, const int ctx_index);
  10412. struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_tag_set *, unsigned int, int);
  10413. +void __blk_mq_complete_request_remote_work(struct work_struct *work);
  10414. int blk_mq_request_started(struct request *rq);
  10415. void blk_mq_start_request(struct request *rq);
  10416. diff -Nur linux-4.4.28.orig/include/linux/bottom_half.h linux-4.4.28/include/linux/bottom_half.h
  10417. --- linux-4.4.28.orig/include/linux/bottom_half.h 2016-10-28 09:53:25.000000000 +0200
  10418. +++ linux-4.4.28/include/linux/bottom_half.h 2016-11-01 21:26:58.724338928 +0100
  10419. @@ -3,6 +3,39 @@
  10420. #include <linux/preempt.h>
  10421. +#ifdef CONFIG_PREEMPT_RT_FULL
  10422. +
  10423. +extern void __local_bh_disable(void);
  10424. +extern void _local_bh_enable(void);
  10425. +extern void __local_bh_enable(void);
  10426. +
  10427. +static inline void local_bh_disable(void)
  10428. +{
  10429. + __local_bh_disable();
  10430. +}
  10431. +
  10432. +static inline void __local_bh_disable_ip(unsigned long ip, unsigned int cnt)
  10433. +{
  10434. + __local_bh_disable();
  10435. +}
  10436. +
  10437. +static inline void local_bh_enable(void)
  10438. +{
  10439. + __local_bh_enable();
  10440. +}
  10441. +
  10442. +static inline void __local_bh_enable_ip(unsigned long ip, unsigned int cnt)
  10443. +{
  10444. + __local_bh_enable();
  10445. +}
  10446. +
  10447. +static inline void local_bh_enable_ip(unsigned long ip)
  10448. +{
  10449. + __local_bh_enable();
  10450. +}
  10451. +
  10452. +#else
  10453. +
  10454. #ifdef CONFIG_TRACE_IRQFLAGS
  10455. extern void __local_bh_disable_ip(unsigned long ip, unsigned int cnt);
  10456. #else
  10457. @@ -30,5 +63,6 @@
  10458. {
  10459. __local_bh_enable_ip(_THIS_IP_, SOFTIRQ_DISABLE_OFFSET);
  10460. }
  10461. +#endif
  10462. #endif /* _LINUX_BH_H */
  10463. diff -Nur linux-4.4.28.orig/include/linux/buffer_head.h linux-4.4.28/include/linux/buffer_head.h
  10464. --- linux-4.4.28.orig/include/linux/buffer_head.h 2016-10-28 09:53:25.000000000 +0200
  10465. +++ linux-4.4.28/include/linux/buffer_head.h 2016-11-01 21:26:58.724338928 +0100
  10466. @@ -75,8 +75,50 @@
  10467. struct address_space *b_assoc_map; /* mapping this buffer is
  10468. associated with */
  10469. atomic_t b_count; /* users using this buffer_head */
  10470. +#ifdef CONFIG_PREEMPT_RT_BASE
  10471. + spinlock_t b_uptodate_lock;
  10472. +#if IS_ENABLED(CONFIG_JBD2)
  10473. + spinlock_t b_state_lock;
  10474. + spinlock_t b_journal_head_lock;
  10475. +#endif
  10476. +#endif
  10477. };
  10478. +static inline unsigned long bh_uptodate_lock_irqsave(struct buffer_head *bh)
  10479. +{
  10480. + unsigned long flags;
  10481. +
  10482. +#ifndef CONFIG_PREEMPT_RT_BASE
  10483. + local_irq_save(flags);
  10484. + bit_spin_lock(BH_Uptodate_Lock, &bh->b_state);
  10485. +#else
  10486. + spin_lock_irqsave(&bh->b_uptodate_lock, flags);
  10487. +#endif
  10488. + return flags;
  10489. +}
  10490. +
  10491. +static inline void
  10492. +bh_uptodate_unlock_irqrestore(struct buffer_head *bh, unsigned long flags)
  10493. +{
  10494. +#ifndef CONFIG_PREEMPT_RT_BASE
  10495. + bit_spin_unlock(BH_Uptodate_Lock, &bh->b_state);
  10496. + local_irq_restore(flags);
  10497. +#else
  10498. + spin_unlock_irqrestore(&bh->b_uptodate_lock, flags);
  10499. +#endif
  10500. +}
  10501. +
  10502. +static inline void buffer_head_init_locks(struct buffer_head *bh)
  10503. +{
  10504. +#ifdef CONFIG_PREEMPT_RT_BASE
  10505. + spin_lock_init(&bh->b_uptodate_lock);
  10506. +#if IS_ENABLED(CONFIG_JBD2)
  10507. + spin_lock_init(&bh->b_state_lock);
  10508. + spin_lock_init(&bh->b_journal_head_lock);
  10509. +#endif
  10510. +#endif
  10511. +}
  10512. +
  10513. /*
  10514. * macro tricks to expand the set_buffer_foo(), clear_buffer_foo()
  10515. * and buffer_foo() functions.
  10516. diff -Nur linux-4.4.28.orig/include/linux/cgroup-defs.h linux-4.4.28/include/linux/cgroup-defs.h
  10517. --- linux-4.4.28.orig/include/linux/cgroup-defs.h 2016-10-28 09:53:25.000000000 +0200
  10518. +++ linux-4.4.28/include/linux/cgroup-defs.h 2016-11-01 21:26:58.728339185 +0100
  10519. @@ -16,6 +16,7 @@
  10520. #include <linux/percpu-refcount.h>
  10521. #include <linux/percpu-rwsem.h>
  10522. #include <linux/workqueue.h>
  10523. +#include <linux/swork.h>
  10524. #ifdef CONFIG_CGROUPS
  10525. @@ -142,6 +143,7 @@
  10526. /* percpu_ref killing and RCU release */
  10527. struct rcu_head rcu_head;
  10528. struct work_struct destroy_work;
  10529. + struct swork_event destroy_swork;
  10530. };
  10531. /*
  10532. diff -Nur linux-4.4.28.orig/include/linux/clk/at91_pmc.h linux-4.4.28/include/linux/clk/at91_pmc.h
  10533. --- linux-4.4.28.orig/include/linux/clk/at91_pmc.h 2016-10-28 09:53:25.000000000 +0200
  10534. +++ linux-4.4.28/include/linux/clk/at91_pmc.h 2016-11-01 21:26:58.728339185 +0100
  10535. @@ -16,18 +16,6 @@
  10536. #ifndef AT91_PMC_H
  10537. #define AT91_PMC_H
  10538. -#ifndef __ASSEMBLY__
  10539. -extern void __iomem *at91_pmc_base;
  10540. -
  10541. -#define at91_pmc_read(field) \
  10542. - readl_relaxed(at91_pmc_base + field)
  10543. -
  10544. -#define at91_pmc_write(field, value) \
  10545. - writel_relaxed(value, at91_pmc_base + field)
  10546. -#else
  10547. -.extern at91_pmc_base
  10548. -#endif
  10549. -
  10550. #define AT91_PMC_SCER 0x00 /* System Clock Enable Register */
  10551. #define AT91_PMC_SCDR 0x04 /* System Clock Disable Register */
  10552. diff -Nur linux-4.4.28.orig/include/linux/completion.h linux-4.4.28/include/linux/completion.h
  10553. --- linux-4.4.28.orig/include/linux/completion.h 2016-10-28 09:53:25.000000000 +0200
  10554. +++ linux-4.4.28/include/linux/completion.h 2016-11-01 21:26:58.728339185 +0100
  10555. @@ -7,8 +7,7 @@
  10556. * Atomic wait-for-completion handler data structures.
  10557. * See kernel/sched/completion.c for details.
  10558. */
  10559. -
  10560. -#include <linux/wait.h>
  10561. +#include <linux/swait.h>
  10562. /*
  10563. * struct completion - structure used to maintain state for a "completion"
  10564. @@ -24,11 +23,11 @@
  10565. */
  10566. struct completion {
  10567. unsigned int done;
  10568. - wait_queue_head_t wait;
  10569. + struct swait_queue_head wait;
  10570. };
  10571. #define COMPLETION_INITIALIZER(work) \
  10572. - { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
  10573. + { 0, __SWAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
  10574. #define COMPLETION_INITIALIZER_ONSTACK(work) \
  10575. ({ init_completion(&work); work; })
  10576. @@ -73,7 +72,7 @@
  10577. static inline void init_completion(struct completion *x)
  10578. {
  10579. x->done = 0;
  10580. - init_waitqueue_head(&x->wait);
  10581. + init_swait_queue_head(&x->wait);
  10582. }
  10583. /**
  10584. diff -Nur linux-4.4.28.orig/include/linux/cpu.h linux-4.4.28/include/linux/cpu.h
  10585. --- linux-4.4.28.orig/include/linux/cpu.h 2016-10-28 09:53:25.000000000 +0200
  10586. +++ linux-4.4.28/include/linux/cpu.h 2016-11-01 21:26:58.728339185 +0100
  10587. @@ -231,6 +231,8 @@
  10588. extern void put_online_cpus(void);
  10589. extern void cpu_hotplug_disable(void);
  10590. extern void cpu_hotplug_enable(void);
  10591. +extern void pin_current_cpu(void);
  10592. +extern void unpin_current_cpu(void);
  10593. #define hotcpu_notifier(fn, pri) cpu_notifier(fn, pri)
  10594. #define __hotcpu_notifier(fn, pri) __cpu_notifier(fn, pri)
  10595. #define register_hotcpu_notifier(nb) register_cpu_notifier(nb)
  10596. @@ -248,6 +250,8 @@
  10597. #define put_online_cpus() do { } while (0)
  10598. #define cpu_hotplug_disable() do { } while (0)
  10599. #define cpu_hotplug_enable() do { } while (0)
  10600. +static inline void pin_current_cpu(void) { }
  10601. +static inline void unpin_current_cpu(void) { }
  10602. #define hotcpu_notifier(fn, pri) do { (void)(fn); } while (0)
  10603. #define __hotcpu_notifier(fn, pri) do { (void)(fn); } while (0)
  10604. /* These aren't inline functions due to a GCC bug. */
  10605. diff -Nur linux-4.4.28.orig/include/linux/delay.h linux-4.4.28/include/linux/delay.h
  10606. --- linux-4.4.28.orig/include/linux/delay.h 2016-10-28 09:53:25.000000000 +0200
  10607. +++ linux-4.4.28/include/linux/delay.h 2016-11-01 21:26:58.728339185 +0100
  10608. @@ -52,4 +52,10 @@
  10609. msleep(seconds * 1000);
  10610. }
  10611. +#ifdef CONFIG_PREEMPT_RT_FULL
  10612. +extern void cpu_chill(void);
  10613. +#else
  10614. +# define cpu_chill() cpu_relax()
  10615. +#endif
  10616. +
  10617. #endif /* defined(_LINUX_DELAY_H) */
  10618. diff -Nur linux-4.4.28.orig/include/linux/ftrace.h linux-4.4.28/include/linux/ftrace.h
  10619. --- linux-4.4.28.orig/include/linux/ftrace.h 2016-10-28 09:53:25.000000000 +0200
  10620. +++ linux-4.4.28/include/linux/ftrace.h 2016-11-01 21:26:58.728339185 +0100
  10621. @@ -694,6 +694,18 @@
  10622. #define CALLER_ADDR5 ((unsigned long)ftrace_return_address(5))
  10623. #define CALLER_ADDR6 ((unsigned long)ftrace_return_address(6))
  10624. +static inline unsigned long get_lock_parent_ip(void)
  10625. +{
  10626. + unsigned long addr = CALLER_ADDR0;
  10627. +
  10628. + if (!in_lock_functions(addr))
  10629. + return addr;
  10630. + addr = CALLER_ADDR1;
  10631. + if (!in_lock_functions(addr))
  10632. + return addr;
  10633. + return CALLER_ADDR2;
  10634. +}
  10635. +
  10636. #ifdef CONFIG_IRQSOFF_TRACER
  10637. extern void time_hardirqs_on(unsigned long a0, unsigned long a1);
  10638. extern void time_hardirqs_off(unsigned long a0, unsigned long a1);
  10639. diff -Nur linux-4.4.28.orig/include/linux/highmem.h linux-4.4.28/include/linux/highmem.h
  10640. --- linux-4.4.28.orig/include/linux/highmem.h 2016-10-28 09:53:25.000000000 +0200
  10641. +++ linux-4.4.28/include/linux/highmem.h 2016-11-01 21:26:58.728339185 +0100
  10642. @@ -7,6 +7,7 @@
  10643. #include <linux/mm.h>
  10644. #include <linux/uaccess.h>
  10645. #include <linux/hardirq.h>
  10646. +#include <linux/sched.h>
  10647. #include <asm/cacheflush.h>
  10648. @@ -65,7 +66,7 @@
  10649. static inline void *kmap_atomic(struct page *page)
  10650. {
  10651. - preempt_disable();
  10652. + preempt_disable_nort();
  10653. pagefault_disable();
  10654. return page_address(page);
  10655. }
  10656. @@ -74,7 +75,7 @@
  10657. static inline void __kunmap_atomic(void *addr)
  10658. {
  10659. pagefault_enable();
  10660. - preempt_enable();
  10661. + preempt_enable_nort();
  10662. }
  10663. #define kmap_atomic_pfn(pfn) kmap_atomic(pfn_to_page(pfn))
  10664. @@ -86,32 +87,51 @@
  10665. #if defined(CONFIG_HIGHMEM) || defined(CONFIG_X86_32)
  10666. +#ifndef CONFIG_PREEMPT_RT_FULL
  10667. DECLARE_PER_CPU(int, __kmap_atomic_idx);
  10668. +#endif
  10669. static inline int kmap_atomic_idx_push(void)
  10670. {
  10671. +#ifndef CONFIG_PREEMPT_RT_FULL
  10672. int idx = __this_cpu_inc_return(__kmap_atomic_idx) - 1;
  10673. -#ifdef CONFIG_DEBUG_HIGHMEM
  10674. +# ifdef CONFIG_DEBUG_HIGHMEM
  10675. WARN_ON_ONCE(in_irq() && !irqs_disabled());
  10676. BUG_ON(idx >= KM_TYPE_NR);
  10677. -#endif
  10678. +# endif
  10679. return idx;
  10680. +#else
  10681. + current->kmap_idx++;
  10682. + BUG_ON(current->kmap_idx > KM_TYPE_NR);
  10683. + return current->kmap_idx - 1;
  10684. +#endif
  10685. }
  10686. static inline int kmap_atomic_idx(void)
  10687. {
  10688. +#ifndef CONFIG_PREEMPT_RT_FULL
  10689. return __this_cpu_read(__kmap_atomic_idx) - 1;
  10690. +#else
  10691. + return current->kmap_idx - 1;
  10692. +#endif
  10693. }
  10694. static inline void kmap_atomic_idx_pop(void)
  10695. {
  10696. -#ifdef CONFIG_DEBUG_HIGHMEM
  10697. +#ifndef CONFIG_PREEMPT_RT_FULL
  10698. +# ifdef CONFIG_DEBUG_HIGHMEM
  10699. int idx = __this_cpu_dec_return(__kmap_atomic_idx);
  10700. BUG_ON(idx < 0);
  10701. -#else
  10702. +# else
  10703. __this_cpu_dec(__kmap_atomic_idx);
  10704. +# endif
  10705. +#else
  10706. + current->kmap_idx--;
  10707. +# ifdef CONFIG_DEBUG_HIGHMEM
  10708. + BUG_ON(current->kmap_idx < 0);
  10709. +# endif
  10710. #endif
  10711. }
  10712. diff -Nur linux-4.4.28.orig/include/linux/hrtimer.h linux-4.4.28/include/linux/hrtimer.h
  10713. --- linux-4.4.28.orig/include/linux/hrtimer.h 2016-10-28 09:53:25.000000000 +0200
  10714. +++ linux-4.4.28/include/linux/hrtimer.h 2016-11-01 21:26:58.728339185 +0100
  10715. @@ -87,6 +87,9 @@
  10716. * @function: timer expiry callback function
  10717. * @base: pointer to the timer base (per cpu and per clock)
  10718. * @state: state information (See bit values above)
  10719. + * @cb_entry: list entry to defer timers from hardirq context
  10720. + * @irqsafe: timer can run in hardirq context
  10721. + * @praecox: timer expiry time if expired at the time of programming
  10722. * @is_rel: Set if the timer was armed relative
  10723. * @start_pid: timer statistics field to store the pid of the task which
  10724. * started the timer
  10725. @@ -103,6 +106,11 @@
  10726. enum hrtimer_restart (*function)(struct hrtimer *);
  10727. struct hrtimer_clock_base *base;
  10728. u8 state;
  10729. + struct list_head cb_entry;
  10730. + int irqsafe;
  10731. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  10732. + ktime_t praecox;
  10733. +#endif
  10734. u8 is_rel;
  10735. #ifdef CONFIG_TIMER_STATS
  10736. int start_pid;
  10737. @@ -123,11 +131,7 @@
  10738. struct task_struct *task;
  10739. };
  10740. -#ifdef CONFIG_64BIT
  10741. # define HRTIMER_CLOCK_BASE_ALIGN 64
  10742. -#else
  10743. -# define HRTIMER_CLOCK_BASE_ALIGN 32
  10744. -#endif
  10745. /**
  10746. * struct hrtimer_clock_base - the timer base for a specific clock
  10747. @@ -136,6 +140,7 @@
  10748. * timer to a base on another cpu.
  10749. * @clockid: clock id for per_cpu support
  10750. * @active: red black tree root node for the active timers
  10751. + * @expired: list head for deferred timers.
  10752. * @get_time: function to retrieve the current time of the clock
  10753. * @offset: offset of this clock to the monotonic base
  10754. */
  10755. @@ -144,6 +149,7 @@
  10756. int index;
  10757. clockid_t clockid;
  10758. struct timerqueue_head active;
  10759. + struct list_head expired;
  10760. ktime_t (*get_time)(void);
  10761. ktime_t offset;
  10762. } __attribute__((__aligned__(HRTIMER_CLOCK_BASE_ALIGN)));
  10763. @@ -187,6 +193,7 @@
  10764. raw_spinlock_t lock;
  10765. seqcount_t seq;
  10766. struct hrtimer *running;
  10767. + struct hrtimer *running_soft;
  10768. unsigned int cpu;
  10769. unsigned int active_bases;
  10770. unsigned int clock_was_set_seq;
  10771. @@ -203,6 +210,9 @@
  10772. unsigned int nr_hangs;
  10773. unsigned int max_hang_time;
  10774. #endif
  10775. +#ifdef CONFIG_PREEMPT_RT_BASE
  10776. + wait_queue_head_t wait;
  10777. +#endif
  10778. struct hrtimer_clock_base clock_base[HRTIMER_MAX_CLOCK_BASES];
  10779. } ____cacheline_aligned;
  10780. @@ -412,6 +422,13 @@
  10781. hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
  10782. }
  10783. +/* Softirq preemption could deadlock timer removal */
  10784. +#ifdef CONFIG_PREEMPT_RT_BASE
  10785. + extern void hrtimer_wait_for_timer(const struct hrtimer *timer);
  10786. +#else
  10787. +# define hrtimer_wait_for_timer(timer) do { cpu_relax(); } while (0)
  10788. +#endif
  10789. +
  10790. /* Query timers: */
  10791. extern ktime_t __hrtimer_get_remaining(const struct hrtimer *timer, bool adjust);
  10792. @@ -436,7 +453,7 @@
  10793. * Helper function to check, whether the timer is running the callback
  10794. * function
  10795. */
  10796. -static inline int hrtimer_callback_running(struct hrtimer *timer)
  10797. +static inline int hrtimer_callback_running(const struct hrtimer *timer)
  10798. {
  10799. return timer->base->cpu_base->running == timer;
  10800. }
  10801. diff -Nur linux-4.4.28.orig/include/linux/idr.h linux-4.4.28/include/linux/idr.h
  10802. --- linux-4.4.28.orig/include/linux/idr.h 2016-10-28 09:53:25.000000000 +0200
  10803. +++ linux-4.4.28/include/linux/idr.h 2016-11-01 21:26:58.728339185 +0100
  10804. @@ -95,10 +95,14 @@
  10805. * Each idr_preload() should be matched with an invocation of this
  10806. * function. See idr_preload() for details.
  10807. */
  10808. +#ifdef CONFIG_PREEMPT_RT_FULL
  10809. +void idr_preload_end(void);
  10810. +#else
  10811. static inline void idr_preload_end(void)
  10812. {
  10813. preempt_enable();
  10814. }
  10815. +#endif
  10816. /**
  10817. * idr_find - return pointer for given id
  10818. diff -Nur linux-4.4.28.orig/include/linux/init_task.h linux-4.4.28/include/linux/init_task.h
  10819. --- linux-4.4.28.orig/include/linux/init_task.h 2016-10-28 09:53:25.000000000 +0200
  10820. +++ linux-4.4.28/include/linux/init_task.h 2016-11-01 21:26:58.728339185 +0100
  10821. @@ -148,9 +148,15 @@
  10822. # define INIT_PERF_EVENTS(tsk)
  10823. #endif
  10824. +#ifdef CONFIG_PREEMPT_RT_BASE
  10825. +# define INIT_TIMER_LIST .posix_timer_list = NULL,
  10826. +#else
  10827. +# define INIT_TIMER_LIST
  10828. +#endif
  10829. +
  10830. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  10831. # define INIT_VTIME(tsk) \
  10832. - .vtime_seqlock = __SEQLOCK_UNLOCKED(tsk.vtime_seqlock), \
  10833. + .vtime_seqcount = SEQCNT_ZERO(tsk.vtime_seqcount), \
  10834. .vtime_snap = 0, \
  10835. .vtime_snap_whence = VTIME_SYS,
  10836. #else
  10837. @@ -239,6 +245,7 @@
  10838. .cpu_timers = INIT_CPU_TIMERS(tsk.cpu_timers), \
  10839. .pi_lock = __RAW_SPIN_LOCK_UNLOCKED(tsk.pi_lock), \
  10840. .timer_slack_ns = 50000, /* 50 usec default slack */ \
  10841. + INIT_TIMER_LIST \
  10842. .pids = { \
  10843. [PIDTYPE_PID] = INIT_PID_LINK(PIDTYPE_PID), \
  10844. [PIDTYPE_PGID] = INIT_PID_LINK(PIDTYPE_PGID), \
  10845. diff -Nur linux-4.4.28.orig/include/linux/interrupt.h linux-4.4.28/include/linux/interrupt.h
  10846. --- linux-4.4.28.orig/include/linux/interrupt.h 2016-10-28 09:53:25.000000000 +0200
  10847. +++ linux-4.4.28/include/linux/interrupt.h 2016-11-01 21:26:58.728339185 +0100
  10848. @@ -61,6 +61,7 @@
  10849. * interrupt handler after suspending interrupts. For system
  10850. * wakeup devices users need to implement wakeup detection in
  10851. * their interrupt handlers.
  10852. + * IRQF_NO_SOFTIRQ_CALL - Do not process softirqs in the irq thread context (RT)
  10853. */
  10854. #define IRQF_SHARED 0x00000080
  10855. #define IRQF_PROBE_SHARED 0x00000100
  10856. @@ -74,6 +75,7 @@
  10857. #define IRQF_NO_THREAD 0x00010000
  10858. #define IRQF_EARLY_RESUME 0x00020000
  10859. #define IRQF_COND_SUSPEND 0x00040000
  10860. +#define IRQF_NO_SOFTIRQ_CALL 0x00080000
  10861. #define IRQF_TIMER (__IRQF_TIMER | IRQF_NO_SUSPEND | IRQF_NO_THREAD)
  10862. @@ -186,7 +188,7 @@
  10863. #ifdef CONFIG_LOCKDEP
  10864. # define local_irq_enable_in_hardirq() do { } while (0)
  10865. #else
  10866. -# define local_irq_enable_in_hardirq() local_irq_enable()
  10867. +# define local_irq_enable_in_hardirq() local_irq_enable_nort()
  10868. #endif
  10869. extern void disable_irq_nosync(unsigned int irq);
  10870. @@ -206,6 +208,7 @@
  10871. * @irq: Interrupt to which notification applies
  10872. * @kref: Reference count, for internal use
  10873. * @work: Work item, for internal use
  10874. + * @list: List item for deferred callbacks
  10875. * @notify: Function to be called on change. This will be
  10876. * called in process context.
  10877. * @release: Function to be called on release. This will be
  10878. @@ -217,6 +220,7 @@
  10879. unsigned int irq;
  10880. struct kref kref;
  10881. struct work_struct work;
  10882. + struct list_head list;
  10883. void (*notify)(struct irq_affinity_notify *, const cpumask_t *mask);
  10884. void (*release)(struct kref *ref);
  10885. };
  10886. @@ -379,9 +383,13 @@
  10887. bool state);
  10888. #ifdef CONFIG_IRQ_FORCED_THREADING
  10889. +# ifndef CONFIG_PREEMPT_RT_BASE
  10890. extern bool force_irqthreads;
  10891. +# else
  10892. +# define force_irqthreads (true)
  10893. +# endif
  10894. #else
  10895. -#define force_irqthreads (0)
  10896. +#define force_irqthreads (false)
  10897. #endif
  10898. #ifndef __ARCH_SET_SOFTIRQ_PENDING
  10899. @@ -438,9 +446,10 @@
  10900. void (*action)(struct softirq_action *);
  10901. };
  10902. +#ifndef CONFIG_PREEMPT_RT_FULL
  10903. asmlinkage void do_softirq(void);
  10904. asmlinkage void __do_softirq(void);
  10905. -
  10906. +static inline void thread_do_softirq(void) { do_softirq(); }
  10907. #ifdef __ARCH_HAS_DO_SOFTIRQ
  10908. void do_softirq_own_stack(void);
  10909. #else
  10910. @@ -449,13 +458,25 @@
  10911. __do_softirq();
  10912. }
  10913. #endif
  10914. +#else
  10915. +extern void thread_do_softirq(void);
  10916. +#endif
  10917. extern void open_softirq(int nr, void (*action)(struct softirq_action *));
  10918. extern void softirq_init(void);
  10919. extern void __raise_softirq_irqoff(unsigned int nr);
  10920. +#ifdef CONFIG_PREEMPT_RT_FULL
  10921. +extern void __raise_softirq_irqoff_ksoft(unsigned int nr);
  10922. +#else
  10923. +static inline void __raise_softirq_irqoff_ksoft(unsigned int nr)
  10924. +{
  10925. + __raise_softirq_irqoff(nr);
  10926. +}
  10927. +#endif
  10928. extern void raise_softirq_irqoff(unsigned int nr);
  10929. extern void raise_softirq(unsigned int nr);
  10930. +extern void softirq_check_pending_idle(void);
  10931. DECLARE_PER_CPU(struct task_struct *, ksoftirqd);
  10932. @@ -477,8 +498,9 @@
  10933. to be executed on some cpu at least once after this.
  10934. * If the tasklet is already scheduled, but its execution is still not
  10935. started, it will be executed only once.
  10936. - * If this tasklet is already running on another CPU (or schedule is called
  10937. - from tasklet itself), it is rescheduled for later.
  10938. + * If this tasklet is already running on another CPU, it is rescheduled
  10939. + for later.
  10940. + * Schedule must not be called from the tasklet itself (a lockup occurs)
  10941. * Tasklet is strictly serialized wrt itself, but not
  10942. wrt another tasklets. If client needs some intertask synchronization,
  10943. he makes it with spinlocks.
  10944. @@ -503,27 +525,36 @@
  10945. enum
  10946. {
  10947. TASKLET_STATE_SCHED, /* Tasklet is scheduled for execution */
  10948. - TASKLET_STATE_RUN /* Tasklet is running (SMP only) */
  10949. + TASKLET_STATE_RUN, /* Tasklet is running (SMP only) */
  10950. + TASKLET_STATE_PENDING /* Tasklet is pending */
  10951. };
  10952. -#ifdef CONFIG_SMP
  10953. +#define TASKLET_STATEF_SCHED (1 << TASKLET_STATE_SCHED)
  10954. +#define TASKLET_STATEF_RUN (1 << TASKLET_STATE_RUN)
  10955. +#define TASKLET_STATEF_PENDING (1 << TASKLET_STATE_PENDING)
  10956. +
  10957. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  10958. static inline int tasklet_trylock(struct tasklet_struct *t)
  10959. {
  10960. return !test_and_set_bit(TASKLET_STATE_RUN, &(t)->state);
  10961. }
  10962. +static inline int tasklet_tryunlock(struct tasklet_struct *t)
  10963. +{
  10964. + return cmpxchg(&t->state, TASKLET_STATEF_RUN, 0) == TASKLET_STATEF_RUN;
  10965. +}
  10966. +
  10967. static inline void tasklet_unlock(struct tasklet_struct *t)
  10968. {
  10969. smp_mb__before_atomic();
  10970. clear_bit(TASKLET_STATE_RUN, &(t)->state);
  10971. }
  10972. -static inline void tasklet_unlock_wait(struct tasklet_struct *t)
  10973. -{
  10974. - while (test_bit(TASKLET_STATE_RUN, &(t)->state)) { barrier(); }
  10975. -}
  10976. +extern void tasklet_unlock_wait(struct tasklet_struct *t);
  10977. +
  10978. #else
  10979. #define tasklet_trylock(t) 1
  10980. +#define tasklet_tryunlock(t) 1
  10981. #define tasklet_unlock_wait(t) do { } while (0)
  10982. #define tasklet_unlock(t) do { } while (0)
  10983. #endif
  10984. @@ -572,12 +603,7 @@
  10985. smp_mb();
  10986. }
  10987. -static inline void tasklet_enable(struct tasklet_struct *t)
  10988. -{
  10989. - smp_mb__before_atomic();
  10990. - atomic_dec(&t->count);
  10991. -}
  10992. -
  10993. +extern void tasklet_enable(struct tasklet_struct *t);
  10994. extern void tasklet_kill(struct tasklet_struct *t);
  10995. extern void tasklet_kill_immediate(struct tasklet_struct *t, unsigned int cpu);
  10996. extern void tasklet_init(struct tasklet_struct *t,
  10997. @@ -608,6 +634,12 @@
  10998. tasklet_kill(&ttimer->tasklet);
  10999. }
  11000. +#ifdef CONFIG_PREEMPT_RT_FULL
  11001. +extern void softirq_early_init(void);
  11002. +#else
  11003. +static inline void softirq_early_init(void) { }
  11004. +#endif
  11005. +
  11006. /*
  11007. * Autoprobing for irqs:
  11008. *
  11009. diff -Nur linux-4.4.28.orig/include/linux/irqdesc.h linux-4.4.28/include/linux/irqdesc.h
  11010. --- linux-4.4.28.orig/include/linux/irqdesc.h 2016-10-28 09:53:25.000000000 +0200
  11011. +++ linux-4.4.28/include/linux/irqdesc.h 2016-11-01 21:26:58.732339449 +0100
  11012. @@ -61,6 +61,7 @@
  11013. unsigned int irqs_unhandled;
  11014. atomic_t threads_handled;
  11015. int threads_handled_last;
  11016. + u64 random_ip;
  11017. raw_spinlock_t lock;
  11018. struct cpumask *percpu_enabled;
  11019. #ifdef CONFIG_SMP
  11020. diff -Nur linux-4.4.28.orig/include/linux/irqflags.h linux-4.4.28/include/linux/irqflags.h
  11021. --- linux-4.4.28.orig/include/linux/irqflags.h 2016-10-28 09:53:25.000000000 +0200
  11022. +++ linux-4.4.28/include/linux/irqflags.h 2016-11-01 21:26:58.732339449 +0100
  11023. @@ -25,8 +25,6 @@
  11024. # define trace_softirqs_enabled(p) ((p)->softirqs_enabled)
  11025. # define trace_hardirq_enter() do { current->hardirq_context++; } while (0)
  11026. # define trace_hardirq_exit() do { current->hardirq_context--; } while (0)
  11027. -# define lockdep_softirq_enter() do { current->softirq_context++; } while (0)
  11028. -# define lockdep_softirq_exit() do { current->softirq_context--; } while (0)
  11029. # define INIT_TRACE_IRQFLAGS .softirqs_enabled = 1,
  11030. #else
  11031. # define trace_hardirqs_on() do { } while (0)
  11032. @@ -39,9 +37,15 @@
  11033. # define trace_softirqs_enabled(p) 0
  11034. # define trace_hardirq_enter() do { } while (0)
  11035. # define trace_hardirq_exit() do { } while (0)
  11036. +# define INIT_TRACE_IRQFLAGS
  11037. +#endif
  11038. +
  11039. +#if defined(CONFIG_TRACE_IRQFLAGS) && !defined(CONFIG_PREEMPT_RT_FULL)
  11040. +# define lockdep_softirq_enter() do { current->softirq_context++; } while (0)
  11041. +# define lockdep_softirq_exit() do { current->softirq_context--; } while (0)
  11042. +#else
  11043. # define lockdep_softirq_enter() do { } while (0)
  11044. # define lockdep_softirq_exit() do { } while (0)
  11045. -# define INIT_TRACE_IRQFLAGS
  11046. #endif
  11047. #if defined(CONFIG_IRQSOFF_TRACER) || \
  11048. @@ -148,4 +152,23 @@
  11049. #define irqs_disabled_flags(flags) raw_irqs_disabled_flags(flags)
  11050. +/*
  11051. + * local_irq* variants depending on RT/!RT
  11052. + */
  11053. +#ifdef CONFIG_PREEMPT_RT_FULL
  11054. +# define local_irq_disable_nort() do { } while (0)
  11055. +# define local_irq_enable_nort() do { } while (0)
  11056. +# define local_irq_save_nort(flags) local_save_flags(flags)
  11057. +# define local_irq_restore_nort(flags) (void)(flags)
  11058. +# define local_irq_disable_rt() local_irq_disable()
  11059. +# define local_irq_enable_rt() local_irq_enable()
  11060. +#else
  11061. +# define local_irq_disable_nort() local_irq_disable()
  11062. +# define local_irq_enable_nort() local_irq_enable()
  11063. +# define local_irq_save_nort(flags) local_irq_save(flags)
  11064. +# define local_irq_restore_nort(flags) local_irq_restore(flags)
  11065. +# define local_irq_disable_rt() do { } while (0)
  11066. +# define local_irq_enable_rt() do { } while (0)
  11067. +#endif
  11068. +
  11069. #endif
  11070. diff -Nur linux-4.4.28.orig/include/linux/irq.h linux-4.4.28/include/linux/irq.h
  11071. --- linux-4.4.28.orig/include/linux/irq.h 2016-10-28 09:53:25.000000000 +0200
  11072. +++ linux-4.4.28/include/linux/irq.h 2016-11-01 21:26:58.728339185 +0100
  11073. @@ -72,6 +72,7 @@
  11074. * IRQ_IS_POLLED - Always polled by another interrupt. Exclude
  11075. * it from the spurious interrupt detection
  11076. * mechanism and from core side polling.
  11077. + * IRQ_NO_SOFTIRQ_CALL - No softirq processing in the irq thread context (RT)
  11078. * IRQ_DISABLE_UNLAZY - Disable lazy irq disable
  11079. */
  11080. enum {
  11081. @@ -99,13 +100,14 @@
  11082. IRQ_PER_CPU_DEVID = (1 << 17),
  11083. IRQ_IS_POLLED = (1 << 18),
  11084. IRQ_DISABLE_UNLAZY = (1 << 19),
  11085. + IRQ_NO_SOFTIRQ_CALL = (1 << 20),
  11086. };
  11087. #define IRQF_MODIFY_MASK \
  11088. (IRQ_TYPE_SENSE_MASK | IRQ_NOPROBE | IRQ_NOREQUEST | \
  11089. IRQ_NOAUTOEN | IRQ_MOVE_PCNTXT | IRQ_LEVEL | IRQ_NO_BALANCING | \
  11090. IRQ_PER_CPU | IRQ_NESTED_THREAD | IRQ_NOTHREAD | IRQ_PER_CPU_DEVID | \
  11091. - IRQ_IS_POLLED | IRQ_DISABLE_UNLAZY)
  11092. + IRQ_IS_POLLED | IRQ_DISABLE_UNLAZY | IRQ_NO_SOFTIRQ_CALL)
  11093. #define IRQ_NO_BALANCING_MASK (IRQ_PER_CPU | IRQ_NO_BALANCING)
  11094. diff -Nur linux-4.4.28.orig/include/linux/irq_work.h linux-4.4.28/include/linux/irq_work.h
  11095. --- linux-4.4.28.orig/include/linux/irq_work.h 2016-10-28 09:53:25.000000000 +0200
  11096. +++ linux-4.4.28/include/linux/irq_work.h 2016-11-01 21:26:58.728339185 +0100
  11097. @@ -16,6 +16,7 @@
  11098. #define IRQ_WORK_BUSY 2UL
  11099. #define IRQ_WORK_FLAGS 3UL
  11100. #define IRQ_WORK_LAZY 4UL /* Doesn't want IPI, wait for tick */
  11101. +#define IRQ_WORK_HARD_IRQ 8UL /* Run hard IRQ context, even on RT */
  11102. struct irq_work {
  11103. unsigned long flags;
  11104. @@ -51,4 +52,10 @@
  11105. static inline void irq_work_run(void) { }
  11106. #endif
  11107. +#if defined(CONFIG_IRQ_WORK) && defined(CONFIG_PREEMPT_RT_FULL)
  11108. +void irq_work_tick_soft(void);
  11109. +#else
  11110. +static inline void irq_work_tick_soft(void) { }
  11111. +#endif
  11112. +
  11113. #endif /* _LINUX_IRQ_WORK_H */
  11114. diff -Nur linux-4.4.28.orig/include/linux/jbd2.h linux-4.4.28/include/linux/jbd2.h
  11115. --- linux-4.4.28.orig/include/linux/jbd2.h 2016-10-28 09:53:25.000000000 +0200
  11116. +++ linux-4.4.28/include/linux/jbd2.h 2016-11-01 21:26:58.732339449 +0100
  11117. @@ -352,32 +352,56 @@
  11118. static inline void jbd_lock_bh_state(struct buffer_head *bh)
  11119. {
  11120. +#ifndef CONFIG_PREEMPT_RT_BASE
  11121. bit_spin_lock(BH_State, &bh->b_state);
  11122. +#else
  11123. + spin_lock(&bh->b_state_lock);
  11124. +#endif
  11125. }
  11126. static inline int jbd_trylock_bh_state(struct buffer_head *bh)
  11127. {
  11128. +#ifndef CONFIG_PREEMPT_RT_BASE
  11129. return bit_spin_trylock(BH_State, &bh->b_state);
  11130. +#else
  11131. + return spin_trylock(&bh->b_state_lock);
  11132. +#endif
  11133. }
  11134. static inline int jbd_is_locked_bh_state(struct buffer_head *bh)
  11135. {
  11136. +#ifndef CONFIG_PREEMPT_RT_BASE
  11137. return bit_spin_is_locked(BH_State, &bh->b_state);
  11138. +#else
  11139. + return spin_is_locked(&bh->b_state_lock);
  11140. +#endif
  11141. }
  11142. static inline void jbd_unlock_bh_state(struct buffer_head *bh)
  11143. {
  11144. +#ifndef CONFIG_PREEMPT_RT_BASE
  11145. bit_spin_unlock(BH_State, &bh->b_state);
  11146. +#else
  11147. + spin_unlock(&bh->b_state_lock);
  11148. +#endif
  11149. }
  11150. static inline void jbd_lock_bh_journal_head(struct buffer_head *bh)
  11151. {
  11152. +#ifndef CONFIG_PREEMPT_RT_BASE
  11153. bit_spin_lock(BH_JournalHead, &bh->b_state);
  11154. +#else
  11155. + spin_lock(&bh->b_journal_head_lock);
  11156. +#endif
  11157. }
  11158. static inline void jbd_unlock_bh_journal_head(struct buffer_head *bh)
  11159. {
  11160. +#ifndef CONFIG_PREEMPT_RT_BASE
  11161. bit_spin_unlock(BH_JournalHead, &bh->b_state);
  11162. +#else
  11163. + spin_unlock(&bh->b_journal_head_lock);
  11164. +#endif
  11165. }
  11166. #define J_ASSERT(assert) BUG_ON(!(assert))
  11167. diff -Nur linux-4.4.28.orig/include/linux/kdb.h linux-4.4.28/include/linux/kdb.h
  11168. --- linux-4.4.28.orig/include/linux/kdb.h 2016-10-28 09:53:25.000000000 +0200
  11169. +++ linux-4.4.28/include/linux/kdb.h 2016-11-01 21:26:58.732339449 +0100
  11170. @@ -167,6 +167,7 @@
  11171. extern __printf(1, 2) int kdb_printf(const char *, ...);
  11172. typedef __printf(1, 2) int (*kdb_printf_t)(const char *, ...);
  11173. +#define in_kdb_printk() (kdb_trap_printk)
  11174. extern void kdb_init(int level);
  11175. /* Access to kdb specific polling devices */
  11176. @@ -201,6 +202,7 @@
  11177. extern int kdb_unregister(char *);
  11178. #else /* ! CONFIG_KGDB_KDB */
  11179. static inline __printf(1, 2) int kdb_printf(const char *fmt, ...) { return 0; }
  11180. +#define in_kdb_printk() (0)
  11181. static inline void kdb_init(int level) {}
  11182. static inline int kdb_register(char *cmd, kdb_func_t func, char *usage,
  11183. char *help, short minlen) { return 0; }
  11184. diff -Nur linux-4.4.28.orig/include/linux/kernel.h linux-4.4.28/include/linux/kernel.h
  11185. --- linux-4.4.28.orig/include/linux/kernel.h 2016-10-28 09:53:25.000000000 +0200
  11186. +++ linux-4.4.28/include/linux/kernel.h 2016-11-01 21:26:58.732339449 +0100
  11187. @@ -188,6 +188,9 @@
  11188. */
  11189. # define might_sleep() \
  11190. do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
  11191. +
  11192. +# define might_sleep_no_state_check() \
  11193. + do { ___might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
  11194. # define sched_annotate_sleep() (current->task_state_change = 0)
  11195. #else
  11196. static inline void ___might_sleep(const char *file, int line,
  11197. @@ -195,6 +198,7 @@
  11198. static inline void __might_sleep(const char *file, int line,
  11199. int preempt_offset) { }
  11200. # define might_sleep() do { might_resched(); } while (0)
  11201. +# define might_sleep_no_state_check() do { might_resched(); } while (0)
  11202. # define sched_annotate_sleep() do { } while (0)
  11203. #endif
  11204. @@ -255,6 +259,7 @@
  11205. __printf(1, 2)
  11206. void panic(const char *fmt, ...)
  11207. __noreturn __cold;
  11208. +void nmi_panic(struct pt_regs *regs, const char *msg);
  11209. extern void oops_enter(void);
  11210. extern void oops_exit(void);
  11211. void print_oops_end_marker(void);
  11212. @@ -448,6 +453,14 @@
  11213. extern bool crash_kexec_post_notifiers;
  11214. /*
  11215. + * panic_cpu is used for synchronizing panic() and crash_kexec() execution. It
  11216. + * holds a CPU number which is executing panic() currently. A value of
  11217. + * PANIC_CPU_INVALID means no CPU has entered panic() or crash_kexec().
  11218. + */
  11219. +extern atomic_t panic_cpu;
  11220. +#define PANIC_CPU_INVALID -1
  11221. +
  11222. +/*
  11223. * Only to be used by arch init code. If the user over-wrote the default
  11224. * CONFIG_PANIC_TIMEOUT, honor it.
  11225. */
  11226. @@ -475,6 +488,7 @@
  11227. SYSTEM_HALT,
  11228. SYSTEM_POWER_OFF,
  11229. SYSTEM_RESTART,
  11230. + SYSTEM_SUSPEND,
  11231. } system_state;
  11232. #define TAINT_PROPRIETARY_MODULE 0
  11233. diff -Nur linux-4.4.28.orig/include/linux/kvm_host.h linux-4.4.28/include/linux/kvm_host.h
  11234. --- linux-4.4.28.orig/include/linux/kvm_host.h 2016-10-28 09:53:25.000000000 +0200
  11235. +++ linux-4.4.28/include/linux/kvm_host.h 2016-11-01 21:26:58.732339449 +0100
  11236. @@ -25,6 +25,7 @@
  11237. #include <linux/irqflags.h>
  11238. #include <linux/context_tracking.h>
  11239. #include <linux/irqbypass.h>
  11240. +#include <linux/swait.h>
  11241. #include <asm/signal.h>
  11242. #include <linux/kvm.h>
  11243. @@ -243,7 +244,7 @@
  11244. int fpu_active;
  11245. int guest_fpu_loaded, guest_xcr0_loaded;
  11246. unsigned char fpu_counter;
  11247. - wait_queue_head_t wq;
  11248. + struct swait_queue_head wq;
  11249. struct pid *pid;
  11250. int sigset_active;
  11251. sigset_t sigset;
  11252. @@ -794,7 +795,7 @@
  11253. }
  11254. #endif
  11255. -static inline wait_queue_head_t *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu)
  11256. +static inline struct swait_queue_head *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu)
  11257. {
  11258. #ifdef __KVM_HAVE_ARCH_WQP
  11259. return vcpu->arch.wqp;
  11260. diff -Nur linux-4.4.28.orig/include/linux/lglock.h linux-4.4.28/include/linux/lglock.h
  11261. --- linux-4.4.28.orig/include/linux/lglock.h 2016-10-28 09:53:25.000000000 +0200
  11262. +++ linux-4.4.28/include/linux/lglock.h 2016-11-01 21:26:58.732339449 +0100
  11263. @@ -34,13 +34,30 @@
  11264. #endif
  11265. struct lglock {
  11266. +#ifdef CONFIG_PREEMPT_RT_FULL
  11267. + struct rt_mutex __percpu *lock;
  11268. +#else
  11269. arch_spinlock_t __percpu *lock;
  11270. +#endif
  11271. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  11272. struct lock_class_key lock_key;
  11273. struct lockdep_map lock_dep_map;
  11274. #endif
  11275. };
  11276. +#ifdef CONFIG_PREEMPT_RT_FULL
  11277. +# define DEFINE_LGLOCK(name) \
  11278. + static DEFINE_PER_CPU(struct rt_mutex, name ## _lock) \
  11279. + = __RT_MUTEX_INITIALIZER( name ## _lock); \
  11280. + struct lglock name = { .lock = &name ## _lock }
  11281. +
  11282. +# define DEFINE_STATIC_LGLOCK(name) \
  11283. + static DEFINE_PER_CPU(struct rt_mutex, name ## _lock) \
  11284. + = __RT_MUTEX_INITIALIZER( name ## _lock); \
  11285. + static struct lglock name = { .lock = &name ## _lock }
  11286. +
  11287. +#else
  11288. +
  11289. #define DEFINE_LGLOCK(name) \
  11290. static DEFINE_PER_CPU(arch_spinlock_t, name ## _lock) \
  11291. = __ARCH_SPIN_LOCK_UNLOCKED; \
  11292. @@ -50,6 +67,7 @@
  11293. static DEFINE_PER_CPU(arch_spinlock_t, name ## _lock) \
  11294. = __ARCH_SPIN_LOCK_UNLOCKED; \
  11295. static struct lglock name = { .lock = &name ## _lock }
  11296. +#endif
  11297. void lg_lock_init(struct lglock *lg, char *name);
  11298. @@ -64,6 +82,12 @@
  11299. void lg_global_lock(struct lglock *lg);
  11300. void lg_global_unlock(struct lglock *lg);
  11301. +#ifndef CONFIG_PREEMPT_RT_FULL
  11302. +#define lg_global_trylock_relax(name) lg_global_lock(name)
  11303. +#else
  11304. +void lg_global_trylock_relax(struct lglock *lg);
  11305. +#endif
  11306. +
  11307. #else
  11308. /* When !CONFIG_SMP, map lglock to spinlock */
  11309. #define lglock spinlock
  11310. diff -Nur linux-4.4.28.orig/include/linux/list_bl.h linux-4.4.28/include/linux/list_bl.h
  11311. --- linux-4.4.28.orig/include/linux/list_bl.h 2016-10-28 09:53:25.000000000 +0200
  11312. +++ linux-4.4.28/include/linux/list_bl.h 2016-11-01 21:26:58.732339449 +0100
  11313. @@ -2,6 +2,7 @@
  11314. #define _LINUX_LIST_BL_H
  11315. #include <linux/list.h>
  11316. +#include <linux/spinlock.h>
  11317. #include <linux/bit_spinlock.h>
  11318. /*
  11319. @@ -32,13 +33,24 @@
  11320. struct hlist_bl_head {
  11321. struct hlist_bl_node *first;
  11322. +#ifdef CONFIG_PREEMPT_RT_BASE
  11323. + raw_spinlock_t lock;
  11324. +#endif
  11325. };
  11326. struct hlist_bl_node {
  11327. struct hlist_bl_node *next, **pprev;
  11328. };
  11329. -#define INIT_HLIST_BL_HEAD(ptr) \
  11330. - ((ptr)->first = NULL)
  11331. +
  11332. +#ifdef CONFIG_PREEMPT_RT_BASE
  11333. +#define INIT_HLIST_BL_HEAD(h) \
  11334. +do { \
  11335. + (h)->first = NULL; \
  11336. + raw_spin_lock_init(&(h)->lock); \
  11337. +} while (0)
  11338. +#else
  11339. +#define INIT_HLIST_BL_HEAD(h) (h)->first = NULL
  11340. +#endif
  11341. static inline void INIT_HLIST_BL_NODE(struct hlist_bl_node *h)
  11342. {
  11343. @@ -118,12 +130,26 @@
  11344. static inline void hlist_bl_lock(struct hlist_bl_head *b)
  11345. {
  11346. +#ifndef CONFIG_PREEMPT_RT_BASE
  11347. bit_spin_lock(0, (unsigned long *)b);
  11348. +#else
  11349. + raw_spin_lock(&b->lock);
  11350. +#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  11351. + __set_bit(0, (unsigned long *)b);
  11352. +#endif
  11353. +#endif
  11354. }
  11355. static inline void hlist_bl_unlock(struct hlist_bl_head *b)
  11356. {
  11357. +#ifndef CONFIG_PREEMPT_RT_BASE
  11358. __bit_spin_unlock(0, (unsigned long *)b);
  11359. +#else
  11360. +#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  11361. + __clear_bit(0, (unsigned long *)b);
  11362. +#endif
  11363. + raw_spin_unlock(&b->lock);
  11364. +#endif
  11365. }
  11366. static inline bool hlist_bl_is_locked(struct hlist_bl_head *b)
  11367. diff -Nur linux-4.4.28.orig/include/linux/locallock.h linux-4.4.28/include/linux/locallock.h
  11368. --- linux-4.4.28.orig/include/linux/locallock.h 1970-01-01 01:00:00.000000000 +0100
  11369. +++ linux-4.4.28/include/linux/locallock.h 2016-11-01 21:26:58.732339449 +0100
  11370. @@ -0,0 +1,276 @@
  11371. +#ifndef _LINUX_LOCALLOCK_H
  11372. +#define _LINUX_LOCALLOCK_H
  11373. +
  11374. +#include <linux/percpu.h>
  11375. +#include <linux/spinlock.h>
  11376. +
  11377. +#ifdef CONFIG_PREEMPT_RT_BASE
  11378. +
  11379. +#ifdef CONFIG_DEBUG_SPINLOCK
  11380. +# define LL_WARN(cond) WARN_ON(cond)
  11381. +#else
  11382. +# define LL_WARN(cond) do { } while (0)
  11383. +#endif
  11384. +
  11385. +/*
  11386. + * per cpu lock based substitute for local_irq_*()
  11387. + */
  11388. +struct local_irq_lock {
  11389. + spinlock_t lock;
  11390. + struct task_struct *owner;
  11391. + int nestcnt;
  11392. + unsigned long flags;
  11393. +};
  11394. +
  11395. +#define DEFINE_LOCAL_IRQ_LOCK(lvar) \
  11396. + DEFINE_PER_CPU(struct local_irq_lock, lvar) = { \
  11397. + .lock = __SPIN_LOCK_UNLOCKED((lvar).lock) }
  11398. +
  11399. +#define DECLARE_LOCAL_IRQ_LOCK(lvar) \
  11400. + DECLARE_PER_CPU(struct local_irq_lock, lvar)
  11401. +
  11402. +#define local_irq_lock_init(lvar) \
  11403. + do { \
  11404. + int __cpu; \
  11405. + for_each_possible_cpu(__cpu) \
  11406. + spin_lock_init(&per_cpu(lvar, __cpu).lock); \
  11407. + } while (0)
  11408. +
  11409. +/*
  11410. + * spin_lock|trylock|unlock_local flavour that does not migrate disable
  11411. + * used for __local_lock|trylock|unlock where get_local_var/put_local_var
  11412. + * already takes care of the migrate_disable/enable
  11413. + * for CONFIG_PREEMPT_BASE map to the normal spin_* calls.
  11414. + */
  11415. +#ifdef CONFIG_PREEMPT_RT_FULL
  11416. +# define spin_lock_local(lock) rt_spin_lock__no_mg(lock)
  11417. +# define spin_trylock_local(lock) rt_spin_trylock__no_mg(lock)
  11418. +# define spin_unlock_local(lock) rt_spin_unlock__no_mg(lock)
  11419. +#else
  11420. +# define spin_lock_local(lock) spin_lock(lock)
  11421. +# define spin_trylock_local(lock) spin_trylock(lock)
  11422. +# define spin_unlock_local(lock) spin_unlock(lock)
  11423. +#endif
  11424. +
  11425. +static inline void __local_lock(struct local_irq_lock *lv)
  11426. +{
  11427. + if (lv->owner != current) {
  11428. + spin_lock_local(&lv->lock);
  11429. + LL_WARN(lv->owner);
  11430. + LL_WARN(lv->nestcnt);
  11431. + lv->owner = current;
  11432. + }
  11433. + lv->nestcnt++;
  11434. +}
  11435. +
  11436. +#define local_lock(lvar) \
  11437. + do { __local_lock(&get_local_var(lvar)); } while (0)
  11438. +
  11439. +#define local_lock_on(lvar, cpu) \
  11440. + do { __local_lock(&per_cpu(lvar, cpu)); } while (0)
  11441. +
  11442. +static inline int __local_trylock(struct local_irq_lock *lv)
  11443. +{
  11444. + if (lv->owner != current && spin_trylock_local(&lv->lock)) {
  11445. + LL_WARN(lv->owner);
  11446. + LL_WARN(lv->nestcnt);
  11447. + lv->owner = current;
  11448. + lv->nestcnt = 1;
  11449. + return 1;
  11450. + }
  11451. + return 0;
  11452. +}
  11453. +
  11454. +#define local_trylock(lvar) \
  11455. + ({ \
  11456. + int __locked; \
  11457. + __locked = __local_trylock(&get_local_var(lvar)); \
  11458. + if (!__locked) \
  11459. + put_local_var(lvar); \
  11460. + __locked; \
  11461. + })
  11462. +
  11463. +static inline void __local_unlock(struct local_irq_lock *lv)
  11464. +{
  11465. + LL_WARN(lv->nestcnt == 0);
  11466. + LL_WARN(lv->owner != current);
  11467. + if (--lv->nestcnt)
  11468. + return;
  11469. +
  11470. + lv->owner = NULL;
  11471. + spin_unlock_local(&lv->lock);
  11472. +}
  11473. +
  11474. +#define local_unlock(lvar) \
  11475. + do { \
  11476. + __local_unlock(this_cpu_ptr(&lvar)); \
  11477. + put_local_var(lvar); \
  11478. + } while (0)
  11479. +
  11480. +#define local_unlock_on(lvar, cpu) \
  11481. + do { __local_unlock(&per_cpu(lvar, cpu)); } while (0)
  11482. +
  11483. +static inline void __local_lock_irq(struct local_irq_lock *lv)
  11484. +{
  11485. + spin_lock_irqsave(&lv->lock, lv->flags);
  11486. + LL_WARN(lv->owner);
  11487. + LL_WARN(lv->nestcnt);
  11488. + lv->owner = current;
  11489. + lv->nestcnt = 1;
  11490. +}
  11491. +
  11492. +#define local_lock_irq(lvar) \
  11493. + do { __local_lock_irq(&get_local_var(lvar)); } while (0)
  11494. +
  11495. +#define local_lock_irq_on(lvar, cpu) \
  11496. + do { __local_lock_irq(&per_cpu(lvar, cpu)); } while (0)
  11497. +
  11498. +static inline void __local_unlock_irq(struct local_irq_lock *lv)
  11499. +{
  11500. + LL_WARN(!lv->nestcnt);
  11501. + LL_WARN(lv->owner != current);
  11502. + lv->owner = NULL;
  11503. + lv->nestcnt = 0;
  11504. + spin_unlock_irq(&lv->lock);
  11505. +}
  11506. +
  11507. +#define local_unlock_irq(lvar) \
  11508. + do { \
  11509. + __local_unlock_irq(this_cpu_ptr(&lvar)); \
  11510. + put_local_var(lvar); \
  11511. + } while (0)
  11512. +
  11513. +#define local_unlock_irq_on(lvar, cpu) \
  11514. + do { \
  11515. + __local_unlock_irq(&per_cpu(lvar, cpu)); \
  11516. + } while (0)
  11517. +
  11518. +static inline int __local_lock_irqsave(struct local_irq_lock *lv)
  11519. +{
  11520. + if (lv->owner != current) {
  11521. + __local_lock_irq(lv);
  11522. + return 0;
  11523. + } else {
  11524. + lv->nestcnt++;
  11525. + return 1;
  11526. + }
  11527. +}
  11528. +
  11529. +#define local_lock_irqsave(lvar, _flags) \
  11530. + do { \
  11531. + if (__local_lock_irqsave(&get_local_var(lvar))) \
  11532. + put_local_var(lvar); \
  11533. + _flags = __this_cpu_read(lvar.flags); \
  11534. + } while (0)
  11535. +
  11536. +#define local_lock_irqsave_on(lvar, _flags, cpu) \
  11537. + do { \
  11538. + __local_lock_irqsave(&per_cpu(lvar, cpu)); \
  11539. + _flags = per_cpu(lvar, cpu).flags; \
  11540. + } while (0)
  11541. +
  11542. +static inline int __local_unlock_irqrestore(struct local_irq_lock *lv,
  11543. + unsigned long flags)
  11544. +{
  11545. + LL_WARN(!lv->nestcnt);
  11546. + LL_WARN(lv->owner != current);
  11547. + if (--lv->nestcnt)
  11548. + return 0;
  11549. +
  11550. + lv->owner = NULL;
  11551. + spin_unlock_irqrestore(&lv->lock, lv->flags);
  11552. + return 1;
  11553. +}
  11554. +
  11555. +#define local_unlock_irqrestore(lvar, flags) \
  11556. + do { \
  11557. + if (__local_unlock_irqrestore(this_cpu_ptr(&lvar), flags)) \
  11558. + put_local_var(lvar); \
  11559. + } while (0)
  11560. +
  11561. +#define local_unlock_irqrestore_on(lvar, flags, cpu) \
  11562. + do { \
  11563. + __local_unlock_irqrestore(&per_cpu(lvar, cpu), flags); \
  11564. + } while (0)
  11565. +
  11566. +#define local_spin_trylock_irq(lvar, lock) \
  11567. + ({ \
  11568. + int __locked; \
  11569. + local_lock_irq(lvar); \
  11570. + __locked = spin_trylock(lock); \
  11571. + if (!__locked) \
  11572. + local_unlock_irq(lvar); \
  11573. + __locked; \
  11574. + })
  11575. +
  11576. +#define local_spin_lock_irq(lvar, lock) \
  11577. + do { \
  11578. + local_lock_irq(lvar); \
  11579. + spin_lock(lock); \
  11580. + } while (0)
  11581. +
  11582. +#define local_spin_unlock_irq(lvar, lock) \
  11583. + do { \
  11584. + spin_unlock(lock); \
  11585. + local_unlock_irq(lvar); \
  11586. + } while (0)
  11587. +
  11588. +#define local_spin_lock_irqsave(lvar, lock, flags) \
  11589. + do { \
  11590. + local_lock_irqsave(lvar, flags); \
  11591. + spin_lock(lock); \
  11592. + } while (0)
  11593. +
  11594. +#define local_spin_unlock_irqrestore(lvar, lock, flags) \
  11595. + do { \
  11596. + spin_unlock(lock); \
  11597. + local_unlock_irqrestore(lvar, flags); \
  11598. + } while (0)
  11599. +
  11600. +#define get_locked_var(lvar, var) \
  11601. + (*({ \
  11602. + local_lock(lvar); \
  11603. + this_cpu_ptr(&var); \
  11604. + }))
  11605. +
  11606. +#define put_locked_var(lvar, var) local_unlock(lvar);
  11607. +
  11608. +#define local_lock_cpu(lvar) \
  11609. + ({ \
  11610. + local_lock(lvar); \
  11611. + smp_processor_id(); \
  11612. + })
  11613. +
  11614. +#define local_unlock_cpu(lvar) local_unlock(lvar)
  11615. +
  11616. +#else /* PREEMPT_RT_BASE */
  11617. +
  11618. +#define DEFINE_LOCAL_IRQ_LOCK(lvar) __typeof__(const int) lvar
  11619. +#define DECLARE_LOCAL_IRQ_LOCK(lvar) extern __typeof__(const int) lvar
  11620. +
  11621. +static inline void local_irq_lock_init(int lvar) { }
  11622. +
  11623. +#define local_lock(lvar) preempt_disable()
  11624. +#define local_unlock(lvar) preempt_enable()
  11625. +#define local_lock_irq(lvar) local_irq_disable()
  11626. +#define local_unlock_irq(lvar) local_irq_enable()
  11627. +#define local_lock_irqsave(lvar, flags) local_irq_save(flags)
  11628. +#define local_unlock_irqrestore(lvar, flags) local_irq_restore(flags)
  11629. +
  11630. +#define local_spin_trylock_irq(lvar, lock) spin_trylock_irq(lock)
  11631. +#define local_spin_lock_irq(lvar, lock) spin_lock_irq(lock)
  11632. +#define local_spin_unlock_irq(lvar, lock) spin_unlock_irq(lock)
  11633. +#define local_spin_lock_irqsave(lvar, lock, flags) \
  11634. + spin_lock_irqsave(lock, flags)
  11635. +#define local_spin_unlock_irqrestore(lvar, lock, flags) \
  11636. + spin_unlock_irqrestore(lock, flags)
  11637. +
  11638. +#define get_locked_var(lvar, var) get_cpu_var(var)
  11639. +#define put_locked_var(lvar, var) put_cpu_var(var)
  11640. +
  11641. +#define local_lock_cpu(lvar) get_cpu()
  11642. +#define local_unlock_cpu(lvar) put_cpu()
  11643. +
  11644. +#endif
  11645. +
  11646. +#endif
  11647. diff -Nur linux-4.4.28.orig/include/linux/mm_types.h linux-4.4.28/include/linux/mm_types.h
  11648. --- linux-4.4.28.orig/include/linux/mm_types.h 2016-10-28 09:53:25.000000000 +0200
  11649. +++ linux-4.4.28/include/linux/mm_types.h 2016-11-01 21:26:58.732339449 +0100
  11650. @@ -11,6 +11,7 @@
  11651. #include <linux/completion.h>
  11652. #include <linux/cpumask.h>
  11653. #include <linux/uprobes.h>
  11654. +#include <linux/rcupdate.h>
  11655. #include <linux/page-flags-layout.h>
  11656. #include <asm/page.h>
  11657. #include <asm/mmu.h>
  11658. @@ -504,6 +505,9 @@
  11659. bool tlb_flush_pending;
  11660. #endif
  11661. struct uprobes_state uprobes_state;
  11662. +#ifdef CONFIG_PREEMPT_RT_BASE
  11663. + struct rcu_head delayed_drop;
  11664. +#endif
  11665. #ifdef CONFIG_X86_INTEL_MPX
  11666. /* address of the bounds directory */
  11667. void __user *bd_addr;
  11668. diff -Nur linux-4.4.28.orig/include/linux/mutex.h linux-4.4.28/include/linux/mutex.h
  11669. --- linux-4.4.28.orig/include/linux/mutex.h 2016-10-28 09:53:25.000000000 +0200
  11670. +++ linux-4.4.28/include/linux/mutex.h 2016-11-01 21:26:58.732339449 +0100
  11671. @@ -19,6 +19,17 @@
  11672. #include <asm/processor.h>
  11673. #include <linux/osq_lock.h>
  11674. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  11675. +# define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
  11676. + , .dep_map = { .name = #lockname }
  11677. +#else
  11678. +# define __DEP_MAP_MUTEX_INITIALIZER(lockname)
  11679. +#endif
  11680. +
  11681. +#ifdef CONFIG_PREEMPT_RT_FULL
  11682. +# include <linux/mutex_rt.h>
  11683. +#else
  11684. +
  11685. /*
  11686. * Simple, straightforward mutexes with strict semantics:
  11687. *
  11688. @@ -99,13 +110,6 @@
  11689. static inline void mutex_destroy(struct mutex *lock) {}
  11690. #endif
  11691. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  11692. -# define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
  11693. - , .dep_map = { .name = #lockname }
  11694. -#else
  11695. -# define __DEP_MAP_MUTEX_INITIALIZER(lockname)
  11696. -#endif
  11697. -
  11698. #define __MUTEX_INITIALIZER(lockname) \
  11699. { .count = ATOMIC_INIT(1) \
  11700. , .wait_lock = __SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
  11701. @@ -173,6 +177,8 @@
  11702. extern int mutex_trylock(struct mutex *lock);
  11703. extern void mutex_unlock(struct mutex *lock);
  11704. +#endif /* !PREEMPT_RT_FULL */
  11705. +
  11706. extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);
  11707. #endif /* __LINUX_MUTEX_H */
  11708. diff -Nur linux-4.4.28.orig/include/linux/mutex_rt.h linux-4.4.28/include/linux/mutex_rt.h
  11709. --- linux-4.4.28.orig/include/linux/mutex_rt.h 1970-01-01 01:00:00.000000000 +0100
  11710. +++ linux-4.4.28/include/linux/mutex_rt.h 2016-11-01 21:26:58.732339449 +0100
  11711. @@ -0,0 +1,84 @@
  11712. +#ifndef __LINUX_MUTEX_RT_H
  11713. +#define __LINUX_MUTEX_RT_H
  11714. +
  11715. +#ifndef __LINUX_MUTEX_H
  11716. +#error "Please include mutex.h"
  11717. +#endif
  11718. +
  11719. +#include <linux/rtmutex.h>
  11720. +
  11721. +/* FIXME: Just for __lockfunc */
  11722. +#include <linux/spinlock.h>
  11723. +
  11724. +struct mutex {
  11725. + struct rt_mutex lock;
  11726. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  11727. + struct lockdep_map dep_map;
  11728. +#endif
  11729. +};
  11730. +
  11731. +#define __MUTEX_INITIALIZER(mutexname) \
  11732. + { \
  11733. + .lock = __RT_MUTEX_INITIALIZER(mutexname.lock) \
  11734. + __DEP_MAP_MUTEX_INITIALIZER(mutexname) \
  11735. + }
  11736. +
  11737. +#define DEFINE_MUTEX(mutexname) \
  11738. + struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
  11739. +
  11740. +extern void __mutex_do_init(struct mutex *lock, const char *name, struct lock_class_key *key);
  11741. +extern void __lockfunc _mutex_lock(struct mutex *lock);
  11742. +extern int __lockfunc _mutex_lock_interruptible(struct mutex *lock);
  11743. +extern int __lockfunc _mutex_lock_killable(struct mutex *lock);
  11744. +extern void __lockfunc _mutex_lock_nested(struct mutex *lock, int subclass);
  11745. +extern void __lockfunc _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest_lock);
  11746. +extern int __lockfunc _mutex_lock_interruptible_nested(struct mutex *lock, int subclass);
  11747. +extern int __lockfunc _mutex_lock_killable_nested(struct mutex *lock, int subclass);
  11748. +extern int __lockfunc _mutex_trylock(struct mutex *lock);
  11749. +extern void __lockfunc _mutex_unlock(struct mutex *lock);
  11750. +
  11751. +#define mutex_is_locked(l) rt_mutex_is_locked(&(l)->lock)
  11752. +#define mutex_lock(l) _mutex_lock(l)
  11753. +#define mutex_lock_interruptible(l) _mutex_lock_interruptible(l)
  11754. +#define mutex_lock_killable(l) _mutex_lock_killable(l)
  11755. +#define mutex_trylock(l) _mutex_trylock(l)
  11756. +#define mutex_unlock(l) _mutex_unlock(l)
  11757. +#define mutex_destroy(l) rt_mutex_destroy(&(l)->lock)
  11758. +
  11759. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  11760. +# define mutex_lock_nested(l, s) _mutex_lock_nested(l, s)
  11761. +# define mutex_lock_interruptible_nested(l, s) \
  11762. + _mutex_lock_interruptible_nested(l, s)
  11763. +# define mutex_lock_killable_nested(l, s) \
  11764. + _mutex_lock_killable_nested(l, s)
  11765. +
  11766. +# define mutex_lock_nest_lock(lock, nest_lock) \
  11767. +do { \
  11768. + typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
  11769. + _mutex_lock_nest_lock(lock, &(nest_lock)->dep_map); \
  11770. +} while (0)
  11771. +
  11772. +#else
  11773. +# define mutex_lock_nested(l, s) _mutex_lock(l)
  11774. +# define mutex_lock_interruptible_nested(l, s) \
  11775. + _mutex_lock_interruptible(l)
  11776. +# define mutex_lock_killable_nested(l, s) \
  11777. + _mutex_lock_killable(l)
  11778. +# define mutex_lock_nest_lock(lock, nest_lock) mutex_lock(lock)
  11779. +#endif
  11780. +
  11781. +# define mutex_init(mutex) \
  11782. +do { \
  11783. + static struct lock_class_key __key; \
  11784. + \
  11785. + rt_mutex_init(&(mutex)->lock); \
  11786. + __mutex_do_init((mutex), #mutex, &__key); \
  11787. +} while (0)
  11788. +
  11789. +# define __mutex_init(mutex, name, key) \
  11790. +do { \
  11791. + rt_mutex_init(&(mutex)->lock); \
  11792. + __mutex_do_init((mutex), name, key); \
  11793. +} while (0)
  11794. +
  11795. +#endif
  11796. diff -Nur linux-4.4.28.orig/include/linux/netdevice.h linux-4.4.28/include/linux/netdevice.h
  11797. --- linux-4.4.28.orig/include/linux/netdevice.h 2016-10-28 09:53:25.000000000 +0200
  11798. +++ linux-4.4.28/include/linux/netdevice.h 2016-11-01 21:26:58.736339702 +0100
  11799. @@ -2248,11 +2248,20 @@
  11800. void synchronize_net(void);
  11801. int init_dummy_netdev(struct net_device *dev);
  11802. +#ifdef CONFIG_PREEMPT_RT_FULL
  11803. +static inline int dev_recursion_level(void)
  11804. +{
  11805. + return current->xmit_recursion;
  11806. +}
  11807. +
  11808. +#else
  11809. +
  11810. DECLARE_PER_CPU(int, xmit_recursion);
  11811. static inline int dev_recursion_level(void)
  11812. {
  11813. return this_cpu_read(xmit_recursion);
  11814. }
  11815. +#endif
  11816. struct net_device *dev_get_by_index(struct net *net, int ifindex);
  11817. struct net_device *__dev_get_by_index(struct net *net, int ifindex);
  11818. @@ -2563,6 +2572,7 @@
  11819. unsigned int dropped;
  11820. struct sk_buff_head input_pkt_queue;
  11821. struct napi_struct backlog;
  11822. + struct sk_buff_head tofree_queue;
  11823. };
  11824. diff -Nur linux-4.4.28.orig/include/linux/netfilter/x_tables.h linux-4.4.28/include/linux/netfilter/x_tables.h
  11825. --- linux-4.4.28.orig/include/linux/netfilter/x_tables.h 2016-10-28 09:53:25.000000000 +0200
  11826. +++ linux-4.4.28/include/linux/netfilter/x_tables.h 2016-11-01 21:26:58.736339702 +0100
  11827. @@ -4,6 +4,7 @@
  11828. #include <linux/netdevice.h>
  11829. #include <linux/static_key.h>
  11830. +#include <linux/locallock.h>
  11831. #include <uapi/linux/netfilter/x_tables.h>
  11832. /**
  11833. @@ -289,6 +290,8 @@
  11834. */
  11835. DECLARE_PER_CPU(seqcount_t, xt_recseq);
  11836. +DECLARE_LOCAL_IRQ_LOCK(xt_write_lock);
  11837. +
  11838. /* xt_tee_enabled - true if x_tables needs to handle reentrancy
  11839. *
  11840. * Enabled if current ip(6)tables ruleset has at least one -j TEE rule.
  11841. @@ -309,6 +312,9 @@
  11842. {
  11843. unsigned int addend;
  11844. + /* RT protection */
  11845. + local_lock(xt_write_lock);
  11846. +
  11847. /*
  11848. * Low order bit of sequence is set if we already
  11849. * called xt_write_recseq_begin().
  11850. @@ -339,6 +345,7 @@
  11851. /* this is kind of a write_seqcount_end(), but addend is 0 or 1 */
  11852. smp_wmb();
  11853. __this_cpu_add(xt_recseq.sequence, addend);
  11854. + local_unlock(xt_write_lock);
  11855. }
  11856. /*
  11857. diff -Nur linux-4.4.28.orig/include/linux/notifier.h linux-4.4.28/include/linux/notifier.h
  11858. --- linux-4.4.28.orig/include/linux/notifier.h 2016-10-28 09:53:25.000000000 +0200
  11859. +++ linux-4.4.28/include/linux/notifier.h 2016-11-01 21:26:58.736339702 +0100
  11860. @@ -6,7 +6,7 @@
  11861. *
  11862. * Alan Cox <Alan.Cox@linux.org>
  11863. */
  11864. -
  11865. +
  11866. #ifndef _LINUX_NOTIFIER_H
  11867. #define _LINUX_NOTIFIER_H
  11868. #include <linux/errno.h>
  11869. @@ -42,9 +42,7 @@
  11870. * in srcu_notifier_call_chain(): no cache bounces and no memory barriers.
  11871. * As compensation, srcu_notifier_chain_unregister() is rather expensive.
  11872. * SRCU notifier chains should be used when the chain will be called very
  11873. - * often but notifier_blocks will seldom be removed. Also, SRCU notifier
  11874. - * chains are slightly more difficult to use because they require special
  11875. - * runtime initialization.
  11876. + * often but notifier_blocks will seldom be removed.
  11877. */
  11878. typedef int (*notifier_fn_t)(struct notifier_block *nb,
  11879. @@ -88,7 +86,7 @@
  11880. (name)->head = NULL; \
  11881. } while (0)
  11882. -/* srcu_notifier_heads must be initialized and cleaned up dynamically */
  11883. +/* srcu_notifier_heads must be cleaned up dynamically */
  11884. extern void srcu_init_notifier_head(struct srcu_notifier_head *nh);
  11885. #define srcu_cleanup_notifier_head(name) \
  11886. cleanup_srcu_struct(&(name)->srcu);
  11887. @@ -101,7 +99,13 @@
  11888. .head = NULL }
  11889. #define RAW_NOTIFIER_INIT(name) { \
  11890. .head = NULL }
  11891. -/* srcu_notifier_heads cannot be initialized statically */
  11892. +
  11893. +#define SRCU_NOTIFIER_INIT(name, pcpu) \
  11894. + { \
  11895. + .mutex = __MUTEX_INITIALIZER(name.mutex), \
  11896. + .head = NULL, \
  11897. + .srcu = __SRCU_STRUCT_INIT(name.srcu, pcpu), \
  11898. + }
  11899. #define ATOMIC_NOTIFIER_HEAD(name) \
  11900. struct atomic_notifier_head name = \
  11901. @@ -113,6 +117,18 @@
  11902. struct raw_notifier_head name = \
  11903. RAW_NOTIFIER_INIT(name)
  11904. +#define _SRCU_NOTIFIER_HEAD(name, mod) \
  11905. + static DEFINE_PER_CPU(struct srcu_struct_array, \
  11906. + name##_head_srcu_array); \
  11907. + mod struct srcu_notifier_head name = \
  11908. + SRCU_NOTIFIER_INIT(name, name##_head_srcu_array)
  11909. +
  11910. +#define SRCU_NOTIFIER_HEAD(name) \
  11911. + _SRCU_NOTIFIER_HEAD(name, )
  11912. +
  11913. +#define SRCU_NOTIFIER_HEAD_STATIC(name) \
  11914. + _SRCU_NOTIFIER_HEAD(name, static)
  11915. +
  11916. #ifdef __KERNEL__
  11917. extern int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
  11918. @@ -182,12 +198,12 @@
  11919. /*
  11920. * Declared notifiers so far. I can imagine quite a few more chains
  11921. - * over time (eg laptop power reset chains, reboot chain (to clean
  11922. + * over time (eg laptop power reset chains, reboot chain (to clean
  11923. * device units up), device [un]mount chain, module load/unload chain,
  11924. - * low memory chain, screenblank chain (for plug in modular screenblankers)
  11925. + * low memory chain, screenblank chain (for plug in modular screenblankers)
  11926. * VC switch chains (for loadable kernel svgalib VC switch helpers) etc...
  11927. */
  11928. -
  11929. +
  11930. /* CPU notfiers are defined in include/linux/cpu.h. */
  11931. /* netdevice notifiers are defined in include/linux/netdevice.h */
  11932. diff -Nur linux-4.4.28.orig/include/linux/percpu.h linux-4.4.28/include/linux/percpu.h
  11933. --- linux-4.4.28.orig/include/linux/percpu.h 2016-10-28 09:53:25.000000000 +0200
  11934. +++ linux-4.4.28/include/linux/percpu.h 2016-11-01 21:26:58.736339702 +0100
  11935. @@ -24,6 +24,35 @@
  11936. PERCPU_MODULE_RESERVE)
  11937. #endif
  11938. +#ifdef CONFIG_PREEMPT_RT_FULL
  11939. +
  11940. +#define get_local_var(var) (*({ \
  11941. + migrate_disable(); \
  11942. + this_cpu_ptr(&var); }))
  11943. +
  11944. +#define put_local_var(var) do { \
  11945. + (void)&(var); \
  11946. + migrate_enable(); \
  11947. +} while (0)
  11948. +
  11949. +# define get_local_ptr(var) ({ \
  11950. + migrate_disable(); \
  11951. + this_cpu_ptr(var); })
  11952. +
  11953. +# define put_local_ptr(var) do { \
  11954. + (void)(var); \
  11955. + migrate_enable(); \
  11956. +} while (0)
  11957. +
  11958. +#else
  11959. +
  11960. +#define get_local_var(var) get_cpu_var(var)
  11961. +#define put_local_var(var) put_cpu_var(var)
  11962. +#define get_local_ptr(var) get_cpu_ptr(var)
  11963. +#define put_local_ptr(var) put_cpu_ptr(var)
  11964. +
  11965. +#endif
  11966. +
  11967. /* minimum unit size, also is the maximum supported allocation size */
  11968. #define PCPU_MIN_UNIT_SIZE PFN_ALIGN(32 << 10)
  11969. diff -Nur linux-4.4.28.orig/include/linux/pid.h linux-4.4.28/include/linux/pid.h
  11970. --- linux-4.4.28.orig/include/linux/pid.h 2016-10-28 09:53:25.000000000 +0200
  11971. +++ linux-4.4.28/include/linux/pid.h 2016-11-01 21:26:58.736339702 +0100
  11972. @@ -2,6 +2,7 @@
  11973. #define _LINUX_PID_H
  11974. #include <linux/rcupdate.h>
  11975. +#include <linux/atomic.h>
  11976. enum pid_type
  11977. {
  11978. diff -Nur linux-4.4.28.orig/include/linux/preempt.h linux-4.4.28/include/linux/preempt.h
  11979. --- linux-4.4.28.orig/include/linux/preempt.h 2016-10-28 09:53:25.000000000 +0200
  11980. +++ linux-4.4.28/include/linux/preempt.h 2016-11-01 21:26:58.736339702 +0100
  11981. @@ -50,7 +50,11 @@
  11982. #define HARDIRQ_OFFSET (1UL << HARDIRQ_SHIFT)
  11983. #define NMI_OFFSET (1UL << NMI_SHIFT)
  11984. -#define SOFTIRQ_DISABLE_OFFSET (2 * SOFTIRQ_OFFSET)
  11985. +#ifndef CONFIG_PREEMPT_RT_FULL
  11986. +# define SOFTIRQ_DISABLE_OFFSET (2 * SOFTIRQ_OFFSET)
  11987. +#else
  11988. +# define SOFTIRQ_DISABLE_OFFSET (0)
  11989. +#endif
  11990. /* We use the MSB mostly because its available */
  11991. #define PREEMPT_NEED_RESCHED 0x80000000
  11992. @@ -59,9 +63,15 @@
  11993. #include <asm/preempt.h>
  11994. #define hardirq_count() (preempt_count() & HARDIRQ_MASK)
  11995. -#define softirq_count() (preempt_count() & SOFTIRQ_MASK)
  11996. #define irq_count() (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_MASK \
  11997. | NMI_MASK))
  11998. +#ifndef CONFIG_PREEMPT_RT_FULL
  11999. +# define softirq_count() (preempt_count() & SOFTIRQ_MASK)
  12000. +# define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET)
  12001. +#else
  12002. +# define softirq_count() (0UL)
  12003. +extern int in_serving_softirq(void);
  12004. +#endif
  12005. /*
  12006. * Are we doing bottom half or hardware interrupt processing?
  12007. @@ -72,7 +82,6 @@
  12008. #define in_irq() (hardirq_count())
  12009. #define in_softirq() (softirq_count())
  12010. #define in_interrupt() (irq_count())
  12011. -#define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET)
  12012. /*
  12013. * Are we in NMI context?
  12014. @@ -91,7 +100,11 @@
  12015. /*
  12016. * The preempt_count offset after spin_lock()
  12017. */
  12018. +#if !defined(CONFIG_PREEMPT_RT_FULL)
  12019. #define PREEMPT_LOCK_OFFSET PREEMPT_DISABLE_OFFSET
  12020. +#else
  12021. +#define PREEMPT_LOCK_OFFSET 0
  12022. +#endif
  12023. /*
  12024. * The preempt_count offset needed for things like:
  12025. @@ -140,6 +153,20 @@
  12026. #define preempt_count_inc() preempt_count_add(1)
  12027. #define preempt_count_dec() preempt_count_sub(1)
  12028. +#ifdef CONFIG_PREEMPT_LAZY
  12029. +#define add_preempt_lazy_count(val) do { preempt_lazy_count() += (val); } while (0)
  12030. +#define sub_preempt_lazy_count(val) do { preempt_lazy_count() -= (val); } while (0)
  12031. +#define inc_preempt_lazy_count() add_preempt_lazy_count(1)
  12032. +#define dec_preempt_lazy_count() sub_preempt_lazy_count(1)
  12033. +#define preempt_lazy_count() (current_thread_info()->preempt_lazy_count)
  12034. +#else
  12035. +#define add_preempt_lazy_count(val) do { } while (0)
  12036. +#define sub_preempt_lazy_count(val) do { } while (0)
  12037. +#define inc_preempt_lazy_count() do { } while (0)
  12038. +#define dec_preempt_lazy_count() do { } while (0)
  12039. +#define preempt_lazy_count() (0)
  12040. +#endif
  12041. +
  12042. #ifdef CONFIG_PREEMPT_COUNT
  12043. #define preempt_disable() \
  12044. @@ -148,13 +175,25 @@
  12045. barrier(); \
  12046. } while (0)
  12047. +#define preempt_lazy_disable() \
  12048. +do { \
  12049. + inc_preempt_lazy_count(); \
  12050. + barrier(); \
  12051. +} while (0)
  12052. +
  12053. #define sched_preempt_enable_no_resched() \
  12054. do { \
  12055. barrier(); \
  12056. preempt_count_dec(); \
  12057. } while (0)
  12058. -#define preempt_enable_no_resched() sched_preempt_enable_no_resched()
  12059. +#ifdef CONFIG_PREEMPT_RT_BASE
  12060. +# define preempt_enable_no_resched() sched_preempt_enable_no_resched()
  12061. +# define preempt_check_resched_rt() preempt_check_resched()
  12062. +#else
  12063. +# define preempt_enable_no_resched() preempt_enable()
  12064. +# define preempt_check_resched_rt() barrier();
  12065. +#endif
  12066. #define preemptible() (preempt_count() == 0 && !irqs_disabled())
  12067. @@ -179,6 +218,13 @@
  12068. __preempt_schedule(); \
  12069. } while (0)
  12070. +#define preempt_lazy_enable() \
  12071. +do { \
  12072. + dec_preempt_lazy_count(); \
  12073. + barrier(); \
  12074. + preempt_check_resched(); \
  12075. +} while (0)
  12076. +
  12077. #else /* !CONFIG_PREEMPT */
  12078. #define preempt_enable() \
  12079. do { \
  12080. @@ -224,6 +270,7 @@
  12081. #define preempt_disable_notrace() barrier()
  12082. #define preempt_enable_no_resched_notrace() barrier()
  12083. #define preempt_enable_notrace() barrier()
  12084. +#define preempt_check_resched_rt() barrier()
  12085. #define preemptible() 0
  12086. #endif /* CONFIG_PREEMPT_COUNT */
  12087. @@ -244,10 +291,31 @@
  12088. } while (0)
  12089. #define preempt_fold_need_resched() \
  12090. do { \
  12091. - if (tif_need_resched()) \
  12092. + if (tif_need_resched_now()) \
  12093. set_preempt_need_resched(); \
  12094. } while (0)
  12095. +#ifdef CONFIG_PREEMPT_RT_FULL
  12096. +# define preempt_disable_rt() preempt_disable()
  12097. +# define preempt_enable_rt() preempt_enable()
  12098. +# define preempt_disable_nort() barrier()
  12099. +# define preempt_enable_nort() barrier()
  12100. +# ifdef CONFIG_SMP
  12101. + extern void migrate_disable(void);
  12102. + extern void migrate_enable(void);
  12103. +# else /* CONFIG_SMP */
  12104. +# define migrate_disable() barrier()
  12105. +# define migrate_enable() barrier()
  12106. +# endif /* CONFIG_SMP */
  12107. +#else
  12108. +# define preempt_disable_rt() barrier()
  12109. +# define preempt_enable_rt() barrier()
  12110. +# define preempt_disable_nort() preempt_disable()
  12111. +# define preempt_enable_nort() preempt_enable()
  12112. +# define migrate_disable() preempt_disable()
  12113. +# define migrate_enable() preempt_enable()
  12114. +#endif
  12115. +
  12116. #ifdef CONFIG_PREEMPT_NOTIFIERS
  12117. struct preempt_notifier;
  12118. diff -Nur linux-4.4.28.orig/include/linux/printk.h linux-4.4.28/include/linux/printk.h
  12119. --- linux-4.4.28.orig/include/linux/printk.h 2016-10-28 09:53:25.000000000 +0200
  12120. +++ linux-4.4.28/include/linux/printk.h 2016-11-01 21:26:58.736339702 +0100
  12121. @@ -117,9 +117,11 @@
  12122. #ifdef CONFIG_EARLY_PRINTK
  12123. extern asmlinkage __printf(1, 2)
  12124. void early_printk(const char *fmt, ...);
  12125. +extern void printk_kill(void);
  12126. #else
  12127. static inline __printf(1, 2) __cold
  12128. void early_printk(const char *s, ...) { }
  12129. +static inline void printk_kill(void) { }
  12130. #endif
  12131. typedef __printf(1, 0) int (*printk_func_t)(const char *fmt, va_list args);
  12132. diff -Nur linux-4.4.28.orig/include/linux/radix-tree.h linux-4.4.28/include/linux/radix-tree.h
  12133. --- linux-4.4.28.orig/include/linux/radix-tree.h 2016-10-28 09:53:25.000000000 +0200
  12134. +++ linux-4.4.28/include/linux/radix-tree.h 2016-11-01 21:26:58.736339702 +0100
  12135. @@ -277,8 +277,13 @@
  12136. unsigned int radix_tree_gang_lookup_slot(struct radix_tree_root *root,
  12137. void ***results, unsigned long *indices,
  12138. unsigned long first_index, unsigned int max_items);
  12139. +#ifndef CONFIG_PREEMPT_RT_FULL
  12140. int radix_tree_preload(gfp_t gfp_mask);
  12141. int radix_tree_maybe_preload(gfp_t gfp_mask);
  12142. +#else
  12143. +static inline int radix_tree_preload(gfp_t gm) { return 0; }
  12144. +static inline int radix_tree_maybe_preload(gfp_t gfp_mask) { return 0; }
  12145. +#endif
  12146. void radix_tree_init(void);
  12147. void *radix_tree_tag_set(struct radix_tree_root *root,
  12148. unsigned long index, unsigned int tag);
  12149. @@ -303,7 +308,7 @@
  12150. static inline void radix_tree_preload_end(void)
  12151. {
  12152. - preempt_enable();
  12153. + preempt_enable_nort();
  12154. }
  12155. /**
  12156. diff -Nur linux-4.4.28.orig/include/linux/random.h linux-4.4.28/include/linux/random.h
  12157. --- linux-4.4.28.orig/include/linux/random.h 2016-10-28 09:53:25.000000000 +0200
  12158. +++ linux-4.4.28/include/linux/random.h 2016-11-01 21:26:58.736339702 +0100
  12159. @@ -20,7 +20,7 @@
  12160. extern void add_device_randomness(const void *, unsigned int);
  12161. extern void add_input_randomness(unsigned int type, unsigned int code,
  12162. unsigned int value);
  12163. -extern void add_interrupt_randomness(int irq, int irq_flags);
  12164. +extern void add_interrupt_randomness(int irq, int irq_flags, __u64 ip);
  12165. extern void get_random_bytes(void *buf, int nbytes);
  12166. extern int add_random_ready_callback(struct random_ready_callback *rdy);
  12167. diff -Nur linux-4.4.28.orig/include/linux/rbtree.h linux-4.4.28/include/linux/rbtree.h
  12168. --- linux-4.4.28.orig/include/linux/rbtree.h 2016-10-28 09:53:25.000000000 +0200
  12169. +++ linux-4.4.28/include/linux/rbtree.h 2016-11-01 21:26:58.736339702 +0100
  12170. @@ -31,7 +31,6 @@
  12171. #include <linux/kernel.h>
  12172. #include <linux/stddef.h>
  12173. -#include <linux/rcupdate.h>
  12174. struct rb_node {
  12175. unsigned long __rb_parent_color;
  12176. @@ -86,14 +85,8 @@
  12177. *rb_link = node;
  12178. }
  12179. -static inline void rb_link_node_rcu(struct rb_node *node, struct rb_node *parent,
  12180. - struct rb_node **rb_link)
  12181. -{
  12182. - node->__rb_parent_color = (unsigned long)parent;
  12183. - node->rb_left = node->rb_right = NULL;
  12184. -
  12185. - rcu_assign_pointer(*rb_link, node);
  12186. -}
  12187. +void rb_link_node_rcu(struct rb_node *node, struct rb_node *parent,
  12188. + struct rb_node **rb_link);
  12189. #define rb_entry_safe(ptr, type, member) \
  12190. ({ typeof(ptr) ____ptr = (ptr); \
  12191. diff -Nur linux-4.4.28.orig/include/linux/rcupdate.h linux-4.4.28/include/linux/rcupdate.h
  12192. --- linux-4.4.28.orig/include/linux/rcupdate.h 2016-10-28 09:53:25.000000000 +0200
  12193. +++ linux-4.4.28/include/linux/rcupdate.h 2016-11-01 21:26:58.740339958 +0100
  12194. @@ -169,6 +169,9 @@
  12195. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  12196. +#ifdef CONFIG_PREEMPT_RT_FULL
  12197. +#define call_rcu_bh call_rcu
  12198. +#else
  12199. /**
  12200. * call_rcu_bh() - Queue an RCU for invocation after a quicker grace period.
  12201. * @head: structure to be used for queueing the RCU updates.
  12202. @@ -192,6 +195,7 @@
  12203. */
  12204. void call_rcu_bh(struct rcu_head *head,
  12205. rcu_callback_t func);
  12206. +#endif
  12207. /**
  12208. * call_rcu_sched() - Queue an RCU for invocation after sched grace period.
  12209. @@ -292,6 +296,11 @@
  12210. * types of kernel builds, the rcu_read_lock() nesting depth is unknowable.
  12211. */
  12212. #define rcu_preempt_depth() (current->rcu_read_lock_nesting)
  12213. +#ifndef CONFIG_PREEMPT_RT_FULL
  12214. +#define sched_rcu_preempt_depth() rcu_preempt_depth()
  12215. +#else
  12216. +static inline int sched_rcu_preempt_depth(void) { return 0; }
  12217. +#endif
  12218. #else /* #ifdef CONFIG_PREEMPT_RCU */
  12219. @@ -317,6 +326,8 @@
  12220. return 0;
  12221. }
  12222. +#define sched_rcu_preempt_depth() rcu_preempt_depth()
  12223. +
  12224. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  12225. /* Internal to kernel */
  12226. @@ -489,7 +500,14 @@
  12227. int debug_lockdep_rcu_enabled(void);
  12228. int rcu_read_lock_held(void);
  12229. +#ifdef CONFIG_PREEMPT_RT_FULL
  12230. +static inline int rcu_read_lock_bh_held(void)
  12231. +{
  12232. + return rcu_read_lock_held();
  12233. +}
  12234. +#else
  12235. int rcu_read_lock_bh_held(void);
  12236. +#endif
  12237. /**
  12238. * rcu_read_lock_sched_held() - might we be in RCU-sched read-side critical section?
  12239. @@ -937,10 +955,14 @@
  12240. static inline void rcu_read_lock_bh(void)
  12241. {
  12242. local_bh_disable();
  12243. +#ifdef CONFIG_PREEMPT_RT_FULL
  12244. + rcu_read_lock();
  12245. +#else
  12246. __acquire(RCU_BH);
  12247. rcu_lock_acquire(&rcu_bh_lock_map);
  12248. RCU_LOCKDEP_WARN(!rcu_is_watching(),
  12249. "rcu_read_lock_bh() used illegally while idle");
  12250. +#endif
  12251. }
  12252. /*
  12253. @@ -950,10 +972,14 @@
  12254. */
  12255. static inline void rcu_read_unlock_bh(void)
  12256. {
  12257. +#ifdef CONFIG_PREEMPT_RT_FULL
  12258. + rcu_read_unlock();
  12259. +#else
  12260. RCU_LOCKDEP_WARN(!rcu_is_watching(),
  12261. "rcu_read_unlock_bh() used illegally while idle");
  12262. rcu_lock_release(&rcu_bh_lock_map);
  12263. __release(RCU_BH);
  12264. +#endif
  12265. local_bh_enable();
  12266. }
  12267. diff -Nur linux-4.4.28.orig/include/linux/rcutree.h linux-4.4.28/include/linux/rcutree.h
  12268. --- linux-4.4.28.orig/include/linux/rcutree.h 2016-10-28 09:53:25.000000000 +0200
  12269. +++ linux-4.4.28/include/linux/rcutree.h 2016-11-01 21:26:58.740339958 +0100
  12270. @@ -44,7 +44,11 @@
  12271. rcu_note_context_switch();
  12272. }
  12273. +#ifdef CONFIG_PREEMPT_RT_FULL
  12274. +# define synchronize_rcu_bh synchronize_rcu
  12275. +#else
  12276. void synchronize_rcu_bh(void);
  12277. +#endif
  12278. void synchronize_sched_expedited(void);
  12279. void synchronize_rcu_expedited(void);
  12280. @@ -72,7 +76,11 @@
  12281. }
  12282. void rcu_barrier(void);
  12283. +#ifdef CONFIG_PREEMPT_RT_FULL
  12284. +# define rcu_barrier_bh rcu_barrier
  12285. +#else
  12286. void rcu_barrier_bh(void);
  12287. +#endif
  12288. void rcu_barrier_sched(void);
  12289. unsigned long get_state_synchronize_rcu(void);
  12290. void cond_synchronize_rcu(unsigned long oldstate);
  12291. @@ -85,12 +93,10 @@
  12292. unsigned long rcu_batches_started_bh(void);
  12293. unsigned long rcu_batches_started_sched(void);
  12294. unsigned long rcu_batches_completed(void);
  12295. -unsigned long rcu_batches_completed_bh(void);
  12296. unsigned long rcu_batches_completed_sched(void);
  12297. void show_rcu_gp_kthreads(void);
  12298. void rcu_force_quiescent_state(void);
  12299. -void rcu_bh_force_quiescent_state(void);
  12300. void rcu_sched_force_quiescent_state(void);
  12301. void rcu_idle_enter(void);
  12302. @@ -105,6 +111,14 @@
  12303. bool rcu_is_watching(void);
  12304. +#ifndef CONFIG_PREEMPT_RT_FULL
  12305. +void rcu_bh_force_quiescent_state(void);
  12306. +unsigned long rcu_batches_completed_bh(void);
  12307. +#else
  12308. +# define rcu_bh_force_quiescent_state rcu_force_quiescent_state
  12309. +# define rcu_batches_completed_bh rcu_batches_completed
  12310. +#endif
  12311. +
  12312. void rcu_all_qs(void);
  12313. #endif /* __LINUX_RCUTREE_H */
  12314. diff -Nur linux-4.4.28.orig/include/linux/rtmutex.h linux-4.4.28/include/linux/rtmutex.h
  12315. --- linux-4.4.28.orig/include/linux/rtmutex.h 2016-10-28 09:53:25.000000000 +0200
  12316. +++ linux-4.4.28/include/linux/rtmutex.h 2016-11-01 21:26:58.740339958 +0100
  12317. @@ -13,11 +13,15 @@
  12318. #define __LINUX_RT_MUTEX_H
  12319. #include <linux/linkage.h>
  12320. +#include <linux/spinlock_types_raw.h>
  12321. #include <linux/rbtree.h>
  12322. -#include <linux/spinlock_types.h>
  12323. extern int max_lock_depth; /* for sysctl */
  12324. +#ifdef CONFIG_DEBUG_MUTEXES
  12325. +#include <linux/debug_locks.h>
  12326. +#endif
  12327. +
  12328. /**
  12329. * The rt_mutex structure
  12330. *
  12331. @@ -31,8 +35,8 @@
  12332. struct rb_root waiters;
  12333. struct rb_node *waiters_leftmost;
  12334. struct task_struct *owner;
  12335. -#ifdef CONFIG_DEBUG_RT_MUTEXES
  12336. int save_state;
  12337. +#ifdef CONFIG_DEBUG_RT_MUTEXES
  12338. const char *name, *file;
  12339. int line;
  12340. void *magic;
  12341. @@ -55,22 +59,33 @@
  12342. # define rt_mutex_debug_check_no_locks_held(task) do { } while (0)
  12343. #endif
  12344. +# define rt_mutex_init(mutex) \
  12345. + do { \
  12346. + raw_spin_lock_init(&(mutex)->wait_lock); \
  12347. + __rt_mutex_init(mutex, #mutex); \
  12348. + } while (0)
  12349. +
  12350. #ifdef CONFIG_DEBUG_RT_MUTEXES
  12351. # define __DEBUG_RT_MUTEX_INITIALIZER(mutexname) \
  12352. , .name = #mutexname, .file = __FILE__, .line = __LINE__
  12353. -# define rt_mutex_init(mutex) __rt_mutex_init(mutex, __func__)
  12354. extern void rt_mutex_debug_task_free(struct task_struct *tsk);
  12355. #else
  12356. # define __DEBUG_RT_MUTEX_INITIALIZER(mutexname)
  12357. -# define rt_mutex_init(mutex) __rt_mutex_init(mutex, NULL)
  12358. # define rt_mutex_debug_task_free(t) do { } while (0)
  12359. #endif
  12360. -#define __RT_MUTEX_INITIALIZER(mutexname) \
  12361. - { .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(mutexname.wait_lock) \
  12362. +#define __RT_MUTEX_INITIALIZER_PLAIN(mutexname) \
  12363. + .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(mutexname.wait_lock) \
  12364. , .waiters = RB_ROOT \
  12365. , .owner = NULL \
  12366. - __DEBUG_RT_MUTEX_INITIALIZER(mutexname)}
  12367. + __DEBUG_RT_MUTEX_INITIALIZER(mutexname)
  12368. +
  12369. +#define __RT_MUTEX_INITIALIZER(mutexname) \
  12370. + { __RT_MUTEX_INITIALIZER_PLAIN(mutexname) }
  12371. +
  12372. +#define __RT_MUTEX_INITIALIZER_SAVE_STATE(mutexname) \
  12373. + { __RT_MUTEX_INITIALIZER_PLAIN(mutexname) \
  12374. + , .save_state = 1 }
  12375. #define DEFINE_RT_MUTEX(mutexname) \
  12376. struct rt_mutex mutexname = __RT_MUTEX_INITIALIZER(mutexname)
  12377. @@ -91,6 +106,7 @@
  12378. extern void rt_mutex_lock(struct rt_mutex *lock);
  12379. extern int rt_mutex_lock_interruptible(struct rt_mutex *lock);
  12380. +extern int rt_mutex_lock_killable(struct rt_mutex *lock);
  12381. extern int rt_mutex_timed_lock(struct rt_mutex *lock,
  12382. struct hrtimer_sleeper *timeout);
  12383. diff -Nur linux-4.4.28.orig/include/linux/rwlock_rt.h linux-4.4.28/include/linux/rwlock_rt.h
  12384. --- linux-4.4.28.orig/include/linux/rwlock_rt.h 1970-01-01 01:00:00.000000000 +0100
  12385. +++ linux-4.4.28/include/linux/rwlock_rt.h 2016-11-01 21:26:58.740339958 +0100
  12386. @@ -0,0 +1,99 @@
  12387. +#ifndef __LINUX_RWLOCK_RT_H
  12388. +#define __LINUX_RWLOCK_RT_H
  12389. +
  12390. +#ifndef __LINUX_SPINLOCK_H
  12391. +#error Do not include directly. Use spinlock.h
  12392. +#endif
  12393. +
  12394. +#define rwlock_init(rwl) \
  12395. +do { \
  12396. + static struct lock_class_key __key; \
  12397. + \
  12398. + rt_mutex_init(&(rwl)->lock); \
  12399. + __rt_rwlock_init(rwl, #rwl, &__key); \
  12400. +} while (0)
  12401. +
  12402. +extern void __lockfunc rt_write_lock(rwlock_t *rwlock);
  12403. +extern void __lockfunc rt_read_lock(rwlock_t *rwlock);
  12404. +extern int __lockfunc rt_write_trylock(rwlock_t *rwlock);
  12405. +extern int __lockfunc rt_write_trylock_irqsave(rwlock_t *trylock, unsigned long *flags);
  12406. +extern int __lockfunc rt_read_trylock(rwlock_t *rwlock);
  12407. +extern void __lockfunc rt_write_unlock(rwlock_t *rwlock);
  12408. +extern void __lockfunc rt_read_unlock(rwlock_t *rwlock);
  12409. +extern unsigned long __lockfunc rt_write_lock_irqsave(rwlock_t *rwlock);
  12410. +extern unsigned long __lockfunc rt_read_lock_irqsave(rwlock_t *rwlock);
  12411. +extern void __rt_rwlock_init(rwlock_t *rwlock, char *name, struct lock_class_key *key);
  12412. +
  12413. +#define read_trylock(lock) __cond_lock(lock, rt_read_trylock(lock))
  12414. +#define write_trylock(lock) __cond_lock(lock, rt_write_trylock(lock))
  12415. +
  12416. +#define write_trylock_irqsave(lock, flags) \
  12417. + __cond_lock(lock, rt_write_trylock_irqsave(lock, &flags))
  12418. +
  12419. +#define read_lock_irqsave(lock, flags) \
  12420. + do { \
  12421. + typecheck(unsigned long, flags); \
  12422. + flags = rt_read_lock_irqsave(lock); \
  12423. + } while (0)
  12424. +
  12425. +#define write_lock_irqsave(lock, flags) \
  12426. + do { \
  12427. + typecheck(unsigned long, flags); \
  12428. + flags = rt_write_lock_irqsave(lock); \
  12429. + } while (0)
  12430. +
  12431. +#define read_lock(lock) rt_read_lock(lock)
  12432. +
  12433. +#define read_lock_bh(lock) \
  12434. + do { \
  12435. + local_bh_disable(); \
  12436. + rt_read_lock(lock); \
  12437. + } while (0)
  12438. +
  12439. +#define read_lock_irq(lock) read_lock(lock)
  12440. +
  12441. +#define write_lock(lock) rt_write_lock(lock)
  12442. +
  12443. +#define write_lock_bh(lock) \
  12444. + do { \
  12445. + local_bh_disable(); \
  12446. + rt_write_lock(lock); \
  12447. + } while (0)
  12448. +
  12449. +#define write_lock_irq(lock) write_lock(lock)
  12450. +
  12451. +#define read_unlock(lock) rt_read_unlock(lock)
  12452. +
  12453. +#define read_unlock_bh(lock) \
  12454. + do { \
  12455. + rt_read_unlock(lock); \
  12456. + local_bh_enable(); \
  12457. + } while (0)
  12458. +
  12459. +#define read_unlock_irq(lock) read_unlock(lock)
  12460. +
  12461. +#define write_unlock(lock) rt_write_unlock(lock)
  12462. +
  12463. +#define write_unlock_bh(lock) \
  12464. + do { \
  12465. + rt_write_unlock(lock); \
  12466. + local_bh_enable(); \
  12467. + } while (0)
  12468. +
  12469. +#define write_unlock_irq(lock) write_unlock(lock)
  12470. +
  12471. +#define read_unlock_irqrestore(lock, flags) \
  12472. + do { \
  12473. + typecheck(unsigned long, flags); \
  12474. + (void) flags; \
  12475. + rt_read_unlock(lock); \
  12476. + } while (0)
  12477. +
  12478. +#define write_unlock_irqrestore(lock, flags) \
  12479. + do { \
  12480. + typecheck(unsigned long, flags); \
  12481. + (void) flags; \
  12482. + rt_write_unlock(lock); \
  12483. + } while (0)
  12484. +
  12485. +#endif
  12486. diff -Nur linux-4.4.28.orig/include/linux/rwlock_types.h linux-4.4.28/include/linux/rwlock_types.h
  12487. --- linux-4.4.28.orig/include/linux/rwlock_types.h 2016-10-28 09:53:25.000000000 +0200
  12488. +++ linux-4.4.28/include/linux/rwlock_types.h 2016-11-01 21:26:58.740339958 +0100
  12489. @@ -1,6 +1,10 @@
  12490. #ifndef __LINUX_RWLOCK_TYPES_H
  12491. #define __LINUX_RWLOCK_TYPES_H
  12492. +#if !defined(__LINUX_SPINLOCK_TYPES_H)
  12493. +# error "Do not include directly, include spinlock_types.h"
  12494. +#endif
  12495. +
  12496. /*
  12497. * include/linux/rwlock_types.h - generic rwlock type definitions
  12498. * and initializers
  12499. @@ -43,6 +47,7 @@
  12500. RW_DEP_MAP_INIT(lockname) }
  12501. #endif
  12502. -#define DEFINE_RWLOCK(x) rwlock_t x = __RW_LOCK_UNLOCKED(x)
  12503. +#define DEFINE_RWLOCK(name) \
  12504. + rwlock_t name __cacheline_aligned_in_smp = __RW_LOCK_UNLOCKED(name)
  12505. #endif /* __LINUX_RWLOCK_TYPES_H */
  12506. diff -Nur linux-4.4.28.orig/include/linux/rwlock_types_rt.h linux-4.4.28/include/linux/rwlock_types_rt.h
  12507. --- linux-4.4.28.orig/include/linux/rwlock_types_rt.h 1970-01-01 01:00:00.000000000 +0100
  12508. +++ linux-4.4.28/include/linux/rwlock_types_rt.h 2016-11-01 21:26:58.740339958 +0100
  12509. @@ -0,0 +1,33 @@
  12510. +#ifndef __LINUX_RWLOCK_TYPES_RT_H
  12511. +#define __LINUX_RWLOCK_TYPES_RT_H
  12512. +
  12513. +#ifndef __LINUX_SPINLOCK_TYPES_H
  12514. +#error "Do not include directly. Include spinlock_types.h instead"
  12515. +#endif
  12516. +
  12517. +/*
  12518. + * rwlocks - rtmutex which allows single reader recursion
  12519. + */
  12520. +typedef struct {
  12521. + struct rt_mutex lock;
  12522. + int read_depth;
  12523. + unsigned int break_lock;
  12524. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12525. + struct lockdep_map dep_map;
  12526. +#endif
  12527. +} rwlock_t;
  12528. +
  12529. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12530. +# define RW_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }
  12531. +#else
  12532. +# define RW_DEP_MAP_INIT(lockname)
  12533. +#endif
  12534. +
  12535. +#define __RW_LOCK_UNLOCKED(name) \
  12536. + { .lock = __RT_MUTEX_INITIALIZER_SAVE_STATE(name.lock), \
  12537. + RW_DEP_MAP_INIT(name) }
  12538. +
  12539. +#define DEFINE_RWLOCK(name) \
  12540. + rwlock_t name __cacheline_aligned_in_smp = __RW_LOCK_UNLOCKED(name)
  12541. +
  12542. +#endif
  12543. diff -Nur linux-4.4.28.orig/include/linux/rwsem.h linux-4.4.28/include/linux/rwsem.h
  12544. --- linux-4.4.28.orig/include/linux/rwsem.h 2016-10-28 09:53:25.000000000 +0200
  12545. +++ linux-4.4.28/include/linux/rwsem.h 2016-11-01 21:26:58.740339958 +0100
  12546. @@ -18,6 +18,10 @@
  12547. #include <linux/osq_lock.h>
  12548. #endif
  12549. +#ifdef CONFIG_PREEMPT_RT_FULL
  12550. +#include <linux/rwsem_rt.h>
  12551. +#else /* PREEMPT_RT_FULL */
  12552. +
  12553. struct rw_semaphore;
  12554. #ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
  12555. @@ -177,4 +181,6 @@
  12556. # define up_read_non_owner(sem) up_read(sem)
  12557. #endif
  12558. +#endif /* !PREEMPT_RT_FULL */
  12559. +
  12560. #endif /* _LINUX_RWSEM_H */
  12561. diff -Nur linux-4.4.28.orig/include/linux/rwsem_rt.h linux-4.4.28/include/linux/rwsem_rt.h
  12562. --- linux-4.4.28.orig/include/linux/rwsem_rt.h 1970-01-01 01:00:00.000000000 +0100
  12563. +++ linux-4.4.28/include/linux/rwsem_rt.h 2016-11-01 21:26:58.740339958 +0100
  12564. @@ -0,0 +1,152 @@
  12565. +#ifndef _LINUX_RWSEM_RT_H
  12566. +#define _LINUX_RWSEM_RT_H
  12567. +
  12568. +#ifndef _LINUX_RWSEM_H
  12569. +#error "Include rwsem.h"
  12570. +#endif
  12571. +
  12572. +/*
  12573. + * RW-semaphores are a spinlock plus a reader-depth count.
  12574. + *
  12575. + * Note that the semantics are different from the usual
  12576. + * Linux rw-sems, in PREEMPT_RT mode we do not allow
  12577. + * multiple readers to hold the lock at once, we only allow
  12578. + * a read-lock owner to read-lock recursively. This is
  12579. + * better for latency, makes the implementation inherently
  12580. + * fair and makes it simpler as well.
  12581. + */
  12582. +
  12583. +#include <linux/rtmutex.h>
  12584. +
  12585. +struct rw_semaphore {
  12586. + struct rt_mutex lock;
  12587. + int read_depth;
  12588. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12589. + struct lockdep_map dep_map;
  12590. +#endif
  12591. +};
  12592. +
  12593. +#define __RWSEM_INITIALIZER(name) \
  12594. + { .lock = __RT_MUTEX_INITIALIZER(name.lock), \
  12595. + RW_DEP_MAP_INIT(name) }
  12596. +
  12597. +#define DECLARE_RWSEM(lockname) \
  12598. + struct rw_semaphore lockname = __RWSEM_INITIALIZER(lockname)
  12599. +
  12600. +extern void __rt_rwsem_init(struct rw_semaphore *rwsem, const char *name,
  12601. + struct lock_class_key *key);
  12602. +
  12603. +#define __rt_init_rwsem(sem, name, key) \
  12604. + do { \
  12605. + rt_mutex_init(&(sem)->lock); \
  12606. + __rt_rwsem_init((sem), (name), (key));\
  12607. + } while (0)
  12608. +
  12609. +#define __init_rwsem(sem, name, key) __rt_init_rwsem(sem, name, key)
  12610. +
  12611. +# define rt_init_rwsem(sem) \
  12612. +do { \
  12613. + static struct lock_class_key __key; \
  12614. + \
  12615. + __rt_init_rwsem((sem), #sem, &__key); \
  12616. +} while (0)
  12617. +
  12618. +extern void rt_down_write(struct rw_semaphore *rwsem);
  12619. +extern void rt_down_read_nested(struct rw_semaphore *rwsem, int subclass);
  12620. +extern void rt_down_write_nested(struct rw_semaphore *rwsem, int subclass);
  12621. +extern void rt_down_write_nested_lock(struct rw_semaphore *rwsem,
  12622. + struct lockdep_map *nest);
  12623. +extern void rt__down_read(struct rw_semaphore *rwsem);
  12624. +extern void rt_down_read(struct rw_semaphore *rwsem);
  12625. +extern int rt_down_write_trylock(struct rw_semaphore *rwsem);
  12626. +extern int rt__down_read_trylock(struct rw_semaphore *rwsem);
  12627. +extern int rt_down_read_trylock(struct rw_semaphore *rwsem);
  12628. +extern void __rt_up_read(struct rw_semaphore *rwsem);
  12629. +extern void rt_up_read(struct rw_semaphore *rwsem);
  12630. +extern void rt_up_write(struct rw_semaphore *rwsem);
  12631. +extern void rt_downgrade_write(struct rw_semaphore *rwsem);
  12632. +
  12633. +#define init_rwsem(sem) rt_init_rwsem(sem)
  12634. +#define rwsem_is_locked(s) rt_mutex_is_locked(&(s)->lock)
  12635. +
  12636. +static inline int rwsem_is_contended(struct rw_semaphore *sem)
  12637. +{
  12638. + /* rt_mutex_has_waiters() */
  12639. + return !RB_EMPTY_ROOT(&sem->lock.waiters);
  12640. +}
  12641. +
  12642. +static inline void __down_read(struct rw_semaphore *sem)
  12643. +{
  12644. + rt__down_read(sem);
  12645. +}
  12646. +
  12647. +static inline void down_read(struct rw_semaphore *sem)
  12648. +{
  12649. + rt_down_read(sem);
  12650. +}
  12651. +
  12652. +static inline int __down_read_trylock(struct rw_semaphore *sem)
  12653. +{
  12654. + return rt__down_read_trylock(sem);
  12655. +}
  12656. +
  12657. +static inline int down_read_trylock(struct rw_semaphore *sem)
  12658. +{
  12659. + return rt_down_read_trylock(sem);
  12660. +}
  12661. +
  12662. +static inline void down_write(struct rw_semaphore *sem)
  12663. +{
  12664. + rt_down_write(sem);
  12665. +}
  12666. +
  12667. +static inline int down_write_trylock(struct rw_semaphore *sem)
  12668. +{
  12669. + return rt_down_write_trylock(sem);
  12670. +}
  12671. +
  12672. +static inline void __up_read(struct rw_semaphore *sem)
  12673. +{
  12674. + __rt_up_read(sem);
  12675. +}
  12676. +
  12677. +static inline void up_read(struct rw_semaphore *sem)
  12678. +{
  12679. + rt_up_read(sem);
  12680. +}
  12681. +
  12682. +static inline void up_write(struct rw_semaphore *sem)
  12683. +{
  12684. + rt_up_write(sem);
  12685. +}
  12686. +
  12687. +static inline void downgrade_write(struct rw_semaphore *sem)
  12688. +{
  12689. + rt_downgrade_write(sem);
  12690. +}
  12691. +
  12692. +static inline void down_read_nested(struct rw_semaphore *sem, int subclass)
  12693. +{
  12694. + return rt_down_read_nested(sem, subclass);
  12695. +}
  12696. +
  12697. +static inline void down_write_nested(struct rw_semaphore *sem, int subclass)
  12698. +{
  12699. + rt_down_write_nested(sem, subclass);
  12700. +}
  12701. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12702. +static inline void down_write_nest_lock(struct rw_semaphore *sem,
  12703. + struct rw_semaphore *nest_lock)
  12704. +{
  12705. + rt_down_write_nested_lock(sem, &nest_lock->dep_map);
  12706. +}
  12707. +
  12708. +#else
  12709. +
  12710. +static inline void down_write_nest_lock(struct rw_semaphore *sem,
  12711. + struct rw_semaphore *nest_lock)
  12712. +{
  12713. + rt_down_write_nested_lock(sem, NULL);
  12714. +}
  12715. +#endif
  12716. +#endif
  12717. diff -Nur linux-4.4.28.orig/include/linux/sched.h linux-4.4.28/include/linux/sched.h
  12718. --- linux-4.4.28.orig/include/linux/sched.h 2016-10-28 09:53:25.000000000 +0200
  12719. +++ linux-4.4.28/include/linux/sched.h 2016-11-01 21:26:58.740339958 +0100
  12720. @@ -26,6 +26,7 @@
  12721. #include <linux/nodemask.h>
  12722. #include <linux/mm_types.h>
  12723. #include <linux/preempt.h>
  12724. +#include <asm/kmap_types.h>
  12725. #include <asm/page.h>
  12726. #include <asm/ptrace.h>
  12727. @@ -182,8 +183,6 @@
  12728. static inline void update_cpu_load_nohz(void) { }
  12729. #endif
  12730. -extern unsigned long get_parent_ip(unsigned long addr);
  12731. -
  12732. extern void dump_cpu_task(int cpu);
  12733. struct seq_file;
  12734. @@ -242,10 +241,7 @@
  12735. TASK_UNINTERRUPTIBLE | __TASK_STOPPED | \
  12736. __TASK_TRACED | EXIT_ZOMBIE | EXIT_DEAD)
  12737. -#define task_is_traced(task) ((task->state & __TASK_TRACED) != 0)
  12738. #define task_is_stopped(task) ((task->state & __TASK_STOPPED) != 0)
  12739. -#define task_is_stopped_or_traced(task) \
  12740. - ((task->state & (__TASK_STOPPED | __TASK_TRACED)) != 0)
  12741. #define task_contributes_to_load(task) \
  12742. ((task->state & TASK_UNINTERRUPTIBLE) != 0 && \
  12743. (task->flags & PF_FROZEN) == 0 && \
  12744. @@ -311,6 +307,11 @@
  12745. #endif
  12746. +#define __set_current_state_no_track(state_value) \
  12747. + do { current->state = (state_value); } while (0)
  12748. +#define set_current_state_no_track(state_value) \
  12749. + set_mb(current->state, (state_value))
  12750. +
  12751. /* Task command name length */
  12752. #define TASK_COMM_LEN 16
  12753. @@ -970,8 +971,18 @@
  12754. struct wake_q_head name = { WAKE_Q_TAIL, &name.first }
  12755. extern void wake_q_add(struct wake_q_head *head,
  12756. - struct task_struct *task);
  12757. -extern void wake_up_q(struct wake_q_head *head);
  12758. + struct task_struct *task);
  12759. +extern void __wake_up_q(struct wake_q_head *head, bool sleeper);
  12760. +
  12761. +static inline void wake_up_q(struct wake_q_head *head)
  12762. +{
  12763. + __wake_up_q(head, false);
  12764. +}
  12765. +
  12766. +static inline void wake_up_q_sleeper(struct wake_q_head *head)
  12767. +{
  12768. + __wake_up_q(head, true);
  12769. +}
  12770. /*
  12771. * sched-domains (multiprocessor balancing) declarations:
  12772. @@ -1379,6 +1390,7 @@
  12773. struct task_struct {
  12774. volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
  12775. + volatile long saved_state; /* saved state for "spinlock sleepers" */
  12776. void *stack;
  12777. atomic_t usage;
  12778. unsigned int flags; /* per process flags, defined below */
  12779. @@ -1415,6 +1427,12 @@
  12780. #endif
  12781. unsigned int policy;
  12782. +#ifdef CONFIG_PREEMPT_RT_FULL
  12783. + int migrate_disable;
  12784. +# ifdef CONFIG_SCHED_DEBUG
  12785. + int migrate_disable_atomic;
  12786. +# endif
  12787. +#endif
  12788. int nr_cpus_allowed;
  12789. cpumask_t cpus_allowed;
  12790. @@ -1522,11 +1540,14 @@
  12791. cputime_t gtime;
  12792. struct prev_cputime prev_cputime;
  12793. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  12794. - seqlock_t vtime_seqlock;
  12795. + seqcount_t vtime_seqcount;
  12796. unsigned long long vtime_snap;
  12797. enum {
  12798. - VTIME_SLEEPING = 0,
  12799. + /* Task is sleeping or running in a CPU with VTIME inactive */
  12800. + VTIME_INACTIVE = 0,
  12801. + /* Task runs in userspace in a CPU with VTIME active */
  12802. VTIME_USER,
  12803. + /* Task runs in kernelspace in a CPU with VTIME active */
  12804. VTIME_SYS,
  12805. } vtime_snap_whence;
  12806. #endif
  12807. @@ -1538,6 +1559,9 @@
  12808. struct task_cputime cputime_expires;
  12809. struct list_head cpu_timers[3];
  12810. +#ifdef CONFIG_PREEMPT_RT_BASE
  12811. + struct task_struct *posix_timer_list;
  12812. +#endif
  12813. /* process credentials */
  12814. const struct cred __rcu *real_cred; /* objective and real subjective task
  12815. @@ -1568,10 +1592,15 @@
  12816. /* signal handlers */
  12817. struct signal_struct *signal;
  12818. struct sighand_struct *sighand;
  12819. + struct sigqueue *sigqueue_cache;
  12820. sigset_t blocked, real_blocked;
  12821. sigset_t saved_sigmask; /* restored if set_restore_sigmask() was used */
  12822. struct sigpending pending;
  12823. +#ifdef CONFIG_PREEMPT_RT_FULL
  12824. + /* TODO: move me into ->restart_block ? */
  12825. + struct siginfo forced_info;
  12826. +#endif
  12827. unsigned long sas_ss_sp;
  12828. size_t sas_ss_size;
  12829. @@ -1795,6 +1824,12 @@
  12830. unsigned long trace;
  12831. /* bitmask and counter of trace recursion */
  12832. unsigned long trace_recursion;
  12833. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  12834. + u64 preempt_timestamp_hist;
  12835. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  12836. + long timer_offset;
  12837. +#endif
  12838. +#endif
  12839. #endif /* CONFIG_TRACING */
  12840. #ifdef CONFIG_MEMCG
  12841. struct mem_cgroup *memcg_in_oom;
  12842. @@ -1811,9 +1846,23 @@
  12843. unsigned int sequential_io;
  12844. unsigned int sequential_io_avg;
  12845. #endif
  12846. +#ifdef CONFIG_PREEMPT_RT_BASE
  12847. + struct rcu_head put_rcu;
  12848. + int softirq_nestcnt;
  12849. + unsigned int softirqs_raised;
  12850. +#endif
  12851. +#ifdef CONFIG_PREEMPT_RT_FULL
  12852. +# if defined CONFIG_HIGHMEM || defined CONFIG_X86_32
  12853. + int kmap_idx;
  12854. + pte_t kmap_pte[KM_TYPE_NR];
  12855. +# endif
  12856. +#endif
  12857. #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
  12858. unsigned long task_state_change;
  12859. #endif
  12860. +#ifdef CONFIG_PREEMPT_RT_FULL
  12861. + int xmit_recursion;
  12862. +#endif
  12863. int pagefault_disabled;
  12864. /* CPU-specific state of this task */
  12865. struct thread_struct thread;
  12866. @@ -1831,9 +1880,6 @@
  12867. # define arch_task_struct_size (sizeof(struct task_struct))
  12868. #endif
  12869. -/* Future-safe accessor for struct task_struct's cpus_allowed. */
  12870. -#define tsk_cpus_allowed(tsk) (&(tsk)->cpus_allowed)
  12871. -
  12872. #define TNF_MIGRATED 0x01
  12873. #define TNF_NO_GROUP 0x02
  12874. #define TNF_SHARED 0x04
  12875. @@ -2023,6 +2069,15 @@
  12876. extern void free_task(struct task_struct *tsk);
  12877. #define get_task_struct(tsk) do { atomic_inc(&(tsk)->usage); } while(0)
  12878. +#ifdef CONFIG_PREEMPT_RT_BASE
  12879. +extern void __put_task_struct_cb(struct rcu_head *rhp);
  12880. +
  12881. +static inline void put_task_struct(struct task_struct *t)
  12882. +{
  12883. + if (atomic_dec_and_test(&t->usage))
  12884. + call_rcu(&t->put_rcu, __put_task_struct_cb);
  12885. +}
  12886. +#else
  12887. extern void __put_task_struct(struct task_struct *t);
  12888. static inline void put_task_struct(struct task_struct *t)
  12889. @@ -2030,6 +2085,7 @@
  12890. if (atomic_dec_and_test(&t->usage))
  12891. __put_task_struct(t);
  12892. }
  12893. +#endif
  12894. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  12895. extern void task_cputime(struct task_struct *t,
  12896. @@ -2068,6 +2124,7 @@
  12897. /*
  12898. * Per process flags
  12899. */
  12900. +#define PF_IN_SOFTIRQ 0x00000001 /* Task is serving softirq */
  12901. #define PF_EXITING 0x00000004 /* getting shut down */
  12902. #define PF_EXITPIDONE 0x00000008 /* pi exit done on shut down */
  12903. #define PF_VCPU 0x00000010 /* I'm a virtual CPU */
  12904. @@ -2232,6 +2289,10 @@
  12905. extern int set_cpus_allowed_ptr(struct task_struct *p,
  12906. const struct cpumask *new_mask);
  12907. +int migrate_me(void);
  12908. +void tell_sched_cpu_down_begin(int cpu);
  12909. +void tell_sched_cpu_down_done(int cpu);
  12910. +
  12911. #else
  12912. static inline void do_set_cpus_allowed(struct task_struct *p,
  12913. const struct cpumask *new_mask)
  12914. @@ -2244,6 +2305,9 @@
  12915. return -EINVAL;
  12916. return 0;
  12917. }
  12918. +static inline int migrate_me(void) { return 0; }
  12919. +static inline void tell_sched_cpu_down_begin(int cpu) { }
  12920. +static inline void tell_sched_cpu_down_done(int cpu) { }
  12921. #endif
  12922. #ifdef CONFIG_NO_HZ_COMMON
  12923. @@ -2453,6 +2517,7 @@
  12924. extern int wake_up_state(struct task_struct *tsk, unsigned int state);
  12925. extern int wake_up_process(struct task_struct *tsk);
  12926. +extern int wake_up_lock_sleeper(struct task_struct * tsk);
  12927. extern void wake_up_new_task(struct task_struct *tsk);
  12928. #ifdef CONFIG_SMP
  12929. extern void kick_process(struct task_struct *tsk);
  12930. @@ -2576,12 +2641,24 @@
  12931. /* mmdrop drops the mm and the page tables */
  12932. extern void __mmdrop(struct mm_struct *);
  12933. +
  12934. static inline void mmdrop(struct mm_struct * mm)
  12935. {
  12936. if (unlikely(atomic_dec_and_test(&mm->mm_count)))
  12937. __mmdrop(mm);
  12938. }
  12939. +#ifdef CONFIG_PREEMPT_RT_BASE
  12940. +extern void __mmdrop_delayed(struct rcu_head *rhp);
  12941. +static inline void mmdrop_delayed(struct mm_struct *mm)
  12942. +{
  12943. + if (atomic_dec_and_test(&mm->mm_count))
  12944. + call_rcu(&mm->delayed_drop, __mmdrop_delayed);
  12945. +}
  12946. +#else
  12947. +# define mmdrop_delayed(mm) mmdrop(mm)
  12948. +#endif
  12949. +
  12950. /* mmput gets rid of the mappings and all user-space */
  12951. extern void mmput(struct mm_struct *);
  12952. /* Grab a reference to a task's mm, if it is not already going away */
  12953. @@ -2891,6 +2968,43 @@
  12954. return unlikely(test_tsk_thread_flag(tsk,TIF_NEED_RESCHED));
  12955. }
  12956. +#ifdef CONFIG_PREEMPT_LAZY
  12957. +static inline void set_tsk_need_resched_lazy(struct task_struct *tsk)
  12958. +{
  12959. + set_tsk_thread_flag(tsk,TIF_NEED_RESCHED_LAZY);
  12960. +}
  12961. +
  12962. +static inline void clear_tsk_need_resched_lazy(struct task_struct *tsk)
  12963. +{
  12964. + clear_tsk_thread_flag(tsk,TIF_NEED_RESCHED_LAZY);
  12965. +}
  12966. +
  12967. +static inline int test_tsk_need_resched_lazy(struct task_struct *tsk)
  12968. +{
  12969. + return unlikely(test_tsk_thread_flag(tsk,TIF_NEED_RESCHED_LAZY));
  12970. +}
  12971. +
  12972. +static inline int need_resched_lazy(void)
  12973. +{
  12974. + return test_thread_flag(TIF_NEED_RESCHED_LAZY);
  12975. +}
  12976. +
  12977. +static inline int need_resched_now(void)
  12978. +{
  12979. + return test_thread_flag(TIF_NEED_RESCHED);
  12980. +}
  12981. +
  12982. +#else
  12983. +static inline void clear_tsk_need_resched_lazy(struct task_struct *tsk) { }
  12984. +static inline int need_resched_lazy(void) { return 0; }
  12985. +
  12986. +static inline int need_resched_now(void)
  12987. +{
  12988. + return test_thread_flag(TIF_NEED_RESCHED);
  12989. +}
  12990. +
  12991. +#endif
  12992. +
  12993. static inline int restart_syscall(void)
  12994. {
  12995. set_tsk_thread_flag(current, TIF_SIGPENDING);
  12996. @@ -2922,6 +3036,51 @@
  12997. return (state & TASK_INTERRUPTIBLE) || __fatal_signal_pending(p);
  12998. }
  12999. +static inline bool __task_is_stopped_or_traced(struct task_struct *task)
  13000. +{
  13001. + if (task->state & (__TASK_STOPPED | __TASK_TRACED))
  13002. + return true;
  13003. +#ifdef CONFIG_PREEMPT_RT_FULL
  13004. + if (task->saved_state & (__TASK_STOPPED | __TASK_TRACED))
  13005. + return true;
  13006. +#endif
  13007. + return false;
  13008. +}
  13009. +
  13010. +static inline bool task_is_stopped_or_traced(struct task_struct *task)
  13011. +{
  13012. + bool traced_stopped;
  13013. +
  13014. +#ifdef CONFIG_PREEMPT_RT_FULL
  13015. + unsigned long flags;
  13016. +
  13017. + raw_spin_lock_irqsave(&task->pi_lock, flags);
  13018. + traced_stopped = __task_is_stopped_or_traced(task);
  13019. + raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  13020. +#else
  13021. + traced_stopped = __task_is_stopped_or_traced(task);
  13022. +#endif
  13023. + return traced_stopped;
  13024. +}
  13025. +
  13026. +static inline bool task_is_traced(struct task_struct *task)
  13027. +{
  13028. + bool traced = false;
  13029. +
  13030. + if (task->state & __TASK_TRACED)
  13031. + return true;
  13032. +#ifdef CONFIG_PREEMPT_RT_FULL
  13033. + /* in case the task is sleeping on tasklist_lock */
  13034. + raw_spin_lock_irq(&task->pi_lock);
  13035. + if (task->state & __TASK_TRACED)
  13036. + traced = true;
  13037. + else if (task->saved_state & __TASK_TRACED)
  13038. + traced = true;
  13039. + raw_spin_unlock_irq(&task->pi_lock);
  13040. +#endif
  13041. + return traced;
  13042. +}
  13043. +
  13044. /*
  13045. * cond_resched() and cond_resched_lock(): latency reduction via
  13046. * explicit rescheduling in places that are safe. The return
  13047. @@ -2943,12 +3102,16 @@
  13048. __cond_resched_lock(lock); \
  13049. })
  13050. +#ifndef CONFIG_PREEMPT_RT_FULL
  13051. extern int __cond_resched_softirq(void);
  13052. #define cond_resched_softirq() ({ \
  13053. ___might_sleep(__FILE__, __LINE__, SOFTIRQ_DISABLE_OFFSET); \
  13054. __cond_resched_softirq(); \
  13055. })
  13056. +#else
  13057. +# define cond_resched_softirq() cond_resched()
  13058. +#endif
  13059. static inline void cond_resched_rcu(void)
  13060. {
  13061. @@ -3110,6 +3273,31 @@
  13062. #endif /* CONFIG_SMP */
  13063. +static inline int __migrate_disabled(struct task_struct *p)
  13064. +{
  13065. +#ifdef CONFIG_PREEMPT_RT_FULL
  13066. + return p->migrate_disable;
  13067. +#else
  13068. + return 0;
  13069. +#endif
  13070. +}
  13071. +
  13072. +/* Future-safe accessor for struct task_struct's cpus_allowed. */
  13073. +static inline const struct cpumask *tsk_cpus_allowed(struct task_struct *p)
  13074. +{
  13075. + if (__migrate_disabled(p))
  13076. + return cpumask_of(task_cpu(p));
  13077. +
  13078. + return &p->cpus_allowed;
  13079. +}
  13080. +
  13081. +static inline int tsk_nr_cpus_allowed(struct task_struct *p)
  13082. +{
  13083. + if (__migrate_disabled(p))
  13084. + return 1;
  13085. + return p->nr_cpus_allowed;
  13086. +}
  13087. +
  13088. extern long sched_setaffinity(pid_t pid, const struct cpumask *new_mask);
  13089. extern long sched_getaffinity(pid_t pid, struct cpumask *mask);
  13090. diff -Nur linux-4.4.28.orig/include/linux/seqlock.h linux-4.4.28/include/linux/seqlock.h
  13091. --- linux-4.4.28.orig/include/linux/seqlock.h 2016-10-28 09:53:25.000000000 +0200
  13092. +++ linux-4.4.28/include/linux/seqlock.h 2016-11-01 21:26:58.744340223 +0100
  13093. @@ -220,20 +220,30 @@
  13094. return __read_seqcount_retry(s, start);
  13095. }
  13096. -
  13097. -
  13098. -static inline void raw_write_seqcount_begin(seqcount_t *s)
  13099. +static inline void __raw_write_seqcount_begin(seqcount_t *s)
  13100. {
  13101. s->sequence++;
  13102. smp_wmb();
  13103. }
  13104. -static inline void raw_write_seqcount_end(seqcount_t *s)
  13105. +static inline void raw_write_seqcount_begin(seqcount_t *s)
  13106. +{
  13107. + preempt_disable_rt();
  13108. + __raw_write_seqcount_begin(s);
  13109. +}
  13110. +
  13111. +static inline void __raw_write_seqcount_end(seqcount_t *s)
  13112. {
  13113. smp_wmb();
  13114. s->sequence++;
  13115. }
  13116. +static inline void raw_write_seqcount_end(seqcount_t *s)
  13117. +{
  13118. + __raw_write_seqcount_end(s);
  13119. + preempt_enable_rt();
  13120. +}
  13121. +
  13122. /**
  13123. * raw_write_seqcount_barrier - do a seq write barrier
  13124. * @s: pointer to seqcount_t
  13125. @@ -425,10 +435,32 @@
  13126. /*
  13127. * Read side functions for starting and finalizing a read side section.
  13128. */
  13129. +#ifndef CONFIG_PREEMPT_RT_FULL
  13130. static inline unsigned read_seqbegin(const seqlock_t *sl)
  13131. {
  13132. return read_seqcount_begin(&sl->seqcount);
  13133. }
  13134. +#else
  13135. +/*
  13136. + * Starvation safe read side for RT
  13137. + */
  13138. +static inline unsigned read_seqbegin(seqlock_t *sl)
  13139. +{
  13140. + unsigned ret;
  13141. +
  13142. +repeat:
  13143. + ret = ACCESS_ONCE(sl->seqcount.sequence);
  13144. + if (unlikely(ret & 1)) {
  13145. + /*
  13146. + * Take the lock and let the writer proceed (i.e. evtl
  13147. + * boost it), otherwise we could loop here forever.
  13148. + */
  13149. + spin_unlock_wait(&sl->lock);
  13150. + goto repeat;
  13151. + }
  13152. + return ret;
  13153. +}
  13154. +#endif
  13155. static inline unsigned read_seqretry(const seqlock_t *sl, unsigned start)
  13156. {
  13157. @@ -443,36 +475,36 @@
  13158. static inline void write_seqlock(seqlock_t *sl)
  13159. {
  13160. spin_lock(&sl->lock);
  13161. - write_seqcount_begin(&sl->seqcount);
  13162. + __raw_write_seqcount_begin(&sl->seqcount);
  13163. }
  13164. static inline void write_sequnlock(seqlock_t *sl)
  13165. {
  13166. - write_seqcount_end(&sl->seqcount);
  13167. + __raw_write_seqcount_end(&sl->seqcount);
  13168. spin_unlock(&sl->lock);
  13169. }
  13170. static inline void write_seqlock_bh(seqlock_t *sl)
  13171. {
  13172. spin_lock_bh(&sl->lock);
  13173. - write_seqcount_begin(&sl->seqcount);
  13174. + __raw_write_seqcount_begin(&sl->seqcount);
  13175. }
  13176. static inline void write_sequnlock_bh(seqlock_t *sl)
  13177. {
  13178. - write_seqcount_end(&sl->seqcount);
  13179. + __raw_write_seqcount_end(&sl->seqcount);
  13180. spin_unlock_bh(&sl->lock);
  13181. }
  13182. static inline void write_seqlock_irq(seqlock_t *sl)
  13183. {
  13184. spin_lock_irq(&sl->lock);
  13185. - write_seqcount_begin(&sl->seqcount);
  13186. + __raw_write_seqcount_begin(&sl->seqcount);
  13187. }
  13188. static inline void write_sequnlock_irq(seqlock_t *sl)
  13189. {
  13190. - write_seqcount_end(&sl->seqcount);
  13191. + __raw_write_seqcount_end(&sl->seqcount);
  13192. spin_unlock_irq(&sl->lock);
  13193. }
  13194. @@ -481,7 +513,7 @@
  13195. unsigned long flags;
  13196. spin_lock_irqsave(&sl->lock, flags);
  13197. - write_seqcount_begin(&sl->seqcount);
  13198. + __raw_write_seqcount_begin(&sl->seqcount);
  13199. return flags;
  13200. }
  13201. @@ -491,7 +523,7 @@
  13202. static inline void
  13203. write_sequnlock_irqrestore(seqlock_t *sl, unsigned long flags)
  13204. {
  13205. - write_seqcount_end(&sl->seqcount);
  13206. + __raw_write_seqcount_end(&sl->seqcount);
  13207. spin_unlock_irqrestore(&sl->lock, flags);
  13208. }
  13209. diff -Nur linux-4.4.28.orig/include/linux/signal.h linux-4.4.28/include/linux/signal.h
  13210. --- linux-4.4.28.orig/include/linux/signal.h 2016-10-28 09:53:25.000000000 +0200
  13211. +++ linux-4.4.28/include/linux/signal.h 2016-11-01 21:26:58.744340223 +0100
  13212. @@ -233,6 +233,7 @@
  13213. }
  13214. extern void flush_sigqueue(struct sigpending *queue);
  13215. +extern void flush_task_sigqueue(struct task_struct *tsk);
  13216. /* Test if 'sig' is valid signal. Use this instead of testing _NSIG directly */
  13217. static inline int valid_signal(unsigned long sig)
  13218. diff -Nur linux-4.4.28.orig/include/linux/skbuff.h linux-4.4.28/include/linux/skbuff.h
  13219. --- linux-4.4.28.orig/include/linux/skbuff.h 2016-10-28 09:53:25.000000000 +0200
  13220. +++ linux-4.4.28/include/linux/skbuff.h 2016-11-01 21:26:58.744340223 +0100
  13221. @@ -203,6 +203,7 @@
  13222. __u32 qlen;
  13223. spinlock_t lock;
  13224. + raw_spinlock_t raw_lock;
  13225. };
  13226. struct sk_buff;
  13227. @@ -1465,6 +1466,12 @@
  13228. __skb_queue_head_init(list);
  13229. }
  13230. +static inline void skb_queue_head_init_raw(struct sk_buff_head *list)
  13231. +{
  13232. + raw_spin_lock_init(&list->raw_lock);
  13233. + __skb_queue_head_init(list);
  13234. +}
  13235. +
  13236. static inline void skb_queue_head_init_class(struct sk_buff_head *list,
  13237. struct lock_class_key *class)
  13238. {
  13239. diff -Nur linux-4.4.28.orig/include/linux/smp.h linux-4.4.28/include/linux/smp.h
  13240. --- linux-4.4.28.orig/include/linux/smp.h 2016-10-28 09:53:25.000000000 +0200
  13241. +++ linux-4.4.28/include/linux/smp.h 2016-11-01 21:26:58.744340223 +0100
  13242. @@ -185,6 +185,9 @@
  13243. #define get_cpu() ({ preempt_disable(); smp_processor_id(); })
  13244. #define put_cpu() preempt_enable()
  13245. +#define get_cpu_light() ({ migrate_disable(); smp_processor_id(); })
  13246. +#define put_cpu_light() migrate_enable()
  13247. +
  13248. /*
  13249. * Callback to arch code if there's nosmp or maxcpus=0 on the
  13250. * boot command line:
  13251. diff -Nur linux-4.4.28.orig/include/linux/spinlock_api_smp.h linux-4.4.28/include/linux/spinlock_api_smp.h
  13252. --- linux-4.4.28.orig/include/linux/spinlock_api_smp.h 2016-10-28 09:53:25.000000000 +0200
  13253. +++ linux-4.4.28/include/linux/spinlock_api_smp.h 2016-11-01 21:26:58.744340223 +0100
  13254. @@ -189,6 +189,8 @@
  13255. return 0;
  13256. }
  13257. -#include <linux/rwlock_api_smp.h>
  13258. +#ifndef CONFIG_PREEMPT_RT_FULL
  13259. +# include <linux/rwlock_api_smp.h>
  13260. +#endif
  13261. #endif /* __LINUX_SPINLOCK_API_SMP_H */
  13262. diff -Nur linux-4.4.28.orig/include/linux/spinlock.h linux-4.4.28/include/linux/spinlock.h
  13263. --- linux-4.4.28.orig/include/linux/spinlock.h 2016-10-28 09:53:25.000000000 +0200
  13264. +++ linux-4.4.28/include/linux/spinlock.h 2016-11-01 21:26:58.744340223 +0100
  13265. @@ -271,7 +271,11 @@
  13266. #define raw_spin_can_lock(lock) (!raw_spin_is_locked(lock))
  13267. /* Include rwlock functions */
  13268. -#include <linux/rwlock.h>
  13269. +#ifdef CONFIG_PREEMPT_RT_FULL
  13270. +# include <linux/rwlock_rt.h>
  13271. +#else
  13272. +# include <linux/rwlock.h>
  13273. +#endif
  13274. /*
  13275. * Pull the _spin_*()/_read_*()/_write_*() functions/declarations:
  13276. @@ -282,6 +286,10 @@
  13277. # include <linux/spinlock_api_up.h>
  13278. #endif
  13279. +#ifdef CONFIG_PREEMPT_RT_FULL
  13280. +# include <linux/spinlock_rt.h>
  13281. +#else /* PREEMPT_RT_FULL */
  13282. +
  13283. /*
  13284. * Map the spin_lock functions to the raw variants for PREEMPT_RT=n
  13285. */
  13286. @@ -347,6 +355,12 @@
  13287. raw_spin_unlock(&lock->rlock);
  13288. }
  13289. +static __always_inline int spin_unlock_no_deboost(spinlock_t *lock)
  13290. +{
  13291. + raw_spin_unlock(&lock->rlock);
  13292. + return 0;
  13293. +}
  13294. +
  13295. static __always_inline void spin_unlock_bh(spinlock_t *lock)
  13296. {
  13297. raw_spin_unlock_bh(&lock->rlock);
  13298. @@ -416,4 +430,6 @@
  13299. #define atomic_dec_and_lock(atomic, lock) \
  13300. __cond_lock(lock, _atomic_dec_and_lock(atomic, lock))
  13301. +#endif /* !PREEMPT_RT_FULL */
  13302. +
  13303. #endif /* __LINUX_SPINLOCK_H */
  13304. diff -Nur linux-4.4.28.orig/include/linux/spinlock_rt.h linux-4.4.28/include/linux/spinlock_rt.h
  13305. --- linux-4.4.28.orig/include/linux/spinlock_rt.h 1970-01-01 01:00:00.000000000 +0100
  13306. +++ linux-4.4.28/include/linux/spinlock_rt.h 2016-11-01 21:26:58.744340223 +0100
  13307. @@ -0,0 +1,165 @@
  13308. +#ifndef __LINUX_SPINLOCK_RT_H
  13309. +#define __LINUX_SPINLOCK_RT_H
  13310. +
  13311. +#ifndef __LINUX_SPINLOCK_H
  13312. +#error Do not include directly. Use spinlock.h
  13313. +#endif
  13314. +
  13315. +#include <linux/bug.h>
  13316. +
  13317. +extern void
  13318. +__rt_spin_lock_init(spinlock_t *lock, char *name, struct lock_class_key *key);
  13319. +
  13320. +#define spin_lock_init(slock) \
  13321. +do { \
  13322. + static struct lock_class_key __key; \
  13323. + \
  13324. + rt_mutex_init(&(slock)->lock); \
  13325. + __rt_spin_lock_init(slock, #slock, &__key); \
  13326. +} while (0)
  13327. +
  13328. +void __lockfunc rt_spin_lock__no_mg(spinlock_t *lock);
  13329. +void __lockfunc rt_spin_unlock__no_mg(spinlock_t *lock);
  13330. +int __lockfunc rt_spin_trylock__no_mg(spinlock_t *lock);
  13331. +
  13332. +extern void __lockfunc rt_spin_lock(spinlock_t *lock);
  13333. +extern unsigned long __lockfunc rt_spin_lock_trace_flags(spinlock_t *lock);
  13334. +extern void __lockfunc rt_spin_lock_nested(spinlock_t *lock, int subclass);
  13335. +extern void __lockfunc rt_spin_unlock(spinlock_t *lock);
  13336. +extern int __lockfunc rt_spin_unlock_no_deboost(spinlock_t *lock);
  13337. +extern void __lockfunc rt_spin_unlock_wait(spinlock_t *lock);
  13338. +extern int __lockfunc rt_spin_trylock_irqsave(spinlock_t *lock, unsigned long *flags);
  13339. +extern int __lockfunc rt_spin_trylock_bh(spinlock_t *lock);
  13340. +extern int __lockfunc rt_spin_trylock(spinlock_t *lock);
  13341. +extern int atomic_dec_and_spin_lock(atomic_t *atomic, spinlock_t *lock);
  13342. +
  13343. +/*
  13344. + * lockdep-less calls, for derived types like rwlock:
  13345. + * (for trylock they can use rt_mutex_trylock() directly.
  13346. + */
  13347. +extern void __lockfunc __rt_spin_lock__no_mg(struct rt_mutex *lock);
  13348. +extern void __lockfunc __rt_spin_lock(struct rt_mutex *lock);
  13349. +extern void __lockfunc __rt_spin_unlock(struct rt_mutex *lock);
  13350. +extern int __lockfunc __rt_spin_trylock(struct rt_mutex *lock);
  13351. +
  13352. +#define spin_lock(lock) rt_spin_lock(lock)
  13353. +
  13354. +#define spin_lock_bh(lock) \
  13355. + do { \
  13356. + local_bh_disable(); \
  13357. + rt_spin_lock(lock); \
  13358. + } while (0)
  13359. +
  13360. +#define spin_lock_irq(lock) spin_lock(lock)
  13361. +
  13362. +#define spin_do_trylock(lock) __cond_lock(lock, rt_spin_trylock(lock))
  13363. +
  13364. +#define spin_trylock(lock) \
  13365. +({ \
  13366. + int __locked; \
  13367. + __locked = spin_do_trylock(lock); \
  13368. + __locked; \
  13369. +})
  13370. +
  13371. +#ifdef CONFIG_LOCKDEP
  13372. +# define spin_lock_nested(lock, subclass) \
  13373. + do { \
  13374. + rt_spin_lock_nested(lock, subclass); \
  13375. + } while (0)
  13376. +
  13377. +#define spin_lock_bh_nested(lock, subclass) \
  13378. + do { \
  13379. + local_bh_disable(); \
  13380. + rt_spin_lock_nested(lock, subclass); \
  13381. + } while (0)
  13382. +
  13383. +# define spin_lock_irqsave_nested(lock, flags, subclass) \
  13384. + do { \
  13385. + typecheck(unsigned long, flags); \
  13386. + flags = 0; \
  13387. + rt_spin_lock_nested(lock, subclass); \
  13388. + } while (0)
  13389. +#else
  13390. +# define spin_lock_nested(lock, subclass) spin_lock(lock)
  13391. +# define spin_lock_bh_nested(lock, subclass) spin_lock_bh(lock)
  13392. +
  13393. +# define spin_lock_irqsave_nested(lock, flags, subclass) \
  13394. + do { \
  13395. + typecheck(unsigned long, flags); \
  13396. + flags = 0; \
  13397. + spin_lock(lock); \
  13398. + } while (0)
  13399. +#endif
  13400. +
  13401. +#define spin_lock_irqsave(lock, flags) \
  13402. + do { \
  13403. + typecheck(unsigned long, flags); \
  13404. + flags = 0; \
  13405. + spin_lock(lock); \
  13406. + } while (0)
  13407. +
  13408. +static inline unsigned long spin_lock_trace_flags(spinlock_t *lock)
  13409. +{
  13410. + unsigned long flags = 0;
  13411. +#ifdef CONFIG_TRACE_IRQFLAGS
  13412. + flags = rt_spin_lock_trace_flags(lock);
  13413. +#else
  13414. + spin_lock(lock); /* lock_local */
  13415. +#endif
  13416. + return flags;
  13417. +}
  13418. +
  13419. +/* FIXME: we need rt_spin_lock_nest_lock */
  13420. +#define spin_lock_nest_lock(lock, nest_lock) spin_lock_nested(lock, 0)
  13421. +
  13422. +#define spin_unlock(lock) rt_spin_unlock(lock)
  13423. +#define spin_unlock_no_deboost(lock) rt_spin_unlock_no_deboost(lock)
  13424. +
  13425. +#define spin_unlock_bh(lock) \
  13426. + do { \
  13427. + rt_spin_unlock(lock); \
  13428. + local_bh_enable(); \
  13429. + } while (0)
  13430. +
  13431. +#define spin_unlock_irq(lock) spin_unlock(lock)
  13432. +
  13433. +#define spin_unlock_irqrestore(lock, flags) \
  13434. + do { \
  13435. + typecheck(unsigned long, flags); \
  13436. + (void) flags; \
  13437. + spin_unlock(lock); \
  13438. + } while (0)
  13439. +
  13440. +#define spin_trylock_bh(lock) __cond_lock(lock, rt_spin_trylock_bh(lock))
  13441. +#define spin_trylock_irq(lock) spin_trylock(lock)
  13442. +
  13443. +#define spin_trylock_irqsave(lock, flags) \
  13444. + rt_spin_trylock_irqsave(lock, &(flags))
  13445. +
  13446. +#define spin_unlock_wait(lock) rt_spin_unlock_wait(lock)
  13447. +
  13448. +#ifdef CONFIG_GENERIC_LOCKBREAK
  13449. +# define spin_is_contended(lock) ((lock)->break_lock)
  13450. +#else
  13451. +# define spin_is_contended(lock) (((void)(lock), 0))
  13452. +#endif
  13453. +
  13454. +static inline int spin_can_lock(spinlock_t *lock)
  13455. +{
  13456. + return !rt_mutex_is_locked(&lock->lock);
  13457. +}
  13458. +
  13459. +static inline int spin_is_locked(spinlock_t *lock)
  13460. +{
  13461. + return rt_mutex_is_locked(&lock->lock);
  13462. +}
  13463. +
  13464. +static inline void assert_spin_locked(spinlock_t *lock)
  13465. +{
  13466. + BUG_ON(!spin_is_locked(lock));
  13467. +}
  13468. +
  13469. +#define atomic_dec_and_lock(atomic, lock) \
  13470. + atomic_dec_and_spin_lock(atomic, lock)
  13471. +
  13472. +#endif
  13473. diff -Nur linux-4.4.28.orig/include/linux/spinlock_types.h linux-4.4.28/include/linux/spinlock_types.h
  13474. --- linux-4.4.28.orig/include/linux/spinlock_types.h 2016-10-28 09:53:25.000000000 +0200
  13475. +++ linux-4.4.28/include/linux/spinlock_types.h 2016-11-01 21:26:58.744340223 +0100
  13476. @@ -9,80 +9,15 @@
  13477. * Released under the General Public License (GPL).
  13478. */
  13479. -#if defined(CONFIG_SMP)
  13480. -# include <asm/spinlock_types.h>
  13481. -#else
  13482. -# include <linux/spinlock_types_up.h>
  13483. -#endif
  13484. -
  13485. -#include <linux/lockdep.h>
  13486. -
  13487. -typedef struct raw_spinlock {
  13488. - arch_spinlock_t raw_lock;
  13489. -#ifdef CONFIG_GENERIC_LOCKBREAK
  13490. - unsigned int break_lock;
  13491. -#endif
  13492. -#ifdef CONFIG_DEBUG_SPINLOCK
  13493. - unsigned int magic, owner_cpu;
  13494. - void *owner;
  13495. -#endif
  13496. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  13497. - struct lockdep_map dep_map;
  13498. -#endif
  13499. -} raw_spinlock_t;
  13500. -
  13501. -#define SPINLOCK_MAGIC 0xdead4ead
  13502. -
  13503. -#define SPINLOCK_OWNER_INIT ((void *)-1L)
  13504. -
  13505. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  13506. -# define SPIN_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }
  13507. -#else
  13508. -# define SPIN_DEP_MAP_INIT(lockname)
  13509. -#endif
  13510. +#include <linux/spinlock_types_raw.h>
  13511. -#ifdef CONFIG_DEBUG_SPINLOCK
  13512. -# define SPIN_DEBUG_INIT(lockname) \
  13513. - .magic = SPINLOCK_MAGIC, \
  13514. - .owner_cpu = -1, \
  13515. - .owner = SPINLOCK_OWNER_INIT,
  13516. +#ifndef CONFIG_PREEMPT_RT_FULL
  13517. +# include <linux/spinlock_types_nort.h>
  13518. +# include <linux/rwlock_types.h>
  13519. #else
  13520. -# define SPIN_DEBUG_INIT(lockname)
  13521. +# include <linux/rtmutex.h>
  13522. +# include <linux/spinlock_types_rt.h>
  13523. +# include <linux/rwlock_types_rt.h>
  13524. #endif
  13525. -#define __RAW_SPIN_LOCK_INITIALIZER(lockname) \
  13526. - { \
  13527. - .raw_lock = __ARCH_SPIN_LOCK_UNLOCKED, \
  13528. - SPIN_DEBUG_INIT(lockname) \
  13529. - SPIN_DEP_MAP_INIT(lockname) }
  13530. -
  13531. -#define __RAW_SPIN_LOCK_UNLOCKED(lockname) \
  13532. - (raw_spinlock_t) __RAW_SPIN_LOCK_INITIALIZER(lockname)
  13533. -
  13534. -#define DEFINE_RAW_SPINLOCK(x) raw_spinlock_t x = __RAW_SPIN_LOCK_UNLOCKED(x)
  13535. -
  13536. -typedef struct spinlock {
  13537. - union {
  13538. - struct raw_spinlock rlock;
  13539. -
  13540. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  13541. -# define LOCK_PADSIZE (offsetof(struct raw_spinlock, dep_map))
  13542. - struct {
  13543. - u8 __padding[LOCK_PADSIZE];
  13544. - struct lockdep_map dep_map;
  13545. - };
  13546. -#endif
  13547. - };
  13548. -} spinlock_t;
  13549. -
  13550. -#define __SPIN_LOCK_INITIALIZER(lockname) \
  13551. - { { .rlock = __RAW_SPIN_LOCK_INITIALIZER(lockname) } }
  13552. -
  13553. -#define __SPIN_LOCK_UNLOCKED(lockname) \
  13554. - (spinlock_t ) __SPIN_LOCK_INITIALIZER(lockname)
  13555. -
  13556. -#define DEFINE_SPINLOCK(x) spinlock_t x = __SPIN_LOCK_UNLOCKED(x)
  13557. -
  13558. -#include <linux/rwlock_types.h>
  13559. -
  13560. #endif /* __LINUX_SPINLOCK_TYPES_H */
  13561. diff -Nur linux-4.4.28.orig/include/linux/spinlock_types_nort.h linux-4.4.28/include/linux/spinlock_types_nort.h
  13562. --- linux-4.4.28.orig/include/linux/spinlock_types_nort.h 1970-01-01 01:00:00.000000000 +0100
  13563. +++ linux-4.4.28/include/linux/spinlock_types_nort.h 2016-11-01 21:26:58.744340223 +0100
  13564. @@ -0,0 +1,33 @@
  13565. +#ifndef __LINUX_SPINLOCK_TYPES_NORT_H
  13566. +#define __LINUX_SPINLOCK_TYPES_NORT_H
  13567. +
  13568. +#ifndef __LINUX_SPINLOCK_TYPES_H
  13569. +#error "Do not include directly. Include spinlock_types.h instead"
  13570. +#endif
  13571. +
  13572. +/*
  13573. + * The non RT version maps spinlocks to raw_spinlocks
  13574. + */
  13575. +typedef struct spinlock {
  13576. + union {
  13577. + struct raw_spinlock rlock;
  13578. +
  13579. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  13580. +# define LOCK_PADSIZE (offsetof(struct raw_spinlock, dep_map))
  13581. + struct {
  13582. + u8 __padding[LOCK_PADSIZE];
  13583. + struct lockdep_map dep_map;
  13584. + };
  13585. +#endif
  13586. + };
  13587. +} spinlock_t;
  13588. +
  13589. +#define __SPIN_LOCK_INITIALIZER(lockname) \
  13590. + { { .rlock = __RAW_SPIN_LOCK_INITIALIZER(lockname) } }
  13591. +
  13592. +#define __SPIN_LOCK_UNLOCKED(lockname) \
  13593. + (spinlock_t ) __SPIN_LOCK_INITIALIZER(lockname)
  13594. +
  13595. +#define DEFINE_SPINLOCK(x) spinlock_t x = __SPIN_LOCK_UNLOCKED(x)
  13596. +
  13597. +#endif
  13598. diff -Nur linux-4.4.28.orig/include/linux/spinlock_types_raw.h linux-4.4.28/include/linux/spinlock_types_raw.h
  13599. --- linux-4.4.28.orig/include/linux/spinlock_types_raw.h 1970-01-01 01:00:00.000000000 +0100
  13600. +++ linux-4.4.28/include/linux/spinlock_types_raw.h 2016-11-01 21:26:58.744340223 +0100
  13601. @@ -0,0 +1,56 @@
  13602. +#ifndef __LINUX_SPINLOCK_TYPES_RAW_H
  13603. +#define __LINUX_SPINLOCK_TYPES_RAW_H
  13604. +
  13605. +#if defined(CONFIG_SMP)
  13606. +# include <asm/spinlock_types.h>
  13607. +#else
  13608. +# include <linux/spinlock_types_up.h>
  13609. +#endif
  13610. +
  13611. +#include <linux/lockdep.h>
  13612. +
  13613. +typedef struct raw_spinlock {
  13614. + arch_spinlock_t raw_lock;
  13615. +#ifdef CONFIG_GENERIC_LOCKBREAK
  13616. + unsigned int break_lock;
  13617. +#endif
  13618. +#ifdef CONFIG_DEBUG_SPINLOCK
  13619. + unsigned int magic, owner_cpu;
  13620. + void *owner;
  13621. +#endif
  13622. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  13623. + struct lockdep_map dep_map;
  13624. +#endif
  13625. +} raw_spinlock_t;
  13626. +
  13627. +#define SPINLOCK_MAGIC 0xdead4ead
  13628. +
  13629. +#define SPINLOCK_OWNER_INIT ((void *)-1L)
  13630. +
  13631. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  13632. +# define SPIN_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }
  13633. +#else
  13634. +# define SPIN_DEP_MAP_INIT(lockname)
  13635. +#endif
  13636. +
  13637. +#ifdef CONFIG_DEBUG_SPINLOCK
  13638. +# define SPIN_DEBUG_INIT(lockname) \
  13639. + .magic = SPINLOCK_MAGIC, \
  13640. + .owner_cpu = -1, \
  13641. + .owner = SPINLOCK_OWNER_INIT,
  13642. +#else
  13643. +# define SPIN_DEBUG_INIT(lockname)
  13644. +#endif
  13645. +
  13646. +#define __RAW_SPIN_LOCK_INITIALIZER(lockname) \
  13647. + { \
  13648. + .raw_lock = __ARCH_SPIN_LOCK_UNLOCKED, \
  13649. + SPIN_DEBUG_INIT(lockname) \
  13650. + SPIN_DEP_MAP_INIT(lockname) }
  13651. +
  13652. +#define __RAW_SPIN_LOCK_UNLOCKED(lockname) \
  13653. + (raw_spinlock_t) __RAW_SPIN_LOCK_INITIALIZER(lockname)
  13654. +
  13655. +#define DEFINE_RAW_SPINLOCK(x) raw_spinlock_t x = __RAW_SPIN_LOCK_UNLOCKED(x)
  13656. +
  13657. +#endif
  13658. diff -Nur linux-4.4.28.orig/include/linux/spinlock_types_rt.h linux-4.4.28/include/linux/spinlock_types_rt.h
  13659. --- linux-4.4.28.orig/include/linux/spinlock_types_rt.h 1970-01-01 01:00:00.000000000 +0100
  13660. +++ linux-4.4.28/include/linux/spinlock_types_rt.h 2016-11-01 21:26:58.744340223 +0100
  13661. @@ -0,0 +1,51 @@
  13662. +#ifndef __LINUX_SPINLOCK_TYPES_RT_H
  13663. +#define __LINUX_SPINLOCK_TYPES_RT_H
  13664. +
  13665. +#ifndef __LINUX_SPINLOCK_TYPES_H
  13666. +#error "Do not include directly. Include spinlock_types.h instead"
  13667. +#endif
  13668. +
  13669. +#include <linux/cache.h>
  13670. +
  13671. +/*
  13672. + * PREEMPT_RT: spinlocks - an RT mutex plus lock-break field:
  13673. + */
  13674. +typedef struct spinlock {
  13675. + struct rt_mutex lock;
  13676. + unsigned int break_lock;
  13677. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  13678. + struct lockdep_map dep_map;
  13679. +#endif
  13680. +} spinlock_t;
  13681. +
  13682. +#ifdef CONFIG_DEBUG_RT_MUTEXES
  13683. +# define __RT_SPIN_INITIALIZER(name) \
  13684. + { \
  13685. + .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock), \
  13686. + .save_state = 1, \
  13687. + .file = __FILE__, \
  13688. + .line = __LINE__ , \
  13689. + }
  13690. +#else
  13691. +# define __RT_SPIN_INITIALIZER(name) \
  13692. + { \
  13693. + .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock), \
  13694. + .save_state = 1, \
  13695. + }
  13696. +#endif
  13697. +
  13698. +/*
  13699. +.wait_list = PLIST_HEAD_INIT_RAW((name).lock.wait_list, (name).lock.wait_lock)
  13700. +*/
  13701. +
  13702. +#define __SPIN_LOCK_UNLOCKED(name) \
  13703. + { .lock = __RT_SPIN_INITIALIZER(name.lock), \
  13704. + SPIN_DEP_MAP_INIT(name) }
  13705. +
  13706. +#define __DEFINE_SPINLOCK(name) \
  13707. + spinlock_t name = __SPIN_LOCK_UNLOCKED(name)
  13708. +
  13709. +#define DEFINE_SPINLOCK(name) \
  13710. + spinlock_t name __cacheline_aligned_in_smp = __SPIN_LOCK_UNLOCKED(name)
  13711. +
  13712. +#endif
  13713. diff -Nur linux-4.4.28.orig/include/linux/srcu.h linux-4.4.28/include/linux/srcu.h
  13714. --- linux-4.4.28.orig/include/linux/srcu.h 2016-10-28 09:53:25.000000000 +0200
  13715. +++ linux-4.4.28/include/linux/srcu.h 2016-11-01 21:26:58.744340223 +0100
  13716. @@ -84,10 +84,10 @@
  13717. void process_srcu(struct work_struct *work);
  13718. -#define __SRCU_STRUCT_INIT(name) \
  13719. +#define __SRCU_STRUCT_INIT(name, pcpu_name) \
  13720. { \
  13721. .completed = -300, \
  13722. - .per_cpu_ref = &name##_srcu_array, \
  13723. + .per_cpu_ref = &pcpu_name, \
  13724. .queue_lock = __SPIN_LOCK_UNLOCKED(name.queue_lock), \
  13725. .running = false, \
  13726. .batch_queue = RCU_BATCH_INIT(name.batch_queue), \
  13727. @@ -104,7 +104,7 @@
  13728. */
  13729. #define __DEFINE_SRCU(name, is_static) \
  13730. static DEFINE_PER_CPU(struct srcu_struct_array, name##_srcu_array);\
  13731. - is_static struct srcu_struct name = __SRCU_STRUCT_INIT(name)
  13732. + is_static struct srcu_struct name = __SRCU_STRUCT_INIT(name, name##_srcu_array)
  13733. #define DEFINE_SRCU(name) __DEFINE_SRCU(name, /* not static */)
  13734. #define DEFINE_STATIC_SRCU(name) __DEFINE_SRCU(name, static)
  13735. diff -Nur linux-4.4.28.orig/include/linux/suspend.h linux-4.4.28/include/linux/suspend.h
  13736. --- linux-4.4.28.orig/include/linux/suspend.h 2016-10-28 09:53:25.000000000 +0200
  13737. +++ linux-4.4.28/include/linux/suspend.h 2016-11-01 21:26:58.748340476 +0100
  13738. @@ -194,6 +194,12 @@
  13739. void (*end)(void);
  13740. };
  13741. +#if defined(CONFIG_SUSPEND) || defined(CONFIG_HIBERNATION)
  13742. +extern bool pm_in_action;
  13743. +#else
  13744. +# define pm_in_action false
  13745. +#endif
  13746. +
  13747. #ifdef CONFIG_SUSPEND
  13748. /**
  13749. * suspend_set_ops - set platform dependent suspend operations
  13750. diff -Nur linux-4.4.28.orig/include/linux/swait.h linux-4.4.28/include/linux/swait.h
  13751. --- linux-4.4.28.orig/include/linux/swait.h 1970-01-01 01:00:00.000000000 +0100
  13752. +++ linux-4.4.28/include/linux/swait.h 2016-11-01 21:26:58.748340476 +0100
  13753. @@ -0,0 +1,173 @@
  13754. +#ifndef _LINUX_SWAIT_H
  13755. +#define _LINUX_SWAIT_H
  13756. +
  13757. +#include <linux/list.h>
  13758. +#include <linux/stddef.h>
  13759. +#include <linux/spinlock.h>
  13760. +#include <asm/current.h>
  13761. +
  13762. +/*
  13763. + * Simple wait queues
  13764. + *
  13765. + * While these are very similar to the other/complex wait queues (wait.h) the
  13766. + * most important difference is that the simple waitqueue allows for
  13767. + * deterministic behaviour -- IOW it has strictly bounded IRQ and lock hold
  13768. + * times.
  13769. + *
  13770. + * In order to make this so, we had to drop a fair number of features of the
  13771. + * other waitqueue code; notably:
  13772. + *
  13773. + * - mixing INTERRUPTIBLE and UNINTERRUPTIBLE sleeps on the same waitqueue;
  13774. + * all wakeups are TASK_NORMAL in order to avoid O(n) lookups for the right
  13775. + * sleeper state.
  13776. + *
  13777. + * - the exclusive mode; because this requires preserving the list order
  13778. + * and this is hard.
  13779. + *
  13780. + * - custom wake functions; because you cannot give any guarantees about
  13781. + * random code.
  13782. + *
  13783. + * As a side effect of this; the data structures are slimmer.
  13784. + *
  13785. + * One would recommend using this wait queue where possible.
  13786. + */
  13787. +
  13788. +struct task_struct;
  13789. +
  13790. +struct swait_queue_head {
  13791. + raw_spinlock_t lock;
  13792. + struct list_head task_list;
  13793. +};
  13794. +
  13795. +struct swait_queue {
  13796. + struct task_struct *task;
  13797. + struct list_head task_list;
  13798. +};
  13799. +
  13800. +#define __SWAITQUEUE_INITIALIZER(name) { \
  13801. + .task = current, \
  13802. + .task_list = LIST_HEAD_INIT((name).task_list), \
  13803. +}
  13804. +
  13805. +#define DECLARE_SWAITQUEUE(name) \
  13806. + struct swait_queue name = __SWAITQUEUE_INITIALIZER(name)
  13807. +
  13808. +#define __SWAIT_QUEUE_HEAD_INITIALIZER(name) { \
  13809. + .lock = __RAW_SPIN_LOCK_UNLOCKED(name.lock), \
  13810. + .task_list = LIST_HEAD_INIT((name).task_list), \
  13811. +}
  13812. +
  13813. +#define DECLARE_SWAIT_QUEUE_HEAD(name) \
  13814. + struct swait_queue_head name = __SWAIT_QUEUE_HEAD_INITIALIZER(name)
  13815. +
  13816. +extern void __init_swait_queue_head(struct swait_queue_head *q, const char *name,
  13817. + struct lock_class_key *key);
  13818. +
  13819. +#define init_swait_queue_head(q) \
  13820. + do { \
  13821. + static struct lock_class_key __key; \
  13822. + __init_swait_queue_head((q), #q, &__key); \
  13823. + } while (0)
  13824. +
  13825. +#ifdef CONFIG_LOCKDEP
  13826. +# define __SWAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
  13827. + ({ init_swait_queue_head(&name); name; })
  13828. +# define DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(name) \
  13829. + struct swait_queue_head name = __SWAIT_QUEUE_HEAD_INIT_ONSTACK(name)
  13830. +#else
  13831. +# define DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(name) \
  13832. + DECLARE_SWAIT_QUEUE_HEAD(name)
  13833. +#endif
  13834. +
  13835. +static inline int swait_active(struct swait_queue_head *q)
  13836. +{
  13837. + return !list_empty(&q->task_list);
  13838. +}
  13839. +
  13840. +extern void swake_up(struct swait_queue_head *q);
  13841. +extern void swake_up_all(struct swait_queue_head *q);
  13842. +extern void swake_up_locked(struct swait_queue_head *q);
  13843. +extern void swake_up_all_locked(struct swait_queue_head *q);
  13844. +
  13845. +extern void __prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait);
  13846. +extern void prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait, int state);
  13847. +extern long prepare_to_swait_event(struct swait_queue_head *q, struct swait_queue *wait, int state);
  13848. +
  13849. +extern void __finish_swait(struct swait_queue_head *q, struct swait_queue *wait);
  13850. +extern void finish_swait(struct swait_queue_head *q, struct swait_queue *wait);
  13851. +
  13852. +/* as per ___wait_event() but for swait, therefore "exclusive == 0" */
  13853. +#define ___swait_event(wq, condition, state, ret, cmd) \
  13854. +({ \
  13855. + struct swait_queue __wait; \
  13856. + long __ret = ret; \
  13857. + \
  13858. + INIT_LIST_HEAD(&__wait.task_list); \
  13859. + for (;;) { \
  13860. + long __int = prepare_to_swait_event(&wq, &__wait, state);\
  13861. + \
  13862. + if (condition) \
  13863. + break; \
  13864. + \
  13865. + if (___wait_is_interruptible(state) && __int) { \
  13866. + __ret = __int; \
  13867. + break; \
  13868. + } \
  13869. + \
  13870. + cmd; \
  13871. + } \
  13872. + finish_swait(&wq, &__wait); \
  13873. + __ret; \
  13874. +})
  13875. +
  13876. +#define __swait_event(wq, condition) \
  13877. + (void)___swait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, \
  13878. + schedule())
  13879. +
  13880. +#define swait_event(wq, condition) \
  13881. +do { \
  13882. + if (condition) \
  13883. + break; \
  13884. + __swait_event(wq, condition); \
  13885. +} while (0)
  13886. +
  13887. +#define __swait_event_timeout(wq, condition, timeout) \
  13888. + ___swait_event(wq, ___wait_cond_timeout(condition), \
  13889. + TASK_UNINTERRUPTIBLE, timeout, \
  13890. + __ret = schedule_timeout(__ret))
  13891. +
  13892. +#define swait_event_timeout(wq, condition, timeout) \
  13893. +({ \
  13894. + long __ret = timeout; \
  13895. + if (!___wait_cond_timeout(condition)) \
  13896. + __ret = __swait_event_timeout(wq, condition, timeout); \
  13897. + __ret; \
  13898. +})
  13899. +
  13900. +#define __swait_event_interruptible(wq, condition) \
  13901. + ___swait_event(wq, condition, TASK_INTERRUPTIBLE, 0, \
  13902. + schedule())
  13903. +
  13904. +#define swait_event_interruptible(wq, condition) \
  13905. +({ \
  13906. + int __ret = 0; \
  13907. + if (!(condition)) \
  13908. + __ret = __swait_event_interruptible(wq, condition); \
  13909. + __ret; \
  13910. +})
  13911. +
  13912. +#define __swait_event_interruptible_timeout(wq, condition, timeout) \
  13913. + ___swait_event(wq, ___wait_cond_timeout(condition), \
  13914. + TASK_INTERRUPTIBLE, timeout, \
  13915. + __ret = schedule_timeout(__ret))
  13916. +
  13917. +#define swait_event_interruptible_timeout(wq, condition, timeout) \
  13918. +({ \
  13919. + long __ret = timeout; \
  13920. + if (!___wait_cond_timeout(condition)) \
  13921. + __ret = __swait_event_interruptible_timeout(wq, \
  13922. + condition, timeout); \
  13923. + __ret; \
  13924. +})
  13925. +
  13926. +#endif /* _LINUX_SWAIT_H */
  13927. diff -Nur linux-4.4.28.orig/include/linux/swap.h linux-4.4.28/include/linux/swap.h
  13928. --- linux-4.4.28.orig/include/linux/swap.h 2016-10-28 09:53:25.000000000 +0200
  13929. +++ linux-4.4.28/include/linux/swap.h 2016-11-01 21:26:58.748340476 +0100
  13930. @@ -11,6 +11,7 @@
  13931. #include <linux/fs.h>
  13932. #include <linux/atomic.h>
  13933. #include <linux/page-flags.h>
  13934. +#include <linux/locallock.h>
  13935. #include <asm/page.h>
  13936. struct notifier_block;
  13937. @@ -252,7 +253,8 @@
  13938. void *workingset_eviction(struct address_space *mapping, struct page *page);
  13939. bool workingset_refault(void *shadow);
  13940. void workingset_activation(struct page *page);
  13941. -extern struct list_lru workingset_shadow_nodes;
  13942. +extern struct list_lru __workingset_shadow_nodes;
  13943. +DECLARE_LOCAL_IRQ_LOCK(workingset_shadow_lock);
  13944. static inline unsigned int workingset_node_pages(struct radix_tree_node *node)
  13945. {
  13946. @@ -298,6 +300,7 @@
  13947. /* linux/mm/swap.c */
  13948. +DECLARE_LOCAL_IRQ_LOCK(swapvec_lock);
  13949. extern void lru_cache_add(struct page *);
  13950. extern void lru_cache_add_anon(struct page *page);
  13951. extern void lru_cache_add_file(struct page *page);
  13952. diff -Nur linux-4.4.28.orig/include/linux/swork.h linux-4.4.28/include/linux/swork.h
  13953. --- linux-4.4.28.orig/include/linux/swork.h 1970-01-01 01:00:00.000000000 +0100
  13954. +++ linux-4.4.28/include/linux/swork.h 2016-11-01 21:26:58.748340476 +0100
  13955. @@ -0,0 +1,24 @@
  13956. +#ifndef _LINUX_SWORK_H
  13957. +#define _LINUX_SWORK_H
  13958. +
  13959. +#include <linux/list.h>
  13960. +
  13961. +struct swork_event {
  13962. + struct list_head item;
  13963. + unsigned long flags;
  13964. + void (*func)(struct swork_event *);
  13965. +};
  13966. +
  13967. +static inline void INIT_SWORK(struct swork_event *event,
  13968. + void (*func)(struct swork_event *))
  13969. +{
  13970. + event->flags = 0;
  13971. + event->func = func;
  13972. +}
  13973. +
  13974. +bool swork_queue(struct swork_event *sev);
  13975. +
  13976. +int swork_get(void);
  13977. +void swork_put(void);
  13978. +
  13979. +#endif /* _LINUX_SWORK_H */
  13980. diff -Nur linux-4.4.28.orig/include/linux/thread_info.h linux-4.4.28/include/linux/thread_info.h
  13981. --- linux-4.4.28.orig/include/linux/thread_info.h 2016-10-28 09:53:25.000000000 +0200
  13982. +++ linux-4.4.28/include/linux/thread_info.h 2016-11-01 21:26:58.748340476 +0100
  13983. @@ -102,7 +102,17 @@
  13984. #define test_thread_flag(flag) \
  13985. test_ti_thread_flag(current_thread_info(), flag)
  13986. -#define tif_need_resched() test_thread_flag(TIF_NEED_RESCHED)
  13987. +#ifdef CONFIG_PREEMPT_LAZY
  13988. +#define tif_need_resched() (test_thread_flag(TIF_NEED_RESCHED) || \
  13989. + test_thread_flag(TIF_NEED_RESCHED_LAZY))
  13990. +#define tif_need_resched_now() (test_thread_flag(TIF_NEED_RESCHED))
  13991. +#define tif_need_resched_lazy() test_thread_flag(TIF_NEED_RESCHED_LAZY))
  13992. +
  13993. +#else
  13994. +#define tif_need_resched() test_thread_flag(TIF_NEED_RESCHED)
  13995. +#define tif_need_resched_now() test_thread_flag(TIF_NEED_RESCHED)
  13996. +#define tif_need_resched_lazy() 0
  13997. +#endif
  13998. #if defined TIF_RESTORE_SIGMASK && !defined HAVE_SET_RESTORE_SIGMASK
  13999. /*
  14000. diff -Nur linux-4.4.28.orig/include/linux/timer.h linux-4.4.28/include/linux/timer.h
  14001. --- linux-4.4.28.orig/include/linux/timer.h 2016-10-28 09:53:25.000000000 +0200
  14002. +++ linux-4.4.28/include/linux/timer.h 2016-11-01 21:26:58.748340476 +0100
  14003. @@ -225,7 +225,7 @@
  14004. extern int try_to_del_timer_sync(struct timer_list *timer);
  14005. -#ifdef CONFIG_SMP
  14006. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  14007. extern int del_timer_sync(struct timer_list *timer);
  14008. #else
  14009. # define del_timer_sync(t) del_timer(t)
  14010. diff -Nur linux-4.4.28.orig/include/linux/trace_events.h linux-4.4.28/include/linux/trace_events.h
  14011. --- linux-4.4.28.orig/include/linux/trace_events.h 2016-10-28 09:53:25.000000000 +0200
  14012. +++ linux-4.4.28/include/linux/trace_events.h 2016-11-01 21:26:58.748340476 +0100
  14013. @@ -66,6 +66,9 @@
  14014. unsigned char flags;
  14015. unsigned char preempt_count;
  14016. int pid;
  14017. + unsigned short migrate_disable;
  14018. + unsigned short padding;
  14019. + unsigned char preempt_lazy_count;
  14020. };
  14021. #define TRACE_EVENT_TYPE_MAX \
  14022. diff -Nur linux-4.4.28.orig/include/linux/uaccess.h linux-4.4.28/include/linux/uaccess.h
  14023. --- linux-4.4.28.orig/include/linux/uaccess.h 2016-10-28 09:53:25.000000000 +0200
  14024. +++ linux-4.4.28/include/linux/uaccess.h 2016-11-01 21:26:58.748340476 +0100
  14025. @@ -24,6 +24,7 @@
  14026. */
  14027. static inline void pagefault_disable(void)
  14028. {
  14029. + migrate_disable();
  14030. pagefault_disabled_inc();
  14031. /*
  14032. * make sure to have issued the store before a pagefault
  14033. @@ -40,6 +41,7 @@
  14034. */
  14035. barrier();
  14036. pagefault_disabled_dec();
  14037. + migrate_enable();
  14038. }
  14039. /*
  14040. diff -Nur linux-4.4.28.orig/include/linux/uprobes.h linux-4.4.28/include/linux/uprobes.h
  14041. --- linux-4.4.28.orig/include/linux/uprobes.h 2016-10-28 09:53:25.000000000 +0200
  14042. +++ linux-4.4.28/include/linux/uprobes.h 2016-11-01 21:26:58.748340476 +0100
  14043. @@ -27,6 +27,7 @@
  14044. #include <linux/errno.h>
  14045. #include <linux/rbtree.h>
  14046. #include <linux/types.h>
  14047. +#include <linux/wait.h>
  14048. struct vm_area_struct;
  14049. struct mm_struct;
  14050. diff -Nur linux-4.4.28.orig/include/linux/vmstat.h linux-4.4.28/include/linux/vmstat.h
  14051. --- linux-4.4.28.orig/include/linux/vmstat.h 2016-10-28 09:53:25.000000000 +0200
  14052. +++ linux-4.4.28/include/linux/vmstat.h 2016-11-01 21:26:58.748340476 +0100
  14053. @@ -33,7 +33,9 @@
  14054. */
  14055. static inline void __count_vm_event(enum vm_event_item item)
  14056. {
  14057. + preempt_disable_rt();
  14058. raw_cpu_inc(vm_event_states.event[item]);
  14059. + preempt_enable_rt();
  14060. }
  14061. static inline void count_vm_event(enum vm_event_item item)
  14062. @@ -43,7 +45,9 @@
  14063. static inline void __count_vm_events(enum vm_event_item item, long delta)
  14064. {
  14065. + preempt_disable_rt();
  14066. raw_cpu_add(vm_event_states.event[item], delta);
  14067. + preempt_enable_rt();
  14068. }
  14069. static inline void count_vm_events(enum vm_event_item item, long delta)
  14070. diff -Nur linux-4.4.28.orig/include/linux/wait.h linux-4.4.28/include/linux/wait.h
  14071. --- linux-4.4.28.orig/include/linux/wait.h 2016-10-28 09:53:25.000000000 +0200
  14072. +++ linux-4.4.28/include/linux/wait.h 2016-11-01 21:26:58.748340476 +0100
  14073. @@ -8,6 +8,7 @@
  14074. #include <linux/spinlock.h>
  14075. #include <asm/current.h>
  14076. #include <uapi/linux/wait.h>
  14077. +#include <linux/atomic.h>
  14078. typedef struct __wait_queue wait_queue_t;
  14079. typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key);
  14080. diff -Nur linux-4.4.28.orig/include/net/dst.h linux-4.4.28/include/net/dst.h
  14081. --- linux-4.4.28.orig/include/net/dst.h 2016-10-28 09:53:25.000000000 +0200
  14082. +++ linux-4.4.28/include/net/dst.h 2016-11-01 21:26:58.748340476 +0100
  14083. @@ -437,7 +437,7 @@
  14084. static inline int dst_neigh_output(struct dst_entry *dst, struct neighbour *n,
  14085. struct sk_buff *skb)
  14086. {
  14087. - const struct hh_cache *hh;
  14088. + struct hh_cache *hh;
  14089. if (dst->pending_confirm) {
  14090. unsigned long now = jiffies;
  14091. diff -Nur linux-4.4.28.orig/include/net/neighbour.h linux-4.4.28/include/net/neighbour.h
  14092. --- linux-4.4.28.orig/include/net/neighbour.h 2016-10-28 09:53:25.000000000 +0200
  14093. +++ linux-4.4.28/include/net/neighbour.h 2016-11-01 21:26:58.748340476 +0100
  14094. @@ -446,7 +446,7 @@
  14095. }
  14096. #endif
  14097. -static inline int neigh_hh_output(const struct hh_cache *hh, struct sk_buff *skb)
  14098. +static inline int neigh_hh_output(struct hh_cache *hh, struct sk_buff *skb)
  14099. {
  14100. unsigned int seq;
  14101. int hh_len;
  14102. @@ -501,7 +501,7 @@
  14103. #define NEIGH_CB(skb) ((struct neighbour_cb *)(skb)->cb)
  14104. -static inline void neigh_ha_snapshot(char *dst, const struct neighbour *n,
  14105. +static inline void neigh_ha_snapshot(char *dst, struct neighbour *n,
  14106. const struct net_device *dev)
  14107. {
  14108. unsigned int seq;
  14109. diff -Nur linux-4.4.28.orig/include/net/netns/ipv4.h linux-4.4.28/include/net/netns/ipv4.h
  14110. --- linux-4.4.28.orig/include/net/netns/ipv4.h 2016-10-28 09:53:25.000000000 +0200
  14111. +++ linux-4.4.28/include/net/netns/ipv4.h 2016-11-01 21:26:58.748340476 +0100
  14112. @@ -70,6 +70,7 @@
  14113. int sysctl_icmp_echo_ignore_all;
  14114. int sysctl_icmp_echo_ignore_broadcasts;
  14115. + int sysctl_icmp_echo_sysrq;
  14116. int sysctl_icmp_ignore_bogus_error_responses;
  14117. int sysctl_icmp_ratelimit;
  14118. int sysctl_icmp_ratemask;
  14119. diff -Nur linux-4.4.28.orig/include/trace/events/hist.h linux-4.4.28/include/trace/events/hist.h
  14120. --- linux-4.4.28.orig/include/trace/events/hist.h 1970-01-01 01:00:00.000000000 +0100
  14121. +++ linux-4.4.28/include/trace/events/hist.h 2016-11-01 21:26:58.748340476 +0100
  14122. @@ -0,0 +1,73 @@
  14123. +#undef TRACE_SYSTEM
  14124. +#define TRACE_SYSTEM hist
  14125. +
  14126. +#if !defined(_TRACE_HIST_H) || defined(TRACE_HEADER_MULTI_READ)
  14127. +#define _TRACE_HIST_H
  14128. +
  14129. +#include "latency_hist.h"
  14130. +#include <linux/tracepoint.h>
  14131. +
  14132. +#if !defined(CONFIG_PREEMPT_OFF_HIST) && !defined(CONFIG_INTERRUPT_OFF_HIST)
  14133. +#define trace_preemptirqsoff_hist(a, b)
  14134. +#define trace_preemptirqsoff_hist_rcuidle(a, b)
  14135. +#else
  14136. +TRACE_EVENT(preemptirqsoff_hist,
  14137. +
  14138. + TP_PROTO(int reason, int starthist),
  14139. +
  14140. + TP_ARGS(reason, starthist),
  14141. +
  14142. + TP_STRUCT__entry(
  14143. + __field(int, reason)
  14144. + __field(int, starthist)
  14145. + ),
  14146. +
  14147. + TP_fast_assign(
  14148. + __entry->reason = reason;
  14149. + __entry->starthist = starthist;
  14150. + ),
  14151. +
  14152. + TP_printk("reason=%s starthist=%s", getaction(__entry->reason),
  14153. + __entry->starthist ? "start" : "stop")
  14154. +);
  14155. +#endif
  14156. +
  14157. +#ifndef CONFIG_MISSED_TIMER_OFFSETS_HIST
  14158. +#define trace_hrtimer_interrupt(a, b, c, d)
  14159. +#else
  14160. +TRACE_EVENT(hrtimer_interrupt,
  14161. +
  14162. + TP_PROTO(int cpu, long long offset, struct task_struct *curr,
  14163. + struct task_struct *task),
  14164. +
  14165. + TP_ARGS(cpu, offset, curr, task),
  14166. +
  14167. + TP_STRUCT__entry(
  14168. + __field(int, cpu)
  14169. + __field(long long, offset)
  14170. + __array(char, ccomm, TASK_COMM_LEN)
  14171. + __field(int, cprio)
  14172. + __array(char, tcomm, TASK_COMM_LEN)
  14173. + __field(int, tprio)
  14174. + ),
  14175. +
  14176. + TP_fast_assign(
  14177. + __entry->cpu = cpu;
  14178. + __entry->offset = offset;
  14179. + memcpy(__entry->ccomm, curr->comm, TASK_COMM_LEN);
  14180. + __entry->cprio = curr->prio;
  14181. + memcpy(__entry->tcomm, task != NULL ? task->comm : "<none>",
  14182. + task != NULL ? TASK_COMM_LEN : 7);
  14183. + __entry->tprio = task != NULL ? task->prio : -1;
  14184. + ),
  14185. +
  14186. + TP_printk("cpu=%d offset=%lld curr=%s[%d] thread=%s[%d]",
  14187. + __entry->cpu, __entry->offset, __entry->ccomm,
  14188. + __entry->cprio, __entry->tcomm, __entry->tprio)
  14189. +);
  14190. +#endif
  14191. +
  14192. +#endif /* _TRACE_HIST_H */
  14193. +
  14194. +/* This part must be outside protection */
  14195. +#include <trace/define_trace.h>
  14196. diff -Nur linux-4.4.28.orig/include/trace/events/latency_hist.h linux-4.4.28/include/trace/events/latency_hist.h
  14197. --- linux-4.4.28.orig/include/trace/events/latency_hist.h 1970-01-01 01:00:00.000000000 +0100
  14198. +++ linux-4.4.28/include/trace/events/latency_hist.h 2016-11-01 21:26:58.752340732 +0100
  14199. @@ -0,0 +1,29 @@
  14200. +#ifndef _LATENCY_HIST_H
  14201. +#define _LATENCY_HIST_H
  14202. +
  14203. +enum hist_action {
  14204. + IRQS_ON,
  14205. + PREEMPT_ON,
  14206. + TRACE_STOP,
  14207. + IRQS_OFF,
  14208. + PREEMPT_OFF,
  14209. + TRACE_START,
  14210. +};
  14211. +
  14212. +static char *actions[] = {
  14213. + "IRQS_ON",
  14214. + "PREEMPT_ON",
  14215. + "TRACE_STOP",
  14216. + "IRQS_OFF",
  14217. + "PREEMPT_OFF",
  14218. + "TRACE_START",
  14219. +};
  14220. +
  14221. +static inline char *getaction(int action)
  14222. +{
  14223. + if (action >= 0 && action <= sizeof(actions)/sizeof(actions[0]))
  14224. + return actions[action];
  14225. + return "unknown";
  14226. +}
  14227. +
  14228. +#endif /* _LATENCY_HIST_H */
  14229. diff -Nur linux-4.4.28.orig/include/trace/events/writeback.h linux-4.4.28/include/trace/events/writeback.h
  14230. --- linux-4.4.28.orig/include/trace/events/writeback.h 2016-10-28 09:53:25.000000000 +0200
  14231. +++ linux-4.4.28/include/trace/events/writeback.h 2016-11-01 21:26:58.752340732 +0100
  14232. @@ -134,58 +134,28 @@
  14233. #ifdef CREATE_TRACE_POINTS
  14234. #ifdef CONFIG_CGROUP_WRITEBACK
  14235. -static inline size_t __trace_wb_cgroup_size(struct bdi_writeback *wb)
  14236. +static inline unsigned int __trace_wb_assign_cgroup(struct bdi_writeback *wb)
  14237. {
  14238. - return kernfs_path_len(wb->memcg_css->cgroup->kn) + 1;
  14239. + return wb->memcg_css->cgroup->kn->ino;
  14240. }
  14241. -static inline void __trace_wb_assign_cgroup(char *buf, struct bdi_writeback *wb)
  14242. -{
  14243. - struct cgroup *cgrp = wb->memcg_css->cgroup;
  14244. - char *path;
  14245. -
  14246. - path = cgroup_path(cgrp, buf, kernfs_path_len(cgrp->kn) + 1);
  14247. - WARN_ON_ONCE(path != buf);
  14248. -}
  14249. -
  14250. -static inline size_t __trace_wbc_cgroup_size(struct writeback_control *wbc)
  14251. -{
  14252. - if (wbc->wb)
  14253. - return __trace_wb_cgroup_size(wbc->wb);
  14254. - else
  14255. - return 2;
  14256. -}
  14257. -
  14258. -static inline void __trace_wbc_assign_cgroup(char *buf,
  14259. - struct writeback_control *wbc)
  14260. +static inline unsigned int __trace_wbc_assign_cgroup(struct writeback_control *wbc)
  14261. {
  14262. if (wbc->wb)
  14263. - __trace_wb_assign_cgroup(buf, wbc->wb);
  14264. + return __trace_wb_assign_cgroup(wbc->wb);
  14265. else
  14266. - strcpy(buf, "/");
  14267. + return -1U;
  14268. }
  14269. -
  14270. #else /* CONFIG_CGROUP_WRITEBACK */
  14271. -static inline size_t __trace_wb_cgroup_size(struct bdi_writeback *wb)
  14272. -{
  14273. - return 2;
  14274. -}
  14275. -
  14276. -static inline void __trace_wb_assign_cgroup(char *buf, struct bdi_writeback *wb)
  14277. -{
  14278. - strcpy(buf, "/");
  14279. -}
  14280. -
  14281. -static inline size_t __trace_wbc_cgroup_size(struct writeback_control *wbc)
  14282. +static inline unsigned int __trace_wb_assign_cgroup(struct bdi_writeback *wb)
  14283. {
  14284. - return 2;
  14285. + return -1U;
  14286. }
  14287. -static inline void __trace_wbc_assign_cgroup(char *buf,
  14288. - struct writeback_control *wbc)
  14289. +static inline unsigned int __trace_wbc_assign_cgroup(struct writeback_control *wbc)
  14290. {
  14291. - strcpy(buf, "/");
  14292. + return -1U;
  14293. }
  14294. #endif /* CONFIG_CGROUP_WRITEBACK */
  14295. @@ -201,7 +171,7 @@
  14296. __array(char, name, 32)
  14297. __field(unsigned long, ino)
  14298. __field(int, sync_mode)
  14299. - __dynamic_array(char, cgroup, __trace_wbc_cgroup_size(wbc))
  14300. + __field(unsigned int, cgroup_ino)
  14301. ),
  14302. TP_fast_assign(
  14303. @@ -209,14 +179,14 @@
  14304. dev_name(inode_to_bdi(inode)->dev), 32);
  14305. __entry->ino = inode->i_ino;
  14306. __entry->sync_mode = wbc->sync_mode;
  14307. - __trace_wbc_assign_cgroup(__get_str(cgroup), wbc);
  14308. + __entry->cgroup_ino = __trace_wbc_assign_cgroup(wbc);
  14309. ),
  14310. - TP_printk("bdi %s: ino=%lu sync_mode=%d cgroup=%s",
  14311. + TP_printk("bdi %s: ino=%lu sync_mode=%d cgroup_ino=%u",
  14312. __entry->name,
  14313. __entry->ino,
  14314. __entry->sync_mode,
  14315. - __get_str(cgroup)
  14316. + __entry->cgroup_ino
  14317. )
  14318. );
  14319. @@ -246,7 +216,7 @@
  14320. __field(int, range_cyclic)
  14321. __field(int, for_background)
  14322. __field(int, reason)
  14323. - __dynamic_array(char, cgroup, __trace_wb_cgroup_size(wb))
  14324. + __field(unsigned int, cgroup_ino)
  14325. ),
  14326. TP_fast_assign(
  14327. strncpy(__entry->name,
  14328. @@ -258,10 +228,10 @@
  14329. __entry->range_cyclic = work->range_cyclic;
  14330. __entry->for_background = work->for_background;
  14331. __entry->reason = work->reason;
  14332. - __trace_wb_assign_cgroup(__get_str(cgroup), wb);
  14333. + __entry->cgroup_ino = __trace_wb_assign_cgroup(wb);
  14334. ),
  14335. TP_printk("bdi %s: sb_dev %d:%d nr_pages=%ld sync_mode=%d "
  14336. - "kupdate=%d range_cyclic=%d background=%d reason=%s cgroup=%s",
  14337. + "kupdate=%d range_cyclic=%d background=%d reason=%s cgroup_ino=%u",
  14338. __entry->name,
  14339. MAJOR(__entry->sb_dev), MINOR(__entry->sb_dev),
  14340. __entry->nr_pages,
  14341. @@ -270,7 +240,7 @@
  14342. __entry->range_cyclic,
  14343. __entry->for_background,
  14344. __print_symbolic(__entry->reason, WB_WORK_REASON),
  14345. - __get_str(cgroup)
  14346. + __entry->cgroup_ino
  14347. )
  14348. );
  14349. #define DEFINE_WRITEBACK_WORK_EVENT(name) \
  14350. @@ -300,15 +270,15 @@
  14351. TP_ARGS(wb),
  14352. TP_STRUCT__entry(
  14353. __array(char, name, 32)
  14354. - __dynamic_array(char, cgroup, __trace_wb_cgroup_size(wb))
  14355. + __field(unsigned int, cgroup_ino)
  14356. ),
  14357. TP_fast_assign(
  14358. strncpy(__entry->name, dev_name(wb->bdi->dev), 32);
  14359. - __trace_wb_assign_cgroup(__get_str(cgroup), wb);
  14360. + __entry->cgroup_ino = __trace_wb_assign_cgroup(wb);
  14361. ),
  14362. - TP_printk("bdi %s: cgroup=%s",
  14363. + TP_printk("bdi %s: cgroup_ino=%u",
  14364. __entry->name,
  14365. - __get_str(cgroup)
  14366. + __entry->cgroup_ino
  14367. )
  14368. );
  14369. #define DEFINE_WRITEBACK_EVENT(name) \
  14370. @@ -347,7 +317,7 @@
  14371. __field(int, range_cyclic)
  14372. __field(long, range_start)
  14373. __field(long, range_end)
  14374. - __dynamic_array(char, cgroup, __trace_wbc_cgroup_size(wbc))
  14375. + __field(unsigned int, cgroup_ino)
  14376. ),
  14377. TP_fast_assign(
  14378. @@ -361,12 +331,12 @@
  14379. __entry->range_cyclic = wbc->range_cyclic;
  14380. __entry->range_start = (long)wbc->range_start;
  14381. __entry->range_end = (long)wbc->range_end;
  14382. - __trace_wbc_assign_cgroup(__get_str(cgroup), wbc);
  14383. + __entry->cgroup_ino = __trace_wbc_assign_cgroup(wbc);
  14384. ),
  14385. TP_printk("bdi %s: towrt=%ld skip=%ld mode=%d kupd=%d "
  14386. "bgrd=%d reclm=%d cyclic=%d "
  14387. - "start=0x%lx end=0x%lx cgroup=%s",
  14388. + "start=0x%lx end=0x%lx cgroup_ino=%u",
  14389. __entry->name,
  14390. __entry->nr_to_write,
  14391. __entry->pages_skipped,
  14392. @@ -377,7 +347,7 @@
  14393. __entry->range_cyclic,
  14394. __entry->range_start,
  14395. __entry->range_end,
  14396. - __get_str(cgroup)
  14397. + __entry->cgroup_ino
  14398. )
  14399. )
  14400. @@ -398,7 +368,7 @@
  14401. __field(long, age)
  14402. __field(int, moved)
  14403. __field(int, reason)
  14404. - __dynamic_array(char, cgroup, __trace_wb_cgroup_size(wb))
  14405. + __field(unsigned int, cgroup_ino)
  14406. ),
  14407. TP_fast_assign(
  14408. unsigned long *older_than_this = work->older_than_this;
  14409. @@ -408,15 +378,15 @@
  14410. (jiffies - *older_than_this) * 1000 / HZ : -1;
  14411. __entry->moved = moved;
  14412. __entry->reason = work->reason;
  14413. - __trace_wb_assign_cgroup(__get_str(cgroup), wb);
  14414. + __entry->cgroup_ino = __trace_wb_assign_cgroup(wb);
  14415. ),
  14416. - TP_printk("bdi %s: older=%lu age=%ld enqueue=%d reason=%s cgroup=%s",
  14417. + TP_printk("bdi %s: older=%lu age=%ld enqueue=%d reason=%s cgroup_ino=%u",
  14418. __entry->name,
  14419. __entry->older, /* older_than_this in jiffies */
  14420. __entry->age, /* older_than_this in relative milliseconds */
  14421. __entry->moved,
  14422. __print_symbolic(__entry->reason, WB_WORK_REASON),
  14423. - __get_str(cgroup)
  14424. + __entry->cgroup_ino
  14425. )
  14426. );
  14427. @@ -484,7 +454,7 @@
  14428. __field(unsigned long, dirty_ratelimit)
  14429. __field(unsigned long, task_ratelimit)
  14430. __field(unsigned long, balanced_dirty_ratelimit)
  14431. - __dynamic_array(char, cgroup, __trace_wb_cgroup_size(wb))
  14432. + __field(unsigned int, cgroup_ino)
  14433. ),
  14434. TP_fast_assign(
  14435. @@ -496,13 +466,13 @@
  14436. __entry->task_ratelimit = KBps(task_ratelimit);
  14437. __entry->balanced_dirty_ratelimit =
  14438. KBps(wb->balanced_dirty_ratelimit);
  14439. - __trace_wb_assign_cgroup(__get_str(cgroup), wb);
  14440. + __entry->cgroup_ino = __trace_wb_assign_cgroup(wb);
  14441. ),
  14442. TP_printk("bdi %s: "
  14443. "write_bw=%lu awrite_bw=%lu dirty_rate=%lu "
  14444. "dirty_ratelimit=%lu task_ratelimit=%lu "
  14445. - "balanced_dirty_ratelimit=%lu cgroup=%s",
  14446. + "balanced_dirty_ratelimit=%lu cgroup_ino=%u",
  14447. __entry->bdi,
  14448. __entry->write_bw, /* write bandwidth */
  14449. __entry->avg_write_bw, /* avg write bandwidth */
  14450. @@ -510,7 +480,7 @@
  14451. __entry->dirty_ratelimit, /* base ratelimit */
  14452. __entry->task_ratelimit, /* ratelimit with position control */
  14453. __entry->balanced_dirty_ratelimit, /* the balanced ratelimit */
  14454. - __get_str(cgroup)
  14455. + __entry->cgroup_ino
  14456. )
  14457. );
  14458. @@ -548,7 +518,7 @@
  14459. __field( long, pause)
  14460. __field(unsigned long, period)
  14461. __field( long, think)
  14462. - __dynamic_array(char, cgroup, __trace_wb_cgroup_size(wb))
  14463. + __field(unsigned int, cgroup_ino)
  14464. ),
  14465. TP_fast_assign(
  14466. @@ -571,7 +541,7 @@
  14467. __entry->period = period * 1000 / HZ;
  14468. __entry->pause = pause * 1000 / HZ;
  14469. __entry->paused = (jiffies - start_time) * 1000 / HZ;
  14470. - __trace_wb_assign_cgroup(__get_str(cgroup), wb);
  14471. + __entry->cgroup_ino = __trace_wb_assign_cgroup(wb);
  14472. ),
  14473. @@ -580,7 +550,7 @@
  14474. "bdi_setpoint=%lu bdi_dirty=%lu "
  14475. "dirty_ratelimit=%lu task_ratelimit=%lu "
  14476. "dirtied=%u dirtied_pause=%u "
  14477. - "paused=%lu pause=%ld period=%lu think=%ld cgroup=%s",
  14478. + "paused=%lu pause=%ld period=%lu think=%ld cgroup_ino=%u",
  14479. __entry->bdi,
  14480. __entry->limit,
  14481. __entry->setpoint,
  14482. @@ -595,7 +565,7 @@
  14483. __entry->pause, /* ms */
  14484. __entry->period, /* ms */
  14485. __entry->think, /* ms */
  14486. - __get_str(cgroup)
  14487. + __entry->cgroup_ino
  14488. )
  14489. );
  14490. @@ -609,8 +579,7 @@
  14491. __field(unsigned long, ino)
  14492. __field(unsigned long, state)
  14493. __field(unsigned long, dirtied_when)
  14494. - __dynamic_array(char, cgroup,
  14495. - __trace_wb_cgroup_size(inode_to_wb(inode)))
  14496. + __field(unsigned int, cgroup_ino)
  14497. ),
  14498. TP_fast_assign(
  14499. @@ -619,16 +588,16 @@
  14500. __entry->ino = inode->i_ino;
  14501. __entry->state = inode->i_state;
  14502. __entry->dirtied_when = inode->dirtied_when;
  14503. - __trace_wb_assign_cgroup(__get_str(cgroup), inode_to_wb(inode));
  14504. + __entry->cgroup_ino = __trace_wb_assign_cgroup(inode_to_wb(inode));
  14505. ),
  14506. - TP_printk("bdi %s: ino=%lu state=%s dirtied_when=%lu age=%lu cgroup=%s",
  14507. + TP_printk("bdi %s: ino=%lu state=%s dirtied_when=%lu age=%lu cgroup_ino=%u",
  14508. __entry->name,
  14509. __entry->ino,
  14510. show_inode_state(__entry->state),
  14511. __entry->dirtied_when,
  14512. (jiffies - __entry->dirtied_when) / HZ,
  14513. - __get_str(cgroup)
  14514. + __entry->cgroup_ino
  14515. )
  14516. );
  14517. @@ -684,7 +653,7 @@
  14518. __field(unsigned long, writeback_index)
  14519. __field(long, nr_to_write)
  14520. __field(unsigned long, wrote)
  14521. - __dynamic_array(char, cgroup, __trace_wbc_cgroup_size(wbc))
  14522. + __field(unsigned int, cgroup_ino)
  14523. ),
  14524. TP_fast_assign(
  14525. @@ -696,11 +665,11 @@
  14526. __entry->writeback_index = inode->i_mapping->writeback_index;
  14527. __entry->nr_to_write = nr_to_write;
  14528. __entry->wrote = nr_to_write - wbc->nr_to_write;
  14529. - __trace_wbc_assign_cgroup(__get_str(cgroup), wbc);
  14530. + __entry->cgroup_ino = __trace_wbc_assign_cgroup(wbc);
  14531. ),
  14532. TP_printk("bdi %s: ino=%lu state=%s dirtied_when=%lu age=%lu "
  14533. - "index=%lu to_write=%ld wrote=%lu cgroup=%s",
  14534. + "index=%lu to_write=%ld wrote=%lu cgroup_ino=%u",
  14535. __entry->name,
  14536. __entry->ino,
  14537. show_inode_state(__entry->state),
  14538. @@ -709,7 +678,7 @@
  14539. __entry->writeback_index,
  14540. __entry->nr_to_write,
  14541. __entry->wrote,
  14542. - __get_str(cgroup)
  14543. + __entry->cgroup_ino
  14544. )
  14545. );
  14546. diff -Nur linux-4.4.28.orig/init/Kconfig linux-4.4.28/init/Kconfig
  14547. --- linux-4.4.28.orig/init/Kconfig 2016-10-28 09:53:25.000000000 +0200
  14548. +++ linux-4.4.28/init/Kconfig 2016-11-01 21:26:58.752340732 +0100
  14549. @@ -498,7 +498,7 @@
  14550. config RCU_EXPERT
  14551. bool "Make expert-level adjustments to RCU configuration"
  14552. - default n
  14553. + default y if PREEMPT_RT_FULL
  14554. help
  14555. This option needs to be enabled if you wish to make
  14556. expert-level adjustments to RCU configuration. By default,
  14557. @@ -614,7 +614,7 @@
  14558. config RCU_FAST_NO_HZ
  14559. bool "Accelerate last non-dyntick-idle CPU's grace periods"
  14560. - depends on NO_HZ_COMMON && SMP && RCU_EXPERT
  14561. + depends on NO_HZ_COMMON && SMP && RCU_EXPERT && !PREEMPT_RT_FULL
  14562. default n
  14563. help
  14564. This option permits CPUs to enter dynticks-idle state even if
  14565. @@ -641,7 +641,7 @@
  14566. config RCU_BOOST
  14567. bool "Enable RCU priority boosting"
  14568. depends on RT_MUTEXES && PREEMPT_RCU && RCU_EXPERT
  14569. - default n
  14570. + default y if PREEMPT_RT_FULL
  14571. help
  14572. This option boosts the priority of preempted RCU readers that
  14573. block the current preemptible RCU grace period for too long.
  14574. @@ -1106,6 +1106,7 @@
  14575. config RT_GROUP_SCHED
  14576. bool "Group scheduling for SCHED_RR/FIFO"
  14577. depends on CGROUP_SCHED
  14578. + depends on !PREEMPT_RT_FULL
  14579. default n
  14580. help
  14581. This feature lets you explicitly allocate real CPU bandwidth
  14582. @@ -1719,6 +1720,7 @@
  14583. config SLAB
  14584. bool "SLAB"
  14585. + depends on !PREEMPT_RT_FULL
  14586. help
  14587. The regular slab allocator that is established and known to work
  14588. well in all environments. It organizes cache hot objects in
  14589. @@ -1737,6 +1739,7 @@
  14590. config SLOB
  14591. depends on EXPERT
  14592. bool "SLOB (Simple Allocator)"
  14593. + depends on !PREEMPT_RT_FULL
  14594. help
  14595. SLOB replaces the stock allocator with a drastically simpler
  14596. allocator. SLOB is generally more space efficient but
  14597. @@ -1746,7 +1749,7 @@
  14598. config SLUB_CPU_PARTIAL
  14599. default y
  14600. - depends on SLUB && SMP
  14601. + depends on SLUB && SMP && !PREEMPT_RT_FULL
  14602. bool "SLUB per cpu partial cache"
  14603. help
  14604. Per cpu partial caches accellerate objects allocation and freeing
  14605. diff -Nur linux-4.4.28.orig/init/main.c linux-4.4.28/init/main.c
  14606. --- linux-4.4.28.orig/init/main.c 2016-10-28 09:53:25.000000000 +0200
  14607. +++ linux-4.4.28/init/main.c 2016-11-01 21:26:58.752340732 +0100
  14608. @@ -530,6 +530,7 @@
  14609. setup_command_line(command_line);
  14610. setup_nr_cpu_ids();
  14611. setup_per_cpu_areas();
  14612. + softirq_early_init();
  14613. smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */
  14614. build_all_zonelists(NULL, NULL);
  14615. diff -Nur linux-4.4.28.orig/init/Makefile linux-4.4.28/init/Makefile
  14616. --- linux-4.4.28.orig/init/Makefile 2016-10-28 09:53:25.000000000 +0200
  14617. +++ linux-4.4.28/init/Makefile 2016-11-01 21:26:58.752340732 +0100
  14618. @@ -33,4 +33,4 @@
  14619. include/generated/compile.h: FORCE
  14620. @$($(quiet)chk_compile.h)
  14621. $(Q)$(CONFIG_SHELL) $(srctree)/scripts/mkcompile_h $@ \
  14622. - "$(UTS_MACHINE)" "$(CONFIG_SMP)" "$(CONFIG_PREEMPT)" "$(CC) $(KBUILD_CFLAGS)"
  14623. + "$(UTS_MACHINE)" "$(CONFIG_SMP)" "$(CONFIG_PREEMPT)" "$(CONFIG_PREEMPT_RT_FULL)" "$(CC) $(KBUILD_CFLAGS)"
  14624. diff -Nur linux-4.4.28.orig/ipc/msg.c linux-4.4.28/ipc/msg.c
  14625. --- linux-4.4.28.orig/ipc/msg.c 2016-10-28 09:53:25.000000000 +0200
  14626. +++ linux-4.4.28/ipc/msg.c 2016-11-01 21:26:58.752340732 +0100
  14627. @@ -183,20 +183,14 @@
  14628. }
  14629. }
  14630. -static void expunge_all(struct msg_queue *msq, int res)
  14631. +static void expunge_all(struct msg_queue *msq, int res,
  14632. + struct wake_q_head *wake_q)
  14633. {
  14634. struct msg_receiver *msr, *t;
  14635. list_for_each_entry_safe(msr, t, &msq->q_receivers, r_list) {
  14636. - msr->r_msg = NULL; /* initialize expunge ordering */
  14637. - wake_up_process(msr->r_tsk);
  14638. - /*
  14639. - * Ensure that the wakeup is visible before setting r_msg as
  14640. - * the receiving end depends on it: either spinning on a nil,
  14641. - * or dealing with -EAGAIN cases. See lockless receive part 1
  14642. - * and 2 in do_msgrcv().
  14643. - */
  14644. - smp_wmb(); /* barrier (B) */
  14645. +
  14646. + wake_q_add(wake_q, msr->r_tsk);
  14647. msr->r_msg = ERR_PTR(res);
  14648. }
  14649. }
  14650. @@ -213,11 +207,13 @@
  14651. {
  14652. struct msg_msg *msg, *t;
  14653. struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
  14654. + WAKE_Q(wake_q);
  14655. - expunge_all(msq, -EIDRM);
  14656. + expunge_all(msq, -EIDRM, &wake_q);
  14657. ss_wakeup(&msq->q_senders, 1);
  14658. msg_rmid(ns, msq);
  14659. ipc_unlock_object(&msq->q_perm);
  14660. + wake_up_q(&wake_q);
  14661. rcu_read_unlock();
  14662. list_for_each_entry_safe(msg, t, &msq->q_messages, m_list) {
  14663. @@ -342,6 +338,7 @@
  14664. struct kern_ipc_perm *ipcp;
  14665. struct msqid64_ds uninitialized_var(msqid64);
  14666. struct msg_queue *msq;
  14667. + WAKE_Q(wake_q);
  14668. int err;
  14669. if (cmd == IPC_SET) {
  14670. @@ -389,7 +386,7 @@
  14671. /* sleeping receivers might be excluded by
  14672. * stricter permissions.
  14673. */
  14674. - expunge_all(msq, -EAGAIN);
  14675. + expunge_all(msq, -EAGAIN, &wake_q);
  14676. /* sleeping senders might be able to send
  14677. * due to a larger queue size.
  14678. */
  14679. @@ -402,6 +399,7 @@
  14680. out_unlock0:
  14681. ipc_unlock_object(&msq->q_perm);
  14682. + wake_up_q(&wake_q);
  14683. out_unlock1:
  14684. rcu_read_unlock();
  14685. out_up:
  14686. @@ -566,7 +564,8 @@
  14687. return 0;
  14688. }
  14689. -static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
  14690. +static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg,
  14691. + struct wake_q_head *wake_q)
  14692. {
  14693. struct msg_receiver *msr, *t;
  14694. @@ -577,27 +576,13 @@
  14695. list_del(&msr->r_list);
  14696. if (msr->r_maxsize < msg->m_ts) {
  14697. - /* initialize pipelined send ordering */
  14698. - msr->r_msg = NULL;
  14699. - wake_up_process(msr->r_tsk);
  14700. - /* barrier (B) see barrier comment below */
  14701. - smp_wmb();
  14702. + wake_q_add(wake_q, msr->r_tsk);
  14703. msr->r_msg = ERR_PTR(-E2BIG);
  14704. } else {
  14705. - msr->r_msg = NULL;
  14706. msq->q_lrpid = task_pid_vnr(msr->r_tsk);
  14707. msq->q_rtime = get_seconds();
  14708. - wake_up_process(msr->r_tsk);
  14709. - /*
  14710. - * Ensure that the wakeup is visible before
  14711. - * setting r_msg, as the receiving can otherwise
  14712. - * exit - once r_msg is set, the receiver can
  14713. - * continue. See lockless receive part 1 and 2
  14714. - * in do_msgrcv(). Barrier (B).
  14715. - */
  14716. - smp_wmb();
  14717. + wake_q_add(wake_q, msr->r_tsk);
  14718. msr->r_msg = msg;
  14719. -
  14720. return 1;
  14721. }
  14722. }
  14723. @@ -613,6 +598,7 @@
  14724. struct msg_msg *msg;
  14725. int err;
  14726. struct ipc_namespace *ns;
  14727. + WAKE_Q(wake_q);
  14728. ns = current->nsproxy->ipc_ns;
  14729. @@ -698,7 +684,7 @@
  14730. msq->q_lspid = task_tgid_vnr(current);
  14731. msq->q_stime = get_seconds();
  14732. - if (!pipelined_send(msq, msg)) {
  14733. + if (!pipelined_send(msq, msg, &wake_q)) {
  14734. /* no one is waiting for this message, enqueue it */
  14735. list_add_tail(&msg->m_list, &msq->q_messages);
  14736. msq->q_cbytes += msgsz;
  14737. @@ -712,6 +698,7 @@
  14738. out_unlock0:
  14739. ipc_unlock_object(&msq->q_perm);
  14740. + wake_up_q(&wake_q);
  14741. out_unlock1:
  14742. rcu_read_unlock();
  14743. if (msg != NULL)
  14744. @@ -932,57 +919,25 @@
  14745. rcu_read_lock();
  14746. /* Lockless receive, part 2:
  14747. - * Wait until pipelined_send or expunge_all are outside of
  14748. - * wake_up_process(). There is a race with exit(), see
  14749. - * ipc/mqueue.c for the details. The correct serialization
  14750. - * ensures that a receiver cannot continue without the wakeup
  14751. - * being visibible _before_ setting r_msg:
  14752. + * The work in pipelined_send() and expunge_all():
  14753. + * - Set pointer to message
  14754. + * - Queue the receiver task for later wakeup
  14755. + * - Wake up the process after the lock is dropped.
  14756. *
  14757. - * CPU 0 CPU 1
  14758. - * <loop receiver>
  14759. - * smp_rmb(); (A) <-- pair -. <waker thread>
  14760. - * <load ->r_msg> | msr->r_msg = NULL;
  14761. - * | wake_up_process();
  14762. - * <continue> `------> smp_wmb(); (B)
  14763. - * msr->r_msg = msg;
  14764. - *
  14765. - * Where (A) orders the message value read and where (B) orders
  14766. - * the write to the r_msg -- done in both pipelined_send and
  14767. - * expunge_all.
  14768. + * Should the process wake up before this wakeup (due to a
  14769. + * signal) it will either see the message and continue …
  14770. */
  14771. - for (;;) {
  14772. - /*
  14773. - * Pairs with writer barrier in pipelined_send
  14774. - * or expunge_all.
  14775. - */
  14776. - smp_rmb(); /* barrier (A) */
  14777. - msg = (struct msg_msg *)msr_d.r_msg;
  14778. - if (msg)
  14779. - break;
  14780. - /*
  14781. - * The cpu_relax() call is a compiler barrier
  14782. - * which forces everything in this loop to be
  14783. - * re-loaded.
  14784. - */
  14785. - cpu_relax();
  14786. - }
  14787. -
  14788. - /* Lockless receive, part 3:
  14789. - * If there is a message or an error then accept it without
  14790. - * locking.
  14791. - */
  14792. + msg = (struct msg_msg *)msr_d.r_msg;
  14793. if (msg != ERR_PTR(-EAGAIN))
  14794. goto out_unlock1;
  14795. - /* Lockless receive, part 3:
  14796. - * Acquire the queue spinlock.
  14797. - */
  14798. + /*
  14799. + * … or see -EAGAIN, acquire the lock to check the message
  14800. + * again.
  14801. + */
  14802. ipc_lock_object(&msq->q_perm);
  14803. - /* Lockless receive, part 4:
  14804. - * Repeat test after acquiring the spinlock.
  14805. - */
  14806. msg = (struct msg_msg *)msr_d.r_msg;
  14807. if (msg != ERR_PTR(-EAGAIN))
  14808. goto out_unlock0;
  14809. diff -Nur linux-4.4.28.orig/ipc/sem.c linux-4.4.28/ipc/sem.c
  14810. --- linux-4.4.28.orig/ipc/sem.c 2016-10-28 09:53:25.000000000 +0200
  14811. +++ linux-4.4.28/ipc/sem.c 2016-11-01 21:26:58.756340996 +0100
  14812. @@ -708,6 +708,13 @@
  14813. static void wake_up_sem_queue_prepare(struct list_head *pt,
  14814. struct sem_queue *q, int error)
  14815. {
  14816. +#ifdef CONFIG_PREEMPT_RT_BASE
  14817. + struct task_struct *p = q->sleeper;
  14818. + get_task_struct(p);
  14819. + q->status = error;
  14820. + wake_up_process(p);
  14821. + put_task_struct(p);
  14822. +#else
  14823. if (list_empty(pt)) {
  14824. /*
  14825. * Hold preempt off so that we don't get preempted and have the
  14826. @@ -719,6 +726,7 @@
  14827. q->pid = error;
  14828. list_add_tail(&q->list, pt);
  14829. +#endif
  14830. }
  14831. /**
  14832. @@ -732,6 +740,7 @@
  14833. */
  14834. static void wake_up_sem_queue_do(struct list_head *pt)
  14835. {
  14836. +#ifndef CONFIG_PREEMPT_RT_BASE
  14837. struct sem_queue *q, *t;
  14838. int did_something;
  14839. @@ -744,6 +753,7 @@
  14840. }
  14841. if (did_something)
  14842. preempt_enable();
  14843. +#endif
  14844. }
  14845. static void unlink_queue(struct sem_array *sma, struct sem_queue *q)
  14846. diff -Nur linux-4.4.28.orig/kernel/cgroup.c linux-4.4.28/kernel/cgroup.c
  14847. --- linux-4.4.28.orig/kernel/cgroup.c 2016-10-28 09:53:25.000000000 +0200
  14848. +++ linux-4.4.28/kernel/cgroup.c 2016-11-01 21:26:58.756340996 +0100
  14849. @@ -4737,10 +4737,10 @@
  14850. queue_work(cgroup_destroy_wq, &css->destroy_work);
  14851. }
  14852. -static void css_release_work_fn(struct work_struct *work)
  14853. +static void css_release_work_fn(struct swork_event *sev)
  14854. {
  14855. struct cgroup_subsys_state *css =
  14856. - container_of(work, struct cgroup_subsys_state, destroy_work);
  14857. + container_of(sev, struct cgroup_subsys_state, destroy_swork);
  14858. struct cgroup_subsys *ss = css->ss;
  14859. struct cgroup *cgrp = css->cgroup;
  14860. @@ -4779,8 +4779,8 @@
  14861. struct cgroup_subsys_state *css =
  14862. container_of(ref, struct cgroup_subsys_state, refcnt);
  14863. - INIT_WORK(&css->destroy_work, css_release_work_fn);
  14864. - queue_work(cgroup_destroy_wq, &css->destroy_work);
  14865. + INIT_SWORK(&css->destroy_swork, css_release_work_fn);
  14866. + swork_queue(&css->destroy_swork);
  14867. }
  14868. static void init_and_link_css(struct cgroup_subsys_state *css,
  14869. @@ -5397,6 +5397,7 @@
  14870. */
  14871. cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
  14872. BUG_ON(!cgroup_destroy_wq);
  14873. + BUG_ON(swork_get());
  14874. /*
  14875. * Used to destroy pidlists and separate to serve as flush domain.
  14876. diff -Nur linux-4.4.28.orig/kernel/cpu.c linux-4.4.28/kernel/cpu.c
  14877. --- linux-4.4.28.orig/kernel/cpu.c 2016-10-28 09:53:25.000000000 +0200
  14878. +++ linux-4.4.28/kernel/cpu.c 2016-11-01 21:26:58.756340996 +0100
  14879. @@ -75,8 +75,8 @@
  14880. #endif
  14881. } cpu_hotplug = {
  14882. .active_writer = NULL,
  14883. - .wq = __WAIT_QUEUE_HEAD_INITIALIZER(cpu_hotplug.wq),
  14884. .lock = __MUTEX_INITIALIZER(cpu_hotplug.lock),
  14885. + .wq = __WAIT_QUEUE_HEAD_INITIALIZER(cpu_hotplug.wq),
  14886. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  14887. .dep_map = {.name = "cpu_hotplug.lock" },
  14888. #endif
  14889. @@ -89,6 +89,289 @@
  14890. #define cpuhp_lock_acquire() lock_map_acquire(&cpu_hotplug.dep_map)
  14891. #define cpuhp_lock_release() lock_map_release(&cpu_hotplug.dep_map)
  14892. +/**
  14893. + * hotplug_pcp - per cpu hotplug descriptor
  14894. + * @unplug: set when pin_current_cpu() needs to sync tasks
  14895. + * @sync_tsk: the task that waits for tasks to finish pinned sections
  14896. + * @refcount: counter of tasks in pinned sections
  14897. + * @grab_lock: set when the tasks entering pinned sections should wait
  14898. + * @synced: notifier for @sync_tsk to tell cpu_down it's finished
  14899. + * @mutex: the mutex to make tasks wait (used when @grab_lock is true)
  14900. + * @mutex_init: zero if the mutex hasn't been initialized yet.
  14901. + *
  14902. + * Although @unplug and @sync_tsk may point to the same task, the @unplug
  14903. + * is used as a flag and still exists after @sync_tsk has exited and
  14904. + * @sync_tsk set to NULL.
  14905. + */
  14906. +struct hotplug_pcp {
  14907. + struct task_struct *unplug;
  14908. + struct task_struct *sync_tsk;
  14909. + int refcount;
  14910. + int grab_lock;
  14911. + struct completion synced;
  14912. + struct completion unplug_wait;
  14913. +#ifdef CONFIG_PREEMPT_RT_FULL
  14914. + /*
  14915. + * Note, on PREEMPT_RT, the hotplug lock must save the state of
  14916. + * the task, otherwise the mutex will cause the task to fail
  14917. + * to sleep when required. (Because it's called from migrate_disable())
  14918. + *
  14919. + * The spinlock_t on PREEMPT_RT is a mutex that saves the task's
  14920. + * state.
  14921. + */
  14922. + spinlock_t lock;
  14923. +#else
  14924. + struct mutex mutex;
  14925. +#endif
  14926. + int mutex_init;
  14927. +};
  14928. +
  14929. +#ifdef CONFIG_PREEMPT_RT_FULL
  14930. +# define hotplug_lock(hp) rt_spin_lock__no_mg(&(hp)->lock)
  14931. +# define hotplug_unlock(hp) rt_spin_unlock__no_mg(&(hp)->lock)
  14932. +#else
  14933. +# define hotplug_lock(hp) mutex_lock(&(hp)->mutex)
  14934. +# define hotplug_unlock(hp) mutex_unlock(&(hp)->mutex)
  14935. +#endif
  14936. +
  14937. +static DEFINE_PER_CPU(struct hotplug_pcp, hotplug_pcp);
  14938. +
  14939. +/**
  14940. + * pin_current_cpu - Prevent the current cpu from being unplugged
  14941. + *
  14942. + * Lightweight version of get_online_cpus() to prevent cpu from being
  14943. + * unplugged when code runs in a migration disabled region.
  14944. + *
  14945. + * Must be called with preemption disabled (preempt_count = 1)!
  14946. + */
  14947. +void pin_current_cpu(void)
  14948. +{
  14949. + struct hotplug_pcp *hp;
  14950. + int force = 0;
  14951. +
  14952. +retry:
  14953. + hp = this_cpu_ptr(&hotplug_pcp);
  14954. +
  14955. + if (!hp->unplug || hp->refcount || force || preempt_count() > 1 ||
  14956. + hp->unplug == current) {
  14957. + hp->refcount++;
  14958. + return;
  14959. + }
  14960. + if (hp->grab_lock) {
  14961. + preempt_enable();
  14962. + hotplug_lock(hp);
  14963. + hotplug_unlock(hp);
  14964. + } else {
  14965. + preempt_enable();
  14966. + /*
  14967. + * Try to push this task off of this CPU.
  14968. + */
  14969. + if (!migrate_me()) {
  14970. + preempt_disable();
  14971. + hp = this_cpu_ptr(&hotplug_pcp);
  14972. + if (!hp->grab_lock) {
  14973. + /*
  14974. + * Just let it continue it's already pinned
  14975. + * or about to sleep.
  14976. + */
  14977. + force = 1;
  14978. + goto retry;
  14979. + }
  14980. + preempt_enable();
  14981. + }
  14982. + }
  14983. + preempt_disable();
  14984. + goto retry;
  14985. +}
  14986. +
  14987. +/**
  14988. + * unpin_current_cpu - Allow unplug of current cpu
  14989. + *
  14990. + * Must be called with preemption or interrupts disabled!
  14991. + */
  14992. +void unpin_current_cpu(void)
  14993. +{
  14994. + struct hotplug_pcp *hp = this_cpu_ptr(&hotplug_pcp);
  14995. +
  14996. + WARN_ON(hp->refcount <= 0);
  14997. +
  14998. + /* This is safe. sync_unplug_thread is pinned to this cpu */
  14999. + if (!--hp->refcount && hp->unplug && hp->unplug != current)
  15000. + wake_up_process(hp->unplug);
  15001. +}
  15002. +
  15003. +static void wait_for_pinned_cpus(struct hotplug_pcp *hp)
  15004. +{
  15005. + set_current_state(TASK_UNINTERRUPTIBLE);
  15006. + while (hp->refcount) {
  15007. + schedule_preempt_disabled();
  15008. + set_current_state(TASK_UNINTERRUPTIBLE);
  15009. + }
  15010. +}
  15011. +
  15012. +static int sync_unplug_thread(void *data)
  15013. +{
  15014. + struct hotplug_pcp *hp = data;
  15015. +
  15016. + wait_for_completion(&hp->unplug_wait);
  15017. + preempt_disable();
  15018. + hp->unplug = current;
  15019. + wait_for_pinned_cpus(hp);
  15020. +
  15021. + /*
  15022. + * This thread will synchronize the cpu_down() with threads
  15023. + * that have pinned the CPU. When the pinned CPU count reaches
  15024. + * zero, we inform the cpu_down code to continue to the next step.
  15025. + */
  15026. + set_current_state(TASK_UNINTERRUPTIBLE);
  15027. + preempt_enable();
  15028. + complete(&hp->synced);
  15029. +
  15030. + /*
  15031. + * If all succeeds, the next step will need tasks to wait till
  15032. + * the CPU is offline before continuing. To do this, the grab_lock
  15033. + * is set and tasks going into pin_current_cpu() will block on the
  15034. + * mutex. But we still need to wait for those that are already in
  15035. + * pinned CPU sections. If the cpu_down() failed, the kthread_should_stop()
  15036. + * will kick this thread out.
  15037. + */
  15038. + while (!hp->grab_lock && !kthread_should_stop()) {
  15039. + schedule();
  15040. + set_current_state(TASK_UNINTERRUPTIBLE);
  15041. + }
  15042. +
  15043. + /* Make sure grab_lock is seen before we see a stale completion */
  15044. + smp_mb();
  15045. +
  15046. + /*
  15047. + * Now just before cpu_down() enters stop machine, we need to make
  15048. + * sure all tasks that are in pinned CPU sections are out, and new
  15049. + * tasks will now grab the lock, keeping them from entering pinned
  15050. + * CPU sections.
  15051. + */
  15052. + if (!kthread_should_stop()) {
  15053. + preempt_disable();
  15054. + wait_for_pinned_cpus(hp);
  15055. + preempt_enable();
  15056. + complete(&hp->synced);
  15057. + }
  15058. +
  15059. + set_current_state(TASK_UNINTERRUPTIBLE);
  15060. + while (!kthread_should_stop()) {
  15061. + schedule();
  15062. + set_current_state(TASK_UNINTERRUPTIBLE);
  15063. + }
  15064. + set_current_state(TASK_RUNNING);
  15065. +
  15066. + /*
  15067. + * Force this thread off this CPU as it's going down and
  15068. + * we don't want any more work on this CPU.
  15069. + */
  15070. + current->flags &= ~PF_NO_SETAFFINITY;
  15071. + set_cpus_allowed_ptr(current, cpu_present_mask);
  15072. + migrate_me();
  15073. + return 0;
  15074. +}
  15075. +
  15076. +static void __cpu_unplug_sync(struct hotplug_pcp *hp)
  15077. +{
  15078. + wake_up_process(hp->sync_tsk);
  15079. + wait_for_completion(&hp->synced);
  15080. +}
  15081. +
  15082. +static void __cpu_unplug_wait(unsigned int cpu)
  15083. +{
  15084. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  15085. +
  15086. + complete(&hp->unplug_wait);
  15087. + wait_for_completion(&hp->synced);
  15088. +}
  15089. +
  15090. +/*
  15091. + * Start the sync_unplug_thread on the target cpu and wait for it to
  15092. + * complete.
  15093. + */
  15094. +static int cpu_unplug_begin(unsigned int cpu)
  15095. +{
  15096. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  15097. + int err;
  15098. +
  15099. + /* Protected by cpu_hotplug.lock */
  15100. + if (!hp->mutex_init) {
  15101. +#ifdef CONFIG_PREEMPT_RT_FULL
  15102. + spin_lock_init(&hp->lock);
  15103. +#else
  15104. + mutex_init(&hp->mutex);
  15105. +#endif
  15106. + hp->mutex_init = 1;
  15107. + }
  15108. +
  15109. + /* Inform the scheduler to migrate tasks off this CPU */
  15110. + tell_sched_cpu_down_begin(cpu);
  15111. +
  15112. + init_completion(&hp->synced);
  15113. + init_completion(&hp->unplug_wait);
  15114. +
  15115. + hp->sync_tsk = kthread_create(sync_unplug_thread, hp, "sync_unplug/%d", cpu);
  15116. + if (IS_ERR(hp->sync_tsk)) {
  15117. + err = PTR_ERR(hp->sync_tsk);
  15118. + hp->sync_tsk = NULL;
  15119. + return err;
  15120. + }
  15121. + kthread_bind(hp->sync_tsk, cpu);
  15122. +
  15123. + /*
  15124. + * Wait for tasks to get out of the pinned sections,
  15125. + * it's still OK if new tasks enter. Some CPU notifiers will
  15126. + * wait for tasks that are going to enter these sections and
  15127. + * we must not have them block.
  15128. + */
  15129. + wake_up_process(hp->sync_tsk);
  15130. + return 0;
  15131. +}
  15132. +
  15133. +static void cpu_unplug_sync(unsigned int cpu)
  15134. +{
  15135. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  15136. +
  15137. + init_completion(&hp->synced);
  15138. + /* The completion needs to be initialzied before setting grab_lock */
  15139. + smp_wmb();
  15140. +
  15141. + /* Grab the mutex before setting grab_lock */
  15142. + hotplug_lock(hp);
  15143. + hp->grab_lock = 1;
  15144. +
  15145. + /*
  15146. + * The CPU notifiers have been completed.
  15147. + * Wait for tasks to get out of pinned CPU sections and have new
  15148. + * tasks block until the CPU is completely down.
  15149. + */
  15150. + __cpu_unplug_sync(hp);
  15151. +
  15152. + /* All done with the sync thread */
  15153. + kthread_stop(hp->sync_tsk);
  15154. + hp->sync_tsk = NULL;
  15155. +}
  15156. +
  15157. +static void cpu_unplug_done(unsigned int cpu)
  15158. +{
  15159. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  15160. +
  15161. + hp->unplug = NULL;
  15162. + /* Let all tasks know cpu unplug is finished before cleaning up */
  15163. + smp_wmb();
  15164. +
  15165. + if (hp->sync_tsk)
  15166. + kthread_stop(hp->sync_tsk);
  15167. +
  15168. + if (hp->grab_lock) {
  15169. + hotplug_unlock(hp);
  15170. + /* protected by cpu_hotplug.lock */
  15171. + hp->grab_lock = 0;
  15172. + }
  15173. + tell_sched_cpu_down_done(cpu);
  15174. +}
  15175. void get_online_cpus(void)
  15176. {
  15177. @@ -338,13 +621,15 @@
  15178. /* Requires cpu_add_remove_lock to be held */
  15179. static int _cpu_down(unsigned int cpu, int tasks_frozen)
  15180. {
  15181. - int err, nr_calls = 0;
  15182. + int mycpu, err, nr_calls = 0;
  15183. void *hcpu = (void *)(long)cpu;
  15184. unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
  15185. struct take_cpu_down_param tcd_param = {
  15186. .mod = mod,
  15187. .hcpu = hcpu,
  15188. };
  15189. + cpumask_var_t cpumask;
  15190. + cpumask_var_t cpumask_org;
  15191. if (num_online_cpus() == 1)
  15192. return -EBUSY;
  15193. @@ -352,7 +637,34 @@
  15194. if (!cpu_online(cpu))
  15195. return -EINVAL;
  15196. + /* Move the downtaker off the unplug cpu */
  15197. + if (!alloc_cpumask_var(&cpumask, GFP_KERNEL))
  15198. + return -ENOMEM;
  15199. + if (!alloc_cpumask_var(&cpumask_org, GFP_KERNEL)) {
  15200. + free_cpumask_var(cpumask);
  15201. + return -ENOMEM;
  15202. + }
  15203. +
  15204. + cpumask_copy(cpumask_org, tsk_cpus_allowed(current));
  15205. + cpumask_andnot(cpumask, cpu_online_mask, cpumask_of(cpu));
  15206. + set_cpus_allowed_ptr(current, cpumask);
  15207. + free_cpumask_var(cpumask);
  15208. + migrate_disable();
  15209. + mycpu = smp_processor_id();
  15210. + if (mycpu == cpu) {
  15211. + printk(KERN_ERR "Yuck! Still on unplug CPU\n!");
  15212. + migrate_enable();
  15213. + err = -EBUSY;
  15214. + goto restore_cpus;
  15215. + }
  15216. + migrate_enable();
  15217. +
  15218. cpu_hotplug_begin();
  15219. + err = cpu_unplug_begin(cpu);
  15220. + if (err) {
  15221. + printk("cpu_unplug_begin(%d) failed\n", cpu);
  15222. + goto out_cancel;
  15223. + }
  15224. err = __cpu_notify(CPU_DOWN_PREPARE | mod, hcpu, -1, &nr_calls);
  15225. if (err) {
  15226. @@ -378,8 +690,12 @@
  15227. else
  15228. synchronize_rcu();
  15229. + __cpu_unplug_wait(cpu);
  15230. smpboot_park_threads(cpu);
  15231. + /* Notifiers are done. Don't let any more tasks pin this CPU. */
  15232. + cpu_unplug_sync(cpu);
  15233. +
  15234. /*
  15235. * Prevent irq alloc/free while the dying cpu reorganizes the
  15236. * interrupt affinities.
  15237. @@ -424,9 +740,14 @@
  15238. check_for_tasks(cpu);
  15239. out_release:
  15240. + cpu_unplug_done(cpu);
  15241. +out_cancel:
  15242. cpu_hotplug_done();
  15243. if (!err)
  15244. cpu_notify_nofail(CPU_POST_DEAD | mod, hcpu);
  15245. +restore_cpus:
  15246. + set_cpus_allowed_ptr(current, cpumask_org);
  15247. + free_cpumask_var(cpumask_org);
  15248. return err;
  15249. }
  15250. diff -Nur linux-4.4.28.orig/kernel/debug/kdb/kdb_io.c linux-4.4.28/kernel/debug/kdb/kdb_io.c
  15251. --- linux-4.4.28.orig/kernel/debug/kdb/kdb_io.c 2016-10-28 09:53:25.000000000 +0200
  15252. +++ linux-4.4.28/kernel/debug/kdb/kdb_io.c 2016-11-01 21:26:58.756340996 +0100
  15253. @@ -554,7 +554,6 @@
  15254. int linecount;
  15255. int colcount;
  15256. int logging, saved_loglevel = 0;
  15257. - int saved_trap_printk;
  15258. int got_printf_lock = 0;
  15259. int retlen = 0;
  15260. int fnd, len;
  15261. @@ -565,8 +564,6 @@
  15262. unsigned long uninitialized_var(flags);
  15263. preempt_disable();
  15264. - saved_trap_printk = kdb_trap_printk;
  15265. - kdb_trap_printk = 0;
  15266. /* Serialize kdb_printf if multiple cpus try to write at once.
  15267. * But if any cpu goes recursive in kdb, just print the output,
  15268. @@ -855,7 +852,6 @@
  15269. } else {
  15270. __release(kdb_printf_lock);
  15271. }
  15272. - kdb_trap_printk = saved_trap_printk;
  15273. preempt_enable();
  15274. return retlen;
  15275. }
  15276. @@ -865,9 +861,11 @@
  15277. va_list ap;
  15278. int r;
  15279. + kdb_trap_printk++;
  15280. va_start(ap, fmt);
  15281. r = vkdb_printf(KDB_MSGSRC_INTERNAL, fmt, ap);
  15282. va_end(ap);
  15283. + kdb_trap_printk--;
  15284. return r;
  15285. }
  15286. diff -Nur linux-4.4.28.orig/kernel/events/core.c linux-4.4.28/kernel/events/core.c
  15287. --- linux-4.4.28.orig/kernel/events/core.c 2016-10-28 09:53:25.000000000 +0200
  15288. +++ linux-4.4.28/kernel/events/core.c 2016-11-01 21:26:58.760341249 +0100
  15289. @@ -802,6 +802,7 @@
  15290. raw_spin_lock_init(&cpuctx->hrtimer_lock);
  15291. hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
  15292. timer->function = perf_mux_hrtimer_handler;
  15293. + timer->irqsafe = 1;
  15294. }
  15295. static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx)
  15296. @@ -7240,6 +7241,7 @@
  15297. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  15298. hwc->hrtimer.function = perf_swevent_hrtimer;
  15299. + hwc->hrtimer.irqsafe = 1;
  15300. /*
  15301. * Since hrtimers have a fixed rate, we can do a static freq->period
  15302. diff -Nur linux-4.4.28.orig/kernel/exit.c linux-4.4.28/kernel/exit.c
  15303. --- linux-4.4.28.orig/kernel/exit.c 2016-10-28 09:53:25.000000000 +0200
  15304. +++ linux-4.4.28/kernel/exit.c 2016-11-01 21:26:58.760341249 +0100
  15305. @@ -144,7 +144,7 @@
  15306. * Do this under ->siglock, we can race with another thread
  15307. * doing sigqueue_free() if we have SIGQUEUE_PREALLOC signals.
  15308. */
  15309. - flush_sigqueue(&tsk->pending);
  15310. + flush_task_sigqueue(tsk);
  15311. tsk->sighand = NULL;
  15312. spin_unlock(&sighand->siglock);
  15313. diff -Nur linux-4.4.28.orig/kernel/fork.c linux-4.4.28/kernel/fork.c
  15314. --- linux-4.4.28.orig/kernel/fork.c 2016-10-28 09:53:25.000000000 +0200
  15315. +++ linux-4.4.28/kernel/fork.c 2016-11-01 21:26:58.764341506 +0100
  15316. @@ -108,7 +108,7 @@
  15317. DEFINE_PER_CPU(unsigned long, process_counts) = 0;
  15318. -__cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */
  15319. +DEFINE_RWLOCK(tasklist_lock); /* outer */
  15320. #ifdef CONFIG_PROVE_RCU
  15321. int lockdep_tasklist_lock_is_held(void)
  15322. @@ -244,7 +244,9 @@
  15323. if (atomic_dec_and_test(&sig->sigcnt))
  15324. free_signal_struct(sig);
  15325. }
  15326. -
  15327. +#ifdef CONFIG_PREEMPT_RT_BASE
  15328. +static
  15329. +#endif
  15330. void __put_task_struct(struct task_struct *tsk)
  15331. {
  15332. WARN_ON(!tsk->exit_state);
  15333. @@ -261,7 +263,18 @@
  15334. if (!profile_handoff_task(tsk))
  15335. free_task(tsk);
  15336. }
  15337. +#ifndef CONFIG_PREEMPT_RT_BASE
  15338. EXPORT_SYMBOL_GPL(__put_task_struct);
  15339. +#else
  15340. +void __put_task_struct_cb(struct rcu_head *rhp)
  15341. +{
  15342. + struct task_struct *tsk = container_of(rhp, struct task_struct, put_rcu);
  15343. +
  15344. + __put_task_struct(tsk);
  15345. +
  15346. +}
  15347. +EXPORT_SYMBOL_GPL(__put_task_struct_cb);
  15348. +#endif
  15349. void __init __weak arch_task_cache_init(void) { }
  15350. @@ -689,6 +702,19 @@
  15351. }
  15352. EXPORT_SYMBOL_GPL(__mmdrop);
  15353. +#ifdef CONFIG_PREEMPT_RT_BASE
  15354. +/*
  15355. + * RCU callback for delayed mm drop. Not strictly rcu, but we don't
  15356. + * want another facility to make this work.
  15357. + */
  15358. +void __mmdrop_delayed(struct rcu_head *rhp)
  15359. +{
  15360. + struct mm_struct *mm = container_of(rhp, struct mm_struct, delayed_drop);
  15361. +
  15362. + __mmdrop(mm);
  15363. +}
  15364. +#endif
  15365. +
  15366. /*
  15367. * Decrement the use count and release all resources for an mm.
  15368. */
  15369. @@ -1239,6 +1265,9 @@
  15370. */
  15371. static void posix_cpu_timers_init(struct task_struct *tsk)
  15372. {
  15373. +#ifdef CONFIG_PREEMPT_RT_BASE
  15374. + tsk->posix_timer_list = NULL;
  15375. +#endif
  15376. tsk->cputime_expires.prof_exp = 0;
  15377. tsk->cputime_expires.virt_exp = 0;
  15378. tsk->cputime_expires.sched_exp = 0;
  15379. @@ -1364,15 +1393,16 @@
  15380. spin_lock_init(&p->alloc_lock);
  15381. init_sigpending(&p->pending);
  15382. + p->sigqueue_cache = NULL;
  15383. p->utime = p->stime = p->gtime = 0;
  15384. p->utimescaled = p->stimescaled = 0;
  15385. prev_cputime_init(&p->prev_cputime);
  15386. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  15387. - seqlock_init(&p->vtime_seqlock);
  15388. + seqcount_init(&p->vtime_seqcount);
  15389. p->vtime_snap = 0;
  15390. - p->vtime_snap_whence = VTIME_SLEEPING;
  15391. + p->vtime_snap_whence = VTIME_INACTIVE;
  15392. #endif
  15393. #if defined(SPLIT_RSS_COUNTING)
  15394. diff -Nur linux-4.4.28.orig/kernel/futex.c linux-4.4.28/kernel/futex.c
  15395. --- linux-4.4.28.orig/kernel/futex.c 2016-10-28 09:53:25.000000000 +0200
  15396. +++ linux-4.4.28/kernel/futex.c 2016-11-01 21:26:58.764341506 +0100
  15397. @@ -815,7 +815,9 @@
  15398. * task still owns the PI-state:
  15399. */
  15400. if (head->next != next) {
  15401. + raw_spin_unlock_irq(&curr->pi_lock);
  15402. spin_unlock(&hb->lock);
  15403. + raw_spin_lock_irq(&curr->pi_lock);
  15404. continue;
  15405. }
  15406. @@ -1210,6 +1212,7 @@
  15407. struct futex_pi_state *pi_state = this->pi_state;
  15408. u32 uninitialized_var(curval), newval;
  15409. WAKE_Q(wake_q);
  15410. + WAKE_Q(wake_sleeper_q);
  15411. bool deboost;
  15412. int ret = 0;
  15413. @@ -1223,7 +1226,7 @@
  15414. if (pi_state->owner != current)
  15415. return -EINVAL;
  15416. - raw_spin_lock(&pi_state->pi_mutex.wait_lock);
  15417. + raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
  15418. new_owner = rt_mutex_next_owner(&pi_state->pi_mutex);
  15419. /*
  15420. @@ -1259,24 +1262,25 @@
  15421. ret = -EINVAL;
  15422. }
  15423. if (ret) {
  15424. - raw_spin_unlock(&pi_state->pi_mutex.wait_lock);
  15425. + raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
  15426. return ret;
  15427. }
  15428. - raw_spin_lock_irq(&pi_state->owner->pi_lock);
  15429. + raw_spin_lock(&pi_state->owner->pi_lock);
  15430. WARN_ON(list_empty(&pi_state->list));
  15431. list_del_init(&pi_state->list);
  15432. - raw_spin_unlock_irq(&pi_state->owner->pi_lock);
  15433. + raw_spin_unlock(&pi_state->owner->pi_lock);
  15434. - raw_spin_lock_irq(&new_owner->pi_lock);
  15435. + raw_spin_lock(&new_owner->pi_lock);
  15436. WARN_ON(!list_empty(&pi_state->list));
  15437. list_add(&pi_state->list, &new_owner->pi_state_list);
  15438. pi_state->owner = new_owner;
  15439. - raw_spin_unlock_irq(&new_owner->pi_lock);
  15440. + raw_spin_unlock(&new_owner->pi_lock);
  15441. - raw_spin_unlock(&pi_state->pi_mutex.wait_lock);
  15442. + raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
  15443. - deboost = rt_mutex_futex_unlock(&pi_state->pi_mutex, &wake_q);
  15444. + deboost = rt_mutex_futex_unlock(&pi_state->pi_mutex, &wake_q,
  15445. + &wake_sleeper_q);
  15446. /*
  15447. * First unlock HB so the waiter does not spin on it once he got woken
  15448. @@ -1284,8 +1288,9 @@
  15449. * deboost first (and lose our higher priority), then the task might get
  15450. * scheduled away before the wake up can take place.
  15451. */
  15452. - spin_unlock(&hb->lock);
  15453. + deboost |= spin_unlock_no_deboost(&hb->lock);
  15454. wake_up_q(&wake_q);
  15455. + wake_up_q_sleeper(&wake_sleeper_q);
  15456. if (deboost)
  15457. rt_mutex_adjust_prio(current);
  15458. @@ -1822,6 +1827,16 @@
  15459. requeue_pi_wake_futex(this, &key2, hb2);
  15460. drop_count++;
  15461. continue;
  15462. + } else if (ret == -EAGAIN) {
  15463. + /*
  15464. + * Waiter was woken by timeout or
  15465. + * signal and has set pi_blocked_on to
  15466. + * PI_WAKEUP_INPROGRESS before we
  15467. + * tried to enqueue it on the rtmutex.
  15468. + */
  15469. + this->pi_state = NULL;
  15470. + free_pi_state(pi_state);
  15471. + continue;
  15472. } else if (ret) {
  15473. /* -EDEADLK */
  15474. this->pi_state = NULL;
  15475. @@ -2139,11 +2154,11 @@
  15476. * we returned due to timeout or signal without taking the
  15477. * rt_mutex. Too late.
  15478. */
  15479. - raw_spin_lock(&q->pi_state->pi_mutex.wait_lock);
  15480. + raw_spin_lock_irq(&q->pi_state->pi_mutex.wait_lock);
  15481. owner = rt_mutex_owner(&q->pi_state->pi_mutex);
  15482. if (!owner)
  15483. owner = rt_mutex_next_owner(&q->pi_state->pi_mutex);
  15484. - raw_spin_unlock(&q->pi_state->pi_mutex.wait_lock);
  15485. + raw_spin_unlock_irq(&q->pi_state->pi_mutex.wait_lock);
  15486. ret = fixup_pi_state_owner(uaddr, q, owner);
  15487. goto out;
  15488. }
  15489. @@ -2691,7 +2706,7 @@
  15490. struct hrtimer_sleeper timeout, *to = NULL;
  15491. struct rt_mutex_waiter rt_waiter;
  15492. struct rt_mutex *pi_mutex = NULL;
  15493. - struct futex_hash_bucket *hb;
  15494. + struct futex_hash_bucket *hb, *hb2;
  15495. union futex_key key2 = FUTEX_KEY_INIT;
  15496. struct futex_q q = futex_q_init;
  15497. int res, ret;
  15498. @@ -2716,10 +2731,7 @@
  15499. * The waiter is allocated on our stack, manipulated by the requeue
  15500. * code while we sleep on uaddr.
  15501. */
  15502. - debug_rt_mutex_init_waiter(&rt_waiter);
  15503. - RB_CLEAR_NODE(&rt_waiter.pi_tree_entry);
  15504. - RB_CLEAR_NODE(&rt_waiter.tree_entry);
  15505. - rt_waiter.task = NULL;
  15506. + rt_mutex_init_waiter(&rt_waiter, false);
  15507. ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE);
  15508. if (unlikely(ret != 0))
  15509. @@ -2750,20 +2762,55 @@
  15510. /* Queue the futex_q, drop the hb lock, wait for wakeup. */
  15511. futex_wait_queue_me(hb, &q, to);
  15512. - spin_lock(&hb->lock);
  15513. - ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
  15514. - spin_unlock(&hb->lock);
  15515. - if (ret)
  15516. - goto out_put_keys;
  15517. + /*
  15518. + * On RT we must avoid races with requeue and trying to block
  15519. + * on two mutexes (hb->lock and uaddr2's rtmutex) by
  15520. + * serializing access to pi_blocked_on with pi_lock.
  15521. + */
  15522. + raw_spin_lock_irq(&current->pi_lock);
  15523. + if (current->pi_blocked_on) {
  15524. + /*
  15525. + * We have been requeued or are in the process of
  15526. + * being requeued.
  15527. + */
  15528. + raw_spin_unlock_irq(&current->pi_lock);
  15529. + } else {
  15530. + /*
  15531. + * Setting pi_blocked_on to PI_WAKEUP_INPROGRESS
  15532. + * prevents a concurrent requeue from moving us to the
  15533. + * uaddr2 rtmutex. After that we can safely acquire
  15534. + * (and possibly block on) hb->lock.
  15535. + */
  15536. + current->pi_blocked_on = PI_WAKEUP_INPROGRESS;
  15537. + raw_spin_unlock_irq(&current->pi_lock);
  15538. +
  15539. + spin_lock(&hb->lock);
  15540. +
  15541. + /*
  15542. + * Clean up pi_blocked_on. We might leak it otherwise
  15543. + * when we succeeded with the hb->lock in the fast
  15544. + * path.
  15545. + */
  15546. + raw_spin_lock_irq(&current->pi_lock);
  15547. + current->pi_blocked_on = NULL;
  15548. + raw_spin_unlock_irq(&current->pi_lock);
  15549. +
  15550. + ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
  15551. + spin_unlock(&hb->lock);
  15552. + if (ret)
  15553. + goto out_put_keys;
  15554. + }
  15555. /*
  15556. - * In order for us to be here, we know our q.key == key2, and since
  15557. - * we took the hb->lock above, we also know that futex_requeue() has
  15558. - * completed and we no longer have to concern ourselves with a wakeup
  15559. - * race with the atomic proxy lock acquisition by the requeue code. The
  15560. - * futex_requeue dropped our key1 reference and incremented our key2
  15561. - * reference count.
  15562. + * In order to be here, we have either been requeued, are in
  15563. + * the process of being requeued, or requeue successfully
  15564. + * acquired uaddr2 on our behalf. If pi_blocked_on was
  15565. + * non-null above, we may be racing with a requeue. Do not
  15566. + * rely on q->lock_ptr to be hb2->lock until after blocking on
  15567. + * hb->lock or hb2->lock. The futex_requeue dropped our key1
  15568. + * reference and incremented our key2 reference count.
  15569. */
  15570. + hb2 = hash_futex(&key2);
  15571. /* Check if the requeue code acquired the second futex for us. */
  15572. if (!q.rt_waiter) {
  15573. @@ -2772,14 +2819,15 @@
  15574. * did a lock-steal - fix up the PI-state in that case.
  15575. */
  15576. if (q.pi_state && (q.pi_state->owner != current)) {
  15577. - spin_lock(q.lock_ptr);
  15578. + spin_lock(&hb2->lock);
  15579. + BUG_ON(&hb2->lock != q.lock_ptr);
  15580. ret = fixup_pi_state_owner(uaddr2, &q, current);
  15581. /*
  15582. * Drop the reference to the pi state which
  15583. * the requeue_pi() code acquired for us.
  15584. */
  15585. free_pi_state(q.pi_state);
  15586. - spin_unlock(q.lock_ptr);
  15587. + spin_unlock(&hb2->lock);
  15588. }
  15589. } else {
  15590. /*
  15591. @@ -2792,7 +2840,8 @@
  15592. ret = rt_mutex_finish_proxy_lock(pi_mutex, to, &rt_waiter);
  15593. debug_rt_mutex_free_waiter(&rt_waiter);
  15594. - spin_lock(q.lock_ptr);
  15595. + spin_lock(&hb2->lock);
  15596. + BUG_ON(&hb2->lock != q.lock_ptr);
  15597. /*
  15598. * Fixup the pi_state owner and possibly acquire the lock if we
  15599. * haven't already.
  15600. diff -Nur linux-4.4.28.orig/kernel/irq/handle.c linux-4.4.28/kernel/irq/handle.c
  15601. --- linux-4.4.28.orig/kernel/irq/handle.c 2016-10-28 09:53:25.000000000 +0200
  15602. +++ linux-4.4.28/kernel/irq/handle.c 2016-11-01 21:26:58.764341506 +0100
  15603. @@ -134,6 +134,8 @@
  15604. irqreturn_t handle_irq_event_percpu(struct irq_desc *desc)
  15605. {
  15606. + struct pt_regs *regs = get_irq_regs();
  15607. + u64 ip = regs ? instruction_pointer(regs) : 0;
  15608. irqreturn_t retval = IRQ_NONE;
  15609. unsigned int flags = 0, irq = desc->irq_data.irq;
  15610. struct irqaction *action = desc->action;
  15611. @@ -176,7 +178,11 @@
  15612. action = action->next;
  15613. }
  15614. - add_interrupt_randomness(irq, flags);
  15615. +#ifdef CONFIG_PREEMPT_RT_FULL
  15616. + desc->random_ip = ip;
  15617. +#else
  15618. + add_interrupt_randomness(irq, flags, ip);
  15619. +#endif
  15620. if (!noirqdebug)
  15621. note_interrupt(desc, retval);
  15622. diff -Nur linux-4.4.28.orig/kernel/irq/irqdesc.c linux-4.4.28/kernel/irq/irqdesc.c
  15623. --- linux-4.4.28.orig/kernel/irq/irqdesc.c 2016-10-28 09:53:25.000000000 +0200
  15624. +++ linux-4.4.28/kernel/irq/irqdesc.c 2016-11-01 21:26:58.764341506 +0100
  15625. @@ -24,10 +24,27 @@
  15626. static struct lock_class_key irq_desc_lock_class;
  15627. #if defined(CONFIG_SMP)
  15628. +static int __init irq_affinity_setup(char *str)
  15629. +{
  15630. + zalloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
  15631. + cpulist_parse(str, irq_default_affinity);
  15632. + /*
  15633. + * Set at least the boot cpu. We don't want to end up with
  15634. + * bugreports caused by random comandline masks
  15635. + */
  15636. + cpumask_set_cpu(smp_processor_id(), irq_default_affinity);
  15637. + return 1;
  15638. +}
  15639. +__setup("irqaffinity=", irq_affinity_setup);
  15640. +
  15641. static void __init init_irq_default_affinity(void)
  15642. {
  15643. - alloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
  15644. - cpumask_setall(irq_default_affinity);
  15645. +#ifdef CONFIG_CPUMASK_OFFSTACK
  15646. + if (!irq_default_affinity)
  15647. + zalloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
  15648. +#endif
  15649. + if (cpumask_empty(irq_default_affinity))
  15650. + cpumask_setall(irq_default_affinity);
  15651. }
  15652. #else
  15653. static void __init init_irq_default_affinity(void)
  15654. diff -Nur linux-4.4.28.orig/kernel/irq/manage.c linux-4.4.28/kernel/irq/manage.c
  15655. --- linux-4.4.28.orig/kernel/irq/manage.c 2016-10-28 09:53:25.000000000 +0200
  15656. +++ linux-4.4.28/kernel/irq/manage.c 2016-11-01 21:26:58.764341506 +0100
  15657. @@ -22,6 +22,7 @@
  15658. #include "internals.h"
  15659. #ifdef CONFIG_IRQ_FORCED_THREADING
  15660. +# ifndef CONFIG_PREEMPT_RT_BASE
  15661. __read_mostly bool force_irqthreads;
  15662. static int __init setup_forced_irqthreads(char *arg)
  15663. @@ -30,6 +31,7 @@
  15664. return 0;
  15665. }
  15666. early_param("threadirqs", setup_forced_irqthreads);
  15667. +# endif
  15668. #endif
  15669. static void __synchronize_hardirq(struct irq_desc *desc)
  15670. @@ -181,6 +183,62 @@
  15671. irq_get_pending(struct cpumask *mask, struct irq_desc *desc) { }
  15672. #endif
  15673. +#ifdef CONFIG_PREEMPT_RT_FULL
  15674. +static void _irq_affinity_notify(struct irq_affinity_notify *notify);
  15675. +static struct task_struct *set_affinity_helper;
  15676. +static LIST_HEAD(affinity_list);
  15677. +static DEFINE_RAW_SPINLOCK(affinity_list_lock);
  15678. +
  15679. +static int set_affinity_thread(void *unused)
  15680. +{
  15681. + while (1) {
  15682. + struct irq_affinity_notify *notify;
  15683. + int empty;
  15684. +
  15685. + set_current_state(TASK_INTERRUPTIBLE);
  15686. +
  15687. + raw_spin_lock_irq(&affinity_list_lock);
  15688. + empty = list_empty(&affinity_list);
  15689. + raw_spin_unlock_irq(&affinity_list_lock);
  15690. +
  15691. + if (empty)
  15692. + schedule();
  15693. + if (kthread_should_stop())
  15694. + break;
  15695. + set_current_state(TASK_RUNNING);
  15696. +try_next:
  15697. + notify = NULL;
  15698. +
  15699. + raw_spin_lock_irq(&affinity_list_lock);
  15700. + if (!list_empty(&affinity_list)) {
  15701. + notify = list_first_entry(&affinity_list,
  15702. + struct irq_affinity_notify, list);
  15703. + list_del_init(&notify->list);
  15704. + }
  15705. + raw_spin_unlock_irq(&affinity_list_lock);
  15706. +
  15707. + if (!notify)
  15708. + continue;
  15709. + _irq_affinity_notify(notify);
  15710. + goto try_next;
  15711. + }
  15712. + return 0;
  15713. +}
  15714. +
  15715. +static void init_helper_thread(void)
  15716. +{
  15717. + if (set_affinity_helper)
  15718. + return;
  15719. + set_affinity_helper = kthread_run(set_affinity_thread, NULL,
  15720. + "affinity-cb");
  15721. + WARN_ON(IS_ERR(set_affinity_helper));
  15722. +}
  15723. +#else
  15724. +
  15725. +static inline void init_helper_thread(void) { }
  15726. +
  15727. +#endif
  15728. +
  15729. int irq_do_set_affinity(struct irq_data *data, const struct cpumask *mask,
  15730. bool force)
  15731. {
  15732. @@ -220,7 +278,17 @@
  15733. if (desc->affinity_notify) {
  15734. kref_get(&desc->affinity_notify->kref);
  15735. +
  15736. +#ifdef CONFIG_PREEMPT_RT_FULL
  15737. + raw_spin_lock(&affinity_list_lock);
  15738. + if (list_empty(&desc->affinity_notify->list))
  15739. + list_add_tail(&affinity_list,
  15740. + &desc->affinity_notify->list);
  15741. + raw_spin_unlock(&affinity_list_lock);
  15742. + wake_up_process(set_affinity_helper);
  15743. +#else
  15744. schedule_work(&desc->affinity_notify->work);
  15745. +#endif
  15746. }
  15747. irqd_set(data, IRQD_AFFINITY_SET);
  15748. @@ -258,10 +326,8 @@
  15749. }
  15750. EXPORT_SYMBOL_GPL(irq_set_affinity_hint);
  15751. -static void irq_affinity_notify(struct work_struct *work)
  15752. +static void _irq_affinity_notify(struct irq_affinity_notify *notify)
  15753. {
  15754. - struct irq_affinity_notify *notify =
  15755. - container_of(work, struct irq_affinity_notify, work);
  15756. struct irq_desc *desc = irq_to_desc(notify->irq);
  15757. cpumask_var_t cpumask;
  15758. unsigned long flags;
  15759. @@ -283,6 +349,13 @@
  15760. kref_put(&notify->kref, notify->release);
  15761. }
  15762. +static void irq_affinity_notify(struct work_struct *work)
  15763. +{
  15764. + struct irq_affinity_notify *notify =
  15765. + container_of(work, struct irq_affinity_notify, work);
  15766. + _irq_affinity_notify(notify);
  15767. +}
  15768. +
  15769. /**
  15770. * irq_set_affinity_notifier - control notification of IRQ affinity changes
  15771. * @irq: Interrupt for which to enable/disable notification
  15772. @@ -312,6 +385,8 @@
  15773. notify->irq = irq;
  15774. kref_init(&notify->kref);
  15775. INIT_WORK(&notify->work, irq_affinity_notify);
  15776. + INIT_LIST_HEAD(&notify->list);
  15777. + init_helper_thread();
  15778. }
  15779. raw_spin_lock_irqsave(&desc->lock, flags);
  15780. @@ -865,7 +940,15 @@
  15781. local_bh_disable();
  15782. ret = action->thread_fn(action->irq, action->dev_id);
  15783. irq_finalize_oneshot(desc, action);
  15784. - local_bh_enable();
  15785. + /*
  15786. + * Interrupts which have real time requirements can be set up
  15787. + * to avoid softirq processing in the thread handler. This is
  15788. + * safe as these interrupts do not raise soft interrupts.
  15789. + */
  15790. + if (irq_settings_no_softirq_call(desc))
  15791. + _local_bh_enable();
  15792. + else
  15793. + local_bh_enable();
  15794. return ret;
  15795. }
  15796. @@ -962,6 +1045,12 @@
  15797. if (action_ret == IRQ_WAKE_THREAD)
  15798. irq_wake_secondary(desc, action);
  15799. +#ifdef CONFIG_PREEMPT_RT_FULL
  15800. + migrate_disable();
  15801. + add_interrupt_randomness(action->irq, 0,
  15802. + desc->random_ip ^ (unsigned long) action);
  15803. + migrate_enable();
  15804. +#endif
  15805. wake_threads_waitq(desc);
  15806. }
  15807. @@ -1315,6 +1404,9 @@
  15808. irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
  15809. }
  15810. + if (new->flags & IRQF_NO_SOFTIRQ_CALL)
  15811. + irq_settings_set_no_softirq_call(desc);
  15812. +
  15813. /* Set default affinity mask once everything is setup */
  15814. setup_affinity(desc, mask);
  15815. @@ -1968,7 +2060,7 @@
  15816. * This call sets the internal irqchip state of an interrupt,
  15817. * depending on the value of @which.
  15818. *
  15819. - * This function should be called with preemption disabled if the
  15820. + * This function should be called with migration disabled if the
  15821. * interrupt controller has per-cpu registers.
  15822. */
  15823. int irq_set_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
  15824. diff -Nur linux-4.4.28.orig/kernel/irq/settings.h linux-4.4.28/kernel/irq/settings.h
  15825. --- linux-4.4.28.orig/kernel/irq/settings.h 2016-10-28 09:53:25.000000000 +0200
  15826. +++ linux-4.4.28/kernel/irq/settings.h 2016-11-01 21:26:58.764341506 +0100
  15827. @@ -16,6 +16,7 @@
  15828. _IRQ_PER_CPU_DEVID = IRQ_PER_CPU_DEVID,
  15829. _IRQ_IS_POLLED = IRQ_IS_POLLED,
  15830. _IRQ_DISABLE_UNLAZY = IRQ_DISABLE_UNLAZY,
  15831. + _IRQ_NO_SOFTIRQ_CALL = IRQ_NO_SOFTIRQ_CALL,
  15832. _IRQF_MODIFY_MASK = IRQF_MODIFY_MASK,
  15833. };
  15834. @@ -30,6 +31,7 @@
  15835. #define IRQ_PER_CPU_DEVID GOT_YOU_MORON
  15836. #define IRQ_IS_POLLED GOT_YOU_MORON
  15837. #define IRQ_DISABLE_UNLAZY GOT_YOU_MORON
  15838. +#define IRQ_NO_SOFTIRQ_CALL GOT_YOU_MORON
  15839. #undef IRQF_MODIFY_MASK
  15840. #define IRQF_MODIFY_MASK GOT_YOU_MORON
  15841. @@ -40,6 +42,16 @@
  15842. desc->status_use_accessors |= (set & _IRQF_MODIFY_MASK);
  15843. }
  15844. +static inline bool irq_settings_no_softirq_call(struct irq_desc *desc)
  15845. +{
  15846. + return desc->status_use_accessors & _IRQ_NO_SOFTIRQ_CALL;
  15847. +}
  15848. +
  15849. +static inline void irq_settings_set_no_softirq_call(struct irq_desc *desc)
  15850. +{
  15851. + desc->status_use_accessors |= _IRQ_NO_SOFTIRQ_CALL;
  15852. +}
  15853. +
  15854. static inline bool irq_settings_is_per_cpu(struct irq_desc *desc)
  15855. {
  15856. return desc->status_use_accessors & _IRQ_PER_CPU;
  15857. diff -Nur linux-4.4.28.orig/kernel/irq/spurious.c linux-4.4.28/kernel/irq/spurious.c
  15858. --- linux-4.4.28.orig/kernel/irq/spurious.c 2016-10-28 09:53:25.000000000 +0200
  15859. +++ linux-4.4.28/kernel/irq/spurious.c 2016-11-01 21:26:58.764341506 +0100
  15860. @@ -444,6 +444,10 @@
  15861. static int __init irqfixup_setup(char *str)
  15862. {
  15863. +#ifdef CONFIG_PREEMPT_RT_BASE
  15864. + pr_warn("irqfixup boot option not supported w/ CONFIG_PREEMPT_RT_BASE\n");
  15865. + return 1;
  15866. +#endif
  15867. irqfixup = 1;
  15868. printk(KERN_WARNING "Misrouted IRQ fixup support enabled.\n");
  15869. printk(KERN_WARNING "This may impact system performance.\n");
  15870. @@ -456,6 +460,10 @@
  15871. static int __init irqpoll_setup(char *str)
  15872. {
  15873. +#ifdef CONFIG_PREEMPT_RT_BASE
  15874. + pr_warn("irqpoll boot option not supported w/ CONFIG_PREEMPT_RT_BASE\n");
  15875. + return 1;
  15876. +#endif
  15877. irqfixup = 2;
  15878. printk(KERN_WARNING "Misrouted IRQ fixup and polling support "
  15879. "enabled\n");
  15880. diff -Nur linux-4.4.28.orig/kernel/irq_work.c linux-4.4.28/kernel/irq_work.c
  15881. --- linux-4.4.28.orig/kernel/irq_work.c 2016-10-28 09:53:25.000000000 +0200
  15882. +++ linux-4.4.28/kernel/irq_work.c 2016-11-01 21:26:58.768341770 +0100
  15883. @@ -17,6 +17,7 @@
  15884. #include <linux/cpu.h>
  15885. #include <linux/notifier.h>
  15886. #include <linux/smp.h>
  15887. +#include <linux/interrupt.h>
  15888. #include <asm/processor.h>
  15889. @@ -65,6 +66,8 @@
  15890. */
  15891. bool irq_work_queue_on(struct irq_work *work, int cpu)
  15892. {
  15893. + struct llist_head *list;
  15894. +
  15895. /* All work should have been flushed before going offline */
  15896. WARN_ON_ONCE(cpu_is_offline(cpu));
  15897. @@ -75,7 +78,12 @@
  15898. if (!irq_work_claim(work))
  15899. return false;
  15900. - if (llist_add(&work->llnode, &per_cpu(raised_list, cpu)))
  15901. + if (IS_ENABLED(CONFIG_PREEMPT_RT_FULL) && !(work->flags & IRQ_WORK_HARD_IRQ))
  15902. + list = &per_cpu(lazy_list, cpu);
  15903. + else
  15904. + list = &per_cpu(raised_list, cpu);
  15905. +
  15906. + if (llist_add(&work->llnode, list))
  15907. arch_send_call_function_single_ipi(cpu);
  15908. return true;
  15909. @@ -86,6 +94,9 @@
  15910. /* Enqueue the irq work @work on the current CPU */
  15911. bool irq_work_queue(struct irq_work *work)
  15912. {
  15913. + struct llist_head *list;
  15914. + bool lazy_work, realtime = IS_ENABLED(CONFIG_PREEMPT_RT_FULL);
  15915. +
  15916. /* Only queue if not already pending */
  15917. if (!irq_work_claim(work))
  15918. return false;
  15919. @@ -93,13 +104,15 @@
  15920. /* Queue the entry and raise the IPI if needed. */
  15921. preempt_disable();
  15922. - /* If the work is "lazy", handle it from next tick if any */
  15923. - if (work->flags & IRQ_WORK_LAZY) {
  15924. - if (llist_add(&work->llnode, this_cpu_ptr(&lazy_list)) &&
  15925. - tick_nohz_tick_stopped())
  15926. - arch_irq_work_raise();
  15927. - } else {
  15928. - if (llist_add(&work->llnode, this_cpu_ptr(&raised_list)))
  15929. + lazy_work = work->flags & IRQ_WORK_LAZY;
  15930. +
  15931. + if (lazy_work || (realtime && !(work->flags & IRQ_WORK_HARD_IRQ)))
  15932. + list = this_cpu_ptr(&lazy_list);
  15933. + else
  15934. + list = this_cpu_ptr(&raised_list);
  15935. +
  15936. + if (llist_add(&work->llnode, list)) {
  15937. + if (!lazy_work || tick_nohz_tick_stopped())
  15938. arch_irq_work_raise();
  15939. }
  15940. @@ -116,9 +129,8 @@
  15941. raised = this_cpu_ptr(&raised_list);
  15942. lazy = this_cpu_ptr(&lazy_list);
  15943. - if (llist_empty(raised) || arch_irq_work_has_interrupt())
  15944. - if (llist_empty(lazy))
  15945. - return false;
  15946. + if (llist_empty(raised) && llist_empty(lazy))
  15947. + return false;
  15948. /* All work should have been flushed before going offline */
  15949. WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
  15950. @@ -132,7 +144,7 @@
  15951. struct irq_work *work;
  15952. struct llist_node *llnode;
  15953. - BUG_ON(!irqs_disabled());
  15954. + BUG_ON_NONRT(!irqs_disabled());
  15955. if (llist_empty(list))
  15956. return;
  15957. @@ -169,7 +181,16 @@
  15958. void irq_work_run(void)
  15959. {
  15960. irq_work_run_list(this_cpu_ptr(&raised_list));
  15961. - irq_work_run_list(this_cpu_ptr(&lazy_list));
  15962. + if (IS_ENABLED(CONFIG_PREEMPT_RT_FULL)) {
  15963. + /*
  15964. + * NOTE: we raise softirq via IPI for safety,
  15965. + * and execute in irq_work_tick() to move the
  15966. + * overhead from hard to soft irq context.
  15967. + */
  15968. + if (!llist_empty(this_cpu_ptr(&lazy_list)))
  15969. + raise_softirq(TIMER_SOFTIRQ);
  15970. + } else
  15971. + irq_work_run_list(this_cpu_ptr(&lazy_list));
  15972. }
  15973. EXPORT_SYMBOL_GPL(irq_work_run);
  15974. @@ -179,8 +200,17 @@
  15975. if (!llist_empty(raised) && !arch_irq_work_has_interrupt())
  15976. irq_work_run_list(raised);
  15977. +
  15978. + if (!IS_ENABLED(CONFIG_PREEMPT_RT_FULL))
  15979. + irq_work_run_list(this_cpu_ptr(&lazy_list));
  15980. +}
  15981. +
  15982. +#if defined(CONFIG_IRQ_WORK) && defined(CONFIG_PREEMPT_RT_FULL)
  15983. +void irq_work_tick_soft(void)
  15984. +{
  15985. irq_work_run_list(this_cpu_ptr(&lazy_list));
  15986. }
  15987. +#endif
  15988. /*
  15989. * Synchronize against the irq_work @entry, ensures the entry is not
  15990. diff -Nur linux-4.4.28.orig/kernel/Kconfig.locks linux-4.4.28/kernel/Kconfig.locks
  15991. --- linux-4.4.28.orig/kernel/Kconfig.locks 2016-10-28 09:53:25.000000000 +0200
  15992. +++ linux-4.4.28/kernel/Kconfig.locks 2016-11-01 21:26:58.756340996 +0100
  15993. @@ -225,11 +225,11 @@
  15994. config MUTEX_SPIN_ON_OWNER
  15995. def_bool y
  15996. - depends on SMP && !DEBUG_MUTEXES && ARCH_SUPPORTS_ATOMIC_RMW
  15997. + depends on SMP && !DEBUG_MUTEXES && ARCH_SUPPORTS_ATOMIC_RMW && !PREEMPT_RT_FULL
  15998. config RWSEM_SPIN_ON_OWNER
  15999. def_bool y
  16000. - depends on SMP && RWSEM_XCHGADD_ALGORITHM && ARCH_SUPPORTS_ATOMIC_RMW
  16001. + depends on SMP && RWSEM_XCHGADD_ALGORITHM && ARCH_SUPPORTS_ATOMIC_RMW && !PREEMPT_RT_FULL
  16002. config LOCK_SPIN_ON_OWNER
  16003. def_bool y
  16004. diff -Nur linux-4.4.28.orig/kernel/Kconfig.preempt linux-4.4.28/kernel/Kconfig.preempt
  16005. --- linux-4.4.28.orig/kernel/Kconfig.preempt 2016-10-28 09:53:25.000000000 +0200
  16006. +++ linux-4.4.28/kernel/Kconfig.preempt 2016-11-01 21:26:58.756340996 +0100
  16007. @@ -1,3 +1,16 @@
  16008. +config PREEMPT
  16009. + bool
  16010. + select PREEMPT_COUNT
  16011. +
  16012. +config PREEMPT_RT_BASE
  16013. + bool
  16014. + select PREEMPT
  16015. +
  16016. +config HAVE_PREEMPT_LAZY
  16017. + bool
  16018. +
  16019. +config PREEMPT_LAZY
  16020. + def_bool y if HAVE_PREEMPT_LAZY && PREEMPT_RT_FULL
  16021. choice
  16022. prompt "Preemption Model"
  16023. @@ -33,9 +46,9 @@
  16024. Select this if you are building a kernel for a desktop system.
  16025. -config PREEMPT
  16026. +config PREEMPT__LL
  16027. bool "Preemptible Kernel (Low-Latency Desktop)"
  16028. - select PREEMPT_COUNT
  16029. + select PREEMPT
  16030. select UNINLINE_SPIN_UNLOCK if !ARCH_INLINE_SPIN_UNLOCK
  16031. help
  16032. This option reduces the latency of the kernel by making
  16033. @@ -52,6 +65,22 @@
  16034. embedded system with latency requirements in the milliseconds
  16035. range.
  16036. +config PREEMPT_RTB
  16037. + bool "Preemptible Kernel (Basic RT)"
  16038. + select PREEMPT_RT_BASE
  16039. + help
  16040. + This option is basically the same as (Low-Latency Desktop) but
  16041. + enables changes which are preliminary for the full preemptible
  16042. + RT kernel.
  16043. +
  16044. +config PREEMPT_RT_FULL
  16045. + bool "Fully Preemptible Kernel (RT)"
  16046. + depends on IRQ_FORCED_THREADING
  16047. + select PREEMPT_RT_BASE
  16048. + select PREEMPT_RCU
  16049. + help
  16050. + All and everything
  16051. +
  16052. endchoice
  16053. config PREEMPT_COUNT
  16054. diff -Nur linux-4.4.28.orig/kernel/ksysfs.c linux-4.4.28/kernel/ksysfs.c
  16055. --- linux-4.4.28.orig/kernel/ksysfs.c 2016-10-28 09:53:25.000000000 +0200
  16056. +++ linux-4.4.28/kernel/ksysfs.c 2016-11-01 21:26:58.768341770 +0100
  16057. @@ -136,6 +136,15 @@
  16058. #endif /* CONFIG_KEXEC_CORE */
  16059. +#if defined(CONFIG_PREEMPT_RT_FULL)
  16060. +static ssize_t realtime_show(struct kobject *kobj,
  16061. + struct kobj_attribute *attr, char *buf)
  16062. +{
  16063. + return sprintf(buf, "%d\n", 1);
  16064. +}
  16065. +KERNEL_ATTR_RO(realtime);
  16066. +#endif
  16067. +
  16068. /* whether file capabilities are enabled */
  16069. static ssize_t fscaps_show(struct kobject *kobj,
  16070. struct kobj_attribute *attr, char *buf)
  16071. @@ -203,6 +212,9 @@
  16072. &vmcoreinfo_attr.attr,
  16073. #endif
  16074. &rcu_expedited_attr.attr,
  16075. +#ifdef CONFIG_PREEMPT_RT_FULL
  16076. + &realtime_attr.attr,
  16077. +#endif
  16078. NULL
  16079. };
  16080. diff -Nur linux-4.4.28.orig/kernel/locking/lglock.c linux-4.4.28/kernel/locking/lglock.c
  16081. --- linux-4.4.28.orig/kernel/locking/lglock.c 2016-10-28 09:53:25.000000000 +0200
  16082. +++ linux-4.4.28/kernel/locking/lglock.c 2016-11-01 21:26:58.768341770 +0100
  16083. @@ -4,6 +4,15 @@
  16084. #include <linux/cpu.h>
  16085. #include <linux/string.h>
  16086. +#ifndef CONFIG_PREEMPT_RT_FULL
  16087. +# define lg_lock_ptr arch_spinlock_t
  16088. +# define lg_do_lock(l) arch_spin_lock(l)
  16089. +# define lg_do_unlock(l) arch_spin_unlock(l)
  16090. +#else
  16091. +# define lg_lock_ptr struct rt_mutex
  16092. +# define lg_do_lock(l) __rt_spin_lock__no_mg(l)
  16093. +# define lg_do_unlock(l) __rt_spin_unlock(l)
  16094. +#endif
  16095. /*
  16096. * Note there is no uninit, so lglocks cannot be defined in
  16097. * modules (but it's fine to use them from there)
  16098. @@ -12,51 +21,60 @@
  16099. void lg_lock_init(struct lglock *lg, char *name)
  16100. {
  16101. +#ifdef CONFIG_PREEMPT_RT_FULL
  16102. + int i;
  16103. +
  16104. + for_each_possible_cpu(i) {
  16105. + struct rt_mutex *lock = per_cpu_ptr(lg->lock, i);
  16106. +
  16107. + rt_mutex_init(lock);
  16108. + }
  16109. +#endif
  16110. LOCKDEP_INIT_MAP(&lg->lock_dep_map, name, &lg->lock_key, 0);
  16111. }
  16112. EXPORT_SYMBOL(lg_lock_init);
  16113. void lg_local_lock(struct lglock *lg)
  16114. {
  16115. - arch_spinlock_t *lock;
  16116. + lg_lock_ptr *lock;
  16117. - preempt_disable();
  16118. + migrate_disable();
  16119. lock_acquire_shared(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  16120. lock = this_cpu_ptr(lg->lock);
  16121. - arch_spin_lock(lock);
  16122. + lg_do_lock(lock);
  16123. }
  16124. EXPORT_SYMBOL(lg_local_lock);
  16125. void lg_local_unlock(struct lglock *lg)
  16126. {
  16127. - arch_spinlock_t *lock;
  16128. + lg_lock_ptr *lock;
  16129. lock_release(&lg->lock_dep_map, 1, _RET_IP_);
  16130. lock = this_cpu_ptr(lg->lock);
  16131. - arch_spin_unlock(lock);
  16132. - preempt_enable();
  16133. + lg_do_unlock(lock);
  16134. + migrate_enable();
  16135. }
  16136. EXPORT_SYMBOL(lg_local_unlock);
  16137. void lg_local_lock_cpu(struct lglock *lg, int cpu)
  16138. {
  16139. - arch_spinlock_t *lock;
  16140. + lg_lock_ptr *lock;
  16141. - preempt_disable();
  16142. + preempt_disable_nort();
  16143. lock_acquire_shared(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  16144. lock = per_cpu_ptr(lg->lock, cpu);
  16145. - arch_spin_lock(lock);
  16146. + lg_do_lock(lock);
  16147. }
  16148. EXPORT_SYMBOL(lg_local_lock_cpu);
  16149. void lg_local_unlock_cpu(struct lglock *lg, int cpu)
  16150. {
  16151. - arch_spinlock_t *lock;
  16152. + lg_lock_ptr *lock;
  16153. lock_release(&lg->lock_dep_map, 1, _RET_IP_);
  16154. lock = per_cpu_ptr(lg->lock, cpu);
  16155. - arch_spin_unlock(lock);
  16156. - preempt_enable();
  16157. + lg_do_unlock(lock);
  16158. + preempt_enable_nort();
  16159. }
  16160. EXPORT_SYMBOL(lg_local_unlock_cpu);
  16161. @@ -68,30 +86,30 @@
  16162. if (cpu2 < cpu1)
  16163. swap(cpu1, cpu2);
  16164. - preempt_disable();
  16165. + preempt_disable_nort();
  16166. lock_acquire_shared(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  16167. - arch_spin_lock(per_cpu_ptr(lg->lock, cpu1));
  16168. - arch_spin_lock(per_cpu_ptr(lg->lock, cpu2));
  16169. + lg_do_lock(per_cpu_ptr(lg->lock, cpu1));
  16170. + lg_do_lock(per_cpu_ptr(lg->lock, cpu2));
  16171. }
  16172. void lg_double_unlock(struct lglock *lg, int cpu1, int cpu2)
  16173. {
  16174. lock_release(&lg->lock_dep_map, 1, _RET_IP_);
  16175. - arch_spin_unlock(per_cpu_ptr(lg->lock, cpu1));
  16176. - arch_spin_unlock(per_cpu_ptr(lg->lock, cpu2));
  16177. - preempt_enable();
  16178. + lg_do_unlock(per_cpu_ptr(lg->lock, cpu1));
  16179. + lg_do_unlock(per_cpu_ptr(lg->lock, cpu2));
  16180. + preempt_enable_nort();
  16181. }
  16182. void lg_global_lock(struct lglock *lg)
  16183. {
  16184. int i;
  16185. - preempt_disable();
  16186. + preempt_disable_nort();
  16187. lock_acquire_exclusive(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  16188. for_each_possible_cpu(i) {
  16189. - arch_spinlock_t *lock;
  16190. + lg_lock_ptr *lock;
  16191. lock = per_cpu_ptr(lg->lock, i);
  16192. - arch_spin_lock(lock);
  16193. + lg_do_lock(lock);
  16194. }
  16195. }
  16196. EXPORT_SYMBOL(lg_global_lock);
  16197. @@ -102,10 +120,35 @@
  16198. lock_release(&lg->lock_dep_map, 1, _RET_IP_);
  16199. for_each_possible_cpu(i) {
  16200. - arch_spinlock_t *lock;
  16201. + lg_lock_ptr *lock;
  16202. lock = per_cpu_ptr(lg->lock, i);
  16203. - arch_spin_unlock(lock);
  16204. + lg_do_unlock(lock);
  16205. }
  16206. - preempt_enable();
  16207. + preempt_enable_nort();
  16208. }
  16209. EXPORT_SYMBOL(lg_global_unlock);
  16210. +
  16211. +#ifdef CONFIG_PREEMPT_RT_FULL
  16212. +/*
  16213. + * HACK: If you use this, you get to keep the pieces.
  16214. + * Used in queue_stop_cpus_work() when stop machinery
  16215. + * is called from inactive CPU, so we can't schedule.
  16216. + */
  16217. +# define lg_do_trylock_relax(l) \
  16218. + do { \
  16219. + while (!__rt_spin_trylock(l)) \
  16220. + cpu_relax(); \
  16221. + } while (0)
  16222. +
  16223. +void lg_global_trylock_relax(struct lglock *lg)
  16224. +{
  16225. + int i;
  16226. +
  16227. + lock_acquire_exclusive(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  16228. + for_each_possible_cpu(i) {
  16229. + lg_lock_ptr *lock;
  16230. + lock = per_cpu_ptr(lg->lock, i);
  16231. + lg_do_trylock_relax(lock);
  16232. + }
  16233. +}
  16234. +#endif
  16235. diff -Nur linux-4.4.28.orig/kernel/locking/lockdep.c linux-4.4.28/kernel/locking/lockdep.c
  16236. --- linux-4.4.28.orig/kernel/locking/lockdep.c 2016-10-28 09:53:25.000000000 +0200
  16237. +++ linux-4.4.28/kernel/locking/lockdep.c 2016-11-01 21:26:58.768341770 +0100
  16238. @@ -3525,6 +3525,7 @@
  16239. }
  16240. }
  16241. +#ifndef CONFIG_PREEMPT_RT_FULL
  16242. /*
  16243. * We dont accurately track softirq state in e.g.
  16244. * hardirq contexts (such as on 4KSTACKS), so only
  16245. @@ -3539,6 +3540,7 @@
  16246. DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
  16247. }
  16248. }
  16249. +#endif
  16250. if (!debug_locks)
  16251. print_irqtrace_events(current);
  16252. diff -Nur linux-4.4.28.orig/kernel/locking/locktorture.c linux-4.4.28/kernel/locking/locktorture.c
  16253. --- linux-4.4.28.orig/kernel/locking/locktorture.c 2016-10-28 09:53:25.000000000 +0200
  16254. +++ linux-4.4.28/kernel/locking/locktorture.c 2016-11-01 21:26:58.768341770 +0100
  16255. @@ -26,7 +26,6 @@
  16256. #include <linux/kthread.h>
  16257. #include <linux/sched/rt.h>
  16258. #include <linux/spinlock.h>
  16259. -#include <linux/rwlock.h>
  16260. #include <linux/mutex.h>
  16261. #include <linux/rwsem.h>
  16262. #include <linux/smp.h>
  16263. diff -Nur linux-4.4.28.orig/kernel/locking/Makefile linux-4.4.28/kernel/locking/Makefile
  16264. --- linux-4.4.28.orig/kernel/locking/Makefile 2016-10-28 09:53:25.000000000 +0200
  16265. +++ linux-4.4.28/kernel/locking/Makefile 2016-11-01 21:26:58.768341770 +0100
  16266. @@ -1,5 +1,5 @@
  16267. -obj-y += mutex.o semaphore.o rwsem.o percpu-rwsem.o
  16268. +obj-y += semaphore.o percpu-rwsem.o
  16269. ifdef CONFIG_FUNCTION_TRACER
  16270. CFLAGS_REMOVE_lockdep.o = $(CC_FLAGS_FTRACE)
  16271. @@ -8,7 +8,11 @@
  16272. CFLAGS_REMOVE_rtmutex-debug.o = $(CC_FLAGS_FTRACE)
  16273. endif
  16274. +ifneq ($(CONFIG_PREEMPT_RT_FULL),y)
  16275. +obj-y += mutex.o
  16276. obj-$(CONFIG_DEBUG_MUTEXES) += mutex-debug.o
  16277. +obj-y += rwsem.o
  16278. +endif
  16279. obj-$(CONFIG_LOCKDEP) += lockdep.o
  16280. ifeq ($(CONFIG_PROC_FS),y)
  16281. obj-$(CONFIG_LOCKDEP) += lockdep_proc.o
  16282. @@ -22,7 +26,10 @@
  16283. obj-$(CONFIG_DEBUG_RT_MUTEXES) += rtmutex-debug.o
  16284. obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock.o
  16285. obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock_debug.o
  16286. +ifneq ($(CONFIG_PREEMPT_RT_FULL),y)
  16287. obj-$(CONFIG_RWSEM_GENERIC_SPINLOCK) += rwsem-spinlock.o
  16288. obj-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem-xadd.o
  16289. +endif
  16290. +obj-$(CONFIG_PREEMPT_RT_FULL) += rt.o
  16291. obj-$(CONFIG_QUEUED_RWLOCKS) += qrwlock.o
  16292. obj-$(CONFIG_LOCK_TORTURE_TEST) += locktorture.o
  16293. diff -Nur linux-4.4.28.orig/kernel/locking/rt.c linux-4.4.28/kernel/locking/rt.c
  16294. --- linux-4.4.28.orig/kernel/locking/rt.c 1970-01-01 01:00:00.000000000 +0100
  16295. +++ linux-4.4.28/kernel/locking/rt.c 2016-11-01 21:26:58.768341770 +0100
  16296. @@ -0,0 +1,474 @@
  16297. +/*
  16298. + * kernel/rt.c
  16299. + *
  16300. + * Real-Time Preemption Support
  16301. + *
  16302. + * started by Ingo Molnar:
  16303. + *
  16304. + * Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  16305. + * Copyright (C) 2006, Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  16306. + *
  16307. + * historic credit for proving that Linux spinlocks can be implemented via
  16308. + * RT-aware mutexes goes to many people: The Pmutex project (Dirk Grambow
  16309. + * and others) who prototyped it on 2.4 and did lots of comparative
  16310. + * research and analysis; TimeSys, for proving that you can implement a
  16311. + * fully preemptible kernel via the use of IRQ threading and mutexes;
  16312. + * Bill Huey for persuasively arguing on lkml that the mutex model is the
  16313. + * right one; and to MontaVista, who ported pmutexes to 2.6.
  16314. + *
  16315. + * This code is a from-scratch implementation and is not based on pmutexes,
  16316. + * but the idea of converting spinlocks to mutexes is used here too.
  16317. + *
  16318. + * lock debugging, locking tree, deadlock detection:
  16319. + *
  16320. + * Copyright (C) 2004, LynuxWorks, Inc., Igor Manyilov, Bill Huey
  16321. + * Released under the General Public License (GPL).
  16322. + *
  16323. + * Includes portions of the generic R/W semaphore implementation from:
  16324. + *
  16325. + * Copyright (c) 2001 David Howells (dhowells@redhat.com).
  16326. + * - Derived partially from idea by Andrea Arcangeli <andrea@suse.de>
  16327. + * - Derived also from comments by Linus
  16328. + *
  16329. + * Pending ownership of locks and ownership stealing:
  16330. + *
  16331. + * Copyright (C) 2005, Kihon Technologies Inc., Steven Rostedt
  16332. + *
  16333. + * (also by Steven Rostedt)
  16334. + * - Converted single pi_lock to individual task locks.
  16335. + *
  16336. + * By Esben Nielsen:
  16337. + * Doing priority inheritance with help of the scheduler.
  16338. + *
  16339. + * Copyright (C) 2006, Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  16340. + * - major rework based on Esben Nielsens initial patch
  16341. + * - replaced thread_info references by task_struct refs
  16342. + * - removed task->pending_owner dependency
  16343. + * - BKL drop/reacquire for semaphore style locks to avoid deadlocks
  16344. + * in the scheduler return path as discussed with Steven Rostedt
  16345. + *
  16346. + * Copyright (C) 2006, Kihon Technologies Inc.
  16347. + * Steven Rostedt <rostedt@goodmis.org>
  16348. + * - debugged and patched Thomas Gleixner's rework.
  16349. + * - added back the cmpxchg to the rework.
  16350. + * - turned atomic require back on for SMP.
  16351. + */
  16352. +
  16353. +#include <linux/spinlock.h>
  16354. +#include <linux/rtmutex.h>
  16355. +#include <linux/sched.h>
  16356. +#include <linux/delay.h>
  16357. +#include <linux/module.h>
  16358. +#include <linux/kallsyms.h>
  16359. +#include <linux/syscalls.h>
  16360. +#include <linux/interrupt.h>
  16361. +#include <linux/plist.h>
  16362. +#include <linux/fs.h>
  16363. +#include <linux/futex.h>
  16364. +#include <linux/hrtimer.h>
  16365. +
  16366. +#include "rtmutex_common.h"
  16367. +
  16368. +/*
  16369. + * struct mutex functions
  16370. + */
  16371. +void __mutex_do_init(struct mutex *mutex, const char *name,
  16372. + struct lock_class_key *key)
  16373. +{
  16374. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  16375. + /*
  16376. + * Make sure we are not reinitializing a held lock:
  16377. + */
  16378. + debug_check_no_locks_freed((void *)mutex, sizeof(*mutex));
  16379. + lockdep_init_map(&mutex->dep_map, name, key, 0);
  16380. +#endif
  16381. + mutex->lock.save_state = 0;
  16382. +}
  16383. +EXPORT_SYMBOL(__mutex_do_init);
  16384. +
  16385. +void __lockfunc _mutex_lock(struct mutex *lock)
  16386. +{
  16387. + mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  16388. + rt_mutex_lock(&lock->lock);
  16389. +}
  16390. +EXPORT_SYMBOL(_mutex_lock);
  16391. +
  16392. +int __lockfunc _mutex_lock_interruptible(struct mutex *lock)
  16393. +{
  16394. + int ret;
  16395. +
  16396. + mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  16397. + ret = rt_mutex_lock_interruptible(&lock->lock);
  16398. + if (ret)
  16399. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  16400. + return ret;
  16401. +}
  16402. +EXPORT_SYMBOL(_mutex_lock_interruptible);
  16403. +
  16404. +int __lockfunc _mutex_lock_killable(struct mutex *lock)
  16405. +{
  16406. + int ret;
  16407. +
  16408. + mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  16409. + ret = rt_mutex_lock_killable(&lock->lock);
  16410. + if (ret)
  16411. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  16412. + return ret;
  16413. +}
  16414. +EXPORT_SYMBOL(_mutex_lock_killable);
  16415. +
  16416. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  16417. +void __lockfunc _mutex_lock_nested(struct mutex *lock, int subclass)
  16418. +{
  16419. + mutex_acquire_nest(&lock->dep_map, subclass, 0, NULL, _RET_IP_);
  16420. + rt_mutex_lock(&lock->lock);
  16421. +}
  16422. +EXPORT_SYMBOL(_mutex_lock_nested);
  16423. +
  16424. +void __lockfunc _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest)
  16425. +{
  16426. + mutex_acquire_nest(&lock->dep_map, 0, 0, nest, _RET_IP_);
  16427. + rt_mutex_lock(&lock->lock);
  16428. +}
  16429. +EXPORT_SYMBOL(_mutex_lock_nest_lock);
  16430. +
  16431. +int __lockfunc _mutex_lock_interruptible_nested(struct mutex *lock, int subclass)
  16432. +{
  16433. + int ret;
  16434. +
  16435. + mutex_acquire_nest(&lock->dep_map, subclass, 0, NULL, _RET_IP_);
  16436. + ret = rt_mutex_lock_interruptible(&lock->lock);
  16437. + if (ret)
  16438. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  16439. + return ret;
  16440. +}
  16441. +EXPORT_SYMBOL(_mutex_lock_interruptible_nested);
  16442. +
  16443. +int __lockfunc _mutex_lock_killable_nested(struct mutex *lock, int subclass)
  16444. +{
  16445. + int ret;
  16446. +
  16447. + mutex_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
  16448. + ret = rt_mutex_lock_killable(&lock->lock);
  16449. + if (ret)
  16450. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  16451. + return ret;
  16452. +}
  16453. +EXPORT_SYMBOL(_mutex_lock_killable_nested);
  16454. +#endif
  16455. +
  16456. +int __lockfunc _mutex_trylock(struct mutex *lock)
  16457. +{
  16458. + int ret = rt_mutex_trylock(&lock->lock);
  16459. +
  16460. + if (ret)
  16461. + mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  16462. +
  16463. + return ret;
  16464. +}
  16465. +EXPORT_SYMBOL(_mutex_trylock);
  16466. +
  16467. +void __lockfunc _mutex_unlock(struct mutex *lock)
  16468. +{
  16469. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  16470. + rt_mutex_unlock(&lock->lock);
  16471. +}
  16472. +EXPORT_SYMBOL(_mutex_unlock);
  16473. +
  16474. +/*
  16475. + * rwlock_t functions
  16476. + */
  16477. +int __lockfunc rt_write_trylock(rwlock_t *rwlock)
  16478. +{
  16479. + int ret;
  16480. +
  16481. + migrate_disable();
  16482. + ret = rt_mutex_trylock(&rwlock->lock);
  16483. + if (ret)
  16484. + rwlock_acquire(&rwlock->dep_map, 0, 1, _RET_IP_);
  16485. + else
  16486. + migrate_enable();
  16487. +
  16488. + return ret;
  16489. +}
  16490. +EXPORT_SYMBOL(rt_write_trylock);
  16491. +
  16492. +int __lockfunc rt_write_trylock_irqsave(rwlock_t *rwlock, unsigned long *flags)
  16493. +{
  16494. + int ret;
  16495. +
  16496. + *flags = 0;
  16497. + ret = rt_write_trylock(rwlock);
  16498. + return ret;
  16499. +}
  16500. +EXPORT_SYMBOL(rt_write_trylock_irqsave);
  16501. +
  16502. +int __lockfunc rt_read_trylock(rwlock_t *rwlock)
  16503. +{
  16504. + struct rt_mutex *lock = &rwlock->lock;
  16505. + int ret = 1;
  16506. +
  16507. + /*
  16508. + * recursive read locks succeed when current owns the lock,
  16509. + * but not when read_depth == 0 which means that the lock is
  16510. + * write locked.
  16511. + */
  16512. + if (rt_mutex_owner(lock) != current) {
  16513. + migrate_disable();
  16514. + ret = rt_mutex_trylock(lock);
  16515. + if (ret)
  16516. + rwlock_acquire(&rwlock->dep_map, 0, 1, _RET_IP_);
  16517. + else
  16518. + migrate_enable();
  16519. +
  16520. + } else if (!rwlock->read_depth) {
  16521. + ret = 0;
  16522. + }
  16523. +
  16524. + if (ret)
  16525. + rwlock->read_depth++;
  16526. +
  16527. + return ret;
  16528. +}
  16529. +EXPORT_SYMBOL(rt_read_trylock);
  16530. +
  16531. +void __lockfunc rt_write_lock(rwlock_t *rwlock)
  16532. +{
  16533. + rwlock_acquire(&rwlock->dep_map, 0, 0, _RET_IP_);
  16534. + __rt_spin_lock(&rwlock->lock);
  16535. +}
  16536. +EXPORT_SYMBOL(rt_write_lock);
  16537. +
  16538. +void __lockfunc rt_read_lock(rwlock_t *rwlock)
  16539. +{
  16540. + struct rt_mutex *lock = &rwlock->lock;
  16541. +
  16542. +
  16543. + /*
  16544. + * recursive read locks succeed when current owns the lock
  16545. + */
  16546. + if (rt_mutex_owner(lock) != current) {
  16547. + rwlock_acquire(&rwlock->dep_map, 0, 0, _RET_IP_);
  16548. + __rt_spin_lock(lock);
  16549. + }
  16550. + rwlock->read_depth++;
  16551. +}
  16552. +
  16553. +EXPORT_SYMBOL(rt_read_lock);
  16554. +
  16555. +void __lockfunc rt_write_unlock(rwlock_t *rwlock)
  16556. +{
  16557. + /* NOTE: we always pass in '1' for nested, for simplicity */
  16558. + rwlock_release(&rwlock->dep_map, 1, _RET_IP_);
  16559. + __rt_spin_unlock(&rwlock->lock);
  16560. + migrate_enable();
  16561. +}
  16562. +EXPORT_SYMBOL(rt_write_unlock);
  16563. +
  16564. +void __lockfunc rt_read_unlock(rwlock_t *rwlock)
  16565. +{
  16566. + /* Release the lock only when read_depth is down to 0 */
  16567. + if (--rwlock->read_depth == 0) {
  16568. + rwlock_release(&rwlock->dep_map, 1, _RET_IP_);
  16569. + __rt_spin_unlock(&rwlock->lock);
  16570. + migrate_enable();
  16571. + }
  16572. +}
  16573. +EXPORT_SYMBOL(rt_read_unlock);
  16574. +
  16575. +unsigned long __lockfunc rt_write_lock_irqsave(rwlock_t *rwlock)
  16576. +{
  16577. + rt_write_lock(rwlock);
  16578. +
  16579. + return 0;
  16580. +}
  16581. +EXPORT_SYMBOL(rt_write_lock_irqsave);
  16582. +
  16583. +unsigned long __lockfunc rt_read_lock_irqsave(rwlock_t *rwlock)
  16584. +{
  16585. + rt_read_lock(rwlock);
  16586. +
  16587. + return 0;
  16588. +}
  16589. +EXPORT_SYMBOL(rt_read_lock_irqsave);
  16590. +
  16591. +void __rt_rwlock_init(rwlock_t *rwlock, char *name, struct lock_class_key *key)
  16592. +{
  16593. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  16594. + /*
  16595. + * Make sure we are not reinitializing a held lock:
  16596. + */
  16597. + debug_check_no_locks_freed((void *)rwlock, sizeof(*rwlock));
  16598. + lockdep_init_map(&rwlock->dep_map, name, key, 0);
  16599. +#endif
  16600. + rwlock->lock.save_state = 1;
  16601. + rwlock->read_depth = 0;
  16602. +}
  16603. +EXPORT_SYMBOL(__rt_rwlock_init);
  16604. +
  16605. +/*
  16606. + * rw_semaphores
  16607. + */
  16608. +
  16609. +void rt_up_write(struct rw_semaphore *rwsem)
  16610. +{
  16611. + rwsem_release(&rwsem->dep_map, 1, _RET_IP_);
  16612. + rt_mutex_unlock(&rwsem->lock);
  16613. +}
  16614. +EXPORT_SYMBOL(rt_up_write);
  16615. +
  16616. +void __rt_up_read(struct rw_semaphore *rwsem)
  16617. +{
  16618. + if (--rwsem->read_depth == 0)
  16619. + rt_mutex_unlock(&rwsem->lock);
  16620. +}
  16621. +
  16622. +void rt_up_read(struct rw_semaphore *rwsem)
  16623. +{
  16624. + rwsem_release(&rwsem->dep_map, 1, _RET_IP_);
  16625. + __rt_up_read(rwsem);
  16626. +}
  16627. +EXPORT_SYMBOL(rt_up_read);
  16628. +
  16629. +/*
  16630. + * downgrade a write lock into a read lock
  16631. + * - just wake up any readers at the front of the queue
  16632. + */
  16633. +void rt_downgrade_write(struct rw_semaphore *rwsem)
  16634. +{
  16635. + BUG_ON(rt_mutex_owner(&rwsem->lock) != current);
  16636. + rwsem->read_depth = 1;
  16637. +}
  16638. +EXPORT_SYMBOL(rt_downgrade_write);
  16639. +
  16640. +int rt_down_write_trylock(struct rw_semaphore *rwsem)
  16641. +{
  16642. + int ret = rt_mutex_trylock(&rwsem->lock);
  16643. +
  16644. + if (ret)
  16645. + rwsem_acquire(&rwsem->dep_map, 0, 1, _RET_IP_);
  16646. + return ret;
  16647. +}
  16648. +EXPORT_SYMBOL(rt_down_write_trylock);
  16649. +
  16650. +void rt_down_write(struct rw_semaphore *rwsem)
  16651. +{
  16652. + rwsem_acquire(&rwsem->dep_map, 0, 0, _RET_IP_);
  16653. + rt_mutex_lock(&rwsem->lock);
  16654. +}
  16655. +EXPORT_SYMBOL(rt_down_write);
  16656. +
  16657. +void rt_down_write_nested(struct rw_semaphore *rwsem, int subclass)
  16658. +{
  16659. + rwsem_acquire(&rwsem->dep_map, subclass, 0, _RET_IP_);
  16660. + rt_mutex_lock(&rwsem->lock);
  16661. +}
  16662. +EXPORT_SYMBOL(rt_down_write_nested);
  16663. +
  16664. +void rt_down_write_nested_lock(struct rw_semaphore *rwsem,
  16665. + struct lockdep_map *nest)
  16666. +{
  16667. + rwsem_acquire_nest(&rwsem->dep_map, 0, 0, nest, _RET_IP_);
  16668. + rt_mutex_lock(&rwsem->lock);
  16669. +}
  16670. +EXPORT_SYMBOL(rt_down_write_nested_lock);
  16671. +
  16672. +int rt__down_read_trylock(struct rw_semaphore *rwsem)
  16673. +{
  16674. + struct rt_mutex *lock = &rwsem->lock;
  16675. + int ret = 1;
  16676. +
  16677. + /*
  16678. + * recursive read locks succeed when current owns the rwsem,
  16679. + * but not when read_depth == 0 which means that the rwsem is
  16680. + * write locked.
  16681. + */
  16682. + if (rt_mutex_owner(lock) != current)
  16683. + ret = rt_mutex_trylock(&rwsem->lock);
  16684. + else if (!rwsem->read_depth)
  16685. + ret = 0;
  16686. +
  16687. + if (ret)
  16688. + rwsem->read_depth++;
  16689. + return ret;
  16690. +
  16691. +}
  16692. +
  16693. +int rt_down_read_trylock(struct rw_semaphore *rwsem)
  16694. +{
  16695. + int ret;
  16696. +
  16697. + ret = rt__down_read_trylock(rwsem);
  16698. + if (ret)
  16699. + rwsem_acquire(&rwsem->dep_map, 0, 1, _RET_IP_);
  16700. +
  16701. + return ret;
  16702. +}
  16703. +EXPORT_SYMBOL(rt_down_read_trylock);
  16704. +
  16705. +void rt__down_read(struct rw_semaphore *rwsem)
  16706. +{
  16707. + struct rt_mutex *lock = &rwsem->lock;
  16708. +
  16709. + if (rt_mutex_owner(lock) != current)
  16710. + rt_mutex_lock(&rwsem->lock);
  16711. + rwsem->read_depth++;
  16712. +}
  16713. +EXPORT_SYMBOL(rt__down_read);
  16714. +
  16715. +static void __rt_down_read(struct rw_semaphore *rwsem, int subclass)
  16716. +{
  16717. + rwsem_acquire_read(&rwsem->dep_map, subclass, 0, _RET_IP_);
  16718. + rt__down_read(rwsem);
  16719. +}
  16720. +
  16721. +void rt_down_read(struct rw_semaphore *rwsem)
  16722. +{
  16723. + __rt_down_read(rwsem, 0);
  16724. +}
  16725. +EXPORT_SYMBOL(rt_down_read);
  16726. +
  16727. +void rt_down_read_nested(struct rw_semaphore *rwsem, int subclass)
  16728. +{
  16729. + __rt_down_read(rwsem, subclass);
  16730. +}
  16731. +EXPORT_SYMBOL(rt_down_read_nested);
  16732. +
  16733. +void __rt_rwsem_init(struct rw_semaphore *rwsem, const char *name,
  16734. + struct lock_class_key *key)
  16735. +{
  16736. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  16737. + /*
  16738. + * Make sure we are not reinitializing a held lock:
  16739. + */
  16740. + debug_check_no_locks_freed((void *)rwsem, sizeof(*rwsem));
  16741. + lockdep_init_map(&rwsem->dep_map, name, key, 0);
  16742. +#endif
  16743. + rwsem->read_depth = 0;
  16744. + rwsem->lock.save_state = 0;
  16745. +}
  16746. +EXPORT_SYMBOL(__rt_rwsem_init);
  16747. +
  16748. +/**
  16749. + * atomic_dec_and_mutex_lock - return holding mutex if we dec to 0
  16750. + * @cnt: the atomic which we are to dec
  16751. + * @lock: the mutex to return holding if we dec to 0
  16752. + *
  16753. + * return true and hold lock if we dec to 0, return false otherwise
  16754. + */
  16755. +int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock)
  16756. +{
  16757. + /* dec if we can't possibly hit 0 */
  16758. + if (atomic_add_unless(cnt, -1, 1))
  16759. + return 0;
  16760. + /* we might hit 0, so take the lock */
  16761. + mutex_lock(lock);
  16762. + if (!atomic_dec_and_test(cnt)) {
  16763. + /* when we actually did the dec, we didn't hit 0 */
  16764. + mutex_unlock(lock);
  16765. + return 0;
  16766. + }
  16767. + /* we hit 0, and we hold the lock */
  16768. + return 1;
  16769. +}
  16770. +EXPORT_SYMBOL(atomic_dec_and_mutex_lock);
  16771. diff -Nur linux-4.4.28.orig/kernel/locking/rtmutex.c linux-4.4.28/kernel/locking/rtmutex.c
  16772. --- linux-4.4.28.orig/kernel/locking/rtmutex.c 2016-10-28 09:53:25.000000000 +0200
  16773. +++ linux-4.4.28/kernel/locking/rtmutex.c 2016-11-01 21:26:58.772342023 +0100
  16774. @@ -7,6 +7,11 @@
  16775. * Copyright (C) 2005-2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  16776. * Copyright (C) 2005 Kihon Technologies Inc., Steven Rostedt
  16777. * Copyright (C) 2006 Esben Nielsen
  16778. + * Adaptive Spinlocks:
  16779. + * Copyright (C) 2008 Novell, Inc., Gregory Haskins, Sven Dietrich,
  16780. + * and Peter Morreale,
  16781. + * Adaptive Spinlocks simplification:
  16782. + * Copyright (C) 2008 Red Hat, Inc., Steven Rostedt <srostedt@redhat.com>
  16783. *
  16784. * See Documentation/locking/rt-mutex-design.txt for details.
  16785. */
  16786. @@ -16,6 +21,7 @@
  16787. #include <linux/sched/rt.h>
  16788. #include <linux/sched/deadline.h>
  16789. #include <linux/timer.h>
  16790. +#include <linux/ww_mutex.h>
  16791. #include "rtmutex_common.h"
  16792. @@ -69,6 +75,12 @@
  16793. clear_rt_mutex_waiters(lock);
  16794. }
  16795. +static int rt_mutex_real_waiter(struct rt_mutex_waiter *waiter)
  16796. +{
  16797. + return waiter && waiter != PI_WAKEUP_INPROGRESS &&
  16798. + waiter != PI_REQUEUE_INPROGRESS;
  16799. +}
  16800. +
  16801. /*
  16802. * We can speed up the acquire/release, if there's no debugging state to be
  16803. * set up.
  16804. @@ -99,13 +111,14 @@
  16805. * 2) Drop lock->wait_lock
  16806. * 3) Try to unlock the lock with cmpxchg
  16807. */
  16808. -static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
  16809. +static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
  16810. + unsigned long flags)
  16811. __releases(lock->wait_lock)
  16812. {
  16813. struct task_struct *owner = rt_mutex_owner(lock);
  16814. clear_rt_mutex_waiters(lock);
  16815. - raw_spin_unlock(&lock->wait_lock);
  16816. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  16817. /*
  16818. * If a new waiter comes in between the unlock and the cmpxchg
  16819. * we have two situations:
  16820. @@ -147,11 +160,12 @@
  16821. /*
  16822. * Simple slow path only version: lock->owner is protected by lock->wait_lock.
  16823. */
  16824. -static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
  16825. +static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
  16826. + unsigned long flags)
  16827. __releases(lock->wait_lock)
  16828. {
  16829. lock->owner = NULL;
  16830. - raw_spin_unlock(&lock->wait_lock);
  16831. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  16832. return true;
  16833. }
  16834. #endif
  16835. @@ -348,6 +362,14 @@
  16836. return debug_rt_mutex_detect_deadlock(waiter, chwalk);
  16837. }
  16838. +static void rt_mutex_wake_waiter(struct rt_mutex_waiter *waiter)
  16839. +{
  16840. + if (waiter->savestate)
  16841. + wake_up_lock_sleeper(waiter->task);
  16842. + else
  16843. + wake_up_process(waiter->task);
  16844. +}
  16845. +
  16846. /*
  16847. * Max number of times we'll walk the boosting chain:
  16848. */
  16849. @@ -355,7 +377,8 @@
  16850. static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
  16851. {
  16852. - return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
  16853. + return rt_mutex_real_waiter(p->pi_blocked_on) ?
  16854. + p->pi_blocked_on->lock : NULL;
  16855. }
  16856. /*
  16857. @@ -433,7 +456,6 @@
  16858. int ret = 0, depth = 0;
  16859. struct rt_mutex *lock;
  16860. bool detect_deadlock;
  16861. - unsigned long flags;
  16862. bool requeue = true;
  16863. detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
  16864. @@ -476,7 +498,7 @@
  16865. /*
  16866. * [1] Task cannot go away as we did a get_task() before !
  16867. */
  16868. - raw_spin_lock_irqsave(&task->pi_lock, flags);
  16869. + raw_spin_lock_irq(&task->pi_lock);
  16870. /*
  16871. * [2] Get the waiter on which @task is blocked on.
  16872. @@ -492,7 +514,7 @@
  16873. * reached or the state of the chain has changed while we
  16874. * dropped the locks.
  16875. */
  16876. - if (!waiter)
  16877. + if (!rt_mutex_real_waiter(waiter))
  16878. goto out_unlock_pi;
  16879. /*
  16880. @@ -560,7 +582,7 @@
  16881. * operations.
  16882. */
  16883. if (!raw_spin_trylock(&lock->wait_lock)) {
  16884. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  16885. + raw_spin_unlock_irq(&task->pi_lock);
  16886. cpu_relax();
  16887. goto retry;
  16888. }
  16889. @@ -591,7 +613,7 @@
  16890. /*
  16891. * No requeue[7] here. Just release @task [8]
  16892. */
  16893. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  16894. + raw_spin_unlock(&task->pi_lock);
  16895. put_task_struct(task);
  16896. /*
  16897. @@ -599,14 +621,14 @@
  16898. * If there is no owner of the lock, end of chain.
  16899. */
  16900. if (!rt_mutex_owner(lock)) {
  16901. - raw_spin_unlock(&lock->wait_lock);
  16902. + raw_spin_unlock_irq(&lock->wait_lock);
  16903. return 0;
  16904. }
  16905. /* [10] Grab the next task, i.e. owner of @lock */
  16906. task = rt_mutex_owner(lock);
  16907. get_task_struct(task);
  16908. - raw_spin_lock_irqsave(&task->pi_lock, flags);
  16909. + raw_spin_lock(&task->pi_lock);
  16910. /*
  16911. * No requeue [11] here. We just do deadlock detection.
  16912. @@ -621,8 +643,8 @@
  16913. top_waiter = rt_mutex_top_waiter(lock);
  16914. /* [13] Drop locks */
  16915. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  16916. - raw_spin_unlock(&lock->wait_lock);
  16917. + raw_spin_unlock(&task->pi_lock);
  16918. + raw_spin_unlock_irq(&lock->wait_lock);
  16919. /* If owner is not blocked, end of chain. */
  16920. if (!next_lock)
  16921. @@ -643,7 +665,7 @@
  16922. rt_mutex_enqueue(lock, waiter);
  16923. /* [8] Release the task */
  16924. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  16925. + raw_spin_unlock(&task->pi_lock);
  16926. put_task_struct(task);
  16927. /*
  16928. @@ -654,21 +676,24 @@
  16929. * follow here. This is the end of the chain we are walking.
  16930. */
  16931. if (!rt_mutex_owner(lock)) {
  16932. + struct rt_mutex_waiter *lock_top_waiter;
  16933. +
  16934. /*
  16935. * If the requeue [7] above changed the top waiter,
  16936. * then we need to wake the new top waiter up to try
  16937. * to get the lock.
  16938. */
  16939. - if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
  16940. - wake_up_process(rt_mutex_top_waiter(lock)->task);
  16941. - raw_spin_unlock(&lock->wait_lock);
  16942. + lock_top_waiter = rt_mutex_top_waiter(lock);
  16943. + if (prerequeue_top_waiter != lock_top_waiter)
  16944. + rt_mutex_wake_waiter(lock_top_waiter);
  16945. + raw_spin_unlock_irq(&lock->wait_lock);
  16946. return 0;
  16947. }
  16948. /* [10] Grab the next task, i.e. the owner of @lock */
  16949. task = rt_mutex_owner(lock);
  16950. get_task_struct(task);
  16951. - raw_spin_lock_irqsave(&task->pi_lock, flags);
  16952. + raw_spin_lock(&task->pi_lock);
  16953. /* [11] requeue the pi waiters if necessary */
  16954. if (waiter == rt_mutex_top_waiter(lock)) {
  16955. @@ -722,8 +747,8 @@
  16956. top_waiter = rt_mutex_top_waiter(lock);
  16957. /* [13] Drop the locks */
  16958. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  16959. - raw_spin_unlock(&lock->wait_lock);
  16960. + raw_spin_unlock(&task->pi_lock);
  16961. + raw_spin_unlock_irq(&lock->wait_lock);
  16962. /*
  16963. * Make the actual exit decisions [12], based on the stored
  16964. @@ -746,28 +771,46 @@
  16965. goto again;
  16966. out_unlock_pi:
  16967. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  16968. + raw_spin_unlock_irq(&task->pi_lock);
  16969. out_put_task:
  16970. put_task_struct(task);
  16971. return ret;
  16972. }
  16973. +
  16974. +#define STEAL_NORMAL 0
  16975. +#define STEAL_LATERAL 1
  16976. +
  16977. +/*
  16978. + * Note that RT tasks are excluded from lateral-steals to prevent the
  16979. + * introduction of an unbounded latency
  16980. + */
  16981. +static inline int lock_is_stealable(struct task_struct *task,
  16982. + struct task_struct *pendowner, int mode)
  16983. +{
  16984. + if (mode == STEAL_NORMAL || rt_task(task)) {
  16985. + if (task->prio >= pendowner->prio)
  16986. + return 0;
  16987. + } else if (task->prio > pendowner->prio)
  16988. + return 0;
  16989. + return 1;
  16990. +}
  16991. +
  16992. /*
  16993. * Try to take an rt-mutex
  16994. *
  16995. - * Must be called with lock->wait_lock held.
  16996. + * Must be called with lock->wait_lock held and interrupts disabled
  16997. *
  16998. * @lock: The lock to be acquired.
  16999. * @task: The task which wants to acquire the lock
  17000. * @waiter: The waiter that is queued to the lock's wait tree if the
  17001. * callsite called task_blocked_on_lock(), otherwise NULL
  17002. */
  17003. -static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  17004. - struct rt_mutex_waiter *waiter)
  17005. +static int __try_to_take_rt_mutex(struct rt_mutex *lock,
  17006. + struct task_struct *task,
  17007. + struct rt_mutex_waiter *waiter, int mode)
  17008. {
  17009. - unsigned long flags;
  17010. -
  17011. /*
  17012. * Before testing whether we can acquire @lock, we set the
  17013. * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
  17014. @@ -803,8 +846,10 @@
  17015. * If waiter is not the highest priority waiter of
  17016. * @lock, give up.
  17017. */
  17018. - if (waiter != rt_mutex_top_waiter(lock))
  17019. + if (waiter != rt_mutex_top_waiter(lock)) {
  17020. + /* XXX lock_is_stealable() ? */
  17021. return 0;
  17022. + }
  17023. /*
  17024. * We can acquire the lock. Remove the waiter from the
  17025. @@ -822,14 +867,10 @@
  17026. * not need to be dequeued.
  17027. */
  17028. if (rt_mutex_has_waiters(lock)) {
  17029. - /*
  17030. - * If @task->prio is greater than or equal to
  17031. - * the top waiter priority (kernel view),
  17032. - * @task lost.
  17033. - */
  17034. - if (task->prio >= rt_mutex_top_waiter(lock)->prio)
  17035. - return 0;
  17036. + struct task_struct *pown = rt_mutex_top_waiter(lock)->task;
  17037. + if (task != pown && !lock_is_stealable(task, pown, mode))
  17038. + return 0;
  17039. /*
  17040. * The current top waiter stays enqueued. We
  17041. * don't have to change anything in the lock
  17042. @@ -852,7 +893,7 @@
  17043. * case, but conditionals are more expensive than a redundant
  17044. * store.
  17045. */
  17046. - raw_spin_lock_irqsave(&task->pi_lock, flags);
  17047. + raw_spin_lock(&task->pi_lock);
  17048. task->pi_blocked_on = NULL;
  17049. /*
  17050. * Finish the lock acquisition. @task is the new owner. If
  17051. @@ -861,7 +902,7 @@
  17052. */
  17053. if (rt_mutex_has_waiters(lock))
  17054. rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
  17055. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  17056. + raw_spin_unlock(&task->pi_lock);
  17057. takeit:
  17058. /* We got the lock. */
  17059. @@ -878,12 +919,444 @@
  17060. return 1;
  17061. }
  17062. +#ifdef CONFIG_PREEMPT_RT_FULL
  17063. +/*
  17064. + * preemptible spin_lock functions:
  17065. + */
  17066. +static inline void rt_spin_lock_fastlock(struct rt_mutex *lock,
  17067. + void (*slowfn)(struct rt_mutex *lock,
  17068. + bool mg_off),
  17069. + bool do_mig_dis)
  17070. +{
  17071. + might_sleep_no_state_check();
  17072. +
  17073. + if (do_mig_dis)
  17074. + migrate_disable();
  17075. +
  17076. + if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
  17077. + rt_mutex_deadlock_account_lock(lock, current);
  17078. + else
  17079. + slowfn(lock, do_mig_dis);
  17080. +}
  17081. +
  17082. +static inline int rt_spin_lock_fastunlock(struct rt_mutex *lock,
  17083. + int (*slowfn)(struct rt_mutex *lock))
  17084. +{
  17085. + if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
  17086. + rt_mutex_deadlock_account_unlock(current);
  17087. + return 0;
  17088. + }
  17089. + return slowfn(lock);
  17090. +}
  17091. +#ifdef CONFIG_SMP
  17092. +/*
  17093. + * Note that owner is a speculative pointer and dereferencing relies
  17094. + * on rcu_read_lock() and the check against the lock owner.
  17095. + */
  17096. +static int adaptive_wait(struct rt_mutex *lock,
  17097. + struct task_struct *owner)
  17098. +{
  17099. + int res = 0;
  17100. +
  17101. + rcu_read_lock();
  17102. + for (;;) {
  17103. + if (owner != rt_mutex_owner(lock))
  17104. + break;
  17105. + /*
  17106. + * Ensure that owner->on_cpu is dereferenced _after_
  17107. + * checking the above to be valid.
  17108. + */
  17109. + barrier();
  17110. + if (!owner->on_cpu) {
  17111. + res = 1;
  17112. + break;
  17113. + }
  17114. + cpu_relax();
  17115. + }
  17116. + rcu_read_unlock();
  17117. + return res;
  17118. +}
  17119. +#else
  17120. +static int adaptive_wait(struct rt_mutex *lock,
  17121. + struct task_struct *orig_owner)
  17122. +{
  17123. + return 1;
  17124. +}
  17125. +#endif
  17126. +
  17127. +static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  17128. + struct rt_mutex_waiter *waiter,
  17129. + struct task_struct *task,
  17130. + enum rtmutex_chainwalk chwalk);
  17131. +/*
  17132. + * Slow path lock function spin_lock style: this variant is very
  17133. + * careful not to miss any non-lock wakeups.
  17134. + *
  17135. + * We store the current state under p->pi_lock in p->saved_state and
  17136. + * the try_to_wake_up() code handles this accordingly.
  17137. + */
  17138. +static void noinline __sched rt_spin_lock_slowlock(struct rt_mutex *lock,
  17139. + bool mg_off)
  17140. +{
  17141. + struct task_struct *lock_owner, *self = current;
  17142. + struct rt_mutex_waiter waiter, *top_waiter;
  17143. + unsigned long flags;
  17144. + int ret;
  17145. +
  17146. + rt_mutex_init_waiter(&waiter, true);
  17147. +
  17148. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  17149. +
  17150. + if (__try_to_take_rt_mutex(lock, self, NULL, STEAL_LATERAL)) {
  17151. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  17152. + return;
  17153. + }
  17154. +
  17155. + BUG_ON(rt_mutex_owner(lock) == self);
  17156. +
  17157. + /*
  17158. + * We save whatever state the task is in and we'll restore it
  17159. + * after acquiring the lock taking real wakeups into account
  17160. + * as well. We are serialized via pi_lock against wakeups. See
  17161. + * try_to_wake_up().
  17162. + */
  17163. + raw_spin_lock(&self->pi_lock);
  17164. + self->saved_state = self->state;
  17165. + __set_current_state_no_track(TASK_UNINTERRUPTIBLE);
  17166. + raw_spin_unlock(&self->pi_lock);
  17167. +
  17168. + ret = task_blocks_on_rt_mutex(lock, &waiter, self, RT_MUTEX_MIN_CHAINWALK);
  17169. + BUG_ON(ret);
  17170. +
  17171. + for (;;) {
  17172. + /* Try to acquire the lock again. */
  17173. + if (__try_to_take_rt_mutex(lock, self, &waiter, STEAL_LATERAL))
  17174. + break;
  17175. +
  17176. + top_waiter = rt_mutex_top_waiter(lock);
  17177. + lock_owner = rt_mutex_owner(lock);
  17178. +
  17179. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  17180. +
  17181. + debug_rt_mutex_print_deadlock(&waiter);
  17182. +
  17183. + if (top_waiter != &waiter || adaptive_wait(lock, lock_owner)) {
  17184. + if (mg_off)
  17185. + migrate_enable();
  17186. + schedule();
  17187. + if (mg_off)
  17188. + migrate_disable();
  17189. + }
  17190. +
  17191. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  17192. +
  17193. + raw_spin_lock(&self->pi_lock);
  17194. + __set_current_state_no_track(TASK_UNINTERRUPTIBLE);
  17195. + raw_spin_unlock(&self->pi_lock);
  17196. + }
  17197. +
  17198. + /*
  17199. + * Restore the task state to current->saved_state. We set it
  17200. + * to the original state above and the try_to_wake_up() code
  17201. + * has possibly updated it when a real (non-rtmutex) wakeup
  17202. + * happened while we were blocked. Clear saved_state so
  17203. + * try_to_wakeup() does not get confused.
  17204. + */
  17205. + raw_spin_lock(&self->pi_lock);
  17206. + __set_current_state_no_track(self->saved_state);
  17207. + self->saved_state = TASK_RUNNING;
  17208. + raw_spin_unlock(&self->pi_lock);
  17209. +
  17210. + /*
  17211. + * try_to_take_rt_mutex() sets the waiter bit
  17212. + * unconditionally. We might have to fix that up:
  17213. + */
  17214. + fixup_rt_mutex_waiters(lock);
  17215. +
  17216. + BUG_ON(rt_mutex_has_waiters(lock) && &waiter == rt_mutex_top_waiter(lock));
  17217. + BUG_ON(!RB_EMPTY_NODE(&waiter.tree_entry));
  17218. +
  17219. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  17220. +
  17221. + debug_rt_mutex_free_waiter(&waiter);
  17222. +}
  17223. +
  17224. +static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
  17225. + struct wake_q_head *wake_sleeper_q,
  17226. + struct rt_mutex *lock);
  17227. +/*
  17228. + * Slow path to release a rt_mutex spin_lock style
  17229. + */
  17230. +static int noinline __sched rt_spin_lock_slowunlock(struct rt_mutex *lock)
  17231. +{
  17232. + unsigned long flags;
  17233. + WAKE_Q(wake_q);
  17234. + WAKE_Q(wake_sleeper_q);
  17235. +
  17236. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  17237. +
  17238. + debug_rt_mutex_unlock(lock);
  17239. +
  17240. + rt_mutex_deadlock_account_unlock(current);
  17241. +
  17242. + if (!rt_mutex_has_waiters(lock)) {
  17243. + lock->owner = NULL;
  17244. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  17245. + return 0;
  17246. + }
  17247. +
  17248. + mark_wakeup_next_waiter(&wake_q, &wake_sleeper_q, lock);
  17249. +
  17250. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  17251. + wake_up_q(&wake_q);
  17252. + wake_up_q_sleeper(&wake_sleeper_q);
  17253. +
  17254. + /* Undo pi boosting.when necessary */
  17255. + rt_mutex_adjust_prio(current);
  17256. + return 0;
  17257. +}
  17258. +
  17259. +static int noinline __sched rt_spin_lock_slowunlock_no_deboost(struct rt_mutex *lock)
  17260. +{
  17261. + unsigned long flags;
  17262. + WAKE_Q(wake_q);
  17263. + WAKE_Q(wake_sleeper_q);
  17264. +
  17265. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  17266. +
  17267. + debug_rt_mutex_unlock(lock);
  17268. +
  17269. + rt_mutex_deadlock_account_unlock(current);
  17270. +
  17271. + if (!rt_mutex_has_waiters(lock)) {
  17272. + lock->owner = NULL;
  17273. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  17274. + return 0;
  17275. + }
  17276. +
  17277. + mark_wakeup_next_waiter(&wake_q, &wake_sleeper_q, lock);
  17278. +
  17279. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  17280. + wake_up_q(&wake_q);
  17281. + wake_up_q_sleeper(&wake_sleeper_q);
  17282. + return 1;
  17283. +}
  17284. +
  17285. +void __lockfunc rt_spin_lock__no_mg(spinlock_t *lock)
  17286. +{
  17287. + rt_spin_lock_fastlock(&lock->lock, rt_spin_lock_slowlock, false);
  17288. + spin_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  17289. +}
  17290. +EXPORT_SYMBOL(rt_spin_lock__no_mg);
  17291. +
  17292. +void __lockfunc rt_spin_lock(spinlock_t *lock)
  17293. +{
  17294. + rt_spin_lock_fastlock(&lock->lock, rt_spin_lock_slowlock, true);
  17295. + spin_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  17296. +}
  17297. +EXPORT_SYMBOL(rt_spin_lock);
  17298. +
  17299. +void __lockfunc __rt_spin_lock(struct rt_mutex *lock)
  17300. +{
  17301. + rt_spin_lock_fastlock(lock, rt_spin_lock_slowlock, true);
  17302. +}
  17303. +EXPORT_SYMBOL(__rt_spin_lock);
  17304. +
  17305. +void __lockfunc __rt_spin_lock__no_mg(struct rt_mutex *lock)
  17306. +{
  17307. + rt_spin_lock_fastlock(lock, rt_spin_lock_slowlock, false);
  17308. +}
  17309. +EXPORT_SYMBOL(__rt_spin_lock__no_mg);
  17310. +
  17311. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  17312. +void __lockfunc rt_spin_lock_nested(spinlock_t *lock, int subclass)
  17313. +{
  17314. + spin_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
  17315. + rt_spin_lock_fastlock(&lock->lock, rt_spin_lock_slowlock, true);
  17316. +}
  17317. +EXPORT_SYMBOL(rt_spin_lock_nested);
  17318. +#endif
  17319. +
  17320. +void __lockfunc rt_spin_unlock__no_mg(spinlock_t *lock)
  17321. +{
  17322. + /* NOTE: we always pass in '1' for nested, for simplicity */
  17323. + spin_release(&lock->dep_map, 1, _RET_IP_);
  17324. + rt_spin_lock_fastunlock(&lock->lock, rt_spin_lock_slowunlock);
  17325. +}
  17326. +EXPORT_SYMBOL(rt_spin_unlock__no_mg);
  17327. +
  17328. +void __lockfunc rt_spin_unlock(spinlock_t *lock)
  17329. +{
  17330. + /* NOTE: we always pass in '1' for nested, for simplicity */
  17331. + spin_release(&lock->dep_map, 1, _RET_IP_);
  17332. + rt_spin_lock_fastunlock(&lock->lock, rt_spin_lock_slowunlock);
  17333. + migrate_enable();
  17334. +}
  17335. +EXPORT_SYMBOL(rt_spin_unlock);
  17336. +
  17337. +int __lockfunc rt_spin_unlock_no_deboost(spinlock_t *lock)
  17338. +{
  17339. + int ret;
  17340. +
  17341. + /* NOTE: we always pass in '1' for nested, for simplicity */
  17342. + spin_release(&lock->dep_map, 1, _RET_IP_);
  17343. + ret = rt_spin_lock_fastunlock(&lock->lock, rt_spin_lock_slowunlock_no_deboost);
  17344. + migrate_enable();
  17345. + return ret;
  17346. +}
  17347. +
  17348. +void __lockfunc __rt_spin_unlock(struct rt_mutex *lock)
  17349. +{
  17350. + rt_spin_lock_fastunlock(lock, rt_spin_lock_slowunlock);
  17351. +}
  17352. +EXPORT_SYMBOL(__rt_spin_unlock);
  17353. +
  17354. +/*
  17355. + * Wait for the lock to get unlocked: instead of polling for an unlock
  17356. + * (like raw spinlocks do), we lock and unlock, to force the kernel to
  17357. + * schedule if there's contention:
  17358. + */
  17359. +void __lockfunc rt_spin_unlock_wait(spinlock_t *lock)
  17360. +{
  17361. + spin_lock(lock);
  17362. + spin_unlock(lock);
  17363. +}
  17364. +EXPORT_SYMBOL(rt_spin_unlock_wait);
  17365. +
  17366. +int __lockfunc __rt_spin_trylock(struct rt_mutex *lock)
  17367. +{
  17368. + return rt_mutex_trylock(lock);
  17369. +}
  17370. +
  17371. +int __lockfunc rt_spin_trylock__no_mg(spinlock_t *lock)
  17372. +{
  17373. + int ret;
  17374. +
  17375. + ret = rt_mutex_trylock(&lock->lock);
  17376. + if (ret)
  17377. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  17378. + return ret;
  17379. +}
  17380. +EXPORT_SYMBOL(rt_spin_trylock__no_mg);
  17381. +
  17382. +int __lockfunc rt_spin_trylock(spinlock_t *lock)
  17383. +{
  17384. + int ret;
  17385. +
  17386. + migrate_disable();
  17387. + ret = rt_mutex_trylock(&lock->lock);
  17388. + if (ret)
  17389. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  17390. + else
  17391. + migrate_enable();
  17392. + return ret;
  17393. +}
  17394. +EXPORT_SYMBOL(rt_spin_trylock);
  17395. +
  17396. +int __lockfunc rt_spin_trylock_bh(spinlock_t *lock)
  17397. +{
  17398. + int ret;
  17399. +
  17400. + local_bh_disable();
  17401. + ret = rt_mutex_trylock(&lock->lock);
  17402. + if (ret) {
  17403. + migrate_disable();
  17404. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  17405. + } else
  17406. + local_bh_enable();
  17407. + return ret;
  17408. +}
  17409. +EXPORT_SYMBOL(rt_spin_trylock_bh);
  17410. +
  17411. +int __lockfunc rt_spin_trylock_irqsave(spinlock_t *lock, unsigned long *flags)
  17412. +{
  17413. + int ret;
  17414. +
  17415. + *flags = 0;
  17416. + ret = rt_mutex_trylock(&lock->lock);
  17417. + if (ret) {
  17418. + migrate_disable();
  17419. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  17420. + }
  17421. + return ret;
  17422. +}
  17423. +EXPORT_SYMBOL(rt_spin_trylock_irqsave);
  17424. +
  17425. +int atomic_dec_and_spin_lock(atomic_t *atomic, spinlock_t *lock)
  17426. +{
  17427. + /* Subtract 1 from counter unless that drops it to 0 (ie. it was 1) */
  17428. + if (atomic_add_unless(atomic, -1, 1))
  17429. + return 0;
  17430. + rt_spin_lock(lock);
  17431. + if (atomic_dec_and_test(atomic))
  17432. + return 1;
  17433. + rt_spin_unlock(lock);
  17434. + return 0;
  17435. +}
  17436. +EXPORT_SYMBOL(atomic_dec_and_spin_lock);
  17437. +
  17438. + void
  17439. +__rt_spin_lock_init(spinlock_t *lock, char *name, struct lock_class_key *key)
  17440. +{
  17441. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  17442. + /*
  17443. + * Make sure we are not reinitializing a held lock:
  17444. + */
  17445. + debug_check_no_locks_freed((void *)lock, sizeof(*lock));
  17446. + lockdep_init_map(&lock->dep_map, name, key, 0);
  17447. +#endif
  17448. +}
  17449. +EXPORT_SYMBOL(__rt_spin_lock_init);
  17450. +
  17451. +#endif /* PREEMPT_RT_FULL */
  17452. +
  17453. +#ifdef CONFIG_PREEMPT_RT_FULL
  17454. + static inline int __sched
  17455. +__mutex_lock_check_stamp(struct rt_mutex *lock, struct ww_acquire_ctx *ctx)
  17456. +{
  17457. + struct ww_mutex *ww = container_of(lock, struct ww_mutex, base.lock);
  17458. + struct ww_acquire_ctx *hold_ctx = ACCESS_ONCE(ww->ctx);
  17459. +
  17460. + if (!hold_ctx)
  17461. + return 0;
  17462. +
  17463. + if (unlikely(ctx == hold_ctx))
  17464. + return -EALREADY;
  17465. +
  17466. + if (ctx->stamp - hold_ctx->stamp <= LONG_MAX &&
  17467. + (ctx->stamp != hold_ctx->stamp || ctx > hold_ctx)) {
  17468. +#ifdef CONFIG_DEBUG_MUTEXES
  17469. + DEBUG_LOCKS_WARN_ON(ctx->contending_lock);
  17470. + ctx->contending_lock = ww;
  17471. +#endif
  17472. + return -EDEADLK;
  17473. + }
  17474. +
  17475. + return 0;
  17476. +}
  17477. +#else
  17478. + static inline int __sched
  17479. +__mutex_lock_check_stamp(struct rt_mutex *lock, struct ww_acquire_ctx *ctx)
  17480. +{
  17481. + BUG();
  17482. + return 0;
  17483. +}
  17484. +
  17485. +#endif
  17486. +
  17487. +static inline int
  17488. +try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  17489. + struct rt_mutex_waiter *waiter)
  17490. +{
  17491. + return __try_to_take_rt_mutex(lock, task, waiter, STEAL_NORMAL);
  17492. +}
  17493. +
  17494. /*
  17495. * Task blocks on lock.
  17496. *
  17497. * Prepare waiter and propagate pi chain
  17498. *
  17499. - * This must be called with lock->wait_lock held.
  17500. + * This must be called with lock->wait_lock held and interrupts disabled
  17501. */
  17502. static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  17503. struct rt_mutex_waiter *waiter,
  17504. @@ -894,7 +1367,6 @@
  17505. struct rt_mutex_waiter *top_waiter = waiter;
  17506. struct rt_mutex *next_lock;
  17507. int chain_walk = 0, res;
  17508. - unsigned long flags;
  17509. /*
  17510. * Early deadlock detection. We really don't want the task to
  17511. @@ -908,7 +1380,24 @@
  17512. if (owner == task)
  17513. return -EDEADLK;
  17514. - raw_spin_lock_irqsave(&task->pi_lock, flags);
  17515. + raw_spin_lock(&task->pi_lock);
  17516. +
  17517. + /*
  17518. + * In the case of futex requeue PI, this will be a proxy
  17519. + * lock. The task will wake unaware that it is enqueueed on
  17520. + * this lock. Avoid blocking on two locks and corrupting
  17521. + * pi_blocked_on via the PI_WAKEUP_INPROGRESS
  17522. + * flag. futex_wait_requeue_pi() sets this when it wakes up
  17523. + * before requeue (due to a signal or timeout). Do not enqueue
  17524. + * the task if PI_WAKEUP_INPROGRESS is set.
  17525. + */
  17526. + if (task != current && task->pi_blocked_on == PI_WAKEUP_INPROGRESS) {
  17527. + raw_spin_unlock(&task->pi_lock);
  17528. + return -EAGAIN;
  17529. + }
  17530. +
  17531. + BUG_ON(rt_mutex_real_waiter(task->pi_blocked_on));
  17532. +
  17533. __rt_mutex_adjust_prio(task);
  17534. waiter->task = task;
  17535. waiter->lock = lock;
  17536. @@ -921,18 +1410,18 @@
  17537. task->pi_blocked_on = waiter;
  17538. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  17539. + raw_spin_unlock(&task->pi_lock);
  17540. if (!owner)
  17541. return 0;
  17542. - raw_spin_lock_irqsave(&owner->pi_lock, flags);
  17543. + raw_spin_lock(&owner->pi_lock);
  17544. if (waiter == rt_mutex_top_waiter(lock)) {
  17545. rt_mutex_dequeue_pi(owner, top_waiter);
  17546. rt_mutex_enqueue_pi(owner, waiter);
  17547. __rt_mutex_adjust_prio(owner);
  17548. - if (owner->pi_blocked_on)
  17549. + if (rt_mutex_real_waiter(owner->pi_blocked_on))
  17550. chain_walk = 1;
  17551. } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
  17552. chain_walk = 1;
  17553. @@ -941,7 +1430,7 @@
  17554. /* Store the lock on which owner is blocked or NULL */
  17555. next_lock = task_blocked_on_lock(owner);
  17556. - raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
  17557. + raw_spin_unlock(&owner->pi_lock);
  17558. /*
  17559. * Even if full deadlock detection is on, if the owner is not
  17560. * blocked itself, we can avoid finding this out in the chain
  17561. @@ -957,12 +1446,12 @@
  17562. */
  17563. get_task_struct(owner);
  17564. - raw_spin_unlock(&lock->wait_lock);
  17565. + raw_spin_unlock_irq(&lock->wait_lock);
  17566. res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
  17567. next_lock, waiter, task);
  17568. - raw_spin_lock(&lock->wait_lock);
  17569. + raw_spin_lock_irq(&lock->wait_lock);
  17570. return res;
  17571. }
  17572. @@ -971,15 +1460,15 @@
  17573. * Remove the top waiter from the current tasks pi waiter tree and
  17574. * queue it up.
  17575. *
  17576. - * Called with lock->wait_lock held.
  17577. + * Called with lock->wait_lock held and interrupts disabled.
  17578. */
  17579. static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
  17580. + struct wake_q_head *wake_sleeper_q,
  17581. struct rt_mutex *lock)
  17582. {
  17583. struct rt_mutex_waiter *waiter;
  17584. - unsigned long flags;
  17585. - raw_spin_lock_irqsave(&current->pi_lock, flags);
  17586. + raw_spin_lock(&current->pi_lock);
  17587. waiter = rt_mutex_top_waiter(lock);
  17588. @@ -1001,15 +1490,18 @@
  17589. */
  17590. lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
  17591. - raw_spin_unlock_irqrestore(&current->pi_lock, flags);
  17592. + raw_spin_unlock(&current->pi_lock);
  17593. - wake_q_add(wake_q, waiter->task);
  17594. + if (waiter->savestate)
  17595. + wake_q_add(wake_sleeper_q, waiter->task);
  17596. + else
  17597. + wake_q_add(wake_q, waiter->task);
  17598. }
  17599. /*
  17600. * Remove a waiter from a lock and give up
  17601. *
  17602. - * Must be called with lock->wait_lock held and
  17603. + * Must be called with lock->wait_lock held and interrupts disabled. I must
  17604. * have just failed to try_to_take_rt_mutex().
  17605. */
  17606. static void remove_waiter(struct rt_mutex *lock,
  17607. @@ -1017,13 +1509,12 @@
  17608. {
  17609. bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
  17610. struct task_struct *owner = rt_mutex_owner(lock);
  17611. - struct rt_mutex *next_lock;
  17612. - unsigned long flags;
  17613. + struct rt_mutex *next_lock = NULL;
  17614. - raw_spin_lock_irqsave(&current->pi_lock, flags);
  17615. + raw_spin_lock(&current->pi_lock);
  17616. rt_mutex_dequeue(lock, waiter);
  17617. current->pi_blocked_on = NULL;
  17618. - raw_spin_unlock_irqrestore(&current->pi_lock, flags);
  17619. + raw_spin_unlock(&current->pi_lock);
  17620. /*
  17621. * Only update priority if the waiter was the highest priority
  17622. @@ -1032,7 +1523,7 @@
  17623. if (!owner || !is_top_waiter)
  17624. return;
  17625. - raw_spin_lock_irqsave(&owner->pi_lock, flags);
  17626. + raw_spin_lock(&owner->pi_lock);
  17627. rt_mutex_dequeue_pi(owner, waiter);
  17628. @@ -1042,9 +1533,10 @@
  17629. __rt_mutex_adjust_prio(owner);
  17630. /* Store the lock on which owner is blocked or NULL */
  17631. - next_lock = task_blocked_on_lock(owner);
  17632. + if (rt_mutex_real_waiter(owner->pi_blocked_on))
  17633. + next_lock = task_blocked_on_lock(owner);
  17634. - raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
  17635. + raw_spin_unlock(&owner->pi_lock);
  17636. /*
  17637. * Don't walk the chain, if the owner task is not blocked
  17638. @@ -1056,12 +1548,12 @@
  17639. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  17640. get_task_struct(owner);
  17641. - raw_spin_unlock(&lock->wait_lock);
  17642. + raw_spin_unlock_irq(&lock->wait_lock);
  17643. rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
  17644. next_lock, NULL, current);
  17645. - raw_spin_lock(&lock->wait_lock);
  17646. + raw_spin_lock_irq(&lock->wait_lock);
  17647. }
  17648. /*
  17649. @@ -1078,17 +1570,17 @@
  17650. raw_spin_lock_irqsave(&task->pi_lock, flags);
  17651. waiter = task->pi_blocked_on;
  17652. - if (!waiter || (waiter->prio == task->prio &&
  17653. + if (!rt_mutex_real_waiter(waiter) || (waiter->prio == task->prio &&
  17654. !dl_prio(task->prio))) {
  17655. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  17656. return;
  17657. }
  17658. next_lock = waiter->lock;
  17659. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  17660. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  17661. get_task_struct(task);
  17662. + raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  17663. rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
  17664. next_lock, NULL, task);
  17665. }
  17666. @@ -1097,16 +1589,17 @@
  17667. * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
  17668. * @lock: the rt_mutex to take
  17669. * @state: the state the task should block in (TASK_INTERRUPTIBLE
  17670. - * or TASK_UNINTERRUPTIBLE)
  17671. + * or TASK_UNINTERRUPTIBLE)
  17672. * @timeout: the pre-initialized and started timer, or NULL for none
  17673. * @waiter: the pre-initialized rt_mutex_waiter
  17674. *
  17675. - * lock->wait_lock must be held by the caller.
  17676. + * Must be called with lock->wait_lock held and interrupts disabled
  17677. */
  17678. static int __sched
  17679. __rt_mutex_slowlock(struct rt_mutex *lock, int state,
  17680. struct hrtimer_sleeper *timeout,
  17681. - struct rt_mutex_waiter *waiter)
  17682. + struct rt_mutex_waiter *waiter,
  17683. + struct ww_acquire_ctx *ww_ctx)
  17684. {
  17685. int ret = 0;
  17686. @@ -1129,13 +1622,19 @@
  17687. break;
  17688. }
  17689. - raw_spin_unlock(&lock->wait_lock);
  17690. + if (ww_ctx && ww_ctx->acquired > 0) {
  17691. + ret = __mutex_lock_check_stamp(lock, ww_ctx);
  17692. + if (ret)
  17693. + break;
  17694. + }
  17695. +
  17696. + raw_spin_unlock_irq(&lock->wait_lock);
  17697. debug_rt_mutex_print_deadlock(waiter);
  17698. schedule();
  17699. - raw_spin_lock(&lock->wait_lock);
  17700. + raw_spin_lock_irq(&lock->wait_lock);
  17701. set_current_state(state);
  17702. }
  17703. @@ -1163,26 +1662,112 @@
  17704. }
  17705. }
  17706. +static __always_inline void ww_mutex_lock_acquired(struct ww_mutex *ww,
  17707. + struct ww_acquire_ctx *ww_ctx)
  17708. +{
  17709. +#ifdef CONFIG_DEBUG_MUTEXES
  17710. + /*
  17711. + * If this WARN_ON triggers, you used ww_mutex_lock to acquire,
  17712. + * but released with a normal mutex_unlock in this call.
  17713. + *
  17714. + * This should never happen, always use ww_mutex_unlock.
  17715. + */
  17716. + DEBUG_LOCKS_WARN_ON(ww->ctx);
  17717. +
  17718. + /*
  17719. + * Not quite done after calling ww_acquire_done() ?
  17720. + */
  17721. + DEBUG_LOCKS_WARN_ON(ww_ctx->done_acquire);
  17722. +
  17723. + if (ww_ctx->contending_lock) {
  17724. + /*
  17725. + * After -EDEADLK you tried to
  17726. + * acquire a different ww_mutex? Bad!
  17727. + */
  17728. + DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock != ww);
  17729. +
  17730. + /*
  17731. + * You called ww_mutex_lock after receiving -EDEADLK,
  17732. + * but 'forgot' to unlock everything else first?
  17733. + */
  17734. + DEBUG_LOCKS_WARN_ON(ww_ctx->acquired > 0);
  17735. + ww_ctx->contending_lock = NULL;
  17736. + }
  17737. +
  17738. + /*
  17739. + * Naughty, using a different class will lead to undefined behavior!
  17740. + */
  17741. + DEBUG_LOCKS_WARN_ON(ww_ctx->ww_class != ww->ww_class);
  17742. +#endif
  17743. + ww_ctx->acquired++;
  17744. +}
  17745. +
  17746. +#ifdef CONFIG_PREEMPT_RT_FULL
  17747. +static void ww_mutex_account_lock(struct rt_mutex *lock,
  17748. + struct ww_acquire_ctx *ww_ctx)
  17749. +{
  17750. + struct ww_mutex *ww = container_of(lock, struct ww_mutex, base.lock);
  17751. + struct rt_mutex_waiter *waiter, *n;
  17752. +
  17753. + /*
  17754. + * This branch gets optimized out for the common case,
  17755. + * and is only important for ww_mutex_lock.
  17756. + */
  17757. + ww_mutex_lock_acquired(ww, ww_ctx);
  17758. + ww->ctx = ww_ctx;
  17759. +
  17760. + /*
  17761. + * Give any possible sleeping processes the chance to wake up,
  17762. + * so they can recheck if they have to back off.
  17763. + */
  17764. + rbtree_postorder_for_each_entry_safe(waiter, n, &lock->waiters,
  17765. + tree_entry) {
  17766. + /* XXX debug rt mutex waiter wakeup */
  17767. +
  17768. + BUG_ON(waiter->lock != lock);
  17769. + rt_mutex_wake_waiter(waiter);
  17770. + }
  17771. +}
  17772. +
  17773. +#else
  17774. +
  17775. +static void ww_mutex_account_lock(struct rt_mutex *lock,
  17776. + struct ww_acquire_ctx *ww_ctx)
  17777. +{
  17778. + BUG();
  17779. +}
  17780. +#endif
  17781. +
  17782. /*
  17783. * Slow path lock function:
  17784. */
  17785. static int __sched
  17786. rt_mutex_slowlock(struct rt_mutex *lock, int state,
  17787. struct hrtimer_sleeper *timeout,
  17788. - enum rtmutex_chainwalk chwalk)
  17789. + enum rtmutex_chainwalk chwalk,
  17790. + struct ww_acquire_ctx *ww_ctx)
  17791. {
  17792. struct rt_mutex_waiter waiter;
  17793. + unsigned long flags;
  17794. int ret = 0;
  17795. - debug_rt_mutex_init_waiter(&waiter);
  17796. - RB_CLEAR_NODE(&waiter.pi_tree_entry);
  17797. - RB_CLEAR_NODE(&waiter.tree_entry);
  17798. + rt_mutex_init_waiter(&waiter, false);
  17799. - raw_spin_lock(&lock->wait_lock);
  17800. + /*
  17801. + * Technically we could use raw_spin_[un]lock_irq() here, but this can
  17802. + * be called in early boot if the cmpxchg() fast path is disabled
  17803. + * (debug, no architecture support). In this case we will acquire the
  17804. + * rtmutex with lock->wait_lock held. But we cannot unconditionally
  17805. + * enable interrupts in that early boot case. So we need to use the
  17806. + * irqsave/restore variants.
  17807. + */
  17808. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  17809. /* Try to acquire the lock again: */
  17810. if (try_to_take_rt_mutex(lock, current, NULL)) {
  17811. - raw_spin_unlock(&lock->wait_lock);
  17812. + if (ww_ctx)
  17813. + ww_mutex_account_lock(lock, ww_ctx);
  17814. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  17815. return 0;
  17816. }
  17817. @@ -1196,13 +1781,23 @@
  17818. if (likely(!ret))
  17819. /* sleep on the mutex */
  17820. - ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
  17821. + ret = __rt_mutex_slowlock(lock, state, timeout, &waiter,
  17822. + ww_ctx);
  17823. + else if (ww_ctx) {
  17824. + /* ww_mutex received EDEADLK, let it become EALREADY */
  17825. + ret = __mutex_lock_check_stamp(lock, ww_ctx);
  17826. + BUG_ON(!ret);
  17827. + }
  17828. if (unlikely(ret)) {
  17829. __set_current_state(TASK_RUNNING);
  17830. if (rt_mutex_has_waiters(lock))
  17831. remove_waiter(lock, &waiter);
  17832. - rt_mutex_handle_deadlock(ret, chwalk, &waiter);
  17833. + /* ww_mutex want to report EDEADLK/EALREADY, let them */
  17834. + if (!ww_ctx)
  17835. + rt_mutex_handle_deadlock(ret, chwalk, &waiter);
  17836. + } else if (ww_ctx) {
  17837. + ww_mutex_account_lock(lock, ww_ctx);
  17838. }
  17839. /*
  17840. @@ -1211,7 +1806,7 @@
  17841. */
  17842. fixup_rt_mutex_waiters(lock);
  17843. - raw_spin_unlock(&lock->wait_lock);
  17844. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  17845. /* Remove pending timer: */
  17846. if (unlikely(timeout))
  17847. @@ -1227,6 +1822,7 @@
  17848. */
  17849. static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
  17850. {
  17851. + unsigned long flags;
  17852. int ret;
  17853. /*
  17854. @@ -1238,10 +1834,10 @@
  17855. return 0;
  17856. /*
  17857. - * The mutex has currently no owner. Lock the wait lock and
  17858. - * try to acquire the lock.
  17859. + * The mutex has currently no owner. Lock the wait lock and try to
  17860. + * acquire the lock. We use irqsave here to support early boot calls.
  17861. */
  17862. - raw_spin_lock(&lock->wait_lock);
  17863. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  17864. ret = try_to_take_rt_mutex(lock, current, NULL);
  17865. @@ -1251,7 +1847,7 @@
  17866. */
  17867. fixup_rt_mutex_waiters(lock);
  17868. - raw_spin_unlock(&lock->wait_lock);
  17869. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  17870. return ret;
  17871. }
  17872. @@ -1261,9 +1857,13 @@
  17873. * Return whether the current task needs to undo a potential priority boosting.
  17874. */
  17875. static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
  17876. - struct wake_q_head *wake_q)
  17877. + struct wake_q_head *wake_q,
  17878. + struct wake_q_head *wake_sleeper_q)
  17879. {
  17880. - raw_spin_lock(&lock->wait_lock);
  17881. + unsigned long flags;
  17882. +
  17883. + /* irqsave required to support early boot calls */
  17884. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  17885. debug_rt_mutex_unlock(lock);
  17886. @@ -1302,10 +1902,10 @@
  17887. */
  17888. while (!rt_mutex_has_waiters(lock)) {
  17889. /* Drops lock->wait_lock ! */
  17890. - if (unlock_rt_mutex_safe(lock) == true)
  17891. + if (unlock_rt_mutex_safe(lock, flags) == true)
  17892. return false;
  17893. /* Relock the rtmutex and try again */
  17894. - raw_spin_lock(&lock->wait_lock);
  17895. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  17896. }
  17897. /*
  17898. @@ -1314,9 +1914,9 @@
  17899. *
  17900. * Queue the next waiter for wakeup once we release the wait_lock.
  17901. */
  17902. - mark_wakeup_next_waiter(wake_q, lock);
  17903. + mark_wakeup_next_waiter(wake_q, wake_sleeper_q, lock);
  17904. - raw_spin_unlock(&lock->wait_lock);
  17905. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  17906. /* check PI boosting */
  17907. return true;
  17908. @@ -1330,31 +1930,36 @@
  17909. */
  17910. static inline int
  17911. rt_mutex_fastlock(struct rt_mutex *lock, int state,
  17912. + struct ww_acquire_ctx *ww_ctx,
  17913. int (*slowfn)(struct rt_mutex *lock, int state,
  17914. struct hrtimer_sleeper *timeout,
  17915. - enum rtmutex_chainwalk chwalk))
  17916. + enum rtmutex_chainwalk chwalk,
  17917. + struct ww_acquire_ctx *ww_ctx))
  17918. {
  17919. if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
  17920. rt_mutex_deadlock_account_lock(lock, current);
  17921. return 0;
  17922. } else
  17923. - return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
  17924. + return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK,
  17925. + ww_ctx);
  17926. }
  17927. static inline int
  17928. rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
  17929. struct hrtimer_sleeper *timeout,
  17930. enum rtmutex_chainwalk chwalk,
  17931. + struct ww_acquire_ctx *ww_ctx,
  17932. int (*slowfn)(struct rt_mutex *lock, int state,
  17933. struct hrtimer_sleeper *timeout,
  17934. - enum rtmutex_chainwalk chwalk))
  17935. + enum rtmutex_chainwalk chwalk,
  17936. + struct ww_acquire_ctx *ww_ctx))
  17937. {
  17938. if (chwalk == RT_MUTEX_MIN_CHAINWALK &&
  17939. likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
  17940. rt_mutex_deadlock_account_lock(lock, current);
  17941. return 0;
  17942. } else
  17943. - return slowfn(lock, state, timeout, chwalk);
  17944. + return slowfn(lock, state, timeout, chwalk, ww_ctx);
  17945. }
  17946. static inline int
  17947. @@ -1371,17 +1976,20 @@
  17948. static inline void
  17949. rt_mutex_fastunlock(struct rt_mutex *lock,
  17950. bool (*slowfn)(struct rt_mutex *lock,
  17951. - struct wake_q_head *wqh))
  17952. + struct wake_q_head *wqh,
  17953. + struct wake_q_head *wq_sleeper))
  17954. {
  17955. WAKE_Q(wake_q);
  17956. + WAKE_Q(wake_sleeper_q);
  17957. if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
  17958. rt_mutex_deadlock_account_unlock(current);
  17959. } else {
  17960. - bool deboost = slowfn(lock, &wake_q);
  17961. + bool deboost = slowfn(lock, &wake_q, &wake_sleeper_q);
  17962. wake_up_q(&wake_q);
  17963. + wake_up_q_sleeper(&wake_sleeper_q);
  17964. /* Undo pi boosting if necessary: */
  17965. if (deboost)
  17966. @@ -1398,7 +2006,7 @@
  17967. {
  17968. might_sleep();
  17969. - rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
  17970. + rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, NULL, rt_mutex_slowlock);
  17971. }
  17972. EXPORT_SYMBOL_GPL(rt_mutex_lock);
  17973. @@ -1415,7 +2023,7 @@
  17974. {
  17975. might_sleep();
  17976. - return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
  17977. + return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, NULL, rt_mutex_slowlock);
  17978. }
  17979. EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
  17980. @@ -1428,11 +2036,30 @@
  17981. might_sleep();
  17982. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  17983. - RT_MUTEX_FULL_CHAINWALK,
  17984. + RT_MUTEX_FULL_CHAINWALK, NULL,
  17985. rt_mutex_slowlock);
  17986. }
  17987. /**
  17988. + * rt_mutex_lock_killable - lock a rt_mutex killable
  17989. + *
  17990. + * @lock: the rt_mutex to be locked
  17991. + * @detect_deadlock: deadlock detection on/off
  17992. + *
  17993. + * Returns:
  17994. + * 0 on success
  17995. + * -EINTR when interrupted by a signal
  17996. + * -EDEADLK when the lock would deadlock (when deadlock detection is on)
  17997. + */
  17998. +int __sched rt_mutex_lock_killable(struct rt_mutex *lock)
  17999. +{
  18000. + might_sleep();
  18001. +
  18002. + return rt_mutex_fastlock(lock, TASK_KILLABLE, NULL, rt_mutex_slowlock);
  18003. +}
  18004. +EXPORT_SYMBOL_GPL(rt_mutex_lock_killable);
  18005. +
  18006. +/**
  18007. * rt_mutex_timed_lock - lock a rt_mutex interruptible
  18008. * the timeout structure is provided
  18009. * by the caller
  18010. @@ -1452,6 +2079,7 @@
  18011. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  18012. RT_MUTEX_MIN_CHAINWALK,
  18013. + NULL,
  18014. rt_mutex_slowlock);
  18015. }
  18016. EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
  18017. @@ -1469,7 +2097,11 @@
  18018. */
  18019. int __sched rt_mutex_trylock(struct rt_mutex *lock)
  18020. {
  18021. +#ifdef CONFIG_PREEMPT_RT_FULL
  18022. + if (WARN_ON_ONCE(in_irq() || in_nmi()))
  18023. +#else
  18024. if (WARN_ON(in_irq() || in_nmi() || in_serving_softirq()))
  18025. +#endif
  18026. return 0;
  18027. return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
  18028. @@ -1495,13 +2127,14 @@
  18029. * required or not.
  18030. */
  18031. bool __sched rt_mutex_futex_unlock(struct rt_mutex *lock,
  18032. - struct wake_q_head *wqh)
  18033. + struct wake_q_head *wqh,
  18034. + struct wake_q_head *wq_sleeper)
  18035. {
  18036. if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
  18037. rt_mutex_deadlock_account_unlock(current);
  18038. return false;
  18039. }
  18040. - return rt_mutex_slowunlock(lock, wqh);
  18041. + return rt_mutex_slowunlock(lock, wqh, wq_sleeper);
  18042. }
  18043. /**
  18044. @@ -1534,13 +2167,12 @@
  18045. void __rt_mutex_init(struct rt_mutex *lock, const char *name)
  18046. {
  18047. lock->owner = NULL;
  18048. - raw_spin_lock_init(&lock->wait_lock);
  18049. lock->waiters = RB_ROOT;
  18050. lock->waiters_leftmost = NULL;
  18051. debug_rt_mutex_init(lock, name);
  18052. }
  18053. -EXPORT_SYMBOL_GPL(__rt_mutex_init);
  18054. +EXPORT_SYMBOL(__rt_mutex_init);
  18055. /**
  18056. * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
  18057. @@ -1555,7 +2187,7 @@
  18058. void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
  18059. struct task_struct *proxy_owner)
  18060. {
  18061. - __rt_mutex_init(lock, NULL);
  18062. + rt_mutex_init(lock);
  18063. debug_rt_mutex_proxy_lock(lock, proxy_owner);
  18064. rt_mutex_set_owner(lock, proxy_owner);
  18065. rt_mutex_deadlock_account_lock(lock, proxy_owner);
  18066. @@ -1596,13 +2228,42 @@
  18067. {
  18068. int ret;
  18069. - raw_spin_lock(&lock->wait_lock);
  18070. + raw_spin_lock_irq(&lock->wait_lock);
  18071. if (try_to_take_rt_mutex(lock, task, NULL)) {
  18072. - raw_spin_unlock(&lock->wait_lock);
  18073. + raw_spin_unlock_irq(&lock->wait_lock);
  18074. return 1;
  18075. }
  18076. +#ifdef CONFIG_PREEMPT_RT_FULL
  18077. + /*
  18078. + * In PREEMPT_RT there's an added race.
  18079. + * If the task, that we are about to requeue, times out,
  18080. + * it can set the PI_WAKEUP_INPROGRESS. This tells the requeue
  18081. + * to skip this task. But right after the task sets
  18082. + * its pi_blocked_on to PI_WAKEUP_INPROGRESS it can then
  18083. + * block on the spin_lock(&hb->lock), which in RT is an rtmutex.
  18084. + * This will replace the PI_WAKEUP_INPROGRESS with the actual
  18085. + * lock that it blocks on. We *must not* place this task
  18086. + * on this proxy lock in that case.
  18087. + *
  18088. + * To prevent this race, we first take the task's pi_lock
  18089. + * and check if it has updated its pi_blocked_on. If it has,
  18090. + * we assume that it woke up and we return -EAGAIN.
  18091. + * Otherwise, we set the task's pi_blocked_on to
  18092. + * PI_REQUEUE_INPROGRESS, so that if the task is waking up
  18093. + * it will know that we are in the process of requeuing it.
  18094. + */
  18095. + raw_spin_lock(&task->pi_lock);
  18096. + if (task->pi_blocked_on) {
  18097. + raw_spin_unlock(&task->pi_lock);
  18098. + raw_spin_unlock_irq(&lock->wait_lock);
  18099. + return -EAGAIN;
  18100. + }
  18101. + task->pi_blocked_on = PI_REQUEUE_INPROGRESS;
  18102. + raw_spin_unlock(&task->pi_lock);
  18103. +#endif
  18104. +
  18105. /* We enforce deadlock detection for futexes */
  18106. ret = task_blocks_on_rt_mutex(lock, waiter, task,
  18107. RT_MUTEX_FULL_CHAINWALK);
  18108. @@ -1617,10 +2278,10 @@
  18109. ret = 0;
  18110. }
  18111. - if (unlikely(ret))
  18112. + if (ret && rt_mutex_has_waiters(lock))
  18113. remove_waiter(lock, waiter);
  18114. - raw_spin_unlock(&lock->wait_lock);
  18115. + raw_spin_unlock_irq(&lock->wait_lock);
  18116. debug_rt_mutex_print_deadlock(waiter);
  18117. @@ -1668,12 +2329,12 @@
  18118. {
  18119. int ret;
  18120. - raw_spin_lock(&lock->wait_lock);
  18121. + raw_spin_lock_irq(&lock->wait_lock);
  18122. set_current_state(TASK_INTERRUPTIBLE);
  18123. /* sleep on the mutex */
  18124. - ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
  18125. + ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter, NULL);
  18126. if (unlikely(ret))
  18127. remove_waiter(lock, waiter);
  18128. @@ -1684,7 +2345,93 @@
  18129. */
  18130. fixup_rt_mutex_waiters(lock);
  18131. - raw_spin_unlock(&lock->wait_lock);
  18132. + raw_spin_unlock_irq(&lock->wait_lock);
  18133. return ret;
  18134. }
  18135. +
  18136. +static inline int
  18137. +ww_mutex_deadlock_injection(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
  18138. +{
  18139. +#ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
  18140. + unsigned tmp;
  18141. +
  18142. + if (ctx->deadlock_inject_countdown-- == 0) {
  18143. + tmp = ctx->deadlock_inject_interval;
  18144. + if (tmp > UINT_MAX/4)
  18145. + tmp = UINT_MAX;
  18146. + else
  18147. + tmp = tmp*2 + tmp + tmp/2;
  18148. +
  18149. + ctx->deadlock_inject_interval = tmp;
  18150. + ctx->deadlock_inject_countdown = tmp;
  18151. + ctx->contending_lock = lock;
  18152. +
  18153. + ww_mutex_unlock(lock);
  18154. +
  18155. + return -EDEADLK;
  18156. + }
  18157. +#endif
  18158. +
  18159. + return 0;
  18160. +}
  18161. +
  18162. +#ifdef CONFIG_PREEMPT_RT_FULL
  18163. +int __sched
  18164. +__ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ww_ctx)
  18165. +{
  18166. + int ret;
  18167. +
  18168. + might_sleep();
  18169. +
  18170. + mutex_acquire_nest(&lock->base.dep_map, 0, 0, &ww_ctx->dep_map, _RET_IP_);
  18171. + ret = rt_mutex_slowlock(&lock->base.lock, TASK_INTERRUPTIBLE, NULL, 0, ww_ctx);
  18172. + if (ret)
  18173. + mutex_release(&lock->base.dep_map, 1, _RET_IP_);
  18174. + else if (!ret && ww_ctx->acquired > 1)
  18175. + return ww_mutex_deadlock_injection(lock, ww_ctx);
  18176. +
  18177. + return ret;
  18178. +}
  18179. +EXPORT_SYMBOL_GPL(__ww_mutex_lock_interruptible);
  18180. +
  18181. +int __sched
  18182. +__ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ww_ctx)
  18183. +{
  18184. + int ret;
  18185. +
  18186. + might_sleep();
  18187. +
  18188. + mutex_acquire_nest(&lock->base.dep_map, 0, 0, &ww_ctx->dep_map, _RET_IP_);
  18189. + ret = rt_mutex_slowlock(&lock->base.lock, TASK_UNINTERRUPTIBLE, NULL, 0, ww_ctx);
  18190. + if (ret)
  18191. + mutex_release(&lock->base.dep_map, 1, _RET_IP_);
  18192. + else if (!ret && ww_ctx->acquired > 1)
  18193. + return ww_mutex_deadlock_injection(lock, ww_ctx);
  18194. +
  18195. + return ret;
  18196. +}
  18197. +EXPORT_SYMBOL_GPL(__ww_mutex_lock);
  18198. +
  18199. +void __sched ww_mutex_unlock(struct ww_mutex *lock)
  18200. +{
  18201. + int nest = !!lock->ctx;
  18202. +
  18203. + /*
  18204. + * The unlocking fastpath is the 0->1 transition from 'locked'
  18205. + * into 'unlocked' state:
  18206. + */
  18207. + if (nest) {
  18208. +#ifdef CONFIG_DEBUG_MUTEXES
  18209. + DEBUG_LOCKS_WARN_ON(!lock->ctx->acquired);
  18210. +#endif
  18211. + if (lock->ctx->acquired > 0)
  18212. + lock->ctx->acquired--;
  18213. + lock->ctx = NULL;
  18214. + }
  18215. +
  18216. + mutex_release(&lock->base.dep_map, nest, _RET_IP_);
  18217. + rt_mutex_unlock(&lock->base.lock);
  18218. +}
  18219. +EXPORT_SYMBOL(ww_mutex_unlock);
  18220. +#endif
  18221. diff -Nur linux-4.4.28.orig/kernel/locking/rtmutex_common.h linux-4.4.28/kernel/locking/rtmutex_common.h
  18222. --- linux-4.4.28.orig/kernel/locking/rtmutex_common.h 2016-10-28 09:53:25.000000000 +0200
  18223. +++ linux-4.4.28/kernel/locking/rtmutex_common.h 2016-11-01 21:26:58.772342023 +0100
  18224. @@ -27,6 +27,7 @@
  18225. struct rb_node pi_tree_entry;
  18226. struct task_struct *task;
  18227. struct rt_mutex *lock;
  18228. + bool savestate;
  18229. #ifdef CONFIG_DEBUG_RT_MUTEXES
  18230. unsigned long ip;
  18231. struct pid *deadlock_task_pid;
  18232. @@ -97,6 +98,9 @@
  18233. /*
  18234. * PI-futex support (proxy locking functions, etc.):
  18235. */
  18236. +#define PI_WAKEUP_INPROGRESS ((struct rt_mutex_waiter *) 1)
  18237. +#define PI_REQUEUE_INPROGRESS ((struct rt_mutex_waiter *) 2)
  18238. +
  18239. extern struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock);
  18240. extern void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
  18241. struct task_struct *proxy_owner);
  18242. @@ -110,7 +114,8 @@
  18243. struct rt_mutex_waiter *waiter);
  18244. extern int rt_mutex_timed_futex_lock(struct rt_mutex *l, struct hrtimer_sleeper *to);
  18245. extern bool rt_mutex_futex_unlock(struct rt_mutex *lock,
  18246. - struct wake_q_head *wqh);
  18247. + struct wake_q_head *wqh,
  18248. + struct wake_q_head *wq_sleeper);
  18249. extern void rt_mutex_adjust_prio(struct task_struct *task);
  18250. #ifdef CONFIG_DEBUG_RT_MUTEXES
  18251. @@ -119,4 +124,14 @@
  18252. # include "rtmutex.h"
  18253. #endif
  18254. +static inline void
  18255. +rt_mutex_init_waiter(struct rt_mutex_waiter *waiter, bool savestate)
  18256. +{
  18257. + debug_rt_mutex_init_waiter(waiter);
  18258. + waiter->task = NULL;
  18259. + waiter->savestate = savestate;
  18260. + RB_CLEAR_NODE(&waiter->pi_tree_entry);
  18261. + RB_CLEAR_NODE(&waiter->tree_entry);
  18262. +}
  18263. +
  18264. #endif
  18265. diff -Nur linux-4.4.28.orig/kernel/locking/spinlock.c linux-4.4.28/kernel/locking/spinlock.c
  18266. --- linux-4.4.28.orig/kernel/locking/spinlock.c 2016-10-28 09:53:25.000000000 +0200
  18267. +++ linux-4.4.28/kernel/locking/spinlock.c 2016-11-01 21:26:58.772342023 +0100
  18268. @@ -124,8 +124,11 @@
  18269. * __[spin|read|write]_lock_bh()
  18270. */
  18271. BUILD_LOCK_OPS(spin, raw_spinlock);
  18272. +
  18273. +#ifndef CONFIG_PREEMPT_RT_FULL
  18274. BUILD_LOCK_OPS(read, rwlock);
  18275. BUILD_LOCK_OPS(write, rwlock);
  18276. +#endif
  18277. #endif
  18278. @@ -209,6 +212,8 @@
  18279. EXPORT_SYMBOL(_raw_spin_unlock_bh);
  18280. #endif
  18281. +#ifndef CONFIG_PREEMPT_RT_FULL
  18282. +
  18283. #ifndef CONFIG_INLINE_READ_TRYLOCK
  18284. int __lockfunc _raw_read_trylock(rwlock_t *lock)
  18285. {
  18286. @@ -353,6 +358,8 @@
  18287. EXPORT_SYMBOL(_raw_write_unlock_bh);
  18288. #endif
  18289. +#endif /* !PREEMPT_RT_FULL */
  18290. +
  18291. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  18292. void __lockfunc _raw_spin_lock_nested(raw_spinlock_t *lock, int subclass)
  18293. diff -Nur linux-4.4.28.orig/kernel/locking/spinlock_debug.c linux-4.4.28/kernel/locking/spinlock_debug.c
  18294. --- linux-4.4.28.orig/kernel/locking/spinlock_debug.c 2016-10-28 09:53:25.000000000 +0200
  18295. +++ linux-4.4.28/kernel/locking/spinlock_debug.c 2016-11-01 21:26:58.772342023 +0100
  18296. @@ -31,6 +31,7 @@
  18297. EXPORT_SYMBOL(__raw_spin_lock_init);
  18298. +#ifndef CONFIG_PREEMPT_RT_FULL
  18299. void __rwlock_init(rwlock_t *lock, const char *name,
  18300. struct lock_class_key *key)
  18301. {
  18302. @@ -48,6 +49,7 @@
  18303. }
  18304. EXPORT_SYMBOL(__rwlock_init);
  18305. +#endif
  18306. static void spin_dump(raw_spinlock_t *lock, const char *msg)
  18307. {
  18308. @@ -159,6 +161,7 @@
  18309. arch_spin_unlock(&lock->raw_lock);
  18310. }
  18311. +#ifndef CONFIG_PREEMPT_RT_FULL
  18312. static void rwlock_bug(rwlock_t *lock, const char *msg)
  18313. {
  18314. if (!debug_locks_off())
  18315. @@ -300,3 +303,5 @@
  18316. debug_write_unlock(lock);
  18317. arch_write_unlock(&lock->raw_lock);
  18318. }
  18319. +
  18320. +#endif
  18321. diff -Nur linux-4.4.28.orig/kernel/panic.c linux-4.4.28/kernel/panic.c
  18322. --- linux-4.4.28.orig/kernel/panic.c 2016-10-28 09:53:25.000000000 +0200
  18323. +++ linux-4.4.28/kernel/panic.c 2016-11-01 21:26:58.772342023 +0100
  18324. @@ -61,6 +61,37 @@
  18325. cpu_relax();
  18326. }
  18327. +/*
  18328. + * Stop ourselves in NMI context if another CPU has already panicked. Arch code
  18329. + * may override this to prepare for crash dumping, e.g. save regs info.
  18330. + */
  18331. +void __weak nmi_panic_self_stop(struct pt_regs *regs)
  18332. +{
  18333. + panic_smp_self_stop();
  18334. +}
  18335. +
  18336. +atomic_t panic_cpu = ATOMIC_INIT(PANIC_CPU_INVALID);
  18337. +
  18338. +/*
  18339. + * A variant of panic() called from NMI context. We return if we've already
  18340. + * panicked on this CPU. If another CPU already panicked, loop in
  18341. + * nmi_panic_self_stop() which can provide architecture dependent code such
  18342. + * as saving register state for crash dump.
  18343. + */
  18344. +void nmi_panic(struct pt_regs *regs, const char *msg)
  18345. +{
  18346. + int old_cpu, cpu;
  18347. +
  18348. + cpu = raw_smp_processor_id();
  18349. + old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, cpu);
  18350. +
  18351. + if (old_cpu == PANIC_CPU_INVALID)
  18352. + panic("%s", msg);
  18353. + else if (old_cpu != cpu)
  18354. + nmi_panic_self_stop(regs);
  18355. +}
  18356. +EXPORT_SYMBOL(nmi_panic);
  18357. +
  18358. /**
  18359. * panic - halt the system
  18360. * @fmt: The text string to print
  18361. @@ -71,17 +102,17 @@
  18362. */
  18363. void panic(const char *fmt, ...)
  18364. {
  18365. - static DEFINE_SPINLOCK(panic_lock);
  18366. static char buf[1024];
  18367. va_list args;
  18368. long i, i_next = 0;
  18369. int state = 0;
  18370. + int old_cpu, this_cpu;
  18371. /*
  18372. * Disable local interrupts. This will prevent panic_smp_self_stop
  18373. * from deadlocking the first cpu that invokes the panic, since
  18374. * there is nothing to prevent an interrupt handler (that runs
  18375. - * after the panic_lock is acquired) from invoking panic again.
  18376. + * after setting panic_cpu) from invoking panic() again.
  18377. */
  18378. local_irq_disable();
  18379. @@ -94,8 +125,16 @@
  18380. * multiple parallel invocations of panic, all other CPUs either
  18381. * stop themself or will wait until they are stopped by the 1st CPU
  18382. * with smp_send_stop().
  18383. + *
  18384. + * `old_cpu == PANIC_CPU_INVALID' means this is the 1st CPU which
  18385. + * comes here, so go ahead.
  18386. + * `old_cpu == this_cpu' means we came from nmi_panic() which sets
  18387. + * panic_cpu to this CPU. In this case, this is also the 1st CPU.
  18388. */
  18389. - if (!spin_trylock(&panic_lock))
  18390. + this_cpu = raw_smp_processor_id();
  18391. + old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, this_cpu);
  18392. +
  18393. + if (old_cpu != PANIC_CPU_INVALID && old_cpu != this_cpu)
  18394. panic_smp_self_stop();
  18395. console_verbose();
  18396. @@ -400,9 +439,11 @@
  18397. static int init_oops_id(void)
  18398. {
  18399. +#ifndef CONFIG_PREEMPT_RT_FULL
  18400. if (!oops_id)
  18401. get_random_bytes(&oops_id, sizeof(oops_id));
  18402. else
  18403. +#endif
  18404. oops_id++;
  18405. return 0;
  18406. diff -Nur linux-4.4.28.orig/kernel/power/hibernate.c linux-4.4.28/kernel/power/hibernate.c
  18407. --- linux-4.4.28.orig/kernel/power/hibernate.c 2016-10-28 09:53:25.000000000 +0200
  18408. +++ linux-4.4.28/kernel/power/hibernate.c 2016-11-01 21:26:58.772342023 +0100
  18409. @@ -285,6 +285,8 @@
  18410. local_irq_disable();
  18411. + system_state = SYSTEM_SUSPEND;
  18412. +
  18413. error = syscore_suspend();
  18414. if (error) {
  18415. printk(KERN_ERR "PM: Some system devices failed to power down, "
  18416. @@ -314,6 +316,7 @@
  18417. syscore_resume();
  18418. Enable_irqs:
  18419. + system_state = SYSTEM_RUNNING;
  18420. local_irq_enable();
  18421. Enable_cpus:
  18422. @@ -438,6 +441,7 @@
  18423. goto Enable_cpus;
  18424. local_irq_disable();
  18425. + system_state = SYSTEM_SUSPEND;
  18426. error = syscore_suspend();
  18427. if (error)
  18428. @@ -471,6 +475,7 @@
  18429. syscore_resume();
  18430. Enable_irqs:
  18431. + system_state = SYSTEM_RUNNING;
  18432. local_irq_enable();
  18433. Enable_cpus:
  18434. @@ -556,6 +561,7 @@
  18435. goto Enable_cpus;
  18436. local_irq_disable();
  18437. + system_state = SYSTEM_SUSPEND;
  18438. syscore_suspend();
  18439. if (pm_wakeup_pending()) {
  18440. error = -EAGAIN;
  18441. @@ -568,6 +574,7 @@
  18442. Power_up:
  18443. syscore_resume();
  18444. + system_state = SYSTEM_RUNNING;
  18445. local_irq_enable();
  18446. Enable_cpus:
  18447. @@ -642,6 +649,10 @@
  18448. cpu_relax();
  18449. }
  18450. +#ifndef CONFIG_SUSPEND
  18451. +bool pm_in_action;
  18452. +#endif
  18453. +
  18454. /**
  18455. * hibernate - Carry out system hibernation, including saving the image.
  18456. */
  18457. @@ -654,6 +665,8 @@
  18458. return -EPERM;
  18459. }
  18460. + pm_in_action = true;
  18461. +
  18462. lock_system_sleep();
  18463. /* The snapshot device should not be opened while we're running */
  18464. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  18465. @@ -719,6 +732,7 @@
  18466. atomic_inc(&snapshot_device_available);
  18467. Unlock:
  18468. unlock_system_sleep();
  18469. + pm_in_action = false;
  18470. return error;
  18471. }
  18472. diff -Nur linux-4.4.28.orig/kernel/power/suspend.c linux-4.4.28/kernel/power/suspend.c
  18473. --- linux-4.4.28.orig/kernel/power/suspend.c 2016-10-28 09:53:25.000000000 +0200
  18474. +++ linux-4.4.28/kernel/power/suspend.c 2016-11-01 21:26:58.772342023 +0100
  18475. @@ -359,6 +359,8 @@
  18476. arch_suspend_disable_irqs();
  18477. BUG_ON(!irqs_disabled());
  18478. + system_state = SYSTEM_SUSPEND;
  18479. +
  18480. error = syscore_suspend();
  18481. if (!error) {
  18482. *wakeup = pm_wakeup_pending();
  18483. @@ -375,6 +377,8 @@
  18484. syscore_resume();
  18485. }
  18486. + system_state = SYSTEM_RUNNING;
  18487. +
  18488. arch_suspend_enable_irqs();
  18489. BUG_ON(irqs_disabled());
  18490. @@ -518,6 +522,8 @@
  18491. return error;
  18492. }
  18493. +bool pm_in_action;
  18494. +
  18495. /**
  18496. * pm_suspend - Externally visible function for suspending the system.
  18497. * @state: System sleep state to enter.
  18498. @@ -532,6 +538,8 @@
  18499. if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX)
  18500. return -EINVAL;
  18501. + pm_in_action = true;
  18502. +
  18503. error = enter_state(state);
  18504. if (error) {
  18505. suspend_stats.fail++;
  18506. @@ -539,6 +547,7 @@
  18507. } else {
  18508. suspend_stats.success++;
  18509. }
  18510. + pm_in_action = false;
  18511. return error;
  18512. }
  18513. EXPORT_SYMBOL(pm_suspend);
  18514. diff -Nur linux-4.4.28.orig/kernel/printk/printk.c linux-4.4.28/kernel/printk/printk.c
  18515. --- linux-4.4.28.orig/kernel/printk/printk.c 2016-10-28 09:53:25.000000000 +0200
  18516. +++ linux-4.4.28/kernel/printk/printk.c 2016-11-01 21:26:58.776342279 +0100
  18517. @@ -241,6 +241,65 @@
  18518. */
  18519. static DEFINE_RAW_SPINLOCK(logbuf_lock);
  18520. +#ifdef CONFIG_EARLY_PRINTK
  18521. +struct console *early_console;
  18522. +
  18523. +static void early_vprintk(const char *fmt, va_list ap)
  18524. +{
  18525. + if (early_console) {
  18526. + char buf[512];
  18527. + int n = vscnprintf(buf, sizeof(buf), fmt, ap);
  18528. +
  18529. + early_console->write(early_console, buf, n);
  18530. + }
  18531. +}
  18532. +
  18533. +asmlinkage void early_printk(const char *fmt, ...)
  18534. +{
  18535. + va_list ap;
  18536. +
  18537. + va_start(ap, fmt);
  18538. + early_vprintk(fmt, ap);
  18539. + va_end(ap);
  18540. +}
  18541. +
  18542. +/*
  18543. + * This is independent of any log levels - a global
  18544. + * kill switch that turns off all of printk.
  18545. + *
  18546. + * Used by the NMI watchdog if early-printk is enabled.
  18547. + */
  18548. +static bool __read_mostly printk_killswitch;
  18549. +
  18550. +static int __init force_early_printk_setup(char *str)
  18551. +{
  18552. + printk_killswitch = true;
  18553. + return 0;
  18554. +}
  18555. +early_param("force_early_printk", force_early_printk_setup);
  18556. +
  18557. +void printk_kill(void)
  18558. +{
  18559. + printk_killswitch = true;
  18560. +}
  18561. +
  18562. +#ifdef CONFIG_PRINTK
  18563. +static int forced_early_printk(const char *fmt, va_list ap)
  18564. +{
  18565. + if (!printk_killswitch)
  18566. + return 0;
  18567. + early_vprintk(fmt, ap);
  18568. + return 1;
  18569. +}
  18570. +#endif
  18571. +
  18572. +#else
  18573. +static inline int forced_early_printk(const char *fmt, va_list ap)
  18574. +{
  18575. + return 0;
  18576. +}
  18577. +#endif
  18578. +
  18579. #ifdef CONFIG_PRINTK
  18580. DECLARE_WAIT_QUEUE_HEAD(log_wait);
  18581. /* the next printk record to read by syslog(READ) or /proc/kmsg */
  18582. @@ -1203,6 +1262,7 @@
  18583. {
  18584. char *text;
  18585. int len = 0;
  18586. + int attempts = 0;
  18587. text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
  18588. if (!text)
  18589. @@ -1214,7 +1274,14 @@
  18590. u64 seq;
  18591. u32 idx;
  18592. enum log_flags prev;
  18593. -
  18594. + int num_msg;
  18595. +try_again:
  18596. + attempts++;
  18597. + if (attempts > 10) {
  18598. + len = -EBUSY;
  18599. + goto out;
  18600. + }
  18601. + num_msg = 0;
  18602. if (clear_seq < log_first_seq) {
  18603. /* messages are gone, move to first available one */
  18604. clear_seq = log_first_seq;
  18605. @@ -1235,6 +1302,14 @@
  18606. prev = msg->flags;
  18607. idx = log_next(idx);
  18608. seq++;
  18609. + num_msg++;
  18610. + if (num_msg > 5) {
  18611. + num_msg = 0;
  18612. + raw_spin_unlock_irq(&logbuf_lock);
  18613. + raw_spin_lock_irq(&logbuf_lock);
  18614. + if (clear_seq < log_first_seq)
  18615. + goto try_again;
  18616. + }
  18617. }
  18618. /* move first record forward until length fits into the buffer */
  18619. @@ -1248,6 +1323,14 @@
  18620. prev = msg->flags;
  18621. idx = log_next(idx);
  18622. seq++;
  18623. + num_msg++;
  18624. + if (num_msg > 5) {
  18625. + num_msg = 0;
  18626. + raw_spin_unlock_irq(&logbuf_lock);
  18627. + raw_spin_lock_irq(&logbuf_lock);
  18628. + if (clear_seq < log_first_seq)
  18629. + goto try_again;
  18630. + }
  18631. }
  18632. /* last message fitting into this dump */
  18633. @@ -1288,6 +1371,7 @@
  18634. clear_seq = log_next_seq;
  18635. clear_idx = log_next_idx;
  18636. }
  18637. +out:
  18638. raw_spin_unlock_irq(&logbuf_lock);
  18639. kfree(text);
  18640. @@ -1443,6 +1527,12 @@
  18641. if (!console_drivers)
  18642. return;
  18643. + if (IS_ENABLED(CONFIG_PREEMPT_RT_BASE)) {
  18644. + if (in_irq() || in_nmi())
  18645. + return;
  18646. + }
  18647. +
  18648. + migrate_disable();
  18649. for_each_console(con) {
  18650. if (exclusive_console && con != exclusive_console)
  18651. continue;
  18652. @@ -1458,6 +1548,7 @@
  18653. else
  18654. con->write(con, text, len);
  18655. }
  18656. + migrate_enable();
  18657. }
  18658. /*
  18659. @@ -1518,6 +1609,15 @@
  18660. static int console_trylock_for_printk(void)
  18661. {
  18662. unsigned int cpu = smp_processor_id();
  18663. +#ifdef CONFIG_PREEMPT_RT_FULL
  18664. + int lock = !early_boot_irqs_disabled && (preempt_count() == 0) &&
  18665. + !irqs_disabled();
  18666. +#else
  18667. + int lock = 1;
  18668. +#endif
  18669. +
  18670. + if (!lock)
  18671. + return 0;
  18672. if (!console_trylock())
  18673. return 0;
  18674. @@ -1672,6 +1772,13 @@
  18675. /* cpu currently holding logbuf_lock in this function */
  18676. static unsigned int logbuf_cpu = UINT_MAX;
  18677. + /*
  18678. + * Fall back to early_printk if a debugging subsystem has
  18679. + * killed printk output
  18680. + */
  18681. + if (unlikely(forced_early_printk(fmt, args)))
  18682. + return 1;
  18683. +
  18684. if (level == LOGLEVEL_SCHED) {
  18685. level = LOGLEVEL_DEFAULT;
  18686. in_sched = true;
  18687. @@ -1813,8 +1920,7 @@
  18688. * console_sem which would prevent anyone from printing to
  18689. * console
  18690. */
  18691. - preempt_disable();
  18692. -
  18693. + migrate_disable();
  18694. /*
  18695. * Try to acquire and then immediately release the console
  18696. * semaphore. The release will print out buffers and wake up
  18697. @@ -1822,7 +1928,7 @@
  18698. */
  18699. if (console_trylock_for_printk())
  18700. console_unlock();
  18701. - preempt_enable();
  18702. + migrate_enable();
  18703. lockdep_on();
  18704. }
  18705. @@ -1961,26 +2067,6 @@
  18706. #endif /* CONFIG_PRINTK */
  18707. -#ifdef CONFIG_EARLY_PRINTK
  18708. -struct console *early_console;
  18709. -
  18710. -asmlinkage __visible void early_printk(const char *fmt, ...)
  18711. -{
  18712. - va_list ap;
  18713. - char buf[512];
  18714. - int n;
  18715. -
  18716. - if (!early_console)
  18717. - return;
  18718. -
  18719. - va_start(ap, fmt);
  18720. - n = vscnprintf(buf, sizeof(buf), fmt, ap);
  18721. - va_end(ap);
  18722. -
  18723. - early_console->write(early_console, buf, n);
  18724. -}
  18725. -#endif
  18726. -
  18727. static int __add_preferred_console(char *name, int idx, char *options,
  18728. char *brl_options)
  18729. {
  18730. @@ -2202,11 +2288,16 @@
  18731. goto out;
  18732. len = cont_print_text(text, size);
  18733. +#ifdef CONFIG_PREEMPT_RT_FULL
  18734. + raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  18735. + call_console_drivers(cont.level, NULL, 0, text, len);
  18736. +#else
  18737. raw_spin_unlock(&logbuf_lock);
  18738. stop_critical_timings();
  18739. call_console_drivers(cont.level, NULL, 0, text, len);
  18740. start_critical_timings();
  18741. local_irq_restore(flags);
  18742. +#endif
  18743. return;
  18744. out:
  18745. raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  18746. @@ -2316,13 +2407,17 @@
  18747. console_idx = log_next(console_idx);
  18748. console_seq++;
  18749. console_prev = msg->flags;
  18750. +#ifdef CONFIG_PREEMPT_RT_FULL
  18751. + raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  18752. + call_console_drivers(level, ext_text, ext_len, text, len);
  18753. +#else
  18754. raw_spin_unlock(&logbuf_lock);
  18755. stop_critical_timings(); /* don't trace print latency */
  18756. call_console_drivers(level, ext_text, ext_len, text, len);
  18757. start_critical_timings();
  18758. local_irq_restore(flags);
  18759. -
  18760. +#endif
  18761. if (do_cond_resched)
  18762. cond_resched();
  18763. }
  18764. @@ -2374,6 +2469,11 @@
  18765. {
  18766. struct console *c;
  18767. + if (IS_ENABLED(CONFIG_PREEMPT_RT_BASE)) {
  18768. + if (in_irq() || in_nmi())
  18769. + return;
  18770. + }
  18771. +
  18772. /*
  18773. * console_unblank can no longer be called in interrupt context unless
  18774. * oops_in_progress is set to 1..
  18775. diff -Nur linux-4.4.28.orig/kernel/ptrace.c linux-4.4.28/kernel/ptrace.c
  18776. --- linux-4.4.28.orig/kernel/ptrace.c 2016-10-28 09:53:25.000000000 +0200
  18777. +++ linux-4.4.28/kernel/ptrace.c 2016-11-01 21:26:58.776342279 +0100
  18778. @@ -129,7 +129,14 @@
  18779. spin_lock_irq(&task->sighand->siglock);
  18780. if (task_is_traced(task) && !__fatal_signal_pending(task)) {
  18781. - task->state = __TASK_TRACED;
  18782. + unsigned long flags;
  18783. +
  18784. + raw_spin_lock_irqsave(&task->pi_lock, flags);
  18785. + if (task->state & __TASK_TRACED)
  18786. + task->state = __TASK_TRACED;
  18787. + else
  18788. + task->saved_state = __TASK_TRACED;
  18789. + raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  18790. ret = true;
  18791. }
  18792. spin_unlock_irq(&task->sighand->siglock);
  18793. diff -Nur linux-4.4.28.orig/kernel/rcu/rcutorture.c linux-4.4.28/kernel/rcu/rcutorture.c
  18794. --- linux-4.4.28.orig/kernel/rcu/rcutorture.c 2016-10-28 09:53:25.000000000 +0200
  18795. +++ linux-4.4.28/kernel/rcu/rcutorture.c 2016-11-01 21:26:58.776342279 +0100
  18796. @@ -390,6 +390,7 @@
  18797. .name = "rcu"
  18798. };
  18799. +#ifndef CONFIG_PREEMPT_RT_FULL
  18800. /*
  18801. * Definitions for rcu_bh torture testing.
  18802. */
  18803. @@ -429,6 +430,12 @@
  18804. .name = "rcu_bh"
  18805. };
  18806. +#else
  18807. +static struct rcu_torture_ops rcu_bh_ops = {
  18808. + .ttype = INVALID_RCU_FLAVOR,
  18809. +};
  18810. +#endif
  18811. +
  18812. /*
  18813. * Don't even think about trying any of these in real life!!!
  18814. * The names includes "busted", and they really means it!
  18815. diff -Nur linux-4.4.28.orig/kernel/rcu/tree.c linux-4.4.28/kernel/rcu/tree.c
  18816. --- linux-4.4.28.orig/kernel/rcu/tree.c 2016-10-28 09:53:25.000000000 +0200
  18817. +++ linux-4.4.28/kernel/rcu/tree.c 2016-11-01 21:26:58.780342543 +0100
  18818. @@ -56,6 +56,11 @@
  18819. #include <linux/random.h>
  18820. #include <linux/trace_events.h>
  18821. #include <linux/suspend.h>
  18822. +#include <linux/delay.h>
  18823. +#include <linux/gfp.h>
  18824. +#include <linux/oom.h>
  18825. +#include <linux/smpboot.h>
  18826. +#include "../time/tick-internal.h"
  18827. #include "tree.h"
  18828. #include "rcu.h"
  18829. @@ -266,6 +271,19 @@
  18830. }
  18831. }
  18832. +#ifdef CONFIG_PREEMPT_RT_FULL
  18833. +static void rcu_preempt_qs(void);
  18834. +
  18835. +void rcu_bh_qs(void)
  18836. +{
  18837. + unsigned long flags;
  18838. +
  18839. + /* Callers to this function, rcu_preempt_qs(), must disable irqs. */
  18840. + local_irq_save(flags);
  18841. + rcu_preempt_qs();
  18842. + local_irq_restore(flags);
  18843. +}
  18844. +#else
  18845. void rcu_bh_qs(void)
  18846. {
  18847. if (__this_cpu_read(rcu_bh_data.cpu_no_qs.s)) {
  18848. @@ -275,6 +293,7 @@
  18849. __this_cpu_write(rcu_bh_data.cpu_no_qs.b.norm, false);
  18850. }
  18851. }
  18852. +#endif
  18853. static DEFINE_PER_CPU(int, rcu_sched_qs_mask);
  18854. @@ -435,11 +454,13 @@
  18855. /*
  18856. * Return the number of RCU BH batches started thus far for debug & stats.
  18857. */
  18858. +#ifndef CONFIG_PREEMPT_RT_FULL
  18859. unsigned long rcu_batches_started_bh(void)
  18860. {
  18861. return rcu_bh_state.gpnum;
  18862. }
  18863. EXPORT_SYMBOL_GPL(rcu_batches_started_bh);
  18864. +#endif
  18865. /*
  18866. * Return the number of RCU batches completed thus far for debug & stats.
  18867. @@ -459,6 +480,7 @@
  18868. }
  18869. EXPORT_SYMBOL_GPL(rcu_batches_completed_sched);
  18870. +#ifndef CONFIG_PREEMPT_RT_FULL
  18871. /*
  18872. * Return the number of RCU BH batches completed thus far for debug & stats.
  18873. */
  18874. @@ -486,6 +508,13 @@
  18875. }
  18876. EXPORT_SYMBOL_GPL(rcu_bh_force_quiescent_state);
  18877. +#else
  18878. +void rcu_force_quiescent_state(void)
  18879. +{
  18880. +}
  18881. +EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
  18882. +#endif
  18883. +
  18884. /*
  18885. * Force a quiescent state for RCU-sched.
  18886. */
  18887. @@ -536,9 +565,11 @@
  18888. case RCU_FLAVOR:
  18889. rsp = rcu_state_p;
  18890. break;
  18891. +#ifndef CONFIG_PREEMPT_RT_FULL
  18892. case RCU_BH_FLAVOR:
  18893. rsp = &rcu_bh_state;
  18894. break;
  18895. +#endif
  18896. case RCU_SCHED_FLAVOR:
  18897. rsp = &rcu_sched_state;
  18898. break;
  18899. @@ -1590,7 +1621,6 @@
  18900. int needmore;
  18901. struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
  18902. - rcu_nocb_gp_cleanup(rsp, rnp);
  18903. rnp->need_future_gp[c & 0x1] = 0;
  18904. needmore = rnp->need_future_gp[(c + 1) & 0x1];
  18905. trace_rcu_future_gp(rnp, rdp, c,
  18906. @@ -1611,7 +1641,7 @@
  18907. !READ_ONCE(rsp->gp_flags) ||
  18908. !rsp->gp_kthread)
  18909. return;
  18910. - wake_up(&rsp->gp_wq);
  18911. + swake_up(&rsp->gp_wq);
  18912. }
  18913. /*
  18914. @@ -1991,6 +2021,7 @@
  18915. int nocb = 0;
  18916. struct rcu_data *rdp;
  18917. struct rcu_node *rnp = rcu_get_root(rsp);
  18918. + struct swait_queue_head *sq;
  18919. WRITE_ONCE(rsp->gp_activity, jiffies);
  18920. raw_spin_lock_irq(&rnp->lock);
  18921. @@ -2029,7 +2060,9 @@
  18922. needgp = __note_gp_changes(rsp, rnp, rdp) || needgp;
  18923. /* smp_mb() provided by prior unlock-lock pair. */
  18924. nocb += rcu_future_gp_cleanup(rsp, rnp);
  18925. + sq = rcu_nocb_gp_get(rnp);
  18926. raw_spin_unlock_irq(&rnp->lock);
  18927. + rcu_nocb_gp_cleanup(sq);
  18928. cond_resched_rcu_qs();
  18929. WRITE_ONCE(rsp->gp_activity, jiffies);
  18930. rcu_gp_slow(rsp, gp_cleanup_delay);
  18931. @@ -2076,7 +2109,7 @@
  18932. READ_ONCE(rsp->gpnum),
  18933. TPS("reqwait"));
  18934. rsp->gp_state = RCU_GP_WAIT_GPS;
  18935. - wait_event_interruptible(rsp->gp_wq,
  18936. + swait_event_interruptible(rsp->gp_wq,
  18937. READ_ONCE(rsp->gp_flags) &
  18938. RCU_GP_FLAG_INIT);
  18939. rsp->gp_state = RCU_GP_DONE_GPS;
  18940. @@ -2106,7 +2139,7 @@
  18941. READ_ONCE(rsp->gpnum),
  18942. TPS("fqswait"));
  18943. rsp->gp_state = RCU_GP_WAIT_FQS;
  18944. - ret = wait_event_interruptible_timeout(rsp->gp_wq,
  18945. + ret = swait_event_interruptible_timeout(rsp->gp_wq,
  18946. rcu_gp_fqs_check_wake(rsp, &gf), j);
  18947. rsp->gp_state = RCU_GP_DOING_FQS;
  18948. /* Locking provides needed memory barriers. */
  18949. @@ -2230,7 +2263,7 @@
  18950. WARN_ON_ONCE(!rcu_gp_in_progress(rsp));
  18951. WRITE_ONCE(rsp->gp_flags, READ_ONCE(rsp->gp_flags) | RCU_GP_FLAG_FQS);
  18952. raw_spin_unlock_irqrestore(&rcu_get_root(rsp)->lock, flags);
  18953. - rcu_gp_kthread_wake(rsp);
  18954. + swake_up(&rsp->gp_wq); /* Memory barrier implied by swake_up() path. */
  18955. }
  18956. /*
  18957. @@ -2891,7 +2924,7 @@
  18958. }
  18959. WRITE_ONCE(rsp->gp_flags, READ_ONCE(rsp->gp_flags) | RCU_GP_FLAG_FQS);
  18960. raw_spin_unlock_irqrestore(&rnp_old->lock, flags);
  18961. - rcu_gp_kthread_wake(rsp);
  18962. + swake_up(&rsp->gp_wq); /* Memory barrier implied by swake_up() path. */
  18963. }
  18964. /*
  18965. @@ -2934,18 +2967,17 @@
  18966. /*
  18967. * Do RCU core processing for the current CPU.
  18968. */
  18969. -static void rcu_process_callbacks(struct softirq_action *unused)
  18970. +static void rcu_process_callbacks(void)
  18971. {
  18972. struct rcu_state *rsp;
  18973. if (cpu_is_offline(smp_processor_id()))
  18974. return;
  18975. - trace_rcu_utilization(TPS("Start RCU core"));
  18976. for_each_rcu_flavor(rsp)
  18977. __rcu_process_callbacks(rsp);
  18978. - trace_rcu_utilization(TPS("End RCU core"));
  18979. }
  18980. +static DEFINE_PER_CPU(struct task_struct *, rcu_cpu_kthread_task);
  18981. /*
  18982. * Schedule RCU callback invocation. If the specified type of RCU
  18983. * does not support RCU priority boosting, just do a direct call,
  18984. @@ -2957,18 +2989,105 @@
  18985. {
  18986. if (unlikely(!READ_ONCE(rcu_scheduler_fully_active)))
  18987. return;
  18988. - if (likely(!rsp->boost)) {
  18989. - rcu_do_batch(rsp, rdp);
  18990. + rcu_do_batch(rsp, rdp);
  18991. +}
  18992. +
  18993. +static void rcu_wake_cond(struct task_struct *t, int status)
  18994. +{
  18995. + /*
  18996. + * If the thread is yielding, only wake it when this
  18997. + * is invoked from idle
  18998. + */
  18999. + if (t && (status != RCU_KTHREAD_YIELDING || is_idle_task(current)))
  19000. + wake_up_process(t);
  19001. +}
  19002. +
  19003. +/*
  19004. + * Wake up this CPU's rcuc kthread to do RCU core processing.
  19005. + */
  19006. +static void invoke_rcu_core(void)
  19007. +{
  19008. + unsigned long flags;
  19009. + struct task_struct *t;
  19010. +
  19011. + if (!cpu_online(smp_processor_id()))
  19012. return;
  19013. + local_irq_save(flags);
  19014. + __this_cpu_write(rcu_cpu_has_work, 1);
  19015. + t = __this_cpu_read(rcu_cpu_kthread_task);
  19016. + if (t != NULL && current != t)
  19017. + rcu_wake_cond(t, __this_cpu_read(rcu_cpu_kthread_status));
  19018. + local_irq_restore(flags);
  19019. +}
  19020. +
  19021. +static void rcu_cpu_kthread_park(unsigned int cpu)
  19022. +{
  19023. + per_cpu(rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
  19024. +}
  19025. +
  19026. +static int rcu_cpu_kthread_should_run(unsigned int cpu)
  19027. +{
  19028. + return __this_cpu_read(rcu_cpu_has_work);
  19029. +}
  19030. +
  19031. +/*
  19032. + * Per-CPU kernel thread that invokes RCU callbacks. This replaces the
  19033. + * RCU softirq used in flavors and configurations of RCU that do not
  19034. + * support RCU priority boosting.
  19035. + */
  19036. +static void rcu_cpu_kthread(unsigned int cpu)
  19037. +{
  19038. + unsigned int *statusp = this_cpu_ptr(&rcu_cpu_kthread_status);
  19039. + char work, *workp = this_cpu_ptr(&rcu_cpu_has_work);
  19040. + int spincnt;
  19041. +
  19042. + for (spincnt = 0; spincnt < 10; spincnt++) {
  19043. + trace_rcu_utilization(TPS("Start CPU kthread@rcu_wait"));
  19044. + local_bh_disable();
  19045. + *statusp = RCU_KTHREAD_RUNNING;
  19046. + this_cpu_inc(rcu_cpu_kthread_loops);
  19047. + local_irq_disable();
  19048. + work = *workp;
  19049. + *workp = 0;
  19050. + local_irq_enable();
  19051. + if (work)
  19052. + rcu_process_callbacks();
  19053. + local_bh_enable();
  19054. + if (*workp == 0) {
  19055. + trace_rcu_utilization(TPS("End CPU kthread@rcu_wait"));
  19056. + *statusp = RCU_KTHREAD_WAITING;
  19057. + return;
  19058. + }
  19059. }
  19060. - invoke_rcu_callbacks_kthread();
  19061. + *statusp = RCU_KTHREAD_YIELDING;
  19062. + trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield"));
  19063. + schedule_timeout_interruptible(2);
  19064. + trace_rcu_utilization(TPS("End CPU kthread@rcu_yield"));
  19065. + *statusp = RCU_KTHREAD_WAITING;
  19066. }
  19067. -static void invoke_rcu_core(void)
  19068. +static struct smp_hotplug_thread rcu_cpu_thread_spec = {
  19069. + .store = &rcu_cpu_kthread_task,
  19070. + .thread_should_run = rcu_cpu_kthread_should_run,
  19071. + .thread_fn = rcu_cpu_kthread,
  19072. + .thread_comm = "rcuc/%u",
  19073. + .setup = rcu_cpu_kthread_setup,
  19074. + .park = rcu_cpu_kthread_park,
  19075. +};
  19076. +
  19077. +/*
  19078. + * Spawn per-CPU RCU core processing kthreads.
  19079. + */
  19080. +static int __init rcu_spawn_core_kthreads(void)
  19081. {
  19082. - if (cpu_online(smp_processor_id()))
  19083. - raise_softirq(RCU_SOFTIRQ);
  19084. + int cpu;
  19085. +
  19086. + for_each_possible_cpu(cpu)
  19087. + per_cpu(rcu_cpu_has_work, cpu) = 0;
  19088. + BUG_ON(smpboot_register_percpu_thread(&rcu_cpu_thread_spec));
  19089. + return 0;
  19090. }
  19091. +early_initcall(rcu_spawn_core_kthreads);
  19092. /*
  19093. * Handle any core-RCU processing required by a call_rcu() invocation.
  19094. @@ -3114,6 +3233,7 @@
  19095. }
  19096. EXPORT_SYMBOL_GPL(call_rcu_sched);
  19097. +#ifndef CONFIG_PREEMPT_RT_FULL
  19098. /*
  19099. * Queue an RCU callback for invocation after a quicker grace period.
  19100. */
  19101. @@ -3122,6 +3242,7 @@
  19102. __call_rcu(head, func, &rcu_bh_state, -1, 0);
  19103. }
  19104. EXPORT_SYMBOL_GPL(call_rcu_bh);
  19105. +#endif
  19106. /*
  19107. * Queue an RCU callback for lazy invocation after a grace period.
  19108. @@ -3213,6 +3334,7 @@
  19109. }
  19110. EXPORT_SYMBOL_GPL(synchronize_sched);
  19111. +#ifndef CONFIG_PREEMPT_RT_FULL
  19112. /**
  19113. * synchronize_rcu_bh - wait until an rcu_bh grace period has elapsed.
  19114. *
  19115. @@ -3239,6 +3361,7 @@
  19116. wait_rcu_gp(call_rcu_bh);
  19117. }
  19118. EXPORT_SYMBOL_GPL(synchronize_rcu_bh);
  19119. +#endif
  19120. /**
  19121. * get_state_synchronize_rcu - Snapshot current RCU state
  19122. @@ -3524,7 +3647,7 @@
  19123. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  19124. if (wake) {
  19125. smp_mb(); /* EGP done before wake_up(). */
  19126. - wake_up(&rsp->expedited_wq);
  19127. + swake_up(&rsp->expedited_wq);
  19128. }
  19129. break;
  19130. }
  19131. @@ -3781,7 +3904,7 @@
  19132. jiffies_start = jiffies;
  19133. for (;;) {
  19134. - ret = wait_event_interruptible_timeout(
  19135. + ret = swait_event_timeout(
  19136. rsp->expedited_wq,
  19137. sync_rcu_preempt_exp_done(rnp_root),
  19138. jiffies_stall);
  19139. @@ -3789,7 +3912,7 @@
  19140. return;
  19141. if (ret < 0) {
  19142. /* Hit a signal, disable CPU stall warnings. */
  19143. - wait_event(rsp->expedited_wq,
  19144. + swait_event(rsp->expedited_wq,
  19145. sync_rcu_preempt_exp_done(rnp_root));
  19146. return;
  19147. }
  19148. @@ -4101,6 +4224,7 @@
  19149. mutex_unlock(&rsp->barrier_mutex);
  19150. }
  19151. +#ifndef CONFIG_PREEMPT_RT_FULL
  19152. /**
  19153. * rcu_barrier_bh - Wait until all in-flight call_rcu_bh() callbacks complete.
  19154. */
  19155. @@ -4109,6 +4233,7 @@
  19156. _rcu_barrier(&rcu_bh_state);
  19157. }
  19158. EXPORT_SYMBOL_GPL(rcu_barrier_bh);
  19159. +#endif
  19160. /**
  19161. * rcu_barrier_sched - Wait for in-flight call_rcu_sched() callbacks.
  19162. @@ -4455,8 +4580,8 @@
  19163. }
  19164. }
  19165. - init_waitqueue_head(&rsp->gp_wq);
  19166. - init_waitqueue_head(&rsp->expedited_wq);
  19167. + init_swait_queue_head(&rsp->gp_wq);
  19168. + init_swait_queue_head(&rsp->expedited_wq);
  19169. rnp = rsp->level[rcu_num_lvls - 1];
  19170. for_each_possible_cpu(i) {
  19171. while (i > rnp->grphi)
  19172. @@ -4576,12 +4701,13 @@
  19173. rcu_bootup_announce();
  19174. rcu_init_geometry();
  19175. +#ifndef CONFIG_PREEMPT_RT_FULL
  19176. rcu_init_one(&rcu_bh_state, &rcu_bh_data);
  19177. +#endif
  19178. rcu_init_one(&rcu_sched_state, &rcu_sched_data);
  19179. if (dump_tree)
  19180. rcu_dump_rcu_node_tree(&rcu_sched_state);
  19181. __rcu_init_preempt();
  19182. - open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
  19183. /*
  19184. * We don't need protection against CPU-hotplug here because
  19185. diff -Nur linux-4.4.28.orig/kernel/rcu/tree.h linux-4.4.28/kernel/rcu/tree.h
  19186. --- linux-4.4.28.orig/kernel/rcu/tree.h 2016-10-28 09:53:25.000000000 +0200
  19187. +++ linux-4.4.28/kernel/rcu/tree.h 2016-11-01 21:26:58.780342543 +0100
  19188. @@ -27,6 +27,7 @@
  19189. #include <linux/threads.h>
  19190. #include <linux/cpumask.h>
  19191. #include <linux/seqlock.h>
  19192. +#include <linux/swait.h>
  19193. #include <linux/stop_machine.h>
  19194. /*
  19195. @@ -241,7 +242,7 @@
  19196. /* Refused to boost: not sure why, though. */
  19197. /* This can happen due to race conditions. */
  19198. #ifdef CONFIG_RCU_NOCB_CPU
  19199. - wait_queue_head_t nocb_gp_wq[2];
  19200. + struct swait_queue_head nocb_gp_wq[2];
  19201. /* Place for rcu_nocb_kthread() to wait GP. */
  19202. #endif /* #ifdef CONFIG_RCU_NOCB_CPU */
  19203. int need_future_gp[2];
  19204. @@ -393,7 +394,7 @@
  19205. atomic_long_t nocb_q_count_lazy; /* invocation (all stages). */
  19206. struct rcu_head *nocb_follower_head; /* CBs ready to invoke. */
  19207. struct rcu_head **nocb_follower_tail;
  19208. - wait_queue_head_t nocb_wq; /* For nocb kthreads to sleep on. */
  19209. + struct swait_queue_head nocb_wq; /* For nocb kthreads to sleep on. */
  19210. struct task_struct *nocb_kthread;
  19211. int nocb_defer_wakeup; /* Defer wakeup of nocb_kthread. */
  19212. @@ -472,7 +473,7 @@
  19213. unsigned long gpnum; /* Current gp number. */
  19214. unsigned long completed; /* # of last completed gp. */
  19215. struct task_struct *gp_kthread; /* Task for grace periods. */
  19216. - wait_queue_head_t gp_wq; /* Where GP task waits. */
  19217. + struct swait_queue_head gp_wq; /* Where GP task waits. */
  19218. short gp_flags; /* Commands for GP task. */
  19219. short gp_state; /* GP kthread sleep state. */
  19220. @@ -504,7 +505,7 @@
  19221. atomic_long_t expedited_workdone3; /* # done by others #3. */
  19222. atomic_long_t expedited_normal; /* # fallbacks to normal. */
  19223. atomic_t expedited_need_qs; /* # CPUs left to check in. */
  19224. - wait_queue_head_t expedited_wq; /* Wait for check-ins. */
  19225. + struct swait_queue_head expedited_wq; /* Wait for check-ins. */
  19226. int ncpus_snap; /* # CPUs seen last time. */
  19227. unsigned long jiffies_force_qs; /* Time at which to invoke */
  19228. @@ -556,18 +557,18 @@
  19229. */
  19230. extern struct rcu_state rcu_sched_state;
  19231. +#ifndef CONFIG_PREEMPT_RT_FULL
  19232. extern struct rcu_state rcu_bh_state;
  19233. +#endif
  19234. #ifdef CONFIG_PREEMPT_RCU
  19235. extern struct rcu_state rcu_preempt_state;
  19236. #endif /* #ifdef CONFIG_PREEMPT_RCU */
  19237. -#ifdef CONFIG_RCU_BOOST
  19238. DECLARE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
  19239. DECLARE_PER_CPU(int, rcu_cpu_kthread_cpu);
  19240. DECLARE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
  19241. DECLARE_PER_CPU(char, rcu_cpu_has_work);
  19242. -#endif /* #ifdef CONFIG_RCU_BOOST */
  19243. #ifndef RCU_TREE_NONCORE
  19244. @@ -587,10 +588,9 @@
  19245. static void __init __rcu_init_preempt(void);
  19246. static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags);
  19247. static void rcu_preempt_boost_start_gp(struct rcu_node *rnp);
  19248. -static void invoke_rcu_callbacks_kthread(void);
  19249. static bool rcu_is_callbacks_kthread(void);
  19250. +static void rcu_cpu_kthread_setup(unsigned int cpu);
  19251. #ifdef CONFIG_RCU_BOOST
  19252. -static void rcu_preempt_do_callbacks(void);
  19253. static int rcu_spawn_one_boost_kthread(struct rcu_state *rsp,
  19254. struct rcu_node *rnp);
  19255. #endif /* #ifdef CONFIG_RCU_BOOST */
  19256. @@ -607,7 +607,8 @@
  19257. static void increment_cpu_stall_ticks(void);
  19258. static bool rcu_nocb_cpu_needs_barrier(struct rcu_state *rsp, int cpu);
  19259. static void rcu_nocb_gp_set(struct rcu_node *rnp, int nrq);
  19260. -static void rcu_nocb_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp);
  19261. +static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp);
  19262. +static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq);
  19263. static void rcu_init_one_nocb(struct rcu_node *rnp);
  19264. static bool __call_rcu_nocb(struct rcu_data *rdp, struct rcu_head *rhp,
  19265. bool lazy, unsigned long flags);
  19266. diff -Nur linux-4.4.28.orig/kernel/rcu/tree_plugin.h linux-4.4.28/kernel/rcu/tree_plugin.h
  19267. --- linux-4.4.28.orig/kernel/rcu/tree_plugin.h 2016-10-28 09:53:25.000000000 +0200
  19268. +++ linux-4.4.28/kernel/rcu/tree_plugin.h 2016-11-01 21:26:58.780342543 +0100
  19269. @@ -24,25 +24,10 @@
  19270. * Paul E. McKenney <paulmck@linux.vnet.ibm.com>
  19271. */
  19272. -#include <linux/delay.h>
  19273. -#include <linux/gfp.h>
  19274. -#include <linux/oom.h>
  19275. -#include <linux/smpboot.h>
  19276. -#include "../time/tick-internal.h"
  19277. -
  19278. #ifdef CONFIG_RCU_BOOST
  19279. #include "../locking/rtmutex_common.h"
  19280. -/*
  19281. - * Control variables for per-CPU and per-rcu_node kthreads. These
  19282. - * handle all flavors of RCU.
  19283. - */
  19284. -static DEFINE_PER_CPU(struct task_struct *, rcu_cpu_kthread_task);
  19285. -DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
  19286. -DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
  19287. -DEFINE_PER_CPU(char, rcu_cpu_has_work);
  19288. -
  19289. #else /* #ifdef CONFIG_RCU_BOOST */
  19290. /*
  19291. @@ -55,6 +40,14 @@
  19292. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  19293. +/*
  19294. + * Control variables for per-CPU and per-rcu_node kthreads. These
  19295. + * handle all flavors of RCU.
  19296. + */
  19297. +DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
  19298. +DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
  19299. +DEFINE_PER_CPU(char, rcu_cpu_has_work);
  19300. +
  19301. #ifdef CONFIG_RCU_NOCB_CPU
  19302. static cpumask_var_t rcu_nocb_mask; /* CPUs to have callbacks offloaded. */
  19303. static bool have_rcu_nocb_mask; /* Was rcu_nocb_mask allocated? */
  19304. @@ -432,7 +425,7 @@
  19305. }
  19306. /* Hardware IRQ handlers cannot block, complain if they get here. */
  19307. - if (in_irq() || in_serving_softirq()) {
  19308. + if (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_OFFSET)) {
  19309. lockdep_rcu_suspicious(__FILE__, __LINE__,
  19310. "rcu_read_unlock() from irq or softirq with blocking in critical section!!!\n");
  19311. pr_alert("->rcu_read_unlock_special: %#x (b: %d, enq: %d nq: %d)\n",
  19312. @@ -645,15 +638,6 @@
  19313. t->rcu_read_unlock_special.b.need_qs = true;
  19314. }
  19315. -#ifdef CONFIG_RCU_BOOST
  19316. -
  19317. -static void rcu_preempt_do_callbacks(void)
  19318. -{
  19319. - rcu_do_batch(rcu_state_p, this_cpu_ptr(rcu_data_p));
  19320. -}
  19321. -
  19322. -#endif /* #ifdef CONFIG_RCU_BOOST */
  19323. -
  19324. /*
  19325. * Queue a preemptible-RCU callback for invocation after a grace period.
  19326. */
  19327. @@ -930,6 +914,19 @@
  19328. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  19329. +/*
  19330. + * If boosting, set rcuc kthreads to realtime priority.
  19331. + */
  19332. +static void rcu_cpu_kthread_setup(unsigned int cpu)
  19333. +{
  19334. +#ifdef CONFIG_RCU_BOOST
  19335. + struct sched_param sp;
  19336. +
  19337. + sp.sched_priority = kthread_prio;
  19338. + sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
  19339. +#endif /* #ifdef CONFIG_RCU_BOOST */
  19340. +}
  19341. +
  19342. #ifdef CONFIG_RCU_BOOST
  19343. #include "../locking/rtmutex_common.h"
  19344. @@ -961,16 +958,6 @@
  19345. #endif /* #else #ifdef CONFIG_RCU_TRACE */
  19346. -static void rcu_wake_cond(struct task_struct *t, int status)
  19347. -{
  19348. - /*
  19349. - * If the thread is yielding, only wake it when this
  19350. - * is invoked from idle
  19351. - */
  19352. - if (status != RCU_KTHREAD_YIELDING || is_idle_task(current))
  19353. - wake_up_process(t);
  19354. -}
  19355. -
  19356. /*
  19357. * Carry out RCU priority boosting on the task indicated by ->exp_tasks
  19358. * or ->boost_tasks, advancing the pointer to the next task in the
  19359. @@ -1115,23 +1102,6 @@
  19360. }
  19361. /*
  19362. - * Wake up the per-CPU kthread to invoke RCU callbacks.
  19363. - */
  19364. -static void invoke_rcu_callbacks_kthread(void)
  19365. -{
  19366. - unsigned long flags;
  19367. -
  19368. - local_irq_save(flags);
  19369. - __this_cpu_write(rcu_cpu_has_work, 1);
  19370. - if (__this_cpu_read(rcu_cpu_kthread_task) != NULL &&
  19371. - current != __this_cpu_read(rcu_cpu_kthread_task)) {
  19372. - rcu_wake_cond(__this_cpu_read(rcu_cpu_kthread_task),
  19373. - __this_cpu_read(rcu_cpu_kthread_status));
  19374. - }
  19375. - local_irq_restore(flags);
  19376. -}
  19377. -
  19378. -/*
  19379. * Is the current CPU running the RCU-callbacks kthread?
  19380. * Caller must have preemption disabled.
  19381. */
  19382. @@ -1186,67 +1156,6 @@
  19383. return 0;
  19384. }
  19385. -static void rcu_kthread_do_work(void)
  19386. -{
  19387. - rcu_do_batch(&rcu_sched_state, this_cpu_ptr(&rcu_sched_data));
  19388. - rcu_do_batch(&rcu_bh_state, this_cpu_ptr(&rcu_bh_data));
  19389. - rcu_preempt_do_callbacks();
  19390. -}
  19391. -
  19392. -static void rcu_cpu_kthread_setup(unsigned int cpu)
  19393. -{
  19394. - struct sched_param sp;
  19395. -
  19396. - sp.sched_priority = kthread_prio;
  19397. - sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
  19398. -}
  19399. -
  19400. -static void rcu_cpu_kthread_park(unsigned int cpu)
  19401. -{
  19402. - per_cpu(rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
  19403. -}
  19404. -
  19405. -static int rcu_cpu_kthread_should_run(unsigned int cpu)
  19406. -{
  19407. - return __this_cpu_read(rcu_cpu_has_work);
  19408. -}
  19409. -
  19410. -/*
  19411. - * Per-CPU kernel thread that invokes RCU callbacks. This replaces the
  19412. - * RCU softirq used in flavors and configurations of RCU that do not
  19413. - * support RCU priority boosting.
  19414. - */
  19415. -static void rcu_cpu_kthread(unsigned int cpu)
  19416. -{
  19417. - unsigned int *statusp = this_cpu_ptr(&rcu_cpu_kthread_status);
  19418. - char work, *workp = this_cpu_ptr(&rcu_cpu_has_work);
  19419. - int spincnt;
  19420. -
  19421. - for (spincnt = 0; spincnt < 10; spincnt++) {
  19422. - trace_rcu_utilization(TPS("Start CPU kthread@rcu_wait"));
  19423. - local_bh_disable();
  19424. - *statusp = RCU_KTHREAD_RUNNING;
  19425. - this_cpu_inc(rcu_cpu_kthread_loops);
  19426. - local_irq_disable();
  19427. - work = *workp;
  19428. - *workp = 0;
  19429. - local_irq_enable();
  19430. - if (work)
  19431. - rcu_kthread_do_work();
  19432. - local_bh_enable();
  19433. - if (*workp == 0) {
  19434. - trace_rcu_utilization(TPS("End CPU kthread@rcu_wait"));
  19435. - *statusp = RCU_KTHREAD_WAITING;
  19436. - return;
  19437. - }
  19438. - }
  19439. - *statusp = RCU_KTHREAD_YIELDING;
  19440. - trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield"));
  19441. - schedule_timeout_interruptible(2);
  19442. - trace_rcu_utilization(TPS("End CPU kthread@rcu_yield"));
  19443. - *statusp = RCU_KTHREAD_WAITING;
  19444. -}
  19445. -
  19446. /*
  19447. * Set the per-rcu_node kthread's affinity to cover all CPUs that are
  19448. * served by the rcu_node in question. The CPU hotplug lock is still
  19449. @@ -1276,26 +1185,12 @@
  19450. free_cpumask_var(cm);
  19451. }
  19452. -static struct smp_hotplug_thread rcu_cpu_thread_spec = {
  19453. - .store = &rcu_cpu_kthread_task,
  19454. - .thread_should_run = rcu_cpu_kthread_should_run,
  19455. - .thread_fn = rcu_cpu_kthread,
  19456. - .thread_comm = "rcuc/%u",
  19457. - .setup = rcu_cpu_kthread_setup,
  19458. - .park = rcu_cpu_kthread_park,
  19459. -};
  19460. -
  19461. /*
  19462. * Spawn boost kthreads -- called as soon as the scheduler is running.
  19463. */
  19464. static void __init rcu_spawn_boost_kthreads(void)
  19465. {
  19466. struct rcu_node *rnp;
  19467. - int cpu;
  19468. -
  19469. - for_each_possible_cpu(cpu)
  19470. - per_cpu(rcu_cpu_has_work, cpu) = 0;
  19471. - BUG_ON(smpboot_register_percpu_thread(&rcu_cpu_thread_spec));
  19472. rcu_for_each_leaf_node(rcu_state_p, rnp)
  19473. (void)rcu_spawn_one_boost_kthread(rcu_state_p, rnp);
  19474. }
  19475. @@ -1318,11 +1213,6 @@
  19476. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  19477. }
  19478. -static void invoke_rcu_callbacks_kthread(void)
  19479. -{
  19480. - WARN_ON_ONCE(1);
  19481. -}
  19482. -
  19483. static bool rcu_is_callbacks_kthread(void)
  19484. {
  19485. return false;
  19486. @@ -1346,7 +1236,7 @@
  19487. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  19488. -#if !defined(CONFIG_RCU_FAST_NO_HZ)
  19489. +#if !defined(CONFIG_RCU_FAST_NO_HZ) || defined(CONFIG_PREEMPT_RT_FULL)
  19490. /*
  19491. * Check to see if any future RCU-related work will need to be done
  19492. @@ -1363,7 +1253,9 @@
  19493. return IS_ENABLED(CONFIG_RCU_NOCB_CPU_ALL)
  19494. ? 0 : rcu_cpu_has_callbacks(NULL);
  19495. }
  19496. +#endif /* !defined(CONFIG_RCU_FAST_NO_HZ) || defined(CONFIG_PREEMPT_RT_FULL) */
  19497. +#if !defined(CONFIG_RCU_FAST_NO_HZ)
  19498. /*
  19499. * Because we do not have RCU_FAST_NO_HZ, don't bother cleaning up
  19500. * after it.
  19501. @@ -1459,6 +1351,8 @@
  19502. return cbs_ready;
  19503. }
  19504. +#ifndef CONFIG_PREEMPT_RT_FULL
  19505. +
  19506. /*
  19507. * Allow the CPU to enter dyntick-idle mode unless it has callbacks ready
  19508. * to invoke. If the CPU has callbacks, try to advance them. Tell the
  19509. @@ -1504,6 +1398,7 @@
  19510. *nextevt = basemono + dj * TICK_NSEC;
  19511. return 0;
  19512. }
  19513. +#endif /* #ifndef CONFIG_PREEMPT_RT_FULL */
  19514. /*
  19515. * Prepare a CPU for idle from an RCU perspective. The first major task
  19516. @@ -1822,9 +1717,9 @@
  19517. * Wake up any no-CBs CPUs' kthreads that were waiting on the just-ended
  19518. * grace period.
  19519. */
  19520. -static void rcu_nocb_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp)
  19521. +static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq)
  19522. {
  19523. - wake_up_all(&rnp->nocb_gp_wq[rnp->completed & 0x1]);
  19524. + swake_up_all(sq);
  19525. }
  19526. /*
  19527. @@ -1840,10 +1735,15 @@
  19528. rnp->need_future_gp[(rnp->completed + 1) & 0x1] += nrq;
  19529. }
  19530. +static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp)
  19531. +{
  19532. + return &rnp->nocb_gp_wq[rnp->completed & 0x1];
  19533. +}
  19534. +
  19535. static void rcu_init_one_nocb(struct rcu_node *rnp)
  19536. {
  19537. - init_waitqueue_head(&rnp->nocb_gp_wq[0]);
  19538. - init_waitqueue_head(&rnp->nocb_gp_wq[1]);
  19539. + init_swait_queue_head(&rnp->nocb_gp_wq[0]);
  19540. + init_swait_queue_head(&rnp->nocb_gp_wq[1]);
  19541. }
  19542. #ifndef CONFIG_RCU_NOCB_CPU_ALL
  19543. @@ -1868,7 +1768,7 @@
  19544. if (READ_ONCE(rdp_leader->nocb_leader_sleep) || force) {
  19545. /* Prior smp_mb__after_atomic() orders against prior enqueue. */
  19546. WRITE_ONCE(rdp_leader->nocb_leader_sleep, false);
  19547. - wake_up(&rdp_leader->nocb_wq);
  19548. + swake_up(&rdp_leader->nocb_wq);
  19549. }
  19550. }
  19551. @@ -2081,7 +1981,7 @@
  19552. */
  19553. trace_rcu_future_gp(rnp, rdp, c, TPS("StartWait"));
  19554. for (;;) {
  19555. - wait_event_interruptible(
  19556. + swait_event_interruptible(
  19557. rnp->nocb_gp_wq[c & 0x1],
  19558. (d = ULONG_CMP_GE(READ_ONCE(rnp->completed), c)));
  19559. if (likely(d))
  19560. @@ -2109,7 +2009,7 @@
  19561. /* Wait for callbacks to appear. */
  19562. if (!rcu_nocb_poll) {
  19563. trace_rcu_nocb_wake(my_rdp->rsp->name, my_rdp->cpu, "Sleep");
  19564. - wait_event_interruptible(my_rdp->nocb_wq,
  19565. + swait_event_interruptible(my_rdp->nocb_wq,
  19566. !READ_ONCE(my_rdp->nocb_leader_sleep));
  19567. /* Memory barrier handled by smp_mb() calls below and repoll. */
  19568. } else if (firsttime) {
  19569. @@ -2184,7 +2084,7 @@
  19570. * List was empty, wake up the follower.
  19571. * Memory barriers supplied by atomic_long_add().
  19572. */
  19573. - wake_up(&rdp->nocb_wq);
  19574. + swake_up(&rdp->nocb_wq);
  19575. }
  19576. }
  19577. @@ -2205,7 +2105,7 @@
  19578. if (!rcu_nocb_poll) {
  19579. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  19580. "FollowerSleep");
  19581. - wait_event_interruptible(rdp->nocb_wq,
  19582. + swait_event_interruptible(rdp->nocb_wq,
  19583. READ_ONCE(rdp->nocb_follower_head));
  19584. } else if (firsttime) {
  19585. /* Don't drown trace log with "Poll"! */
  19586. @@ -2364,7 +2264,7 @@
  19587. static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
  19588. {
  19589. rdp->nocb_tail = &rdp->nocb_head;
  19590. - init_waitqueue_head(&rdp->nocb_wq);
  19591. + init_swait_queue_head(&rdp->nocb_wq);
  19592. rdp->nocb_follower_tail = &rdp->nocb_follower_head;
  19593. }
  19594. @@ -2514,7 +2414,7 @@
  19595. return false;
  19596. }
  19597. -static void rcu_nocb_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp)
  19598. +static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq)
  19599. {
  19600. }
  19601. @@ -2522,6 +2422,11 @@
  19602. {
  19603. }
  19604. +static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp)
  19605. +{
  19606. + return NULL;
  19607. +}
  19608. +
  19609. static void rcu_init_one_nocb(struct rcu_node *rnp)
  19610. {
  19611. }
  19612. diff -Nur linux-4.4.28.orig/kernel/rcu/update.c linux-4.4.28/kernel/rcu/update.c
  19613. --- linux-4.4.28.orig/kernel/rcu/update.c 2016-10-28 09:53:25.000000000 +0200
  19614. +++ linux-4.4.28/kernel/rcu/update.c 2016-11-01 21:26:58.780342543 +0100
  19615. @@ -276,6 +276,7 @@
  19616. }
  19617. EXPORT_SYMBOL_GPL(rcu_read_lock_held);
  19618. +#ifndef CONFIG_PREEMPT_RT_FULL
  19619. /**
  19620. * rcu_read_lock_bh_held() - might we be in RCU-bh read-side critical section?
  19621. *
  19622. @@ -302,6 +303,7 @@
  19623. return in_softirq() || irqs_disabled();
  19624. }
  19625. EXPORT_SYMBOL_GPL(rcu_read_lock_bh_held);
  19626. +#endif
  19627. #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  19628. diff -Nur linux-4.4.28.orig/kernel/relay.c linux-4.4.28/kernel/relay.c
  19629. --- linux-4.4.28.orig/kernel/relay.c 2016-10-28 09:53:25.000000000 +0200
  19630. +++ linux-4.4.28/kernel/relay.c 2016-11-01 21:26:58.780342543 +0100
  19631. @@ -336,6 +336,10 @@
  19632. {
  19633. struct rchan_buf *buf = (struct rchan_buf *)data;
  19634. wake_up_interruptible(&buf->read_wait);
  19635. + /*
  19636. + * Stupid polling for now:
  19637. + */
  19638. + mod_timer(&buf->timer, jiffies + 1);
  19639. }
  19640. /**
  19641. @@ -353,6 +357,7 @@
  19642. init_waitqueue_head(&buf->read_wait);
  19643. kref_init(&buf->kref);
  19644. setup_timer(&buf->timer, wakeup_readers, (unsigned long)buf);
  19645. + mod_timer(&buf->timer, jiffies + 1);
  19646. } else
  19647. del_timer_sync(&buf->timer);
  19648. @@ -736,15 +741,6 @@
  19649. else
  19650. buf->early_bytes += buf->chan->subbuf_size -
  19651. buf->padding[old_subbuf];
  19652. - smp_mb();
  19653. - if (waitqueue_active(&buf->read_wait))
  19654. - /*
  19655. - * Calling wake_up_interruptible() from here
  19656. - * will deadlock if we happen to be logging
  19657. - * from the scheduler (trying to re-grab
  19658. - * rq->lock), so defer it.
  19659. - */
  19660. - mod_timer(&buf->timer, jiffies + 1);
  19661. }
  19662. old = buf->data;
  19663. diff -Nur linux-4.4.28.orig/kernel/sched/completion.c linux-4.4.28/kernel/sched/completion.c
  19664. --- linux-4.4.28.orig/kernel/sched/completion.c 2016-10-28 09:53:25.000000000 +0200
  19665. +++ linux-4.4.28/kernel/sched/completion.c 2016-11-01 21:26:58.780342543 +0100
  19666. @@ -30,10 +30,10 @@
  19667. {
  19668. unsigned long flags;
  19669. - spin_lock_irqsave(&x->wait.lock, flags);
  19670. + raw_spin_lock_irqsave(&x->wait.lock, flags);
  19671. x->done++;
  19672. - __wake_up_locked(&x->wait, TASK_NORMAL, 1);
  19673. - spin_unlock_irqrestore(&x->wait.lock, flags);
  19674. + swake_up_locked(&x->wait);
  19675. + raw_spin_unlock_irqrestore(&x->wait.lock, flags);
  19676. }
  19677. EXPORT_SYMBOL(complete);
  19678. @@ -50,10 +50,10 @@
  19679. {
  19680. unsigned long flags;
  19681. - spin_lock_irqsave(&x->wait.lock, flags);
  19682. + raw_spin_lock_irqsave(&x->wait.lock, flags);
  19683. x->done += UINT_MAX/2;
  19684. - __wake_up_locked(&x->wait, TASK_NORMAL, 0);
  19685. - spin_unlock_irqrestore(&x->wait.lock, flags);
  19686. + swake_up_all_locked(&x->wait);
  19687. + raw_spin_unlock_irqrestore(&x->wait.lock, flags);
  19688. }
  19689. EXPORT_SYMBOL(complete_all);
  19690. @@ -62,20 +62,20 @@
  19691. long (*action)(long), long timeout, int state)
  19692. {
  19693. if (!x->done) {
  19694. - DECLARE_WAITQUEUE(wait, current);
  19695. + DECLARE_SWAITQUEUE(wait);
  19696. - __add_wait_queue_tail_exclusive(&x->wait, &wait);
  19697. + __prepare_to_swait(&x->wait, &wait);
  19698. do {
  19699. if (signal_pending_state(state, current)) {
  19700. timeout = -ERESTARTSYS;
  19701. break;
  19702. }
  19703. __set_current_state(state);
  19704. - spin_unlock_irq(&x->wait.lock);
  19705. + raw_spin_unlock_irq(&x->wait.lock);
  19706. timeout = action(timeout);
  19707. - spin_lock_irq(&x->wait.lock);
  19708. + raw_spin_lock_irq(&x->wait.lock);
  19709. } while (!x->done && timeout);
  19710. - __remove_wait_queue(&x->wait, &wait);
  19711. + __finish_swait(&x->wait, &wait);
  19712. if (!x->done)
  19713. return timeout;
  19714. }
  19715. @@ -89,9 +89,9 @@
  19716. {
  19717. might_sleep();
  19718. - spin_lock_irq(&x->wait.lock);
  19719. + raw_spin_lock_irq(&x->wait.lock);
  19720. timeout = do_wait_for_common(x, action, timeout, state);
  19721. - spin_unlock_irq(&x->wait.lock);
  19722. + raw_spin_unlock_irq(&x->wait.lock);
  19723. return timeout;
  19724. }
  19725. @@ -277,12 +277,12 @@
  19726. if (!READ_ONCE(x->done))
  19727. return 0;
  19728. - spin_lock_irqsave(&x->wait.lock, flags);
  19729. + raw_spin_lock_irqsave(&x->wait.lock, flags);
  19730. if (!x->done)
  19731. ret = 0;
  19732. else
  19733. x->done--;
  19734. - spin_unlock_irqrestore(&x->wait.lock, flags);
  19735. + raw_spin_unlock_irqrestore(&x->wait.lock, flags);
  19736. return ret;
  19737. }
  19738. EXPORT_SYMBOL(try_wait_for_completion);
  19739. @@ -311,7 +311,7 @@
  19740. * after it's acquired the lock.
  19741. */
  19742. smp_rmb();
  19743. - spin_unlock_wait(&x->wait.lock);
  19744. + raw_spin_unlock_wait(&x->wait.lock);
  19745. return true;
  19746. }
  19747. EXPORT_SYMBOL(completion_done);
  19748. diff -Nur linux-4.4.28.orig/kernel/sched/core.c linux-4.4.28/kernel/sched/core.c
  19749. --- linux-4.4.28.orig/kernel/sched/core.c 2016-10-28 09:53:25.000000000 +0200
  19750. +++ linux-4.4.28/kernel/sched/core.c 2016-11-01 21:26:58.784342796 +0100
  19751. @@ -260,7 +260,11 @@
  19752. * Number of tasks to iterate in a single balance run.
  19753. * Limited because this is done with IRQs disabled.
  19754. */
  19755. +#ifndef CONFIG_PREEMPT_RT_FULL
  19756. const_debug unsigned int sysctl_sched_nr_migrate = 32;
  19757. +#else
  19758. +const_debug unsigned int sysctl_sched_nr_migrate = 8;
  19759. +#endif
  19760. /*
  19761. * period over which we average the RT time consumption, measured
  19762. @@ -438,6 +442,7 @@
  19763. hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  19764. rq->hrtick_timer.function = hrtick;
  19765. + rq->hrtick_timer.irqsafe = 1;
  19766. }
  19767. #else /* CONFIG_SCHED_HRTICK */
  19768. static inline void hrtick_clear(struct rq *rq)
  19769. @@ -542,7 +547,7 @@
  19770. head->lastp = &node->next;
  19771. }
  19772. -void wake_up_q(struct wake_q_head *head)
  19773. +void __wake_up_q(struct wake_q_head *head, bool sleeper)
  19774. {
  19775. struct wake_q_node *node = head->first;
  19776. @@ -559,7 +564,10 @@
  19777. * wake_up_process() implies a wmb() to pair with the queueing
  19778. * in wake_q_add() so as not to miss wakeups.
  19779. */
  19780. - wake_up_process(task);
  19781. + if (sleeper)
  19782. + wake_up_lock_sleeper(task);
  19783. + else
  19784. + wake_up_process(task);
  19785. put_task_struct(task);
  19786. }
  19787. }
  19788. @@ -595,6 +603,38 @@
  19789. trace_sched_wake_idle_without_ipi(cpu);
  19790. }
  19791. +#ifdef CONFIG_PREEMPT_LAZY
  19792. +void resched_curr_lazy(struct rq *rq)
  19793. +{
  19794. + struct task_struct *curr = rq->curr;
  19795. + int cpu;
  19796. +
  19797. + if (!sched_feat(PREEMPT_LAZY)) {
  19798. + resched_curr(rq);
  19799. + return;
  19800. + }
  19801. +
  19802. + lockdep_assert_held(&rq->lock);
  19803. +
  19804. + if (test_tsk_need_resched(curr))
  19805. + return;
  19806. +
  19807. + if (test_tsk_need_resched_lazy(curr))
  19808. + return;
  19809. +
  19810. + set_tsk_need_resched_lazy(curr);
  19811. +
  19812. + cpu = cpu_of(rq);
  19813. + if (cpu == smp_processor_id())
  19814. + return;
  19815. +
  19816. + /* NEED_RESCHED_LAZY must be visible before we test polling */
  19817. + smp_mb();
  19818. + if (!tsk_is_polling(curr))
  19819. + smp_send_reschedule(cpu);
  19820. +}
  19821. +#endif
  19822. +
  19823. void resched_cpu(int cpu)
  19824. {
  19825. struct rq *rq = cpu_rq(cpu);
  19826. @@ -618,11 +658,14 @@
  19827. */
  19828. int get_nohz_timer_target(void)
  19829. {
  19830. - int i, cpu = smp_processor_id();
  19831. + int i, cpu;
  19832. struct sched_domain *sd;
  19833. + preempt_disable_rt();
  19834. + cpu = smp_processor_id();
  19835. +
  19836. if (!idle_cpu(cpu) && is_housekeeping_cpu(cpu))
  19837. - return cpu;
  19838. + goto preempt_en_rt;
  19839. rcu_read_lock();
  19840. for_each_domain(cpu, sd) {
  19841. @@ -641,6 +684,8 @@
  19842. cpu = housekeeping_any_cpu();
  19843. unlock:
  19844. rcu_read_unlock();
  19845. +preempt_en_rt:
  19846. + preempt_enable_rt();
  19847. return cpu;
  19848. }
  19849. /*
  19850. @@ -1174,6 +1219,11 @@
  19851. lockdep_assert_held(&p->pi_lock);
  19852. + if (__migrate_disabled(p)) {
  19853. + cpumask_copy(&p->cpus_allowed, new_mask);
  19854. + return;
  19855. + }
  19856. +
  19857. queued = task_on_rq_queued(p);
  19858. running = task_current(rq, p);
  19859. @@ -1196,6 +1246,84 @@
  19860. enqueue_task(rq, p, ENQUEUE_RESTORE);
  19861. }
  19862. +static DEFINE_PER_CPU(struct cpumask, sched_cpumasks);
  19863. +static DEFINE_MUTEX(sched_down_mutex);
  19864. +static cpumask_t sched_down_cpumask;
  19865. +
  19866. +void tell_sched_cpu_down_begin(int cpu)
  19867. +{
  19868. + mutex_lock(&sched_down_mutex);
  19869. + cpumask_set_cpu(cpu, &sched_down_cpumask);
  19870. + mutex_unlock(&sched_down_mutex);
  19871. +}
  19872. +
  19873. +void tell_sched_cpu_down_done(int cpu)
  19874. +{
  19875. + mutex_lock(&sched_down_mutex);
  19876. + cpumask_clear_cpu(cpu, &sched_down_cpumask);
  19877. + mutex_unlock(&sched_down_mutex);
  19878. +}
  19879. +
  19880. +/**
  19881. + * migrate_me - try to move the current task off this cpu
  19882. + *
  19883. + * Used by the pin_current_cpu() code to try to get tasks
  19884. + * to move off the current CPU as it is going down.
  19885. + * It will only move the task if the task isn't pinned to
  19886. + * the CPU (with migrate_disable, affinity or NO_SETAFFINITY)
  19887. + * and the task has to be in a RUNNING state. Otherwise the
  19888. + * movement of the task will wake it up (change its state
  19889. + * to running) when the task did not expect it.
  19890. + *
  19891. + * Returns 1 if it succeeded in moving the current task
  19892. + * 0 otherwise.
  19893. + */
  19894. +int migrate_me(void)
  19895. +{
  19896. + struct task_struct *p = current;
  19897. + struct migration_arg arg;
  19898. + struct cpumask *cpumask;
  19899. + struct cpumask *mask;
  19900. + unsigned long flags;
  19901. + unsigned int dest_cpu;
  19902. + struct rq *rq;
  19903. +
  19904. + /*
  19905. + * We can not migrate tasks bounded to a CPU or tasks not
  19906. + * running. The movement of the task will wake it up.
  19907. + */
  19908. + if (p->flags & PF_NO_SETAFFINITY || p->state)
  19909. + return 0;
  19910. +
  19911. + mutex_lock(&sched_down_mutex);
  19912. + rq = task_rq_lock(p, &flags);
  19913. +
  19914. + cpumask = this_cpu_ptr(&sched_cpumasks);
  19915. + mask = &p->cpus_allowed;
  19916. +
  19917. + cpumask_andnot(cpumask, mask, &sched_down_cpumask);
  19918. +
  19919. + if (!cpumask_weight(cpumask)) {
  19920. + /* It's only on this CPU? */
  19921. + task_rq_unlock(rq, p, &flags);
  19922. + mutex_unlock(&sched_down_mutex);
  19923. + return 0;
  19924. + }
  19925. +
  19926. + dest_cpu = cpumask_any_and(cpu_active_mask, cpumask);
  19927. +
  19928. + arg.task = p;
  19929. + arg.dest_cpu = dest_cpu;
  19930. +
  19931. + task_rq_unlock(rq, p, &flags);
  19932. +
  19933. + stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
  19934. + tlb_migrate_finish(p->mm);
  19935. + mutex_unlock(&sched_down_mutex);
  19936. +
  19937. + return 1;
  19938. +}
  19939. +
  19940. /*
  19941. * Change a given task's CPU affinity. Migrate the thread to a
  19942. * proper CPU and schedule it away if the CPU it's executing on
  19943. @@ -1235,7 +1363,7 @@
  19944. do_set_cpus_allowed(p, new_mask);
  19945. /* Can the task run on the task's current CPU? If so, we're done */
  19946. - if (cpumask_test_cpu(task_cpu(p), new_mask))
  19947. + if (cpumask_test_cpu(task_cpu(p), new_mask) || __migrate_disabled(p))
  19948. goto out;
  19949. dest_cpu = cpumask_any_and(cpu_active_mask, new_mask);
  19950. @@ -1411,6 +1539,18 @@
  19951. return ret;
  19952. }
  19953. +static bool check_task_state(struct task_struct *p, long match_state)
  19954. +{
  19955. + bool match = false;
  19956. +
  19957. + raw_spin_lock_irq(&p->pi_lock);
  19958. + if (p->state == match_state || p->saved_state == match_state)
  19959. + match = true;
  19960. + raw_spin_unlock_irq(&p->pi_lock);
  19961. +
  19962. + return match;
  19963. +}
  19964. +
  19965. /*
  19966. * wait_task_inactive - wait for a thread to unschedule.
  19967. *
  19968. @@ -1455,7 +1595,7 @@
  19969. * is actually now running somewhere else!
  19970. */
  19971. while (task_running(rq, p)) {
  19972. - if (match_state && unlikely(p->state != match_state))
  19973. + if (match_state && !check_task_state(p, match_state))
  19974. return 0;
  19975. cpu_relax();
  19976. }
  19977. @@ -1470,7 +1610,8 @@
  19978. running = task_running(rq, p);
  19979. queued = task_on_rq_queued(p);
  19980. ncsw = 0;
  19981. - if (!match_state || p->state == match_state)
  19982. + if (!match_state || p->state == match_state ||
  19983. + p->saved_state == match_state)
  19984. ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
  19985. task_rq_unlock(rq, p, &flags);
  19986. @@ -1627,7 +1768,7 @@
  19987. {
  19988. lockdep_assert_held(&p->pi_lock);
  19989. - if (p->nr_cpus_allowed > 1)
  19990. + if (tsk_nr_cpus_allowed(p) > 1)
  19991. cpu = p->sched_class->select_task_rq(p, cpu, sd_flags, wake_flags);
  19992. /*
  19993. @@ -1707,10 +1848,6 @@
  19994. {
  19995. activate_task(rq, p, en_flags);
  19996. p->on_rq = TASK_ON_RQ_QUEUED;
  19997. -
  19998. - /* if a worker is waking up, notify workqueue */
  19999. - if (p->flags & PF_WQ_WORKER)
  20000. - wq_worker_waking_up(p, cpu_of(rq));
  20001. }
  20002. /*
  20003. @@ -1937,8 +2074,27 @@
  20004. */
  20005. smp_mb__before_spinlock();
  20006. raw_spin_lock_irqsave(&p->pi_lock, flags);
  20007. - if (!(p->state & state))
  20008. + if (!(p->state & state)) {
  20009. + /*
  20010. + * The task might be running due to a spinlock sleeper
  20011. + * wakeup. Check the saved state and set it to running
  20012. + * if the wakeup condition is true.
  20013. + */
  20014. + if (!(wake_flags & WF_LOCK_SLEEPER)) {
  20015. + if (p->saved_state & state) {
  20016. + p->saved_state = TASK_RUNNING;
  20017. + success = 1;
  20018. + }
  20019. + }
  20020. goto out;
  20021. + }
  20022. +
  20023. + /*
  20024. + * If this is a regular wakeup, then we can unconditionally
  20025. + * clear the saved state of a "lock sleeper".
  20026. + */
  20027. + if (!(wake_flags & WF_LOCK_SLEEPER))
  20028. + p->saved_state = TASK_RUNNING;
  20029. trace_sched_waking(p);
  20030. @@ -2030,52 +2186,6 @@
  20031. }
  20032. /**
  20033. - * try_to_wake_up_local - try to wake up a local task with rq lock held
  20034. - * @p: the thread to be awakened
  20035. - *
  20036. - * Put @p on the run-queue if it's not already there. The caller must
  20037. - * ensure that this_rq() is locked, @p is bound to this_rq() and not
  20038. - * the current task.
  20039. - */
  20040. -static void try_to_wake_up_local(struct task_struct *p)
  20041. -{
  20042. - struct rq *rq = task_rq(p);
  20043. -
  20044. - if (WARN_ON_ONCE(rq != this_rq()) ||
  20045. - WARN_ON_ONCE(p == current))
  20046. - return;
  20047. -
  20048. - lockdep_assert_held(&rq->lock);
  20049. -
  20050. - if (!raw_spin_trylock(&p->pi_lock)) {
  20051. - /*
  20052. - * This is OK, because current is on_cpu, which avoids it being
  20053. - * picked for load-balance and preemption/IRQs are still
  20054. - * disabled avoiding further scheduler activity on it and we've
  20055. - * not yet picked a replacement task.
  20056. - */
  20057. - lockdep_unpin_lock(&rq->lock);
  20058. - raw_spin_unlock(&rq->lock);
  20059. - raw_spin_lock(&p->pi_lock);
  20060. - raw_spin_lock(&rq->lock);
  20061. - lockdep_pin_lock(&rq->lock);
  20062. - }
  20063. -
  20064. - if (!(p->state & TASK_NORMAL))
  20065. - goto out;
  20066. -
  20067. - trace_sched_waking(p);
  20068. -
  20069. - if (!task_on_rq_queued(p))
  20070. - ttwu_activate(rq, p, ENQUEUE_WAKEUP);
  20071. -
  20072. - ttwu_do_wakeup(rq, p, 0);
  20073. - ttwu_stat(p, smp_processor_id(), 0);
  20074. -out:
  20075. - raw_spin_unlock(&p->pi_lock);
  20076. -}
  20077. -
  20078. -/**
  20079. * wake_up_process - Wake up a specific process
  20080. * @p: The process to be woken up.
  20081. *
  20082. @@ -2093,6 +2203,18 @@
  20083. }
  20084. EXPORT_SYMBOL(wake_up_process);
  20085. +/**
  20086. + * wake_up_lock_sleeper - Wake up a specific process blocked on a "sleeping lock"
  20087. + * @p: The process to be woken up.
  20088. + *
  20089. + * Same as wake_up_process() above, but wake_flags=WF_LOCK_SLEEPER to indicate
  20090. + * the nature of the wakeup.
  20091. + */
  20092. +int wake_up_lock_sleeper(struct task_struct *p)
  20093. +{
  20094. + return try_to_wake_up(p, TASK_ALL, WF_LOCK_SLEEPER);
  20095. +}
  20096. +
  20097. int wake_up_state(struct task_struct *p, unsigned int state)
  20098. {
  20099. return try_to_wake_up(p, state, 0);
  20100. @@ -2279,6 +2401,9 @@
  20101. p->on_cpu = 0;
  20102. #endif
  20103. init_task_preempt_count(p);
  20104. +#ifdef CONFIG_HAVE_PREEMPT_LAZY
  20105. + task_thread_info(p)->preempt_lazy_count = 0;
  20106. +#endif
  20107. #ifdef CONFIG_SMP
  20108. plist_node_init(&p->pushable_tasks, MAX_PRIO);
  20109. RB_CLEAR_NODE(&p->pushable_dl_tasks);
  20110. @@ -2603,8 +2728,12 @@
  20111. finish_arch_post_lock_switch();
  20112. fire_sched_in_preempt_notifiers(current);
  20113. + /*
  20114. + * We use mmdrop_delayed() here so we don't have to do the
  20115. + * full __mmdrop() when we are the last user.
  20116. + */
  20117. if (mm)
  20118. - mmdrop(mm);
  20119. + mmdrop_delayed(mm);
  20120. if (unlikely(prev_state == TASK_DEAD)) {
  20121. if (prev->sched_class->task_dead)
  20122. prev->sched_class->task_dead(prev);
  20123. @@ -2935,16 +3064,6 @@
  20124. }
  20125. #endif
  20126. -notrace unsigned long get_parent_ip(unsigned long addr)
  20127. -{
  20128. - if (in_lock_functions(addr)) {
  20129. - addr = CALLER_ADDR2;
  20130. - if (in_lock_functions(addr))
  20131. - addr = CALLER_ADDR3;
  20132. - }
  20133. - return addr;
  20134. -}
  20135. -
  20136. #if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
  20137. defined(CONFIG_PREEMPT_TRACER))
  20138. @@ -2966,7 +3085,7 @@
  20139. PREEMPT_MASK - 10);
  20140. #endif
  20141. if (preempt_count() == val) {
  20142. - unsigned long ip = get_parent_ip(CALLER_ADDR1);
  20143. + unsigned long ip = get_lock_parent_ip();
  20144. #ifdef CONFIG_DEBUG_PREEMPT
  20145. current->preempt_disable_ip = ip;
  20146. #endif
  20147. @@ -2993,7 +3112,7 @@
  20148. #endif
  20149. if (preempt_count() == val)
  20150. - trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
  20151. + trace_preempt_on(CALLER_ADDR0, get_lock_parent_ip());
  20152. __preempt_count_sub(val);
  20153. }
  20154. EXPORT_SYMBOL(preempt_count_sub);
  20155. @@ -3048,6 +3167,77 @@
  20156. schedstat_inc(this_rq(), sched_count);
  20157. }
  20158. +#if defined(CONFIG_PREEMPT_RT_FULL) && defined(CONFIG_SMP)
  20159. +
  20160. +void migrate_disable(void)
  20161. +{
  20162. + struct task_struct *p = current;
  20163. +
  20164. + if (in_atomic() || irqs_disabled()) {
  20165. +#ifdef CONFIG_SCHED_DEBUG
  20166. + p->migrate_disable_atomic++;
  20167. +#endif
  20168. + return;
  20169. + }
  20170. +
  20171. +#ifdef CONFIG_SCHED_DEBUG
  20172. + if (unlikely(p->migrate_disable_atomic)) {
  20173. + tracing_off();
  20174. + WARN_ON_ONCE(1);
  20175. + }
  20176. +#endif
  20177. +
  20178. + if (p->migrate_disable) {
  20179. + p->migrate_disable++;
  20180. + return;
  20181. + }
  20182. +
  20183. + preempt_disable();
  20184. + preempt_lazy_disable();
  20185. + pin_current_cpu();
  20186. + p->migrate_disable = 1;
  20187. + preempt_enable();
  20188. +}
  20189. +EXPORT_SYMBOL(migrate_disable);
  20190. +
  20191. +void migrate_enable(void)
  20192. +{
  20193. + struct task_struct *p = current;
  20194. +
  20195. + if (in_atomic() || irqs_disabled()) {
  20196. +#ifdef CONFIG_SCHED_DEBUG
  20197. + p->migrate_disable_atomic--;
  20198. +#endif
  20199. + return;
  20200. + }
  20201. +
  20202. +#ifdef CONFIG_SCHED_DEBUG
  20203. + if (unlikely(p->migrate_disable_atomic)) {
  20204. + tracing_off();
  20205. + WARN_ON_ONCE(1);
  20206. + }
  20207. +#endif
  20208. + WARN_ON_ONCE(p->migrate_disable <= 0);
  20209. +
  20210. + if (p->migrate_disable > 1) {
  20211. + p->migrate_disable--;
  20212. + return;
  20213. + }
  20214. +
  20215. + preempt_disable();
  20216. + /*
  20217. + * Clearing migrate_disable causes tsk_cpus_allowed to
  20218. + * show the tasks original cpu affinity.
  20219. + */
  20220. + p->migrate_disable = 0;
  20221. +
  20222. + unpin_current_cpu();
  20223. + preempt_enable();
  20224. + preempt_lazy_enable();
  20225. +}
  20226. +EXPORT_SYMBOL(migrate_enable);
  20227. +#endif
  20228. +
  20229. /*
  20230. * Pick up the highest-prio task:
  20231. */
  20232. @@ -3172,19 +3362,6 @@
  20233. } else {
  20234. deactivate_task(rq, prev, DEQUEUE_SLEEP);
  20235. prev->on_rq = 0;
  20236. -
  20237. - /*
  20238. - * If a worker went to sleep, notify and ask workqueue
  20239. - * whether it wants to wake up a task to maintain
  20240. - * concurrency.
  20241. - */
  20242. - if (prev->flags & PF_WQ_WORKER) {
  20243. - struct task_struct *to_wakeup;
  20244. -
  20245. - to_wakeup = wq_worker_sleeping(prev, cpu);
  20246. - if (to_wakeup)
  20247. - try_to_wake_up_local(to_wakeup);
  20248. - }
  20249. }
  20250. switch_count = &prev->nvcsw;
  20251. }
  20252. @@ -3194,6 +3371,7 @@
  20253. next = pick_next_task(rq, prev);
  20254. clear_tsk_need_resched(prev);
  20255. + clear_tsk_need_resched_lazy(prev);
  20256. clear_preempt_need_resched();
  20257. rq->clock_skip_update = 0;
  20258. @@ -3215,9 +3393,20 @@
  20259. static inline void sched_submit_work(struct task_struct *tsk)
  20260. {
  20261. - if (!tsk->state || tsk_is_pi_blocked(tsk))
  20262. + if (!tsk->state)
  20263. return;
  20264. /*
  20265. + * If a worker went to sleep, notify and ask workqueue whether
  20266. + * it wants to wake up a task to maintain concurrency.
  20267. + */
  20268. + if (tsk->flags & PF_WQ_WORKER)
  20269. + wq_worker_sleeping(tsk);
  20270. +
  20271. +
  20272. + if (tsk_is_pi_blocked(tsk))
  20273. + return;
  20274. +
  20275. + /*
  20276. * If we are going to sleep and we have plugged IO queued,
  20277. * make sure to submit it to avoid deadlocks.
  20278. */
  20279. @@ -3225,6 +3414,12 @@
  20280. blk_schedule_flush_plug(tsk);
  20281. }
  20282. +static void sched_update_worker(struct task_struct *tsk)
  20283. +{
  20284. + if (tsk->flags & PF_WQ_WORKER)
  20285. + wq_worker_running(tsk);
  20286. +}
  20287. +
  20288. asmlinkage __visible void __sched schedule(void)
  20289. {
  20290. struct task_struct *tsk = current;
  20291. @@ -3235,6 +3430,7 @@
  20292. __schedule(false);
  20293. sched_preempt_enable_no_resched();
  20294. } while (need_resched());
  20295. + sched_update_worker(tsk);
  20296. }
  20297. EXPORT_SYMBOL(schedule);
  20298. @@ -3283,6 +3479,30 @@
  20299. } while (need_resched());
  20300. }
  20301. +#ifdef CONFIG_PREEMPT_LAZY
  20302. +/*
  20303. + * If TIF_NEED_RESCHED is then we allow to be scheduled away since this is
  20304. + * set by a RT task. Oterwise we try to avoid beeing scheduled out as long as
  20305. + * preempt_lazy_count counter >0.
  20306. + */
  20307. +static __always_inline int preemptible_lazy(void)
  20308. +{
  20309. + if (test_thread_flag(TIF_NEED_RESCHED))
  20310. + return 1;
  20311. + if (current_thread_info()->preempt_lazy_count)
  20312. + return 0;
  20313. + return 1;
  20314. +}
  20315. +
  20316. +#else
  20317. +
  20318. +static inline int preemptible_lazy(void)
  20319. +{
  20320. + return 1;
  20321. +}
  20322. +
  20323. +#endif
  20324. +
  20325. #ifdef CONFIG_PREEMPT
  20326. /*
  20327. * this is the entry point to schedule() from in-kernel preemption
  20328. @@ -3297,6 +3517,8 @@
  20329. */
  20330. if (likely(!preemptible()))
  20331. return;
  20332. + if (!preemptible_lazy())
  20333. + return;
  20334. preempt_schedule_common();
  20335. }
  20336. @@ -3323,6 +3545,8 @@
  20337. if (likely(!preemptible()))
  20338. return;
  20339. + if (!preemptible_lazy())
  20340. + return;
  20341. do {
  20342. preempt_disable_notrace();
  20343. @@ -3332,7 +3556,16 @@
  20344. * an infinite recursion.
  20345. */
  20346. prev_ctx = exception_enter();
  20347. + /*
  20348. + * The add/subtract must not be traced by the function
  20349. + * tracer. But we still want to account for the
  20350. + * preempt off latency tracer. Since the _notrace versions
  20351. + * of add/subtract skip the accounting for latency tracer
  20352. + * we must force it manually.
  20353. + */
  20354. + start_critical_timings();
  20355. __schedule(true);
  20356. + stop_critical_timings();
  20357. exception_exit(prev_ctx);
  20358. preempt_enable_no_resched_notrace();
  20359. @@ -4676,6 +4909,7 @@
  20360. }
  20361. EXPORT_SYMBOL(__cond_resched_lock);
  20362. +#ifndef CONFIG_PREEMPT_RT_FULL
  20363. int __sched __cond_resched_softirq(void)
  20364. {
  20365. BUG_ON(!in_softirq());
  20366. @@ -4689,6 +4923,7 @@
  20367. return 0;
  20368. }
  20369. EXPORT_SYMBOL(__cond_resched_softirq);
  20370. +#endif
  20371. /**
  20372. * yield - yield the current processor to other threads.
  20373. @@ -5055,7 +5290,9 @@
  20374. /* Set the preempt count _outside_ the spinlocks! */
  20375. init_idle_preempt_count(idle, cpu);
  20376. -
  20377. +#ifdef CONFIG_HAVE_PREEMPT_LAZY
  20378. + task_thread_info(idle)->preempt_lazy_count = 0;
  20379. +#endif
  20380. /*
  20381. * The idle tasks have their own, simple scheduling class:
  20382. */
  20383. @@ -5196,6 +5433,8 @@
  20384. #endif /* CONFIG_NUMA_BALANCING */
  20385. #ifdef CONFIG_HOTPLUG_CPU
  20386. +static DEFINE_PER_CPU(struct mm_struct *, idle_last_mm);
  20387. +
  20388. /*
  20389. * Ensures that the idle task is using init_mm right before its cpu goes
  20390. * offline.
  20391. @@ -5210,7 +5449,11 @@
  20392. switch_mm(mm, &init_mm, current);
  20393. finish_arch_post_lock_switch();
  20394. }
  20395. - mmdrop(mm);
  20396. + /*
  20397. + * Defer the cleanup to an alive cpu. On RT we can neither
  20398. + * call mmdrop() nor mmdrop_delayed() from here.
  20399. + */
  20400. + per_cpu(idle_last_mm, smp_processor_id()) = mm;
  20401. }
  20402. /*
  20403. @@ -5583,6 +5826,10 @@
  20404. case CPU_DEAD:
  20405. calc_load_migrate(rq);
  20406. + if (per_cpu(idle_last_mm, cpu)) {
  20407. + mmdrop(per_cpu(idle_last_mm, cpu));
  20408. + per_cpu(idle_last_mm, cpu) = NULL;
  20409. + }
  20410. break;
  20411. #endif
  20412. }
  20413. @@ -7566,7 +7813,7 @@
  20414. #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
  20415. static inline int preempt_count_equals(int preempt_offset)
  20416. {
  20417. - int nested = preempt_count() + rcu_preempt_depth();
  20418. + int nested = preempt_count() + sched_rcu_preempt_depth();
  20419. return (nested == preempt_offset);
  20420. }
  20421. diff -Nur linux-4.4.28.orig/kernel/sched/cpudeadline.c linux-4.4.28/kernel/sched/cpudeadline.c
  20422. --- linux-4.4.28.orig/kernel/sched/cpudeadline.c 2016-10-28 09:53:25.000000000 +0200
  20423. +++ linux-4.4.28/kernel/sched/cpudeadline.c 2016-11-01 21:26:58.784342796 +0100
  20424. @@ -103,10 +103,10 @@
  20425. const struct sched_dl_entity *dl_se = &p->dl;
  20426. if (later_mask &&
  20427. - cpumask_and(later_mask, cp->free_cpus, &p->cpus_allowed)) {
  20428. + cpumask_and(later_mask, cp->free_cpus, tsk_cpus_allowed(p))) {
  20429. best_cpu = cpumask_any(later_mask);
  20430. goto out;
  20431. - } else if (cpumask_test_cpu(cpudl_maximum(cp), &p->cpus_allowed) &&
  20432. + } else if (cpumask_test_cpu(cpudl_maximum(cp), tsk_cpus_allowed(p)) &&
  20433. dl_time_before(dl_se->deadline, cp->elements[0].dl)) {
  20434. best_cpu = cpudl_maximum(cp);
  20435. if (later_mask)
  20436. diff -Nur linux-4.4.28.orig/kernel/sched/cpupri.c linux-4.4.28/kernel/sched/cpupri.c
  20437. --- linux-4.4.28.orig/kernel/sched/cpupri.c 2016-10-28 09:53:25.000000000 +0200
  20438. +++ linux-4.4.28/kernel/sched/cpupri.c 2016-11-01 21:26:58.784342796 +0100
  20439. @@ -103,11 +103,11 @@
  20440. if (skip)
  20441. continue;
  20442. - if (cpumask_any_and(&p->cpus_allowed, vec->mask) >= nr_cpu_ids)
  20443. + if (cpumask_any_and(tsk_cpus_allowed(p), vec->mask) >= nr_cpu_ids)
  20444. continue;
  20445. if (lowest_mask) {
  20446. - cpumask_and(lowest_mask, &p->cpus_allowed, vec->mask);
  20447. + cpumask_and(lowest_mask, tsk_cpus_allowed(p), vec->mask);
  20448. /*
  20449. * We have to ensure that we have at least one bit
  20450. diff -Nur linux-4.4.28.orig/kernel/sched/cputime.c linux-4.4.28/kernel/sched/cputime.c
  20451. --- linux-4.4.28.orig/kernel/sched/cputime.c 2016-10-28 09:53:25.000000000 +0200
  20452. +++ linux-4.4.28/kernel/sched/cputime.c 2016-11-01 21:26:58.788343053 +0100
  20453. @@ -685,7 +685,7 @@
  20454. {
  20455. unsigned long long delta = vtime_delta(tsk);
  20456. - WARN_ON_ONCE(tsk->vtime_snap_whence == VTIME_SLEEPING);
  20457. + WARN_ON_ONCE(tsk->vtime_snap_whence == VTIME_INACTIVE);
  20458. tsk->vtime_snap += delta;
  20459. /* CHECKME: always safe to convert nsecs to cputime? */
  20460. @@ -701,37 +701,37 @@
  20461. void vtime_account_system(struct task_struct *tsk)
  20462. {
  20463. - write_seqlock(&tsk->vtime_seqlock);
  20464. + write_seqcount_begin(&tsk->vtime_seqcount);
  20465. __vtime_account_system(tsk);
  20466. - write_sequnlock(&tsk->vtime_seqlock);
  20467. + write_seqcount_end(&tsk->vtime_seqcount);
  20468. }
  20469. void vtime_gen_account_irq_exit(struct task_struct *tsk)
  20470. {
  20471. - write_seqlock(&tsk->vtime_seqlock);
  20472. + write_seqcount_begin(&tsk->vtime_seqcount);
  20473. __vtime_account_system(tsk);
  20474. if (context_tracking_in_user())
  20475. tsk->vtime_snap_whence = VTIME_USER;
  20476. - write_sequnlock(&tsk->vtime_seqlock);
  20477. + write_seqcount_end(&tsk->vtime_seqcount);
  20478. }
  20479. void vtime_account_user(struct task_struct *tsk)
  20480. {
  20481. cputime_t delta_cpu;
  20482. - write_seqlock(&tsk->vtime_seqlock);
  20483. + write_seqcount_begin(&tsk->vtime_seqcount);
  20484. delta_cpu = get_vtime_delta(tsk);
  20485. tsk->vtime_snap_whence = VTIME_SYS;
  20486. account_user_time(tsk, delta_cpu, cputime_to_scaled(delta_cpu));
  20487. - write_sequnlock(&tsk->vtime_seqlock);
  20488. + write_seqcount_end(&tsk->vtime_seqcount);
  20489. }
  20490. void vtime_user_enter(struct task_struct *tsk)
  20491. {
  20492. - write_seqlock(&tsk->vtime_seqlock);
  20493. + write_seqcount_begin(&tsk->vtime_seqcount);
  20494. __vtime_account_system(tsk);
  20495. tsk->vtime_snap_whence = VTIME_USER;
  20496. - write_sequnlock(&tsk->vtime_seqlock);
  20497. + write_seqcount_end(&tsk->vtime_seqcount);
  20498. }
  20499. void vtime_guest_enter(struct task_struct *tsk)
  20500. @@ -743,19 +743,19 @@
  20501. * synchronization against the reader (task_gtime())
  20502. * that can thus safely catch up with a tickless delta.
  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_enter);
  20512. void vtime_guest_exit(struct task_struct *tsk)
  20513. {
  20514. - write_seqlock(&tsk->vtime_seqlock);
  20515. + write_seqcount_begin(&tsk->vtime_seqcount);
  20516. __vtime_account_system(tsk);
  20517. current->flags &= ~PF_VCPU;
  20518. - write_sequnlock(&tsk->vtime_seqlock);
  20519. + write_seqcount_end(&tsk->vtime_seqcount);
  20520. }
  20521. EXPORT_SYMBOL_GPL(vtime_guest_exit);
  20522. @@ -768,24 +768,26 @@
  20523. void arch_vtime_task_switch(struct task_struct *prev)
  20524. {
  20525. - write_seqlock(&prev->vtime_seqlock);
  20526. - prev->vtime_snap_whence = VTIME_SLEEPING;
  20527. - write_sequnlock(&prev->vtime_seqlock);
  20528. + write_seqcount_begin(&prev->vtime_seqcount);
  20529. + prev->vtime_snap_whence = VTIME_INACTIVE;
  20530. + write_seqcount_end(&prev->vtime_seqcount);
  20531. - write_seqlock(&current->vtime_seqlock);
  20532. + write_seqcount_begin(&current->vtime_seqcount);
  20533. current->vtime_snap_whence = VTIME_SYS;
  20534. current->vtime_snap = sched_clock_cpu(smp_processor_id());
  20535. - write_sequnlock(&current->vtime_seqlock);
  20536. + write_seqcount_end(&current->vtime_seqcount);
  20537. }
  20538. void vtime_init_idle(struct task_struct *t, int cpu)
  20539. {
  20540. unsigned long flags;
  20541. - write_seqlock_irqsave(&t->vtime_seqlock, flags);
  20542. + local_irq_save(flags);
  20543. + write_seqcount_begin(&t->vtime_seqcount);
  20544. t->vtime_snap_whence = VTIME_SYS;
  20545. t->vtime_snap = sched_clock_cpu(cpu);
  20546. - write_sequnlock_irqrestore(&t->vtime_seqlock, flags);
  20547. + write_seqcount_end(&t->vtime_seqcount);
  20548. + local_irq_restore(flags);
  20549. }
  20550. cputime_t task_gtime(struct task_struct *t)
  20551. @@ -797,13 +799,13 @@
  20552. return t->gtime;
  20553. do {
  20554. - seq = read_seqbegin(&t->vtime_seqlock);
  20555. + seq = read_seqcount_begin(&t->vtime_seqcount);
  20556. gtime = t->gtime;
  20557. if (t->flags & PF_VCPU)
  20558. gtime += vtime_delta(t);
  20559. - } while (read_seqretry(&t->vtime_seqlock, seq));
  20560. + } while (read_seqcount_retry(&t->vtime_seqcount, seq));
  20561. return gtime;
  20562. }
  20563. @@ -826,7 +828,7 @@
  20564. *udelta = 0;
  20565. *sdelta = 0;
  20566. - seq = read_seqbegin(&t->vtime_seqlock);
  20567. + seq = read_seqcount_begin(&t->vtime_seqcount);
  20568. if (u_dst)
  20569. *u_dst = *u_src;
  20570. @@ -834,7 +836,7 @@
  20571. *s_dst = *s_src;
  20572. /* Task is sleeping, nothing to add */
  20573. - if (t->vtime_snap_whence == VTIME_SLEEPING ||
  20574. + if (t->vtime_snap_whence == VTIME_INACTIVE ||
  20575. is_idle_task(t))
  20576. continue;
  20577. @@ -850,7 +852,7 @@
  20578. if (t->vtime_snap_whence == VTIME_SYS)
  20579. *sdelta = delta;
  20580. }
  20581. - } while (read_seqretry(&t->vtime_seqlock, seq));
  20582. + } while (read_seqcount_retry(&t->vtime_seqcount, seq));
  20583. }
  20584. diff -Nur linux-4.4.28.orig/kernel/sched/deadline.c linux-4.4.28/kernel/sched/deadline.c
  20585. --- linux-4.4.28.orig/kernel/sched/deadline.c 2016-10-28 09:53:25.000000000 +0200
  20586. +++ linux-4.4.28/kernel/sched/deadline.c 2016-11-01 21:26:58.788343053 +0100
  20587. @@ -134,7 +134,7 @@
  20588. {
  20589. struct task_struct *p = dl_task_of(dl_se);
  20590. - if (p->nr_cpus_allowed > 1)
  20591. + if (tsk_nr_cpus_allowed(p) > 1)
  20592. dl_rq->dl_nr_migratory++;
  20593. update_dl_migration(dl_rq);
  20594. @@ -144,7 +144,7 @@
  20595. {
  20596. struct task_struct *p = dl_task_of(dl_se);
  20597. - if (p->nr_cpus_allowed > 1)
  20598. + if (tsk_nr_cpus_allowed(p) > 1)
  20599. dl_rq->dl_nr_migratory--;
  20600. update_dl_migration(dl_rq);
  20601. @@ -697,6 +697,7 @@
  20602. hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  20603. timer->function = dl_task_timer;
  20604. + timer->irqsafe = 1;
  20605. }
  20606. static
  20607. @@ -989,7 +990,7 @@
  20608. enqueue_dl_entity(&p->dl, pi_se, flags);
  20609. - if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
  20610. + if (!task_current(rq, p) && tsk_nr_cpus_allowed(p) > 1)
  20611. enqueue_pushable_dl_task(rq, p);
  20612. }
  20613. @@ -1067,9 +1068,9 @@
  20614. * try to make it stay here, it might be important.
  20615. */
  20616. if (unlikely(dl_task(curr)) &&
  20617. - (curr->nr_cpus_allowed < 2 ||
  20618. + (tsk_nr_cpus_allowed(curr) < 2 ||
  20619. !dl_entity_preempt(&p->dl, &curr->dl)) &&
  20620. - (p->nr_cpus_allowed > 1)) {
  20621. + (tsk_nr_cpus_allowed(p) > 1)) {
  20622. int target = find_later_rq(p);
  20623. if (target != -1 &&
  20624. @@ -1090,7 +1091,7 @@
  20625. * Current can't be migrated, useless to reschedule,
  20626. * let's hope p can move out.
  20627. */
  20628. - if (rq->curr->nr_cpus_allowed == 1 ||
  20629. + if (tsk_nr_cpus_allowed(rq->curr) == 1 ||
  20630. cpudl_find(&rq->rd->cpudl, rq->curr, NULL) == -1)
  20631. return;
  20632. @@ -1098,7 +1099,7 @@
  20633. * p is migratable, so let's not schedule it and
  20634. * see if it is pushed or pulled somewhere else.
  20635. */
  20636. - if (p->nr_cpus_allowed != 1 &&
  20637. + if (tsk_nr_cpus_allowed(p) != 1 &&
  20638. cpudl_find(&rq->rd->cpudl, p, NULL) != -1)
  20639. return;
  20640. @@ -1212,7 +1213,7 @@
  20641. {
  20642. update_curr_dl(rq);
  20643. - if (on_dl_rq(&p->dl) && p->nr_cpus_allowed > 1)
  20644. + if (on_dl_rq(&p->dl) && tsk_nr_cpus_allowed(p) > 1)
  20645. enqueue_pushable_dl_task(rq, p);
  20646. }
  20647. @@ -1335,7 +1336,7 @@
  20648. if (unlikely(!later_mask))
  20649. return -1;
  20650. - if (task->nr_cpus_allowed == 1)
  20651. + if (tsk_nr_cpus_allowed(task) == 1)
  20652. return -1;
  20653. /*
  20654. @@ -1441,7 +1442,7 @@
  20655. if (double_lock_balance(rq, later_rq)) {
  20656. if (unlikely(task_rq(task) != rq ||
  20657. !cpumask_test_cpu(later_rq->cpu,
  20658. - &task->cpus_allowed) ||
  20659. + tsk_cpus_allowed(task)) ||
  20660. task_running(rq, task) ||
  20661. !task_on_rq_queued(task))) {
  20662. double_unlock_balance(rq, later_rq);
  20663. @@ -1480,7 +1481,7 @@
  20664. BUG_ON(rq->cpu != task_cpu(p));
  20665. BUG_ON(task_current(rq, p));
  20666. - BUG_ON(p->nr_cpus_allowed <= 1);
  20667. + BUG_ON(tsk_nr_cpus_allowed(p) <= 1);
  20668. BUG_ON(!task_on_rq_queued(p));
  20669. BUG_ON(!dl_task(p));
  20670. @@ -1519,7 +1520,7 @@
  20671. */
  20672. if (dl_task(rq->curr) &&
  20673. dl_time_before(next_task->dl.deadline, rq->curr->dl.deadline) &&
  20674. - rq->curr->nr_cpus_allowed > 1) {
  20675. + tsk_nr_cpus_allowed(rq->curr) > 1) {
  20676. resched_curr(rq);
  20677. return 0;
  20678. }
  20679. @@ -1666,9 +1667,9 @@
  20680. {
  20681. if (!task_running(rq, p) &&
  20682. !test_tsk_need_resched(rq->curr) &&
  20683. - p->nr_cpus_allowed > 1 &&
  20684. + tsk_nr_cpus_allowed(p) > 1 &&
  20685. dl_task(rq->curr) &&
  20686. - (rq->curr->nr_cpus_allowed < 2 ||
  20687. + (tsk_nr_cpus_allowed(rq->curr) < 2 ||
  20688. !dl_entity_preempt(&p->dl, &rq->curr->dl))) {
  20689. push_dl_tasks(rq);
  20690. }
  20691. @@ -1769,7 +1770,7 @@
  20692. {
  20693. if (task_on_rq_queued(p) && rq->curr != p) {
  20694. #ifdef CONFIG_SMP
  20695. - if (p->nr_cpus_allowed > 1 && rq->dl.overloaded)
  20696. + if (tsk_nr_cpus_allowed(p) > 1 && rq->dl.overloaded)
  20697. queue_push_tasks(rq);
  20698. #else
  20699. if (dl_task(rq->curr))
  20700. diff -Nur linux-4.4.28.orig/kernel/sched/debug.c linux-4.4.28/kernel/sched/debug.c
  20701. --- linux-4.4.28.orig/kernel/sched/debug.c 2016-10-28 09:53:25.000000000 +0200
  20702. +++ linux-4.4.28/kernel/sched/debug.c 2016-11-01 21:26:58.788343053 +0100
  20703. @@ -251,6 +251,9 @@
  20704. P(rt_throttled);
  20705. PN(rt_time);
  20706. PN(rt_runtime);
  20707. +#ifdef CONFIG_SMP
  20708. + P(rt_nr_migratory);
  20709. +#endif
  20710. #undef PN
  20711. #undef P
  20712. @@ -635,6 +638,10 @@
  20713. #endif
  20714. P(policy);
  20715. P(prio);
  20716. +#ifdef CONFIG_PREEMPT_RT_FULL
  20717. + P(migrate_disable);
  20718. +#endif
  20719. + P(nr_cpus_allowed);
  20720. #undef PN
  20721. #undef __PN
  20722. #undef P
  20723. diff -Nur linux-4.4.28.orig/kernel/sched/fair.c linux-4.4.28/kernel/sched/fair.c
  20724. --- linux-4.4.28.orig/kernel/sched/fair.c 2016-10-28 09:53:25.000000000 +0200
  20725. +++ linux-4.4.28/kernel/sched/fair.c 2016-11-01 21:26:58.792343317 +0100
  20726. @@ -3166,7 +3166,7 @@
  20727. ideal_runtime = sched_slice(cfs_rq, curr);
  20728. delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
  20729. if (delta_exec > ideal_runtime) {
  20730. - resched_curr(rq_of(cfs_rq));
  20731. + resched_curr_lazy(rq_of(cfs_rq));
  20732. /*
  20733. * The current task ran long enough, ensure it doesn't get
  20734. * re-elected due to buddy favours.
  20735. @@ -3190,7 +3190,7 @@
  20736. return;
  20737. if (delta > ideal_runtime)
  20738. - resched_curr(rq_of(cfs_rq));
  20739. + resched_curr_lazy(rq_of(cfs_rq));
  20740. }
  20741. static void
  20742. @@ -3330,7 +3330,7 @@
  20743. * validating it and just reschedule.
  20744. */
  20745. if (queued) {
  20746. - resched_curr(rq_of(cfs_rq));
  20747. + resched_curr_lazy(rq_of(cfs_rq));
  20748. return;
  20749. }
  20750. /*
  20751. @@ -3512,7 +3512,7 @@
  20752. * hierarchy can be throttled
  20753. */
  20754. if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr))
  20755. - resched_curr(rq_of(cfs_rq));
  20756. + resched_curr_lazy(rq_of(cfs_rq));
  20757. }
  20758. static __always_inline
  20759. @@ -4124,7 +4124,7 @@
  20760. if (delta < 0) {
  20761. if (rq->curr == p)
  20762. - resched_curr(rq);
  20763. + resched_curr_lazy(rq);
  20764. return;
  20765. }
  20766. hrtick_start(rq, delta);
  20767. @@ -5213,7 +5213,7 @@
  20768. return;
  20769. preempt:
  20770. - resched_curr(rq);
  20771. + resched_curr_lazy(rq);
  20772. /*
  20773. * Only set the backward buddy when the current task is still
  20774. * on the rq. This can happen when a wakeup gets interleaved
  20775. @@ -7964,7 +7964,7 @@
  20776. * 'current' within the tree based on its new key value.
  20777. */
  20778. swap(curr->vruntime, se->vruntime);
  20779. - resched_curr(rq);
  20780. + resched_curr_lazy(rq);
  20781. }
  20782. se->vruntime -= cfs_rq->min_vruntime;
  20783. @@ -7989,7 +7989,7 @@
  20784. */
  20785. if (rq->curr == p) {
  20786. if (p->prio > oldprio)
  20787. - resched_curr(rq);
  20788. + resched_curr_lazy(rq);
  20789. } else
  20790. check_preempt_curr(rq, p, 0);
  20791. }
  20792. diff -Nur linux-4.4.28.orig/kernel/sched/features.h linux-4.4.28/kernel/sched/features.h
  20793. --- linux-4.4.28.orig/kernel/sched/features.h 2016-10-28 09:53:25.000000000 +0200
  20794. +++ linux-4.4.28/kernel/sched/features.h 2016-11-01 21:26:58.792343317 +0100
  20795. @@ -45,11 +45,19 @@
  20796. */
  20797. SCHED_FEAT(NONTASK_CAPACITY, true)
  20798. +#ifdef CONFIG_PREEMPT_RT_FULL
  20799. +SCHED_FEAT(TTWU_QUEUE, false)
  20800. +# ifdef CONFIG_PREEMPT_LAZY
  20801. +SCHED_FEAT(PREEMPT_LAZY, true)
  20802. +# endif
  20803. +#else
  20804. +
  20805. /*
  20806. * Queue remote wakeups on the target CPU and process them
  20807. * using the scheduler IPI. Reduces rq->lock contention/bounces.
  20808. */
  20809. SCHED_FEAT(TTWU_QUEUE, true)
  20810. +#endif
  20811. #ifdef HAVE_RT_PUSH_IPI
  20812. /*
  20813. diff -Nur linux-4.4.28.orig/kernel/sched/Makefile linux-4.4.28/kernel/sched/Makefile
  20814. --- linux-4.4.28.orig/kernel/sched/Makefile 2016-10-28 09:53:25.000000000 +0200
  20815. +++ linux-4.4.28/kernel/sched/Makefile 2016-11-01 21:26:58.780342543 +0100
  20816. @@ -13,7 +13,7 @@
  20817. obj-y += core.o loadavg.o clock.o cputime.o
  20818. obj-y += idle_task.o fair.o rt.o deadline.o stop_task.o
  20819. -obj-y += wait.o completion.o idle.o
  20820. +obj-y += wait.o swait.o swork.o completion.o idle.o
  20821. obj-$(CONFIG_SMP) += cpupri.o cpudeadline.o
  20822. obj-$(CONFIG_SCHED_AUTOGROUP) += auto_group.o
  20823. obj-$(CONFIG_SCHEDSTATS) += stats.o
  20824. diff -Nur linux-4.4.28.orig/kernel/sched/rt.c linux-4.4.28/kernel/sched/rt.c
  20825. --- linux-4.4.28.orig/kernel/sched/rt.c 2016-10-28 09:53:25.000000000 +0200
  20826. +++ linux-4.4.28/kernel/sched/rt.c 2016-11-01 21:26:58.792343317 +0100
  20827. @@ -47,6 +47,7 @@
  20828. hrtimer_init(&rt_b->rt_period_timer,
  20829. CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  20830. + rt_b->rt_period_timer.irqsafe = 1;
  20831. rt_b->rt_period_timer.function = sched_rt_period_timer;
  20832. }
  20833. @@ -93,6 +94,7 @@
  20834. rt_rq->push_cpu = nr_cpu_ids;
  20835. raw_spin_lock_init(&rt_rq->push_lock);
  20836. init_irq_work(&rt_rq->push_work, push_irq_work_func);
  20837. + rt_rq->push_work.flags |= IRQ_WORK_HARD_IRQ;
  20838. #endif
  20839. #endif /* CONFIG_SMP */
  20840. /* We start is dequeued state, because no RT tasks are queued */
  20841. @@ -326,7 +328,7 @@
  20842. rt_rq = &rq_of_rt_rq(rt_rq)->rt;
  20843. rt_rq->rt_nr_total++;
  20844. - if (p->nr_cpus_allowed > 1)
  20845. + if (tsk_nr_cpus_allowed(p) > 1)
  20846. rt_rq->rt_nr_migratory++;
  20847. update_rt_migration(rt_rq);
  20848. @@ -343,7 +345,7 @@
  20849. rt_rq = &rq_of_rt_rq(rt_rq)->rt;
  20850. rt_rq->rt_nr_total--;
  20851. - if (p->nr_cpus_allowed > 1)
  20852. + if (tsk_nr_cpus_allowed(p) > 1)
  20853. rt_rq->rt_nr_migratory--;
  20854. update_rt_migration(rt_rq);
  20855. @@ -1262,7 +1264,7 @@
  20856. enqueue_rt_entity(rt_se, flags & ENQUEUE_HEAD);
  20857. - if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
  20858. + if (!task_current(rq, p) && tsk_nr_cpus_allowed(p) > 1)
  20859. enqueue_pushable_task(rq, p);
  20860. }
  20861. @@ -1351,7 +1353,7 @@
  20862. * will have to sort it out.
  20863. */
  20864. if (curr && unlikely(rt_task(curr)) &&
  20865. - (curr->nr_cpus_allowed < 2 ||
  20866. + (tsk_nr_cpus_allowed(curr) < 2 ||
  20867. curr->prio <= p->prio)) {
  20868. int target = find_lowest_rq(p);
  20869. @@ -1375,7 +1377,7 @@
  20870. * Current can't be migrated, useless to reschedule,
  20871. * let's hope p can move out.
  20872. */
  20873. - if (rq->curr->nr_cpus_allowed == 1 ||
  20874. + if (tsk_nr_cpus_allowed(rq->curr) == 1 ||
  20875. !cpupri_find(&rq->rd->cpupri, rq->curr, NULL))
  20876. return;
  20877. @@ -1383,7 +1385,7 @@
  20878. * p is migratable, so let's not schedule it and
  20879. * see if it is pushed or pulled somewhere else.
  20880. */
  20881. - if (p->nr_cpus_allowed != 1
  20882. + if (tsk_nr_cpus_allowed(p) != 1
  20883. && cpupri_find(&rq->rd->cpupri, p, NULL))
  20884. return;
  20885. @@ -1517,7 +1519,7 @@
  20886. * The previous task needs to be made eligible for pushing
  20887. * if it is still active
  20888. */
  20889. - if (on_rt_rq(&p->rt) && p->nr_cpus_allowed > 1)
  20890. + if (on_rt_rq(&p->rt) && tsk_nr_cpus_allowed(p) > 1)
  20891. enqueue_pushable_task(rq, p);
  20892. }
  20893. @@ -1567,7 +1569,7 @@
  20894. if (unlikely(!lowest_mask))
  20895. return -1;
  20896. - if (task->nr_cpus_allowed == 1)
  20897. + if (tsk_nr_cpus_allowed(task) == 1)
  20898. return -1; /* No other targets possible */
  20899. if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask))
  20900. @@ -1699,7 +1701,7 @@
  20901. BUG_ON(rq->cpu != task_cpu(p));
  20902. BUG_ON(task_current(rq, p));
  20903. - BUG_ON(p->nr_cpus_allowed <= 1);
  20904. + BUG_ON(tsk_nr_cpus_allowed(p) <= 1);
  20905. BUG_ON(!task_on_rq_queued(p));
  20906. BUG_ON(!rt_task(p));
  20907. @@ -2059,9 +2061,9 @@
  20908. {
  20909. if (!task_running(rq, p) &&
  20910. !test_tsk_need_resched(rq->curr) &&
  20911. - p->nr_cpus_allowed > 1 &&
  20912. + tsk_nr_cpus_allowed(p) > 1 &&
  20913. (dl_task(rq->curr) || rt_task(rq->curr)) &&
  20914. - (rq->curr->nr_cpus_allowed < 2 ||
  20915. + (tsk_nr_cpus_allowed(rq->curr) < 2 ||
  20916. rq->curr->prio <= p->prio))
  20917. push_rt_tasks(rq);
  20918. }
  20919. @@ -2134,7 +2136,7 @@
  20920. */
  20921. if (task_on_rq_queued(p) && rq->curr != p) {
  20922. #ifdef CONFIG_SMP
  20923. - if (p->nr_cpus_allowed > 1 && rq->rt.overloaded)
  20924. + if (tsk_nr_cpus_allowed(p) > 1 && rq->rt.overloaded)
  20925. queue_push_tasks(rq);
  20926. #else
  20927. if (p->prio < rq->curr->prio)
  20928. diff -Nur linux-4.4.28.orig/kernel/sched/sched.h linux-4.4.28/kernel/sched/sched.h
  20929. --- linux-4.4.28.orig/kernel/sched/sched.h 2016-10-28 09:53:25.000000000 +0200
  20930. +++ linux-4.4.28/kernel/sched/sched.h 2016-11-01 21:26:58.792343317 +0100
  20931. @@ -1100,6 +1100,7 @@
  20932. #define WF_SYNC 0x01 /* waker goes to sleep after wakeup */
  20933. #define WF_FORK 0x02 /* child wakeup after fork */
  20934. #define WF_MIGRATED 0x4 /* internal use, task got migrated */
  20935. +#define WF_LOCK_SLEEPER 0x08 /* wakeup spinlock "sleeper" */
  20936. /*
  20937. * To aid in avoiding the subversion of "niceness" due to uneven distribution
  20938. @@ -1299,6 +1300,15 @@
  20939. extern void resched_curr(struct rq *rq);
  20940. extern void resched_cpu(int cpu);
  20941. +#ifdef CONFIG_PREEMPT_LAZY
  20942. +extern void resched_curr_lazy(struct rq *rq);
  20943. +#else
  20944. +static inline void resched_curr_lazy(struct rq *rq)
  20945. +{
  20946. + resched_curr(rq);
  20947. +}
  20948. +#endif
  20949. +
  20950. extern struct rt_bandwidth def_rt_bandwidth;
  20951. extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
  20952. diff -Nur linux-4.4.28.orig/kernel/sched/swait.c linux-4.4.28/kernel/sched/swait.c
  20953. --- linux-4.4.28.orig/kernel/sched/swait.c 1970-01-01 01:00:00.000000000 +0100
  20954. +++ linux-4.4.28/kernel/sched/swait.c 2016-11-01 21:26:58.792343317 +0100
  20955. @@ -0,0 +1,143 @@
  20956. +#include <linux/sched.h>
  20957. +#include <linux/swait.h>
  20958. +#include <linux/suspend.h>
  20959. +
  20960. +void __init_swait_queue_head(struct swait_queue_head *q, const char *name,
  20961. + struct lock_class_key *key)
  20962. +{
  20963. + raw_spin_lock_init(&q->lock);
  20964. + lockdep_set_class_and_name(&q->lock, key, name);
  20965. + INIT_LIST_HEAD(&q->task_list);
  20966. +}
  20967. +EXPORT_SYMBOL(__init_swait_queue_head);
  20968. +
  20969. +/*
  20970. + * The thing about the wake_up_state() return value; I think we can ignore it.
  20971. + *
  20972. + * If for some reason it would return 0, that means the previously waiting
  20973. + * task is already running, so it will observe condition true (or has already).
  20974. + */
  20975. +void swake_up_locked(struct swait_queue_head *q)
  20976. +{
  20977. + struct swait_queue *curr;
  20978. +
  20979. + if (list_empty(&q->task_list))
  20980. + return;
  20981. +
  20982. + curr = list_first_entry(&q->task_list, typeof(*curr), task_list);
  20983. + wake_up_process(curr->task);
  20984. + list_del_init(&curr->task_list);
  20985. +}
  20986. +EXPORT_SYMBOL(swake_up_locked);
  20987. +
  20988. +void swake_up_all_locked(struct swait_queue_head *q)
  20989. +{
  20990. + struct swait_queue *curr;
  20991. + int wakes = 0;
  20992. +
  20993. + while (!list_empty(&q->task_list)) {
  20994. +
  20995. + curr = list_first_entry(&q->task_list, typeof(*curr),
  20996. + task_list);
  20997. + wake_up_process(curr->task);
  20998. + list_del_init(&curr->task_list);
  20999. + wakes++;
  21000. + }
  21001. + if (pm_in_action)
  21002. + return;
  21003. + WARN(wakes > 2, "complate_all() with %d waiters\n", wakes);
  21004. +}
  21005. +EXPORT_SYMBOL(swake_up_all_locked);
  21006. +
  21007. +void swake_up(struct swait_queue_head *q)
  21008. +{
  21009. + unsigned long flags;
  21010. +
  21011. + if (!swait_active(q))
  21012. + return;
  21013. +
  21014. + raw_spin_lock_irqsave(&q->lock, flags);
  21015. + swake_up_locked(q);
  21016. + raw_spin_unlock_irqrestore(&q->lock, flags);
  21017. +}
  21018. +EXPORT_SYMBOL(swake_up);
  21019. +
  21020. +/*
  21021. + * Does not allow usage from IRQ disabled, since we must be able to
  21022. + * release IRQs to guarantee bounded hold time.
  21023. + */
  21024. +void swake_up_all(struct swait_queue_head *q)
  21025. +{
  21026. + struct swait_queue *curr;
  21027. + LIST_HEAD(tmp);
  21028. +
  21029. + if (!swait_active(q))
  21030. + return;
  21031. +
  21032. + raw_spin_lock_irq(&q->lock);
  21033. + list_splice_init(&q->task_list, &tmp);
  21034. + while (!list_empty(&tmp)) {
  21035. + curr = list_first_entry(&tmp, typeof(*curr), task_list);
  21036. +
  21037. + wake_up_state(curr->task, TASK_NORMAL);
  21038. + list_del_init(&curr->task_list);
  21039. +
  21040. + if (list_empty(&tmp))
  21041. + break;
  21042. +
  21043. + raw_spin_unlock_irq(&q->lock);
  21044. + raw_spin_lock_irq(&q->lock);
  21045. + }
  21046. + raw_spin_unlock_irq(&q->lock);
  21047. +}
  21048. +EXPORT_SYMBOL(swake_up_all);
  21049. +
  21050. +void __prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait)
  21051. +{
  21052. + wait->task = current;
  21053. + if (list_empty(&wait->task_list))
  21054. + list_add(&wait->task_list, &q->task_list);
  21055. +}
  21056. +
  21057. +void prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait, int state)
  21058. +{
  21059. + unsigned long flags;
  21060. +
  21061. + raw_spin_lock_irqsave(&q->lock, flags);
  21062. + __prepare_to_swait(q, wait);
  21063. + set_current_state(state);
  21064. + raw_spin_unlock_irqrestore(&q->lock, flags);
  21065. +}
  21066. +EXPORT_SYMBOL(prepare_to_swait);
  21067. +
  21068. +long prepare_to_swait_event(struct swait_queue_head *q, struct swait_queue *wait, int state)
  21069. +{
  21070. + if (signal_pending_state(state, current))
  21071. + return -ERESTARTSYS;
  21072. +
  21073. + prepare_to_swait(q, wait, state);
  21074. +
  21075. + return 0;
  21076. +}
  21077. +EXPORT_SYMBOL(prepare_to_swait_event);
  21078. +
  21079. +void __finish_swait(struct swait_queue_head *q, struct swait_queue *wait)
  21080. +{
  21081. + __set_current_state(TASK_RUNNING);
  21082. + if (!list_empty(&wait->task_list))
  21083. + list_del_init(&wait->task_list);
  21084. +}
  21085. +
  21086. +void finish_swait(struct swait_queue_head *q, struct swait_queue *wait)
  21087. +{
  21088. + unsigned long flags;
  21089. +
  21090. + __set_current_state(TASK_RUNNING);
  21091. +
  21092. + if (!list_empty_careful(&wait->task_list)) {
  21093. + raw_spin_lock_irqsave(&q->lock, flags);
  21094. + list_del_init(&wait->task_list);
  21095. + raw_spin_unlock_irqrestore(&q->lock, flags);
  21096. + }
  21097. +}
  21098. +EXPORT_SYMBOL(finish_swait);
  21099. diff -Nur linux-4.4.28.orig/kernel/sched/swork.c linux-4.4.28/kernel/sched/swork.c
  21100. --- linux-4.4.28.orig/kernel/sched/swork.c 1970-01-01 01:00:00.000000000 +0100
  21101. +++ linux-4.4.28/kernel/sched/swork.c 2016-11-01 21:26:58.792343317 +0100
  21102. @@ -0,0 +1,173 @@
  21103. +/*
  21104. + * Copyright (C) 2014 BMW Car IT GmbH, Daniel Wagner daniel.wagner@bmw-carit.de
  21105. + *
  21106. + * Provides a framework for enqueuing callbacks from irq context
  21107. + * PREEMPT_RT_FULL safe. The callbacks are executed in kthread context.
  21108. + */
  21109. +
  21110. +#include <linux/swait.h>
  21111. +#include <linux/swork.h>
  21112. +#include <linux/kthread.h>
  21113. +#include <linux/slab.h>
  21114. +#include <linux/spinlock.h>
  21115. +#include <linux/export.h>
  21116. +
  21117. +#define SWORK_EVENT_PENDING (1 << 0)
  21118. +
  21119. +static DEFINE_MUTEX(worker_mutex);
  21120. +static struct sworker *glob_worker;
  21121. +
  21122. +struct sworker {
  21123. + struct list_head events;
  21124. + struct swait_queue_head wq;
  21125. +
  21126. + raw_spinlock_t lock;
  21127. +
  21128. + struct task_struct *task;
  21129. + int refs;
  21130. +};
  21131. +
  21132. +static bool swork_readable(struct sworker *worker)
  21133. +{
  21134. + bool r;
  21135. +
  21136. + if (kthread_should_stop())
  21137. + return true;
  21138. +
  21139. + raw_spin_lock_irq(&worker->lock);
  21140. + r = !list_empty(&worker->events);
  21141. + raw_spin_unlock_irq(&worker->lock);
  21142. +
  21143. + return r;
  21144. +}
  21145. +
  21146. +static int swork_kthread(void *arg)
  21147. +{
  21148. + struct sworker *worker = arg;
  21149. +
  21150. + for (;;) {
  21151. + swait_event_interruptible(worker->wq,
  21152. + swork_readable(worker));
  21153. + if (kthread_should_stop())
  21154. + break;
  21155. +
  21156. + raw_spin_lock_irq(&worker->lock);
  21157. + while (!list_empty(&worker->events)) {
  21158. + struct swork_event *sev;
  21159. +
  21160. + sev = list_first_entry(&worker->events,
  21161. + struct swork_event, item);
  21162. + list_del(&sev->item);
  21163. + raw_spin_unlock_irq(&worker->lock);
  21164. +
  21165. + WARN_ON_ONCE(!test_and_clear_bit(SWORK_EVENT_PENDING,
  21166. + &sev->flags));
  21167. + sev->func(sev);
  21168. + raw_spin_lock_irq(&worker->lock);
  21169. + }
  21170. + raw_spin_unlock_irq(&worker->lock);
  21171. + }
  21172. + return 0;
  21173. +}
  21174. +
  21175. +static struct sworker *swork_create(void)
  21176. +{
  21177. + struct sworker *worker;
  21178. +
  21179. + worker = kzalloc(sizeof(*worker), GFP_KERNEL);
  21180. + if (!worker)
  21181. + return ERR_PTR(-ENOMEM);
  21182. +
  21183. + INIT_LIST_HEAD(&worker->events);
  21184. + raw_spin_lock_init(&worker->lock);
  21185. + init_swait_queue_head(&worker->wq);
  21186. +
  21187. + worker->task = kthread_run(swork_kthread, worker, "kswork");
  21188. + if (IS_ERR(worker->task)) {
  21189. + kfree(worker);
  21190. + return ERR_PTR(-ENOMEM);
  21191. + }
  21192. +
  21193. + return worker;
  21194. +}
  21195. +
  21196. +static void swork_destroy(struct sworker *worker)
  21197. +{
  21198. + kthread_stop(worker->task);
  21199. +
  21200. + WARN_ON(!list_empty(&worker->events));
  21201. + kfree(worker);
  21202. +}
  21203. +
  21204. +/**
  21205. + * swork_queue - queue swork
  21206. + *
  21207. + * Returns %false if @work was already on a queue, %true otherwise.
  21208. + *
  21209. + * The work is queued and processed on a random CPU
  21210. + */
  21211. +bool swork_queue(struct swork_event *sev)
  21212. +{
  21213. + unsigned long flags;
  21214. +
  21215. + if (test_and_set_bit(SWORK_EVENT_PENDING, &sev->flags))
  21216. + return false;
  21217. +
  21218. + raw_spin_lock_irqsave(&glob_worker->lock, flags);
  21219. + list_add_tail(&sev->item, &glob_worker->events);
  21220. + raw_spin_unlock_irqrestore(&glob_worker->lock, flags);
  21221. +
  21222. + swake_up(&glob_worker->wq);
  21223. + return true;
  21224. +}
  21225. +EXPORT_SYMBOL_GPL(swork_queue);
  21226. +
  21227. +/**
  21228. + * swork_get - get an instance of the sworker
  21229. + *
  21230. + * Returns an negative error code if the initialization if the worker did not
  21231. + * work, %0 otherwise.
  21232. + *
  21233. + */
  21234. +int swork_get(void)
  21235. +{
  21236. + struct sworker *worker;
  21237. +
  21238. + mutex_lock(&worker_mutex);
  21239. + if (!glob_worker) {
  21240. + worker = swork_create();
  21241. + if (IS_ERR(worker)) {
  21242. + mutex_unlock(&worker_mutex);
  21243. + return -ENOMEM;
  21244. + }
  21245. +
  21246. + glob_worker = worker;
  21247. + }
  21248. +
  21249. + glob_worker->refs++;
  21250. + mutex_unlock(&worker_mutex);
  21251. +
  21252. + return 0;
  21253. +}
  21254. +EXPORT_SYMBOL_GPL(swork_get);
  21255. +
  21256. +/**
  21257. + * swork_put - puts an instance of the sworker
  21258. + *
  21259. + * Will destroy the sworker thread. This function must not be called until all
  21260. + * queued events have been completed.
  21261. + */
  21262. +void swork_put(void)
  21263. +{
  21264. + mutex_lock(&worker_mutex);
  21265. +
  21266. + glob_worker->refs--;
  21267. + if (glob_worker->refs > 0)
  21268. + goto out;
  21269. +
  21270. + swork_destroy(glob_worker);
  21271. + glob_worker = NULL;
  21272. +out:
  21273. + mutex_unlock(&worker_mutex);
  21274. +}
  21275. +EXPORT_SYMBOL_GPL(swork_put);
  21276. diff -Nur linux-4.4.28.orig/kernel/signal.c linux-4.4.28/kernel/signal.c
  21277. --- linux-4.4.28.orig/kernel/signal.c 2016-10-28 09:53:25.000000000 +0200
  21278. +++ linux-4.4.28/kernel/signal.c 2016-11-01 21:26:58.796343570 +0100
  21279. @@ -14,6 +14,7 @@
  21280. #include <linux/export.h>
  21281. #include <linux/init.h>
  21282. #include <linux/sched.h>
  21283. +#include <linux/sched/rt.h>
  21284. #include <linux/fs.h>
  21285. #include <linux/tty.h>
  21286. #include <linux/binfmts.h>
  21287. @@ -352,13 +353,30 @@
  21288. return false;
  21289. }
  21290. +static inline struct sigqueue *get_task_cache(struct task_struct *t)
  21291. +{
  21292. + struct sigqueue *q = t->sigqueue_cache;
  21293. +
  21294. + if (cmpxchg(&t->sigqueue_cache, q, NULL) != q)
  21295. + return NULL;
  21296. + return q;
  21297. +}
  21298. +
  21299. +static inline int put_task_cache(struct task_struct *t, struct sigqueue *q)
  21300. +{
  21301. + if (cmpxchg(&t->sigqueue_cache, NULL, q) == NULL)
  21302. + return 0;
  21303. + return 1;
  21304. +}
  21305. +
  21306. /*
  21307. * allocate a new signal queue record
  21308. * - this may be called without locks if and only if t == current, otherwise an
  21309. * appropriate lock must be held to stop the target task from exiting
  21310. */
  21311. static struct sigqueue *
  21312. -__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
  21313. +__sigqueue_do_alloc(int sig, struct task_struct *t, gfp_t flags,
  21314. + int override_rlimit, int fromslab)
  21315. {
  21316. struct sigqueue *q = NULL;
  21317. struct user_struct *user;
  21318. @@ -375,7 +393,10 @@
  21319. if (override_rlimit ||
  21320. atomic_read(&user->sigpending) <=
  21321. task_rlimit(t, RLIMIT_SIGPENDING)) {
  21322. - q = kmem_cache_alloc(sigqueue_cachep, flags);
  21323. + if (!fromslab)
  21324. + q = get_task_cache(t);
  21325. + if (!q)
  21326. + q = kmem_cache_alloc(sigqueue_cachep, flags);
  21327. } else {
  21328. print_dropped_signal(sig);
  21329. }
  21330. @@ -392,6 +413,13 @@
  21331. return q;
  21332. }
  21333. +static struct sigqueue *
  21334. +__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags,
  21335. + int override_rlimit)
  21336. +{
  21337. + return __sigqueue_do_alloc(sig, t, flags, override_rlimit, 0);
  21338. +}
  21339. +
  21340. static void __sigqueue_free(struct sigqueue *q)
  21341. {
  21342. if (q->flags & SIGQUEUE_PREALLOC)
  21343. @@ -401,6 +429,21 @@
  21344. kmem_cache_free(sigqueue_cachep, q);
  21345. }
  21346. +static void sigqueue_free_current(struct sigqueue *q)
  21347. +{
  21348. + struct user_struct *up;
  21349. +
  21350. + if (q->flags & SIGQUEUE_PREALLOC)
  21351. + return;
  21352. +
  21353. + up = q->user;
  21354. + if (rt_prio(current->normal_prio) && !put_task_cache(current, q)) {
  21355. + atomic_dec(&up->sigpending);
  21356. + free_uid(up);
  21357. + } else
  21358. + __sigqueue_free(q);
  21359. +}
  21360. +
  21361. void flush_sigqueue(struct sigpending *queue)
  21362. {
  21363. struct sigqueue *q;
  21364. @@ -414,6 +457,21 @@
  21365. }
  21366. /*
  21367. + * Called from __exit_signal. Flush tsk->pending and
  21368. + * tsk->sigqueue_cache
  21369. + */
  21370. +void flush_task_sigqueue(struct task_struct *tsk)
  21371. +{
  21372. + struct sigqueue *q;
  21373. +
  21374. + flush_sigqueue(&tsk->pending);
  21375. +
  21376. + q = get_task_cache(tsk);
  21377. + if (q)
  21378. + kmem_cache_free(sigqueue_cachep, q);
  21379. +}
  21380. +
  21381. +/*
  21382. * Flush all pending signals for this kthread.
  21383. */
  21384. void flush_signals(struct task_struct *t)
  21385. @@ -525,7 +583,7 @@
  21386. still_pending:
  21387. list_del_init(&first->list);
  21388. copy_siginfo(info, &first->info);
  21389. - __sigqueue_free(first);
  21390. + sigqueue_free_current(first);
  21391. } else {
  21392. /*
  21393. * Ok, it wasn't in the queue. This must be
  21394. @@ -560,6 +618,8 @@
  21395. {
  21396. int signr;
  21397. + WARN_ON_ONCE(tsk != current);
  21398. +
  21399. /* We only dequeue private signals from ourselves, we don't let
  21400. * signalfd steal them
  21401. */
  21402. @@ -1156,8 +1216,8 @@
  21403. * We don't want to have recursive SIGSEGV's etc, for example,
  21404. * that is why we also clear SIGNAL_UNKILLABLE.
  21405. */
  21406. -int
  21407. -force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  21408. +static int
  21409. +do_force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  21410. {
  21411. unsigned long int flags;
  21412. int ret, blocked, ignored;
  21413. @@ -1182,6 +1242,39 @@
  21414. return ret;
  21415. }
  21416. +int force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  21417. +{
  21418. +/*
  21419. + * On some archs, PREEMPT_RT has to delay sending a signal from a trap
  21420. + * since it can not enable preemption, and the signal code's spin_locks
  21421. + * turn into mutexes. Instead, it must set TIF_NOTIFY_RESUME which will
  21422. + * send the signal on exit of the trap.
  21423. + */
  21424. +#ifdef ARCH_RT_DELAYS_SIGNAL_SEND
  21425. + if (in_atomic()) {
  21426. + if (WARN_ON_ONCE(t != current))
  21427. + return 0;
  21428. + if (WARN_ON_ONCE(t->forced_info.si_signo))
  21429. + return 0;
  21430. +
  21431. + if (is_si_special(info)) {
  21432. + WARN_ON_ONCE(info != SEND_SIG_PRIV);
  21433. + t->forced_info.si_signo = sig;
  21434. + t->forced_info.si_errno = 0;
  21435. + t->forced_info.si_code = SI_KERNEL;
  21436. + t->forced_info.si_pid = 0;
  21437. + t->forced_info.si_uid = 0;
  21438. + } else {
  21439. + t->forced_info = *info;
  21440. + }
  21441. +
  21442. + set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
  21443. + return 0;
  21444. + }
  21445. +#endif
  21446. + return do_force_sig_info(sig, info, t);
  21447. +}
  21448. +
  21449. /*
  21450. * Nuke all other threads in the group.
  21451. */
  21452. @@ -1216,12 +1309,12 @@
  21453. * Disable interrupts early to avoid deadlocks.
  21454. * See rcu_read_unlock() comment header for details.
  21455. */
  21456. - local_irq_save(*flags);
  21457. + local_irq_save_nort(*flags);
  21458. rcu_read_lock();
  21459. sighand = rcu_dereference(tsk->sighand);
  21460. if (unlikely(sighand == NULL)) {
  21461. rcu_read_unlock();
  21462. - local_irq_restore(*flags);
  21463. + local_irq_restore_nort(*flags);
  21464. break;
  21465. }
  21466. /*
  21467. @@ -1242,7 +1335,7 @@
  21468. }
  21469. spin_unlock(&sighand->siglock);
  21470. rcu_read_unlock();
  21471. - local_irq_restore(*flags);
  21472. + local_irq_restore_nort(*flags);
  21473. }
  21474. return sighand;
  21475. @@ -1485,7 +1578,8 @@
  21476. */
  21477. struct sigqueue *sigqueue_alloc(void)
  21478. {
  21479. - struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
  21480. + /* Preallocated sigqueue objects always from the slabcache ! */
  21481. + struct sigqueue *q = __sigqueue_do_alloc(-1, current, GFP_KERNEL, 0, 1);
  21482. if (q)
  21483. q->flags |= SIGQUEUE_PREALLOC;
  21484. @@ -1846,15 +1940,7 @@
  21485. if (gstop_done && ptrace_reparented(current))
  21486. do_notify_parent_cldstop(current, false, why);
  21487. - /*
  21488. - * Don't want to allow preemption here, because
  21489. - * sys_ptrace() needs this task to be inactive.
  21490. - *
  21491. - * XXX: implement read_unlock_no_resched().
  21492. - */
  21493. - preempt_disable();
  21494. read_unlock(&tasklist_lock);
  21495. - preempt_enable_no_resched();
  21496. freezable_schedule();
  21497. } else {
  21498. /*
  21499. diff -Nur linux-4.4.28.orig/kernel/softirq.c linux-4.4.28/kernel/softirq.c
  21500. --- linux-4.4.28.orig/kernel/softirq.c 2016-10-28 09:53:25.000000000 +0200
  21501. +++ linux-4.4.28/kernel/softirq.c 2016-11-01 21:26:58.796343570 +0100
  21502. @@ -21,10 +21,12 @@
  21503. #include <linux/freezer.h>
  21504. #include <linux/kthread.h>
  21505. #include <linux/rcupdate.h>
  21506. +#include <linux/delay.h>
  21507. #include <linux/ftrace.h>
  21508. #include <linux/smp.h>
  21509. #include <linux/smpboot.h>
  21510. #include <linux/tick.h>
  21511. +#include <linux/locallock.h>
  21512. #include <linux/irq.h>
  21513. #define CREATE_TRACE_POINTS
  21514. @@ -56,12 +58,108 @@
  21515. static struct softirq_action softirq_vec[NR_SOFTIRQS] __cacheline_aligned_in_smp;
  21516. DEFINE_PER_CPU(struct task_struct *, ksoftirqd);
  21517. +#ifdef CONFIG_PREEMPT_RT_FULL
  21518. +#define TIMER_SOFTIRQS ((1 << TIMER_SOFTIRQ) | (1 << HRTIMER_SOFTIRQ))
  21519. +DEFINE_PER_CPU(struct task_struct *, ktimer_softirqd);
  21520. +#endif
  21521. const char * const softirq_to_name[NR_SOFTIRQS] = {
  21522. "HI", "TIMER", "NET_TX", "NET_RX", "BLOCK", "BLOCK_IOPOLL",
  21523. "TASKLET", "SCHED", "HRTIMER", "RCU"
  21524. };
  21525. +#ifdef CONFIG_NO_HZ_COMMON
  21526. +# ifdef CONFIG_PREEMPT_RT_FULL
  21527. +
  21528. +struct softirq_runner {
  21529. + struct task_struct *runner[NR_SOFTIRQS];
  21530. +};
  21531. +
  21532. +static DEFINE_PER_CPU(struct softirq_runner, softirq_runners);
  21533. +
  21534. +static inline void softirq_set_runner(unsigned int sirq)
  21535. +{
  21536. + struct softirq_runner *sr = this_cpu_ptr(&softirq_runners);
  21537. +
  21538. + sr->runner[sirq] = current;
  21539. +}
  21540. +
  21541. +static inline void softirq_clr_runner(unsigned int sirq)
  21542. +{
  21543. + struct softirq_runner *sr = this_cpu_ptr(&softirq_runners);
  21544. +
  21545. + sr->runner[sirq] = NULL;
  21546. +}
  21547. +
  21548. +/*
  21549. + * On preempt-rt a softirq running context might be blocked on a
  21550. + * lock. There might be no other runnable task on this CPU because the
  21551. + * lock owner runs on some other CPU. So we have to go into idle with
  21552. + * the pending bit set. Therefor we need to check this otherwise we
  21553. + * warn about false positives which confuses users and defeats the
  21554. + * whole purpose of this test.
  21555. + *
  21556. + * This code is called with interrupts disabled.
  21557. + */
  21558. +void softirq_check_pending_idle(void)
  21559. +{
  21560. + static int rate_limit;
  21561. + struct softirq_runner *sr = this_cpu_ptr(&softirq_runners);
  21562. + u32 warnpending;
  21563. + int i;
  21564. +
  21565. + if (rate_limit >= 10)
  21566. + return;
  21567. +
  21568. + warnpending = local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK;
  21569. + for (i = 0; i < NR_SOFTIRQS; i++) {
  21570. + struct task_struct *tsk = sr->runner[i];
  21571. +
  21572. + /*
  21573. + * The wakeup code in rtmutex.c wakes up the task
  21574. + * _before_ it sets pi_blocked_on to NULL under
  21575. + * tsk->pi_lock. So we need to check for both: state
  21576. + * and pi_blocked_on.
  21577. + */
  21578. + if (tsk) {
  21579. + raw_spin_lock(&tsk->pi_lock);
  21580. + if (tsk->pi_blocked_on || tsk->state == TASK_RUNNING) {
  21581. + /* Clear all bits pending in that task */
  21582. + warnpending &= ~(tsk->softirqs_raised);
  21583. + warnpending &= ~(1 << i);
  21584. + }
  21585. + raw_spin_unlock(&tsk->pi_lock);
  21586. + }
  21587. + }
  21588. +
  21589. + if (warnpending) {
  21590. + printk(KERN_ERR "NOHZ: local_softirq_pending %02x\n",
  21591. + warnpending);
  21592. + rate_limit++;
  21593. + }
  21594. +}
  21595. +# else
  21596. +/*
  21597. + * On !PREEMPT_RT we just printk rate limited:
  21598. + */
  21599. +void softirq_check_pending_idle(void)
  21600. +{
  21601. + static int rate_limit;
  21602. +
  21603. + if (rate_limit < 10 &&
  21604. + (local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK)) {
  21605. + printk(KERN_ERR "NOHZ: local_softirq_pending %02x\n",
  21606. + local_softirq_pending());
  21607. + rate_limit++;
  21608. + }
  21609. +}
  21610. +# endif
  21611. +
  21612. +#else /* !CONFIG_NO_HZ_COMMON */
  21613. +static inline void softirq_set_runner(unsigned int sirq) { }
  21614. +static inline void softirq_clr_runner(unsigned int sirq) { }
  21615. +#endif
  21616. +
  21617. /*
  21618. * we cannot loop indefinitely here to avoid userspace starvation,
  21619. * but we also don't want to introduce a worst case 1/HZ latency
  21620. @@ -77,6 +175,79 @@
  21621. wake_up_process(tsk);
  21622. }
  21623. +#ifdef CONFIG_PREEMPT_RT_FULL
  21624. +static void wakeup_timer_softirqd(void)
  21625. +{
  21626. + /* Interrupts are disabled: no need to stop preemption */
  21627. + struct task_struct *tsk = __this_cpu_read(ktimer_softirqd);
  21628. +
  21629. + if (tsk && tsk->state != TASK_RUNNING)
  21630. + wake_up_process(tsk);
  21631. +}
  21632. +#endif
  21633. +
  21634. +static void handle_softirq(unsigned int vec_nr)
  21635. +{
  21636. + struct softirq_action *h = softirq_vec + vec_nr;
  21637. + int prev_count;
  21638. +
  21639. + prev_count = preempt_count();
  21640. +
  21641. + kstat_incr_softirqs_this_cpu(vec_nr);
  21642. +
  21643. + trace_softirq_entry(vec_nr);
  21644. + h->action(h);
  21645. + trace_softirq_exit(vec_nr);
  21646. + if (unlikely(prev_count != preempt_count())) {
  21647. + pr_err("huh, entered softirq %u %s %p with preempt_count %08x, exited with %08x?\n",
  21648. + vec_nr, softirq_to_name[vec_nr], h->action,
  21649. + prev_count, preempt_count());
  21650. + preempt_count_set(prev_count);
  21651. + }
  21652. +}
  21653. +
  21654. +#ifndef CONFIG_PREEMPT_RT_FULL
  21655. +static inline int ksoftirqd_softirq_pending(void)
  21656. +{
  21657. + return local_softirq_pending();
  21658. +}
  21659. +
  21660. +static void handle_pending_softirqs(u32 pending)
  21661. +{
  21662. + struct softirq_action *h = softirq_vec;
  21663. + int softirq_bit;
  21664. +
  21665. + local_irq_enable();
  21666. +
  21667. + h = softirq_vec;
  21668. +
  21669. + while ((softirq_bit = ffs(pending))) {
  21670. + unsigned int vec_nr;
  21671. +
  21672. + h += softirq_bit - 1;
  21673. + vec_nr = h - softirq_vec;
  21674. + handle_softirq(vec_nr);
  21675. +
  21676. + h++;
  21677. + pending >>= softirq_bit;
  21678. + }
  21679. +
  21680. + rcu_bh_qs();
  21681. + local_irq_disable();
  21682. +}
  21683. +
  21684. +static void run_ksoftirqd(unsigned int cpu)
  21685. +{
  21686. + local_irq_disable();
  21687. + if (ksoftirqd_softirq_pending()) {
  21688. + __do_softirq();
  21689. + local_irq_enable();
  21690. + cond_resched_rcu_qs();
  21691. + return;
  21692. + }
  21693. + local_irq_enable();
  21694. +}
  21695. +
  21696. /*
  21697. * preempt_count and SOFTIRQ_OFFSET usage:
  21698. * - preempt_count is changed by SOFTIRQ_OFFSET on entering or leaving
  21699. @@ -116,9 +287,9 @@
  21700. if (preempt_count() == cnt) {
  21701. #ifdef CONFIG_DEBUG_PREEMPT
  21702. - current->preempt_disable_ip = get_parent_ip(CALLER_ADDR1);
  21703. + current->preempt_disable_ip = get_lock_parent_ip();
  21704. #endif
  21705. - trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
  21706. + trace_preempt_off(CALLER_ADDR0, get_lock_parent_ip());
  21707. }
  21708. }
  21709. EXPORT_SYMBOL(__local_bh_disable_ip);
  21710. @@ -232,10 +403,8 @@
  21711. unsigned long end = jiffies + MAX_SOFTIRQ_TIME;
  21712. unsigned long old_flags = current->flags;
  21713. int max_restart = MAX_SOFTIRQ_RESTART;
  21714. - struct softirq_action *h;
  21715. bool in_hardirq;
  21716. __u32 pending;
  21717. - int softirq_bit;
  21718. /*
  21719. * Mask out PF_MEMALLOC s current task context is borrowed for the
  21720. @@ -254,36 +423,7 @@
  21721. /* Reset the pending bitmask before enabling irqs */
  21722. set_softirq_pending(0);
  21723. - local_irq_enable();
  21724. -
  21725. - h = softirq_vec;
  21726. -
  21727. - while ((softirq_bit = ffs(pending))) {
  21728. - unsigned int vec_nr;
  21729. - int prev_count;
  21730. -
  21731. - h += softirq_bit - 1;
  21732. -
  21733. - vec_nr = h - softirq_vec;
  21734. - prev_count = preempt_count();
  21735. -
  21736. - kstat_incr_softirqs_this_cpu(vec_nr);
  21737. -
  21738. - trace_softirq_entry(vec_nr);
  21739. - h->action(h);
  21740. - trace_softirq_exit(vec_nr);
  21741. - if (unlikely(prev_count != preempt_count())) {
  21742. - pr_err("huh, entered softirq %u %s %p with preempt_count %08x, exited with %08x?\n",
  21743. - vec_nr, softirq_to_name[vec_nr], h->action,
  21744. - prev_count, preempt_count());
  21745. - preempt_count_set(prev_count);
  21746. - }
  21747. - h++;
  21748. - pending >>= softirq_bit;
  21749. - }
  21750. -
  21751. - rcu_bh_qs();
  21752. - local_irq_disable();
  21753. + handle_pending_softirqs(pending);
  21754. pending = local_softirq_pending();
  21755. if (pending) {
  21756. @@ -320,6 +460,310 @@
  21757. }
  21758. /*
  21759. + * This function must run with irqs disabled!
  21760. + */
  21761. +void raise_softirq_irqoff(unsigned int nr)
  21762. +{
  21763. + __raise_softirq_irqoff(nr);
  21764. +
  21765. + /*
  21766. + * If we're in an interrupt or softirq, we're done
  21767. + * (this also catches softirq-disabled code). We will
  21768. + * actually run the softirq once we return from
  21769. + * the irq or softirq.
  21770. + *
  21771. + * Otherwise we wake up ksoftirqd to make sure we
  21772. + * schedule the softirq soon.
  21773. + */
  21774. + if (!in_interrupt())
  21775. + wakeup_softirqd();
  21776. +}
  21777. +
  21778. +void __raise_softirq_irqoff(unsigned int nr)
  21779. +{
  21780. + trace_softirq_raise(nr);
  21781. + or_softirq_pending(1UL << nr);
  21782. +}
  21783. +
  21784. +static inline void local_bh_disable_nort(void) { local_bh_disable(); }
  21785. +static inline void _local_bh_enable_nort(void) { _local_bh_enable(); }
  21786. +static void ksoftirqd_set_sched_params(unsigned int cpu) { }
  21787. +
  21788. +#else /* !PREEMPT_RT_FULL */
  21789. +
  21790. +/*
  21791. + * On RT we serialize softirq execution with a cpu local lock per softirq
  21792. + */
  21793. +static DEFINE_PER_CPU(struct local_irq_lock [NR_SOFTIRQS], local_softirq_locks);
  21794. +
  21795. +void __init softirq_early_init(void)
  21796. +{
  21797. + int i;
  21798. +
  21799. + for (i = 0; i < NR_SOFTIRQS; i++)
  21800. + local_irq_lock_init(local_softirq_locks[i]);
  21801. +}
  21802. +
  21803. +static void lock_softirq(int which)
  21804. +{
  21805. + local_lock(local_softirq_locks[which]);
  21806. +}
  21807. +
  21808. +static void unlock_softirq(int which)
  21809. +{
  21810. + local_unlock(local_softirq_locks[which]);
  21811. +}
  21812. +
  21813. +static void do_single_softirq(int which)
  21814. +{
  21815. + unsigned long old_flags = current->flags;
  21816. +
  21817. + current->flags &= ~PF_MEMALLOC;
  21818. + vtime_account_irq_enter(current);
  21819. + current->flags |= PF_IN_SOFTIRQ;
  21820. + lockdep_softirq_enter();
  21821. + local_irq_enable();
  21822. + handle_softirq(which);
  21823. + local_irq_disable();
  21824. + lockdep_softirq_exit();
  21825. + current->flags &= ~PF_IN_SOFTIRQ;
  21826. + vtime_account_irq_enter(current);
  21827. + tsk_restore_flags(current, old_flags, PF_MEMALLOC);
  21828. +}
  21829. +
  21830. +/*
  21831. + * Called with interrupts disabled. Process softirqs which were raised
  21832. + * in current context (or on behalf of ksoftirqd).
  21833. + */
  21834. +static void do_current_softirqs(void)
  21835. +{
  21836. + while (current->softirqs_raised) {
  21837. + int i = __ffs(current->softirqs_raised);
  21838. + unsigned int pending, mask = (1U << i);
  21839. +
  21840. + current->softirqs_raised &= ~mask;
  21841. + local_irq_enable();
  21842. +
  21843. + /*
  21844. + * If the lock is contended, we boost the owner to
  21845. + * process the softirq or leave the critical section
  21846. + * now.
  21847. + */
  21848. + lock_softirq(i);
  21849. + local_irq_disable();
  21850. + softirq_set_runner(i);
  21851. + /*
  21852. + * Check with the local_softirq_pending() bits,
  21853. + * whether we need to process this still or if someone
  21854. + * else took care of it.
  21855. + */
  21856. + pending = local_softirq_pending();
  21857. + if (pending & mask) {
  21858. + set_softirq_pending(pending & ~mask);
  21859. + do_single_softirq(i);
  21860. + }
  21861. + softirq_clr_runner(i);
  21862. + WARN_ON(current->softirq_nestcnt != 1);
  21863. + local_irq_enable();
  21864. + unlock_softirq(i);
  21865. + local_irq_disable();
  21866. + }
  21867. +}
  21868. +
  21869. +void __local_bh_disable(void)
  21870. +{
  21871. + if (++current->softirq_nestcnt == 1)
  21872. + migrate_disable();
  21873. +}
  21874. +EXPORT_SYMBOL(__local_bh_disable);
  21875. +
  21876. +void __local_bh_enable(void)
  21877. +{
  21878. + if (WARN_ON(current->softirq_nestcnt == 0))
  21879. + return;
  21880. +
  21881. + local_irq_disable();
  21882. + if (current->softirq_nestcnt == 1 && current->softirqs_raised)
  21883. + do_current_softirqs();
  21884. + local_irq_enable();
  21885. +
  21886. + if (--current->softirq_nestcnt == 0)
  21887. + migrate_enable();
  21888. +}
  21889. +EXPORT_SYMBOL(__local_bh_enable);
  21890. +
  21891. +void _local_bh_enable(void)
  21892. +{
  21893. + if (WARN_ON(current->softirq_nestcnt == 0))
  21894. + return;
  21895. + if (--current->softirq_nestcnt == 0)
  21896. + migrate_enable();
  21897. +}
  21898. +EXPORT_SYMBOL(_local_bh_enable);
  21899. +
  21900. +int in_serving_softirq(void)
  21901. +{
  21902. + return current->flags & PF_IN_SOFTIRQ;
  21903. +}
  21904. +EXPORT_SYMBOL(in_serving_softirq);
  21905. +
  21906. +/* Called with preemption disabled */
  21907. +static void run_ksoftirqd(unsigned int cpu)
  21908. +{
  21909. + local_irq_disable();
  21910. + current->softirq_nestcnt++;
  21911. +
  21912. + do_current_softirqs();
  21913. + current->softirq_nestcnt--;
  21914. + local_irq_enable();
  21915. + cond_resched_rcu_qs();
  21916. +}
  21917. +
  21918. +/*
  21919. + * Called from netif_rx_ni(). Preemption enabled, but migration
  21920. + * disabled. So the cpu can't go away under us.
  21921. + */
  21922. +void thread_do_softirq(void)
  21923. +{
  21924. + if (!in_serving_softirq() && current->softirqs_raised) {
  21925. + current->softirq_nestcnt++;
  21926. + do_current_softirqs();
  21927. + current->softirq_nestcnt--;
  21928. + }
  21929. +}
  21930. +
  21931. +static void do_raise_softirq_irqoff(unsigned int nr)
  21932. +{
  21933. + unsigned int mask;
  21934. +
  21935. + mask = 1UL << nr;
  21936. +
  21937. + trace_softirq_raise(nr);
  21938. + or_softirq_pending(mask);
  21939. +
  21940. + /*
  21941. + * If we are not in a hard interrupt and inside a bh disabled
  21942. + * region, we simply raise the flag on current. local_bh_enable()
  21943. + * will make sure that the softirq is executed. Otherwise we
  21944. + * delegate it to ksoftirqd.
  21945. + */
  21946. + if (!in_irq() && current->softirq_nestcnt)
  21947. + current->softirqs_raised |= mask;
  21948. + else if (!__this_cpu_read(ksoftirqd) || !__this_cpu_read(ktimer_softirqd))
  21949. + return;
  21950. +
  21951. + if (mask & TIMER_SOFTIRQS)
  21952. + __this_cpu_read(ktimer_softirqd)->softirqs_raised |= mask;
  21953. + else
  21954. + __this_cpu_read(ksoftirqd)->softirqs_raised |= mask;
  21955. +}
  21956. +
  21957. +static void wakeup_proper_softirq(unsigned int nr)
  21958. +{
  21959. + if ((1UL << nr) & TIMER_SOFTIRQS)
  21960. + wakeup_timer_softirqd();
  21961. + else
  21962. + wakeup_softirqd();
  21963. +}
  21964. +
  21965. +
  21966. +void __raise_softirq_irqoff(unsigned int nr)
  21967. +{
  21968. + do_raise_softirq_irqoff(nr);
  21969. + if (!in_irq() && !current->softirq_nestcnt)
  21970. + wakeup_proper_softirq(nr);
  21971. +}
  21972. +
  21973. +/*
  21974. + * Same as __raise_softirq_irqoff() but will process them in ksoftirqd
  21975. + */
  21976. +void __raise_softirq_irqoff_ksoft(unsigned int nr)
  21977. +{
  21978. + unsigned int mask;
  21979. +
  21980. + if (WARN_ON_ONCE(!__this_cpu_read(ksoftirqd) ||
  21981. + !__this_cpu_read(ktimer_softirqd)))
  21982. + return;
  21983. + mask = 1UL << nr;
  21984. +
  21985. + trace_softirq_raise(nr);
  21986. + or_softirq_pending(mask);
  21987. + if (mask & TIMER_SOFTIRQS)
  21988. + __this_cpu_read(ktimer_softirqd)->softirqs_raised |= mask;
  21989. + else
  21990. + __this_cpu_read(ksoftirqd)->softirqs_raised |= mask;
  21991. + wakeup_proper_softirq(nr);
  21992. +}
  21993. +
  21994. +/*
  21995. + * This function must run with irqs disabled!
  21996. + */
  21997. +void raise_softirq_irqoff(unsigned int nr)
  21998. +{
  21999. + do_raise_softirq_irqoff(nr);
  22000. +
  22001. + /*
  22002. + * If we're in an hard interrupt we let irq return code deal
  22003. + * with the wakeup of ksoftirqd.
  22004. + */
  22005. + if (in_irq())
  22006. + return;
  22007. + /*
  22008. + * If we are in thread context but outside of a bh disabled
  22009. + * region, we need to wake ksoftirqd as well.
  22010. + *
  22011. + * CHECKME: Some of the places which do that could be wrapped
  22012. + * into local_bh_disable/enable pairs. Though it's unclear
  22013. + * whether this is worth the effort. To find those places just
  22014. + * raise a WARN() if the condition is met.
  22015. + */
  22016. + if (!current->softirq_nestcnt)
  22017. + wakeup_proper_softirq(nr);
  22018. +}
  22019. +
  22020. +static inline int ksoftirqd_softirq_pending(void)
  22021. +{
  22022. + return current->softirqs_raised;
  22023. +}
  22024. +
  22025. +static inline void local_bh_disable_nort(void) { }
  22026. +static inline void _local_bh_enable_nort(void) { }
  22027. +
  22028. +static inline void ksoftirqd_set_sched_params(unsigned int cpu)
  22029. +{
  22030. + /* Take over all but timer pending softirqs when starting */
  22031. + local_irq_disable();
  22032. + current->softirqs_raised = local_softirq_pending() & ~TIMER_SOFTIRQS;
  22033. + local_irq_enable();
  22034. +}
  22035. +
  22036. +static inline void ktimer_softirqd_set_sched_params(unsigned int cpu)
  22037. +{
  22038. + struct sched_param param = { .sched_priority = 1 };
  22039. +
  22040. + sched_setscheduler(current, SCHED_FIFO, &param);
  22041. +
  22042. + /* Take over timer pending softirqs when starting */
  22043. + local_irq_disable();
  22044. + current->softirqs_raised = local_softirq_pending() & TIMER_SOFTIRQS;
  22045. + local_irq_enable();
  22046. +}
  22047. +
  22048. +static inline void ktimer_softirqd_clr_sched_params(unsigned int cpu,
  22049. + bool online)
  22050. +{
  22051. + struct sched_param param = { .sched_priority = 0 };
  22052. +
  22053. + sched_setscheduler(current, SCHED_NORMAL, &param);
  22054. +}
  22055. +
  22056. +static int ktimer_softirqd_should_run(unsigned int cpu)
  22057. +{
  22058. + return current->softirqs_raised;
  22059. +}
  22060. +
  22061. +#endif /* PREEMPT_RT_FULL */
  22062. +/*
  22063. * Enter an interrupt context.
  22064. */
  22065. void irq_enter(void)
  22066. @@ -330,9 +774,9 @@
  22067. * Prevent raise_softirq from needlessly waking up ksoftirqd
  22068. * here, as softirq will be serviced on return from interrupt.
  22069. */
  22070. - local_bh_disable();
  22071. + local_bh_disable_nort();
  22072. tick_irq_enter();
  22073. - _local_bh_enable();
  22074. + _local_bh_enable_nort();
  22075. }
  22076. __irq_enter();
  22077. @@ -340,6 +784,7 @@
  22078. static inline void invoke_softirq(void)
  22079. {
  22080. +#ifndef CONFIG_PREEMPT_RT_FULL
  22081. if (!force_irqthreads) {
  22082. #ifdef CONFIG_HAVE_IRQ_EXIT_ON_IRQ_STACK
  22083. /*
  22084. @@ -359,6 +804,18 @@
  22085. } else {
  22086. wakeup_softirqd();
  22087. }
  22088. +#else /* PREEMPT_RT_FULL */
  22089. + unsigned long flags;
  22090. +
  22091. + local_irq_save(flags);
  22092. + if (__this_cpu_read(ksoftirqd) &&
  22093. + __this_cpu_read(ksoftirqd)->softirqs_raised)
  22094. + wakeup_softirqd();
  22095. + if (__this_cpu_read(ktimer_softirqd) &&
  22096. + __this_cpu_read(ktimer_softirqd)->softirqs_raised)
  22097. + wakeup_timer_softirqd();
  22098. + local_irq_restore(flags);
  22099. +#endif
  22100. }
  22101. static inline void tick_irq_exit(void)
  22102. @@ -395,26 +852,6 @@
  22103. trace_hardirq_exit(); /* must be last! */
  22104. }
  22105. -/*
  22106. - * This function must run with irqs disabled!
  22107. - */
  22108. -inline void raise_softirq_irqoff(unsigned int nr)
  22109. -{
  22110. - __raise_softirq_irqoff(nr);
  22111. -
  22112. - /*
  22113. - * If we're in an interrupt or softirq, we're done
  22114. - * (this also catches softirq-disabled code). We will
  22115. - * actually run the softirq once we return from
  22116. - * the irq or softirq.
  22117. - *
  22118. - * Otherwise we wake up ksoftirqd to make sure we
  22119. - * schedule the softirq soon.
  22120. - */
  22121. - if (!in_interrupt())
  22122. - wakeup_softirqd();
  22123. -}
  22124. -
  22125. void raise_softirq(unsigned int nr)
  22126. {
  22127. unsigned long flags;
  22128. @@ -424,12 +861,6 @@
  22129. local_irq_restore(flags);
  22130. }
  22131. -void __raise_softirq_irqoff(unsigned int nr)
  22132. -{
  22133. - trace_softirq_raise(nr);
  22134. - or_softirq_pending(1UL << nr);
  22135. -}
  22136. -
  22137. void open_softirq(int nr, void (*action)(struct softirq_action *))
  22138. {
  22139. softirq_vec[nr].action = action;
  22140. @@ -446,15 +877,45 @@
  22141. static DEFINE_PER_CPU(struct tasklet_head, tasklet_vec);
  22142. static DEFINE_PER_CPU(struct tasklet_head, tasklet_hi_vec);
  22143. +static void inline
  22144. +__tasklet_common_schedule(struct tasklet_struct *t, struct tasklet_head *head, unsigned int nr)
  22145. +{
  22146. + if (tasklet_trylock(t)) {
  22147. +again:
  22148. + /* We may have been preempted before tasklet_trylock
  22149. + * and __tasklet_action may have already run.
  22150. + * So double check the sched bit while the takslet
  22151. + * is locked before adding it to the list.
  22152. + */
  22153. + if (test_bit(TASKLET_STATE_SCHED, &t->state)) {
  22154. + t->next = NULL;
  22155. + *head->tail = t;
  22156. + head->tail = &(t->next);
  22157. + raise_softirq_irqoff(nr);
  22158. + tasklet_unlock(t);
  22159. + } else {
  22160. + /* This is subtle. If we hit the corner case above
  22161. + * It is possible that we get preempted right here,
  22162. + * and another task has successfully called
  22163. + * tasklet_schedule(), then this function, and
  22164. + * failed on the trylock. Thus we must be sure
  22165. + * before releasing the tasklet lock, that the
  22166. + * SCHED_BIT is clear. Otherwise the tasklet
  22167. + * may get its SCHED_BIT set, but not added to the
  22168. + * list
  22169. + */
  22170. + if (!tasklet_tryunlock(t))
  22171. + goto again;
  22172. + }
  22173. + }
  22174. +}
  22175. +
  22176. void __tasklet_schedule(struct tasklet_struct *t)
  22177. {
  22178. unsigned long flags;
  22179. local_irq_save(flags);
  22180. - t->next = NULL;
  22181. - *__this_cpu_read(tasklet_vec.tail) = t;
  22182. - __this_cpu_write(tasklet_vec.tail, &(t->next));
  22183. - raise_softirq_irqoff(TASKLET_SOFTIRQ);
  22184. + __tasklet_common_schedule(t, this_cpu_ptr(&tasklet_vec), TASKLET_SOFTIRQ);
  22185. local_irq_restore(flags);
  22186. }
  22187. EXPORT_SYMBOL(__tasklet_schedule);
  22188. @@ -464,10 +925,7 @@
  22189. unsigned long flags;
  22190. local_irq_save(flags);
  22191. - t->next = NULL;
  22192. - *__this_cpu_read(tasklet_hi_vec.tail) = t;
  22193. - __this_cpu_write(tasklet_hi_vec.tail, &(t->next));
  22194. - raise_softirq_irqoff(HI_SOFTIRQ);
  22195. + __tasklet_common_schedule(t, this_cpu_ptr(&tasklet_hi_vec), HI_SOFTIRQ);
  22196. local_irq_restore(flags);
  22197. }
  22198. EXPORT_SYMBOL(__tasklet_hi_schedule);
  22199. @@ -476,82 +934,122 @@
  22200. {
  22201. BUG_ON(!irqs_disabled());
  22202. - t->next = __this_cpu_read(tasklet_hi_vec.head);
  22203. - __this_cpu_write(tasklet_hi_vec.head, t);
  22204. - __raise_softirq_irqoff(HI_SOFTIRQ);
  22205. + __tasklet_hi_schedule(t);
  22206. }
  22207. EXPORT_SYMBOL(__tasklet_hi_schedule_first);
  22208. -static void tasklet_action(struct softirq_action *a)
  22209. +void tasklet_enable(struct tasklet_struct *t)
  22210. {
  22211. - struct tasklet_struct *list;
  22212. + if (!atomic_dec_and_test(&t->count))
  22213. + return;
  22214. + if (test_and_clear_bit(TASKLET_STATE_PENDING, &t->state))
  22215. + tasklet_schedule(t);
  22216. +}
  22217. +EXPORT_SYMBOL(tasklet_enable);
  22218. - local_irq_disable();
  22219. - list = __this_cpu_read(tasklet_vec.head);
  22220. - __this_cpu_write(tasklet_vec.head, NULL);
  22221. - __this_cpu_write(tasklet_vec.tail, this_cpu_ptr(&tasklet_vec.head));
  22222. - local_irq_enable();
  22223. +static void __tasklet_action(struct softirq_action *a,
  22224. + struct tasklet_struct *list)
  22225. +{
  22226. + int loops = 1000000;
  22227. while (list) {
  22228. struct tasklet_struct *t = list;
  22229. list = list->next;
  22230. - if (tasklet_trylock(t)) {
  22231. - if (!atomic_read(&t->count)) {
  22232. - if (!test_and_clear_bit(TASKLET_STATE_SCHED,
  22233. - &t->state))
  22234. - BUG();
  22235. - t->func(t->data);
  22236. - tasklet_unlock(t);
  22237. - continue;
  22238. - }
  22239. - tasklet_unlock(t);
  22240. + /*
  22241. + * Should always succeed - after a tasklist got on the
  22242. + * list (after getting the SCHED bit set from 0 to 1),
  22243. + * nothing but the tasklet softirq it got queued to can
  22244. + * lock it:
  22245. + */
  22246. + if (!tasklet_trylock(t)) {
  22247. + WARN_ON(1);
  22248. + continue;
  22249. }
  22250. - local_irq_disable();
  22251. t->next = NULL;
  22252. - *__this_cpu_read(tasklet_vec.tail) = t;
  22253. - __this_cpu_write(tasklet_vec.tail, &(t->next));
  22254. - __raise_softirq_irqoff(TASKLET_SOFTIRQ);
  22255. - local_irq_enable();
  22256. +
  22257. + /*
  22258. + * If we cannot handle the tasklet because it's disabled,
  22259. + * mark it as pending. tasklet_enable() will later
  22260. + * re-schedule the tasklet.
  22261. + */
  22262. + if (unlikely(atomic_read(&t->count))) {
  22263. +out_disabled:
  22264. + /* implicit unlock: */
  22265. + wmb();
  22266. + t->state = TASKLET_STATEF_PENDING;
  22267. + continue;
  22268. + }
  22269. +
  22270. + /*
  22271. + * After this point on the tasklet might be rescheduled
  22272. + * on another CPU, but it can only be added to another
  22273. + * CPU's tasklet list if we unlock the tasklet (which we
  22274. + * dont do yet).
  22275. + */
  22276. + if (!test_and_clear_bit(TASKLET_STATE_SCHED, &t->state))
  22277. + WARN_ON(1);
  22278. +
  22279. +again:
  22280. + t->func(t->data);
  22281. +
  22282. + /*
  22283. + * Try to unlock the tasklet. We must use cmpxchg, because
  22284. + * another CPU might have scheduled or disabled the tasklet.
  22285. + * We only allow the STATE_RUN -> 0 transition here.
  22286. + */
  22287. + while (!tasklet_tryunlock(t)) {
  22288. + /*
  22289. + * If it got disabled meanwhile, bail out:
  22290. + */
  22291. + if (atomic_read(&t->count))
  22292. + goto out_disabled;
  22293. + /*
  22294. + * If it got scheduled meanwhile, re-execute
  22295. + * the tasklet function:
  22296. + */
  22297. + if (test_and_clear_bit(TASKLET_STATE_SCHED, &t->state))
  22298. + goto again;
  22299. + if (!--loops) {
  22300. + printk("hm, tasklet state: %08lx\n", t->state);
  22301. + WARN_ON(1);
  22302. + tasklet_unlock(t);
  22303. + break;
  22304. + }
  22305. + }
  22306. }
  22307. }
  22308. +static void tasklet_action(struct softirq_action *a)
  22309. +{
  22310. + struct tasklet_struct *list;
  22311. +
  22312. + local_irq_disable();
  22313. +
  22314. + list = __this_cpu_read(tasklet_vec.head);
  22315. + __this_cpu_write(tasklet_vec.head, NULL);
  22316. + __this_cpu_write(tasklet_vec.tail, this_cpu_ptr(&tasklet_vec.head));
  22317. +
  22318. + local_irq_enable();
  22319. +
  22320. + __tasklet_action(a, list);
  22321. +}
  22322. +
  22323. static void tasklet_hi_action(struct softirq_action *a)
  22324. {
  22325. struct tasklet_struct *list;
  22326. local_irq_disable();
  22327. +
  22328. list = __this_cpu_read(tasklet_hi_vec.head);
  22329. __this_cpu_write(tasklet_hi_vec.head, NULL);
  22330. __this_cpu_write(tasklet_hi_vec.tail, this_cpu_ptr(&tasklet_hi_vec.head));
  22331. - local_irq_enable();
  22332. - while (list) {
  22333. - struct tasklet_struct *t = list;
  22334. -
  22335. - list = list->next;
  22336. -
  22337. - if (tasklet_trylock(t)) {
  22338. - if (!atomic_read(&t->count)) {
  22339. - if (!test_and_clear_bit(TASKLET_STATE_SCHED,
  22340. - &t->state))
  22341. - BUG();
  22342. - t->func(t->data);
  22343. - tasklet_unlock(t);
  22344. - continue;
  22345. - }
  22346. - tasklet_unlock(t);
  22347. - }
  22348. + local_irq_enable();
  22349. - local_irq_disable();
  22350. - t->next = NULL;
  22351. - *__this_cpu_read(tasklet_hi_vec.tail) = t;
  22352. - __this_cpu_write(tasklet_hi_vec.tail, &(t->next));
  22353. - __raise_softirq_irqoff(HI_SOFTIRQ);
  22354. - local_irq_enable();
  22355. - }
  22356. + __tasklet_action(a, list);
  22357. }
  22358. void tasklet_init(struct tasklet_struct *t,
  22359. @@ -572,7 +1070,7 @@
  22360. while (test_and_set_bit(TASKLET_STATE_SCHED, &t->state)) {
  22361. do {
  22362. - yield();
  22363. + msleep(1);
  22364. } while (test_bit(TASKLET_STATE_SCHED, &t->state));
  22365. }
  22366. tasklet_unlock_wait(t);
  22367. @@ -646,25 +1144,26 @@
  22368. open_softirq(HI_SOFTIRQ, tasklet_hi_action);
  22369. }
  22370. -static int ksoftirqd_should_run(unsigned int cpu)
  22371. -{
  22372. - return local_softirq_pending();
  22373. -}
  22374. -
  22375. -static void run_ksoftirqd(unsigned int cpu)
  22376. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  22377. +void tasklet_unlock_wait(struct tasklet_struct *t)
  22378. {
  22379. - local_irq_disable();
  22380. - if (local_softirq_pending()) {
  22381. + while (test_bit(TASKLET_STATE_RUN, &(t)->state)) {
  22382. /*
  22383. - * We can safely run softirq on inline stack, as we are not deep
  22384. - * in the task stack here.
  22385. + * Hack for now to avoid this busy-loop:
  22386. */
  22387. - __do_softirq();
  22388. - local_irq_enable();
  22389. - cond_resched_rcu_qs();
  22390. - return;
  22391. +#ifdef CONFIG_PREEMPT_RT_FULL
  22392. + msleep(1);
  22393. +#else
  22394. + barrier();
  22395. +#endif
  22396. }
  22397. - local_irq_enable();
  22398. +}
  22399. +EXPORT_SYMBOL(tasklet_unlock_wait);
  22400. +#endif
  22401. +
  22402. +static int ksoftirqd_should_run(unsigned int cpu)
  22403. +{
  22404. + return ksoftirqd_softirq_pending();
  22405. }
  22406. #ifdef CONFIG_HOTPLUG_CPU
  22407. @@ -746,16 +1245,31 @@
  22408. static struct smp_hotplug_thread softirq_threads = {
  22409. .store = &ksoftirqd,
  22410. + .setup = ksoftirqd_set_sched_params,
  22411. .thread_should_run = ksoftirqd_should_run,
  22412. .thread_fn = run_ksoftirqd,
  22413. .thread_comm = "ksoftirqd/%u",
  22414. };
  22415. +#ifdef CONFIG_PREEMPT_RT_FULL
  22416. +static struct smp_hotplug_thread softirq_timer_threads = {
  22417. + .store = &ktimer_softirqd,
  22418. + .setup = ktimer_softirqd_set_sched_params,
  22419. + .cleanup = ktimer_softirqd_clr_sched_params,
  22420. + .thread_should_run = ktimer_softirqd_should_run,
  22421. + .thread_fn = run_ksoftirqd,
  22422. + .thread_comm = "ktimersoftd/%u",
  22423. +};
  22424. +#endif
  22425. +
  22426. static __init int spawn_ksoftirqd(void)
  22427. {
  22428. register_cpu_notifier(&cpu_nfb);
  22429. BUG_ON(smpboot_register_percpu_thread(&softirq_threads));
  22430. +#ifdef CONFIG_PREEMPT_RT_FULL
  22431. + BUG_ON(smpboot_register_percpu_thread(&softirq_timer_threads));
  22432. +#endif
  22433. return 0;
  22434. }
  22435. diff -Nur linux-4.4.28.orig/kernel/stop_machine.c linux-4.4.28/kernel/stop_machine.c
  22436. --- linux-4.4.28.orig/kernel/stop_machine.c 2016-10-28 09:53:25.000000000 +0200
  22437. +++ linux-4.4.28/kernel/stop_machine.c 2016-11-01 21:26:58.796343570 +0100
  22438. @@ -37,7 +37,7 @@
  22439. struct cpu_stopper {
  22440. struct task_struct *thread;
  22441. - spinlock_t lock;
  22442. + raw_spinlock_t lock;
  22443. bool enabled; /* is this stopper enabled? */
  22444. struct list_head works; /* list of pending works */
  22445. @@ -86,12 +86,12 @@
  22446. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  22447. unsigned long flags;
  22448. - spin_lock_irqsave(&stopper->lock, flags);
  22449. + raw_spin_lock_irqsave(&stopper->lock, flags);
  22450. if (stopper->enabled)
  22451. __cpu_stop_queue_work(stopper, work);
  22452. else
  22453. cpu_stop_signal_done(work->done, false);
  22454. - spin_unlock_irqrestore(&stopper->lock, flags);
  22455. + raw_spin_unlock_irqrestore(&stopper->lock, flags);
  22456. }
  22457. /**
  22458. @@ -224,8 +224,8 @@
  22459. int err;
  22460. lg_double_lock(&stop_cpus_lock, cpu1, cpu2);
  22461. - spin_lock_irq(&stopper1->lock);
  22462. - spin_lock_nested(&stopper2->lock, SINGLE_DEPTH_NESTING);
  22463. + raw_spin_lock_irq(&stopper1->lock);
  22464. + raw_spin_lock_nested(&stopper2->lock, SINGLE_DEPTH_NESTING);
  22465. err = -ENOENT;
  22466. if (!stopper1->enabled || !stopper2->enabled)
  22467. @@ -235,8 +235,8 @@
  22468. __cpu_stop_queue_work(stopper1, work1);
  22469. __cpu_stop_queue_work(stopper2, work2);
  22470. unlock:
  22471. - spin_unlock(&stopper2->lock);
  22472. - spin_unlock_irq(&stopper1->lock);
  22473. + raw_spin_unlock(&stopper2->lock);
  22474. + raw_spin_unlock_irq(&stopper1->lock);
  22475. lg_double_unlock(&stop_cpus_lock, cpu1, cpu2);
  22476. return err;
  22477. @@ -258,7 +258,7 @@
  22478. struct cpu_stop_work work1, work2;
  22479. struct multi_stop_data msdata;
  22480. - preempt_disable();
  22481. + preempt_disable_nort();
  22482. msdata = (struct multi_stop_data){
  22483. .fn = fn,
  22484. .data = arg,
  22485. @@ -278,11 +278,11 @@
  22486. if (cpu1 > cpu2)
  22487. swap(cpu1, cpu2);
  22488. if (cpu_stop_queue_two_works(cpu1, &work1, cpu2, &work2)) {
  22489. - preempt_enable();
  22490. + preempt_enable_nort();
  22491. return -ENOENT;
  22492. }
  22493. - preempt_enable();
  22494. + preempt_enable_nort();
  22495. wait_for_completion(&done.completion);
  22496. @@ -315,17 +315,20 @@
  22497. static void queue_stop_cpus_work(const struct cpumask *cpumask,
  22498. cpu_stop_fn_t fn, void *arg,
  22499. - struct cpu_stop_done *done)
  22500. + struct cpu_stop_done *done, bool inactive)
  22501. {
  22502. struct cpu_stop_work *work;
  22503. unsigned int cpu;
  22504. /*
  22505. - * Disable preemption while queueing to avoid getting
  22506. - * preempted by a stopper which might wait for other stoppers
  22507. - * to enter @fn which can lead to deadlock.
  22508. + * Make sure that all work is queued on all cpus before
  22509. + * any of the cpus can execute it.
  22510. */
  22511. - lg_global_lock(&stop_cpus_lock);
  22512. + if (!inactive)
  22513. + lg_global_lock(&stop_cpus_lock);
  22514. + else
  22515. + lg_global_trylock_relax(&stop_cpus_lock);
  22516. +
  22517. for_each_cpu(cpu, cpumask) {
  22518. work = &per_cpu(cpu_stopper.stop_work, cpu);
  22519. work->fn = fn;
  22520. @@ -342,7 +345,7 @@
  22521. struct cpu_stop_done done;
  22522. cpu_stop_init_done(&done, cpumask_weight(cpumask));
  22523. - queue_stop_cpus_work(cpumask, fn, arg, &done);
  22524. + queue_stop_cpus_work(cpumask, fn, arg, &done, false);
  22525. wait_for_completion(&done.completion);
  22526. return done.executed ? done.ret : -ENOENT;
  22527. }
  22528. @@ -422,9 +425,9 @@
  22529. unsigned long flags;
  22530. int run;
  22531. - spin_lock_irqsave(&stopper->lock, flags);
  22532. + raw_spin_lock_irqsave(&stopper->lock, flags);
  22533. run = !list_empty(&stopper->works);
  22534. - spin_unlock_irqrestore(&stopper->lock, flags);
  22535. + raw_spin_unlock_irqrestore(&stopper->lock, flags);
  22536. return run;
  22537. }
  22538. @@ -436,13 +439,13 @@
  22539. repeat:
  22540. work = NULL;
  22541. - spin_lock_irq(&stopper->lock);
  22542. + raw_spin_lock_irq(&stopper->lock);
  22543. if (!list_empty(&stopper->works)) {
  22544. work = list_first_entry(&stopper->works,
  22545. struct cpu_stop_work, list);
  22546. list_del_init(&work->list);
  22547. }
  22548. - spin_unlock_irq(&stopper->lock);
  22549. + raw_spin_unlock_irq(&stopper->lock);
  22550. if (work) {
  22551. cpu_stop_fn_t fn = work->fn;
  22552. @@ -450,6 +453,16 @@
  22553. struct cpu_stop_done *done = work->done;
  22554. char ksym_buf[KSYM_NAME_LEN] __maybe_unused;
  22555. + /*
  22556. + * Wait until the stopper finished scheduling on all
  22557. + * cpus
  22558. + */
  22559. + lg_global_lock(&stop_cpus_lock);
  22560. + /*
  22561. + * Let other cpu threads continue as well
  22562. + */
  22563. + lg_global_unlock(&stop_cpus_lock);
  22564. +
  22565. /* cpu stop callbacks are not allowed to sleep */
  22566. preempt_disable();
  22567. @@ -520,10 +533,12 @@
  22568. for_each_possible_cpu(cpu) {
  22569. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  22570. - spin_lock_init(&stopper->lock);
  22571. + raw_spin_lock_init(&stopper->lock);
  22572. INIT_LIST_HEAD(&stopper->works);
  22573. }
  22574. + lg_lock_init(&stop_cpus_lock, "stop_cpus_lock");
  22575. +
  22576. BUG_ON(smpboot_register_percpu_thread(&cpu_stop_threads));
  22577. stop_machine_unpark(raw_smp_processor_id());
  22578. stop_machine_initialized = true;
  22579. @@ -620,7 +635,7 @@
  22580. set_state(&msdata, MULTI_STOP_PREPARE);
  22581. cpu_stop_init_done(&done, num_active_cpus());
  22582. queue_stop_cpus_work(cpu_active_mask, multi_cpu_stop, &msdata,
  22583. - &done);
  22584. + &done, true);
  22585. ret = multi_cpu_stop(&msdata);
  22586. /* Busy wait for completion. */
  22587. diff -Nur linux-4.4.28.orig/kernel/time/hrtimer.c linux-4.4.28/kernel/time/hrtimer.c
  22588. --- linux-4.4.28.orig/kernel/time/hrtimer.c 2016-10-28 09:53:25.000000000 +0200
  22589. +++ linux-4.4.28/kernel/time/hrtimer.c 2016-11-01 21:26:58.796343570 +0100
  22590. @@ -48,11 +48,13 @@
  22591. #include <linux/sched/rt.h>
  22592. #include <linux/sched/deadline.h>
  22593. #include <linux/timer.h>
  22594. +#include <linux/kthread.h>
  22595. #include <linux/freezer.h>
  22596. #include <asm/uaccess.h>
  22597. #include <trace/events/timer.h>
  22598. +#include <trace/events/hist.h>
  22599. #include "tick-internal.h"
  22600. @@ -717,6 +719,44 @@
  22601. static DECLARE_WORK(hrtimer_work, clock_was_set_work);
  22602. +#ifdef CONFIG_PREEMPT_RT_FULL
  22603. +/*
  22604. + * RT can not call schedule_work from real interrupt context.
  22605. + * Need to make a thread to do the real work.
  22606. + */
  22607. +static struct task_struct *clock_set_delay_thread;
  22608. +static bool do_clock_set_delay;
  22609. +
  22610. +static int run_clock_set_delay(void *ignore)
  22611. +{
  22612. + while (!kthread_should_stop()) {
  22613. + set_current_state(TASK_INTERRUPTIBLE);
  22614. + if (do_clock_set_delay) {
  22615. + do_clock_set_delay = false;
  22616. + schedule_work(&hrtimer_work);
  22617. + }
  22618. + schedule();
  22619. + }
  22620. + __set_current_state(TASK_RUNNING);
  22621. + return 0;
  22622. +}
  22623. +
  22624. +void clock_was_set_delayed(void)
  22625. +{
  22626. + do_clock_set_delay = true;
  22627. + /* Make visible before waking up process */
  22628. + smp_wmb();
  22629. + wake_up_process(clock_set_delay_thread);
  22630. +}
  22631. +
  22632. +static __init int create_clock_set_delay_thread(void)
  22633. +{
  22634. + clock_set_delay_thread = kthread_run(run_clock_set_delay, NULL, "kclksetdelayd");
  22635. + BUG_ON(!clock_set_delay_thread);
  22636. + return 0;
  22637. +}
  22638. +early_initcall(create_clock_set_delay_thread);
  22639. +#else /* PREEMPT_RT_FULL */
  22640. /*
  22641. * Called from timekeeping and resume code to reprogramm the hrtimer
  22642. * interrupt device on all cpus.
  22643. @@ -725,6 +765,7 @@
  22644. {
  22645. schedule_work(&hrtimer_work);
  22646. }
  22647. +#endif
  22648. #else
  22649. @@ -734,11 +775,8 @@
  22650. static inline void hrtimer_switch_to_hres(void) { }
  22651. static inline void
  22652. hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
  22653. -static inline int hrtimer_reprogram(struct hrtimer *timer,
  22654. - struct hrtimer_clock_base *base)
  22655. -{
  22656. - return 0;
  22657. -}
  22658. +static inline void hrtimer_reprogram(struct hrtimer *timer,
  22659. + struct hrtimer_clock_base *base) { }
  22660. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
  22661. static inline void retrigger_next_event(void *arg) { }
  22662. @@ -870,6 +908,32 @@
  22663. }
  22664. EXPORT_SYMBOL_GPL(hrtimer_forward);
  22665. +#ifdef CONFIG_PREEMPT_RT_BASE
  22666. +# define wake_up_timer_waiters(b) wake_up(&(b)->wait)
  22667. +
  22668. +/**
  22669. + * hrtimer_wait_for_timer - Wait for a running timer
  22670. + *
  22671. + * @timer: timer to wait for
  22672. + *
  22673. + * The function waits in case the timers callback function is
  22674. + * currently executed on the waitqueue of the timer base. The
  22675. + * waitqueue is woken up after the timer callback function has
  22676. + * finished execution.
  22677. + */
  22678. +void hrtimer_wait_for_timer(const struct hrtimer *timer)
  22679. +{
  22680. + struct hrtimer_clock_base *base = timer->base;
  22681. +
  22682. + if (base && base->cpu_base && !timer->irqsafe)
  22683. + wait_event(base->cpu_base->wait,
  22684. + !(hrtimer_callback_running(timer)));
  22685. +}
  22686. +
  22687. +#else
  22688. +# define wake_up_timer_waiters(b) do { } while (0)
  22689. +#endif
  22690. +
  22691. /*
  22692. * enqueue_hrtimer - internal function to (re)start a timer
  22693. *
  22694. @@ -911,6 +975,11 @@
  22695. if (!(state & HRTIMER_STATE_ENQUEUED))
  22696. return;
  22697. + if (unlikely(!list_empty(&timer->cb_entry))) {
  22698. + list_del_init(&timer->cb_entry);
  22699. + return;
  22700. + }
  22701. +
  22702. if (!timerqueue_del(&base->active, &timer->node))
  22703. cpu_base->active_bases &= ~(1 << base->index);
  22704. @@ -1006,7 +1075,16 @@
  22705. new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
  22706. timer_stats_hrtimer_set_start_info(timer);
  22707. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  22708. + {
  22709. + ktime_t now = new_base->get_time();
  22710. + if (ktime_to_ns(tim) < ktime_to_ns(now))
  22711. + timer->praecox = now;
  22712. + else
  22713. + timer->praecox = ktime_set(0, 0);
  22714. + }
  22715. +#endif
  22716. leftmost = enqueue_hrtimer(timer, new_base);
  22717. if (!leftmost)
  22718. goto unlock;
  22719. @@ -1078,7 +1156,7 @@
  22720. if (ret >= 0)
  22721. return ret;
  22722. - cpu_relax();
  22723. + hrtimer_wait_for_timer(timer);
  22724. }
  22725. }
  22726. EXPORT_SYMBOL_GPL(hrtimer_cancel);
  22727. @@ -1142,6 +1220,7 @@
  22728. base = hrtimer_clockid_to_base(clock_id);
  22729. timer->base = &cpu_base->clock_base[base];
  22730. + INIT_LIST_HEAD(&timer->cb_entry);
  22731. timerqueue_init(&timer->node);
  22732. #ifdef CONFIG_TIMER_STATS
  22733. @@ -1182,6 +1261,7 @@
  22734. seq = raw_read_seqcount_begin(&cpu_base->seq);
  22735. if (timer->state != HRTIMER_STATE_INACTIVE ||
  22736. + cpu_base->running_soft == timer ||
  22737. cpu_base->running == timer)
  22738. return true;
  22739. @@ -1280,10 +1360,112 @@
  22740. cpu_base->running = NULL;
  22741. }
  22742. +#ifdef CONFIG_PREEMPT_RT_BASE
  22743. +static void hrtimer_rt_reprogram(int restart, struct hrtimer *timer,
  22744. + struct hrtimer_clock_base *base)
  22745. +{
  22746. + int leftmost;
  22747. +
  22748. + if (restart != HRTIMER_NORESTART &&
  22749. + !(timer->state & HRTIMER_STATE_ENQUEUED)) {
  22750. +
  22751. + leftmost = enqueue_hrtimer(timer, base);
  22752. + if (!leftmost)
  22753. + return;
  22754. +#ifdef CONFIG_HIGH_RES_TIMERS
  22755. + if (!hrtimer_is_hres_active(timer)) {
  22756. + /*
  22757. + * Kick to reschedule the next tick to handle the new timer
  22758. + * on dynticks target.
  22759. + */
  22760. + if (base->cpu_base->nohz_active)
  22761. + wake_up_nohz_cpu(base->cpu_base->cpu);
  22762. + } else {
  22763. +
  22764. + hrtimer_reprogram(timer, base);
  22765. + }
  22766. +#endif
  22767. + }
  22768. +}
  22769. +
  22770. +/*
  22771. + * The changes in mainline which removed the callback modes from
  22772. + * hrtimer are not yet working with -rt. The non wakeup_process()
  22773. + * based callbacks which involve sleeping locks need to be treated
  22774. + * seperately.
  22775. + */
  22776. +static void hrtimer_rt_run_pending(void)
  22777. +{
  22778. + enum hrtimer_restart (*fn)(struct hrtimer *);
  22779. + struct hrtimer_cpu_base *cpu_base;
  22780. + struct hrtimer_clock_base *base;
  22781. + struct hrtimer *timer;
  22782. + int index, restart;
  22783. +
  22784. + local_irq_disable();
  22785. + cpu_base = &per_cpu(hrtimer_bases, smp_processor_id());
  22786. +
  22787. + raw_spin_lock(&cpu_base->lock);
  22788. +
  22789. + for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
  22790. + base = &cpu_base->clock_base[index];
  22791. +
  22792. + while (!list_empty(&base->expired)) {
  22793. + timer = list_first_entry(&base->expired,
  22794. + struct hrtimer, cb_entry);
  22795. +
  22796. + /*
  22797. + * Same as the above __run_hrtimer function
  22798. + * just we run with interrupts enabled.
  22799. + */
  22800. + debug_deactivate(timer);
  22801. + cpu_base->running_soft = timer;
  22802. + raw_write_seqcount_barrier(&cpu_base->seq);
  22803. +
  22804. + __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, 0);
  22805. + timer_stats_account_hrtimer(timer);
  22806. + fn = timer->function;
  22807. +
  22808. + raw_spin_unlock_irq(&cpu_base->lock);
  22809. + restart = fn(timer);
  22810. + raw_spin_lock_irq(&cpu_base->lock);
  22811. +
  22812. + hrtimer_rt_reprogram(restart, timer, base);
  22813. + raw_write_seqcount_barrier(&cpu_base->seq);
  22814. +
  22815. + WARN_ON_ONCE(cpu_base->running_soft != timer);
  22816. + cpu_base->running_soft = NULL;
  22817. + }
  22818. + }
  22819. +
  22820. + raw_spin_unlock_irq(&cpu_base->lock);
  22821. +
  22822. + wake_up_timer_waiters(cpu_base);
  22823. +}
  22824. +
  22825. +static int hrtimer_rt_defer(struct hrtimer *timer)
  22826. +{
  22827. + if (timer->irqsafe)
  22828. + return 0;
  22829. +
  22830. + __remove_hrtimer(timer, timer->base, timer->state, 0);
  22831. + list_add_tail(&timer->cb_entry, &timer->base->expired);
  22832. + return 1;
  22833. +}
  22834. +
  22835. +#else
  22836. +
  22837. +static inline int hrtimer_rt_defer(struct hrtimer *timer) { return 0; }
  22838. +
  22839. +#endif
  22840. +
  22841. +static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer);
  22842. +
  22843. static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now)
  22844. {
  22845. struct hrtimer_clock_base *base = cpu_base->clock_base;
  22846. unsigned int active = cpu_base->active_bases;
  22847. + int raise = 0;
  22848. for (; active; base++, active >>= 1) {
  22849. struct timerqueue_node *node;
  22850. @@ -1299,6 +1481,15 @@
  22851. timer = container_of(node, struct hrtimer, node);
  22852. + trace_hrtimer_interrupt(raw_smp_processor_id(),
  22853. + ktime_to_ns(ktime_sub(ktime_to_ns(timer->praecox) ?
  22854. + timer->praecox : hrtimer_get_expires(timer),
  22855. + basenow)),
  22856. + current,
  22857. + timer->function == hrtimer_wakeup ?
  22858. + container_of(timer, struct hrtimer_sleeper,
  22859. + timer)->task : NULL);
  22860. +
  22861. /*
  22862. * The immediate goal for using the softexpires is
  22863. * minimizing wakeups, not running timers at the
  22864. @@ -1314,9 +1505,14 @@
  22865. if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer))
  22866. break;
  22867. - __run_hrtimer(cpu_base, base, timer, &basenow);
  22868. + if (!hrtimer_rt_defer(timer))
  22869. + __run_hrtimer(cpu_base, base, timer, &basenow);
  22870. + else
  22871. + raise = 1;
  22872. }
  22873. }
  22874. + if (raise)
  22875. + raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  22876. }
  22877. #ifdef CONFIG_HIGH_RES_TIMERS
  22878. @@ -1479,16 +1675,18 @@
  22879. void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
  22880. {
  22881. sl->timer.function = hrtimer_wakeup;
  22882. + sl->timer.irqsafe = 1;
  22883. sl->task = task;
  22884. }
  22885. EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
  22886. -static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
  22887. +static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode,
  22888. + unsigned long state)
  22889. {
  22890. hrtimer_init_sleeper(t, current);
  22891. do {
  22892. - set_current_state(TASK_INTERRUPTIBLE);
  22893. + set_current_state(state);
  22894. hrtimer_start_expires(&t->timer, mode);
  22895. if (likely(t->task))
  22896. @@ -1530,7 +1728,8 @@
  22897. HRTIMER_MODE_ABS);
  22898. hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
  22899. - if (do_nanosleep(&t, HRTIMER_MODE_ABS))
  22900. + /* cpu_chill() does not care about restart state. */
  22901. + if (do_nanosleep(&t, HRTIMER_MODE_ABS, TASK_INTERRUPTIBLE))
  22902. goto out;
  22903. rmtp = restart->nanosleep.rmtp;
  22904. @@ -1547,8 +1746,10 @@
  22905. return ret;
  22906. }
  22907. -long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  22908. - const enum hrtimer_mode mode, const clockid_t clockid)
  22909. +static long
  22910. +__hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  22911. + const enum hrtimer_mode mode, const clockid_t clockid,
  22912. + unsigned long state)
  22913. {
  22914. struct restart_block *restart;
  22915. struct hrtimer_sleeper t;
  22916. @@ -1561,7 +1762,7 @@
  22917. hrtimer_init_on_stack(&t.timer, clockid, mode);
  22918. hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
  22919. - if (do_nanosleep(&t, mode))
  22920. + if (do_nanosleep(&t, mode, state))
  22921. goto out;
  22922. /* Absolute timers do not update the rmtp value and restart: */
  22923. @@ -1588,6 +1789,12 @@
  22924. return ret;
  22925. }
  22926. +long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  22927. + const enum hrtimer_mode mode, const clockid_t clockid)
  22928. +{
  22929. + return __hrtimer_nanosleep(rqtp, rmtp, mode, clockid, TASK_INTERRUPTIBLE);
  22930. +}
  22931. +
  22932. SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
  22933. struct timespec __user *, rmtp)
  22934. {
  22935. @@ -1602,6 +1809,26 @@
  22936. return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
  22937. }
  22938. +#ifdef CONFIG_PREEMPT_RT_FULL
  22939. +/*
  22940. + * Sleep for 1 ms in hope whoever holds what we want will let it go.
  22941. + */
  22942. +void cpu_chill(void)
  22943. +{
  22944. + struct timespec tu = {
  22945. + .tv_nsec = NSEC_PER_MSEC,
  22946. + };
  22947. + unsigned int freeze_flag = current->flags & PF_NOFREEZE;
  22948. +
  22949. + current->flags |= PF_NOFREEZE;
  22950. + __hrtimer_nanosleep(&tu, NULL, HRTIMER_MODE_REL, CLOCK_MONOTONIC,
  22951. + TASK_UNINTERRUPTIBLE);
  22952. + if (!freeze_flag)
  22953. + current->flags &= ~PF_NOFREEZE;
  22954. +}
  22955. +EXPORT_SYMBOL(cpu_chill);
  22956. +#endif
  22957. +
  22958. /*
  22959. * Functions related to boot-time initialization:
  22960. */
  22961. @@ -1613,10 +1840,14 @@
  22962. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  22963. cpu_base->clock_base[i].cpu_base = cpu_base;
  22964. timerqueue_init_head(&cpu_base->clock_base[i].active);
  22965. + INIT_LIST_HEAD(&cpu_base->clock_base[i].expired);
  22966. }
  22967. cpu_base->cpu = cpu;
  22968. hrtimer_init_hres(cpu_base);
  22969. +#ifdef CONFIG_PREEMPT_RT_BASE
  22970. + init_waitqueue_head(&cpu_base->wait);
  22971. +#endif
  22972. }
  22973. #ifdef CONFIG_HOTPLUG_CPU
  22974. @@ -1714,11 +1945,21 @@
  22975. .notifier_call = hrtimer_cpu_notify,
  22976. };
  22977. +#ifdef CONFIG_PREEMPT_RT_BASE
  22978. +static void run_hrtimer_softirq(struct softirq_action *h)
  22979. +{
  22980. + hrtimer_rt_run_pending();
  22981. +}
  22982. +#endif
  22983. +
  22984. void __init hrtimers_init(void)
  22985. {
  22986. hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
  22987. (void *)(long)smp_processor_id());
  22988. register_cpu_notifier(&hrtimers_nb);
  22989. +#ifdef CONFIG_PREEMPT_RT_BASE
  22990. + open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
  22991. +#endif
  22992. }
  22993. /**
  22994. diff -Nur linux-4.4.28.orig/kernel/time/itimer.c linux-4.4.28/kernel/time/itimer.c
  22995. --- linux-4.4.28.orig/kernel/time/itimer.c 2016-10-28 09:53:25.000000000 +0200
  22996. +++ linux-4.4.28/kernel/time/itimer.c 2016-11-01 21:26:58.796343570 +0100
  22997. @@ -213,6 +213,7 @@
  22998. /* We are sharing ->siglock with it_real_fn() */
  22999. if (hrtimer_try_to_cancel(timer) < 0) {
  23000. spin_unlock_irq(&tsk->sighand->siglock);
  23001. + hrtimer_wait_for_timer(&tsk->signal->real_timer);
  23002. goto again;
  23003. }
  23004. expires = timeval_to_ktime(value->it_value);
  23005. diff -Nur linux-4.4.28.orig/kernel/time/jiffies.c linux-4.4.28/kernel/time/jiffies.c
  23006. --- linux-4.4.28.orig/kernel/time/jiffies.c 2016-10-28 09:53:25.000000000 +0200
  23007. +++ linux-4.4.28/kernel/time/jiffies.c 2016-11-01 21:26:58.796343570 +0100
  23008. @@ -74,7 +74,8 @@
  23009. .max_cycles = 10,
  23010. };
  23011. -__cacheline_aligned_in_smp DEFINE_SEQLOCK(jiffies_lock);
  23012. +__cacheline_aligned_in_smp DEFINE_RAW_SPINLOCK(jiffies_lock);
  23013. +__cacheline_aligned_in_smp seqcount_t jiffies_seq;
  23014. #if (BITS_PER_LONG < 64)
  23015. u64 get_jiffies_64(void)
  23016. @@ -83,9 +84,9 @@
  23017. u64 ret;
  23018. do {
  23019. - seq = read_seqbegin(&jiffies_lock);
  23020. + seq = read_seqcount_begin(&jiffies_seq);
  23021. ret = jiffies_64;
  23022. - } while (read_seqretry(&jiffies_lock, seq));
  23023. + } while (read_seqcount_retry(&jiffies_seq, seq));
  23024. return ret;
  23025. }
  23026. EXPORT_SYMBOL(get_jiffies_64);
  23027. diff -Nur linux-4.4.28.orig/kernel/time/ntp.c linux-4.4.28/kernel/time/ntp.c
  23028. --- linux-4.4.28.orig/kernel/time/ntp.c 2016-10-28 09:53:25.000000000 +0200
  23029. +++ linux-4.4.28/kernel/time/ntp.c 2016-11-01 21:26:58.796343570 +0100
  23030. @@ -10,6 +10,7 @@
  23031. #include <linux/workqueue.h>
  23032. #include <linux/hrtimer.h>
  23033. #include <linux/jiffies.h>
  23034. +#include <linux/kthread.h>
  23035. #include <linux/math64.h>
  23036. #include <linux/timex.h>
  23037. #include <linux/time.h>
  23038. @@ -562,10 +563,52 @@
  23039. &sync_cmos_work, timespec64_to_jiffies(&next));
  23040. }
  23041. +#ifdef CONFIG_PREEMPT_RT_FULL
  23042. +/*
  23043. + * RT can not call schedule_delayed_work from real interrupt context.
  23044. + * Need to make a thread to do the real work.
  23045. + */
  23046. +static struct task_struct *cmos_delay_thread;
  23047. +static bool do_cmos_delay;
  23048. +
  23049. +static int run_cmos_delay(void *ignore)
  23050. +{
  23051. + while (!kthread_should_stop()) {
  23052. + set_current_state(TASK_INTERRUPTIBLE);
  23053. + if (do_cmos_delay) {
  23054. + do_cmos_delay = false;
  23055. + queue_delayed_work(system_power_efficient_wq,
  23056. + &sync_cmos_work, 0);
  23057. + }
  23058. + schedule();
  23059. + }
  23060. + __set_current_state(TASK_RUNNING);
  23061. + return 0;
  23062. +}
  23063. +
  23064. +void ntp_notify_cmos_timer(void)
  23065. +{
  23066. + do_cmos_delay = true;
  23067. + /* Make visible before waking up process */
  23068. + smp_wmb();
  23069. + wake_up_process(cmos_delay_thread);
  23070. +}
  23071. +
  23072. +static __init int create_cmos_delay_thread(void)
  23073. +{
  23074. + cmos_delay_thread = kthread_run(run_cmos_delay, NULL, "kcmosdelayd");
  23075. + BUG_ON(!cmos_delay_thread);
  23076. + return 0;
  23077. +}
  23078. +early_initcall(create_cmos_delay_thread);
  23079. +
  23080. +#else
  23081. +
  23082. void ntp_notify_cmos_timer(void)
  23083. {
  23084. queue_delayed_work(system_power_efficient_wq, &sync_cmos_work, 0);
  23085. }
  23086. +#endif /* CONFIG_PREEMPT_RT_FULL */
  23087. #else
  23088. void ntp_notify_cmos_timer(void) { }
  23089. diff -Nur linux-4.4.28.orig/kernel/time/posix-cpu-timers.c linux-4.4.28/kernel/time/posix-cpu-timers.c
  23090. --- linux-4.4.28.orig/kernel/time/posix-cpu-timers.c 2016-10-28 09:53:25.000000000 +0200
  23091. +++ linux-4.4.28/kernel/time/posix-cpu-timers.c 2016-11-01 21:26:58.800343826 +0100
  23092. @@ -3,6 +3,7 @@
  23093. */
  23094. #include <linux/sched.h>
  23095. +#include <linux/sched/rt.h>
  23096. #include <linux/posix-timers.h>
  23097. #include <linux/errno.h>
  23098. #include <linux/math64.h>
  23099. @@ -650,7 +651,7 @@
  23100. /*
  23101. * Disarm any old timer after extracting its expiry time.
  23102. */
  23103. - WARN_ON_ONCE(!irqs_disabled());
  23104. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  23105. ret = 0;
  23106. old_incr = timer->it.cpu.incr;
  23107. @@ -1092,7 +1093,7 @@
  23108. /*
  23109. * Now re-arm for the new expiry time.
  23110. */
  23111. - WARN_ON_ONCE(!irqs_disabled());
  23112. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  23113. arm_timer(timer);
  23114. unlock_task_sighand(p, &flags);
  23115. @@ -1183,13 +1184,13 @@
  23116. * already updated our counts. We need to check if any timers fire now.
  23117. * Interrupts are disabled.
  23118. */
  23119. -void run_posix_cpu_timers(struct task_struct *tsk)
  23120. +static void __run_posix_cpu_timers(struct task_struct *tsk)
  23121. {
  23122. LIST_HEAD(firing);
  23123. struct k_itimer *timer, *next;
  23124. unsigned long flags;
  23125. - WARN_ON_ONCE(!irqs_disabled());
  23126. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  23127. /*
  23128. * The fast path checks that there are no expired thread or thread
  23129. @@ -1243,6 +1244,190 @@
  23130. }
  23131. }
  23132. +#ifdef CONFIG_PREEMPT_RT_BASE
  23133. +#include <linux/kthread.h>
  23134. +#include <linux/cpu.h>
  23135. +DEFINE_PER_CPU(struct task_struct *, posix_timer_task);
  23136. +DEFINE_PER_CPU(struct task_struct *, posix_timer_tasklist);
  23137. +
  23138. +static int posix_cpu_timers_thread(void *data)
  23139. +{
  23140. + int cpu = (long)data;
  23141. +
  23142. + BUG_ON(per_cpu(posix_timer_task,cpu) != current);
  23143. +
  23144. + while (!kthread_should_stop()) {
  23145. + struct task_struct *tsk = NULL;
  23146. + struct task_struct *next = NULL;
  23147. +
  23148. + if (cpu_is_offline(cpu))
  23149. + goto wait_to_die;
  23150. +
  23151. + /* grab task list */
  23152. + raw_local_irq_disable();
  23153. + tsk = per_cpu(posix_timer_tasklist, cpu);
  23154. + per_cpu(posix_timer_tasklist, cpu) = NULL;
  23155. + raw_local_irq_enable();
  23156. +
  23157. + /* its possible the list is empty, just return */
  23158. + if (!tsk) {
  23159. + set_current_state(TASK_INTERRUPTIBLE);
  23160. + schedule();
  23161. + __set_current_state(TASK_RUNNING);
  23162. + continue;
  23163. + }
  23164. +
  23165. + /* Process task list */
  23166. + while (1) {
  23167. + /* save next */
  23168. + next = tsk->posix_timer_list;
  23169. +
  23170. + /* run the task timers, clear its ptr and
  23171. + * unreference it
  23172. + */
  23173. + __run_posix_cpu_timers(tsk);
  23174. + tsk->posix_timer_list = NULL;
  23175. + put_task_struct(tsk);
  23176. +
  23177. + /* check if this is the last on the list */
  23178. + if (next == tsk)
  23179. + break;
  23180. + tsk = next;
  23181. + }
  23182. + }
  23183. + return 0;
  23184. +
  23185. +wait_to_die:
  23186. + /* Wait for kthread_stop */
  23187. + set_current_state(TASK_INTERRUPTIBLE);
  23188. + while (!kthread_should_stop()) {
  23189. + schedule();
  23190. + set_current_state(TASK_INTERRUPTIBLE);
  23191. + }
  23192. + __set_current_state(TASK_RUNNING);
  23193. + return 0;
  23194. +}
  23195. +
  23196. +static inline int __fastpath_timer_check(struct task_struct *tsk)
  23197. +{
  23198. + /* tsk == current, ensure it is safe to use ->signal/sighand */
  23199. + if (unlikely(tsk->exit_state))
  23200. + return 0;
  23201. +
  23202. + if (!task_cputime_zero(&tsk->cputime_expires))
  23203. + return 1;
  23204. +
  23205. + if (!task_cputime_zero(&tsk->signal->cputime_expires))
  23206. + return 1;
  23207. +
  23208. + return 0;
  23209. +}
  23210. +
  23211. +void run_posix_cpu_timers(struct task_struct *tsk)
  23212. +{
  23213. + unsigned long cpu = smp_processor_id();
  23214. + struct task_struct *tasklist;
  23215. +
  23216. + BUG_ON(!irqs_disabled());
  23217. + if(!per_cpu(posix_timer_task, cpu))
  23218. + return;
  23219. + /* get per-cpu references */
  23220. + tasklist = per_cpu(posix_timer_tasklist, cpu);
  23221. +
  23222. + /* check to see if we're already queued */
  23223. + if (!tsk->posix_timer_list && __fastpath_timer_check(tsk)) {
  23224. + get_task_struct(tsk);
  23225. + if (tasklist) {
  23226. + tsk->posix_timer_list = tasklist;
  23227. + } else {
  23228. + /*
  23229. + * The list is terminated by a self-pointing
  23230. + * task_struct
  23231. + */
  23232. + tsk->posix_timer_list = tsk;
  23233. + }
  23234. + per_cpu(posix_timer_tasklist, cpu) = tsk;
  23235. +
  23236. + wake_up_process(per_cpu(posix_timer_task, cpu));
  23237. + }
  23238. +}
  23239. +
  23240. +/*
  23241. + * posix_cpu_thread_call - callback that gets triggered when a CPU is added.
  23242. + * Here we can start up the necessary migration thread for the new CPU.
  23243. + */
  23244. +static int posix_cpu_thread_call(struct notifier_block *nfb,
  23245. + unsigned long action, void *hcpu)
  23246. +{
  23247. + int cpu = (long)hcpu;
  23248. + struct task_struct *p;
  23249. + struct sched_param param;
  23250. +
  23251. + switch (action) {
  23252. + case CPU_UP_PREPARE:
  23253. + p = kthread_create(posix_cpu_timers_thread, hcpu,
  23254. + "posixcputmr/%d",cpu);
  23255. + if (IS_ERR(p))
  23256. + return NOTIFY_BAD;
  23257. + p->flags |= PF_NOFREEZE;
  23258. + kthread_bind(p, cpu);
  23259. + /* Must be high prio to avoid getting starved */
  23260. + param.sched_priority = MAX_RT_PRIO-1;
  23261. + sched_setscheduler(p, SCHED_FIFO, &param);
  23262. + per_cpu(posix_timer_task,cpu) = p;
  23263. + break;
  23264. + case CPU_ONLINE:
  23265. + /* Strictly unneccessary, as first user will wake it. */
  23266. + wake_up_process(per_cpu(posix_timer_task,cpu));
  23267. + break;
  23268. +#ifdef CONFIG_HOTPLUG_CPU
  23269. + case CPU_UP_CANCELED:
  23270. + /* Unbind it from offline cpu so it can run. Fall thru. */
  23271. + kthread_bind(per_cpu(posix_timer_task, cpu),
  23272. + cpumask_any(cpu_online_mask));
  23273. + kthread_stop(per_cpu(posix_timer_task,cpu));
  23274. + per_cpu(posix_timer_task,cpu) = NULL;
  23275. + break;
  23276. + case CPU_DEAD:
  23277. + kthread_stop(per_cpu(posix_timer_task,cpu));
  23278. + per_cpu(posix_timer_task,cpu) = NULL;
  23279. + break;
  23280. +#endif
  23281. + }
  23282. + return NOTIFY_OK;
  23283. +}
  23284. +
  23285. +/* Register at highest priority so that task migration (migrate_all_tasks)
  23286. + * happens before everything else.
  23287. + */
  23288. +static struct notifier_block posix_cpu_thread_notifier = {
  23289. + .notifier_call = posix_cpu_thread_call,
  23290. + .priority = 10
  23291. +};
  23292. +
  23293. +static int __init posix_cpu_thread_init(void)
  23294. +{
  23295. + void *hcpu = (void *)(long)smp_processor_id();
  23296. + /* Start one for boot CPU. */
  23297. + unsigned long cpu;
  23298. +
  23299. + /* init the per-cpu posix_timer_tasklets */
  23300. + for_each_possible_cpu(cpu)
  23301. + per_cpu(posix_timer_tasklist, cpu) = NULL;
  23302. +
  23303. + posix_cpu_thread_call(&posix_cpu_thread_notifier, CPU_UP_PREPARE, hcpu);
  23304. + posix_cpu_thread_call(&posix_cpu_thread_notifier, CPU_ONLINE, hcpu);
  23305. + register_cpu_notifier(&posix_cpu_thread_notifier);
  23306. + return 0;
  23307. +}
  23308. +early_initcall(posix_cpu_thread_init);
  23309. +#else /* CONFIG_PREEMPT_RT_BASE */
  23310. +void run_posix_cpu_timers(struct task_struct *tsk)
  23311. +{
  23312. + __run_posix_cpu_timers(tsk);
  23313. +}
  23314. +#endif /* CONFIG_PREEMPT_RT_BASE */
  23315. +
  23316. /*
  23317. * Set one of the process-wide special case CPU timers or RLIMIT_CPU.
  23318. * The tsk->sighand->siglock must be held by the caller.
  23319. diff -Nur linux-4.4.28.orig/kernel/time/posix-timers.c linux-4.4.28/kernel/time/posix-timers.c
  23320. --- linux-4.4.28.orig/kernel/time/posix-timers.c 2016-10-28 09:53:25.000000000 +0200
  23321. +++ linux-4.4.28/kernel/time/posix-timers.c 2016-11-01 21:26:58.800343826 +0100
  23322. @@ -506,6 +506,7 @@
  23323. static struct pid *good_sigevent(sigevent_t * event)
  23324. {
  23325. struct task_struct *rtn = current->group_leader;
  23326. + int sig = event->sigev_signo;
  23327. if ((event->sigev_notify & SIGEV_THREAD_ID ) &&
  23328. (!(rtn = find_task_by_vpid(event->sigev_notify_thread_id)) ||
  23329. @@ -514,7 +515,8 @@
  23330. return NULL;
  23331. if (((event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) &&
  23332. - ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX)))
  23333. + (sig <= 0 || sig > SIGRTMAX || sig_kernel_only(sig) ||
  23334. + sig_kernel_coredump(sig)))
  23335. return NULL;
  23336. return task_pid(rtn);
  23337. @@ -826,6 +828,20 @@
  23338. return overrun;
  23339. }
  23340. +/*
  23341. + * Protected by RCU!
  23342. + */
  23343. +static void timer_wait_for_callback(struct k_clock *kc, struct k_itimer *timr)
  23344. +{
  23345. +#ifdef CONFIG_PREEMPT_RT_FULL
  23346. + if (kc->timer_set == common_timer_set)
  23347. + hrtimer_wait_for_timer(&timr->it.real.timer);
  23348. + else
  23349. + /* FIXME: Whacky hack for posix-cpu-timers */
  23350. + schedule_timeout(1);
  23351. +#endif
  23352. +}
  23353. +
  23354. /* Set a POSIX.1b interval timer. */
  23355. /* timr->it_lock is taken. */
  23356. static int
  23357. @@ -903,6 +919,7 @@
  23358. if (!timr)
  23359. return -EINVAL;
  23360. + rcu_read_lock();
  23361. kc = clockid_to_kclock(timr->it_clock);
  23362. if (WARN_ON_ONCE(!kc || !kc->timer_set))
  23363. error = -EINVAL;
  23364. @@ -911,9 +928,12 @@
  23365. unlock_timer(timr, flag);
  23366. if (error == TIMER_RETRY) {
  23367. + timer_wait_for_callback(kc, timr);
  23368. rtn = NULL; // We already got the old time...
  23369. + rcu_read_unlock();
  23370. goto retry;
  23371. }
  23372. + rcu_read_unlock();
  23373. if (old_setting && !error &&
  23374. copy_to_user(old_setting, &old_spec, sizeof (old_spec)))
  23375. @@ -951,10 +971,15 @@
  23376. if (!timer)
  23377. return -EINVAL;
  23378. + rcu_read_lock();
  23379. if (timer_delete_hook(timer) == TIMER_RETRY) {
  23380. unlock_timer(timer, flags);
  23381. + timer_wait_for_callback(clockid_to_kclock(timer->it_clock),
  23382. + timer);
  23383. + rcu_read_unlock();
  23384. goto retry_delete;
  23385. }
  23386. + rcu_read_unlock();
  23387. spin_lock(&current->sighand->siglock);
  23388. list_del(&timer->list);
  23389. @@ -980,8 +1005,18 @@
  23390. retry_delete:
  23391. spin_lock_irqsave(&timer->it_lock, flags);
  23392. + /* On RT we can race with a deletion */
  23393. + if (!timer->it_signal) {
  23394. + unlock_timer(timer, flags);
  23395. + return;
  23396. + }
  23397. +
  23398. if (timer_delete_hook(timer) == TIMER_RETRY) {
  23399. + rcu_read_lock();
  23400. unlock_timer(timer, flags);
  23401. + timer_wait_for_callback(clockid_to_kclock(timer->it_clock),
  23402. + timer);
  23403. + rcu_read_unlock();
  23404. goto retry_delete;
  23405. }
  23406. list_del(&timer->list);
  23407. diff -Nur linux-4.4.28.orig/kernel/time/tick-broadcast-hrtimer.c linux-4.4.28/kernel/time/tick-broadcast-hrtimer.c
  23408. --- linux-4.4.28.orig/kernel/time/tick-broadcast-hrtimer.c 2016-10-28 09:53:25.000000000 +0200
  23409. +++ linux-4.4.28/kernel/time/tick-broadcast-hrtimer.c 2016-11-01 21:26:58.800343826 +0100
  23410. @@ -106,5 +106,6 @@
  23411. {
  23412. hrtimer_init(&bctimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  23413. bctimer.function = bc_handler;
  23414. + bctimer.irqsafe = true;
  23415. clockevents_register_device(&ce_broadcast_hrtimer);
  23416. }
  23417. diff -Nur linux-4.4.28.orig/kernel/time/tick-common.c linux-4.4.28/kernel/time/tick-common.c
  23418. --- linux-4.4.28.orig/kernel/time/tick-common.c 2016-10-28 09:53:25.000000000 +0200
  23419. +++ linux-4.4.28/kernel/time/tick-common.c 2016-11-01 21:26:58.800343826 +0100
  23420. @@ -79,13 +79,15 @@
  23421. static void tick_periodic(int cpu)
  23422. {
  23423. if (tick_do_timer_cpu == cpu) {
  23424. - write_seqlock(&jiffies_lock);
  23425. + raw_spin_lock(&jiffies_lock);
  23426. + write_seqcount_begin(&jiffies_seq);
  23427. /* Keep track of the next tick event */
  23428. tick_next_period = ktime_add(tick_next_period, tick_period);
  23429. do_timer(1);
  23430. - write_sequnlock(&jiffies_lock);
  23431. + write_seqcount_end(&jiffies_seq);
  23432. + raw_spin_unlock(&jiffies_lock);
  23433. update_wall_time();
  23434. }
  23435. @@ -157,9 +159,9 @@
  23436. ktime_t next;
  23437. do {
  23438. - seq = read_seqbegin(&jiffies_lock);
  23439. + seq = read_seqcount_begin(&jiffies_seq);
  23440. next = tick_next_period;
  23441. - } while (read_seqretry(&jiffies_lock, seq));
  23442. + } while (read_seqcount_retry(&jiffies_seq, seq));
  23443. clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
  23444. diff -Nur linux-4.4.28.orig/kernel/time/tick-sched.c linux-4.4.28/kernel/time/tick-sched.c
  23445. --- linux-4.4.28.orig/kernel/time/tick-sched.c 2016-10-28 09:53:25.000000000 +0200
  23446. +++ linux-4.4.28/kernel/time/tick-sched.c 2016-11-01 21:26:58.800343826 +0100
  23447. @@ -62,7 +62,8 @@
  23448. return;
  23449. /* Reevalute with jiffies_lock held */
  23450. - write_seqlock(&jiffies_lock);
  23451. + raw_spin_lock(&jiffies_lock);
  23452. + write_seqcount_begin(&jiffies_seq);
  23453. delta = ktime_sub(now, last_jiffies_update);
  23454. if (delta.tv64 >= tick_period.tv64) {
  23455. @@ -85,10 +86,12 @@
  23456. /* Keep the tick_next_period variable up to date */
  23457. tick_next_period = ktime_add(last_jiffies_update, tick_period);
  23458. } else {
  23459. - write_sequnlock(&jiffies_lock);
  23460. + write_seqcount_end(&jiffies_seq);
  23461. + raw_spin_unlock(&jiffies_lock);
  23462. return;
  23463. }
  23464. - write_sequnlock(&jiffies_lock);
  23465. + write_seqcount_end(&jiffies_seq);
  23466. + raw_spin_unlock(&jiffies_lock);
  23467. update_wall_time();
  23468. }
  23469. @@ -99,12 +102,14 @@
  23470. {
  23471. ktime_t period;
  23472. - write_seqlock(&jiffies_lock);
  23473. + raw_spin_lock(&jiffies_lock);
  23474. + write_seqcount_begin(&jiffies_seq);
  23475. /* Did we start the jiffies update yet ? */
  23476. if (last_jiffies_update.tv64 == 0)
  23477. last_jiffies_update = tick_next_period;
  23478. period = last_jiffies_update;
  23479. - write_sequnlock(&jiffies_lock);
  23480. + write_seqcount_end(&jiffies_seq);
  23481. + raw_spin_unlock(&jiffies_lock);
  23482. return period;
  23483. }
  23484. @@ -176,6 +181,11 @@
  23485. return false;
  23486. }
  23487. + if (!arch_irq_work_has_interrupt()) {
  23488. + trace_tick_stop(0, "missing irq work interrupt\n");
  23489. + return false;
  23490. + }
  23491. +
  23492. /* sched_clock_tick() needs us? */
  23493. #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
  23494. /*
  23495. @@ -204,6 +214,7 @@
  23496. static DEFINE_PER_CPU(struct irq_work, nohz_full_kick_work) = {
  23497. .func = nohz_full_kick_work_func,
  23498. + .flags = IRQ_WORK_HARD_IRQ,
  23499. };
  23500. /*
  23501. @@ -578,10 +589,10 @@
  23502. /* Read jiffies and the time when jiffies were updated last */
  23503. do {
  23504. - seq = read_seqbegin(&jiffies_lock);
  23505. + seq = read_seqcount_begin(&jiffies_seq);
  23506. basemono = last_jiffies_update.tv64;
  23507. basejiff = jiffies;
  23508. - } while (read_seqretry(&jiffies_lock, seq));
  23509. + } while (read_seqcount_retry(&jiffies_seq, seq));
  23510. ts->last_jiffies = basejiff;
  23511. if (rcu_needs_cpu(basemono, &next_rcu) ||
  23512. @@ -753,14 +764,7 @@
  23513. return false;
  23514. if (unlikely(local_softirq_pending() && cpu_online(cpu))) {
  23515. - static int ratelimit;
  23516. -
  23517. - if (ratelimit < 10 &&
  23518. - (local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK)) {
  23519. - pr_warn("NOHZ: local_softirq_pending %02x\n",
  23520. - (unsigned int) local_softirq_pending());
  23521. - ratelimit++;
  23522. - }
  23523. + softirq_check_pending_idle();
  23524. return false;
  23525. }
  23526. @@ -1100,6 +1104,7 @@
  23527. * Emulate tick processing via per-CPU hrtimers:
  23528. */
  23529. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  23530. + ts->sched_timer.irqsafe = 1;
  23531. ts->sched_timer.function = tick_sched_timer;
  23532. /* Get the next period (per cpu) */
  23533. diff -Nur linux-4.4.28.orig/kernel/time/timekeeping.c linux-4.4.28/kernel/time/timekeeping.c
  23534. --- linux-4.4.28.orig/kernel/time/timekeeping.c 2016-10-28 09:53:25.000000000 +0200
  23535. +++ linux-4.4.28/kernel/time/timekeeping.c 2016-11-01 21:26:58.800343826 +0100
  23536. @@ -2070,8 +2070,10 @@
  23537. */
  23538. void xtime_update(unsigned long ticks)
  23539. {
  23540. - write_seqlock(&jiffies_lock);
  23541. + raw_spin_lock(&jiffies_lock);
  23542. + write_seqcount_begin(&jiffies_seq);
  23543. do_timer(ticks);
  23544. - write_sequnlock(&jiffies_lock);
  23545. + write_seqcount_end(&jiffies_seq);
  23546. + raw_spin_unlock(&jiffies_lock);
  23547. update_wall_time();
  23548. }
  23549. diff -Nur linux-4.4.28.orig/kernel/time/timekeeping.h linux-4.4.28/kernel/time/timekeeping.h
  23550. --- linux-4.4.28.orig/kernel/time/timekeeping.h 2016-10-28 09:53:25.000000000 +0200
  23551. +++ linux-4.4.28/kernel/time/timekeeping.h 2016-11-01 21:26:58.800343826 +0100
  23552. @@ -19,7 +19,8 @@
  23553. extern void do_timer(unsigned long ticks);
  23554. extern void update_wall_time(void);
  23555. -extern seqlock_t jiffies_lock;
  23556. +extern raw_spinlock_t jiffies_lock;
  23557. +extern seqcount_t jiffies_seq;
  23558. #define CS_NAME_LEN 32
  23559. diff -Nur linux-4.4.28.orig/kernel/time/timer.c linux-4.4.28/kernel/time/timer.c
  23560. --- linux-4.4.28.orig/kernel/time/timer.c 2016-10-28 09:53:25.000000000 +0200
  23561. +++ linux-4.4.28/kernel/time/timer.c 2016-11-01 21:26:58.804344090 +0100
  23562. @@ -80,6 +80,9 @@
  23563. struct tvec_base {
  23564. spinlock_t lock;
  23565. struct timer_list *running_timer;
  23566. +#ifdef CONFIG_PREEMPT_RT_FULL
  23567. + wait_queue_head_t wait_for_running_timer;
  23568. +#endif
  23569. unsigned long timer_jiffies;
  23570. unsigned long next_timer;
  23571. unsigned long active_timers;
  23572. @@ -777,6 +780,39 @@
  23573. cpu_relax();
  23574. }
  23575. }
  23576. +#ifdef CONFIG_PREEMPT_RT_FULL
  23577. +static inline struct tvec_base *switch_timer_base(struct timer_list *timer,
  23578. + struct tvec_base *old,
  23579. + struct tvec_base *new)
  23580. +{
  23581. + /*
  23582. + * We cannot do the below because we might be preempted and
  23583. + * then the preempter would see NULL and loop forever.
  23584. + */
  23585. + if (spin_trylock(&new->lock)) {
  23586. + WRITE_ONCE(timer->flags,
  23587. + (timer->flags & ~TIMER_BASEMASK) | new->cpu);
  23588. + spin_unlock(&old->lock);
  23589. + return new;
  23590. + }
  23591. + return old;
  23592. +}
  23593. +
  23594. +#else
  23595. +static inline struct tvec_base *switch_timer_base(struct timer_list *timer,
  23596. + struct tvec_base *old,
  23597. + struct tvec_base *new)
  23598. +{
  23599. + /* See the comment in lock_timer_base() */
  23600. + timer->flags |= TIMER_MIGRATING;
  23601. +
  23602. + spin_unlock(&old->lock);
  23603. + spin_lock(&new->lock);
  23604. + WRITE_ONCE(timer->flags,
  23605. + (timer->flags & ~TIMER_BASEMASK) | new->cpu);
  23606. + return new;
  23607. +}
  23608. +#endif
  23609. static inline int
  23610. __mod_timer(struct timer_list *timer, unsigned long expires,
  23611. @@ -807,16 +843,8 @@
  23612. * handler yet has not finished. This also guarantees that
  23613. * the timer is serialized wrt itself.
  23614. */
  23615. - if (likely(base->running_timer != timer)) {
  23616. - /* See the comment in lock_timer_base() */
  23617. - timer->flags |= TIMER_MIGRATING;
  23618. -
  23619. - spin_unlock(&base->lock);
  23620. - base = new_base;
  23621. - spin_lock(&base->lock);
  23622. - WRITE_ONCE(timer->flags,
  23623. - (timer->flags & ~TIMER_BASEMASK) | base->cpu);
  23624. - }
  23625. + if (likely(base->running_timer != timer))
  23626. + base = switch_timer_base(timer, base, new_base);
  23627. }
  23628. timer->expires = expires;
  23629. @@ -1006,6 +1034,33 @@
  23630. }
  23631. EXPORT_SYMBOL_GPL(add_timer_on);
  23632. +#ifdef CONFIG_PREEMPT_RT_FULL
  23633. +/*
  23634. + * Wait for a running timer
  23635. + */
  23636. +static void wait_for_running_timer(struct timer_list *timer)
  23637. +{
  23638. + struct tvec_base *base;
  23639. + u32 tf = timer->flags;
  23640. +
  23641. + if (tf & TIMER_MIGRATING)
  23642. + return;
  23643. +
  23644. + base = per_cpu_ptr(&tvec_bases, tf & TIMER_CPUMASK);
  23645. + wait_event(base->wait_for_running_timer,
  23646. + base->running_timer != timer);
  23647. +}
  23648. +
  23649. +# define wakeup_timer_waiters(b) wake_up_all(&(b)->wait_for_running_timer)
  23650. +#else
  23651. +static inline void wait_for_running_timer(struct timer_list *timer)
  23652. +{
  23653. + cpu_relax();
  23654. +}
  23655. +
  23656. +# define wakeup_timer_waiters(b) do { } while (0)
  23657. +#endif
  23658. +
  23659. /**
  23660. * del_timer - deactive a timer.
  23661. * @timer: the timer to be deactivated
  23662. @@ -1063,7 +1118,7 @@
  23663. }
  23664. EXPORT_SYMBOL(try_to_del_timer_sync);
  23665. -#ifdef CONFIG_SMP
  23666. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  23667. /**
  23668. * del_timer_sync - deactivate a timer and wait for the handler to finish.
  23669. * @timer: the timer to be deactivated
  23670. @@ -1123,7 +1178,7 @@
  23671. int ret = try_to_del_timer_sync(timer);
  23672. if (ret >= 0)
  23673. return ret;
  23674. - cpu_relax();
  23675. + wait_for_running_timer(timer);
  23676. }
  23677. }
  23678. EXPORT_SYMBOL(del_timer_sync);
  23679. @@ -1248,16 +1303,18 @@
  23680. if (irqsafe) {
  23681. spin_unlock(&base->lock);
  23682. call_timer_fn(timer, fn, data);
  23683. + base->running_timer = NULL;
  23684. spin_lock(&base->lock);
  23685. } else {
  23686. spin_unlock_irq(&base->lock);
  23687. call_timer_fn(timer, fn, data);
  23688. + base->running_timer = NULL;
  23689. spin_lock_irq(&base->lock);
  23690. }
  23691. }
  23692. }
  23693. - base->running_timer = NULL;
  23694. spin_unlock_irq(&base->lock);
  23695. + wakeup_timer_waiters(base);
  23696. }
  23697. #ifdef CONFIG_NO_HZ_COMMON
  23698. @@ -1390,6 +1447,14 @@
  23699. if (cpu_is_offline(smp_processor_id()))
  23700. return expires;
  23701. +#ifdef CONFIG_PREEMPT_RT_FULL
  23702. + /*
  23703. + * On PREEMPT_RT we cannot sleep here. As a result we can't take
  23704. + * the base lock to check when the next timer is pending and so
  23705. + * we assume the next jiffy.
  23706. + */
  23707. + return basem + TICK_NSEC;
  23708. +#endif
  23709. spin_lock(&base->lock);
  23710. if (base->active_timers) {
  23711. if (time_before_eq(base->next_timer, base->timer_jiffies))
  23712. @@ -1416,13 +1481,13 @@
  23713. /* Note: this timer irq context must be accounted for as well. */
  23714. account_process_tick(p, user_tick);
  23715. + scheduler_tick();
  23716. run_local_timers();
  23717. rcu_check_callbacks(user_tick);
  23718. -#ifdef CONFIG_IRQ_WORK
  23719. +#if defined(CONFIG_IRQ_WORK)
  23720. if (in_irq())
  23721. irq_work_tick();
  23722. #endif
  23723. - scheduler_tick();
  23724. run_posix_cpu_timers(p);
  23725. }
  23726. @@ -1433,6 +1498,8 @@
  23727. {
  23728. struct tvec_base *base = this_cpu_ptr(&tvec_bases);
  23729. + irq_work_tick_soft();
  23730. +
  23731. if (time_after_eq(jiffies, base->timer_jiffies))
  23732. __run_timers(base);
  23733. }
  23734. @@ -1589,7 +1656,7 @@
  23735. BUG_ON(cpu_online(cpu));
  23736. old_base = per_cpu_ptr(&tvec_bases, cpu);
  23737. - new_base = get_cpu_ptr(&tvec_bases);
  23738. + new_base = get_local_ptr(&tvec_bases);
  23739. /*
  23740. * The caller is globally serialized and nobody else
  23741. * takes two locks at once, deadlock is not possible.
  23742. @@ -1613,7 +1680,7 @@
  23743. spin_unlock(&old_base->lock);
  23744. spin_unlock_irq(&new_base->lock);
  23745. - put_cpu_ptr(&tvec_bases);
  23746. + put_local_ptr(&tvec_bases);
  23747. }
  23748. static int timer_cpu_notify(struct notifier_block *self,
  23749. @@ -1645,6 +1712,9 @@
  23750. base->cpu = cpu;
  23751. spin_lock_init(&base->lock);
  23752. +#ifdef CONFIG_PREEMPT_RT_FULL
  23753. + init_waitqueue_head(&base->wait_for_running_timer);
  23754. +#endif
  23755. base->timer_jiffies = jiffies;
  23756. base->next_timer = base->timer_jiffies;
  23757. diff -Nur linux-4.4.28.orig/kernel/trace/Kconfig linux-4.4.28/kernel/trace/Kconfig
  23758. --- linux-4.4.28.orig/kernel/trace/Kconfig 2016-10-28 09:53:25.000000000 +0200
  23759. +++ linux-4.4.28/kernel/trace/Kconfig 2016-11-01 21:26:58.804344090 +0100
  23760. @@ -187,6 +187,24 @@
  23761. enabled. This option and the preempt-off timing option can be
  23762. used together or separately.)
  23763. +config INTERRUPT_OFF_HIST
  23764. + bool "Interrupts-off Latency Histogram"
  23765. + depends on IRQSOFF_TRACER
  23766. + help
  23767. + This option generates continuously updated histograms (one per cpu)
  23768. + of the duration of time periods with interrupts disabled. The
  23769. + histograms are disabled by default. To enable them, write a non-zero
  23770. + number to
  23771. +
  23772. + /sys/kernel/debug/tracing/latency_hist/enable/preemptirqsoff
  23773. +
  23774. + If PREEMPT_OFF_HIST is also selected, additional histograms (one
  23775. + per cpu) are generated that accumulate the duration of time periods
  23776. + when both interrupts and preemption are disabled. The histogram data
  23777. + will be located in the debug file system at
  23778. +
  23779. + /sys/kernel/debug/tracing/latency_hist/irqsoff
  23780. +
  23781. config PREEMPT_TRACER
  23782. bool "Preemption-off Latency Tracer"
  23783. default n
  23784. @@ -211,6 +229,24 @@
  23785. enabled. This option and the irqs-off timing option can be
  23786. used together or separately.)
  23787. +config PREEMPT_OFF_HIST
  23788. + bool "Preemption-off Latency Histogram"
  23789. + depends on PREEMPT_TRACER
  23790. + help
  23791. + This option generates continuously updated histograms (one per cpu)
  23792. + of the duration of time periods with preemption disabled. The
  23793. + histograms are disabled by default. To enable them, write a non-zero
  23794. + number to
  23795. +
  23796. + /sys/kernel/debug/tracing/latency_hist/enable/preemptirqsoff
  23797. +
  23798. + If INTERRUPT_OFF_HIST is also selected, additional histograms (one
  23799. + per cpu) are generated that accumulate the duration of time periods
  23800. + when both interrupts and preemption are disabled. The histogram data
  23801. + will be located in the debug file system at
  23802. +
  23803. + /sys/kernel/debug/tracing/latency_hist/preemptoff
  23804. +
  23805. config SCHED_TRACER
  23806. bool "Scheduling Latency Tracer"
  23807. select GENERIC_TRACER
  23808. @@ -221,6 +257,74 @@
  23809. This tracer tracks the latency of the highest priority task
  23810. to be scheduled in, starting from the point it has woken up.
  23811. +config WAKEUP_LATENCY_HIST
  23812. + bool "Scheduling Latency Histogram"
  23813. + depends on SCHED_TRACER
  23814. + help
  23815. + This option generates continuously updated histograms (one per cpu)
  23816. + of the scheduling latency of the highest priority task.
  23817. + The histograms are disabled by default. To enable them, write a
  23818. + non-zero number to
  23819. +
  23820. + /sys/kernel/debug/tracing/latency_hist/enable/wakeup
  23821. +
  23822. + Two different algorithms are used, one to determine the latency of
  23823. + processes that exclusively use the highest priority of the system and
  23824. + another one to determine the latency of processes that share the
  23825. + highest system priority with other processes. The former is used to
  23826. + improve hardware and system software, the latter to optimize the
  23827. + priority design of a given system. The histogram data will be
  23828. + located in the debug file system at
  23829. +
  23830. + /sys/kernel/debug/tracing/latency_hist/wakeup
  23831. +
  23832. + and
  23833. +
  23834. + /sys/kernel/debug/tracing/latency_hist/wakeup/sharedprio
  23835. +
  23836. + If both Scheduling Latency Histogram and Missed Timer Offsets
  23837. + Histogram are selected, additional histogram data will be collected
  23838. + that contain, in addition to the wakeup latency, the timer latency, in
  23839. + case the wakeup was triggered by an expired timer. These histograms
  23840. + are available in the
  23841. +
  23842. + /sys/kernel/debug/tracing/latency_hist/timerandwakeup
  23843. +
  23844. + directory. They reflect the apparent interrupt and scheduling latency
  23845. + and are best suitable to determine the worst-case latency of a given
  23846. + system. To enable these histograms, write a non-zero number to
  23847. +
  23848. + /sys/kernel/debug/tracing/latency_hist/enable/timerandwakeup
  23849. +
  23850. +config MISSED_TIMER_OFFSETS_HIST
  23851. + depends on HIGH_RES_TIMERS
  23852. + select GENERIC_TRACER
  23853. + bool "Missed Timer Offsets Histogram"
  23854. + help
  23855. + Generate a histogram of missed timer offsets in microseconds. The
  23856. + histograms are disabled by default. To enable them, write a non-zero
  23857. + number to
  23858. +
  23859. + /sys/kernel/debug/tracing/latency_hist/enable/missed_timer_offsets
  23860. +
  23861. + The histogram data will be located in the debug file system at
  23862. +
  23863. + /sys/kernel/debug/tracing/latency_hist/missed_timer_offsets
  23864. +
  23865. + If both Scheduling Latency Histogram and Missed Timer Offsets
  23866. + Histogram are selected, additional histogram data will be collected
  23867. + that contain, in addition to the wakeup latency, the timer latency, in
  23868. + case the wakeup was triggered by an expired timer. These histograms
  23869. + are available in the
  23870. +
  23871. + /sys/kernel/debug/tracing/latency_hist/timerandwakeup
  23872. +
  23873. + directory. They reflect the apparent interrupt and scheduling latency
  23874. + and are best suitable to determine the worst-case latency of a given
  23875. + system. To enable these histograms, write a non-zero number to
  23876. +
  23877. + /sys/kernel/debug/tracing/latency_hist/enable/timerandwakeup
  23878. +
  23879. config ENABLE_DEFAULT_TRACERS
  23880. bool "Trace process context switches and events"
  23881. depends on !GENERIC_TRACER
  23882. diff -Nur linux-4.4.28.orig/kernel/trace/latency_hist.c linux-4.4.28/kernel/trace/latency_hist.c
  23883. --- linux-4.4.28.orig/kernel/trace/latency_hist.c 1970-01-01 01:00:00.000000000 +0100
  23884. +++ linux-4.4.28/kernel/trace/latency_hist.c 2016-11-01 21:26:58.804344090 +0100
  23885. @@ -0,0 +1,1178 @@
  23886. +/*
  23887. + * kernel/trace/latency_hist.c
  23888. + *
  23889. + * Add support for histograms of preemption-off latency and
  23890. + * interrupt-off latency and wakeup latency, it depends on
  23891. + * Real-Time Preemption Support.
  23892. + *
  23893. + * Copyright (C) 2005 MontaVista Software, Inc.
  23894. + * Yi Yang <yyang@ch.mvista.com>
  23895. + *
  23896. + * Converted to work with the new latency tracer.
  23897. + * Copyright (C) 2008 Red Hat, Inc.
  23898. + * Steven Rostedt <srostedt@redhat.com>
  23899. + *
  23900. + */
  23901. +#include <linux/module.h>
  23902. +#include <linux/debugfs.h>
  23903. +#include <linux/seq_file.h>
  23904. +#include <linux/percpu.h>
  23905. +#include <linux/kallsyms.h>
  23906. +#include <linux/uaccess.h>
  23907. +#include <linux/sched.h>
  23908. +#include <linux/sched/rt.h>
  23909. +#include <linux/slab.h>
  23910. +#include <linux/atomic.h>
  23911. +#include <asm/div64.h>
  23912. +
  23913. +#include "trace.h"
  23914. +#include <trace/events/sched.h>
  23915. +
  23916. +#define NSECS_PER_USECS 1000L
  23917. +
  23918. +#define CREATE_TRACE_POINTS
  23919. +#include <trace/events/hist.h>
  23920. +
  23921. +enum {
  23922. + IRQSOFF_LATENCY = 0,
  23923. + PREEMPTOFF_LATENCY,
  23924. + PREEMPTIRQSOFF_LATENCY,
  23925. + WAKEUP_LATENCY,
  23926. + WAKEUP_LATENCY_SHAREDPRIO,
  23927. + MISSED_TIMER_OFFSETS,
  23928. + TIMERANDWAKEUP_LATENCY,
  23929. + MAX_LATENCY_TYPE,
  23930. +};
  23931. +
  23932. +#define MAX_ENTRY_NUM 10240
  23933. +
  23934. +struct hist_data {
  23935. + atomic_t hist_mode; /* 0 log, 1 don't log */
  23936. + long offset; /* set it to MAX_ENTRY_NUM/2 for a bipolar scale */
  23937. + long min_lat;
  23938. + long max_lat;
  23939. + unsigned long long below_hist_bound_samples;
  23940. + unsigned long long above_hist_bound_samples;
  23941. + long long accumulate_lat;
  23942. + unsigned long long total_samples;
  23943. + unsigned long long hist_array[MAX_ENTRY_NUM];
  23944. +};
  23945. +
  23946. +struct enable_data {
  23947. + int latency_type;
  23948. + int enabled;
  23949. +};
  23950. +
  23951. +static char *latency_hist_dir_root = "latency_hist";
  23952. +
  23953. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  23954. +static DEFINE_PER_CPU(struct hist_data, irqsoff_hist);
  23955. +static char *irqsoff_hist_dir = "irqsoff";
  23956. +static DEFINE_PER_CPU(cycles_t, hist_irqsoff_start);
  23957. +static DEFINE_PER_CPU(int, hist_irqsoff_counting);
  23958. +#endif
  23959. +
  23960. +#ifdef CONFIG_PREEMPT_OFF_HIST
  23961. +static DEFINE_PER_CPU(struct hist_data, preemptoff_hist);
  23962. +static char *preemptoff_hist_dir = "preemptoff";
  23963. +static DEFINE_PER_CPU(cycles_t, hist_preemptoff_start);
  23964. +static DEFINE_PER_CPU(int, hist_preemptoff_counting);
  23965. +#endif
  23966. +
  23967. +#if defined(CONFIG_PREEMPT_OFF_HIST) && defined(CONFIG_INTERRUPT_OFF_HIST)
  23968. +static DEFINE_PER_CPU(struct hist_data, preemptirqsoff_hist);
  23969. +static char *preemptirqsoff_hist_dir = "preemptirqsoff";
  23970. +static DEFINE_PER_CPU(cycles_t, hist_preemptirqsoff_start);
  23971. +static DEFINE_PER_CPU(int, hist_preemptirqsoff_counting);
  23972. +#endif
  23973. +
  23974. +#if defined(CONFIG_PREEMPT_OFF_HIST) || defined(CONFIG_INTERRUPT_OFF_HIST)
  23975. +static notrace void probe_preemptirqsoff_hist(void *v, int reason, int start);
  23976. +static struct enable_data preemptirqsoff_enabled_data = {
  23977. + .latency_type = PREEMPTIRQSOFF_LATENCY,
  23978. + .enabled = 0,
  23979. +};
  23980. +#endif
  23981. +
  23982. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  23983. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  23984. +struct maxlatproc_data {
  23985. + char comm[FIELD_SIZEOF(struct task_struct, comm)];
  23986. + char current_comm[FIELD_SIZEOF(struct task_struct, comm)];
  23987. + int pid;
  23988. + int current_pid;
  23989. + int prio;
  23990. + int current_prio;
  23991. + long latency;
  23992. + long timeroffset;
  23993. + cycle_t timestamp;
  23994. +};
  23995. +#endif
  23996. +
  23997. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  23998. +static DEFINE_PER_CPU(struct hist_data, wakeup_latency_hist);
  23999. +static DEFINE_PER_CPU(struct hist_data, wakeup_latency_hist_sharedprio);
  24000. +static char *wakeup_latency_hist_dir = "wakeup";
  24001. +static char *wakeup_latency_hist_dir_sharedprio = "sharedprio";
  24002. +static notrace void probe_wakeup_latency_hist_start(void *v,
  24003. + struct task_struct *p);
  24004. +static notrace void probe_wakeup_latency_hist_stop(void *v,
  24005. + bool preempt, struct task_struct *prev, struct task_struct *next);
  24006. +static notrace void probe_sched_migrate_task(void *,
  24007. + struct task_struct *task, int cpu);
  24008. +static struct enable_data wakeup_latency_enabled_data = {
  24009. + .latency_type = WAKEUP_LATENCY,
  24010. + .enabled = 0,
  24011. +};
  24012. +static DEFINE_PER_CPU(struct maxlatproc_data, wakeup_maxlatproc);
  24013. +static DEFINE_PER_CPU(struct maxlatproc_data, wakeup_maxlatproc_sharedprio);
  24014. +static DEFINE_PER_CPU(struct task_struct *, wakeup_task);
  24015. +static DEFINE_PER_CPU(int, wakeup_sharedprio);
  24016. +static unsigned long wakeup_pid;
  24017. +#endif
  24018. +
  24019. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  24020. +static DEFINE_PER_CPU(struct hist_data, missed_timer_offsets);
  24021. +static char *missed_timer_offsets_dir = "missed_timer_offsets";
  24022. +static notrace void probe_hrtimer_interrupt(void *v, int cpu,
  24023. + long long offset, struct task_struct *curr, struct task_struct *task);
  24024. +static struct enable_data missed_timer_offsets_enabled_data = {
  24025. + .latency_type = MISSED_TIMER_OFFSETS,
  24026. + .enabled = 0,
  24027. +};
  24028. +static DEFINE_PER_CPU(struct maxlatproc_data, missed_timer_offsets_maxlatproc);
  24029. +static unsigned long missed_timer_offsets_pid;
  24030. +#endif
  24031. +
  24032. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  24033. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24034. +static DEFINE_PER_CPU(struct hist_data, timerandwakeup_latency_hist);
  24035. +static char *timerandwakeup_latency_hist_dir = "timerandwakeup";
  24036. +static struct enable_data timerandwakeup_enabled_data = {
  24037. + .latency_type = TIMERANDWAKEUP_LATENCY,
  24038. + .enabled = 0,
  24039. +};
  24040. +static DEFINE_PER_CPU(struct maxlatproc_data, timerandwakeup_maxlatproc);
  24041. +#endif
  24042. +
  24043. +void notrace latency_hist(int latency_type, int cpu, long latency,
  24044. + long timeroffset, cycle_t stop,
  24045. + struct task_struct *p)
  24046. +{
  24047. + struct hist_data *my_hist;
  24048. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  24049. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24050. + struct maxlatproc_data *mp = NULL;
  24051. +#endif
  24052. +
  24053. + if (!cpu_possible(cpu) || latency_type < 0 ||
  24054. + latency_type >= MAX_LATENCY_TYPE)
  24055. + return;
  24056. +
  24057. + switch (latency_type) {
  24058. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  24059. + case IRQSOFF_LATENCY:
  24060. + my_hist = &per_cpu(irqsoff_hist, cpu);
  24061. + break;
  24062. +#endif
  24063. +#ifdef CONFIG_PREEMPT_OFF_HIST
  24064. + case PREEMPTOFF_LATENCY:
  24065. + my_hist = &per_cpu(preemptoff_hist, cpu);
  24066. + break;
  24067. +#endif
  24068. +#if defined(CONFIG_PREEMPT_OFF_HIST) && defined(CONFIG_INTERRUPT_OFF_HIST)
  24069. + case PREEMPTIRQSOFF_LATENCY:
  24070. + my_hist = &per_cpu(preemptirqsoff_hist, cpu);
  24071. + break;
  24072. +#endif
  24073. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  24074. + case WAKEUP_LATENCY:
  24075. + my_hist = &per_cpu(wakeup_latency_hist, cpu);
  24076. + mp = &per_cpu(wakeup_maxlatproc, cpu);
  24077. + break;
  24078. + case WAKEUP_LATENCY_SHAREDPRIO:
  24079. + my_hist = &per_cpu(wakeup_latency_hist_sharedprio, cpu);
  24080. + mp = &per_cpu(wakeup_maxlatproc_sharedprio, cpu);
  24081. + break;
  24082. +#endif
  24083. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  24084. + case MISSED_TIMER_OFFSETS:
  24085. + my_hist = &per_cpu(missed_timer_offsets, cpu);
  24086. + mp = &per_cpu(missed_timer_offsets_maxlatproc, cpu);
  24087. + break;
  24088. +#endif
  24089. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  24090. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24091. + case TIMERANDWAKEUP_LATENCY:
  24092. + my_hist = &per_cpu(timerandwakeup_latency_hist, cpu);
  24093. + mp = &per_cpu(timerandwakeup_maxlatproc, cpu);
  24094. + break;
  24095. +#endif
  24096. +
  24097. + default:
  24098. + return;
  24099. + }
  24100. +
  24101. + latency += my_hist->offset;
  24102. +
  24103. + if (atomic_read(&my_hist->hist_mode) == 0)
  24104. + return;
  24105. +
  24106. + if (latency < 0 || latency >= MAX_ENTRY_NUM) {
  24107. + if (latency < 0)
  24108. + my_hist->below_hist_bound_samples++;
  24109. + else
  24110. + my_hist->above_hist_bound_samples++;
  24111. + } else
  24112. + my_hist->hist_array[latency]++;
  24113. +
  24114. + if (unlikely(latency > my_hist->max_lat ||
  24115. + my_hist->min_lat == LONG_MAX)) {
  24116. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  24117. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24118. + if (latency_type == WAKEUP_LATENCY ||
  24119. + latency_type == WAKEUP_LATENCY_SHAREDPRIO ||
  24120. + latency_type == MISSED_TIMER_OFFSETS ||
  24121. + latency_type == TIMERANDWAKEUP_LATENCY) {
  24122. + strncpy(mp->comm, p->comm, sizeof(mp->comm));
  24123. + strncpy(mp->current_comm, current->comm,
  24124. + sizeof(mp->current_comm));
  24125. + mp->pid = task_pid_nr(p);
  24126. + mp->current_pid = task_pid_nr(current);
  24127. + mp->prio = p->prio;
  24128. + mp->current_prio = current->prio;
  24129. + mp->latency = latency;
  24130. + mp->timeroffset = timeroffset;
  24131. + mp->timestamp = stop;
  24132. + }
  24133. +#endif
  24134. + my_hist->max_lat = latency;
  24135. + }
  24136. + if (unlikely(latency < my_hist->min_lat))
  24137. + my_hist->min_lat = latency;
  24138. + my_hist->total_samples++;
  24139. + my_hist->accumulate_lat += latency;
  24140. +}
  24141. +
  24142. +static void *l_start(struct seq_file *m, loff_t *pos)
  24143. +{
  24144. + loff_t *index_ptr = NULL;
  24145. + loff_t index = *pos;
  24146. + struct hist_data *my_hist = m->private;
  24147. +
  24148. + if (index == 0) {
  24149. + char minstr[32], avgstr[32], maxstr[32];
  24150. +
  24151. + atomic_dec(&my_hist->hist_mode);
  24152. +
  24153. + if (likely(my_hist->total_samples)) {
  24154. + long avg = (long) div64_s64(my_hist->accumulate_lat,
  24155. + my_hist->total_samples);
  24156. + snprintf(minstr, sizeof(minstr), "%ld",
  24157. + my_hist->min_lat - my_hist->offset);
  24158. + snprintf(avgstr, sizeof(avgstr), "%ld",
  24159. + avg - my_hist->offset);
  24160. + snprintf(maxstr, sizeof(maxstr), "%ld",
  24161. + my_hist->max_lat - my_hist->offset);
  24162. + } else {
  24163. + strcpy(minstr, "<undef>");
  24164. + strcpy(avgstr, minstr);
  24165. + strcpy(maxstr, minstr);
  24166. + }
  24167. +
  24168. + seq_printf(m, "#Minimum latency: %s microseconds\n"
  24169. + "#Average latency: %s microseconds\n"
  24170. + "#Maximum latency: %s microseconds\n"
  24171. + "#Total samples: %llu\n"
  24172. + "#There are %llu samples lower than %ld"
  24173. + " microseconds.\n"
  24174. + "#There are %llu samples greater or equal"
  24175. + " than %ld microseconds.\n"
  24176. + "#usecs\t%16s\n",
  24177. + minstr, avgstr, maxstr,
  24178. + my_hist->total_samples,
  24179. + my_hist->below_hist_bound_samples,
  24180. + -my_hist->offset,
  24181. + my_hist->above_hist_bound_samples,
  24182. + MAX_ENTRY_NUM - my_hist->offset,
  24183. + "samples");
  24184. + }
  24185. + if (index < MAX_ENTRY_NUM) {
  24186. + index_ptr = kmalloc(sizeof(loff_t), GFP_KERNEL);
  24187. + if (index_ptr)
  24188. + *index_ptr = index;
  24189. + }
  24190. +
  24191. + return index_ptr;
  24192. +}
  24193. +
  24194. +static void *l_next(struct seq_file *m, void *p, loff_t *pos)
  24195. +{
  24196. + loff_t *index_ptr = p;
  24197. + struct hist_data *my_hist = m->private;
  24198. +
  24199. + if (++*pos >= MAX_ENTRY_NUM) {
  24200. + atomic_inc(&my_hist->hist_mode);
  24201. + return NULL;
  24202. + }
  24203. + *index_ptr = *pos;
  24204. + return index_ptr;
  24205. +}
  24206. +
  24207. +static void l_stop(struct seq_file *m, void *p)
  24208. +{
  24209. + kfree(p);
  24210. +}
  24211. +
  24212. +static int l_show(struct seq_file *m, void *p)
  24213. +{
  24214. + int index = *(loff_t *) p;
  24215. + struct hist_data *my_hist = m->private;
  24216. +
  24217. + seq_printf(m, "%6ld\t%16llu\n", index - my_hist->offset,
  24218. + my_hist->hist_array[index]);
  24219. + return 0;
  24220. +}
  24221. +
  24222. +static const struct seq_operations latency_hist_seq_op = {
  24223. + .start = l_start,
  24224. + .next = l_next,
  24225. + .stop = l_stop,
  24226. + .show = l_show
  24227. +};
  24228. +
  24229. +static int latency_hist_open(struct inode *inode, struct file *file)
  24230. +{
  24231. + int ret;
  24232. +
  24233. + ret = seq_open(file, &latency_hist_seq_op);
  24234. + if (!ret) {
  24235. + struct seq_file *seq = file->private_data;
  24236. + seq->private = inode->i_private;
  24237. + }
  24238. + return ret;
  24239. +}
  24240. +
  24241. +static const struct file_operations latency_hist_fops = {
  24242. + .open = latency_hist_open,
  24243. + .read = seq_read,
  24244. + .llseek = seq_lseek,
  24245. + .release = seq_release,
  24246. +};
  24247. +
  24248. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  24249. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24250. +static void clear_maxlatprocdata(struct maxlatproc_data *mp)
  24251. +{
  24252. + mp->comm[0] = mp->current_comm[0] = '\0';
  24253. + mp->prio = mp->current_prio = mp->pid = mp->current_pid =
  24254. + mp->latency = mp->timeroffset = -1;
  24255. + mp->timestamp = 0;
  24256. +}
  24257. +#endif
  24258. +
  24259. +static void hist_reset(struct hist_data *hist)
  24260. +{
  24261. + atomic_dec(&hist->hist_mode);
  24262. +
  24263. + memset(hist->hist_array, 0, sizeof(hist->hist_array));
  24264. + hist->below_hist_bound_samples = 0ULL;
  24265. + hist->above_hist_bound_samples = 0ULL;
  24266. + hist->min_lat = LONG_MAX;
  24267. + hist->max_lat = LONG_MIN;
  24268. + hist->total_samples = 0ULL;
  24269. + hist->accumulate_lat = 0LL;
  24270. +
  24271. + atomic_inc(&hist->hist_mode);
  24272. +}
  24273. +
  24274. +static ssize_t
  24275. +latency_hist_reset(struct file *file, const char __user *a,
  24276. + size_t size, loff_t *off)
  24277. +{
  24278. + int cpu;
  24279. + struct hist_data *hist = NULL;
  24280. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  24281. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24282. + struct maxlatproc_data *mp = NULL;
  24283. +#endif
  24284. + off_t latency_type = (off_t) file->private_data;
  24285. +
  24286. + for_each_online_cpu(cpu) {
  24287. +
  24288. + switch (latency_type) {
  24289. +#ifdef CONFIG_PREEMPT_OFF_HIST
  24290. + case PREEMPTOFF_LATENCY:
  24291. + hist = &per_cpu(preemptoff_hist, cpu);
  24292. + break;
  24293. +#endif
  24294. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  24295. + case IRQSOFF_LATENCY:
  24296. + hist = &per_cpu(irqsoff_hist, cpu);
  24297. + break;
  24298. +#endif
  24299. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  24300. + case PREEMPTIRQSOFF_LATENCY:
  24301. + hist = &per_cpu(preemptirqsoff_hist, cpu);
  24302. + break;
  24303. +#endif
  24304. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  24305. + case WAKEUP_LATENCY:
  24306. + hist = &per_cpu(wakeup_latency_hist, cpu);
  24307. + mp = &per_cpu(wakeup_maxlatproc, cpu);
  24308. + break;
  24309. + case WAKEUP_LATENCY_SHAREDPRIO:
  24310. + hist = &per_cpu(wakeup_latency_hist_sharedprio, cpu);
  24311. + mp = &per_cpu(wakeup_maxlatproc_sharedprio, cpu);
  24312. + break;
  24313. +#endif
  24314. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  24315. + case MISSED_TIMER_OFFSETS:
  24316. + hist = &per_cpu(missed_timer_offsets, cpu);
  24317. + mp = &per_cpu(missed_timer_offsets_maxlatproc, cpu);
  24318. + break;
  24319. +#endif
  24320. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  24321. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24322. + case TIMERANDWAKEUP_LATENCY:
  24323. + hist = &per_cpu(timerandwakeup_latency_hist, cpu);
  24324. + mp = &per_cpu(timerandwakeup_maxlatproc, cpu);
  24325. + break;
  24326. +#endif
  24327. + }
  24328. +
  24329. + hist_reset(hist);
  24330. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  24331. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24332. + if (latency_type == WAKEUP_LATENCY ||
  24333. + latency_type == WAKEUP_LATENCY_SHAREDPRIO ||
  24334. + latency_type == MISSED_TIMER_OFFSETS ||
  24335. + latency_type == TIMERANDWAKEUP_LATENCY)
  24336. + clear_maxlatprocdata(mp);
  24337. +#endif
  24338. + }
  24339. +
  24340. + return size;
  24341. +}
  24342. +
  24343. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  24344. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24345. +static ssize_t
  24346. +show_pid(struct file *file, char __user *ubuf, size_t cnt, loff_t *ppos)
  24347. +{
  24348. + char buf[64];
  24349. + int r;
  24350. + unsigned long *this_pid = file->private_data;
  24351. +
  24352. + r = snprintf(buf, sizeof(buf), "%lu\n", *this_pid);
  24353. + return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  24354. +}
  24355. +
  24356. +static ssize_t do_pid(struct file *file, const char __user *ubuf,
  24357. + size_t cnt, loff_t *ppos)
  24358. +{
  24359. + char buf[64];
  24360. + unsigned long pid;
  24361. + unsigned long *this_pid = file->private_data;
  24362. +
  24363. + if (cnt >= sizeof(buf))
  24364. + return -EINVAL;
  24365. +
  24366. + if (copy_from_user(&buf, ubuf, cnt))
  24367. + return -EFAULT;
  24368. +
  24369. + buf[cnt] = '\0';
  24370. +
  24371. + if (kstrtoul(buf, 10, &pid))
  24372. + return -EINVAL;
  24373. +
  24374. + *this_pid = pid;
  24375. +
  24376. + return cnt;
  24377. +}
  24378. +#endif
  24379. +
  24380. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  24381. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24382. +static ssize_t
  24383. +show_maxlatproc(struct file *file, char __user *ubuf, size_t cnt, loff_t *ppos)
  24384. +{
  24385. + int r;
  24386. + struct maxlatproc_data *mp = file->private_data;
  24387. + int strmaxlen = (TASK_COMM_LEN * 2) + (8 * 8);
  24388. + unsigned long long t;
  24389. + unsigned long usecs, secs;
  24390. + char *buf;
  24391. +
  24392. + if (mp->pid == -1 || mp->current_pid == -1) {
  24393. + buf = "(none)\n";
  24394. + return simple_read_from_buffer(ubuf, cnt, ppos, buf,
  24395. + strlen(buf));
  24396. + }
  24397. +
  24398. + buf = kmalloc(strmaxlen, GFP_KERNEL);
  24399. + if (buf == NULL)
  24400. + return -ENOMEM;
  24401. +
  24402. + t = ns2usecs(mp->timestamp);
  24403. + usecs = do_div(t, USEC_PER_SEC);
  24404. + secs = (unsigned long) t;
  24405. + r = snprintf(buf, strmaxlen,
  24406. + "%d %d %ld (%ld) %s <- %d %d %s %lu.%06lu\n", mp->pid,
  24407. + MAX_RT_PRIO-1 - mp->prio, mp->latency, mp->timeroffset, mp->comm,
  24408. + mp->current_pid, MAX_RT_PRIO-1 - mp->current_prio, mp->current_comm,
  24409. + secs, usecs);
  24410. + r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  24411. + kfree(buf);
  24412. + return r;
  24413. +}
  24414. +#endif
  24415. +
  24416. +static ssize_t
  24417. +show_enable(struct file *file, char __user *ubuf, size_t cnt, loff_t *ppos)
  24418. +{
  24419. + char buf[64];
  24420. + struct enable_data *ed = file->private_data;
  24421. + int r;
  24422. +
  24423. + r = snprintf(buf, sizeof(buf), "%d\n", ed->enabled);
  24424. + return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  24425. +}
  24426. +
  24427. +static ssize_t
  24428. +do_enable(struct file *file, const char __user *ubuf, size_t cnt, loff_t *ppos)
  24429. +{
  24430. + char buf[64];
  24431. + long enable;
  24432. + struct enable_data *ed = file->private_data;
  24433. +
  24434. + if (cnt >= sizeof(buf))
  24435. + return -EINVAL;
  24436. +
  24437. + if (copy_from_user(&buf, ubuf, cnt))
  24438. + return -EFAULT;
  24439. +
  24440. + buf[cnt] = 0;
  24441. +
  24442. + if (kstrtoul(buf, 10, &enable))
  24443. + return -EINVAL;
  24444. +
  24445. + if ((enable && ed->enabled) || (!enable && !ed->enabled))
  24446. + return cnt;
  24447. +
  24448. + if (enable) {
  24449. + int ret;
  24450. +
  24451. + switch (ed->latency_type) {
  24452. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  24453. + case PREEMPTIRQSOFF_LATENCY:
  24454. + ret = register_trace_preemptirqsoff_hist(
  24455. + probe_preemptirqsoff_hist, NULL);
  24456. + if (ret) {
  24457. + pr_info("wakeup trace: Couldn't assign "
  24458. + "probe_preemptirqsoff_hist "
  24459. + "to trace_preemptirqsoff_hist\n");
  24460. + return ret;
  24461. + }
  24462. + break;
  24463. +#endif
  24464. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  24465. + case WAKEUP_LATENCY:
  24466. + ret = register_trace_sched_wakeup(
  24467. + probe_wakeup_latency_hist_start, NULL);
  24468. + if (ret) {
  24469. + pr_info("wakeup trace: Couldn't assign "
  24470. + "probe_wakeup_latency_hist_start "
  24471. + "to trace_sched_wakeup\n");
  24472. + return ret;
  24473. + }
  24474. + ret = register_trace_sched_wakeup_new(
  24475. + probe_wakeup_latency_hist_start, NULL);
  24476. + if (ret) {
  24477. + pr_info("wakeup trace: Couldn't assign "
  24478. + "probe_wakeup_latency_hist_start "
  24479. + "to trace_sched_wakeup_new\n");
  24480. + unregister_trace_sched_wakeup(
  24481. + probe_wakeup_latency_hist_start, NULL);
  24482. + return ret;
  24483. + }
  24484. + ret = register_trace_sched_switch(
  24485. + probe_wakeup_latency_hist_stop, NULL);
  24486. + if (ret) {
  24487. + pr_info("wakeup trace: Couldn't assign "
  24488. + "probe_wakeup_latency_hist_stop "
  24489. + "to trace_sched_switch\n");
  24490. + unregister_trace_sched_wakeup(
  24491. + probe_wakeup_latency_hist_start, NULL);
  24492. + unregister_trace_sched_wakeup_new(
  24493. + probe_wakeup_latency_hist_start, NULL);
  24494. + return ret;
  24495. + }
  24496. + ret = register_trace_sched_migrate_task(
  24497. + probe_sched_migrate_task, NULL);
  24498. + if (ret) {
  24499. + pr_info("wakeup trace: Couldn't assign "
  24500. + "probe_sched_migrate_task "
  24501. + "to trace_sched_migrate_task\n");
  24502. + unregister_trace_sched_wakeup(
  24503. + probe_wakeup_latency_hist_start, NULL);
  24504. + unregister_trace_sched_wakeup_new(
  24505. + probe_wakeup_latency_hist_start, NULL);
  24506. + unregister_trace_sched_switch(
  24507. + probe_wakeup_latency_hist_stop, NULL);
  24508. + return ret;
  24509. + }
  24510. + break;
  24511. +#endif
  24512. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  24513. + case MISSED_TIMER_OFFSETS:
  24514. + ret = register_trace_hrtimer_interrupt(
  24515. + probe_hrtimer_interrupt, NULL);
  24516. + if (ret) {
  24517. + pr_info("wakeup trace: Couldn't assign "
  24518. + "probe_hrtimer_interrupt "
  24519. + "to trace_hrtimer_interrupt\n");
  24520. + return ret;
  24521. + }
  24522. + break;
  24523. +#endif
  24524. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  24525. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24526. + case TIMERANDWAKEUP_LATENCY:
  24527. + if (!wakeup_latency_enabled_data.enabled ||
  24528. + !missed_timer_offsets_enabled_data.enabled)
  24529. + return -EINVAL;
  24530. + break;
  24531. +#endif
  24532. + default:
  24533. + break;
  24534. + }
  24535. + } else {
  24536. + switch (ed->latency_type) {
  24537. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  24538. + case PREEMPTIRQSOFF_LATENCY:
  24539. + {
  24540. + int cpu;
  24541. +
  24542. + unregister_trace_preemptirqsoff_hist(
  24543. + probe_preemptirqsoff_hist, NULL);
  24544. + for_each_online_cpu(cpu) {
  24545. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  24546. + per_cpu(hist_irqsoff_counting,
  24547. + cpu) = 0;
  24548. +#endif
  24549. +#ifdef CONFIG_PREEMPT_OFF_HIST
  24550. + per_cpu(hist_preemptoff_counting,
  24551. + cpu) = 0;
  24552. +#endif
  24553. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  24554. + per_cpu(hist_preemptirqsoff_counting,
  24555. + cpu) = 0;
  24556. +#endif
  24557. + }
  24558. + }
  24559. + break;
  24560. +#endif
  24561. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  24562. + case WAKEUP_LATENCY:
  24563. + {
  24564. + int cpu;
  24565. +
  24566. + unregister_trace_sched_wakeup(
  24567. + probe_wakeup_latency_hist_start, NULL);
  24568. + unregister_trace_sched_wakeup_new(
  24569. + probe_wakeup_latency_hist_start, NULL);
  24570. + unregister_trace_sched_switch(
  24571. + probe_wakeup_latency_hist_stop, NULL);
  24572. + unregister_trace_sched_migrate_task(
  24573. + probe_sched_migrate_task, NULL);
  24574. +
  24575. + for_each_online_cpu(cpu) {
  24576. + per_cpu(wakeup_task, cpu) = NULL;
  24577. + per_cpu(wakeup_sharedprio, cpu) = 0;
  24578. + }
  24579. + }
  24580. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  24581. + timerandwakeup_enabled_data.enabled = 0;
  24582. +#endif
  24583. + break;
  24584. +#endif
  24585. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  24586. + case MISSED_TIMER_OFFSETS:
  24587. + unregister_trace_hrtimer_interrupt(
  24588. + probe_hrtimer_interrupt, NULL);
  24589. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  24590. + timerandwakeup_enabled_data.enabled = 0;
  24591. +#endif
  24592. + break;
  24593. +#endif
  24594. + default:
  24595. + break;
  24596. + }
  24597. + }
  24598. + ed->enabled = enable;
  24599. + return cnt;
  24600. +}
  24601. +
  24602. +static const struct file_operations latency_hist_reset_fops = {
  24603. + .open = tracing_open_generic,
  24604. + .write = latency_hist_reset,
  24605. +};
  24606. +
  24607. +static const struct file_operations enable_fops = {
  24608. + .open = tracing_open_generic,
  24609. + .read = show_enable,
  24610. + .write = do_enable,
  24611. +};
  24612. +
  24613. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  24614. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24615. +static const struct file_operations pid_fops = {
  24616. + .open = tracing_open_generic,
  24617. + .read = show_pid,
  24618. + .write = do_pid,
  24619. +};
  24620. +
  24621. +static const struct file_operations maxlatproc_fops = {
  24622. + .open = tracing_open_generic,
  24623. + .read = show_maxlatproc,
  24624. +};
  24625. +#endif
  24626. +
  24627. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  24628. +static notrace void probe_preemptirqsoff_hist(void *v, int reason,
  24629. + int starthist)
  24630. +{
  24631. + int cpu = raw_smp_processor_id();
  24632. + int time_set = 0;
  24633. +
  24634. + if (starthist) {
  24635. + cycle_t uninitialized_var(start);
  24636. +
  24637. + if (!preempt_count() && !irqs_disabled())
  24638. + return;
  24639. +
  24640. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  24641. + if ((reason == IRQS_OFF || reason == TRACE_START) &&
  24642. + !per_cpu(hist_irqsoff_counting, cpu)) {
  24643. + per_cpu(hist_irqsoff_counting, cpu) = 1;
  24644. + start = ftrace_now(cpu);
  24645. + time_set++;
  24646. + per_cpu(hist_irqsoff_start, cpu) = start;
  24647. + }
  24648. +#endif
  24649. +
  24650. +#ifdef CONFIG_PREEMPT_OFF_HIST
  24651. + if ((reason == PREEMPT_OFF || reason == TRACE_START) &&
  24652. + !per_cpu(hist_preemptoff_counting, cpu)) {
  24653. + per_cpu(hist_preemptoff_counting, cpu) = 1;
  24654. + if (!(time_set++))
  24655. + start = ftrace_now(cpu);
  24656. + per_cpu(hist_preemptoff_start, cpu) = start;
  24657. + }
  24658. +#endif
  24659. +
  24660. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  24661. + if (per_cpu(hist_irqsoff_counting, cpu) &&
  24662. + per_cpu(hist_preemptoff_counting, cpu) &&
  24663. + !per_cpu(hist_preemptirqsoff_counting, cpu)) {
  24664. + per_cpu(hist_preemptirqsoff_counting, cpu) = 1;
  24665. + if (!time_set)
  24666. + start = ftrace_now(cpu);
  24667. + per_cpu(hist_preemptirqsoff_start, cpu) = start;
  24668. + }
  24669. +#endif
  24670. + } else {
  24671. + cycle_t uninitialized_var(stop);
  24672. +
  24673. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  24674. + if ((reason == IRQS_ON || reason == TRACE_STOP) &&
  24675. + per_cpu(hist_irqsoff_counting, cpu)) {
  24676. + cycle_t start = per_cpu(hist_irqsoff_start, cpu);
  24677. +
  24678. + stop = ftrace_now(cpu);
  24679. + time_set++;
  24680. + if (start) {
  24681. + long latency = ((long) (stop - start)) /
  24682. + NSECS_PER_USECS;
  24683. +
  24684. + latency_hist(IRQSOFF_LATENCY, cpu, latency, 0,
  24685. + stop, NULL);
  24686. + }
  24687. + per_cpu(hist_irqsoff_counting, cpu) = 0;
  24688. + }
  24689. +#endif
  24690. +
  24691. +#ifdef CONFIG_PREEMPT_OFF_HIST
  24692. + if ((reason == PREEMPT_ON || reason == TRACE_STOP) &&
  24693. + per_cpu(hist_preemptoff_counting, cpu)) {
  24694. + cycle_t start = per_cpu(hist_preemptoff_start, cpu);
  24695. +
  24696. + if (!(time_set++))
  24697. + stop = ftrace_now(cpu);
  24698. + if (start) {
  24699. + long latency = ((long) (stop - start)) /
  24700. + NSECS_PER_USECS;
  24701. +
  24702. + latency_hist(PREEMPTOFF_LATENCY, cpu, latency,
  24703. + 0, stop, NULL);
  24704. + }
  24705. + per_cpu(hist_preemptoff_counting, cpu) = 0;
  24706. + }
  24707. +#endif
  24708. +
  24709. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  24710. + if ((!per_cpu(hist_irqsoff_counting, cpu) ||
  24711. + !per_cpu(hist_preemptoff_counting, cpu)) &&
  24712. + per_cpu(hist_preemptirqsoff_counting, cpu)) {
  24713. + cycle_t start = per_cpu(hist_preemptirqsoff_start, cpu);
  24714. +
  24715. + if (!time_set)
  24716. + stop = ftrace_now(cpu);
  24717. + if (start) {
  24718. + long latency = ((long) (stop - start)) /
  24719. + NSECS_PER_USECS;
  24720. +
  24721. + latency_hist(PREEMPTIRQSOFF_LATENCY, cpu,
  24722. + latency, 0, stop, NULL);
  24723. + }
  24724. + per_cpu(hist_preemptirqsoff_counting, cpu) = 0;
  24725. + }
  24726. +#endif
  24727. + }
  24728. +}
  24729. +#endif
  24730. +
  24731. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  24732. +static DEFINE_RAW_SPINLOCK(wakeup_lock);
  24733. +static notrace void probe_sched_migrate_task(void *v, struct task_struct *task,
  24734. + int cpu)
  24735. +{
  24736. + int old_cpu = task_cpu(task);
  24737. +
  24738. + if (cpu != old_cpu) {
  24739. + unsigned long flags;
  24740. + struct task_struct *cpu_wakeup_task;
  24741. +
  24742. + raw_spin_lock_irqsave(&wakeup_lock, flags);
  24743. +
  24744. + cpu_wakeup_task = per_cpu(wakeup_task, old_cpu);
  24745. + if (task == cpu_wakeup_task) {
  24746. + put_task_struct(cpu_wakeup_task);
  24747. + per_cpu(wakeup_task, old_cpu) = NULL;
  24748. + cpu_wakeup_task = per_cpu(wakeup_task, cpu) = task;
  24749. + get_task_struct(cpu_wakeup_task);
  24750. + }
  24751. +
  24752. + raw_spin_unlock_irqrestore(&wakeup_lock, flags);
  24753. + }
  24754. +}
  24755. +
  24756. +static notrace void probe_wakeup_latency_hist_start(void *v,
  24757. + struct task_struct *p)
  24758. +{
  24759. + unsigned long flags;
  24760. + struct task_struct *curr = current;
  24761. + int cpu = task_cpu(p);
  24762. + struct task_struct *cpu_wakeup_task;
  24763. +
  24764. + raw_spin_lock_irqsave(&wakeup_lock, flags);
  24765. +
  24766. + cpu_wakeup_task = per_cpu(wakeup_task, cpu);
  24767. +
  24768. + if (wakeup_pid) {
  24769. + if ((cpu_wakeup_task && p->prio == cpu_wakeup_task->prio) ||
  24770. + p->prio == curr->prio)
  24771. + per_cpu(wakeup_sharedprio, cpu) = 1;
  24772. + if (likely(wakeup_pid != task_pid_nr(p)))
  24773. + goto out;
  24774. + } else {
  24775. + if (likely(!rt_task(p)) ||
  24776. + (cpu_wakeup_task && p->prio > cpu_wakeup_task->prio) ||
  24777. + p->prio > curr->prio)
  24778. + goto out;
  24779. + if ((cpu_wakeup_task && p->prio == cpu_wakeup_task->prio) ||
  24780. + p->prio == curr->prio)
  24781. + per_cpu(wakeup_sharedprio, cpu) = 1;
  24782. + }
  24783. +
  24784. + if (cpu_wakeup_task)
  24785. + put_task_struct(cpu_wakeup_task);
  24786. + cpu_wakeup_task = per_cpu(wakeup_task, cpu) = p;
  24787. + get_task_struct(cpu_wakeup_task);
  24788. + cpu_wakeup_task->preempt_timestamp_hist =
  24789. + ftrace_now(raw_smp_processor_id());
  24790. +out:
  24791. + raw_spin_unlock_irqrestore(&wakeup_lock, flags);
  24792. +}
  24793. +
  24794. +static notrace void probe_wakeup_latency_hist_stop(void *v,
  24795. + bool preempt, struct task_struct *prev, struct task_struct *next)
  24796. +{
  24797. + unsigned long flags;
  24798. + int cpu = task_cpu(next);
  24799. + long latency;
  24800. + cycle_t stop;
  24801. + struct task_struct *cpu_wakeup_task;
  24802. +
  24803. + raw_spin_lock_irqsave(&wakeup_lock, flags);
  24804. +
  24805. + cpu_wakeup_task = per_cpu(wakeup_task, cpu);
  24806. +
  24807. + if (cpu_wakeup_task == NULL)
  24808. + goto out;
  24809. +
  24810. + /* Already running? */
  24811. + if (unlikely(current == cpu_wakeup_task))
  24812. + goto out_reset;
  24813. +
  24814. + if (next != cpu_wakeup_task) {
  24815. + if (next->prio < cpu_wakeup_task->prio)
  24816. + goto out_reset;
  24817. +
  24818. + if (next->prio == cpu_wakeup_task->prio)
  24819. + per_cpu(wakeup_sharedprio, cpu) = 1;
  24820. +
  24821. + goto out;
  24822. + }
  24823. +
  24824. + if (current->prio == cpu_wakeup_task->prio)
  24825. + per_cpu(wakeup_sharedprio, cpu) = 1;
  24826. +
  24827. + /*
  24828. + * The task we are waiting for is about to be switched to.
  24829. + * Calculate latency and store it in histogram.
  24830. + */
  24831. + stop = ftrace_now(raw_smp_processor_id());
  24832. +
  24833. + latency = ((long) (stop - next->preempt_timestamp_hist)) /
  24834. + NSECS_PER_USECS;
  24835. +
  24836. + if (per_cpu(wakeup_sharedprio, cpu)) {
  24837. + latency_hist(WAKEUP_LATENCY_SHAREDPRIO, cpu, latency, 0, stop,
  24838. + next);
  24839. + per_cpu(wakeup_sharedprio, cpu) = 0;
  24840. + } else {
  24841. + latency_hist(WAKEUP_LATENCY, cpu, latency, 0, stop, next);
  24842. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  24843. + if (timerandwakeup_enabled_data.enabled) {
  24844. + latency_hist(TIMERANDWAKEUP_LATENCY, cpu,
  24845. + next->timer_offset + latency, next->timer_offset,
  24846. + stop, next);
  24847. + }
  24848. +#endif
  24849. + }
  24850. +
  24851. +out_reset:
  24852. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  24853. + next->timer_offset = 0;
  24854. +#endif
  24855. + put_task_struct(cpu_wakeup_task);
  24856. + per_cpu(wakeup_task, cpu) = NULL;
  24857. +out:
  24858. + raw_spin_unlock_irqrestore(&wakeup_lock, flags);
  24859. +}
  24860. +#endif
  24861. +
  24862. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  24863. +static notrace void probe_hrtimer_interrupt(void *v, int cpu,
  24864. + long long latency_ns, struct task_struct *curr,
  24865. + struct task_struct *task)
  24866. +{
  24867. + if (latency_ns <= 0 && task != NULL && rt_task(task) &&
  24868. + (task->prio < curr->prio ||
  24869. + (task->prio == curr->prio &&
  24870. + !cpumask_test_cpu(cpu, &task->cpus_allowed)))) {
  24871. + long latency;
  24872. + cycle_t now;
  24873. +
  24874. + if (missed_timer_offsets_pid) {
  24875. + if (likely(missed_timer_offsets_pid !=
  24876. + task_pid_nr(task)))
  24877. + return;
  24878. + }
  24879. +
  24880. + now = ftrace_now(cpu);
  24881. + latency = (long) div_s64(-latency_ns, NSECS_PER_USECS);
  24882. + latency_hist(MISSED_TIMER_OFFSETS, cpu, latency, latency, now,
  24883. + task);
  24884. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  24885. + task->timer_offset = latency;
  24886. +#endif
  24887. + }
  24888. +}
  24889. +#endif
  24890. +
  24891. +static __init int latency_hist_init(void)
  24892. +{
  24893. + struct dentry *latency_hist_root = NULL;
  24894. + struct dentry *dentry;
  24895. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  24896. + struct dentry *dentry_sharedprio;
  24897. +#endif
  24898. + struct dentry *entry;
  24899. + struct dentry *enable_root;
  24900. + int i = 0;
  24901. + struct hist_data *my_hist;
  24902. + char name[64];
  24903. + char *cpufmt = "CPU%d";
  24904. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  24905. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24906. + char *cpufmt_maxlatproc = "max_latency-CPU%d";
  24907. + struct maxlatproc_data *mp = NULL;
  24908. +#endif
  24909. +
  24910. + dentry = tracing_init_dentry();
  24911. + latency_hist_root = debugfs_create_dir(latency_hist_dir_root, dentry);
  24912. + enable_root = debugfs_create_dir("enable", latency_hist_root);
  24913. +
  24914. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  24915. + dentry = debugfs_create_dir(irqsoff_hist_dir, latency_hist_root);
  24916. + for_each_possible_cpu(i) {
  24917. + sprintf(name, cpufmt, i);
  24918. + entry = debugfs_create_file(name, 0444, dentry,
  24919. + &per_cpu(irqsoff_hist, i), &latency_hist_fops);
  24920. + my_hist = &per_cpu(irqsoff_hist, i);
  24921. + atomic_set(&my_hist->hist_mode, 1);
  24922. + my_hist->min_lat = LONG_MAX;
  24923. + }
  24924. + entry = debugfs_create_file("reset", 0644, dentry,
  24925. + (void *)IRQSOFF_LATENCY, &latency_hist_reset_fops);
  24926. +#endif
  24927. +
  24928. +#ifdef CONFIG_PREEMPT_OFF_HIST
  24929. + dentry = debugfs_create_dir(preemptoff_hist_dir,
  24930. + latency_hist_root);
  24931. + for_each_possible_cpu(i) {
  24932. + sprintf(name, cpufmt, i);
  24933. + entry = debugfs_create_file(name, 0444, dentry,
  24934. + &per_cpu(preemptoff_hist, i), &latency_hist_fops);
  24935. + my_hist = &per_cpu(preemptoff_hist, i);
  24936. + atomic_set(&my_hist->hist_mode, 1);
  24937. + my_hist->min_lat = LONG_MAX;
  24938. + }
  24939. + entry = debugfs_create_file("reset", 0644, dentry,
  24940. + (void *)PREEMPTOFF_LATENCY, &latency_hist_reset_fops);
  24941. +#endif
  24942. +
  24943. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  24944. + dentry = debugfs_create_dir(preemptirqsoff_hist_dir,
  24945. + latency_hist_root);
  24946. + for_each_possible_cpu(i) {
  24947. + sprintf(name, cpufmt, i);
  24948. + entry = debugfs_create_file(name, 0444, dentry,
  24949. + &per_cpu(preemptirqsoff_hist, i), &latency_hist_fops);
  24950. + my_hist = &per_cpu(preemptirqsoff_hist, i);
  24951. + atomic_set(&my_hist->hist_mode, 1);
  24952. + my_hist->min_lat = LONG_MAX;
  24953. + }
  24954. + entry = debugfs_create_file("reset", 0644, dentry,
  24955. + (void *)PREEMPTIRQSOFF_LATENCY, &latency_hist_reset_fops);
  24956. +#endif
  24957. +
  24958. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  24959. + entry = debugfs_create_file("preemptirqsoff", 0644,
  24960. + enable_root, (void *)&preemptirqsoff_enabled_data,
  24961. + &enable_fops);
  24962. +#endif
  24963. +
  24964. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  24965. + dentry = debugfs_create_dir(wakeup_latency_hist_dir,
  24966. + latency_hist_root);
  24967. + dentry_sharedprio = debugfs_create_dir(
  24968. + wakeup_latency_hist_dir_sharedprio, dentry);
  24969. + for_each_possible_cpu(i) {
  24970. + sprintf(name, cpufmt, i);
  24971. +
  24972. + entry = debugfs_create_file(name, 0444, dentry,
  24973. + &per_cpu(wakeup_latency_hist, i),
  24974. + &latency_hist_fops);
  24975. + my_hist = &per_cpu(wakeup_latency_hist, i);
  24976. + atomic_set(&my_hist->hist_mode, 1);
  24977. + my_hist->min_lat = LONG_MAX;
  24978. +
  24979. + entry = debugfs_create_file(name, 0444, dentry_sharedprio,
  24980. + &per_cpu(wakeup_latency_hist_sharedprio, i),
  24981. + &latency_hist_fops);
  24982. + my_hist = &per_cpu(wakeup_latency_hist_sharedprio, i);
  24983. + atomic_set(&my_hist->hist_mode, 1);
  24984. + my_hist->min_lat = LONG_MAX;
  24985. +
  24986. + sprintf(name, cpufmt_maxlatproc, i);
  24987. +
  24988. + mp = &per_cpu(wakeup_maxlatproc, i);
  24989. + entry = debugfs_create_file(name, 0444, dentry, mp,
  24990. + &maxlatproc_fops);
  24991. + clear_maxlatprocdata(mp);
  24992. +
  24993. + mp = &per_cpu(wakeup_maxlatproc_sharedprio, i);
  24994. + entry = debugfs_create_file(name, 0444, dentry_sharedprio, mp,
  24995. + &maxlatproc_fops);
  24996. + clear_maxlatprocdata(mp);
  24997. + }
  24998. + entry = debugfs_create_file("pid", 0644, dentry,
  24999. + (void *)&wakeup_pid, &pid_fops);
  25000. + entry = debugfs_create_file("reset", 0644, dentry,
  25001. + (void *)WAKEUP_LATENCY, &latency_hist_reset_fops);
  25002. + entry = debugfs_create_file("reset", 0644, dentry_sharedprio,
  25003. + (void *)WAKEUP_LATENCY_SHAREDPRIO, &latency_hist_reset_fops);
  25004. + entry = debugfs_create_file("wakeup", 0644,
  25005. + enable_root, (void *)&wakeup_latency_enabled_data,
  25006. + &enable_fops);
  25007. +#endif
  25008. +
  25009. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  25010. + dentry = debugfs_create_dir(missed_timer_offsets_dir,
  25011. + latency_hist_root);
  25012. + for_each_possible_cpu(i) {
  25013. + sprintf(name, cpufmt, i);
  25014. + entry = debugfs_create_file(name, 0444, dentry,
  25015. + &per_cpu(missed_timer_offsets, i), &latency_hist_fops);
  25016. + my_hist = &per_cpu(missed_timer_offsets, i);
  25017. + atomic_set(&my_hist->hist_mode, 1);
  25018. + my_hist->min_lat = LONG_MAX;
  25019. +
  25020. + sprintf(name, cpufmt_maxlatproc, i);
  25021. + mp = &per_cpu(missed_timer_offsets_maxlatproc, i);
  25022. + entry = debugfs_create_file(name, 0444, dentry, mp,
  25023. + &maxlatproc_fops);
  25024. + clear_maxlatprocdata(mp);
  25025. + }
  25026. + entry = debugfs_create_file("pid", 0644, dentry,
  25027. + (void *)&missed_timer_offsets_pid, &pid_fops);
  25028. + entry = debugfs_create_file("reset", 0644, dentry,
  25029. + (void *)MISSED_TIMER_OFFSETS, &latency_hist_reset_fops);
  25030. + entry = debugfs_create_file("missed_timer_offsets", 0644,
  25031. + enable_root, (void *)&missed_timer_offsets_enabled_data,
  25032. + &enable_fops);
  25033. +#endif
  25034. +
  25035. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  25036. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  25037. + dentry = debugfs_create_dir(timerandwakeup_latency_hist_dir,
  25038. + latency_hist_root);
  25039. + for_each_possible_cpu(i) {
  25040. + sprintf(name, cpufmt, i);
  25041. + entry = debugfs_create_file(name, 0444, dentry,
  25042. + &per_cpu(timerandwakeup_latency_hist, i),
  25043. + &latency_hist_fops);
  25044. + my_hist = &per_cpu(timerandwakeup_latency_hist, i);
  25045. + atomic_set(&my_hist->hist_mode, 1);
  25046. + my_hist->min_lat = LONG_MAX;
  25047. +
  25048. + sprintf(name, cpufmt_maxlatproc, i);
  25049. + mp = &per_cpu(timerandwakeup_maxlatproc, i);
  25050. + entry = debugfs_create_file(name, 0444, dentry, mp,
  25051. + &maxlatproc_fops);
  25052. + clear_maxlatprocdata(mp);
  25053. + }
  25054. + entry = debugfs_create_file("reset", 0644, dentry,
  25055. + (void *)TIMERANDWAKEUP_LATENCY, &latency_hist_reset_fops);
  25056. + entry = debugfs_create_file("timerandwakeup", 0644,
  25057. + enable_root, (void *)&timerandwakeup_enabled_data,
  25058. + &enable_fops);
  25059. +#endif
  25060. + return 0;
  25061. +}
  25062. +
  25063. +device_initcall(latency_hist_init);
  25064. diff -Nur linux-4.4.28.orig/kernel/trace/Makefile linux-4.4.28/kernel/trace/Makefile
  25065. --- linux-4.4.28.orig/kernel/trace/Makefile 2016-10-28 09:53:25.000000000 +0200
  25066. +++ linux-4.4.28/kernel/trace/Makefile 2016-11-01 21:26:58.804344090 +0100
  25067. @@ -40,6 +40,10 @@
  25068. obj-$(CONFIG_IRQSOFF_TRACER) += trace_irqsoff.o
  25069. obj-$(CONFIG_PREEMPT_TRACER) += trace_irqsoff.o
  25070. obj-$(CONFIG_SCHED_TRACER) += trace_sched_wakeup.o
  25071. +obj-$(CONFIG_INTERRUPT_OFF_HIST) += latency_hist.o
  25072. +obj-$(CONFIG_PREEMPT_OFF_HIST) += latency_hist.o
  25073. +obj-$(CONFIG_WAKEUP_LATENCY_HIST) += latency_hist.o
  25074. +obj-$(CONFIG_MISSED_TIMER_OFFSETS_HIST) += latency_hist.o
  25075. obj-$(CONFIG_NOP_TRACER) += trace_nop.o
  25076. obj-$(CONFIG_STACK_TRACER) += trace_stack.o
  25077. obj-$(CONFIG_MMIOTRACE) += trace_mmiotrace.o
  25078. diff -Nur linux-4.4.28.orig/kernel/trace/trace.c linux-4.4.28/kernel/trace/trace.c
  25079. --- linux-4.4.28.orig/kernel/trace/trace.c 2016-10-28 09:53:25.000000000 +0200
  25080. +++ linux-4.4.28/kernel/trace/trace.c 2016-11-01 21:26:58.808344343 +0100
  25081. @@ -1652,6 +1652,7 @@
  25082. struct task_struct *tsk = current;
  25083. entry->preempt_count = pc & 0xff;
  25084. + entry->preempt_lazy_count = preempt_lazy_count();
  25085. entry->pid = (tsk) ? tsk->pid : 0;
  25086. entry->flags =
  25087. #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
  25088. @@ -1661,8 +1662,11 @@
  25089. #endif
  25090. ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
  25091. ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
  25092. - (tif_need_resched() ? TRACE_FLAG_NEED_RESCHED : 0) |
  25093. + (tif_need_resched_now() ? TRACE_FLAG_NEED_RESCHED : 0) |
  25094. + (need_resched_lazy() ? TRACE_FLAG_NEED_RESCHED_LAZY : 0) |
  25095. (test_preempt_need_resched() ? TRACE_FLAG_PREEMPT_RESCHED : 0);
  25096. +
  25097. + entry->migrate_disable = (tsk) ? __migrate_disabled(tsk) & 0xFF : 0;
  25098. }
  25099. EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
  25100. @@ -2555,14 +2559,17 @@
  25101. static void print_lat_help_header(struct seq_file *m)
  25102. {
  25103. - seq_puts(m, "# _------=> CPU# \n"
  25104. - "# / _-----=> irqs-off \n"
  25105. - "# | / _----=> need-resched \n"
  25106. - "# || / _---=> hardirq/softirq \n"
  25107. - "# ||| / _--=> preempt-depth \n"
  25108. - "# |||| / delay \n"
  25109. - "# cmd pid ||||| time | caller \n"
  25110. - "# \\ / ||||| \\ | / \n");
  25111. + seq_puts(m, "# _--------=> CPU# \n"
  25112. + "# / _-------=> irqs-off \n"
  25113. + "# | / _------=> need-resched \n"
  25114. + "# || / _-----=> need-resched_lazy \n"
  25115. + "# ||| / _----=> hardirq/softirq \n"
  25116. + "# |||| / _---=> preempt-depth \n"
  25117. + "# ||||| / _--=> preempt-lazy-depth\n"
  25118. + "# |||||| / _-=> migrate-disable \n"
  25119. + "# ||||||| / delay \n"
  25120. + "# cmd pid |||||||| time | caller \n"
  25121. + "# \\ / |||||||| \\ | / \n");
  25122. }
  25123. static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
  25124. @@ -2588,11 +2595,14 @@
  25125. print_event_info(buf, m);
  25126. seq_puts(m, "# _-----=> irqs-off\n"
  25127. "# / _----=> need-resched\n"
  25128. - "# | / _---=> hardirq/softirq\n"
  25129. - "# || / _--=> preempt-depth\n"
  25130. - "# ||| / delay\n"
  25131. - "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n"
  25132. - "# | | | |||| | |\n");
  25133. + "# |/ _-----=> need-resched_lazy\n"
  25134. + "# || / _---=> hardirq/softirq\n"
  25135. + "# ||| / _--=> preempt-depth\n"
  25136. + "# |||| / _-=> preempt-lazy-depth\n"
  25137. + "# ||||| / _-=> migrate-disable \n"
  25138. + "# |||||| / delay\n"
  25139. + "# TASK-PID CPU# ||||||| TIMESTAMP FUNCTION\n"
  25140. + "# | | | ||||||| | |\n");
  25141. }
  25142. void
  25143. diff -Nur linux-4.4.28.orig/kernel/trace/trace_events.c linux-4.4.28/kernel/trace/trace_events.c
  25144. --- linux-4.4.28.orig/kernel/trace/trace_events.c 2016-10-28 09:53:25.000000000 +0200
  25145. +++ linux-4.4.28/kernel/trace/trace_events.c 2016-11-01 21:26:58.808344343 +0100
  25146. @@ -188,6 +188,8 @@
  25147. __common_field(unsigned char, flags);
  25148. __common_field(unsigned char, preempt_count);
  25149. __common_field(int, pid);
  25150. + __common_field(unsigned short, migrate_disable);
  25151. + __common_field(unsigned short, padding);
  25152. return ret;
  25153. }
  25154. @@ -244,6 +246,14 @@
  25155. local_save_flags(fbuffer->flags);
  25156. fbuffer->pc = preempt_count();
  25157. + /*
  25158. + * If CONFIG_PREEMPT is enabled, then the tracepoint itself disables
  25159. + * preemption (adding one to the preempt_count). Since we are
  25160. + * interested in the preempt_count at the time the tracepoint was
  25161. + * hit, we need to subtract one to offset the increment.
  25162. + */
  25163. + if (IS_ENABLED(CONFIG_PREEMPT))
  25164. + fbuffer->pc--;
  25165. fbuffer->trace_file = trace_file;
  25166. fbuffer->event =
  25167. diff -Nur linux-4.4.28.orig/kernel/trace/trace.h linux-4.4.28/kernel/trace/trace.h
  25168. --- linux-4.4.28.orig/kernel/trace/trace.h 2016-10-28 09:53:25.000000000 +0200
  25169. +++ linux-4.4.28/kernel/trace/trace.h 2016-11-01 21:26:58.808344343 +0100
  25170. @@ -117,6 +117,7 @@
  25171. * NEED_RESCHED - reschedule is requested
  25172. * HARDIRQ - inside an interrupt handler
  25173. * SOFTIRQ - inside a softirq handler
  25174. + * NEED_RESCHED_LAZY - lazy reschedule is requested
  25175. */
  25176. enum trace_flag_type {
  25177. TRACE_FLAG_IRQS_OFF = 0x01,
  25178. @@ -125,6 +126,7 @@
  25179. TRACE_FLAG_HARDIRQ = 0x08,
  25180. TRACE_FLAG_SOFTIRQ = 0x10,
  25181. TRACE_FLAG_PREEMPT_RESCHED = 0x20,
  25182. + TRACE_FLAG_NEED_RESCHED_LAZY = 0x40,
  25183. };
  25184. #define TRACE_BUF_SIZE 1024
  25185. diff -Nur linux-4.4.28.orig/kernel/trace/trace_irqsoff.c linux-4.4.28/kernel/trace/trace_irqsoff.c
  25186. --- linux-4.4.28.orig/kernel/trace/trace_irqsoff.c 2016-10-28 09:53:25.000000000 +0200
  25187. +++ linux-4.4.28/kernel/trace/trace_irqsoff.c 2016-11-01 21:26:58.808344343 +0100
  25188. @@ -13,6 +13,7 @@
  25189. #include <linux/uaccess.h>
  25190. #include <linux/module.h>
  25191. #include <linux/ftrace.h>
  25192. +#include <trace/events/hist.h>
  25193. #include "trace.h"
  25194. @@ -424,11 +425,13 @@
  25195. {
  25196. if (preempt_trace() || irq_trace())
  25197. start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  25198. + trace_preemptirqsoff_hist_rcuidle(TRACE_START, 1);
  25199. }
  25200. EXPORT_SYMBOL_GPL(start_critical_timings);
  25201. void stop_critical_timings(void)
  25202. {
  25203. + trace_preemptirqsoff_hist_rcuidle(TRACE_STOP, 0);
  25204. if (preempt_trace() || irq_trace())
  25205. stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  25206. }
  25207. @@ -438,6 +441,7 @@
  25208. #ifdef CONFIG_PROVE_LOCKING
  25209. void time_hardirqs_on(unsigned long a0, unsigned long a1)
  25210. {
  25211. + trace_preemptirqsoff_hist_rcuidle(IRQS_ON, 0);
  25212. if (!preempt_trace() && irq_trace())
  25213. stop_critical_timing(a0, a1);
  25214. }
  25215. @@ -446,6 +450,7 @@
  25216. {
  25217. if (!preempt_trace() && irq_trace())
  25218. start_critical_timing(a0, a1);
  25219. + trace_preemptirqsoff_hist_rcuidle(IRQS_OFF, 1);
  25220. }
  25221. #else /* !CONFIG_PROVE_LOCKING */
  25222. @@ -471,6 +476,7 @@
  25223. */
  25224. void trace_hardirqs_on(void)
  25225. {
  25226. + trace_preemptirqsoff_hist(IRQS_ON, 0);
  25227. if (!preempt_trace() && irq_trace())
  25228. stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  25229. }
  25230. @@ -480,11 +486,13 @@
  25231. {
  25232. if (!preempt_trace() && irq_trace())
  25233. start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  25234. + trace_preemptirqsoff_hist(IRQS_OFF, 1);
  25235. }
  25236. EXPORT_SYMBOL(trace_hardirqs_off);
  25237. __visible void trace_hardirqs_on_caller(unsigned long caller_addr)
  25238. {
  25239. + trace_preemptirqsoff_hist(IRQS_ON, 0);
  25240. if (!preempt_trace() && irq_trace())
  25241. stop_critical_timing(CALLER_ADDR0, caller_addr);
  25242. }
  25243. @@ -494,6 +502,7 @@
  25244. {
  25245. if (!preempt_trace() && irq_trace())
  25246. start_critical_timing(CALLER_ADDR0, caller_addr);
  25247. + trace_preemptirqsoff_hist(IRQS_OFF, 1);
  25248. }
  25249. EXPORT_SYMBOL(trace_hardirqs_off_caller);
  25250. @@ -503,12 +512,14 @@
  25251. #ifdef CONFIG_PREEMPT_TRACER
  25252. void trace_preempt_on(unsigned long a0, unsigned long a1)
  25253. {
  25254. + trace_preemptirqsoff_hist(PREEMPT_ON, 0);
  25255. if (preempt_trace() && !irq_trace())
  25256. stop_critical_timing(a0, a1);
  25257. }
  25258. void trace_preempt_off(unsigned long a0, unsigned long a1)
  25259. {
  25260. + trace_preemptirqsoff_hist(PREEMPT_ON, 1);
  25261. if (preempt_trace() && !irq_trace())
  25262. start_critical_timing(a0, a1);
  25263. }
  25264. diff -Nur linux-4.4.28.orig/kernel/trace/trace_output.c linux-4.4.28/kernel/trace/trace_output.c
  25265. --- linux-4.4.28.orig/kernel/trace/trace_output.c 2016-10-28 09:53:25.000000000 +0200
  25266. +++ linux-4.4.28/kernel/trace/trace_output.c 2016-11-01 21:26:58.808344343 +0100
  25267. @@ -386,6 +386,7 @@
  25268. {
  25269. char hardsoft_irq;
  25270. char need_resched;
  25271. + char need_resched_lazy;
  25272. char irqs_off;
  25273. int hardirq;
  25274. int softirq;
  25275. @@ -413,6 +414,8 @@
  25276. need_resched = '.';
  25277. break;
  25278. }
  25279. + need_resched_lazy =
  25280. + (entry->flags & TRACE_FLAG_NEED_RESCHED_LAZY) ? 'L' : '.';
  25281. hardsoft_irq =
  25282. (hardirq && softirq) ? 'H' :
  25283. @@ -420,14 +423,25 @@
  25284. softirq ? 's' :
  25285. '.';
  25286. - trace_seq_printf(s, "%c%c%c",
  25287. - irqs_off, need_resched, hardsoft_irq);
  25288. + trace_seq_printf(s, "%c%c%c%c",
  25289. + irqs_off, need_resched, need_resched_lazy,
  25290. + hardsoft_irq);
  25291. if (entry->preempt_count)
  25292. trace_seq_printf(s, "%x", entry->preempt_count);
  25293. else
  25294. trace_seq_putc(s, '.');
  25295. + if (entry->preempt_lazy_count)
  25296. + trace_seq_printf(s, "%x", entry->preempt_lazy_count);
  25297. + else
  25298. + trace_seq_putc(s, '.');
  25299. +
  25300. + if (entry->migrate_disable)
  25301. + trace_seq_printf(s, "%x", entry->migrate_disable);
  25302. + else
  25303. + trace_seq_putc(s, '.');
  25304. +
  25305. return !trace_seq_has_overflowed(s);
  25306. }
  25307. diff -Nur linux-4.4.28.orig/kernel/user.c linux-4.4.28/kernel/user.c
  25308. --- linux-4.4.28.orig/kernel/user.c 2016-10-28 09:53:25.000000000 +0200
  25309. +++ linux-4.4.28/kernel/user.c 2016-11-01 21:26:58.808344343 +0100
  25310. @@ -161,11 +161,11 @@
  25311. if (!up)
  25312. return;
  25313. - local_irq_save(flags);
  25314. + local_irq_save_nort(flags);
  25315. if (atomic_dec_and_lock(&up->__count, &uidhash_lock))
  25316. free_user(up, flags);
  25317. else
  25318. - local_irq_restore(flags);
  25319. + local_irq_restore_nort(flags);
  25320. }
  25321. struct user_struct *alloc_uid(kuid_t uid)
  25322. diff -Nur linux-4.4.28.orig/kernel/watchdog.c linux-4.4.28/kernel/watchdog.c
  25323. --- linux-4.4.28.orig/kernel/watchdog.c 2016-10-28 09:53:25.000000000 +0200
  25324. +++ linux-4.4.28/kernel/watchdog.c 2016-11-01 21:26:58.808344343 +0100
  25325. @@ -299,6 +299,8 @@
  25326. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  25327. +static DEFINE_RAW_SPINLOCK(watchdog_output_lock);
  25328. +
  25329. static struct perf_event_attr wd_hw_attr = {
  25330. .type = PERF_TYPE_HARDWARE,
  25331. .config = PERF_COUNT_HW_CPU_CYCLES,
  25332. @@ -333,6 +335,13 @@
  25333. /* only print hardlockups once */
  25334. if (__this_cpu_read(hard_watchdog_warn) == true)
  25335. return;
  25336. + /*
  25337. + * If early-printk is enabled then make sure we do not
  25338. + * lock up in printk() and kill console logging:
  25339. + */
  25340. + printk_kill();
  25341. +
  25342. + raw_spin_lock(&watchdog_output_lock);
  25343. pr_emerg("Watchdog detected hard LOCKUP on cpu %d", this_cpu);
  25344. print_modules();
  25345. @@ -350,8 +359,9 @@
  25346. !test_and_set_bit(0, &hardlockup_allcpu_dumped))
  25347. trigger_allbutself_cpu_backtrace();
  25348. + raw_spin_unlock(&watchdog_output_lock);
  25349. if (hardlockup_panic)
  25350. - panic("Hard LOCKUP");
  25351. + nmi_panic(regs, "Hard LOCKUP");
  25352. __this_cpu_write(hard_watchdog_warn, true);
  25353. return;
  25354. @@ -497,6 +507,7 @@
  25355. /* kick off the timer for the hardlockup detector */
  25356. hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  25357. hrtimer->function = watchdog_timer_fn;
  25358. + hrtimer->irqsafe = 1;
  25359. /* Enable the perf event */
  25360. watchdog_nmi_enable(cpu);
  25361. diff -Nur linux-4.4.28.orig/kernel/workqueue.c linux-4.4.28/kernel/workqueue.c
  25362. --- linux-4.4.28.orig/kernel/workqueue.c 2016-10-28 09:53:25.000000000 +0200
  25363. +++ linux-4.4.28/kernel/workqueue.c 2016-11-01 21:26:58.812344600 +0100
  25364. @@ -48,6 +48,8 @@
  25365. #include <linux/nodemask.h>
  25366. #include <linux/moduleparam.h>
  25367. #include <linux/uaccess.h>
  25368. +#include <linux/locallock.h>
  25369. +#include <linux/delay.h>
  25370. #include "workqueue_internal.h"
  25371. @@ -121,11 +123,16 @@
  25372. * cpu or grabbing pool->lock is enough for read access. If
  25373. * POOL_DISASSOCIATED is set, it's identical to L.
  25374. *
  25375. + * On RT we need the extra protection via rt_lock_idle_list() for
  25376. + * the list manipulations against read access from
  25377. + * wq_worker_sleeping(). All other places are nicely serialized via
  25378. + * pool->lock.
  25379. + *
  25380. * A: pool->attach_mutex protected.
  25381. *
  25382. * PL: wq_pool_mutex protected.
  25383. *
  25384. - * PR: wq_pool_mutex protected for writes. Sched-RCU protected for reads.
  25385. + * PR: wq_pool_mutex protected for writes. RCU protected for reads.
  25386. *
  25387. * PW: wq_pool_mutex and wq->mutex protected for writes. Either for reads.
  25388. *
  25389. @@ -134,7 +141,7 @@
  25390. *
  25391. * WQ: wq->mutex protected.
  25392. *
  25393. - * WR: wq->mutex protected for writes. Sched-RCU protected for reads.
  25394. + * WR: wq->mutex protected for writes. RCU protected for reads.
  25395. *
  25396. * MD: wq_mayday_lock protected.
  25397. */
  25398. @@ -183,7 +190,7 @@
  25399. atomic_t nr_running ____cacheline_aligned_in_smp;
  25400. /*
  25401. - * Destruction of pool is sched-RCU protected to allow dereferences
  25402. + * Destruction of pool is RCU protected to allow dereferences
  25403. * from get_work_pool().
  25404. */
  25405. struct rcu_head rcu;
  25406. @@ -212,7 +219,7 @@
  25407. /*
  25408. * Release of unbound pwq is punted to system_wq. See put_pwq()
  25409. * and pwq_unbound_release_workfn() for details. pool_workqueue
  25410. - * itself is also sched-RCU protected so that the first pwq can be
  25411. + * itself is also RCU protected so that the first pwq can be
  25412. * determined without grabbing wq->mutex.
  25413. */
  25414. struct work_struct unbound_release_work;
  25415. @@ -331,6 +338,8 @@
  25416. struct workqueue_struct *system_freezable_power_efficient_wq __read_mostly;
  25417. EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq);
  25418. +static DEFINE_LOCAL_IRQ_LOCK(pendingb_lock);
  25419. +
  25420. static int worker_thread(void *__worker);
  25421. static void workqueue_sysfs_unregister(struct workqueue_struct *wq);
  25422. @@ -338,20 +347,20 @@
  25423. #include <trace/events/workqueue.h>
  25424. #define assert_rcu_or_pool_mutex() \
  25425. - RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held() && \
  25426. + RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
  25427. !lockdep_is_held(&wq_pool_mutex), \
  25428. - "sched RCU or wq_pool_mutex should be held")
  25429. + "RCU or wq_pool_mutex should be held")
  25430. #define assert_rcu_or_wq_mutex(wq) \
  25431. - RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held() && \
  25432. + RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
  25433. !lockdep_is_held(&wq->mutex), \
  25434. - "sched RCU or wq->mutex should be held")
  25435. + "RCU or wq->mutex should be held")
  25436. #define assert_rcu_or_wq_mutex_or_pool_mutex(wq) \
  25437. - RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held() && \
  25438. + RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
  25439. !lockdep_is_held(&wq->mutex) && \
  25440. !lockdep_is_held(&wq_pool_mutex), \
  25441. - "sched RCU, wq->mutex or wq_pool_mutex should be held")
  25442. + "RCU, wq->mutex or wq_pool_mutex should be held")
  25443. #define for_each_cpu_worker_pool(pool, cpu) \
  25444. for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \
  25445. @@ -363,7 +372,7 @@
  25446. * @pool: iteration cursor
  25447. * @pi: integer used for iteration
  25448. *
  25449. - * This must be called either with wq_pool_mutex held or sched RCU read
  25450. + * This must be called either with wq_pool_mutex held or RCU read
  25451. * locked. If the pool needs to be used beyond the locking in effect, the
  25452. * caller is responsible for guaranteeing that the pool stays online.
  25453. *
  25454. @@ -395,7 +404,7 @@
  25455. * @pwq: iteration cursor
  25456. * @wq: the target workqueue
  25457. *
  25458. - * This must be called either with wq->mutex held or sched RCU read locked.
  25459. + * This must be called either with wq->mutex held or RCU read locked.
  25460. * If the pwq needs to be used beyond the locking in effect, the caller is
  25461. * responsible for guaranteeing that the pwq stays online.
  25462. *
  25463. @@ -407,6 +416,31 @@
  25464. if (({ assert_rcu_or_wq_mutex(wq); false; })) { } \
  25465. else
  25466. +#ifdef CONFIG_PREEMPT_RT_BASE
  25467. +static inline void rt_lock_idle_list(struct worker_pool *pool)
  25468. +{
  25469. + preempt_disable();
  25470. +}
  25471. +static inline void rt_unlock_idle_list(struct worker_pool *pool)
  25472. +{
  25473. + preempt_enable();
  25474. +}
  25475. +static inline void sched_lock_idle_list(struct worker_pool *pool) { }
  25476. +static inline void sched_unlock_idle_list(struct worker_pool *pool) { }
  25477. +#else
  25478. +static inline void rt_lock_idle_list(struct worker_pool *pool) { }
  25479. +static inline void rt_unlock_idle_list(struct worker_pool *pool) { }
  25480. +static inline void sched_lock_idle_list(struct worker_pool *pool)
  25481. +{
  25482. + spin_lock_irq(&pool->lock);
  25483. +}
  25484. +static inline void sched_unlock_idle_list(struct worker_pool *pool)
  25485. +{
  25486. + spin_unlock_irq(&pool->lock);
  25487. +}
  25488. +#endif
  25489. +
  25490. +
  25491. #ifdef CONFIG_DEBUG_OBJECTS_WORK
  25492. static struct debug_obj_descr work_debug_descr;
  25493. @@ -557,7 +591,7 @@
  25494. * @wq: the target workqueue
  25495. * @node: the node ID
  25496. *
  25497. - * This must be called with any of wq_pool_mutex, wq->mutex or sched RCU
  25498. + * This must be called with any of wq_pool_mutex, wq->mutex or RCU
  25499. * read locked.
  25500. * If the pwq needs to be used beyond the locking in effect, the caller is
  25501. * responsible for guaranteeing that the pwq stays online.
  25502. @@ -701,8 +735,8 @@
  25503. * @work: the work item of interest
  25504. *
  25505. * Pools are created and destroyed under wq_pool_mutex, and allows read
  25506. - * access under sched-RCU read lock. As such, this function should be
  25507. - * called under wq_pool_mutex or with preemption disabled.
  25508. + * access under RCU read lock. As such, this function should be
  25509. + * called under wq_pool_mutex or inside of a rcu_read_lock() region.
  25510. *
  25511. * All fields of the returned pool are accessible as long as the above
  25512. * mentioned locking is in effect. If the returned pool needs to be used
  25513. @@ -839,51 +873,44 @@
  25514. */
  25515. static void wake_up_worker(struct worker_pool *pool)
  25516. {
  25517. - struct worker *worker = first_idle_worker(pool);
  25518. + struct worker *worker;
  25519. +
  25520. + rt_lock_idle_list(pool);
  25521. +
  25522. + worker = first_idle_worker(pool);
  25523. if (likely(worker))
  25524. wake_up_process(worker->task);
  25525. +
  25526. + rt_unlock_idle_list(pool);
  25527. }
  25528. /**
  25529. - * wq_worker_waking_up - a worker is waking up
  25530. - * @task: task waking up
  25531. - * @cpu: CPU @task is waking up to
  25532. + * wq_worker_running - a worker is running again
  25533. + * @task: task returning from sleep
  25534. *
  25535. - * This function is called during try_to_wake_up() when a worker is
  25536. - * being awoken.
  25537. - *
  25538. - * CONTEXT:
  25539. - * spin_lock_irq(rq->lock)
  25540. + * This function is called when a worker returns from schedule()
  25541. */
  25542. -void wq_worker_waking_up(struct task_struct *task, int cpu)
  25543. +void wq_worker_running(struct task_struct *task)
  25544. {
  25545. struct worker *worker = kthread_data(task);
  25546. - if (!(worker->flags & WORKER_NOT_RUNNING)) {
  25547. - WARN_ON_ONCE(worker->pool->cpu != cpu);
  25548. + if (!worker->sleeping)
  25549. + return;
  25550. + if (!(worker->flags & WORKER_NOT_RUNNING))
  25551. atomic_inc(&worker->pool->nr_running);
  25552. - }
  25553. + worker->sleeping = 0;
  25554. }
  25555. /**
  25556. * wq_worker_sleeping - a worker is going to sleep
  25557. * @task: task going to sleep
  25558. - * @cpu: CPU in question, must be the current CPU number
  25559. - *
  25560. - * This function is called during schedule() when a busy worker is
  25561. - * going to sleep. Worker on the same cpu can be woken up by
  25562. - * returning pointer to its task.
  25563. - *
  25564. - * CONTEXT:
  25565. - * spin_lock_irq(rq->lock)
  25566. - *
  25567. - * Return:
  25568. - * Worker task on @cpu to wake up, %NULL if none.
  25569. + * This function is called from schedule() when a busy worker is
  25570. + * going to sleep.
  25571. */
  25572. -struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
  25573. +void wq_worker_sleeping(struct task_struct *task)
  25574. {
  25575. - struct worker *worker = kthread_data(task), *to_wakeup = NULL;
  25576. + struct worker *worker = kthread_data(task);
  25577. struct worker_pool *pool;
  25578. /*
  25579. @@ -892,29 +919,26 @@
  25580. * checking NOT_RUNNING.
  25581. */
  25582. if (worker->flags & WORKER_NOT_RUNNING)
  25583. - return NULL;
  25584. + return;
  25585. pool = worker->pool;
  25586. - /* this can only happen on the local cpu */
  25587. - if (WARN_ON_ONCE(cpu != raw_smp_processor_id() || pool->cpu != cpu))
  25588. - return NULL;
  25589. + if (WARN_ON_ONCE(worker->sleeping))
  25590. + return;
  25591. +
  25592. + worker->sleeping = 1;
  25593. /*
  25594. * The counterpart of the following dec_and_test, implied mb,
  25595. * worklist not empty test sequence is in insert_work().
  25596. * Please read comment there.
  25597. - *
  25598. - * NOT_RUNNING is clear. This means that we're bound to and
  25599. - * running on the local cpu w/ rq lock held and preemption
  25600. - * disabled, which in turn means that none else could be
  25601. - * manipulating idle_list, so dereferencing idle_list without pool
  25602. - * lock is safe.
  25603. */
  25604. if (atomic_dec_and_test(&pool->nr_running) &&
  25605. - !list_empty(&pool->worklist))
  25606. - to_wakeup = first_idle_worker(pool);
  25607. - return to_wakeup ? to_wakeup->task : NULL;
  25608. + !list_empty(&pool->worklist)) {
  25609. + sched_lock_idle_list(pool);
  25610. + wake_up_worker(pool);
  25611. + sched_unlock_idle_list(pool);
  25612. + }
  25613. }
  25614. /**
  25615. @@ -1108,12 +1132,12 @@
  25616. {
  25617. if (pwq) {
  25618. /*
  25619. - * As both pwqs and pools are sched-RCU protected, the
  25620. + * As both pwqs and pools are RCU protected, the
  25621. * following lock operations are safe.
  25622. */
  25623. - spin_lock_irq(&pwq->pool->lock);
  25624. + local_spin_lock_irq(pendingb_lock, &pwq->pool->lock);
  25625. put_pwq(pwq);
  25626. - spin_unlock_irq(&pwq->pool->lock);
  25627. + local_spin_unlock_irq(pendingb_lock, &pwq->pool->lock);
  25628. }
  25629. }
  25630. @@ -1215,7 +1239,7 @@
  25631. struct worker_pool *pool;
  25632. struct pool_workqueue *pwq;
  25633. - local_irq_save(*flags);
  25634. + local_lock_irqsave(pendingb_lock, *flags);
  25635. /* try to steal the timer if it exists */
  25636. if (is_dwork) {
  25637. @@ -1234,6 +1258,7 @@
  25638. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
  25639. return 0;
  25640. + rcu_read_lock();
  25641. /*
  25642. * The queueing is in progress, or it is already queued. Try to
  25643. * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
  25644. @@ -1272,14 +1297,16 @@
  25645. set_work_pool_and_keep_pending(work, pool->id);
  25646. spin_unlock(&pool->lock);
  25647. + rcu_read_unlock();
  25648. return 1;
  25649. }
  25650. spin_unlock(&pool->lock);
  25651. fail:
  25652. - local_irq_restore(*flags);
  25653. + rcu_read_unlock();
  25654. + local_unlock_irqrestore(pendingb_lock, *flags);
  25655. if (work_is_canceling(work))
  25656. return -ENOENT;
  25657. - cpu_relax();
  25658. + cpu_chill();
  25659. return -EAGAIN;
  25660. }
  25661. @@ -1348,7 +1375,7 @@
  25662. * queued or lose PENDING. Grabbing PENDING and queueing should
  25663. * happen with IRQ disabled.
  25664. */
  25665. - WARN_ON_ONCE(!irqs_disabled());
  25666. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  25667. debug_work_activate(work);
  25668. @@ -1356,6 +1383,8 @@
  25669. if (unlikely(wq->flags & __WQ_DRAINING) &&
  25670. WARN_ON_ONCE(!is_chained_work(wq)))
  25671. return;
  25672. +
  25673. + rcu_read_lock();
  25674. retry:
  25675. if (req_cpu == WORK_CPU_UNBOUND)
  25676. cpu = raw_smp_processor_id();
  25677. @@ -1412,10 +1441,8 @@
  25678. /* pwq determined, queue */
  25679. trace_workqueue_queue_work(req_cpu, pwq, work);
  25680. - if (WARN_ON(!list_empty(&work->entry))) {
  25681. - spin_unlock(&pwq->pool->lock);
  25682. - return;
  25683. - }
  25684. + if (WARN_ON(!list_empty(&work->entry)))
  25685. + goto out;
  25686. pwq->nr_in_flight[pwq->work_color]++;
  25687. work_flags = work_color_to_flags(pwq->work_color);
  25688. @@ -1431,7 +1458,9 @@
  25689. insert_work(pwq, work, worklist, work_flags);
  25690. +out:
  25691. spin_unlock(&pwq->pool->lock);
  25692. + rcu_read_unlock();
  25693. }
  25694. /**
  25695. @@ -1451,14 +1480,14 @@
  25696. bool ret = false;
  25697. unsigned long flags;
  25698. - local_irq_save(flags);
  25699. + local_lock_irqsave(pendingb_lock,flags);
  25700. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
  25701. __queue_work(cpu, wq, work);
  25702. ret = true;
  25703. }
  25704. - local_irq_restore(flags);
  25705. + local_unlock_irqrestore(pendingb_lock, flags);
  25706. return ret;
  25707. }
  25708. EXPORT_SYMBOL(queue_work_on);
  25709. @@ -1525,14 +1554,14 @@
  25710. unsigned long flags;
  25711. /* read the comment in __queue_work() */
  25712. - local_irq_save(flags);
  25713. + local_lock_irqsave(pendingb_lock, flags);
  25714. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
  25715. __queue_delayed_work(cpu, wq, dwork, delay);
  25716. ret = true;
  25717. }
  25718. - local_irq_restore(flags);
  25719. + local_unlock_irqrestore(pendingb_lock, flags);
  25720. return ret;
  25721. }
  25722. EXPORT_SYMBOL(queue_delayed_work_on);
  25723. @@ -1567,7 +1596,7 @@
  25724. if (likely(ret >= 0)) {
  25725. __queue_delayed_work(cpu, wq, dwork, delay);
  25726. - local_irq_restore(flags);
  25727. + local_unlock_irqrestore(pendingb_lock, flags);
  25728. }
  25729. /* -ENOENT from try_to_grab_pending() becomes %true */
  25730. @@ -1600,7 +1629,9 @@
  25731. worker->last_active = jiffies;
  25732. /* idle_list is LIFO */
  25733. + rt_lock_idle_list(pool);
  25734. list_add(&worker->entry, &pool->idle_list);
  25735. + rt_unlock_idle_list(pool);
  25736. if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
  25737. mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
  25738. @@ -1633,7 +1664,9 @@
  25739. return;
  25740. worker_clr_flags(worker, WORKER_IDLE);
  25741. pool->nr_idle--;
  25742. + rt_lock_idle_list(pool);
  25743. list_del_init(&worker->entry);
  25744. + rt_unlock_idle_list(pool);
  25745. }
  25746. static struct worker *alloc_worker(int node)
  25747. @@ -1799,7 +1832,9 @@
  25748. pool->nr_workers--;
  25749. pool->nr_idle--;
  25750. + rt_lock_idle_list(pool);
  25751. list_del_init(&worker->entry);
  25752. + rt_unlock_idle_list(pool);
  25753. worker->flags |= WORKER_DIE;
  25754. wake_up_process(worker->task);
  25755. }
  25756. @@ -2716,14 +2751,14 @@
  25757. might_sleep();
  25758. - local_irq_disable();
  25759. + rcu_read_lock();
  25760. pool = get_work_pool(work);
  25761. if (!pool) {
  25762. - local_irq_enable();
  25763. + rcu_read_unlock();
  25764. return false;
  25765. }
  25766. - spin_lock(&pool->lock);
  25767. + spin_lock_irq(&pool->lock);
  25768. /* see the comment in try_to_grab_pending() with the same code */
  25769. pwq = get_work_pwq(work);
  25770. if (pwq) {
  25771. @@ -2750,10 +2785,11 @@
  25772. else
  25773. lock_map_acquire_read(&pwq->wq->lockdep_map);
  25774. lock_map_release(&pwq->wq->lockdep_map);
  25775. -
  25776. + rcu_read_unlock();
  25777. return true;
  25778. already_gone:
  25779. spin_unlock_irq(&pool->lock);
  25780. + rcu_read_unlock();
  25781. return false;
  25782. }
  25783. @@ -2840,7 +2876,7 @@
  25784. /* tell other tasks trying to grab @work to back off */
  25785. mark_work_canceling(work);
  25786. - local_irq_restore(flags);
  25787. + local_unlock_irqrestore(pendingb_lock, flags);
  25788. flush_work(work);
  25789. clear_work_data(work);
  25790. @@ -2895,10 +2931,10 @@
  25791. */
  25792. bool flush_delayed_work(struct delayed_work *dwork)
  25793. {
  25794. - local_irq_disable();
  25795. + local_lock_irq(pendingb_lock);
  25796. if (del_timer_sync(&dwork->timer))
  25797. __queue_work(dwork->cpu, dwork->wq, &dwork->work);
  25798. - local_irq_enable();
  25799. + local_unlock_irq(pendingb_lock);
  25800. return flush_work(&dwork->work);
  25801. }
  25802. EXPORT_SYMBOL(flush_delayed_work);
  25803. @@ -2933,7 +2969,7 @@
  25804. set_work_pool_and_clear_pending(&dwork->work,
  25805. get_work_pool_id(&dwork->work));
  25806. - local_irq_restore(flags);
  25807. + local_unlock_irqrestore(pendingb_lock, flags);
  25808. return ret;
  25809. }
  25810. EXPORT_SYMBOL(cancel_delayed_work);
  25811. @@ -3161,7 +3197,7 @@
  25812. * put_unbound_pool - put a worker_pool
  25813. * @pool: worker_pool to put
  25814. *
  25815. - * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU
  25816. + * Put @pool. If its refcnt reaches zero, it gets destroyed in RCU
  25817. * safe manner. get_unbound_pool() calls this function on its failure path
  25818. * and this function should be able to release pools which went through,
  25819. * successfully or not, init_worker_pool().
  25820. @@ -3215,8 +3251,8 @@
  25821. del_timer_sync(&pool->idle_timer);
  25822. del_timer_sync(&pool->mayday_timer);
  25823. - /* sched-RCU protected to allow dereferences from get_work_pool() */
  25824. - call_rcu_sched(&pool->rcu, rcu_free_pool);
  25825. + /* RCU protected to allow dereferences from get_work_pool() */
  25826. + call_rcu(&pool->rcu, rcu_free_pool);
  25827. }
  25828. /**
  25829. @@ -3323,14 +3359,14 @@
  25830. put_unbound_pool(pool);
  25831. mutex_unlock(&wq_pool_mutex);
  25832. - call_rcu_sched(&pwq->rcu, rcu_free_pwq);
  25833. + call_rcu(&pwq->rcu, rcu_free_pwq);
  25834. /*
  25835. * If we're the last pwq going away, @wq is already dead and no one
  25836. * is gonna access it anymore. Schedule RCU free.
  25837. */
  25838. if (is_last)
  25839. - call_rcu_sched(&wq->rcu, rcu_free_wq);
  25840. + call_rcu(&wq->rcu, rcu_free_wq);
  25841. }
  25842. /**
  25843. @@ -3983,7 +4019,7 @@
  25844. * The base ref is never dropped on per-cpu pwqs. Directly
  25845. * schedule RCU free.
  25846. */
  25847. - call_rcu_sched(&wq->rcu, rcu_free_wq);
  25848. + call_rcu(&wq->rcu, rcu_free_wq);
  25849. } else {
  25850. /*
  25851. * We're the sole accessor of @wq at this point. Directly
  25852. @@ -4076,7 +4112,8 @@
  25853. struct pool_workqueue *pwq;
  25854. bool ret;
  25855. - rcu_read_lock_sched();
  25856. + rcu_read_lock();
  25857. + preempt_disable();
  25858. if (cpu == WORK_CPU_UNBOUND)
  25859. cpu = smp_processor_id();
  25860. @@ -4087,7 +4124,8 @@
  25861. pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
  25862. ret = !list_empty(&pwq->delayed_works);
  25863. - rcu_read_unlock_sched();
  25864. + preempt_enable();
  25865. + rcu_read_unlock();
  25866. return ret;
  25867. }
  25868. @@ -4113,15 +4151,15 @@
  25869. if (work_pending(work))
  25870. ret |= WORK_BUSY_PENDING;
  25871. - local_irq_save(flags);
  25872. + rcu_read_lock();
  25873. pool = get_work_pool(work);
  25874. if (pool) {
  25875. - spin_lock(&pool->lock);
  25876. + spin_lock_irqsave(&pool->lock, flags);
  25877. if (find_worker_executing_work(pool, work))
  25878. ret |= WORK_BUSY_RUNNING;
  25879. - spin_unlock(&pool->lock);
  25880. + spin_unlock_irqrestore(&pool->lock, flags);
  25881. }
  25882. - local_irq_restore(flags);
  25883. + rcu_read_unlock();
  25884. return ret;
  25885. }
  25886. @@ -4310,7 +4348,7 @@
  25887. unsigned long flags;
  25888. int pi;
  25889. - rcu_read_lock_sched();
  25890. + rcu_read_lock();
  25891. pr_info("Showing busy workqueues and worker pools:\n");
  25892. @@ -4361,7 +4399,7 @@
  25893. spin_unlock_irqrestore(&pool->lock, flags);
  25894. }
  25895. - rcu_read_unlock_sched();
  25896. + rcu_read_unlock();
  25897. }
  25898. /*
  25899. @@ -4722,16 +4760,16 @@
  25900. * nr_active is monotonically decreasing. It's safe
  25901. * to peek without lock.
  25902. */
  25903. - rcu_read_lock_sched();
  25904. + rcu_read_lock();
  25905. for_each_pwq(pwq, wq) {
  25906. WARN_ON_ONCE(pwq->nr_active < 0);
  25907. if (pwq->nr_active) {
  25908. busy = true;
  25909. - rcu_read_unlock_sched();
  25910. + rcu_read_unlock();
  25911. goto out_unlock;
  25912. }
  25913. }
  25914. - rcu_read_unlock_sched();
  25915. + rcu_read_unlock();
  25916. }
  25917. out_unlock:
  25918. mutex_unlock(&wq_pool_mutex);
  25919. @@ -4921,7 +4959,8 @@
  25920. const char *delim = "";
  25921. int node, written = 0;
  25922. - rcu_read_lock_sched();
  25923. + get_online_cpus();
  25924. + rcu_read_lock();
  25925. for_each_node(node) {
  25926. written += scnprintf(buf + written, PAGE_SIZE - written,
  25927. "%s%d:%d", delim, node,
  25928. @@ -4929,7 +4968,8 @@
  25929. delim = " ";
  25930. }
  25931. written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
  25932. - rcu_read_unlock_sched();
  25933. + rcu_read_unlock();
  25934. + put_online_cpus();
  25935. return written;
  25936. }
  25937. diff -Nur linux-4.4.28.orig/kernel/workqueue_internal.h linux-4.4.28/kernel/workqueue_internal.h
  25938. --- linux-4.4.28.orig/kernel/workqueue_internal.h 2016-10-28 09:53:25.000000000 +0200
  25939. +++ linux-4.4.28/kernel/workqueue_internal.h 2016-11-01 21:26:58.812344600 +0100
  25940. @@ -43,6 +43,7 @@
  25941. unsigned long last_active; /* L: last active timestamp */
  25942. unsigned int flags; /* X: flags */
  25943. int id; /* I: worker id */
  25944. + int sleeping; /* None */
  25945. /*
  25946. * Opaque string set with work_set_desc(). Printed out with task
  25947. @@ -68,7 +69,7 @@
  25948. * Scheduler hooks for concurrency managed workqueue. Only to be used from
  25949. * sched/core.c and workqueue.c.
  25950. */
  25951. -void wq_worker_waking_up(struct task_struct *task, int cpu);
  25952. -struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu);
  25953. +void wq_worker_running(struct task_struct *task);
  25954. +void wq_worker_sleeping(struct task_struct *task);
  25955. #endif /* _KERNEL_WORKQUEUE_INTERNAL_H */
  25956. diff -Nur linux-4.4.28.orig/lib/debugobjects.c linux-4.4.28/lib/debugobjects.c
  25957. --- linux-4.4.28.orig/lib/debugobjects.c 2016-10-28 09:53:25.000000000 +0200
  25958. +++ linux-4.4.28/lib/debugobjects.c 2016-11-01 21:26:58.812344600 +0100
  25959. @@ -309,7 +309,10 @@
  25960. struct debug_obj *obj;
  25961. unsigned long flags;
  25962. - fill_pool();
  25963. +#ifdef CONFIG_PREEMPT_RT_FULL
  25964. + if (preempt_count() == 0 && !irqs_disabled())
  25965. +#endif
  25966. + fill_pool();
  25967. db = get_bucket((unsigned long) addr);
  25968. diff -Nur linux-4.4.28.orig/lib/idr.c linux-4.4.28/lib/idr.c
  25969. --- linux-4.4.28.orig/lib/idr.c 2016-10-28 09:53:25.000000000 +0200
  25970. +++ linux-4.4.28/lib/idr.c 2016-11-01 21:26:58.812344600 +0100
  25971. @@ -30,6 +30,7 @@
  25972. #include <linux/idr.h>
  25973. #include <linux/spinlock.h>
  25974. #include <linux/percpu.h>
  25975. +#include <linux/locallock.h>
  25976. #define MAX_IDR_SHIFT (sizeof(int) * 8 - 1)
  25977. #define MAX_IDR_BIT (1U << MAX_IDR_SHIFT)
  25978. @@ -45,6 +46,37 @@
  25979. static DEFINE_PER_CPU(int, idr_preload_cnt);
  25980. static DEFINE_SPINLOCK(simple_ida_lock);
  25981. +#ifdef CONFIG_PREEMPT_RT_FULL
  25982. +static DEFINE_LOCAL_IRQ_LOCK(idr_lock);
  25983. +
  25984. +static inline void idr_preload_lock(void)
  25985. +{
  25986. + local_lock(idr_lock);
  25987. +}
  25988. +
  25989. +static inline void idr_preload_unlock(void)
  25990. +{
  25991. + local_unlock(idr_lock);
  25992. +}
  25993. +
  25994. +void idr_preload_end(void)
  25995. +{
  25996. + idr_preload_unlock();
  25997. +}
  25998. +EXPORT_SYMBOL(idr_preload_end);
  25999. +#else
  26000. +static inline void idr_preload_lock(void)
  26001. +{
  26002. + preempt_disable();
  26003. +}
  26004. +
  26005. +static inline void idr_preload_unlock(void)
  26006. +{
  26007. + preempt_enable();
  26008. +}
  26009. +#endif
  26010. +
  26011. +
  26012. /* the maximum ID which can be allocated given idr->layers */
  26013. static int idr_max(int layers)
  26014. {
  26015. @@ -115,14 +147,14 @@
  26016. * context. See idr_preload() for details.
  26017. */
  26018. if (!in_interrupt()) {
  26019. - preempt_disable();
  26020. + idr_preload_lock();
  26021. new = __this_cpu_read(idr_preload_head);
  26022. if (new) {
  26023. __this_cpu_write(idr_preload_head, new->ary[0]);
  26024. __this_cpu_dec(idr_preload_cnt);
  26025. new->ary[0] = NULL;
  26026. }
  26027. - preempt_enable();
  26028. + idr_preload_unlock();
  26029. if (new)
  26030. return new;
  26031. }
  26032. @@ -366,7 +398,6 @@
  26033. idr_mark_full(pa, id);
  26034. }
  26035. -
  26036. /**
  26037. * idr_preload - preload for idr_alloc()
  26038. * @gfp_mask: allocation mask to use for preloading
  26039. @@ -401,7 +432,7 @@
  26040. WARN_ON_ONCE(in_interrupt());
  26041. might_sleep_if(gfpflags_allow_blocking(gfp_mask));
  26042. - preempt_disable();
  26043. + idr_preload_lock();
  26044. /*
  26045. * idr_alloc() is likely to succeed w/o full idr_layer buffer and
  26046. @@ -413,9 +444,9 @@
  26047. while (__this_cpu_read(idr_preload_cnt) < MAX_IDR_FREE) {
  26048. struct idr_layer *new;
  26049. - preempt_enable();
  26050. + idr_preload_unlock();
  26051. new = kmem_cache_zalloc(idr_layer_cache, gfp_mask);
  26052. - preempt_disable();
  26053. + idr_preload_lock();
  26054. if (!new)
  26055. break;
  26056. diff -Nur linux-4.4.28.orig/lib/Kconfig linux-4.4.28/lib/Kconfig
  26057. --- linux-4.4.28.orig/lib/Kconfig 2016-10-28 09:53:25.000000000 +0200
  26058. +++ linux-4.4.28/lib/Kconfig 2016-11-01 21:26:58.812344600 +0100
  26059. @@ -397,6 +397,7 @@
  26060. config CPUMASK_OFFSTACK
  26061. bool "Force CPU masks off stack" if DEBUG_PER_CPU_MAPS
  26062. + depends on !PREEMPT_RT_FULL
  26063. help
  26064. Use dynamic allocation for cpumask_var_t, instead of putting
  26065. them on the stack. This is a bit more expensive, but avoids
  26066. diff -Nur linux-4.4.28.orig/lib/locking-selftest.c linux-4.4.28/lib/locking-selftest.c
  26067. --- linux-4.4.28.orig/lib/locking-selftest.c 2016-10-28 09:53:25.000000000 +0200
  26068. +++ linux-4.4.28/lib/locking-selftest.c 2016-11-01 21:26:58.816344864 +0100
  26069. @@ -590,6 +590,8 @@
  26070. #include "locking-selftest-spin-hardirq.h"
  26071. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_spin)
  26072. +#ifndef CONFIG_PREEMPT_RT_FULL
  26073. +
  26074. #include "locking-selftest-rlock-hardirq.h"
  26075. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_rlock)
  26076. @@ -605,9 +607,12 @@
  26077. #include "locking-selftest-wlock-softirq.h"
  26078. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_wlock)
  26079. +#endif
  26080. +
  26081. #undef E1
  26082. #undef E2
  26083. +#ifndef CONFIG_PREEMPT_RT_FULL
  26084. /*
  26085. * Enabling hardirqs with a softirq-safe lock held:
  26086. */
  26087. @@ -640,6 +645,8 @@
  26088. #undef E1
  26089. #undef E2
  26090. +#endif
  26091. +
  26092. /*
  26093. * Enabling irqs with an irq-safe lock held:
  26094. */
  26095. @@ -663,6 +670,8 @@
  26096. #include "locking-selftest-spin-hardirq.h"
  26097. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_spin)
  26098. +#ifndef CONFIG_PREEMPT_RT_FULL
  26099. +
  26100. #include "locking-selftest-rlock-hardirq.h"
  26101. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_rlock)
  26102. @@ -678,6 +687,8 @@
  26103. #include "locking-selftest-wlock-softirq.h"
  26104. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock)
  26105. +#endif
  26106. +
  26107. #undef E1
  26108. #undef E2
  26109. @@ -709,6 +720,8 @@
  26110. #include "locking-selftest-spin-hardirq.h"
  26111. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_spin)
  26112. +#ifndef CONFIG_PREEMPT_RT_FULL
  26113. +
  26114. #include "locking-selftest-rlock-hardirq.h"
  26115. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_rlock)
  26116. @@ -724,6 +737,8 @@
  26117. #include "locking-selftest-wlock-softirq.h"
  26118. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock)
  26119. +#endif
  26120. +
  26121. #undef E1
  26122. #undef E2
  26123. #undef E3
  26124. @@ -757,6 +772,8 @@
  26125. #include "locking-selftest-spin-hardirq.h"
  26126. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_spin)
  26127. +#ifndef CONFIG_PREEMPT_RT_FULL
  26128. +
  26129. #include "locking-selftest-rlock-hardirq.h"
  26130. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_rlock)
  26131. @@ -772,10 +789,14 @@
  26132. #include "locking-selftest-wlock-softirq.h"
  26133. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_wlock)
  26134. +#endif
  26135. +
  26136. #undef E1
  26137. #undef E2
  26138. #undef E3
  26139. +#ifndef CONFIG_PREEMPT_RT_FULL
  26140. +
  26141. /*
  26142. * read-lock / write-lock irq inversion.
  26143. *
  26144. @@ -838,6 +859,10 @@
  26145. #undef E2
  26146. #undef E3
  26147. +#endif
  26148. +
  26149. +#ifndef CONFIG_PREEMPT_RT_FULL
  26150. +
  26151. /*
  26152. * read-lock / write-lock recursion that is actually safe.
  26153. */
  26154. @@ -876,6 +901,8 @@
  26155. #undef E2
  26156. #undef E3
  26157. +#endif
  26158. +
  26159. /*
  26160. * read-lock / write-lock recursion that is unsafe.
  26161. */
  26162. @@ -1858,6 +1885,7 @@
  26163. printk(" --------------------------------------------------------------------------\n");
  26164. +#ifndef CONFIG_PREEMPT_RT_FULL
  26165. /*
  26166. * irq-context testcases:
  26167. */
  26168. @@ -1870,6 +1898,28 @@
  26169. DO_TESTCASE_6x2("irq read-recursion", irq_read_recursion);
  26170. // DO_TESTCASE_6x2B("irq read-recursion #2", irq_read_recursion2);
  26171. +#else
  26172. + /* On -rt, we only do hardirq context test for raw spinlock */
  26173. + DO_TESTCASE_1B("hard-irqs-on + irq-safe-A", irqsafe1_hard_spin, 12);
  26174. + DO_TESTCASE_1B("hard-irqs-on + irq-safe-A", irqsafe1_hard_spin, 21);
  26175. +
  26176. + DO_TESTCASE_1B("hard-safe-A + irqs-on", irqsafe2B_hard_spin, 12);
  26177. + DO_TESTCASE_1B("hard-safe-A + irqs-on", irqsafe2B_hard_spin, 21);
  26178. +
  26179. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 123);
  26180. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 132);
  26181. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 213);
  26182. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 231);
  26183. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 312);
  26184. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 321);
  26185. +
  26186. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 123);
  26187. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 132);
  26188. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 213);
  26189. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 231);
  26190. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 312);
  26191. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 321);
  26192. +#endif
  26193. ww_tests();
  26194. diff -Nur linux-4.4.28.orig/lib/percpu_ida.c linux-4.4.28/lib/percpu_ida.c
  26195. --- linux-4.4.28.orig/lib/percpu_ida.c 2016-10-28 09:53:25.000000000 +0200
  26196. +++ linux-4.4.28/lib/percpu_ida.c 2016-11-01 21:26:58.816344864 +0100
  26197. @@ -26,6 +26,9 @@
  26198. #include <linux/string.h>
  26199. #include <linux/spinlock.h>
  26200. #include <linux/percpu_ida.h>
  26201. +#include <linux/locallock.h>
  26202. +
  26203. +static DEFINE_LOCAL_IRQ_LOCK(irq_off_lock);
  26204. struct percpu_ida_cpu {
  26205. /*
  26206. @@ -148,13 +151,13 @@
  26207. unsigned long flags;
  26208. int tag;
  26209. - local_irq_save(flags);
  26210. + local_lock_irqsave(irq_off_lock, flags);
  26211. tags = this_cpu_ptr(pool->tag_cpu);
  26212. /* Fastpath */
  26213. tag = alloc_local_tag(tags);
  26214. if (likely(tag >= 0)) {
  26215. - local_irq_restore(flags);
  26216. + local_unlock_irqrestore(irq_off_lock, flags);
  26217. return tag;
  26218. }
  26219. @@ -173,6 +176,7 @@
  26220. if (!tags->nr_free)
  26221. alloc_global_tags(pool, tags);
  26222. +
  26223. if (!tags->nr_free)
  26224. steal_tags(pool, tags);
  26225. @@ -184,7 +188,7 @@
  26226. }
  26227. spin_unlock(&pool->lock);
  26228. - local_irq_restore(flags);
  26229. + local_unlock_irqrestore(irq_off_lock, flags);
  26230. if (tag >= 0 || state == TASK_RUNNING)
  26231. break;
  26232. @@ -196,7 +200,7 @@
  26233. schedule();
  26234. - local_irq_save(flags);
  26235. + local_lock_irqsave(irq_off_lock, flags);
  26236. tags = this_cpu_ptr(pool->tag_cpu);
  26237. }
  26238. if (state != TASK_RUNNING)
  26239. @@ -221,7 +225,7 @@
  26240. BUG_ON(tag >= pool->nr_tags);
  26241. - local_irq_save(flags);
  26242. + local_lock_irqsave(irq_off_lock, flags);
  26243. tags = this_cpu_ptr(pool->tag_cpu);
  26244. spin_lock(&tags->lock);
  26245. @@ -253,7 +257,7 @@
  26246. spin_unlock(&pool->lock);
  26247. }
  26248. - local_irq_restore(flags);
  26249. + local_unlock_irqrestore(irq_off_lock, flags);
  26250. }
  26251. EXPORT_SYMBOL_GPL(percpu_ida_free);
  26252. @@ -345,7 +349,7 @@
  26253. struct percpu_ida_cpu *remote;
  26254. unsigned cpu, i, err = 0;
  26255. - local_irq_save(flags);
  26256. + local_lock_irqsave(irq_off_lock, flags);
  26257. for_each_possible_cpu(cpu) {
  26258. remote = per_cpu_ptr(pool->tag_cpu, cpu);
  26259. spin_lock(&remote->lock);
  26260. @@ -367,7 +371,7 @@
  26261. }
  26262. spin_unlock(&pool->lock);
  26263. out:
  26264. - local_irq_restore(flags);
  26265. + local_unlock_irqrestore(irq_off_lock, flags);
  26266. return err;
  26267. }
  26268. EXPORT_SYMBOL_GPL(percpu_ida_for_each_free);
  26269. diff -Nur linux-4.4.28.orig/lib/radix-tree.c linux-4.4.28/lib/radix-tree.c
  26270. --- linux-4.4.28.orig/lib/radix-tree.c 2016-10-28 09:53:25.000000000 +0200
  26271. +++ linux-4.4.28/lib/radix-tree.c 2016-11-01 21:26:58.816344864 +0100
  26272. @@ -196,13 +196,14 @@
  26273. * succeed in getting a node here (and never reach
  26274. * kmem_cache_alloc)
  26275. */
  26276. - rtp = this_cpu_ptr(&radix_tree_preloads);
  26277. + rtp = &get_cpu_var(radix_tree_preloads);
  26278. if (rtp->nr) {
  26279. ret = rtp->nodes;
  26280. rtp->nodes = ret->private_data;
  26281. ret->private_data = NULL;
  26282. rtp->nr--;
  26283. }
  26284. + put_cpu_var(radix_tree_preloads);
  26285. /*
  26286. * Update the allocation stack trace as this is more useful
  26287. * for debugging.
  26288. @@ -242,6 +243,7 @@
  26289. call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
  26290. }
  26291. +#ifndef CONFIG_PREEMPT_RT_FULL
  26292. /*
  26293. * Load up this CPU's radix_tree_node buffer with sufficient objects to
  26294. * ensure that the addition of a single element in the tree cannot fail. On
  26295. @@ -310,6 +312,7 @@
  26296. return 0;
  26297. }
  26298. EXPORT_SYMBOL(radix_tree_maybe_preload);
  26299. +#endif
  26300. /*
  26301. * Return the maximum key which can be store into a
  26302. diff -Nur linux-4.4.28.orig/lib/rbtree.c linux-4.4.28/lib/rbtree.c
  26303. --- linux-4.4.28.orig/lib/rbtree.c 2016-10-28 09:53:25.000000000 +0200
  26304. +++ linux-4.4.28/lib/rbtree.c 2016-11-01 21:26:58.816344864 +0100
  26305. @@ -23,6 +23,7 @@
  26306. #include <linux/rbtree_augmented.h>
  26307. #include <linux/export.h>
  26308. +#include <linux/rcupdate.h>
  26309. /*
  26310. * red-black trees properties: http://en.wikipedia.org/wiki/Rbtree
  26311. @@ -590,3 +591,13 @@
  26312. return rb_left_deepest_node(root->rb_node);
  26313. }
  26314. EXPORT_SYMBOL(rb_first_postorder);
  26315. +
  26316. +void rb_link_node_rcu(struct rb_node *node, struct rb_node *parent,
  26317. + struct rb_node **rb_link)
  26318. +{
  26319. + node->__rb_parent_color = (unsigned long)parent;
  26320. + node->rb_left = node->rb_right = NULL;
  26321. +
  26322. + rcu_assign_pointer(*rb_link, node);
  26323. +}
  26324. +EXPORT_SYMBOL(rb_link_node_rcu);
  26325. diff -Nur linux-4.4.28.orig/lib/scatterlist.c linux-4.4.28/lib/scatterlist.c
  26326. --- linux-4.4.28.orig/lib/scatterlist.c 2016-10-28 09:53:25.000000000 +0200
  26327. +++ linux-4.4.28/lib/scatterlist.c 2016-11-01 21:26:58.816344864 +0100
  26328. @@ -620,7 +620,7 @@
  26329. flush_kernel_dcache_page(miter->page);
  26330. if (miter->__flags & SG_MITER_ATOMIC) {
  26331. - WARN_ON_ONCE(preemptible());
  26332. + WARN_ON_ONCE(!pagefault_disabled());
  26333. kunmap_atomic(miter->addr);
  26334. } else
  26335. kunmap(miter->page);
  26336. @@ -664,7 +664,7 @@
  26337. if (!sg_miter_skip(&miter, skip))
  26338. return false;
  26339. - local_irq_save(flags);
  26340. + local_irq_save_nort(flags);
  26341. while (sg_miter_next(&miter) && offset < buflen) {
  26342. unsigned int len;
  26343. @@ -681,7 +681,7 @@
  26344. sg_miter_stop(&miter);
  26345. - local_irq_restore(flags);
  26346. + local_irq_restore_nort(flags);
  26347. return offset;
  26348. }
  26349. EXPORT_SYMBOL(sg_copy_buffer);
  26350. diff -Nur linux-4.4.28.orig/lib/smp_processor_id.c linux-4.4.28/lib/smp_processor_id.c
  26351. --- linux-4.4.28.orig/lib/smp_processor_id.c 2016-10-28 09:53:25.000000000 +0200
  26352. +++ linux-4.4.28/lib/smp_processor_id.c 2016-11-01 21:26:58.816344864 +0100
  26353. @@ -39,8 +39,9 @@
  26354. if (!printk_ratelimit())
  26355. goto out_enable;
  26356. - printk(KERN_ERR "BUG: using %s%s() in preemptible [%08x] code: %s/%d\n",
  26357. - what1, what2, preempt_count() - 1, current->comm, current->pid);
  26358. + printk(KERN_ERR "BUG: using %s%s() in preemptible [%08x %08x] code: %s/%d\n",
  26359. + what1, what2, preempt_count() - 1, __migrate_disabled(current),
  26360. + current->comm, current->pid);
  26361. print_symbol("caller is %s\n", (long)__builtin_return_address(0));
  26362. dump_stack();
  26363. diff -Nur linux-4.4.28.orig/Makefile linux-4.4.28/Makefile
  26364. --- linux-4.4.28.orig/Makefile 2016-10-28 09:53:25.000000000 +0200
  26365. +++ linux-4.4.28/Makefile 2016-11-01 21:26:56.688207681 +0100
  26366. @@ -394,7 +394,7 @@
  26367. KBUILD_CFLAGS := -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs \
  26368. -fno-strict-aliasing -fno-common \
  26369. -Werror-implicit-function-declaration \
  26370. - -Wno-format-security \
  26371. + -Wno-format-security -fno-PIE \
  26372. -std=gnu89
  26373. KBUILD_AFLAGS_KERNEL :=
  26374. @@ -783,6 +783,9 @@
  26375. # Prohibit date/time macros, which would make the build non-deterministic
  26376. KBUILD_CFLAGS += $(call cc-option,-Werror=date-time)
  26377. +# enforce correct pointer usage
  26378. +KBUILD_CFLAGS += $(call cc-option,-Werror=incompatible-pointer-types)
  26379. +
  26380. # use the deterministic mode of AR if available
  26381. KBUILD_ARFLAGS := $(call ar-option,D)
  26382. diff -Nur linux-4.4.28.orig/mm/backing-dev.c linux-4.4.28/mm/backing-dev.c
  26383. --- linux-4.4.28.orig/mm/backing-dev.c 2016-10-28 09:53:25.000000000 +0200
  26384. +++ linux-4.4.28/mm/backing-dev.c 2016-11-01 21:26:58.816344864 +0100
  26385. @@ -457,9 +457,9 @@
  26386. {
  26387. unsigned long flags;
  26388. - local_irq_save(flags);
  26389. + local_irq_save_nort(flags);
  26390. if (!atomic_dec_and_lock(&congested->refcnt, &cgwb_lock)) {
  26391. - local_irq_restore(flags);
  26392. + local_irq_restore_nort(flags);
  26393. return;
  26394. }
  26395. diff -Nur linux-4.4.28.orig/mm/compaction.c linux-4.4.28/mm/compaction.c
  26396. --- linux-4.4.28.orig/mm/compaction.c 2016-10-28 09:53:25.000000000 +0200
  26397. +++ linux-4.4.28/mm/compaction.c 2016-11-01 21:26:58.816344864 +0100
  26398. @@ -1430,10 +1430,12 @@
  26399. cc->migrate_pfn & ~((1UL << cc->order) - 1);
  26400. if (cc->last_migrated_pfn < current_block_start) {
  26401. - cpu = get_cpu();
  26402. + cpu = get_cpu_light();
  26403. + local_lock_irq(swapvec_lock);
  26404. lru_add_drain_cpu(cpu);
  26405. + local_unlock_irq(swapvec_lock);
  26406. drain_local_pages(zone);
  26407. - put_cpu();
  26408. + put_cpu_light();
  26409. /* No more flushing until we migrate again */
  26410. cc->last_migrated_pfn = 0;
  26411. }
  26412. diff -Nur linux-4.4.28.orig/mm/filemap.c linux-4.4.28/mm/filemap.c
  26413. --- linux-4.4.28.orig/mm/filemap.c 2016-10-28 09:53:25.000000000 +0200
  26414. +++ linux-4.4.28/mm/filemap.c 2016-11-01 21:29:56.667809032 +0100
  26415. @@ -144,9 +144,12 @@
  26416. * node->private_list is protected by
  26417. * mapping->tree_lock.
  26418. */
  26419. - if (!list_empty(&node->private_list))
  26420. - list_lru_del(&workingset_shadow_nodes,
  26421. - &node->private_list);
  26422. + if (!list_empty(&node->private_list)) {
  26423. + local_lock(workingset_shadow_lock);
  26424. + list_lru_del(&__workingset_shadow_nodes,
  26425. + &node->private_list);
  26426. + local_unlock(workingset_shadow_lock);
  26427. + }
  26428. }
  26429. return 0;
  26430. }
  26431. @@ -218,7 +221,9 @@
  26432. if (!workingset_node_pages(node) &&
  26433. list_empty(&node->private_list)) {
  26434. node->private_data = mapping;
  26435. - list_lru_add(&workingset_shadow_nodes, &node->private_list);
  26436. + local_lock(workingset_shadow_lock);
  26437. + list_lru_add(&__workingset_shadow_nodes, &node->private_list);
  26438. + local_unlock(workingset_shadow_lock);
  26439. }
  26440. }
  26441. diff -Nur linux-4.4.28.orig/mm/highmem.c linux-4.4.28/mm/highmem.c
  26442. --- linux-4.4.28.orig/mm/highmem.c 2016-10-28 09:53:25.000000000 +0200
  26443. +++ linux-4.4.28/mm/highmem.c 2016-11-01 21:26:58.820345117 +0100
  26444. @@ -29,10 +29,11 @@
  26445. #include <linux/kgdb.h>
  26446. #include <asm/tlbflush.h>
  26447. -
  26448. +#ifndef CONFIG_PREEMPT_RT_FULL
  26449. #if defined(CONFIG_HIGHMEM) || defined(CONFIG_X86_32)
  26450. DEFINE_PER_CPU(int, __kmap_atomic_idx);
  26451. #endif
  26452. +#endif
  26453. /*
  26454. * Virtual_count is not a pure "count".
  26455. @@ -107,8 +108,9 @@
  26456. unsigned long totalhigh_pages __read_mostly;
  26457. EXPORT_SYMBOL(totalhigh_pages);
  26458. -
  26459. +#ifndef CONFIG_PREEMPT_RT_FULL
  26460. EXPORT_PER_CPU_SYMBOL(__kmap_atomic_idx);
  26461. +#endif
  26462. unsigned int nr_free_highpages (void)
  26463. {
  26464. diff -Nur linux-4.4.28.orig/mm/Kconfig linux-4.4.28/mm/Kconfig
  26465. --- linux-4.4.28.orig/mm/Kconfig 2016-10-28 09:53:25.000000000 +0200
  26466. +++ linux-4.4.28/mm/Kconfig 2016-11-01 21:26:58.816344864 +0100
  26467. @@ -392,7 +392,7 @@
  26468. config TRANSPARENT_HUGEPAGE
  26469. bool "Transparent Hugepage Support"
  26470. - depends on HAVE_ARCH_TRANSPARENT_HUGEPAGE
  26471. + depends on HAVE_ARCH_TRANSPARENT_HUGEPAGE && !PREEMPT_RT_FULL
  26472. select COMPACTION
  26473. help
  26474. Transparent Hugepages allows the kernel to use huge pages and
  26475. diff -Nur linux-4.4.28.orig/mm/memcontrol.c linux-4.4.28/mm/memcontrol.c
  26476. --- linux-4.4.28.orig/mm/memcontrol.c 2016-10-28 09:53:25.000000000 +0200
  26477. +++ linux-4.4.28/mm/memcontrol.c 2016-11-01 21:26:58.820345117 +0100
  26478. @@ -67,6 +67,8 @@
  26479. #include <net/sock.h>
  26480. #include <net/ip.h>
  26481. #include <net/tcp_memcontrol.h>
  26482. +#include <linux/locallock.h>
  26483. +
  26484. #include "slab.h"
  26485. #include <asm/uaccess.h>
  26486. @@ -87,6 +89,7 @@
  26487. #define do_swap_account 0
  26488. #endif
  26489. +static DEFINE_LOCAL_IRQ_LOCK(event_lock);
  26490. static const char * const mem_cgroup_stat_names[] = {
  26491. "cache",
  26492. "rss",
  26493. @@ -1922,14 +1925,17 @@
  26494. */
  26495. static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
  26496. {
  26497. - struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
  26498. + struct memcg_stock_pcp *stock;
  26499. + int cpu = get_cpu_light();
  26500. +
  26501. + stock = &per_cpu(memcg_stock, cpu);
  26502. if (stock->cached != memcg) { /* reset if necessary */
  26503. drain_stock(stock);
  26504. stock->cached = memcg;
  26505. }
  26506. stock->nr_pages += nr_pages;
  26507. - put_cpu_var(memcg_stock);
  26508. + put_cpu_light();
  26509. }
  26510. /*
  26511. @@ -1945,7 +1951,7 @@
  26512. return;
  26513. /* Notify other cpus that system-wide "drain" is running */
  26514. get_online_cpus();
  26515. - curcpu = get_cpu();
  26516. + curcpu = get_cpu_light();
  26517. for_each_online_cpu(cpu) {
  26518. struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
  26519. struct mem_cgroup *memcg;
  26520. @@ -1962,7 +1968,7 @@
  26521. schedule_work_on(cpu, &stock->work);
  26522. }
  26523. }
  26524. - put_cpu();
  26525. + put_cpu_light();
  26526. put_online_cpus();
  26527. mutex_unlock(&percpu_charge_mutex);
  26528. }
  26529. @@ -4700,12 +4706,12 @@
  26530. ret = 0;
  26531. - local_irq_disable();
  26532. + local_lock_irq(event_lock);
  26533. mem_cgroup_charge_statistics(to, page, nr_pages);
  26534. memcg_check_events(to, page);
  26535. mem_cgroup_charge_statistics(from, page, -nr_pages);
  26536. memcg_check_events(from, page);
  26537. - local_irq_enable();
  26538. + local_unlock_irq(event_lock);
  26539. out_unlock:
  26540. unlock_page(page);
  26541. out:
  26542. @@ -5495,10 +5501,10 @@
  26543. VM_BUG_ON_PAGE(!PageTransHuge(page), page);
  26544. }
  26545. - local_irq_disable();
  26546. + local_lock_irq(event_lock);
  26547. mem_cgroup_charge_statistics(memcg, page, nr_pages);
  26548. memcg_check_events(memcg, page);
  26549. - local_irq_enable();
  26550. + local_unlock_irq(event_lock);
  26551. if (do_swap_account && PageSwapCache(page)) {
  26552. swp_entry_t entry = { .val = page_private(page) };
  26553. @@ -5554,14 +5560,14 @@
  26554. memcg_oom_recover(memcg);
  26555. }
  26556. - local_irq_save(flags);
  26557. + local_lock_irqsave(event_lock, flags);
  26558. __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS], nr_anon);
  26559. __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_CACHE], nr_file);
  26560. __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE], nr_huge);
  26561. __this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT], pgpgout);
  26562. __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
  26563. memcg_check_events(memcg, dummy_page);
  26564. - local_irq_restore(flags);
  26565. + local_unlock_irqrestore(event_lock, flags);
  26566. if (!mem_cgroup_is_root(memcg))
  26567. css_put_many(&memcg->css, nr_pages);
  26568. @@ -5753,6 +5759,7 @@
  26569. {
  26570. struct mem_cgroup *memcg, *swap_memcg;
  26571. unsigned short oldid;
  26572. + unsigned long flags;
  26573. VM_BUG_ON_PAGE(PageLRU(page), page);
  26574. VM_BUG_ON_PAGE(page_count(page), page);
  26575. @@ -5793,12 +5800,16 @@
  26576. * important here to have the interrupts disabled because it is the
  26577. * only synchronisation we have for udpating the per-CPU variables.
  26578. */
  26579. + local_lock_irqsave(event_lock, flags);
  26580. +#ifndef CONFIG_PREEMPT_RT_BASE
  26581. VM_BUG_ON(!irqs_disabled());
  26582. +#endif
  26583. mem_cgroup_charge_statistics(memcg, page, -1);
  26584. memcg_check_events(memcg, page);
  26585. if (!mem_cgroup_is_root(memcg))
  26586. css_put(&memcg->css);
  26587. + local_unlock_irqrestore(event_lock, flags);
  26588. }
  26589. /**
  26590. diff -Nur linux-4.4.28.orig/mm/mmu_context.c linux-4.4.28/mm/mmu_context.c
  26591. --- linux-4.4.28.orig/mm/mmu_context.c 2016-10-28 09:53:25.000000000 +0200
  26592. +++ linux-4.4.28/mm/mmu_context.c 2016-11-01 21:26:58.820345117 +0100
  26593. @@ -23,6 +23,7 @@
  26594. struct task_struct *tsk = current;
  26595. task_lock(tsk);
  26596. + preempt_disable_rt();
  26597. active_mm = tsk->active_mm;
  26598. if (active_mm != mm) {
  26599. atomic_inc(&mm->mm_count);
  26600. @@ -30,6 +31,7 @@
  26601. }
  26602. tsk->mm = mm;
  26603. switch_mm(active_mm, mm, tsk);
  26604. + preempt_enable_rt();
  26605. task_unlock(tsk);
  26606. #ifdef finish_arch_post_lock_switch
  26607. finish_arch_post_lock_switch();
  26608. diff -Nur linux-4.4.28.orig/mm/page_alloc.c linux-4.4.28/mm/page_alloc.c
  26609. --- linux-4.4.28.orig/mm/page_alloc.c 2016-10-28 09:53:25.000000000 +0200
  26610. +++ linux-4.4.28/mm/page_alloc.c 2016-11-01 21:26:58.824345373 +0100
  26611. @@ -60,6 +60,7 @@
  26612. #include <linux/page_ext.h>
  26613. #include <linux/hugetlb.h>
  26614. #include <linux/sched/rt.h>
  26615. +#include <linux/locallock.h>
  26616. #include <linux/page_owner.h>
  26617. #include <linux/kthread.h>
  26618. @@ -264,6 +265,18 @@
  26619. EXPORT_SYMBOL(nr_online_nodes);
  26620. #endif
  26621. +static DEFINE_LOCAL_IRQ_LOCK(pa_lock);
  26622. +
  26623. +#ifdef CONFIG_PREEMPT_RT_BASE
  26624. +# define cpu_lock_irqsave(cpu, flags) \
  26625. + local_lock_irqsave_on(pa_lock, flags, cpu)
  26626. +# define cpu_unlock_irqrestore(cpu, flags) \
  26627. + local_unlock_irqrestore_on(pa_lock, flags, cpu)
  26628. +#else
  26629. +# define cpu_lock_irqsave(cpu, flags) local_irq_save(flags)
  26630. +# define cpu_unlock_irqrestore(cpu, flags) local_irq_restore(flags)
  26631. +#endif
  26632. +
  26633. int page_group_by_mobility_disabled __read_mostly;
  26634. #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
  26635. @@ -786,7 +799,7 @@
  26636. }
  26637. /*
  26638. - * Frees a number of pages from the PCP lists
  26639. + * Frees a number of pages which have been collected from the pcp lists.
  26640. * Assumes all pages on list are in same zone, and of same order.
  26641. * count is the number of pages to free.
  26642. *
  26643. @@ -797,18 +810,53 @@
  26644. * pinned" detection logic.
  26645. */
  26646. static void free_pcppages_bulk(struct zone *zone, int count,
  26647. - struct per_cpu_pages *pcp)
  26648. + struct list_head *list)
  26649. {
  26650. - int migratetype = 0;
  26651. - int batch_free = 0;
  26652. int to_free = count;
  26653. unsigned long nr_scanned;
  26654. + unsigned long flags;
  26655. +
  26656. + spin_lock_irqsave(&zone->lock, flags);
  26657. - spin_lock(&zone->lock);
  26658. nr_scanned = zone_page_state(zone, NR_PAGES_SCANNED);
  26659. if (nr_scanned)
  26660. __mod_zone_page_state(zone, NR_PAGES_SCANNED, -nr_scanned);
  26661. + while (!list_empty(list)) {
  26662. + struct page *page = list_first_entry(list, struct page, lru);
  26663. + int mt; /* migratetype of the to-be-freed page */
  26664. +
  26665. + /* must delete as __free_one_page list manipulates */
  26666. + list_del(&page->lru);
  26667. +
  26668. + mt = get_pcppage_migratetype(page);
  26669. + /* MIGRATE_ISOLATE page should not go to pcplists */
  26670. + VM_BUG_ON_PAGE(is_migrate_isolate(mt), page);
  26671. + /* Pageblock could have been isolated meanwhile */
  26672. + if (unlikely(has_isolate_pageblock(zone)))
  26673. + mt = get_pageblock_migratetype(page);
  26674. +
  26675. + __free_one_page(page, page_to_pfn(page), zone, 0, mt);
  26676. + trace_mm_page_pcpu_drain(page, 0, mt);
  26677. + to_free--;
  26678. + }
  26679. + WARN_ON(to_free != 0);
  26680. + spin_unlock_irqrestore(&zone->lock, flags);
  26681. +}
  26682. +
  26683. +/*
  26684. + * Moves a number of pages from the PCP lists to free list which
  26685. + * is freed outside of the locked region.
  26686. + *
  26687. + * Assumes all pages on list are in same zone, and of same order.
  26688. + * count is the number of pages to free.
  26689. + */
  26690. +static void isolate_pcp_pages(int to_free, struct per_cpu_pages *src,
  26691. + struct list_head *dst)
  26692. +{
  26693. + int migratetype = 0;
  26694. + int batch_free = 0;
  26695. +
  26696. while (to_free) {
  26697. struct page *page;
  26698. struct list_head *list;
  26699. @@ -824,7 +872,7 @@
  26700. batch_free++;
  26701. if (++migratetype == MIGRATE_PCPTYPES)
  26702. migratetype = 0;
  26703. - list = &pcp->lists[migratetype];
  26704. + list = &src->lists[migratetype];
  26705. } while (list_empty(list));
  26706. /* This is the only non-empty list. Free them all. */
  26707. @@ -832,24 +880,12 @@
  26708. batch_free = to_free;
  26709. do {
  26710. - int mt; /* migratetype of the to-be-freed page */
  26711. -
  26712. - page = list_entry(list->prev, struct page, lru);
  26713. - /* must delete as __free_one_page list manipulates */
  26714. + page = list_last_entry(list, struct page, lru);
  26715. list_del(&page->lru);
  26716. - mt = get_pcppage_migratetype(page);
  26717. - /* MIGRATE_ISOLATE page should not go to pcplists */
  26718. - VM_BUG_ON_PAGE(is_migrate_isolate(mt), page);
  26719. - /* Pageblock could have been isolated meanwhile */
  26720. - if (unlikely(has_isolate_pageblock(zone)))
  26721. - mt = get_pageblock_migratetype(page);
  26722. -
  26723. - __free_one_page(page, page_to_pfn(page), zone, 0, mt);
  26724. - trace_mm_page_pcpu_drain(page, 0, mt);
  26725. + list_add(&page->lru, dst);
  26726. } while (--to_free && --batch_free && !list_empty(list));
  26727. }
  26728. - spin_unlock(&zone->lock);
  26729. }
  26730. static void free_one_page(struct zone *zone,
  26731. @@ -858,7 +894,9 @@
  26732. int migratetype)
  26733. {
  26734. unsigned long nr_scanned;
  26735. - spin_lock(&zone->lock);
  26736. + unsigned long flags;
  26737. +
  26738. + spin_lock_irqsave(&zone->lock, flags);
  26739. nr_scanned = zone_page_state(zone, NR_PAGES_SCANNED);
  26740. if (nr_scanned)
  26741. __mod_zone_page_state(zone, NR_PAGES_SCANNED, -nr_scanned);
  26742. @@ -868,7 +906,7 @@
  26743. migratetype = get_pfnblock_migratetype(page, pfn);
  26744. }
  26745. __free_one_page(page, pfn, zone, order, migratetype);
  26746. - spin_unlock(&zone->lock);
  26747. + spin_unlock_irqrestore(&zone->lock, flags);
  26748. }
  26749. static int free_tail_pages_check(struct page *head_page, struct page *page)
  26750. @@ -1019,10 +1057,10 @@
  26751. return;
  26752. migratetype = get_pfnblock_migratetype(page, pfn);
  26753. - local_irq_save(flags);
  26754. + local_lock_irqsave(pa_lock, flags);
  26755. __count_vm_events(PGFREE, 1 << order);
  26756. free_one_page(page_zone(page), page, pfn, order, migratetype);
  26757. - local_irq_restore(flags);
  26758. + local_unlock_irqrestore(pa_lock, flags);
  26759. }
  26760. static void __init __free_pages_boot_core(struct page *page,
  26761. @@ -1879,16 +1917,18 @@
  26762. void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp)
  26763. {
  26764. unsigned long flags;
  26765. + LIST_HEAD(dst);
  26766. int to_drain, batch;
  26767. - local_irq_save(flags);
  26768. + local_lock_irqsave(pa_lock, flags);
  26769. batch = READ_ONCE(pcp->batch);
  26770. to_drain = min(pcp->count, batch);
  26771. if (to_drain > 0) {
  26772. - free_pcppages_bulk(zone, to_drain, pcp);
  26773. + isolate_pcp_pages(to_drain, pcp, &dst);
  26774. pcp->count -= to_drain;
  26775. }
  26776. - local_irq_restore(flags);
  26777. + local_unlock_irqrestore(pa_lock, flags);
  26778. + free_pcppages_bulk(zone, to_drain, &dst);
  26779. }
  26780. #endif
  26781. @@ -1904,16 +1944,21 @@
  26782. unsigned long flags;
  26783. struct per_cpu_pageset *pset;
  26784. struct per_cpu_pages *pcp;
  26785. + LIST_HEAD(dst);
  26786. + int count;
  26787. - local_irq_save(flags);
  26788. + cpu_lock_irqsave(cpu, flags);
  26789. pset = per_cpu_ptr(zone->pageset, cpu);
  26790. pcp = &pset->pcp;
  26791. - if (pcp->count) {
  26792. - free_pcppages_bulk(zone, pcp->count, pcp);
  26793. + count = pcp->count;
  26794. + if (count) {
  26795. + isolate_pcp_pages(count, pcp, &dst);
  26796. pcp->count = 0;
  26797. }
  26798. - local_irq_restore(flags);
  26799. + cpu_unlock_irqrestore(cpu, flags);
  26800. + if (count)
  26801. + free_pcppages_bulk(zone, count, &dst);
  26802. }
  26803. /*
  26804. @@ -1999,8 +2044,17 @@
  26805. else
  26806. cpumask_clear_cpu(cpu, &cpus_with_pcps);
  26807. }
  26808. +#ifndef CONFIG_PREEMPT_RT_BASE
  26809. on_each_cpu_mask(&cpus_with_pcps, (smp_call_func_t) drain_local_pages,
  26810. zone, 1);
  26811. +#else
  26812. + for_each_cpu(cpu, &cpus_with_pcps) {
  26813. + if (zone)
  26814. + drain_pages_zone(cpu, zone);
  26815. + else
  26816. + drain_pages(cpu);
  26817. + }
  26818. +#endif
  26819. }
  26820. #ifdef CONFIG_HIBERNATION
  26821. @@ -2056,7 +2110,7 @@
  26822. migratetype = get_pfnblock_migratetype(page, pfn);
  26823. set_pcppage_migratetype(page, migratetype);
  26824. - local_irq_save(flags);
  26825. + local_lock_irqsave(pa_lock, flags);
  26826. __count_vm_event(PGFREE);
  26827. /*
  26828. @@ -2082,12 +2136,17 @@
  26829. pcp->count++;
  26830. if (pcp->count >= pcp->high) {
  26831. unsigned long batch = READ_ONCE(pcp->batch);
  26832. - free_pcppages_bulk(zone, batch, pcp);
  26833. + LIST_HEAD(dst);
  26834. +
  26835. + isolate_pcp_pages(batch, pcp, &dst);
  26836. pcp->count -= batch;
  26837. + local_unlock_irqrestore(pa_lock, flags);
  26838. + free_pcppages_bulk(zone, batch, &dst);
  26839. + return;
  26840. }
  26841. out:
  26842. - local_irq_restore(flags);
  26843. + local_unlock_irqrestore(pa_lock, flags);
  26844. }
  26845. /*
  26846. @@ -2222,7 +2281,7 @@
  26847. struct per_cpu_pages *pcp;
  26848. struct list_head *list;
  26849. - local_irq_save(flags);
  26850. + local_lock_irqsave(pa_lock, flags);
  26851. pcp = &this_cpu_ptr(zone->pageset)->pcp;
  26852. list = &pcp->lists[migratetype];
  26853. if (list_empty(list)) {
  26854. @@ -2254,7 +2313,7 @@
  26855. */
  26856. WARN_ON_ONCE(order > 1);
  26857. }
  26858. - spin_lock_irqsave(&zone->lock, flags);
  26859. + local_spin_lock_irqsave(pa_lock, &zone->lock, flags);
  26860. page = NULL;
  26861. if (alloc_flags & ALLOC_HARDER) {
  26862. @@ -2264,11 +2323,13 @@
  26863. }
  26864. if (!page)
  26865. page = __rmqueue(zone, order, migratetype, gfp_flags);
  26866. - spin_unlock(&zone->lock);
  26867. - if (!page)
  26868. + if (!page) {
  26869. + spin_unlock(&zone->lock);
  26870. goto failed;
  26871. + }
  26872. __mod_zone_freepage_state(zone, -(1 << order),
  26873. get_pcppage_migratetype(page));
  26874. + spin_unlock(&zone->lock);
  26875. }
  26876. __mod_zone_page_state(zone, NR_ALLOC_BATCH, -(1 << order));
  26877. @@ -2278,13 +2339,13 @@
  26878. __count_zone_vm_events(PGALLOC, zone, 1 << order);
  26879. zone_statistics(preferred_zone, zone, gfp_flags);
  26880. - local_irq_restore(flags);
  26881. + local_unlock_irqrestore(pa_lock, flags);
  26882. VM_BUG_ON_PAGE(bad_range(zone, page), page);
  26883. return page;
  26884. failed:
  26885. - local_irq_restore(flags);
  26886. + local_unlock_irqrestore(pa_lock, flags);
  26887. return NULL;
  26888. }
  26889. @@ -5950,6 +6011,7 @@
  26890. void __init page_alloc_init(void)
  26891. {
  26892. hotcpu_notifier(page_alloc_cpu_notify, 0);
  26893. + local_irq_lock_init(pa_lock);
  26894. }
  26895. /*
  26896. @@ -6844,7 +6906,7 @@
  26897. struct per_cpu_pageset *pset;
  26898. /* avoid races with drain_pages() */
  26899. - local_irq_save(flags);
  26900. + local_lock_irqsave(pa_lock, flags);
  26901. if (zone->pageset != &boot_pageset) {
  26902. for_each_online_cpu(cpu) {
  26903. pset = per_cpu_ptr(zone->pageset, cpu);
  26904. @@ -6853,7 +6915,7 @@
  26905. free_percpu(zone->pageset);
  26906. zone->pageset = &boot_pageset;
  26907. }
  26908. - local_irq_restore(flags);
  26909. + local_unlock_irqrestore(pa_lock, flags);
  26910. }
  26911. #ifdef CONFIG_MEMORY_HOTREMOVE
  26912. diff -Nur linux-4.4.28.orig/mm/slab.h linux-4.4.28/mm/slab.h
  26913. --- linux-4.4.28.orig/mm/slab.h 2016-10-28 09:53:25.000000000 +0200
  26914. +++ linux-4.4.28/mm/slab.h 2016-11-01 21:26:58.824345373 +0100
  26915. @@ -324,7 +324,11 @@
  26916. * The slab lists for all objects.
  26917. */
  26918. struct kmem_cache_node {
  26919. +#ifdef CONFIG_SLUB
  26920. + raw_spinlock_t list_lock;
  26921. +#else
  26922. spinlock_t list_lock;
  26923. +#endif
  26924. #ifdef CONFIG_SLAB
  26925. struct list_head slabs_partial; /* partial list first, better asm code */
  26926. diff -Nur linux-4.4.28.orig/mm/slub.c linux-4.4.28/mm/slub.c
  26927. --- linux-4.4.28.orig/mm/slub.c 2016-10-28 09:53:25.000000000 +0200
  26928. +++ linux-4.4.28/mm/slub.c 2016-11-01 21:26:58.828345638 +0100
  26929. @@ -1075,7 +1075,7 @@
  26930. void *object = head;
  26931. int cnt = 0;
  26932. - spin_lock_irqsave(&n->list_lock, *flags);
  26933. + raw_spin_lock_irqsave(&n->list_lock, *flags);
  26934. slab_lock(page);
  26935. if (!check_slab(s, page))
  26936. @@ -1136,7 +1136,7 @@
  26937. fail:
  26938. slab_unlock(page);
  26939. - spin_unlock_irqrestore(&n->list_lock, *flags);
  26940. + raw_spin_unlock_irqrestore(&n->list_lock, *flags);
  26941. slab_fix(s, "Object at 0x%p not freed", object);
  26942. return NULL;
  26943. }
  26944. @@ -1263,6 +1263,12 @@
  26945. #endif /* CONFIG_SLUB_DEBUG */
  26946. +struct slub_free_list {
  26947. + raw_spinlock_t lock;
  26948. + struct list_head list;
  26949. +};
  26950. +static DEFINE_PER_CPU(struct slub_free_list, slub_free_list);
  26951. +
  26952. /*
  26953. * Hooks for other subsystems that check memory allocations. In a typical
  26954. * production configuration these hooks all should produce no code at all.
  26955. @@ -1399,10 +1405,17 @@
  26956. gfp_t alloc_gfp;
  26957. void *start, *p;
  26958. int idx, order;
  26959. + bool enableirqs = false;
  26960. flags &= gfp_allowed_mask;
  26961. if (gfpflags_allow_blocking(flags))
  26962. + enableirqs = true;
  26963. +#ifdef CONFIG_PREEMPT_RT_FULL
  26964. + if (system_state == SYSTEM_RUNNING)
  26965. + enableirqs = true;
  26966. +#endif
  26967. + if (enableirqs)
  26968. local_irq_enable();
  26969. flags |= s->allocflags;
  26970. @@ -1473,7 +1486,7 @@
  26971. page->frozen = 1;
  26972. out:
  26973. - if (gfpflags_allow_blocking(flags))
  26974. + if (enableirqs)
  26975. local_irq_disable();
  26976. if (!page)
  26977. return NULL;
  26978. @@ -1529,6 +1542,16 @@
  26979. __free_kmem_pages(page, order);
  26980. }
  26981. +static void free_delayed(struct list_head *h)
  26982. +{
  26983. + while(!list_empty(h)) {
  26984. + struct page *page = list_first_entry(h, struct page, lru);
  26985. +
  26986. + list_del(&page->lru);
  26987. + __free_slab(page->slab_cache, page);
  26988. + }
  26989. +}
  26990. +
  26991. #define need_reserve_slab_rcu \
  26992. (sizeof(((struct page *)NULL)->lru) < sizeof(struct rcu_head))
  26993. @@ -1560,6 +1583,12 @@
  26994. }
  26995. call_rcu(head, rcu_free_slab);
  26996. + } else if (irqs_disabled()) {
  26997. + struct slub_free_list *f = this_cpu_ptr(&slub_free_list);
  26998. +
  26999. + raw_spin_lock(&f->lock);
  27000. + list_add(&page->lru, &f->list);
  27001. + raw_spin_unlock(&f->lock);
  27002. } else
  27003. __free_slab(s, page);
  27004. }
  27005. @@ -1673,7 +1702,7 @@
  27006. if (!n || !n->nr_partial)
  27007. return NULL;
  27008. - spin_lock(&n->list_lock);
  27009. + raw_spin_lock(&n->list_lock);
  27010. list_for_each_entry_safe(page, page2, &n->partial, lru) {
  27011. void *t;
  27012. @@ -1698,7 +1727,7 @@
  27013. break;
  27014. }
  27015. - spin_unlock(&n->list_lock);
  27016. + raw_spin_unlock(&n->list_lock);
  27017. return object;
  27018. }
  27019. @@ -1944,7 +1973,7 @@
  27020. * that acquire_slab() will see a slab page that
  27021. * is frozen
  27022. */
  27023. - spin_lock(&n->list_lock);
  27024. + raw_spin_lock(&n->list_lock);
  27025. }
  27026. } else {
  27027. m = M_FULL;
  27028. @@ -1955,7 +1984,7 @@
  27029. * slabs from diagnostic functions will not see
  27030. * any frozen slabs.
  27031. */
  27032. - spin_lock(&n->list_lock);
  27033. + raw_spin_lock(&n->list_lock);
  27034. }
  27035. }
  27036. @@ -1990,7 +2019,7 @@
  27037. goto redo;
  27038. if (lock)
  27039. - spin_unlock(&n->list_lock);
  27040. + raw_spin_unlock(&n->list_lock);
  27041. if (m == M_FREE) {
  27042. stat(s, DEACTIVATE_EMPTY);
  27043. @@ -2022,10 +2051,10 @@
  27044. n2 = get_node(s, page_to_nid(page));
  27045. if (n != n2) {
  27046. if (n)
  27047. - spin_unlock(&n->list_lock);
  27048. + raw_spin_unlock(&n->list_lock);
  27049. n = n2;
  27050. - spin_lock(&n->list_lock);
  27051. + raw_spin_lock(&n->list_lock);
  27052. }
  27053. do {
  27054. @@ -2054,7 +2083,7 @@
  27055. }
  27056. if (n)
  27057. - spin_unlock(&n->list_lock);
  27058. + raw_spin_unlock(&n->list_lock);
  27059. while (discard_page) {
  27060. page = discard_page;
  27061. @@ -2093,14 +2122,21 @@
  27062. pobjects = oldpage->pobjects;
  27063. pages = oldpage->pages;
  27064. if (drain && pobjects > s->cpu_partial) {
  27065. + struct slub_free_list *f;
  27066. unsigned long flags;
  27067. + LIST_HEAD(tofree);
  27068. /*
  27069. * partial array is full. Move the existing
  27070. * set to the per node partial list.
  27071. */
  27072. local_irq_save(flags);
  27073. unfreeze_partials(s, this_cpu_ptr(s->cpu_slab));
  27074. + f = this_cpu_ptr(&slub_free_list);
  27075. + raw_spin_lock(&f->lock);
  27076. + list_splice_init(&f->list, &tofree);
  27077. + raw_spin_unlock(&f->lock);
  27078. local_irq_restore(flags);
  27079. + free_delayed(&tofree);
  27080. oldpage = NULL;
  27081. pobjects = 0;
  27082. pages = 0;
  27083. @@ -2172,7 +2208,22 @@
  27084. static void flush_all(struct kmem_cache *s)
  27085. {
  27086. + LIST_HEAD(tofree);
  27087. + int cpu;
  27088. +
  27089. on_each_cpu_cond(has_cpu_slab, flush_cpu_slab, s, 1, GFP_ATOMIC);
  27090. + for_each_online_cpu(cpu) {
  27091. + struct slub_free_list *f;
  27092. +
  27093. + if (!has_cpu_slab(cpu, s))
  27094. + continue;
  27095. +
  27096. + f = &per_cpu(slub_free_list, cpu);
  27097. + raw_spin_lock_irq(&f->lock);
  27098. + list_splice_init(&f->list, &tofree);
  27099. + raw_spin_unlock_irq(&f->lock);
  27100. + free_delayed(&tofree);
  27101. + }
  27102. }
  27103. /*
  27104. @@ -2208,10 +2259,10 @@
  27105. unsigned long x = 0;
  27106. struct page *page;
  27107. - spin_lock_irqsave(&n->list_lock, flags);
  27108. + raw_spin_lock_irqsave(&n->list_lock, flags);
  27109. list_for_each_entry(page, &n->partial, lru)
  27110. x += get_count(page);
  27111. - spin_unlock_irqrestore(&n->list_lock, flags);
  27112. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  27113. return x;
  27114. }
  27115. #endif /* CONFIG_SLUB_DEBUG || CONFIG_SYSFS */
  27116. @@ -2349,8 +2400,10 @@
  27117. * already disabled (which is the case for bulk allocation).
  27118. */
  27119. static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
  27120. - unsigned long addr, struct kmem_cache_cpu *c)
  27121. + unsigned long addr, struct kmem_cache_cpu *c,
  27122. + struct list_head *to_free)
  27123. {
  27124. + struct slub_free_list *f;
  27125. void *freelist;
  27126. struct page *page;
  27127. @@ -2410,6 +2463,13 @@
  27128. VM_BUG_ON(!c->page->frozen);
  27129. c->freelist = get_freepointer(s, freelist);
  27130. c->tid = next_tid(c->tid);
  27131. +
  27132. +out:
  27133. + f = this_cpu_ptr(&slub_free_list);
  27134. + raw_spin_lock(&f->lock);
  27135. + list_splice_init(&f->list, to_free);
  27136. + raw_spin_unlock(&f->lock);
  27137. +
  27138. return freelist;
  27139. new_slab:
  27140. @@ -2441,7 +2501,7 @@
  27141. deactivate_slab(s, page, get_freepointer(s, freelist));
  27142. c->page = NULL;
  27143. c->freelist = NULL;
  27144. - return freelist;
  27145. + goto out;
  27146. }
  27147. /*
  27148. @@ -2453,6 +2513,7 @@
  27149. {
  27150. void *p;
  27151. unsigned long flags;
  27152. + LIST_HEAD(tofree);
  27153. local_irq_save(flags);
  27154. #ifdef CONFIG_PREEMPT
  27155. @@ -2464,8 +2525,9 @@
  27156. c = this_cpu_ptr(s->cpu_slab);
  27157. #endif
  27158. - p = ___slab_alloc(s, gfpflags, node, addr, c);
  27159. + p = ___slab_alloc(s, gfpflags, node, addr, c, &tofree);
  27160. local_irq_restore(flags);
  27161. + free_delayed(&tofree);
  27162. return p;
  27163. }
  27164. @@ -2652,7 +2714,7 @@
  27165. do {
  27166. if (unlikely(n)) {
  27167. - spin_unlock_irqrestore(&n->list_lock, flags);
  27168. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  27169. n = NULL;
  27170. }
  27171. prior = page->freelist;
  27172. @@ -2684,7 +2746,7 @@
  27173. * Otherwise the list_lock will synchronize with
  27174. * other processors updating the list of slabs.
  27175. */
  27176. - spin_lock_irqsave(&n->list_lock, flags);
  27177. + raw_spin_lock_irqsave(&n->list_lock, flags);
  27178. }
  27179. }
  27180. @@ -2726,7 +2788,7 @@
  27181. add_partial(n, page, DEACTIVATE_TO_TAIL);
  27182. stat(s, FREE_ADD_PARTIAL);
  27183. }
  27184. - spin_unlock_irqrestore(&n->list_lock, flags);
  27185. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  27186. return;
  27187. slab_empty:
  27188. @@ -2741,7 +2803,7 @@
  27189. remove_full(s, n, page);
  27190. }
  27191. - spin_unlock_irqrestore(&n->list_lock, flags);
  27192. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  27193. stat(s, FREE_SLAB);
  27194. discard_slab(s, page);
  27195. }
  27196. @@ -2913,6 +2975,7 @@
  27197. void **p)
  27198. {
  27199. struct kmem_cache_cpu *c;
  27200. + LIST_HEAD(to_free);
  27201. int i;
  27202. /* memcg and kmem_cache debug support */
  27203. @@ -2936,7 +2999,7 @@
  27204. * of re-populating per CPU c->freelist
  27205. */
  27206. p[i] = ___slab_alloc(s, flags, NUMA_NO_NODE,
  27207. - _RET_IP_, c);
  27208. + _RET_IP_, c, &to_free);
  27209. if (unlikely(!p[i]))
  27210. goto error;
  27211. @@ -2948,6 +3011,7 @@
  27212. }
  27213. c->tid = next_tid(c->tid);
  27214. local_irq_enable();
  27215. + free_delayed(&to_free);
  27216. /* Clear memory outside IRQ disabled fastpath loop */
  27217. if (unlikely(flags & __GFP_ZERO)) {
  27218. @@ -3095,7 +3159,7 @@
  27219. init_kmem_cache_node(struct kmem_cache_node *n)
  27220. {
  27221. n->nr_partial = 0;
  27222. - spin_lock_init(&n->list_lock);
  27223. + raw_spin_lock_init(&n->list_lock);
  27224. INIT_LIST_HEAD(&n->partial);
  27225. #ifdef CONFIG_SLUB_DEBUG
  27226. atomic_long_set(&n->nr_slabs, 0);
  27227. @@ -3677,7 +3741,7 @@
  27228. for (i = 0; i < SHRINK_PROMOTE_MAX; i++)
  27229. INIT_LIST_HEAD(promote + i);
  27230. - spin_lock_irqsave(&n->list_lock, flags);
  27231. + raw_spin_lock_irqsave(&n->list_lock, flags);
  27232. /*
  27233. * Build lists of slabs to discard or promote.
  27234. @@ -3708,7 +3772,7 @@
  27235. for (i = SHRINK_PROMOTE_MAX - 1; i >= 0; i--)
  27236. list_splice(promote + i, &n->partial);
  27237. - spin_unlock_irqrestore(&n->list_lock, flags);
  27238. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  27239. /* Release empty slabs */
  27240. list_for_each_entry_safe(page, t, &discard, lru)
  27241. @@ -3884,6 +3948,12 @@
  27242. {
  27243. static __initdata struct kmem_cache boot_kmem_cache,
  27244. boot_kmem_cache_node;
  27245. + int cpu;
  27246. +
  27247. + for_each_possible_cpu(cpu) {
  27248. + raw_spin_lock_init(&per_cpu(slub_free_list, cpu).lock);
  27249. + INIT_LIST_HEAD(&per_cpu(slub_free_list, cpu).list);
  27250. + }
  27251. if (debug_guardpage_minorder())
  27252. slub_max_order = 0;
  27253. @@ -4127,7 +4197,7 @@
  27254. struct page *page;
  27255. unsigned long flags;
  27256. - spin_lock_irqsave(&n->list_lock, flags);
  27257. + raw_spin_lock_irqsave(&n->list_lock, flags);
  27258. list_for_each_entry(page, &n->partial, lru) {
  27259. validate_slab_slab(s, page, map);
  27260. @@ -4149,7 +4219,7 @@
  27261. s->name, count, atomic_long_read(&n->nr_slabs));
  27262. out:
  27263. - spin_unlock_irqrestore(&n->list_lock, flags);
  27264. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  27265. return count;
  27266. }
  27267. @@ -4337,12 +4407,12 @@
  27268. if (!atomic_long_read(&n->nr_slabs))
  27269. continue;
  27270. - spin_lock_irqsave(&n->list_lock, flags);
  27271. + raw_spin_lock_irqsave(&n->list_lock, flags);
  27272. list_for_each_entry(page, &n->partial, lru)
  27273. process_slab(&t, s, page, alloc, map);
  27274. list_for_each_entry(page, &n->full, lru)
  27275. process_slab(&t, s, page, alloc, map);
  27276. - spin_unlock_irqrestore(&n->list_lock, flags);
  27277. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  27278. }
  27279. for (i = 0; i < t.count; i++) {
  27280. diff -Nur linux-4.4.28.orig/mm/swap.c linux-4.4.28/mm/swap.c
  27281. --- linux-4.4.28.orig/mm/swap.c 2016-10-28 09:53:25.000000000 +0200
  27282. +++ linux-4.4.28/mm/swap.c 2016-11-01 21:26:58.828345638 +0100
  27283. @@ -31,6 +31,7 @@
  27284. #include <linux/memcontrol.h>
  27285. #include <linux/gfp.h>
  27286. #include <linux/uio.h>
  27287. +#include <linux/locallock.h>
  27288. #include <linux/hugetlb.h>
  27289. #include <linux/page_idle.h>
  27290. @@ -46,6 +47,9 @@
  27291. static DEFINE_PER_CPU(struct pagevec, lru_rotate_pvecs);
  27292. static DEFINE_PER_CPU(struct pagevec, lru_deactivate_file_pvecs);
  27293. +static DEFINE_LOCAL_IRQ_LOCK(rotate_lock);
  27294. +DEFINE_LOCAL_IRQ_LOCK(swapvec_lock);
  27295. +
  27296. /*
  27297. * This path almost never happens for VM activity - pages are normally
  27298. * freed via pagevecs. But it gets used by networking.
  27299. @@ -481,11 +485,11 @@
  27300. unsigned long flags;
  27301. page_cache_get(page);
  27302. - local_irq_save(flags);
  27303. + local_lock_irqsave(rotate_lock, flags);
  27304. pvec = this_cpu_ptr(&lru_rotate_pvecs);
  27305. if (!pagevec_add(pvec, page))
  27306. pagevec_move_tail(pvec);
  27307. - local_irq_restore(flags);
  27308. + local_unlock_irqrestore(rotate_lock, flags);
  27309. }
  27310. }
  27311. @@ -536,12 +540,13 @@
  27312. void activate_page(struct page *page)
  27313. {
  27314. if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
  27315. - struct pagevec *pvec = &get_cpu_var(activate_page_pvecs);
  27316. + struct pagevec *pvec = &get_locked_var(swapvec_lock,
  27317. + activate_page_pvecs);
  27318. page_cache_get(page);
  27319. if (!pagevec_add(pvec, page))
  27320. pagevec_lru_move_fn(pvec, __activate_page, NULL);
  27321. - put_cpu_var(activate_page_pvecs);
  27322. + put_locked_var(swapvec_lock, activate_page_pvecs);
  27323. }
  27324. }
  27325. @@ -567,7 +572,7 @@
  27326. static void __lru_cache_activate_page(struct page *page)
  27327. {
  27328. - struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
  27329. + struct pagevec *pvec = &get_locked_var(swapvec_lock, lru_add_pvec);
  27330. int i;
  27331. /*
  27332. @@ -589,7 +594,7 @@
  27333. }
  27334. }
  27335. - put_cpu_var(lru_add_pvec);
  27336. + put_locked_var(swapvec_lock, lru_add_pvec);
  27337. }
  27338. /*
  27339. @@ -630,13 +635,13 @@
  27340. static void __lru_cache_add(struct page *page)
  27341. {
  27342. - struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
  27343. + struct pagevec *pvec = &get_locked_var(swapvec_lock, lru_add_pvec);
  27344. page_cache_get(page);
  27345. if (!pagevec_space(pvec))
  27346. __pagevec_lru_add(pvec);
  27347. pagevec_add(pvec, page);
  27348. - put_cpu_var(lru_add_pvec);
  27349. + put_locked_var(swapvec_lock, lru_add_pvec);
  27350. }
  27351. /**
  27352. @@ -816,9 +821,15 @@
  27353. unsigned long flags;
  27354. /* No harm done if a racing interrupt already did this */
  27355. - local_irq_save(flags);
  27356. +#ifdef CONFIG_PREEMPT_RT_BASE
  27357. + local_lock_irqsave_on(rotate_lock, flags, cpu);
  27358. + pagevec_move_tail(pvec);
  27359. + local_unlock_irqrestore_on(rotate_lock, flags, cpu);
  27360. +#else
  27361. + local_lock_irqsave(rotate_lock, flags);
  27362. pagevec_move_tail(pvec);
  27363. - local_irq_restore(flags);
  27364. + local_unlock_irqrestore(rotate_lock, flags);
  27365. +#endif
  27366. }
  27367. pvec = &per_cpu(lru_deactivate_file_pvecs, cpu);
  27368. @@ -846,26 +857,47 @@
  27369. return;
  27370. if (likely(get_page_unless_zero(page))) {
  27371. - struct pagevec *pvec = &get_cpu_var(lru_deactivate_file_pvecs);
  27372. + struct pagevec *pvec = &get_locked_var(swapvec_lock,
  27373. + lru_deactivate_file_pvecs);
  27374. if (!pagevec_add(pvec, page))
  27375. pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
  27376. - put_cpu_var(lru_deactivate_file_pvecs);
  27377. + put_locked_var(swapvec_lock, lru_deactivate_file_pvecs);
  27378. }
  27379. }
  27380. void lru_add_drain(void)
  27381. {
  27382. - lru_add_drain_cpu(get_cpu());
  27383. - put_cpu();
  27384. + lru_add_drain_cpu(local_lock_cpu(swapvec_lock));
  27385. + local_unlock_cpu(swapvec_lock);
  27386. }
  27387. +
  27388. +#ifdef CONFIG_PREEMPT_RT_BASE
  27389. +static inline void remote_lru_add_drain(int cpu, struct cpumask *has_work)
  27390. +{
  27391. + local_lock_on(swapvec_lock, cpu);
  27392. + lru_add_drain_cpu(cpu);
  27393. + local_unlock_on(swapvec_lock, cpu);
  27394. +}
  27395. +
  27396. +#else
  27397. +
  27398. static void lru_add_drain_per_cpu(struct work_struct *dummy)
  27399. {
  27400. lru_add_drain();
  27401. }
  27402. static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);
  27403. +static inline void remote_lru_add_drain(int cpu, struct cpumask *has_work)
  27404. +{
  27405. + struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
  27406. +
  27407. + INIT_WORK(work, lru_add_drain_per_cpu);
  27408. + schedule_work_on(cpu, work);
  27409. + cpumask_set_cpu(cpu, has_work);
  27410. +}
  27411. +#endif
  27412. void lru_add_drain_all(void)
  27413. {
  27414. @@ -878,20 +910,17 @@
  27415. cpumask_clear(&has_work);
  27416. for_each_online_cpu(cpu) {
  27417. - struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
  27418. -
  27419. if (pagevec_count(&per_cpu(lru_add_pvec, cpu)) ||
  27420. pagevec_count(&per_cpu(lru_rotate_pvecs, cpu)) ||
  27421. pagevec_count(&per_cpu(lru_deactivate_file_pvecs, cpu)) ||
  27422. - need_activate_page_drain(cpu)) {
  27423. - INIT_WORK(work, lru_add_drain_per_cpu);
  27424. - schedule_work_on(cpu, work);
  27425. - cpumask_set_cpu(cpu, &has_work);
  27426. - }
  27427. + need_activate_page_drain(cpu))
  27428. + remote_lru_add_drain(cpu, &has_work);
  27429. }
  27430. +#ifndef CONFIG_PREEMPT_RT_BASE
  27431. for_each_cpu(cpu, &has_work)
  27432. flush_work(&per_cpu(lru_add_drain_work, cpu));
  27433. +#endif
  27434. put_online_cpus();
  27435. mutex_unlock(&lock);
  27436. diff -Nur linux-4.4.28.orig/mm/truncate.c linux-4.4.28/mm/truncate.c
  27437. --- linux-4.4.28.orig/mm/truncate.c 2016-10-28 09:53:25.000000000 +0200
  27438. +++ linux-4.4.28/mm/truncate.c 2016-11-01 21:26:58.828345638 +0100
  27439. @@ -56,8 +56,11 @@
  27440. * protected by mapping->tree_lock.
  27441. */
  27442. if (!workingset_node_shadows(node) &&
  27443. - !list_empty(&node->private_list))
  27444. - list_lru_del(&workingset_shadow_nodes, &node->private_list);
  27445. + !list_empty(&node->private_list)) {
  27446. + local_lock(workingset_shadow_lock);
  27447. + list_lru_del(&__workingset_shadow_nodes, &node->private_list);
  27448. + local_unlock(workingset_shadow_lock);
  27449. + }
  27450. __radix_tree_delete_node(&mapping->page_tree, node);
  27451. unlock:
  27452. spin_unlock_irq(&mapping->tree_lock);
  27453. diff -Nur linux-4.4.28.orig/mm/vmalloc.c linux-4.4.28/mm/vmalloc.c
  27454. --- linux-4.4.28.orig/mm/vmalloc.c 2016-10-28 09:53:25.000000000 +0200
  27455. +++ linux-4.4.28/mm/vmalloc.c 2016-11-01 21:26:58.828345638 +0100
  27456. @@ -821,7 +821,7 @@
  27457. struct vmap_block *vb;
  27458. struct vmap_area *va;
  27459. unsigned long vb_idx;
  27460. - int node, err;
  27461. + int node, err, cpu;
  27462. void *vaddr;
  27463. node = numa_node_id();
  27464. @@ -864,11 +864,12 @@
  27465. BUG_ON(err);
  27466. radix_tree_preload_end();
  27467. - vbq = &get_cpu_var(vmap_block_queue);
  27468. + cpu = get_cpu_light();
  27469. + vbq = this_cpu_ptr(&vmap_block_queue);
  27470. spin_lock(&vbq->lock);
  27471. list_add_tail_rcu(&vb->free_list, &vbq->free);
  27472. spin_unlock(&vbq->lock);
  27473. - put_cpu_var(vmap_block_queue);
  27474. + put_cpu_light();
  27475. return vaddr;
  27476. }
  27477. @@ -937,6 +938,7 @@
  27478. struct vmap_block *vb;
  27479. void *vaddr = NULL;
  27480. unsigned int order;
  27481. + int cpu;
  27482. BUG_ON(offset_in_page(size));
  27483. BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
  27484. @@ -951,7 +953,8 @@
  27485. order = get_order(size);
  27486. rcu_read_lock();
  27487. - vbq = &get_cpu_var(vmap_block_queue);
  27488. + cpu = get_cpu_light();
  27489. + vbq = this_cpu_ptr(&vmap_block_queue);
  27490. list_for_each_entry_rcu(vb, &vbq->free, free_list) {
  27491. unsigned long pages_off;
  27492. @@ -974,7 +977,7 @@
  27493. break;
  27494. }
  27495. - put_cpu_var(vmap_block_queue);
  27496. + put_cpu_light();
  27497. rcu_read_unlock();
  27498. /* Allocate new block if nothing was found */
  27499. diff -Nur linux-4.4.28.orig/mm/vmstat.c linux-4.4.28/mm/vmstat.c
  27500. --- linux-4.4.28.orig/mm/vmstat.c 2016-10-28 09:53:25.000000000 +0200
  27501. +++ linux-4.4.28/mm/vmstat.c 2016-11-01 21:26:58.828345638 +0100
  27502. @@ -226,6 +226,7 @@
  27503. long x;
  27504. long t;
  27505. + preempt_disable_rt();
  27506. x = delta + __this_cpu_read(*p);
  27507. t = __this_cpu_read(pcp->stat_threshold);
  27508. @@ -235,6 +236,7 @@
  27509. x = 0;
  27510. }
  27511. __this_cpu_write(*p, x);
  27512. + preempt_enable_rt();
  27513. }
  27514. EXPORT_SYMBOL(__mod_zone_page_state);
  27515. @@ -267,6 +269,7 @@
  27516. s8 __percpu *p = pcp->vm_stat_diff + item;
  27517. s8 v, t;
  27518. + preempt_disable_rt();
  27519. v = __this_cpu_inc_return(*p);
  27520. t = __this_cpu_read(pcp->stat_threshold);
  27521. if (unlikely(v > t)) {
  27522. @@ -275,6 +278,7 @@
  27523. zone_page_state_add(v + overstep, zone, item);
  27524. __this_cpu_write(*p, -overstep);
  27525. }
  27526. + preempt_enable_rt();
  27527. }
  27528. void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
  27529. @@ -289,6 +293,7 @@
  27530. s8 __percpu *p = pcp->vm_stat_diff + item;
  27531. s8 v, t;
  27532. + preempt_disable_rt();
  27533. v = __this_cpu_dec_return(*p);
  27534. t = __this_cpu_read(pcp->stat_threshold);
  27535. if (unlikely(v < - t)) {
  27536. @@ -297,6 +302,7 @@
  27537. zone_page_state_add(v - overstep, zone, item);
  27538. __this_cpu_write(*p, overstep);
  27539. }
  27540. + preempt_enable_rt();
  27541. }
  27542. void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
  27543. diff -Nur linux-4.4.28.orig/mm/workingset.c linux-4.4.28/mm/workingset.c
  27544. --- linux-4.4.28.orig/mm/workingset.c 2016-10-28 09:53:25.000000000 +0200
  27545. +++ linux-4.4.28/mm/workingset.c 2016-11-01 21:26:58.832345891 +0100
  27546. @@ -264,7 +264,8 @@
  27547. * point where they would still be useful.
  27548. */
  27549. -struct list_lru workingset_shadow_nodes;
  27550. +struct list_lru __workingset_shadow_nodes;
  27551. +DEFINE_LOCAL_IRQ_LOCK(workingset_shadow_lock);
  27552. static unsigned long count_shadow_nodes(struct shrinker *shrinker,
  27553. struct shrink_control *sc)
  27554. @@ -274,9 +275,9 @@
  27555. unsigned long pages;
  27556. /* list_lru lock nests inside IRQ-safe mapping->tree_lock */
  27557. - local_irq_disable();
  27558. - shadow_nodes = list_lru_shrink_count(&workingset_shadow_nodes, sc);
  27559. - local_irq_enable();
  27560. + local_lock_irq(workingset_shadow_lock);
  27561. + shadow_nodes = list_lru_shrink_count(&__workingset_shadow_nodes, sc);
  27562. + local_unlock_irq(workingset_shadow_lock);
  27563. pages = node_present_pages(sc->nid);
  27564. /*
  27565. @@ -361,9 +362,9 @@
  27566. spin_unlock(&mapping->tree_lock);
  27567. ret = LRU_REMOVED_RETRY;
  27568. out:
  27569. - local_irq_enable();
  27570. + local_unlock_irq(workingset_shadow_lock);
  27571. cond_resched();
  27572. - local_irq_disable();
  27573. + local_lock_irq(workingset_shadow_lock);
  27574. spin_lock(lru_lock);
  27575. return ret;
  27576. }
  27577. @@ -374,10 +375,10 @@
  27578. unsigned long ret;
  27579. /* list_lru lock nests inside IRQ-safe mapping->tree_lock */
  27580. - local_irq_disable();
  27581. - ret = list_lru_shrink_walk(&workingset_shadow_nodes, sc,
  27582. + local_lock_irq(workingset_shadow_lock);
  27583. + ret = list_lru_shrink_walk(&__workingset_shadow_nodes, sc,
  27584. shadow_lru_isolate, NULL);
  27585. - local_irq_enable();
  27586. + local_unlock_irq(workingset_shadow_lock);
  27587. return ret;
  27588. }
  27589. @@ -398,7 +399,7 @@
  27590. {
  27591. int ret;
  27592. - ret = list_lru_init_key(&workingset_shadow_nodes, &shadow_nodes_key);
  27593. + ret = list_lru_init_key(&__workingset_shadow_nodes, &shadow_nodes_key);
  27594. if (ret)
  27595. goto err;
  27596. ret = register_shrinker(&workingset_shadow_shrinker);
  27597. @@ -406,7 +407,7 @@
  27598. goto err_list_lru;
  27599. return 0;
  27600. err_list_lru:
  27601. - list_lru_destroy(&workingset_shadow_nodes);
  27602. + list_lru_destroy(&__workingset_shadow_nodes);
  27603. err:
  27604. return ret;
  27605. }
  27606. diff -Nur linux-4.4.28.orig/mm/zsmalloc.c linux-4.4.28/mm/zsmalloc.c
  27607. --- linux-4.4.28.orig/mm/zsmalloc.c 2016-10-28 09:53:25.000000000 +0200
  27608. +++ linux-4.4.28/mm/zsmalloc.c 2016-11-01 21:26:58.832345891 +0100
  27609. @@ -64,6 +64,7 @@
  27610. #include <linux/debugfs.h>
  27611. #include <linux/zsmalloc.h>
  27612. #include <linux/zpool.h>
  27613. +#include <linux/locallock.h>
  27614. /*
  27615. * This must be power of 2 and greater than of equal to sizeof(link_free).
  27616. @@ -403,6 +404,7 @@
  27617. /* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
  27618. static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
  27619. +static DEFINE_LOCAL_IRQ_LOCK(zs_map_area_lock);
  27620. static int is_first_page(struct page *page)
  27621. {
  27622. @@ -1289,7 +1291,7 @@
  27623. class = pool->size_class[class_idx];
  27624. off = obj_idx_to_offset(page, obj_idx, class->size);
  27625. - area = &get_cpu_var(zs_map_area);
  27626. + area = &get_locked_var(zs_map_area_lock, zs_map_area);
  27627. area->vm_mm = mm;
  27628. if (off + class->size <= PAGE_SIZE) {
  27629. /* this object is contained entirely within a page */
  27630. @@ -1342,7 +1344,7 @@
  27631. __zs_unmap_object(area, pages, off, class->size);
  27632. }
  27633. - put_cpu_var(zs_map_area);
  27634. + put_locked_var(zs_map_area_lock, zs_map_area);
  27635. unpin_tag(handle);
  27636. }
  27637. EXPORT_SYMBOL_GPL(zs_unmap_object);
  27638. diff -Nur linux-4.4.28.orig/net/core/dev.c linux-4.4.28/net/core/dev.c
  27639. --- linux-4.4.28.orig/net/core/dev.c 2016-10-28 09:53:25.000000000 +0200
  27640. +++ linux-4.4.28/net/core/dev.c 2016-11-01 21:26:58.836346147 +0100
  27641. @@ -186,6 +186,7 @@
  27642. static DEFINE_HASHTABLE(napi_hash, 8);
  27643. static seqcount_t devnet_rename_seq;
  27644. +static DEFINE_MUTEX(devnet_rename_mutex);
  27645. static inline void dev_base_seq_inc(struct net *net)
  27646. {
  27647. @@ -207,14 +208,14 @@
  27648. static inline void rps_lock(struct softnet_data *sd)
  27649. {
  27650. #ifdef CONFIG_RPS
  27651. - spin_lock(&sd->input_pkt_queue.lock);
  27652. + raw_spin_lock(&sd->input_pkt_queue.raw_lock);
  27653. #endif
  27654. }
  27655. static inline void rps_unlock(struct softnet_data *sd)
  27656. {
  27657. #ifdef CONFIG_RPS
  27658. - spin_unlock(&sd->input_pkt_queue.lock);
  27659. + raw_spin_unlock(&sd->input_pkt_queue.raw_lock);
  27660. #endif
  27661. }
  27662. @@ -884,7 +885,8 @@
  27663. strcpy(name, dev->name);
  27664. rcu_read_unlock();
  27665. if (read_seqcount_retry(&devnet_rename_seq, seq)) {
  27666. - cond_resched();
  27667. + mutex_lock(&devnet_rename_mutex);
  27668. + mutex_unlock(&devnet_rename_mutex);
  27669. goto retry;
  27670. }
  27671. @@ -1153,20 +1155,17 @@
  27672. if (dev->flags & IFF_UP)
  27673. return -EBUSY;
  27674. - write_seqcount_begin(&devnet_rename_seq);
  27675. + mutex_lock(&devnet_rename_mutex);
  27676. + __raw_write_seqcount_begin(&devnet_rename_seq);
  27677. - if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
  27678. - write_seqcount_end(&devnet_rename_seq);
  27679. - return 0;
  27680. - }
  27681. + if (strncmp(newname, dev->name, IFNAMSIZ) == 0)
  27682. + goto outunlock;
  27683. memcpy(oldname, dev->name, IFNAMSIZ);
  27684. err = dev_get_valid_name(net, dev, newname);
  27685. - if (err < 0) {
  27686. - write_seqcount_end(&devnet_rename_seq);
  27687. - return err;
  27688. - }
  27689. + if (err < 0)
  27690. + goto outunlock;
  27691. if (oldname[0] && !strchr(oldname, '%'))
  27692. netdev_info(dev, "renamed from %s\n", oldname);
  27693. @@ -1179,11 +1178,12 @@
  27694. if (ret) {
  27695. memcpy(dev->name, oldname, IFNAMSIZ);
  27696. dev->name_assign_type = old_assign_type;
  27697. - write_seqcount_end(&devnet_rename_seq);
  27698. - return ret;
  27699. + err = ret;
  27700. + goto outunlock;
  27701. }
  27702. - write_seqcount_end(&devnet_rename_seq);
  27703. + __raw_write_seqcount_end(&devnet_rename_seq);
  27704. + mutex_unlock(&devnet_rename_mutex);
  27705. netdev_adjacent_rename_links(dev, oldname);
  27706. @@ -1204,7 +1204,8 @@
  27707. /* err >= 0 after dev_alloc_name() or stores the first errno */
  27708. if (err >= 0) {
  27709. err = ret;
  27710. - write_seqcount_begin(&devnet_rename_seq);
  27711. + mutex_lock(&devnet_rename_mutex);
  27712. + __raw_write_seqcount_begin(&devnet_rename_seq);
  27713. memcpy(dev->name, oldname, IFNAMSIZ);
  27714. memcpy(oldname, newname, IFNAMSIZ);
  27715. dev->name_assign_type = old_assign_type;
  27716. @@ -1217,6 +1218,11 @@
  27717. }
  27718. return err;
  27719. +
  27720. +outunlock:
  27721. + __raw_write_seqcount_end(&devnet_rename_seq);
  27722. + mutex_unlock(&devnet_rename_mutex);
  27723. + return err;
  27724. }
  27725. /**
  27726. @@ -2246,6 +2252,7 @@
  27727. sd->output_queue_tailp = &q->next_sched;
  27728. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  27729. local_irq_restore(flags);
  27730. + preempt_check_resched_rt();
  27731. }
  27732. void __netif_schedule(struct Qdisc *q)
  27733. @@ -2327,6 +2334,7 @@
  27734. __this_cpu_write(softnet_data.completion_queue, skb);
  27735. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  27736. local_irq_restore(flags);
  27737. + preempt_check_resched_rt();
  27738. }
  27739. EXPORT_SYMBOL(__dev_kfree_skb_irq);
  27740. @@ -2883,7 +2891,11 @@
  27741. * This permits __QDISC___STATE_RUNNING owner to get the lock more
  27742. * often and dequeue packets faster.
  27743. */
  27744. +#ifdef CONFIG_PREEMPT_RT_FULL
  27745. + contended = true;
  27746. +#else
  27747. contended = qdisc_is_running(q);
  27748. +#endif
  27749. if (unlikely(contended))
  27750. spin_lock(&q->busylock);
  27751. @@ -2943,9 +2955,44 @@
  27752. #define skb_update_prio(skb)
  27753. #endif
  27754. +#ifdef CONFIG_PREEMPT_RT_FULL
  27755. +
  27756. +static inline int xmit_rec_read(void)
  27757. +{
  27758. + return current->xmit_recursion;
  27759. +}
  27760. +
  27761. +static inline void xmit_rec_inc(void)
  27762. +{
  27763. + current->xmit_recursion++;
  27764. +}
  27765. +
  27766. +static inline void xmit_rec_dec(void)
  27767. +{
  27768. + current->xmit_recursion--;
  27769. +}
  27770. +
  27771. +#else
  27772. +
  27773. DEFINE_PER_CPU(int, xmit_recursion);
  27774. EXPORT_SYMBOL(xmit_recursion);
  27775. +static inline int xmit_rec_read(void)
  27776. +{
  27777. + return __this_cpu_read(xmit_recursion);
  27778. +}
  27779. +
  27780. +static inline void xmit_rec_inc(void)
  27781. +{
  27782. + __this_cpu_inc(xmit_recursion);
  27783. +}
  27784. +
  27785. +static inline void xmit_rec_dec(void)
  27786. +{
  27787. + __this_cpu_dec(xmit_recursion);
  27788. +}
  27789. +#endif
  27790. +
  27791. #define RECURSION_LIMIT 10
  27792. /**
  27793. @@ -3138,7 +3185,7 @@
  27794. if (txq->xmit_lock_owner != cpu) {
  27795. - if (__this_cpu_read(xmit_recursion) > RECURSION_LIMIT)
  27796. + if (xmit_rec_read() > RECURSION_LIMIT)
  27797. goto recursion_alert;
  27798. skb = validate_xmit_skb(skb, dev);
  27799. @@ -3148,9 +3195,9 @@
  27800. HARD_TX_LOCK(dev, txq, cpu);
  27801. if (!netif_xmit_stopped(txq)) {
  27802. - __this_cpu_inc(xmit_recursion);
  27803. + xmit_rec_inc();
  27804. skb = dev_hard_start_xmit(skb, dev, txq, &rc);
  27805. - __this_cpu_dec(xmit_recursion);
  27806. + xmit_rec_dec();
  27807. if (dev_xmit_complete(rc)) {
  27808. HARD_TX_UNLOCK(dev, txq);
  27809. goto out;
  27810. @@ -3524,6 +3571,7 @@
  27811. rps_unlock(sd);
  27812. local_irq_restore(flags);
  27813. + preempt_check_resched_rt();
  27814. atomic_long_inc(&skb->dev->rx_dropped);
  27815. kfree_skb(skb);
  27816. @@ -3542,7 +3590,7 @@
  27817. struct rps_dev_flow voidflow, *rflow = &voidflow;
  27818. int cpu;
  27819. - preempt_disable();
  27820. + migrate_disable();
  27821. rcu_read_lock();
  27822. cpu = get_rps_cpu(skb->dev, skb, &rflow);
  27823. @@ -3552,13 +3600,13 @@
  27824. ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
  27825. rcu_read_unlock();
  27826. - preempt_enable();
  27827. + migrate_enable();
  27828. } else
  27829. #endif
  27830. {
  27831. unsigned int qtail;
  27832. - ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
  27833. - put_cpu();
  27834. + ret = enqueue_to_backlog(skb, get_cpu_light(), &qtail);
  27835. + put_cpu_light();
  27836. }
  27837. return ret;
  27838. }
  27839. @@ -3592,16 +3640,44 @@
  27840. trace_netif_rx_ni_entry(skb);
  27841. - preempt_disable();
  27842. + local_bh_disable();
  27843. err = netif_rx_internal(skb);
  27844. - if (local_softirq_pending())
  27845. - do_softirq();
  27846. - preempt_enable();
  27847. + local_bh_enable();
  27848. return err;
  27849. }
  27850. EXPORT_SYMBOL(netif_rx_ni);
  27851. +#ifdef CONFIG_PREEMPT_RT_FULL
  27852. +/*
  27853. + * RT runs ksoftirqd as a real time thread and the root_lock is a
  27854. + * "sleeping spinlock". If the trylock fails then we can go into an
  27855. + * infinite loop when ksoftirqd preempted the task which actually
  27856. + * holds the lock, because we requeue q and raise NET_TX softirq
  27857. + * causing ksoftirqd to loop forever.
  27858. + *
  27859. + * It's safe to use spin_lock on RT here as softirqs run in thread
  27860. + * context and cannot deadlock against the thread which is holding
  27861. + * root_lock.
  27862. + *
  27863. + * On !RT the trylock might fail, but there we bail out from the
  27864. + * softirq loop after 10 attempts which we can't do on RT. And the
  27865. + * task holding root_lock cannot be preempted, so the only downside of
  27866. + * that trylock is that we need 10 loops to decide that we should have
  27867. + * given up in the first one :)
  27868. + */
  27869. +static inline int take_root_lock(spinlock_t *lock)
  27870. +{
  27871. + spin_lock(lock);
  27872. + return 1;
  27873. +}
  27874. +#else
  27875. +static inline int take_root_lock(spinlock_t *lock)
  27876. +{
  27877. + return spin_trylock(lock);
  27878. +}
  27879. +#endif
  27880. +
  27881. static void net_tx_action(struct softirq_action *h)
  27882. {
  27883. struct softnet_data *sd = this_cpu_ptr(&softnet_data);
  27884. @@ -3643,7 +3719,7 @@
  27885. head = head->next_sched;
  27886. root_lock = qdisc_lock(q);
  27887. - if (spin_trylock(root_lock)) {
  27888. + if (take_root_lock(root_lock)) {
  27889. smp_mb__before_atomic();
  27890. clear_bit(__QDISC_STATE_SCHED,
  27891. &q->state);
  27892. @@ -4065,7 +4141,7 @@
  27893. skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
  27894. if (skb->dev == dev) {
  27895. __skb_unlink(skb, &sd->input_pkt_queue);
  27896. - kfree_skb(skb);
  27897. + __skb_queue_tail(&sd->tofree_queue, skb);
  27898. input_queue_head_incr(sd);
  27899. }
  27900. }
  27901. @@ -4074,10 +4150,13 @@
  27902. skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
  27903. if (skb->dev == dev) {
  27904. __skb_unlink(skb, &sd->process_queue);
  27905. - kfree_skb(skb);
  27906. + __skb_queue_tail(&sd->tofree_queue, skb);
  27907. input_queue_head_incr(sd);
  27908. }
  27909. }
  27910. +
  27911. + if (!skb_queue_empty(&sd->tofree_queue))
  27912. + raise_softirq_irqoff(NET_RX_SOFTIRQ);
  27913. }
  27914. static int napi_gro_complete(struct sk_buff *skb)
  27915. @@ -4531,6 +4610,7 @@
  27916. sd->rps_ipi_list = NULL;
  27917. local_irq_enable();
  27918. + preempt_check_resched_rt();
  27919. /* Send pending IPI's to kick RPS processing on remote cpus. */
  27920. while (remsd) {
  27921. @@ -4544,6 +4624,7 @@
  27922. } else
  27923. #endif
  27924. local_irq_enable();
  27925. + preempt_check_resched_rt();
  27926. }
  27927. static bool sd_has_rps_ipi_waiting(struct softnet_data *sd)
  27928. @@ -4625,6 +4706,7 @@
  27929. local_irq_save(flags);
  27930. ____napi_schedule(this_cpu_ptr(&softnet_data), n);
  27931. local_irq_restore(flags);
  27932. + preempt_check_resched_rt();
  27933. }
  27934. EXPORT_SYMBOL(__napi_schedule);
  27935. @@ -4901,7 +4983,7 @@
  27936. list_splice_tail(&repoll, &list);
  27937. list_splice(&list, &sd->poll_list);
  27938. if (!list_empty(&sd->poll_list))
  27939. - __raise_softirq_irqoff(NET_RX_SOFTIRQ);
  27940. + __raise_softirq_irqoff_ksoft(NET_RX_SOFTIRQ);
  27941. net_rps_action_and_irq_enable(sd);
  27942. }
  27943. @@ -7234,7 +7316,7 @@
  27944. void synchronize_net(void)
  27945. {
  27946. might_sleep();
  27947. - if (rtnl_is_locked())
  27948. + if (rtnl_is_locked() && !IS_ENABLED(CONFIG_PREEMPT_RT_FULL))
  27949. synchronize_rcu_expedited();
  27950. else
  27951. synchronize_rcu();
  27952. @@ -7475,16 +7557,20 @@
  27953. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  27954. local_irq_enable();
  27955. + preempt_check_resched_rt();
  27956. /* Process offline CPU's input_pkt_queue */
  27957. while ((skb = __skb_dequeue(&oldsd->process_queue))) {
  27958. netif_rx_ni(skb);
  27959. input_queue_head_incr(oldsd);
  27960. }
  27961. - while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) {
  27962. + while ((skb = __skb_dequeue(&oldsd->input_pkt_queue))) {
  27963. netif_rx_ni(skb);
  27964. input_queue_head_incr(oldsd);
  27965. }
  27966. + while ((skb = __skb_dequeue(&oldsd->tofree_queue))) {
  27967. + kfree_skb(skb);
  27968. + }
  27969. return NOTIFY_OK;
  27970. }
  27971. @@ -7786,8 +7872,9 @@
  27972. for_each_possible_cpu(i) {
  27973. struct softnet_data *sd = &per_cpu(softnet_data, i);
  27974. - skb_queue_head_init(&sd->input_pkt_queue);
  27975. - skb_queue_head_init(&sd->process_queue);
  27976. + skb_queue_head_init_raw(&sd->input_pkt_queue);
  27977. + skb_queue_head_init_raw(&sd->process_queue);
  27978. + skb_queue_head_init_raw(&sd->tofree_queue);
  27979. INIT_LIST_HEAD(&sd->poll_list);
  27980. sd->output_queue_tailp = &sd->output_queue;
  27981. #ifdef CONFIG_RPS
  27982. diff -Nur linux-4.4.28.orig/net/core/skbuff.c linux-4.4.28/net/core/skbuff.c
  27983. --- linux-4.4.28.orig/net/core/skbuff.c 2016-10-28 09:53:25.000000000 +0200
  27984. +++ linux-4.4.28/net/core/skbuff.c 2016-11-01 21:26:58.836346147 +0100
  27985. @@ -63,6 +63,7 @@
  27986. #include <linux/errqueue.h>
  27987. #include <linux/prefetch.h>
  27988. #include <linux/if_vlan.h>
  27989. +#include <linux/locallock.h>
  27990. #include <net/protocol.h>
  27991. #include <net/dst.h>
  27992. @@ -351,6 +352,8 @@
  27993. static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
  27994. static DEFINE_PER_CPU(struct page_frag_cache, napi_alloc_cache);
  27995. +static DEFINE_LOCAL_IRQ_LOCK(netdev_alloc_lock);
  27996. +static DEFINE_LOCAL_IRQ_LOCK(napi_alloc_cache_lock);
  27997. static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
  27998. {
  27999. @@ -358,10 +361,10 @@
  28000. unsigned long flags;
  28001. void *data;
  28002. - local_irq_save(flags);
  28003. + local_lock_irqsave(netdev_alloc_lock, flags);
  28004. nc = this_cpu_ptr(&netdev_alloc_cache);
  28005. data = __alloc_page_frag(nc, fragsz, gfp_mask);
  28006. - local_irq_restore(flags);
  28007. + local_unlock_irqrestore(netdev_alloc_lock, flags);
  28008. return data;
  28009. }
  28010. @@ -380,9 +383,13 @@
  28011. static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
  28012. {
  28013. - struct page_frag_cache *nc = this_cpu_ptr(&napi_alloc_cache);
  28014. + struct page_frag_cache *nc;
  28015. + void *data;
  28016. - return __alloc_page_frag(nc, fragsz, gfp_mask);
  28017. + nc = &get_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  28018. + data = __alloc_page_frag(nc, fragsz, gfp_mask);
  28019. + put_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  28020. + return data;
  28021. }
  28022. void *napi_alloc_frag(unsigned int fragsz)
  28023. @@ -429,13 +436,13 @@
  28024. if (sk_memalloc_socks())
  28025. gfp_mask |= __GFP_MEMALLOC;
  28026. - local_irq_save(flags);
  28027. + local_lock_irqsave(netdev_alloc_lock, flags);
  28028. nc = this_cpu_ptr(&netdev_alloc_cache);
  28029. data = __alloc_page_frag(nc, len, gfp_mask);
  28030. pfmemalloc = nc->pfmemalloc;
  28031. - local_irq_restore(flags);
  28032. + local_unlock_irqrestore(netdev_alloc_lock, flags);
  28033. if (unlikely(!data))
  28034. return NULL;
  28035. @@ -476,9 +483,10 @@
  28036. struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
  28037. gfp_t gfp_mask)
  28038. {
  28039. - struct page_frag_cache *nc = this_cpu_ptr(&napi_alloc_cache);
  28040. + struct page_frag_cache *nc;
  28041. struct sk_buff *skb;
  28042. void *data;
  28043. + bool pfmemalloc;
  28044. len += NET_SKB_PAD + NET_IP_ALIGN;
  28045. @@ -496,7 +504,11 @@
  28046. if (sk_memalloc_socks())
  28047. gfp_mask |= __GFP_MEMALLOC;
  28048. + nc = &get_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  28049. data = __alloc_page_frag(nc, len, gfp_mask);
  28050. + pfmemalloc = nc->pfmemalloc;
  28051. + put_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  28052. +
  28053. if (unlikely(!data))
  28054. return NULL;
  28055. @@ -507,7 +519,7 @@
  28056. }
  28057. /* use OR instead of assignment to avoid clearing of bits in mask */
  28058. - if (nc->pfmemalloc)
  28059. + if (pfmemalloc)
  28060. skb->pfmemalloc = 1;
  28061. skb->head_frag = 1;
  28062. diff -Nur linux-4.4.28.orig/net/core/sock.c linux-4.4.28/net/core/sock.c
  28063. --- linux-4.4.28.orig/net/core/sock.c 2016-10-28 09:53:25.000000000 +0200
  28064. +++ linux-4.4.28/net/core/sock.c 2016-11-01 21:26:58.836346147 +0100
  28065. @@ -2435,12 +2435,11 @@
  28066. if (sk->sk_lock.owned)
  28067. __lock_sock(sk);
  28068. sk->sk_lock.owned = 1;
  28069. - spin_unlock(&sk->sk_lock.slock);
  28070. + spin_unlock_bh(&sk->sk_lock.slock);
  28071. /*
  28072. * The sk_lock has mutex_lock() semantics here:
  28073. */
  28074. mutex_acquire(&sk->sk_lock.dep_map, subclass, 0, _RET_IP_);
  28075. - local_bh_enable();
  28076. }
  28077. EXPORT_SYMBOL(lock_sock_nested);
  28078. diff -Nur linux-4.4.28.orig/net/ipv4/icmp.c linux-4.4.28/net/ipv4/icmp.c
  28079. --- linux-4.4.28.orig/net/ipv4/icmp.c 2016-10-28 09:53:25.000000000 +0200
  28080. +++ linux-4.4.28/net/ipv4/icmp.c 2016-11-01 21:26:58.836346147 +0100
  28081. @@ -69,6 +69,7 @@
  28082. #include <linux/jiffies.h>
  28083. #include <linux/kernel.h>
  28084. #include <linux/fcntl.h>
  28085. +#include <linux/sysrq.h>
  28086. #include <linux/socket.h>
  28087. #include <linux/in.h>
  28088. #include <linux/inet.h>
  28089. @@ -77,6 +78,7 @@
  28090. #include <linux/string.h>
  28091. #include <linux/netfilter_ipv4.h>
  28092. #include <linux/slab.h>
  28093. +#include <linux/locallock.h>
  28094. #include <net/snmp.h>
  28095. #include <net/ip.h>
  28096. #include <net/route.h>
  28097. @@ -204,6 +206,8 @@
  28098. *
  28099. * On SMP we have one ICMP socket per-cpu.
  28100. */
  28101. +static DEFINE_LOCAL_IRQ_LOCK(icmp_sk_lock);
  28102. +
  28103. static struct sock *icmp_sk(struct net *net)
  28104. {
  28105. return *this_cpu_ptr(net->ipv4.icmp_sk);
  28106. @@ -215,12 +219,14 @@
  28107. local_bh_disable();
  28108. + local_lock(icmp_sk_lock);
  28109. sk = icmp_sk(net);
  28110. if (unlikely(!spin_trylock(&sk->sk_lock.slock))) {
  28111. /* This can happen if the output path signals a
  28112. * dst_link_failure() for an outgoing ICMP packet.
  28113. */
  28114. + local_unlock(icmp_sk_lock);
  28115. local_bh_enable();
  28116. return NULL;
  28117. }
  28118. @@ -230,6 +236,7 @@
  28119. static inline void icmp_xmit_unlock(struct sock *sk)
  28120. {
  28121. spin_unlock_bh(&sk->sk_lock.slock);
  28122. + local_unlock(icmp_sk_lock);
  28123. }
  28124. int sysctl_icmp_msgs_per_sec __read_mostly = 1000;
  28125. @@ -358,6 +365,7 @@
  28126. struct sock *sk;
  28127. struct sk_buff *skb;
  28128. + local_lock(icmp_sk_lock);
  28129. sk = icmp_sk(dev_net((*rt)->dst.dev));
  28130. if (ip_append_data(sk, fl4, icmp_glue_bits, icmp_param,
  28131. icmp_param->data_len+icmp_param->head_len,
  28132. @@ -380,6 +388,7 @@
  28133. skb->ip_summed = CHECKSUM_NONE;
  28134. ip_push_pending_frames(sk, fl4);
  28135. }
  28136. + local_unlock(icmp_sk_lock);
  28137. }
  28138. /*
  28139. @@ -891,6 +900,30 @@
  28140. }
  28141. /*
  28142. + * 32bit and 64bit have different timestamp length, so we check for
  28143. + * the cookie at offset 20 and verify it is repeated at offset 50
  28144. + */
  28145. +#define CO_POS0 20
  28146. +#define CO_POS1 50
  28147. +#define CO_SIZE sizeof(int)
  28148. +#define ICMP_SYSRQ_SIZE 57
  28149. +
  28150. +/*
  28151. + * We got a ICMP_SYSRQ_SIZE sized ping request. Check for the cookie
  28152. + * pattern and if it matches send the next byte as a trigger to sysrq.
  28153. + */
  28154. +static void icmp_check_sysrq(struct net *net, struct sk_buff *skb)
  28155. +{
  28156. + int cookie = htonl(net->ipv4.sysctl_icmp_echo_sysrq);
  28157. + char *p = skb->data;
  28158. +
  28159. + if (!memcmp(&cookie, p + CO_POS0, CO_SIZE) &&
  28160. + !memcmp(&cookie, p + CO_POS1, CO_SIZE) &&
  28161. + p[CO_POS0 + CO_SIZE] == p[CO_POS1 + CO_SIZE])
  28162. + handle_sysrq(p[CO_POS0 + CO_SIZE]);
  28163. +}
  28164. +
  28165. +/*
  28166. * Handle ICMP_ECHO ("ping") requests.
  28167. *
  28168. * RFC 1122: 3.2.2.6 MUST have an echo server that answers ICMP echo
  28169. @@ -917,6 +950,11 @@
  28170. icmp_param.data_len = skb->len;
  28171. icmp_param.head_len = sizeof(struct icmphdr);
  28172. icmp_reply(&icmp_param, skb);
  28173. +
  28174. + if (skb->len == ICMP_SYSRQ_SIZE &&
  28175. + net->ipv4.sysctl_icmp_echo_sysrq) {
  28176. + icmp_check_sysrq(net, skb);
  28177. + }
  28178. }
  28179. /* should there be an ICMP stat for ignored echos? */
  28180. return true;
  28181. diff -Nur linux-4.4.28.orig/net/ipv4/sysctl_net_ipv4.c linux-4.4.28/net/ipv4/sysctl_net_ipv4.c
  28182. --- linux-4.4.28.orig/net/ipv4/sysctl_net_ipv4.c 2016-10-28 09:53:25.000000000 +0200
  28183. +++ linux-4.4.28/net/ipv4/sysctl_net_ipv4.c 2016-11-01 21:26:58.836346147 +0100
  28184. @@ -818,6 +818,13 @@
  28185. .proc_handler = proc_dointvec
  28186. },
  28187. {
  28188. + .procname = "icmp_echo_sysrq",
  28189. + .data = &init_net.ipv4.sysctl_icmp_echo_sysrq,
  28190. + .maxlen = sizeof(int),
  28191. + .mode = 0644,
  28192. + .proc_handler = proc_dointvec
  28193. + },
  28194. + {
  28195. .procname = "icmp_ignore_bogus_error_responses",
  28196. .data = &init_net.ipv4.sysctl_icmp_ignore_bogus_error_responses,
  28197. .maxlen = sizeof(int),
  28198. diff -Nur linux-4.4.28.orig/net/ipv4/tcp_ipv4.c linux-4.4.28/net/ipv4/tcp_ipv4.c
  28199. --- linux-4.4.28.orig/net/ipv4/tcp_ipv4.c 2016-10-28 09:53:25.000000000 +0200
  28200. +++ linux-4.4.28/net/ipv4/tcp_ipv4.c 2016-11-01 21:26:58.840346411 +0100
  28201. @@ -62,6 +62,7 @@
  28202. #include <linux/init.h>
  28203. #include <linux/times.h>
  28204. #include <linux/slab.h>
  28205. +#include <linux/locallock.h>
  28206. #include <net/net_namespace.h>
  28207. #include <net/icmp.h>
  28208. @@ -566,6 +567,7 @@
  28209. }
  28210. EXPORT_SYMBOL(tcp_v4_send_check);
  28211. +static DEFINE_LOCAL_IRQ_LOCK(tcp_sk_lock);
  28212. /*
  28213. * This routine will send an RST to the other tcp.
  28214. *
  28215. @@ -687,10 +689,13 @@
  28216. arg.bound_dev_if = sk->sk_bound_dev_if;
  28217. arg.tos = ip_hdr(skb)->tos;
  28218. +
  28219. + local_lock(tcp_sk_lock);
  28220. ip_send_unicast_reply(*this_cpu_ptr(net->ipv4.tcp_sk),
  28221. skb, &TCP_SKB_CB(skb)->header.h4.opt,
  28222. ip_hdr(skb)->saddr, ip_hdr(skb)->daddr,
  28223. &arg, arg.iov[0].iov_len);
  28224. + local_unlock(tcp_sk_lock);
  28225. TCP_INC_STATS_BH(net, TCP_MIB_OUTSEGS);
  28226. TCP_INC_STATS_BH(net, TCP_MIB_OUTRSTS);
  28227. @@ -772,10 +777,12 @@
  28228. if (oif)
  28229. arg.bound_dev_if = oif;
  28230. arg.tos = tos;
  28231. + local_lock(tcp_sk_lock);
  28232. ip_send_unicast_reply(*this_cpu_ptr(net->ipv4.tcp_sk),
  28233. skb, &TCP_SKB_CB(skb)->header.h4.opt,
  28234. ip_hdr(skb)->saddr, ip_hdr(skb)->daddr,
  28235. &arg, arg.iov[0].iov_len);
  28236. + local_unlock(tcp_sk_lock);
  28237. TCP_INC_STATS_BH(net, TCP_MIB_OUTSEGS);
  28238. }
  28239. diff -Nur linux-4.4.28.orig/net/mac80211/rx.c linux-4.4.28/net/mac80211/rx.c
  28240. --- linux-4.4.28.orig/net/mac80211/rx.c 2016-10-28 09:53:25.000000000 +0200
  28241. +++ linux-4.4.28/net/mac80211/rx.c 2016-11-01 21:26:58.840346411 +0100
  28242. @@ -3574,7 +3574,7 @@
  28243. struct ieee80211_supported_band *sband;
  28244. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  28245. - WARN_ON_ONCE(softirq_count() == 0);
  28246. + WARN_ON_ONCE_NONRT(softirq_count() == 0);
  28247. if (WARN_ON(status->band >= IEEE80211_NUM_BANDS))
  28248. goto drop;
  28249. diff -Nur linux-4.4.28.orig/net/netfilter/core.c linux-4.4.28/net/netfilter/core.c
  28250. --- linux-4.4.28.orig/net/netfilter/core.c 2016-10-28 09:53:25.000000000 +0200
  28251. +++ linux-4.4.28/net/netfilter/core.c 2016-11-01 21:26:58.840346411 +0100
  28252. @@ -22,11 +22,17 @@
  28253. #include <linux/proc_fs.h>
  28254. #include <linux/mutex.h>
  28255. #include <linux/slab.h>
  28256. +#include <linux/locallock.h>
  28257. #include <net/net_namespace.h>
  28258. #include <net/sock.h>
  28259. #include "nf_internals.h"
  28260. +#ifdef CONFIG_PREEMPT_RT_BASE
  28261. +DEFINE_LOCAL_IRQ_LOCK(xt_write_lock);
  28262. +EXPORT_PER_CPU_SYMBOL(xt_write_lock);
  28263. +#endif
  28264. +
  28265. static DEFINE_MUTEX(afinfo_mutex);
  28266. const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO] __read_mostly;
  28267. diff -Nur linux-4.4.28.orig/net/packet/af_packet.c linux-4.4.28/net/packet/af_packet.c
  28268. --- linux-4.4.28.orig/net/packet/af_packet.c 2016-10-28 09:53:25.000000000 +0200
  28269. +++ linux-4.4.28/net/packet/af_packet.c 2016-11-01 21:26:58.844346664 +0100
  28270. @@ -63,6 +63,7 @@
  28271. #include <linux/if_packet.h>
  28272. #include <linux/wireless.h>
  28273. #include <linux/kernel.h>
  28274. +#include <linux/delay.h>
  28275. #include <linux/kmod.h>
  28276. #include <linux/slab.h>
  28277. #include <linux/vmalloc.h>
  28278. @@ -695,7 +696,7 @@
  28279. if (BLOCK_NUM_PKTS(pbd)) {
  28280. while (atomic_read(&pkc->blk_fill_in_prog)) {
  28281. /* Waiting for skb_copy_bits to finish... */
  28282. - cpu_relax();
  28283. + cpu_chill();
  28284. }
  28285. }
  28286. @@ -957,7 +958,7 @@
  28287. if (!(status & TP_STATUS_BLK_TMO)) {
  28288. while (atomic_read(&pkc->blk_fill_in_prog)) {
  28289. /* Waiting for skb_copy_bits to finish... */
  28290. - cpu_relax();
  28291. + cpu_chill();
  28292. }
  28293. }
  28294. prb_close_block(pkc, pbd, po, status);
  28295. diff -Nur linux-4.4.28.orig/net/rds/ib_rdma.c linux-4.4.28/net/rds/ib_rdma.c
  28296. --- linux-4.4.28.orig/net/rds/ib_rdma.c 2016-10-28 09:53:25.000000000 +0200
  28297. +++ linux-4.4.28/net/rds/ib_rdma.c 2016-11-01 21:26:58.844346664 +0100
  28298. @@ -34,6 +34,7 @@
  28299. #include <linux/slab.h>
  28300. #include <linux/rculist.h>
  28301. #include <linux/llist.h>
  28302. +#include <linux/delay.h>
  28303. #include "rds.h"
  28304. #include "ib.h"
  28305. @@ -313,7 +314,7 @@
  28306. for_each_online_cpu(cpu) {
  28307. flag = &per_cpu(clean_list_grace, cpu);
  28308. while (test_bit(CLEAN_LIST_BUSY_BIT, flag))
  28309. - cpu_relax();
  28310. + cpu_chill();
  28311. }
  28312. }
  28313. diff -Nur linux-4.4.28.orig/net/sched/sch_generic.c linux-4.4.28/net/sched/sch_generic.c
  28314. --- linux-4.4.28.orig/net/sched/sch_generic.c 2016-10-28 09:53:25.000000000 +0200
  28315. +++ linux-4.4.28/net/sched/sch_generic.c 2016-11-01 21:26:58.844346664 +0100
  28316. @@ -893,7 +893,7 @@
  28317. /* Wait for outstanding qdisc_run calls. */
  28318. list_for_each_entry(dev, head, close_list)
  28319. while (some_qdisc_is_busy(dev))
  28320. - yield();
  28321. + msleep(1);
  28322. }
  28323. void dev_deactivate(struct net_device *dev)
  28324. diff -Nur linux-4.4.28.orig/net/sunrpc/svc_xprt.c linux-4.4.28/net/sunrpc/svc_xprt.c
  28325. --- linux-4.4.28.orig/net/sunrpc/svc_xprt.c 2016-10-28 09:53:25.000000000 +0200
  28326. +++ linux-4.4.28/net/sunrpc/svc_xprt.c 2016-11-01 21:26:58.844346664 +0100
  28327. @@ -340,7 +340,7 @@
  28328. goto out;
  28329. }
  28330. - cpu = get_cpu();
  28331. + cpu = get_cpu_light();
  28332. pool = svc_pool_for_cpu(xprt->xpt_server, cpu);
  28333. atomic_long_inc(&pool->sp_stats.packets);
  28334. @@ -376,7 +376,7 @@
  28335. atomic_long_inc(&pool->sp_stats.threads_woken);
  28336. wake_up_process(rqstp->rq_task);
  28337. - put_cpu();
  28338. + put_cpu_light();
  28339. goto out;
  28340. }
  28341. rcu_read_unlock();
  28342. @@ -397,7 +397,7 @@
  28343. goto redo_search;
  28344. }
  28345. rqstp = NULL;
  28346. - put_cpu();
  28347. + put_cpu_light();
  28348. out:
  28349. trace_svc_xprt_do_enqueue(xprt, rqstp);
  28350. }
  28351. diff -Nur linux-4.4.28.orig/scripts/mkcompile_h linux-4.4.28/scripts/mkcompile_h
  28352. --- linux-4.4.28.orig/scripts/mkcompile_h 2016-10-28 09:53:25.000000000 +0200
  28353. +++ linux-4.4.28/scripts/mkcompile_h 2016-11-01 21:26:58.844346664 +0100
  28354. @@ -4,7 +4,8 @@
  28355. ARCH=$2
  28356. SMP=$3
  28357. PREEMPT=$4
  28358. -CC=$5
  28359. +RT=$5
  28360. +CC=$6
  28361. vecho() { [ "${quiet}" = "silent_" ] || echo "$@" ; }
  28362. @@ -57,6 +58,7 @@
  28363. CONFIG_FLAGS=""
  28364. if [ -n "$SMP" ] ; then CONFIG_FLAGS="SMP"; fi
  28365. if [ -n "$PREEMPT" ] ; then CONFIG_FLAGS="$CONFIG_FLAGS PREEMPT"; fi
  28366. +if [ -n "$RT" ] ; then CONFIG_FLAGS="$CONFIG_FLAGS RT"; fi
  28367. UTS_VERSION="$UTS_VERSION $CONFIG_FLAGS $TIMESTAMP"
  28368. # Truncate to maximum length
  28369. diff -Nur linux-4.4.28.orig/sound/core/pcm_native.c linux-4.4.28/sound/core/pcm_native.c
  28370. --- linux-4.4.28.orig/sound/core/pcm_native.c 2016-10-28 09:53:25.000000000 +0200
  28371. +++ linux-4.4.28/sound/core/pcm_native.c 2016-11-01 21:26:58.844346664 +0100
  28372. @@ -135,7 +135,7 @@
  28373. void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
  28374. {
  28375. if (!substream->pcm->nonatomic)
  28376. - local_irq_disable();
  28377. + local_irq_disable_nort();
  28378. snd_pcm_stream_lock(substream);
  28379. }
  28380. EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
  28381. @@ -150,7 +150,7 @@
  28382. {
  28383. snd_pcm_stream_unlock(substream);
  28384. if (!substream->pcm->nonatomic)
  28385. - local_irq_enable();
  28386. + local_irq_enable_nort();
  28387. }
  28388. EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
  28389. @@ -158,7 +158,7 @@
  28390. {
  28391. unsigned long flags = 0;
  28392. if (!substream->pcm->nonatomic)
  28393. - local_irq_save(flags);
  28394. + local_irq_save_nort(flags);
  28395. snd_pcm_stream_lock(substream);
  28396. return flags;
  28397. }
  28398. @@ -176,7 +176,7 @@
  28399. {
  28400. snd_pcm_stream_unlock(substream);
  28401. if (!substream->pcm->nonatomic)
  28402. - local_irq_restore(flags);
  28403. + local_irq_restore_nort(flags);
  28404. }
  28405. EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
  28406. diff -Nur linux-4.4.28.orig/virt/kvm/async_pf.c linux-4.4.28/virt/kvm/async_pf.c
  28407. --- linux-4.4.28.orig/virt/kvm/async_pf.c 2016-10-28 09:53:25.000000000 +0200
  28408. +++ linux-4.4.28/virt/kvm/async_pf.c 2016-11-01 21:26:58.844346664 +0100
  28409. @@ -98,8 +98,8 @@
  28410. * This memory barrier pairs with prepare_to_wait's set_current_state()
  28411. */
  28412. smp_mb();
  28413. - if (waitqueue_active(&vcpu->wq))
  28414. - wake_up_interruptible(&vcpu->wq);
  28415. + if (swait_active(&vcpu->wq))
  28416. + swake_up(&vcpu->wq);
  28417. mmput(mm);
  28418. kvm_put_kvm(vcpu->kvm);
  28419. diff -Nur linux-4.4.28.orig/virt/kvm/kvm_main.c linux-4.4.28/virt/kvm/kvm_main.c
  28420. --- linux-4.4.28.orig/virt/kvm/kvm_main.c 2016-10-28 09:53:25.000000000 +0200
  28421. +++ linux-4.4.28/virt/kvm/kvm_main.c 2016-11-01 21:26:58.848346921 +0100
  28422. @@ -228,8 +228,7 @@
  28423. vcpu->kvm = kvm;
  28424. vcpu->vcpu_id = id;
  28425. vcpu->pid = NULL;
  28426. - vcpu->halt_poll_ns = 0;
  28427. - init_waitqueue_head(&vcpu->wq);
  28428. + init_swait_queue_head(&vcpu->wq);
  28429. kvm_async_pf_vcpu_init(vcpu);
  28430. vcpu->pre_pcpu = -1;
  28431. @@ -2005,7 +2004,7 @@
  28432. void kvm_vcpu_block(struct kvm_vcpu *vcpu)
  28433. {
  28434. ktime_t start, cur;
  28435. - DEFINE_WAIT(wait);
  28436. + DECLARE_SWAITQUEUE(wait);
  28437. bool waited = false;
  28438. u64 block_ns;
  28439. @@ -2030,7 +2029,7 @@
  28440. kvm_arch_vcpu_blocking(vcpu);
  28441. for (;;) {
  28442. - prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
  28443. + prepare_to_swait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
  28444. if (kvm_vcpu_check_block(vcpu) < 0)
  28445. break;
  28446. @@ -2039,7 +2038,7 @@
  28447. schedule();
  28448. }
  28449. - finish_wait(&vcpu->wq, &wait);
  28450. + finish_swait(&vcpu->wq, &wait);
  28451. cur = ktime_get();
  28452. kvm_arch_vcpu_unblocking(vcpu);
  28453. @@ -2071,11 +2070,11 @@
  28454. {
  28455. int me;
  28456. int cpu = vcpu->cpu;
  28457. - wait_queue_head_t *wqp;
  28458. + struct swait_queue_head *wqp;
  28459. wqp = kvm_arch_vcpu_wq(vcpu);
  28460. - if (waitqueue_active(wqp)) {
  28461. - wake_up_interruptible(wqp);
  28462. + if (swait_active(wqp)) {
  28463. + swake_up(wqp);
  28464. ++vcpu->stat.halt_wakeup;
  28465. }
  28466. @@ -2176,7 +2175,7 @@
  28467. continue;
  28468. if (vcpu == me)
  28469. continue;
  28470. - if (waitqueue_active(&vcpu->wq) && !kvm_arch_vcpu_runnable(vcpu))
  28471. + if (swait_active(&vcpu->wq) && !kvm_arch_vcpu_runnable(vcpu))
  28472. continue;
  28473. if (!kvm_vcpu_eligible_for_directed_yield(vcpu))
  28474. continue;