patch-realtime 946 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228
  1. diff --git a/Documentation/hwlat_detector.txt b/Documentation/hwlat_detector.txt
  2. new file mode 100644
  3. index 000000000000..cb61516483d3
  4. --- /dev/null
  5. +++ b/Documentation/hwlat_detector.txt
  6. @@ -0,0 +1,64 @@
  7. +Introduction:
  8. +-------------
  9. +
  10. +The module hwlat_detector is a special purpose kernel module that is used to
  11. +detect large system latencies induced by the behavior of certain underlying
  12. +hardware or firmware, independent of Linux itself. The code was developed
  13. +originally to detect SMIs (System Management Interrupts) on x86 systems,
  14. +however there is nothing x86 specific about this patchset. It was
  15. +originally written for use by the "RT" patch since the Real Time
  16. +kernel is highly latency sensitive.
  17. +
  18. +SMIs are usually not serviced by the Linux kernel, which typically does not
  19. +even know that they are occuring. SMIs are instead are set up by BIOS code
  20. +and are serviced by BIOS code, usually for "critical" events such as
  21. +management of thermal sensors and fans. Sometimes though, SMIs are used for
  22. +other tasks and those tasks can spend an inordinate amount of time in the
  23. +handler (sometimes measured in milliseconds). Obviously this is a problem if
  24. +you are trying to keep event service latencies down in the microsecond range.
  25. +
  26. +The hardware latency detector works by hogging all of the cpus for configurable
  27. +amounts of time (by calling stop_machine()), polling the CPU Time Stamp Counter
  28. +for some period, then looking for gaps in the TSC data. Any gap indicates a
  29. +time when the polling was interrupted and since the machine is stopped and
  30. +interrupts turned off the only thing that could do that would be an SMI.
  31. +
  32. +Note that the SMI detector should *NEVER* be used in a production environment.
  33. +It is intended to be run manually to determine if the hardware platform has a
  34. +problem with long system firmware service routines.
  35. +
  36. +Usage:
  37. +------
  38. +
  39. +Loading the module hwlat_detector passing the parameter "enabled=1" (or by
  40. +setting the "enable" entry in "hwlat_detector" debugfs toggled on) is the only
  41. +step required to start the hwlat_detector. It is possible to redefine the
  42. +threshold in microseconds (us) above which latency spikes will be taken
  43. +into account (parameter "threshold=").
  44. +
  45. +Example:
  46. +
  47. + # modprobe hwlat_detector enabled=1 threshold=100
  48. +
  49. +After the module is loaded, it creates a directory named "hwlat_detector" under
  50. +the debugfs mountpoint, "/debug/hwlat_detector" for this text. It is necessary
  51. +to have debugfs mounted, which might be on /sys/debug on your system.
  52. +
  53. +The /debug/hwlat_detector interface contains the following files:
  54. +
  55. +count - number of latency spikes observed since last reset
  56. +enable - a global enable/disable toggle (0/1), resets count
  57. +max - maximum hardware latency actually observed (usecs)
  58. +sample - a pipe from which to read current raw sample data
  59. + in the format <timestamp> <latency observed usecs>
  60. + (can be opened O_NONBLOCK for a single sample)
  61. +threshold - minimum latency value to be considered (usecs)
  62. +width - time period to sample with CPUs held (usecs)
  63. + must be less than the total window size (enforced)
  64. +window - total period of sampling, width being inside (usecs)
  65. +
  66. +By default we will set width to 500,000 and window to 1,000,000, meaning that
  67. +we will sample every 1,000,000 usecs (1s) for 500,000 usecs (0.5s). If we
  68. +observe any latencies that exceed the threshold (initially 100 usecs),
  69. +then we write to a global sample ring buffer of 8K samples, which is
  70. +consumed by reading from the "sample" (pipe) debugfs file interface.
  71. diff --git a/Documentation/kernel-parameters.txt b/Documentation/kernel-parameters.txt
  72. index ca64ca566099..19bef799c902 100644
  73. --- a/Documentation/kernel-parameters.txt
  74. +++ b/Documentation/kernel-parameters.txt
  75. @@ -1640,6 +1640,15 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
  76. ip= [IP_PNP]
  77. See Documentation/filesystems/nfs/nfsroot.txt.
  78. + irqaffinity= [SMP] Set the default irq affinity mask
  79. + Format:
  80. + <cpu number>,...,<cpu number>
  81. + or
  82. + <cpu number>-<cpu number>
  83. + (must be a positive range in ascending order)
  84. + or a mixture
  85. + <cpu number>,...,<cpu number>-<cpu number>
  86. +
  87. irqfixup [HW]
  88. When an interrupt is not handled search all handlers
  89. for it. Intended to get systems with badly broken
  90. diff --git a/Documentation/sysrq.txt b/Documentation/sysrq.txt
  91. index 13f5619b2203..f64d075ba647 100644
  92. --- a/Documentation/sysrq.txt
  93. +++ b/Documentation/sysrq.txt
  94. @@ -59,10 +59,17 @@ On PowerPC - Press 'ALT - Print Screen (or F13) - <command key>,
  95. On other - If you know of the key combos for other architectures, please
  96. let me know so I can add them to this section.
  97. -On all - write a character to /proc/sysrq-trigger. e.g.:
  98. -
  99. +On all - write a character to /proc/sysrq-trigger, e.g.:
  100. echo t > /proc/sysrq-trigger
  101. +On all - Enable network SysRq by writing a cookie to icmp_echo_sysrq, e.g.
  102. + echo 0x01020304 >/proc/sys/net/ipv4/icmp_echo_sysrq
  103. + Send an ICMP echo request with this pattern plus the particular
  104. + SysRq command key. Example:
  105. + # ping -c1 -s57 -p0102030468
  106. + will trigger the SysRq-H (help) command.
  107. +
  108. +
  109. * What are the 'command' keys?
  110. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  111. 'b' - Will immediately reboot the system without syncing or unmounting
  112. diff --git a/Documentation/trace/histograms.txt b/Documentation/trace/histograms.txt
  113. new file mode 100644
  114. index 000000000000..6f2aeabf7faa
  115. --- /dev/null
  116. +++ b/Documentation/trace/histograms.txt
  117. @@ -0,0 +1,186 @@
  118. + Using the Linux Kernel Latency Histograms
  119. +
  120. +
  121. +This document gives a short explanation how to enable, configure and use
  122. +latency histograms. Latency histograms are primarily relevant in the
  123. +context of real-time enabled kernels (CONFIG_PREEMPT/CONFIG_PREEMPT_RT)
  124. +and are used in the quality management of the Linux real-time
  125. +capabilities.
  126. +
  127. +
  128. +* Purpose of latency histograms
  129. +
  130. +A latency histogram continuously accumulates the frequencies of latency
  131. +data. There are two types of histograms
  132. +- potential sources of latencies
  133. +- effective latencies
  134. +
  135. +
  136. +* Potential sources of latencies
  137. +
  138. +Potential sources of latencies are code segments where interrupts,
  139. +preemption or both are disabled (aka critical sections). To create
  140. +histograms of potential sources of latency, the kernel stores the time
  141. +stamp at the start of a critical section, determines the time elapsed
  142. +when the end of the section is reached, and increments the frequency
  143. +counter of that latency value - irrespective of whether any concurrently
  144. +running process is affected by latency or not.
  145. +- Configuration items (in the Kernel hacking/Tracers submenu)
  146. + CONFIG_INTERRUPT_OFF_LATENCY
  147. + CONFIG_PREEMPT_OFF_LATENCY
  148. +
  149. +
  150. +* Effective latencies
  151. +
  152. +Effective latencies are actually occuring during wakeup of a process. To
  153. +determine effective latencies, the kernel stores the time stamp when a
  154. +process is scheduled to be woken up, and determines the duration of the
  155. +wakeup time shortly before control is passed over to this process. Note
  156. +that the apparent latency in user space may be somewhat longer, since the
  157. +process may be interrupted after control is passed over to it but before
  158. +the execution in user space takes place. Simply measuring the interval
  159. +between enqueuing and wakeup may also not appropriate in cases when a
  160. +process is scheduled as a result of a timer expiration. The timer may have
  161. +missed its deadline, e.g. due to disabled interrupts, but this latency
  162. +would not be registered. Therefore, the offsets of missed timers are
  163. +recorded in a separate histogram. If both wakeup latency and missed timer
  164. +offsets are configured and enabled, a third histogram may be enabled that
  165. +records the overall latency as a sum of the timer latency, if any, and the
  166. +wakeup latency. This histogram is called "timerandwakeup".
  167. +- Configuration items (in the Kernel hacking/Tracers submenu)
  168. + CONFIG_WAKEUP_LATENCY
  169. + CONFIG_MISSED_TIMER_OFSETS
  170. +
  171. +
  172. +* Usage
  173. +
  174. +The interface to the administration of the latency histograms is located
  175. +in the debugfs file system. To mount it, either enter
  176. +
  177. +mount -t sysfs nodev /sys
  178. +mount -t debugfs nodev /sys/kernel/debug
  179. +
  180. +from shell command line level, or add
  181. +
  182. +nodev /sys sysfs defaults 0 0
  183. +nodev /sys/kernel/debug debugfs defaults 0 0
  184. +
  185. +to the file /etc/fstab. All latency histogram related files are then
  186. +available in the directory /sys/kernel/debug/tracing/latency_hist. A
  187. +particular histogram type is enabled by writing non-zero to the related
  188. +variable in the /sys/kernel/debug/tracing/latency_hist/enable directory.
  189. +Select "preemptirqsoff" for the histograms of potential sources of
  190. +latencies and "wakeup" for histograms of effective latencies etc. The
  191. +histogram data - one per CPU - are available in the files
  192. +
  193. +/sys/kernel/debug/tracing/latency_hist/preemptoff/CPUx
  194. +/sys/kernel/debug/tracing/latency_hist/irqsoff/CPUx
  195. +/sys/kernel/debug/tracing/latency_hist/preemptirqsoff/CPUx
  196. +/sys/kernel/debug/tracing/latency_hist/wakeup/CPUx
  197. +/sys/kernel/debug/tracing/latency_hist/wakeup/sharedprio/CPUx
  198. +/sys/kernel/debug/tracing/latency_hist/missed_timer_offsets/CPUx
  199. +/sys/kernel/debug/tracing/latency_hist/timerandwakeup/CPUx
  200. +
  201. +The histograms are reset by writing non-zero to the file "reset" in a
  202. +particular latency directory. To reset all latency data, use
  203. +
  204. +#!/bin/sh
  205. +
  206. +TRACINGDIR=/sys/kernel/debug/tracing
  207. +HISTDIR=$TRACINGDIR/latency_hist
  208. +
  209. +if test -d $HISTDIR
  210. +then
  211. + cd $HISTDIR
  212. + for i in `find . | grep /reset$`
  213. + do
  214. + echo 1 >$i
  215. + done
  216. +fi
  217. +
  218. +
  219. +* Data format
  220. +
  221. +Latency data are stored with a resolution of one microsecond. The
  222. +maximum latency is 10,240 microseconds. The data are only valid, if the
  223. +overflow register is empty. Every output line contains the latency in
  224. +microseconds in the first row and the number of samples in the second
  225. +row. To display only lines with a positive latency count, use, for
  226. +example,
  227. +
  228. +grep -v " 0$" /sys/kernel/debug/tracing/latency_hist/preemptoff/CPU0
  229. +
  230. +#Minimum latency: 0 microseconds.
  231. +#Average latency: 0 microseconds.
  232. +#Maximum latency: 25 microseconds.
  233. +#Total samples: 3104770694
  234. +#There are 0 samples greater or equal than 10240 microseconds
  235. +#usecs samples
  236. + 0 2984486876
  237. + 1 49843506
  238. + 2 58219047
  239. + 3 5348126
  240. + 4 2187960
  241. + 5 3388262
  242. + 6 959289
  243. + 7 208294
  244. + 8 40420
  245. + 9 4485
  246. + 10 14918
  247. + 11 18340
  248. + 12 25052
  249. + 13 19455
  250. + 14 5602
  251. + 15 969
  252. + 16 47
  253. + 17 18
  254. + 18 14
  255. + 19 1
  256. + 20 3
  257. + 21 2
  258. + 22 5
  259. + 23 2
  260. + 25 1
  261. +
  262. +
  263. +* Wakeup latency of a selected process
  264. +
  265. +To only collect wakeup latency data of a particular process, write the
  266. +PID of the requested process to
  267. +
  268. +/sys/kernel/debug/tracing/latency_hist/wakeup/pid
  269. +
  270. +PIDs are not considered, if this variable is set to 0.
  271. +
  272. +
  273. +* Details of the process with the highest wakeup latency so far
  274. +
  275. +Selected data of the process that suffered from the highest wakeup
  276. +latency that occurred in a particular CPU are available in the file
  277. +
  278. +/sys/kernel/debug/tracing/latency_hist/wakeup/max_latency-CPUx.
  279. +
  280. +In addition, other relevant system data at the time when the
  281. +latency occurred are given.
  282. +
  283. +The format of the data is (all in one line):
  284. +<PID> <Priority> <Latency> (<Timeroffset>) <Command> \
  285. +<- <PID> <Priority> <Command> <Timestamp>
  286. +
  287. +The value of <Timeroffset> is only relevant in the combined timer
  288. +and wakeup latency recording. In the wakeup recording, it is
  289. +always 0, in the missed_timer_offsets recording, it is the same
  290. +as <Latency>.
  291. +
  292. +When retrospectively searching for the origin of a latency and
  293. +tracing was not enabled, it may be helpful to know the name and
  294. +some basic data of the task that (finally) was switching to the
  295. +late real-tlme task. In addition to the victim's data, also the
  296. +data of the possible culprit are therefore displayed after the
  297. +"<-" symbol.
  298. +
  299. +Finally, the timestamp of the time when the latency occurred
  300. +in <seconds>.<microseconds> after the most recent system boot
  301. +is provided.
  302. +
  303. +These data are also reset when the wakeup histogram is reset.
  304. diff --git a/Makefile b/Makefile
  305. index a5ecb29c6ed3..5521b0c3abf2 100644
  306. --- a/Makefile
  307. +++ b/Makefile
  308. @@ -785,6 +785,9 @@ KBUILD_CFLAGS += $(call cc-option,-Werror=strict-prototypes)
  309. # Prohibit date/time macros, which would make the build non-deterministic
  310. KBUILD_CFLAGS += $(call cc-option,-Werror=date-time)
  311. +# enforce correct pointer usage
  312. +KBUILD_CFLAGS += $(call cc-option,-Werror=incompatible-pointer-types)
  313. +
  314. # use the deterministic mode of AR if available
  315. KBUILD_ARFLAGS := $(call ar-option,D)
  316. diff --git a/arch/Kconfig b/arch/Kconfig
  317. index 4e949e58b192..3b26d76933fb 100644
  318. --- a/arch/Kconfig
  319. +++ b/arch/Kconfig
  320. @@ -9,6 +9,7 @@ config OPROFILE
  321. tristate "OProfile system profiling"
  322. depends on PROFILING
  323. depends on HAVE_OPROFILE
  324. + depends on !PREEMPT_RT_FULL
  325. select RING_BUFFER
  326. select RING_BUFFER_ALLOW_SWAP
  327. help
  328. @@ -52,6 +53,7 @@ config KPROBES
  329. config JUMP_LABEL
  330. bool "Optimize very unlikely/likely branches"
  331. depends on HAVE_ARCH_JUMP_LABEL
  332. + depends on (!INTERRUPT_OFF_HIST && !PREEMPT_OFF_HIST && !WAKEUP_LATENCY_HIST && !MISSED_TIMER_OFFSETS_HIST)
  333. help
  334. This option enables a transparent branch optimization that
  335. makes certain almost-always-true or almost-always-false branch
  336. diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig
  337. index 34e1569a11ee..79c4603e9453 100644
  338. --- a/arch/arm/Kconfig
  339. +++ b/arch/arm/Kconfig
  340. @@ -33,7 +33,7 @@ config ARM
  341. select HARDIRQS_SW_RESEND
  342. select HAVE_ARCH_AUDITSYSCALL if (AEABI && !OABI_COMPAT)
  343. select HAVE_ARCH_BITREVERSE if (CPU_32v7M || CPU_32v7) && !CPU_32v6
  344. - select HAVE_ARCH_JUMP_LABEL if !XIP_KERNEL && !CPU_ENDIAN_BE32
  345. + select HAVE_ARCH_JUMP_LABEL if !XIP_KERNEL && !CPU_ENDIAN_BE32 && !PREEMPT_RT_BASE
  346. select HAVE_ARCH_KGDB if !CPU_ENDIAN_BE32
  347. select HAVE_ARCH_SECCOMP_FILTER if (AEABI && !OABI_COMPAT)
  348. select HAVE_ARCH_TRACEHOOK
  349. @@ -68,6 +68,7 @@ config ARM
  350. select HAVE_PERF_EVENTS
  351. select HAVE_PERF_REGS
  352. select HAVE_PERF_USER_STACK_DUMP
  353. + select HAVE_PREEMPT_LAZY
  354. select HAVE_RCU_TABLE_FREE if (SMP && ARM_LPAE)
  355. select HAVE_REGS_AND_STACK_ACCESS_API
  356. select HAVE_SYSCALL_TRACEPOINTS
  357. diff --git a/arch/arm/include/asm/switch_to.h b/arch/arm/include/asm/switch_to.h
  358. index 12ebfcc1d539..c962084605bc 100644
  359. --- a/arch/arm/include/asm/switch_to.h
  360. +++ b/arch/arm/include/asm/switch_to.h
  361. @@ -3,6 +3,13 @@
  362. #include <linux/thread_info.h>
  363. +#if defined CONFIG_PREEMPT_RT_FULL && defined CONFIG_HIGHMEM
  364. +void switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p);
  365. +#else
  366. +static inline void
  367. +switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p) { }
  368. +#endif
  369. +
  370. /*
  371. * For v7 SMP cores running a preemptible kernel we may be pre-empted
  372. * during a TLB maintenance operation, so execute an inner-shareable dsb
  373. @@ -25,6 +32,7 @@ extern struct task_struct *__switch_to(struct task_struct *, struct thread_info
  374. #define switch_to(prev,next,last) \
  375. do { \
  376. __complete_pending_tlbi(); \
  377. + switch_kmaps(prev, next); \
  378. last = __switch_to(prev,task_thread_info(prev), task_thread_info(next)); \
  379. } while (0)
  380. diff --git a/arch/arm/include/asm/thread_info.h b/arch/arm/include/asm/thread_info.h
  381. index 776757d1604a..1f36a4eccc72 100644
  382. --- a/arch/arm/include/asm/thread_info.h
  383. +++ b/arch/arm/include/asm/thread_info.h
  384. @@ -49,6 +49,7 @@ struct cpu_context_save {
  385. struct thread_info {
  386. unsigned long flags; /* low level flags */
  387. int preempt_count; /* 0 => preemptable, <0 => bug */
  388. + int preempt_lazy_count; /* 0 => preemptable, <0 => bug */
  389. mm_segment_t addr_limit; /* address limit */
  390. struct task_struct *task; /* main task structure */
  391. __u32 cpu; /* cpu */
  392. @@ -142,7 +143,8 @@ extern int vfp_restore_user_hwstate(struct user_vfp __user *,
  393. #define TIF_SYSCALL_TRACE 4 /* syscall trace active */
  394. #define TIF_SYSCALL_AUDIT 5 /* syscall auditing active */
  395. #define TIF_SYSCALL_TRACEPOINT 6 /* syscall tracepoint instrumentation */
  396. -#define TIF_SECCOMP 7 /* seccomp syscall filtering active */
  397. +#define TIF_SECCOMP 8 /* seccomp syscall filtering active */
  398. +#define TIF_NEED_RESCHED_LAZY 7
  399. #define TIF_NOHZ 12 /* in adaptive nohz mode */
  400. #define TIF_USING_IWMMXT 17
  401. @@ -152,6 +154,7 @@ extern int vfp_restore_user_hwstate(struct user_vfp __user *,
  402. #define _TIF_SIGPENDING (1 << TIF_SIGPENDING)
  403. #define _TIF_NEED_RESCHED (1 << TIF_NEED_RESCHED)
  404. #define _TIF_NOTIFY_RESUME (1 << TIF_NOTIFY_RESUME)
  405. +#define _TIF_NEED_RESCHED_LAZY (1 << TIF_NEED_RESCHED_LAZY)
  406. #define _TIF_UPROBE (1 << TIF_UPROBE)
  407. #define _TIF_SYSCALL_TRACE (1 << TIF_SYSCALL_TRACE)
  408. #define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
  409. @@ -167,7 +170,8 @@ extern int vfp_restore_user_hwstate(struct user_vfp __user *,
  410. * Change these and you break ASM code in entry-common.S
  411. */
  412. #define _TIF_WORK_MASK (_TIF_NEED_RESCHED | _TIF_SIGPENDING | \
  413. - _TIF_NOTIFY_RESUME | _TIF_UPROBE)
  414. + _TIF_NOTIFY_RESUME | _TIF_UPROBE | \
  415. + _TIF_NEED_RESCHED_LAZY)
  416. #endif /* __KERNEL__ */
  417. #endif /* __ASM_ARM_THREAD_INFO_H */
  418. diff --git a/arch/arm/kernel/asm-offsets.c b/arch/arm/kernel/asm-offsets.c
  419. index 871b8267d211..4dbe70de7318 100644
  420. --- a/arch/arm/kernel/asm-offsets.c
  421. +++ b/arch/arm/kernel/asm-offsets.c
  422. @@ -65,6 +65,7 @@ int main(void)
  423. BLANK();
  424. DEFINE(TI_FLAGS, offsetof(struct thread_info, flags));
  425. DEFINE(TI_PREEMPT, offsetof(struct thread_info, preempt_count));
  426. + DEFINE(TI_PREEMPT_LAZY, offsetof(struct thread_info, preempt_lazy_count));
  427. DEFINE(TI_ADDR_LIMIT, offsetof(struct thread_info, addr_limit));
  428. DEFINE(TI_TASK, offsetof(struct thread_info, task));
  429. DEFINE(TI_CPU, offsetof(struct thread_info, cpu));
  430. diff --git a/arch/arm/kernel/entry-armv.S b/arch/arm/kernel/entry-armv.S
  431. index 3ce377f7251f..d044cea59f54 100644
  432. --- a/arch/arm/kernel/entry-armv.S
  433. +++ b/arch/arm/kernel/entry-armv.S
  434. @@ -215,11 +215,18 @@ __irq_svc:
  435. #ifdef CONFIG_PREEMPT
  436. get_thread_info tsk
  437. ldr r8, [tsk, #TI_PREEMPT] @ get preempt count
  438. - ldr r0, [tsk, #TI_FLAGS] @ get flags
  439. teq r8, #0 @ if preempt count != 0
  440. + bne 1f @ return from exeption
  441. + ldr r0, [tsk, #TI_FLAGS] @ get flags
  442. + tst r0, #_TIF_NEED_RESCHED @ if NEED_RESCHED is set
  443. + blne svc_preempt @ preempt!
  444. +
  445. + ldr r8, [tsk, #TI_PREEMPT_LAZY] @ get preempt lazy count
  446. + teq r8, #0 @ if preempt lazy count != 0
  447. movne r0, #0 @ force flags to 0
  448. - tst r0, #_TIF_NEED_RESCHED
  449. + tst r0, #_TIF_NEED_RESCHED_LAZY
  450. blne svc_preempt
  451. +1:
  452. #endif
  453. svc_exit r5, irq = 1 @ return from exception
  454. @@ -234,8 +241,14 @@ svc_preempt:
  455. 1: bl preempt_schedule_irq @ irq en/disable is done inside
  456. ldr r0, [tsk, #TI_FLAGS] @ get new tasks TI_FLAGS
  457. tst r0, #_TIF_NEED_RESCHED
  458. + bne 1b
  459. + tst r0, #_TIF_NEED_RESCHED_LAZY
  460. reteq r8 @ go again
  461. - b 1b
  462. + ldr r0, [tsk, #TI_PREEMPT_LAZY] @ get preempt lazy count
  463. + teq r0, #0 @ if preempt lazy count != 0
  464. + beq 1b
  465. + ret r8 @ go again
  466. +
  467. #endif
  468. __und_fault:
  469. diff --git a/arch/arm/kernel/entry-common.S b/arch/arm/kernel/entry-common.S
  470. index 30a7228eaceb..c3bd6cbfce4b 100644
  471. --- a/arch/arm/kernel/entry-common.S
  472. +++ b/arch/arm/kernel/entry-common.S
  473. @@ -36,7 +36,9 @@ ret_fast_syscall:
  474. UNWIND(.cantunwind )
  475. disable_irq_notrace @ disable interrupts
  476. ldr r1, [tsk, #TI_FLAGS] @ re-check for syscall tracing
  477. - tst r1, #_TIF_SYSCALL_WORK | _TIF_WORK_MASK
  478. + tst r1, #((_TIF_SYSCALL_WORK | _TIF_WORK_MASK) & ~_TIF_SECCOMP)
  479. + bne fast_work_pending
  480. + tst r1, #_TIF_SECCOMP
  481. bne fast_work_pending
  482. /* perform architecture specific actions before user return */
  483. @@ -62,8 +64,11 @@ ret_fast_syscall:
  484. str r0, [sp, #S_R0 + S_OFF]! @ save returned r0
  485. disable_irq_notrace @ disable interrupts
  486. ldr r1, [tsk, #TI_FLAGS] @ re-check for syscall tracing
  487. - tst r1, #_TIF_SYSCALL_WORK | _TIF_WORK_MASK
  488. + tst r1, #((_TIF_SYSCALL_WORK | _TIF_WORK_MASK) & ~_TIF_SECCOMP)
  489. + bne do_slower_path
  490. + tst r1, #_TIF_SECCOMP
  491. beq no_work_pending
  492. +do_slower_path:
  493. UNWIND(.fnend )
  494. ENDPROC(ret_fast_syscall)
  495. diff --git a/arch/arm/kernel/patch.c b/arch/arm/kernel/patch.c
  496. index 69bda1a5707e..1f665acaa6a9 100644
  497. --- a/arch/arm/kernel/patch.c
  498. +++ b/arch/arm/kernel/patch.c
  499. @@ -15,7 +15,7 @@ struct patch {
  500. unsigned int insn;
  501. };
  502. -static DEFINE_SPINLOCK(patch_lock);
  503. +static DEFINE_RAW_SPINLOCK(patch_lock);
  504. static void __kprobes *patch_map(void *addr, int fixmap, unsigned long *flags)
  505. __acquires(&patch_lock)
  506. @@ -32,7 +32,7 @@ static void __kprobes *patch_map(void *addr, int fixmap, unsigned long *flags)
  507. return addr;
  508. if (flags)
  509. - spin_lock_irqsave(&patch_lock, *flags);
  510. + raw_spin_lock_irqsave(&patch_lock, *flags);
  511. else
  512. __acquire(&patch_lock);
  513. @@ -47,7 +47,7 @@ static void __kprobes patch_unmap(int fixmap, unsigned long *flags)
  514. clear_fixmap(fixmap);
  515. if (flags)
  516. - spin_unlock_irqrestore(&patch_lock, *flags);
  517. + raw_spin_unlock_irqrestore(&patch_lock, *flags);
  518. else
  519. __release(&patch_lock);
  520. }
  521. diff --git a/arch/arm/kernel/process.c b/arch/arm/kernel/process.c
  522. index 4adfb46e3ee9..15f1d94b47c5 100644
  523. --- a/arch/arm/kernel/process.c
  524. +++ b/arch/arm/kernel/process.c
  525. @@ -319,6 +319,30 @@ unsigned long arch_randomize_brk(struct mm_struct *mm)
  526. }
  527. #ifdef CONFIG_MMU
  528. +/*
  529. + * CONFIG_SPLIT_PTLOCK_CPUS results in a page->ptl lock. If the lock is not
  530. + * initialized by pgtable_page_ctor() then a coredump of the vector page will
  531. + * fail.
  532. + */
  533. +static int __init vectors_user_mapping_init_page(void)
  534. +{
  535. + struct page *page;
  536. + unsigned long addr = 0xffff0000;
  537. + pgd_t *pgd;
  538. + pud_t *pud;
  539. + pmd_t *pmd;
  540. +
  541. + pgd = pgd_offset_k(addr);
  542. + pud = pud_offset(pgd, addr);
  543. + pmd = pmd_offset(pud, addr);
  544. + page = pmd_page(*(pmd));
  545. +
  546. + pgtable_page_ctor(page);
  547. +
  548. + return 0;
  549. +}
  550. +late_initcall(vectors_user_mapping_init_page);
  551. +
  552. #ifdef CONFIG_KUSER_HELPERS
  553. /*
  554. * The vectors page is always readable from user space for the
  555. diff --git a/arch/arm/kernel/signal.c b/arch/arm/kernel/signal.c
  556. index 7b8f2141427b..96541e00b74a 100644
  557. --- a/arch/arm/kernel/signal.c
  558. +++ b/arch/arm/kernel/signal.c
  559. @@ -572,7 +572,8 @@ do_work_pending(struct pt_regs *regs, unsigned int thread_flags, int syscall)
  560. */
  561. trace_hardirqs_off();
  562. do {
  563. - if (likely(thread_flags & _TIF_NEED_RESCHED)) {
  564. + if (likely(thread_flags & (_TIF_NEED_RESCHED |
  565. + _TIF_NEED_RESCHED_LAZY))) {
  566. schedule();
  567. } else {
  568. if (unlikely(!user_mode(regs)))
  569. diff --git a/arch/arm/kernel/smp.c b/arch/arm/kernel/smp.c
  570. index b26361355dae..e5754e3b03c4 100644
  571. --- a/arch/arm/kernel/smp.c
  572. +++ b/arch/arm/kernel/smp.c
  573. @@ -230,8 +230,6 @@ int __cpu_disable(void)
  574. flush_cache_louis();
  575. local_flush_tlb_all();
  576. - clear_tasks_mm_cpumask(cpu);
  577. -
  578. return 0;
  579. }
  580. @@ -247,6 +245,9 @@ void __cpu_die(unsigned int cpu)
  581. pr_err("CPU%u: cpu didn't die\n", cpu);
  582. return;
  583. }
  584. +
  585. + clear_tasks_mm_cpumask(cpu);
  586. +
  587. pr_notice("CPU%u: shutdown\n", cpu);
  588. /*
  589. diff --git a/arch/arm/kernel/unwind.c b/arch/arm/kernel/unwind.c
  590. index 0bee233fef9a..314cfb232a63 100644
  591. --- a/arch/arm/kernel/unwind.c
  592. +++ b/arch/arm/kernel/unwind.c
  593. @@ -93,7 +93,7 @@ extern const struct unwind_idx __start_unwind_idx[];
  594. static const struct unwind_idx *__origin_unwind_idx;
  595. extern const struct unwind_idx __stop_unwind_idx[];
  596. -static DEFINE_SPINLOCK(unwind_lock);
  597. +static DEFINE_RAW_SPINLOCK(unwind_lock);
  598. static LIST_HEAD(unwind_tables);
  599. /* Convert a prel31 symbol to an absolute address */
  600. @@ -201,7 +201,7 @@ static const struct unwind_idx *unwind_find_idx(unsigned long addr)
  601. /* module unwind tables */
  602. struct unwind_table *table;
  603. - spin_lock_irqsave(&unwind_lock, flags);
  604. + raw_spin_lock_irqsave(&unwind_lock, flags);
  605. list_for_each_entry(table, &unwind_tables, list) {
  606. if (addr >= table->begin_addr &&
  607. addr < table->end_addr) {
  608. @@ -213,7 +213,7 @@ static const struct unwind_idx *unwind_find_idx(unsigned long addr)
  609. break;
  610. }
  611. }
  612. - spin_unlock_irqrestore(&unwind_lock, flags);
  613. + raw_spin_unlock_irqrestore(&unwind_lock, flags);
  614. }
  615. pr_debug("%s: idx = %p\n", __func__, idx);
  616. @@ -529,9 +529,9 @@ struct unwind_table *unwind_table_add(unsigned long start, unsigned long size,
  617. tab->begin_addr = text_addr;
  618. tab->end_addr = text_addr + text_size;
  619. - spin_lock_irqsave(&unwind_lock, flags);
  620. + raw_spin_lock_irqsave(&unwind_lock, flags);
  621. list_add_tail(&tab->list, &unwind_tables);
  622. - spin_unlock_irqrestore(&unwind_lock, flags);
  623. + raw_spin_unlock_irqrestore(&unwind_lock, flags);
  624. return tab;
  625. }
  626. @@ -543,9 +543,9 @@ void unwind_table_del(struct unwind_table *tab)
  627. if (!tab)
  628. return;
  629. - spin_lock_irqsave(&unwind_lock, flags);
  630. + raw_spin_lock_irqsave(&unwind_lock, flags);
  631. list_del(&tab->list);
  632. - spin_unlock_irqrestore(&unwind_lock, flags);
  633. + raw_spin_unlock_irqrestore(&unwind_lock, flags);
  634. kfree(tab);
  635. }
  636. diff --git a/arch/arm/kvm/arm.c b/arch/arm/kvm/arm.c
  637. index d7bef2144760..36a3e51492f7 100644
  638. --- a/arch/arm/kvm/arm.c
  639. +++ b/arch/arm/kvm/arm.c
  640. @@ -496,18 +496,18 @@ static void kvm_arm_resume_guest(struct kvm *kvm)
  641. struct kvm_vcpu *vcpu;
  642. kvm_for_each_vcpu(i, vcpu, kvm) {
  643. - wait_queue_head_t *wq = kvm_arch_vcpu_wq(vcpu);
  644. + struct swait_queue_head *wq = kvm_arch_vcpu_wq(vcpu);
  645. vcpu->arch.pause = false;
  646. - wake_up_interruptible(wq);
  647. + swake_up(wq);
  648. }
  649. }
  650. static void vcpu_sleep(struct kvm_vcpu *vcpu)
  651. {
  652. - wait_queue_head_t *wq = kvm_arch_vcpu_wq(vcpu);
  653. + struct swait_queue_head *wq = kvm_arch_vcpu_wq(vcpu);
  654. - wait_event_interruptible(*wq, ((!vcpu->arch.power_off) &&
  655. + swait_event_interruptible(*wq, ((!vcpu->arch.power_off) &&
  656. (!vcpu->arch.pause)));
  657. }
  658. @@ -566,7 +566,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  659. * involves poking the GIC, which must be done in a
  660. * non-preemptible context.
  661. */
  662. - preempt_disable();
  663. + migrate_disable();
  664. kvm_timer_flush_hwstate(vcpu);
  665. kvm_vgic_flush_hwstate(vcpu);
  666. @@ -585,7 +585,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  667. local_irq_enable();
  668. kvm_timer_sync_hwstate(vcpu);
  669. kvm_vgic_sync_hwstate(vcpu);
  670. - preempt_enable();
  671. + migrate_enable();
  672. continue;
  673. }
  674. @@ -639,7 +639,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  675. kvm_vgic_sync_hwstate(vcpu);
  676. - preempt_enable();
  677. + migrate_enable();
  678. ret = handle_exit(vcpu, run, ret);
  679. }
  680. diff --git a/arch/arm/kvm/psci.c b/arch/arm/kvm/psci.c
  681. index 443db0c43d7c..a08d7a93aebb 100644
  682. --- a/arch/arm/kvm/psci.c
  683. +++ b/arch/arm/kvm/psci.c
  684. @@ -70,7 +70,7 @@ static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)
  685. {
  686. struct kvm *kvm = source_vcpu->kvm;
  687. struct kvm_vcpu *vcpu = NULL;
  688. - wait_queue_head_t *wq;
  689. + struct swait_queue_head *wq;
  690. unsigned long cpu_id;
  691. unsigned long context_id;
  692. phys_addr_t target_pc;
  693. @@ -119,7 +119,7 @@ static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)
  694. smp_mb(); /* Make sure the above is visible */
  695. wq = kvm_arch_vcpu_wq(vcpu);
  696. - wake_up_interruptible(wq);
  697. + swake_up(wq);
  698. return PSCI_RET_SUCCESS;
  699. }
  700. diff --git a/arch/arm/mach-at91/Kconfig b/arch/arm/mach-at91/Kconfig
  701. index 28656c2b54a0..3f501305ca26 100644
  702. --- a/arch/arm/mach-at91/Kconfig
  703. +++ b/arch/arm/mach-at91/Kconfig
  704. @@ -99,6 +99,7 @@ config HAVE_AT91_USB_CLK
  705. config COMMON_CLK_AT91
  706. bool
  707. select COMMON_CLK
  708. + select MFD_SYSCON
  709. config HAVE_AT91_SMD
  710. bool
  711. diff --git a/arch/arm/mach-at91/at91rm9200.c b/arch/arm/mach-at91/at91rm9200.c
  712. index c1a7c6cc00e1..63b4fa25b48a 100644
  713. --- a/arch/arm/mach-at91/at91rm9200.c
  714. +++ b/arch/arm/mach-at91/at91rm9200.c
  715. @@ -12,7 +12,6 @@
  716. #include <linux/of_platform.h>
  717. #include <asm/mach/arch.h>
  718. -#include <asm/system_misc.h>
  719. #include "generic.h"
  720. #include "soc.h"
  721. @@ -33,7 +32,6 @@ static void __init at91rm9200_dt_device_init(void)
  722. of_platform_populate(NULL, of_default_bus_match_table, NULL, soc_dev);
  723. - arm_pm_idle = at91rm9200_idle;
  724. at91rm9200_pm_init();
  725. }
  726. diff --git a/arch/arm/mach-at91/at91sam9.c b/arch/arm/mach-at91/at91sam9.c
  727. index 7eb64f763034..cada2a6412b3 100644
  728. --- a/arch/arm/mach-at91/at91sam9.c
  729. +++ b/arch/arm/mach-at91/at91sam9.c
  730. @@ -62,8 +62,6 @@ static void __init at91sam9_common_init(void)
  731. soc_dev = soc_device_to_device(soc);
  732. of_platform_populate(NULL, of_default_bus_match_table, NULL, soc_dev);
  733. -
  734. - arm_pm_idle = at91sam9_idle;
  735. }
  736. static void __init at91sam9_dt_device_init(void)
  737. diff --git a/arch/arm/mach-at91/generic.h b/arch/arm/mach-at91/generic.h
  738. index b0fa7dc7286d..28ca57a2060f 100644
  739. --- a/arch/arm/mach-at91/generic.h
  740. +++ b/arch/arm/mach-at91/generic.h
  741. @@ -11,27 +11,18 @@
  742. #ifndef _AT91_GENERIC_H
  743. #define _AT91_GENERIC_H
  744. -#include <linux/of.h>
  745. -#include <linux/reboot.h>
  746. -
  747. - /* Map io */
  748. -extern void __init at91_map_io(void);
  749. -extern void __init at91_alt_map_io(void);
  750. -
  751. -/* idle */
  752. -extern void at91rm9200_idle(void);
  753. -extern void at91sam9_idle(void);
  754. -
  755. #ifdef CONFIG_PM
  756. extern void __init at91rm9200_pm_init(void);
  757. extern void __init at91sam9260_pm_init(void);
  758. extern void __init at91sam9g45_pm_init(void);
  759. extern void __init at91sam9x5_pm_init(void);
  760. +extern void __init sama5_pm_init(void);
  761. #else
  762. static inline void __init at91rm9200_pm_init(void) { }
  763. static inline void __init at91sam9260_pm_init(void) { }
  764. static inline void __init at91sam9g45_pm_init(void) { }
  765. static inline void __init at91sam9x5_pm_init(void) { }
  766. +static inline void __init sama5_pm_init(void) { }
  767. #endif
  768. #endif /* _AT91_GENERIC_H */
  769. diff --git a/arch/arm/mach-at91/pm.c b/arch/arm/mach-at91/pm.c
  770. index d687f860a2da..ab53332a9637 100644
  771. --- a/arch/arm/mach-at91/pm.c
  772. +++ b/arch/arm/mach-at91/pm.c
  773. @@ -31,10 +31,13 @@
  774. #include <asm/mach/irq.h>
  775. #include <asm/fncpy.h>
  776. #include <asm/cacheflush.h>
  777. +#include <asm/system_misc.h>
  778. #include "generic.h"
  779. #include "pm.h"
  780. +static void __iomem *pmc;
  781. +
  782. /*
  783. * FIXME: this is needed to communicate between the pinctrl driver and
  784. * the PM implementation in the machine. Possibly part of the PM
  785. @@ -87,7 +90,7 @@ static int at91_pm_verify_clocks(void)
  786. unsigned long scsr;
  787. int i;
  788. - scsr = at91_pmc_read(AT91_PMC_SCSR);
  789. + scsr = readl(pmc + AT91_PMC_SCSR);
  790. /* USB must not be using PLLB */
  791. if ((scsr & at91_pm_data.uhp_udp_mask) != 0) {
  792. @@ -101,8 +104,7 @@ static int at91_pm_verify_clocks(void)
  793. if ((scsr & (AT91_PMC_PCK0 << i)) == 0)
  794. continue;
  795. -
  796. - css = at91_pmc_read(AT91_PMC_PCKR(i)) & AT91_PMC_CSS;
  797. + css = readl(pmc + AT91_PMC_PCKR(i)) & AT91_PMC_CSS;
  798. if (css != AT91_PMC_CSS_SLOW) {
  799. pr_err("AT91: PM - Suspend-to-RAM with PCK%d src %d\n", i, css);
  800. return 0;
  801. @@ -145,8 +147,8 @@ static void at91_pm_suspend(suspend_state_t state)
  802. flush_cache_all();
  803. outer_disable();
  804. - at91_suspend_sram_fn(at91_pmc_base, at91_ramc_base[0],
  805. - at91_ramc_base[1], pm_data);
  806. + at91_suspend_sram_fn(pmc, at91_ramc_base[0],
  807. + at91_ramc_base[1], pm_data);
  808. outer_resume();
  809. }
  810. @@ -369,6 +371,21 @@ static __init void at91_dt_ramc(void)
  811. at91_pm_set_standby(standby);
  812. }
  813. +void at91rm9200_idle(void)
  814. +{
  815. + /*
  816. + * Disable the processor clock. The processor will be automatically
  817. + * re-enabled by an interrupt or by a reset.
  818. + */
  819. + writel(AT91_PMC_PCK, pmc + AT91_PMC_SCDR);
  820. +}
  821. +
  822. +void at91sam9_idle(void)
  823. +{
  824. + writel(AT91_PMC_PCK, pmc + AT91_PMC_SCDR);
  825. + cpu_do_idle();
  826. +}
  827. +
  828. static void __init at91_pm_sram_init(void)
  829. {
  830. struct gen_pool *sram_pool;
  831. @@ -415,13 +432,36 @@ static void __init at91_pm_sram_init(void)
  832. &at91_pm_suspend_in_sram, at91_pm_suspend_in_sram_sz);
  833. }
  834. -static void __init at91_pm_init(void)
  835. +static const struct of_device_id atmel_pmc_ids[] __initconst = {
  836. + { .compatible = "atmel,at91rm9200-pmc" },
  837. + { .compatible = "atmel,at91sam9260-pmc" },
  838. + { .compatible = "atmel,at91sam9g45-pmc" },
  839. + { .compatible = "atmel,at91sam9n12-pmc" },
  840. + { .compatible = "atmel,at91sam9x5-pmc" },
  841. + { .compatible = "atmel,sama5d3-pmc" },
  842. + { .compatible = "atmel,sama5d2-pmc" },
  843. + { /* sentinel */ },
  844. +};
  845. +
  846. +static void __init at91_pm_init(void (*pm_idle)(void))
  847. {
  848. - at91_pm_sram_init();
  849. + struct device_node *pmc_np;
  850. if (at91_cpuidle_device.dev.platform_data)
  851. platform_device_register(&at91_cpuidle_device);
  852. + pmc_np = of_find_matching_node(NULL, atmel_pmc_ids);
  853. + pmc = of_iomap(pmc_np, 0);
  854. + if (!pmc) {
  855. + pr_err("AT91: PM not supported, PMC not found\n");
  856. + return;
  857. + }
  858. +
  859. + if (pm_idle)
  860. + arm_pm_idle = pm_idle;
  861. +
  862. + at91_pm_sram_init();
  863. +
  864. if (at91_suspend_sram_fn)
  865. suspend_set_ops(&at91_pm_ops);
  866. else
  867. @@ -440,7 +480,7 @@ void __init at91rm9200_pm_init(void)
  868. at91_pm_data.uhp_udp_mask = AT91RM9200_PMC_UHP | AT91RM9200_PMC_UDP;
  869. at91_pm_data.memctrl = AT91_MEMCTRL_MC;
  870. - at91_pm_init();
  871. + at91_pm_init(at91rm9200_idle);
  872. }
  873. void __init at91sam9260_pm_init(void)
  874. @@ -448,7 +488,7 @@ void __init at91sam9260_pm_init(void)
  875. at91_dt_ramc();
  876. at91_pm_data.memctrl = AT91_MEMCTRL_SDRAMC;
  877. at91_pm_data.uhp_udp_mask = AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP;
  878. - return at91_pm_init();
  879. + at91_pm_init(at91sam9_idle);
  880. }
  881. void __init at91sam9g45_pm_init(void)
  882. @@ -456,7 +496,7 @@ void __init at91sam9g45_pm_init(void)
  883. at91_dt_ramc();
  884. at91_pm_data.uhp_udp_mask = AT91SAM926x_PMC_UHP;
  885. at91_pm_data.memctrl = AT91_MEMCTRL_DDRSDR;
  886. - return at91_pm_init();
  887. + at91_pm_init(at91sam9_idle);
  888. }
  889. void __init at91sam9x5_pm_init(void)
  890. @@ -464,5 +504,13 @@ void __init at91sam9x5_pm_init(void)
  891. at91_dt_ramc();
  892. at91_pm_data.uhp_udp_mask = AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP;
  893. at91_pm_data.memctrl = AT91_MEMCTRL_DDRSDR;
  894. - return at91_pm_init();
  895. + at91_pm_init(at91sam9_idle);
  896. +}
  897. +
  898. +void __init sama5_pm_init(void)
  899. +{
  900. + at91_dt_ramc();
  901. + at91_pm_data.uhp_udp_mask = AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP;
  902. + at91_pm_data.memctrl = AT91_MEMCTRL_DDRSDR;
  903. + at91_pm_init(NULL);
  904. }
  905. diff --git a/arch/arm/mach-at91/sama5.c b/arch/arm/mach-at91/sama5.c
  906. index d9cf6799aec0..df8fdf1cf66d 100644
  907. --- a/arch/arm/mach-at91/sama5.c
  908. +++ b/arch/arm/mach-at91/sama5.c
  909. @@ -51,7 +51,7 @@ static void __init sama5_dt_device_init(void)
  910. soc_dev = soc_device_to_device(soc);
  911. of_platform_populate(NULL, of_default_bus_match_table, NULL, soc_dev);
  912. - at91sam9x5_pm_init();
  913. + sama5_pm_init();
  914. }
  915. static const char *const sama5_dt_board_compat[] __initconst = {
  916. diff --git a/arch/arm/mach-exynos/platsmp.c b/arch/arm/mach-exynos/platsmp.c
  917. index 98a2c0cbb833..310dce500d3e 100644
  918. --- a/arch/arm/mach-exynos/platsmp.c
  919. +++ b/arch/arm/mach-exynos/platsmp.c
  920. @@ -230,7 +230,7 @@ static void __iomem *scu_base_addr(void)
  921. return (void __iomem *)(S5P_VA_SCU);
  922. }
  923. -static DEFINE_SPINLOCK(boot_lock);
  924. +static DEFINE_RAW_SPINLOCK(boot_lock);
  925. static void exynos_secondary_init(unsigned int cpu)
  926. {
  927. @@ -243,8 +243,8 @@ static void exynos_secondary_init(unsigned int cpu)
  928. /*
  929. * Synchronise with the boot thread.
  930. */
  931. - spin_lock(&boot_lock);
  932. - spin_unlock(&boot_lock);
  933. + raw_spin_lock(&boot_lock);
  934. + raw_spin_unlock(&boot_lock);
  935. }
  936. int exynos_set_boot_addr(u32 core_id, unsigned long boot_addr)
  937. @@ -308,7 +308,7 @@ static int exynos_boot_secondary(unsigned int cpu, struct task_struct *idle)
  938. * Set synchronisation state between this boot processor
  939. * and the secondary one
  940. */
  941. - spin_lock(&boot_lock);
  942. + raw_spin_lock(&boot_lock);
  943. /*
  944. * The secondary processor is waiting to be released from
  945. @@ -335,7 +335,7 @@ static int exynos_boot_secondary(unsigned int cpu, struct task_struct *idle)
  946. if (timeout == 0) {
  947. printk(KERN_ERR "cpu1 power enable failed");
  948. - spin_unlock(&boot_lock);
  949. + raw_spin_unlock(&boot_lock);
  950. return -ETIMEDOUT;
  951. }
  952. }
  953. @@ -381,7 +381,7 @@ static int exynos_boot_secondary(unsigned int cpu, struct task_struct *idle)
  954. * calibrations, then wait for it to finish
  955. */
  956. fail:
  957. - spin_unlock(&boot_lock);
  958. + raw_spin_unlock(&boot_lock);
  959. return pen_release != -1 ? ret : 0;
  960. }
  961. diff --git a/arch/arm/mach-hisi/platmcpm.c b/arch/arm/mach-hisi/platmcpm.c
  962. index b5f8f5ffda79..9753a84df9c4 100644
  963. --- a/arch/arm/mach-hisi/platmcpm.c
  964. +++ b/arch/arm/mach-hisi/platmcpm.c
  965. @@ -61,7 +61,7 @@
  966. static void __iomem *sysctrl, *fabric;
  967. static int hip04_cpu_table[HIP04_MAX_CLUSTERS][HIP04_MAX_CPUS_PER_CLUSTER];
  968. -static DEFINE_SPINLOCK(boot_lock);
  969. +static DEFINE_RAW_SPINLOCK(boot_lock);
  970. static u32 fabric_phys_addr;
  971. /*
  972. * [0]: bootwrapper physical address
  973. @@ -113,7 +113,7 @@ static int hip04_boot_secondary(unsigned int l_cpu, struct task_struct *idle)
  974. if (cluster >= HIP04_MAX_CLUSTERS || cpu >= HIP04_MAX_CPUS_PER_CLUSTER)
  975. return -EINVAL;
  976. - spin_lock_irq(&boot_lock);
  977. + raw_spin_lock_irq(&boot_lock);
  978. if (hip04_cpu_table[cluster][cpu])
  979. goto out;
  980. @@ -147,7 +147,7 @@ static int hip04_boot_secondary(unsigned int l_cpu, struct task_struct *idle)
  981. out:
  982. hip04_cpu_table[cluster][cpu]++;
  983. - spin_unlock_irq(&boot_lock);
  984. + raw_spin_unlock_irq(&boot_lock);
  985. return 0;
  986. }
  987. @@ -162,11 +162,11 @@ static void hip04_cpu_die(unsigned int l_cpu)
  988. cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  989. cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  990. - spin_lock(&boot_lock);
  991. + raw_spin_lock(&boot_lock);
  992. hip04_cpu_table[cluster][cpu]--;
  993. if (hip04_cpu_table[cluster][cpu] == 1) {
  994. /* A power_up request went ahead of us. */
  995. - spin_unlock(&boot_lock);
  996. + raw_spin_unlock(&boot_lock);
  997. return;
  998. } else if (hip04_cpu_table[cluster][cpu] > 1) {
  999. pr_err("Cluster %d CPU%d boots multiple times\n", cluster, cpu);
  1000. @@ -174,7 +174,7 @@ static void hip04_cpu_die(unsigned int l_cpu)
  1001. }
  1002. last_man = hip04_cluster_is_down(cluster);
  1003. - spin_unlock(&boot_lock);
  1004. + raw_spin_unlock(&boot_lock);
  1005. if (last_man) {
  1006. /* Since it's Cortex A15, disable L2 prefetching. */
  1007. asm volatile(
  1008. @@ -203,7 +203,7 @@ static int hip04_cpu_kill(unsigned int l_cpu)
  1009. cpu >= HIP04_MAX_CPUS_PER_CLUSTER);
  1010. count = TIMEOUT_MSEC / POLL_MSEC;
  1011. - spin_lock_irq(&boot_lock);
  1012. + raw_spin_lock_irq(&boot_lock);
  1013. for (tries = 0; tries < count; tries++) {
  1014. if (hip04_cpu_table[cluster][cpu])
  1015. goto err;
  1016. @@ -211,10 +211,10 @@ static int hip04_cpu_kill(unsigned int l_cpu)
  1017. data = readl_relaxed(sysctrl + SC_CPU_RESET_STATUS(cluster));
  1018. if (data & CORE_WFI_STATUS(cpu))
  1019. break;
  1020. - spin_unlock_irq(&boot_lock);
  1021. + raw_spin_unlock_irq(&boot_lock);
  1022. /* Wait for clean L2 when the whole cluster is down. */
  1023. msleep(POLL_MSEC);
  1024. - spin_lock_irq(&boot_lock);
  1025. + raw_spin_lock_irq(&boot_lock);
  1026. }
  1027. if (tries >= count)
  1028. goto err;
  1029. @@ -231,10 +231,10 @@ static int hip04_cpu_kill(unsigned int l_cpu)
  1030. goto err;
  1031. if (hip04_cluster_is_down(cluster))
  1032. hip04_set_snoop_filter(cluster, 0);
  1033. - spin_unlock_irq(&boot_lock);
  1034. + raw_spin_unlock_irq(&boot_lock);
  1035. return 1;
  1036. err:
  1037. - spin_unlock_irq(&boot_lock);
  1038. + raw_spin_unlock_irq(&boot_lock);
  1039. return 0;
  1040. }
  1041. #endif
  1042. diff --git a/arch/arm/mach-imx/Kconfig b/arch/arm/mach-imx/Kconfig
  1043. index 8ceda2844c4f..08bcf8fb76f2 100644
  1044. --- a/arch/arm/mach-imx/Kconfig
  1045. +++ b/arch/arm/mach-imx/Kconfig
  1046. @@ -524,7 +524,7 @@ config SOC_IMX6Q
  1047. bool "i.MX6 Quad/DualLite support"
  1048. select ARM_ERRATA_764369 if SMP
  1049. select HAVE_ARM_SCU if SMP
  1050. - select HAVE_ARM_TWD if SMP
  1051. + select HAVE_ARM_TWD
  1052. select PCI_DOMAINS if PCI
  1053. select PINCTRL_IMX6Q
  1054. select SOC_IMX6
  1055. diff --git a/arch/arm/mach-omap2/omap-smp.c b/arch/arm/mach-omap2/omap-smp.c
  1056. index 79e1f876d1c9..7e625c17f78e 100644
  1057. --- a/arch/arm/mach-omap2/omap-smp.c
  1058. +++ b/arch/arm/mach-omap2/omap-smp.c
  1059. @@ -43,7 +43,7 @@
  1060. /* SCU base address */
  1061. static void __iomem *scu_base;
  1062. -static DEFINE_SPINLOCK(boot_lock);
  1063. +static DEFINE_RAW_SPINLOCK(boot_lock);
  1064. void __iomem *omap4_get_scu_base(void)
  1065. {
  1066. @@ -74,8 +74,8 @@ static void omap4_secondary_init(unsigned int cpu)
  1067. /*
  1068. * Synchronise with the boot thread.
  1069. */
  1070. - spin_lock(&boot_lock);
  1071. - spin_unlock(&boot_lock);
  1072. + raw_spin_lock(&boot_lock);
  1073. + raw_spin_unlock(&boot_lock);
  1074. }
  1075. static int omap4_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1076. @@ -89,7 +89,7 @@ static int omap4_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1077. * Set synchronisation state between this boot processor
  1078. * and the secondary one
  1079. */
  1080. - spin_lock(&boot_lock);
  1081. + raw_spin_lock(&boot_lock);
  1082. /*
  1083. * Update the AuxCoreBoot0 with boot state for secondary core.
  1084. @@ -166,7 +166,7 @@ static int omap4_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1085. * Now the secondary core is starting up let it run its
  1086. * calibrations, then wait for it to finish
  1087. */
  1088. - spin_unlock(&boot_lock);
  1089. + raw_spin_unlock(&boot_lock);
  1090. return 0;
  1091. }
  1092. diff --git a/arch/arm/mach-prima2/platsmp.c b/arch/arm/mach-prima2/platsmp.c
  1093. index e46c91094dde..dcb3ed0c26da 100644
  1094. --- a/arch/arm/mach-prima2/platsmp.c
  1095. +++ b/arch/arm/mach-prima2/platsmp.c
  1096. @@ -22,7 +22,7 @@
  1097. static void __iomem *clk_base;
  1098. -static DEFINE_SPINLOCK(boot_lock);
  1099. +static DEFINE_RAW_SPINLOCK(boot_lock);
  1100. static void sirfsoc_secondary_init(unsigned int cpu)
  1101. {
  1102. @@ -36,8 +36,8 @@ static void sirfsoc_secondary_init(unsigned int cpu)
  1103. /*
  1104. * Synchronise with the boot thread.
  1105. */
  1106. - spin_lock(&boot_lock);
  1107. - spin_unlock(&boot_lock);
  1108. + raw_spin_lock(&boot_lock);
  1109. + raw_spin_unlock(&boot_lock);
  1110. }
  1111. static const struct of_device_id clk_ids[] = {
  1112. @@ -75,7 +75,7 @@ static int sirfsoc_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1113. /* make sure write buffer is drained */
  1114. mb();
  1115. - spin_lock(&boot_lock);
  1116. + raw_spin_lock(&boot_lock);
  1117. /*
  1118. * The secondary processor is waiting to be released from
  1119. @@ -107,7 +107,7 @@ static int sirfsoc_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1120. * now the secondary core is starting up let it run its
  1121. * calibrations, then wait for it to finish
  1122. */
  1123. - spin_unlock(&boot_lock);
  1124. + raw_spin_unlock(&boot_lock);
  1125. return pen_release != -1 ? -ENOSYS : 0;
  1126. }
  1127. diff --git a/arch/arm/mach-qcom/platsmp.c b/arch/arm/mach-qcom/platsmp.c
  1128. index 9b00123a315d..0a49fe1bc8cf 100644
  1129. --- a/arch/arm/mach-qcom/platsmp.c
  1130. +++ b/arch/arm/mach-qcom/platsmp.c
  1131. @@ -46,7 +46,7 @@
  1132. extern void secondary_startup_arm(void);
  1133. -static DEFINE_SPINLOCK(boot_lock);
  1134. +static DEFINE_RAW_SPINLOCK(boot_lock);
  1135. #ifdef CONFIG_HOTPLUG_CPU
  1136. static void qcom_cpu_die(unsigned int cpu)
  1137. @@ -60,8 +60,8 @@ static void qcom_secondary_init(unsigned int cpu)
  1138. /*
  1139. * Synchronise with the boot thread.
  1140. */
  1141. - spin_lock(&boot_lock);
  1142. - spin_unlock(&boot_lock);
  1143. + raw_spin_lock(&boot_lock);
  1144. + raw_spin_unlock(&boot_lock);
  1145. }
  1146. static int scss_release_secondary(unsigned int cpu)
  1147. @@ -284,7 +284,7 @@ static int qcom_boot_secondary(unsigned int cpu, int (*func)(unsigned int))
  1148. * set synchronisation state between this boot processor
  1149. * and the secondary one
  1150. */
  1151. - spin_lock(&boot_lock);
  1152. + raw_spin_lock(&boot_lock);
  1153. /*
  1154. * Send the secondary CPU a soft interrupt, thereby causing
  1155. @@ -297,7 +297,7 @@ static int qcom_boot_secondary(unsigned int cpu, int (*func)(unsigned int))
  1156. * now the secondary core is starting up let it run its
  1157. * calibrations, then wait for it to finish
  1158. */
  1159. - spin_unlock(&boot_lock);
  1160. + raw_spin_unlock(&boot_lock);
  1161. return ret;
  1162. }
  1163. diff --git a/arch/arm/mach-spear/platsmp.c b/arch/arm/mach-spear/platsmp.c
  1164. index fd4297713d67..b0553b2c2d53 100644
  1165. --- a/arch/arm/mach-spear/platsmp.c
  1166. +++ b/arch/arm/mach-spear/platsmp.c
  1167. @@ -32,7 +32,7 @@ static void write_pen_release(int val)
  1168. sync_cache_w(&pen_release);
  1169. }
  1170. -static DEFINE_SPINLOCK(boot_lock);
  1171. +static DEFINE_RAW_SPINLOCK(boot_lock);
  1172. static void __iomem *scu_base = IOMEM(VA_SCU_BASE);
  1173. @@ -47,8 +47,8 @@ static void spear13xx_secondary_init(unsigned int cpu)
  1174. /*
  1175. * Synchronise with the boot thread.
  1176. */
  1177. - spin_lock(&boot_lock);
  1178. - spin_unlock(&boot_lock);
  1179. + raw_spin_lock(&boot_lock);
  1180. + raw_spin_unlock(&boot_lock);
  1181. }
  1182. static int spear13xx_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1183. @@ -59,7 +59,7 @@ static int spear13xx_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1184. * set synchronisation state between this boot processor
  1185. * and the secondary one
  1186. */
  1187. - spin_lock(&boot_lock);
  1188. + raw_spin_lock(&boot_lock);
  1189. /*
  1190. * The secondary processor is waiting to be released from
  1191. @@ -84,7 +84,7 @@ static int spear13xx_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1192. * now the secondary core is starting up let it run its
  1193. * calibrations, then wait for it to finish
  1194. */
  1195. - spin_unlock(&boot_lock);
  1196. + raw_spin_unlock(&boot_lock);
  1197. return pen_release != -1 ? -ENOSYS : 0;
  1198. }
  1199. diff --git a/arch/arm/mach-sti/platsmp.c b/arch/arm/mach-sti/platsmp.c
  1200. index c4ad6eae67fa..e830b20b212f 100644
  1201. --- a/arch/arm/mach-sti/platsmp.c
  1202. +++ b/arch/arm/mach-sti/platsmp.c
  1203. @@ -35,7 +35,7 @@ static void write_pen_release(int val)
  1204. sync_cache_w(&pen_release);
  1205. }
  1206. -static DEFINE_SPINLOCK(boot_lock);
  1207. +static DEFINE_RAW_SPINLOCK(boot_lock);
  1208. static void sti_secondary_init(unsigned int cpu)
  1209. {
  1210. @@ -48,8 +48,8 @@ static void sti_secondary_init(unsigned int cpu)
  1211. /*
  1212. * Synchronise with the boot thread.
  1213. */
  1214. - spin_lock(&boot_lock);
  1215. - spin_unlock(&boot_lock);
  1216. + raw_spin_lock(&boot_lock);
  1217. + raw_spin_unlock(&boot_lock);
  1218. }
  1219. static int sti_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1220. @@ -60,7 +60,7 @@ static int sti_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1221. * set synchronisation state between this boot processor
  1222. * and the secondary one
  1223. */
  1224. - spin_lock(&boot_lock);
  1225. + raw_spin_lock(&boot_lock);
  1226. /*
  1227. * The secondary processor is waiting to be released from
  1228. @@ -91,7 +91,7 @@ static int sti_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1229. * now the secondary core is starting up let it run its
  1230. * calibrations, then wait for it to finish
  1231. */
  1232. - spin_unlock(&boot_lock);
  1233. + raw_spin_unlock(&boot_lock);
  1234. return pen_release != -1 ? -ENOSYS : 0;
  1235. }
  1236. diff --git a/arch/arm/mm/fault.c b/arch/arm/mm/fault.c
  1237. index c095455d496e..276ac73f8650 100644
  1238. --- a/arch/arm/mm/fault.c
  1239. +++ b/arch/arm/mm/fault.c
  1240. @@ -430,6 +430,9 @@ do_translation_fault(unsigned long addr, unsigned int fsr,
  1241. if (addr < TASK_SIZE)
  1242. return do_page_fault(addr, fsr, regs);
  1243. + if (interrupts_enabled(regs))
  1244. + local_irq_enable();
  1245. +
  1246. if (user_mode(regs))
  1247. goto bad_area;
  1248. @@ -497,6 +500,9 @@ do_translation_fault(unsigned long addr, unsigned int fsr,
  1249. static int
  1250. do_sect_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  1251. {
  1252. + if (interrupts_enabled(regs))
  1253. + local_irq_enable();
  1254. +
  1255. do_bad_area(addr, fsr, regs);
  1256. return 0;
  1257. }
  1258. diff --git a/arch/arm/mm/highmem.c b/arch/arm/mm/highmem.c
  1259. index d02f8187b1cc..542692dbd40a 100644
  1260. --- a/arch/arm/mm/highmem.c
  1261. +++ b/arch/arm/mm/highmem.c
  1262. @@ -34,6 +34,11 @@ static inline pte_t get_fixmap_pte(unsigned long vaddr)
  1263. return *ptep;
  1264. }
  1265. +static unsigned int fixmap_idx(int type)
  1266. +{
  1267. + return FIX_KMAP_BEGIN + type + KM_TYPE_NR * smp_processor_id();
  1268. +}
  1269. +
  1270. void *kmap(struct page *page)
  1271. {
  1272. might_sleep();
  1273. @@ -54,12 +59,13 @@ EXPORT_SYMBOL(kunmap);
  1274. void *kmap_atomic(struct page *page)
  1275. {
  1276. + pte_t pte = mk_pte(page, kmap_prot);
  1277. unsigned int idx;
  1278. unsigned long vaddr;
  1279. void *kmap;
  1280. int type;
  1281. - preempt_disable();
  1282. + preempt_disable_nort();
  1283. pagefault_disable();
  1284. if (!PageHighMem(page))
  1285. return page_address(page);
  1286. @@ -79,7 +85,7 @@ void *kmap_atomic(struct page *page)
  1287. type = kmap_atomic_idx_push();
  1288. - idx = FIX_KMAP_BEGIN + type + KM_TYPE_NR * smp_processor_id();
  1289. + idx = fixmap_idx(type);
  1290. vaddr = __fix_to_virt(idx);
  1291. #ifdef CONFIG_DEBUG_HIGHMEM
  1292. /*
  1293. @@ -93,7 +99,10 @@ void *kmap_atomic(struct page *page)
  1294. * in place, so the contained TLB flush ensures the TLB is updated
  1295. * with the new mapping.
  1296. */
  1297. - set_fixmap_pte(idx, mk_pte(page, kmap_prot));
  1298. +#ifdef CONFIG_PREEMPT_RT_FULL
  1299. + current->kmap_pte[type] = pte;
  1300. +#endif
  1301. + set_fixmap_pte(idx, pte);
  1302. return (void *)vaddr;
  1303. }
  1304. @@ -106,44 +115,75 @@ void __kunmap_atomic(void *kvaddr)
  1305. if (kvaddr >= (void *)FIXADDR_START) {
  1306. type = kmap_atomic_idx();
  1307. - idx = FIX_KMAP_BEGIN + type + KM_TYPE_NR * smp_processor_id();
  1308. + idx = fixmap_idx(type);
  1309. if (cache_is_vivt())
  1310. __cpuc_flush_dcache_area((void *)vaddr, PAGE_SIZE);
  1311. +#ifdef CONFIG_PREEMPT_RT_FULL
  1312. + current->kmap_pte[type] = __pte(0);
  1313. +#endif
  1314. #ifdef CONFIG_DEBUG_HIGHMEM
  1315. BUG_ON(vaddr != __fix_to_virt(idx));
  1316. - set_fixmap_pte(idx, __pte(0));
  1317. #else
  1318. (void) idx; /* to kill a warning */
  1319. #endif
  1320. + set_fixmap_pte(idx, __pte(0));
  1321. kmap_atomic_idx_pop();
  1322. } else if (vaddr >= PKMAP_ADDR(0) && vaddr < PKMAP_ADDR(LAST_PKMAP)) {
  1323. /* this address was obtained through kmap_high_get() */
  1324. kunmap_high(pte_page(pkmap_page_table[PKMAP_NR(vaddr)]));
  1325. }
  1326. pagefault_enable();
  1327. - preempt_enable();
  1328. + preempt_enable_nort();
  1329. }
  1330. EXPORT_SYMBOL(__kunmap_atomic);
  1331. void *kmap_atomic_pfn(unsigned long pfn)
  1332. {
  1333. + pte_t pte = pfn_pte(pfn, kmap_prot);
  1334. unsigned long vaddr;
  1335. int idx, type;
  1336. struct page *page = pfn_to_page(pfn);
  1337. - preempt_disable();
  1338. + preempt_disable_nort();
  1339. pagefault_disable();
  1340. if (!PageHighMem(page))
  1341. return page_address(page);
  1342. type = kmap_atomic_idx_push();
  1343. - idx = FIX_KMAP_BEGIN + type + KM_TYPE_NR * smp_processor_id();
  1344. + idx = fixmap_idx(type);
  1345. vaddr = __fix_to_virt(idx);
  1346. #ifdef CONFIG_DEBUG_HIGHMEM
  1347. BUG_ON(!pte_none(get_fixmap_pte(vaddr)));
  1348. #endif
  1349. - set_fixmap_pte(idx, pfn_pte(pfn, kmap_prot));
  1350. +#ifdef CONFIG_PREEMPT_RT_FULL
  1351. + current->kmap_pte[type] = pte;
  1352. +#endif
  1353. + set_fixmap_pte(idx, pte);
  1354. return (void *)vaddr;
  1355. }
  1356. +#if defined CONFIG_PREEMPT_RT_FULL
  1357. +void switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p)
  1358. +{
  1359. + int i;
  1360. +
  1361. + /*
  1362. + * Clear @prev's kmap_atomic mappings
  1363. + */
  1364. + for (i = 0; i < prev_p->kmap_idx; i++) {
  1365. + int idx = fixmap_idx(i);
  1366. +
  1367. + set_fixmap_pte(idx, __pte(0));
  1368. + }
  1369. + /*
  1370. + * Restore @next_p's kmap_atomic mappings
  1371. + */
  1372. + for (i = 0; i < next_p->kmap_idx; i++) {
  1373. + int idx = fixmap_idx(i);
  1374. +
  1375. + if (!pte_none(next_p->kmap_pte[i]))
  1376. + set_fixmap_pte(idx, next_p->kmap_pte[i]);
  1377. + }
  1378. +}
  1379. +#endif
  1380. diff --git a/arch/arm/plat-versatile/platsmp.c b/arch/arm/plat-versatile/platsmp.c
  1381. index 53feb90c840c..b4a8d54fc3f3 100644
  1382. --- a/arch/arm/plat-versatile/platsmp.c
  1383. +++ b/arch/arm/plat-versatile/platsmp.c
  1384. @@ -30,7 +30,7 @@ static void write_pen_release(int val)
  1385. sync_cache_w(&pen_release);
  1386. }
  1387. -static DEFINE_SPINLOCK(boot_lock);
  1388. +static DEFINE_RAW_SPINLOCK(boot_lock);
  1389. void versatile_secondary_init(unsigned int cpu)
  1390. {
  1391. @@ -43,8 +43,8 @@ void versatile_secondary_init(unsigned int cpu)
  1392. /*
  1393. * Synchronise with the boot thread.
  1394. */
  1395. - spin_lock(&boot_lock);
  1396. - spin_unlock(&boot_lock);
  1397. + raw_spin_lock(&boot_lock);
  1398. + raw_spin_unlock(&boot_lock);
  1399. }
  1400. int versatile_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1401. @@ -55,7 +55,7 @@ int versatile_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1402. * Set synchronisation state between this boot processor
  1403. * and the secondary one
  1404. */
  1405. - spin_lock(&boot_lock);
  1406. + raw_spin_lock(&boot_lock);
  1407. /*
  1408. * This is really belt and braces; we hold unintended secondary
  1409. @@ -85,7 +85,7 @@ int versatile_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1410. * now the secondary core is starting up let it run its
  1411. * calibrations, then wait for it to finish
  1412. */
  1413. - spin_unlock(&boot_lock);
  1414. + raw_spin_unlock(&boot_lock);
  1415. return pen_release != -1 ? -ENOSYS : 0;
  1416. }
  1417. diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig
  1418. index 14cdc6dea493..9196cf82f7be 100644
  1419. --- a/arch/arm64/Kconfig
  1420. +++ b/arch/arm64/Kconfig
  1421. @@ -76,6 +76,7 @@ config ARM64
  1422. select HAVE_PERF_REGS
  1423. select HAVE_PERF_USER_STACK_DUMP
  1424. select HAVE_RCU_TABLE_FREE
  1425. + select HAVE_PREEMPT_LAZY
  1426. select HAVE_SYSCALL_TRACEPOINTS
  1427. select IOMMU_DMA if IOMMU_SUPPORT
  1428. select IRQ_DOMAIN
  1429. @@ -582,7 +583,7 @@ config XEN_DOM0
  1430. config XEN
  1431. bool "Xen guest support on ARM64"
  1432. - depends on ARM64 && OF
  1433. + depends on ARM64 && OF && !PREEMPT_RT_FULL
  1434. select SWIOTLB_XEN
  1435. help
  1436. Say Y if you want to run Linux in a Virtual Machine on Xen on ARM64.
  1437. diff --git a/arch/arm64/include/asm/thread_info.h b/arch/arm64/include/asm/thread_info.h
  1438. index 90c7ff233735..5f4e89fbc290 100644
  1439. --- a/arch/arm64/include/asm/thread_info.h
  1440. +++ b/arch/arm64/include/asm/thread_info.h
  1441. @@ -49,6 +49,7 @@ struct thread_info {
  1442. mm_segment_t addr_limit; /* address limit */
  1443. struct task_struct *task; /* main task structure */
  1444. int preempt_count; /* 0 => preemptable, <0 => bug */
  1445. + int preempt_lazy_count; /* 0 => preemptable, <0 => bug */
  1446. int cpu; /* cpu */
  1447. };
  1448. @@ -103,6 +104,7 @@ static inline struct thread_info *current_thread_info(void)
  1449. #define TIF_NEED_RESCHED 1
  1450. #define TIF_NOTIFY_RESUME 2 /* callback before returning to user */
  1451. #define TIF_FOREIGN_FPSTATE 3 /* CPU's FP state is not current's */
  1452. +#define TIF_NEED_RESCHED_LAZY 4
  1453. #define TIF_NOHZ 7
  1454. #define TIF_SYSCALL_TRACE 8
  1455. #define TIF_SYSCALL_AUDIT 9
  1456. @@ -118,6 +120,7 @@ static inline struct thread_info *current_thread_info(void)
  1457. #define _TIF_NEED_RESCHED (1 << TIF_NEED_RESCHED)
  1458. #define _TIF_NOTIFY_RESUME (1 << TIF_NOTIFY_RESUME)
  1459. #define _TIF_FOREIGN_FPSTATE (1 << TIF_FOREIGN_FPSTATE)
  1460. +#define _TIF_NEED_RESCHED_LAZY (1 << TIF_NEED_RESCHED_LAZY)
  1461. #define _TIF_NOHZ (1 << TIF_NOHZ)
  1462. #define _TIF_SYSCALL_TRACE (1 << TIF_SYSCALL_TRACE)
  1463. #define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
  1464. @@ -126,7 +129,8 @@ static inline struct thread_info *current_thread_info(void)
  1465. #define _TIF_32BIT (1 << TIF_32BIT)
  1466. #define _TIF_WORK_MASK (_TIF_NEED_RESCHED | _TIF_SIGPENDING | \
  1467. - _TIF_NOTIFY_RESUME | _TIF_FOREIGN_FPSTATE)
  1468. + _TIF_NOTIFY_RESUME | _TIF_FOREIGN_FPSTATE | \
  1469. + _TIF_NEED_RESCHED_LAZY)
  1470. #define _TIF_SYSCALL_WORK (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | \
  1471. _TIF_SYSCALL_TRACEPOINT | _TIF_SECCOMP | \
  1472. diff --git a/arch/arm64/kernel/asm-offsets.c b/arch/arm64/kernel/asm-offsets.c
  1473. index 087cf9a65359..d74475928399 100644
  1474. --- a/arch/arm64/kernel/asm-offsets.c
  1475. +++ b/arch/arm64/kernel/asm-offsets.c
  1476. @@ -35,6 +35,7 @@ int main(void)
  1477. BLANK();
  1478. DEFINE(TI_FLAGS, offsetof(struct thread_info, flags));
  1479. DEFINE(TI_PREEMPT, offsetof(struct thread_info, preempt_count));
  1480. + DEFINE(TI_PREEMPT_LAZY, offsetof(struct thread_info, preempt_lazy_count));
  1481. DEFINE(TI_ADDR_LIMIT, offsetof(struct thread_info, addr_limit));
  1482. DEFINE(TI_TASK, offsetof(struct thread_info, task));
  1483. DEFINE(TI_CPU, offsetof(struct thread_info, cpu));
  1484. diff --git a/arch/arm64/kernel/entry.S b/arch/arm64/kernel/entry.S
  1485. index bd14849beb73..cf92d4ee51db 100644
  1486. --- a/arch/arm64/kernel/entry.S
  1487. +++ b/arch/arm64/kernel/entry.S
  1488. @@ -376,11 +376,16 @@ el1_irq:
  1489. #ifdef CONFIG_PREEMPT
  1490. get_thread_info tsk
  1491. ldr w24, [tsk, #TI_PREEMPT] // get preempt count
  1492. - cbnz w24, 1f // preempt count != 0
  1493. + cbnz w24, 2f // preempt count != 0
  1494. ldr x0, [tsk, #TI_FLAGS] // get flags
  1495. - tbz x0, #TIF_NEED_RESCHED, 1f // needs rescheduling?
  1496. - bl el1_preempt
  1497. + tbnz x0, #TIF_NEED_RESCHED, 1f // needs rescheduling?
  1498. +
  1499. + ldr w24, [tsk, #TI_PREEMPT_LAZY] // get preempt lazy count
  1500. + cbnz w24, 2f // preempt lazy count != 0
  1501. + tbz x0, #TIF_NEED_RESCHED_LAZY, 2f // needs rescheduling?
  1502. 1:
  1503. + bl el1_preempt
  1504. +2:
  1505. #endif
  1506. #ifdef CONFIG_TRACE_IRQFLAGS
  1507. bl trace_hardirqs_on
  1508. @@ -394,6 +399,7 @@ el1_preempt:
  1509. 1: bl preempt_schedule_irq // irq en/disable is done inside
  1510. ldr x0, [tsk, #TI_FLAGS] // get new tasks TI_FLAGS
  1511. tbnz x0, #TIF_NEED_RESCHED, 1b // needs rescheduling?
  1512. + tbnz x0, #TIF_NEED_RESCHED_LAZY, 1b // needs rescheduling?
  1513. ret x24
  1514. #endif
  1515. @@ -638,6 +644,7 @@ ret_fast_syscall_trace:
  1516. */
  1517. work_pending:
  1518. tbnz x1, #TIF_NEED_RESCHED, work_resched
  1519. + tbnz x1, #TIF_NEED_RESCHED_LAZY, work_resched
  1520. /* TIF_SIGPENDING, TIF_NOTIFY_RESUME or TIF_FOREIGN_FPSTATE case */
  1521. ldr x2, [sp, #S_PSTATE]
  1522. mov x0, sp // 'regs'
  1523. diff --git a/arch/mips/Kconfig b/arch/mips/Kconfig
  1524. index 8b0424abc84c..5422d4c0bbdf 100644
  1525. --- a/arch/mips/Kconfig
  1526. +++ b/arch/mips/Kconfig
  1527. @@ -2411,7 +2411,7 @@ config CPU_R4400_WORKAROUNDS
  1528. #
  1529. config HIGHMEM
  1530. bool "High Memory Support"
  1531. - depends on 32BIT && CPU_SUPPORTS_HIGHMEM && SYS_SUPPORTS_HIGHMEM && !CPU_MIPS32_3_5_EVA
  1532. + depends on 32BIT && CPU_SUPPORTS_HIGHMEM && SYS_SUPPORTS_HIGHMEM && !CPU_MIPS32_3_5_EVA && !PREEMPT_RT_FULL
  1533. config CPU_SUPPORTS_HIGHMEM
  1534. bool
  1535. diff --git a/arch/mips/kvm/mips.c b/arch/mips/kvm/mips.c
  1536. index a017b23ee4aa..8d4d9270140f 100644
  1537. --- a/arch/mips/kvm/mips.c
  1538. +++ b/arch/mips/kvm/mips.c
  1539. @@ -454,8 +454,8 @@ int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
  1540. dvcpu->arch.wait = 0;
  1541. - if (waitqueue_active(&dvcpu->wq))
  1542. - wake_up_interruptible(&dvcpu->wq);
  1543. + if (swait_active(&dvcpu->wq))
  1544. + swake_up(&dvcpu->wq);
  1545. return 0;
  1546. }
  1547. @@ -1183,8 +1183,8 @@ static void kvm_mips_comparecount_func(unsigned long data)
  1548. kvm_mips_callbacks->queue_timer_int(vcpu);
  1549. vcpu->arch.wait = 0;
  1550. - if (waitqueue_active(&vcpu->wq))
  1551. - wake_up_interruptible(&vcpu->wq);
  1552. + if (swait_active(&vcpu->wq))
  1553. + swake_up(&vcpu->wq);
  1554. }
  1555. /* low level hrtimer wake routine */
  1556. diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig
  1557. index db49e0d796b1..1d2be228661c 100644
  1558. --- a/arch/powerpc/Kconfig
  1559. +++ b/arch/powerpc/Kconfig
  1560. @@ -60,10 +60,11 @@ config LOCKDEP_SUPPORT
  1561. config RWSEM_GENERIC_SPINLOCK
  1562. bool
  1563. + default y if PREEMPT_RT_FULL
  1564. config RWSEM_XCHGADD_ALGORITHM
  1565. bool
  1566. - default y
  1567. + default y if !PREEMPT_RT_FULL
  1568. config GENERIC_LOCKBREAK
  1569. bool
  1570. @@ -141,6 +142,7 @@ config PPC
  1571. select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST
  1572. select GENERIC_STRNCPY_FROM_USER
  1573. select GENERIC_STRNLEN_USER
  1574. + select HAVE_PREEMPT_LAZY
  1575. select HAVE_MOD_ARCH_SPECIFIC
  1576. select MODULES_USE_ELF_RELA
  1577. select CLONE_BACKWARDS
  1578. @@ -319,7 +321,7 @@ menu "Kernel options"
  1579. config HIGHMEM
  1580. bool "High memory support"
  1581. - depends on PPC32
  1582. + depends on PPC32 && !PREEMPT_RT_FULL
  1583. source kernel/Kconfig.hz
  1584. source kernel/Kconfig.preempt
  1585. diff --git a/arch/powerpc/include/asm/kvm_host.h b/arch/powerpc/include/asm/kvm_host.h
  1586. index a92d95aee42d..20376580583f 100644
  1587. --- a/arch/powerpc/include/asm/kvm_host.h
  1588. +++ b/arch/powerpc/include/asm/kvm_host.h
  1589. @@ -286,7 +286,7 @@ struct kvmppc_vcore {
  1590. struct list_head runnable_threads;
  1591. struct list_head preempt_list;
  1592. spinlock_t lock;
  1593. - wait_queue_head_t wq;
  1594. + struct swait_queue_head wq;
  1595. spinlock_t stoltb_lock; /* protects stolen_tb and preempt_tb */
  1596. u64 stolen_tb;
  1597. u64 preempt_tb;
  1598. @@ -627,7 +627,7 @@ struct kvm_vcpu_arch {
  1599. u8 prodded;
  1600. u32 last_inst;
  1601. - wait_queue_head_t *wqp;
  1602. + struct swait_queue_head *wqp;
  1603. struct kvmppc_vcore *vcore;
  1604. int ret;
  1605. int trap;
  1606. diff --git a/arch/powerpc/include/asm/thread_info.h b/arch/powerpc/include/asm/thread_info.h
  1607. index 7efee4a3240b..40e6fa1b85b2 100644
  1608. --- a/arch/powerpc/include/asm/thread_info.h
  1609. +++ b/arch/powerpc/include/asm/thread_info.h
  1610. @@ -42,6 +42,8 @@ struct thread_info {
  1611. int cpu; /* cpu we're on */
  1612. int preempt_count; /* 0 => preemptable,
  1613. <0 => BUG */
  1614. + int preempt_lazy_count; /* 0 => preemptable,
  1615. + <0 => BUG */
  1616. unsigned long local_flags; /* private flags for thread */
  1617. /* low level flags - has atomic operations done on it */
  1618. @@ -82,8 +84,7 @@ static inline struct thread_info *current_thread_info(void)
  1619. #define TIF_SYSCALL_TRACE 0 /* syscall trace active */
  1620. #define TIF_SIGPENDING 1 /* signal pending */
  1621. #define TIF_NEED_RESCHED 2 /* rescheduling necessary */
  1622. -#define TIF_POLLING_NRFLAG 3 /* true if poll_idle() is polling
  1623. - TIF_NEED_RESCHED */
  1624. +#define TIF_NEED_RESCHED_LAZY 3 /* lazy rescheduling necessary */
  1625. #define TIF_32BIT 4 /* 32 bit binary */
  1626. #define TIF_RESTORE_TM 5 /* need to restore TM FP/VEC/VSX */
  1627. #define TIF_SYSCALL_AUDIT 7 /* syscall auditing active */
  1628. @@ -101,6 +102,8 @@ static inline struct thread_info *current_thread_info(void)
  1629. #if defined(CONFIG_PPC64)
  1630. #define TIF_ELF2ABI 18 /* function descriptors must die! */
  1631. #endif
  1632. +#define TIF_POLLING_NRFLAG 19 /* true if poll_idle() is polling
  1633. + TIF_NEED_RESCHED */
  1634. /* as above, but as bit values */
  1635. #define _TIF_SYSCALL_TRACE (1<<TIF_SYSCALL_TRACE)
  1636. @@ -119,14 +122,16 @@ static inline struct thread_info *current_thread_info(void)
  1637. #define _TIF_SYSCALL_TRACEPOINT (1<<TIF_SYSCALL_TRACEPOINT)
  1638. #define _TIF_EMULATE_STACK_STORE (1<<TIF_EMULATE_STACK_STORE)
  1639. #define _TIF_NOHZ (1<<TIF_NOHZ)
  1640. +#define _TIF_NEED_RESCHED_LAZY (1<<TIF_NEED_RESCHED_LAZY)
  1641. #define _TIF_SYSCALL_DOTRACE (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | \
  1642. _TIF_SECCOMP | _TIF_SYSCALL_TRACEPOINT | \
  1643. _TIF_NOHZ)
  1644. #define _TIF_USER_WORK_MASK (_TIF_SIGPENDING | _TIF_NEED_RESCHED | \
  1645. _TIF_NOTIFY_RESUME | _TIF_UPROBE | \
  1646. - _TIF_RESTORE_TM)
  1647. + _TIF_RESTORE_TM | _TIF_NEED_RESCHED_LAZY)
  1648. #define _TIF_PERSYSCALL_MASK (_TIF_RESTOREALL|_TIF_NOERROR)
  1649. +#define _TIF_NEED_RESCHED_MASK (_TIF_NEED_RESCHED | _TIF_NEED_RESCHED_LAZY)
  1650. /* Bits in local_flags */
  1651. /* Don't move TLF_NAPPING without adjusting the code in entry_32.S */
  1652. diff --git a/arch/powerpc/kernel/asm-offsets.c b/arch/powerpc/kernel/asm-offsets.c
  1653. index 40da69163d51..bd040815334b 100644
  1654. --- a/arch/powerpc/kernel/asm-offsets.c
  1655. +++ b/arch/powerpc/kernel/asm-offsets.c
  1656. @@ -160,6 +160,7 @@ int main(void)
  1657. DEFINE(TI_FLAGS, offsetof(struct thread_info, flags));
  1658. DEFINE(TI_LOCAL_FLAGS, offsetof(struct thread_info, local_flags));
  1659. DEFINE(TI_PREEMPT, offsetof(struct thread_info, preempt_count));
  1660. + DEFINE(TI_PREEMPT_LAZY, offsetof(struct thread_info, preempt_lazy_count));
  1661. DEFINE(TI_TASK, offsetof(struct thread_info, task));
  1662. DEFINE(TI_CPU, offsetof(struct thread_info, cpu));
  1663. diff --git a/arch/powerpc/kernel/entry_32.S b/arch/powerpc/kernel/entry_32.S
  1664. index 2405631e91a2..c21b4b42eaa0 100644
  1665. --- a/arch/powerpc/kernel/entry_32.S
  1666. +++ b/arch/powerpc/kernel/entry_32.S
  1667. @@ -818,7 +818,14 @@ resume_kernel:
  1668. cmpwi 0,r0,0 /* if non-zero, just restore regs and return */
  1669. bne restore
  1670. andi. r8,r8,_TIF_NEED_RESCHED
  1671. + bne+ 1f
  1672. + lwz r0,TI_PREEMPT_LAZY(r9)
  1673. + cmpwi 0,r0,0 /* if non-zero, just restore regs and return */
  1674. + bne restore
  1675. + lwz r0,TI_FLAGS(r9)
  1676. + andi. r0,r0,_TIF_NEED_RESCHED_LAZY
  1677. beq+ restore
  1678. +1:
  1679. lwz r3,_MSR(r1)
  1680. andi. r0,r3,MSR_EE /* interrupts off? */
  1681. beq restore /* don't schedule if so */
  1682. @@ -829,11 +836,11 @@ resume_kernel:
  1683. */
  1684. bl trace_hardirqs_off
  1685. #endif
  1686. -1: bl preempt_schedule_irq
  1687. +2: bl preempt_schedule_irq
  1688. CURRENT_THREAD_INFO(r9, r1)
  1689. lwz r3,TI_FLAGS(r9)
  1690. - andi. r0,r3,_TIF_NEED_RESCHED
  1691. - bne- 1b
  1692. + andi. r0,r3,_TIF_NEED_RESCHED_MASK
  1693. + bne- 2b
  1694. #ifdef CONFIG_TRACE_IRQFLAGS
  1695. /* And now, to properly rebalance the above, we tell lockdep they
  1696. * are being turned back on, which will happen when we return
  1697. @@ -1154,7 +1161,7 @@ global_dbcr0:
  1698. #endif /* !(CONFIG_4xx || CONFIG_BOOKE) */
  1699. do_work: /* r10 contains MSR_KERNEL here */
  1700. - andi. r0,r9,_TIF_NEED_RESCHED
  1701. + andi. r0,r9,_TIF_NEED_RESCHED_MASK
  1702. beq do_user_signal
  1703. do_resched: /* r10 contains MSR_KERNEL here */
  1704. @@ -1175,7 +1182,7 @@ recheck:
  1705. MTMSRD(r10) /* disable interrupts */
  1706. CURRENT_THREAD_INFO(r9, r1)
  1707. lwz r9,TI_FLAGS(r9)
  1708. - andi. r0,r9,_TIF_NEED_RESCHED
  1709. + andi. r0,r9,_TIF_NEED_RESCHED_MASK
  1710. bne- do_resched
  1711. andi. r0,r9,_TIF_USER_WORK_MASK
  1712. beq restore_user
  1713. diff --git a/arch/powerpc/kernel/entry_64.S b/arch/powerpc/kernel/entry_64.S
  1714. index f6fd0332c3a2..96235fe0a581 100644
  1715. --- a/arch/powerpc/kernel/entry_64.S
  1716. +++ b/arch/powerpc/kernel/entry_64.S
  1717. @@ -683,7 +683,7 @@ _GLOBAL(ret_from_except_lite)
  1718. #else
  1719. beq restore
  1720. #endif
  1721. -1: andi. r0,r4,_TIF_NEED_RESCHED
  1722. +1: andi. r0,r4,_TIF_NEED_RESCHED_MASK
  1723. beq 2f
  1724. bl restore_interrupts
  1725. SCHEDULE_USER
  1726. @@ -745,10 +745,18 @@ resume_kernel:
  1727. #ifdef CONFIG_PREEMPT
  1728. /* Check if we need to preempt */
  1729. + lwz r8,TI_PREEMPT(r9)
  1730. + cmpwi 0,r8,0 /* if non-zero, just restore regs and return */
  1731. + bne restore
  1732. andi. r0,r4,_TIF_NEED_RESCHED
  1733. + bne+ check_count
  1734. +
  1735. + andi. r0,r4,_TIF_NEED_RESCHED_LAZY
  1736. beq+ restore
  1737. + lwz r8,TI_PREEMPT_LAZY(r9)
  1738. +
  1739. /* Check that preempt_count() == 0 and interrupts are enabled */
  1740. - lwz r8,TI_PREEMPT(r9)
  1741. +check_count:
  1742. cmpwi cr1,r8,0
  1743. ld r0,SOFTE(r1)
  1744. cmpdi r0,0
  1745. @@ -765,7 +773,7 @@ resume_kernel:
  1746. /* Re-test flags and eventually loop */
  1747. CURRENT_THREAD_INFO(r9, r1)
  1748. ld r4,TI_FLAGS(r9)
  1749. - andi. r0,r4,_TIF_NEED_RESCHED
  1750. + andi. r0,r4,_TIF_NEED_RESCHED_MASK
  1751. bne 1b
  1752. /*
  1753. diff --git a/arch/powerpc/kernel/irq.c b/arch/powerpc/kernel/irq.c
  1754. index 290559df1e8b..070afa6da35d 100644
  1755. --- a/arch/powerpc/kernel/irq.c
  1756. +++ b/arch/powerpc/kernel/irq.c
  1757. @@ -614,6 +614,7 @@ void irq_ctx_init(void)
  1758. }
  1759. }
  1760. +#ifndef CONFIG_PREEMPT_RT_FULL
  1761. void do_softirq_own_stack(void)
  1762. {
  1763. struct thread_info *curtp, *irqtp;
  1764. @@ -631,6 +632,7 @@ void do_softirq_own_stack(void)
  1765. if (irqtp->flags)
  1766. set_bits(irqtp->flags, &curtp->flags);
  1767. }
  1768. +#endif
  1769. irq_hw_number_t virq_to_hw(unsigned int virq)
  1770. {
  1771. diff --git a/arch/powerpc/kernel/misc_32.S b/arch/powerpc/kernel/misc_32.S
  1772. index df4efa304b2c..9cb0c2f6e7ac 100644
  1773. --- a/arch/powerpc/kernel/misc_32.S
  1774. +++ b/arch/powerpc/kernel/misc_32.S
  1775. @@ -40,6 +40,7 @@
  1776. * We store the saved ksp_limit in the unused part
  1777. * of the STACK_FRAME_OVERHEAD
  1778. */
  1779. +#ifndef CONFIG_PREEMPT_RT_FULL
  1780. _GLOBAL(call_do_softirq)
  1781. mflr r0
  1782. stw r0,4(r1)
  1783. @@ -56,6 +57,7 @@ _GLOBAL(call_do_softirq)
  1784. stw r10,THREAD+KSP_LIMIT(r2)
  1785. mtlr r0
  1786. blr
  1787. +#endif
  1788. /*
  1789. * void call_do_irq(struct pt_regs *regs, struct thread_info *irqtp);
  1790. diff --git a/arch/powerpc/kernel/misc_64.S b/arch/powerpc/kernel/misc_64.S
  1791. index db475d41b57a..96b7ef80e05d 100644
  1792. --- a/arch/powerpc/kernel/misc_64.S
  1793. +++ b/arch/powerpc/kernel/misc_64.S
  1794. @@ -30,6 +30,7 @@
  1795. .text
  1796. +#ifndef CONFIG_PREEMPT_RT_FULL
  1797. _GLOBAL(call_do_softirq)
  1798. mflr r0
  1799. std r0,16(r1)
  1800. @@ -40,6 +41,7 @@ _GLOBAL(call_do_softirq)
  1801. ld r0,16(r1)
  1802. mtlr r0
  1803. blr
  1804. +#endif
  1805. _GLOBAL(call_do_irq)
  1806. mflr r0
  1807. diff --git a/arch/powerpc/kvm/Kconfig b/arch/powerpc/kvm/Kconfig
  1808. index c2024ac9d4e8..2303788da7e1 100644
  1809. --- a/arch/powerpc/kvm/Kconfig
  1810. +++ b/arch/powerpc/kvm/Kconfig
  1811. @@ -172,6 +172,7 @@ config KVM_E500MC
  1812. config KVM_MPIC
  1813. bool "KVM in-kernel MPIC emulation"
  1814. depends on KVM && E500
  1815. + depends on !PREEMPT_RT_FULL
  1816. select HAVE_KVM_IRQCHIP
  1817. select HAVE_KVM_IRQFD
  1818. select HAVE_KVM_IRQ_ROUTING
  1819. diff --git a/arch/powerpc/kvm/book3s_hv.c b/arch/powerpc/kvm/book3s_hv.c
  1820. index 3c3a367b6e59..2ac7a8b108ac 100644
  1821. --- a/arch/powerpc/kvm/book3s_hv.c
  1822. +++ b/arch/powerpc/kvm/book3s_hv.c
  1823. @@ -114,11 +114,11 @@ static bool kvmppc_ipi_thread(int cpu)
  1824. static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
  1825. {
  1826. int cpu;
  1827. - wait_queue_head_t *wqp;
  1828. + struct swait_queue_head *wqp;
  1829. wqp = kvm_arch_vcpu_wq(vcpu);
  1830. - if (waitqueue_active(wqp)) {
  1831. - wake_up_interruptible(wqp);
  1832. + if (swait_active(wqp)) {
  1833. + swake_up(wqp);
  1834. ++vcpu->stat.halt_wakeup;
  1835. }
  1836. @@ -707,8 +707,8 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
  1837. tvcpu->arch.prodded = 1;
  1838. smp_mb();
  1839. if (vcpu->arch.ceded) {
  1840. - if (waitqueue_active(&vcpu->wq)) {
  1841. - wake_up_interruptible(&vcpu->wq);
  1842. + if (swait_active(&vcpu->wq)) {
  1843. + swake_up(&vcpu->wq);
  1844. vcpu->stat.halt_wakeup++;
  1845. }
  1846. }
  1847. @@ -1453,7 +1453,7 @@ static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int core)
  1848. INIT_LIST_HEAD(&vcore->runnable_threads);
  1849. spin_lock_init(&vcore->lock);
  1850. spin_lock_init(&vcore->stoltb_lock);
  1851. - init_waitqueue_head(&vcore->wq);
  1852. + init_swait_queue_head(&vcore->wq);
  1853. vcore->preempt_tb = TB_NIL;
  1854. vcore->lpcr = kvm->arch.lpcr;
  1855. vcore->first_vcpuid = core * threads_per_subcore;
  1856. @@ -2525,10 +2525,9 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
  1857. {
  1858. struct kvm_vcpu *vcpu;
  1859. int do_sleep = 1;
  1860. + DECLARE_SWAITQUEUE(wait);
  1861. - DEFINE_WAIT(wait);
  1862. -
  1863. - prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
  1864. + prepare_to_swait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
  1865. /*
  1866. * Check one last time for pending exceptions and ceded state after
  1867. @@ -2542,7 +2541,7 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
  1868. }
  1869. if (!do_sleep) {
  1870. - finish_wait(&vc->wq, &wait);
  1871. + finish_swait(&vc->wq, &wait);
  1872. return;
  1873. }
  1874. @@ -2550,7 +2549,7 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
  1875. trace_kvmppc_vcore_blocked(vc, 0);
  1876. spin_unlock(&vc->lock);
  1877. schedule();
  1878. - finish_wait(&vc->wq, &wait);
  1879. + finish_swait(&vc->wq, &wait);
  1880. spin_lock(&vc->lock);
  1881. vc->vcore_state = VCORE_INACTIVE;
  1882. trace_kvmppc_vcore_blocked(vc, 1);
  1883. @@ -2606,7 +2605,7 @@ static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
  1884. kvmppc_start_thread(vcpu, vc);
  1885. trace_kvm_guest_enter(vcpu);
  1886. } else if (vc->vcore_state == VCORE_SLEEPING) {
  1887. - wake_up(&vc->wq);
  1888. + swake_up(&vc->wq);
  1889. }
  1890. }
  1891. diff --git a/arch/powerpc/platforms/ps3/device-init.c b/arch/powerpc/platforms/ps3/device-init.c
  1892. index 3f175e8aedb4..c4c02f91904c 100644
  1893. --- a/arch/powerpc/platforms/ps3/device-init.c
  1894. +++ b/arch/powerpc/platforms/ps3/device-init.c
  1895. @@ -752,7 +752,7 @@ static int ps3_notification_read_write(struct ps3_notification_device *dev,
  1896. }
  1897. pr_debug("%s:%u: notification %s issued\n", __func__, __LINE__, op);
  1898. - res = wait_event_interruptible(dev->done.wait,
  1899. + res = swait_event_interruptible(dev->done.wait,
  1900. dev->done.done || kthread_should_stop());
  1901. if (kthread_should_stop())
  1902. res = -EINTR;
  1903. diff --git a/arch/s390/include/asm/kvm_host.h b/arch/s390/include/asm/kvm_host.h
  1904. index e9a983f40a24..bbdc539fb3c6 100644
  1905. --- a/arch/s390/include/asm/kvm_host.h
  1906. +++ b/arch/s390/include/asm/kvm_host.h
  1907. @@ -427,7 +427,7 @@ struct kvm_s390_irq_payload {
  1908. struct kvm_s390_local_interrupt {
  1909. spinlock_t lock;
  1910. struct kvm_s390_float_interrupt *float_int;
  1911. - wait_queue_head_t *wq;
  1912. + struct swait_queue_head *wq;
  1913. atomic_t *cpuflags;
  1914. DECLARE_BITMAP(sigp_emerg_pending, KVM_MAX_VCPUS);
  1915. struct kvm_s390_irq_payload irq;
  1916. diff --git a/arch/s390/kvm/interrupt.c b/arch/s390/kvm/interrupt.c
  1917. index 6a75352f453c..cc862c486002 100644
  1918. --- a/arch/s390/kvm/interrupt.c
  1919. +++ b/arch/s390/kvm/interrupt.c
  1920. @@ -868,13 +868,13 @@ no_timer:
  1921. void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu)
  1922. {
  1923. - if (waitqueue_active(&vcpu->wq)) {
  1924. + if (swait_active(&vcpu->wq)) {
  1925. /*
  1926. * The vcpu gave up the cpu voluntarily, mark it as a good
  1927. * yield-candidate.
  1928. */
  1929. vcpu->preempted = true;
  1930. - wake_up_interruptible(&vcpu->wq);
  1931. + swake_up(&vcpu->wq);
  1932. vcpu->stat.halt_wakeup++;
  1933. }
  1934. }
  1935. diff --git a/arch/sh/kernel/irq.c b/arch/sh/kernel/irq.c
  1936. index 6c0378c0b8b5..abd58b4dff97 100644
  1937. --- a/arch/sh/kernel/irq.c
  1938. +++ b/arch/sh/kernel/irq.c
  1939. @@ -147,6 +147,7 @@ void irq_ctx_exit(int cpu)
  1940. hardirq_ctx[cpu] = NULL;
  1941. }
  1942. +#ifndef CONFIG_PREEMPT_RT_FULL
  1943. void do_softirq_own_stack(void)
  1944. {
  1945. struct thread_info *curctx;
  1946. @@ -174,6 +175,7 @@ void do_softirq_own_stack(void)
  1947. "r5", "r6", "r7", "r8", "r9", "r15", "t", "pr"
  1948. );
  1949. }
  1950. +#endif
  1951. #else
  1952. static inline void handle_one_irq(unsigned int irq)
  1953. {
  1954. diff --git a/arch/sparc/Kconfig b/arch/sparc/Kconfig
  1955. index 56442d2d7bbc..8c9598f534c9 100644
  1956. --- a/arch/sparc/Kconfig
  1957. +++ b/arch/sparc/Kconfig
  1958. @@ -189,12 +189,10 @@ config NR_CPUS
  1959. source kernel/Kconfig.hz
  1960. config RWSEM_GENERIC_SPINLOCK
  1961. - bool
  1962. - default y if SPARC32
  1963. + def_bool PREEMPT_RT_FULL
  1964. config RWSEM_XCHGADD_ALGORITHM
  1965. - bool
  1966. - default y if SPARC64
  1967. + def_bool !RWSEM_GENERIC_SPINLOCK && !PREEMPT_RT_FULL
  1968. config GENERIC_HWEIGHT
  1969. bool
  1970. diff --git a/arch/sparc/kernel/irq_64.c b/arch/sparc/kernel/irq_64.c
  1971. index e22416ce56ea..d359de71153a 100644
  1972. --- a/arch/sparc/kernel/irq_64.c
  1973. +++ b/arch/sparc/kernel/irq_64.c
  1974. @@ -854,6 +854,7 @@ void __irq_entry handler_irq(int pil, struct pt_regs *regs)
  1975. set_irq_regs(old_regs);
  1976. }
  1977. +#ifndef CONFIG_PREEMPT_RT_FULL
  1978. void do_softirq_own_stack(void)
  1979. {
  1980. void *orig_sp, *sp = softirq_stack[smp_processor_id()];
  1981. @@ -868,6 +869,7 @@ void do_softirq_own_stack(void)
  1982. __asm__ __volatile__("mov %0, %%sp"
  1983. : : "r" (orig_sp));
  1984. }
  1985. +#endif
  1986. #ifdef CONFIG_HOTPLUG_CPU
  1987. void fixup_irqs(void)
  1988. diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
  1989. index 436639a31624..6ee1dd0deadc 100644
  1990. --- a/arch/x86/Kconfig
  1991. +++ b/arch/x86/Kconfig
  1992. @@ -17,6 +17,7 @@ config X86_64
  1993. ### Arch settings
  1994. config X86
  1995. def_bool y
  1996. + select HAVE_PREEMPT_LAZY
  1997. select ACPI_LEGACY_TABLES_LOOKUP if ACPI
  1998. select ACPI_SYSTEM_POWER_STATES_SUPPORT if ACPI
  1999. select ANON_INODES
  2000. @@ -212,8 +213,11 @@ config ARCH_MAY_HAVE_PC_FDC
  2001. def_bool y
  2002. depends on ISA_DMA_API
  2003. +config RWSEM_GENERIC_SPINLOCK
  2004. + def_bool PREEMPT_RT_FULL
  2005. +
  2006. config RWSEM_XCHGADD_ALGORITHM
  2007. - def_bool y
  2008. + def_bool !RWSEM_GENERIC_SPINLOCK && !PREEMPT_RT_FULL
  2009. config GENERIC_CALIBRATE_DELAY
  2010. def_bool y
  2011. @@ -848,7 +852,7 @@ config IOMMU_HELPER
  2012. config MAXSMP
  2013. bool "Enable Maximum number of SMP Processors and NUMA Nodes"
  2014. depends on X86_64 && SMP && DEBUG_KERNEL
  2015. - select CPUMASK_OFFSTACK
  2016. + select CPUMASK_OFFSTACK if !PREEMPT_RT_FULL
  2017. ---help---
  2018. Enable maximum number of CPUS and NUMA Nodes for this architecture.
  2019. If unsure, say N.
  2020. diff --git a/arch/x86/crypto/aesni-intel_glue.c b/arch/x86/crypto/aesni-intel_glue.c
  2021. index 3633ad6145c5..c6d5458ee7f9 100644
  2022. --- a/arch/x86/crypto/aesni-intel_glue.c
  2023. +++ b/arch/x86/crypto/aesni-intel_glue.c
  2024. @@ -383,14 +383,14 @@ static int ecb_encrypt(struct blkcipher_desc *desc,
  2025. err = blkcipher_walk_virt(desc, &walk);
  2026. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  2027. - kernel_fpu_begin();
  2028. while ((nbytes = walk.nbytes)) {
  2029. + kernel_fpu_begin();
  2030. aesni_ecb_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  2031. - nbytes & AES_BLOCK_MASK);
  2032. + nbytes & AES_BLOCK_MASK);
  2033. + kernel_fpu_end();
  2034. nbytes &= AES_BLOCK_SIZE - 1;
  2035. err = blkcipher_walk_done(desc, &walk, nbytes);
  2036. }
  2037. - kernel_fpu_end();
  2038. return err;
  2039. }
  2040. @@ -407,14 +407,14 @@ static int ecb_decrypt(struct blkcipher_desc *desc,
  2041. err = blkcipher_walk_virt(desc, &walk);
  2042. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  2043. - kernel_fpu_begin();
  2044. while ((nbytes = walk.nbytes)) {
  2045. + kernel_fpu_begin();
  2046. aesni_ecb_dec(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  2047. nbytes & AES_BLOCK_MASK);
  2048. + kernel_fpu_end();
  2049. nbytes &= AES_BLOCK_SIZE - 1;
  2050. err = blkcipher_walk_done(desc, &walk, nbytes);
  2051. }
  2052. - kernel_fpu_end();
  2053. return err;
  2054. }
  2055. @@ -431,14 +431,14 @@ static int cbc_encrypt(struct blkcipher_desc *desc,
  2056. err = blkcipher_walk_virt(desc, &walk);
  2057. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  2058. - kernel_fpu_begin();
  2059. while ((nbytes = walk.nbytes)) {
  2060. + kernel_fpu_begin();
  2061. aesni_cbc_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  2062. nbytes & AES_BLOCK_MASK, walk.iv);
  2063. + kernel_fpu_end();
  2064. nbytes &= AES_BLOCK_SIZE - 1;
  2065. err = blkcipher_walk_done(desc, &walk, nbytes);
  2066. }
  2067. - kernel_fpu_end();
  2068. return err;
  2069. }
  2070. @@ -455,14 +455,14 @@ static int cbc_decrypt(struct blkcipher_desc *desc,
  2071. err = blkcipher_walk_virt(desc, &walk);
  2072. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  2073. - kernel_fpu_begin();
  2074. while ((nbytes = walk.nbytes)) {
  2075. + kernel_fpu_begin();
  2076. aesni_cbc_dec(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  2077. nbytes & AES_BLOCK_MASK, walk.iv);
  2078. + kernel_fpu_end();
  2079. nbytes &= AES_BLOCK_SIZE - 1;
  2080. err = blkcipher_walk_done(desc, &walk, nbytes);
  2081. }
  2082. - kernel_fpu_end();
  2083. return err;
  2084. }
  2085. @@ -514,18 +514,20 @@ static int ctr_crypt(struct blkcipher_desc *desc,
  2086. err = blkcipher_walk_virt_block(desc, &walk, AES_BLOCK_SIZE);
  2087. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  2088. - kernel_fpu_begin();
  2089. while ((nbytes = walk.nbytes) >= AES_BLOCK_SIZE) {
  2090. + kernel_fpu_begin();
  2091. aesni_ctr_enc_tfm(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  2092. nbytes & AES_BLOCK_MASK, walk.iv);
  2093. + kernel_fpu_end();
  2094. nbytes &= AES_BLOCK_SIZE - 1;
  2095. err = blkcipher_walk_done(desc, &walk, nbytes);
  2096. }
  2097. if (walk.nbytes) {
  2098. + kernel_fpu_begin();
  2099. ctr_crypt_final(ctx, &walk);
  2100. + kernel_fpu_end();
  2101. err = blkcipher_walk_done(desc, &walk, 0);
  2102. }
  2103. - kernel_fpu_end();
  2104. return err;
  2105. }
  2106. diff --git a/arch/x86/crypto/cast5_avx_glue.c b/arch/x86/crypto/cast5_avx_glue.c
  2107. index 8648158f3916..d7699130ee36 100644
  2108. --- a/arch/x86/crypto/cast5_avx_glue.c
  2109. +++ b/arch/x86/crypto/cast5_avx_glue.c
  2110. @@ -59,7 +59,7 @@ static inline void cast5_fpu_end(bool fpu_enabled)
  2111. static int ecb_crypt(struct blkcipher_desc *desc, struct blkcipher_walk *walk,
  2112. bool enc)
  2113. {
  2114. - bool fpu_enabled = false;
  2115. + bool fpu_enabled;
  2116. struct cast5_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  2117. const unsigned int bsize = CAST5_BLOCK_SIZE;
  2118. unsigned int nbytes;
  2119. @@ -75,7 +75,7 @@ static int ecb_crypt(struct blkcipher_desc *desc, struct blkcipher_walk *walk,
  2120. u8 *wsrc = walk->src.virt.addr;
  2121. u8 *wdst = walk->dst.virt.addr;
  2122. - fpu_enabled = cast5_fpu_begin(fpu_enabled, nbytes);
  2123. + fpu_enabled = cast5_fpu_begin(false, nbytes);
  2124. /* Process multi-block batch */
  2125. if (nbytes >= bsize * CAST5_PARALLEL_BLOCKS) {
  2126. @@ -103,10 +103,9 @@ static int ecb_crypt(struct blkcipher_desc *desc, struct blkcipher_walk *walk,
  2127. } while (nbytes >= bsize);
  2128. done:
  2129. + cast5_fpu_end(fpu_enabled);
  2130. err = blkcipher_walk_done(desc, walk, nbytes);
  2131. }
  2132. -
  2133. - cast5_fpu_end(fpu_enabled);
  2134. return err;
  2135. }
  2136. @@ -227,7 +226,7 @@ done:
  2137. static int cbc_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  2138. struct scatterlist *src, unsigned int nbytes)
  2139. {
  2140. - bool fpu_enabled = false;
  2141. + bool fpu_enabled;
  2142. struct blkcipher_walk walk;
  2143. int err;
  2144. @@ -236,12 +235,11 @@ static int cbc_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  2145. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  2146. while ((nbytes = walk.nbytes)) {
  2147. - fpu_enabled = cast5_fpu_begin(fpu_enabled, nbytes);
  2148. + fpu_enabled = cast5_fpu_begin(false, nbytes);
  2149. nbytes = __cbc_decrypt(desc, &walk);
  2150. + cast5_fpu_end(fpu_enabled);
  2151. err = blkcipher_walk_done(desc, &walk, nbytes);
  2152. }
  2153. -
  2154. - cast5_fpu_end(fpu_enabled);
  2155. return err;
  2156. }
  2157. @@ -311,7 +309,7 @@ done:
  2158. static int ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  2159. struct scatterlist *src, unsigned int nbytes)
  2160. {
  2161. - bool fpu_enabled = false;
  2162. + bool fpu_enabled;
  2163. struct blkcipher_walk walk;
  2164. int err;
  2165. @@ -320,13 +318,12 @@ static int ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  2166. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  2167. while ((nbytes = walk.nbytes) >= CAST5_BLOCK_SIZE) {
  2168. - fpu_enabled = cast5_fpu_begin(fpu_enabled, nbytes);
  2169. + fpu_enabled = cast5_fpu_begin(false, nbytes);
  2170. nbytes = __ctr_crypt(desc, &walk);
  2171. + cast5_fpu_end(fpu_enabled);
  2172. err = blkcipher_walk_done(desc, &walk, nbytes);
  2173. }
  2174. - cast5_fpu_end(fpu_enabled);
  2175. -
  2176. if (walk.nbytes) {
  2177. ctr_crypt_final(desc, &walk);
  2178. err = blkcipher_walk_done(desc, &walk, 0);
  2179. diff --git a/arch/x86/crypto/glue_helper.c b/arch/x86/crypto/glue_helper.c
  2180. index 6a85598931b5..3a506ce7ed93 100644
  2181. --- a/arch/x86/crypto/glue_helper.c
  2182. +++ b/arch/x86/crypto/glue_helper.c
  2183. @@ -39,7 +39,7 @@ static int __glue_ecb_crypt_128bit(const struct common_glue_ctx *gctx,
  2184. void *ctx = crypto_blkcipher_ctx(desc->tfm);
  2185. const unsigned int bsize = 128 / 8;
  2186. unsigned int nbytes, i, func_bytes;
  2187. - bool fpu_enabled = false;
  2188. + bool fpu_enabled;
  2189. int err;
  2190. err = blkcipher_walk_virt(desc, walk);
  2191. @@ -49,7 +49,7 @@ static int __glue_ecb_crypt_128bit(const struct common_glue_ctx *gctx,
  2192. u8 *wdst = walk->dst.virt.addr;
  2193. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  2194. - desc, fpu_enabled, nbytes);
  2195. + desc, false, nbytes);
  2196. for (i = 0; i < gctx->num_funcs; i++) {
  2197. func_bytes = bsize * gctx->funcs[i].num_blocks;
  2198. @@ -71,10 +71,10 @@ static int __glue_ecb_crypt_128bit(const struct common_glue_ctx *gctx,
  2199. }
  2200. done:
  2201. + glue_fpu_end(fpu_enabled);
  2202. err = blkcipher_walk_done(desc, walk, nbytes);
  2203. }
  2204. - glue_fpu_end(fpu_enabled);
  2205. return err;
  2206. }
  2207. @@ -194,7 +194,7 @@ int glue_cbc_decrypt_128bit(const struct common_glue_ctx *gctx,
  2208. struct scatterlist *src, unsigned int nbytes)
  2209. {
  2210. const unsigned int bsize = 128 / 8;
  2211. - bool fpu_enabled = false;
  2212. + bool fpu_enabled;
  2213. struct blkcipher_walk walk;
  2214. int err;
  2215. @@ -203,12 +203,12 @@ int glue_cbc_decrypt_128bit(const struct common_glue_ctx *gctx,
  2216. while ((nbytes = walk.nbytes)) {
  2217. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  2218. - desc, fpu_enabled, nbytes);
  2219. + desc, false, nbytes);
  2220. nbytes = __glue_cbc_decrypt_128bit(gctx, desc, &walk);
  2221. + glue_fpu_end(fpu_enabled);
  2222. err = blkcipher_walk_done(desc, &walk, nbytes);
  2223. }
  2224. - glue_fpu_end(fpu_enabled);
  2225. return err;
  2226. }
  2227. EXPORT_SYMBOL_GPL(glue_cbc_decrypt_128bit);
  2228. @@ -277,7 +277,7 @@ int glue_ctr_crypt_128bit(const struct common_glue_ctx *gctx,
  2229. struct scatterlist *src, unsigned int nbytes)
  2230. {
  2231. const unsigned int bsize = 128 / 8;
  2232. - bool fpu_enabled = false;
  2233. + bool fpu_enabled;
  2234. struct blkcipher_walk walk;
  2235. int err;
  2236. @@ -286,13 +286,12 @@ int glue_ctr_crypt_128bit(const struct common_glue_ctx *gctx,
  2237. while ((nbytes = walk.nbytes) >= bsize) {
  2238. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  2239. - desc, fpu_enabled, nbytes);
  2240. + desc, false, nbytes);
  2241. nbytes = __glue_ctr_crypt_128bit(gctx, desc, &walk);
  2242. + glue_fpu_end(fpu_enabled);
  2243. err = blkcipher_walk_done(desc, &walk, nbytes);
  2244. }
  2245. - glue_fpu_end(fpu_enabled);
  2246. -
  2247. if (walk.nbytes) {
  2248. glue_ctr_crypt_final_128bit(
  2249. gctx->funcs[gctx->num_funcs - 1].fn_u.ctr, desc, &walk);
  2250. @@ -347,7 +346,7 @@ int glue_xts_crypt_128bit(const struct common_glue_ctx *gctx,
  2251. void *tweak_ctx, void *crypt_ctx)
  2252. {
  2253. const unsigned int bsize = 128 / 8;
  2254. - bool fpu_enabled = false;
  2255. + bool fpu_enabled;
  2256. struct blkcipher_walk walk;
  2257. int err;
  2258. @@ -360,21 +359,21 @@ int glue_xts_crypt_128bit(const struct common_glue_ctx *gctx,
  2259. /* set minimum length to bsize, for tweak_fn */
  2260. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  2261. - desc, fpu_enabled,
  2262. + desc, false,
  2263. nbytes < bsize ? bsize : nbytes);
  2264. -
  2265. /* calculate first value of T */
  2266. tweak_fn(tweak_ctx, walk.iv, walk.iv);
  2267. + glue_fpu_end(fpu_enabled);
  2268. while (nbytes) {
  2269. + fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  2270. + desc, false, nbytes);
  2271. nbytes = __glue_xts_crypt_128bit(gctx, crypt_ctx, desc, &walk);
  2272. + glue_fpu_end(fpu_enabled);
  2273. err = blkcipher_walk_done(desc, &walk, nbytes);
  2274. nbytes = walk.nbytes;
  2275. }
  2276. -
  2277. - glue_fpu_end(fpu_enabled);
  2278. -
  2279. return err;
  2280. }
  2281. EXPORT_SYMBOL_GPL(glue_xts_crypt_128bit);
  2282. diff --git a/arch/x86/entry/common.c b/arch/x86/entry/common.c
  2283. index 1a4477cedc49..75a301b6a5b6 100644
  2284. --- a/arch/x86/entry/common.c
  2285. +++ b/arch/x86/entry/common.c
  2286. @@ -220,7 +220,7 @@ long syscall_trace_enter(struct pt_regs *regs)
  2287. #define EXIT_TO_USERMODE_LOOP_FLAGS \
  2288. (_TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_UPROBE | \
  2289. - _TIF_NEED_RESCHED | _TIF_USER_RETURN_NOTIFY)
  2290. + _TIF_NEED_RESCHED_MASK | _TIF_USER_RETURN_NOTIFY)
  2291. static void exit_to_usermode_loop(struct pt_regs *regs, u32 cached_flags)
  2292. {
  2293. @@ -236,9 +236,16 @@ static void exit_to_usermode_loop(struct pt_regs *regs, u32 cached_flags)
  2294. /* We have work to do. */
  2295. local_irq_enable();
  2296. - if (cached_flags & _TIF_NEED_RESCHED)
  2297. + if (cached_flags & _TIF_NEED_RESCHED_MASK)
  2298. schedule();
  2299. +#ifdef ARCH_RT_DELAYS_SIGNAL_SEND
  2300. + if (unlikely(current->forced_info.si_signo)) {
  2301. + struct task_struct *t = current;
  2302. + force_sig_info(t->forced_info.si_signo, &t->forced_info, t);
  2303. + t->forced_info.si_signo = 0;
  2304. + }
  2305. +#endif
  2306. if (cached_flags & _TIF_UPROBE)
  2307. uprobe_notify_resume(regs);
  2308. diff --git a/arch/x86/entry/entry_32.S b/arch/x86/entry/entry_32.S
  2309. index ae678ad128a9..3bcef8bdb911 100644
  2310. --- a/arch/x86/entry/entry_32.S
  2311. +++ b/arch/x86/entry/entry_32.S
  2312. @@ -278,8 +278,24 @@ END(ret_from_exception)
  2313. ENTRY(resume_kernel)
  2314. DISABLE_INTERRUPTS(CLBR_ANY)
  2315. need_resched:
  2316. + # preempt count == 0 + NEED_RS set?
  2317. cmpl $0, PER_CPU_VAR(__preempt_count)
  2318. +#ifndef CONFIG_PREEMPT_LAZY
  2319. jnz restore_all
  2320. +#else
  2321. + jz test_int_off
  2322. +
  2323. + # atleast preempt count == 0 ?
  2324. + cmpl $_PREEMPT_ENABLED,PER_CPU_VAR(__preempt_count)
  2325. + jne restore_all
  2326. +
  2327. + cmpl $0,TI_preempt_lazy_count(%ebp) # non-zero preempt_lazy_count ?
  2328. + jnz restore_all
  2329. +
  2330. + testl $_TIF_NEED_RESCHED_LAZY, TI_flags(%ebp)
  2331. + jz restore_all
  2332. +test_int_off:
  2333. +#endif
  2334. testl $X86_EFLAGS_IF, PT_EFLAGS(%esp) # interrupts off (exception path) ?
  2335. jz restore_all
  2336. call preempt_schedule_irq
  2337. diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S
  2338. index a55697d19824..316081a2ca85 100644
  2339. --- a/arch/x86/entry/entry_64.S
  2340. +++ b/arch/x86/entry/entry_64.S
  2341. @@ -579,7 +579,23 @@ retint_kernel:
  2342. bt $9, EFLAGS(%rsp) /* were interrupts off? */
  2343. jnc 1f
  2344. 0: cmpl $0, PER_CPU_VAR(__preempt_count)
  2345. +#ifndef CONFIG_PREEMPT_LAZY
  2346. jnz 1f
  2347. +#else
  2348. + jz do_preempt_schedule_irq
  2349. +
  2350. + # atleast preempt count == 0 ?
  2351. + cmpl $_PREEMPT_ENABLED,PER_CPU_VAR(__preempt_count)
  2352. + jnz 1f
  2353. +
  2354. + GET_THREAD_INFO(%rcx)
  2355. + cmpl $0, TI_preempt_lazy_count(%rcx)
  2356. + jnz 1f
  2357. +
  2358. + bt $TIF_NEED_RESCHED_LAZY,TI_flags(%rcx)
  2359. + jnc 1f
  2360. +do_preempt_schedule_irq:
  2361. +#endif
  2362. call preempt_schedule_irq
  2363. jmp 0b
  2364. 1:
  2365. @@ -867,6 +883,7 @@ bad_gs:
  2366. jmp 2b
  2367. .previous
  2368. +#ifndef CONFIG_PREEMPT_RT_FULL
  2369. /* Call softirq on interrupt stack. Interrupts are off. */
  2370. ENTRY(do_softirq_own_stack)
  2371. pushq %rbp
  2372. @@ -879,6 +896,7 @@ ENTRY(do_softirq_own_stack)
  2373. decl PER_CPU_VAR(irq_count)
  2374. ret
  2375. END(do_softirq_own_stack)
  2376. +#endif
  2377. #ifdef CONFIG_XEN
  2378. idtentry xen_hypervisor_callback xen_do_hypervisor_callback has_error_code=0
  2379. diff --git a/arch/x86/include/asm/preempt.h b/arch/x86/include/asm/preempt.h
  2380. index 01bcde84d3e4..6f432adc55cd 100644
  2381. --- a/arch/x86/include/asm/preempt.h
  2382. +++ b/arch/x86/include/asm/preempt.h
  2383. @@ -79,17 +79,46 @@ static __always_inline void __preempt_count_sub(int val)
  2384. * a decrement which hits zero means we have no preempt_count and should
  2385. * reschedule.
  2386. */
  2387. -static __always_inline bool __preempt_count_dec_and_test(void)
  2388. +static __always_inline bool ____preempt_count_dec_and_test(void)
  2389. {
  2390. GEN_UNARY_RMWcc("decl", __preempt_count, __percpu_arg(0), "e");
  2391. }
  2392. +static __always_inline bool __preempt_count_dec_and_test(void)
  2393. +{
  2394. + if (____preempt_count_dec_and_test())
  2395. + return true;
  2396. +#ifdef CONFIG_PREEMPT_LAZY
  2397. + if (current_thread_info()->preempt_lazy_count)
  2398. + return false;
  2399. + return test_thread_flag(TIF_NEED_RESCHED_LAZY);
  2400. +#else
  2401. + return false;
  2402. +#endif
  2403. +}
  2404. +
  2405. /*
  2406. * Returns true when we need to resched and can (barring IRQ state).
  2407. */
  2408. static __always_inline bool should_resched(int preempt_offset)
  2409. {
  2410. +#ifdef CONFIG_PREEMPT_LAZY
  2411. + u32 tmp;
  2412. +
  2413. + tmp = raw_cpu_read_4(__preempt_count);
  2414. + if (tmp == preempt_offset)
  2415. + return true;
  2416. +
  2417. + /* preempt count == 0 ? */
  2418. + tmp &= ~PREEMPT_NEED_RESCHED;
  2419. + if (tmp)
  2420. + return false;
  2421. + if (current_thread_info()->preempt_lazy_count)
  2422. + return false;
  2423. + return test_thread_flag(TIF_NEED_RESCHED_LAZY);
  2424. +#else
  2425. return unlikely(raw_cpu_read_4(__preempt_count) == preempt_offset);
  2426. +#endif
  2427. }
  2428. #ifdef CONFIG_PREEMPT
  2429. diff --git a/arch/x86/include/asm/signal.h b/arch/x86/include/asm/signal.h
  2430. index 2138c9ae19ee..3f5b4ee2e2c1 100644
  2431. --- a/arch/x86/include/asm/signal.h
  2432. +++ b/arch/x86/include/asm/signal.h
  2433. @@ -23,6 +23,19 @@ typedef struct {
  2434. unsigned long sig[_NSIG_WORDS];
  2435. } sigset_t;
  2436. +/*
  2437. + * Because some traps use the IST stack, we must keep preemption
  2438. + * disabled while calling do_trap(), but do_trap() may call
  2439. + * force_sig_info() which will grab the signal spin_locks for the
  2440. + * task, which in PREEMPT_RT_FULL are mutexes. By defining
  2441. + * ARCH_RT_DELAYS_SIGNAL_SEND the force_sig_info() will set
  2442. + * TIF_NOTIFY_RESUME and set up the signal to be sent on exit of the
  2443. + * trap.
  2444. + */
  2445. +#if defined(CONFIG_PREEMPT_RT_FULL)
  2446. +#define ARCH_RT_DELAYS_SIGNAL_SEND
  2447. +#endif
  2448. +
  2449. #ifndef CONFIG_COMPAT
  2450. typedef sigset_t compat_sigset_t;
  2451. #endif
  2452. diff --git a/arch/x86/include/asm/stackprotector.h b/arch/x86/include/asm/stackprotector.h
  2453. index 58505f01962f..02fa39652cd6 100644
  2454. --- a/arch/x86/include/asm/stackprotector.h
  2455. +++ b/arch/x86/include/asm/stackprotector.h
  2456. @@ -59,7 +59,7 @@
  2457. */
  2458. static __always_inline void boot_init_stack_canary(void)
  2459. {
  2460. - u64 canary;
  2461. + u64 uninitialized_var(canary);
  2462. u64 tsc;
  2463. #ifdef CONFIG_X86_64
  2464. @@ -70,8 +70,15 @@ static __always_inline void boot_init_stack_canary(void)
  2465. * of randomness. The TSC only matters for very early init,
  2466. * there it already has some randomness on most systems. Later
  2467. * on during the bootup the random pool has true entropy too.
  2468. + *
  2469. + * For preempt-rt we need to weaken the randomness a bit, as
  2470. + * we can't call into the random generator from atomic context
  2471. + * due to locking constraints. We just leave canary
  2472. + * uninitialized and use the TSC based randomness on top of it.
  2473. */
  2474. +#ifndef CONFIG_PREEMPT_RT_FULL
  2475. get_random_bytes(&canary, sizeof(canary));
  2476. +#endif
  2477. tsc = rdtsc();
  2478. canary += tsc + (tsc << 32UL);
  2479. diff --git a/arch/x86/include/asm/thread_info.h b/arch/x86/include/asm/thread_info.h
  2480. index c7b551028740..ddb63bd90e3c 100644
  2481. --- a/arch/x86/include/asm/thread_info.h
  2482. +++ b/arch/x86/include/asm/thread_info.h
  2483. @@ -58,6 +58,8 @@ struct thread_info {
  2484. __u32 status; /* thread synchronous flags */
  2485. __u32 cpu; /* current CPU */
  2486. mm_segment_t addr_limit;
  2487. + int preempt_lazy_count; /* 0 => lazy preemptable
  2488. + <0 => BUG */
  2489. unsigned int sig_on_uaccess_error:1;
  2490. unsigned int uaccess_err:1; /* uaccess failed */
  2491. };
  2492. @@ -95,6 +97,7 @@ struct thread_info {
  2493. #define TIF_SYSCALL_EMU 6 /* syscall emulation active */
  2494. #define TIF_SYSCALL_AUDIT 7 /* syscall auditing active */
  2495. #define TIF_SECCOMP 8 /* secure computing */
  2496. +#define TIF_NEED_RESCHED_LAZY 9 /* lazy rescheduling necessary */
  2497. #define TIF_USER_RETURN_NOTIFY 11 /* notify kernel of userspace return */
  2498. #define TIF_UPROBE 12 /* breakpointed or singlestepping */
  2499. #define TIF_NOTSC 16 /* TSC is not accessible in userland */
  2500. @@ -119,6 +122,7 @@ struct thread_info {
  2501. #define _TIF_SYSCALL_EMU (1 << TIF_SYSCALL_EMU)
  2502. #define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
  2503. #define _TIF_SECCOMP (1 << TIF_SECCOMP)
  2504. +#define _TIF_NEED_RESCHED_LAZY (1 << TIF_NEED_RESCHED_LAZY)
  2505. #define _TIF_USER_RETURN_NOTIFY (1 << TIF_USER_RETURN_NOTIFY)
  2506. #define _TIF_UPROBE (1 << TIF_UPROBE)
  2507. #define _TIF_NOTSC (1 << TIF_NOTSC)
  2508. @@ -152,6 +156,8 @@ struct thread_info {
  2509. #define _TIF_WORK_CTXSW_PREV (_TIF_WORK_CTXSW|_TIF_USER_RETURN_NOTIFY)
  2510. #define _TIF_WORK_CTXSW_NEXT (_TIF_WORK_CTXSW)
  2511. +#define _TIF_NEED_RESCHED_MASK (_TIF_NEED_RESCHED | _TIF_NEED_RESCHED_LAZY)
  2512. +
  2513. #define STACK_WARN (THREAD_SIZE/8)
  2514. /*
  2515. diff --git a/arch/x86/include/asm/uv/uv_bau.h b/arch/x86/include/asm/uv/uv_bau.h
  2516. index fc808b83fccb..ebb40118abf5 100644
  2517. --- a/arch/x86/include/asm/uv/uv_bau.h
  2518. +++ b/arch/x86/include/asm/uv/uv_bau.h
  2519. @@ -615,9 +615,9 @@ struct bau_control {
  2520. cycles_t send_message;
  2521. cycles_t period_end;
  2522. cycles_t period_time;
  2523. - spinlock_t uvhub_lock;
  2524. - spinlock_t queue_lock;
  2525. - spinlock_t disable_lock;
  2526. + raw_spinlock_t uvhub_lock;
  2527. + raw_spinlock_t queue_lock;
  2528. + raw_spinlock_t disable_lock;
  2529. /* tunables */
  2530. int max_concurr;
  2531. int max_concurr_const;
  2532. @@ -776,15 +776,15 @@ static inline int atom_asr(short i, struct atomic_short *v)
  2533. * to be lowered below the current 'v'. atomic_add_unless can only stop
  2534. * on equal.
  2535. */
  2536. -static inline int atomic_inc_unless_ge(spinlock_t *lock, atomic_t *v, int u)
  2537. +static inline int atomic_inc_unless_ge(raw_spinlock_t *lock, atomic_t *v, int u)
  2538. {
  2539. - spin_lock(lock);
  2540. + raw_spin_lock(lock);
  2541. if (atomic_read(v) >= u) {
  2542. - spin_unlock(lock);
  2543. + raw_spin_unlock(lock);
  2544. return 0;
  2545. }
  2546. atomic_inc(v);
  2547. - spin_unlock(lock);
  2548. + raw_spin_unlock(lock);
  2549. return 1;
  2550. }
  2551. diff --git a/arch/x86/include/asm/uv/uv_hub.h b/arch/x86/include/asm/uv/uv_hub.h
  2552. index ea7074784cc4..01ec643ce66e 100644
  2553. --- a/arch/x86/include/asm/uv/uv_hub.h
  2554. +++ b/arch/x86/include/asm/uv/uv_hub.h
  2555. @@ -492,7 +492,7 @@ struct uv_blade_info {
  2556. unsigned short nr_online_cpus;
  2557. unsigned short pnode;
  2558. short memory_nid;
  2559. - spinlock_t nmi_lock; /* obsolete, see uv_hub_nmi */
  2560. + raw_spinlock_t nmi_lock; /* obsolete, see uv_hub_nmi */
  2561. unsigned long nmi_count; /* obsolete, see uv_hub_nmi */
  2562. };
  2563. extern struct uv_blade_info *uv_blade_info;
  2564. diff --git a/arch/x86/kernel/acpi/boot.c b/arch/x86/kernel/acpi/boot.c
  2565. index e75907601a41..a29fc4f84fc4 100644
  2566. --- a/arch/x86/kernel/acpi/boot.c
  2567. +++ b/arch/x86/kernel/acpi/boot.c
  2568. @@ -87,7 +87,9 @@ static u64 acpi_lapic_addr __initdata = APIC_DEFAULT_PHYS_BASE;
  2569. * ->ioapic_mutex
  2570. * ->ioapic_lock
  2571. */
  2572. +#ifdef CONFIG_X86_IO_APIC
  2573. static DEFINE_MUTEX(acpi_ioapic_lock);
  2574. +#endif
  2575. /* --------------------------------------------------------------------------
  2576. Boot-time Configuration
  2577. diff --git a/arch/x86/kernel/apic/io_apic.c b/arch/x86/kernel/apic/io_apic.c
  2578. index 1e5d2f07416b..ac0127bb155e 100644
  2579. --- a/arch/x86/kernel/apic/io_apic.c
  2580. +++ b/arch/x86/kernel/apic/io_apic.c
  2581. @@ -1711,7 +1711,8 @@ static bool io_apic_level_ack_pending(struct mp_chip_data *data)
  2582. static inline bool ioapic_irqd_mask(struct irq_data *data)
  2583. {
  2584. /* If we are moving the irq we need to mask it */
  2585. - if (unlikely(irqd_is_setaffinity_pending(data))) {
  2586. + if (unlikely(irqd_is_setaffinity_pending(data) &&
  2587. + !irqd_irq_inprogress(data))) {
  2588. mask_ioapic_irq(data);
  2589. return true;
  2590. }
  2591. diff --git a/arch/x86/kernel/apic/x2apic_uv_x.c b/arch/x86/kernel/apic/x2apic_uv_x.c
  2592. index 4a139465f1d4..ad2afff02b36 100644
  2593. --- a/arch/x86/kernel/apic/x2apic_uv_x.c
  2594. +++ b/arch/x86/kernel/apic/x2apic_uv_x.c
  2595. @@ -947,7 +947,7 @@ void __init uv_system_init(void)
  2596. uv_blade_info[blade].pnode = pnode;
  2597. uv_blade_info[blade].nr_possible_cpus = 0;
  2598. uv_blade_info[blade].nr_online_cpus = 0;
  2599. - spin_lock_init(&uv_blade_info[blade].nmi_lock);
  2600. + raw_spin_lock_init(&uv_blade_info[blade].nmi_lock);
  2601. min_pnode = min(pnode, min_pnode);
  2602. max_pnode = max(pnode, max_pnode);
  2603. blade++;
  2604. diff --git a/arch/x86/kernel/asm-offsets.c b/arch/x86/kernel/asm-offsets.c
  2605. index 439df975bc7a..b7954ddd6a0a 100644
  2606. --- a/arch/x86/kernel/asm-offsets.c
  2607. +++ b/arch/x86/kernel/asm-offsets.c
  2608. @@ -32,6 +32,7 @@ void common(void) {
  2609. OFFSET(TI_flags, thread_info, flags);
  2610. OFFSET(TI_status, thread_info, status);
  2611. OFFSET(TI_addr_limit, thread_info, addr_limit);
  2612. + OFFSET(TI_preempt_lazy_count, thread_info, preempt_lazy_count);
  2613. BLANK();
  2614. OFFSET(crypto_tfm_ctx_offset, crypto_tfm, __crt_ctx);
  2615. @@ -89,4 +90,5 @@ void common(void) {
  2616. BLANK();
  2617. DEFINE(PTREGS_SIZE, sizeof(struct pt_regs));
  2618. + DEFINE(_PREEMPT_ENABLED, PREEMPT_ENABLED);
  2619. }
  2620. diff --git a/arch/x86/kernel/cpu/mcheck/mce.c b/arch/x86/kernel/cpu/mcheck/mce.c
  2621. index 7e8a736d09db..430a4ec07811 100644
  2622. --- a/arch/x86/kernel/cpu/mcheck/mce.c
  2623. +++ b/arch/x86/kernel/cpu/mcheck/mce.c
  2624. @@ -41,6 +41,8 @@
  2625. #include <linux/debugfs.h>
  2626. #include <linux/irq_work.h>
  2627. #include <linux/export.h>
  2628. +#include <linux/jiffies.h>
  2629. +#include <linux/swork.h>
  2630. #include <asm/processor.h>
  2631. #include <asm/traps.h>
  2632. @@ -1236,7 +1238,7 @@ void mce_log_therm_throt_event(__u64 status)
  2633. static unsigned long check_interval = INITIAL_CHECK_INTERVAL;
  2634. static DEFINE_PER_CPU(unsigned long, mce_next_interval); /* in jiffies */
  2635. -static DEFINE_PER_CPU(struct timer_list, mce_timer);
  2636. +static DEFINE_PER_CPU(struct hrtimer, mce_timer);
  2637. static unsigned long mce_adjust_timer_default(unsigned long interval)
  2638. {
  2639. @@ -1245,32 +1247,18 @@ static unsigned long mce_adjust_timer_default(unsigned long interval)
  2640. static unsigned long (*mce_adjust_timer)(unsigned long interval) = mce_adjust_timer_default;
  2641. -static void __restart_timer(struct timer_list *t, unsigned long interval)
  2642. +static enum hrtimer_restart __restart_timer(struct hrtimer *timer, unsigned long interval)
  2643. {
  2644. - unsigned long when = jiffies + interval;
  2645. - unsigned long flags;
  2646. -
  2647. - local_irq_save(flags);
  2648. -
  2649. - if (timer_pending(t)) {
  2650. - if (time_before(when, t->expires))
  2651. - mod_timer_pinned(t, when);
  2652. - } else {
  2653. - t->expires = round_jiffies(when);
  2654. - add_timer_on(t, smp_processor_id());
  2655. - }
  2656. -
  2657. - local_irq_restore(flags);
  2658. + if (!interval)
  2659. + return HRTIMER_NORESTART;
  2660. + hrtimer_forward_now(timer, ns_to_ktime(jiffies_to_nsecs(interval)));
  2661. + return HRTIMER_RESTART;
  2662. }
  2663. -static void mce_timer_fn(unsigned long data)
  2664. +static enum hrtimer_restart mce_timer_fn(struct hrtimer *timer)
  2665. {
  2666. - struct timer_list *t = this_cpu_ptr(&mce_timer);
  2667. - int cpu = smp_processor_id();
  2668. unsigned long iv;
  2669. - WARN_ON(cpu != data);
  2670. -
  2671. iv = __this_cpu_read(mce_next_interval);
  2672. if (mce_available(this_cpu_ptr(&cpu_info))) {
  2673. @@ -1293,7 +1281,7 @@ static void mce_timer_fn(unsigned long data)
  2674. done:
  2675. __this_cpu_write(mce_next_interval, iv);
  2676. - __restart_timer(t, iv);
  2677. + return __restart_timer(timer, iv);
  2678. }
  2679. /*
  2680. @@ -1301,7 +1289,7 @@ done:
  2681. */
  2682. void mce_timer_kick(unsigned long interval)
  2683. {
  2684. - struct timer_list *t = this_cpu_ptr(&mce_timer);
  2685. + struct hrtimer *t = this_cpu_ptr(&mce_timer);
  2686. unsigned long iv = __this_cpu_read(mce_next_interval);
  2687. __restart_timer(t, interval);
  2688. @@ -1316,7 +1304,7 @@ static void mce_timer_delete_all(void)
  2689. int cpu;
  2690. for_each_online_cpu(cpu)
  2691. - del_timer_sync(&per_cpu(mce_timer, cpu));
  2692. + hrtimer_cancel(&per_cpu(mce_timer, cpu));
  2693. }
  2694. static void mce_do_trigger(struct work_struct *work)
  2695. @@ -1326,6 +1314,56 @@ static void mce_do_trigger(struct work_struct *work)
  2696. static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
  2697. +static void __mce_notify_work(struct swork_event *event)
  2698. +{
  2699. + /* Not more than two messages every minute */
  2700. + static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
  2701. +
  2702. + /* wake processes polling /dev/mcelog */
  2703. + wake_up_interruptible(&mce_chrdev_wait);
  2704. +
  2705. + /*
  2706. + * There is no risk of missing notifications because
  2707. + * work_pending is always cleared before the function is
  2708. + * executed.
  2709. + */
  2710. + if (mce_helper[0] && !work_pending(&mce_trigger_work))
  2711. + schedule_work(&mce_trigger_work);
  2712. +
  2713. + if (__ratelimit(&ratelimit))
  2714. + pr_info(HW_ERR "Machine check events logged\n");
  2715. +}
  2716. +
  2717. +#ifdef CONFIG_PREEMPT_RT_FULL
  2718. +static bool notify_work_ready __read_mostly;
  2719. +static struct swork_event notify_work;
  2720. +
  2721. +static int mce_notify_work_init(void)
  2722. +{
  2723. + int err;
  2724. +
  2725. + err = swork_get();
  2726. + if (err)
  2727. + return err;
  2728. +
  2729. + INIT_SWORK(&notify_work, __mce_notify_work);
  2730. + notify_work_ready = true;
  2731. + return 0;
  2732. +}
  2733. +
  2734. +static void mce_notify_work(void)
  2735. +{
  2736. + if (notify_work_ready)
  2737. + swork_queue(&notify_work);
  2738. +}
  2739. +#else
  2740. +static void mce_notify_work(void)
  2741. +{
  2742. + __mce_notify_work(NULL);
  2743. +}
  2744. +static inline int mce_notify_work_init(void) { return 0; }
  2745. +#endif
  2746. +
  2747. /*
  2748. * Notify the user(s) about new machine check events.
  2749. * Can be called from interrupt context, but not from machine check/NMI
  2750. @@ -1333,19 +1371,8 @@ static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
  2751. */
  2752. int mce_notify_irq(void)
  2753. {
  2754. - /* Not more than two messages every minute */
  2755. - static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
  2756. -
  2757. if (test_and_clear_bit(0, &mce_need_notify)) {
  2758. - /* wake processes polling /dev/mcelog */
  2759. - wake_up_interruptible(&mce_chrdev_wait);
  2760. -
  2761. - if (mce_helper[0])
  2762. - schedule_work(&mce_trigger_work);
  2763. -
  2764. - if (__ratelimit(&ratelimit))
  2765. - pr_info(HW_ERR "Machine check events logged\n");
  2766. -
  2767. + mce_notify_work();
  2768. return 1;
  2769. }
  2770. return 0;
  2771. @@ -1639,7 +1666,7 @@ static void __mcheck_cpu_clear_vendor(struct cpuinfo_x86 *c)
  2772. }
  2773. }
  2774. -static void mce_start_timer(unsigned int cpu, struct timer_list *t)
  2775. +static void mce_start_timer(unsigned int cpu, struct hrtimer *t)
  2776. {
  2777. unsigned long iv = check_interval * HZ;
  2778. @@ -1648,16 +1675,17 @@ static void mce_start_timer(unsigned int cpu, struct timer_list *t)
  2779. per_cpu(mce_next_interval, cpu) = iv;
  2780. - t->expires = round_jiffies(jiffies + iv);
  2781. - add_timer_on(t, cpu);
  2782. + hrtimer_start_range_ns(t, ns_to_ktime(jiffies_to_usecs(iv) * 1000ULL),
  2783. + 0, HRTIMER_MODE_REL_PINNED);
  2784. }
  2785. static void __mcheck_cpu_init_timer(void)
  2786. {
  2787. - struct timer_list *t = this_cpu_ptr(&mce_timer);
  2788. + struct hrtimer *t = this_cpu_ptr(&mce_timer);
  2789. unsigned int cpu = smp_processor_id();
  2790. - setup_timer(t, mce_timer_fn, cpu);
  2791. + hrtimer_init(t, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  2792. + t->function = mce_timer_fn;
  2793. mce_start_timer(cpu, t);
  2794. }
  2795. @@ -2376,6 +2404,8 @@ static void mce_disable_cpu(void *h)
  2796. if (!mce_available(raw_cpu_ptr(&cpu_info)))
  2797. return;
  2798. + hrtimer_cancel(this_cpu_ptr(&mce_timer));
  2799. +
  2800. if (!(action & CPU_TASKS_FROZEN))
  2801. cmci_clear();
  2802. @@ -2398,6 +2428,7 @@ static void mce_reenable_cpu(void *h)
  2803. if (b->init)
  2804. wrmsrl(MSR_IA32_MCx_CTL(i), b->ctl);
  2805. }
  2806. + __mcheck_cpu_init_timer();
  2807. }
  2808. /* Get notified when a cpu comes on/off. Be hotplug friendly. */
  2809. @@ -2405,7 +2436,6 @@ static int
  2810. mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
  2811. {
  2812. unsigned int cpu = (unsigned long)hcpu;
  2813. - struct timer_list *t = &per_cpu(mce_timer, cpu);
  2814. switch (action & ~CPU_TASKS_FROZEN) {
  2815. case CPU_ONLINE:
  2816. @@ -2425,11 +2455,9 @@ mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
  2817. break;
  2818. case CPU_DOWN_PREPARE:
  2819. smp_call_function_single(cpu, mce_disable_cpu, &action, 1);
  2820. - del_timer_sync(t);
  2821. break;
  2822. case CPU_DOWN_FAILED:
  2823. smp_call_function_single(cpu, mce_reenable_cpu, &action, 1);
  2824. - mce_start_timer(cpu, t);
  2825. break;
  2826. }
  2827. @@ -2468,6 +2496,10 @@ static __init int mcheck_init_device(void)
  2828. goto err_out;
  2829. }
  2830. + err = mce_notify_work_init();
  2831. + if (err)
  2832. + goto err_out;
  2833. +
  2834. if (!zalloc_cpumask_var(&mce_device_initialized, GFP_KERNEL)) {
  2835. err = -ENOMEM;
  2836. goto err_out;
  2837. diff --git a/arch/x86/kernel/cpu/perf_event_intel_rapl.c b/arch/x86/kernel/cpu/perf_event_intel_rapl.c
  2838. index ed446bdcbf31..d2ac364e2118 100644
  2839. --- a/arch/x86/kernel/cpu/perf_event_intel_rapl.c
  2840. +++ b/arch/x86/kernel/cpu/perf_event_intel_rapl.c
  2841. @@ -117,7 +117,7 @@ static struct perf_pmu_events_attr event_attr_##v = { \
  2842. };
  2843. struct rapl_pmu {
  2844. - spinlock_t lock;
  2845. + raw_spinlock_t lock;
  2846. int n_active; /* number of active events */
  2847. struct list_head active_list;
  2848. struct pmu *pmu; /* pointer to rapl_pmu_class */
  2849. @@ -220,13 +220,13 @@ static enum hrtimer_restart rapl_hrtimer_handle(struct hrtimer *hrtimer)
  2850. if (!pmu->n_active)
  2851. return HRTIMER_NORESTART;
  2852. - spin_lock_irqsave(&pmu->lock, flags);
  2853. + raw_spin_lock_irqsave(&pmu->lock, flags);
  2854. list_for_each_entry(event, &pmu->active_list, active_entry) {
  2855. rapl_event_update(event);
  2856. }
  2857. - spin_unlock_irqrestore(&pmu->lock, flags);
  2858. + raw_spin_unlock_irqrestore(&pmu->lock, flags);
  2859. hrtimer_forward_now(hrtimer, pmu->timer_interval);
  2860. @@ -263,9 +263,9 @@ static void rapl_pmu_event_start(struct perf_event *event, int mode)
  2861. struct rapl_pmu *pmu = __this_cpu_read(rapl_pmu);
  2862. unsigned long flags;
  2863. - spin_lock_irqsave(&pmu->lock, flags);
  2864. + raw_spin_lock_irqsave(&pmu->lock, flags);
  2865. __rapl_pmu_event_start(pmu, event);
  2866. - spin_unlock_irqrestore(&pmu->lock, flags);
  2867. + raw_spin_unlock_irqrestore(&pmu->lock, flags);
  2868. }
  2869. static void rapl_pmu_event_stop(struct perf_event *event, int mode)
  2870. @@ -274,7 +274,7 @@ static void rapl_pmu_event_stop(struct perf_event *event, int mode)
  2871. struct hw_perf_event *hwc = &event->hw;
  2872. unsigned long flags;
  2873. - spin_lock_irqsave(&pmu->lock, flags);
  2874. + raw_spin_lock_irqsave(&pmu->lock, flags);
  2875. /* mark event as deactivated and stopped */
  2876. if (!(hwc->state & PERF_HES_STOPPED)) {
  2877. @@ -299,7 +299,7 @@ static void rapl_pmu_event_stop(struct perf_event *event, int mode)
  2878. hwc->state |= PERF_HES_UPTODATE;
  2879. }
  2880. - spin_unlock_irqrestore(&pmu->lock, flags);
  2881. + raw_spin_unlock_irqrestore(&pmu->lock, flags);
  2882. }
  2883. static int rapl_pmu_event_add(struct perf_event *event, int mode)
  2884. @@ -308,14 +308,14 @@ static int rapl_pmu_event_add(struct perf_event *event, int mode)
  2885. struct hw_perf_event *hwc = &event->hw;
  2886. unsigned long flags;
  2887. - spin_lock_irqsave(&pmu->lock, flags);
  2888. + raw_spin_lock_irqsave(&pmu->lock, flags);
  2889. hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  2890. if (mode & PERF_EF_START)
  2891. __rapl_pmu_event_start(pmu, event);
  2892. - spin_unlock_irqrestore(&pmu->lock, flags);
  2893. + raw_spin_unlock_irqrestore(&pmu->lock, flags);
  2894. return 0;
  2895. }
  2896. @@ -603,7 +603,7 @@ static int rapl_cpu_prepare(int cpu)
  2897. pmu = kzalloc_node(sizeof(*pmu), GFP_KERNEL, cpu_to_node(cpu));
  2898. if (!pmu)
  2899. return -1;
  2900. - spin_lock_init(&pmu->lock);
  2901. + raw_spin_lock_init(&pmu->lock);
  2902. INIT_LIST_HEAD(&pmu->active_list);
  2903. diff --git a/arch/x86/kernel/dumpstack_32.c b/arch/x86/kernel/dumpstack_32.c
  2904. index 464ffd69b92e..00db1aad1548 100644
  2905. --- a/arch/x86/kernel/dumpstack_32.c
  2906. +++ b/arch/x86/kernel/dumpstack_32.c
  2907. @@ -42,7 +42,7 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,
  2908. unsigned long *stack, unsigned long bp,
  2909. const struct stacktrace_ops *ops, void *data)
  2910. {
  2911. - const unsigned cpu = get_cpu();
  2912. + const unsigned cpu = get_cpu_light();
  2913. int graph = 0;
  2914. u32 *prev_esp;
  2915. @@ -86,7 +86,7 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,
  2916. break;
  2917. touch_nmi_watchdog();
  2918. }
  2919. - put_cpu();
  2920. + put_cpu_light();
  2921. }
  2922. EXPORT_SYMBOL(dump_trace);
  2923. diff --git a/arch/x86/kernel/dumpstack_64.c b/arch/x86/kernel/dumpstack_64.c
  2924. index 5f1c6266eb30..c331e3fef465 100644
  2925. --- a/arch/x86/kernel/dumpstack_64.c
  2926. +++ b/arch/x86/kernel/dumpstack_64.c
  2927. @@ -152,7 +152,7 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,
  2928. unsigned long *stack, unsigned long bp,
  2929. const struct stacktrace_ops *ops, void *data)
  2930. {
  2931. - const unsigned cpu = get_cpu();
  2932. + const unsigned cpu = get_cpu_light();
  2933. struct thread_info *tinfo;
  2934. unsigned long *irq_stack = (unsigned long *)per_cpu(irq_stack_ptr, cpu);
  2935. unsigned long dummy;
  2936. @@ -241,7 +241,7 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,
  2937. * This handles the process stack:
  2938. */
  2939. bp = ops->walk_stack(tinfo, stack, bp, ops, data, NULL, &graph);
  2940. - put_cpu();
  2941. + put_cpu_light();
  2942. }
  2943. EXPORT_SYMBOL(dump_trace);
  2944. @@ -255,7 +255,7 @@ show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
  2945. int cpu;
  2946. int i;
  2947. - preempt_disable();
  2948. + migrate_disable();
  2949. cpu = smp_processor_id();
  2950. irq_stack_end = (unsigned long *)(per_cpu(irq_stack_ptr, cpu));
  2951. @@ -291,7 +291,7 @@ show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
  2952. pr_cont(" %016lx", *stack++);
  2953. touch_nmi_watchdog();
  2954. }
  2955. - preempt_enable();
  2956. + migrate_enable();
  2957. pr_cont("\n");
  2958. show_trace_log_lvl(task, regs, sp, bp, log_lvl);
  2959. diff --git a/arch/x86/kernel/irq_32.c b/arch/x86/kernel/irq_32.c
  2960. index 38da8f29a9c8..ce71f7098f15 100644
  2961. --- a/arch/x86/kernel/irq_32.c
  2962. +++ b/arch/x86/kernel/irq_32.c
  2963. @@ -128,6 +128,7 @@ void irq_ctx_init(int cpu)
  2964. cpu, per_cpu(hardirq_stack, cpu), per_cpu(softirq_stack, cpu));
  2965. }
  2966. +#ifndef CONFIG_PREEMPT_RT_FULL
  2967. void do_softirq_own_stack(void)
  2968. {
  2969. struct thread_info *curstk;
  2970. @@ -146,6 +147,7 @@ void do_softirq_own_stack(void)
  2971. call_on_stack(__do_softirq, isp);
  2972. }
  2973. +#endif
  2974. bool handle_irq(struct irq_desc *desc, struct pt_regs *regs)
  2975. {
  2976. diff --git a/arch/x86/kernel/kvm.c b/arch/x86/kernel/kvm.c
  2977. index 47190bd399e7..807950860fb7 100644
  2978. --- a/arch/x86/kernel/kvm.c
  2979. +++ b/arch/x86/kernel/kvm.c
  2980. @@ -36,6 +36,7 @@
  2981. #include <linux/kprobes.h>
  2982. #include <linux/debugfs.h>
  2983. #include <linux/nmi.h>
  2984. +#include <linux/swait.h>
  2985. #include <asm/timer.h>
  2986. #include <asm/cpu.h>
  2987. #include <asm/traps.h>
  2988. @@ -91,14 +92,14 @@ static void kvm_io_delay(void)
  2989. struct kvm_task_sleep_node {
  2990. struct hlist_node link;
  2991. - wait_queue_head_t wq;
  2992. + struct swait_queue_head wq;
  2993. u32 token;
  2994. int cpu;
  2995. bool halted;
  2996. };
  2997. static struct kvm_task_sleep_head {
  2998. - spinlock_t lock;
  2999. + raw_spinlock_t lock;
  3000. struct hlist_head list;
  3001. } async_pf_sleepers[KVM_TASK_SLEEP_HASHSIZE];
  3002. @@ -122,17 +123,17 @@ void kvm_async_pf_task_wait(u32 token)
  3003. u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
  3004. struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
  3005. struct kvm_task_sleep_node n, *e;
  3006. - DEFINE_WAIT(wait);
  3007. + DECLARE_SWAITQUEUE(wait);
  3008. rcu_irq_enter();
  3009. - spin_lock(&b->lock);
  3010. + raw_spin_lock(&b->lock);
  3011. e = _find_apf_task(b, token);
  3012. if (e) {
  3013. /* dummy entry exist -> wake up was delivered ahead of PF */
  3014. hlist_del(&e->link);
  3015. kfree(e);
  3016. - spin_unlock(&b->lock);
  3017. + raw_spin_unlock(&b->lock);
  3018. rcu_irq_exit();
  3019. return;
  3020. @@ -141,13 +142,13 @@ void kvm_async_pf_task_wait(u32 token)
  3021. n.token = token;
  3022. n.cpu = smp_processor_id();
  3023. n.halted = is_idle_task(current) || preempt_count() > 1;
  3024. - init_waitqueue_head(&n.wq);
  3025. + init_swait_queue_head(&n.wq);
  3026. hlist_add_head(&n.link, &b->list);
  3027. - spin_unlock(&b->lock);
  3028. + raw_spin_unlock(&b->lock);
  3029. for (;;) {
  3030. if (!n.halted)
  3031. - prepare_to_wait(&n.wq, &wait, TASK_UNINTERRUPTIBLE);
  3032. + prepare_to_swait(&n.wq, &wait, TASK_UNINTERRUPTIBLE);
  3033. if (hlist_unhashed(&n.link))
  3034. break;
  3035. @@ -166,7 +167,7 @@ void kvm_async_pf_task_wait(u32 token)
  3036. }
  3037. }
  3038. if (!n.halted)
  3039. - finish_wait(&n.wq, &wait);
  3040. + finish_swait(&n.wq, &wait);
  3041. rcu_irq_exit();
  3042. return;
  3043. @@ -178,8 +179,8 @@ static void apf_task_wake_one(struct kvm_task_sleep_node *n)
  3044. hlist_del_init(&n->link);
  3045. if (n->halted)
  3046. smp_send_reschedule(n->cpu);
  3047. - else if (waitqueue_active(&n->wq))
  3048. - wake_up(&n->wq);
  3049. + else if (swait_active(&n->wq))
  3050. + swake_up(&n->wq);
  3051. }
  3052. static void apf_task_wake_all(void)
  3053. @@ -189,14 +190,14 @@ static void apf_task_wake_all(void)
  3054. for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++) {
  3055. struct hlist_node *p, *next;
  3056. struct kvm_task_sleep_head *b = &async_pf_sleepers[i];
  3057. - spin_lock(&b->lock);
  3058. + raw_spin_lock(&b->lock);
  3059. hlist_for_each_safe(p, next, &b->list) {
  3060. struct kvm_task_sleep_node *n =
  3061. hlist_entry(p, typeof(*n), link);
  3062. if (n->cpu == smp_processor_id())
  3063. apf_task_wake_one(n);
  3064. }
  3065. - spin_unlock(&b->lock);
  3066. + raw_spin_unlock(&b->lock);
  3067. }
  3068. }
  3069. @@ -212,7 +213,7 @@ void kvm_async_pf_task_wake(u32 token)
  3070. }
  3071. again:
  3072. - spin_lock(&b->lock);
  3073. + raw_spin_lock(&b->lock);
  3074. n = _find_apf_task(b, token);
  3075. if (!n) {
  3076. /*
  3077. @@ -225,17 +226,17 @@ again:
  3078. * Allocation failed! Busy wait while other cpu
  3079. * handles async PF.
  3080. */
  3081. - spin_unlock(&b->lock);
  3082. + raw_spin_unlock(&b->lock);
  3083. cpu_relax();
  3084. goto again;
  3085. }
  3086. n->token = token;
  3087. n->cpu = smp_processor_id();
  3088. - init_waitqueue_head(&n->wq);
  3089. + init_swait_queue_head(&n->wq);
  3090. hlist_add_head(&n->link, &b->list);
  3091. } else
  3092. apf_task_wake_one(n);
  3093. - spin_unlock(&b->lock);
  3094. + raw_spin_unlock(&b->lock);
  3095. return;
  3096. }
  3097. EXPORT_SYMBOL_GPL(kvm_async_pf_task_wake);
  3098. @@ -486,7 +487,7 @@ void __init kvm_guest_init(void)
  3099. paravirt_ops_setup();
  3100. register_reboot_notifier(&kvm_pv_reboot_nb);
  3101. for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++)
  3102. - spin_lock_init(&async_pf_sleepers[i].lock);
  3103. + raw_spin_lock_init(&async_pf_sleepers[i].lock);
  3104. if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF))
  3105. x86_init.irqs.trap_init = kvm_apf_trap_init;
  3106. diff --git a/arch/x86/kernel/nmi.c b/arch/x86/kernel/nmi.c
  3107. index 697f90db0e37..424aec4a4c71 100644
  3108. --- a/arch/x86/kernel/nmi.c
  3109. +++ b/arch/x86/kernel/nmi.c
  3110. @@ -231,7 +231,7 @@ pci_serr_error(unsigned char reason, struct pt_regs *regs)
  3111. #endif
  3112. if (panic_on_unrecovered_nmi)
  3113. - panic("NMI: Not continuing");
  3114. + nmi_panic(regs, "NMI: Not continuing");
  3115. pr_emerg("Dazed and confused, but trying to continue\n");
  3116. @@ -255,8 +255,16 @@ io_check_error(unsigned char reason, struct pt_regs *regs)
  3117. reason, smp_processor_id());
  3118. show_regs(regs);
  3119. - if (panic_on_io_nmi)
  3120. - panic("NMI IOCK error: Not continuing");
  3121. + if (panic_on_io_nmi) {
  3122. + nmi_panic(regs, "NMI IOCK error: Not continuing");
  3123. +
  3124. + /*
  3125. + * If we end up here, it means we have received an NMI while
  3126. + * processing panic(). Simply return without delaying and
  3127. + * re-enabling NMIs.
  3128. + */
  3129. + return;
  3130. + }
  3131. /* Re-enable the IOCK line, wait for a few seconds */
  3132. reason = (reason & NMI_REASON_CLEAR_MASK) | NMI_REASON_CLEAR_IOCHK;
  3133. @@ -297,7 +305,7 @@ unknown_nmi_error(unsigned char reason, struct pt_regs *regs)
  3134. pr_emerg("Do you have a strange power saving mode enabled?\n");
  3135. if (unknown_nmi_panic || panic_on_unrecovered_nmi)
  3136. - panic("NMI: Not continuing");
  3137. + nmi_panic(regs, "NMI: Not continuing");
  3138. pr_emerg("Dazed and confused, but trying to continue\n");
  3139. }
  3140. diff --git a/arch/x86/kernel/process_32.c b/arch/x86/kernel/process_32.c
  3141. index 9f950917528b..4dd4beae917a 100644
  3142. --- a/arch/x86/kernel/process_32.c
  3143. +++ b/arch/x86/kernel/process_32.c
  3144. @@ -35,6 +35,7 @@
  3145. #include <linux/uaccess.h>
  3146. #include <linux/io.h>
  3147. #include <linux/kdebug.h>
  3148. +#include <linux/highmem.h>
  3149. #include <asm/pgtable.h>
  3150. #include <asm/ldt.h>
  3151. @@ -210,6 +211,35 @@ start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
  3152. }
  3153. EXPORT_SYMBOL_GPL(start_thread);
  3154. +#ifdef CONFIG_PREEMPT_RT_FULL
  3155. +static void switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p)
  3156. +{
  3157. + int i;
  3158. +
  3159. + /*
  3160. + * Clear @prev's kmap_atomic mappings
  3161. + */
  3162. + for (i = 0; i < prev_p->kmap_idx; i++) {
  3163. + int idx = i + KM_TYPE_NR * smp_processor_id();
  3164. + pte_t *ptep = kmap_pte - idx;
  3165. +
  3166. + kpte_clear_flush(ptep, __fix_to_virt(FIX_KMAP_BEGIN + idx));
  3167. + }
  3168. + /*
  3169. + * Restore @next_p's kmap_atomic mappings
  3170. + */
  3171. + for (i = 0; i < next_p->kmap_idx; i++) {
  3172. + int idx = i + KM_TYPE_NR * smp_processor_id();
  3173. +
  3174. + if (!pte_none(next_p->kmap_pte[i]))
  3175. + set_pte(kmap_pte - idx, next_p->kmap_pte[i]);
  3176. + }
  3177. +}
  3178. +#else
  3179. +static inline void
  3180. +switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p) { }
  3181. +#endif
  3182. +
  3183. /*
  3184. * switch_to(x,y) should switch tasks from x to y.
  3185. @@ -286,6 +316,8 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
  3186. task_thread_info(next_p)->flags & _TIF_WORK_CTXSW_NEXT))
  3187. __switch_to_xtra(prev_p, next_p, tss);
  3188. + switch_kmaps(prev_p, next_p);
  3189. +
  3190. /*
  3191. * Leave lazy mode, flushing any hypercalls made here.
  3192. * This must be done before restoring TLS segments so
  3193. diff --git a/arch/x86/kernel/reboot.c b/arch/x86/kernel/reboot.c
  3194. index f660d63f40fe..8384207adde2 100644
  3195. --- a/arch/x86/kernel/reboot.c
  3196. +++ b/arch/x86/kernel/reboot.c
  3197. @@ -726,6 +726,7 @@ static int crashing_cpu;
  3198. static nmi_shootdown_cb shootdown_callback;
  3199. static atomic_t waiting_for_crash_ipi;
  3200. +static int crash_ipi_issued;
  3201. static int crash_nmi_callback(unsigned int val, struct pt_regs *regs)
  3202. {
  3203. @@ -788,6 +789,9 @@ void nmi_shootdown_cpus(nmi_shootdown_cb callback)
  3204. smp_send_nmi_allbutself();
  3205. + /* Kick CPUs looping in NMI context. */
  3206. + WRITE_ONCE(crash_ipi_issued, 1);
  3207. +
  3208. msecs = 1000; /* Wait at most a second for the other cpus to stop */
  3209. while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
  3210. mdelay(1);
  3211. @@ -796,6 +800,22 @@ void nmi_shootdown_cpus(nmi_shootdown_cb callback)
  3212. /* Leave the nmi callback set */
  3213. }
  3214. +
  3215. +/* Override the weak function in kernel/panic.c */
  3216. +void nmi_panic_self_stop(struct pt_regs *regs)
  3217. +{
  3218. + while (1) {
  3219. + /*
  3220. + * Wait for the crash dumping IPI to be issued, and then
  3221. + * call its callback directly.
  3222. + */
  3223. + if (READ_ONCE(crash_ipi_issued))
  3224. + crash_nmi_callback(0, regs); /* Don't return */
  3225. +
  3226. + cpu_relax();
  3227. + }
  3228. +}
  3229. +
  3230. #else /* !CONFIG_SMP */
  3231. void nmi_shootdown_cpus(nmi_shootdown_cb callback)
  3232. {
  3233. diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
  3234. index 1c96f09367ae..ffb6d9859122 100644
  3235. --- a/arch/x86/kvm/lapic.c
  3236. +++ b/arch/x86/kvm/lapic.c
  3237. @@ -1195,7 +1195,7 @@ static void apic_update_lvtt(struct kvm_lapic *apic)
  3238. static void apic_timer_expired(struct kvm_lapic *apic)
  3239. {
  3240. struct kvm_vcpu *vcpu = apic->vcpu;
  3241. - wait_queue_head_t *q = &vcpu->wq;
  3242. + struct swait_queue_head *q = &vcpu->wq;
  3243. struct kvm_timer *ktimer = &apic->lapic_timer;
  3244. if (atomic_read(&apic->lapic_timer.pending))
  3245. @@ -1204,8 +1204,8 @@ static void apic_timer_expired(struct kvm_lapic *apic)
  3246. atomic_inc(&apic->lapic_timer.pending);
  3247. kvm_set_pending_timer(vcpu);
  3248. - if (waitqueue_active(q))
  3249. - wake_up_interruptible(q);
  3250. + if (swait_active(q))
  3251. + swake_up(q);
  3252. if (apic_lvtt_tscdeadline(apic))
  3253. ktimer->expired_tscdeadline = ktimer->tscdeadline;
  3254. @@ -1801,6 +1801,7 @@ int kvm_create_lapic(struct kvm_vcpu *vcpu)
  3255. hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
  3256. HRTIMER_MODE_ABS);
  3257. apic->lapic_timer.timer.function = apic_timer_fn;
  3258. + apic->lapic_timer.timer.irqsafe = 1;
  3259. /*
  3260. * APIC is created enabled. This will prevent kvm_lapic_set_base from
  3261. diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
  3262. index ae2b9cd358f2..50c9861d4219 100644
  3263. --- a/arch/x86/kvm/x86.c
  3264. +++ b/arch/x86/kvm/x86.c
  3265. @@ -5827,6 +5827,13 @@ int kvm_arch_init(void *opaque)
  3266. goto out;
  3267. }
  3268. +#ifdef CONFIG_PREEMPT_RT_FULL
  3269. + if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
  3270. + printk(KERN_ERR "RT requires X86_FEATURE_CONSTANT_TSC\n");
  3271. + return -EOPNOTSUPP;
  3272. + }
  3273. +#endif
  3274. +
  3275. r = kvm_mmu_module_init();
  3276. if (r)
  3277. goto out_free_percpu;
  3278. diff --git a/arch/x86/mm/highmem_32.c b/arch/x86/mm/highmem_32.c
  3279. index a6d739258137..bd24ba1c4a86 100644
  3280. --- a/arch/x86/mm/highmem_32.c
  3281. +++ b/arch/x86/mm/highmem_32.c
  3282. @@ -32,10 +32,11 @@ EXPORT_SYMBOL(kunmap);
  3283. */
  3284. void *kmap_atomic_prot(struct page *page, pgprot_t prot)
  3285. {
  3286. + pte_t pte = mk_pte(page, prot);
  3287. unsigned long vaddr;
  3288. int idx, type;
  3289. - preempt_disable();
  3290. + preempt_disable_nort();
  3291. pagefault_disable();
  3292. if (!PageHighMem(page))
  3293. @@ -45,7 +46,10 @@ void *kmap_atomic_prot(struct page *page, pgprot_t prot)
  3294. idx = type + KM_TYPE_NR*smp_processor_id();
  3295. vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
  3296. BUG_ON(!pte_none(*(kmap_pte-idx)));
  3297. - set_pte(kmap_pte-idx, mk_pte(page, prot));
  3298. +#ifdef CONFIG_PREEMPT_RT_FULL
  3299. + current->kmap_pte[type] = pte;
  3300. +#endif
  3301. + set_pte(kmap_pte-idx, pte);
  3302. arch_flush_lazy_mmu_mode();
  3303. return (void *)vaddr;
  3304. @@ -88,6 +92,9 @@ void __kunmap_atomic(void *kvaddr)
  3305. * is a bad idea also, in case the page changes cacheability
  3306. * attributes or becomes a protected page in a hypervisor.
  3307. */
  3308. +#ifdef CONFIG_PREEMPT_RT_FULL
  3309. + current->kmap_pte[type] = __pte(0);
  3310. +#endif
  3311. kpte_clear_flush(kmap_pte-idx, vaddr);
  3312. kmap_atomic_idx_pop();
  3313. arch_flush_lazy_mmu_mode();
  3314. @@ -100,7 +107,7 @@ void __kunmap_atomic(void *kvaddr)
  3315. #endif
  3316. pagefault_enable();
  3317. - preempt_enable();
  3318. + preempt_enable_nort();
  3319. }
  3320. EXPORT_SYMBOL(__kunmap_atomic);
  3321. diff --git a/arch/x86/mm/iomap_32.c b/arch/x86/mm/iomap_32.c
  3322. index 9c0ff045fdd4..dd25dd1671b6 100644
  3323. --- a/arch/x86/mm/iomap_32.c
  3324. +++ b/arch/x86/mm/iomap_32.c
  3325. @@ -56,6 +56,7 @@ EXPORT_SYMBOL_GPL(iomap_free);
  3326. void *kmap_atomic_prot_pfn(unsigned long pfn, pgprot_t prot)
  3327. {
  3328. + pte_t pte = pfn_pte(pfn, prot);
  3329. unsigned long vaddr;
  3330. int idx, type;
  3331. @@ -65,7 +66,12 @@ void *kmap_atomic_prot_pfn(unsigned long pfn, pgprot_t prot)
  3332. type = kmap_atomic_idx_push();
  3333. idx = type + KM_TYPE_NR * smp_processor_id();
  3334. vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
  3335. - set_pte(kmap_pte - idx, pfn_pte(pfn, prot));
  3336. + WARN_ON(!pte_none(*(kmap_pte - idx)));
  3337. +
  3338. +#ifdef CONFIG_PREEMPT_RT_FULL
  3339. + current->kmap_pte[type] = pte;
  3340. +#endif
  3341. + set_pte(kmap_pte - idx, pte);
  3342. arch_flush_lazy_mmu_mode();
  3343. return (void *)vaddr;
  3344. @@ -113,6 +119,9 @@ iounmap_atomic(void __iomem *kvaddr)
  3345. * is a bad idea also, in case the page changes cacheability
  3346. * attributes or becomes a protected page in a hypervisor.
  3347. */
  3348. +#ifdef CONFIG_PREEMPT_RT_FULL
  3349. + current->kmap_pte[type] = __pte(0);
  3350. +#endif
  3351. kpte_clear_flush(kmap_pte-idx, vaddr);
  3352. kmap_atomic_idx_pop();
  3353. }
  3354. diff --git a/arch/x86/mm/pageattr.c b/arch/x86/mm/pageattr.c
  3355. index b599a780a5a9..2e85c4117daf 100644
  3356. --- a/arch/x86/mm/pageattr.c
  3357. +++ b/arch/x86/mm/pageattr.c
  3358. @@ -208,7 +208,15 @@ static void cpa_flush_array(unsigned long *start, int numpages, int cache,
  3359. int in_flags, struct page **pages)
  3360. {
  3361. unsigned int i, level;
  3362. +#ifdef CONFIG_PREEMPT
  3363. + /*
  3364. + * Avoid wbinvd() because it causes latencies on all CPUs,
  3365. + * regardless of any CPU isolation that may be in effect.
  3366. + */
  3367. + unsigned long do_wbinvd = 0;
  3368. +#else
  3369. unsigned long do_wbinvd = cache && numpages >= 1024; /* 4M threshold */
  3370. +#endif
  3371. BUG_ON(irqs_disabled());
  3372. diff --git a/arch/x86/platform/uv/tlb_uv.c b/arch/x86/platform/uv/tlb_uv.c
  3373. index 3b6ec42718e4..7871083de089 100644
  3374. --- a/arch/x86/platform/uv/tlb_uv.c
  3375. +++ b/arch/x86/platform/uv/tlb_uv.c
  3376. @@ -714,9 +714,9 @@ static void destination_plugged(struct bau_desc *bau_desc,
  3377. quiesce_local_uvhub(hmaster);
  3378. - spin_lock(&hmaster->queue_lock);
  3379. + raw_spin_lock(&hmaster->queue_lock);
  3380. reset_with_ipi(&bau_desc->distribution, bcp);
  3381. - spin_unlock(&hmaster->queue_lock);
  3382. + raw_spin_unlock(&hmaster->queue_lock);
  3383. end_uvhub_quiesce(hmaster);
  3384. @@ -736,9 +736,9 @@ static void destination_timeout(struct bau_desc *bau_desc,
  3385. quiesce_local_uvhub(hmaster);
  3386. - spin_lock(&hmaster->queue_lock);
  3387. + raw_spin_lock(&hmaster->queue_lock);
  3388. reset_with_ipi(&bau_desc->distribution, bcp);
  3389. - spin_unlock(&hmaster->queue_lock);
  3390. + raw_spin_unlock(&hmaster->queue_lock);
  3391. end_uvhub_quiesce(hmaster);
  3392. @@ -759,7 +759,7 @@ static void disable_for_period(struct bau_control *bcp, struct ptc_stats *stat)
  3393. cycles_t tm1;
  3394. hmaster = bcp->uvhub_master;
  3395. - spin_lock(&hmaster->disable_lock);
  3396. + raw_spin_lock(&hmaster->disable_lock);
  3397. if (!bcp->baudisabled) {
  3398. stat->s_bau_disabled++;
  3399. tm1 = get_cycles();
  3400. @@ -772,7 +772,7 @@ static void disable_for_period(struct bau_control *bcp, struct ptc_stats *stat)
  3401. }
  3402. }
  3403. }
  3404. - spin_unlock(&hmaster->disable_lock);
  3405. + raw_spin_unlock(&hmaster->disable_lock);
  3406. }
  3407. static void count_max_concurr(int stat, struct bau_control *bcp,
  3408. @@ -835,7 +835,7 @@ static void record_send_stats(cycles_t time1, cycles_t time2,
  3409. */
  3410. static void uv1_throttle(struct bau_control *hmaster, struct ptc_stats *stat)
  3411. {
  3412. - spinlock_t *lock = &hmaster->uvhub_lock;
  3413. + raw_spinlock_t *lock = &hmaster->uvhub_lock;
  3414. atomic_t *v;
  3415. v = &hmaster->active_descriptor_count;
  3416. @@ -968,7 +968,7 @@ static int check_enable(struct bau_control *bcp, struct ptc_stats *stat)
  3417. struct bau_control *hmaster;
  3418. hmaster = bcp->uvhub_master;
  3419. - spin_lock(&hmaster->disable_lock);
  3420. + raw_spin_lock(&hmaster->disable_lock);
  3421. if (bcp->baudisabled && (get_cycles() >= bcp->set_bau_on_time)) {
  3422. stat->s_bau_reenabled++;
  3423. for_each_present_cpu(tcpu) {
  3424. @@ -980,10 +980,10 @@ static int check_enable(struct bau_control *bcp, struct ptc_stats *stat)
  3425. tbcp->period_giveups = 0;
  3426. }
  3427. }
  3428. - spin_unlock(&hmaster->disable_lock);
  3429. + raw_spin_unlock(&hmaster->disable_lock);
  3430. return 0;
  3431. }
  3432. - spin_unlock(&hmaster->disable_lock);
  3433. + raw_spin_unlock(&hmaster->disable_lock);
  3434. return -1;
  3435. }
  3436. @@ -1901,9 +1901,9 @@ static void __init init_per_cpu_tunables(void)
  3437. bcp->cong_reps = congested_reps;
  3438. bcp->disabled_period = sec_2_cycles(disabled_period);
  3439. bcp->giveup_limit = giveup_limit;
  3440. - spin_lock_init(&bcp->queue_lock);
  3441. - spin_lock_init(&bcp->uvhub_lock);
  3442. - spin_lock_init(&bcp->disable_lock);
  3443. + raw_spin_lock_init(&bcp->queue_lock);
  3444. + raw_spin_lock_init(&bcp->uvhub_lock);
  3445. + raw_spin_lock_init(&bcp->disable_lock);
  3446. }
  3447. }
  3448. diff --git a/arch/x86/platform/uv/uv_time.c b/arch/x86/platform/uv/uv_time.c
  3449. index 2b158a9fa1d7..5e0b122620cb 100644
  3450. --- a/arch/x86/platform/uv/uv_time.c
  3451. +++ b/arch/x86/platform/uv/uv_time.c
  3452. @@ -57,7 +57,7 @@ static DEFINE_PER_CPU(struct clock_event_device, cpu_ced);
  3453. /* There is one of these allocated per node */
  3454. struct uv_rtc_timer_head {
  3455. - spinlock_t lock;
  3456. + raw_spinlock_t lock;
  3457. /* next cpu waiting for timer, local node relative: */
  3458. int next_cpu;
  3459. /* number of cpus on this node: */
  3460. @@ -177,7 +177,7 @@ static __init int uv_rtc_allocate_timers(void)
  3461. uv_rtc_deallocate_timers();
  3462. return -ENOMEM;
  3463. }
  3464. - spin_lock_init(&head->lock);
  3465. + raw_spin_lock_init(&head->lock);
  3466. head->ncpus = uv_blade_nr_possible_cpus(bid);
  3467. head->next_cpu = -1;
  3468. blade_info[bid] = head;
  3469. @@ -231,7 +231,7 @@ static int uv_rtc_set_timer(int cpu, u64 expires)
  3470. unsigned long flags;
  3471. int next_cpu;
  3472. - spin_lock_irqsave(&head->lock, flags);
  3473. + raw_spin_lock_irqsave(&head->lock, flags);
  3474. next_cpu = head->next_cpu;
  3475. *t = expires;
  3476. @@ -243,12 +243,12 @@ static int uv_rtc_set_timer(int cpu, u64 expires)
  3477. if (uv_setup_intr(cpu, expires)) {
  3478. *t = ULLONG_MAX;
  3479. uv_rtc_find_next_timer(head, pnode);
  3480. - spin_unlock_irqrestore(&head->lock, flags);
  3481. + raw_spin_unlock_irqrestore(&head->lock, flags);
  3482. return -ETIME;
  3483. }
  3484. }
  3485. - spin_unlock_irqrestore(&head->lock, flags);
  3486. + raw_spin_unlock_irqrestore(&head->lock, flags);
  3487. return 0;
  3488. }
  3489. @@ -267,7 +267,7 @@ static int uv_rtc_unset_timer(int cpu, int force)
  3490. unsigned long flags;
  3491. int rc = 0;
  3492. - spin_lock_irqsave(&head->lock, flags);
  3493. + raw_spin_lock_irqsave(&head->lock, flags);
  3494. if ((head->next_cpu == bcpu && uv_read_rtc(NULL) >= *t) || force)
  3495. rc = 1;
  3496. @@ -279,7 +279,7 @@ static int uv_rtc_unset_timer(int cpu, int force)
  3497. uv_rtc_find_next_timer(head, pnode);
  3498. }
  3499. - spin_unlock_irqrestore(&head->lock, flags);
  3500. + raw_spin_unlock_irqrestore(&head->lock, flags);
  3501. return rc;
  3502. }
  3503. @@ -299,13 +299,18 @@ static int uv_rtc_unset_timer(int cpu, int force)
  3504. static cycle_t uv_read_rtc(struct clocksource *cs)
  3505. {
  3506. unsigned long offset;
  3507. + cycle_t cycles;
  3508. + preempt_disable();
  3509. if (uv_get_min_hub_revision_id() == 1)
  3510. offset = 0;
  3511. else
  3512. offset = (uv_blade_processor_id() * L1_CACHE_BYTES) % PAGE_SIZE;
  3513. - return (cycle_t)uv_read_local_mmr(UVH_RTC | offset);
  3514. + cycles = (cycle_t)uv_read_local_mmr(UVH_RTC | offset);
  3515. + preempt_enable();
  3516. +
  3517. + return cycles;
  3518. }
  3519. /*
  3520. diff --git a/block/blk-core.c b/block/blk-core.c
  3521. index ef083e7a37c5..7a7926b1b4b5 100644
  3522. --- a/block/blk-core.c
  3523. +++ b/block/blk-core.c
  3524. @@ -125,6 +125,9 @@ void blk_rq_init(struct request_queue *q, struct request *rq)
  3525. INIT_LIST_HEAD(&rq->queuelist);
  3526. INIT_LIST_HEAD(&rq->timeout_list);
  3527. +#ifdef CONFIG_PREEMPT_RT_FULL
  3528. + INIT_WORK(&rq->work, __blk_mq_complete_request_remote_work);
  3529. +#endif
  3530. rq->cpu = -1;
  3531. rq->q = q;
  3532. rq->__sector = (sector_t) -1;
  3533. @@ -233,7 +236,7 @@ EXPORT_SYMBOL(blk_start_queue_async);
  3534. **/
  3535. void blk_start_queue(struct request_queue *q)
  3536. {
  3537. - WARN_ON(!irqs_disabled());
  3538. + WARN_ON_NONRT(!irqs_disabled());
  3539. queue_flag_clear(QUEUE_FLAG_STOPPED, q);
  3540. __blk_run_queue(q);
  3541. @@ -659,7 +662,7 @@ int blk_queue_enter(struct request_queue *q, gfp_t gfp)
  3542. if (!gfpflags_allow_blocking(gfp))
  3543. return -EBUSY;
  3544. - ret = wait_event_interruptible(q->mq_freeze_wq,
  3545. + ret = swait_event_interruptible(q->mq_freeze_wq,
  3546. !atomic_read(&q->mq_freeze_depth) ||
  3547. blk_queue_dying(q));
  3548. if (blk_queue_dying(q))
  3549. @@ -679,7 +682,7 @@ static void blk_queue_usage_counter_release(struct percpu_ref *ref)
  3550. struct request_queue *q =
  3551. container_of(ref, struct request_queue, q_usage_counter);
  3552. - wake_up_all(&q->mq_freeze_wq);
  3553. + swake_up_all(&q->mq_freeze_wq);
  3554. }
  3555. struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
  3556. @@ -741,7 +744,7 @@ struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
  3557. q->bypass_depth = 1;
  3558. __set_bit(QUEUE_FLAG_BYPASS, &q->queue_flags);
  3559. - init_waitqueue_head(&q->mq_freeze_wq);
  3560. + init_swait_queue_head(&q->mq_freeze_wq);
  3561. /*
  3562. * Init percpu_ref in atomic mode so that it's faster to shutdown.
  3563. @@ -3222,7 +3225,7 @@ static void queue_unplugged(struct request_queue *q, unsigned int depth,
  3564. blk_run_queue_async(q);
  3565. else
  3566. __blk_run_queue(q);
  3567. - spin_unlock(q->queue_lock);
  3568. + spin_unlock_irq(q->queue_lock);
  3569. }
  3570. static void flush_plug_callbacks(struct blk_plug *plug, bool from_schedule)
  3571. @@ -3270,7 +3273,6 @@ EXPORT_SYMBOL(blk_check_plugged);
  3572. void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
  3573. {
  3574. struct request_queue *q;
  3575. - unsigned long flags;
  3576. struct request *rq;
  3577. LIST_HEAD(list);
  3578. unsigned int depth;
  3579. @@ -3290,11 +3292,6 @@ void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
  3580. q = NULL;
  3581. depth = 0;
  3582. - /*
  3583. - * Save and disable interrupts here, to avoid doing it for every
  3584. - * queue lock we have to take.
  3585. - */
  3586. - local_irq_save(flags);
  3587. while (!list_empty(&list)) {
  3588. rq = list_entry_rq(list.next);
  3589. list_del_init(&rq->queuelist);
  3590. @@ -3307,7 +3304,7 @@ void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
  3591. queue_unplugged(q, depth, from_schedule);
  3592. q = rq->q;
  3593. depth = 0;
  3594. - spin_lock(q->queue_lock);
  3595. + spin_lock_irq(q->queue_lock);
  3596. }
  3597. /*
  3598. @@ -3334,8 +3331,6 @@ void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
  3599. */
  3600. if (q)
  3601. queue_unplugged(q, depth, from_schedule);
  3602. -
  3603. - local_irq_restore(flags);
  3604. }
  3605. void blk_finish_plug(struct blk_plug *plug)
  3606. diff --git a/block/blk-ioc.c b/block/blk-ioc.c
  3607. index 381cb50a673c..dc8785233d94 100644
  3608. --- a/block/blk-ioc.c
  3609. +++ b/block/blk-ioc.c
  3610. @@ -7,6 +7,7 @@
  3611. #include <linux/bio.h>
  3612. #include <linux/blkdev.h>
  3613. #include <linux/slab.h>
  3614. +#include <linux/delay.h>
  3615. #include "blk.h"
  3616. @@ -109,7 +110,7 @@ static void ioc_release_fn(struct work_struct *work)
  3617. spin_unlock(q->queue_lock);
  3618. } else {
  3619. spin_unlock_irqrestore(&ioc->lock, flags);
  3620. - cpu_relax();
  3621. + cpu_chill();
  3622. spin_lock_irqsave_nested(&ioc->lock, flags, 1);
  3623. }
  3624. }
  3625. @@ -187,7 +188,7 @@ retry:
  3626. spin_unlock(icq->q->queue_lock);
  3627. } else {
  3628. spin_unlock_irqrestore(&ioc->lock, flags);
  3629. - cpu_relax();
  3630. + cpu_chill();
  3631. goto retry;
  3632. }
  3633. }
  3634. diff --git a/block/blk-iopoll.c b/block/blk-iopoll.c
  3635. index 0736729d6494..3e21e31d0d7e 100644
  3636. --- a/block/blk-iopoll.c
  3637. +++ b/block/blk-iopoll.c
  3638. @@ -35,6 +35,7 @@ void blk_iopoll_sched(struct blk_iopoll *iop)
  3639. list_add_tail(&iop->list, this_cpu_ptr(&blk_cpu_iopoll));
  3640. __raise_softirq_irqoff(BLOCK_IOPOLL_SOFTIRQ);
  3641. local_irq_restore(flags);
  3642. + preempt_check_resched_rt();
  3643. }
  3644. EXPORT_SYMBOL(blk_iopoll_sched);
  3645. @@ -132,6 +133,7 @@ static void blk_iopoll_softirq(struct softirq_action *h)
  3646. __raise_softirq_irqoff(BLOCK_IOPOLL_SOFTIRQ);
  3647. local_irq_enable();
  3648. + preempt_check_resched_rt();
  3649. }
  3650. /**
  3651. @@ -201,6 +203,7 @@ static int blk_iopoll_cpu_notify(struct notifier_block *self,
  3652. this_cpu_ptr(&blk_cpu_iopoll));
  3653. __raise_softirq_irqoff(BLOCK_IOPOLL_SOFTIRQ);
  3654. local_irq_enable();
  3655. + preempt_check_resched_rt();
  3656. }
  3657. return NOTIFY_OK;
  3658. diff --git a/block/blk-mq-cpu.c b/block/blk-mq-cpu.c
  3659. index bb3ed488f7b5..628c6c13c482 100644
  3660. --- a/block/blk-mq-cpu.c
  3661. +++ b/block/blk-mq-cpu.c
  3662. @@ -16,7 +16,7 @@
  3663. #include "blk-mq.h"
  3664. static LIST_HEAD(blk_mq_cpu_notify_list);
  3665. -static DEFINE_RAW_SPINLOCK(blk_mq_cpu_notify_lock);
  3666. +static DEFINE_SPINLOCK(blk_mq_cpu_notify_lock);
  3667. static int blk_mq_main_cpu_notify(struct notifier_block *self,
  3668. unsigned long action, void *hcpu)
  3669. @@ -25,7 +25,10 @@ static int blk_mq_main_cpu_notify(struct notifier_block *self,
  3670. struct blk_mq_cpu_notifier *notify;
  3671. int ret = NOTIFY_OK;
  3672. - raw_spin_lock(&blk_mq_cpu_notify_lock);
  3673. + if (action != CPU_POST_DEAD)
  3674. + return NOTIFY_OK;
  3675. +
  3676. + spin_lock(&blk_mq_cpu_notify_lock);
  3677. list_for_each_entry(notify, &blk_mq_cpu_notify_list, list) {
  3678. ret = notify->notify(notify->data, action, cpu);
  3679. @@ -33,7 +36,7 @@ static int blk_mq_main_cpu_notify(struct notifier_block *self,
  3680. break;
  3681. }
  3682. - raw_spin_unlock(&blk_mq_cpu_notify_lock);
  3683. + spin_unlock(&blk_mq_cpu_notify_lock);
  3684. return ret;
  3685. }
  3686. @@ -41,16 +44,16 @@ void blk_mq_register_cpu_notifier(struct blk_mq_cpu_notifier *notifier)
  3687. {
  3688. BUG_ON(!notifier->notify);
  3689. - raw_spin_lock(&blk_mq_cpu_notify_lock);
  3690. + spin_lock(&blk_mq_cpu_notify_lock);
  3691. list_add_tail(&notifier->list, &blk_mq_cpu_notify_list);
  3692. - raw_spin_unlock(&blk_mq_cpu_notify_lock);
  3693. + spin_unlock(&blk_mq_cpu_notify_lock);
  3694. }
  3695. void blk_mq_unregister_cpu_notifier(struct blk_mq_cpu_notifier *notifier)
  3696. {
  3697. - raw_spin_lock(&blk_mq_cpu_notify_lock);
  3698. + spin_lock(&blk_mq_cpu_notify_lock);
  3699. list_del(&notifier->list);
  3700. - raw_spin_unlock(&blk_mq_cpu_notify_lock);
  3701. + spin_unlock(&blk_mq_cpu_notify_lock);
  3702. }
  3703. void blk_mq_init_cpu_notifier(struct blk_mq_cpu_notifier *notifier,
  3704. diff --git a/block/blk-mq.c b/block/blk-mq.c
  3705. index 0d1af3e44efb..e4fc80184dd8 100644
  3706. --- a/block/blk-mq.c
  3707. +++ b/block/blk-mq.c
  3708. @@ -92,7 +92,7 @@ EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_start);
  3709. static void blk_mq_freeze_queue_wait(struct request_queue *q)
  3710. {
  3711. - wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
  3712. + swait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
  3713. }
  3714. /*
  3715. @@ -130,7 +130,7 @@ void blk_mq_unfreeze_queue(struct request_queue *q)
  3716. WARN_ON_ONCE(freeze_depth < 0);
  3717. if (!freeze_depth) {
  3718. percpu_ref_reinit(&q->q_usage_counter);
  3719. - wake_up_all(&q->mq_freeze_wq);
  3720. + swake_up_all(&q->mq_freeze_wq);
  3721. }
  3722. }
  3723. EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
  3724. @@ -149,7 +149,7 @@ void blk_mq_wake_waiters(struct request_queue *q)
  3725. * dying, we need to ensure that processes currently waiting on
  3726. * the queue are notified as well.
  3727. */
  3728. - wake_up_all(&q->mq_freeze_wq);
  3729. + swake_up_all(&q->mq_freeze_wq);
  3730. }
  3731. bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
  3732. @@ -196,6 +196,9 @@ static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
  3733. rq->resid_len = 0;
  3734. rq->sense = NULL;
  3735. +#ifdef CONFIG_PREEMPT_RT_FULL
  3736. + INIT_WORK(&rq->work, __blk_mq_complete_request_remote_work);
  3737. +#endif
  3738. INIT_LIST_HEAD(&rq->timeout_list);
  3739. rq->timeout = 0;
  3740. @@ -325,6 +328,17 @@ void blk_mq_end_request(struct request *rq, int error)
  3741. }
  3742. EXPORT_SYMBOL(blk_mq_end_request);
  3743. +#ifdef CONFIG_PREEMPT_RT_FULL
  3744. +
  3745. +void __blk_mq_complete_request_remote_work(struct work_struct *work)
  3746. +{
  3747. + struct request *rq = container_of(work, struct request, work);
  3748. +
  3749. + rq->q->softirq_done_fn(rq);
  3750. +}
  3751. +
  3752. +#else
  3753. +
  3754. static void __blk_mq_complete_request_remote(void *data)
  3755. {
  3756. struct request *rq = data;
  3757. @@ -332,6 +346,8 @@ static void __blk_mq_complete_request_remote(void *data)
  3758. rq->q->softirq_done_fn(rq);
  3759. }
  3760. +#endif
  3761. +
  3762. static void blk_mq_ipi_complete_request(struct request *rq)
  3763. {
  3764. struct blk_mq_ctx *ctx = rq->mq_ctx;
  3765. @@ -343,19 +359,23 @@ static void blk_mq_ipi_complete_request(struct request *rq)
  3766. return;
  3767. }
  3768. - cpu = get_cpu();
  3769. + cpu = get_cpu_light();
  3770. if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
  3771. shared = cpus_share_cache(cpu, ctx->cpu);
  3772. if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
  3773. +#ifdef CONFIG_PREEMPT_RT_FULL
  3774. + schedule_work_on(ctx->cpu, &rq->work);
  3775. +#else
  3776. rq->csd.func = __blk_mq_complete_request_remote;
  3777. rq->csd.info = rq;
  3778. rq->csd.flags = 0;
  3779. smp_call_function_single_async(ctx->cpu, &rq->csd);
  3780. +#endif
  3781. } else {
  3782. rq->q->softirq_done_fn(rq);
  3783. }
  3784. - put_cpu();
  3785. + put_cpu_light();
  3786. }
  3787. static void __blk_mq_complete_request(struct request *rq)
  3788. @@ -862,14 +882,14 @@ void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
  3789. return;
  3790. if (!async) {
  3791. - int cpu = get_cpu();
  3792. + int cpu = get_cpu_light();
  3793. if (cpumask_test_cpu(cpu, hctx->cpumask)) {
  3794. __blk_mq_run_hw_queue(hctx);
  3795. - put_cpu();
  3796. + put_cpu_light();
  3797. return;
  3798. }
  3799. - put_cpu();
  3800. + put_cpu_light();
  3801. }
  3802. kblockd_schedule_delayed_work_on(blk_mq_hctx_next_cpu(hctx),
  3803. @@ -1616,7 +1636,7 @@ static int blk_mq_hctx_notify(void *data, unsigned long action,
  3804. {
  3805. struct blk_mq_hw_ctx *hctx = data;
  3806. - if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
  3807. + if (action == CPU_POST_DEAD)
  3808. return blk_mq_hctx_cpu_offline(hctx, cpu);
  3809. /*
  3810. diff --git a/block/blk-mq.h b/block/blk-mq.h
  3811. index 713820b47b31..3cb6feb4fe23 100644
  3812. --- a/block/blk-mq.h
  3813. +++ b/block/blk-mq.h
  3814. @@ -74,7 +74,10 @@ struct blk_align_bitmap {
  3815. static inline struct blk_mq_ctx *__blk_mq_get_ctx(struct request_queue *q,
  3816. unsigned int cpu)
  3817. {
  3818. - return per_cpu_ptr(q->queue_ctx, cpu);
  3819. + struct blk_mq_ctx *ctx;
  3820. +
  3821. + ctx = per_cpu_ptr(q->queue_ctx, cpu);
  3822. + return ctx;
  3823. }
  3824. /*
  3825. @@ -85,12 +88,12 @@ static inline struct blk_mq_ctx *__blk_mq_get_ctx(struct request_queue *q,
  3826. */
  3827. static inline struct blk_mq_ctx *blk_mq_get_ctx(struct request_queue *q)
  3828. {
  3829. - return __blk_mq_get_ctx(q, get_cpu());
  3830. + return __blk_mq_get_ctx(q, get_cpu_light());
  3831. }
  3832. static inline void blk_mq_put_ctx(struct blk_mq_ctx *ctx)
  3833. {
  3834. - put_cpu();
  3835. + put_cpu_light();
  3836. }
  3837. struct blk_mq_alloc_data {
  3838. diff --git a/block/blk-softirq.c b/block/blk-softirq.c
  3839. index 53b1737e978d..81c3c0a62edf 100644
  3840. --- a/block/blk-softirq.c
  3841. +++ b/block/blk-softirq.c
  3842. @@ -51,6 +51,7 @@ static void trigger_softirq(void *data)
  3843. raise_softirq_irqoff(BLOCK_SOFTIRQ);
  3844. local_irq_restore(flags);
  3845. + preempt_check_resched_rt();
  3846. }
  3847. /*
  3848. @@ -93,6 +94,7 @@ static int blk_cpu_notify(struct notifier_block *self, unsigned long action,
  3849. this_cpu_ptr(&blk_cpu_done));
  3850. raise_softirq_irqoff(BLOCK_SOFTIRQ);
  3851. local_irq_enable();
  3852. + preempt_check_resched_rt();
  3853. }
  3854. return NOTIFY_OK;
  3855. @@ -150,6 +152,7 @@ do_local:
  3856. goto do_local;
  3857. local_irq_restore(flags);
  3858. + preempt_check_resched_rt();
  3859. }
  3860. /**
  3861. diff --git a/block/bounce.c b/block/bounce.c
  3862. index 1cb5dd3a5da1..2f1ec8a67cbe 100644
  3863. --- a/block/bounce.c
  3864. +++ b/block/bounce.c
  3865. @@ -55,11 +55,11 @@ static void bounce_copy_vec(struct bio_vec *to, unsigned char *vfrom)
  3866. unsigned long flags;
  3867. unsigned char *vto;
  3868. - local_irq_save(flags);
  3869. + local_irq_save_nort(flags);
  3870. vto = kmap_atomic(to->bv_page);
  3871. memcpy(vto + to->bv_offset, vfrom, to->bv_len);
  3872. kunmap_atomic(vto);
  3873. - local_irq_restore(flags);
  3874. + local_irq_restore_nort(flags);
  3875. }
  3876. #else /* CONFIG_HIGHMEM */
  3877. diff --git a/crypto/algapi.c b/crypto/algapi.c
  3878. index 43f5bdb6b570..b54fd9187d12 100644
  3879. --- a/crypto/algapi.c
  3880. +++ b/crypto/algapi.c
  3881. @@ -720,13 +720,13 @@ EXPORT_SYMBOL_GPL(crypto_spawn_tfm2);
  3882. int crypto_register_notifier(struct notifier_block *nb)
  3883. {
  3884. - return blocking_notifier_chain_register(&crypto_chain, nb);
  3885. + return srcu_notifier_chain_register(&crypto_chain, nb);
  3886. }
  3887. EXPORT_SYMBOL_GPL(crypto_register_notifier);
  3888. int crypto_unregister_notifier(struct notifier_block *nb)
  3889. {
  3890. - return blocking_notifier_chain_unregister(&crypto_chain, nb);
  3891. + return srcu_notifier_chain_unregister(&crypto_chain, nb);
  3892. }
  3893. EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
  3894. diff --git a/crypto/api.c b/crypto/api.c
  3895. index bbc147cb5dec..bc1a848f02ec 100644
  3896. --- a/crypto/api.c
  3897. +++ b/crypto/api.c
  3898. @@ -31,7 +31,7 @@ EXPORT_SYMBOL_GPL(crypto_alg_list);
  3899. DECLARE_RWSEM(crypto_alg_sem);
  3900. EXPORT_SYMBOL_GPL(crypto_alg_sem);
  3901. -BLOCKING_NOTIFIER_HEAD(crypto_chain);
  3902. +SRCU_NOTIFIER_HEAD(crypto_chain);
  3903. EXPORT_SYMBOL_GPL(crypto_chain);
  3904. static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg);
  3905. @@ -236,10 +236,10 @@ int crypto_probing_notify(unsigned long val, void *v)
  3906. {
  3907. int ok;
  3908. - ok = blocking_notifier_call_chain(&crypto_chain, val, v);
  3909. + ok = srcu_notifier_call_chain(&crypto_chain, val, v);
  3910. if (ok == NOTIFY_DONE) {
  3911. request_module("cryptomgr");
  3912. - ok = blocking_notifier_call_chain(&crypto_chain, val, v);
  3913. + ok = srcu_notifier_call_chain(&crypto_chain, val, v);
  3914. }
  3915. return ok;
  3916. diff --git a/crypto/internal.h b/crypto/internal.h
  3917. index 00e42a3ed814..2e85551e235f 100644
  3918. --- a/crypto/internal.h
  3919. +++ b/crypto/internal.h
  3920. @@ -47,7 +47,7 @@ struct crypto_larval {
  3921. extern struct list_head crypto_alg_list;
  3922. extern struct rw_semaphore crypto_alg_sem;
  3923. -extern struct blocking_notifier_head crypto_chain;
  3924. +extern struct srcu_notifier_head crypto_chain;
  3925. #ifdef CONFIG_PROC_FS
  3926. void __init crypto_init_proc(void);
  3927. @@ -143,7 +143,7 @@ static inline int crypto_is_moribund(struct crypto_alg *alg)
  3928. static inline void crypto_notify(unsigned long val, void *v)
  3929. {
  3930. - blocking_notifier_call_chain(&crypto_chain, val, v);
  3931. + srcu_notifier_call_chain(&crypto_chain, val, v);
  3932. }
  3933. #endif /* _CRYPTO_INTERNAL_H */
  3934. diff --git a/drivers/acpi/acpica/acglobal.h b/drivers/acpi/acpica/acglobal.h
  3935. index faa97604d878..941497f31cf0 100644
  3936. --- a/drivers/acpi/acpica/acglobal.h
  3937. +++ b/drivers/acpi/acpica/acglobal.h
  3938. @@ -116,7 +116,7 @@ ACPI_GLOBAL(u8, acpi_gbl_global_lock_pending);
  3939. * interrupt level
  3940. */
  3941. ACPI_GLOBAL(acpi_spinlock, acpi_gbl_gpe_lock); /* For GPE data structs and registers */
  3942. -ACPI_GLOBAL(acpi_spinlock, acpi_gbl_hardware_lock); /* For ACPI H/W except GPE registers */
  3943. +ACPI_GLOBAL(acpi_raw_spinlock, acpi_gbl_hardware_lock); /* For ACPI H/W except GPE registers */
  3944. ACPI_GLOBAL(acpi_spinlock, acpi_gbl_reference_count_lock);
  3945. /* Mutex for _OSI support */
  3946. diff --git a/drivers/acpi/acpica/hwregs.c b/drivers/acpi/acpica/hwregs.c
  3947. index 3cf77afd142c..dc32e72132f1 100644
  3948. --- a/drivers/acpi/acpica/hwregs.c
  3949. +++ b/drivers/acpi/acpica/hwregs.c
  3950. @@ -269,14 +269,14 @@ acpi_status acpi_hw_clear_acpi_status(void)
  3951. ACPI_BITMASK_ALL_FIXED_STATUS,
  3952. ACPI_FORMAT_UINT64(acpi_gbl_xpm1a_status.address)));
  3953. - lock_flags = acpi_os_acquire_lock(acpi_gbl_hardware_lock);
  3954. + raw_spin_lock_irqsave(acpi_gbl_hardware_lock, lock_flags);
  3955. /* Clear the fixed events in PM1 A/B */
  3956. status = acpi_hw_register_write(ACPI_REGISTER_PM1_STATUS,
  3957. ACPI_BITMASK_ALL_FIXED_STATUS);
  3958. - acpi_os_release_lock(acpi_gbl_hardware_lock, lock_flags);
  3959. + raw_spin_unlock_irqrestore(acpi_gbl_hardware_lock, lock_flags);
  3960. if (ACPI_FAILURE(status)) {
  3961. goto exit;
  3962. diff --git a/drivers/acpi/acpica/hwxface.c b/drivers/acpi/acpica/hwxface.c
  3963. index b2e50d8007fe..ff007084dc48 100644
  3964. --- a/drivers/acpi/acpica/hwxface.c
  3965. +++ b/drivers/acpi/acpica/hwxface.c
  3966. @@ -374,7 +374,7 @@ acpi_status acpi_write_bit_register(u32 register_id, u32 value)
  3967. return_ACPI_STATUS(AE_BAD_PARAMETER);
  3968. }
  3969. - lock_flags = acpi_os_acquire_lock(acpi_gbl_hardware_lock);
  3970. + raw_spin_lock_irqsave(acpi_gbl_hardware_lock, lock_flags);
  3971. /*
  3972. * At this point, we know that the parent register is one of the
  3973. @@ -435,7 +435,7 @@ acpi_status acpi_write_bit_register(u32 register_id, u32 value)
  3974. unlock_and_exit:
  3975. - acpi_os_release_lock(acpi_gbl_hardware_lock, lock_flags);
  3976. + raw_spin_unlock_irqrestore(acpi_gbl_hardware_lock, lock_flags);
  3977. return_ACPI_STATUS(status);
  3978. }
  3979. diff --git a/drivers/acpi/acpica/utmutex.c b/drivers/acpi/acpica/utmutex.c
  3980. index ce406e39b669..41a75eb3ae9d 100644
  3981. --- a/drivers/acpi/acpica/utmutex.c
  3982. +++ b/drivers/acpi/acpica/utmutex.c
  3983. @@ -88,7 +88,7 @@ acpi_status acpi_ut_mutex_initialize(void)
  3984. return_ACPI_STATUS (status);
  3985. }
  3986. - status = acpi_os_create_lock (&acpi_gbl_hardware_lock);
  3987. + status = acpi_os_create_raw_lock (&acpi_gbl_hardware_lock);
  3988. if (ACPI_FAILURE (status)) {
  3989. return_ACPI_STATUS (status);
  3990. }
  3991. @@ -156,7 +156,7 @@ void acpi_ut_mutex_terminate(void)
  3992. /* Delete the spinlocks */
  3993. acpi_os_delete_lock(acpi_gbl_gpe_lock);
  3994. - acpi_os_delete_lock(acpi_gbl_hardware_lock);
  3995. + acpi_os_delete_raw_lock(acpi_gbl_hardware_lock);
  3996. acpi_os_delete_lock(acpi_gbl_reference_count_lock);
  3997. /* Delete the reader/writer lock */
  3998. diff --git a/drivers/ata/libata-sff.c b/drivers/ata/libata-sff.c
  3999. index 7dbba387d12a..65beb7abb4e7 100644
  4000. --- a/drivers/ata/libata-sff.c
  4001. +++ b/drivers/ata/libata-sff.c
  4002. @@ -678,9 +678,9 @@ unsigned int ata_sff_data_xfer_noirq(struct ata_device *dev, unsigned char *buf,
  4003. unsigned long flags;
  4004. unsigned int consumed;
  4005. - local_irq_save(flags);
  4006. + local_irq_save_nort(flags);
  4007. consumed = ata_sff_data_xfer32(dev, buf, buflen, rw);
  4008. - local_irq_restore(flags);
  4009. + local_irq_restore_nort(flags);
  4010. return consumed;
  4011. }
  4012. @@ -719,7 +719,7 @@ static void ata_pio_sector(struct ata_queued_cmd *qc)
  4013. unsigned long flags;
  4014. /* FIXME: use a bounce buffer */
  4015. - local_irq_save(flags);
  4016. + local_irq_save_nort(flags);
  4017. buf = kmap_atomic(page);
  4018. /* do the actual data transfer */
  4019. @@ -727,7 +727,7 @@ static void ata_pio_sector(struct ata_queued_cmd *qc)
  4020. do_write);
  4021. kunmap_atomic(buf);
  4022. - local_irq_restore(flags);
  4023. + local_irq_restore_nort(flags);
  4024. } else {
  4025. buf = page_address(page);
  4026. ap->ops->sff_data_xfer(qc->dev, buf + offset, qc->sect_size,
  4027. @@ -864,7 +864,7 @@ next_sg:
  4028. unsigned long flags;
  4029. /* FIXME: use bounce buffer */
  4030. - local_irq_save(flags);
  4031. + local_irq_save_nort(flags);
  4032. buf = kmap_atomic(page);
  4033. /* do the actual data transfer */
  4034. @@ -872,7 +872,7 @@ next_sg:
  4035. count, rw);
  4036. kunmap_atomic(buf);
  4037. - local_irq_restore(flags);
  4038. + local_irq_restore_nort(flags);
  4039. } else {
  4040. buf = page_address(page);
  4041. consumed = ap->ops->sff_data_xfer(dev, buf + offset,
  4042. diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
  4043. index 62a93b685c54..5ad3c4e652a8 100644
  4044. --- a/drivers/block/zram/zram_drv.c
  4045. +++ b/drivers/block/zram/zram_drv.c
  4046. @@ -520,6 +520,8 @@ static struct zram_meta *zram_meta_alloc(char *pool_name, u64 disksize)
  4047. goto out_error;
  4048. }
  4049. + zram_meta_init_table_locks(meta, disksize);
  4050. +
  4051. return meta;
  4052. out_error:
  4053. @@ -568,12 +570,12 @@ static int zram_decompress_page(struct zram *zram, char *mem, u32 index)
  4054. unsigned long handle;
  4055. size_t size;
  4056. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  4057. + zram_lock_table(&meta->table[index]);
  4058. handle = meta->table[index].handle;
  4059. size = zram_get_obj_size(meta, index);
  4060. if (!handle || zram_test_flag(meta, index, ZRAM_ZERO)) {
  4061. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  4062. + zram_unlock_table(&meta->table[index]);
  4063. memset(mem, 0, PAGE_SIZE);
  4064. return 0;
  4065. }
  4066. @@ -584,7 +586,7 @@ static int zram_decompress_page(struct zram *zram, char *mem, u32 index)
  4067. else
  4068. ret = zcomp_decompress(zram->comp, cmem, size, mem);
  4069. zs_unmap_object(meta->mem_pool, handle);
  4070. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  4071. + zram_unlock_table(&meta->table[index]);
  4072. /* Should NEVER happen. Return bio error if it does. */
  4073. if (unlikely(ret)) {
  4074. @@ -604,14 +606,14 @@ static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
  4075. struct zram_meta *meta = zram->meta;
  4076. page = bvec->bv_page;
  4077. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  4078. + zram_lock_table(&meta->table[index]);
  4079. if (unlikely(!meta->table[index].handle) ||
  4080. zram_test_flag(meta, index, ZRAM_ZERO)) {
  4081. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  4082. + zram_unlock_table(&meta->table[index]);
  4083. handle_zero_page(bvec);
  4084. return 0;
  4085. }
  4086. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  4087. + zram_unlock_table(&meta->table[index]);
  4088. if (is_partial_io(bvec))
  4089. /* Use a temporary buffer to decompress the page */
  4090. @@ -689,10 +691,10 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
  4091. if (user_mem)
  4092. kunmap_atomic(user_mem);
  4093. /* Free memory associated with this sector now. */
  4094. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  4095. + zram_lock_table(&meta->table[index]);
  4096. zram_free_page(zram, index);
  4097. zram_set_flag(meta, index, ZRAM_ZERO);
  4098. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  4099. + zram_unlock_table(&meta->table[index]);
  4100. atomic64_inc(&zram->stats.zero_pages);
  4101. ret = 0;
  4102. @@ -752,12 +754,12 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
  4103. * Free memory associated with this sector
  4104. * before overwriting unused sectors.
  4105. */
  4106. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  4107. + zram_lock_table(&meta->table[index]);
  4108. zram_free_page(zram, index);
  4109. meta->table[index].handle = handle;
  4110. zram_set_obj_size(meta, index, clen);
  4111. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  4112. + zram_unlock_table(&meta->table[index]);
  4113. /* Update stats */
  4114. atomic64_add(clen, &zram->stats.compr_data_size);
  4115. @@ -800,9 +802,9 @@ static void zram_bio_discard(struct zram *zram, u32 index,
  4116. }
  4117. while (n >= PAGE_SIZE) {
  4118. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  4119. + zram_lock_table(&meta->table[index]);
  4120. zram_free_page(zram, index);
  4121. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  4122. + zram_unlock_table(&meta->table[index]);
  4123. atomic64_inc(&zram->stats.notify_free);
  4124. index++;
  4125. n -= PAGE_SIZE;
  4126. @@ -928,9 +930,9 @@ static void zram_slot_free_notify(struct block_device *bdev,
  4127. zram = bdev->bd_disk->private_data;
  4128. meta = zram->meta;
  4129. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  4130. + zram_lock_table(&meta->table[index]);
  4131. zram_free_page(zram, index);
  4132. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  4133. + zram_unlock_table(&meta->table[index]);
  4134. atomic64_inc(&zram->stats.notify_free);
  4135. }
  4136. diff --git a/drivers/block/zram/zram_drv.h b/drivers/block/zram/zram_drv.h
  4137. index 8e92339686d7..9e3e953d680e 100644
  4138. --- a/drivers/block/zram/zram_drv.h
  4139. +++ b/drivers/block/zram/zram_drv.h
  4140. @@ -72,6 +72,9 @@ enum zram_pageflags {
  4141. struct zram_table_entry {
  4142. unsigned long handle;
  4143. unsigned long value;
  4144. +#ifdef CONFIG_PREEMPT_RT_BASE
  4145. + spinlock_t lock;
  4146. +#endif
  4147. };
  4148. struct zram_stats {
  4149. @@ -119,4 +122,42 @@ struct zram {
  4150. */
  4151. bool claim; /* Protected by bdev->bd_mutex */
  4152. };
  4153. +
  4154. +#ifndef CONFIG_PREEMPT_RT_BASE
  4155. +static inline void zram_lock_table(struct zram_table_entry *table)
  4156. +{
  4157. + bit_spin_lock(ZRAM_ACCESS, &table->value);
  4158. +}
  4159. +
  4160. +static inline void zram_unlock_table(struct zram_table_entry *table)
  4161. +{
  4162. + bit_spin_unlock(ZRAM_ACCESS, &table->value);
  4163. +}
  4164. +
  4165. +static inline void zram_meta_init_table_locks(struct zram_meta *meta, u64 disksize) { }
  4166. +#else /* CONFIG_PREEMPT_RT_BASE */
  4167. +static inline void zram_lock_table(struct zram_table_entry *table)
  4168. +{
  4169. + spin_lock(&table->lock);
  4170. + __set_bit(ZRAM_ACCESS, &table->value);
  4171. +}
  4172. +
  4173. +static inline void zram_unlock_table(struct zram_table_entry *table)
  4174. +{
  4175. + __clear_bit(ZRAM_ACCESS, &table->value);
  4176. + spin_unlock(&table->lock);
  4177. +}
  4178. +
  4179. +static inline void zram_meta_init_table_locks(struct zram_meta *meta, u64 disksize)
  4180. +{
  4181. + size_t num_pages = disksize >> PAGE_SHIFT;
  4182. + size_t index;
  4183. +
  4184. + for (index = 0; index < num_pages; index++) {
  4185. + spinlock_t *lock = &meta->table[index].lock;
  4186. + spin_lock_init(lock);
  4187. + }
  4188. +}
  4189. +#endif /* CONFIG_PREEMPT_RT_BASE */
  4190. +
  4191. #endif
  4192. diff --git a/drivers/char/random.c b/drivers/char/random.c
  4193. index 491a4dce13fe..cf69b6b42208 100644
  4194. --- a/drivers/char/random.c
  4195. +++ b/drivers/char/random.c
  4196. @@ -799,8 +799,6 @@ static void add_timer_randomness(struct timer_rand_state *state, unsigned num)
  4197. } sample;
  4198. long delta, delta2, delta3;
  4199. - preempt_disable();
  4200. -
  4201. sample.jiffies = jiffies;
  4202. sample.cycles = random_get_entropy();
  4203. sample.num = num;
  4204. @@ -841,7 +839,6 @@ static void add_timer_randomness(struct timer_rand_state *state, unsigned num)
  4205. */
  4206. credit_entropy_bits(r, min_t(int, fls(delta>>1), 11));
  4207. }
  4208. - preempt_enable();
  4209. }
  4210. void add_input_randomness(unsigned int type, unsigned int code,
  4211. @@ -894,28 +891,27 @@ static __u32 get_reg(struct fast_pool *f, struct pt_regs *regs)
  4212. return *(ptr + f->reg_idx++);
  4213. }
  4214. -void add_interrupt_randomness(int irq, int irq_flags)
  4215. +void add_interrupt_randomness(int irq, int irq_flags, __u64 ip)
  4216. {
  4217. struct entropy_store *r;
  4218. struct fast_pool *fast_pool = this_cpu_ptr(&irq_randomness);
  4219. - struct pt_regs *regs = get_irq_regs();
  4220. unsigned long now = jiffies;
  4221. cycles_t cycles = random_get_entropy();
  4222. __u32 c_high, j_high;
  4223. - __u64 ip;
  4224. unsigned long seed;
  4225. int credit = 0;
  4226. if (cycles == 0)
  4227. - cycles = get_reg(fast_pool, regs);
  4228. + cycles = get_reg(fast_pool, NULL);
  4229. c_high = (sizeof(cycles) > 4) ? cycles >> 32 : 0;
  4230. j_high = (sizeof(now) > 4) ? now >> 32 : 0;
  4231. fast_pool->pool[0] ^= cycles ^ j_high ^ irq;
  4232. fast_pool->pool[1] ^= now ^ c_high;
  4233. - ip = regs ? instruction_pointer(regs) : _RET_IP_;
  4234. + if (!ip)
  4235. + ip = _RET_IP_;
  4236. fast_pool->pool[2] ^= ip;
  4237. fast_pool->pool[3] ^= (sizeof(ip) > 4) ? ip >> 32 :
  4238. - get_reg(fast_pool, regs);
  4239. + get_reg(fast_pool, NULL);
  4240. fast_mix(fast_pool);
  4241. add_interrupt_bench(cycles);
  4242. diff --git a/drivers/clk/at91/clk-generated.c b/drivers/clk/at91/clk-generated.c
  4243. index abc80949e1dd..4ad3298eb372 100644
  4244. --- a/drivers/clk/at91/clk-generated.c
  4245. +++ b/drivers/clk/at91/clk-generated.c
  4246. @@ -15,8 +15,8 @@
  4247. #include <linux/clkdev.h>
  4248. #include <linux/clk/at91_pmc.h>
  4249. #include <linux/of.h>
  4250. -#include <linux/of_address.h>
  4251. -#include <linux/io.h>
  4252. +#include <linux/mfd/syscon.h>
  4253. +#include <linux/regmap.h>
  4254. #include "pmc.h"
  4255. @@ -28,8 +28,9 @@
  4256. struct clk_generated {
  4257. struct clk_hw hw;
  4258. - struct at91_pmc *pmc;
  4259. + struct regmap *regmap;
  4260. struct clk_range range;
  4261. + spinlock_t *lock;
  4262. u32 id;
  4263. u32 gckdiv;
  4264. u8 parent_id;
  4265. @@ -41,49 +42,52 @@ struct clk_generated {
  4266. static int clk_generated_enable(struct clk_hw *hw)
  4267. {
  4268. struct clk_generated *gck = to_clk_generated(hw);
  4269. - struct at91_pmc *pmc = gck->pmc;
  4270. - u32 tmp;
  4271. + unsigned long flags;
  4272. pr_debug("GCLK: %s, gckdiv = %d, parent id = %d\n",
  4273. __func__, gck->gckdiv, gck->parent_id);
  4274. - pmc_lock(pmc);
  4275. - pmc_write(pmc, AT91_PMC_PCR, (gck->id & AT91_PMC_PCR_PID_MASK));
  4276. - tmp = pmc_read(pmc, AT91_PMC_PCR) &
  4277. - ~(AT91_PMC_PCR_GCKDIV_MASK | AT91_PMC_PCR_GCKCSS_MASK);
  4278. - pmc_write(pmc, AT91_PMC_PCR, tmp | AT91_PMC_PCR_GCKCSS(gck->parent_id)
  4279. - | AT91_PMC_PCR_CMD
  4280. - | AT91_PMC_PCR_GCKDIV(gck->gckdiv)
  4281. - | AT91_PMC_PCR_GCKEN);
  4282. - pmc_unlock(pmc);
  4283. + spin_lock_irqsave(gck->lock, flags);
  4284. + regmap_write(gck->regmap, AT91_PMC_PCR,
  4285. + (gck->id & AT91_PMC_PCR_PID_MASK));
  4286. + regmap_update_bits(gck->regmap, AT91_PMC_PCR,
  4287. + AT91_PMC_PCR_GCKDIV_MASK | AT91_PMC_PCR_GCKCSS_MASK |
  4288. + AT91_PMC_PCR_CMD | AT91_PMC_PCR_GCKEN,
  4289. + AT91_PMC_PCR_GCKCSS(gck->parent_id) |
  4290. + AT91_PMC_PCR_CMD |
  4291. + AT91_PMC_PCR_GCKDIV(gck->gckdiv) |
  4292. + AT91_PMC_PCR_GCKEN);
  4293. + spin_unlock_irqrestore(gck->lock, flags);
  4294. return 0;
  4295. }
  4296. static void clk_generated_disable(struct clk_hw *hw)
  4297. {
  4298. struct clk_generated *gck = to_clk_generated(hw);
  4299. - struct at91_pmc *pmc = gck->pmc;
  4300. - u32 tmp;
  4301. -
  4302. - pmc_lock(pmc);
  4303. - pmc_write(pmc, AT91_PMC_PCR, (gck->id & AT91_PMC_PCR_PID_MASK));
  4304. - tmp = pmc_read(pmc, AT91_PMC_PCR) & ~AT91_PMC_PCR_GCKEN;
  4305. - pmc_write(pmc, AT91_PMC_PCR, tmp | AT91_PMC_PCR_CMD);
  4306. - pmc_unlock(pmc);
  4307. + unsigned long flags;
  4308. +
  4309. + spin_lock_irqsave(gck->lock, flags);
  4310. + regmap_write(gck->regmap, AT91_PMC_PCR,
  4311. + (gck->id & AT91_PMC_PCR_PID_MASK));
  4312. + regmap_update_bits(gck->regmap, AT91_PMC_PCR,
  4313. + AT91_PMC_PCR_CMD | AT91_PMC_PCR_GCKEN,
  4314. + AT91_PMC_PCR_CMD);
  4315. + spin_unlock_irqrestore(gck->lock, flags);
  4316. }
  4317. static int clk_generated_is_enabled(struct clk_hw *hw)
  4318. {
  4319. struct clk_generated *gck = to_clk_generated(hw);
  4320. - struct at91_pmc *pmc = gck->pmc;
  4321. - int ret;
  4322. + unsigned long flags;
  4323. + unsigned int status;
  4324. - pmc_lock(pmc);
  4325. - pmc_write(pmc, AT91_PMC_PCR, (gck->id & AT91_PMC_PCR_PID_MASK));
  4326. - ret = !!(pmc_read(pmc, AT91_PMC_PCR) & AT91_PMC_PCR_GCKEN);
  4327. - pmc_unlock(pmc);
  4328. + spin_lock_irqsave(gck->lock, flags);
  4329. + regmap_write(gck->regmap, AT91_PMC_PCR,
  4330. + (gck->id & AT91_PMC_PCR_PID_MASK));
  4331. + regmap_read(gck->regmap, AT91_PMC_PCR, &status);
  4332. + spin_unlock_irqrestore(gck->lock, flags);
  4333. - return ret;
  4334. + return status & AT91_PMC_PCR_GCKEN ? 1 : 0;
  4335. }
  4336. static unsigned long
  4337. @@ -214,13 +218,14 @@ static const struct clk_ops generated_ops = {
  4338. */
  4339. static void clk_generated_startup(struct clk_generated *gck)
  4340. {
  4341. - struct at91_pmc *pmc = gck->pmc;
  4342. u32 tmp;
  4343. + unsigned long flags;
  4344. - pmc_lock(pmc);
  4345. - pmc_write(pmc, AT91_PMC_PCR, (gck->id & AT91_PMC_PCR_PID_MASK));
  4346. - tmp = pmc_read(pmc, AT91_PMC_PCR);
  4347. - pmc_unlock(pmc);
  4348. + spin_lock_irqsave(gck->lock, flags);
  4349. + regmap_write(gck->regmap, AT91_PMC_PCR,
  4350. + (gck->id & AT91_PMC_PCR_PID_MASK));
  4351. + regmap_read(gck->regmap, AT91_PMC_PCR, &tmp);
  4352. + spin_unlock_irqrestore(gck->lock, flags);
  4353. gck->parent_id = (tmp & AT91_PMC_PCR_GCKCSS_MASK)
  4354. >> AT91_PMC_PCR_GCKCSS_OFFSET;
  4355. @@ -229,8 +234,8 @@ static void clk_generated_startup(struct clk_generated *gck)
  4356. }
  4357. static struct clk * __init
  4358. -at91_clk_register_generated(struct at91_pmc *pmc, const char *name,
  4359. - const char **parent_names, u8 num_parents,
  4360. +at91_clk_register_generated(struct regmap *regmap, spinlock_t *lock, const char
  4361. + *name, const char **parent_names, u8 num_parents,
  4362. u8 id, const struct clk_range *range)
  4363. {
  4364. struct clk_generated *gck;
  4365. @@ -249,7 +254,8 @@ at91_clk_register_generated(struct at91_pmc *pmc, const char *name,
  4366. gck->id = id;
  4367. gck->hw.init = &init;
  4368. - gck->pmc = pmc;
  4369. + gck->regmap = regmap;
  4370. + gck->lock = lock;
  4371. gck->range = *range;
  4372. clk = clk_register(NULL, &gck->hw);
  4373. @@ -261,8 +267,7 @@ at91_clk_register_generated(struct at91_pmc *pmc, const char *name,
  4374. return clk;
  4375. }
  4376. -void __init of_sama5d2_clk_generated_setup(struct device_node *np,
  4377. - struct at91_pmc *pmc)
  4378. +void __init of_sama5d2_clk_generated_setup(struct device_node *np)
  4379. {
  4380. int num;
  4381. u32 id;
  4382. @@ -272,6 +277,7 @@ void __init of_sama5d2_clk_generated_setup(struct device_node *np,
  4383. const char *parent_names[GENERATED_SOURCE_MAX];
  4384. struct device_node *gcknp;
  4385. struct clk_range range = CLK_RANGE(0, 0);
  4386. + struct regmap *regmap;
  4387. num_parents = of_clk_get_parent_count(np);
  4388. if (num_parents <= 0 || num_parents > GENERATED_SOURCE_MAX)
  4389. @@ -283,6 +289,10 @@ void __init of_sama5d2_clk_generated_setup(struct device_node *np,
  4390. if (!num || num > PERIPHERAL_MAX)
  4391. return;
  4392. + regmap = syscon_node_to_regmap(of_get_parent(np));
  4393. + if (IS_ERR(regmap))
  4394. + return;
  4395. +
  4396. for_each_child_of_node(np, gcknp) {
  4397. if (of_property_read_u32(gcknp, "reg", &id))
  4398. continue;
  4399. @@ -296,11 +306,14 @@ void __init of_sama5d2_clk_generated_setup(struct device_node *np,
  4400. of_at91_get_clk_range(gcknp, "atmel,clk-output-range",
  4401. &range);
  4402. - clk = at91_clk_register_generated(pmc, name, parent_names,
  4403. - num_parents, id, &range);
  4404. + clk = at91_clk_register_generated(regmap, &pmc_pcr_lock, name,
  4405. + parent_names, num_parents,
  4406. + id, &range);
  4407. if (IS_ERR(clk))
  4408. continue;
  4409. of_clk_add_provider(gcknp, of_clk_src_simple_get, clk);
  4410. }
  4411. }
  4412. +CLK_OF_DECLARE(of_sama5d2_clk_generated_setup, "atmel,sama5d2-clk-generated",
  4413. + of_sama5d2_clk_generated_setup);
  4414. diff --git a/drivers/clk/at91/clk-h32mx.c b/drivers/clk/at91/clk-h32mx.c
  4415. index a165230e7eda..8e20c8a76db7 100644
  4416. --- a/drivers/clk/at91/clk-h32mx.c
  4417. +++ b/drivers/clk/at91/clk-h32mx.c
  4418. @@ -15,15 +15,9 @@
  4419. #include <linux/clk-provider.h>
  4420. #include <linux/clkdev.h>
  4421. #include <linux/clk/at91_pmc.h>
  4422. -#include <linux/delay.h>
  4423. #include <linux/of.h>
  4424. -#include <linux/of_address.h>
  4425. -#include <linux/of_irq.h>
  4426. -#include <linux/io.h>
  4427. -#include <linux/interrupt.h>
  4428. -#include <linux/irq.h>
  4429. -#include <linux/sched.h>
  4430. -#include <linux/wait.h>
  4431. +#include <linux/regmap.h>
  4432. +#include <linux/mfd/syscon.h>
  4433. #include "pmc.h"
  4434. @@ -31,7 +25,7 @@
  4435. struct clk_sama5d4_h32mx {
  4436. struct clk_hw hw;
  4437. - struct at91_pmc *pmc;
  4438. + struct regmap *regmap;
  4439. };
  4440. #define to_clk_sama5d4_h32mx(hw) container_of(hw, struct clk_sama5d4_h32mx, hw)
  4441. @@ -40,8 +34,10 @@ static unsigned long clk_sama5d4_h32mx_recalc_rate(struct clk_hw *hw,
  4442. unsigned long parent_rate)
  4443. {
  4444. struct clk_sama5d4_h32mx *h32mxclk = to_clk_sama5d4_h32mx(hw);
  4445. + unsigned int mckr;
  4446. - if (pmc_read(h32mxclk->pmc, AT91_PMC_MCKR) & AT91_PMC_H32MXDIV)
  4447. + regmap_read(h32mxclk->regmap, AT91_PMC_MCKR, &mckr);
  4448. + if (mckr & AT91_PMC_H32MXDIV)
  4449. return parent_rate / 2;
  4450. if (parent_rate > H32MX_MAX_FREQ)
  4451. @@ -70,18 +66,16 @@ static int clk_sama5d4_h32mx_set_rate(struct clk_hw *hw, unsigned long rate,
  4452. unsigned long parent_rate)
  4453. {
  4454. struct clk_sama5d4_h32mx *h32mxclk = to_clk_sama5d4_h32mx(hw);
  4455. - struct at91_pmc *pmc = h32mxclk->pmc;
  4456. - u32 tmp;
  4457. + u32 mckr = 0;
  4458. if (parent_rate != rate && (parent_rate / 2) != rate)
  4459. return -EINVAL;
  4460. - pmc_lock(pmc);
  4461. - tmp = pmc_read(pmc, AT91_PMC_MCKR) & ~AT91_PMC_H32MXDIV;
  4462. if ((parent_rate / 2) == rate)
  4463. - tmp |= AT91_PMC_H32MXDIV;
  4464. - pmc_write(pmc, AT91_PMC_MCKR, tmp);
  4465. - pmc_unlock(pmc);
  4466. + mckr = AT91_PMC_H32MXDIV;
  4467. +
  4468. + regmap_update_bits(h32mxclk->regmap, AT91_PMC_MCKR,
  4469. + AT91_PMC_H32MXDIV, mckr);
  4470. return 0;
  4471. }
  4472. @@ -92,14 +86,18 @@ static const struct clk_ops h32mx_ops = {
  4473. .set_rate = clk_sama5d4_h32mx_set_rate,
  4474. };
  4475. -void __init of_sama5d4_clk_h32mx_setup(struct device_node *np,
  4476. - struct at91_pmc *pmc)
  4477. +static void __init of_sama5d4_clk_h32mx_setup(struct device_node *np)
  4478. {
  4479. struct clk_sama5d4_h32mx *h32mxclk;
  4480. struct clk_init_data init;
  4481. const char *parent_name;
  4482. + struct regmap *regmap;
  4483. struct clk *clk;
  4484. + regmap = syscon_node_to_regmap(of_get_parent(np));
  4485. + if (IS_ERR(regmap))
  4486. + return;
  4487. +
  4488. h32mxclk = kzalloc(sizeof(*h32mxclk), GFP_KERNEL);
  4489. if (!h32mxclk)
  4490. return;
  4491. @@ -113,7 +111,7 @@ void __init of_sama5d4_clk_h32mx_setup(struct device_node *np,
  4492. init.flags = CLK_SET_RATE_GATE;
  4493. h32mxclk->hw.init = &init;
  4494. - h32mxclk->pmc = pmc;
  4495. + h32mxclk->regmap = regmap;
  4496. clk = clk_register(NULL, &h32mxclk->hw);
  4497. if (IS_ERR(clk)) {
  4498. @@ -123,3 +121,5 @@ void __init of_sama5d4_clk_h32mx_setup(struct device_node *np,
  4499. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  4500. }
  4501. +CLK_OF_DECLARE(of_sama5d4_clk_h32mx_setup, "atmel,sama5d4-clk-h32mx",
  4502. + of_sama5d4_clk_h32mx_setup);
  4503. diff --git a/drivers/clk/at91/clk-main.c b/drivers/clk/at91/clk-main.c
  4504. index fd7247deabdc..4bfc94d6c26e 100644
  4505. --- a/drivers/clk/at91/clk-main.c
  4506. +++ b/drivers/clk/at91/clk-main.c
  4507. @@ -13,13 +13,8 @@
  4508. #include <linux/clk/at91_pmc.h>
  4509. #include <linux/delay.h>
  4510. #include <linux/of.h>
  4511. -#include <linux/of_address.h>
  4512. -#include <linux/of_irq.h>
  4513. -#include <linux/io.h>
  4514. -#include <linux/interrupt.h>
  4515. -#include <linux/irq.h>
  4516. -#include <linux/sched.h>
  4517. -#include <linux/wait.h>
  4518. +#include <linux/mfd/syscon.h>
  4519. +#include <linux/regmap.h>
  4520. #include "pmc.h"
  4521. @@ -34,18 +29,14 @@
  4522. struct clk_main_osc {
  4523. struct clk_hw hw;
  4524. - struct at91_pmc *pmc;
  4525. - unsigned int irq;
  4526. - wait_queue_head_t wait;
  4527. + struct regmap *regmap;
  4528. };
  4529. #define to_clk_main_osc(hw) container_of(hw, struct clk_main_osc, hw)
  4530. struct clk_main_rc_osc {
  4531. struct clk_hw hw;
  4532. - struct at91_pmc *pmc;
  4533. - unsigned int irq;
  4534. - wait_queue_head_t wait;
  4535. + struct regmap *regmap;
  4536. unsigned long frequency;
  4537. unsigned long accuracy;
  4538. };
  4539. @@ -54,51 +45,47 @@ struct clk_main_rc_osc {
  4540. struct clk_rm9200_main {
  4541. struct clk_hw hw;
  4542. - struct at91_pmc *pmc;
  4543. + struct regmap *regmap;
  4544. };
  4545. #define to_clk_rm9200_main(hw) container_of(hw, struct clk_rm9200_main, hw)
  4546. struct clk_sam9x5_main {
  4547. struct clk_hw hw;
  4548. - struct at91_pmc *pmc;
  4549. - unsigned int irq;
  4550. - wait_queue_head_t wait;
  4551. + struct regmap *regmap;
  4552. u8 parent;
  4553. };
  4554. #define to_clk_sam9x5_main(hw) container_of(hw, struct clk_sam9x5_main, hw)
  4555. -static irqreturn_t clk_main_osc_irq_handler(int irq, void *dev_id)
  4556. +static inline bool clk_main_osc_ready(struct regmap *regmap)
  4557. {
  4558. - struct clk_main_osc *osc = dev_id;
  4559. + unsigned int status;
  4560. - wake_up(&osc->wait);
  4561. - disable_irq_nosync(osc->irq);
  4562. + regmap_read(regmap, AT91_PMC_SR, &status);
  4563. - return IRQ_HANDLED;
  4564. + return status & AT91_PMC_MOSCS;
  4565. }
  4566. static int clk_main_osc_prepare(struct clk_hw *hw)
  4567. {
  4568. struct clk_main_osc *osc = to_clk_main_osc(hw);
  4569. - struct at91_pmc *pmc = osc->pmc;
  4570. + struct regmap *regmap = osc->regmap;
  4571. u32 tmp;
  4572. - tmp = pmc_read(pmc, AT91_CKGR_MOR) & ~MOR_KEY_MASK;
  4573. + regmap_read(regmap, AT91_CKGR_MOR, &tmp);
  4574. + tmp &= ~MOR_KEY_MASK;
  4575. +
  4576. if (tmp & AT91_PMC_OSCBYPASS)
  4577. return 0;
  4578. if (!(tmp & AT91_PMC_MOSCEN)) {
  4579. tmp |= AT91_PMC_MOSCEN | AT91_PMC_KEY;
  4580. - pmc_write(pmc, AT91_CKGR_MOR, tmp);
  4581. + regmap_write(regmap, AT91_CKGR_MOR, tmp);
  4582. }
  4583. - while (!(pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCS)) {
  4584. - enable_irq(osc->irq);
  4585. - wait_event(osc->wait,
  4586. - pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCS);
  4587. - }
  4588. + while (!clk_main_osc_ready(regmap))
  4589. + cpu_relax();
  4590. return 0;
  4591. }
  4592. @@ -106,9 +93,10 @@ static int clk_main_osc_prepare(struct clk_hw *hw)
  4593. static void clk_main_osc_unprepare(struct clk_hw *hw)
  4594. {
  4595. struct clk_main_osc *osc = to_clk_main_osc(hw);
  4596. - struct at91_pmc *pmc = osc->pmc;
  4597. - u32 tmp = pmc_read(pmc, AT91_CKGR_MOR);
  4598. + struct regmap *regmap = osc->regmap;
  4599. + u32 tmp;
  4600. + regmap_read(regmap, AT91_CKGR_MOR, &tmp);
  4601. if (tmp & AT91_PMC_OSCBYPASS)
  4602. return;
  4603. @@ -116,20 +104,22 @@ static void clk_main_osc_unprepare(struct clk_hw *hw)
  4604. return;
  4605. tmp &= ~(AT91_PMC_KEY | AT91_PMC_MOSCEN);
  4606. - pmc_write(pmc, AT91_CKGR_MOR, tmp | AT91_PMC_KEY);
  4607. + regmap_write(regmap, AT91_CKGR_MOR, tmp | AT91_PMC_KEY);
  4608. }
  4609. static int clk_main_osc_is_prepared(struct clk_hw *hw)
  4610. {
  4611. struct clk_main_osc *osc = to_clk_main_osc(hw);
  4612. - struct at91_pmc *pmc = osc->pmc;
  4613. - u32 tmp = pmc_read(pmc, AT91_CKGR_MOR);
  4614. + struct regmap *regmap = osc->regmap;
  4615. + u32 tmp, status;
  4616. + regmap_read(regmap, AT91_CKGR_MOR, &tmp);
  4617. if (tmp & AT91_PMC_OSCBYPASS)
  4618. return 1;
  4619. - return !!((pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCS) &&
  4620. - (pmc_read(pmc, AT91_CKGR_MOR) & AT91_PMC_MOSCEN));
  4621. + regmap_read(regmap, AT91_PMC_SR, &status);
  4622. +
  4623. + return (status & AT91_PMC_MOSCS) && (tmp & AT91_PMC_MOSCEN);
  4624. }
  4625. static const struct clk_ops main_osc_ops = {
  4626. @@ -139,18 +129,16 @@ static const struct clk_ops main_osc_ops = {
  4627. };
  4628. static struct clk * __init
  4629. -at91_clk_register_main_osc(struct at91_pmc *pmc,
  4630. - unsigned int irq,
  4631. +at91_clk_register_main_osc(struct regmap *regmap,
  4632. const char *name,
  4633. const char *parent_name,
  4634. bool bypass)
  4635. {
  4636. - int ret;
  4637. struct clk_main_osc *osc;
  4638. struct clk *clk = NULL;
  4639. struct clk_init_data init;
  4640. - if (!pmc || !irq || !name || !parent_name)
  4641. + if (!name || !parent_name)
  4642. return ERR_PTR(-EINVAL);
  4643. osc = kzalloc(sizeof(*osc), GFP_KERNEL);
  4644. @@ -164,85 +152,70 @@ at91_clk_register_main_osc(struct at91_pmc *pmc,
  4645. init.flags = CLK_IGNORE_UNUSED;
  4646. osc->hw.init = &init;
  4647. - osc->pmc = pmc;
  4648. - osc->irq = irq;
  4649. -
  4650. - init_waitqueue_head(&osc->wait);
  4651. - irq_set_status_flags(osc->irq, IRQ_NOAUTOEN);
  4652. - ret = request_irq(osc->irq, clk_main_osc_irq_handler,
  4653. - IRQF_TRIGGER_HIGH, name, osc);
  4654. - if (ret) {
  4655. - kfree(osc);
  4656. - return ERR_PTR(ret);
  4657. - }
  4658. + osc->regmap = regmap;
  4659. if (bypass)
  4660. - pmc_write(pmc, AT91_CKGR_MOR,
  4661. - (pmc_read(pmc, AT91_CKGR_MOR) &
  4662. - ~(MOR_KEY_MASK | AT91_PMC_MOSCEN)) |
  4663. - AT91_PMC_OSCBYPASS | AT91_PMC_KEY);
  4664. + regmap_update_bits(regmap,
  4665. + AT91_CKGR_MOR, MOR_KEY_MASK |
  4666. + AT91_PMC_MOSCEN,
  4667. + AT91_PMC_OSCBYPASS | AT91_PMC_KEY);
  4668. clk = clk_register(NULL, &osc->hw);
  4669. - if (IS_ERR(clk)) {
  4670. - free_irq(irq, osc);
  4671. + if (IS_ERR(clk))
  4672. kfree(osc);
  4673. - }
  4674. return clk;
  4675. }
  4676. -void __init of_at91rm9200_clk_main_osc_setup(struct device_node *np,
  4677. - struct at91_pmc *pmc)
  4678. +static void __init of_at91rm9200_clk_main_osc_setup(struct device_node *np)
  4679. {
  4680. struct clk *clk;
  4681. - unsigned int irq;
  4682. const char *name = np->name;
  4683. const char *parent_name;
  4684. + struct regmap *regmap;
  4685. bool bypass;
  4686. of_property_read_string(np, "clock-output-names", &name);
  4687. bypass = of_property_read_bool(np, "atmel,osc-bypass");
  4688. parent_name = of_clk_get_parent_name(np, 0);
  4689. - irq = irq_of_parse_and_map(np, 0);
  4690. - if (!irq)
  4691. + regmap = syscon_node_to_regmap(of_get_parent(np));
  4692. + if (IS_ERR(regmap))
  4693. return;
  4694. - clk = at91_clk_register_main_osc(pmc, irq, name, parent_name, bypass);
  4695. + clk = at91_clk_register_main_osc(regmap, name, parent_name, bypass);
  4696. if (IS_ERR(clk))
  4697. return;
  4698. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  4699. }
  4700. +CLK_OF_DECLARE(at91rm9200_clk_main_osc, "atmel,at91rm9200-clk-main-osc",
  4701. + of_at91rm9200_clk_main_osc_setup);
  4702. -static irqreturn_t clk_main_rc_osc_irq_handler(int irq, void *dev_id)
  4703. +static bool clk_main_rc_osc_ready(struct regmap *regmap)
  4704. {
  4705. - struct clk_main_rc_osc *osc = dev_id;
  4706. + unsigned int status;
  4707. - wake_up(&osc->wait);
  4708. - disable_irq_nosync(osc->irq);
  4709. + regmap_read(regmap, AT91_PMC_SR, &status);
  4710. - return IRQ_HANDLED;
  4711. + return status & AT91_PMC_MOSCRCS;
  4712. }
  4713. static int clk_main_rc_osc_prepare(struct clk_hw *hw)
  4714. {
  4715. struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
  4716. - struct at91_pmc *pmc = osc->pmc;
  4717. - u32 tmp;
  4718. + struct regmap *regmap = osc->regmap;
  4719. + unsigned int mor;
  4720. - tmp = pmc_read(pmc, AT91_CKGR_MOR) & ~MOR_KEY_MASK;
  4721. + regmap_read(regmap, AT91_CKGR_MOR, &mor);
  4722. - if (!(tmp & AT91_PMC_MOSCRCEN)) {
  4723. - tmp |= AT91_PMC_MOSCRCEN | AT91_PMC_KEY;
  4724. - pmc_write(pmc, AT91_CKGR_MOR, tmp);
  4725. - }
  4726. + if (!(mor & AT91_PMC_MOSCRCEN))
  4727. + regmap_update_bits(regmap, AT91_CKGR_MOR,
  4728. + MOR_KEY_MASK | AT91_PMC_MOSCRCEN,
  4729. + AT91_PMC_MOSCRCEN | AT91_PMC_KEY);
  4730. - while (!(pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCRCS)) {
  4731. - enable_irq(osc->irq);
  4732. - wait_event(osc->wait,
  4733. - pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCRCS);
  4734. - }
  4735. + while (!clk_main_rc_osc_ready(regmap))
  4736. + cpu_relax();
  4737. return 0;
  4738. }
  4739. @@ -250,23 +223,28 @@ static int clk_main_rc_osc_prepare(struct clk_hw *hw)
  4740. static void clk_main_rc_osc_unprepare(struct clk_hw *hw)
  4741. {
  4742. struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
  4743. - struct at91_pmc *pmc = osc->pmc;
  4744. - u32 tmp = pmc_read(pmc, AT91_CKGR_MOR);
  4745. + struct regmap *regmap = osc->regmap;
  4746. + unsigned int mor;
  4747. +
  4748. + regmap_read(regmap, AT91_CKGR_MOR, &mor);
  4749. - if (!(tmp & AT91_PMC_MOSCRCEN))
  4750. + if (!(mor & AT91_PMC_MOSCRCEN))
  4751. return;
  4752. - tmp &= ~(MOR_KEY_MASK | AT91_PMC_MOSCRCEN);
  4753. - pmc_write(pmc, AT91_CKGR_MOR, tmp | AT91_PMC_KEY);
  4754. + regmap_update_bits(regmap, AT91_CKGR_MOR,
  4755. + MOR_KEY_MASK | AT91_PMC_MOSCRCEN, AT91_PMC_KEY);
  4756. }
  4757. static int clk_main_rc_osc_is_prepared(struct clk_hw *hw)
  4758. {
  4759. struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
  4760. - struct at91_pmc *pmc = osc->pmc;
  4761. + struct regmap *regmap = osc->regmap;
  4762. + unsigned int mor, status;
  4763. - return !!((pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCRCS) &&
  4764. - (pmc_read(pmc, AT91_CKGR_MOR) & AT91_PMC_MOSCRCEN));
  4765. + regmap_read(regmap, AT91_CKGR_MOR, &mor);
  4766. + regmap_read(regmap, AT91_PMC_SR, &status);
  4767. +
  4768. + return (mor & AT91_PMC_MOSCRCEN) && (status & AT91_PMC_MOSCRCS);
  4769. }
  4770. static unsigned long clk_main_rc_osc_recalc_rate(struct clk_hw *hw,
  4771. @@ -294,17 +272,15 @@ static const struct clk_ops main_rc_osc_ops = {
  4772. };
  4773. static struct clk * __init
  4774. -at91_clk_register_main_rc_osc(struct at91_pmc *pmc,
  4775. - unsigned int irq,
  4776. +at91_clk_register_main_rc_osc(struct regmap *regmap,
  4777. const char *name,
  4778. u32 frequency, u32 accuracy)
  4779. {
  4780. - int ret;
  4781. struct clk_main_rc_osc *osc;
  4782. struct clk *clk = NULL;
  4783. struct clk_init_data init;
  4784. - if (!pmc || !irq || !name || !frequency)
  4785. + if (!name || !frequency)
  4786. return ERR_PTR(-EINVAL);
  4787. osc = kzalloc(sizeof(*osc), GFP_KERNEL);
  4788. @@ -318,63 +294,53 @@ at91_clk_register_main_rc_osc(struct at91_pmc *pmc,
  4789. init.flags = CLK_IS_ROOT | CLK_IGNORE_UNUSED;
  4790. osc->hw.init = &init;
  4791. - osc->pmc = pmc;
  4792. - osc->irq = irq;
  4793. + osc->regmap = regmap;
  4794. osc->frequency = frequency;
  4795. osc->accuracy = accuracy;
  4796. - init_waitqueue_head(&osc->wait);
  4797. - irq_set_status_flags(osc->irq, IRQ_NOAUTOEN);
  4798. - ret = request_irq(osc->irq, clk_main_rc_osc_irq_handler,
  4799. - IRQF_TRIGGER_HIGH, name, osc);
  4800. - if (ret)
  4801. - return ERR_PTR(ret);
  4802. -
  4803. clk = clk_register(NULL, &osc->hw);
  4804. - if (IS_ERR(clk)) {
  4805. - free_irq(irq, osc);
  4806. + if (IS_ERR(clk))
  4807. kfree(osc);
  4808. - }
  4809. return clk;
  4810. }
  4811. -void __init of_at91sam9x5_clk_main_rc_osc_setup(struct device_node *np,
  4812. - struct at91_pmc *pmc)
  4813. +static void __init of_at91sam9x5_clk_main_rc_osc_setup(struct device_node *np)
  4814. {
  4815. struct clk *clk;
  4816. - unsigned int irq;
  4817. u32 frequency = 0;
  4818. u32 accuracy = 0;
  4819. const char *name = np->name;
  4820. + struct regmap *regmap;
  4821. of_property_read_string(np, "clock-output-names", &name);
  4822. of_property_read_u32(np, "clock-frequency", &frequency);
  4823. of_property_read_u32(np, "clock-accuracy", &accuracy);
  4824. - irq = irq_of_parse_and_map(np, 0);
  4825. - if (!irq)
  4826. + regmap = syscon_node_to_regmap(of_get_parent(np));
  4827. + if (IS_ERR(regmap))
  4828. return;
  4829. - clk = at91_clk_register_main_rc_osc(pmc, irq, name, frequency,
  4830. - accuracy);
  4831. + clk = at91_clk_register_main_rc_osc(regmap, name, frequency, accuracy);
  4832. if (IS_ERR(clk))
  4833. return;
  4834. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  4835. }
  4836. +CLK_OF_DECLARE(at91sam9x5_clk_main_rc_osc, "atmel,at91sam9x5-clk-main-rc-osc",
  4837. + of_at91sam9x5_clk_main_rc_osc_setup);
  4838. -static int clk_main_probe_frequency(struct at91_pmc *pmc)
  4839. +static int clk_main_probe_frequency(struct regmap *regmap)
  4840. {
  4841. unsigned long prep_time, timeout;
  4842. - u32 tmp;
  4843. + unsigned int mcfr;
  4844. timeout = jiffies + usecs_to_jiffies(MAINFRDY_TIMEOUT);
  4845. do {
  4846. prep_time = jiffies;
  4847. - tmp = pmc_read(pmc, AT91_CKGR_MCFR);
  4848. - if (tmp & AT91_PMC_MAINRDY)
  4849. + regmap_read(regmap, AT91_CKGR_MCFR, &mcfr);
  4850. + if (mcfr & AT91_PMC_MAINRDY)
  4851. return 0;
  4852. usleep_range(MAINF_LOOP_MIN_WAIT, MAINF_LOOP_MAX_WAIT);
  4853. } while (time_before(prep_time, timeout));
  4854. @@ -382,34 +348,37 @@ static int clk_main_probe_frequency(struct at91_pmc *pmc)
  4855. return -ETIMEDOUT;
  4856. }
  4857. -static unsigned long clk_main_recalc_rate(struct at91_pmc *pmc,
  4858. +static unsigned long clk_main_recalc_rate(struct regmap *regmap,
  4859. unsigned long parent_rate)
  4860. {
  4861. - u32 tmp;
  4862. + unsigned int mcfr;
  4863. if (parent_rate)
  4864. return parent_rate;
  4865. pr_warn("Main crystal frequency not set, using approximate value\n");
  4866. - tmp = pmc_read(pmc, AT91_CKGR_MCFR);
  4867. - if (!(tmp & AT91_PMC_MAINRDY))
  4868. + regmap_read(regmap, AT91_CKGR_MCFR, &mcfr);
  4869. + if (!(mcfr & AT91_PMC_MAINRDY))
  4870. return 0;
  4871. - return ((tmp & AT91_PMC_MAINF) * SLOW_CLOCK_FREQ) / MAINF_DIV;
  4872. + return ((mcfr & AT91_PMC_MAINF) * SLOW_CLOCK_FREQ) / MAINF_DIV;
  4873. }
  4874. static int clk_rm9200_main_prepare(struct clk_hw *hw)
  4875. {
  4876. struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw);
  4877. - return clk_main_probe_frequency(clkmain->pmc);
  4878. + return clk_main_probe_frequency(clkmain->regmap);
  4879. }
  4880. static int clk_rm9200_main_is_prepared(struct clk_hw *hw)
  4881. {
  4882. struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw);
  4883. + unsigned int status;
  4884. +
  4885. + regmap_read(clkmain->regmap, AT91_CKGR_MCFR, &status);
  4886. - return !!(pmc_read(clkmain->pmc, AT91_CKGR_MCFR) & AT91_PMC_MAINRDY);
  4887. + return status & AT91_PMC_MAINRDY ? 1 : 0;
  4888. }
  4889. static unsigned long clk_rm9200_main_recalc_rate(struct clk_hw *hw,
  4890. @@ -417,7 +386,7 @@ static unsigned long clk_rm9200_main_recalc_rate(struct clk_hw *hw,
  4891. {
  4892. struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw);
  4893. - return clk_main_recalc_rate(clkmain->pmc, parent_rate);
  4894. + return clk_main_recalc_rate(clkmain->regmap, parent_rate);
  4895. }
  4896. static const struct clk_ops rm9200_main_ops = {
  4897. @@ -427,7 +396,7 @@ static const struct clk_ops rm9200_main_ops = {
  4898. };
  4899. static struct clk * __init
  4900. -at91_clk_register_rm9200_main(struct at91_pmc *pmc,
  4901. +at91_clk_register_rm9200_main(struct regmap *regmap,
  4902. const char *name,
  4903. const char *parent_name)
  4904. {
  4905. @@ -435,7 +404,7 @@ at91_clk_register_rm9200_main(struct at91_pmc *pmc,
  4906. struct clk *clk = NULL;
  4907. struct clk_init_data init;
  4908. - if (!pmc || !name)
  4909. + if (!name)
  4910. return ERR_PTR(-EINVAL);
  4911. if (!parent_name)
  4912. @@ -452,7 +421,7 @@ at91_clk_register_rm9200_main(struct at91_pmc *pmc,
  4913. init.flags = 0;
  4914. clkmain->hw.init = &init;
  4915. - clkmain->pmc = pmc;
  4916. + clkmain->regmap = regmap;
  4917. clk = clk_register(NULL, &clkmain->hw);
  4918. if (IS_ERR(clk))
  4919. @@ -461,52 +430,54 @@ at91_clk_register_rm9200_main(struct at91_pmc *pmc,
  4920. return clk;
  4921. }
  4922. -void __init of_at91rm9200_clk_main_setup(struct device_node *np,
  4923. - struct at91_pmc *pmc)
  4924. +static void __init of_at91rm9200_clk_main_setup(struct device_node *np)
  4925. {
  4926. struct clk *clk;
  4927. const char *parent_name;
  4928. const char *name = np->name;
  4929. + struct regmap *regmap;
  4930. parent_name = of_clk_get_parent_name(np, 0);
  4931. of_property_read_string(np, "clock-output-names", &name);
  4932. - clk = at91_clk_register_rm9200_main(pmc, name, parent_name);
  4933. + regmap = syscon_node_to_regmap(of_get_parent(np));
  4934. + if (IS_ERR(regmap))
  4935. + return;
  4936. +
  4937. + clk = at91_clk_register_rm9200_main(regmap, name, parent_name);
  4938. if (IS_ERR(clk))
  4939. return;
  4940. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  4941. }
  4942. +CLK_OF_DECLARE(at91rm9200_clk_main, "atmel,at91rm9200-clk-main",
  4943. + of_at91rm9200_clk_main_setup);
  4944. -static irqreturn_t clk_sam9x5_main_irq_handler(int irq, void *dev_id)
  4945. +static inline bool clk_sam9x5_main_ready(struct regmap *regmap)
  4946. {
  4947. - struct clk_sam9x5_main *clkmain = dev_id;
  4948. + unsigned int status;
  4949. - wake_up(&clkmain->wait);
  4950. - disable_irq_nosync(clkmain->irq);
  4951. + regmap_read(regmap, AT91_PMC_SR, &status);
  4952. - return IRQ_HANDLED;
  4953. + return status & AT91_PMC_MOSCSELS ? 1 : 0;
  4954. }
  4955. static int clk_sam9x5_main_prepare(struct clk_hw *hw)
  4956. {
  4957. struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
  4958. - struct at91_pmc *pmc = clkmain->pmc;
  4959. + struct regmap *regmap = clkmain->regmap;
  4960. - while (!(pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCSELS)) {
  4961. - enable_irq(clkmain->irq);
  4962. - wait_event(clkmain->wait,
  4963. - pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCSELS);
  4964. - }
  4965. + while (!clk_sam9x5_main_ready(regmap))
  4966. + cpu_relax();
  4967. - return clk_main_probe_frequency(pmc);
  4968. + return clk_main_probe_frequency(regmap);
  4969. }
  4970. static int clk_sam9x5_main_is_prepared(struct clk_hw *hw)
  4971. {
  4972. struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
  4973. - return !!(pmc_read(clkmain->pmc, AT91_PMC_SR) & AT91_PMC_MOSCSELS);
  4974. + return clk_sam9x5_main_ready(clkmain->regmap);
  4975. }
  4976. static unsigned long clk_sam9x5_main_recalc_rate(struct clk_hw *hw,
  4977. @@ -514,30 +485,28 @@ static unsigned long clk_sam9x5_main_recalc_rate(struct clk_hw *hw,
  4978. {
  4979. struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
  4980. - return clk_main_recalc_rate(clkmain->pmc, parent_rate);
  4981. + return clk_main_recalc_rate(clkmain->regmap, parent_rate);
  4982. }
  4983. static int clk_sam9x5_main_set_parent(struct clk_hw *hw, u8 index)
  4984. {
  4985. struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
  4986. - struct at91_pmc *pmc = clkmain->pmc;
  4987. - u32 tmp;
  4988. + struct regmap *regmap = clkmain->regmap;
  4989. + unsigned int tmp;
  4990. if (index > 1)
  4991. return -EINVAL;
  4992. - tmp = pmc_read(pmc, AT91_CKGR_MOR) & ~MOR_KEY_MASK;
  4993. + regmap_read(regmap, AT91_CKGR_MOR, &tmp);
  4994. + tmp &= ~MOR_KEY_MASK;
  4995. if (index && !(tmp & AT91_PMC_MOSCSEL))
  4996. - pmc_write(pmc, AT91_CKGR_MOR, tmp | AT91_PMC_MOSCSEL);
  4997. + regmap_write(regmap, AT91_CKGR_MOR, tmp | AT91_PMC_MOSCSEL);
  4998. else if (!index && (tmp & AT91_PMC_MOSCSEL))
  4999. - pmc_write(pmc, AT91_CKGR_MOR, tmp & ~AT91_PMC_MOSCSEL);
  5000. + regmap_write(regmap, AT91_CKGR_MOR, tmp & ~AT91_PMC_MOSCSEL);
  5001. - while (!(pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCSELS)) {
  5002. - enable_irq(clkmain->irq);
  5003. - wait_event(clkmain->wait,
  5004. - pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCSELS);
  5005. - }
  5006. + while (!clk_sam9x5_main_ready(regmap))
  5007. + cpu_relax();
  5008. return 0;
  5009. }
  5010. @@ -545,8 +514,11 @@ static int clk_sam9x5_main_set_parent(struct clk_hw *hw, u8 index)
  5011. static u8 clk_sam9x5_main_get_parent(struct clk_hw *hw)
  5012. {
  5013. struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
  5014. + unsigned int status;
  5015. +
  5016. + regmap_read(clkmain->regmap, AT91_CKGR_MOR, &status);
  5017. - return !!(pmc_read(clkmain->pmc, AT91_CKGR_MOR) & AT91_PMC_MOSCEN);
  5018. + return status & AT91_PMC_MOSCEN ? 1 : 0;
  5019. }
  5020. static const struct clk_ops sam9x5_main_ops = {
  5021. @@ -558,18 +530,17 @@ static const struct clk_ops sam9x5_main_ops = {
  5022. };
  5023. static struct clk * __init
  5024. -at91_clk_register_sam9x5_main(struct at91_pmc *pmc,
  5025. - unsigned int irq,
  5026. +at91_clk_register_sam9x5_main(struct regmap *regmap,
  5027. const char *name,
  5028. const char **parent_names,
  5029. int num_parents)
  5030. {
  5031. - int ret;
  5032. struct clk_sam9x5_main *clkmain;
  5033. struct clk *clk = NULL;
  5034. struct clk_init_data init;
  5035. + unsigned int status;
  5036. - if (!pmc || !irq || !name)
  5037. + if (!name)
  5038. return ERR_PTR(-EINVAL);
  5039. if (!parent_names || !num_parents)
  5040. @@ -586,51 +557,42 @@ at91_clk_register_sam9x5_main(struct at91_pmc *pmc,
  5041. init.flags = CLK_SET_PARENT_GATE;
  5042. clkmain->hw.init = &init;
  5043. - clkmain->pmc = pmc;
  5044. - clkmain->irq = irq;
  5045. - clkmain->parent = !!(pmc_read(clkmain->pmc, AT91_CKGR_MOR) &
  5046. - AT91_PMC_MOSCEN);
  5047. - init_waitqueue_head(&clkmain->wait);
  5048. - irq_set_status_flags(clkmain->irq, IRQ_NOAUTOEN);
  5049. - ret = request_irq(clkmain->irq, clk_sam9x5_main_irq_handler,
  5050. - IRQF_TRIGGER_HIGH, name, clkmain);
  5051. - if (ret)
  5052. - return ERR_PTR(ret);
  5053. + clkmain->regmap = regmap;
  5054. + regmap_read(clkmain->regmap, AT91_CKGR_MOR, &status);
  5055. + clkmain->parent = status & AT91_PMC_MOSCEN ? 1 : 0;
  5056. clk = clk_register(NULL, &clkmain->hw);
  5057. - if (IS_ERR(clk)) {
  5058. - free_irq(clkmain->irq, clkmain);
  5059. + if (IS_ERR(clk))
  5060. kfree(clkmain);
  5061. - }
  5062. return clk;
  5063. }
  5064. -void __init of_at91sam9x5_clk_main_setup(struct device_node *np,
  5065. - struct at91_pmc *pmc)
  5066. +static void __init of_at91sam9x5_clk_main_setup(struct device_node *np)
  5067. {
  5068. struct clk *clk;
  5069. const char *parent_names[2];
  5070. int num_parents;
  5071. - unsigned int irq;
  5072. const char *name = np->name;
  5073. + struct regmap *regmap;
  5074. num_parents = of_clk_get_parent_count(np);
  5075. if (num_parents <= 0 || num_parents > 2)
  5076. return;
  5077. of_clk_parent_fill(np, parent_names, num_parents);
  5078. + regmap = syscon_node_to_regmap(of_get_parent(np));
  5079. + if (IS_ERR(regmap))
  5080. + return;
  5081. of_property_read_string(np, "clock-output-names", &name);
  5082. - irq = irq_of_parse_and_map(np, 0);
  5083. - if (!irq)
  5084. - return;
  5085. -
  5086. - clk = at91_clk_register_sam9x5_main(pmc, irq, name, parent_names,
  5087. + clk = at91_clk_register_sam9x5_main(regmap, name, parent_names,
  5088. num_parents);
  5089. if (IS_ERR(clk))
  5090. return;
  5091. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  5092. }
  5093. +CLK_OF_DECLARE(at91sam9x5_clk_main, "atmel,at91sam9x5-clk-main",
  5094. + of_at91sam9x5_clk_main_setup);
  5095. diff --git a/drivers/clk/at91/clk-master.c b/drivers/clk/at91/clk-master.c
  5096. index 620ea323356b..7d4a1864ea7c 100644
  5097. --- a/drivers/clk/at91/clk-master.c
  5098. +++ b/drivers/clk/at91/clk-master.c
  5099. @@ -12,13 +12,8 @@
  5100. #include <linux/clkdev.h>
  5101. #include <linux/clk/at91_pmc.h>
  5102. #include <linux/of.h>
  5103. -#include <linux/of_address.h>
  5104. -#include <linux/of_irq.h>
  5105. -#include <linux/io.h>
  5106. -#include <linux/wait.h>
  5107. -#include <linux/sched.h>
  5108. -#include <linux/interrupt.h>
  5109. -#include <linux/irq.h>
  5110. +#include <linux/mfd/syscon.h>
  5111. +#include <linux/regmap.h>
  5112. #include "pmc.h"
  5113. @@ -44,32 +39,26 @@ struct clk_master_layout {
  5114. struct clk_master {
  5115. struct clk_hw hw;
  5116. - struct at91_pmc *pmc;
  5117. - unsigned int irq;
  5118. - wait_queue_head_t wait;
  5119. + struct regmap *regmap;
  5120. const struct clk_master_layout *layout;
  5121. const struct clk_master_characteristics *characteristics;
  5122. };
  5123. -static irqreturn_t clk_master_irq_handler(int irq, void *dev_id)
  5124. +static inline bool clk_master_ready(struct regmap *regmap)
  5125. {
  5126. - struct clk_master *master = (struct clk_master *)dev_id;
  5127. + unsigned int status;
  5128. - wake_up(&master->wait);
  5129. - disable_irq_nosync(master->irq);
  5130. + regmap_read(regmap, AT91_PMC_SR, &status);
  5131. - return IRQ_HANDLED;
  5132. + return status & AT91_PMC_MCKRDY ? 1 : 0;
  5133. }
  5134. +
  5135. static int clk_master_prepare(struct clk_hw *hw)
  5136. {
  5137. struct clk_master *master = to_clk_master(hw);
  5138. - struct at91_pmc *pmc = master->pmc;
  5139. - while (!(pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MCKRDY)) {
  5140. - enable_irq(master->irq);
  5141. - wait_event(master->wait,
  5142. - pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MCKRDY);
  5143. - }
  5144. + while (!clk_master_ready(master->regmap))
  5145. + cpu_relax();
  5146. return 0;
  5147. }
  5148. @@ -78,7 +67,7 @@ static int clk_master_is_prepared(struct clk_hw *hw)
  5149. {
  5150. struct clk_master *master = to_clk_master(hw);
  5151. - return !!(pmc_read(master->pmc, AT91_PMC_SR) & AT91_PMC_MCKRDY);
  5152. + return clk_master_ready(master->regmap);
  5153. }
  5154. static unsigned long clk_master_recalc_rate(struct clk_hw *hw,
  5155. @@ -88,18 +77,16 @@ static unsigned long clk_master_recalc_rate(struct clk_hw *hw,
  5156. u8 div;
  5157. unsigned long rate = parent_rate;
  5158. struct clk_master *master = to_clk_master(hw);
  5159. - struct at91_pmc *pmc = master->pmc;
  5160. const struct clk_master_layout *layout = master->layout;
  5161. const struct clk_master_characteristics *characteristics =
  5162. master->characteristics;
  5163. - u32 tmp;
  5164. + unsigned int mckr;
  5165. - pmc_lock(pmc);
  5166. - tmp = pmc_read(pmc, AT91_PMC_MCKR) & layout->mask;
  5167. - pmc_unlock(pmc);
  5168. + regmap_read(master->regmap, AT91_PMC_MCKR, &mckr);
  5169. + mckr &= layout->mask;
  5170. - pres = (tmp >> layout->pres_shift) & MASTER_PRES_MASK;
  5171. - div = (tmp >> MASTER_DIV_SHIFT) & MASTER_DIV_MASK;
  5172. + pres = (mckr >> layout->pres_shift) & MASTER_PRES_MASK;
  5173. + div = (mckr >> MASTER_DIV_SHIFT) & MASTER_DIV_MASK;
  5174. if (characteristics->have_div3_pres && pres == MASTER_PRES_MAX)
  5175. rate /= 3;
  5176. @@ -119,9 +106,11 @@ static unsigned long clk_master_recalc_rate(struct clk_hw *hw,
  5177. static u8 clk_master_get_parent(struct clk_hw *hw)
  5178. {
  5179. struct clk_master *master = to_clk_master(hw);
  5180. - struct at91_pmc *pmc = master->pmc;
  5181. + unsigned int mckr;
  5182. - return pmc_read(pmc, AT91_PMC_MCKR) & AT91_PMC_CSS;
  5183. + regmap_read(master->regmap, AT91_PMC_MCKR, &mckr);
  5184. +
  5185. + return mckr & AT91_PMC_CSS;
  5186. }
  5187. static const struct clk_ops master_ops = {
  5188. @@ -132,18 +121,17 @@ static const struct clk_ops master_ops = {
  5189. };
  5190. static struct clk * __init
  5191. -at91_clk_register_master(struct at91_pmc *pmc, unsigned int irq,
  5192. +at91_clk_register_master(struct regmap *regmap,
  5193. const char *name, int num_parents,
  5194. const char **parent_names,
  5195. const struct clk_master_layout *layout,
  5196. const struct clk_master_characteristics *characteristics)
  5197. {
  5198. - int ret;
  5199. struct clk_master *master;
  5200. struct clk *clk = NULL;
  5201. struct clk_init_data init;
  5202. - if (!pmc || !irq || !name || !num_parents || !parent_names)
  5203. + if (!name || !num_parents || !parent_names)
  5204. return ERR_PTR(-EINVAL);
  5205. master = kzalloc(sizeof(*master), GFP_KERNEL);
  5206. @@ -159,20 +147,10 @@ at91_clk_register_master(struct at91_pmc *pmc, unsigned int irq,
  5207. master->hw.init = &init;
  5208. master->layout = layout;
  5209. master->characteristics = characteristics;
  5210. - master->pmc = pmc;
  5211. - master->irq = irq;
  5212. - init_waitqueue_head(&master->wait);
  5213. - irq_set_status_flags(master->irq, IRQ_NOAUTOEN);
  5214. - ret = request_irq(master->irq, clk_master_irq_handler,
  5215. - IRQF_TRIGGER_HIGH, "clk-master", master);
  5216. - if (ret) {
  5217. - kfree(master);
  5218. - return ERR_PTR(ret);
  5219. - }
  5220. + master->regmap = regmap;
  5221. clk = clk_register(NULL, &master->hw);
  5222. if (IS_ERR(clk)) {
  5223. - free_irq(master->irq, master);
  5224. kfree(master);
  5225. }
  5226. @@ -217,15 +195,15 @@ out_free_characteristics:
  5227. }
  5228. static void __init
  5229. -of_at91_clk_master_setup(struct device_node *np, struct at91_pmc *pmc,
  5230. +of_at91_clk_master_setup(struct device_node *np,
  5231. const struct clk_master_layout *layout)
  5232. {
  5233. struct clk *clk;
  5234. int num_parents;
  5235. - unsigned int irq;
  5236. const char *parent_names[MASTER_SOURCE_MAX];
  5237. const char *name = np->name;
  5238. struct clk_master_characteristics *characteristics;
  5239. + struct regmap *regmap;
  5240. num_parents = of_clk_get_parent_count(np);
  5241. if (num_parents <= 0 || num_parents > MASTER_SOURCE_MAX)
  5242. @@ -239,11 +217,11 @@ of_at91_clk_master_setup(struct device_node *np, struct at91_pmc *pmc,
  5243. if (!characteristics)
  5244. return;
  5245. - irq = irq_of_parse_and_map(np, 0);
  5246. - if (!irq)
  5247. - goto out_free_characteristics;
  5248. + regmap = syscon_node_to_regmap(of_get_parent(np));
  5249. + if (IS_ERR(regmap))
  5250. + return;
  5251. - clk = at91_clk_register_master(pmc, irq, name, num_parents,
  5252. + clk = at91_clk_register_master(regmap, name, num_parents,
  5253. parent_names, layout,
  5254. characteristics);
  5255. if (IS_ERR(clk))
  5256. @@ -256,14 +234,16 @@ out_free_characteristics:
  5257. kfree(characteristics);
  5258. }
  5259. -void __init of_at91rm9200_clk_master_setup(struct device_node *np,
  5260. - struct at91_pmc *pmc)
  5261. +static void __init of_at91rm9200_clk_master_setup(struct device_node *np)
  5262. {
  5263. - of_at91_clk_master_setup(np, pmc, &at91rm9200_master_layout);
  5264. + of_at91_clk_master_setup(np, &at91rm9200_master_layout);
  5265. }
  5266. +CLK_OF_DECLARE(at91rm9200_clk_master, "atmel,at91rm9200-clk-master",
  5267. + of_at91rm9200_clk_master_setup);
  5268. -void __init of_at91sam9x5_clk_master_setup(struct device_node *np,
  5269. - struct at91_pmc *pmc)
  5270. +static void __init of_at91sam9x5_clk_master_setup(struct device_node *np)
  5271. {
  5272. - of_at91_clk_master_setup(np, pmc, &at91sam9x5_master_layout);
  5273. + of_at91_clk_master_setup(np, &at91sam9x5_master_layout);
  5274. }
  5275. +CLK_OF_DECLARE(at91sam9x5_clk_master, "atmel,at91sam9x5-clk-master",
  5276. + of_at91sam9x5_clk_master_setup);
  5277. diff --git a/drivers/clk/at91/clk-peripheral.c b/drivers/clk/at91/clk-peripheral.c
  5278. index 58f3b568e9cb..d69cd2a121b1 100644
  5279. --- a/drivers/clk/at91/clk-peripheral.c
  5280. +++ b/drivers/clk/at91/clk-peripheral.c
  5281. @@ -12,11 +12,13 @@
  5282. #include <linux/clkdev.h>
  5283. #include <linux/clk/at91_pmc.h>
  5284. #include <linux/of.h>
  5285. -#include <linux/of_address.h>
  5286. -#include <linux/io.h>
  5287. +#include <linux/mfd/syscon.h>
  5288. +#include <linux/regmap.h>
  5289. #include "pmc.h"
  5290. +DEFINE_SPINLOCK(pmc_pcr_lock);
  5291. +
  5292. #define PERIPHERAL_MAX 64
  5293. #define PERIPHERAL_AT91RM9200 0
  5294. @@ -33,7 +35,7 @@
  5295. struct clk_peripheral {
  5296. struct clk_hw hw;
  5297. - struct at91_pmc *pmc;
  5298. + struct regmap *regmap;
  5299. u32 id;
  5300. };
  5301. @@ -41,8 +43,9 @@ struct clk_peripheral {
  5302. struct clk_sam9x5_peripheral {
  5303. struct clk_hw hw;
  5304. - struct at91_pmc *pmc;
  5305. + struct regmap *regmap;
  5306. struct clk_range range;
  5307. + spinlock_t *lock;
  5308. u32 id;
  5309. u32 div;
  5310. bool auto_div;
  5311. @@ -54,7 +57,6 @@ struct clk_sam9x5_peripheral {
  5312. static int clk_peripheral_enable(struct clk_hw *hw)
  5313. {
  5314. struct clk_peripheral *periph = to_clk_peripheral(hw);
  5315. - struct at91_pmc *pmc = periph->pmc;
  5316. int offset = AT91_PMC_PCER;
  5317. u32 id = periph->id;
  5318. @@ -62,14 +64,14 @@ static int clk_peripheral_enable(struct clk_hw *hw)
  5319. return 0;
  5320. if (id > PERIPHERAL_ID_MAX)
  5321. offset = AT91_PMC_PCER1;
  5322. - pmc_write(pmc, offset, PERIPHERAL_MASK(id));
  5323. + regmap_write(periph->regmap, offset, PERIPHERAL_MASK(id));
  5324. +
  5325. return 0;
  5326. }
  5327. static void clk_peripheral_disable(struct clk_hw *hw)
  5328. {
  5329. struct clk_peripheral *periph = to_clk_peripheral(hw);
  5330. - struct at91_pmc *pmc = periph->pmc;
  5331. int offset = AT91_PMC_PCDR;
  5332. u32 id = periph->id;
  5333. @@ -77,21 +79,23 @@ static void clk_peripheral_disable(struct clk_hw *hw)
  5334. return;
  5335. if (id > PERIPHERAL_ID_MAX)
  5336. offset = AT91_PMC_PCDR1;
  5337. - pmc_write(pmc, offset, PERIPHERAL_MASK(id));
  5338. + regmap_write(periph->regmap, offset, PERIPHERAL_MASK(id));
  5339. }
  5340. static int clk_peripheral_is_enabled(struct clk_hw *hw)
  5341. {
  5342. struct clk_peripheral *periph = to_clk_peripheral(hw);
  5343. - struct at91_pmc *pmc = periph->pmc;
  5344. int offset = AT91_PMC_PCSR;
  5345. + unsigned int status;
  5346. u32 id = periph->id;
  5347. if (id < PERIPHERAL_ID_MIN)
  5348. return 1;
  5349. if (id > PERIPHERAL_ID_MAX)
  5350. offset = AT91_PMC_PCSR1;
  5351. - return !!(pmc_read(pmc, offset) & PERIPHERAL_MASK(id));
  5352. + regmap_read(periph->regmap, offset, &status);
  5353. +
  5354. + return status & PERIPHERAL_MASK(id) ? 1 : 0;
  5355. }
  5356. static const struct clk_ops peripheral_ops = {
  5357. @@ -101,14 +105,14 @@ static const struct clk_ops peripheral_ops = {
  5358. };
  5359. static struct clk * __init
  5360. -at91_clk_register_peripheral(struct at91_pmc *pmc, const char *name,
  5361. +at91_clk_register_peripheral(struct regmap *regmap, const char *name,
  5362. const char *parent_name, u32 id)
  5363. {
  5364. struct clk_peripheral *periph;
  5365. struct clk *clk = NULL;
  5366. struct clk_init_data init;
  5367. - if (!pmc || !name || !parent_name || id > PERIPHERAL_ID_MAX)
  5368. + if (!name || !parent_name || id > PERIPHERAL_ID_MAX)
  5369. return ERR_PTR(-EINVAL);
  5370. periph = kzalloc(sizeof(*periph), GFP_KERNEL);
  5371. @@ -123,7 +127,7 @@ at91_clk_register_peripheral(struct at91_pmc *pmc, const char *name,
  5372. periph->id = id;
  5373. periph->hw.init = &init;
  5374. - periph->pmc = pmc;
  5375. + periph->regmap = regmap;
  5376. clk = clk_register(NULL, &periph->hw);
  5377. if (IS_ERR(clk))
  5378. @@ -160,53 +164,58 @@ static void clk_sam9x5_peripheral_autodiv(struct clk_sam9x5_peripheral *periph)
  5379. static int clk_sam9x5_peripheral_enable(struct clk_hw *hw)
  5380. {
  5381. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  5382. - struct at91_pmc *pmc = periph->pmc;
  5383. - u32 tmp;
  5384. + unsigned long flags;
  5385. if (periph->id < PERIPHERAL_ID_MIN)
  5386. return 0;
  5387. - pmc_lock(pmc);
  5388. - pmc_write(pmc, AT91_PMC_PCR, (periph->id & AT91_PMC_PCR_PID_MASK));
  5389. - tmp = pmc_read(pmc, AT91_PMC_PCR) & ~AT91_PMC_PCR_DIV_MASK;
  5390. - pmc_write(pmc, AT91_PMC_PCR, tmp | AT91_PMC_PCR_DIV(periph->div)
  5391. - | AT91_PMC_PCR_CMD
  5392. - | AT91_PMC_PCR_EN);
  5393. - pmc_unlock(pmc);
  5394. + spin_lock_irqsave(periph->lock, flags);
  5395. + regmap_write(periph->regmap, AT91_PMC_PCR,
  5396. + (periph->id & AT91_PMC_PCR_PID_MASK));
  5397. + regmap_update_bits(periph->regmap, AT91_PMC_PCR,
  5398. + AT91_PMC_PCR_DIV_MASK | AT91_PMC_PCR_CMD |
  5399. + AT91_PMC_PCR_EN,
  5400. + AT91_PMC_PCR_DIV(periph->div) |
  5401. + AT91_PMC_PCR_CMD |
  5402. + AT91_PMC_PCR_EN);
  5403. + spin_unlock_irqrestore(periph->lock, flags);
  5404. +
  5405. return 0;
  5406. }
  5407. static void clk_sam9x5_peripheral_disable(struct clk_hw *hw)
  5408. {
  5409. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  5410. - struct at91_pmc *pmc = periph->pmc;
  5411. - u32 tmp;
  5412. + unsigned long flags;
  5413. if (periph->id < PERIPHERAL_ID_MIN)
  5414. return;
  5415. - pmc_lock(pmc);
  5416. - pmc_write(pmc, AT91_PMC_PCR, (periph->id & AT91_PMC_PCR_PID_MASK));
  5417. - tmp = pmc_read(pmc, AT91_PMC_PCR) & ~AT91_PMC_PCR_EN;
  5418. - pmc_write(pmc, AT91_PMC_PCR, tmp | AT91_PMC_PCR_CMD);
  5419. - pmc_unlock(pmc);
  5420. + spin_lock_irqsave(periph->lock, flags);
  5421. + regmap_write(periph->regmap, AT91_PMC_PCR,
  5422. + (periph->id & AT91_PMC_PCR_PID_MASK));
  5423. + regmap_update_bits(periph->regmap, AT91_PMC_PCR,
  5424. + AT91_PMC_PCR_EN | AT91_PMC_PCR_CMD,
  5425. + AT91_PMC_PCR_CMD);
  5426. + spin_unlock_irqrestore(periph->lock, flags);
  5427. }
  5428. static int clk_sam9x5_peripheral_is_enabled(struct clk_hw *hw)
  5429. {
  5430. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  5431. - struct at91_pmc *pmc = periph->pmc;
  5432. - int ret;
  5433. + unsigned long flags;
  5434. + unsigned int status;
  5435. if (periph->id < PERIPHERAL_ID_MIN)
  5436. return 1;
  5437. - pmc_lock(pmc);
  5438. - pmc_write(pmc, AT91_PMC_PCR, (periph->id & AT91_PMC_PCR_PID_MASK));
  5439. - ret = !!(pmc_read(pmc, AT91_PMC_PCR) & AT91_PMC_PCR_EN);
  5440. - pmc_unlock(pmc);
  5441. + spin_lock_irqsave(periph->lock, flags);
  5442. + regmap_write(periph->regmap, AT91_PMC_PCR,
  5443. + (periph->id & AT91_PMC_PCR_PID_MASK));
  5444. + regmap_read(periph->regmap, AT91_PMC_PCR, &status);
  5445. + spin_unlock_irqrestore(periph->lock, flags);
  5446. - return ret;
  5447. + return status & AT91_PMC_PCR_EN ? 1 : 0;
  5448. }
  5449. static unsigned long
  5450. @@ -214,19 +223,20 @@ clk_sam9x5_peripheral_recalc_rate(struct clk_hw *hw,
  5451. unsigned long parent_rate)
  5452. {
  5453. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  5454. - struct at91_pmc *pmc = periph->pmc;
  5455. - u32 tmp;
  5456. + unsigned long flags;
  5457. + unsigned int status;
  5458. if (periph->id < PERIPHERAL_ID_MIN)
  5459. return parent_rate;
  5460. - pmc_lock(pmc);
  5461. - pmc_write(pmc, AT91_PMC_PCR, (periph->id & AT91_PMC_PCR_PID_MASK));
  5462. - tmp = pmc_read(pmc, AT91_PMC_PCR);
  5463. - pmc_unlock(pmc);
  5464. + spin_lock_irqsave(periph->lock, flags);
  5465. + regmap_write(periph->regmap, AT91_PMC_PCR,
  5466. + (periph->id & AT91_PMC_PCR_PID_MASK));
  5467. + regmap_read(periph->regmap, AT91_PMC_PCR, &status);
  5468. + spin_unlock_irqrestore(periph->lock, flags);
  5469. - if (tmp & AT91_PMC_PCR_EN) {
  5470. - periph->div = PERIPHERAL_RSHIFT(tmp);
  5471. + if (status & AT91_PMC_PCR_EN) {
  5472. + periph->div = PERIPHERAL_RSHIFT(status);
  5473. periph->auto_div = false;
  5474. } else {
  5475. clk_sam9x5_peripheral_autodiv(periph);
  5476. @@ -318,15 +328,15 @@ static const struct clk_ops sam9x5_peripheral_ops = {
  5477. };
  5478. static struct clk * __init
  5479. -at91_clk_register_sam9x5_peripheral(struct at91_pmc *pmc, const char *name,
  5480. - const char *parent_name, u32 id,
  5481. - const struct clk_range *range)
  5482. +at91_clk_register_sam9x5_peripheral(struct regmap *regmap, spinlock_t *lock,
  5483. + const char *name, const char *parent_name,
  5484. + u32 id, const struct clk_range *range)
  5485. {
  5486. struct clk_sam9x5_peripheral *periph;
  5487. struct clk *clk = NULL;
  5488. struct clk_init_data init;
  5489. - if (!pmc || !name || !parent_name)
  5490. + if (!name || !parent_name)
  5491. return ERR_PTR(-EINVAL);
  5492. periph = kzalloc(sizeof(*periph), GFP_KERNEL);
  5493. @@ -342,7 +352,8 @@ at91_clk_register_sam9x5_peripheral(struct at91_pmc *pmc, const char *name,
  5494. periph->id = id;
  5495. periph->hw.init = &init;
  5496. periph->div = 0;
  5497. - periph->pmc = pmc;
  5498. + periph->regmap = regmap;
  5499. + periph->lock = lock;
  5500. periph->auto_div = true;
  5501. periph->range = *range;
  5502. @@ -356,7 +367,7 @@ at91_clk_register_sam9x5_peripheral(struct at91_pmc *pmc, const char *name,
  5503. }
  5504. static void __init
  5505. -of_at91_clk_periph_setup(struct device_node *np, struct at91_pmc *pmc, u8 type)
  5506. +of_at91_clk_periph_setup(struct device_node *np, u8 type)
  5507. {
  5508. int num;
  5509. u32 id;
  5510. @@ -364,6 +375,7 @@ of_at91_clk_periph_setup(struct device_node *np, struct at91_pmc *pmc, u8 type)
  5511. const char *parent_name;
  5512. const char *name;
  5513. struct device_node *periphclknp;
  5514. + struct regmap *regmap;
  5515. parent_name = of_clk_get_parent_name(np, 0);
  5516. if (!parent_name)
  5517. @@ -373,6 +385,10 @@ of_at91_clk_periph_setup(struct device_node *np, struct at91_pmc *pmc, u8 type)
  5518. if (!num || num > PERIPHERAL_MAX)
  5519. return;
  5520. + regmap = syscon_node_to_regmap(of_get_parent(np));
  5521. + if (IS_ERR(regmap))
  5522. + return;
  5523. +
  5524. for_each_child_of_node(np, periphclknp) {
  5525. if (of_property_read_u32(periphclknp, "reg", &id))
  5526. continue;
  5527. @@ -384,7 +400,7 @@ of_at91_clk_periph_setup(struct device_node *np, struct at91_pmc *pmc, u8 type)
  5528. name = periphclknp->name;
  5529. if (type == PERIPHERAL_AT91RM9200) {
  5530. - clk = at91_clk_register_peripheral(pmc, name,
  5531. + clk = at91_clk_register_peripheral(regmap, name,
  5532. parent_name, id);
  5533. } else {
  5534. struct clk_range range = CLK_RANGE(0, 0);
  5535. @@ -393,7 +409,9 @@ of_at91_clk_periph_setup(struct device_node *np, struct at91_pmc *pmc, u8 type)
  5536. "atmel,clk-output-range",
  5537. &range);
  5538. - clk = at91_clk_register_sam9x5_peripheral(pmc, name,
  5539. + clk = at91_clk_register_sam9x5_peripheral(regmap,
  5540. + &pmc_pcr_lock,
  5541. + name,
  5542. parent_name,
  5543. id, &range);
  5544. }
  5545. @@ -405,14 +423,16 @@ of_at91_clk_periph_setup(struct device_node *np, struct at91_pmc *pmc, u8 type)
  5546. }
  5547. }
  5548. -void __init of_at91rm9200_clk_periph_setup(struct device_node *np,
  5549. - struct at91_pmc *pmc)
  5550. +static void __init of_at91rm9200_clk_periph_setup(struct device_node *np)
  5551. {
  5552. - of_at91_clk_periph_setup(np, pmc, PERIPHERAL_AT91RM9200);
  5553. + of_at91_clk_periph_setup(np, PERIPHERAL_AT91RM9200);
  5554. }
  5555. +CLK_OF_DECLARE(at91rm9200_clk_periph, "atmel,at91rm9200-clk-peripheral",
  5556. + of_at91rm9200_clk_periph_setup);
  5557. -void __init of_at91sam9x5_clk_periph_setup(struct device_node *np,
  5558. - struct at91_pmc *pmc)
  5559. +static void __init of_at91sam9x5_clk_periph_setup(struct device_node *np)
  5560. {
  5561. - of_at91_clk_periph_setup(np, pmc, PERIPHERAL_AT91SAM9X5);
  5562. + of_at91_clk_periph_setup(np, PERIPHERAL_AT91SAM9X5);
  5563. }
  5564. +CLK_OF_DECLARE(at91sam9x5_clk_periph, "atmel,at91sam9x5-clk-peripheral",
  5565. + of_at91sam9x5_clk_periph_setup);
  5566. diff --git a/drivers/clk/at91/clk-pll.c b/drivers/clk/at91/clk-pll.c
  5567. index 18b60f4895a6..fb2e0b56d4b7 100644
  5568. --- a/drivers/clk/at91/clk-pll.c
  5569. +++ b/drivers/clk/at91/clk-pll.c
  5570. @@ -12,14 +12,8 @@
  5571. #include <linux/clkdev.h>
  5572. #include <linux/clk/at91_pmc.h>
  5573. #include <linux/of.h>
  5574. -#include <linux/of_address.h>
  5575. -#include <linux/of_irq.h>
  5576. -#include <linux/io.h>
  5577. -#include <linux/kernel.h>
  5578. -#include <linux/wait.h>
  5579. -#include <linux/sched.h>
  5580. -#include <linux/interrupt.h>
  5581. -#include <linux/irq.h>
  5582. +#include <linux/mfd/syscon.h>
  5583. +#include <linux/regmap.h>
  5584. #include "pmc.h"
  5585. @@ -58,9 +52,7 @@ struct clk_pll_layout {
  5586. struct clk_pll {
  5587. struct clk_hw hw;
  5588. - struct at91_pmc *pmc;
  5589. - unsigned int irq;
  5590. - wait_queue_head_t wait;
  5591. + struct regmap *regmap;
  5592. u8 id;
  5593. u8 div;
  5594. u8 range;
  5595. @@ -69,20 +61,19 @@ struct clk_pll {
  5596. const struct clk_pll_characteristics *characteristics;
  5597. };
  5598. -static irqreturn_t clk_pll_irq_handler(int irq, void *dev_id)
  5599. +static inline bool clk_pll_ready(struct regmap *regmap, int id)
  5600. {
  5601. - struct clk_pll *pll = (struct clk_pll *)dev_id;
  5602. + unsigned int status;
  5603. - wake_up(&pll->wait);
  5604. - disable_irq_nosync(pll->irq);
  5605. + regmap_read(regmap, AT91_PMC_SR, &status);
  5606. - return IRQ_HANDLED;
  5607. + return status & PLL_STATUS_MASK(id) ? 1 : 0;
  5608. }
  5609. static int clk_pll_prepare(struct clk_hw *hw)
  5610. {
  5611. struct clk_pll *pll = to_clk_pll(hw);
  5612. - struct at91_pmc *pmc = pll->pmc;
  5613. + struct regmap *regmap = pll->regmap;
  5614. const struct clk_pll_layout *layout = pll->layout;
  5615. const struct clk_pll_characteristics *characteristics =
  5616. pll->characteristics;
  5617. @@ -90,39 +81,34 @@ static int clk_pll_prepare(struct clk_hw *hw)
  5618. u32 mask = PLL_STATUS_MASK(id);
  5619. int offset = PLL_REG(id);
  5620. u8 out = 0;
  5621. - u32 pllr, icpr;
  5622. + unsigned int pllr;
  5623. + unsigned int status;
  5624. u8 div;
  5625. u16 mul;
  5626. - pllr = pmc_read(pmc, offset);
  5627. + regmap_read(regmap, offset, &pllr);
  5628. div = PLL_DIV(pllr);
  5629. mul = PLL_MUL(pllr, layout);
  5630. - if ((pmc_read(pmc, AT91_PMC_SR) & mask) &&
  5631. + regmap_read(regmap, AT91_PMC_SR, &status);
  5632. + if ((status & mask) &&
  5633. (div == pll->div && mul == pll->mul))
  5634. return 0;
  5635. if (characteristics->out)
  5636. out = characteristics->out[pll->range];
  5637. - if (characteristics->icpll) {
  5638. - icpr = pmc_read(pmc, AT91_PMC_PLLICPR) & ~PLL_ICPR_MASK(id);
  5639. - icpr |= (characteristics->icpll[pll->range] <<
  5640. - PLL_ICPR_SHIFT(id));
  5641. - pmc_write(pmc, AT91_PMC_PLLICPR, icpr);
  5642. - }
  5643. - pllr &= ~layout->pllr_mask;
  5644. - pllr |= layout->pllr_mask &
  5645. - (pll->div | (PLL_MAX_COUNT << PLL_COUNT_SHIFT) |
  5646. - (out << PLL_OUT_SHIFT) |
  5647. - ((pll->mul & layout->mul_mask) << layout->mul_shift));
  5648. - pmc_write(pmc, offset, pllr);
  5649. -
  5650. - while (!(pmc_read(pmc, AT91_PMC_SR) & mask)) {
  5651. - enable_irq(pll->irq);
  5652. - wait_event(pll->wait,
  5653. - pmc_read(pmc, AT91_PMC_SR) & mask);
  5654. - }
  5655. + if (characteristics->icpll)
  5656. + regmap_update_bits(regmap, AT91_PMC_PLLICPR, PLL_ICPR_MASK(id),
  5657. + characteristics->icpll[pll->range] << PLL_ICPR_SHIFT(id));
  5658. +
  5659. + regmap_update_bits(regmap, offset, layout->pllr_mask,
  5660. + pll->div | (PLL_MAX_COUNT << PLL_COUNT_SHIFT) |
  5661. + (out << PLL_OUT_SHIFT) |
  5662. + ((pll->mul & layout->mul_mask) << layout->mul_shift));
  5663. +
  5664. + while (!clk_pll_ready(regmap, pll->id))
  5665. + cpu_relax();
  5666. return 0;
  5667. }
  5668. @@ -130,32 +116,35 @@ static int clk_pll_prepare(struct clk_hw *hw)
  5669. static int clk_pll_is_prepared(struct clk_hw *hw)
  5670. {
  5671. struct clk_pll *pll = to_clk_pll(hw);
  5672. - struct at91_pmc *pmc = pll->pmc;
  5673. - return !!(pmc_read(pmc, AT91_PMC_SR) &
  5674. - PLL_STATUS_MASK(pll->id));
  5675. + return clk_pll_ready(pll->regmap, pll->id);
  5676. }
  5677. static void clk_pll_unprepare(struct clk_hw *hw)
  5678. {
  5679. struct clk_pll *pll = to_clk_pll(hw);
  5680. - struct at91_pmc *pmc = pll->pmc;
  5681. - const struct clk_pll_layout *layout = pll->layout;
  5682. - int offset = PLL_REG(pll->id);
  5683. - u32 tmp = pmc_read(pmc, offset) & ~(layout->pllr_mask);
  5684. + unsigned int mask = pll->layout->pllr_mask;
  5685. - pmc_write(pmc, offset, tmp);
  5686. + regmap_update_bits(pll->regmap, PLL_REG(pll->id), mask, ~mask);
  5687. }
  5688. static unsigned long clk_pll_recalc_rate(struct clk_hw *hw,
  5689. unsigned long parent_rate)
  5690. {
  5691. struct clk_pll *pll = to_clk_pll(hw);
  5692. + unsigned int pllr;
  5693. + u16 mul;
  5694. + u8 div;
  5695. - if (!pll->div || !pll->mul)
  5696. + regmap_read(pll->regmap, PLL_REG(pll->id), &pllr);
  5697. +
  5698. + div = PLL_DIV(pllr);
  5699. + mul = PLL_MUL(pllr, pll->layout);
  5700. +
  5701. + if (!div || !mul)
  5702. return 0;
  5703. - return (parent_rate / pll->div) * (pll->mul + 1);
  5704. + return (parent_rate / div) * (mul + 1);
  5705. }
  5706. static long clk_pll_get_best_div_mul(struct clk_pll *pll, unsigned long rate,
  5707. @@ -308,7 +297,7 @@ static const struct clk_ops pll_ops = {
  5708. };
  5709. static struct clk * __init
  5710. -at91_clk_register_pll(struct at91_pmc *pmc, unsigned int irq, const char *name,
  5711. +at91_clk_register_pll(struct regmap *regmap, const char *name,
  5712. const char *parent_name, u8 id,
  5713. const struct clk_pll_layout *layout,
  5714. const struct clk_pll_characteristics *characteristics)
  5715. @@ -316,9 +305,8 @@ at91_clk_register_pll(struct at91_pmc *pmc, unsigned int irq, const char *name,
  5716. struct clk_pll *pll;
  5717. struct clk *clk = NULL;
  5718. struct clk_init_data init;
  5719. - int ret;
  5720. int offset = PLL_REG(id);
  5721. - u32 tmp;
  5722. + unsigned int pllr;
  5723. if (id > PLL_MAX_ID)
  5724. return ERR_PTR(-EINVAL);
  5725. @@ -337,23 +325,13 @@ at91_clk_register_pll(struct at91_pmc *pmc, unsigned int irq, const char *name,
  5726. pll->hw.init = &init;
  5727. pll->layout = layout;
  5728. pll->characteristics = characteristics;
  5729. - pll->pmc = pmc;
  5730. - pll->irq = irq;
  5731. - tmp = pmc_read(pmc, offset) & layout->pllr_mask;
  5732. - pll->div = PLL_DIV(tmp);
  5733. - pll->mul = PLL_MUL(tmp, layout);
  5734. - init_waitqueue_head(&pll->wait);
  5735. - irq_set_status_flags(pll->irq, IRQ_NOAUTOEN);
  5736. - ret = request_irq(pll->irq, clk_pll_irq_handler, IRQF_TRIGGER_HIGH,
  5737. - id ? "clk-pllb" : "clk-plla", pll);
  5738. - if (ret) {
  5739. - kfree(pll);
  5740. - return ERR_PTR(ret);
  5741. - }
  5742. + pll->regmap = regmap;
  5743. + regmap_read(regmap, offset, &pllr);
  5744. + pll->div = PLL_DIV(pllr);
  5745. + pll->mul = PLL_MUL(pllr, layout);
  5746. clk = clk_register(NULL, &pll->hw);
  5747. if (IS_ERR(clk)) {
  5748. - free_irq(pll->irq, pll);
  5749. kfree(pll);
  5750. }
  5751. @@ -483,12 +461,12 @@ out_free_characteristics:
  5752. }
  5753. static void __init
  5754. -of_at91_clk_pll_setup(struct device_node *np, struct at91_pmc *pmc,
  5755. +of_at91_clk_pll_setup(struct device_node *np,
  5756. const struct clk_pll_layout *layout)
  5757. {
  5758. u32 id;
  5759. - unsigned int irq;
  5760. struct clk *clk;
  5761. + struct regmap *regmap;
  5762. const char *parent_name;
  5763. const char *name = np->name;
  5764. struct clk_pll_characteristics *characteristics;
  5765. @@ -500,15 +478,15 @@ of_at91_clk_pll_setup(struct device_node *np, struct at91_pmc *pmc,
  5766. of_property_read_string(np, "clock-output-names", &name);
  5767. - characteristics = of_at91_clk_pll_get_characteristics(np);
  5768. - if (!characteristics)
  5769. + regmap = syscon_node_to_regmap(of_get_parent(np));
  5770. + if (IS_ERR(regmap))
  5771. return;
  5772. - irq = irq_of_parse_and_map(np, 0);
  5773. - if (!irq)
  5774. + characteristics = of_at91_clk_pll_get_characteristics(np);
  5775. + if (!characteristics)
  5776. return;
  5777. - clk = at91_clk_register_pll(pmc, irq, name, parent_name, id, layout,
  5778. + clk = at91_clk_register_pll(regmap, name, parent_name, id, layout,
  5779. characteristics);
  5780. if (IS_ERR(clk))
  5781. goto out_free_characteristics;
  5782. @@ -520,26 +498,30 @@ out_free_characteristics:
  5783. kfree(characteristics);
  5784. }
  5785. -void __init of_at91rm9200_clk_pll_setup(struct device_node *np,
  5786. - struct at91_pmc *pmc)
  5787. +static void __init of_at91rm9200_clk_pll_setup(struct device_node *np)
  5788. {
  5789. - of_at91_clk_pll_setup(np, pmc, &at91rm9200_pll_layout);
  5790. + of_at91_clk_pll_setup(np, &at91rm9200_pll_layout);
  5791. }
  5792. +CLK_OF_DECLARE(at91rm9200_clk_pll, "atmel,at91rm9200-clk-pll",
  5793. + of_at91rm9200_clk_pll_setup);
  5794. -void __init of_at91sam9g45_clk_pll_setup(struct device_node *np,
  5795. - struct at91_pmc *pmc)
  5796. +static void __init of_at91sam9g45_clk_pll_setup(struct device_node *np)
  5797. {
  5798. - of_at91_clk_pll_setup(np, pmc, &at91sam9g45_pll_layout);
  5799. + of_at91_clk_pll_setup(np, &at91sam9g45_pll_layout);
  5800. }
  5801. +CLK_OF_DECLARE(at91sam9g45_clk_pll, "atmel,at91sam9g45-clk-pll",
  5802. + of_at91sam9g45_clk_pll_setup);
  5803. -void __init of_at91sam9g20_clk_pllb_setup(struct device_node *np,
  5804. - struct at91_pmc *pmc)
  5805. +static void __init of_at91sam9g20_clk_pllb_setup(struct device_node *np)
  5806. {
  5807. - of_at91_clk_pll_setup(np, pmc, &at91sam9g20_pllb_layout);
  5808. + of_at91_clk_pll_setup(np, &at91sam9g20_pllb_layout);
  5809. }
  5810. +CLK_OF_DECLARE(at91sam9g20_clk_pllb, "atmel,at91sam9g20-clk-pllb",
  5811. + of_at91sam9g20_clk_pllb_setup);
  5812. -void __init of_sama5d3_clk_pll_setup(struct device_node *np,
  5813. - struct at91_pmc *pmc)
  5814. +static void __init of_sama5d3_clk_pll_setup(struct device_node *np)
  5815. {
  5816. - of_at91_clk_pll_setup(np, pmc, &sama5d3_pll_layout);
  5817. + of_at91_clk_pll_setup(np, &sama5d3_pll_layout);
  5818. }
  5819. +CLK_OF_DECLARE(sama5d3_clk_pll, "atmel,sama5d3-clk-pll",
  5820. + of_sama5d3_clk_pll_setup);
  5821. diff --git a/drivers/clk/at91/clk-plldiv.c b/drivers/clk/at91/clk-plldiv.c
  5822. index ea226562bb40..2bed26481027 100644
  5823. --- a/drivers/clk/at91/clk-plldiv.c
  5824. +++ b/drivers/clk/at91/clk-plldiv.c
  5825. @@ -12,8 +12,8 @@
  5826. #include <linux/clkdev.h>
  5827. #include <linux/clk/at91_pmc.h>
  5828. #include <linux/of.h>
  5829. -#include <linux/of_address.h>
  5830. -#include <linux/io.h>
  5831. +#include <linux/mfd/syscon.h>
  5832. +#include <linux/regmap.h>
  5833. #include "pmc.h"
  5834. @@ -21,16 +21,18 @@
  5835. struct clk_plldiv {
  5836. struct clk_hw hw;
  5837. - struct at91_pmc *pmc;
  5838. + struct regmap *regmap;
  5839. };
  5840. static unsigned long clk_plldiv_recalc_rate(struct clk_hw *hw,
  5841. unsigned long parent_rate)
  5842. {
  5843. struct clk_plldiv *plldiv = to_clk_plldiv(hw);
  5844. - struct at91_pmc *pmc = plldiv->pmc;
  5845. + unsigned int mckr;
  5846. - if (pmc_read(pmc, AT91_PMC_MCKR) & AT91_PMC_PLLADIV2)
  5847. + regmap_read(plldiv->regmap, AT91_PMC_MCKR, &mckr);
  5848. +
  5849. + if (mckr & AT91_PMC_PLLADIV2)
  5850. return parent_rate / 2;
  5851. return parent_rate;
  5852. @@ -57,18 +59,12 @@ static int clk_plldiv_set_rate(struct clk_hw *hw, unsigned long rate,
  5853. unsigned long parent_rate)
  5854. {
  5855. struct clk_plldiv *plldiv = to_clk_plldiv(hw);
  5856. - struct at91_pmc *pmc = plldiv->pmc;
  5857. - u32 tmp;
  5858. - if (parent_rate != rate && (parent_rate / 2) != rate)
  5859. + if ((parent_rate != rate) && (parent_rate / 2 != rate))
  5860. return -EINVAL;
  5861. - pmc_lock(pmc);
  5862. - tmp = pmc_read(pmc, AT91_PMC_MCKR) & ~AT91_PMC_PLLADIV2;
  5863. - if ((parent_rate / 2) == rate)
  5864. - tmp |= AT91_PMC_PLLADIV2;
  5865. - pmc_write(pmc, AT91_PMC_MCKR, tmp);
  5866. - pmc_unlock(pmc);
  5867. + regmap_update_bits(plldiv->regmap, AT91_PMC_MCKR, AT91_PMC_PLLADIV2,
  5868. + parent_rate != rate ? AT91_PMC_PLLADIV2 : 0);
  5869. return 0;
  5870. }
  5871. @@ -80,7 +76,7 @@ static const struct clk_ops plldiv_ops = {
  5872. };
  5873. static struct clk * __init
  5874. -at91_clk_register_plldiv(struct at91_pmc *pmc, const char *name,
  5875. +at91_clk_register_plldiv(struct regmap *regmap, const char *name,
  5876. const char *parent_name)
  5877. {
  5878. struct clk_plldiv *plldiv;
  5879. @@ -98,7 +94,7 @@ at91_clk_register_plldiv(struct at91_pmc *pmc, const char *name,
  5880. init.flags = CLK_SET_RATE_GATE;
  5881. plldiv->hw.init = &init;
  5882. - plldiv->pmc = pmc;
  5883. + plldiv->regmap = regmap;
  5884. clk = clk_register(NULL, &plldiv->hw);
  5885. @@ -109,27 +105,27 @@ at91_clk_register_plldiv(struct at91_pmc *pmc, const char *name,
  5886. }
  5887. static void __init
  5888. -of_at91_clk_plldiv_setup(struct device_node *np, struct at91_pmc *pmc)
  5889. +of_at91sam9x5_clk_plldiv_setup(struct device_node *np)
  5890. {
  5891. struct clk *clk;
  5892. const char *parent_name;
  5893. const char *name = np->name;
  5894. + struct regmap *regmap;
  5895. parent_name = of_clk_get_parent_name(np, 0);
  5896. of_property_read_string(np, "clock-output-names", &name);
  5897. - clk = at91_clk_register_plldiv(pmc, name, parent_name);
  5898. + regmap = syscon_node_to_regmap(of_get_parent(np));
  5899. + if (IS_ERR(regmap))
  5900. + return;
  5901. + clk = at91_clk_register_plldiv(regmap, name, parent_name);
  5902. if (IS_ERR(clk))
  5903. return;
  5904. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  5905. return;
  5906. }
  5907. -
  5908. -void __init of_at91sam9x5_clk_plldiv_setup(struct device_node *np,
  5909. - struct at91_pmc *pmc)
  5910. -{
  5911. - of_at91_clk_plldiv_setup(np, pmc);
  5912. -}
  5913. +CLK_OF_DECLARE(at91sam9x5_clk_plldiv, "atmel,at91sam9x5-clk-plldiv",
  5914. + of_at91sam9x5_clk_plldiv_setup);
  5915. diff --git a/drivers/clk/at91/clk-programmable.c b/drivers/clk/at91/clk-programmable.c
  5916. index 14b270b85fec..bc0be629671b 100644
  5917. --- a/drivers/clk/at91/clk-programmable.c
  5918. +++ b/drivers/clk/at91/clk-programmable.c
  5919. @@ -12,10 +12,8 @@
  5920. #include <linux/clkdev.h>
  5921. #include <linux/clk/at91_pmc.h>
  5922. #include <linux/of.h>
  5923. -#include <linux/of_address.h>
  5924. -#include <linux/io.h>
  5925. -#include <linux/wait.h>
  5926. -#include <linux/sched.h>
  5927. +#include <linux/mfd/syscon.h>
  5928. +#include <linux/regmap.h>
  5929. #include "pmc.h"
  5930. @@ -24,6 +22,7 @@
  5931. #define PROG_STATUS_MASK(id) (1 << ((id) + 8))
  5932. #define PROG_PRES_MASK 0x7
  5933. +#define PROG_PRES(layout, pckr) ((pckr >> layout->pres_shift) & PROG_PRES_MASK)
  5934. #define PROG_MAX_RM9200_CSS 3
  5935. struct clk_programmable_layout {
  5936. @@ -34,7 +33,7 @@ struct clk_programmable_layout {
  5937. struct clk_programmable {
  5938. struct clk_hw hw;
  5939. - struct at91_pmc *pmc;
  5940. + struct regmap *regmap;
  5941. u8 id;
  5942. const struct clk_programmable_layout *layout;
  5943. };
  5944. @@ -44,14 +43,12 @@ struct clk_programmable {
  5945. static unsigned long clk_programmable_recalc_rate(struct clk_hw *hw,
  5946. unsigned long parent_rate)
  5947. {
  5948. - u32 pres;
  5949. struct clk_programmable *prog = to_clk_programmable(hw);
  5950. - struct at91_pmc *pmc = prog->pmc;
  5951. - const struct clk_programmable_layout *layout = prog->layout;
  5952. + unsigned int pckr;
  5953. +
  5954. + regmap_read(prog->regmap, AT91_PMC_PCKR(prog->id), &pckr);
  5955. - pres = (pmc_read(pmc, AT91_PMC_PCKR(prog->id)) >> layout->pres_shift) &
  5956. - PROG_PRES_MASK;
  5957. - return parent_rate >> pres;
  5958. + return parent_rate >> PROG_PRES(prog->layout, pckr);
  5959. }
  5960. static int clk_programmable_determine_rate(struct clk_hw *hw,
  5961. @@ -101,36 +98,36 @@ static int clk_programmable_set_parent(struct clk_hw *hw, u8 index)
  5962. {
  5963. struct clk_programmable *prog = to_clk_programmable(hw);
  5964. const struct clk_programmable_layout *layout = prog->layout;
  5965. - struct at91_pmc *pmc = prog->pmc;
  5966. - u32 tmp = pmc_read(pmc, AT91_PMC_PCKR(prog->id)) & ~layout->css_mask;
  5967. + unsigned int mask = layout->css_mask;
  5968. + unsigned int pckr = 0;
  5969. if (layout->have_slck_mck)
  5970. - tmp &= AT91_PMC_CSSMCK_MCK;
  5971. + mask |= AT91_PMC_CSSMCK_MCK;
  5972. if (index > layout->css_mask) {
  5973. - if (index > PROG_MAX_RM9200_CSS && layout->have_slck_mck) {
  5974. - tmp |= AT91_PMC_CSSMCK_MCK;
  5975. - return 0;
  5976. - } else {
  5977. + if (index > PROG_MAX_RM9200_CSS && !layout->have_slck_mck)
  5978. return -EINVAL;
  5979. - }
  5980. +
  5981. + pckr |= AT91_PMC_CSSMCK_MCK;
  5982. }
  5983. - pmc_write(pmc, AT91_PMC_PCKR(prog->id), tmp | index);
  5984. + regmap_update_bits(prog->regmap, AT91_PMC_PCKR(prog->id), mask, pckr);
  5985. +
  5986. return 0;
  5987. }
  5988. static u8 clk_programmable_get_parent(struct clk_hw *hw)
  5989. {
  5990. - u32 tmp;
  5991. - u8 ret;
  5992. struct clk_programmable *prog = to_clk_programmable(hw);
  5993. - struct at91_pmc *pmc = prog->pmc;
  5994. const struct clk_programmable_layout *layout = prog->layout;
  5995. + unsigned int pckr;
  5996. + u8 ret;
  5997. +
  5998. + regmap_read(prog->regmap, AT91_PMC_PCKR(prog->id), &pckr);
  5999. +
  6000. + ret = pckr & layout->css_mask;
  6001. - tmp = pmc_read(pmc, AT91_PMC_PCKR(prog->id));
  6002. - ret = tmp & layout->css_mask;
  6003. - if (layout->have_slck_mck && (tmp & AT91_PMC_CSSMCK_MCK) && !ret)
  6004. + if (layout->have_slck_mck && (pckr & AT91_PMC_CSSMCK_MCK) && !ret)
  6005. ret = PROG_MAX_RM9200_CSS + 1;
  6006. return ret;
  6007. @@ -140,26 +137,27 @@ static int clk_programmable_set_rate(struct clk_hw *hw, unsigned long rate,
  6008. unsigned long parent_rate)
  6009. {
  6010. struct clk_programmable *prog = to_clk_programmable(hw);
  6011. - struct at91_pmc *pmc = prog->pmc;
  6012. const struct clk_programmable_layout *layout = prog->layout;
  6013. unsigned long div = parent_rate / rate;
  6014. + unsigned int pckr;
  6015. int shift = 0;
  6016. - u32 tmp = pmc_read(pmc, AT91_PMC_PCKR(prog->id)) &
  6017. - ~(PROG_PRES_MASK << layout->pres_shift);
  6018. +
  6019. + regmap_read(prog->regmap, AT91_PMC_PCKR(prog->id), &pckr);
  6020. if (!div)
  6021. return -EINVAL;
  6022. shift = fls(div) - 1;
  6023. - if (div != (1<<shift))
  6024. + if (div != (1 << shift))
  6025. return -EINVAL;
  6026. if (shift >= PROG_PRES_MASK)
  6027. return -EINVAL;
  6028. - pmc_write(pmc, AT91_PMC_PCKR(prog->id),
  6029. - tmp | (shift << layout->pres_shift));
  6030. + regmap_update_bits(prog->regmap, AT91_PMC_PCKR(prog->id),
  6031. + PROG_PRES_MASK << layout->pres_shift,
  6032. + shift << layout->pres_shift);
  6033. return 0;
  6034. }
  6035. @@ -173,7 +171,7 @@ static const struct clk_ops programmable_ops = {
  6036. };
  6037. static struct clk * __init
  6038. -at91_clk_register_programmable(struct at91_pmc *pmc,
  6039. +at91_clk_register_programmable(struct regmap *regmap,
  6040. const char *name, const char **parent_names,
  6041. u8 num_parents, u8 id,
  6042. const struct clk_programmable_layout *layout)
  6043. @@ -198,7 +196,7 @@ at91_clk_register_programmable(struct at91_pmc *pmc,
  6044. prog->id = id;
  6045. prog->layout = layout;
  6046. prog->hw.init = &init;
  6047. - prog->pmc = pmc;
  6048. + prog->regmap = regmap;
  6049. clk = clk_register(NULL, &prog->hw);
  6050. if (IS_ERR(clk))
  6051. @@ -226,7 +224,7 @@ static const struct clk_programmable_layout at91sam9x5_programmable_layout = {
  6052. };
  6053. static void __init
  6054. -of_at91_clk_prog_setup(struct device_node *np, struct at91_pmc *pmc,
  6055. +of_at91_clk_prog_setup(struct device_node *np,
  6056. const struct clk_programmable_layout *layout)
  6057. {
  6058. int num;
  6059. @@ -236,6 +234,7 @@ of_at91_clk_prog_setup(struct device_node *np, struct at91_pmc *pmc,
  6060. const char *parent_names[PROG_SOURCE_MAX];
  6061. const char *name;
  6062. struct device_node *progclknp;
  6063. + struct regmap *regmap;
  6064. num_parents = of_clk_get_parent_count(np);
  6065. if (num_parents <= 0 || num_parents > PROG_SOURCE_MAX)
  6066. @@ -247,6 +246,10 @@ of_at91_clk_prog_setup(struct device_node *np, struct at91_pmc *pmc,
  6067. if (!num || num > (PROG_ID_MAX + 1))
  6068. return;
  6069. + regmap = syscon_node_to_regmap(of_get_parent(np));
  6070. + if (IS_ERR(regmap))
  6071. + return;
  6072. +
  6073. for_each_child_of_node(np, progclknp) {
  6074. if (of_property_read_u32(progclknp, "reg", &id))
  6075. continue;
  6076. @@ -254,7 +257,7 @@ of_at91_clk_prog_setup(struct device_node *np, struct at91_pmc *pmc,
  6077. if (of_property_read_string(np, "clock-output-names", &name))
  6078. name = progclknp->name;
  6079. - clk = at91_clk_register_programmable(pmc, name,
  6080. + clk = at91_clk_register_programmable(regmap, name,
  6081. parent_names, num_parents,
  6082. id, layout);
  6083. if (IS_ERR(clk))
  6084. @@ -265,20 +268,23 @@ of_at91_clk_prog_setup(struct device_node *np, struct at91_pmc *pmc,
  6085. }
  6086. -void __init of_at91rm9200_clk_prog_setup(struct device_node *np,
  6087. - struct at91_pmc *pmc)
  6088. +static void __init of_at91rm9200_clk_prog_setup(struct device_node *np)
  6089. {
  6090. - of_at91_clk_prog_setup(np, pmc, &at91rm9200_programmable_layout);
  6091. + of_at91_clk_prog_setup(np, &at91rm9200_programmable_layout);
  6092. }
  6093. +CLK_OF_DECLARE(at91rm9200_clk_prog, "atmel,at91rm9200-clk-programmable",
  6094. + of_at91rm9200_clk_prog_setup);
  6095. -void __init of_at91sam9g45_clk_prog_setup(struct device_node *np,
  6096. - struct at91_pmc *pmc)
  6097. +static void __init of_at91sam9g45_clk_prog_setup(struct device_node *np)
  6098. {
  6099. - of_at91_clk_prog_setup(np, pmc, &at91sam9g45_programmable_layout);
  6100. + of_at91_clk_prog_setup(np, &at91sam9g45_programmable_layout);
  6101. }
  6102. +CLK_OF_DECLARE(at91sam9g45_clk_prog, "atmel,at91sam9g45-clk-programmable",
  6103. + of_at91sam9g45_clk_prog_setup);
  6104. -void __init of_at91sam9x5_clk_prog_setup(struct device_node *np,
  6105. - struct at91_pmc *pmc)
  6106. +static void __init of_at91sam9x5_clk_prog_setup(struct device_node *np)
  6107. {
  6108. - of_at91_clk_prog_setup(np, pmc, &at91sam9x5_programmable_layout);
  6109. + of_at91_clk_prog_setup(np, &at91sam9x5_programmable_layout);
  6110. }
  6111. +CLK_OF_DECLARE(at91sam9x5_clk_prog, "atmel,at91sam9x5-clk-programmable",
  6112. + of_at91sam9x5_clk_prog_setup);
  6113. diff --git a/drivers/clk/at91/clk-slow.c b/drivers/clk/at91/clk-slow.c
  6114. index d0d5076a9b94..221c09684ba3 100644
  6115. --- a/drivers/clk/at91/clk-slow.c
  6116. +++ b/drivers/clk/at91/clk-slow.c
  6117. @@ -13,17 +13,11 @@
  6118. #include <linux/clk.h>
  6119. #include <linux/clk-provider.h>
  6120. #include <linux/clkdev.h>
  6121. -#include <linux/slab.h>
  6122. #include <linux/clk/at91_pmc.h>
  6123. #include <linux/delay.h>
  6124. #include <linux/of.h>
  6125. -#include <linux/of_address.h>
  6126. -#include <linux/of_irq.h>
  6127. -#include <linux/io.h>
  6128. -#include <linux/interrupt.h>
  6129. -#include <linux/irq.h>
  6130. -#include <linux/sched.h>
  6131. -#include <linux/wait.h>
  6132. +#include <linux/mfd/syscon.h>
  6133. +#include <linux/regmap.h>
  6134. #include "pmc.h"
  6135. #include "sckc.h"
  6136. @@ -59,7 +53,7 @@ struct clk_slow_rc_osc {
  6137. struct clk_sam9260_slow {
  6138. struct clk_hw hw;
  6139. - struct at91_pmc *pmc;
  6140. + struct regmap *regmap;
  6141. };
  6142. #define to_clk_sam9260_slow(hw) container_of(hw, struct clk_sam9260_slow, hw)
  6143. @@ -393,8 +387,11 @@ void __init of_at91sam9x5_clk_slow_setup(struct device_node *np,
  6144. static u8 clk_sam9260_slow_get_parent(struct clk_hw *hw)
  6145. {
  6146. struct clk_sam9260_slow *slowck = to_clk_sam9260_slow(hw);
  6147. + unsigned int status;
  6148. - return !!(pmc_read(slowck->pmc, AT91_PMC_SR) & AT91_PMC_OSCSEL);
  6149. + regmap_read(slowck->regmap, AT91_PMC_SR, &status);
  6150. +
  6151. + return status & AT91_PMC_OSCSEL ? 1 : 0;
  6152. }
  6153. static const struct clk_ops sam9260_slow_ops = {
  6154. @@ -402,7 +399,7 @@ static const struct clk_ops sam9260_slow_ops = {
  6155. };
  6156. static struct clk * __init
  6157. -at91_clk_register_sam9260_slow(struct at91_pmc *pmc,
  6158. +at91_clk_register_sam9260_slow(struct regmap *regmap,
  6159. const char *name,
  6160. const char **parent_names,
  6161. int num_parents)
  6162. @@ -411,7 +408,7 @@ at91_clk_register_sam9260_slow(struct at91_pmc *pmc,
  6163. struct clk *clk = NULL;
  6164. struct clk_init_data init;
  6165. - if (!pmc || !name)
  6166. + if (!name)
  6167. return ERR_PTR(-EINVAL);
  6168. if (!parent_names || !num_parents)
  6169. @@ -428,7 +425,7 @@ at91_clk_register_sam9260_slow(struct at91_pmc *pmc,
  6170. init.flags = 0;
  6171. slowck->hw.init = &init;
  6172. - slowck->pmc = pmc;
  6173. + slowck->regmap = regmap;
  6174. clk = clk_register(NULL, &slowck->hw);
  6175. if (IS_ERR(clk))
  6176. @@ -439,29 +436,34 @@ at91_clk_register_sam9260_slow(struct at91_pmc *pmc,
  6177. return clk;
  6178. }
  6179. -void __init of_at91sam9260_clk_slow_setup(struct device_node *np,
  6180. - struct at91_pmc *pmc)
  6181. +static void __init of_at91sam9260_clk_slow_setup(struct device_node *np)
  6182. {
  6183. struct clk *clk;
  6184. const char *parent_names[2];
  6185. int num_parents;
  6186. const char *name = np->name;
  6187. + struct regmap *regmap;
  6188. num_parents = of_clk_get_parent_count(np);
  6189. if (num_parents != 2)
  6190. return;
  6191. of_clk_parent_fill(np, parent_names, num_parents);
  6192. + regmap = syscon_node_to_regmap(of_get_parent(np));
  6193. + if (IS_ERR(regmap))
  6194. + return;
  6195. of_property_read_string(np, "clock-output-names", &name);
  6196. - clk = at91_clk_register_sam9260_slow(pmc, name, parent_names,
  6197. + clk = at91_clk_register_sam9260_slow(regmap, name, parent_names,
  6198. num_parents);
  6199. if (IS_ERR(clk))
  6200. return;
  6201. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  6202. }
  6203. +CLK_OF_DECLARE(at91sam9260_clk_slow, "atmel,at91sam9260-clk-slow",
  6204. + of_at91sam9260_clk_slow_setup);
  6205. /*
  6206. * FIXME: All slow clk users are not properly claiming it (get + prepare +
  6207. diff --git a/drivers/clk/at91/clk-smd.c b/drivers/clk/at91/clk-smd.c
  6208. index a7f8501cfa05..e6948a52005a 100644
  6209. --- a/drivers/clk/at91/clk-smd.c
  6210. +++ b/drivers/clk/at91/clk-smd.c
  6211. @@ -12,8 +12,8 @@
  6212. #include <linux/clkdev.h>
  6213. #include <linux/clk/at91_pmc.h>
  6214. #include <linux/of.h>
  6215. -#include <linux/of_address.h>
  6216. -#include <linux/io.h>
  6217. +#include <linux/mfd/syscon.h>
  6218. +#include <linux/regmap.h>
  6219. #include "pmc.h"
  6220. @@ -24,7 +24,7 @@
  6221. struct at91sam9x5_clk_smd {
  6222. struct clk_hw hw;
  6223. - struct at91_pmc *pmc;
  6224. + struct regmap *regmap;
  6225. };
  6226. #define to_at91sam9x5_clk_smd(hw) \
  6227. @@ -33,13 +33,13 @@ struct at91sam9x5_clk_smd {
  6228. static unsigned long at91sam9x5_clk_smd_recalc_rate(struct clk_hw *hw,
  6229. unsigned long parent_rate)
  6230. {
  6231. - u32 tmp;
  6232. - u8 smddiv;
  6233. struct at91sam9x5_clk_smd *smd = to_at91sam9x5_clk_smd(hw);
  6234. - struct at91_pmc *pmc = smd->pmc;
  6235. + unsigned int smdr;
  6236. + u8 smddiv;
  6237. +
  6238. + regmap_read(smd->regmap, AT91_PMC_SMD, &smdr);
  6239. + smddiv = (smdr & AT91_PMC_SMD_DIV) >> SMD_DIV_SHIFT;
  6240. - tmp = pmc_read(pmc, AT91_PMC_SMD);
  6241. - smddiv = (tmp & AT91_PMC_SMD_DIV) >> SMD_DIV_SHIFT;
  6242. return parent_rate / (smddiv + 1);
  6243. }
  6244. @@ -67,40 +67,38 @@ static long at91sam9x5_clk_smd_round_rate(struct clk_hw *hw, unsigned long rate,
  6245. static int at91sam9x5_clk_smd_set_parent(struct clk_hw *hw, u8 index)
  6246. {
  6247. - u32 tmp;
  6248. struct at91sam9x5_clk_smd *smd = to_at91sam9x5_clk_smd(hw);
  6249. - struct at91_pmc *pmc = smd->pmc;
  6250. if (index > 1)
  6251. return -EINVAL;
  6252. - tmp = pmc_read(pmc, AT91_PMC_SMD) & ~AT91_PMC_SMDS;
  6253. - if (index)
  6254. - tmp |= AT91_PMC_SMDS;
  6255. - pmc_write(pmc, AT91_PMC_SMD, tmp);
  6256. +
  6257. + regmap_update_bits(smd->regmap, AT91_PMC_SMD, AT91_PMC_SMDS,
  6258. + index ? AT91_PMC_SMDS : 0);
  6259. +
  6260. return 0;
  6261. }
  6262. static u8 at91sam9x5_clk_smd_get_parent(struct clk_hw *hw)
  6263. {
  6264. struct at91sam9x5_clk_smd *smd = to_at91sam9x5_clk_smd(hw);
  6265. - struct at91_pmc *pmc = smd->pmc;
  6266. + unsigned int smdr;
  6267. - return pmc_read(pmc, AT91_PMC_SMD) & AT91_PMC_SMDS;
  6268. + regmap_read(smd->regmap, AT91_PMC_SMD, &smdr);
  6269. +
  6270. + return smdr & AT91_PMC_SMDS;
  6271. }
  6272. static int at91sam9x5_clk_smd_set_rate(struct clk_hw *hw, unsigned long rate,
  6273. unsigned long parent_rate)
  6274. {
  6275. - u32 tmp;
  6276. struct at91sam9x5_clk_smd *smd = to_at91sam9x5_clk_smd(hw);
  6277. - struct at91_pmc *pmc = smd->pmc;
  6278. unsigned long div = parent_rate / rate;
  6279. if (parent_rate % rate || div < 1 || div > (SMD_MAX_DIV + 1))
  6280. return -EINVAL;
  6281. - tmp = pmc_read(pmc, AT91_PMC_SMD) & ~AT91_PMC_SMD_DIV;
  6282. - tmp |= (div - 1) << SMD_DIV_SHIFT;
  6283. - pmc_write(pmc, AT91_PMC_SMD, tmp);
  6284. +
  6285. + regmap_update_bits(smd->regmap, AT91_PMC_SMD, AT91_PMC_SMD_DIV,
  6286. + (div - 1) << SMD_DIV_SHIFT);
  6287. return 0;
  6288. }
  6289. @@ -114,7 +112,7 @@ static const struct clk_ops at91sam9x5_smd_ops = {
  6290. };
  6291. static struct clk * __init
  6292. -at91sam9x5_clk_register_smd(struct at91_pmc *pmc, const char *name,
  6293. +at91sam9x5_clk_register_smd(struct regmap *regmap, const char *name,
  6294. const char **parent_names, u8 num_parents)
  6295. {
  6296. struct at91sam9x5_clk_smd *smd;
  6297. @@ -132,7 +130,7 @@ at91sam9x5_clk_register_smd(struct at91_pmc *pmc, const char *name,
  6298. init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
  6299. smd->hw.init = &init;
  6300. - smd->pmc = pmc;
  6301. + smd->regmap = regmap;
  6302. clk = clk_register(NULL, &smd->hw);
  6303. if (IS_ERR(clk))
  6304. @@ -141,13 +139,13 @@ at91sam9x5_clk_register_smd(struct at91_pmc *pmc, const char *name,
  6305. return clk;
  6306. }
  6307. -void __init of_at91sam9x5_clk_smd_setup(struct device_node *np,
  6308. - struct at91_pmc *pmc)
  6309. +static void __init of_at91sam9x5_clk_smd_setup(struct device_node *np)
  6310. {
  6311. struct clk *clk;
  6312. int num_parents;
  6313. const char *parent_names[SMD_SOURCE_MAX];
  6314. const char *name = np->name;
  6315. + struct regmap *regmap;
  6316. num_parents = of_clk_get_parent_count(np);
  6317. if (num_parents <= 0 || num_parents > SMD_SOURCE_MAX)
  6318. @@ -157,10 +155,16 @@ void __init of_at91sam9x5_clk_smd_setup(struct device_node *np,
  6319. of_property_read_string(np, "clock-output-names", &name);
  6320. - clk = at91sam9x5_clk_register_smd(pmc, name, parent_names,
  6321. + regmap = syscon_node_to_regmap(of_get_parent(np));
  6322. + if (IS_ERR(regmap))
  6323. + return;
  6324. +
  6325. + clk = at91sam9x5_clk_register_smd(regmap, name, parent_names,
  6326. num_parents);
  6327. if (IS_ERR(clk))
  6328. return;
  6329. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  6330. }
  6331. +CLK_OF_DECLARE(at91sam9x5_clk_smd, "atmel,at91sam9x5-clk-smd",
  6332. + of_at91sam9x5_clk_smd_setup);
  6333. diff --git a/drivers/clk/at91/clk-system.c b/drivers/clk/at91/clk-system.c
  6334. index 3f5314344286..8f35d8172909 100644
  6335. --- a/drivers/clk/at91/clk-system.c
  6336. +++ b/drivers/clk/at91/clk-system.c
  6337. @@ -12,13 +12,8 @@
  6338. #include <linux/clkdev.h>
  6339. #include <linux/clk/at91_pmc.h>
  6340. #include <linux/of.h>
  6341. -#include <linux/of_address.h>
  6342. -#include <linux/io.h>
  6343. -#include <linux/irq.h>
  6344. -#include <linux/of_irq.h>
  6345. -#include <linux/interrupt.h>
  6346. -#include <linux/wait.h>
  6347. -#include <linux/sched.h>
  6348. +#include <linux/mfd/syscon.h>
  6349. +#include <linux/regmap.h>
  6350. #include "pmc.h"
  6351. @@ -29,9 +24,7 @@
  6352. #define to_clk_system(hw) container_of(hw, struct clk_system, hw)
  6353. struct clk_system {
  6354. struct clk_hw hw;
  6355. - struct at91_pmc *pmc;
  6356. - unsigned int irq;
  6357. - wait_queue_head_t wait;
  6358. + struct regmap *regmap;
  6359. u8 id;
  6360. };
  6361. @@ -39,58 +32,54 @@ static inline int is_pck(int id)
  6362. {
  6363. return (id >= 8) && (id <= 15);
  6364. }
  6365. -static irqreturn_t clk_system_irq_handler(int irq, void *dev_id)
  6366. +
  6367. +static inline bool clk_system_ready(struct regmap *regmap, int id)
  6368. {
  6369. - struct clk_system *sys = (struct clk_system *)dev_id;
  6370. + unsigned int status;
  6371. - wake_up(&sys->wait);
  6372. - disable_irq_nosync(sys->irq);
  6373. + regmap_read(regmap, AT91_PMC_SR, &status);
  6374. - return IRQ_HANDLED;
  6375. + return status & (1 << id) ? 1 : 0;
  6376. }
  6377. static int clk_system_prepare(struct clk_hw *hw)
  6378. {
  6379. struct clk_system *sys = to_clk_system(hw);
  6380. - struct at91_pmc *pmc = sys->pmc;
  6381. - u32 mask = 1 << sys->id;
  6382. - pmc_write(pmc, AT91_PMC_SCER, mask);
  6383. + regmap_write(sys->regmap, AT91_PMC_SCER, 1 << sys->id);
  6384. if (!is_pck(sys->id))
  6385. return 0;
  6386. - while (!(pmc_read(pmc, AT91_PMC_SR) & mask)) {
  6387. - if (sys->irq) {
  6388. - enable_irq(sys->irq);
  6389. - wait_event(sys->wait,
  6390. - pmc_read(pmc, AT91_PMC_SR) & mask);
  6391. - } else
  6392. - cpu_relax();
  6393. - }
  6394. + while (!clk_system_ready(sys->regmap, sys->id))
  6395. + cpu_relax();
  6396. +
  6397. return 0;
  6398. }
  6399. static void clk_system_unprepare(struct clk_hw *hw)
  6400. {
  6401. struct clk_system *sys = to_clk_system(hw);
  6402. - struct at91_pmc *pmc = sys->pmc;
  6403. - pmc_write(pmc, AT91_PMC_SCDR, 1 << sys->id);
  6404. + regmap_write(sys->regmap, AT91_PMC_SCDR, 1 << sys->id);
  6405. }
  6406. static int clk_system_is_prepared(struct clk_hw *hw)
  6407. {
  6408. struct clk_system *sys = to_clk_system(hw);
  6409. - struct at91_pmc *pmc = sys->pmc;
  6410. + unsigned int status;
  6411. +
  6412. + regmap_read(sys->regmap, AT91_PMC_SCSR, &status);
  6413. - if (!(pmc_read(pmc, AT91_PMC_SCSR) & (1 << sys->id)))
  6414. + if (!(status & (1 << sys->id)))
  6415. return 0;
  6416. if (!is_pck(sys->id))
  6417. return 1;
  6418. - return !!(pmc_read(pmc, AT91_PMC_SR) & (1 << sys->id));
  6419. + regmap_read(sys->regmap, AT91_PMC_SR, &status);
  6420. +
  6421. + return status & (1 << sys->id) ? 1 : 0;
  6422. }
  6423. static const struct clk_ops system_ops = {
  6424. @@ -100,13 +89,12 @@ static const struct clk_ops system_ops = {
  6425. };
  6426. static struct clk * __init
  6427. -at91_clk_register_system(struct at91_pmc *pmc, const char *name,
  6428. - const char *parent_name, u8 id, int irq)
  6429. +at91_clk_register_system(struct regmap *regmap, const char *name,
  6430. + const char *parent_name, u8 id)
  6431. {
  6432. struct clk_system *sys;
  6433. struct clk *clk = NULL;
  6434. struct clk_init_data init;
  6435. - int ret;
  6436. if (!parent_name || id > SYSTEM_MAX_ID)
  6437. return ERR_PTR(-EINVAL);
  6438. @@ -123,44 +111,33 @@ at91_clk_register_system(struct at91_pmc *pmc, const char *name,
  6439. sys->id = id;
  6440. sys->hw.init = &init;
  6441. - sys->pmc = pmc;
  6442. - sys->irq = irq;
  6443. - if (irq) {
  6444. - init_waitqueue_head(&sys->wait);
  6445. - irq_set_status_flags(sys->irq, IRQ_NOAUTOEN);
  6446. - ret = request_irq(sys->irq, clk_system_irq_handler,
  6447. - IRQF_TRIGGER_HIGH, name, sys);
  6448. - if (ret) {
  6449. - kfree(sys);
  6450. - return ERR_PTR(ret);
  6451. - }
  6452. - }
  6453. + sys->regmap = regmap;
  6454. clk = clk_register(NULL, &sys->hw);
  6455. - if (IS_ERR(clk)) {
  6456. - if (irq)
  6457. - free_irq(sys->irq, sys);
  6458. + if (IS_ERR(clk))
  6459. kfree(sys);
  6460. - }
  6461. return clk;
  6462. }
  6463. -static void __init
  6464. -of_at91_clk_sys_setup(struct device_node *np, struct at91_pmc *pmc)
  6465. +static void __init of_at91rm9200_clk_sys_setup(struct device_node *np)
  6466. {
  6467. int num;
  6468. - int irq = 0;
  6469. u32 id;
  6470. struct clk *clk;
  6471. const char *name;
  6472. struct device_node *sysclknp;
  6473. const char *parent_name;
  6474. + struct regmap *regmap;
  6475. num = of_get_child_count(np);
  6476. if (num > (SYSTEM_MAX_ID + 1))
  6477. return;
  6478. + regmap = syscon_node_to_regmap(of_get_parent(np));
  6479. + if (IS_ERR(regmap))
  6480. + return;
  6481. +
  6482. for_each_child_of_node(np, sysclknp) {
  6483. if (of_property_read_u32(sysclknp, "reg", &id))
  6484. continue;
  6485. @@ -168,21 +145,14 @@ of_at91_clk_sys_setup(struct device_node *np, struct at91_pmc *pmc)
  6486. if (of_property_read_string(np, "clock-output-names", &name))
  6487. name = sysclknp->name;
  6488. - if (is_pck(id))
  6489. - irq = irq_of_parse_and_map(sysclknp, 0);
  6490. -
  6491. parent_name = of_clk_get_parent_name(sysclknp, 0);
  6492. - clk = at91_clk_register_system(pmc, name, parent_name, id, irq);
  6493. + clk = at91_clk_register_system(regmap, name, parent_name, id);
  6494. if (IS_ERR(clk))
  6495. continue;
  6496. of_clk_add_provider(sysclknp, of_clk_src_simple_get, clk);
  6497. }
  6498. }
  6499. -
  6500. -void __init of_at91rm9200_clk_sys_setup(struct device_node *np,
  6501. - struct at91_pmc *pmc)
  6502. -{
  6503. - of_at91_clk_sys_setup(np, pmc);
  6504. -}
  6505. +CLK_OF_DECLARE(at91rm9200_clk_sys, "atmel,at91rm9200-clk-system",
  6506. + of_at91rm9200_clk_sys_setup);
  6507. diff --git a/drivers/clk/at91/clk-usb.c b/drivers/clk/at91/clk-usb.c
  6508. index 8ab8502778a2..650ca45892c0 100644
  6509. --- a/drivers/clk/at91/clk-usb.c
  6510. +++ b/drivers/clk/at91/clk-usb.c
  6511. @@ -12,8 +12,8 @@
  6512. #include <linux/clkdev.h>
  6513. #include <linux/clk/at91_pmc.h>
  6514. #include <linux/of.h>
  6515. -#include <linux/of_address.h>
  6516. -#include <linux/io.h>
  6517. +#include <linux/mfd/syscon.h>
  6518. +#include <linux/regmap.h>
  6519. #include "pmc.h"
  6520. @@ -27,7 +27,7 @@
  6521. struct at91sam9x5_clk_usb {
  6522. struct clk_hw hw;
  6523. - struct at91_pmc *pmc;
  6524. + struct regmap *regmap;
  6525. };
  6526. #define to_at91sam9x5_clk_usb(hw) \
  6527. @@ -35,7 +35,7 @@ struct at91sam9x5_clk_usb {
  6528. struct at91rm9200_clk_usb {
  6529. struct clk_hw hw;
  6530. - struct at91_pmc *pmc;
  6531. + struct regmap *regmap;
  6532. u32 divisors[4];
  6533. };
  6534. @@ -45,13 +45,12 @@ struct at91rm9200_clk_usb {
  6535. static unsigned long at91sam9x5_clk_usb_recalc_rate(struct clk_hw *hw,
  6536. unsigned long parent_rate)
  6537. {
  6538. - u32 tmp;
  6539. - u8 usbdiv;
  6540. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  6541. - struct at91_pmc *pmc = usb->pmc;
  6542. + unsigned int usbr;
  6543. + u8 usbdiv;
  6544. - tmp = pmc_read(pmc, AT91_PMC_USB);
  6545. - usbdiv = (tmp & AT91_PMC_OHCIUSBDIV) >> SAM9X5_USB_DIV_SHIFT;
  6546. + regmap_read(usb->regmap, AT91_PMC_USB, &usbr);
  6547. + usbdiv = (usbr & AT91_PMC_OHCIUSBDIV) >> SAM9X5_USB_DIV_SHIFT;
  6548. return DIV_ROUND_CLOSEST(parent_rate, (usbdiv + 1));
  6549. }
  6550. @@ -109,33 +108,31 @@ static int at91sam9x5_clk_usb_determine_rate(struct clk_hw *hw,
  6551. static int at91sam9x5_clk_usb_set_parent(struct clk_hw *hw, u8 index)
  6552. {
  6553. - u32 tmp;
  6554. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  6555. - struct at91_pmc *pmc = usb->pmc;
  6556. if (index > 1)
  6557. return -EINVAL;
  6558. - tmp = pmc_read(pmc, AT91_PMC_USB) & ~AT91_PMC_USBS;
  6559. - if (index)
  6560. - tmp |= AT91_PMC_USBS;
  6561. - pmc_write(pmc, AT91_PMC_USB, tmp);
  6562. +
  6563. + regmap_update_bits(usb->regmap, AT91_PMC_USB, AT91_PMC_USBS,
  6564. + index ? AT91_PMC_USBS : 0);
  6565. +
  6566. return 0;
  6567. }
  6568. static u8 at91sam9x5_clk_usb_get_parent(struct clk_hw *hw)
  6569. {
  6570. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  6571. - struct at91_pmc *pmc = usb->pmc;
  6572. + unsigned int usbr;
  6573. - return pmc_read(pmc, AT91_PMC_USB) & AT91_PMC_USBS;
  6574. + regmap_read(usb->regmap, AT91_PMC_USB, &usbr);
  6575. +
  6576. + return usbr & AT91_PMC_USBS;
  6577. }
  6578. static int at91sam9x5_clk_usb_set_rate(struct clk_hw *hw, unsigned long rate,
  6579. unsigned long parent_rate)
  6580. {
  6581. - u32 tmp;
  6582. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  6583. - struct at91_pmc *pmc = usb->pmc;
  6584. unsigned long div;
  6585. if (!rate)
  6586. @@ -145,9 +142,8 @@ static int at91sam9x5_clk_usb_set_rate(struct clk_hw *hw, unsigned long rate,
  6587. if (div > SAM9X5_USB_MAX_DIV + 1 || !div)
  6588. return -EINVAL;
  6589. - tmp = pmc_read(pmc, AT91_PMC_USB) & ~AT91_PMC_OHCIUSBDIV;
  6590. - tmp |= (div - 1) << SAM9X5_USB_DIV_SHIFT;
  6591. - pmc_write(pmc, AT91_PMC_USB, tmp);
  6592. + regmap_update_bits(usb->regmap, AT91_PMC_USB, AT91_PMC_OHCIUSBDIV,
  6593. + (div - 1) << SAM9X5_USB_DIV_SHIFT);
  6594. return 0;
  6595. }
  6596. @@ -163,28 +159,28 @@ static const struct clk_ops at91sam9x5_usb_ops = {
  6597. static int at91sam9n12_clk_usb_enable(struct clk_hw *hw)
  6598. {
  6599. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  6600. - struct at91_pmc *pmc = usb->pmc;
  6601. - pmc_write(pmc, AT91_PMC_USB,
  6602. - pmc_read(pmc, AT91_PMC_USB) | AT91_PMC_USBS);
  6603. + regmap_update_bits(usb->regmap, AT91_PMC_USB, AT91_PMC_USBS,
  6604. + AT91_PMC_USBS);
  6605. +
  6606. return 0;
  6607. }
  6608. static void at91sam9n12_clk_usb_disable(struct clk_hw *hw)
  6609. {
  6610. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  6611. - struct at91_pmc *pmc = usb->pmc;
  6612. - pmc_write(pmc, AT91_PMC_USB,
  6613. - pmc_read(pmc, AT91_PMC_USB) & ~AT91_PMC_USBS);
  6614. + regmap_update_bits(usb->regmap, AT91_PMC_USB, AT91_PMC_USBS, 0);
  6615. }
  6616. static int at91sam9n12_clk_usb_is_enabled(struct clk_hw *hw)
  6617. {
  6618. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  6619. - struct at91_pmc *pmc = usb->pmc;
  6620. + unsigned int usbr;
  6621. - return !!(pmc_read(pmc, AT91_PMC_USB) & AT91_PMC_USBS);
  6622. + regmap_read(usb->regmap, AT91_PMC_USB, &usbr);
  6623. +
  6624. + return usbr & AT91_PMC_USBS;
  6625. }
  6626. static const struct clk_ops at91sam9n12_usb_ops = {
  6627. @@ -197,7 +193,7 @@ static const struct clk_ops at91sam9n12_usb_ops = {
  6628. };
  6629. static struct clk * __init
  6630. -at91sam9x5_clk_register_usb(struct at91_pmc *pmc, const char *name,
  6631. +at91sam9x5_clk_register_usb(struct regmap *regmap, const char *name,
  6632. const char **parent_names, u8 num_parents)
  6633. {
  6634. struct at91sam9x5_clk_usb *usb;
  6635. @@ -216,7 +212,7 @@ at91sam9x5_clk_register_usb(struct at91_pmc *pmc, const char *name,
  6636. CLK_SET_RATE_PARENT;
  6637. usb->hw.init = &init;
  6638. - usb->pmc = pmc;
  6639. + usb->regmap = regmap;
  6640. clk = clk_register(NULL, &usb->hw);
  6641. if (IS_ERR(clk))
  6642. @@ -226,7 +222,7 @@ at91sam9x5_clk_register_usb(struct at91_pmc *pmc, const char *name,
  6643. }
  6644. static struct clk * __init
  6645. -at91sam9n12_clk_register_usb(struct at91_pmc *pmc, const char *name,
  6646. +at91sam9n12_clk_register_usb(struct regmap *regmap, const char *name,
  6647. const char *parent_name)
  6648. {
  6649. struct at91sam9x5_clk_usb *usb;
  6650. @@ -244,7 +240,7 @@ at91sam9n12_clk_register_usb(struct at91_pmc *pmc, const char *name,
  6651. init.flags = CLK_SET_RATE_GATE | CLK_SET_RATE_PARENT;
  6652. usb->hw.init = &init;
  6653. - usb->pmc = pmc;
  6654. + usb->regmap = regmap;
  6655. clk = clk_register(NULL, &usb->hw);
  6656. if (IS_ERR(clk))
  6657. @@ -257,12 +253,12 @@ static unsigned long at91rm9200_clk_usb_recalc_rate(struct clk_hw *hw,
  6658. unsigned long parent_rate)
  6659. {
  6660. struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
  6661. - struct at91_pmc *pmc = usb->pmc;
  6662. - u32 tmp;
  6663. + unsigned int pllbr;
  6664. u8 usbdiv;
  6665. - tmp = pmc_read(pmc, AT91_CKGR_PLLBR);
  6666. - usbdiv = (tmp & AT91_PMC_USBDIV) >> RM9200_USB_DIV_SHIFT;
  6667. + regmap_read(usb->regmap, AT91_CKGR_PLLBR, &pllbr);
  6668. +
  6669. + usbdiv = (pllbr & AT91_PMC_USBDIV) >> RM9200_USB_DIV_SHIFT;
  6670. if (usb->divisors[usbdiv])
  6671. return parent_rate / usb->divisors[usbdiv];
  6672. @@ -310,10 +306,8 @@ static long at91rm9200_clk_usb_round_rate(struct clk_hw *hw, unsigned long rate,
  6673. static int at91rm9200_clk_usb_set_rate(struct clk_hw *hw, unsigned long rate,
  6674. unsigned long parent_rate)
  6675. {
  6676. - u32 tmp;
  6677. int i;
  6678. struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
  6679. - struct at91_pmc *pmc = usb->pmc;
  6680. unsigned long div;
  6681. if (!rate)
  6682. @@ -323,10 +317,10 @@ static int at91rm9200_clk_usb_set_rate(struct clk_hw *hw, unsigned long rate,
  6683. for (i = 0; i < RM9200_USB_DIV_TAB_SIZE; i++) {
  6684. if (usb->divisors[i] == div) {
  6685. - tmp = pmc_read(pmc, AT91_CKGR_PLLBR) &
  6686. - ~AT91_PMC_USBDIV;
  6687. - tmp |= i << RM9200_USB_DIV_SHIFT;
  6688. - pmc_write(pmc, AT91_CKGR_PLLBR, tmp);
  6689. + regmap_update_bits(usb->regmap, AT91_CKGR_PLLBR,
  6690. + AT91_PMC_USBDIV,
  6691. + i << RM9200_USB_DIV_SHIFT);
  6692. +
  6693. return 0;
  6694. }
  6695. }
  6696. @@ -341,7 +335,7 @@ static const struct clk_ops at91rm9200_usb_ops = {
  6697. };
  6698. static struct clk * __init
  6699. -at91rm9200_clk_register_usb(struct at91_pmc *pmc, const char *name,
  6700. +at91rm9200_clk_register_usb(struct regmap *regmap, const char *name,
  6701. const char *parent_name, const u32 *divisors)
  6702. {
  6703. struct at91rm9200_clk_usb *usb;
  6704. @@ -359,7 +353,7 @@ at91rm9200_clk_register_usb(struct at91_pmc *pmc, const char *name,
  6705. init.flags = CLK_SET_RATE_PARENT;
  6706. usb->hw.init = &init;
  6707. - usb->pmc = pmc;
  6708. + usb->regmap = regmap;
  6709. memcpy(usb->divisors, divisors, sizeof(usb->divisors));
  6710. clk = clk_register(NULL, &usb->hw);
  6711. @@ -369,13 +363,13 @@ at91rm9200_clk_register_usb(struct at91_pmc *pmc, const char *name,
  6712. return clk;
  6713. }
  6714. -void __init of_at91sam9x5_clk_usb_setup(struct device_node *np,
  6715. - struct at91_pmc *pmc)
  6716. +static void __init of_at91sam9x5_clk_usb_setup(struct device_node *np)
  6717. {
  6718. struct clk *clk;
  6719. int num_parents;
  6720. const char *parent_names[USB_SOURCE_MAX];
  6721. const char *name = np->name;
  6722. + struct regmap *regmap;
  6723. num_parents = of_clk_get_parent_count(np);
  6724. if (num_parents <= 0 || num_parents > USB_SOURCE_MAX)
  6725. @@ -385,19 +379,26 @@ void __init of_at91sam9x5_clk_usb_setup(struct device_node *np,
  6726. of_property_read_string(np, "clock-output-names", &name);
  6727. - clk = at91sam9x5_clk_register_usb(pmc, name, parent_names, num_parents);
  6728. + regmap = syscon_node_to_regmap(of_get_parent(np));
  6729. + if (IS_ERR(regmap))
  6730. + return;
  6731. +
  6732. + clk = at91sam9x5_clk_register_usb(regmap, name, parent_names,
  6733. + num_parents);
  6734. if (IS_ERR(clk))
  6735. return;
  6736. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  6737. }
  6738. +CLK_OF_DECLARE(at91sam9x5_clk_usb, "atmel,at91sam9x5-clk-usb",
  6739. + of_at91sam9x5_clk_usb_setup);
  6740. -void __init of_at91sam9n12_clk_usb_setup(struct device_node *np,
  6741. - struct at91_pmc *pmc)
  6742. +static void __init of_at91sam9n12_clk_usb_setup(struct device_node *np)
  6743. {
  6744. struct clk *clk;
  6745. const char *parent_name;
  6746. const char *name = np->name;
  6747. + struct regmap *regmap;
  6748. parent_name = of_clk_get_parent_name(np, 0);
  6749. if (!parent_name)
  6750. @@ -405,20 +406,26 @@ void __init of_at91sam9n12_clk_usb_setup(struct device_node *np,
  6751. of_property_read_string(np, "clock-output-names", &name);
  6752. - clk = at91sam9n12_clk_register_usb(pmc, name, parent_name);
  6753. + regmap = syscon_node_to_regmap(of_get_parent(np));
  6754. + if (IS_ERR(regmap))
  6755. + return;
  6756. +
  6757. + clk = at91sam9n12_clk_register_usb(regmap, name, parent_name);
  6758. if (IS_ERR(clk))
  6759. return;
  6760. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  6761. }
  6762. +CLK_OF_DECLARE(at91sam9n12_clk_usb, "atmel,at91sam9n12-clk-usb",
  6763. + of_at91sam9n12_clk_usb_setup);
  6764. -void __init of_at91rm9200_clk_usb_setup(struct device_node *np,
  6765. - struct at91_pmc *pmc)
  6766. +static void __init of_at91rm9200_clk_usb_setup(struct device_node *np)
  6767. {
  6768. struct clk *clk;
  6769. const char *parent_name;
  6770. const char *name = np->name;
  6771. u32 divisors[4] = {0, 0, 0, 0};
  6772. + struct regmap *regmap;
  6773. parent_name = of_clk_get_parent_name(np, 0);
  6774. if (!parent_name)
  6775. @@ -430,9 +437,15 @@ void __init of_at91rm9200_clk_usb_setup(struct device_node *np,
  6776. of_property_read_string(np, "clock-output-names", &name);
  6777. - clk = at91rm9200_clk_register_usb(pmc, name, parent_name, divisors);
  6778. + regmap = syscon_node_to_regmap(of_get_parent(np));
  6779. + if (IS_ERR(regmap))
  6780. + return;
  6781. +
  6782. + clk = at91rm9200_clk_register_usb(regmap, name, parent_name, divisors);
  6783. if (IS_ERR(clk))
  6784. return;
  6785. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  6786. }
  6787. +CLK_OF_DECLARE(at91rm9200_clk_usb, "atmel,at91rm9200-clk-usb",
  6788. + of_at91rm9200_clk_usb_setup);
  6789. diff --git a/drivers/clk/at91/clk-utmi.c b/drivers/clk/at91/clk-utmi.c
  6790. index ca561e90a60f..61fcf399e58c 100644
  6791. --- a/drivers/clk/at91/clk-utmi.c
  6792. +++ b/drivers/clk/at91/clk-utmi.c
  6793. @@ -11,14 +11,9 @@
  6794. #include <linux/clk-provider.h>
  6795. #include <linux/clkdev.h>
  6796. #include <linux/clk/at91_pmc.h>
  6797. -#include <linux/interrupt.h>
  6798. -#include <linux/irq.h>
  6799. #include <linux/of.h>
  6800. -#include <linux/of_address.h>
  6801. -#include <linux/of_irq.h>
  6802. -#include <linux/io.h>
  6803. -#include <linux/sched.h>
  6804. -#include <linux/wait.h>
  6805. +#include <linux/mfd/syscon.h>
  6806. +#include <linux/regmap.h>
  6807. #include "pmc.h"
  6808. @@ -26,37 +21,30 @@
  6809. struct clk_utmi {
  6810. struct clk_hw hw;
  6811. - struct at91_pmc *pmc;
  6812. - unsigned int irq;
  6813. - wait_queue_head_t wait;
  6814. + struct regmap *regmap;
  6815. };
  6816. #define to_clk_utmi(hw) container_of(hw, struct clk_utmi, hw)
  6817. -static irqreturn_t clk_utmi_irq_handler(int irq, void *dev_id)
  6818. +static inline bool clk_utmi_ready(struct regmap *regmap)
  6819. {
  6820. - struct clk_utmi *utmi = (struct clk_utmi *)dev_id;
  6821. + unsigned int status;
  6822. - wake_up(&utmi->wait);
  6823. - disable_irq_nosync(utmi->irq);
  6824. + regmap_read(regmap, AT91_PMC_SR, &status);
  6825. - return IRQ_HANDLED;
  6826. + return status & AT91_PMC_LOCKU;
  6827. }
  6828. static int clk_utmi_prepare(struct clk_hw *hw)
  6829. {
  6830. struct clk_utmi *utmi = to_clk_utmi(hw);
  6831. - struct at91_pmc *pmc = utmi->pmc;
  6832. - u32 tmp = pmc_read(pmc, AT91_CKGR_UCKR) | AT91_PMC_UPLLEN |
  6833. - AT91_PMC_UPLLCOUNT | AT91_PMC_BIASEN;
  6834. + unsigned int uckr = AT91_PMC_UPLLEN | AT91_PMC_UPLLCOUNT |
  6835. + AT91_PMC_BIASEN;
  6836. - pmc_write(pmc, AT91_CKGR_UCKR, tmp);
  6837. + regmap_update_bits(utmi->regmap, AT91_CKGR_UCKR, uckr, uckr);
  6838. - while (!(pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_LOCKU)) {
  6839. - enable_irq(utmi->irq);
  6840. - wait_event(utmi->wait,
  6841. - pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_LOCKU);
  6842. - }
  6843. + while (!clk_utmi_ready(utmi->regmap))
  6844. + cpu_relax();
  6845. return 0;
  6846. }
  6847. @@ -64,18 +52,15 @@ static int clk_utmi_prepare(struct clk_hw *hw)
  6848. static int clk_utmi_is_prepared(struct clk_hw *hw)
  6849. {
  6850. struct clk_utmi *utmi = to_clk_utmi(hw);
  6851. - struct at91_pmc *pmc = utmi->pmc;
  6852. - return !!(pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_LOCKU);
  6853. + return clk_utmi_ready(utmi->regmap);
  6854. }
  6855. static void clk_utmi_unprepare(struct clk_hw *hw)
  6856. {
  6857. struct clk_utmi *utmi = to_clk_utmi(hw);
  6858. - struct at91_pmc *pmc = utmi->pmc;
  6859. - u32 tmp = pmc_read(pmc, AT91_CKGR_UCKR) & ~AT91_PMC_UPLLEN;
  6860. - pmc_write(pmc, AT91_CKGR_UCKR, tmp);
  6861. + regmap_update_bits(utmi->regmap, AT91_CKGR_UCKR, AT91_PMC_UPLLEN, 0);
  6862. }
  6863. static unsigned long clk_utmi_recalc_rate(struct clk_hw *hw,
  6864. @@ -93,10 +78,9 @@ static const struct clk_ops utmi_ops = {
  6865. };
  6866. static struct clk * __init
  6867. -at91_clk_register_utmi(struct at91_pmc *pmc, unsigned int irq,
  6868. +at91_clk_register_utmi(struct regmap *regmap,
  6869. const char *name, const char *parent_name)
  6870. {
  6871. - int ret;
  6872. struct clk_utmi *utmi;
  6873. struct clk *clk = NULL;
  6874. struct clk_init_data init;
  6875. @@ -112,52 +96,36 @@ at91_clk_register_utmi(struct at91_pmc *pmc, unsigned int irq,
  6876. init.flags = CLK_SET_RATE_GATE;
  6877. utmi->hw.init = &init;
  6878. - utmi->pmc = pmc;
  6879. - utmi->irq = irq;
  6880. - init_waitqueue_head(&utmi->wait);
  6881. - irq_set_status_flags(utmi->irq, IRQ_NOAUTOEN);
  6882. - ret = request_irq(utmi->irq, clk_utmi_irq_handler,
  6883. - IRQF_TRIGGER_HIGH, "clk-utmi", utmi);
  6884. - if (ret) {
  6885. - kfree(utmi);
  6886. - return ERR_PTR(ret);
  6887. - }
  6888. + utmi->regmap = regmap;
  6889. clk = clk_register(NULL, &utmi->hw);
  6890. - if (IS_ERR(clk)) {
  6891. - free_irq(utmi->irq, utmi);
  6892. + if (IS_ERR(clk))
  6893. kfree(utmi);
  6894. - }
  6895. return clk;
  6896. }
  6897. -static void __init
  6898. -of_at91_clk_utmi_setup(struct device_node *np, struct at91_pmc *pmc)
  6899. +static void __init of_at91sam9x5_clk_utmi_setup(struct device_node *np)
  6900. {
  6901. - unsigned int irq;
  6902. struct clk *clk;
  6903. const char *parent_name;
  6904. const char *name = np->name;
  6905. + struct regmap *regmap;
  6906. parent_name = of_clk_get_parent_name(np, 0);
  6907. of_property_read_string(np, "clock-output-names", &name);
  6908. - irq = irq_of_parse_and_map(np, 0);
  6909. - if (!irq)
  6910. + regmap = syscon_node_to_regmap(of_get_parent(np));
  6911. + if (IS_ERR(regmap))
  6912. return;
  6913. - clk = at91_clk_register_utmi(pmc, irq, name, parent_name);
  6914. + clk = at91_clk_register_utmi(regmap, name, parent_name);
  6915. if (IS_ERR(clk))
  6916. return;
  6917. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  6918. return;
  6919. }
  6920. -
  6921. -void __init of_at91sam9x5_clk_utmi_setup(struct device_node *np,
  6922. - struct at91_pmc *pmc)
  6923. -{
  6924. - of_at91_clk_utmi_setup(np, pmc);
  6925. -}
  6926. +CLK_OF_DECLARE(at91sam9x5_clk_utmi, "atmel,at91sam9x5-clk-utmi",
  6927. + of_at91sam9x5_clk_utmi_setup);
  6928. diff --git a/drivers/clk/at91/pmc.c b/drivers/clk/at91/pmc.c
  6929. index 8476b570779b..526df5ba042d 100644
  6930. --- a/drivers/clk/at91/pmc.c
  6931. +++ b/drivers/clk/at91/pmc.c
  6932. @@ -12,36 +12,13 @@
  6933. #include <linux/clkdev.h>
  6934. #include <linux/clk/at91_pmc.h>
  6935. #include <linux/of.h>
  6936. -#include <linux/of_address.h>
  6937. -#include <linux/io.h>
  6938. -#include <linux/interrupt.h>
  6939. -#include <linux/irq.h>
  6940. -#include <linux/irqchip/chained_irq.h>
  6941. -#include <linux/irqdomain.h>
  6942. -#include <linux/of_irq.h>
  6943. +#include <linux/mfd/syscon.h>
  6944. +#include <linux/regmap.h>
  6945. #include <asm/proc-fns.h>
  6946. #include "pmc.h"
  6947. -void __iomem *at91_pmc_base;
  6948. -EXPORT_SYMBOL_GPL(at91_pmc_base);
  6949. -
  6950. -void at91rm9200_idle(void)
  6951. -{
  6952. - /*
  6953. - * Disable the processor clock. The processor will be automatically
  6954. - * re-enabled by an interrupt or by a reset.
  6955. - */
  6956. - at91_pmc_write(AT91_PMC_SCDR, AT91_PMC_PCK);
  6957. -}
  6958. -
  6959. -void at91sam9_idle(void)
  6960. -{
  6961. - at91_pmc_write(AT91_PMC_SCDR, AT91_PMC_PCK);
  6962. - cpu_do_idle();
  6963. -}
  6964. -
  6965. int of_at91_get_clk_range(struct device_node *np, const char *propname,
  6966. struct clk_range *range)
  6967. {
  6968. @@ -64,402 +41,3 @@ int of_at91_get_clk_range(struct device_node *np, const char *propname,
  6969. return 0;
  6970. }
  6971. EXPORT_SYMBOL_GPL(of_at91_get_clk_range);
  6972. -
  6973. -static void pmc_irq_mask(struct irq_data *d)
  6974. -{
  6975. - struct at91_pmc *pmc = irq_data_get_irq_chip_data(d);
  6976. -
  6977. - pmc_write(pmc, AT91_PMC_IDR, 1 << d->hwirq);
  6978. -}
  6979. -
  6980. -static void pmc_irq_unmask(struct irq_data *d)
  6981. -{
  6982. - struct at91_pmc *pmc = irq_data_get_irq_chip_data(d);
  6983. -
  6984. - pmc_write(pmc, AT91_PMC_IER, 1 << d->hwirq);
  6985. -}
  6986. -
  6987. -static int pmc_irq_set_type(struct irq_data *d, unsigned type)
  6988. -{
  6989. - if (type != IRQ_TYPE_LEVEL_HIGH) {
  6990. - pr_warn("PMC: type not supported (support only IRQ_TYPE_LEVEL_HIGH type)\n");
  6991. - return -EINVAL;
  6992. - }
  6993. -
  6994. - return 0;
  6995. -}
  6996. -
  6997. -static void pmc_irq_suspend(struct irq_data *d)
  6998. -{
  6999. - struct at91_pmc *pmc = irq_data_get_irq_chip_data(d);
  7000. -
  7001. - pmc->imr = pmc_read(pmc, AT91_PMC_IMR);
  7002. - pmc_write(pmc, AT91_PMC_IDR, pmc->imr);
  7003. -}
  7004. -
  7005. -static void pmc_irq_resume(struct irq_data *d)
  7006. -{
  7007. - struct at91_pmc *pmc = irq_data_get_irq_chip_data(d);
  7008. -
  7009. - pmc_write(pmc, AT91_PMC_IER, pmc->imr);
  7010. -}
  7011. -
  7012. -static struct irq_chip pmc_irq = {
  7013. - .name = "PMC",
  7014. - .irq_disable = pmc_irq_mask,
  7015. - .irq_mask = pmc_irq_mask,
  7016. - .irq_unmask = pmc_irq_unmask,
  7017. - .irq_set_type = pmc_irq_set_type,
  7018. - .irq_suspend = pmc_irq_suspend,
  7019. - .irq_resume = pmc_irq_resume,
  7020. -};
  7021. -
  7022. -static struct lock_class_key pmc_lock_class;
  7023. -
  7024. -static int pmc_irq_map(struct irq_domain *h, unsigned int virq,
  7025. - irq_hw_number_t hw)
  7026. -{
  7027. - struct at91_pmc *pmc = h->host_data;
  7028. -
  7029. - irq_set_lockdep_class(virq, &pmc_lock_class);
  7030. -
  7031. - irq_set_chip_and_handler(virq, &pmc_irq,
  7032. - handle_level_irq);
  7033. - irq_set_chip_data(virq, pmc);
  7034. -
  7035. - return 0;
  7036. -}
  7037. -
  7038. -static int pmc_irq_domain_xlate(struct irq_domain *d,
  7039. - struct device_node *ctrlr,
  7040. - const u32 *intspec, unsigned int intsize,
  7041. - irq_hw_number_t *out_hwirq,
  7042. - unsigned int *out_type)
  7043. -{
  7044. - struct at91_pmc *pmc = d->host_data;
  7045. - const struct at91_pmc_caps *caps = pmc->caps;
  7046. -
  7047. - if (WARN_ON(intsize < 1))
  7048. - return -EINVAL;
  7049. -
  7050. - *out_hwirq = intspec[0];
  7051. -
  7052. - if (!(caps->available_irqs & (1 << *out_hwirq)))
  7053. - return -EINVAL;
  7054. -
  7055. - *out_type = IRQ_TYPE_LEVEL_HIGH;
  7056. -
  7057. - return 0;
  7058. -}
  7059. -
  7060. -static const struct irq_domain_ops pmc_irq_ops = {
  7061. - .map = pmc_irq_map,
  7062. - .xlate = pmc_irq_domain_xlate,
  7063. -};
  7064. -
  7065. -static irqreturn_t pmc_irq_handler(int irq, void *data)
  7066. -{
  7067. - struct at91_pmc *pmc = (struct at91_pmc *)data;
  7068. - unsigned long sr;
  7069. - int n;
  7070. -
  7071. - sr = pmc_read(pmc, AT91_PMC_SR) & pmc_read(pmc, AT91_PMC_IMR);
  7072. - if (!sr)
  7073. - return IRQ_NONE;
  7074. -
  7075. - for_each_set_bit(n, &sr, BITS_PER_LONG)
  7076. - generic_handle_irq(irq_find_mapping(pmc->irqdomain, n));
  7077. -
  7078. - return IRQ_HANDLED;
  7079. -}
  7080. -
  7081. -static const struct at91_pmc_caps at91rm9200_caps = {
  7082. - .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_LOCKB |
  7083. - AT91_PMC_MCKRDY | AT91_PMC_PCK0RDY |
  7084. - AT91_PMC_PCK1RDY | AT91_PMC_PCK2RDY |
  7085. - AT91_PMC_PCK3RDY,
  7086. -};
  7087. -
  7088. -static const struct at91_pmc_caps at91sam9260_caps = {
  7089. - .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_LOCKB |
  7090. - AT91_PMC_MCKRDY | AT91_PMC_PCK0RDY |
  7091. - AT91_PMC_PCK1RDY,
  7092. -};
  7093. -
  7094. -static const struct at91_pmc_caps at91sam9g45_caps = {
  7095. - .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_MCKRDY |
  7096. - AT91_PMC_LOCKU | AT91_PMC_PCK0RDY |
  7097. - AT91_PMC_PCK1RDY,
  7098. -};
  7099. -
  7100. -static const struct at91_pmc_caps at91sam9n12_caps = {
  7101. - .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_LOCKB |
  7102. - AT91_PMC_MCKRDY | AT91_PMC_PCK0RDY |
  7103. - AT91_PMC_PCK1RDY | AT91_PMC_MOSCSELS |
  7104. - AT91_PMC_MOSCRCS | AT91_PMC_CFDEV,
  7105. -};
  7106. -
  7107. -static const struct at91_pmc_caps at91sam9x5_caps = {
  7108. - .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_MCKRDY |
  7109. - AT91_PMC_LOCKU | AT91_PMC_PCK0RDY |
  7110. - AT91_PMC_PCK1RDY | AT91_PMC_MOSCSELS |
  7111. - AT91_PMC_MOSCRCS | AT91_PMC_CFDEV,
  7112. -};
  7113. -
  7114. -static const struct at91_pmc_caps sama5d2_caps = {
  7115. - .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_MCKRDY |
  7116. - AT91_PMC_LOCKU | AT91_PMC_PCK0RDY |
  7117. - AT91_PMC_PCK1RDY | AT91_PMC_PCK2RDY |
  7118. - AT91_PMC_MOSCSELS | AT91_PMC_MOSCRCS |
  7119. - AT91_PMC_CFDEV | AT91_PMC_GCKRDY,
  7120. -};
  7121. -
  7122. -static const struct at91_pmc_caps sama5d3_caps = {
  7123. - .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_MCKRDY |
  7124. - AT91_PMC_LOCKU | AT91_PMC_PCK0RDY |
  7125. - AT91_PMC_PCK1RDY | AT91_PMC_PCK2RDY |
  7126. - AT91_PMC_MOSCSELS | AT91_PMC_MOSCRCS |
  7127. - AT91_PMC_CFDEV,
  7128. -};
  7129. -
  7130. -static struct at91_pmc *__init at91_pmc_init(struct device_node *np,
  7131. - void __iomem *regbase, int virq,
  7132. - const struct at91_pmc_caps *caps)
  7133. -{
  7134. - struct at91_pmc *pmc;
  7135. -
  7136. - if (!regbase || !virq || !caps)
  7137. - return NULL;
  7138. -
  7139. - at91_pmc_base = regbase;
  7140. -
  7141. - pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
  7142. - if (!pmc)
  7143. - return NULL;
  7144. -
  7145. - spin_lock_init(&pmc->lock);
  7146. - pmc->regbase = regbase;
  7147. - pmc->virq = virq;
  7148. - pmc->caps = caps;
  7149. -
  7150. - pmc->irqdomain = irq_domain_add_linear(np, 32, &pmc_irq_ops, pmc);
  7151. -
  7152. - if (!pmc->irqdomain)
  7153. - goto out_free_pmc;
  7154. -
  7155. - pmc_write(pmc, AT91_PMC_IDR, 0xffffffff);
  7156. - if (request_irq(pmc->virq, pmc_irq_handler,
  7157. - IRQF_SHARED | IRQF_COND_SUSPEND, "pmc", pmc))
  7158. - goto out_remove_irqdomain;
  7159. -
  7160. - return pmc;
  7161. -
  7162. -out_remove_irqdomain:
  7163. - irq_domain_remove(pmc->irqdomain);
  7164. -out_free_pmc:
  7165. - kfree(pmc);
  7166. -
  7167. - return NULL;
  7168. -}
  7169. -
  7170. -static const struct of_device_id pmc_clk_ids[] __initconst = {
  7171. - /* Slow oscillator */
  7172. - {
  7173. - .compatible = "atmel,at91sam9260-clk-slow",
  7174. - .data = of_at91sam9260_clk_slow_setup,
  7175. - },
  7176. - /* Main clock */
  7177. - {
  7178. - .compatible = "atmel,at91rm9200-clk-main-osc",
  7179. - .data = of_at91rm9200_clk_main_osc_setup,
  7180. - },
  7181. - {
  7182. - .compatible = "atmel,at91sam9x5-clk-main-rc-osc",
  7183. - .data = of_at91sam9x5_clk_main_rc_osc_setup,
  7184. - },
  7185. - {
  7186. - .compatible = "atmel,at91rm9200-clk-main",
  7187. - .data = of_at91rm9200_clk_main_setup,
  7188. - },
  7189. - {
  7190. - .compatible = "atmel,at91sam9x5-clk-main",
  7191. - .data = of_at91sam9x5_clk_main_setup,
  7192. - },
  7193. - /* PLL clocks */
  7194. - {
  7195. - .compatible = "atmel,at91rm9200-clk-pll",
  7196. - .data = of_at91rm9200_clk_pll_setup,
  7197. - },
  7198. - {
  7199. - .compatible = "atmel,at91sam9g45-clk-pll",
  7200. - .data = of_at91sam9g45_clk_pll_setup,
  7201. - },
  7202. - {
  7203. - .compatible = "atmel,at91sam9g20-clk-pllb",
  7204. - .data = of_at91sam9g20_clk_pllb_setup,
  7205. - },
  7206. - {
  7207. - .compatible = "atmel,sama5d3-clk-pll",
  7208. - .data = of_sama5d3_clk_pll_setup,
  7209. - },
  7210. - {
  7211. - .compatible = "atmel,at91sam9x5-clk-plldiv",
  7212. - .data = of_at91sam9x5_clk_plldiv_setup,
  7213. - },
  7214. - /* Master clock */
  7215. - {
  7216. - .compatible = "atmel,at91rm9200-clk-master",
  7217. - .data = of_at91rm9200_clk_master_setup,
  7218. - },
  7219. - {
  7220. - .compatible = "atmel,at91sam9x5-clk-master",
  7221. - .data = of_at91sam9x5_clk_master_setup,
  7222. - },
  7223. - /* System clocks */
  7224. - {
  7225. - .compatible = "atmel,at91rm9200-clk-system",
  7226. - .data = of_at91rm9200_clk_sys_setup,
  7227. - },
  7228. - /* Peripheral clocks */
  7229. - {
  7230. - .compatible = "atmel,at91rm9200-clk-peripheral",
  7231. - .data = of_at91rm9200_clk_periph_setup,
  7232. - },
  7233. - {
  7234. - .compatible = "atmel,at91sam9x5-clk-peripheral",
  7235. - .data = of_at91sam9x5_clk_periph_setup,
  7236. - },
  7237. - /* Programmable clocks */
  7238. - {
  7239. - .compatible = "atmel,at91rm9200-clk-programmable",
  7240. - .data = of_at91rm9200_clk_prog_setup,
  7241. - },
  7242. - {
  7243. - .compatible = "atmel,at91sam9g45-clk-programmable",
  7244. - .data = of_at91sam9g45_clk_prog_setup,
  7245. - },
  7246. - {
  7247. - .compatible = "atmel,at91sam9x5-clk-programmable",
  7248. - .data = of_at91sam9x5_clk_prog_setup,
  7249. - },
  7250. - /* UTMI clock */
  7251. -#if defined(CONFIG_HAVE_AT91_UTMI)
  7252. - {
  7253. - .compatible = "atmel,at91sam9x5-clk-utmi",
  7254. - .data = of_at91sam9x5_clk_utmi_setup,
  7255. - },
  7256. -#endif
  7257. - /* USB clock */
  7258. -#if defined(CONFIG_HAVE_AT91_USB_CLK)
  7259. - {
  7260. - .compatible = "atmel,at91rm9200-clk-usb",
  7261. - .data = of_at91rm9200_clk_usb_setup,
  7262. - },
  7263. - {
  7264. - .compatible = "atmel,at91sam9x5-clk-usb",
  7265. - .data = of_at91sam9x5_clk_usb_setup,
  7266. - },
  7267. - {
  7268. - .compatible = "atmel,at91sam9n12-clk-usb",
  7269. - .data = of_at91sam9n12_clk_usb_setup,
  7270. - },
  7271. -#endif
  7272. - /* SMD clock */
  7273. -#if defined(CONFIG_HAVE_AT91_SMD)
  7274. - {
  7275. - .compatible = "atmel,at91sam9x5-clk-smd",
  7276. - .data = of_at91sam9x5_clk_smd_setup,
  7277. - },
  7278. -#endif
  7279. -#if defined(CONFIG_HAVE_AT91_H32MX)
  7280. - {
  7281. - .compatible = "atmel,sama5d4-clk-h32mx",
  7282. - .data = of_sama5d4_clk_h32mx_setup,
  7283. - },
  7284. -#endif
  7285. -#if defined(CONFIG_HAVE_AT91_GENERATED_CLK)
  7286. - {
  7287. - .compatible = "atmel,sama5d2-clk-generated",
  7288. - .data = of_sama5d2_clk_generated_setup,
  7289. - },
  7290. -#endif
  7291. - { /*sentinel*/ }
  7292. -};
  7293. -
  7294. -static void __init of_at91_pmc_setup(struct device_node *np,
  7295. - const struct at91_pmc_caps *caps)
  7296. -{
  7297. - struct at91_pmc *pmc;
  7298. - struct device_node *childnp;
  7299. - void (*clk_setup)(struct device_node *, struct at91_pmc *);
  7300. - const struct of_device_id *clk_id;
  7301. - void __iomem *regbase = of_iomap(np, 0);
  7302. - int virq;
  7303. -
  7304. - if (!regbase)
  7305. - return;
  7306. -
  7307. - virq = irq_of_parse_and_map(np, 0);
  7308. - if (!virq)
  7309. - return;
  7310. -
  7311. - pmc = at91_pmc_init(np, regbase, virq, caps);
  7312. - if (!pmc)
  7313. - return;
  7314. - for_each_child_of_node(np, childnp) {
  7315. - clk_id = of_match_node(pmc_clk_ids, childnp);
  7316. - if (!clk_id)
  7317. - continue;
  7318. - clk_setup = clk_id->data;
  7319. - clk_setup(childnp, pmc);
  7320. - }
  7321. -}
  7322. -
  7323. -static void __init of_at91rm9200_pmc_setup(struct device_node *np)
  7324. -{
  7325. - of_at91_pmc_setup(np, &at91rm9200_caps);
  7326. -}
  7327. -CLK_OF_DECLARE(at91rm9200_clk_pmc, "atmel,at91rm9200-pmc",
  7328. - of_at91rm9200_pmc_setup);
  7329. -
  7330. -static void __init of_at91sam9260_pmc_setup(struct device_node *np)
  7331. -{
  7332. - of_at91_pmc_setup(np, &at91sam9260_caps);
  7333. -}
  7334. -CLK_OF_DECLARE(at91sam9260_clk_pmc, "atmel,at91sam9260-pmc",
  7335. - of_at91sam9260_pmc_setup);
  7336. -
  7337. -static void __init of_at91sam9g45_pmc_setup(struct device_node *np)
  7338. -{
  7339. - of_at91_pmc_setup(np, &at91sam9g45_caps);
  7340. -}
  7341. -CLK_OF_DECLARE(at91sam9g45_clk_pmc, "atmel,at91sam9g45-pmc",
  7342. - of_at91sam9g45_pmc_setup);
  7343. -
  7344. -static void __init of_at91sam9n12_pmc_setup(struct device_node *np)
  7345. -{
  7346. - of_at91_pmc_setup(np, &at91sam9n12_caps);
  7347. -}
  7348. -CLK_OF_DECLARE(at91sam9n12_clk_pmc, "atmel,at91sam9n12-pmc",
  7349. - of_at91sam9n12_pmc_setup);
  7350. -
  7351. -static void __init of_at91sam9x5_pmc_setup(struct device_node *np)
  7352. -{
  7353. - of_at91_pmc_setup(np, &at91sam9x5_caps);
  7354. -}
  7355. -CLK_OF_DECLARE(at91sam9x5_clk_pmc, "atmel,at91sam9x5-pmc",
  7356. - of_at91sam9x5_pmc_setup);
  7357. -
  7358. -static void __init of_sama5d2_pmc_setup(struct device_node *np)
  7359. -{
  7360. - of_at91_pmc_setup(np, &sama5d2_caps);
  7361. -}
  7362. -CLK_OF_DECLARE(sama5d2_clk_pmc, "atmel,sama5d2-pmc",
  7363. - of_sama5d2_pmc_setup);
  7364. -
  7365. -static void __init of_sama5d3_pmc_setup(struct device_node *np)
  7366. -{
  7367. - of_at91_pmc_setup(np, &sama5d3_caps);
  7368. -}
  7369. -CLK_OF_DECLARE(sama5d3_clk_pmc, "atmel,sama5d3-pmc",
  7370. - of_sama5d3_pmc_setup);
  7371. diff --git a/drivers/clk/at91/pmc.h b/drivers/clk/at91/pmc.h
  7372. index f65739272779..5771fff0ee3f 100644
  7373. --- a/drivers/clk/at91/pmc.h
  7374. +++ b/drivers/clk/at91/pmc.h
  7375. @@ -14,8 +14,11 @@
  7376. #include <linux/io.h>
  7377. #include <linux/irqdomain.h>
  7378. +#include <linux/regmap.h>
  7379. #include <linux/spinlock.h>
  7380. +extern spinlock_t pmc_pcr_lock;
  7381. +
  7382. struct clk_range {
  7383. unsigned long min;
  7384. unsigned long max;
  7385. @@ -23,102 +26,7 @@ struct clk_range {
  7386. #define CLK_RANGE(MIN, MAX) {.min = MIN, .max = MAX,}
  7387. -struct at91_pmc_caps {
  7388. - u32 available_irqs;
  7389. -};
  7390. -
  7391. -struct at91_pmc {
  7392. - void __iomem *regbase;
  7393. - int virq;
  7394. - spinlock_t lock;
  7395. - const struct at91_pmc_caps *caps;
  7396. - struct irq_domain *irqdomain;
  7397. - u32 imr;
  7398. -};
  7399. -
  7400. -static inline void pmc_lock(struct at91_pmc *pmc)
  7401. -{
  7402. - spin_lock(&pmc->lock);
  7403. -}
  7404. -
  7405. -static inline void pmc_unlock(struct at91_pmc *pmc)
  7406. -{
  7407. - spin_unlock(&pmc->lock);
  7408. -}
  7409. -
  7410. -static inline u32 pmc_read(struct at91_pmc *pmc, int offset)
  7411. -{
  7412. - return readl(pmc->regbase + offset);
  7413. -}
  7414. -
  7415. -static inline void pmc_write(struct at91_pmc *pmc, int offset, u32 value)
  7416. -{
  7417. - writel(value, pmc->regbase + offset);
  7418. -}
  7419. -
  7420. int of_at91_get_clk_range(struct device_node *np, const char *propname,
  7421. struct clk_range *range);
  7422. -void of_at91sam9260_clk_slow_setup(struct device_node *np,
  7423. - struct at91_pmc *pmc);
  7424. -
  7425. -void of_at91rm9200_clk_main_osc_setup(struct device_node *np,
  7426. - struct at91_pmc *pmc);
  7427. -void of_at91sam9x5_clk_main_rc_osc_setup(struct device_node *np,
  7428. - struct at91_pmc *pmc);
  7429. -void of_at91rm9200_clk_main_setup(struct device_node *np,
  7430. - struct at91_pmc *pmc);
  7431. -void of_at91sam9x5_clk_main_setup(struct device_node *np,
  7432. - struct at91_pmc *pmc);
  7433. -
  7434. -void of_at91rm9200_clk_pll_setup(struct device_node *np,
  7435. - struct at91_pmc *pmc);
  7436. -void of_at91sam9g45_clk_pll_setup(struct device_node *np,
  7437. - struct at91_pmc *pmc);
  7438. -void of_at91sam9g20_clk_pllb_setup(struct device_node *np,
  7439. - struct at91_pmc *pmc);
  7440. -void of_sama5d3_clk_pll_setup(struct device_node *np,
  7441. - struct at91_pmc *pmc);
  7442. -void of_at91sam9x5_clk_plldiv_setup(struct device_node *np,
  7443. - struct at91_pmc *pmc);
  7444. -
  7445. -void of_at91rm9200_clk_master_setup(struct device_node *np,
  7446. - struct at91_pmc *pmc);
  7447. -void of_at91sam9x5_clk_master_setup(struct device_node *np,
  7448. - struct at91_pmc *pmc);
  7449. -
  7450. -void of_at91rm9200_clk_sys_setup(struct device_node *np,
  7451. - struct at91_pmc *pmc);
  7452. -
  7453. -void of_at91rm9200_clk_periph_setup(struct device_node *np,
  7454. - struct at91_pmc *pmc);
  7455. -void of_at91sam9x5_clk_periph_setup(struct device_node *np,
  7456. - struct at91_pmc *pmc);
  7457. -
  7458. -void of_at91rm9200_clk_prog_setup(struct device_node *np,
  7459. - struct at91_pmc *pmc);
  7460. -void of_at91sam9g45_clk_prog_setup(struct device_node *np,
  7461. - struct at91_pmc *pmc);
  7462. -void of_at91sam9x5_clk_prog_setup(struct device_node *np,
  7463. - struct at91_pmc *pmc);
  7464. -
  7465. -void of_at91sam9x5_clk_utmi_setup(struct device_node *np,
  7466. - struct at91_pmc *pmc);
  7467. -
  7468. -void of_at91rm9200_clk_usb_setup(struct device_node *np,
  7469. - struct at91_pmc *pmc);
  7470. -void of_at91sam9x5_clk_usb_setup(struct device_node *np,
  7471. - struct at91_pmc *pmc);
  7472. -void of_at91sam9n12_clk_usb_setup(struct device_node *np,
  7473. - struct at91_pmc *pmc);
  7474. -
  7475. -void of_at91sam9x5_clk_smd_setup(struct device_node *np,
  7476. - struct at91_pmc *pmc);
  7477. -
  7478. -void of_sama5d4_clk_h32mx_setup(struct device_node *np,
  7479. - struct at91_pmc *pmc);
  7480. -
  7481. -void of_sama5d2_clk_generated_setup(struct device_node *np,
  7482. - struct at91_pmc *pmc);
  7483. -
  7484. #endif /* __PMC_H_ */
  7485. diff --git a/drivers/clocksource/tcb_clksrc.c b/drivers/clocksource/tcb_clksrc.c
  7486. index 4da2af9694a2..5b6f57f500b8 100644
  7487. --- a/drivers/clocksource/tcb_clksrc.c
  7488. +++ b/drivers/clocksource/tcb_clksrc.c
  7489. @@ -23,8 +23,7 @@
  7490. * this 32 bit free-running counter. the second channel is not used.
  7491. *
  7492. * - The third channel may be used to provide a 16-bit clockevent
  7493. - * source, used in either periodic or oneshot mode. This runs
  7494. - * at 32 KiHZ, and can handle delays of up to two seconds.
  7495. + * source, used in either periodic or oneshot mode.
  7496. *
  7497. * A boot clocksource and clockevent source are also currently needed,
  7498. * unless the relevant platforms (ARM/AT91, AVR32/AT32) are changed so
  7499. @@ -74,6 +73,8 @@ static struct clocksource clksrc = {
  7500. struct tc_clkevt_device {
  7501. struct clock_event_device clkevt;
  7502. struct clk *clk;
  7503. + bool clk_enabled;
  7504. + u32 freq;
  7505. void __iomem *regs;
  7506. };
  7507. @@ -82,15 +83,26 @@ static struct tc_clkevt_device *to_tc_clkevt(struct clock_event_device *clkevt)
  7508. return container_of(clkevt, struct tc_clkevt_device, clkevt);
  7509. }
  7510. -/* For now, we always use the 32K clock ... this optimizes for NO_HZ,
  7511. - * because using one of the divided clocks would usually mean the
  7512. - * tick rate can never be less than several dozen Hz (vs 0.5 Hz).
  7513. - *
  7514. - * A divided clock could be good for high resolution timers, since
  7515. - * 30.5 usec resolution can seem "low".
  7516. - */
  7517. static u32 timer_clock;
  7518. +static void tc_clk_disable(struct clock_event_device *d)
  7519. +{
  7520. + struct tc_clkevt_device *tcd = to_tc_clkevt(d);
  7521. +
  7522. + clk_disable(tcd->clk);
  7523. + tcd->clk_enabled = false;
  7524. +}
  7525. +
  7526. +static void tc_clk_enable(struct clock_event_device *d)
  7527. +{
  7528. + struct tc_clkevt_device *tcd = to_tc_clkevt(d);
  7529. +
  7530. + if (tcd->clk_enabled)
  7531. + return;
  7532. + clk_enable(tcd->clk);
  7533. + tcd->clk_enabled = true;
  7534. +}
  7535. +
  7536. static int tc_shutdown(struct clock_event_device *d)
  7537. {
  7538. struct tc_clkevt_device *tcd = to_tc_clkevt(d);
  7539. @@ -98,8 +110,14 @@ static int tc_shutdown(struct clock_event_device *d)
  7540. __raw_writel(0xff, regs + ATMEL_TC_REG(2, IDR));
  7541. __raw_writel(ATMEL_TC_CLKDIS, regs + ATMEL_TC_REG(2, CCR));
  7542. + return 0;
  7543. +}
  7544. +
  7545. +static int tc_shutdown_clk_off(struct clock_event_device *d)
  7546. +{
  7547. + tc_shutdown(d);
  7548. if (!clockevent_state_detached(d))
  7549. - clk_disable(tcd->clk);
  7550. + tc_clk_disable(d);
  7551. return 0;
  7552. }
  7553. @@ -112,9 +130,9 @@ static int tc_set_oneshot(struct clock_event_device *d)
  7554. if (clockevent_state_oneshot(d) || clockevent_state_periodic(d))
  7555. tc_shutdown(d);
  7556. - clk_enable(tcd->clk);
  7557. + tc_clk_enable(d);
  7558. - /* slow clock, count up to RC, then irq and stop */
  7559. + /* count up to RC, then irq and stop */
  7560. __raw_writel(timer_clock | ATMEL_TC_CPCSTOP | ATMEL_TC_WAVE |
  7561. ATMEL_TC_WAVESEL_UP_AUTO, regs + ATMEL_TC_REG(2, CMR));
  7562. __raw_writel(ATMEL_TC_CPCS, regs + ATMEL_TC_REG(2, IER));
  7563. @@ -134,12 +152,12 @@ static int tc_set_periodic(struct clock_event_device *d)
  7564. /* By not making the gentime core emulate periodic mode on top
  7565. * of oneshot, we get lower overhead and improved accuracy.
  7566. */
  7567. - clk_enable(tcd->clk);
  7568. + tc_clk_enable(d);
  7569. - /* slow clock, count up to RC, then irq and restart */
  7570. + /* count up to RC, then irq and restart */
  7571. __raw_writel(timer_clock | ATMEL_TC_WAVE | ATMEL_TC_WAVESEL_UP_AUTO,
  7572. regs + ATMEL_TC_REG(2, CMR));
  7573. - __raw_writel((32768 + HZ / 2) / HZ, tcaddr + ATMEL_TC_REG(2, RC));
  7574. + __raw_writel((tcd->freq + HZ / 2) / HZ, tcaddr + ATMEL_TC_REG(2, RC));
  7575. /* Enable clock and interrupts on RC compare */
  7576. __raw_writel(ATMEL_TC_CPCS, regs + ATMEL_TC_REG(2, IER));
  7577. @@ -166,9 +184,13 @@ static struct tc_clkevt_device clkevt = {
  7578. .features = CLOCK_EVT_FEAT_PERIODIC |
  7579. CLOCK_EVT_FEAT_ONESHOT,
  7580. /* Should be lower than at91rm9200's system timer */
  7581. +#ifdef CONFIG_ATMEL_TCB_CLKSRC_USE_SLOW_CLOCK
  7582. .rating = 125,
  7583. +#else
  7584. + .rating = 200,
  7585. +#endif
  7586. .set_next_event = tc_next_event,
  7587. - .set_state_shutdown = tc_shutdown,
  7588. + .set_state_shutdown = tc_shutdown_clk_off,
  7589. .set_state_periodic = tc_set_periodic,
  7590. .set_state_oneshot = tc_set_oneshot,
  7591. },
  7592. @@ -188,8 +210,9 @@ static irqreturn_t ch2_irq(int irq, void *handle)
  7593. return IRQ_NONE;
  7594. }
  7595. -static int __init setup_clkevents(struct atmel_tc *tc, int clk32k_divisor_idx)
  7596. +static int __init setup_clkevents(struct atmel_tc *tc, int divisor_idx)
  7597. {
  7598. + unsigned divisor = atmel_tc_divisors[divisor_idx];
  7599. int ret;
  7600. struct clk *t2_clk = tc->clk[2];
  7601. int irq = tc->irq[2];
  7602. @@ -210,7 +233,11 @@ static int __init setup_clkevents(struct atmel_tc *tc, int clk32k_divisor_idx)
  7603. clkevt.regs = tc->regs;
  7604. clkevt.clk = t2_clk;
  7605. - timer_clock = clk32k_divisor_idx;
  7606. + timer_clock = divisor_idx;
  7607. + if (!divisor)
  7608. + clkevt.freq = 32768;
  7609. + else
  7610. + clkevt.freq = clk_get_rate(t2_clk) / divisor;
  7611. clkevt.clkevt.cpumask = cpumask_of(0);
  7612. @@ -221,7 +248,7 @@ static int __init setup_clkevents(struct atmel_tc *tc, int clk32k_divisor_idx)
  7613. return ret;
  7614. }
  7615. - clockevents_config_and_register(&clkevt.clkevt, 32768, 1, 0xffff);
  7616. + clockevents_config_and_register(&clkevt.clkevt, clkevt.freq, 1, 0xffff);
  7617. return ret;
  7618. }
  7619. @@ -358,7 +385,11 @@ static int __init tcb_clksrc_init(void)
  7620. goto err_disable_t1;
  7621. /* channel 2: periodic and oneshot timer support */
  7622. +#ifdef CONFIG_ATMEL_TCB_CLKSRC_USE_SLOW_CLOCK
  7623. ret = setup_clkevents(tc, clk32k_divisor_idx);
  7624. +#else
  7625. + ret = setup_clkevents(tc, best_divisor_idx);
  7626. +#endif
  7627. if (ret)
  7628. goto err_unregister_clksrc;
  7629. diff --git a/drivers/clocksource/timer-atmel-pit.c b/drivers/clocksource/timer-atmel-pit.c
  7630. index d911c5dca8f1..7a40f7e88468 100644
  7631. --- a/drivers/clocksource/timer-atmel-pit.c
  7632. +++ b/drivers/clocksource/timer-atmel-pit.c
  7633. @@ -46,6 +46,7 @@ struct pit_data {
  7634. u32 cycle;
  7635. u32 cnt;
  7636. unsigned int irq;
  7637. + bool irq_requested;
  7638. struct clk *mck;
  7639. };
  7640. @@ -96,15 +97,29 @@ static int pit_clkevt_shutdown(struct clock_event_device *dev)
  7641. /* disable irq, leaving the clocksource active */
  7642. pit_write(data->base, AT91_PIT_MR, (data->cycle - 1) | AT91_PIT_PITEN);
  7643. + if (data->irq_requested) {
  7644. + free_irq(data->irq, data);
  7645. + data->irq_requested = false;
  7646. + }
  7647. return 0;
  7648. }
  7649. +static irqreturn_t at91sam926x_pit_interrupt(int irq, void *dev_id);
  7650. /*
  7651. * Clockevent device: interrupts every 1/HZ (== pit_cycles * MCK/16)
  7652. */
  7653. static int pit_clkevt_set_periodic(struct clock_event_device *dev)
  7654. {
  7655. struct pit_data *data = clkevt_to_pit_data(dev);
  7656. + int ret;
  7657. +
  7658. + ret = request_irq(data->irq, at91sam926x_pit_interrupt,
  7659. + IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
  7660. + "at91_tick", data);
  7661. + if (ret)
  7662. + panic(pr_fmt("Unable to setup IRQ\n"));
  7663. +
  7664. + data->irq_requested = true;
  7665. /* update clocksource counter */
  7666. data->cnt += data->cycle * PIT_PICNT(pit_read(data->base, AT91_PIT_PIVR));
  7667. @@ -181,7 +196,6 @@ static void __init at91sam926x_pit_common_init(struct pit_data *data)
  7668. {
  7669. unsigned long pit_rate;
  7670. unsigned bits;
  7671. - int ret;
  7672. /*
  7673. * Use our actual MCK to figure out how many MCK/16 ticks per
  7674. @@ -206,13 +220,6 @@ static void __init at91sam926x_pit_common_init(struct pit_data *data)
  7675. data->clksrc.flags = CLOCK_SOURCE_IS_CONTINUOUS;
  7676. clocksource_register_hz(&data->clksrc, pit_rate);
  7677. - /* Set up irq handler */
  7678. - ret = request_irq(data->irq, at91sam926x_pit_interrupt,
  7679. - IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
  7680. - "at91_tick", data);
  7681. - if (ret)
  7682. - panic(pr_fmt("Unable to setup IRQ\n"));
  7683. -
  7684. /* Set up and register clockevents */
  7685. data->clkevt.name = "pit";
  7686. data->clkevt.features = CLOCK_EVT_FEAT_PERIODIC;
  7687. diff --git a/drivers/clocksource/timer-atmel-st.c b/drivers/clocksource/timer-atmel-st.c
  7688. index 29d21d68df5a..103d0fd70cc4 100644
  7689. --- a/drivers/clocksource/timer-atmel-st.c
  7690. +++ b/drivers/clocksource/timer-atmel-st.c
  7691. @@ -115,18 +115,29 @@ static void clkdev32k_disable_and_flush_irq(void)
  7692. last_crtr = read_CRTR();
  7693. }
  7694. +static int atmel_st_irq;
  7695. +
  7696. static int clkevt32k_shutdown(struct clock_event_device *evt)
  7697. {
  7698. clkdev32k_disable_and_flush_irq();
  7699. irqmask = 0;
  7700. regmap_write(regmap_st, AT91_ST_IER, irqmask);
  7701. + free_irq(atmel_st_irq, regmap_st);
  7702. return 0;
  7703. }
  7704. static int clkevt32k_set_oneshot(struct clock_event_device *dev)
  7705. {
  7706. + int ret;
  7707. +
  7708. clkdev32k_disable_and_flush_irq();
  7709. + ret = request_irq(atmel_st_irq, at91rm9200_timer_interrupt,
  7710. + IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
  7711. + "at91_tick", regmap_st);
  7712. + if (ret)
  7713. + panic(pr_fmt("Unable to setup IRQ\n"));
  7714. +
  7715. /*
  7716. * ALM for oneshot irqs, set by next_event()
  7717. * before 32 seconds have passed.
  7718. @@ -139,8 +150,16 @@ static int clkevt32k_set_oneshot(struct clock_event_device *dev)
  7719. static int clkevt32k_set_periodic(struct clock_event_device *dev)
  7720. {
  7721. + int ret;
  7722. +
  7723. clkdev32k_disable_and_flush_irq();
  7724. + ret = request_irq(atmel_st_irq, at91rm9200_timer_interrupt,
  7725. + IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
  7726. + "at91_tick", regmap_st);
  7727. + if (ret)
  7728. + panic(pr_fmt("Unable to setup IRQ\n"));
  7729. +
  7730. /* PIT for periodic irqs; fixed rate of 1/HZ */
  7731. irqmask = AT91_ST_PITS;
  7732. regmap_write(regmap_st, AT91_ST_PIMR, timer_latch);
  7733. @@ -198,7 +217,7 @@ static void __init atmel_st_timer_init(struct device_node *node)
  7734. {
  7735. struct clk *sclk;
  7736. unsigned int sclk_rate, val;
  7737. - int irq, ret;
  7738. + int ret;
  7739. regmap_st = syscon_node_to_regmap(node);
  7740. if (IS_ERR(regmap_st))
  7741. @@ -210,17 +229,10 @@ static void __init atmel_st_timer_init(struct device_node *node)
  7742. regmap_read(regmap_st, AT91_ST_SR, &val);
  7743. /* Get the interrupts property */
  7744. - irq = irq_of_parse_and_map(node, 0);
  7745. - if (!irq)
  7746. + atmel_st_irq = irq_of_parse_and_map(node, 0);
  7747. + if (!atmel_st_irq)
  7748. panic(pr_fmt("Unable to get IRQ from DT\n"));
  7749. - /* Make IRQs happen for the system timer */
  7750. - ret = request_irq(irq, at91rm9200_timer_interrupt,
  7751. - IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
  7752. - "at91_tick", regmap_st);
  7753. - if (ret)
  7754. - panic(pr_fmt("Unable to setup IRQ\n"));
  7755. -
  7756. sclk = of_clk_get(node, 0);
  7757. if (IS_ERR(sclk))
  7758. panic(pr_fmt("Unable to get slow clock\n"));
  7759. diff --git a/drivers/cpufreq/Kconfig.x86 b/drivers/cpufreq/Kconfig.x86
  7760. index c59bdcb83217..8f23161d80be 100644
  7761. --- a/drivers/cpufreq/Kconfig.x86
  7762. +++ b/drivers/cpufreq/Kconfig.x86
  7763. @@ -123,7 +123,7 @@ config X86_POWERNOW_K7_ACPI
  7764. config X86_POWERNOW_K8
  7765. tristate "AMD Opteron/Athlon64 PowerNow!"
  7766. - depends on ACPI && ACPI_PROCESSOR && X86_ACPI_CPUFREQ
  7767. + depends on ACPI && ACPI_PROCESSOR && X86_ACPI_CPUFREQ && !PREEMPT_RT_BASE
  7768. help
  7769. This adds the CPUFreq driver for K8/early Opteron/Athlon64 processors.
  7770. Support for K10 and newer processors is now in acpi-cpufreq.
  7771. diff --git a/drivers/cpuidle/coupled.c b/drivers/cpuidle/coupled.c
  7772. index 344058f8501a..d5657d50ac40 100644
  7773. --- a/drivers/cpuidle/coupled.c
  7774. +++ b/drivers/cpuidle/coupled.c
  7775. @@ -119,7 +119,6 @@ struct cpuidle_coupled {
  7776. #define CPUIDLE_COUPLED_NOT_IDLE (-1)
  7777. -static DEFINE_MUTEX(cpuidle_coupled_lock);
  7778. static DEFINE_PER_CPU(struct call_single_data, cpuidle_coupled_poke_cb);
  7779. /*
  7780. diff --git a/drivers/gpu/drm/i915/i915_gem_execbuffer.c b/drivers/gpu/drm/i915/i915_gem_execbuffer.c
  7781. index 6ed7d63a0688..9da7482ad256 100644
  7782. --- a/drivers/gpu/drm/i915/i915_gem_execbuffer.c
  7783. +++ b/drivers/gpu/drm/i915/i915_gem_execbuffer.c
  7784. @@ -1264,7 +1264,9 @@ i915_gem_ringbuffer_submission(struct i915_execbuffer_params *params,
  7785. if (ret)
  7786. return ret;
  7787. +#ifndef CONFIG_PREEMPT_RT_BASE
  7788. trace_i915_gem_ring_dispatch(params->request, params->dispatch_flags);
  7789. +#endif
  7790. i915_gem_execbuffer_move_to_active(vmas, params->request);
  7791. i915_gem_execbuffer_retire_commands(params);
  7792. diff --git a/drivers/gpu/drm/i915/i915_gem_shrinker.c b/drivers/gpu/drm/i915/i915_gem_shrinker.c
  7793. index c0a96f1ee18e..deb1e207fa3c 100644
  7794. --- a/drivers/gpu/drm/i915/i915_gem_shrinker.c
  7795. +++ b/drivers/gpu/drm/i915/i915_gem_shrinker.c
  7796. @@ -39,7 +39,7 @@ static bool mutex_is_locked_by(struct mutex *mutex, struct task_struct *task)
  7797. if (!mutex_is_locked(mutex))
  7798. return false;
  7799. -#if defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_MUTEX_SPIN_ON_OWNER)
  7800. +#if (defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_MUTEX_SPIN_ON_OWNER)) && !defined(CONFIG_PREEMPT_RT_BASE)
  7801. return mutex->owner == task;
  7802. #else
  7803. /* Since UP may be pre-empted, we cannot assume that we own the lock */
  7804. diff --git a/drivers/gpu/drm/i915/i915_irq.c b/drivers/gpu/drm/i915/i915_irq.c
  7805. index b7b0a38acd67..148dcb1349d5 100644
  7806. --- a/drivers/gpu/drm/i915/i915_irq.c
  7807. +++ b/drivers/gpu/drm/i915/i915_irq.c
  7808. @@ -812,6 +812,7 @@ static int i915_get_crtc_scanoutpos(struct drm_device *dev, unsigned int pipe,
  7809. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  7810. /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
  7811. + preempt_disable_rt();
  7812. /* Get optional system timestamp before query. */
  7813. if (stime)
  7814. @@ -863,6 +864,7 @@ static int i915_get_crtc_scanoutpos(struct drm_device *dev, unsigned int pipe,
  7815. *etime = ktime_get();
  7816. /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
  7817. + preempt_enable_rt();
  7818. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  7819. diff --git a/drivers/gpu/drm/i915/intel_display.c b/drivers/gpu/drm/i915/intel_display.c
  7820. index 4f5d07bb3511..8ecd5c016dba 100644
  7821. --- a/drivers/gpu/drm/i915/intel_display.c
  7822. +++ b/drivers/gpu/drm/i915/intel_display.c
  7823. @@ -11400,7 +11400,7 @@ void intel_check_page_flip(struct drm_device *dev, int pipe)
  7824. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7825. struct intel_unpin_work *work;
  7826. - WARN_ON(!in_interrupt());
  7827. + WARN_ON_NONRT(!in_interrupt());
  7828. if (crtc == NULL)
  7829. return;
  7830. diff --git a/drivers/gpu/drm/i915/intel_sprite.c b/drivers/gpu/drm/i915/intel_sprite.c
  7831. index 2cc6aa072f4c..b79d33f14868 100644
  7832. --- a/drivers/gpu/drm/i915/intel_sprite.c
  7833. +++ b/drivers/gpu/drm/i915/intel_sprite.c
  7834. @@ -38,6 +38,7 @@
  7835. #include "intel_drv.h"
  7836. #include <drm/i915_drm.h>
  7837. #include "i915_drv.h"
  7838. +#include <linux/locallock.h>
  7839. static bool
  7840. format_is_yuv(uint32_t format)
  7841. @@ -64,6 +65,8 @@ static int usecs_to_scanlines(const struct drm_display_mode *adjusted_mode,
  7842. 1000 * adjusted_mode->crtc_htotal);
  7843. }
  7844. +static DEFINE_LOCAL_IRQ_LOCK(pipe_update_lock);
  7845. +
  7846. /**
  7847. * intel_pipe_update_start() - start update of a set of display registers
  7848. * @crtc: the crtc of which the registers are going to be updated
  7849. @@ -96,7 +99,7 @@ void intel_pipe_update_start(struct intel_crtc *crtc)
  7850. min = vblank_start - usecs_to_scanlines(adjusted_mode, 100);
  7851. max = vblank_start - 1;
  7852. - local_irq_disable();
  7853. + local_lock_irq(pipe_update_lock);
  7854. if (min <= 0 || max <= 0)
  7855. return;
  7856. @@ -126,11 +129,11 @@ void intel_pipe_update_start(struct intel_crtc *crtc)
  7857. break;
  7858. }
  7859. - local_irq_enable();
  7860. + local_unlock_irq(pipe_update_lock);
  7861. timeout = schedule_timeout(timeout);
  7862. - local_irq_disable();
  7863. + local_lock_irq(pipe_update_lock);
  7864. }
  7865. finish_wait(wq, &wait);
  7866. @@ -164,7 +167,7 @@ void intel_pipe_update_end(struct intel_crtc *crtc)
  7867. trace_i915_pipe_update_end(crtc, end_vbl_count, scanline_end);
  7868. - local_irq_enable();
  7869. + local_unlock_irq(pipe_update_lock);
  7870. if (crtc->debug.start_vbl_count &&
  7871. crtc->debug.start_vbl_count != end_vbl_count) {
  7872. diff --git a/drivers/gpu/drm/radeon/radeon_display.c b/drivers/gpu/drm/radeon/radeon_display.c
  7873. index 3645b223aa37..642854b2ed2c 100644
  7874. --- a/drivers/gpu/drm/radeon/radeon_display.c
  7875. +++ b/drivers/gpu/drm/radeon/radeon_display.c
  7876. @@ -1862,6 +1862,7 @@ int radeon_get_crtc_scanoutpos(struct drm_device *dev, unsigned int pipe,
  7877. struct radeon_device *rdev = dev->dev_private;
  7878. /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
  7879. + preempt_disable_rt();
  7880. /* Get optional system timestamp before query. */
  7881. if (stime)
  7882. @@ -1954,6 +1955,7 @@ int radeon_get_crtc_scanoutpos(struct drm_device *dev, unsigned int pipe,
  7883. *etime = ktime_get();
  7884. /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
  7885. + preempt_enable_rt();
  7886. /* Decode into vertical and horizontal scanout position. */
  7887. *vpos = position & 0x1fff;
  7888. diff --git a/drivers/hv/vmbus_drv.c b/drivers/hv/vmbus_drv.c
  7889. index 802dcb409030..d6d9427860d8 100644
  7890. --- a/drivers/hv/vmbus_drv.c
  7891. +++ b/drivers/hv/vmbus_drv.c
  7892. @@ -820,7 +820,7 @@ static void vmbus_isr(void)
  7893. tasklet_schedule(&msg_dpc);
  7894. }
  7895. - add_interrupt_randomness(HYPERVISOR_CALLBACK_VECTOR, 0);
  7896. + add_interrupt_randomness(HYPERVISOR_CALLBACK_VECTOR, 0, 0);
  7897. }
  7898. diff --git a/drivers/i2c/busses/i2c-omap.c b/drivers/i2c/busses/i2c-omap.c
  7899. index 08d26ba61ed3..46b89dd42b10 100644
  7900. --- a/drivers/i2c/busses/i2c-omap.c
  7901. +++ b/drivers/i2c/busses/i2c-omap.c
  7902. @@ -995,15 +995,12 @@ omap_i2c_isr(int irq, void *dev_id)
  7903. u16 mask;
  7904. u16 stat;
  7905. - spin_lock(&omap->lock);
  7906. - mask = omap_i2c_read_reg(omap, OMAP_I2C_IE_REG);
  7907. stat = omap_i2c_read_reg(omap, OMAP_I2C_STAT_REG);
  7908. + mask = omap_i2c_read_reg(omap, OMAP_I2C_IE_REG);
  7909. if (stat & mask)
  7910. ret = IRQ_WAKE_THREAD;
  7911. - spin_unlock(&omap->lock);
  7912. -
  7913. return ret;
  7914. }
  7915. diff --git a/drivers/ide/alim15x3.c b/drivers/ide/alim15x3.c
  7916. index 36f76e28a0bf..394f142f90c7 100644
  7917. --- a/drivers/ide/alim15x3.c
  7918. +++ b/drivers/ide/alim15x3.c
  7919. @@ -234,7 +234,7 @@ static int init_chipset_ali15x3(struct pci_dev *dev)
  7920. isa_dev = pci_get_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533, NULL);
  7921. - local_irq_save(flags);
  7922. + local_irq_save_nort(flags);
  7923. if (m5229_revision < 0xC2) {
  7924. /*
  7925. @@ -325,7 +325,7 @@ out:
  7926. }
  7927. pci_dev_put(north);
  7928. pci_dev_put(isa_dev);
  7929. - local_irq_restore(flags);
  7930. + local_irq_restore_nort(flags);
  7931. return 0;
  7932. }
  7933. diff --git a/drivers/ide/hpt366.c b/drivers/ide/hpt366.c
  7934. index 696b6c1ec940..0d0a96629b73 100644
  7935. --- a/drivers/ide/hpt366.c
  7936. +++ b/drivers/ide/hpt366.c
  7937. @@ -1241,7 +1241,7 @@ static int init_dma_hpt366(ide_hwif_t *hwif,
  7938. dma_old = inb(base + 2);
  7939. - local_irq_save(flags);
  7940. + local_irq_save_nort(flags);
  7941. dma_new = dma_old;
  7942. pci_read_config_byte(dev, hwif->channel ? 0x4b : 0x43, &masterdma);
  7943. @@ -1252,7 +1252,7 @@ static int init_dma_hpt366(ide_hwif_t *hwif,
  7944. if (dma_new != dma_old)
  7945. outb(dma_new, base + 2);
  7946. - local_irq_restore(flags);
  7947. + local_irq_restore_nort(flags);
  7948. printk(KERN_INFO " %s: BM-DMA at 0x%04lx-0x%04lx\n",
  7949. hwif->name, base, base + 7);
  7950. diff --git a/drivers/ide/ide-io-std.c b/drivers/ide/ide-io-std.c
  7951. index 19763977568c..4169433faab5 100644
  7952. --- a/drivers/ide/ide-io-std.c
  7953. +++ b/drivers/ide/ide-io-std.c
  7954. @@ -175,7 +175,7 @@ void ide_input_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf,
  7955. unsigned long uninitialized_var(flags);
  7956. if ((io_32bit & 2) && !mmio) {
  7957. - local_irq_save(flags);
  7958. + local_irq_save_nort(flags);
  7959. ata_vlb_sync(io_ports->nsect_addr);
  7960. }
  7961. @@ -186,7 +186,7 @@ void ide_input_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf,
  7962. insl(data_addr, buf, words);
  7963. if ((io_32bit & 2) && !mmio)
  7964. - local_irq_restore(flags);
  7965. + local_irq_restore_nort(flags);
  7966. if (((len + 1) & 3) < 2)
  7967. return;
  7968. @@ -219,7 +219,7 @@ void ide_output_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf,
  7969. unsigned long uninitialized_var(flags);
  7970. if ((io_32bit & 2) && !mmio) {
  7971. - local_irq_save(flags);
  7972. + local_irq_save_nort(flags);
  7973. ata_vlb_sync(io_ports->nsect_addr);
  7974. }
  7975. @@ -230,7 +230,7 @@ void ide_output_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf,
  7976. outsl(data_addr, buf, words);
  7977. if ((io_32bit & 2) && !mmio)
  7978. - local_irq_restore(flags);
  7979. + local_irq_restore_nort(flags);
  7980. if (((len + 1) & 3) < 2)
  7981. return;
  7982. diff --git a/drivers/ide/ide-io.c b/drivers/ide/ide-io.c
  7983. index 669ea1e45795..e12e43e62245 100644
  7984. --- a/drivers/ide/ide-io.c
  7985. +++ b/drivers/ide/ide-io.c
  7986. @@ -659,7 +659,7 @@ void ide_timer_expiry (unsigned long data)
  7987. /* disable_irq_nosync ?? */
  7988. disable_irq(hwif->irq);
  7989. /* local CPU only, as if we were handling an interrupt */
  7990. - local_irq_disable();
  7991. + local_irq_disable_nort();
  7992. if (hwif->polling) {
  7993. startstop = handler(drive);
  7994. } else if (drive_is_ready(drive)) {
  7995. diff --git a/drivers/ide/ide-iops.c b/drivers/ide/ide-iops.c
  7996. index 376f2dc410c5..f014dd1b73dc 100644
  7997. --- a/drivers/ide/ide-iops.c
  7998. +++ b/drivers/ide/ide-iops.c
  7999. @@ -129,12 +129,12 @@ int __ide_wait_stat(ide_drive_t *drive, u8 good, u8 bad,
  8000. if ((stat & ATA_BUSY) == 0)
  8001. break;
  8002. - local_irq_restore(flags);
  8003. + local_irq_restore_nort(flags);
  8004. *rstat = stat;
  8005. return -EBUSY;
  8006. }
  8007. }
  8008. - local_irq_restore(flags);
  8009. + local_irq_restore_nort(flags);
  8010. }
  8011. /*
  8012. * Allow status to settle, then read it again.
  8013. diff --git a/drivers/ide/ide-probe.c b/drivers/ide/ide-probe.c
  8014. index 0b63facd1d87..4ceba37afc0c 100644
  8015. --- a/drivers/ide/ide-probe.c
  8016. +++ b/drivers/ide/ide-probe.c
  8017. @@ -196,10 +196,10 @@ static void do_identify(ide_drive_t *drive, u8 cmd, u16 *id)
  8018. int bswap = 1;
  8019. /* local CPU only; some systems need this */
  8020. - local_irq_save(flags);
  8021. + local_irq_save_nort(flags);
  8022. /* read 512 bytes of id info */
  8023. hwif->tp_ops->input_data(drive, NULL, id, SECTOR_SIZE);
  8024. - local_irq_restore(flags);
  8025. + local_irq_restore_nort(flags);
  8026. drive->dev_flags |= IDE_DFLAG_ID_READ;
  8027. #ifdef DEBUG
  8028. diff --git a/drivers/ide/ide-taskfile.c b/drivers/ide/ide-taskfile.c
  8029. index a716693417a3..be0568c722d6 100644
  8030. --- a/drivers/ide/ide-taskfile.c
  8031. +++ b/drivers/ide/ide-taskfile.c
  8032. @@ -250,7 +250,7 @@ void ide_pio_bytes(ide_drive_t *drive, struct ide_cmd *cmd,
  8033. page_is_high = PageHighMem(page);
  8034. if (page_is_high)
  8035. - local_irq_save(flags);
  8036. + local_irq_save_nort(flags);
  8037. buf = kmap_atomic(page) + offset;
  8038. @@ -271,7 +271,7 @@ void ide_pio_bytes(ide_drive_t *drive, struct ide_cmd *cmd,
  8039. kunmap_atomic(buf);
  8040. if (page_is_high)
  8041. - local_irq_restore(flags);
  8042. + local_irq_restore_nort(flags);
  8043. len -= nr_bytes;
  8044. }
  8045. @@ -414,7 +414,7 @@ static ide_startstop_t pre_task_out_intr(ide_drive_t *drive,
  8046. }
  8047. if ((drive->dev_flags & IDE_DFLAG_UNMASK) == 0)
  8048. - local_irq_disable();
  8049. + local_irq_disable_nort();
  8050. ide_set_handler(drive, &task_pio_intr, WAIT_WORSTCASE);
  8051. diff --git a/drivers/infiniband/ulp/ipoib/ipoib_multicast.c b/drivers/infiniband/ulp/ipoib/ipoib_multicast.c
  8052. index 5580ab0b5781..a123d0439c4c 100644
  8053. --- a/drivers/infiniband/ulp/ipoib/ipoib_multicast.c
  8054. +++ b/drivers/infiniband/ulp/ipoib/ipoib_multicast.c
  8055. @@ -862,7 +862,7 @@ void ipoib_mcast_restart_task(struct work_struct *work)
  8056. ipoib_dbg_mcast(priv, "restarting multicast task\n");
  8057. - local_irq_save(flags);
  8058. + local_irq_save_nort(flags);
  8059. netif_addr_lock(dev);
  8060. spin_lock(&priv->lock);
  8061. @@ -944,7 +944,7 @@ void ipoib_mcast_restart_task(struct work_struct *work)
  8062. spin_unlock(&priv->lock);
  8063. netif_addr_unlock(dev);
  8064. - local_irq_restore(flags);
  8065. + local_irq_restore_nort(flags);
  8066. /*
  8067. * make sure the in-flight joins have finished before we attempt
  8068. diff --git a/drivers/input/gameport/gameport.c b/drivers/input/gameport/gameport.c
  8069. index 4a2a9e370be7..e970d9afd179 100644
  8070. --- a/drivers/input/gameport/gameport.c
  8071. +++ b/drivers/input/gameport/gameport.c
  8072. @@ -91,13 +91,13 @@ static int gameport_measure_speed(struct gameport *gameport)
  8073. tx = ~0;
  8074. for (i = 0; i < 50; i++) {
  8075. - local_irq_save(flags);
  8076. + local_irq_save_nort(flags);
  8077. t1 = ktime_get_ns();
  8078. for (t = 0; t < 50; t++)
  8079. gameport_read(gameport);
  8080. t2 = ktime_get_ns();
  8081. t3 = ktime_get_ns();
  8082. - local_irq_restore(flags);
  8083. + local_irq_restore_nort(flags);
  8084. udelay(i * 10);
  8085. t = (t2 - t1) - (t3 - t2);
  8086. if (t < tx)
  8087. @@ -124,12 +124,12 @@ static int old_gameport_measure_speed(struct gameport *gameport)
  8088. tx = 1 << 30;
  8089. for(i = 0; i < 50; i++) {
  8090. - local_irq_save(flags);
  8091. + local_irq_save_nort(flags);
  8092. GET_TIME(t1);
  8093. for (t = 0; t < 50; t++) gameport_read(gameport);
  8094. GET_TIME(t2);
  8095. GET_TIME(t3);
  8096. - local_irq_restore(flags);
  8097. + local_irq_restore_nort(flags);
  8098. udelay(i * 10);
  8099. if ((t = DELTA(t2,t1) - DELTA(t3,t2)) < tx) tx = t;
  8100. }
  8101. @@ -148,11 +148,11 @@ static int old_gameport_measure_speed(struct gameport *gameport)
  8102. tx = 1 << 30;
  8103. for(i = 0; i < 50; i++) {
  8104. - local_irq_save(flags);
  8105. + local_irq_save_nort(flags);
  8106. t1 = rdtsc();
  8107. for (t = 0; t < 50; t++) gameport_read(gameport);
  8108. t2 = rdtsc();
  8109. - local_irq_restore(flags);
  8110. + local_irq_restore_nort(flags);
  8111. udelay(i * 10);
  8112. if (t2 - t1 < tx) tx = t2 - t1;
  8113. }
  8114. diff --git a/drivers/iommu/amd_iommu.c b/drivers/iommu/amd_iommu.c
  8115. index a0ef57483ebb..b08732bae11a 100644
  8116. --- a/drivers/iommu/amd_iommu.c
  8117. +++ b/drivers/iommu/amd_iommu.c
  8118. @@ -2022,10 +2022,10 @@ static int __attach_device(struct iommu_dev_data *dev_data,
  8119. int ret;
  8120. /*
  8121. - * Must be called with IRQs disabled. Warn here to detect early
  8122. - * when its not.
  8123. + * Must be called with IRQs disabled on a non RT kernel. Warn here to
  8124. + * detect early when its not.
  8125. */
  8126. - WARN_ON(!irqs_disabled());
  8127. + WARN_ON_NONRT(!irqs_disabled());
  8128. /* lock domain */
  8129. spin_lock(&domain->lock);
  8130. @@ -2188,10 +2188,10 @@ static void __detach_device(struct iommu_dev_data *dev_data)
  8131. struct protection_domain *domain;
  8132. /*
  8133. - * Must be called with IRQs disabled. Warn here to detect early
  8134. - * when its not.
  8135. + * Must be called with IRQs disabled on a non RT kernel. Warn here to
  8136. + * detect early when its not.
  8137. */
  8138. - WARN_ON(!irqs_disabled());
  8139. + WARN_ON_NONRT(!irqs_disabled());
  8140. if (WARN_ON(!dev_data->domain))
  8141. return;
  8142. diff --git a/drivers/leds/trigger/Kconfig b/drivers/leds/trigger/Kconfig
  8143. index 5bda6a9b56bb..d6286584c807 100644
  8144. --- a/drivers/leds/trigger/Kconfig
  8145. +++ b/drivers/leds/trigger/Kconfig
  8146. @@ -61,7 +61,7 @@ config LEDS_TRIGGER_BACKLIGHT
  8147. config LEDS_TRIGGER_CPU
  8148. bool "LED CPU Trigger"
  8149. - depends on LEDS_TRIGGERS
  8150. + depends on LEDS_TRIGGERS && !PREEMPT_RT_BASE
  8151. help
  8152. This allows LEDs to be controlled by active CPUs. This shows
  8153. the active CPUs across an array of LEDs so you can see which
  8154. diff --git a/drivers/md/bcache/Kconfig b/drivers/md/bcache/Kconfig
  8155. index 4d200883c505..98b64ed5cb81 100644
  8156. --- a/drivers/md/bcache/Kconfig
  8157. +++ b/drivers/md/bcache/Kconfig
  8158. @@ -1,6 +1,7 @@
  8159. config BCACHE
  8160. tristate "Block device as cache"
  8161. + depends on !PREEMPT_RT_FULL
  8162. ---help---
  8163. Allows a block device to be used as cache for other devices; uses
  8164. a btree for indexing and the layout is optimized for SSDs.
  8165. diff --git a/drivers/md/dm.c b/drivers/md/dm.c
  8166. index 320eb3c4bb6b..aba2d71149e3 100644
  8167. --- a/drivers/md/dm.c
  8168. +++ b/drivers/md/dm.c
  8169. @@ -2185,7 +2185,7 @@ static void dm_request_fn(struct request_queue *q)
  8170. /* Establish tio->ti before queuing work (map_tio_request) */
  8171. tio->ti = ti;
  8172. queue_kthread_work(&md->kworker, &tio->work);
  8173. - BUG_ON(!irqs_disabled());
  8174. + BUG_ON_NONRT(!irqs_disabled());
  8175. }
  8176. goto out;
  8177. diff --git a/drivers/md/raid5.c b/drivers/md/raid5.c
  8178. index 4384b46cee1a..6415f094a4eb 100644
  8179. --- a/drivers/md/raid5.c
  8180. +++ b/drivers/md/raid5.c
  8181. @@ -1920,8 +1920,9 @@ static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
  8182. struct raid5_percpu *percpu;
  8183. unsigned long cpu;
  8184. - cpu = get_cpu();
  8185. + cpu = get_cpu_light();
  8186. percpu = per_cpu_ptr(conf->percpu, cpu);
  8187. + spin_lock(&percpu->lock);
  8188. if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
  8189. ops_run_biofill(sh);
  8190. overlap_clear++;
  8191. @@ -1977,7 +1978,8 @@ static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
  8192. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  8193. wake_up(&sh->raid_conf->wait_for_overlap);
  8194. }
  8195. - put_cpu();
  8196. + spin_unlock(&percpu->lock);
  8197. + put_cpu_light();
  8198. }
  8199. static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp)
  8200. @@ -6416,6 +6418,7 @@ static int raid5_alloc_percpu(struct r5conf *conf)
  8201. __func__, cpu);
  8202. break;
  8203. }
  8204. + spin_lock_init(&per_cpu_ptr(conf->percpu, cpu)->lock);
  8205. }
  8206. put_online_cpus();
  8207. diff --git a/drivers/md/raid5.h b/drivers/md/raid5.h
  8208. index 517d4b68a1be..efe91887ecd7 100644
  8209. --- a/drivers/md/raid5.h
  8210. +++ b/drivers/md/raid5.h
  8211. @@ -504,6 +504,7 @@ struct r5conf {
  8212. int recovery_disabled;
  8213. /* per cpu variables */
  8214. struct raid5_percpu {
  8215. + spinlock_t lock; /* Protection for -RT */
  8216. struct page *spare_page; /* Used when checking P/Q in raid6 */
  8217. struct flex_array *scribble; /* space for constructing buffer
  8218. * lists and performing address
  8219. diff --git a/drivers/media/platform/vsp1/vsp1_video.c b/drivers/media/platform/vsp1/vsp1_video.c
  8220. index 5ce88e1f5d71..b4f8cd74ecb8 100644
  8221. --- a/drivers/media/platform/vsp1/vsp1_video.c
  8222. +++ b/drivers/media/platform/vsp1/vsp1_video.c
  8223. @@ -520,7 +520,7 @@ static bool vsp1_pipeline_stopped(struct vsp1_pipeline *pipe)
  8224. bool stopped;
  8225. spin_lock_irqsave(&pipe->irqlock, flags);
  8226. - stopped = pipe->state == VSP1_PIPELINE_STOPPED,
  8227. + stopped = pipe->state == VSP1_PIPELINE_STOPPED;
  8228. spin_unlock_irqrestore(&pipe->irqlock, flags);
  8229. return stopped;
  8230. diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig
  8231. index 4bf7d50b1bc7..6f7e99ad6e29 100644
  8232. --- a/drivers/misc/Kconfig
  8233. +++ b/drivers/misc/Kconfig
  8234. @@ -54,6 +54,7 @@ config AD525X_DPOT_SPI
  8235. config ATMEL_TCLIB
  8236. bool "Atmel AT32/AT91 Timer/Counter Library"
  8237. depends on (AVR32 || ARCH_AT91)
  8238. + default y if PREEMPT_RT_FULL
  8239. help
  8240. Select this if you want a library to allocate the Timer/Counter
  8241. blocks found on many Atmel processors. This facilitates using
  8242. @@ -69,8 +70,7 @@ config ATMEL_TCB_CLKSRC
  8243. are combined to make a single 32-bit timer.
  8244. When GENERIC_CLOCKEVENTS is defined, the third timer channel
  8245. - may be used as a clock event device supporting oneshot mode
  8246. - (delays of up to two seconds) based on the 32 KiHz clock.
  8247. + may be used as a clock event device supporting oneshot mode.
  8248. config ATMEL_TCB_CLKSRC_BLOCK
  8249. int
  8250. @@ -84,6 +84,15 @@ config ATMEL_TCB_CLKSRC_BLOCK
  8251. TC can be used for other purposes, such as PWM generation and
  8252. interval timing.
  8253. +config ATMEL_TCB_CLKSRC_USE_SLOW_CLOCK
  8254. + bool "TC Block use 32 KiHz clock"
  8255. + depends on ATMEL_TCB_CLKSRC
  8256. + default y if !PREEMPT_RT_FULL
  8257. + help
  8258. + Select this to use 32 KiHz base clock rate as TC block clock
  8259. + source for clock events.
  8260. +
  8261. +
  8262. config DUMMY_IRQ
  8263. tristate "Dummy IRQ handler"
  8264. default n
  8265. @@ -113,6 +122,35 @@ config IBM_ASM
  8266. for information on the specific driver level and support statement
  8267. for your IBM server.
  8268. +config HWLAT_DETECTOR
  8269. + tristate "Testing module to detect hardware-induced latencies"
  8270. + depends on DEBUG_FS
  8271. + depends on RING_BUFFER
  8272. + default m
  8273. + ---help---
  8274. + A simple hardware latency detector. Use this module to detect
  8275. + large latencies introduced by the behavior of the underlying
  8276. + system firmware external to Linux. We do this using periodic
  8277. + use of stop_machine to grab all available CPUs and measure
  8278. + for unexplainable gaps in the CPU timestamp counter(s). By
  8279. + default, the module is not enabled until the "enable" file
  8280. + within the "hwlat_detector" debugfs directory is toggled.
  8281. +
  8282. + This module is often used to detect SMI (System Management
  8283. + Interrupts) on x86 systems, though is not x86 specific. To
  8284. + this end, we default to using a sample window of 1 second,
  8285. + during which we will sample for 0.5 seconds. If an SMI or
  8286. + similar event occurs during that time, it is recorded
  8287. + into an 8K samples global ring buffer until retreived.
  8288. +
  8289. + WARNING: This software should never be enabled (it can be built
  8290. + but should not be turned on after it is loaded) in a production
  8291. + environment where high latencies are a concern since the
  8292. + sampling mechanism actually introduces latencies for
  8293. + regular tasks while the CPU(s) are being held.
  8294. +
  8295. + If unsure, say N
  8296. +
  8297. config PHANTOM
  8298. tristate "Sensable PHANToM (PCI)"
  8299. depends on PCI
  8300. diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
  8301. index 537d7f3b78da..ec4aecba0656 100644
  8302. --- a/drivers/misc/Makefile
  8303. +++ b/drivers/misc/Makefile
  8304. @@ -39,6 +39,7 @@ obj-$(CONFIG_C2PORT) += c2port/
  8305. obj-$(CONFIG_HMC6352) += hmc6352.o
  8306. obj-y += eeprom/
  8307. obj-y += cb710/
  8308. +obj-$(CONFIG_HWLAT_DETECTOR) += hwlat_detector.o
  8309. obj-$(CONFIG_SPEAR13XX_PCIE_GADGET) += spear13xx_pcie_gadget.o
  8310. obj-$(CONFIG_VMWARE_BALLOON) += vmw_balloon.o
  8311. obj-$(CONFIG_ARM_CHARLCD) += arm-charlcd.o
  8312. diff --git a/drivers/misc/hwlat_detector.c b/drivers/misc/hwlat_detector.c
  8313. new file mode 100644
  8314. index 000000000000..52f5ad5fd9c0
  8315. --- /dev/null
  8316. +++ b/drivers/misc/hwlat_detector.c
  8317. @@ -0,0 +1,1240 @@
  8318. +/*
  8319. + * hwlat_detector.c - A simple Hardware Latency detector.
  8320. + *
  8321. + * Use this module to detect large system latencies induced by the behavior of
  8322. + * certain underlying system hardware or firmware, independent of Linux itself.
  8323. + * The code was developed originally to detect the presence of SMIs on Intel
  8324. + * and AMD systems, although there is no dependency upon x86 herein.
  8325. + *
  8326. + * The classical example usage of this module is in detecting the presence of
  8327. + * SMIs or System Management Interrupts on Intel and AMD systems. An SMI is a
  8328. + * somewhat special form of hardware interrupt spawned from earlier CPU debug
  8329. + * modes in which the (BIOS/EFI/etc.) firmware arranges for the South Bridge
  8330. + * LPC (or other device) to generate a special interrupt under certain
  8331. + * circumstances, for example, upon expiration of a special SMI timer device,
  8332. + * due to certain external thermal readings, on certain I/O address accesses,
  8333. + * and other situations. An SMI hits a special CPU pin, triggers a special
  8334. + * SMI mode (complete with special memory map), and the OS is unaware.
  8335. + *
  8336. + * Although certain hardware-inducing latencies are necessary (for example,
  8337. + * a modern system often requires an SMI handler for correct thermal control
  8338. + * and remote management) they can wreak havoc upon any OS-level performance
  8339. + * guarantees toward low-latency, especially when the OS is not even made
  8340. + * aware of the presence of these interrupts. For this reason, we need a
  8341. + * somewhat brute force mechanism to detect these interrupts. In this case,
  8342. + * we do it by hogging all of the CPU(s) for configurable timer intervals,
  8343. + * sampling the built-in CPU timer, looking for discontiguous readings.
  8344. + *
  8345. + * WARNING: This implementation necessarily introduces latencies. Therefore,
  8346. + * you should NEVER use this module in a production environment
  8347. + * requiring any kind of low-latency performance guarantee(s).
  8348. + *
  8349. + * Copyright (C) 2008-2009 Jon Masters, Red Hat, Inc. <jcm@redhat.com>
  8350. + *
  8351. + * Includes useful feedback from Clark Williams <clark@redhat.com>
  8352. + *
  8353. + * This file is licensed under the terms of the GNU General Public
  8354. + * License version 2. This program is licensed "as is" without any
  8355. + * warranty of any kind, whether express or implied.
  8356. + */
  8357. +
  8358. +#include <linux/module.h>
  8359. +#include <linux/init.h>
  8360. +#include <linux/ring_buffer.h>
  8361. +#include <linux/time.h>
  8362. +#include <linux/hrtimer.h>
  8363. +#include <linux/kthread.h>
  8364. +#include <linux/debugfs.h>
  8365. +#include <linux/seq_file.h>
  8366. +#include <linux/uaccess.h>
  8367. +#include <linux/version.h>
  8368. +#include <linux/delay.h>
  8369. +#include <linux/slab.h>
  8370. +#include <linux/trace_clock.h>
  8371. +
  8372. +#define BUF_SIZE_DEFAULT 262144UL /* 8K*(sizeof(entry)) */
  8373. +#define BUF_FLAGS (RB_FL_OVERWRITE) /* no block on full */
  8374. +#define U64STR_SIZE 22 /* 20 digits max */
  8375. +
  8376. +#define VERSION "1.0.0"
  8377. +#define BANNER "hwlat_detector: "
  8378. +#define DRVNAME "hwlat_detector"
  8379. +#define DEFAULT_SAMPLE_WINDOW 1000000 /* 1s */
  8380. +#define DEFAULT_SAMPLE_WIDTH 500000 /* 0.5s */
  8381. +#define DEFAULT_LAT_THRESHOLD 10 /* 10us */
  8382. +
  8383. +/* Module metadata */
  8384. +
  8385. +MODULE_LICENSE("GPL");
  8386. +MODULE_AUTHOR("Jon Masters <jcm@redhat.com>");
  8387. +MODULE_DESCRIPTION("A simple hardware latency detector");
  8388. +MODULE_VERSION(VERSION);
  8389. +
  8390. +/* Module parameters */
  8391. +
  8392. +static int debug;
  8393. +static int enabled;
  8394. +static int threshold;
  8395. +
  8396. +module_param(debug, int, 0); /* enable debug */
  8397. +module_param(enabled, int, 0); /* enable detector */
  8398. +module_param(threshold, int, 0); /* latency threshold */
  8399. +
  8400. +/* Buffering and sampling */
  8401. +
  8402. +static struct ring_buffer *ring_buffer; /* sample buffer */
  8403. +static DEFINE_MUTEX(ring_buffer_mutex); /* lock changes */
  8404. +static unsigned long buf_size = BUF_SIZE_DEFAULT;
  8405. +static struct task_struct *kthread; /* sampling thread */
  8406. +
  8407. +/* DebugFS filesystem entries */
  8408. +
  8409. +static struct dentry *debug_dir; /* debugfs directory */
  8410. +static struct dentry *debug_max; /* maximum TSC delta */
  8411. +static struct dentry *debug_count; /* total detect count */
  8412. +static struct dentry *debug_sample_width; /* sample width us */
  8413. +static struct dentry *debug_sample_window; /* sample window us */
  8414. +static struct dentry *debug_sample; /* raw samples us */
  8415. +static struct dentry *debug_threshold; /* threshold us */
  8416. +static struct dentry *debug_enable; /* enable/disable */
  8417. +
  8418. +/* Individual samples and global state */
  8419. +
  8420. +struct sample; /* latency sample */
  8421. +struct data; /* Global state */
  8422. +
  8423. +/* Sampling functions */
  8424. +static int __buffer_add_sample(struct sample *sample);
  8425. +static struct sample *buffer_get_sample(struct sample *sample);
  8426. +
  8427. +/* Threading and state */
  8428. +static int kthread_fn(void *unused);
  8429. +static int start_kthread(void);
  8430. +static int stop_kthread(void);
  8431. +static void __reset_stats(void);
  8432. +static int init_stats(void);
  8433. +
  8434. +/* Debugfs interface */
  8435. +static ssize_t simple_data_read(struct file *filp, char __user *ubuf,
  8436. + size_t cnt, loff_t *ppos, const u64 *entry);
  8437. +static ssize_t simple_data_write(struct file *filp, const char __user *ubuf,
  8438. + size_t cnt, loff_t *ppos, u64 *entry);
  8439. +static int debug_sample_fopen(struct inode *inode, struct file *filp);
  8440. +static ssize_t debug_sample_fread(struct file *filp, char __user *ubuf,
  8441. + size_t cnt, loff_t *ppos);
  8442. +static int debug_sample_release(struct inode *inode, struct file *filp);
  8443. +static int debug_enable_fopen(struct inode *inode, struct file *filp);
  8444. +static ssize_t debug_enable_fread(struct file *filp, char __user *ubuf,
  8445. + size_t cnt, loff_t *ppos);
  8446. +static ssize_t debug_enable_fwrite(struct file *file,
  8447. + const char __user *user_buffer,
  8448. + size_t user_size, loff_t *offset);
  8449. +
  8450. +/* Initialization functions */
  8451. +static int init_debugfs(void);
  8452. +static void free_debugfs(void);
  8453. +static int detector_init(void);
  8454. +static void detector_exit(void);
  8455. +
  8456. +/* Individual latency samples are stored here when detected and packed into
  8457. + * the ring_buffer circular buffer, where they are overwritten when
  8458. + * more than buf_size/sizeof(sample) samples are received. */
  8459. +struct sample {
  8460. + u64 seqnum; /* unique sequence */
  8461. + u64 duration; /* ktime delta */
  8462. + u64 outer_duration; /* ktime delta (outer loop) */
  8463. + struct timespec timestamp; /* wall time */
  8464. + unsigned long lost;
  8465. +};
  8466. +
  8467. +/* keep the global state somewhere. */
  8468. +static struct data {
  8469. +
  8470. + struct mutex lock; /* protect changes */
  8471. +
  8472. + u64 count; /* total since reset */
  8473. + u64 max_sample; /* max hardware latency */
  8474. + u64 threshold; /* sample threshold level */
  8475. +
  8476. + u64 sample_window; /* total sampling window (on+off) */
  8477. + u64 sample_width; /* active sampling portion of window */
  8478. +
  8479. + atomic_t sample_open; /* whether the sample file is open */
  8480. +
  8481. + wait_queue_head_t wq; /* waitqeue for new sample values */
  8482. +
  8483. +} data;
  8484. +
  8485. +/**
  8486. + * __buffer_add_sample - add a new latency sample recording to the ring buffer
  8487. + * @sample: The new latency sample value
  8488. + *
  8489. + * This receives a new latency sample and records it in a global ring buffer.
  8490. + * No additional locking is used in this case.
  8491. + */
  8492. +static int __buffer_add_sample(struct sample *sample)
  8493. +{
  8494. + return ring_buffer_write(ring_buffer,
  8495. + sizeof(struct sample), sample);
  8496. +}
  8497. +
  8498. +/**
  8499. + * buffer_get_sample - remove a hardware latency sample from the ring buffer
  8500. + * @sample: Pre-allocated storage for the sample
  8501. + *
  8502. + * This retrieves a hardware latency sample from the global circular buffer
  8503. + */
  8504. +static struct sample *buffer_get_sample(struct sample *sample)
  8505. +{
  8506. + struct ring_buffer_event *e = NULL;
  8507. + struct sample *s = NULL;
  8508. + unsigned int cpu = 0;
  8509. +
  8510. + if (!sample)
  8511. + return NULL;
  8512. +
  8513. + mutex_lock(&ring_buffer_mutex);
  8514. + for_each_online_cpu(cpu) {
  8515. + e = ring_buffer_consume(ring_buffer, cpu, NULL, &sample->lost);
  8516. + if (e)
  8517. + break;
  8518. + }
  8519. +
  8520. + if (e) {
  8521. + s = ring_buffer_event_data(e);
  8522. + memcpy(sample, s, sizeof(struct sample));
  8523. + } else
  8524. + sample = NULL;
  8525. + mutex_unlock(&ring_buffer_mutex);
  8526. +
  8527. + return sample;
  8528. +}
  8529. +
  8530. +#ifndef CONFIG_TRACING
  8531. +#define time_type ktime_t
  8532. +#define time_get() ktime_get()
  8533. +#define time_to_us(x) ktime_to_us(x)
  8534. +#define time_sub(a, b) ktime_sub(a, b)
  8535. +#define init_time(a, b) (a).tv64 = b
  8536. +#define time_u64(a) ((a).tv64)
  8537. +#else
  8538. +#define time_type u64
  8539. +#define time_get() trace_clock_local()
  8540. +#define time_to_us(x) div_u64(x, 1000)
  8541. +#define time_sub(a, b) ((a) - (b))
  8542. +#define init_time(a, b) (a = b)
  8543. +#define time_u64(a) a
  8544. +#endif
  8545. +/**
  8546. + * get_sample - sample the CPU TSC and look for likely hardware latencies
  8547. + *
  8548. + * Used to repeatedly capture the CPU TSC (or similar), looking for potential
  8549. + * hardware-induced latency. Called with interrupts disabled and with
  8550. + * data.lock held.
  8551. + */
  8552. +static int get_sample(void)
  8553. +{
  8554. + time_type start, t1, t2, last_t2;
  8555. + s64 diff, total = 0;
  8556. + u64 sample = 0;
  8557. + u64 outer_sample = 0;
  8558. + int ret = -1;
  8559. +
  8560. + init_time(last_t2, 0);
  8561. + start = time_get(); /* start timestamp */
  8562. +
  8563. + do {
  8564. +
  8565. + t1 = time_get(); /* we'll look for a discontinuity */
  8566. + t2 = time_get();
  8567. +
  8568. + if (time_u64(last_t2)) {
  8569. + /* Check the delta from outer loop (t2 to next t1) */
  8570. + diff = time_to_us(time_sub(t1, last_t2));
  8571. + /* This shouldn't happen */
  8572. + if (diff < 0) {
  8573. + pr_err(BANNER "time running backwards\n");
  8574. + goto out;
  8575. + }
  8576. + if (diff > outer_sample)
  8577. + outer_sample = diff;
  8578. + }
  8579. + last_t2 = t2;
  8580. +
  8581. + total = time_to_us(time_sub(t2, start)); /* sample width */
  8582. +
  8583. + /* This checks the inner loop (t1 to t2) */
  8584. + diff = time_to_us(time_sub(t2, t1)); /* current diff */
  8585. +
  8586. + /* This shouldn't happen */
  8587. + if (diff < 0) {
  8588. + pr_err(BANNER "time running backwards\n");
  8589. + goto out;
  8590. + }
  8591. +
  8592. + if (diff > sample)
  8593. + sample = diff; /* only want highest value */
  8594. +
  8595. + } while (total <= data.sample_width);
  8596. +
  8597. + ret = 0;
  8598. +
  8599. + /* If we exceed the threshold value, we have found a hardware latency */
  8600. + if (sample > data.threshold || outer_sample > data.threshold) {
  8601. + struct sample s;
  8602. +
  8603. + ret = 1;
  8604. +
  8605. + data.count++;
  8606. + s.seqnum = data.count;
  8607. + s.duration = sample;
  8608. + s.outer_duration = outer_sample;
  8609. + s.timestamp = CURRENT_TIME;
  8610. + __buffer_add_sample(&s);
  8611. +
  8612. + /* Keep a running maximum ever recorded hardware latency */
  8613. + if (sample > data.max_sample)
  8614. + data.max_sample = sample;
  8615. + }
  8616. +
  8617. +out:
  8618. + return ret;
  8619. +}
  8620. +
  8621. +/*
  8622. + * kthread_fn - The CPU time sampling/hardware latency detection kernel thread
  8623. + * @unused: A required part of the kthread API.
  8624. + *
  8625. + * Used to periodically sample the CPU TSC via a call to get_sample. We
  8626. + * disable interrupts, which does (intentionally) introduce latency since we
  8627. + * need to ensure nothing else might be running (and thus pre-empting).
  8628. + * Obviously this should never be used in production environments.
  8629. + *
  8630. + * Currently this runs on which ever CPU it was scheduled on, but most
  8631. + * real-worald hardware latency situations occur across several CPUs,
  8632. + * but we might later generalize this if we find there are any actualy
  8633. + * systems with alternate SMI delivery or other hardware latencies.
  8634. + */
  8635. +static int kthread_fn(void *unused)
  8636. +{
  8637. + int ret;
  8638. + u64 interval;
  8639. +
  8640. + while (!kthread_should_stop()) {
  8641. +
  8642. + mutex_lock(&data.lock);
  8643. +
  8644. + local_irq_disable();
  8645. + ret = get_sample();
  8646. + local_irq_enable();
  8647. +
  8648. + if (ret > 0)
  8649. + wake_up(&data.wq); /* wake up reader(s) */
  8650. +
  8651. + interval = data.sample_window - data.sample_width;
  8652. + do_div(interval, USEC_PER_MSEC); /* modifies interval value */
  8653. +
  8654. + mutex_unlock(&data.lock);
  8655. +
  8656. + if (msleep_interruptible(interval))
  8657. + break;
  8658. + }
  8659. +
  8660. + return 0;
  8661. +}
  8662. +
  8663. +/**
  8664. + * start_kthread - Kick off the hardware latency sampling/detector kthread
  8665. + *
  8666. + * This starts a kernel thread that will sit and sample the CPU timestamp
  8667. + * counter (TSC or similar) and look for potential hardware latencies.
  8668. + */
  8669. +static int start_kthread(void)
  8670. +{
  8671. + kthread = kthread_run(kthread_fn, NULL,
  8672. + DRVNAME);
  8673. + if (IS_ERR(kthread)) {
  8674. + pr_err(BANNER "could not start sampling thread\n");
  8675. + enabled = 0;
  8676. + return -ENOMEM;
  8677. + }
  8678. +
  8679. + return 0;
  8680. +}
  8681. +
  8682. +/**
  8683. + * stop_kthread - Inform the hardware latency samping/detector kthread to stop
  8684. + *
  8685. + * This kicks the running hardware latency sampling/detector kernel thread and
  8686. + * tells it to stop sampling now. Use this on unload and at system shutdown.
  8687. + */
  8688. +static int stop_kthread(void)
  8689. +{
  8690. + int ret;
  8691. +
  8692. + ret = kthread_stop(kthread);
  8693. +
  8694. + return ret;
  8695. +}
  8696. +
  8697. +/**
  8698. + * __reset_stats - Reset statistics for the hardware latency detector
  8699. + *
  8700. + * We use data to store various statistics and global state. We call this
  8701. + * function in order to reset those when "enable" is toggled on or off, and
  8702. + * also at initialization. Should be called with data.lock held.
  8703. + */
  8704. +static void __reset_stats(void)
  8705. +{
  8706. + data.count = 0;
  8707. + data.max_sample = 0;
  8708. + ring_buffer_reset(ring_buffer); /* flush out old sample entries */
  8709. +}
  8710. +
  8711. +/**
  8712. + * init_stats - Setup global state statistics for the hardware latency detector
  8713. + *
  8714. + * We use data to store various statistics and global state. We also use
  8715. + * a global ring buffer (ring_buffer) to keep raw samples of detected hardware
  8716. + * induced system latencies. This function initializes these structures and
  8717. + * allocates the global ring buffer also.
  8718. + */
  8719. +static int init_stats(void)
  8720. +{
  8721. + int ret = -ENOMEM;
  8722. +
  8723. + mutex_init(&data.lock);
  8724. + init_waitqueue_head(&data.wq);
  8725. + atomic_set(&data.sample_open, 0);
  8726. +
  8727. + ring_buffer = ring_buffer_alloc(buf_size, BUF_FLAGS);
  8728. +
  8729. + if (WARN(!ring_buffer, KERN_ERR BANNER
  8730. + "failed to allocate ring buffer!\n"))
  8731. + goto out;
  8732. +
  8733. + __reset_stats();
  8734. + data.threshold = threshold ?: DEFAULT_LAT_THRESHOLD; /* threshold us */
  8735. + data.sample_window = DEFAULT_SAMPLE_WINDOW; /* window us */
  8736. + data.sample_width = DEFAULT_SAMPLE_WIDTH; /* width us */
  8737. +
  8738. + ret = 0;
  8739. +
  8740. +out:
  8741. + return ret;
  8742. +
  8743. +}
  8744. +
  8745. +/*
  8746. + * simple_data_read - Wrapper read function for global state debugfs entries
  8747. + * @filp: The active open file structure for the debugfs "file"
  8748. + * @ubuf: The userspace provided buffer to read value into
  8749. + * @cnt: The maximum number of bytes to read
  8750. + * @ppos: The current "file" position
  8751. + * @entry: The entry to read from
  8752. + *
  8753. + * This function provides a generic read implementation for the global state
  8754. + * "data" structure debugfs filesystem entries. It would be nice to use
  8755. + * simple_attr_read directly, but we need to make sure that the data.lock
  8756. + * is held during the actual read.
  8757. + */
  8758. +static ssize_t simple_data_read(struct file *filp, char __user *ubuf,
  8759. + size_t cnt, loff_t *ppos, const u64 *entry)
  8760. +{
  8761. + char buf[U64STR_SIZE];
  8762. + u64 val = 0;
  8763. + int len = 0;
  8764. +
  8765. + memset(buf, 0, sizeof(buf));
  8766. +
  8767. + if (!entry)
  8768. + return -EFAULT;
  8769. +
  8770. + mutex_lock(&data.lock);
  8771. + val = *entry;
  8772. + mutex_unlock(&data.lock);
  8773. +
  8774. + len = snprintf(buf, sizeof(buf), "%llu\n", (unsigned long long)val);
  8775. +
  8776. + return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
  8777. +
  8778. +}
  8779. +
  8780. +/*
  8781. + * simple_data_write - Wrapper write function for global state debugfs entries
  8782. + * @filp: The active open file structure for the debugfs "file"
  8783. + * @ubuf: The userspace provided buffer to write value from
  8784. + * @cnt: The maximum number of bytes to write
  8785. + * @ppos: The current "file" position
  8786. + * @entry: The entry to write to
  8787. + *
  8788. + * This function provides a generic write implementation for the global state
  8789. + * "data" structure debugfs filesystem entries. It would be nice to use
  8790. + * simple_attr_write directly, but we need to make sure that the data.lock
  8791. + * is held during the actual write.
  8792. + */
  8793. +static ssize_t simple_data_write(struct file *filp, const char __user *ubuf,
  8794. + size_t cnt, loff_t *ppos, u64 *entry)
  8795. +{
  8796. + char buf[U64STR_SIZE];
  8797. + int csize = min(cnt, sizeof(buf));
  8798. + u64 val = 0;
  8799. + int err = 0;
  8800. +
  8801. + memset(buf, '\0', sizeof(buf));
  8802. + if (copy_from_user(buf, ubuf, csize))
  8803. + return -EFAULT;
  8804. +
  8805. + buf[U64STR_SIZE-1] = '\0'; /* just in case */
  8806. + err = kstrtoull(buf, 10, &val);
  8807. + if (err)
  8808. + return -EINVAL;
  8809. +
  8810. + mutex_lock(&data.lock);
  8811. + *entry = val;
  8812. + mutex_unlock(&data.lock);
  8813. +
  8814. + return csize;
  8815. +}
  8816. +
  8817. +/**
  8818. + * debug_count_fopen - Open function for "count" debugfs entry
  8819. + * @inode: The in-kernel inode representation of the debugfs "file"
  8820. + * @filp: The active open file structure for the debugfs "file"
  8821. + *
  8822. + * This function provides an open implementation for the "count" debugfs
  8823. + * interface to the hardware latency detector.
  8824. + */
  8825. +static int debug_count_fopen(struct inode *inode, struct file *filp)
  8826. +{
  8827. + return 0;
  8828. +}
  8829. +
  8830. +/**
  8831. + * debug_count_fread - Read function for "count" debugfs entry
  8832. + * @filp: The active open file structure for the debugfs "file"
  8833. + * @ubuf: The userspace provided buffer to read value into
  8834. + * @cnt: The maximum number of bytes to read
  8835. + * @ppos: The current "file" position
  8836. + *
  8837. + * This function provides a read implementation for the "count" debugfs
  8838. + * interface to the hardware latency detector. Can be used to read the
  8839. + * number of latency readings exceeding the configured threshold since
  8840. + * the detector was last reset (e.g. by writing a zero into "count").
  8841. + */
  8842. +static ssize_t debug_count_fread(struct file *filp, char __user *ubuf,
  8843. + size_t cnt, loff_t *ppos)
  8844. +{
  8845. + return simple_data_read(filp, ubuf, cnt, ppos, &data.count);
  8846. +}
  8847. +
  8848. +/**
  8849. + * debug_count_fwrite - Write function for "count" debugfs entry
  8850. + * @filp: The active open file structure for the debugfs "file"
  8851. + * @ubuf: The user buffer that contains the value to write
  8852. + * @cnt: The maximum number of bytes to write to "file"
  8853. + * @ppos: The current position in the debugfs "file"
  8854. + *
  8855. + * This function provides a write implementation for the "count" debugfs
  8856. + * interface to the hardware latency detector. Can be used to write a
  8857. + * desired value, especially to zero the total count.
  8858. + */
  8859. +static ssize_t debug_count_fwrite(struct file *filp,
  8860. + const char __user *ubuf,
  8861. + size_t cnt,
  8862. + loff_t *ppos)
  8863. +{
  8864. + return simple_data_write(filp, ubuf, cnt, ppos, &data.count);
  8865. +}
  8866. +
  8867. +/**
  8868. + * debug_enable_fopen - Dummy open function for "enable" debugfs interface
  8869. + * @inode: The in-kernel inode representation of the debugfs "file"
  8870. + * @filp: The active open file structure for the debugfs "file"
  8871. + *
  8872. + * This function provides an open implementation for the "enable" debugfs
  8873. + * interface to the hardware latency detector.
  8874. + */
  8875. +static int debug_enable_fopen(struct inode *inode, struct file *filp)
  8876. +{
  8877. + return 0;
  8878. +}
  8879. +
  8880. +/**
  8881. + * debug_enable_fread - Read function for "enable" debugfs interface
  8882. + * @filp: The active open file structure for the debugfs "file"
  8883. + * @ubuf: The userspace provided buffer to read value into
  8884. + * @cnt: The maximum number of bytes to read
  8885. + * @ppos: The current "file" position
  8886. + *
  8887. + * This function provides a read implementation for the "enable" debugfs
  8888. + * interface to the hardware latency detector. Can be used to determine
  8889. + * whether the detector is currently enabled ("0\n" or "1\n" returned).
  8890. + */
  8891. +static ssize_t debug_enable_fread(struct file *filp, char __user *ubuf,
  8892. + size_t cnt, loff_t *ppos)
  8893. +{
  8894. + char buf[4];
  8895. +
  8896. + if ((cnt < sizeof(buf)) || (*ppos))
  8897. + return 0;
  8898. +
  8899. + buf[0] = enabled ? '1' : '0';
  8900. + buf[1] = '\n';
  8901. + buf[2] = '\0';
  8902. + if (copy_to_user(ubuf, buf, strlen(buf)))
  8903. + return -EFAULT;
  8904. + return *ppos = strlen(buf);
  8905. +}
  8906. +
  8907. +/**
  8908. + * debug_enable_fwrite - Write function for "enable" debugfs interface
  8909. + * @filp: The active open file structure for the debugfs "file"
  8910. + * @ubuf: The user buffer that contains the value to write
  8911. + * @cnt: The maximum number of bytes to write to "file"
  8912. + * @ppos: The current position in the debugfs "file"
  8913. + *
  8914. + * This function provides a write implementation for the "enable" debugfs
  8915. + * interface to the hardware latency detector. Can be used to enable or
  8916. + * disable the detector, which will have the side-effect of possibly
  8917. + * also resetting the global stats and kicking off the measuring
  8918. + * kthread (on an enable) or the converse (upon a disable).
  8919. + */
  8920. +static ssize_t debug_enable_fwrite(struct file *filp,
  8921. + const char __user *ubuf,
  8922. + size_t cnt,
  8923. + loff_t *ppos)
  8924. +{
  8925. + char buf[4];
  8926. + int csize = min(cnt, sizeof(buf));
  8927. + long val = 0;
  8928. + int err = 0;
  8929. +
  8930. + memset(buf, '\0', sizeof(buf));
  8931. + if (copy_from_user(buf, ubuf, csize))
  8932. + return -EFAULT;
  8933. +
  8934. + buf[sizeof(buf)-1] = '\0'; /* just in case */
  8935. + err = kstrtoul(buf, 10, &val);
  8936. + if (err)
  8937. + return -EINVAL;
  8938. +
  8939. + if (val) {
  8940. + if (enabled)
  8941. + goto unlock;
  8942. + enabled = 1;
  8943. + __reset_stats();
  8944. + if (start_kthread())
  8945. + return -EFAULT;
  8946. + } else {
  8947. + if (!enabled)
  8948. + goto unlock;
  8949. + enabled = 0;
  8950. + err = stop_kthread();
  8951. + if (err) {
  8952. + pr_err(BANNER "cannot stop kthread\n");
  8953. + return -EFAULT;
  8954. + }
  8955. + wake_up(&data.wq); /* reader(s) should return */
  8956. + }
  8957. +unlock:
  8958. + return csize;
  8959. +}
  8960. +
  8961. +/**
  8962. + * debug_max_fopen - Open function for "max" debugfs entry
  8963. + * @inode: The in-kernel inode representation of the debugfs "file"
  8964. + * @filp: The active open file structure for the debugfs "file"
  8965. + *
  8966. + * This function provides an open implementation for the "max" debugfs
  8967. + * interface to the hardware latency detector.
  8968. + */
  8969. +static int debug_max_fopen(struct inode *inode, struct file *filp)
  8970. +{
  8971. + return 0;
  8972. +}
  8973. +
  8974. +/**
  8975. + * debug_max_fread - Read function for "max" debugfs entry
  8976. + * @filp: The active open file structure for the debugfs "file"
  8977. + * @ubuf: The userspace provided buffer to read value into
  8978. + * @cnt: The maximum number of bytes to read
  8979. + * @ppos: The current "file" position
  8980. + *
  8981. + * This function provides a read implementation for the "max" debugfs
  8982. + * interface to the hardware latency detector. Can be used to determine
  8983. + * the maximum latency value observed since it was last reset.
  8984. + */
  8985. +static ssize_t debug_max_fread(struct file *filp, char __user *ubuf,
  8986. + size_t cnt, loff_t *ppos)
  8987. +{
  8988. + return simple_data_read(filp, ubuf, cnt, ppos, &data.max_sample);
  8989. +}
  8990. +
  8991. +/**
  8992. + * debug_max_fwrite - Write function for "max" debugfs entry
  8993. + * @filp: The active open file structure for the debugfs "file"
  8994. + * @ubuf: The user buffer that contains the value to write
  8995. + * @cnt: The maximum number of bytes to write to "file"
  8996. + * @ppos: The current position in the debugfs "file"
  8997. + *
  8998. + * This function provides a write implementation for the "max" debugfs
  8999. + * interface to the hardware latency detector. Can be used to reset the
  9000. + * maximum or set it to some other desired value - if, then, subsequent
  9001. + * measurements exceed this value, the maximum will be updated.
  9002. + */
  9003. +static ssize_t debug_max_fwrite(struct file *filp,
  9004. + const char __user *ubuf,
  9005. + size_t cnt,
  9006. + loff_t *ppos)
  9007. +{
  9008. + return simple_data_write(filp, ubuf, cnt, ppos, &data.max_sample);
  9009. +}
  9010. +
  9011. +
  9012. +/**
  9013. + * debug_sample_fopen - An open function for "sample" debugfs interface
  9014. + * @inode: The in-kernel inode representation of this debugfs "file"
  9015. + * @filp: The active open file structure for the debugfs "file"
  9016. + *
  9017. + * This function handles opening the "sample" file within the hardware
  9018. + * latency detector debugfs directory interface. This file is used to read
  9019. + * raw samples from the global ring_buffer and allows the user to see a
  9020. + * running latency history. Can be opened blocking or non-blocking,
  9021. + * affecting whether it behaves as a buffer read pipe, or does not.
  9022. + * Implements simple locking to prevent multiple simultaneous use.
  9023. + */
  9024. +static int debug_sample_fopen(struct inode *inode, struct file *filp)
  9025. +{
  9026. + if (!atomic_add_unless(&data.sample_open, 1, 1))
  9027. + return -EBUSY;
  9028. + else
  9029. + return 0;
  9030. +}
  9031. +
  9032. +/**
  9033. + * debug_sample_fread - A read function for "sample" debugfs interface
  9034. + * @filp: The active open file structure for the debugfs "file"
  9035. + * @ubuf: The user buffer that will contain the samples read
  9036. + * @cnt: The maximum bytes to read from the debugfs "file"
  9037. + * @ppos: The current position in the debugfs "file"
  9038. + *
  9039. + * This function handles reading from the "sample" file within the hardware
  9040. + * latency detector debugfs directory interface. This file is used to read
  9041. + * raw samples from the global ring_buffer and allows the user to see a
  9042. + * running latency history. By default this will block pending a new
  9043. + * value written into the sample buffer, unless there are already a
  9044. + * number of value(s) waiting in the buffer, or the sample file was
  9045. + * previously opened in a non-blocking mode of operation.
  9046. + */
  9047. +static ssize_t debug_sample_fread(struct file *filp, char __user *ubuf,
  9048. + size_t cnt, loff_t *ppos)
  9049. +{
  9050. + int len = 0;
  9051. + char buf[64];
  9052. + struct sample *sample = NULL;
  9053. +
  9054. + if (!enabled)
  9055. + return 0;
  9056. +
  9057. + sample = kzalloc(sizeof(struct sample), GFP_KERNEL);
  9058. + if (!sample)
  9059. + return -ENOMEM;
  9060. +
  9061. + while (!buffer_get_sample(sample)) {
  9062. +
  9063. + DEFINE_WAIT(wait);
  9064. +
  9065. + if (filp->f_flags & O_NONBLOCK) {
  9066. + len = -EAGAIN;
  9067. + goto out;
  9068. + }
  9069. +
  9070. + prepare_to_wait(&data.wq, &wait, TASK_INTERRUPTIBLE);
  9071. + schedule();
  9072. + finish_wait(&data.wq, &wait);
  9073. +
  9074. + if (signal_pending(current)) {
  9075. + len = -EINTR;
  9076. + goto out;
  9077. + }
  9078. +
  9079. + if (!enabled) { /* enable was toggled */
  9080. + len = 0;
  9081. + goto out;
  9082. + }
  9083. + }
  9084. +
  9085. + len = snprintf(buf, sizeof(buf), "%010lu.%010lu\t%llu\t%llu\n",
  9086. + sample->timestamp.tv_sec,
  9087. + sample->timestamp.tv_nsec,
  9088. + sample->duration,
  9089. + sample->outer_duration);
  9090. +
  9091. +
  9092. + /* handling partial reads is more trouble than it's worth */
  9093. + if (len > cnt)
  9094. + goto out;
  9095. +
  9096. + if (copy_to_user(ubuf, buf, len))
  9097. + len = -EFAULT;
  9098. +
  9099. +out:
  9100. + kfree(sample);
  9101. + return len;
  9102. +}
  9103. +
  9104. +/**
  9105. + * debug_sample_release - Release function for "sample" debugfs interface
  9106. + * @inode: The in-kernel inode represenation of the debugfs "file"
  9107. + * @filp: The active open file structure for the debugfs "file"
  9108. + *
  9109. + * This function completes the close of the debugfs interface "sample" file.
  9110. + * Frees the sample_open "lock" so that other users may open the interface.
  9111. + */
  9112. +static int debug_sample_release(struct inode *inode, struct file *filp)
  9113. +{
  9114. + atomic_dec(&data.sample_open);
  9115. +
  9116. + return 0;
  9117. +}
  9118. +
  9119. +/**
  9120. + * debug_threshold_fopen - Open function for "threshold" debugfs entry
  9121. + * @inode: The in-kernel inode representation of the debugfs "file"
  9122. + * @filp: The active open file structure for the debugfs "file"
  9123. + *
  9124. + * This function provides an open implementation for the "threshold" debugfs
  9125. + * interface to the hardware latency detector.
  9126. + */
  9127. +static int debug_threshold_fopen(struct inode *inode, struct file *filp)
  9128. +{
  9129. + return 0;
  9130. +}
  9131. +
  9132. +/**
  9133. + * debug_threshold_fread - Read function for "threshold" debugfs entry
  9134. + * @filp: The active open file structure for the debugfs "file"
  9135. + * @ubuf: The userspace provided buffer to read value into
  9136. + * @cnt: The maximum number of bytes to read
  9137. + * @ppos: The current "file" position
  9138. + *
  9139. + * This function provides a read implementation for the "threshold" debugfs
  9140. + * interface to the hardware latency detector. It can be used to determine
  9141. + * the current threshold level at which a latency will be recorded in the
  9142. + * global ring buffer, typically on the order of 10us.
  9143. + */
  9144. +static ssize_t debug_threshold_fread(struct file *filp, char __user *ubuf,
  9145. + size_t cnt, loff_t *ppos)
  9146. +{
  9147. + return simple_data_read(filp, ubuf, cnt, ppos, &data.threshold);
  9148. +}
  9149. +
  9150. +/**
  9151. + * debug_threshold_fwrite - Write function for "threshold" debugfs entry
  9152. + * @filp: The active open file structure for the debugfs "file"
  9153. + * @ubuf: The user buffer that contains the value to write
  9154. + * @cnt: The maximum number of bytes to write to "file"
  9155. + * @ppos: The current position in the debugfs "file"
  9156. + *
  9157. + * This function provides a write implementation for the "threshold" debugfs
  9158. + * interface to the hardware latency detector. It can be used to configure
  9159. + * the threshold level at which any subsequently detected latencies will
  9160. + * be recorded into the global ring buffer.
  9161. + */
  9162. +static ssize_t debug_threshold_fwrite(struct file *filp,
  9163. + const char __user *ubuf,
  9164. + size_t cnt,
  9165. + loff_t *ppos)
  9166. +{
  9167. + int ret;
  9168. +
  9169. + ret = simple_data_write(filp, ubuf, cnt, ppos, &data.threshold);
  9170. +
  9171. + if (enabled)
  9172. + wake_up_process(kthread);
  9173. +
  9174. + return ret;
  9175. +}
  9176. +
  9177. +/**
  9178. + * debug_width_fopen - Open function for "width" debugfs entry
  9179. + * @inode: The in-kernel inode representation of the debugfs "file"
  9180. + * @filp: The active open file structure for the debugfs "file"
  9181. + *
  9182. + * This function provides an open implementation for the "width" debugfs
  9183. + * interface to the hardware latency detector.
  9184. + */
  9185. +static int debug_width_fopen(struct inode *inode, struct file *filp)
  9186. +{
  9187. + return 0;
  9188. +}
  9189. +
  9190. +/**
  9191. + * debug_width_fread - Read function for "width" debugfs entry
  9192. + * @filp: The active open file structure for the debugfs "file"
  9193. + * @ubuf: The userspace provided buffer to read value into
  9194. + * @cnt: The maximum number of bytes to read
  9195. + * @ppos: The current "file" position
  9196. + *
  9197. + * This function provides a read implementation for the "width" debugfs
  9198. + * interface to the hardware latency detector. It can be used to determine
  9199. + * for how many us of the total window us we will actively sample for any
  9200. + * hardware-induced latecy periods. Obviously, it is not possible to
  9201. + * sample constantly and have the system respond to a sample reader, or,
  9202. + * worse, without having the system appear to have gone out to lunch.
  9203. + */
  9204. +static ssize_t debug_width_fread(struct file *filp, char __user *ubuf,
  9205. + size_t cnt, loff_t *ppos)
  9206. +{
  9207. + return simple_data_read(filp, ubuf, cnt, ppos, &data.sample_width);
  9208. +}
  9209. +
  9210. +/**
  9211. + * debug_width_fwrite - Write function for "width" debugfs entry
  9212. + * @filp: The active open file structure for the debugfs "file"
  9213. + * @ubuf: The user buffer that contains the value to write
  9214. + * @cnt: The maximum number of bytes to write to "file"
  9215. + * @ppos: The current position in the debugfs "file"
  9216. + *
  9217. + * This function provides a write implementation for the "width" debugfs
  9218. + * interface to the hardware latency detector. It can be used to configure
  9219. + * for how many us of the total window us we will actively sample for any
  9220. + * hardware-induced latency periods. Obviously, it is not possible to
  9221. + * sample constantly and have the system respond to a sample reader, or,
  9222. + * worse, without having the system appear to have gone out to lunch. It
  9223. + * is enforced that width is less that the total window size.
  9224. + */
  9225. +static ssize_t debug_width_fwrite(struct file *filp,
  9226. + const char __user *ubuf,
  9227. + size_t cnt,
  9228. + loff_t *ppos)
  9229. +{
  9230. + char buf[U64STR_SIZE];
  9231. + int csize = min(cnt, sizeof(buf));
  9232. + u64 val = 0;
  9233. + int err = 0;
  9234. +
  9235. + memset(buf, '\0', sizeof(buf));
  9236. + if (copy_from_user(buf, ubuf, csize))
  9237. + return -EFAULT;
  9238. +
  9239. + buf[U64STR_SIZE-1] = '\0'; /* just in case */
  9240. + err = kstrtoull(buf, 10, &val);
  9241. + if (err)
  9242. + return -EINVAL;
  9243. +
  9244. + mutex_lock(&data.lock);
  9245. + if (val < data.sample_window)
  9246. + data.sample_width = val;
  9247. + else {
  9248. + mutex_unlock(&data.lock);
  9249. + return -EINVAL;
  9250. + }
  9251. + mutex_unlock(&data.lock);
  9252. +
  9253. + if (enabled)
  9254. + wake_up_process(kthread);
  9255. +
  9256. + return csize;
  9257. +}
  9258. +
  9259. +/**
  9260. + * debug_window_fopen - Open function for "window" debugfs entry
  9261. + * @inode: The in-kernel inode representation of the debugfs "file"
  9262. + * @filp: The active open file structure for the debugfs "file"
  9263. + *
  9264. + * This function provides an open implementation for the "window" debugfs
  9265. + * interface to the hardware latency detector. The window is the total time
  9266. + * in us that will be considered one sample period. Conceptually, windows
  9267. + * occur back-to-back and contain a sample width period during which
  9268. + * actual sampling occurs.
  9269. + */
  9270. +static int debug_window_fopen(struct inode *inode, struct file *filp)
  9271. +{
  9272. + return 0;
  9273. +}
  9274. +
  9275. +/**
  9276. + * debug_window_fread - Read function for "window" debugfs entry
  9277. + * @filp: The active open file structure for the debugfs "file"
  9278. + * @ubuf: The userspace provided buffer to read value into
  9279. + * @cnt: The maximum number of bytes to read
  9280. + * @ppos: The current "file" position
  9281. + *
  9282. + * This function provides a read implementation for the "window" debugfs
  9283. + * interface to the hardware latency detector. The window is the total time
  9284. + * in us that will be considered one sample period. Conceptually, windows
  9285. + * occur back-to-back and contain a sample width period during which
  9286. + * actual sampling occurs. Can be used to read the total window size.
  9287. + */
  9288. +static ssize_t debug_window_fread(struct file *filp, char __user *ubuf,
  9289. + size_t cnt, loff_t *ppos)
  9290. +{
  9291. + return simple_data_read(filp, ubuf, cnt, ppos, &data.sample_window);
  9292. +}
  9293. +
  9294. +/**
  9295. + * debug_window_fwrite - Write function for "window" debugfs entry
  9296. + * @filp: The active open file structure for the debugfs "file"
  9297. + * @ubuf: The user buffer that contains the value to write
  9298. + * @cnt: The maximum number of bytes to write to "file"
  9299. + * @ppos: The current position in the debugfs "file"
  9300. + *
  9301. + * This function provides a write implementation for the "window" debufds
  9302. + * interface to the hardware latency detetector. The window is the total time
  9303. + * in us that will be considered one sample period. Conceptually, windows
  9304. + * occur back-to-back and contain a sample width period during which
  9305. + * actual sampling occurs. Can be used to write a new total window size. It
  9306. + * is enfoced that any value written must be greater than the sample width
  9307. + * size, or an error results.
  9308. + */
  9309. +static ssize_t debug_window_fwrite(struct file *filp,
  9310. + const char __user *ubuf,
  9311. + size_t cnt,
  9312. + loff_t *ppos)
  9313. +{
  9314. + char buf[U64STR_SIZE];
  9315. + int csize = min(cnt, sizeof(buf));
  9316. + u64 val = 0;
  9317. + int err = 0;
  9318. +
  9319. + memset(buf, '\0', sizeof(buf));
  9320. + if (copy_from_user(buf, ubuf, csize))
  9321. + return -EFAULT;
  9322. +
  9323. + buf[U64STR_SIZE-1] = '\0'; /* just in case */
  9324. + err = kstrtoull(buf, 10, &val);
  9325. + if (err)
  9326. + return -EINVAL;
  9327. +
  9328. + mutex_lock(&data.lock);
  9329. + if (data.sample_width < val)
  9330. + data.sample_window = val;
  9331. + else {
  9332. + mutex_unlock(&data.lock);
  9333. + return -EINVAL;
  9334. + }
  9335. + mutex_unlock(&data.lock);
  9336. +
  9337. + return csize;
  9338. +}
  9339. +
  9340. +/*
  9341. + * Function pointers for the "count" debugfs file operations
  9342. + */
  9343. +static const struct file_operations count_fops = {
  9344. + .open = debug_count_fopen,
  9345. + .read = debug_count_fread,
  9346. + .write = debug_count_fwrite,
  9347. + .owner = THIS_MODULE,
  9348. +};
  9349. +
  9350. +/*
  9351. + * Function pointers for the "enable" debugfs file operations
  9352. + */
  9353. +static const struct file_operations enable_fops = {
  9354. + .open = debug_enable_fopen,
  9355. + .read = debug_enable_fread,
  9356. + .write = debug_enable_fwrite,
  9357. + .owner = THIS_MODULE,
  9358. +};
  9359. +
  9360. +/*
  9361. + * Function pointers for the "max" debugfs file operations
  9362. + */
  9363. +static const struct file_operations max_fops = {
  9364. + .open = debug_max_fopen,
  9365. + .read = debug_max_fread,
  9366. + .write = debug_max_fwrite,
  9367. + .owner = THIS_MODULE,
  9368. +};
  9369. +
  9370. +/*
  9371. + * Function pointers for the "sample" debugfs file operations
  9372. + */
  9373. +static const struct file_operations sample_fops = {
  9374. + .open = debug_sample_fopen,
  9375. + .read = debug_sample_fread,
  9376. + .release = debug_sample_release,
  9377. + .owner = THIS_MODULE,
  9378. +};
  9379. +
  9380. +/*
  9381. + * Function pointers for the "threshold" debugfs file operations
  9382. + */
  9383. +static const struct file_operations threshold_fops = {
  9384. + .open = debug_threshold_fopen,
  9385. + .read = debug_threshold_fread,
  9386. + .write = debug_threshold_fwrite,
  9387. + .owner = THIS_MODULE,
  9388. +};
  9389. +
  9390. +/*
  9391. + * Function pointers for the "width" debugfs file operations
  9392. + */
  9393. +static const struct file_operations width_fops = {
  9394. + .open = debug_width_fopen,
  9395. + .read = debug_width_fread,
  9396. + .write = debug_width_fwrite,
  9397. + .owner = THIS_MODULE,
  9398. +};
  9399. +
  9400. +/*
  9401. + * Function pointers for the "window" debugfs file operations
  9402. + */
  9403. +static const struct file_operations window_fops = {
  9404. + .open = debug_window_fopen,
  9405. + .read = debug_window_fread,
  9406. + .write = debug_window_fwrite,
  9407. + .owner = THIS_MODULE,
  9408. +};
  9409. +
  9410. +/**
  9411. + * init_debugfs - A function to initialize the debugfs interface files
  9412. + *
  9413. + * This function creates entries in debugfs for "hwlat_detector", including
  9414. + * files to read values from the detector, current samples, and the
  9415. + * maximum sample that has been captured since the hardware latency
  9416. + * dectector was started.
  9417. + */
  9418. +static int init_debugfs(void)
  9419. +{
  9420. + int ret = -ENOMEM;
  9421. +
  9422. + debug_dir = debugfs_create_dir(DRVNAME, NULL);
  9423. + if (!debug_dir)
  9424. + goto err_debug_dir;
  9425. +
  9426. + debug_sample = debugfs_create_file("sample", 0444,
  9427. + debug_dir, NULL,
  9428. + &sample_fops);
  9429. + if (!debug_sample)
  9430. + goto err_sample;
  9431. +
  9432. + debug_count = debugfs_create_file("count", 0444,
  9433. + debug_dir, NULL,
  9434. + &count_fops);
  9435. + if (!debug_count)
  9436. + goto err_count;
  9437. +
  9438. + debug_max = debugfs_create_file("max", 0444,
  9439. + debug_dir, NULL,
  9440. + &max_fops);
  9441. + if (!debug_max)
  9442. + goto err_max;
  9443. +
  9444. + debug_sample_window = debugfs_create_file("window", 0644,
  9445. + debug_dir, NULL,
  9446. + &window_fops);
  9447. + if (!debug_sample_window)
  9448. + goto err_window;
  9449. +
  9450. + debug_sample_width = debugfs_create_file("width", 0644,
  9451. + debug_dir, NULL,
  9452. + &width_fops);
  9453. + if (!debug_sample_width)
  9454. + goto err_width;
  9455. +
  9456. + debug_threshold = debugfs_create_file("threshold", 0644,
  9457. + debug_dir, NULL,
  9458. + &threshold_fops);
  9459. + if (!debug_threshold)
  9460. + goto err_threshold;
  9461. +
  9462. + debug_enable = debugfs_create_file("enable", 0644,
  9463. + debug_dir, &enabled,
  9464. + &enable_fops);
  9465. + if (!debug_enable)
  9466. + goto err_enable;
  9467. +
  9468. + else {
  9469. + ret = 0;
  9470. + goto out;
  9471. + }
  9472. +
  9473. +err_enable:
  9474. + debugfs_remove(debug_threshold);
  9475. +err_threshold:
  9476. + debugfs_remove(debug_sample_width);
  9477. +err_width:
  9478. + debugfs_remove(debug_sample_window);
  9479. +err_window:
  9480. + debugfs_remove(debug_max);
  9481. +err_max:
  9482. + debugfs_remove(debug_count);
  9483. +err_count:
  9484. + debugfs_remove(debug_sample);
  9485. +err_sample:
  9486. + debugfs_remove(debug_dir);
  9487. +err_debug_dir:
  9488. +out:
  9489. + return ret;
  9490. +}
  9491. +
  9492. +/**
  9493. + * free_debugfs - A function to cleanup the debugfs file interface
  9494. + */
  9495. +static void free_debugfs(void)
  9496. +{
  9497. + /* could also use a debugfs_remove_recursive */
  9498. + debugfs_remove(debug_enable);
  9499. + debugfs_remove(debug_threshold);
  9500. + debugfs_remove(debug_sample_width);
  9501. + debugfs_remove(debug_sample_window);
  9502. + debugfs_remove(debug_max);
  9503. + debugfs_remove(debug_count);
  9504. + debugfs_remove(debug_sample);
  9505. + debugfs_remove(debug_dir);
  9506. +}
  9507. +
  9508. +/**
  9509. + * detector_init - Standard module initialization code
  9510. + */
  9511. +static int detector_init(void)
  9512. +{
  9513. + int ret = -ENOMEM;
  9514. +
  9515. + pr_info(BANNER "version %s\n", VERSION);
  9516. +
  9517. + ret = init_stats();
  9518. + if (ret)
  9519. + goto out;
  9520. +
  9521. + ret = init_debugfs();
  9522. + if (ret)
  9523. + goto err_stats;
  9524. +
  9525. + if (enabled)
  9526. + ret = start_kthread();
  9527. +
  9528. + goto out;
  9529. +
  9530. +err_stats:
  9531. + ring_buffer_free(ring_buffer);
  9532. +out:
  9533. + return ret;
  9534. +
  9535. +}
  9536. +
  9537. +/**
  9538. + * detector_exit - Standard module cleanup code
  9539. + */
  9540. +static void detector_exit(void)
  9541. +{
  9542. + int err;
  9543. +
  9544. + if (enabled) {
  9545. + enabled = 0;
  9546. + err = stop_kthread();
  9547. + if (err)
  9548. + pr_err(BANNER "cannot stop kthread\n");
  9549. + }
  9550. +
  9551. + free_debugfs();
  9552. + ring_buffer_free(ring_buffer); /* free up the ring buffer */
  9553. +
  9554. +}
  9555. +
  9556. +module_init(detector_init);
  9557. +module_exit(detector_exit);
  9558. diff --git a/drivers/mmc/host/mmci.c b/drivers/mmc/host/mmci.c
  9559. index acece3299756..58ea04a03fa9 100644
  9560. --- a/drivers/mmc/host/mmci.c
  9561. +++ b/drivers/mmc/host/mmci.c
  9562. @@ -1155,15 +1155,12 @@ static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
  9563. struct sg_mapping_iter *sg_miter = &host->sg_miter;
  9564. struct variant_data *variant = host->variant;
  9565. void __iomem *base = host->base;
  9566. - unsigned long flags;
  9567. u32 status;
  9568. status = readl(base + MMCISTATUS);
  9569. dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
  9570. - local_irq_save(flags);
  9571. -
  9572. do {
  9573. unsigned int remain, len;
  9574. char *buffer;
  9575. @@ -1203,8 +1200,6 @@ static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
  9576. sg_miter_stop(sg_miter);
  9577. - local_irq_restore(flags);
  9578. -
  9579. /*
  9580. * If we have less than the fifo 'half-full' threshold to transfer,
  9581. * trigger a PIO interrupt as soon as any data is available.
  9582. diff --git a/drivers/net/ethernet/3com/3c59x.c b/drivers/net/ethernet/3com/3c59x.c
  9583. index 2839af00f20c..4348b9c850d3 100644
  9584. --- a/drivers/net/ethernet/3com/3c59x.c
  9585. +++ b/drivers/net/ethernet/3com/3c59x.c
  9586. @@ -842,9 +842,9 @@ static void poll_vortex(struct net_device *dev)
  9587. {
  9588. struct vortex_private *vp = netdev_priv(dev);
  9589. unsigned long flags;
  9590. - local_irq_save(flags);
  9591. + local_irq_save_nort(flags);
  9592. (vp->full_bus_master_rx ? boomerang_interrupt:vortex_interrupt)(dev->irq,dev);
  9593. - local_irq_restore(flags);
  9594. + local_irq_restore_nort(flags);
  9595. }
  9596. #endif
  9597. @@ -1916,12 +1916,12 @@ static void vortex_tx_timeout(struct net_device *dev)
  9598. * Block interrupts because vortex_interrupt does a bare spin_lock()
  9599. */
  9600. unsigned long flags;
  9601. - local_irq_save(flags);
  9602. + local_irq_save_nort(flags);
  9603. if (vp->full_bus_master_tx)
  9604. boomerang_interrupt(dev->irq, dev);
  9605. else
  9606. vortex_interrupt(dev->irq, dev);
  9607. - local_irq_restore(flags);
  9608. + local_irq_restore_nort(flags);
  9609. }
  9610. }
  9611. diff --git a/drivers/net/ethernet/atheros/atl1c/atl1c_main.c b/drivers/net/ethernet/atheros/atl1c/atl1c_main.c
  9612. index 8b5988e210d5..cf9928ccdd7e 100644
  9613. --- a/drivers/net/ethernet/atheros/atl1c/atl1c_main.c
  9614. +++ b/drivers/net/ethernet/atheros/atl1c/atl1c_main.c
  9615. @@ -2221,11 +2221,7 @@ static netdev_tx_t atl1c_xmit_frame(struct sk_buff *skb,
  9616. }
  9617. tpd_req = atl1c_cal_tpd_req(skb);
  9618. - if (!spin_trylock_irqsave(&adapter->tx_lock, flags)) {
  9619. - if (netif_msg_pktdata(adapter))
  9620. - dev_info(&adapter->pdev->dev, "tx locked\n");
  9621. - return NETDEV_TX_LOCKED;
  9622. - }
  9623. + spin_lock_irqsave(&adapter->tx_lock, flags);
  9624. if (atl1c_tpd_avail(adapter, type) < tpd_req) {
  9625. /* no enough descriptor, just stop queue */
  9626. diff --git a/drivers/net/ethernet/atheros/atl1e/atl1e_main.c b/drivers/net/ethernet/atheros/atl1e/atl1e_main.c
  9627. index 59a03a193e83..734f7a7ad2c3 100644
  9628. --- a/drivers/net/ethernet/atheros/atl1e/atl1e_main.c
  9629. +++ b/drivers/net/ethernet/atheros/atl1e/atl1e_main.c
  9630. @@ -1880,8 +1880,7 @@ static netdev_tx_t atl1e_xmit_frame(struct sk_buff *skb,
  9631. return NETDEV_TX_OK;
  9632. }
  9633. tpd_req = atl1e_cal_tdp_req(skb);
  9634. - if (!spin_trylock_irqsave(&adapter->tx_lock, flags))
  9635. - return NETDEV_TX_LOCKED;
  9636. + spin_lock_irqsave(&adapter->tx_lock, flags);
  9637. if (atl1e_tpd_avail(adapter) < tpd_req) {
  9638. /* no enough descriptor, just stop queue */
  9639. diff --git a/drivers/net/ethernet/chelsio/cxgb/sge.c b/drivers/net/ethernet/chelsio/cxgb/sge.c
  9640. index 526ea74e82d9..86f467a2c485 100644
  9641. --- a/drivers/net/ethernet/chelsio/cxgb/sge.c
  9642. +++ b/drivers/net/ethernet/chelsio/cxgb/sge.c
  9643. @@ -1664,8 +1664,7 @@ static int t1_sge_tx(struct sk_buff *skb, struct adapter *adapter,
  9644. struct cmdQ *q = &sge->cmdQ[qid];
  9645. unsigned int credits, pidx, genbit, count, use_sched_skb = 0;
  9646. - if (!spin_trylock(&q->lock))
  9647. - return NETDEV_TX_LOCKED;
  9648. + spin_lock(&q->lock);
  9649. reclaim_completed_tx(sge, q);
  9650. diff --git a/drivers/net/ethernet/neterion/s2io.c b/drivers/net/ethernet/neterion/s2io.c
  9651. index 9ba975853ec6..813cfa698160 100644
  9652. --- a/drivers/net/ethernet/neterion/s2io.c
  9653. +++ b/drivers/net/ethernet/neterion/s2io.c
  9654. @@ -4084,12 +4084,7 @@ static netdev_tx_t s2io_xmit(struct sk_buff *skb, struct net_device *dev)
  9655. [skb->priority & (MAX_TX_FIFOS - 1)];
  9656. fifo = &mac_control->fifos[queue];
  9657. - if (do_spin_lock)
  9658. - spin_lock_irqsave(&fifo->tx_lock, flags);
  9659. - else {
  9660. - if (unlikely(!spin_trylock_irqsave(&fifo->tx_lock, flags)))
  9661. - return NETDEV_TX_LOCKED;
  9662. - }
  9663. + spin_lock_irqsave(&fifo->tx_lock, flags);
  9664. if (sp->config.multiq) {
  9665. if (__netif_subqueue_stopped(dev, fifo->fifo_no)) {
  9666. diff --git a/drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c b/drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c
  9667. index 3b98b263bad0..ca4add749410 100644
  9668. --- a/drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c
  9669. +++ b/drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c
  9670. @@ -2137,10 +2137,8 @@ static int pch_gbe_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
  9671. struct pch_gbe_tx_ring *tx_ring = adapter->tx_ring;
  9672. unsigned long flags;
  9673. - if (!spin_trylock_irqsave(&tx_ring->tx_lock, flags)) {
  9674. - /* Collision - tell upper layer to requeue */
  9675. - return NETDEV_TX_LOCKED;
  9676. - }
  9677. + spin_lock_irqsave(&tx_ring->tx_lock, flags);
  9678. +
  9679. if (unlikely(!PCH_GBE_DESC_UNUSED(tx_ring))) {
  9680. netif_stop_queue(netdev);
  9681. spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
  9682. diff --git a/drivers/net/ethernet/realtek/8139too.c b/drivers/net/ethernet/realtek/8139too.c
  9683. index ef668d300800..d987d571fdd6 100644
  9684. --- a/drivers/net/ethernet/realtek/8139too.c
  9685. +++ b/drivers/net/ethernet/realtek/8139too.c
  9686. @@ -2229,7 +2229,7 @@ static void rtl8139_poll_controller(struct net_device *dev)
  9687. struct rtl8139_private *tp = netdev_priv(dev);
  9688. const int irq = tp->pci_dev->irq;
  9689. - disable_irq(irq);
  9690. + disable_irq_nosync(irq);
  9691. rtl8139_interrupt(irq, dev);
  9692. enable_irq(irq);
  9693. }
  9694. diff --git a/drivers/net/ethernet/tehuti/tehuti.c b/drivers/net/ethernet/tehuti/tehuti.c
  9695. index 14c9d1baa85c..e1a5305418a8 100644
  9696. --- a/drivers/net/ethernet/tehuti/tehuti.c
  9697. +++ b/drivers/net/ethernet/tehuti/tehuti.c
  9698. @@ -1629,13 +1629,8 @@ static netdev_tx_t bdx_tx_transmit(struct sk_buff *skb,
  9699. unsigned long flags;
  9700. ENTER;
  9701. - local_irq_save(flags);
  9702. - if (!spin_trylock(&priv->tx_lock)) {
  9703. - local_irq_restore(flags);
  9704. - DBG("%s[%s]: TX locked, returning NETDEV_TX_LOCKED\n",
  9705. - BDX_DRV_NAME, ndev->name);
  9706. - return NETDEV_TX_LOCKED;
  9707. - }
  9708. +
  9709. + spin_lock_irqsave(&priv->tx_lock, flags);
  9710. /* build tx descriptor */
  9711. BDX_ASSERT(f->m.wptr >= f->m.memsz); /* started with valid wptr */
  9712. diff --git a/drivers/net/rionet.c b/drivers/net/rionet.c
  9713. index e7034c55e796..2e4ee0f912bf 100644
  9714. --- a/drivers/net/rionet.c
  9715. +++ b/drivers/net/rionet.c
  9716. @@ -174,11 +174,7 @@ static int rionet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  9717. unsigned long flags;
  9718. int add_num = 1;
  9719. - local_irq_save(flags);
  9720. - if (!spin_trylock(&rnet->tx_lock)) {
  9721. - local_irq_restore(flags);
  9722. - return NETDEV_TX_LOCKED;
  9723. - }
  9724. + spin_lock_irqsave(&rnet->tx_lock, flags);
  9725. if (is_multicast_ether_addr(eth->h_dest))
  9726. add_num = nets[rnet->mport->id].nact;
  9727. diff --git a/drivers/net/wireless/orinoco/orinoco_usb.c b/drivers/net/wireless/orinoco/orinoco_usb.c
  9728. index f2cd513d54b2..6c0f4c9638a2 100644
  9729. --- a/drivers/net/wireless/orinoco/orinoco_usb.c
  9730. +++ b/drivers/net/wireless/orinoco/orinoco_usb.c
  9731. @@ -697,7 +697,7 @@ static void ezusb_req_ctx_wait(struct ezusb_priv *upriv,
  9732. while (!ctx->done.done && msecs--)
  9733. udelay(1000);
  9734. } else {
  9735. - wait_event_interruptible(ctx->done.wait,
  9736. + swait_event_interruptible(ctx->done.wait,
  9737. ctx->done.done);
  9738. }
  9739. break;
  9740. diff --git a/drivers/pci/access.c b/drivers/pci/access.c
  9741. index 59ac36fe7c42..7a45a20af78a 100644
  9742. --- a/drivers/pci/access.c
  9743. +++ b/drivers/pci/access.c
  9744. @@ -561,7 +561,7 @@ void pci_cfg_access_unlock(struct pci_dev *dev)
  9745. WARN_ON(!dev->block_cfg_access);
  9746. dev->block_cfg_access = 0;
  9747. - wake_up_all(&pci_cfg_wait);
  9748. + wake_up_all_locked(&pci_cfg_wait);
  9749. raw_spin_unlock_irqrestore(&pci_lock, flags);
  9750. }
  9751. EXPORT_SYMBOL_GPL(pci_cfg_access_unlock);
  9752. diff --git a/drivers/pinctrl/qcom/pinctrl-msm.c b/drivers/pinctrl/qcom/pinctrl-msm.c
  9753. index 9736f9be5447..5fe9b173dcb3 100644
  9754. --- a/drivers/pinctrl/qcom/pinctrl-msm.c
  9755. +++ b/drivers/pinctrl/qcom/pinctrl-msm.c
  9756. @@ -60,7 +60,7 @@ struct msm_pinctrl {
  9757. struct notifier_block restart_nb;
  9758. int irq;
  9759. - spinlock_t lock;
  9760. + raw_spinlock_t lock;
  9761. DECLARE_BITMAP(dual_edge_irqs, MAX_NR_GPIO);
  9762. DECLARE_BITMAP(enabled_irqs, MAX_NR_GPIO);
  9763. @@ -156,14 +156,14 @@ static int msm_pinmux_set_mux(struct pinctrl_dev *pctldev,
  9764. if (WARN_ON(i == g->nfuncs))
  9765. return -EINVAL;
  9766. - spin_lock_irqsave(&pctrl->lock, flags);
  9767. + raw_spin_lock_irqsave(&pctrl->lock, flags);
  9768. val = readl(pctrl->regs + g->ctl_reg);
  9769. val &= ~(0x7 << g->mux_bit);
  9770. val |= i << g->mux_bit;
  9771. writel(val, pctrl->regs + g->ctl_reg);
  9772. - spin_unlock_irqrestore(&pctrl->lock, flags);
  9773. + raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  9774. return 0;
  9775. }
  9776. @@ -326,14 +326,14 @@ static int msm_config_group_set(struct pinctrl_dev *pctldev,
  9777. break;
  9778. case PIN_CONFIG_OUTPUT:
  9779. /* set output value */
  9780. - spin_lock_irqsave(&pctrl->lock, flags);
  9781. + raw_spin_lock_irqsave(&pctrl->lock, flags);
  9782. val = readl(pctrl->regs + g->io_reg);
  9783. if (arg)
  9784. val |= BIT(g->out_bit);
  9785. else
  9786. val &= ~BIT(g->out_bit);
  9787. writel(val, pctrl->regs + g->io_reg);
  9788. - spin_unlock_irqrestore(&pctrl->lock, flags);
  9789. + raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  9790. /* enable output */
  9791. arg = 1;
  9792. @@ -354,12 +354,12 @@ static int msm_config_group_set(struct pinctrl_dev *pctldev,
  9793. return -EINVAL;
  9794. }
  9795. - spin_lock_irqsave(&pctrl->lock, flags);
  9796. + raw_spin_lock_irqsave(&pctrl->lock, flags);
  9797. val = readl(pctrl->regs + g->ctl_reg);
  9798. val &= ~(mask << bit);
  9799. val |= arg << bit;
  9800. writel(val, pctrl->regs + g->ctl_reg);
  9801. - spin_unlock_irqrestore(&pctrl->lock, flags);
  9802. + raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  9803. }
  9804. return 0;
  9805. @@ -387,13 +387,13 @@ static int msm_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  9806. g = &pctrl->soc->groups[offset];
  9807. - spin_lock_irqsave(&pctrl->lock, flags);
  9808. + raw_spin_lock_irqsave(&pctrl->lock, flags);
  9809. val = readl(pctrl->regs + g->ctl_reg);
  9810. val &= ~BIT(g->oe_bit);
  9811. writel(val, pctrl->regs + g->ctl_reg);
  9812. - spin_unlock_irqrestore(&pctrl->lock, flags);
  9813. + raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  9814. return 0;
  9815. }
  9816. @@ -407,7 +407,7 @@ static int msm_gpio_direction_output(struct gpio_chip *chip, unsigned offset, in
  9817. g = &pctrl->soc->groups[offset];
  9818. - spin_lock_irqsave(&pctrl->lock, flags);
  9819. + raw_spin_lock_irqsave(&pctrl->lock, flags);
  9820. val = readl(pctrl->regs + g->io_reg);
  9821. if (value)
  9822. @@ -420,7 +420,7 @@ static int msm_gpio_direction_output(struct gpio_chip *chip, unsigned offset, in
  9823. val |= BIT(g->oe_bit);
  9824. writel(val, pctrl->regs + g->ctl_reg);
  9825. - spin_unlock_irqrestore(&pctrl->lock, flags);
  9826. + raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  9827. return 0;
  9828. }
  9829. @@ -446,7 +446,7 @@ static void msm_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  9830. g = &pctrl->soc->groups[offset];
  9831. - spin_lock_irqsave(&pctrl->lock, flags);
  9832. + raw_spin_lock_irqsave(&pctrl->lock, flags);
  9833. val = readl(pctrl->regs + g->io_reg);
  9834. if (value)
  9835. @@ -455,7 +455,7 @@ static void msm_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  9836. val &= ~BIT(g->out_bit);
  9837. writel(val, pctrl->regs + g->io_reg);
  9838. - spin_unlock_irqrestore(&pctrl->lock, flags);
  9839. + raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  9840. }
  9841. #ifdef CONFIG_DEBUG_FS
  9842. @@ -574,7 +574,7 @@ static void msm_gpio_irq_mask(struct irq_data *d)
  9843. g = &pctrl->soc->groups[d->hwirq];
  9844. - spin_lock_irqsave(&pctrl->lock, flags);
  9845. + raw_spin_lock_irqsave(&pctrl->lock, flags);
  9846. val = readl(pctrl->regs + g->intr_cfg_reg);
  9847. val &= ~BIT(g->intr_enable_bit);
  9848. @@ -582,7 +582,7 @@ static void msm_gpio_irq_mask(struct irq_data *d)
  9849. clear_bit(d->hwirq, pctrl->enabled_irqs);
  9850. - spin_unlock_irqrestore(&pctrl->lock, flags);
  9851. + raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  9852. }
  9853. static void msm_gpio_irq_unmask(struct irq_data *d)
  9854. @@ -595,7 +595,7 @@ static void msm_gpio_irq_unmask(struct irq_data *d)
  9855. g = &pctrl->soc->groups[d->hwirq];
  9856. - spin_lock_irqsave(&pctrl->lock, flags);
  9857. + raw_spin_lock_irqsave(&pctrl->lock, flags);
  9858. val = readl(pctrl->regs + g->intr_cfg_reg);
  9859. val |= BIT(g->intr_enable_bit);
  9860. @@ -603,7 +603,7 @@ static void msm_gpio_irq_unmask(struct irq_data *d)
  9861. set_bit(d->hwirq, pctrl->enabled_irqs);
  9862. - spin_unlock_irqrestore(&pctrl->lock, flags);
  9863. + raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  9864. }
  9865. static void msm_gpio_irq_ack(struct irq_data *d)
  9866. @@ -616,7 +616,7 @@ static void msm_gpio_irq_ack(struct irq_data *d)
  9867. g = &pctrl->soc->groups[d->hwirq];
  9868. - spin_lock_irqsave(&pctrl->lock, flags);
  9869. + raw_spin_lock_irqsave(&pctrl->lock, flags);
  9870. val = readl(pctrl->regs + g->intr_status_reg);
  9871. if (g->intr_ack_high)
  9872. @@ -628,7 +628,7 @@ static void msm_gpio_irq_ack(struct irq_data *d)
  9873. if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
  9874. msm_gpio_update_dual_edge_pos(pctrl, g, d);
  9875. - spin_unlock_irqrestore(&pctrl->lock, flags);
  9876. + raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  9877. }
  9878. static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  9879. @@ -641,7 +641,7 @@ static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  9880. g = &pctrl->soc->groups[d->hwirq];
  9881. - spin_lock_irqsave(&pctrl->lock, flags);
  9882. + raw_spin_lock_irqsave(&pctrl->lock, flags);
  9883. /*
  9884. * For hw without possibility of detecting both edges
  9885. @@ -715,7 +715,7 @@ static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  9886. if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
  9887. msm_gpio_update_dual_edge_pos(pctrl, g, d);
  9888. - spin_unlock_irqrestore(&pctrl->lock, flags);
  9889. + raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  9890. if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
  9891. irq_set_handler_locked(d, handle_level_irq);
  9892. @@ -731,11 +731,11 @@ static int msm_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
  9893. struct msm_pinctrl *pctrl = to_msm_pinctrl(gc);
  9894. unsigned long flags;
  9895. - spin_lock_irqsave(&pctrl->lock, flags);
  9896. + raw_spin_lock_irqsave(&pctrl->lock, flags);
  9897. irq_set_irq_wake(pctrl->irq, on);
  9898. - spin_unlock_irqrestore(&pctrl->lock, flags);
  9899. + raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  9900. return 0;
  9901. }
  9902. @@ -881,7 +881,7 @@ int msm_pinctrl_probe(struct platform_device *pdev,
  9903. pctrl->soc = soc_data;
  9904. pctrl->chip = msm_gpio_template;
  9905. - spin_lock_init(&pctrl->lock);
  9906. + raw_spin_lock_init(&pctrl->lock);
  9907. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  9908. pctrl->regs = devm_ioremap_resource(&pdev->dev, res);
  9909. diff --git a/drivers/scsi/fcoe/fcoe.c b/drivers/scsi/fcoe/fcoe.c
  9910. index f4424063b860..cbbbebd86c6e 100644
  9911. --- a/drivers/scsi/fcoe/fcoe.c
  9912. +++ b/drivers/scsi/fcoe/fcoe.c
  9913. @@ -1286,7 +1286,7 @@ static void fcoe_percpu_thread_destroy(unsigned int cpu)
  9914. struct sk_buff *skb;
  9915. #ifdef CONFIG_SMP
  9916. struct fcoe_percpu_s *p0;
  9917. - unsigned targ_cpu = get_cpu();
  9918. + unsigned targ_cpu = get_cpu_light();
  9919. #endif /* CONFIG_SMP */
  9920. FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
  9921. @@ -1342,7 +1342,7 @@ static void fcoe_percpu_thread_destroy(unsigned int cpu)
  9922. kfree_skb(skb);
  9923. spin_unlock_bh(&p->fcoe_rx_list.lock);
  9924. }
  9925. - put_cpu();
  9926. + put_cpu_light();
  9927. #else
  9928. /*
  9929. * This a non-SMP scenario where the singular Rx thread is
  9930. @@ -1566,11 +1566,11 @@ err2:
  9931. static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
  9932. {
  9933. struct fcoe_percpu_s *fps;
  9934. - int rc;
  9935. + int rc, cpu = get_cpu_light();
  9936. - fps = &get_cpu_var(fcoe_percpu);
  9937. + fps = &per_cpu(fcoe_percpu, cpu);
  9938. rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
  9939. - put_cpu_var(fcoe_percpu);
  9940. + put_cpu_light();
  9941. return rc;
  9942. }
  9943. @@ -1766,11 +1766,11 @@ static inline int fcoe_filter_frames(struct fc_lport *lport,
  9944. return 0;
  9945. }
  9946. - stats = per_cpu_ptr(lport->stats, get_cpu());
  9947. + stats = per_cpu_ptr(lport->stats, get_cpu_light());
  9948. stats->InvalidCRCCount++;
  9949. if (stats->InvalidCRCCount < 5)
  9950. printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
  9951. - put_cpu();
  9952. + put_cpu_light();
  9953. return -EINVAL;
  9954. }
  9955. @@ -1814,7 +1814,7 @@ static void fcoe_recv_frame(struct sk_buff *skb)
  9956. */
  9957. hp = (struct fcoe_hdr *) skb_network_header(skb);
  9958. - stats = per_cpu_ptr(lport->stats, get_cpu());
  9959. + stats = per_cpu_ptr(lport->stats, get_cpu_light());
  9960. if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
  9961. if (stats->ErrorFrames < 5)
  9962. printk(KERN_WARNING "fcoe: FCoE version "
  9963. @@ -1846,13 +1846,13 @@ static void fcoe_recv_frame(struct sk_buff *skb)
  9964. goto drop;
  9965. if (!fcoe_filter_frames(lport, fp)) {
  9966. - put_cpu();
  9967. + put_cpu_light();
  9968. fc_exch_recv(lport, fp);
  9969. return;
  9970. }
  9971. drop:
  9972. stats->ErrorFrames++;
  9973. - put_cpu();
  9974. + put_cpu_light();
  9975. kfree_skb(skb);
  9976. }
  9977. diff --git a/drivers/scsi/fcoe/fcoe_ctlr.c b/drivers/scsi/fcoe/fcoe_ctlr.c
  9978. index 34a1b1f333b4..d91131210695 100644
  9979. --- a/drivers/scsi/fcoe/fcoe_ctlr.c
  9980. +++ b/drivers/scsi/fcoe/fcoe_ctlr.c
  9981. @@ -831,7 +831,7 @@ static unsigned long fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
  9982. INIT_LIST_HEAD(&del_list);
  9983. - stats = per_cpu_ptr(fip->lp->stats, get_cpu());
  9984. + stats = per_cpu_ptr(fip->lp->stats, get_cpu_light());
  9985. list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
  9986. deadline = fcf->time + fcf->fka_period + fcf->fka_period / 2;
  9987. @@ -867,7 +867,7 @@ static unsigned long fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
  9988. sel_time = fcf->time;
  9989. }
  9990. }
  9991. - put_cpu();
  9992. + put_cpu_light();
  9993. list_for_each_entry_safe(fcf, next, &del_list, list) {
  9994. /* Removes fcf from current list */
  9995. diff --git a/drivers/scsi/libfc/fc_exch.c b/drivers/scsi/libfc/fc_exch.c
  9996. index 30f9ef0c0d4f..6c686bc01a82 100644
  9997. --- a/drivers/scsi/libfc/fc_exch.c
  9998. +++ b/drivers/scsi/libfc/fc_exch.c
  9999. @@ -814,10 +814,10 @@ static struct fc_exch *fc_exch_em_alloc(struct fc_lport *lport,
  10000. }
  10001. memset(ep, 0, sizeof(*ep));
  10002. - cpu = get_cpu();
  10003. + cpu = get_cpu_light();
  10004. pool = per_cpu_ptr(mp->pool, cpu);
  10005. spin_lock_bh(&pool->lock);
  10006. - put_cpu();
  10007. + put_cpu_light();
  10008. /* peek cache of free slot */
  10009. if (pool->left != FC_XID_UNKNOWN) {
  10010. diff --git a/drivers/scsi/libsas/sas_ata.c b/drivers/scsi/libsas/sas_ata.c
  10011. index 6f5e2720ffad..ee8a8ed49b89 100644
  10012. --- a/drivers/scsi/libsas/sas_ata.c
  10013. +++ b/drivers/scsi/libsas/sas_ata.c
  10014. @@ -190,7 +190,7 @@ static unsigned int sas_ata_qc_issue(struct ata_queued_cmd *qc)
  10015. /* TODO: audit callers to ensure they are ready for qc_issue to
  10016. * unconditionally re-enable interrupts
  10017. */
  10018. - local_irq_save(flags);
  10019. + local_irq_save_nort(flags);
  10020. spin_unlock(ap->lock);
  10021. /* If the device fell off, no sense in issuing commands */
  10022. @@ -255,7 +255,7 @@ static unsigned int sas_ata_qc_issue(struct ata_queued_cmd *qc)
  10023. out:
  10024. spin_lock(ap->lock);
  10025. - local_irq_restore(flags);
  10026. + local_irq_restore_nort(flags);
  10027. return ret;
  10028. }
  10029. diff --git a/drivers/scsi/qla2xxx/qla_inline.h b/drivers/scsi/qla2xxx/qla_inline.h
  10030. index fee9eb7c8a60..b42d4adc42dc 100644
  10031. --- a/drivers/scsi/qla2xxx/qla_inline.h
  10032. +++ b/drivers/scsi/qla2xxx/qla_inline.h
  10033. @@ -59,12 +59,12 @@ qla2x00_poll(struct rsp_que *rsp)
  10034. {
  10035. unsigned long flags;
  10036. struct qla_hw_data *ha = rsp->hw;
  10037. - local_irq_save(flags);
  10038. + local_irq_save_nort(flags);
  10039. if (IS_P3P_TYPE(ha))
  10040. qla82xx_poll(0, rsp);
  10041. else
  10042. ha->isp_ops->intr_handler(0, rsp);
  10043. - local_irq_restore(flags);
  10044. + local_irq_restore_nort(flags);
  10045. }
  10046. static inline uint8_t *
  10047. diff --git a/drivers/thermal/x86_pkg_temp_thermal.c b/drivers/thermal/x86_pkg_temp_thermal.c
  10048. index 7fc919f7da4d..e03fa17b8670 100644
  10049. --- a/drivers/thermal/x86_pkg_temp_thermal.c
  10050. +++ b/drivers/thermal/x86_pkg_temp_thermal.c
  10051. @@ -29,6 +29,7 @@
  10052. #include <linux/pm.h>
  10053. #include <linux/thermal.h>
  10054. #include <linux/debugfs.h>
  10055. +#include <linux/swork.h>
  10056. #include <asm/cpu_device_id.h>
  10057. #include <asm/mce.h>
  10058. @@ -352,7 +353,7 @@ static void pkg_temp_thermal_threshold_work_fn(struct work_struct *work)
  10059. }
  10060. }
  10061. -static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  10062. +static void platform_thermal_notify_work(struct swork_event *event)
  10063. {
  10064. unsigned long flags;
  10065. int cpu = smp_processor_id();
  10066. @@ -369,7 +370,7 @@ static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  10067. pkg_work_scheduled[phy_id]) {
  10068. disable_pkg_thres_interrupt();
  10069. spin_unlock_irqrestore(&pkg_work_lock, flags);
  10070. - return -EINVAL;
  10071. + return;
  10072. }
  10073. pkg_work_scheduled[phy_id] = 1;
  10074. spin_unlock_irqrestore(&pkg_work_lock, flags);
  10075. @@ -378,9 +379,48 @@ static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  10076. schedule_delayed_work_on(cpu,
  10077. &per_cpu(pkg_temp_thermal_threshold_work, cpu),
  10078. msecs_to_jiffies(notify_delay_ms));
  10079. +}
  10080. +
  10081. +#ifdef CONFIG_PREEMPT_RT_FULL
  10082. +static struct swork_event notify_work;
  10083. +
  10084. +static int thermal_notify_work_init(void)
  10085. +{
  10086. + int err;
  10087. +
  10088. + err = swork_get();
  10089. + if (err)
  10090. + return err;
  10091. +
  10092. + INIT_SWORK(&notify_work, platform_thermal_notify_work);
  10093. return 0;
  10094. }
  10095. +static void thermal_notify_work_cleanup(void)
  10096. +{
  10097. + swork_put();
  10098. +}
  10099. +
  10100. +static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  10101. +{
  10102. + swork_queue(&notify_work);
  10103. + return 0;
  10104. +}
  10105. +
  10106. +#else /* !CONFIG_PREEMPT_RT_FULL */
  10107. +
  10108. +static int thermal_notify_work_init(void) { return 0; }
  10109. +
  10110. +static void thermal_notify_work_cleanup(void) { }
  10111. +
  10112. +static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  10113. +{
  10114. + platform_thermal_notify_work(NULL);
  10115. +
  10116. + return 0;
  10117. +}
  10118. +#endif /* CONFIG_PREEMPT_RT_FULL */
  10119. +
  10120. static int find_siblings_cpu(int cpu)
  10121. {
  10122. int i;
  10123. @@ -584,6 +624,9 @@ static int __init pkg_temp_thermal_init(void)
  10124. if (!x86_match_cpu(pkg_temp_thermal_ids))
  10125. return -ENODEV;
  10126. + if (!thermal_notify_work_init())
  10127. + return -ENODEV;
  10128. +
  10129. spin_lock_init(&pkg_work_lock);
  10130. platform_thermal_package_notify =
  10131. pkg_temp_thermal_platform_thermal_notify;
  10132. @@ -608,7 +651,7 @@ err_ret:
  10133. kfree(pkg_work_scheduled);
  10134. platform_thermal_package_notify = NULL;
  10135. platform_thermal_package_rate_control = NULL;
  10136. -
  10137. + thermal_notify_work_cleanup();
  10138. return -ENODEV;
  10139. }
  10140. @@ -633,6 +676,7 @@ static void __exit pkg_temp_thermal_exit(void)
  10141. mutex_unlock(&phy_dev_list_mutex);
  10142. platform_thermal_package_notify = NULL;
  10143. platform_thermal_package_rate_control = NULL;
  10144. + thermal_notify_work_cleanup();
  10145. for_each_online_cpu(i)
  10146. cancel_delayed_work_sync(
  10147. &per_cpu(pkg_temp_thermal_threshold_work, i));
  10148. diff --git a/drivers/tty/serial/8250/8250_core.c b/drivers/tty/serial/8250/8250_core.c
  10149. index 39126460c1f5..af7701ca4d48 100644
  10150. --- a/drivers/tty/serial/8250/8250_core.c
  10151. +++ b/drivers/tty/serial/8250/8250_core.c
  10152. @@ -58,7 +58,16 @@ static struct uart_driver serial8250_reg;
  10153. static unsigned int skip_txen_test; /* force skip of txen test at init time */
  10154. -#define PASS_LIMIT 512
  10155. +/*
  10156. + * On -rt we can have a more delays, and legitimately
  10157. + * so - so don't drop work spuriously and spam the
  10158. + * syslog:
  10159. + */
  10160. +#ifdef CONFIG_PREEMPT_RT_FULL
  10161. +# define PASS_LIMIT 1000000
  10162. +#else
  10163. +# define PASS_LIMIT 512
  10164. +#endif
  10165. #include <asm/serial.h>
  10166. /*
  10167. diff --git a/drivers/tty/serial/8250/8250_port.c b/drivers/tty/serial/8250/8250_port.c
  10168. index 56ccbcefdd85..a0b9e854672c 100644
  10169. --- a/drivers/tty/serial/8250/8250_port.c
  10170. +++ b/drivers/tty/serial/8250/8250_port.c
  10171. @@ -35,6 +35,7 @@
  10172. #include <linux/nmi.h>
  10173. #include <linux/mutex.h>
  10174. #include <linux/slab.h>
  10175. +#include <linux/kdb.h>
  10176. #include <linux/uaccess.h>
  10177. #include <linux/pm_runtime.h>
  10178. @@ -2843,9 +2844,9 @@ void serial8250_console_write(struct uart_8250_port *up, const char *s,
  10179. serial8250_rpm_get(up);
  10180. - if (port->sysrq)
  10181. + if (port->sysrq || oops_in_progress)
  10182. locked = 0;
  10183. - else if (oops_in_progress)
  10184. + else if (in_kdb_printk())
  10185. locked = spin_trylock_irqsave(&port->lock, flags);
  10186. else
  10187. spin_lock_irqsave(&port->lock, flags);
  10188. diff --git a/drivers/tty/serial/amba-pl011.c b/drivers/tty/serial/amba-pl011.c
  10189. index 899a77187bde..3ff6363b3751 100644
  10190. --- a/drivers/tty/serial/amba-pl011.c
  10191. +++ b/drivers/tty/serial/amba-pl011.c
  10192. @@ -2067,13 +2067,19 @@ pl011_console_write(struct console *co, const char *s, unsigned int count)
  10193. clk_enable(uap->clk);
  10194. - local_irq_save(flags);
  10195. + /*
  10196. + * local_irq_save(flags);
  10197. + *
  10198. + * This local_irq_save() is nonsense. If we come in via sysrq
  10199. + * handling then interrupts are already disabled. Aside of
  10200. + * that the port.sysrq check is racy on SMP regardless.
  10201. + */
  10202. if (uap->port.sysrq)
  10203. locked = 0;
  10204. else if (oops_in_progress)
  10205. - locked = spin_trylock(&uap->port.lock);
  10206. + locked = spin_trylock_irqsave(&uap->port.lock, flags);
  10207. else
  10208. - spin_lock(&uap->port.lock);
  10209. + spin_lock_irqsave(&uap->port.lock, flags);
  10210. /*
  10211. * First save the CR then disable the interrupts
  10212. @@ -2098,8 +2104,7 @@ pl011_console_write(struct console *co, const char *s, unsigned int count)
  10213. writew(old_cr, uap->port.membase + UART011_CR);
  10214. if (locked)
  10215. - spin_unlock(&uap->port.lock);
  10216. - local_irq_restore(flags);
  10217. + spin_unlock_irqrestore(&uap->port.lock, flags);
  10218. clk_disable(uap->clk);
  10219. }
  10220. diff --git a/drivers/tty/serial/omap-serial.c b/drivers/tty/serial/omap-serial.c
  10221. index de1c143b475f..3fef536a1844 100644
  10222. --- a/drivers/tty/serial/omap-serial.c
  10223. +++ b/drivers/tty/serial/omap-serial.c
  10224. @@ -1257,13 +1257,10 @@ serial_omap_console_write(struct console *co, const char *s,
  10225. pm_runtime_get_sync(up->dev);
  10226. - local_irq_save(flags);
  10227. - if (up->port.sysrq)
  10228. - locked = 0;
  10229. - else if (oops_in_progress)
  10230. - locked = spin_trylock(&up->port.lock);
  10231. + if (up->port.sysrq || oops_in_progress)
  10232. + locked = spin_trylock_irqsave(&up->port.lock, flags);
  10233. else
  10234. - spin_lock(&up->port.lock);
  10235. + spin_lock_irqsave(&up->port.lock, flags);
  10236. /*
  10237. * First save the IER then disable the interrupts
  10238. @@ -1292,8 +1289,7 @@ serial_omap_console_write(struct console *co, const char *s,
  10239. pm_runtime_mark_last_busy(up->dev);
  10240. pm_runtime_put_autosuspend(up->dev);
  10241. if (locked)
  10242. - spin_unlock(&up->port.lock);
  10243. - local_irq_restore(flags);
  10244. + spin_unlock_irqrestore(&up->port.lock, flags);
  10245. }
  10246. static int __init
  10247. diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
  10248. index c3f4f2ab7b33..ae1ffb5f7610 100644
  10249. --- a/drivers/usb/core/hcd.c
  10250. +++ b/drivers/usb/core/hcd.c
  10251. @@ -1738,9 +1738,9 @@ static void __usb_hcd_giveback_urb(struct urb *urb)
  10252. * and no one may trigger the above deadlock situation when
  10253. * running complete() in tasklet.
  10254. */
  10255. - local_irq_save(flags);
  10256. + local_irq_save_nort(flags);
  10257. urb->complete(urb);
  10258. - local_irq_restore(flags);
  10259. + local_irq_restore_nort(flags);
  10260. usb_anchor_resume_wakeups(anchor);
  10261. atomic_dec(&urb->use_count);
  10262. diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c
  10263. index 9ad5145d3103..bf883695cdfb 100644
  10264. --- a/drivers/usb/gadget/function/f_fs.c
  10265. +++ b/drivers/usb/gadget/function/f_fs.c
  10266. @@ -1404,7 +1404,7 @@ static void ffs_data_put(struct ffs_data *ffs)
  10267. pr_info("%s(): freeing\n", __func__);
  10268. ffs_data_clear(ffs);
  10269. BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
  10270. - waitqueue_active(&ffs->ep0req_completion.wait));
  10271. + swait_active(&ffs->ep0req_completion.wait));
  10272. kfree(ffs->dev_name);
  10273. kfree(ffs);
  10274. }
  10275. diff --git a/drivers/usb/gadget/legacy/inode.c b/drivers/usb/gadget/legacy/inode.c
  10276. index de014436fb22..e52700c7555e 100644
  10277. --- a/drivers/usb/gadget/legacy/inode.c
  10278. +++ b/drivers/usb/gadget/legacy/inode.c
  10279. @@ -345,7 +345,7 @@ ep_io (struct ep_data *epdata, void *buf, unsigned len)
  10280. spin_unlock_irq (&epdata->dev->lock);
  10281. if (likely (value == 0)) {
  10282. - value = wait_event_interruptible (done.wait, done.done);
  10283. + value = swait_event_interruptible (done.wait, done.done);
  10284. if (value != 0) {
  10285. spin_lock_irq (&epdata->dev->lock);
  10286. if (likely (epdata->ep != NULL)) {
  10287. @@ -354,7 +354,7 @@ ep_io (struct ep_data *epdata, void *buf, unsigned len)
  10288. usb_ep_dequeue (epdata->ep, epdata->req);
  10289. spin_unlock_irq (&epdata->dev->lock);
  10290. - wait_event (done.wait, done.done);
  10291. + swait_event (done.wait, done.done);
  10292. if (epdata->status == -ECONNRESET)
  10293. epdata->status = -EINTR;
  10294. } else {
  10295. diff --git a/drivers/usb/gadget/udc/atmel_usba_udc.c b/drivers/usb/gadget/udc/atmel_usba_udc.c
  10296. index f92f5aff0dd5..f9bba26e3655 100644
  10297. --- a/drivers/usb/gadget/udc/atmel_usba_udc.c
  10298. +++ b/drivers/usb/gadget/udc/atmel_usba_udc.c
  10299. @@ -17,7 +17,9 @@
  10300. #include <linux/device.h>
  10301. #include <linux/dma-mapping.h>
  10302. #include <linux/list.h>
  10303. +#include <linux/mfd/syscon.h>
  10304. #include <linux/platform_device.h>
  10305. +#include <linux/regmap.h>
  10306. #include <linux/usb/ch9.h>
  10307. #include <linux/usb/gadget.h>
  10308. #include <linux/usb/atmel_usba_udc.h>
  10309. @@ -1888,20 +1890,15 @@ static int atmel_usba_stop(struct usb_gadget *gadget)
  10310. #ifdef CONFIG_OF
  10311. static void at91sam9rl_toggle_bias(struct usba_udc *udc, int is_on)
  10312. {
  10313. - unsigned int uckr = at91_pmc_read(AT91_CKGR_UCKR);
  10314. -
  10315. - if (is_on)
  10316. - at91_pmc_write(AT91_CKGR_UCKR, uckr | AT91_PMC_BIASEN);
  10317. - else
  10318. - at91_pmc_write(AT91_CKGR_UCKR, uckr & ~(AT91_PMC_BIASEN));
  10319. + regmap_update_bits(udc->pmc, AT91_CKGR_UCKR, AT91_PMC_BIASEN,
  10320. + is_on ? AT91_PMC_BIASEN : 0);
  10321. }
  10322. static void at91sam9g45_pulse_bias(struct usba_udc *udc)
  10323. {
  10324. - unsigned int uckr = at91_pmc_read(AT91_CKGR_UCKR);
  10325. -
  10326. - at91_pmc_write(AT91_CKGR_UCKR, uckr & ~(AT91_PMC_BIASEN));
  10327. - at91_pmc_write(AT91_CKGR_UCKR, uckr | AT91_PMC_BIASEN);
  10328. + regmap_update_bits(udc->pmc, AT91_CKGR_UCKR, AT91_PMC_BIASEN, 0);
  10329. + regmap_update_bits(udc->pmc, AT91_CKGR_UCKR, AT91_PMC_BIASEN,
  10330. + AT91_PMC_BIASEN);
  10331. }
  10332. static const struct usba_udc_errata at91sam9rl_errata = {
  10333. @@ -1938,6 +1935,9 @@ static struct usba_ep * atmel_udc_of_init(struct platform_device *pdev,
  10334. return ERR_PTR(-EINVAL);
  10335. udc->errata = match->data;
  10336. + udc->pmc = syscon_regmap_lookup_by_compatible("atmel,at91sam9g45-pmc");
  10337. + if (udc->errata && IS_ERR(udc->pmc))
  10338. + return ERR_CAST(udc->pmc);
  10339. udc->num_ep = 0;
  10340. diff --git a/drivers/usb/gadget/udc/atmel_usba_udc.h b/drivers/usb/gadget/udc/atmel_usba_udc.h
  10341. index ea448a344767..3e1c9d589dfa 100644
  10342. --- a/drivers/usb/gadget/udc/atmel_usba_udc.h
  10343. +++ b/drivers/usb/gadget/udc/atmel_usba_udc.h
  10344. @@ -354,6 +354,8 @@ struct usba_udc {
  10345. struct dentry *debugfs_root;
  10346. struct dentry *debugfs_regs;
  10347. #endif
  10348. +
  10349. + struct regmap *pmc;
  10350. };
  10351. static inline struct usba_ep *to_usba_ep(struct usb_ep *ep)
  10352. diff --git a/fs/aio.c b/fs/aio.c
  10353. index fe4f49212b99..c3194afdc3df 100644
  10354. --- a/fs/aio.c
  10355. +++ b/fs/aio.c
  10356. @@ -40,6 +40,7 @@
  10357. #include <linux/ramfs.h>
  10358. #include <linux/percpu-refcount.h>
  10359. #include <linux/mount.h>
  10360. +#include <linux/swork.h>
  10361. #include <asm/kmap_types.h>
  10362. #include <asm/uaccess.h>
  10363. @@ -115,7 +116,7 @@ struct kioctx {
  10364. struct page **ring_pages;
  10365. long nr_pages;
  10366. - struct work_struct free_work;
  10367. + struct swork_event free_work;
  10368. /*
  10369. * signals when all in-flight requests are done
  10370. @@ -258,6 +259,7 @@ static int __init aio_setup(void)
  10371. .mount = aio_mount,
  10372. .kill_sb = kill_anon_super,
  10373. };
  10374. + BUG_ON(swork_get());
  10375. aio_mnt = kern_mount(&aio_fs);
  10376. if (IS_ERR(aio_mnt))
  10377. panic("Failed to create aio fs mount.");
  10378. @@ -573,9 +575,9 @@ static int kiocb_cancel(struct aio_kiocb *kiocb)
  10379. return cancel(&kiocb->common);
  10380. }
  10381. -static void free_ioctx(struct work_struct *work)
  10382. +static void free_ioctx(struct swork_event *sev)
  10383. {
  10384. - struct kioctx *ctx = container_of(work, struct kioctx, free_work);
  10385. + struct kioctx *ctx = container_of(sev, struct kioctx, free_work);
  10386. pr_debug("freeing %p\n", ctx);
  10387. @@ -594,8 +596,8 @@ static void free_ioctx_reqs(struct percpu_ref *ref)
  10388. if (ctx->rq_wait && atomic_dec_and_test(&ctx->rq_wait->count))
  10389. complete(&ctx->rq_wait->comp);
  10390. - INIT_WORK(&ctx->free_work, free_ioctx);
  10391. - schedule_work(&ctx->free_work);
  10392. + INIT_SWORK(&ctx->free_work, free_ioctx);
  10393. + swork_queue(&ctx->free_work);
  10394. }
  10395. /*
  10396. @@ -603,9 +605,9 @@ static void free_ioctx_reqs(struct percpu_ref *ref)
  10397. * and ctx->users has dropped to 0, so we know no more kiocbs can be submitted -
  10398. * now it's safe to cancel any that need to be.
  10399. */
  10400. -static void free_ioctx_users(struct percpu_ref *ref)
  10401. +static void free_ioctx_users_work(struct swork_event *sev)
  10402. {
  10403. - struct kioctx *ctx = container_of(ref, struct kioctx, users);
  10404. + struct kioctx *ctx = container_of(sev, struct kioctx, free_work);
  10405. struct aio_kiocb *req;
  10406. spin_lock_irq(&ctx->ctx_lock);
  10407. @@ -624,6 +626,14 @@ static void free_ioctx_users(struct percpu_ref *ref)
  10408. percpu_ref_put(&ctx->reqs);
  10409. }
  10410. +static void free_ioctx_users(struct percpu_ref *ref)
  10411. +{
  10412. + struct kioctx *ctx = container_of(ref, struct kioctx, users);
  10413. +
  10414. + INIT_SWORK(&ctx->free_work, free_ioctx_users_work);
  10415. + swork_queue(&ctx->free_work);
  10416. +}
  10417. +
  10418. static int ioctx_add_table(struct kioctx *ctx, struct mm_struct *mm)
  10419. {
  10420. unsigned i, new_nr;
  10421. diff --git a/fs/autofs4/autofs_i.h b/fs/autofs4/autofs_i.h
  10422. index 502d3892d8a4..05af8d3e6e88 100644
  10423. --- a/fs/autofs4/autofs_i.h
  10424. +++ b/fs/autofs4/autofs_i.h
  10425. @@ -34,6 +34,7 @@
  10426. #include <linux/sched.h>
  10427. #include <linux/mount.h>
  10428. #include <linux/namei.h>
  10429. +#include <linux/delay.h>
  10430. #include <asm/current.h>
  10431. #include <asm/uaccess.h>
  10432. diff --git a/fs/autofs4/expire.c b/fs/autofs4/expire.c
  10433. index 7a5a598a2d94..d08bcdc30566 100644
  10434. --- a/fs/autofs4/expire.c
  10435. +++ b/fs/autofs4/expire.c
  10436. @@ -150,7 +150,7 @@ again:
  10437. parent = p->d_parent;
  10438. if (!spin_trylock(&parent->d_lock)) {
  10439. spin_unlock(&p->d_lock);
  10440. - cpu_relax();
  10441. + cpu_chill();
  10442. goto relock;
  10443. }
  10444. spin_unlock(&p->d_lock);
  10445. diff --git a/fs/buffer.c b/fs/buffer.c
  10446. index 4f4cd959da7c..72b27e17b907 100644
  10447. --- a/fs/buffer.c
  10448. +++ b/fs/buffer.c
  10449. @@ -305,8 +305,7 @@ static void end_buffer_async_read(struct buffer_head *bh, int uptodate)
  10450. * decide that the page is now completely done.
  10451. */
  10452. first = page_buffers(page);
  10453. - local_irq_save(flags);
  10454. - bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  10455. + flags = bh_uptodate_lock_irqsave(first);
  10456. clear_buffer_async_read(bh);
  10457. unlock_buffer(bh);
  10458. tmp = bh;
  10459. @@ -319,8 +318,7 @@ static void end_buffer_async_read(struct buffer_head *bh, int uptodate)
  10460. }
  10461. tmp = tmp->b_this_page;
  10462. } while (tmp != bh);
  10463. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  10464. - local_irq_restore(flags);
  10465. + bh_uptodate_unlock_irqrestore(first, flags);
  10466. /*
  10467. * If none of the buffers had errors and they are all
  10468. @@ -332,9 +330,7 @@ static void end_buffer_async_read(struct buffer_head *bh, int uptodate)
  10469. return;
  10470. still_busy:
  10471. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  10472. - local_irq_restore(flags);
  10473. - return;
  10474. + bh_uptodate_unlock_irqrestore(first, flags);
  10475. }
  10476. /*
  10477. @@ -362,8 +358,7 @@ void end_buffer_async_write(struct buffer_head *bh, int uptodate)
  10478. }
  10479. first = page_buffers(page);
  10480. - local_irq_save(flags);
  10481. - bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  10482. + flags = bh_uptodate_lock_irqsave(first);
  10483. clear_buffer_async_write(bh);
  10484. unlock_buffer(bh);
  10485. @@ -375,15 +370,12 @@ void end_buffer_async_write(struct buffer_head *bh, int uptodate)
  10486. }
  10487. tmp = tmp->b_this_page;
  10488. }
  10489. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  10490. - local_irq_restore(flags);
  10491. + bh_uptodate_unlock_irqrestore(first, flags);
  10492. end_page_writeback(page);
  10493. return;
  10494. still_busy:
  10495. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  10496. - local_irq_restore(flags);
  10497. - return;
  10498. + bh_uptodate_unlock_irqrestore(first, flags);
  10499. }
  10500. EXPORT_SYMBOL(end_buffer_async_write);
  10501. @@ -3325,6 +3317,7 @@ struct buffer_head *alloc_buffer_head(gfp_t gfp_flags)
  10502. struct buffer_head *ret = kmem_cache_zalloc(bh_cachep, gfp_flags);
  10503. if (ret) {
  10504. INIT_LIST_HEAD(&ret->b_assoc_buffers);
  10505. + buffer_head_init_locks(ret);
  10506. preempt_disable();
  10507. __this_cpu_inc(bh_accounting.nr);
  10508. recalc_bh_state();
  10509. diff --git a/fs/dcache.c b/fs/dcache.c
  10510. index 849c1c1e787b..995995130191 100644
  10511. --- a/fs/dcache.c
  10512. +++ b/fs/dcache.c
  10513. @@ -19,6 +19,7 @@
  10514. #include <linux/mm.h>
  10515. #include <linux/fs.h>
  10516. #include <linux/fsnotify.h>
  10517. +#include <linux/delay.h>
  10518. #include <linux/slab.h>
  10519. #include <linux/init.h>
  10520. #include <linux/hash.h>
  10521. @@ -747,6 +748,8 @@ static inline bool fast_dput(struct dentry *dentry)
  10522. */
  10523. void dput(struct dentry *dentry)
  10524. {
  10525. + struct dentry *parent;
  10526. +
  10527. if (unlikely(!dentry))
  10528. return;
  10529. @@ -783,9 +786,18 @@ repeat:
  10530. return;
  10531. kill_it:
  10532. - dentry = dentry_kill(dentry);
  10533. - if (dentry) {
  10534. - cond_resched();
  10535. + parent = dentry_kill(dentry);
  10536. + if (parent) {
  10537. + int r;
  10538. +
  10539. + if (parent == dentry) {
  10540. + /* the task with the highest priority won't schedule */
  10541. + r = cond_resched();
  10542. + if (!r)
  10543. + cpu_chill();
  10544. + } else {
  10545. + dentry = parent;
  10546. + }
  10547. goto repeat;
  10548. }
  10549. }
  10550. @@ -2397,7 +2409,7 @@ again:
  10551. if (dentry->d_lockref.count == 1) {
  10552. if (!spin_trylock(&inode->i_lock)) {
  10553. spin_unlock(&dentry->d_lock);
  10554. - cpu_relax();
  10555. + cpu_chill();
  10556. goto again;
  10557. }
  10558. dentry->d_flags &= ~DCACHE_CANT_MOUNT;
  10559. diff --git a/fs/eventpoll.c b/fs/eventpoll.c
  10560. index 1e009cad8d5c..d0c12504d3b4 100644
  10561. --- a/fs/eventpoll.c
  10562. +++ b/fs/eventpoll.c
  10563. @@ -505,12 +505,12 @@ static int ep_poll_wakeup_proc(void *priv, void *cookie, int call_nests)
  10564. */
  10565. static void ep_poll_safewake(wait_queue_head_t *wq)
  10566. {
  10567. - int this_cpu = get_cpu();
  10568. + int this_cpu = get_cpu_light();
  10569. ep_call_nested(&poll_safewake_ncalls, EP_MAX_NESTS,
  10570. ep_poll_wakeup_proc, NULL, wq, (void *) (long) this_cpu);
  10571. - put_cpu();
  10572. + put_cpu_light();
  10573. }
  10574. static void ep_remove_wait_queue(struct eppoll_entry *pwq)
  10575. diff --git a/fs/exec.c b/fs/exec.c
  10576. index 3a6de10d3891..74962ac0a738 100644
  10577. --- a/fs/exec.c
  10578. +++ b/fs/exec.c
  10579. @@ -866,12 +866,14 @@ static int exec_mmap(struct mm_struct *mm)
  10580. }
  10581. }
  10582. task_lock(tsk);
  10583. + preempt_disable_rt();
  10584. active_mm = tsk->active_mm;
  10585. tsk->mm = mm;
  10586. tsk->active_mm = mm;
  10587. activate_mm(active_mm, mm);
  10588. tsk->mm->vmacache_seqnum = 0;
  10589. vmacache_flush(tsk);
  10590. + preempt_enable_rt();
  10591. task_unlock(tsk);
  10592. if (old_mm) {
  10593. up_read(&old_mm->mmap_sem);
  10594. diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
  10595. index 2871576fbca4..d1137790ea58 100644
  10596. --- a/fs/f2fs/f2fs.h
  10597. +++ b/fs/f2fs/f2fs.h
  10598. @@ -24,7 +24,6 @@
  10599. #ifdef CONFIG_F2FS_CHECK_FS
  10600. #define f2fs_bug_on(sbi, condition) BUG_ON(condition)
  10601. -#define f2fs_down_write(x, y) down_write_nest_lock(x, y)
  10602. #else
  10603. #define f2fs_bug_on(sbi, condition) \
  10604. do { \
  10605. @@ -33,7 +32,6 @@
  10606. set_sbi_flag(sbi, SBI_NEED_FSCK); \
  10607. } \
  10608. } while (0)
  10609. -#define f2fs_down_write(x, y) down_write(x)
  10610. #endif
  10611. /*
  10612. @@ -959,7 +957,7 @@ static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
  10613. static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
  10614. {
  10615. - f2fs_down_write(&sbi->cp_rwsem, &sbi->cp_mutex);
  10616. + down_write(&sbi->cp_rwsem);
  10617. }
  10618. static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
  10619. diff --git a/fs/jbd2/checkpoint.c b/fs/jbd2/checkpoint.c
  10620. index 684996c8a3a4..6e18a06aaabe 100644
  10621. --- a/fs/jbd2/checkpoint.c
  10622. +++ b/fs/jbd2/checkpoint.c
  10623. @@ -116,6 +116,8 @@ void __jbd2_log_wait_for_space(journal_t *journal)
  10624. nblocks = jbd2_space_needed(journal);
  10625. while (jbd2_log_space_left(journal) < nblocks) {
  10626. write_unlock(&journal->j_state_lock);
  10627. + if (current->plug)
  10628. + io_schedule();
  10629. mutex_lock(&journal->j_checkpoint_mutex);
  10630. /*
  10631. diff --git a/fs/namespace.c b/fs/namespace.c
  10632. index f26d18d69712..6e0e024715e8 100644
  10633. --- a/fs/namespace.c
  10634. +++ b/fs/namespace.c
  10635. @@ -14,6 +14,7 @@
  10636. #include <linux/mnt_namespace.h>
  10637. #include <linux/user_namespace.h>
  10638. #include <linux/namei.h>
  10639. +#include <linux/delay.h>
  10640. #include <linux/security.h>
  10641. #include <linux/idr.h>
  10642. #include <linux/init.h> /* init_rootfs */
  10643. @@ -356,8 +357,11 @@ int __mnt_want_write(struct vfsmount *m)
  10644. * incremented count after it has set MNT_WRITE_HOLD.
  10645. */
  10646. smp_mb();
  10647. - while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD)
  10648. - cpu_relax();
  10649. + while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD) {
  10650. + preempt_enable();
  10651. + cpu_chill();
  10652. + preempt_disable();
  10653. + }
  10654. /*
  10655. * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
  10656. * be set to match its requirements. So we must not load that until
  10657. diff --git a/fs/ntfs/aops.c b/fs/ntfs/aops.c
  10658. index 7521e11db728..f0de4b6b8bf3 100644
  10659. --- a/fs/ntfs/aops.c
  10660. +++ b/fs/ntfs/aops.c
  10661. @@ -107,8 +107,7 @@ static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
  10662. "0x%llx.", (unsigned long long)bh->b_blocknr);
  10663. }
  10664. first = page_buffers(page);
  10665. - local_irq_save(flags);
  10666. - bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  10667. + flags = bh_uptodate_lock_irqsave(first);
  10668. clear_buffer_async_read(bh);
  10669. unlock_buffer(bh);
  10670. tmp = bh;
  10671. @@ -123,8 +122,7 @@ static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
  10672. }
  10673. tmp = tmp->b_this_page;
  10674. } while (tmp != bh);
  10675. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  10676. - local_irq_restore(flags);
  10677. + bh_uptodate_unlock_irqrestore(first, flags);
  10678. /*
  10679. * If none of the buffers had errors then we can set the page uptodate,
  10680. * but we first have to perform the post read mst fixups, if the
  10681. @@ -145,13 +143,13 @@ static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
  10682. recs = PAGE_CACHE_SIZE / rec_size;
  10683. /* Should have been verified before we got here... */
  10684. BUG_ON(!recs);
  10685. - local_irq_save(flags);
  10686. + local_irq_save_nort(flags);
  10687. kaddr = kmap_atomic(page);
  10688. for (i = 0; i < recs; i++)
  10689. post_read_mst_fixup((NTFS_RECORD*)(kaddr +
  10690. i * rec_size), rec_size);
  10691. kunmap_atomic(kaddr);
  10692. - local_irq_restore(flags);
  10693. + local_irq_restore_nort(flags);
  10694. flush_dcache_page(page);
  10695. if (likely(page_uptodate && !PageError(page)))
  10696. SetPageUptodate(page);
  10697. @@ -159,9 +157,7 @@ static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
  10698. unlock_page(page);
  10699. return;
  10700. still_busy:
  10701. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  10702. - local_irq_restore(flags);
  10703. - return;
  10704. + bh_uptodate_unlock_irqrestore(first, flags);
  10705. }
  10706. /**
  10707. diff --git a/fs/timerfd.c b/fs/timerfd.c
  10708. index 1327a02ec778..4260febcb029 100644
  10709. --- a/fs/timerfd.c
  10710. +++ b/fs/timerfd.c
  10711. @@ -461,7 +461,10 @@ static int do_timerfd_settime(int ufd, int flags,
  10712. break;
  10713. }
  10714. spin_unlock_irq(&ctx->wqh.lock);
  10715. - cpu_relax();
  10716. + if (isalarm(ctx))
  10717. + hrtimer_wait_for_timer(&ctx->t.alarm.timer);
  10718. + else
  10719. + hrtimer_wait_for_timer(&ctx->t.tmr);
  10720. }
  10721. /*
  10722. diff --git a/include/acpi/platform/aclinux.h b/include/acpi/platform/aclinux.h
  10723. index 323e5daece54..cc5fbd534fd4 100644
  10724. --- a/include/acpi/platform/aclinux.h
  10725. +++ b/include/acpi/platform/aclinux.h
  10726. @@ -127,6 +127,7 @@
  10727. #define acpi_cache_t struct kmem_cache
  10728. #define acpi_spinlock spinlock_t *
  10729. +#define acpi_raw_spinlock raw_spinlock_t *
  10730. #define acpi_cpu_flags unsigned long
  10731. /* Use native linux version of acpi_os_allocate_zeroed */
  10732. @@ -145,6 +146,20 @@
  10733. #define ACPI_USE_ALTERNATE_PROTOTYPE_acpi_os_get_thread_id
  10734. #define ACPI_USE_ALTERNATE_PROTOTYPE_acpi_os_create_lock
  10735. +#define acpi_os_create_raw_lock(__handle) \
  10736. +({ \
  10737. + raw_spinlock_t *lock = ACPI_ALLOCATE(sizeof(*lock)); \
  10738. + \
  10739. + if (lock) { \
  10740. + *(__handle) = lock; \
  10741. + raw_spin_lock_init(*(__handle)); \
  10742. + } \
  10743. + lock ? AE_OK : AE_NO_MEMORY; \
  10744. + })
  10745. +
  10746. +#define acpi_os_delete_raw_lock(__handle) kfree(__handle)
  10747. +
  10748. +
  10749. /*
  10750. * OSL interfaces used by debugger/disassembler
  10751. */
  10752. diff --git a/include/asm-generic/bug.h b/include/asm-generic/bug.h
  10753. index 630dd2372238..850e4d993a88 100644
  10754. --- a/include/asm-generic/bug.h
  10755. +++ b/include/asm-generic/bug.h
  10756. @@ -206,6 +206,20 @@ extern void warn_slowpath_null(const char *file, const int line);
  10757. # define WARN_ON_SMP(x) ({0;})
  10758. #endif
  10759. +#ifdef CONFIG_PREEMPT_RT_BASE
  10760. +# define BUG_ON_RT(c) BUG_ON(c)
  10761. +# define BUG_ON_NONRT(c) do { } while (0)
  10762. +# define WARN_ON_RT(condition) WARN_ON(condition)
  10763. +# define WARN_ON_NONRT(condition) do { } while (0)
  10764. +# define WARN_ON_ONCE_NONRT(condition) do { } while (0)
  10765. +#else
  10766. +# define BUG_ON_RT(c) do { } while (0)
  10767. +# define BUG_ON_NONRT(c) BUG_ON(c)
  10768. +# define WARN_ON_RT(condition) do { } while (0)
  10769. +# define WARN_ON_NONRT(condition) WARN_ON(condition)
  10770. +# define WARN_ON_ONCE_NONRT(condition) WARN_ON_ONCE(condition)
  10771. +#endif
  10772. +
  10773. #endif /* __ASSEMBLY__ */
  10774. #endif
  10775. diff --git a/include/asm-generic/preempt.h b/include/asm-generic/preempt.h
  10776. index 5d8ffa3e6f8c..c1cde3577551 100644
  10777. --- a/include/asm-generic/preempt.h
  10778. +++ b/include/asm-generic/preempt.h
  10779. @@ -7,10 +7,10 @@
  10780. static __always_inline int preempt_count(void)
  10781. {
  10782. - return current_thread_info()->preempt_count;
  10783. + return READ_ONCE(current_thread_info()->preempt_count);
  10784. }
  10785. -static __always_inline int *preempt_count_ptr(void)
  10786. +static __always_inline volatile int *preempt_count_ptr(void)
  10787. {
  10788. return &current_thread_info()->preempt_count;
  10789. }
  10790. diff --git a/include/linux/blk-mq.h b/include/linux/blk-mq.h
  10791. index daf17d70aeca..463df8954255 100644
  10792. --- a/include/linux/blk-mq.h
  10793. +++ b/include/linux/blk-mq.h
  10794. @@ -212,6 +212,7 @@ static inline u16 blk_mq_unique_tag_to_tag(u32 unique_tag)
  10795. struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *, const int ctx_index);
  10796. struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_tag_set *, unsigned int, int);
  10797. +void __blk_mq_complete_request_remote_work(struct work_struct *work);
  10798. int blk_mq_request_started(struct request *rq);
  10799. void blk_mq_start_request(struct request *rq);
  10800. diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
  10801. index fe14382f9664..a82143ad6702 100644
  10802. --- a/include/linux/blkdev.h
  10803. +++ b/include/linux/blkdev.h
  10804. @@ -89,6 +89,7 @@ struct request {
  10805. struct list_head queuelist;
  10806. union {
  10807. struct call_single_data csd;
  10808. + struct work_struct work;
  10809. unsigned long fifo_time;
  10810. };
  10811. @@ -455,7 +456,7 @@ struct request_queue {
  10812. struct throtl_data *td;
  10813. #endif
  10814. struct rcu_head rcu_head;
  10815. - wait_queue_head_t mq_freeze_wq;
  10816. + struct swait_queue_head mq_freeze_wq;
  10817. struct percpu_ref q_usage_counter;
  10818. struct list_head all_q_node;
  10819. diff --git a/include/linux/bottom_half.h b/include/linux/bottom_half.h
  10820. index 8fdcb783197d..d07dbeec7bc1 100644
  10821. --- a/include/linux/bottom_half.h
  10822. +++ b/include/linux/bottom_half.h
  10823. @@ -3,6 +3,39 @@
  10824. #include <linux/preempt.h>
  10825. +#ifdef CONFIG_PREEMPT_RT_FULL
  10826. +
  10827. +extern void __local_bh_disable(void);
  10828. +extern void _local_bh_enable(void);
  10829. +extern void __local_bh_enable(void);
  10830. +
  10831. +static inline void local_bh_disable(void)
  10832. +{
  10833. + __local_bh_disable();
  10834. +}
  10835. +
  10836. +static inline void __local_bh_disable_ip(unsigned long ip, unsigned int cnt)
  10837. +{
  10838. + __local_bh_disable();
  10839. +}
  10840. +
  10841. +static inline void local_bh_enable(void)
  10842. +{
  10843. + __local_bh_enable();
  10844. +}
  10845. +
  10846. +static inline void __local_bh_enable_ip(unsigned long ip, unsigned int cnt)
  10847. +{
  10848. + __local_bh_enable();
  10849. +}
  10850. +
  10851. +static inline void local_bh_enable_ip(unsigned long ip)
  10852. +{
  10853. + __local_bh_enable();
  10854. +}
  10855. +
  10856. +#else
  10857. +
  10858. #ifdef CONFIG_TRACE_IRQFLAGS
  10859. extern void __local_bh_disable_ip(unsigned long ip, unsigned int cnt);
  10860. #else
  10861. @@ -30,5 +63,6 @@ static inline void local_bh_enable(void)
  10862. {
  10863. __local_bh_enable_ip(_THIS_IP_, SOFTIRQ_DISABLE_OFFSET);
  10864. }
  10865. +#endif
  10866. #endif /* _LINUX_BH_H */
  10867. diff --git a/include/linux/buffer_head.h b/include/linux/buffer_head.h
  10868. index 89d9aa9e79bf..4a201008b02d 100644
  10869. --- a/include/linux/buffer_head.h
  10870. +++ b/include/linux/buffer_head.h
  10871. @@ -75,8 +75,50 @@ struct buffer_head {
  10872. struct address_space *b_assoc_map; /* mapping this buffer is
  10873. associated with */
  10874. atomic_t b_count; /* users using this buffer_head */
  10875. +#ifdef CONFIG_PREEMPT_RT_BASE
  10876. + spinlock_t b_uptodate_lock;
  10877. +#if IS_ENABLED(CONFIG_JBD2)
  10878. + spinlock_t b_state_lock;
  10879. + spinlock_t b_journal_head_lock;
  10880. +#endif
  10881. +#endif
  10882. };
  10883. +static inline unsigned long bh_uptodate_lock_irqsave(struct buffer_head *bh)
  10884. +{
  10885. + unsigned long flags;
  10886. +
  10887. +#ifndef CONFIG_PREEMPT_RT_BASE
  10888. + local_irq_save(flags);
  10889. + bit_spin_lock(BH_Uptodate_Lock, &bh->b_state);
  10890. +#else
  10891. + spin_lock_irqsave(&bh->b_uptodate_lock, flags);
  10892. +#endif
  10893. + return flags;
  10894. +}
  10895. +
  10896. +static inline void
  10897. +bh_uptodate_unlock_irqrestore(struct buffer_head *bh, unsigned long flags)
  10898. +{
  10899. +#ifndef CONFIG_PREEMPT_RT_BASE
  10900. + bit_spin_unlock(BH_Uptodate_Lock, &bh->b_state);
  10901. + local_irq_restore(flags);
  10902. +#else
  10903. + spin_unlock_irqrestore(&bh->b_uptodate_lock, flags);
  10904. +#endif
  10905. +}
  10906. +
  10907. +static inline void buffer_head_init_locks(struct buffer_head *bh)
  10908. +{
  10909. +#ifdef CONFIG_PREEMPT_RT_BASE
  10910. + spin_lock_init(&bh->b_uptodate_lock);
  10911. +#if IS_ENABLED(CONFIG_JBD2)
  10912. + spin_lock_init(&bh->b_state_lock);
  10913. + spin_lock_init(&bh->b_journal_head_lock);
  10914. +#endif
  10915. +#endif
  10916. +}
  10917. +
  10918. /*
  10919. * macro tricks to expand the set_buffer_foo(), clear_buffer_foo()
  10920. * and buffer_foo() functions.
  10921. diff --git a/include/linux/cgroup-defs.h b/include/linux/cgroup-defs.h
  10922. index 8da263299754..0cc474291e08 100644
  10923. --- a/include/linux/cgroup-defs.h
  10924. +++ b/include/linux/cgroup-defs.h
  10925. @@ -16,6 +16,7 @@
  10926. #include <linux/percpu-refcount.h>
  10927. #include <linux/percpu-rwsem.h>
  10928. #include <linux/workqueue.h>
  10929. +#include <linux/swork.h>
  10930. #ifdef CONFIG_CGROUPS
  10931. @@ -142,6 +143,7 @@ struct cgroup_subsys_state {
  10932. /* percpu_ref killing and RCU release */
  10933. struct rcu_head rcu_head;
  10934. struct work_struct destroy_work;
  10935. + struct swork_event destroy_swork;
  10936. };
  10937. /*
  10938. diff --git a/include/linux/clk/at91_pmc.h b/include/linux/clk/at91_pmc.h
  10939. index 1e6932222e11..17f413bbbedf 100644
  10940. --- a/include/linux/clk/at91_pmc.h
  10941. +++ b/include/linux/clk/at91_pmc.h
  10942. @@ -16,18 +16,6 @@
  10943. #ifndef AT91_PMC_H
  10944. #define AT91_PMC_H
  10945. -#ifndef __ASSEMBLY__
  10946. -extern void __iomem *at91_pmc_base;
  10947. -
  10948. -#define at91_pmc_read(field) \
  10949. - readl_relaxed(at91_pmc_base + field)
  10950. -
  10951. -#define at91_pmc_write(field, value) \
  10952. - writel_relaxed(value, at91_pmc_base + field)
  10953. -#else
  10954. -.extern at91_pmc_base
  10955. -#endif
  10956. -
  10957. #define AT91_PMC_SCER 0x00 /* System Clock Enable Register */
  10958. #define AT91_PMC_SCDR 0x04 /* System Clock Disable Register */
  10959. diff --git a/include/linux/completion.h b/include/linux/completion.h
  10960. index 5d5aaae3af43..3bca1590e29f 100644
  10961. --- a/include/linux/completion.h
  10962. +++ b/include/linux/completion.h
  10963. @@ -7,8 +7,7 @@
  10964. * Atomic wait-for-completion handler data structures.
  10965. * See kernel/sched/completion.c for details.
  10966. */
  10967. -
  10968. -#include <linux/wait.h>
  10969. +#include <linux/swait.h>
  10970. /*
  10971. * struct completion - structure used to maintain state for a "completion"
  10972. @@ -24,11 +23,11 @@
  10973. */
  10974. struct completion {
  10975. unsigned int done;
  10976. - wait_queue_head_t wait;
  10977. + struct swait_queue_head wait;
  10978. };
  10979. #define COMPLETION_INITIALIZER(work) \
  10980. - { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
  10981. + { 0, __SWAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
  10982. #define COMPLETION_INITIALIZER_ONSTACK(work) \
  10983. ({ init_completion(&work); work; })
  10984. @@ -73,7 +72,7 @@ struct completion {
  10985. static inline void init_completion(struct completion *x)
  10986. {
  10987. x->done = 0;
  10988. - init_waitqueue_head(&x->wait);
  10989. + init_swait_queue_head(&x->wait);
  10990. }
  10991. /**
  10992. diff --git a/include/linux/cpu.h b/include/linux/cpu.h
  10993. index 3ea9aae2387d..5de5f28a5ef3 100644
  10994. --- a/include/linux/cpu.h
  10995. +++ b/include/linux/cpu.h
  10996. @@ -224,6 +224,8 @@ extern void get_online_cpus(void);
  10997. extern void put_online_cpus(void);
  10998. extern void cpu_hotplug_disable(void);
  10999. extern void cpu_hotplug_enable(void);
  11000. +extern void pin_current_cpu(void);
  11001. +extern void unpin_current_cpu(void);
  11002. #define hotcpu_notifier(fn, pri) cpu_notifier(fn, pri)
  11003. #define __hotcpu_notifier(fn, pri) __cpu_notifier(fn, pri)
  11004. #define register_hotcpu_notifier(nb) register_cpu_notifier(nb)
  11005. @@ -241,6 +243,8 @@ static inline void cpu_hotplug_done(void) {}
  11006. #define put_online_cpus() do { } while (0)
  11007. #define cpu_hotplug_disable() do { } while (0)
  11008. #define cpu_hotplug_enable() do { } while (0)
  11009. +static inline void pin_current_cpu(void) { }
  11010. +static inline void unpin_current_cpu(void) { }
  11011. #define hotcpu_notifier(fn, pri) do { (void)(fn); } while (0)
  11012. #define __hotcpu_notifier(fn, pri) do { (void)(fn); } while (0)
  11013. /* These aren't inline functions due to a GCC bug. */
  11014. diff --git a/include/linux/delay.h b/include/linux/delay.h
  11015. index a6ecb34cf547..37caab306336 100644
  11016. --- a/include/linux/delay.h
  11017. +++ b/include/linux/delay.h
  11018. @@ -52,4 +52,10 @@ static inline void ssleep(unsigned int seconds)
  11019. msleep(seconds * 1000);
  11020. }
  11021. +#ifdef CONFIG_PREEMPT_RT_FULL
  11022. +extern void cpu_chill(void);
  11023. +#else
  11024. +# define cpu_chill() cpu_relax()
  11025. +#endif
  11026. +
  11027. #endif /* defined(_LINUX_DELAY_H) */
  11028. diff --git a/include/linux/ftrace.h b/include/linux/ftrace.h
  11029. index 60048c50404e..f2cd67624f18 100644
  11030. --- a/include/linux/ftrace.h
  11031. +++ b/include/linux/ftrace.h
  11032. @@ -694,6 +694,18 @@ static inline void __ftrace_enabled_restore(int enabled)
  11033. #define CALLER_ADDR5 ((unsigned long)ftrace_return_address(5))
  11034. #define CALLER_ADDR6 ((unsigned long)ftrace_return_address(6))
  11035. +static inline unsigned long get_lock_parent_ip(void)
  11036. +{
  11037. + unsigned long addr = CALLER_ADDR0;
  11038. +
  11039. + if (!in_lock_functions(addr))
  11040. + return addr;
  11041. + addr = CALLER_ADDR1;
  11042. + if (!in_lock_functions(addr))
  11043. + return addr;
  11044. + return CALLER_ADDR2;
  11045. +}
  11046. +
  11047. #ifdef CONFIG_IRQSOFF_TRACER
  11048. extern void time_hardirqs_on(unsigned long a0, unsigned long a1);
  11049. extern void time_hardirqs_off(unsigned long a0, unsigned long a1);
  11050. diff --git a/include/linux/highmem.h b/include/linux/highmem.h
  11051. index bb3f3297062a..a117a33ef72c 100644
  11052. --- a/include/linux/highmem.h
  11053. +++ b/include/linux/highmem.h
  11054. @@ -7,6 +7,7 @@
  11055. #include <linux/mm.h>
  11056. #include <linux/uaccess.h>
  11057. #include <linux/hardirq.h>
  11058. +#include <linux/sched.h>
  11059. #include <asm/cacheflush.h>
  11060. @@ -65,7 +66,7 @@ static inline void kunmap(struct page *page)
  11061. static inline void *kmap_atomic(struct page *page)
  11062. {
  11063. - preempt_disable();
  11064. + preempt_disable_nort();
  11065. pagefault_disable();
  11066. return page_address(page);
  11067. }
  11068. @@ -74,7 +75,7 @@ static inline void *kmap_atomic(struct page *page)
  11069. static inline void __kunmap_atomic(void *addr)
  11070. {
  11071. pagefault_enable();
  11072. - preempt_enable();
  11073. + preempt_enable_nort();
  11074. }
  11075. #define kmap_atomic_pfn(pfn) kmap_atomic(pfn_to_page(pfn))
  11076. @@ -86,32 +87,51 @@ static inline void __kunmap_atomic(void *addr)
  11077. #if defined(CONFIG_HIGHMEM) || defined(CONFIG_X86_32)
  11078. +#ifndef CONFIG_PREEMPT_RT_FULL
  11079. DECLARE_PER_CPU(int, __kmap_atomic_idx);
  11080. +#endif
  11081. static inline int kmap_atomic_idx_push(void)
  11082. {
  11083. +#ifndef CONFIG_PREEMPT_RT_FULL
  11084. int idx = __this_cpu_inc_return(__kmap_atomic_idx) - 1;
  11085. -#ifdef CONFIG_DEBUG_HIGHMEM
  11086. +# ifdef CONFIG_DEBUG_HIGHMEM
  11087. WARN_ON_ONCE(in_irq() && !irqs_disabled());
  11088. BUG_ON(idx >= KM_TYPE_NR);
  11089. -#endif
  11090. +# endif
  11091. return idx;
  11092. +#else
  11093. + current->kmap_idx++;
  11094. + BUG_ON(current->kmap_idx > KM_TYPE_NR);
  11095. + return current->kmap_idx - 1;
  11096. +#endif
  11097. }
  11098. static inline int kmap_atomic_idx(void)
  11099. {
  11100. +#ifndef CONFIG_PREEMPT_RT_FULL
  11101. return __this_cpu_read(__kmap_atomic_idx) - 1;
  11102. +#else
  11103. + return current->kmap_idx - 1;
  11104. +#endif
  11105. }
  11106. static inline void kmap_atomic_idx_pop(void)
  11107. {
  11108. -#ifdef CONFIG_DEBUG_HIGHMEM
  11109. +#ifndef CONFIG_PREEMPT_RT_FULL
  11110. +# ifdef CONFIG_DEBUG_HIGHMEM
  11111. int idx = __this_cpu_dec_return(__kmap_atomic_idx);
  11112. BUG_ON(idx < 0);
  11113. -#else
  11114. +# else
  11115. __this_cpu_dec(__kmap_atomic_idx);
  11116. +# endif
  11117. +#else
  11118. + current->kmap_idx--;
  11119. +# ifdef CONFIG_DEBUG_HIGHMEM
  11120. + BUG_ON(current->kmap_idx < 0);
  11121. +# endif
  11122. #endif
  11123. }
  11124. diff --git a/include/linux/hrtimer.h b/include/linux/hrtimer.h
  11125. index 2ead22dd74a0..8fbcdfa5dc77 100644
  11126. --- a/include/linux/hrtimer.h
  11127. +++ b/include/linux/hrtimer.h
  11128. @@ -87,6 +87,9 @@ enum hrtimer_restart {
  11129. * @function: timer expiry callback function
  11130. * @base: pointer to the timer base (per cpu and per clock)
  11131. * @state: state information (See bit values above)
  11132. + * @cb_entry: list entry to defer timers from hardirq context
  11133. + * @irqsafe: timer can run in hardirq context
  11134. + * @praecox: timer expiry time if expired at the time of programming
  11135. * @is_rel: Set if the timer was armed relative
  11136. * @start_pid: timer statistics field to store the pid of the task which
  11137. * started the timer
  11138. @@ -103,6 +106,11 @@ struct hrtimer {
  11139. enum hrtimer_restart (*function)(struct hrtimer *);
  11140. struct hrtimer_clock_base *base;
  11141. u8 state;
  11142. + struct list_head cb_entry;
  11143. + int irqsafe;
  11144. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  11145. + ktime_t praecox;
  11146. +#endif
  11147. u8 is_rel;
  11148. #ifdef CONFIG_TIMER_STATS
  11149. int start_pid;
  11150. @@ -123,11 +131,7 @@ struct hrtimer_sleeper {
  11151. struct task_struct *task;
  11152. };
  11153. -#ifdef CONFIG_64BIT
  11154. # define HRTIMER_CLOCK_BASE_ALIGN 64
  11155. -#else
  11156. -# define HRTIMER_CLOCK_BASE_ALIGN 32
  11157. -#endif
  11158. /**
  11159. * struct hrtimer_clock_base - the timer base for a specific clock
  11160. @@ -136,6 +140,7 @@ struct hrtimer_sleeper {
  11161. * timer to a base on another cpu.
  11162. * @clockid: clock id for per_cpu support
  11163. * @active: red black tree root node for the active timers
  11164. + * @expired: list head for deferred timers.
  11165. * @get_time: function to retrieve the current time of the clock
  11166. * @offset: offset of this clock to the monotonic base
  11167. */
  11168. @@ -144,6 +149,7 @@ struct hrtimer_clock_base {
  11169. int index;
  11170. clockid_t clockid;
  11171. struct timerqueue_head active;
  11172. + struct list_head expired;
  11173. ktime_t (*get_time)(void);
  11174. ktime_t offset;
  11175. } __attribute__((__aligned__(HRTIMER_CLOCK_BASE_ALIGN)));
  11176. @@ -187,6 +193,7 @@ struct hrtimer_cpu_base {
  11177. raw_spinlock_t lock;
  11178. seqcount_t seq;
  11179. struct hrtimer *running;
  11180. + struct hrtimer *running_soft;
  11181. unsigned int cpu;
  11182. unsigned int active_bases;
  11183. unsigned int clock_was_set_seq;
  11184. @@ -203,6 +210,9 @@ struct hrtimer_cpu_base {
  11185. unsigned int nr_hangs;
  11186. unsigned int max_hang_time;
  11187. #endif
  11188. +#ifdef CONFIG_PREEMPT_RT_BASE
  11189. + wait_queue_head_t wait;
  11190. +#endif
  11191. struct hrtimer_clock_base clock_base[HRTIMER_MAX_CLOCK_BASES];
  11192. } ____cacheline_aligned;
  11193. @@ -412,6 +422,13 @@ static inline void hrtimer_restart(struct hrtimer *timer)
  11194. hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
  11195. }
  11196. +/* Softirq preemption could deadlock timer removal */
  11197. +#ifdef CONFIG_PREEMPT_RT_BASE
  11198. + extern void hrtimer_wait_for_timer(const struct hrtimer *timer);
  11199. +#else
  11200. +# define hrtimer_wait_for_timer(timer) do { cpu_relax(); } while (0)
  11201. +#endif
  11202. +
  11203. /* Query timers: */
  11204. extern ktime_t __hrtimer_get_remaining(const struct hrtimer *timer, bool adjust);
  11205. @@ -436,7 +453,7 @@ static inline int hrtimer_is_queued(struct hrtimer *timer)
  11206. * Helper function to check, whether the timer is running the callback
  11207. * function
  11208. */
  11209. -static inline int hrtimer_callback_running(struct hrtimer *timer)
  11210. +static inline int hrtimer_callback_running(const struct hrtimer *timer)
  11211. {
  11212. return timer->base->cpu_base->running == timer;
  11213. }
  11214. diff --git a/include/linux/idr.h b/include/linux/idr.h
  11215. index 013fd9bc4cb6..f62be0aec911 100644
  11216. --- a/include/linux/idr.h
  11217. +++ b/include/linux/idr.h
  11218. @@ -95,10 +95,14 @@ bool idr_is_empty(struct idr *idp);
  11219. * Each idr_preload() should be matched with an invocation of this
  11220. * function. See idr_preload() for details.
  11221. */
  11222. +#ifdef CONFIG_PREEMPT_RT_FULL
  11223. +void idr_preload_end(void);
  11224. +#else
  11225. static inline void idr_preload_end(void)
  11226. {
  11227. preempt_enable();
  11228. }
  11229. +#endif
  11230. /**
  11231. * idr_find - return pointer for given id
  11232. diff --git a/include/linux/init_task.h b/include/linux/init_task.h
  11233. index 1c1ff7e4faa4..60fadde71a44 100644
  11234. --- a/include/linux/init_task.h
  11235. +++ b/include/linux/init_task.h
  11236. @@ -148,9 +148,15 @@ extern struct task_group root_task_group;
  11237. # define INIT_PERF_EVENTS(tsk)
  11238. #endif
  11239. +#ifdef CONFIG_PREEMPT_RT_BASE
  11240. +# define INIT_TIMER_LIST .posix_timer_list = NULL,
  11241. +#else
  11242. +# define INIT_TIMER_LIST
  11243. +#endif
  11244. +
  11245. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  11246. # define INIT_VTIME(tsk) \
  11247. - .vtime_seqlock = __SEQLOCK_UNLOCKED(tsk.vtime_seqlock), \
  11248. + .vtime_seqcount = SEQCNT_ZERO(tsk.vtime_seqcount), \
  11249. .vtime_snap = 0, \
  11250. .vtime_snap_whence = VTIME_SYS,
  11251. #else
  11252. @@ -239,6 +245,7 @@ extern struct task_group root_task_group;
  11253. .cpu_timers = INIT_CPU_TIMERS(tsk.cpu_timers), \
  11254. .pi_lock = __RAW_SPIN_LOCK_UNLOCKED(tsk.pi_lock), \
  11255. .timer_slack_ns = 50000, /* 50 usec default slack */ \
  11256. + INIT_TIMER_LIST \
  11257. .pids = { \
  11258. [PIDTYPE_PID] = INIT_PID_LINK(PIDTYPE_PID), \
  11259. [PIDTYPE_PGID] = INIT_PID_LINK(PIDTYPE_PGID), \
  11260. diff --git a/include/linux/interrupt.h b/include/linux/interrupt.h
  11261. index ad16809c8596..655cee096aed 100644
  11262. --- a/include/linux/interrupt.h
  11263. +++ b/include/linux/interrupt.h
  11264. @@ -61,6 +61,7 @@
  11265. * interrupt handler after suspending interrupts. For system
  11266. * wakeup devices users need to implement wakeup detection in
  11267. * their interrupt handlers.
  11268. + * IRQF_NO_SOFTIRQ_CALL - Do not process softirqs in the irq thread context (RT)
  11269. */
  11270. #define IRQF_SHARED 0x00000080
  11271. #define IRQF_PROBE_SHARED 0x00000100
  11272. @@ -74,6 +75,7 @@
  11273. #define IRQF_NO_THREAD 0x00010000
  11274. #define IRQF_EARLY_RESUME 0x00020000
  11275. #define IRQF_COND_SUSPEND 0x00040000
  11276. +#define IRQF_NO_SOFTIRQ_CALL 0x00080000
  11277. #define IRQF_TIMER (__IRQF_TIMER | IRQF_NO_SUSPEND | IRQF_NO_THREAD)
  11278. @@ -186,7 +188,7 @@ extern void devm_free_irq(struct device *dev, unsigned int irq, void *dev_id);
  11279. #ifdef CONFIG_LOCKDEP
  11280. # define local_irq_enable_in_hardirq() do { } while (0)
  11281. #else
  11282. -# define local_irq_enable_in_hardirq() local_irq_enable()
  11283. +# define local_irq_enable_in_hardirq() local_irq_enable_nort()
  11284. #endif
  11285. extern void disable_irq_nosync(unsigned int irq);
  11286. @@ -206,6 +208,7 @@ extern void resume_device_irqs(void);
  11287. * @irq: Interrupt to which notification applies
  11288. * @kref: Reference count, for internal use
  11289. * @work: Work item, for internal use
  11290. + * @list: List item for deferred callbacks
  11291. * @notify: Function to be called on change. This will be
  11292. * called in process context.
  11293. * @release: Function to be called on release. This will be
  11294. @@ -217,6 +220,7 @@ struct irq_affinity_notify {
  11295. unsigned int irq;
  11296. struct kref kref;
  11297. struct work_struct work;
  11298. + struct list_head list;
  11299. void (*notify)(struct irq_affinity_notify *, const cpumask_t *mask);
  11300. void (*release)(struct kref *ref);
  11301. };
  11302. @@ -379,9 +383,13 @@ extern int irq_set_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
  11303. bool state);
  11304. #ifdef CONFIG_IRQ_FORCED_THREADING
  11305. +# ifndef CONFIG_PREEMPT_RT_BASE
  11306. extern bool force_irqthreads;
  11307. +# else
  11308. +# define force_irqthreads (true)
  11309. +# endif
  11310. #else
  11311. -#define force_irqthreads (0)
  11312. +#define force_irqthreads (false)
  11313. #endif
  11314. #ifndef __ARCH_SET_SOFTIRQ_PENDING
  11315. @@ -438,9 +446,10 @@ struct softirq_action
  11316. void (*action)(struct softirq_action *);
  11317. };
  11318. +#ifndef CONFIG_PREEMPT_RT_FULL
  11319. asmlinkage void do_softirq(void);
  11320. asmlinkage void __do_softirq(void);
  11321. -
  11322. +static inline void thread_do_softirq(void) { do_softirq(); }
  11323. #ifdef __ARCH_HAS_DO_SOFTIRQ
  11324. void do_softirq_own_stack(void);
  11325. #else
  11326. @@ -449,13 +458,25 @@ static inline void do_softirq_own_stack(void)
  11327. __do_softirq();
  11328. }
  11329. #endif
  11330. +#else
  11331. +extern void thread_do_softirq(void);
  11332. +#endif
  11333. extern void open_softirq(int nr, void (*action)(struct softirq_action *));
  11334. extern void softirq_init(void);
  11335. extern void __raise_softirq_irqoff(unsigned int nr);
  11336. +#ifdef CONFIG_PREEMPT_RT_FULL
  11337. +extern void __raise_softirq_irqoff_ksoft(unsigned int nr);
  11338. +#else
  11339. +static inline void __raise_softirq_irqoff_ksoft(unsigned int nr)
  11340. +{
  11341. + __raise_softirq_irqoff(nr);
  11342. +}
  11343. +#endif
  11344. extern void raise_softirq_irqoff(unsigned int nr);
  11345. extern void raise_softirq(unsigned int nr);
  11346. +extern void softirq_check_pending_idle(void);
  11347. DECLARE_PER_CPU(struct task_struct *, ksoftirqd);
  11348. @@ -477,8 +498,9 @@ static inline struct task_struct *this_cpu_ksoftirqd(void)
  11349. to be executed on some cpu at least once after this.
  11350. * If the tasklet is already scheduled, but its execution is still not
  11351. started, it will be executed only once.
  11352. - * If this tasklet is already running on another CPU (or schedule is called
  11353. - from tasklet itself), it is rescheduled for later.
  11354. + * If this tasklet is already running on another CPU, it is rescheduled
  11355. + for later.
  11356. + * Schedule must not be called from the tasklet itself (a lockup occurs)
  11357. * Tasklet is strictly serialized wrt itself, but not
  11358. wrt another tasklets. If client needs some intertask synchronization,
  11359. he makes it with spinlocks.
  11360. @@ -503,27 +525,36 @@ struct tasklet_struct name = { NULL, 0, ATOMIC_INIT(1), func, data }
  11361. enum
  11362. {
  11363. TASKLET_STATE_SCHED, /* Tasklet is scheduled for execution */
  11364. - TASKLET_STATE_RUN /* Tasklet is running (SMP only) */
  11365. + TASKLET_STATE_RUN, /* Tasklet is running (SMP only) */
  11366. + TASKLET_STATE_PENDING /* Tasklet is pending */
  11367. };
  11368. -#ifdef CONFIG_SMP
  11369. +#define TASKLET_STATEF_SCHED (1 << TASKLET_STATE_SCHED)
  11370. +#define TASKLET_STATEF_RUN (1 << TASKLET_STATE_RUN)
  11371. +#define TASKLET_STATEF_PENDING (1 << TASKLET_STATE_PENDING)
  11372. +
  11373. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  11374. static inline int tasklet_trylock(struct tasklet_struct *t)
  11375. {
  11376. return !test_and_set_bit(TASKLET_STATE_RUN, &(t)->state);
  11377. }
  11378. +static inline int tasklet_tryunlock(struct tasklet_struct *t)
  11379. +{
  11380. + return cmpxchg(&t->state, TASKLET_STATEF_RUN, 0) == TASKLET_STATEF_RUN;
  11381. +}
  11382. +
  11383. static inline void tasklet_unlock(struct tasklet_struct *t)
  11384. {
  11385. smp_mb__before_atomic();
  11386. clear_bit(TASKLET_STATE_RUN, &(t)->state);
  11387. }
  11388. -static inline void tasklet_unlock_wait(struct tasklet_struct *t)
  11389. -{
  11390. - while (test_bit(TASKLET_STATE_RUN, &(t)->state)) { barrier(); }
  11391. -}
  11392. +extern void tasklet_unlock_wait(struct tasklet_struct *t);
  11393. +
  11394. #else
  11395. #define tasklet_trylock(t) 1
  11396. +#define tasklet_tryunlock(t) 1
  11397. #define tasklet_unlock_wait(t) do { } while (0)
  11398. #define tasklet_unlock(t) do { } while (0)
  11399. #endif
  11400. @@ -572,12 +603,7 @@ static inline void tasklet_disable(struct tasklet_struct *t)
  11401. smp_mb();
  11402. }
  11403. -static inline void tasklet_enable(struct tasklet_struct *t)
  11404. -{
  11405. - smp_mb__before_atomic();
  11406. - atomic_dec(&t->count);
  11407. -}
  11408. -
  11409. +extern void tasklet_enable(struct tasklet_struct *t);
  11410. extern void tasklet_kill(struct tasklet_struct *t);
  11411. extern void tasklet_kill_immediate(struct tasklet_struct *t, unsigned int cpu);
  11412. extern void tasklet_init(struct tasklet_struct *t,
  11413. @@ -608,6 +634,12 @@ void tasklet_hrtimer_cancel(struct tasklet_hrtimer *ttimer)
  11414. tasklet_kill(&ttimer->tasklet);
  11415. }
  11416. +#ifdef CONFIG_PREEMPT_RT_FULL
  11417. +extern void softirq_early_init(void);
  11418. +#else
  11419. +static inline void softirq_early_init(void) { }
  11420. +#endif
  11421. +
  11422. /*
  11423. * Autoprobing for irqs:
  11424. *
  11425. diff --git a/include/linux/irq.h b/include/linux/irq.h
  11426. index f7cade00c525..dac9e11ba037 100644
  11427. --- a/include/linux/irq.h
  11428. +++ b/include/linux/irq.h
  11429. @@ -72,6 +72,7 @@ enum irqchip_irq_state;
  11430. * IRQ_IS_POLLED - Always polled by another interrupt. Exclude
  11431. * it from the spurious interrupt detection
  11432. * mechanism and from core side polling.
  11433. + * IRQ_NO_SOFTIRQ_CALL - No softirq processing in the irq thread context (RT)
  11434. * IRQ_DISABLE_UNLAZY - Disable lazy irq disable
  11435. */
  11436. enum {
  11437. @@ -99,13 +100,14 @@ enum {
  11438. IRQ_PER_CPU_DEVID = (1 << 17),
  11439. IRQ_IS_POLLED = (1 << 18),
  11440. IRQ_DISABLE_UNLAZY = (1 << 19),
  11441. + IRQ_NO_SOFTIRQ_CALL = (1 << 20),
  11442. };
  11443. #define IRQF_MODIFY_MASK \
  11444. (IRQ_TYPE_SENSE_MASK | IRQ_NOPROBE | IRQ_NOREQUEST | \
  11445. IRQ_NOAUTOEN | IRQ_MOVE_PCNTXT | IRQ_LEVEL | IRQ_NO_BALANCING | \
  11446. IRQ_PER_CPU | IRQ_NESTED_THREAD | IRQ_NOTHREAD | IRQ_PER_CPU_DEVID | \
  11447. - IRQ_IS_POLLED | IRQ_DISABLE_UNLAZY)
  11448. + IRQ_IS_POLLED | IRQ_DISABLE_UNLAZY | IRQ_NO_SOFTIRQ_CALL)
  11449. #define IRQ_NO_BALANCING_MASK (IRQ_PER_CPU | IRQ_NO_BALANCING)
  11450. diff --git a/include/linux/irq_work.h b/include/linux/irq_work.h
  11451. index 47b9ebd4a74f..2543aab05daa 100644
  11452. --- a/include/linux/irq_work.h
  11453. +++ b/include/linux/irq_work.h
  11454. @@ -16,6 +16,7 @@
  11455. #define IRQ_WORK_BUSY 2UL
  11456. #define IRQ_WORK_FLAGS 3UL
  11457. #define IRQ_WORK_LAZY 4UL /* Doesn't want IPI, wait for tick */
  11458. +#define IRQ_WORK_HARD_IRQ 8UL /* Run hard IRQ context, even on RT */
  11459. struct irq_work {
  11460. unsigned long flags;
  11461. @@ -51,4 +52,10 @@ static inline bool irq_work_needs_cpu(void) { return false; }
  11462. static inline void irq_work_run(void) { }
  11463. #endif
  11464. +#if defined(CONFIG_IRQ_WORK) && defined(CONFIG_PREEMPT_RT_FULL)
  11465. +void irq_work_tick_soft(void);
  11466. +#else
  11467. +static inline void irq_work_tick_soft(void) { }
  11468. +#endif
  11469. +
  11470. #endif /* _LINUX_IRQ_WORK_H */
  11471. diff --git a/include/linux/irqdesc.h b/include/linux/irqdesc.h
  11472. index a587a33363c7..ad57402a242d 100644
  11473. --- a/include/linux/irqdesc.h
  11474. +++ b/include/linux/irqdesc.h
  11475. @@ -61,6 +61,7 @@ struct irq_desc {
  11476. unsigned int irqs_unhandled;
  11477. atomic_t threads_handled;
  11478. int threads_handled_last;
  11479. + u64 random_ip;
  11480. raw_spinlock_t lock;
  11481. struct cpumask *percpu_enabled;
  11482. #ifdef CONFIG_SMP
  11483. diff --git a/include/linux/irqflags.h b/include/linux/irqflags.h
  11484. index 5dd1272d1ab2..9b77034f7c5e 100644
  11485. --- a/include/linux/irqflags.h
  11486. +++ b/include/linux/irqflags.h
  11487. @@ -25,8 +25,6 @@
  11488. # define trace_softirqs_enabled(p) ((p)->softirqs_enabled)
  11489. # define trace_hardirq_enter() do { current->hardirq_context++; } while (0)
  11490. # define trace_hardirq_exit() do { current->hardirq_context--; } while (0)
  11491. -# define lockdep_softirq_enter() do { current->softirq_context++; } while (0)
  11492. -# define lockdep_softirq_exit() do { current->softirq_context--; } while (0)
  11493. # define INIT_TRACE_IRQFLAGS .softirqs_enabled = 1,
  11494. #else
  11495. # define trace_hardirqs_on() do { } while (0)
  11496. @@ -39,9 +37,15 @@
  11497. # define trace_softirqs_enabled(p) 0
  11498. # define trace_hardirq_enter() do { } while (0)
  11499. # define trace_hardirq_exit() do { } while (0)
  11500. +# define INIT_TRACE_IRQFLAGS
  11501. +#endif
  11502. +
  11503. +#if defined(CONFIG_TRACE_IRQFLAGS) && !defined(CONFIG_PREEMPT_RT_FULL)
  11504. +# define lockdep_softirq_enter() do { current->softirq_context++; } while (0)
  11505. +# define lockdep_softirq_exit() do { current->softirq_context--; } while (0)
  11506. +#else
  11507. # define lockdep_softirq_enter() do { } while (0)
  11508. # define lockdep_softirq_exit() do { } while (0)
  11509. -# define INIT_TRACE_IRQFLAGS
  11510. #endif
  11511. #if defined(CONFIG_IRQSOFF_TRACER) || \
  11512. @@ -148,4 +152,23 @@
  11513. #define irqs_disabled_flags(flags) raw_irqs_disabled_flags(flags)
  11514. +/*
  11515. + * local_irq* variants depending on RT/!RT
  11516. + */
  11517. +#ifdef CONFIG_PREEMPT_RT_FULL
  11518. +# define local_irq_disable_nort() do { } while (0)
  11519. +# define local_irq_enable_nort() do { } while (0)
  11520. +# define local_irq_save_nort(flags) local_save_flags(flags)
  11521. +# define local_irq_restore_nort(flags) (void)(flags)
  11522. +# define local_irq_disable_rt() local_irq_disable()
  11523. +# define local_irq_enable_rt() local_irq_enable()
  11524. +#else
  11525. +# define local_irq_disable_nort() local_irq_disable()
  11526. +# define local_irq_enable_nort() local_irq_enable()
  11527. +# define local_irq_save_nort(flags) local_irq_save(flags)
  11528. +# define local_irq_restore_nort(flags) local_irq_restore(flags)
  11529. +# define local_irq_disable_rt() do { } while (0)
  11530. +# define local_irq_enable_rt() do { } while (0)
  11531. +#endif
  11532. +
  11533. #endif
  11534. diff --git a/include/linux/jbd2.h b/include/linux/jbd2.h
  11535. index 65407f6c9120..eb5aabe4e18c 100644
  11536. --- a/include/linux/jbd2.h
  11537. +++ b/include/linux/jbd2.h
  11538. @@ -352,32 +352,56 @@ static inline struct journal_head *bh2jh(struct buffer_head *bh)
  11539. static inline void jbd_lock_bh_state(struct buffer_head *bh)
  11540. {
  11541. +#ifndef CONFIG_PREEMPT_RT_BASE
  11542. bit_spin_lock(BH_State, &bh->b_state);
  11543. +#else
  11544. + spin_lock(&bh->b_state_lock);
  11545. +#endif
  11546. }
  11547. static inline int jbd_trylock_bh_state(struct buffer_head *bh)
  11548. {
  11549. +#ifndef CONFIG_PREEMPT_RT_BASE
  11550. return bit_spin_trylock(BH_State, &bh->b_state);
  11551. +#else
  11552. + return spin_trylock(&bh->b_state_lock);
  11553. +#endif
  11554. }
  11555. static inline int jbd_is_locked_bh_state(struct buffer_head *bh)
  11556. {
  11557. +#ifndef CONFIG_PREEMPT_RT_BASE
  11558. return bit_spin_is_locked(BH_State, &bh->b_state);
  11559. +#else
  11560. + return spin_is_locked(&bh->b_state_lock);
  11561. +#endif
  11562. }
  11563. static inline void jbd_unlock_bh_state(struct buffer_head *bh)
  11564. {
  11565. +#ifndef CONFIG_PREEMPT_RT_BASE
  11566. bit_spin_unlock(BH_State, &bh->b_state);
  11567. +#else
  11568. + spin_unlock(&bh->b_state_lock);
  11569. +#endif
  11570. }
  11571. static inline void jbd_lock_bh_journal_head(struct buffer_head *bh)
  11572. {
  11573. +#ifndef CONFIG_PREEMPT_RT_BASE
  11574. bit_spin_lock(BH_JournalHead, &bh->b_state);
  11575. +#else
  11576. + spin_lock(&bh->b_journal_head_lock);
  11577. +#endif
  11578. }
  11579. static inline void jbd_unlock_bh_journal_head(struct buffer_head *bh)
  11580. {
  11581. +#ifndef CONFIG_PREEMPT_RT_BASE
  11582. bit_spin_unlock(BH_JournalHead, &bh->b_state);
  11583. +#else
  11584. + spin_unlock(&bh->b_journal_head_lock);
  11585. +#endif
  11586. }
  11587. #define J_ASSERT(assert) BUG_ON(!(assert))
  11588. diff --git a/include/linux/kdb.h b/include/linux/kdb.h
  11589. index a19bcf9e762e..897495386446 100644
  11590. --- a/include/linux/kdb.h
  11591. +++ b/include/linux/kdb.h
  11592. @@ -167,6 +167,7 @@ extern __printf(2, 0) int vkdb_printf(enum kdb_msgsrc src, const char *fmt,
  11593. extern __printf(1, 2) int kdb_printf(const char *, ...);
  11594. typedef __printf(1, 2) int (*kdb_printf_t)(const char *, ...);
  11595. +#define in_kdb_printk() (kdb_trap_printk)
  11596. extern void kdb_init(int level);
  11597. /* Access to kdb specific polling devices */
  11598. @@ -201,6 +202,7 @@ extern int kdb_register_flags(char *, kdb_func_t, char *, char *,
  11599. extern int kdb_unregister(char *);
  11600. #else /* ! CONFIG_KGDB_KDB */
  11601. static inline __printf(1, 2) int kdb_printf(const char *fmt, ...) { return 0; }
  11602. +#define in_kdb_printk() (0)
  11603. static inline void kdb_init(int level) {}
  11604. static inline int kdb_register(char *cmd, kdb_func_t func, char *usage,
  11605. char *help, short minlen) { return 0; }
  11606. diff --git a/include/linux/kernel.h b/include/linux/kernel.h
  11607. index 50220cab738c..d68f639f7330 100644
  11608. --- a/include/linux/kernel.h
  11609. +++ b/include/linux/kernel.h
  11610. @@ -188,6 +188,9 @@ extern int _cond_resched(void);
  11611. */
  11612. # define might_sleep() \
  11613. do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
  11614. +
  11615. +# define might_sleep_no_state_check() \
  11616. + do { ___might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
  11617. # define sched_annotate_sleep() (current->task_state_change = 0)
  11618. #else
  11619. static inline void ___might_sleep(const char *file, int line,
  11620. @@ -195,6 +198,7 @@ extern int _cond_resched(void);
  11621. static inline void __might_sleep(const char *file, int line,
  11622. int preempt_offset) { }
  11623. # define might_sleep() do { might_resched(); } while (0)
  11624. +# define might_sleep_no_state_check() do { might_resched(); } while (0)
  11625. # define sched_annotate_sleep() do { } while (0)
  11626. #endif
  11627. @@ -255,6 +259,7 @@ extern long (*panic_blink)(int state);
  11628. __printf(1, 2)
  11629. void panic(const char *fmt, ...)
  11630. __noreturn __cold;
  11631. +void nmi_panic(struct pt_regs *regs, const char *msg);
  11632. extern void oops_enter(void);
  11633. extern void oops_exit(void);
  11634. void print_oops_end_marker(void);
  11635. @@ -448,6 +453,14 @@ extern int sysctl_panic_on_stackoverflow;
  11636. extern bool crash_kexec_post_notifiers;
  11637. /*
  11638. + * panic_cpu is used for synchronizing panic() and crash_kexec() execution. It
  11639. + * holds a CPU number which is executing panic() currently. A value of
  11640. + * PANIC_CPU_INVALID means no CPU has entered panic() or crash_kexec().
  11641. + */
  11642. +extern atomic_t panic_cpu;
  11643. +#define PANIC_CPU_INVALID -1
  11644. +
  11645. +/*
  11646. * Only to be used by arch init code. If the user over-wrote the default
  11647. * CONFIG_PANIC_TIMEOUT, honor it.
  11648. */
  11649. @@ -475,6 +488,7 @@ extern enum system_states {
  11650. SYSTEM_HALT,
  11651. SYSTEM_POWER_OFF,
  11652. SYSTEM_RESTART,
  11653. + SYSTEM_SUSPEND,
  11654. } system_state;
  11655. #define TAINT_PROPRIETARY_MODULE 0
  11656. diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
  11657. index d7ce4e3280db..0b6d392b38e7 100644
  11658. --- a/include/linux/kvm_host.h
  11659. +++ b/include/linux/kvm_host.h
  11660. @@ -25,6 +25,7 @@
  11661. #include <linux/irqflags.h>
  11662. #include <linux/context_tracking.h>
  11663. #include <linux/irqbypass.h>
  11664. +#include <linux/swait.h>
  11665. #include <asm/signal.h>
  11666. #include <linux/kvm.h>
  11667. @@ -243,7 +244,7 @@ struct kvm_vcpu {
  11668. int fpu_active;
  11669. int guest_fpu_loaded, guest_xcr0_loaded;
  11670. unsigned char fpu_counter;
  11671. - wait_queue_head_t wq;
  11672. + struct swait_queue_head wq;
  11673. struct pid *pid;
  11674. int sigset_active;
  11675. sigset_t sigset;
  11676. @@ -794,7 +795,7 @@ static inline bool kvm_arch_has_assigned_device(struct kvm *kvm)
  11677. }
  11678. #endif
  11679. -static inline wait_queue_head_t *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu)
  11680. +static inline struct swait_queue_head *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu)
  11681. {
  11682. #ifdef __KVM_HAVE_ARCH_WQP
  11683. return vcpu->arch.wqp;
  11684. diff --git a/include/linux/lglock.h b/include/linux/lglock.h
  11685. index c92ebd100d9b..6f035f635d0e 100644
  11686. --- a/include/linux/lglock.h
  11687. +++ b/include/linux/lglock.h
  11688. @@ -34,13 +34,30 @@
  11689. #endif
  11690. struct lglock {
  11691. +#ifdef CONFIG_PREEMPT_RT_FULL
  11692. + struct rt_mutex __percpu *lock;
  11693. +#else
  11694. arch_spinlock_t __percpu *lock;
  11695. +#endif
  11696. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  11697. struct lock_class_key lock_key;
  11698. struct lockdep_map lock_dep_map;
  11699. #endif
  11700. };
  11701. +#ifdef CONFIG_PREEMPT_RT_FULL
  11702. +# define DEFINE_LGLOCK(name) \
  11703. + static DEFINE_PER_CPU(struct rt_mutex, name ## _lock) \
  11704. + = __RT_MUTEX_INITIALIZER( name ## _lock); \
  11705. + struct lglock name = { .lock = &name ## _lock }
  11706. +
  11707. +# define DEFINE_STATIC_LGLOCK(name) \
  11708. + static DEFINE_PER_CPU(struct rt_mutex, name ## _lock) \
  11709. + = __RT_MUTEX_INITIALIZER( name ## _lock); \
  11710. + static struct lglock name = { .lock = &name ## _lock }
  11711. +
  11712. +#else
  11713. +
  11714. #define DEFINE_LGLOCK(name) \
  11715. static DEFINE_PER_CPU(arch_spinlock_t, name ## _lock) \
  11716. = __ARCH_SPIN_LOCK_UNLOCKED; \
  11717. @@ -50,6 +67,7 @@ struct lglock {
  11718. static DEFINE_PER_CPU(arch_spinlock_t, name ## _lock) \
  11719. = __ARCH_SPIN_LOCK_UNLOCKED; \
  11720. static struct lglock name = { .lock = &name ## _lock }
  11721. +#endif
  11722. void lg_lock_init(struct lglock *lg, char *name);
  11723. @@ -64,6 +82,12 @@ void lg_double_unlock(struct lglock *lg, int cpu1, int cpu2);
  11724. void lg_global_lock(struct lglock *lg);
  11725. void lg_global_unlock(struct lglock *lg);
  11726. +#ifndef CONFIG_PREEMPT_RT_FULL
  11727. +#define lg_global_trylock_relax(name) lg_global_lock(name)
  11728. +#else
  11729. +void lg_global_trylock_relax(struct lglock *lg);
  11730. +#endif
  11731. +
  11732. #else
  11733. /* When !CONFIG_SMP, map lglock to spinlock */
  11734. #define lglock spinlock
  11735. diff --git a/include/linux/list_bl.h b/include/linux/list_bl.h
  11736. index 8132214e8efd..89ffaa7bd342 100644
  11737. --- a/include/linux/list_bl.h
  11738. +++ b/include/linux/list_bl.h
  11739. @@ -2,6 +2,7 @@
  11740. #define _LINUX_LIST_BL_H
  11741. #include <linux/list.h>
  11742. +#include <linux/spinlock.h>
  11743. #include <linux/bit_spinlock.h>
  11744. /*
  11745. @@ -32,13 +33,24 @@
  11746. struct hlist_bl_head {
  11747. struct hlist_bl_node *first;
  11748. +#ifdef CONFIG_PREEMPT_RT_BASE
  11749. + raw_spinlock_t lock;
  11750. +#endif
  11751. };
  11752. struct hlist_bl_node {
  11753. struct hlist_bl_node *next, **pprev;
  11754. };
  11755. -#define INIT_HLIST_BL_HEAD(ptr) \
  11756. - ((ptr)->first = NULL)
  11757. +
  11758. +#ifdef CONFIG_PREEMPT_RT_BASE
  11759. +#define INIT_HLIST_BL_HEAD(h) \
  11760. +do { \
  11761. + (h)->first = NULL; \
  11762. + raw_spin_lock_init(&(h)->lock); \
  11763. +} while (0)
  11764. +#else
  11765. +#define INIT_HLIST_BL_HEAD(h) (h)->first = NULL
  11766. +#endif
  11767. static inline void INIT_HLIST_BL_NODE(struct hlist_bl_node *h)
  11768. {
  11769. @@ -118,12 +130,26 @@ static inline void hlist_bl_del_init(struct hlist_bl_node *n)
  11770. static inline void hlist_bl_lock(struct hlist_bl_head *b)
  11771. {
  11772. +#ifndef CONFIG_PREEMPT_RT_BASE
  11773. bit_spin_lock(0, (unsigned long *)b);
  11774. +#else
  11775. + raw_spin_lock(&b->lock);
  11776. +#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  11777. + __set_bit(0, (unsigned long *)b);
  11778. +#endif
  11779. +#endif
  11780. }
  11781. static inline void hlist_bl_unlock(struct hlist_bl_head *b)
  11782. {
  11783. +#ifndef CONFIG_PREEMPT_RT_BASE
  11784. __bit_spin_unlock(0, (unsigned long *)b);
  11785. +#else
  11786. +#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  11787. + __clear_bit(0, (unsigned long *)b);
  11788. +#endif
  11789. + raw_spin_unlock(&b->lock);
  11790. +#endif
  11791. }
  11792. static inline bool hlist_bl_is_locked(struct hlist_bl_head *b)
  11793. diff --git a/include/linux/locallock.h b/include/linux/locallock.h
  11794. new file mode 100644
  11795. index 000000000000..e572a3971631
  11796. --- /dev/null
  11797. +++ b/include/linux/locallock.h
  11798. @@ -0,0 +1,276 @@
  11799. +#ifndef _LINUX_LOCALLOCK_H
  11800. +#define _LINUX_LOCALLOCK_H
  11801. +
  11802. +#include <linux/percpu.h>
  11803. +#include <linux/spinlock.h>
  11804. +
  11805. +#ifdef CONFIG_PREEMPT_RT_BASE
  11806. +
  11807. +#ifdef CONFIG_DEBUG_SPINLOCK
  11808. +# define LL_WARN(cond) WARN_ON(cond)
  11809. +#else
  11810. +# define LL_WARN(cond) do { } while (0)
  11811. +#endif
  11812. +
  11813. +/*
  11814. + * per cpu lock based substitute for local_irq_*()
  11815. + */
  11816. +struct local_irq_lock {
  11817. + spinlock_t lock;
  11818. + struct task_struct *owner;
  11819. + int nestcnt;
  11820. + unsigned long flags;
  11821. +};
  11822. +
  11823. +#define DEFINE_LOCAL_IRQ_LOCK(lvar) \
  11824. + DEFINE_PER_CPU(struct local_irq_lock, lvar) = { \
  11825. + .lock = __SPIN_LOCK_UNLOCKED((lvar).lock) }
  11826. +
  11827. +#define DECLARE_LOCAL_IRQ_LOCK(lvar) \
  11828. + DECLARE_PER_CPU(struct local_irq_lock, lvar)
  11829. +
  11830. +#define local_irq_lock_init(lvar) \
  11831. + do { \
  11832. + int __cpu; \
  11833. + for_each_possible_cpu(__cpu) \
  11834. + spin_lock_init(&per_cpu(lvar, __cpu).lock); \
  11835. + } while (0)
  11836. +
  11837. +/*
  11838. + * spin_lock|trylock|unlock_local flavour that does not migrate disable
  11839. + * used for __local_lock|trylock|unlock where get_local_var/put_local_var
  11840. + * already takes care of the migrate_disable/enable
  11841. + * for CONFIG_PREEMPT_BASE map to the normal spin_* calls.
  11842. + */
  11843. +#ifdef CONFIG_PREEMPT_RT_FULL
  11844. +# define spin_lock_local(lock) rt_spin_lock__no_mg(lock)
  11845. +# define spin_trylock_local(lock) rt_spin_trylock__no_mg(lock)
  11846. +# define spin_unlock_local(lock) rt_spin_unlock__no_mg(lock)
  11847. +#else
  11848. +# define spin_lock_local(lock) spin_lock(lock)
  11849. +# define spin_trylock_local(lock) spin_trylock(lock)
  11850. +# define spin_unlock_local(lock) spin_unlock(lock)
  11851. +#endif
  11852. +
  11853. +static inline void __local_lock(struct local_irq_lock *lv)
  11854. +{
  11855. + if (lv->owner != current) {
  11856. + spin_lock_local(&lv->lock);
  11857. + LL_WARN(lv->owner);
  11858. + LL_WARN(lv->nestcnt);
  11859. + lv->owner = current;
  11860. + }
  11861. + lv->nestcnt++;
  11862. +}
  11863. +
  11864. +#define local_lock(lvar) \
  11865. + do { __local_lock(&get_local_var(lvar)); } while (0)
  11866. +
  11867. +#define local_lock_on(lvar, cpu) \
  11868. + do { __local_lock(&per_cpu(lvar, cpu)); } while (0)
  11869. +
  11870. +static inline int __local_trylock(struct local_irq_lock *lv)
  11871. +{
  11872. + if (lv->owner != current && spin_trylock_local(&lv->lock)) {
  11873. + LL_WARN(lv->owner);
  11874. + LL_WARN(lv->nestcnt);
  11875. + lv->owner = current;
  11876. + lv->nestcnt = 1;
  11877. + return 1;
  11878. + }
  11879. + return 0;
  11880. +}
  11881. +
  11882. +#define local_trylock(lvar) \
  11883. + ({ \
  11884. + int __locked; \
  11885. + __locked = __local_trylock(&get_local_var(lvar)); \
  11886. + if (!__locked) \
  11887. + put_local_var(lvar); \
  11888. + __locked; \
  11889. + })
  11890. +
  11891. +static inline void __local_unlock(struct local_irq_lock *lv)
  11892. +{
  11893. + LL_WARN(lv->nestcnt == 0);
  11894. + LL_WARN(lv->owner != current);
  11895. + if (--lv->nestcnt)
  11896. + return;
  11897. +
  11898. + lv->owner = NULL;
  11899. + spin_unlock_local(&lv->lock);
  11900. +}
  11901. +
  11902. +#define local_unlock(lvar) \
  11903. + do { \
  11904. + __local_unlock(this_cpu_ptr(&lvar)); \
  11905. + put_local_var(lvar); \
  11906. + } while (0)
  11907. +
  11908. +#define local_unlock_on(lvar, cpu) \
  11909. + do { __local_unlock(&per_cpu(lvar, cpu)); } while (0)
  11910. +
  11911. +static inline void __local_lock_irq(struct local_irq_lock *lv)
  11912. +{
  11913. + spin_lock_irqsave(&lv->lock, lv->flags);
  11914. + LL_WARN(lv->owner);
  11915. + LL_WARN(lv->nestcnt);
  11916. + lv->owner = current;
  11917. + lv->nestcnt = 1;
  11918. +}
  11919. +
  11920. +#define local_lock_irq(lvar) \
  11921. + do { __local_lock_irq(&get_local_var(lvar)); } while (0)
  11922. +
  11923. +#define local_lock_irq_on(lvar, cpu) \
  11924. + do { __local_lock_irq(&per_cpu(lvar, cpu)); } while (0)
  11925. +
  11926. +static inline void __local_unlock_irq(struct local_irq_lock *lv)
  11927. +{
  11928. + LL_WARN(!lv->nestcnt);
  11929. + LL_WARN(lv->owner != current);
  11930. + lv->owner = NULL;
  11931. + lv->nestcnt = 0;
  11932. + spin_unlock_irq(&lv->lock);
  11933. +}
  11934. +
  11935. +#define local_unlock_irq(lvar) \
  11936. + do { \
  11937. + __local_unlock_irq(this_cpu_ptr(&lvar)); \
  11938. + put_local_var(lvar); \
  11939. + } while (0)
  11940. +
  11941. +#define local_unlock_irq_on(lvar, cpu) \
  11942. + do { \
  11943. + __local_unlock_irq(&per_cpu(lvar, cpu)); \
  11944. + } while (0)
  11945. +
  11946. +static inline int __local_lock_irqsave(struct local_irq_lock *lv)
  11947. +{
  11948. + if (lv->owner != current) {
  11949. + __local_lock_irq(lv);
  11950. + return 0;
  11951. + } else {
  11952. + lv->nestcnt++;
  11953. + return 1;
  11954. + }
  11955. +}
  11956. +
  11957. +#define local_lock_irqsave(lvar, _flags) \
  11958. + do { \
  11959. + if (__local_lock_irqsave(&get_local_var(lvar))) \
  11960. + put_local_var(lvar); \
  11961. + _flags = __this_cpu_read(lvar.flags); \
  11962. + } while (0)
  11963. +
  11964. +#define local_lock_irqsave_on(lvar, _flags, cpu) \
  11965. + do { \
  11966. + __local_lock_irqsave(&per_cpu(lvar, cpu)); \
  11967. + _flags = per_cpu(lvar, cpu).flags; \
  11968. + } while (0)
  11969. +
  11970. +static inline int __local_unlock_irqrestore(struct local_irq_lock *lv,
  11971. + unsigned long flags)
  11972. +{
  11973. + LL_WARN(!lv->nestcnt);
  11974. + LL_WARN(lv->owner != current);
  11975. + if (--lv->nestcnt)
  11976. + return 0;
  11977. +
  11978. + lv->owner = NULL;
  11979. + spin_unlock_irqrestore(&lv->lock, lv->flags);
  11980. + return 1;
  11981. +}
  11982. +
  11983. +#define local_unlock_irqrestore(lvar, flags) \
  11984. + do { \
  11985. + if (__local_unlock_irqrestore(this_cpu_ptr(&lvar), flags)) \
  11986. + put_local_var(lvar); \
  11987. + } while (0)
  11988. +
  11989. +#define local_unlock_irqrestore_on(lvar, flags, cpu) \
  11990. + do { \
  11991. + __local_unlock_irqrestore(&per_cpu(lvar, cpu), flags); \
  11992. + } while (0)
  11993. +
  11994. +#define local_spin_trylock_irq(lvar, lock) \
  11995. + ({ \
  11996. + int __locked; \
  11997. + local_lock_irq(lvar); \
  11998. + __locked = spin_trylock(lock); \
  11999. + if (!__locked) \
  12000. + local_unlock_irq(lvar); \
  12001. + __locked; \
  12002. + })
  12003. +
  12004. +#define local_spin_lock_irq(lvar, lock) \
  12005. + do { \
  12006. + local_lock_irq(lvar); \
  12007. + spin_lock(lock); \
  12008. + } while (0)
  12009. +
  12010. +#define local_spin_unlock_irq(lvar, lock) \
  12011. + do { \
  12012. + spin_unlock(lock); \
  12013. + local_unlock_irq(lvar); \
  12014. + } while (0)
  12015. +
  12016. +#define local_spin_lock_irqsave(lvar, lock, flags) \
  12017. + do { \
  12018. + local_lock_irqsave(lvar, flags); \
  12019. + spin_lock(lock); \
  12020. + } while (0)
  12021. +
  12022. +#define local_spin_unlock_irqrestore(lvar, lock, flags) \
  12023. + do { \
  12024. + spin_unlock(lock); \
  12025. + local_unlock_irqrestore(lvar, flags); \
  12026. + } while (0)
  12027. +
  12028. +#define get_locked_var(lvar, var) \
  12029. + (*({ \
  12030. + local_lock(lvar); \
  12031. + this_cpu_ptr(&var); \
  12032. + }))
  12033. +
  12034. +#define put_locked_var(lvar, var) local_unlock(lvar);
  12035. +
  12036. +#define local_lock_cpu(lvar) \
  12037. + ({ \
  12038. + local_lock(lvar); \
  12039. + smp_processor_id(); \
  12040. + })
  12041. +
  12042. +#define local_unlock_cpu(lvar) local_unlock(lvar)
  12043. +
  12044. +#else /* PREEMPT_RT_BASE */
  12045. +
  12046. +#define DEFINE_LOCAL_IRQ_LOCK(lvar) __typeof__(const int) lvar
  12047. +#define DECLARE_LOCAL_IRQ_LOCK(lvar) extern __typeof__(const int) lvar
  12048. +
  12049. +static inline void local_irq_lock_init(int lvar) { }
  12050. +
  12051. +#define local_lock(lvar) preempt_disable()
  12052. +#define local_unlock(lvar) preempt_enable()
  12053. +#define local_lock_irq(lvar) local_irq_disable()
  12054. +#define local_unlock_irq(lvar) local_irq_enable()
  12055. +#define local_lock_irqsave(lvar, flags) local_irq_save(flags)
  12056. +#define local_unlock_irqrestore(lvar, flags) local_irq_restore(flags)
  12057. +
  12058. +#define local_spin_trylock_irq(lvar, lock) spin_trylock_irq(lock)
  12059. +#define local_spin_lock_irq(lvar, lock) spin_lock_irq(lock)
  12060. +#define local_spin_unlock_irq(lvar, lock) spin_unlock_irq(lock)
  12061. +#define local_spin_lock_irqsave(lvar, lock, flags) \
  12062. + spin_lock_irqsave(lock, flags)
  12063. +#define local_spin_unlock_irqrestore(lvar, lock, flags) \
  12064. + spin_unlock_irqrestore(lock, flags)
  12065. +
  12066. +#define get_locked_var(lvar, var) get_cpu_var(var)
  12067. +#define put_locked_var(lvar, var) put_cpu_var(var)
  12068. +
  12069. +#define local_lock_cpu(lvar) get_cpu()
  12070. +#define local_unlock_cpu(lvar) put_cpu()
  12071. +
  12072. +#endif
  12073. +
  12074. +#endif
  12075. diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
  12076. index 2ccccbfcd532..4b016eb6265b 100644
  12077. --- a/include/linux/mm_types.h
  12078. +++ b/include/linux/mm_types.h
  12079. @@ -11,6 +11,7 @@
  12080. #include <linux/completion.h>
  12081. #include <linux/cpumask.h>
  12082. #include <linux/uprobes.h>
  12083. +#include <linux/rcupdate.h>
  12084. #include <linux/page-flags-layout.h>
  12085. #include <asm/page.h>
  12086. #include <asm/mmu.h>
  12087. @@ -505,6 +506,9 @@ struct mm_struct {
  12088. bool tlb_flush_pending;
  12089. #endif
  12090. struct uprobes_state uprobes_state;
  12091. +#ifdef CONFIG_PREEMPT_RT_BASE
  12092. + struct rcu_head delayed_drop;
  12093. +#endif
  12094. #ifdef CONFIG_X86_INTEL_MPX
  12095. /* address of the bounds directory */
  12096. void __user *bd_addr;
  12097. diff --git a/include/linux/module.h b/include/linux/module.h
  12098. index b229a9961d02..5fea847cf95c 100644
  12099. --- a/include/linux/module.h
  12100. +++ b/include/linux/module.h
  12101. @@ -500,6 +500,7 @@ static inline int module_is_live(struct module *mod)
  12102. struct module *__module_text_address(unsigned long addr);
  12103. struct module *__module_address(unsigned long addr);
  12104. bool is_module_address(unsigned long addr);
  12105. +bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr);
  12106. bool is_module_percpu_address(unsigned long addr);
  12107. bool is_module_text_address(unsigned long addr);
  12108. @@ -665,6 +666,11 @@ static inline bool is_module_percpu_address(unsigned long addr)
  12109. return false;
  12110. }
  12111. +static inline bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
  12112. +{
  12113. + return false;
  12114. +}
  12115. +
  12116. static inline bool is_module_text_address(unsigned long addr)
  12117. {
  12118. return false;
  12119. diff --git a/include/linux/mutex.h b/include/linux/mutex.h
  12120. index 2cb7531e7d7a..b3fdfc820216 100644
  12121. --- a/include/linux/mutex.h
  12122. +++ b/include/linux/mutex.h
  12123. @@ -19,6 +19,17 @@
  12124. #include <asm/processor.h>
  12125. #include <linux/osq_lock.h>
  12126. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12127. +# define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
  12128. + , .dep_map = { .name = #lockname }
  12129. +#else
  12130. +# define __DEP_MAP_MUTEX_INITIALIZER(lockname)
  12131. +#endif
  12132. +
  12133. +#ifdef CONFIG_PREEMPT_RT_FULL
  12134. +# include <linux/mutex_rt.h>
  12135. +#else
  12136. +
  12137. /*
  12138. * Simple, straightforward mutexes with strict semantics:
  12139. *
  12140. @@ -99,13 +110,6 @@ do { \
  12141. static inline void mutex_destroy(struct mutex *lock) {}
  12142. #endif
  12143. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12144. -# define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
  12145. - , .dep_map = { .name = #lockname }
  12146. -#else
  12147. -# define __DEP_MAP_MUTEX_INITIALIZER(lockname)
  12148. -#endif
  12149. -
  12150. #define __MUTEX_INITIALIZER(lockname) \
  12151. { .count = ATOMIC_INIT(1) \
  12152. , .wait_lock = __SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
  12153. @@ -173,6 +177,8 @@ extern int __must_check mutex_lock_killable(struct mutex *lock);
  12154. extern int mutex_trylock(struct mutex *lock);
  12155. extern void mutex_unlock(struct mutex *lock);
  12156. +#endif /* !PREEMPT_RT_FULL */
  12157. +
  12158. extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);
  12159. #endif /* __LINUX_MUTEX_H */
  12160. diff --git a/include/linux/mutex_rt.h b/include/linux/mutex_rt.h
  12161. new file mode 100644
  12162. index 000000000000..e0284edec655
  12163. --- /dev/null
  12164. +++ b/include/linux/mutex_rt.h
  12165. @@ -0,0 +1,89 @@
  12166. +#ifndef __LINUX_MUTEX_RT_H
  12167. +#define __LINUX_MUTEX_RT_H
  12168. +
  12169. +#ifndef __LINUX_MUTEX_H
  12170. +#error "Please include mutex.h"
  12171. +#endif
  12172. +
  12173. +#include <linux/rtmutex.h>
  12174. +
  12175. +/* FIXME: Just for __lockfunc */
  12176. +#include <linux/spinlock.h>
  12177. +
  12178. +struct mutex {
  12179. + struct rt_mutex lock;
  12180. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12181. + struct lockdep_map dep_map;
  12182. +#endif
  12183. +};
  12184. +
  12185. +#define __MUTEX_INITIALIZER(mutexname) \
  12186. + { \
  12187. + .lock = __RT_MUTEX_INITIALIZER(mutexname.lock) \
  12188. + __DEP_MAP_MUTEX_INITIALIZER(mutexname) \
  12189. + }
  12190. +
  12191. +#define DEFINE_MUTEX(mutexname) \
  12192. + struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
  12193. +
  12194. +extern void __mutex_do_init(struct mutex *lock, const char *name, struct lock_class_key *key);
  12195. +extern void __lockfunc _mutex_lock(struct mutex *lock);
  12196. +extern int __lockfunc _mutex_lock_interruptible(struct mutex *lock);
  12197. +extern int __lockfunc _mutex_lock_killable(struct mutex *lock);
  12198. +extern void __lockfunc _mutex_lock_nested(struct mutex *lock, int subclass);
  12199. +extern void __lockfunc _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest_lock);
  12200. +extern int __lockfunc _mutex_lock_interruptible_nested(struct mutex *lock, int subclass);
  12201. +extern int __lockfunc _mutex_lock_killable_nested(struct mutex *lock, int subclass);
  12202. +extern int __lockfunc _mutex_trylock(struct mutex *lock);
  12203. +extern void __lockfunc _mutex_unlock(struct mutex *lock);
  12204. +
  12205. +#define mutex_is_locked(l) rt_mutex_is_locked(&(l)->lock)
  12206. +#define mutex_lock(l) _mutex_lock(l)
  12207. +#define mutex_lock_interruptible(l) _mutex_lock_interruptible(l)
  12208. +#define mutex_lock_killable(l) _mutex_lock_killable(l)
  12209. +#define mutex_trylock(l) _mutex_trylock(l)
  12210. +#define mutex_unlock(l) _mutex_unlock(l)
  12211. +
  12212. +#ifdef CONFIG_DEBUG_MUTEXES
  12213. +#define mutex_destroy(l) rt_mutex_destroy(&(l)->lock)
  12214. +#else
  12215. +static inline void mutex_destroy(struct mutex *lock) {}
  12216. +#endif
  12217. +
  12218. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12219. +# define mutex_lock_nested(l, s) _mutex_lock_nested(l, s)
  12220. +# define mutex_lock_interruptible_nested(l, s) \
  12221. + _mutex_lock_interruptible_nested(l, s)
  12222. +# define mutex_lock_killable_nested(l, s) \
  12223. + _mutex_lock_killable_nested(l, s)
  12224. +
  12225. +# define mutex_lock_nest_lock(lock, nest_lock) \
  12226. +do { \
  12227. + typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
  12228. + _mutex_lock_nest_lock(lock, &(nest_lock)->dep_map); \
  12229. +} while (0)
  12230. +
  12231. +#else
  12232. +# define mutex_lock_nested(l, s) _mutex_lock(l)
  12233. +# define mutex_lock_interruptible_nested(l, s) \
  12234. + _mutex_lock_interruptible(l)
  12235. +# define mutex_lock_killable_nested(l, s) \
  12236. + _mutex_lock_killable(l)
  12237. +# define mutex_lock_nest_lock(lock, nest_lock) mutex_lock(lock)
  12238. +#endif
  12239. +
  12240. +# define mutex_init(mutex) \
  12241. +do { \
  12242. + static struct lock_class_key __key; \
  12243. + \
  12244. + rt_mutex_init(&(mutex)->lock); \
  12245. + __mutex_do_init((mutex), #mutex, &__key); \
  12246. +} while (0)
  12247. +
  12248. +# define __mutex_init(mutex, name, key) \
  12249. +do { \
  12250. + rt_mutex_init(&(mutex)->lock); \
  12251. + __mutex_do_init((mutex), name, key); \
  12252. +} while (0)
  12253. +
  12254. +#endif
  12255. diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
  12256. index 4035bbe40971..81ebe70d107d 100644
  12257. --- a/include/linux/netdevice.h
  12258. +++ b/include/linux/netdevice.h
  12259. @@ -390,7 +390,19 @@ typedef enum rx_handler_result rx_handler_result_t;
  12260. typedef rx_handler_result_t rx_handler_func_t(struct sk_buff **pskb);
  12261. void __napi_schedule(struct napi_struct *n);
  12262. +
  12263. +/*
  12264. + * When PREEMPT_RT_FULL is defined, all device interrupt handlers
  12265. + * run as threads, and they can also be preempted (without PREEMPT_RT
  12266. + * interrupt threads can not be preempted). Which means that calling
  12267. + * __napi_schedule_irqoff() from an interrupt handler can be preempted
  12268. + * and can corrupt the napi->poll_list.
  12269. + */
  12270. +#ifdef CONFIG_PREEMPT_RT_FULL
  12271. +#define __napi_schedule_irqoff(n) __napi_schedule(n)
  12272. +#else
  12273. void __napi_schedule_irqoff(struct napi_struct *n);
  12274. +#endif
  12275. static inline bool napi_disable_pending(struct napi_struct *n)
  12276. {
  12277. @@ -2288,11 +2300,20 @@ void netdev_freemem(struct net_device *dev);
  12278. void synchronize_net(void);
  12279. int init_dummy_netdev(struct net_device *dev);
  12280. +#ifdef CONFIG_PREEMPT_RT_FULL
  12281. +static inline int dev_recursion_level(void)
  12282. +{
  12283. + return current->xmit_recursion;
  12284. +}
  12285. +
  12286. +#else
  12287. +
  12288. DECLARE_PER_CPU(int, xmit_recursion);
  12289. static inline int dev_recursion_level(void)
  12290. {
  12291. return this_cpu_read(xmit_recursion);
  12292. }
  12293. +#endif
  12294. struct net_device *dev_get_by_index(struct net *net, int ifindex);
  12295. struct net_device *__dev_get_by_index(struct net *net, int ifindex);
  12296. @@ -2610,6 +2631,7 @@ struct softnet_data {
  12297. unsigned int dropped;
  12298. struct sk_buff_head input_pkt_queue;
  12299. struct napi_struct backlog;
  12300. + struct sk_buff_head tofree_queue;
  12301. };
  12302. diff --git a/include/linux/netfilter/x_tables.h b/include/linux/netfilter/x_tables.h
  12303. index 04078e8a4803..a61c9609e32f 100644
  12304. --- a/include/linux/netfilter/x_tables.h
  12305. +++ b/include/linux/netfilter/x_tables.h
  12306. @@ -4,6 +4,7 @@
  12307. #include <linux/netdevice.h>
  12308. #include <linux/static_key.h>
  12309. +#include <linux/locallock.h>
  12310. #include <uapi/linux/netfilter/x_tables.h>
  12311. /**
  12312. @@ -289,6 +290,8 @@ void xt_free_table_info(struct xt_table_info *info);
  12313. */
  12314. DECLARE_PER_CPU(seqcount_t, xt_recseq);
  12315. +DECLARE_LOCAL_IRQ_LOCK(xt_write_lock);
  12316. +
  12317. /* xt_tee_enabled - true if x_tables needs to handle reentrancy
  12318. *
  12319. * Enabled if current ip(6)tables ruleset has at least one -j TEE rule.
  12320. @@ -309,6 +312,9 @@ static inline unsigned int xt_write_recseq_begin(void)
  12321. {
  12322. unsigned int addend;
  12323. + /* RT protection */
  12324. + local_lock(xt_write_lock);
  12325. +
  12326. /*
  12327. * Low order bit of sequence is set if we already
  12328. * called xt_write_recseq_begin().
  12329. @@ -339,6 +345,7 @@ static inline void xt_write_recseq_end(unsigned int addend)
  12330. /* this is kind of a write_seqcount_end(), but addend is 0 or 1 */
  12331. smp_wmb();
  12332. __this_cpu_add(xt_recseq.sequence, addend);
  12333. + local_unlock(xt_write_lock);
  12334. }
  12335. /*
  12336. diff --git a/include/linux/notifier.h b/include/linux/notifier.h
  12337. index d14a4c362465..2e4414a0c1c4 100644
  12338. --- a/include/linux/notifier.h
  12339. +++ b/include/linux/notifier.h
  12340. @@ -6,7 +6,7 @@
  12341. *
  12342. * Alan Cox <Alan.Cox@linux.org>
  12343. */
  12344. -
  12345. +
  12346. #ifndef _LINUX_NOTIFIER_H
  12347. #define _LINUX_NOTIFIER_H
  12348. #include <linux/errno.h>
  12349. @@ -42,9 +42,7 @@
  12350. * in srcu_notifier_call_chain(): no cache bounces and no memory barriers.
  12351. * As compensation, srcu_notifier_chain_unregister() is rather expensive.
  12352. * SRCU notifier chains should be used when the chain will be called very
  12353. - * often but notifier_blocks will seldom be removed. Also, SRCU notifier
  12354. - * chains are slightly more difficult to use because they require special
  12355. - * runtime initialization.
  12356. + * often but notifier_blocks will seldom be removed.
  12357. */
  12358. typedef int (*notifier_fn_t)(struct notifier_block *nb,
  12359. @@ -88,7 +86,7 @@ struct srcu_notifier_head {
  12360. (name)->head = NULL; \
  12361. } while (0)
  12362. -/* srcu_notifier_heads must be initialized and cleaned up dynamically */
  12363. +/* srcu_notifier_heads must be cleaned up dynamically */
  12364. extern void srcu_init_notifier_head(struct srcu_notifier_head *nh);
  12365. #define srcu_cleanup_notifier_head(name) \
  12366. cleanup_srcu_struct(&(name)->srcu);
  12367. @@ -101,7 +99,13 @@ extern void srcu_init_notifier_head(struct srcu_notifier_head *nh);
  12368. .head = NULL }
  12369. #define RAW_NOTIFIER_INIT(name) { \
  12370. .head = NULL }
  12371. -/* srcu_notifier_heads cannot be initialized statically */
  12372. +
  12373. +#define SRCU_NOTIFIER_INIT(name, pcpu) \
  12374. + { \
  12375. + .mutex = __MUTEX_INITIALIZER(name.mutex), \
  12376. + .head = NULL, \
  12377. + .srcu = __SRCU_STRUCT_INIT(name.srcu, pcpu), \
  12378. + }
  12379. #define ATOMIC_NOTIFIER_HEAD(name) \
  12380. struct atomic_notifier_head name = \
  12381. @@ -113,6 +117,18 @@ extern void srcu_init_notifier_head(struct srcu_notifier_head *nh);
  12382. struct raw_notifier_head name = \
  12383. RAW_NOTIFIER_INIT(name)
  12384. +#define _SRCU_NOTIFIER_HEAD(name, mod) \
  12385. + static DEFINE_PER_CPU(struct srcu_struct_array, \
  12386. + name##_head_srcu_array); \
  12387. + mod struct srcu_notifier_head name = \
  12388. + SRCU_NOTIFIER_INIT(name, name##_head_srcu_array)
  12389. +
  12390. +#define SRCU_NOTIFIER_HEAD(name) \
  12391. + _SRCU_NOTIFIER_HEAD(name, )
  12392. +
  12393. +#define SRCU_NOTIFIER_HEAD_STATIC(name) \
  12394. + _SRCU_NOTIFIER_HEAD(name, static)
  12395. +
  12396. #ifdef __KERNEL__
  12397. extern int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
  12398. @@ -182,12 +198,12 @@ static inline int notifier_to_errno(int ret)
  12399. /*
  12400. * Declared notifiers so far. I can imagine quite a few more chains
  12401. - * over time (eg laptop power reset chains, reboot chain (to clean
  12402. + * over time (eg laptop power reset chains, reboot chain (to clean
  12403. * device units up), device [un]mount chain, module load/unload chain,
  12404. - * low memory chain, screenblank chain (for plug in modular screenblankers)
  12405. + * low memory chain, screenblank chain (for plug in modular screenblankers)
  12406. * VC switch chains (for loadable kernel svgalib VC switch helpers) etc...
  12407. */
  12408. -
  12409. +
  12410. /* CPU notfiers are defined in include/linux/cpu.h. */
  12411. /* netdevice notifiers are defined in include/linux/netdevice.h */
  12412. diff --git a/include/linux/percpu.h b/include/linux/percpu.h
  12413. index caebf2a758dc..4ecc057b6e27 100644
  12414. --- a/include/linux/percpu.h
  12415. +++ b/include/linux/percpu.h
  12416. @@ -24,6 +24,35 @@
  12417. PERCPU_MODULE_RESERVE)
  12418. #endif
  12419. +#ifdef CONFIG_PREEMPT_RT_FULL
  12420. +
  12421. +#define get_local_var(var) (*({ \
  12422. + migrate_disable(); \
  12423. + this_cpu_ptr(&var); }))
  12424. +
  12425. +#define put_local_var(var) do { \
  12426. + (void)&(var); \
  12427. + migrate_enable(); \
  12428. +} while (0)
  12429. +
  12430. +# define get_local_ptr(var) ({ \
  12431. + migrate_disable(); \
  12432. + this_cpu_ptr(var); })
  12433. +
  12434. +# define put_local_ptr(var) do { \
  12435. + (void)(var); \
  12436. + migrate_enable(); \
  12437. +} while (0)
  12438. +
  12439. +#else
  12440. +
  12441. +#define get_local_var(var) get_cpu_var(var)
  12442. +#define put_local_var(var) put_cpu_var(var)
  12443. +#define get_local_ptr(var) get_cpu_ptr(var)
  12444. +#define put_local_ptr(var) put_cpu_ptr(var)
  12445. +
  12446. +#endif
  12447. +
  12448. /* minimum unit size, also is the maximum supported allocation size */
  12449. #define PCPU_MIN_UNIT_SIZE PFN_ALIGN(32 << 10)
  12450. @@ -116,6 +145,7 @@ extern int __init pcpu_page_first_chunk(size_t reserved_size,
  12451. #endif
  12452. extern void __percpu *__alloc_reserved_percpu(size_t size, size_t align);
  12453. +extern bool __is_kernel_percpu_address(unsigned long addr, unsigned long *can_addr);
  12454. extern bool is_kernel_percpu_address(unsigned long addr);
  12455. #if !defined(CONFIG_SMP) || !defined(CONFIG_HAVE_SETUP_PER_CPU_AREA)
  12456. diff --git a/include/linux/pid.h b/include/linux/pid.h
  12457. index 23705a53abba..2cc64b779f03 100644
  12458. --- a/include/linux/pid.h
  12459. +++ b/include/linux/pid.h
  12460. @@ -2,6 +2,7 @@
  12461. #define _LINUX_PID_H
  12462. #include <linux/rcupdate.h>
  12463. +#include <linux/atomic.h>
  12464. enum pid_type
  12465. {
  12466. diff --git a/include/linux/preempt.h b/include/linux/preempt.h
  12467. index 75e4e30677f1..1cfb1cb72354 100644
  12468. --- a/include/linux/preempt.h
  12469. +++ b/include/linux/preempt.h
  12470. @@ -50,7 +50,11 @@
  12471. #define HARDIRQ_OFFSET (1UL << HARDIRQ_SHIFT)
  12472. #define NMI_OFFSET (1UL << NMI_SHIFT)
  12473. -#define SOFTIRQ_DISABLE_OFFSET (2 * SOFTIRQ_OFFSET)
  12474. +#ifndef CONFIG_PREEMPT_RT_FULL
  12475. +# define SOFTIRQ_DISABLE_OFFSET (2 * SOFTIRQ_OFFSET)
  12476. +#else
  12477. +# define SOFTIRQ_DISABLE_OFFSET (0)
  12478. +#endif
  12479. /* We use the MSB mostly because its available */
  12480. #define PREEMPT_NEED_RESCHED 0x80000000
  12481. @@ -59,9 +63,15 @@
  12482. #include <asm/preempt.h>
  12483. #define hardirq_count() (preempt_count() & HARDIRQ_MASK)
  12484. -#define softirq_count() (preempt_count() & SOFTIRQ_MASK)
  12485. #define irq_count() (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_MASK \
  12486. | NMI_MASK))
  12487. +#ifndef CONFIG_PREEMPT_RT_FULL
  12488. +# define softirq_count() (preempt_count() & SOFTIRQ_MASK)
  12489. +# define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET)
  12490. +#else
  12491. +# define softirq_count() (0UL)
  12492. +extern int in_serving_softirq(void);
  12493. +#endif
  12494. /*
  12495. * Are we doing bottom half or hardware interrupt processing?
  12496. @@ -72,7 +82,6 @@
  12497. #define in_irq() (hardirq_count())
  12498. #define in_softirq() (softirq_count())
  12499. #define in_interrupt() (irq_count())
  12500. -#define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET)
  12501. /*
  12502. * Are we in NMI context?
  12503. @@ -91,7 +100,11 @@
  12504. /*
  12505. * The preempt_count offset after spin_lock()
  12506. */
  12507. +#if !defined(CONFIG_PREEMPT_RT_FULL)
  12508. #define PREEMPT_LOCK_OFFSET PREEMPT_DISABLE_OFFSET
  12509. +#else
  12510. +#define PREEMPT_LOCK_OFFSET 0
  12511. +#endif
  12512. /*
  12513. * The preempt_count offset needed for things like:
  12514. @@ -140,6 +153,20 @@ extern void preempt_count_sub(int val);
  12515. #define preempt_count_inc() preempt_count_add(1)
  12516. #define preempt_count_dec() preempt_count_sub(1)
  12517. +#ifdef CONFIG_PREEMPT_LAZY
  12518. +#define add_preempt_lazy_count(val) do { preempt_lazy_count() += (val); } while (0)
  12519. +#define sub_preempt_lazy_count(val) do { preempt_lazy_count() -= (val); } while (0)
  12520. +#define inc_preempt_lazy_count() add_preempt_lazy_count(1)
  12521. +#define dec_preempt_lazy_count() sub_preempt_lazy_count(1)
  12522. +#define preempt_lazy_count() (current_thread_info()->preempt_lazy_count)
  12523. +#else
  12524. +#define add_preempt_lazy_count(val) do { } while (0)
  12525. +#define sub_preempt_lazy_count(val) do { } while (0)
  12526. +#define inc_preempt_lazy_count() do { } while (0)
  12527. +#define dec_preempt_lazy_count() do { } while (0)
  12528. +#define preempt_lazy_count() (0)
  12529. +#endif
  12530. +
  12531. #ifdef CONFIG_PREEMPT_COUNT
  12532. #define preempt_disable() \
  12533. @@ -148,13 +175,25 @@ do { \
  12534. barrier(); \
  12535. } while (0)
  12536. +#define preempt_lazy_disable() \
  12537. +do { \
  12538. + inc_preempt_lazy_count(); \
  12539. + barrier(); \
  12540. +} while (0)
  12541. +
  12542. #define sched_preempt_enable_no_resched() \
  12543. do { \
  12544. barrier(); \
  12545. preempt_count_dec(); \
  12546. } while (0)
  12547. -#define preempt_enable_no_resched() sched_preempt_enable_no_resched()
  12548. +#ifdef CONFIG_PREEMPT_RT_BASE
  12549. +# define preempt_enable_no_resched() sched_preempt_enable_no_resched()
  12550. +# define preempt_check_resched_rt() preempt_check_resched()
  12551. +#else
  12552. +# define preempt_enable_no_resched() preempt_enable()
  12553. +# define preempt_check_resched_rt() barrier();
  12554. +#endif
  12555. #define preemptible() (preempt_count() == 0 && !irqs_disabled())
  12556. @@ -179,6 +218,13 @@ do { \
  12557. __preempt_schedule(); \
  12558. } while (0)
  12559. +#define preempt_lazy_enable() \
  12560. +do { \
  12561. + dec_preempt_lazy_count(); \
  12562. + barrier(); \
  12563. + preempt_check_resched(); \
  12564. +} while (0)
  12565. +
  12566. #else /* !CONFIG_PREEMPT */
  12567. #define preempt_enable() \
  12568. do { \
  12569. @@ -224,6 +270,7 @@ do { \
  12570. #define preempt_disable_notrace() barrier()
  12571. #define preempt_enable_no_resched_notrace() barrier()
  12572. #define preempt_enable_notrace() barrier()
  12573. +#define preempt_check_resched_rt() barrier()
  12574. #define preemptible() 0
  12575. #endif /* CONFIG_PREEMPT_COUNT */
  12576. @@ -244,10 +291,31 @@ do { \
  12577. } while (0)
  12578. #define preempt_fold_need_resched() \
  12579. do { \
  12580. - if (tif_need_resched()) \
  12581. + if (tif_need_resched_now()) \
  12582. set_preempt_need_resched(); \
  12583. } while (0)
  12584. +#ifdef CONFIG_PREEMPT_RT_FULL
  12585. +# define preempt_disable_rt() preempt_disable()
  12586. +# define preempt_enable_rt() preempt_enable()
  12587. +# define preempt_disable_nort() barrier()
  12588. +# define preempt_enable_nort() barrier()
  12589. +# ifdef CONFIG_SMP
  12590. + extern void migrate_disable(void);
  12591. + extern void migrate_enable(void);
  12592. +# else /* CONFIG_SMP */
  12593. +# define migrate_disable() barrier()
  12594. +# define migrate_enable() barrier()
  12595. +# endif /* CONFIG_SMP */
  12596. +#else
  12597. +# define preempt_disable_rt() barrier()
  12598. +# define preempt_enable_rt() barrier()
  12599. +# define preempt_disable_nort() preempt_disable()
  12600. +# define preempt_enable_nort() preempt_enable()
  12601. +# define migrate_disable() preempt_disable()
  12602. +# define migrate_enable() preempt_enable()
  12603. +#endif
  12604. +
  12605. #ifdef CONFIG_PREEMPT_NOTIFIERS
  12606. struct preempt_notifier;
  12607. diff --git a/include/linux/printk.h b/include/linux/printk.h
  12608. index 9729565c25ff..9cdca696b718 100644
  12609. --- a/include/linux/printk.h
  12610. +++ b/include/linux/printk.h
  12611. @@ -117,9 +117,11 @@ int no_printk(const char *fmt, ...)
  12612. #ifdef CONFIG_EARLY_PRINTK
  12613. extern asmlinkage __printf(1, 2)
  12614. void early_printk(const char *fmt, ...);
  12615. +extern void printk_kill(void);
  12616. #else
  12617. static inline __printf(1, 2) __cold
  12618. void early_printk(const char *s, ...) { }
  12619. +static inline void printk_kill(void) { }
  12620. #endif
  12621. typedef __printf(1, 0) int (*printk_func_t)(const char *fmt, va_list args);
  12622. diff --git a/include/linux/radix-tree.h b/include/linux/radix-tree.h
  12623. index 5d5174b59802..327dddaf4c8f 100644
  12624. --- a/include/linux/radix-tree.h
  12625. +++ b/include/linux/radix-tree.h
  12626. @@ -279,6 +279,8 @@ unsigned int radix_tree_gang_lookup_slot(struct radix_tree_root *root,
  12627. unsigned long first_index, unsigned int max_items);
  12628. int radix_tree_preload(gfp_t gfp_mask);
  12629. int radix_tree_maybe_preload(gfp_t gfp_mask);
  12630. +void radix_tree_preload_end(void);
  12631. +
  12632. void radix_tree_init(void);
  12633. void *radix_tree_tag_set(struct radix_tree_root *root,
  12634. unsigned long index, unsigned int tag);
  12635. @@ -301,11 +303,6 @@ unsigned long radix_tree_range_tag_if_tagged(struct radix_tree_root *root,
  12636. int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag);
  12637. unsigned long radix_tree_locate_item(struct radix_tree_root *root, void *item);
  12638. -static inline void radix_tree_preload_end(void)
  12639. -{
  12640. - preempt_enable();
  12641. -}
  12642. -
  12643. /**
  12644. * struct radix_tree_iter - radix tree iterator state
  12645. *
  12646. diff --git a/include/linux/random.h b/include/linux/random.h
  12647. index a75840c1aa71..1a804361670c 100644
  12648. --- a/include/linux/random.h
  12649. +++ b/include/linux/random.h
  12650. @@ -20,7 +20,7 @@ struct random_ready_callback {
  12651. extern void add_device_randomness(const void *, unsigned int);
  12652. extern void add_input_randomness(unsigned int type, unsigned int code,
  12653. unsigned int value);
  12654. -extern void add_interrupt_randomness(int irq, int irq_flags);
  12655. +extern void add_interrupt_randomness(int irq, int irq_flags, __u64 ip);
  12656. extern void get_random_bytes(void *buf, int nbytes);
  12657. extern int add_random_ready_callback(struct random_ready_callback *rdy);
  12658. diff --git a/include/linux/rbtree.h b/include/linux/rbtree.h
  12659. index a5aa7ae671f4..24ddffd25492 100644
  12660. --- a/include/linux/rbtree.h
  12661. +++ b/include/linux/rbtree.h
  12662. @@ -31,7 +31,6 @@
  12663. #include <linux/kernel.h>
  12664. #include <linux/stddef.h>
  12665. -#include <linux/rcupdate.h>
  12666. struct rb_node {
  12667. unsigned long __rb_parent_color;
  12668. @@ -86,14 +85,8 @@ static inline void rb_link_node(struct rb_node *node, struct rb_node *parent,
  12669. *rb_link = node;
  12670. }
  12671. -static inline void rb_link_node_rcu(struct rb_node *node, struct rb_node *parent,
  12672. - struct rb_node **rb_link)
  12673. -{
  12674. - node->__rb_parent_color = (unsigned long)parent;
  12675. - node->rb_left = node->rb_right = NULL;
  12676. -
  12677. - rcu_assign_pointer(*rb_link, node);
  12678. -}
  12679. +void rb_link_node_rcu(struct rb_node *node, struct rb_node *parent,
  12680. + struct rb_node **rb_link);
  12681. #define rb_entry_safe(ptr, type, member) \
  12682. ({ typeof(ptr) ____ptr = (ptr); \
  12683. diff --git a/include/linux/rcupdate.h b/include/linux/rcupdate.h
  12684. index a0189ba67fde..c2f5f955163d 100644
  12685. --- a/include/linux/rcupdate.h
  12686. +++ b/include/linux/rcupdate.h
  12687. @@ -169,6 +169,9 @@ void call_rcu(struct rcu_head *head,
  12688. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  12689. +#ifdef CONFIG_PREEMPT_RT_FULL
  12690. +#define call_rcu_bh call_rcu
  12691. +#else
  12692. /**
  12693. * call_rcu_bh() - Queue an RCU for invocation after a quicker grace period.
  12694. * @head: structure to be used for queueing the RCU updates.
  12695. @@ -192,6 +195,7 @@ void call_rcu(struct rcu_head *head,
  12696. */
  12697. void call_rcu_bh(struct rcu_head *head,
  12698. rcu_callback_t func);
  12699. +#endif
  12700. /**
  12701. * call_rcu_sched() - Queue an RCU for invocation after sched grace period.
  12702. @@ -292,6 +296,11 @@ void synchronize_rcu(void);
  12703. * types of kernel builds, the rcu_read_lock() nesting depth is unknowable.
  12704. */
  12705. #define rcu_preempt_depth() (current->rcu_read_lock_nesting)
  12706. +#ifndef CONFIG_PREEMPT_RT_FULL
  12707. +#define sched_rcu_preempt_depth() rcu_preempt_depth()
  12708. +#else
  12709. +static inline int sched_rcu_preempt_depth(void) { return 0; }
  12710. +#endif
  12711. #else /* #ifdef CONFIG_PREEMPT_RCU */
  12712. @@ -317,6 +326,8 @@ static inline int rcu_preempt_depth(void)
  12713. return 0;
  12714. }
  12715. +#define sched_rcu_preempt_depth() rcu_preempt_depth()
  12716. +
  12717. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  12718. /* Internal to kernel */
  12719. @@ -489,7 +500,14 @@ extern struct lockdep_map rcu_callback_map;
  12720. int debug_lockdep_rcu_enabled(void);
  12721. int rcu_read_lock_held(void);
  12722. +#ifdef CONFIG_PREEMPT_RT_FULL
  12723. +static inline int rcu_read_lock_bh_held(void)
  12724. +{
  12725. + return rcu_read_lock_held();
  12726. +}
  12727. +#else
  12728. int rcu_read_lock_bh_held(void);
  12729. +#endif
  12730. /**
  12731. * rcu_read_lock_sched_held() - might we be in RCU-sched read-side critical section?
  12732. @@ -937,10 +955,14 @@ static inline void rcu_read_unlock(void)
  12733. static inline void rcu_read_lock_bh(void)
  12734. {
  12735. local_bh_disable();
  12736. +#ifdef CONFIG_PREEMPT_RT_FULL
  12737. + rcu_read_lock();
  12738. +#else
  12739. __acquire(RCU_BH);
  12740. rcu_lock_acquire(&rcu_bh_lock_map);
  12741. RCU_LOCKDEP_WARN(!rcu_is_watching(),
  12742. "rcu_read_lock_bh() used illegally while idle");
  12743. +#endif
  12744. }
  12745. /*
  12746. @@ -950,10 +972,14 @@ static inline void rcu_read_lock_bh(void)
  12747. */
  12748. static inline void rcu_read_unlock_bh(void)
  12749. {
  12750. +#ifdef CONFIG_PREEMPT_RT_FULL
  12751. + rcu_read_unlock();
  12752. +#else
  12753. RCU_LOCKDEP_WARN(!rcu_is_watching(),
  12754. "rcu_read_unlock_bh() used illegally while idle");
  12755. rcu_lock_release(&rcu_bh_lock_map);
  12756. __release(RCU_BH);
  12757. +#endif
  12758. local_bh_enable();
  12759. }
  12760. diff --git a/include/linux/rcutree.h b/include/linux/rcutree.h
  12761. index 60d15a080d7c..436c9e62bfc6 100644
  12762. --- a/include/linux/rcutree.h
  12763. +++ b/include/linux/rcutree.h
  12764. @@ -44,7 +44,11 @@ static inline void rcu_virt_note_context_switch(int cpu)
  12765. rcu_note_context_switch();
  12766. }
  12767. +#ifdef CONFIG_PREEMPT_RT_FULL
  12768. +# define synchronize_rcu_bh synchronize_rcu
  12769. +#else
  12770. void synchronize_rcu_bh(void);
  12771. +#endif
  12772. void synchronize_sched_expedited(void);
  12773. void synchronize_rcu_expedited(void);
  12774. @@ -72,7 +76,11 @@ static inline void synchronize_rcu_bh_expedited(void)
  12775. }
  12776. void rcu_barrier(void);
  12777. +#ifdef CONFIG_PREEMPT_RT_FULL
  12778. +# define rcu_barrier_bh rcu_barrier
  12779. +#else
  12780. void rcu_barrier_bh(void);
  12781. +#endif
  12782. void rcu_barrier_sched(void);
  12783. unsigned long get_state_synchronize_rcu(void);
  12784. void cond_synchronize_rcu(unsigned long oldstate);
  12785. @@ -85,12 +93,10 @@ unsigned long rcu_batches_started(void);
  12786. unsigned long rcu_batches_started_bh(void);
  12787. unsigned long rcu_batches_started_sched(void);
  12788. unsigned long rcu_batches_completed(void);
  12789. -unsigned long rcu_batches_completed_bh(void);
  12790. unsigned long rcu_batches_completed_sched(void);
  12791. void show_rcu_gp_kthreads(void);
  12792. void rcu_force_quiescent_state(void);
  12793. -void rcu_bh_force_quiescent_state(void);
  12794. void rcu_sched_force_quiescent_state(void);
  12795. void rcu_idle_enter(void);
  12796. @@ -105,6 +111,14 @@ extern int rcu_scheduler_active __read_mostly;
  12797. bool rcu_is_watching(void);
  12798. +#ifndef CONFIG_PREEMPT_RT_FULL
  12799. +void rcu_bh_force_quiescent_state(void);
  12800. +unsigned long rcu_batches_completed_bh(void);
  12801. +#else
  12802. +# define rcu_bh_force_quiescent_state rcu_force_quiescent_state
  12803. +# define rcu_batches_completed_bh rcu_batches_completed
  12804. +#endif
  12805. +
  12806. void rcu_all_qs(void);
  12807. #endif /* __LINUX_RCUTREE_H */
  12808. diff --git a/include/linux/rtmutex.h b/include/linux/rtmutex.h
  12809. index 1abba5ce2a2f..30211c627511 100644
  12810. --- a/include/linux/rtmutex.h
  12811. +++ b/include/linux/rtmutex.h
  12812. @@ -13,11 +13,15 @@
  12813. #define __LINUX_RT_MUTEX_H
  12814. #include <linux/linkage.h>
  12815. +#include <linux/spinlock_types_raw.h>
  12816. #include <linux/rbtree.h>
  12817. -#include <linux/spinlock_types.h>
  12818. extern int max_lock_depth; /* for sysctl */
  12819. +#ifdef CONFIG_DEBUG_MUTEXES
  12820. +#include <linux/debug_locks.h>
  12821. +#endif
  12822. +
  12823. /**
  12824. * The rt_mutex structure
  12825. *
  12826. @@ -31,8 +35,8 @@ struct rt_mutex {
  12827. struct rb_root waiters;
  12828. struct rb_node *waiters_leftmost;
  12829. struct task_struct *owner;
  12830. -#ifdef CONFIG_DEBUG_RT_MUTEXES
  12831. int save_state;
  12832. +#ifdef CONFIG_DEBUG_RT_MUTEXES
  12833. const char *name, *file;
  12834. int line;
  12835. void *magic;
  12836. @@ -55,22 +59,33 @@ struct hrtimer_sleeper;
  12837. # define rt_mutex_debug_check_no_locks_held(task) do { } while (0)
  12838. #endif
  12839. +# define rt_mutex_init(mutex) \
  12840. + do { \
  12841. + raw_spin_lock_init(&(mutex)->wait_lock); \
  12842. + __rt_mutex_init(mutex, #mutex); \
  12843. + } while (0)
  12844. +
  12845. #ifdef CONFIG_DEBUG_RT_MUTEXES
  12846. # define __DEBUG_RT_MUTEX_INITIALIZER(mutexname) \
  12847. , .name = #mutexname, .file = __FILE__, .line = __LINE__
  12848. -# define rt_mutex_init(mutex) __rt_mutex_init(mutex, __func__)
  12849. extern void rt_mutex_debug_task_free(struct task_struct *tsk);
  12850. #else
  12851. # define __DEBUG_RT_MUTEX_INITIALIZER(mutexname)
  12852. -# define rt_mutex_init(mutex) __rt_mutex_init(mutex, NULL)
  12853. # define rt_mutex_debug_task_free(t) do { } while (0)
  12854. #endif
  12855. -#define __RT_MUTEX_INITIALIZER(mutexname) \
  12856. - { .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(mutexname.wait_lock) \
  12857. +#define __RT_MUTEX_INITIALIZER_PLAIN(mutexname) \
  12858. + .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(mutexname.wait_lock) \
  12859. , .waiters = RB_ROOT \
  12860. , .owner = NULL \
  12861. - __DEBUG_RT_MUTEX_INITIALIZER(mutexname)}
  12862. + __DEBUG_RT_MUTEX_INITIALIZER(mutexname)
  12863. +
  12864. +#define __RT_MUTEX_INITIALIZER(mutexname) \
  12865. + { __RT_MUTEX_INITIALIZER_PLAIN(mutexname) }
  12866. +
  12867. +#define __RT_MUTEX_INITIALIZER_SAVE_STATE(mutexname) \
  12868. + { __RT_MUTEX_INITIALIZER_PLAIN(mutexname) \
  12869. + , .save_state = 1 }
  12870. #define DEFINE_RT_MUTEX(mutexname) \
  12871. struct rt_mutex mutexname = __RT_MUTEX_INITIALIZER(mutexname)
  12872. @@ -91,6 +106,7 @@ extern void rt_mutex_destroy(struct rt_mutex *lock);
  12873. extern void rt_mutex_lock(struct rt_mutex *lock);
  12874. extern int rt_mutex_lock_interruptible(struct rt_mutex *lock);
  12875. +extern int rt_mutex_lock_killable(struct rt_mutex *lock);
  12876. extern int rt_mutex_timed_lock(struct rt_mutex *lock,
  12877. struct hrtimer_sleeper *timeout);
  12878. diff --git a/include/linux/rwlock_rt.h b/include/linux/rwlock_rt.h
  12879. new file mode 100644
  12880. index 000000000000..49ed2d45d3be
  12881. --- /dev/null
  12882. +++ b/include/linux/rwlock_rt.h
  12883. @@ -0,0 +1,99 @@
  12884. +#ifndef __LINUX_RWLOCK_RT_H
  12885. +#define __LINUX_RWLOCK_RT_H
  12886. +
  12887. +#ifndef __LINUX_SPINLOCK_H
  12888. +#error Do not include directly. Use spinlock.h
  12889. +#endif
  12890. +
  12891. +#define rwlock_init(rwl) \
  12892. +do { \
  12893. + static struct lock_class_key __key; \
  12894. + \
  12895. + rt_mutex_init(&(rwl)->lock); \
  12896. + __rt_rwlock_init(rwl, #rwl, &__key); \
  12897. +} while (0)
  12898. +
  12899. +extern void __lockfunc rt_write_lock(rwlock_t *rwlock);
  12900. +extern void __lockfunc rt_read_lock(rwlock_t *rwlock);
  12901. +extern int __lockfunc rt_write_trylock(rwlock_t *rwlock);
  12902. +extern int __lockfunc rt_write_trylock_irqsave(rwlock_t *trylock, unsigned long *flags);
  12903. +extern int __lockfunc rt_read_trylock(rwlock_t *rwlock);
  12904. +extern void __lockfunc rt_write_unlock(rwlock_t *rwlock);
  12905. +extern void __lockfunc rt_read_unlock(rwlock_t *rwlock);
  12906. +extern unsigned long __lockfunc rt_write_lock_irqsave(rwlock_t *rwlock);
  12907. +extern unsigned long __lockfunc rt_read_lock_irqsave(rwlock_t *rwlock);
  12908. +extern void __rt_rwlock_init(rwlock_t *rwlock, char *name, struct lock_class_key *key);
  12909. +
  12910. +#define read_trylock(lock) __cond_lock(lock, rt_read_trylock(lock))
  12911. +#define write_trylock(lock) __cond_lock(lock, rt_write_trylock(lock))
  12912. +
  12913. +#define write_trylock_irqsave(lock, flags) \
  12914. + __cond_lock(lock, rt_write_trylock_irqsave(lock, &flags))
  12915. +
  12916. +#define read_lock_irqsave(lock, flags) \
  12917. + do { \
  12918. + typecheck(unsigned long, flags); \
  12919. + flags = rt_read_lock_irqsave(lock); \
  12920. + } while (0)
  12921. +
  12922. +#define write_lock_irqsave(lock, flags) \
  12923. + do { \
  12924. + typecheck(unsigned long, flags); \
  12925. + flags = rt_write_lock_irqsave(lock); \
  12926. + } while (0)
  12927. +
  12928. +#define read_lock(lock) rt_read_lock(lock)
  12929. +
  12930. +#define read_lock_bh(lock) \
  12931. + do { \
  12932. + local_bh_disable(); \
  12933. + rt_read_lock(lock); \
  12934. + } while (0)
  12935. +
  12936. +#define read_lock_irq(lock) read_lock(lock)
  12937. +
  12938. +#define write_lock(lock) rt_write_lock(lock)
  12939. +
  12940. +#define write_lock_bh(lock) \
  12941. + do { \
  12942. + local_bh_disable(); \
  12943. + rt_write_lock(lock); \
  12944. + } while (0)
  12945. +
  12946. +#define write_lock_irq(lock) write_lock(lock)
  12947. +
  12948. +#define read_unlock(lock) rt_read_unlock(lock)
  12949. +
  12950. +#define read_unlock_bh(lock) \
  12951. + do { \
  12952. + rt_read_unlock(lock); \
  12953. + local_bh_enable(); \
  12954. + } while (0)
  12955. +
  12956. +#define read_unlock_irq(lock) read_unlock(lock)
  12957. +
  12958. +#define write_unlock(lock) rt_write_unlock(lock)
  12959. +
  12960. +#define write_unlock_bh(lock) \
  12961. + do { \
  12962. + rt_write_unlock(lock); \
  12963. + local_bh_enable(); \
  12964. + } while (0)
  12965. +
  12966. +#define write_unlock_irq(lock) write_unlock(lock)
  12967. +
  12968. +#define read_unlock_irqrestore(lock, flags) \
  12969. + do { \
  12970. + typecheck(unsigned long, flags); \
  12971. + (void) flags; \
  12972. + rt_read_unlock(lock); \
  12973. + } while (0)
  12974. +
  12975. +#define write_unlock_irqrestore(lock, flags) \
  12976. + do { \
  12977. + typecheck(unsigned long, flags); \
  12978. + (void) flags; \
  12979. + rt_write_unlock(lock); \
  12980. + } while (0)
  12981. +
  12982. +#endif
  12983. diff --git a/include/linux/rwlock_types.h b/include/linux/rwlock_types.h
  12984. index cc0072e93e36..d0da966ad7a0 100644
  12985. --- a/include/linux/rwlock_types.h
  12986. +++ b/include/linux/rwlock_types.h
  12987. @@ -1,6 +1,10 @@
  12988. #ifndef __LINUX_RWLOCK_TYPES_H
  12989. #define __LINUX_RWLOCK_TYPES_H
  12990. +#if !defined(__LINUX_SPINLOCK_TYPES_H)
  12991. +# error "Do not include directly, include spinlock_types.h"
  12992. +#endif
  12993. +
  12994. /*
  12995. * include/linux/rwlock_types.h - generic rwlock type definitions
  12996. * and initializers
  12997. @@ -43,6 +47,7 @@ typedef struct {
  12998. RW_DEP_MAP_INIT(lockname) }
  12999. #endif
  13000. -#define DEFINE_RWLOCK(x) rwlock_t x = __RW_LOCK_UNLOCKED(x)
  13001. +#define DEFINE_RWLOCK(name) \
  13002. + rwlock_t name __cacheline_aligned_in_smp = __RW_LOCK_UNLOCKED(name)
  13003. #endif /* __LINUX_RWLOCK_TYPES_H */
  13004. diff --git a/include/linux/rwlock_types_rt.h b/include/linux/rwlock_types_rt.h
  13005. new file mode 100644
  13006. index 000000000000..b13832119591
  13007. --- /dev/null
  13008. +++ b/include/linux/rwlock_types_rt.h
  13009. @@ -0,0 +1,33 @@
  13010. +#ifndef __LINUX_RWLOCK_TYPES_RT_H
  13011. +#define __LINUX_RWLOCK_TYPES_RT_H
  13012. +
  13013. +#ifndef __LINUX_SPINLOCK_TYPES_H
  13014. +#error "Do not include directly. Include spinlock_types.h instead"
  13015. +#endif
  13016. +
  13017. +/*
  13018. + * rwlocks - rtmutex which allows single reader recursion
  13019. + */
  13020. +typedef struct {
  13021. + struct rt_mutex lock;
  13022. + int read_depth;
  13023. + unsigned int break_lock;
  13024. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  13025. + struct lockdep_map dep_map;
  13026. +#endif
  13027. +} rwlock_t;
  13028. +
  13029. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  13030. +# define RW_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }
  13031. +#else
  13032. +# define RW_DEP_MAP_INIT(lockname)
  13033. +#endif
  13034. +
  13035. +#define __RW_LOCK_UNLOCKED(name) \
  13036. + { .lock = __RT_MUTEX_INITIALIZER_SAVE_STATE(name.lock), \
  13037. + RW_DEP_MAP_INIT(name) }
  13038. +
  13039. +#define DEFINE_RWLOCK(name) \
  13040. + rwlock_t name __cacheline_aligned_in_smp = __RW_LOCK_UNLOCKED(name)
  13041. +
  13042. +#endif
  13043. diff --git a/include/linux/rwsem.h b/include/linux/rwsem.h
  13044. index 8f498cdde280..2b2148431f14 100644
  13045. --- a/include/linux/rwsem.h
  13046. +++ b/include/linux/rwsem.h
  13047. @@ -18,6 +18,10 @@
  13048. #include <linux/osq_lock.h>
  13049. #endif
  13050. +#ifdef CONFIG_PREEMPT_RT_FULL
  13051. +#include <linux/rwsem_rt.h>
  13052. +#else /* PREEMPT_RT_FULL */
  13053. +
  13054. struct rw_semaphore;
  13055. #ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
  13056. @@ -177,4 +181,6 @@ extern void up_read_non_owner(struct rw_semaphore *sem);
  13057. # define up_read_non_owner(sem) up_read(sem)
  13058. #endif
  13059. +#endif /* !PREEMPT_RT_FULL */
  13060. +
  13061. #endif /* _LINUX_RWSEM_H */
  13062. diff --git a/include/linux/rwsem_rt.h b/include/linux/rwsem_rt.h
  13063. new file mode 100644
  13064. index 000000000000..f97860b2e2a4
  13065. --- /dev/null
  13066. +++ b/include/linux/rwsem_rt.h
  13067. @@ -0,0 +1,152 @@
  13068. +#ifndef _LINUX_RWSEM_RT_H
  13069. +#define _LINUX_RWSEM_RT_H
  13070. +
  13071. +#ifndef _LINUX_RWSEM_H
  13072. +#error "Include rwsem.h"
  13073. +#endif
  13074. +
  13075. +/*
  13076. + * RW-semaphores are a spinlock plus a reader-depth count.
  13077. + *
  13078. + * Note that the semantics are different from the usual
  13079. + * Linux rw-sems, in PREEMPT_RT mode we do not allow
  13080. + * multiple readers to hold the lock at once, we only allow
  13081. + * a read-lock owner to read-lock recursively. This is
  13082. + * better for latency, makes the implementation inherently
  13083. + * fair and makes it simpler as well.
  13084. + */
  13085. +
  13086. +#include <linux/rtmutex.h>
  13087. +
  13088. +struct rw_semaphore {
  13089. + struct rt_mutex lock;
  13090. + int read_depth;
  13091. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  13092. + struct lockdep_map dep_map;
  13093. +#endif
  13094. +};
  13095. +
  13096. +#define __RWSEM_INITIALIZER(name) \
  13097. + { .lock = __RT_MUTEX_INITIALIZER(name.lock), \
  13098. + RW_DEP_MAP_INIT(name) }
  13099. +
  13100. +#define DECLARE_RWSEM(lockname) \
  13101. + struct rw_semaphore lockname = __RWSEM_INITIALIZER(lockname)
  13102. +
  13103. +extern void __rt_rwsem_init(struct rw_semaphore *rwsem, const char *name,
  13104. + struct lock_class_key *key);
  13105. +
  13106. +#define __rt_init_rwsem(sem, name, key) \
  13107. + do { \
  13108. + rt_mutex_init(&(sem)->lock); \
  13109. + __rt_rwsem_init((sem), (name), (key));\
  13110. + } while (0)
  13111. +
  13112. +#define __init_rwsem(sem, name, key) __rt_init_rwsem(sem, name, key)
  13113. +
  13114. +# define rt_init_rwsem(sem) \
  13115. +do { \
  13116. + static struct lock_class_key __key; \
  13117. + \
  13118. + __rt_init_rwsem((sem), #sem, &__key); \
  13119. +} while (0)
  13120. +
  13121. +extern void rt_down_write(struct rw_semaphore *rwsem);
  13122. +extern void rt_down_read_nested(struct rw_semaphore *rwsem, int subclass);
  13123. +extern void rt_down_write_nested(struct rw_semaphore *rwsem, int subclass);
  13124. +extern void rt_down_write_nested_lock(struct rw_semaphore *rwsem,
  13125. + struct lockdep_map *nest);
  13126. +extern void rt__down_read(struct rw_semaphore *rwsem);
  13127. +extern void rt_down_read(struct rw_semaphore *rwsem);
  13128. +extern int rt_down_write_trylock(struct rw_semaphore *rwsem);
  13129. +extern int rt__down_read_trylock(struct rw_semaphore *rwsem);
  13130. +extern int rt_down_read_trylock(struct rw_semaphore *rwsem);
  13131. +extern void __rt_up_read(struct rw_semaphore *rwsem);
  13132. +extern void rt_up_read(struct rw_semaphore *rwsem);
  13133. +extern void rt_up_write(struct rw_semaphore *rwsem);
  13134. +extern void rt_downgrade_write(struct rw_semaphore *rwsem);
  13135. +
  13136. +#define init_rwsem(sem) rt_init_rwsem(sem)
  13137. +#define rwsem_is_locked(s) rt_mutex_is_locked(&(s)->lock)
  13138. +
  13139. +static inline int rwsem_is_contended(struct rw_semaphore *sem)
  13140. +{
  13141. + /* rt_mutex_has_waiters() */
  13142. + return !RB_EMPTY_ROOT(&sem->lock.waiters);
  13143. +}
  13144. +
  13145. +static inline void __down_read(struct rw_semaphore *sem)
  13146. +{
  13147. + rt__down_read(sem);
  13148. +}
  13149. +
  13150. +static inline void down_read(struct rw_semaphore *sem)
  13151. +{
  13152. + rt_down_read(sem);
  13153. +}
  13154. +
  13155. +static inline int __down_read_trylock(struct rw_semaphore *sem)
  13156. +{
  13157. + return rt__down_read_trylock(sem);
  13158. +}
  13159. +
  13160. +static inline int down_read_trylock(struct rw_semaphore *sem)
  13161. +{
  13162. + return rt_down_read_trylock(sem);
  13163. +}
  13164. +
  13165. +static inline void down_write(struct rw_semaphore *sem)
  13166. +{
  13167. + rt_down_write(sem);
  13168. +}
  13169. +
  13170. +static inline int down_write_trylock(struct rw_semaphore *sem)
  13171. +{
  13172. + return rt_down_write_trylock(sem);
  13173. +}
  13174. +
  13175. +static inline void __up_read(struct rw_semaphore *sem)
  13176. +{
  13177. + __rt_up_read(sem);
  13178. +}
  13179. +
  13180. +static inline void up_read(struct rw_semaphore *sem)
  13181. +{
  13182. + rt_up_read(sem);
  13183. +}
  13184. +
  13185. +static inline void up_write(struct rw_semaphore *sem)
  13186. +{
  13187. + rt_up_write(sem);
  13188. +}
  13189. +
  13190. +static inline void downgrade_write(struct rw_semaphore *sem)
  13191. +{
  13192. + rt_downgrade_write(sem);
  13193. +}
  13194. +
  13195. +static inline void down_read_nested(struct rw_semaphore *sem, int subclass)
  13196. +{
  13197. + return rt_down_read_nested(sem, subclass);
  13198. +}
  13199. +
  13200. +static inline void down_write_nested(struct rw_semaphore *sem, int subclass)
  13201. +{
  13202. + rt_down_write_nested(sem, subclass);
  13203. +}
  13204. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  13205. +static inline void down_write_nest_lock(struct rw_semaphore *sem,
  13206. + struct rw_semaphore *nest_lock)
  13207. +{
  13208. + rt_down_write_nested_lock(sem, &nest_lock->dep_map);
  13209. +}
  13210. +
  13211. +#else
  13212. +
  13213. +static inline void down_write_nest_lock(struct rw_semaphore *sem,
  13214. + struct rw_semaphore *nest_lock)
  13215. +{
  13216. + rt_down_write_nested_lock(sem, NULL);
  13217. +}
  13218. +#endif
  13219. +#endif
  13220. diff --git a/include/linux/sched.h b/include/linux/sched.h
  13221. index 352213b360d7..1c7aa9446684 100644
  13222. --- a/include/linux/sched.h
  13223. +++ b/include/linux/sched.h
  13224. @@ -26,6 +26,7 @@ struct sched_param {
  13225. #include <linux/nodemask.h>
  13226. #include <linux/mm_types.h>
  13227. #include <linux/preempt.h>
  13228. +#include <asm/kmap_types.h>
  13229. #include <asm/page.h>
  13230. #include <asm/ptrace.h>
  13231. @@ -182,8 +183,6 @@ extern void update_cpu_load_nohz(void);
  13232. static inline void update_cpu_load_nohz(void) { }
  13233. #endif
  13234. -extern unsigned long get_parent_ip(unsigned long addr);
  13235. -
  13236. extern void dump_cpu_task(int cpu);
  13237. struct seq_file;
  13238. @@ -242,10 +241,7 @@ extern char ___assert_task_state[1 - 2*!!(
  13239. TASK_UNINTERRUPTIBLE | __TASK_STOPPED | \
  13240. __TASK_TRACED | EXIT_ZOMBIE | EXIT_DEAD)
  13241. -#define task_is_traced(task) ((task->state & __TASK_TRACED) != 0)
  13242. #define task_is_stopped(task) ((task->state & __TASK_STOPPED) != 0)
  13243. -#define task_is_stopped_or_traced(task) \
  13244. - ((task->state & (__TASK_STOPPED | __TASK_TRACED)) != 0)
  13245. #define task_contributes_to_load(task) \
  13246. ((task->state & TASK_UNINTERRUPTIBLE) != 0 && \
  13247. (task->flags & PF_FROZEN) == 0 && \
  13248. @@ -311,6 +307,11 @@ extern char ___assert_task_state[1 - 2*!!(
  13249. #endif
  13250. +#define __set_current_state_no_track(state_value) \
  13251. + do { current->state = (state_value); } while (0)
  13252. +#define set_current_state_no_track(state_value) \
  13253. + set_mb(current->state, (state_value))
  13254. +
  13255. /* Task command name length */
  13256. #define TASK_COMM_LEN 16
  13257. @@ -970,8 +971,18 @@ struct wake_q_head {
  13258. struct wake_q_head name = { WAKE_Q_TAIL, &name.first }
  13259. extern void wake_q_add(struct wake_q_head *head,
  13260. - struct task_struct *task);
  13261. -extern void wake_up_q(struct wake_q_head *head);
  13262. + struct task_struct *task);
  13263. +extern void __wake_up_q(struct wake_q_head *head, bool sleeper);
  13264. +
  13265. +static inline void wake_up_q(struct wake_q_head *head)
  13266. +{
  13267. + __wake_up_q(head, false);
  13268. +}
  13269. +
  13270. +static inline void wake_up_q_sleeper(struct wake_q_head *head)
  13271. +{
  13272. + __wake_up_q(head, true);
  13273. +}
  13274. /*
  13275. * sched-domains (multiprocessor balancing) declarations:
  13276. @@ -1379,6 +1390,7 @@ struct tlbflush_unmap_batch {
  13277. struct task_struct {
  13278. volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
  13279. + volatile long saved_state; /* saved state for "spinlock sleepers" */
  13280. void *stack;
  13281. atomic_t usage;
  13282. unsigned int flags; /* per process flags, defined below */
  13283. @@ -1415,6 +1427,12 @@ struct task_struct {
  13284. #endif
  13285. unsigned int policy;
  13286. +#ifdef CONFIG_PREEMPT_RT_FULL
  13287. + int migrate_disable;
  13288. +# ifdef CONFIG_SCHED_DEBUG
  13289. + int migrate_disable_atomic;
  13290. +# endif
  13291. +#endif
  13292. int nr_cpus_allowed;
  13293. cpumask_t cpus_allowed;
  13294. @@ -1526,11 +1544,14 @@ struct task_struct {
  13295. cputime_t gtime;
  13296. struct prev_cputime prev_cputime;
  13297. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  13298. - seqlock_t vtime_seqlock;
  13299. + seqcount_t vtime_seqcount;
  13300. unsigned long long vtime_snap;
  13301. enum {
  13302. - VTIME_SLEEPING = 0,
  13303. + /* Task is sleeping or running in a CPU with VTIME inactive */
  13304. + VTIME_INACTIVE = 0,
  13305. + /* Task runs in userspace in a CPU with VTIME active */
  13306. VTIME_USER,
  13307. + /* Task runs in kernelspace in a CPU with VTIME active */
  13308. VTIME_SYS,
  13309. } vtime_snap_whence;
  13310. #endif
  13311. @@ -1542,6 +1563,9 @@ struct task_struct {
  13312. struct task_cputime cputime_expires;
  13313. struct list_head cpu_timers[3];
  13314. +#ifdef CONFIG_PREEMPT_RT_BASE
  13315. + struct task_struct *posix_timer_list;
  13316. +#endif
  13317. /* process credentials */
  13318. const struct cred __rcu *ptracer_cred; /* Tracer's credentials at attach */
  13319. @@ -1573,10 +1597,15 @@ struct task_struct {
  13320. /* signal handlers */
  13321. struct signal_struct *signal;
  13322. struct sighand_struct *sighand;
  13323. + struct sigqueue *sigqueue_cache;
  13324. sigset_t blocked, real_blocked;
  13325. sigset_t saved_sigmask; /* restored if set_restore_sigmask() was used */
  13326. struct sigpending pending;
  13327. +#ifdef CONFIG_PREEMPT_RT_FULL
  13328. + /* TODO: move me into ->restart_block ? */
  13329. + struct siginfo forced_info;
  13330. +#endif
  13331. unsigned long sas_ss_sp;
  13332. size_t sas_ss_size;
  13333. @@ -1800,6 +1829,12 @@ struct task_struct {
  13334. unsigned long trace;
  13335. /* bitmask and counter of trace recursion */
  13336. unsigned long trace_recursion;
  13337. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  13338. + u64 preempt_timestamp_hist;
  13339. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  13340. + long timer_offset;
  13341. +#endif
  13342. +#endif
  13343. #endif /* CONFIG_TRACING */
  13344. #ifdef CONFIG_MEMCG
  13345. struct mem_cgroup *memcg_in_oom;
  13346. @@ -1816,9 +1851,23 @@ struct task_struct {
  13347. unsigned int sequential_io;
  13348. unsigned int sequential_io_avg;
  13349. #endif
  13350. +#ifdef CONFIG_PREEMPT_RT_BASE
  13351. + struct rcu_head put_rcu;
  13352. + int softirq_nestcnt;
  13353. + unsigned int softirqs_raised;
  13354. +#endif
  13355. +#ifdef CONFIG_PREEMPT_RT_FULL
  13356. +# if defined CONFIG_HIGHMEM || defined CONFIG_X86_32
  13357. + int kmap_idx;
  13358. + pte_t kmap_pte[KM_TYPE_NR];
  13359. +# endif
  13360. +#endif
  13361. #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
  13362. unsigned long task_state_change;
  13363. #endif
  13364. +#ifdef CONFIG_PREEMPT_RT_FULL
  13365. + int xmit_recursion;
  13366. +#endif
  13367. int pagefault_disabled;
  13368. /* CPU-specific state of this task */
  13369. struct thread_struct thread;
  13370. @@ -1836,9 +1885,6 @@ extern int arch_task_struct_size __read_mostly;
  13371. # define arch_task_struct_size (sizeof(struct task_struct))
  13372. #endif
  13373. -/* Future-safe accessor for struct task_struct's cpus_allowed. */
  13374. -#define tsk_cpus_allowed(tsk) (&(tsk)->cpus_allowed)
  13375. -
  13376. #define TNF_MIGRATED 0x01
  13377. #define TNF_NO_GROUP 0x02
  13378. #define TNF_SHARED 0x04
  13379. @@ -2028,6 +2074,15 @@ extern struct pid *cad_pid;
  13380. extern void free_task(struct task_struct *tsk);
  13381. #define get_task_struct(tsk) do { atomic_inc(&(tsk)->usage); } while(0)
  13382. +#ifdef CONFIG_PREEMPT_RT_BASE
  13383. +extern void __put_task_struct_cb(struct rcu_head *rhp);
  13384. +
  13385. +static inline void put_task_struct(struct task_struct *t)
  13386. +{
  13387. + if (atomic_dec_and_test(&t->usage))
  13388. + call_rcu(&t->put_rcu, __put_task_struct_cb);
  13389. +}
  13390. +#else
  13391. extern void __put_task_struct(struct task_struct *t);
  13392. static inline void put_task_struct(struct task_struct *t)
  13393. @@ -2035,6 +2090,7 @@ static inline void put_task_struct(struct task_struct *t)
  13394. if (atomic_dec_and_test(&t->usage))
  13395. __put_task_struct(t);
  13396. }
  13397. +#endif
  13398. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  13399. extern void task_cputime(struct task_struct *t,
  13400. @@ -2073,6 +2129,7 @@ extern void thread_group_cputime_adjusted(struct task_struct *p, cputime_t *ut,
  13401. /*
  13402. * Per process flags
  13403. */
  13404. +#define PF_IN_SOFTIRQ 0x00000001 /* Task is serving softirq */
  13405. #define PF_EXITING 0x00000004 /* getting shut down */
  13406. #define PF_EXITPIDONE 0x00000008 /* pi exit done on shut down */
  13407. #define PF_VCPU 0x00000010 /* I'm a virtual CPU */
  13408. @@ -2237,6 +2294,10 @@ extern void do_set_cpus_allowed(struct task_struct *p,
  13409. extern int set_cpus_allowed_ptr(struct task_struct *p,
  13410. const struct cpumask *new_mask);
  13411. +int migrate_me(void);
  13412. +void tell_sched_cpu_down_begin(int cpu);
  13413. +void tell_sched_cpu_down_done(int cpu);
  13414. +
  13415. #else
  13416. static inline void do_set_cpus_allowed(struct task_struct *p,
  13417. const struct cpumask *new_mask)
  13418. @@ -2249,6 +2310,9 @@ static inline int set_cpus_allowed_ptr(struct task_struct *p,
  13419. return -EINVAL;
  13420. return 0;
  13421. }
  13422. +static inline int migrate_me(void) { return 0; }
  13423. +static inline void tell_sched_cpu_down_begin(int cpu) { }
  13424. +static inline void tell_sched_cpu_down_done(int cpu) { }
  13425. #endif
  13426. #ifdef CONFIG_NO_HZ_COMMON
  13427. @@ -2458,6 +2522,7 @@ extern void xtime_update(unsigned long ticks);
  13428. extern int wake_up_state(struct task_struct *tsk, unsigned int state);
  13429. extern int wake_up_process(struct task_struct *tsk);
  13430. +extern int wake_up_lock_sleeper(struct task_struct * tsk);
  13431. extern void wake_up_new_task(struct task_struct *tsk);
  13432. #ifdef CONFIG_SMP
  13433. extern void kick_process(struct task_struct *tsk);
  13434. @@ -2581,12 +2646,24 @@ extern struct mm_struct * mm_alloc(void);
  13435. /* mmdrop drops the mm and the page tables */
  13436. extern void __mmdrop(struct mm_struct *);
  13437. +
  13438. static inline void mmdrop(struct mm_struct * mm)
  13439. {
  13440. if (unlikely(atomic_dec_and_test(&mm->mm_count)))
  13441. __mmdrop(mm);
  13442. }
  13443. +#ifdef CONFIG_PREEMPT_RT_BASE
  13444. +extern void __mmdrop_delayed(struct rcu_head *rhp);
  13445. +static inline void mmdrop_delayed(struct mm_struct *mm)
  13446. +{
  13447. + if (atomic_dec_and_test(&mm->mm_count))
  13448. + call_rcu(&mm->delayed_drop, __mmdrop_delayed);
  13449. +}
  13450. +#else
  13451. +# define mmdrop_delayed(mm) mmdrop(mm)
  13452. +#endif
  13453. +
  13454. /* mmput gets rid of the mappings and all user-space */
  13455. extern void mmput(struct mm_struct *);
  13456. /* Grab a reference to a task's mm, if it is not already going away */
  13457. @@ -2896,6 +2973,43 @@ static inline int test_tsk_need_resched(struct task_struct *tsk)
  13458. return unlikely(test_tsk_thread_flag(tsk,TIF_NEED_RESCHED));
  13459. }
  13460. +#ifdef CONFIG_PREEMPT_LAZY
  13461. +static inline void set_tsk_need_resched_lazy(struct task_struct *tsk)
  13462. +{
  13463. + set_tsk_thread_flag(tsk,TIF_NEED_RESCHED_LAZY);
  13464. +}
  13465. +
  13466. +static inline void clear_tsk_need_resched_lazy(struct task_struct *tsk)
  13467. +{
  13468. + clear_tsk_thread_flag(tsk,TIF_NEED_RESCHED_LAZY);
  13469. +}
  13470. +
  13471. +static inline int test_tsk_need_resched_lazy(struct task_struct *tsk)
  13472. +{
  13473. + return unlikely(test_tsk_thread_flag(tsk,TIF_NEED_RESCHED_LAZY));
  13474. +}
  13475. +
  13476. +static inline int need_resched_lazy(void)
  13477. +{
  13478. + return test_thread_flag(TIF_NEED_RESCHED_LAZY);
  13479. +}
  13480. +
  13481. +static inline int need_resched_now(void)
  13482. +{
  13483. + return test_thread_flag(TIF_NEED_RESCHED);
  13484. +}
  13485. +
  13486. +#else
  13487. +static inline void clear_tsk_need_resched_lazy(struct task_struct *tsk) { }
  13488. +static inline int need_resched_lazy(void) { return 0; }
  13489. +
  13490. +static inline int need_resched_now(void)
  13491. +{
  13492. + return test_thread_flag(TIF_NEED_RESCHED);
  13493. +}
  13494. +
  13495. +#endif
  13496. +
  13497. static inline int restart_syscall(void)
  13498. {
  13499. set_tsk_thread_flag(current, TIF_SIGPENDING);
  13500. @@ -2927,6 +3041,51 @@ static inline int signal_pending_state(long state, struct task_struct *p)
  13501. return (state & TASK_INTERRUPTIBLE) || __fatal_signal_pending(p);
  13502. }
  13503. +static inline bool __task_is_stopped_or_traced(struct task_struct *task)
  13504. +{
  13505. + if (task->state & (__TASK_STOPPED | __TASK_TRACED))
  13506. + return true;
  13507. +#ifdef CONFIG_PREEMPT_RT_FULL
  13508. + if (task->saved_state & (__TASK_STOPPED | __TASK_TRACED))
  13509. + return true;
  13510. +#endif
  13511. + return false;
  13512. +}
  13513. +
  13514. +static inline bool task_is_stopped_or_traced(struct task_struct *task)
  13515. +{
  13516. + bool traced_stopped;
  13517. +
  13518. +#ifdef CONFIG_PREEMPT_RT_FULL
  13519. + unsigned long flags;
  13520. +
  13521. + raw_spin_lock_irqsave(&task->pi_lock, flags);
  13522. + traced_stopped = __task_is_stopped_or_traced(task);
  13523. + raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  13524. +#else
  13525. + traced_stopped = __task_is_stopped_or_traced(task);
  13526. +#endif
  13527. + return traced_stopped;
  13528. +}
  13529. +
  13530. +static inline bool task_is_traced(struct task_struct *task)
  13531. +{
  13532. + bool traced = false;
  13533. +
  13534. + if (task->state & __TASK_TRACED)
  13535. + return true;
  13536. +#ifdef CONFIG_PREEMPT_RT_FULL
  13537. + /* in case the task is sleeping on tasklist_lock */
  13538. + raw_spin_lock_irq(&task->pi_lock);
  13539. + if (task->state & __TASK_TRACED)
  13540. + traced = true;
  13541. + else if (task->saved_state & __TASK_TRACED)
  13542. + traced = true;
  13543. + raw_spin_unlock_irq(&task->pi_lock);
  13544. +#endif
  13545. + return traced;
  13546. +}
  13547. +
  13548. /*
  13549. * cond_resched() and cond_resched_lock(): latency reduction via
  13550. * explicit rescheduling in places that are safe. The return
  13551. @@ -2948,12 +3107,16 @@ extern int __cond_resched_lock(spinlock_t *lock);
  13552. __cond_resched_lock(lock); \
  13553. })
  13554. +#ifndef CONFIG_PREEMPT_RT_FULL
  13555. extern int __cond_resched_softirq(void);
  13556. #define cond_resched_softirq() ({ \
  13557. ___might_sleep(__FILE__, __LINE__, SOFTIRQ_DISABLE_OFFSET); \
  13558. __cond_resched_softirq(); \
  13559. })
  13560. +#else
  13561. +# define cond_resched_softirq() cond_resched()
  13562. +#endif
  13563. static inline void cond_resched_rcu(void)
  13564. {
  13565. @@ -3115,6 +3278,31 @@ static inline void set_task_cpu(struct task_struct *p, unsigned int cpu)
  13566. #endif /* CONFIG_SMP */
  13567. +static inline int __migrate_disabled(struct task_struct *p)
  13568. +{
  13569. +#ifdef CONFIG_PREEMPT_RT_FULL
  13570. + return p->migrate_disable;
  13571. +#else
  13572. + return 0;
  13573. +#endif
  13574. +}
  13575. +
  13576. +/* Future-safe accessor for struct task_struct's cpus_allowed. */
  13577. +static inline const struct cpumask *tsk_cpus_allowed(struct task_struct *p)
  13578. +{
  13579. + if (__migrate_disabled(p))
  13580. + return cpumask_of(task_cpu(p));
  13581. +
  13582. + return &p->cpus_allowed;
  13583. +}
  13584. +
  13585. +static inline int tsk_nr_cpus_allowed(struct task_struct *p)
  13586. +{
  13587. + if (__migrate_disabled(p))
  13588. + return 1;
  13589. + return p->nr_cpus_allowed;
  13590. +}
  13591. +
  13592. extern long sched_setaffinity(pid_t pid, const struct cpumask *new_mask);
  13593. extern long sched_getaffinity(pid_t pid, struct cpumask *mask);
  13594. diff --git a/include/linux/seqlock.h b/include/linux/seqlock.h
  13595. index e0582106ef4f..b14f4d2368aa 100644
  13596. --- a/include/linux/seqlock.h
  13597. +++ b/include/linux/seqlock.h
  13598. @@ -220,20 +220,30 @@ static inline int read_seqcount_retry(const seqcount_t *s, unsigned start)
  13599. return __read_seqcount_retry(s, start);
  13600. }
  13601. -
  13602. -
  13603. -static inline void raw_write_seqcount_begin(seqcount_t *s)
  13604. +static inline void __raw_write_seqcount_begin(seqcount_t *s)
  13605. {
  13606. s->sequence++;
  13607. smp_wmb();
  13608. }
  13609. -static inline void raw_write_seqcount_end(seqcount_t *s)
  13610. +static inline void raw_write_seqcount_begin(seqcount_t *s)
  13611. +{
  13612. + preempt_disable_rt();
  13613. + __raw_write_seqcount_begin(s);
  13614. +}
  13615. +
  13616. +static inline void __raw_write_seqcount_end(seqcount_t *s)
  13617. {
  13618. smp_wmb();
  13619. s->sequence++;
  13620. }
  13621. +static inline void raw_write_seqcount_end(seqcount_t *s)
  13622. +{
  13623. + __raw_write_seqcount_end(s);
  13624. + preempt_enable_rt();
  13625. +}
  13626. +
  13627. /**
  13628. * raw_write_seqcount_barrier - do a seq write barrier
  13629. * @s: pointer to seqcount_t
  13630. @@ -425,10 +435,32 @@ typedef struct {
  13631. /*
  13632. * Read side functions for starting and finalizing a read side section.
  13633. */
  13634. +#ifndef CONFIG_PREEMPT_RT_FULL
  13635. static inline unsigned read_seqbegin(const seqlock_t *sl)
  13636. {
  13637. return read_seqcount_begin(&sl->seqcount);
  13638. }
  13639. +#else
  13640. +/*
  13641. + * Starvation safe read side for RT
  13642. + */
  13643. +static inline unsigned read_seqbegin(seqlock_t *sl)
  13644. +{
  13645. + unsigned ret;
  13646. +
  13647. +repeat:
  13648. + ret = ACCESS_ONCE(sl->seqcount.sequence);
  13649. + if (unlikely(ret & 1)) {
  13650. + /*
  13651. + * Take the lock and let the writer proceed (i.e. evtl
  13652. + * boost it), otherwise we could loop here forever.
  13653. + */
  13654. + spin_unlock_wait(&sl->lock);
  13655. + goto repeat;
  13656. + }
  13657. + return ret;
  13658. +}
  13659. +#endif
  13660. static inline unsigned read_seqretry(const seqlock_t *sl, unsigned start)
  13661. {
  13662. @@ -443,36 +475,36 @@ static inline unsigned read_seqretry(const seqlock_t *sl, unsigned start)
  13663. static inline void write_seqlock(seqlock_t *sl)
  13664. {
  13665. spin_lock(&sl->lock);
  13666. - write_seqcount_begin(&sl->seqcount);
  13667. + __raw_write_seqcount_begin(&sl->seqcount);
  13668. }
  13669. static inline void write_sequnlock(seqlock_t *sl)
  13670. {
  13671. - write_seqcount_end(&sl->seqcount);
  13672. + __raw_write_seqcount_end(&sl->seqcount);
  13673. spin_unlock(&sl->lock);
  13674. }
  13675. static inline void write_seqlock_bh(seqlock_t *sl)
  13676. {
  13677. spin_lock_bh(&sl->lock);
  13678. - write_seqcount_begin(&sl->seqcount);
  13679. + __raw_write_seqcount_begin(&sl->seqcount);
  13680. }
  13681. static inline void write_sequnlock_bh(seqlock_t *sl)
  13682. {
  13683. - write_seqcount_end(&sl->seqcount);
  13684. + __raw_write_seqcount_end(&sl->seqcount);
  13685. spin_unlock_bh(&sl->lock);
  13686. }
  13687. static inline void write_seqlock_irq(seqlock_t *sl)
  13688. {
  13689. spin_lock_irq(&sl->lock);
  13690. - write_seqcount_begin(&sl->seqcount);
  13691. + __raw_write_seqcount_begin(&sl->seqcount);
  13692. }
  13693. static inline void write_sequnlock_irq(seqlock_t *sl)
  13694. {
  13695. - write_seqcount_end(&sl->seqcount);
  13696. + __raw_write_seqcount_end(&sl->seqcount);
  13697. spin_unlock_irq(&sl->lock);
  13698. }
  13699. @@ -481,7 +513,7 @@ static inline unsigned long __write_seqlock_irqsave(seqlock_t *sl)
  13700. unsigned long flags;
  13701. spin_lock_irqsave(&sl->lock, flags);
  13702. - write_seqcount_begin(&sl->seqcount);
  13703. + __raw_write_seqcount_begin(&sl->seqcount);
  13704. return flags;
  13705. }
  13706. @@ -491,7 +523,7 @@ static inline unsigned long __write_seqlock_irqsave(seqlock_t *sl)
  13707. static inline void
  13708. write_sequnlock_irqrestore(seqlock_t *sl, unsigned long flags)
  13709. {
  13710. - write_seqcount_end(&sl->seqcount);
  13711. + __raw_write_seqcount_end(&sl->seqcount);
  13712. spin_unlock_irqrestore(&sl->lock, flags);
  13713. }
  13714. diff --git a/include/linux/signal.h b/include/linux/signal.h
  13715. index d80259afb9e5..ddd1e6866a54 100644
  13716. --- a/include/linux/signal.h
  13717. +++ b/include/linux/signal.h
  13718. @@ -233,6 +233,7 @@ static inline void init_sigpending(struct sigpending *sig)
  13719. }
  13720. extern void flush_sigqueue(struct sigpending *queue);
  13721. +extern void flush_task_sigqueue(struct task_struct *tsk);
  13722. /* Test if 'sig' is valid signal. Use this instead of testing _NSIG directly */
  13723. static inline int valid_signal(unsigned long sig)
  13724. diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h
  13725. index d443d9ab0236..2d1c7f9b7fd0 100644
  13726. --- a/include/linux/skbuff.h
  13727. +++ b/include/linux/skbuff.h
  13728. @@ -203,6 +203,7 @@ struct sk_buff_head {
  13729. __u32 qlen;
  13730. spinlock_t lock;
  13731. + raw_spinlock_t raw_lock;
  13732. };
  13733. struct sk_buff;
  13734. @@ -1465,6 +1466,12 @@ static inline void skb_queue_head_init(struct sk_buff_head *list)
  13735. __skb_queue_head_init(list);
  13736. }
  13737. +static inline void skb_queue_head_init_raw(struct sk_buff_head *list)
  13738. +{
  13739. + raw_spin_lock_init(&list->raw_lock);
  13740. + __skb_queue_head_init(list);
  13741. +}
  13742. +
  13743. static inline void skb_queue_head_init_class(struct sk_buff_head *list,
  13744. struct lock_class_key *class)
  13745. {
  13746. diff --git a/include/linux/smp.h b/include/linux/smp.h
  13747. index c4414074bd88..e6ab36aeaaab 100644
  13748. --- a/include/linux/smp.h
  13749. +++ b/include/linux/smp.h
  13750. @@ -185,6 +185,9 @@ static inline void smp_init(void) { }
  13751. #define get_cpu() ({ preempt_disable(); smp_processor_id(); })
  13752. #define put_cpu() preempt_enable()
  13753. +#define get_cpu_light() ({ migrate_disable(); smp_processor_id(); })
  13754. +#define put_cpu_light() migrate_enable()
  13755. +
  13756. /*
  13757. * Callback to arch code if there's nosmp or maxcpus=0 on the
  13758. * boot command line:
  13759. diff --git a/include/linux/spinlock.h b/include/linux/spinlock.h
  13760. index 47dd0cebd204..02928fa5499d 100644
  13761. --- a/include/linux/spinlock.h
  13762. +++ b/include/linux/spinlock.h
  13763. @@ -271,7 +271,11 @@ static inline void do_raw_spin_unlock(raw_spinlock_t *lock) __releases(lock)
  13764. #define raw_spin_can_lock(lock) (!raw_spin_is_locked(lock))
  13765. /* Include rwlock functions */
  13766. -#include <linux/rwlock.h>
  13767. +#ifdef CONFIG_PREEMPT_RT_FULL
  13768. +# include <linux/rwlock_rt.h>
  13769. +#else
  13770. +# include <linux/rwlock.h>
  13771. +#endif
  13772. /*
  13773. * Pull the _spin_*()/_read_*()/_write_*() functions/declarations:
  13774. @@ -282,6 +286,10 @@ static inline void do_raw_spin_unlock(raw_spinlock_t *lock) __releases(lock)
  13775. # include <linux/spinlock_api_up.h>
  13776. #endif
  13777. +#ifdef CONFIG_PREEMPT_RT_FULL
  13778. +# include <linux/spinlock_rt.h>
  13779. +#else /* PREEMPT_RT_FULL */
  13780. +
  13781. /*
  13782. * Map the spin_lock functions to the raw variants for PREEMPT_RT=n
  13783. */
  13784. @@ -347,6 +355,12 @@ static __always_inline void spin_unlock(spinlock_t *lock)
  13785. raw_spin_unlock(&lock->rlock);
  13786. }
  13787. +static __always_inline int spin_unlock_no_deboost(spinlock_t *lock)
  13788. +{
  13789. + raw_spin_unlock(&lock->rlock);
  13790. + return 0;
  13791. +}
  13792. +
  13793. static __always_inline void spin_unlock_bh(spinlock_t *lock)
  13794. {
  13795. raw_spin_unlock_bh(&lock->rlock);
  13796. @@ -416,4 +430,6 @@ extern int _atomic_dec_and_lock(atomic_t *atomic, spinlock_t *lock);
  13797. #define atomic_dec_and_lock(atomic, lock) \
  13798. __cond_lock(lock, _atomic_dec_and_lock(atomic, lock))
  13799. +#endif /* !PREEMPT_RT_FULL */
  13800. +
  13801. #endif /* __LINUX_SPINLOCK_H */
  13802. diff --git a/include/linux/spinlock_api_smp.h b/include/linux/spinlock_api_smp.h
  13803. index 5344268e6e62..043263f30e81 100644
  13804. --- a/include/linux/spinlock_api_smp.h
  13805. +++ b/include/linux/spinlock_api_smp.h
  13806. @@ -189,6 +189,8 @@ static inline int __raw_spin_trylock_bh(raw_spinlock_t *lock)
  13807. return 0;
  13808. }
  13809. -#include <linux/rwlock_api_smp.h>
  13810. +#ifndef CONFIG_PREEMPT_RT_FULL
  13811. +# include <linux/rwlock_api_smp.h>
  13812. +#endif
  13813. #endif /* __LINUX_SPINLOCK_API_SMP_H */
  13814. diff --git a/include/linux/spinlock_rt.h b/include/linux/spinlock_rt.h
  13815. new file mode 100644
  13816. index 000000000000..7eb87584e843
  13817. --- /dev/null
  13818. +++ b/include/linux/spinlock_rt.h
  13819. @@ -0,0 +1,165 @@
  13820. +#ifndef __LINUX_SPINLOCK_RT_H
  13821. +#define __LINUX_SPINLOCK_RT_H
  13822. +
  13823. +#ifndef __LINUX_SPINLOCK_H
  13824. +#error Do not include directly. Use spinlock.h
  13825. +#endif
  13826. +
  13827. +#include <linux/bug.h>
  13828. +
  13829. +extern void
  13830. +__rt_spin_lock_init(spinlock_t *lock, char *name, struct lock_class_key *key);
  13831. +
  13832. +#define spin_lock_init(slock) \
  13833. +do { \
  13834. + static struct lock_class_key __key; \
  13835. + \
  13836. + rt_mutex_init(&(slock)->lock); \
  13837. + __rt_spin_lock_init(slock, #slock, &__key); \
  13838. +} while (0)
  13839. +
  13840. +void __lockfunc rt_spin_lock__no_mg(spinlock_t *lock);
  13841. +void __lockfunc rt_spin_unlock__no_mg(spinlock_t *lock);
  13842. +int __lockfunc rt_spin_trylock__no_mg(spinlock_t *lock);
  13843. +
  13844. +extern void __lockfunc rt_spin_lock(spinlock_t *lock);
  13845. +extern unsigned long __lockfunc rt_spin_lock_trace_flags(spinlock_t *lock);
  13846. +extern void __lockfunc rt_spin_lock_nested(spinlock_t *lock, int subclass);
  13847. +extern void __lockfunc rt_spin_unlock(spinlock_t *lock);
  13848. +extern int __lockfunc rt_spin_unlock_no_deboost(spinlock_t *lock);
  13849. +extern void __lockfunc rt_spin_unlock_wait(spinlock_t *lock);
  13850. +extern int __lockfunc rt_spin_trylock_irqsave(spinlock_t *lock, unsigned long *flags);
  13851. +extern int __lockfunc rt_spin_trylock_bh(spinlock_t *lock);
  13852. +extern int __lockfunc rt_spin_trylock(spinlock_t *lock);
  13853. +extern int atomic_dec_and_spin_lock(atomic_t *atomic, spinlock_t *lock);
  13854. +
  13855. +/*
  13856. + * lockdep-less calls, for derived types like rwlock:
  13857. + * (for trylock they can use rt_mutex_trylock() directly.
  13858. + */
  13859. +extern void __lockfunc __rt_spin_lock__no_mg(struct rt_mutex *lock);
  13860. +extern void __lockfunc __rt_spin_lock(struct rt_mutex *lock);
  13861. +extern void __lockfunc __rt_spin_unlock(struct rt_mutex *lock);
  13862. +extern int __lockfunc __rt_spin_trylock(struct rt_mutex *lock);
  13863. +
  13864. +#define spin_lock(lock) rt_spin_lock(lock)
  13865. +
  13866. +#define spin_lock_bh(lock) \
  13867. + do { \
  13868. + local_bh_disable(); \
  13869. + rt_spin_lock(lock); \
  13870. + } while (0)
  13871. +
  13872. +#define spin_lock_irq(lock) spin_lock(lock)
  13873. +
  13874. +#define spin_do_trylock(lock) __cond_lock(lock, rt_spin_trylock(lock))
  13875. +
  13876. +#define spin_trylock(lock) \
  13877. +({ \
  13878. + int __locked; \
  13879. + __locked = spin_do_trylock(lock); \
  13880. + __locked; \
  13881. +})
  13882. +
  13883. +#ifdef CONFIG_LOCKDEP
  13884. +# define spin_lock_nested(lock, subclass) \
  13885. + do { \
  13886. + rt_spin_lock_nested(lock, subclass); \
  13887. + } while (0)
  13888. +
  13889. +#define spin_lock_bh_nested(lock, subclass) \
  13890. + do { \
  13891. + local_bh_disable(); \
  13892. + rt_spin_lock_nested(lock, subclass); \
  13893. + } while (0)
  13894. +
  13895. +# define spin_lock_irqsave_nested(lock, flags, subclass) \
  13896. + do { \
  13897. + typecheck(unsigned long, flags); \
  13898. + flags = 0; \
  13899. + rt_spin_lock_nested(lock, subclass); \
  13900. + } while (0)
  13901. +#else
  13902. +# define spin_lock_nested(lock, subclass) spin_lock(lock)
  13903. +# define spin_lock_bh_nested(lock, subclass) spin_lock_bh(lock)
  13904. +
  13905. +# define spin_lock_irqsave_nested(lock, flags, subclass) \
  13906. + do { \
  13907. + typecheck(unsigned long, flags); \
  13908. + flags = 0; \
  13909. + spin_lock(lock); \
  13910. + } while (0)
  13911. +#endif
  13912. +
  13913. +#define spin_lock_irqsave(lock, flags) \
  13914. + do { \
  13915. + typecheck(unsigned long, flags); \
  13916. + flags = 0; \
  13917. + spin_lock(lock); \
  13918. + } while (0)
  13919. +
  13920. +static inline unsigned long spin_lock_trace_flags(spinlock_t *lock)
  13921. +{
  13922. + unsigned long flags = 0;
  13923. +#ifdef CONFIG_TRACE_IRQFLAGS
  13924. + flags = rt_spin_lock_trace_flags(lock);
  13925. +#else
  13926. + spin_lock(lock); /* lock_local */
  13927. +#endif
  13928. + return flags;
  13929. +}
  13930. +
  13931. +/* FIXME: we need rt_spin_lock_nest_lock */
  13932. +#define spin_lock_nest_lock(lock, nest_lock) spin_lock_nested(lock, 0)
  13933. +
  13934. +#define spin_unlock(lock) rt_spin_unlock(lock)
  13935. +#define spin_unlock_no_deboost(lock) rt_spin_unlock_no_deboost(lock)
  13936. +
  13937. +#define spin_unlock_bh(lock) \
  13938. + do { \
  13939. + rt_spin_unlock(lock); \
  13940. + local_bh_enable(); \
  13941. + } while (0)
  13942. +
  13943. +#define spin_unlock_irq(lock) spin_unlock(lock)
  13944. +
  13945. +#define spin_unlock_irqrestore(lock, flags) \
  13946. + do { \
  13947. + typecheck(unsigned long, flags); \
  13948. + (void) flags; \
  13949. + spin_unlock(lock); \
  13950. + } while (0)
  13951. +
  13952. +#define spin_trylock_bh(lock) __cond_lock(lock, rt_spin_trylock_bh(lock))
  13953. +#define spin_trylock_irq(lock) spin_trylock(lock)
  13954. +
  13955. +#define spin_trylock_irqsave(lock, flags) \
  13956. + rt_spin_trylock_irqsave(lock, &(flags))
  13957. +
  13958. +#define spin_unlock_wait(lock) rt_spin_unlock_wait(lock)
  13959. +
  13960. +#ifdef CONFIG_GENERIC_LOCKBREAK
  13961. +# define spin_is_contended(lock) ((lock)->break_lock)
  13962. +#else
  13963. +# define spin_is_contended(lock) (((void)(lock), 0))
  13964. +#endif
  13965. +
  13966. +static inline int spin_can_lock(spinlock_t *lock)
  13967. +{
  13968. + return !rt_mutex_is_locked(&lock->lock);
  13969. +}
  13970. +
  13971. +static inline int spin_is_locked(spinlock_t *lock)
  13972. +{
  13973. + return rt_mutex_is_locked(&lock->lock);
  13974. +}
  13975. +
  13976. +static inline void assert_spin_locked(spinlock_t *lock)
  13977. +{
  13978. + BUG_ON(!spin_is_locked(lock));
  13979. +}
  13980. +
  13981. +#define atomic_dec_and_lock(atomic, lock) \
  13982. + atomic_dec_and_spin_lock(atomic, lock)
  13983. +
  13984. +#endif
  13985. diff --git a/include/linux/spinlock_types.h b/include/linux/spinlock_types.h
  13986. index 73548eb13a5d..10bac715ea96 100644
  13987. --- a/include/linux/spinlock_types.h
  13988. +++ b/include/linux/spinlock_types.h
  13989. @@ -9,80 +9,15 @@
  13990. * Released under the General Public License (GPL).
  13991. */
  13992. -#if defined(CONFIG_SMP)
  13993. -# include <asm/spinlock_types.h>
  13994. -#else
  13995. -# include <linux/spinlock_types_up.h>
  13996. -#endif
  13997. -
  13998. -#include <linux/lockdep.h>
  13999. -
  14000. -typedef struct raw_spinlock {
  14001. - arch_spinlock_t raw_lock;
  14002. -#ifdef CONFIG_GENERIC_LOCKBREAK
  14003. - unsigned int break_lock;
  14004. -#endif
  14005. -#ifdef CONFIG_DEBUG_SPINLOCK
  14006. - unsigned int magic, owner_cpu;
  14007. - void *owner;
  14008. -#endif
  14009. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  14010. - struct lockdep_map dep_map;
  14011. -#endif
  14012. -} raw_spinlock_t;
  14013. -
  14014. -#define SPINLOCK_MAGIC 0xdead4ead
  14015. -
  14016. -#define SPINLOCK_OWNER_INIT ((void *)-1L)
  14017. -
  14018. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  14019. -# define SPIN_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }
  14020. -#else
  14021. -# define SPIN_DEP_MAP_INIT(lockname)
  14022. -#endif
  14023. +#include <linux/spinlock_types_raw.h>
  14024. -#ifdef CONFIG_DEBUG_SPINLOCK
  14025. -# define SPIN_DEBUG_INIT(lockname) \
  14026. - .magic = SPINLOCK_MAGIC, \
  14027. - .owner_cpu = -1, \
  14028. - .owner = SPINLOCK_OWNER_INIT,
  14029. +#ifndef CONFIG_PREEMPT_RT_FULL
  14030. +# include <linux/spinlock_types_nort.h>
  14031. +# include <linux/rwlock_types.h>
  14032. #else
  14033. -# define SPIN_DEBUG_INIT(lockname)
  14034. +# include <linux/rtmutex.h>
  14035. +# include <linux/spinlock_types_rt.h>
  14036. +# include <linux/rwlock_types_rt.h>
  14037. #endif
  14038. -#define __RAW_SPIN_LOCK_INITIALIZER(lockname) \
  14039. - { \
  14040. - .raw_lock = __ARCH_SPIN_LOCK_UNLOCKED, \
  14041. - SPIN_DEBUG_INIT(lockname) \
  14042. - SPIN_DEP_MAP_INIT(lockname) }
  14043. -
  14044. -#define __RAW_SPIN_LOCK_UNLOCKED(lockname) \
  14045. - (raw_spinlock_t) __RAW_SPIN_LOCK_INITIALIZER(lockname)
  14046. -
  14047. -#define DEFINE_RAW_SPINLOCK(x) raw_spinlock_t x = __RAW_SPIN_LOCK_UNLOCKED(x)
  14048. -
  14049. -typedef struct spinlock {
  14050. - union {
  14051. - struct raw_spinlock rlock;
  14052. -
  14053. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  14054. -# define LOCK_PADSIZE (offsetof(struct raw_spinlock, dep_map))
  14055. - struct {
  14056. - u8 __padding[LOCK_PADSIZE];
  14057. - struct lockdep_map dep_map;
  14058. - };
  14059. -#endif
  14060. - };
  14061. -} spinlock_t;
  14062. -
  14063. -#define __SPIN_LOCK_INITIALIZER(lockname) \
  14064. - { { .rlock = __RAW_SPIN_LOCK_INITIALIZER(lockname) } }
  14065. -
  14066. -#define __SPIN_LOCK_UNLOCKED(lockname) \
  14067. - (spinlock_t ) __SPIN_LOCK_INITIALIZER(lockname)
  14068. -
  14069. -#define DEFINE_SPINLOCK(x) spinlock_t x = __SPIN_LOCK_UNLOCKED(x)
  14070. -
  14071. -#include <linux/rwlock_types.h>
  14072. -
  14073. #endif /* __LINUX_SPINLOCK_TYPES_H */
  14074. diff --git a/include/linux/spinlock_types_nort.h b/include/linux/spinlock_types_nort.h
  14075. new file mode 100644
  14076. index 000000000000..f1dac1fb1d6a
  14077. --- /dev/null
  14078. +++ b/include/linux/spinlock_types_nort.h
  14079. @@ -0,0 +1,33 @@
  14080. +#ifndef __LINUX_SPINLOCK_TYPES_NORT_H
  14081. +#define __LINUX_SPINLOCK_TYPES_NORT_H
  14082. +
  14083. +#ifndef __LINUX_SPINLOCK_TYPES_H
  14084. +#error "Do not include directly. Include spinlock_types.h instead"
  14085. +#endif
  14086. +
  14087. +/*
  14088. + * The non RT version maps spinlocks to raw_spinlocks
  14089. + */
  14090. +typedef struct spinlock {
  14091. + union {
  14092. + struct raw_spinlock rlock;
  14093. +
  14094. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  14095. +# define LOCK_PADSIZE (offsetof(struct raw_spinlock, dep_map))
  14096. + struct {
  14097. + u8 __padding[LOCK_PADSIZE];
  14098. + struct lockdep_map dep_map;
  14099. + };
  14100. +#endif
  14101. + };
  14102. +} spinlock_t;
  14103. +
  14104. +#define __SPIN_LOCK_INITIALIZER(lockname) \
  14105. + { { .rlock = __RAW_SPIN_LOCK_INITIALIZER(lockname) } }
  14106. +
  14107. +#define __SPIN_LOCK_UNLOCKED(lockname) \
  14108. + (spinlock_t ) __SPIN_LOCK_INITIALIZER(lockname)
  14109. +
  14110. +#define DEFINE_SPINLOCK(x) spinlock_t x = __SPIN_LOCK_UNLOCKED(x)
  14111. +
  14112. +#endif
  14113. diff --git a/include/linux/spinlock_types_raw.h b/include/linux/spinlock_types_raw.h
  14114. new file mode 100644
  14115. index 000000000000..edffc4d53fc9
  14116. --- /dev/null
  14117. +++ b/include/linux/spinlock_types_raw.h
  14118. @@ -0,0 +1,56 @@
  14119. +#ifndef __LINUX_SPINLOCK_TYPES_RAW_H
  14120. +#define __LINUX_SPINLOCK_TYPES_RAW_H
  14121. +
  14122. +#if defined(CONFIG_SMP)
  14123. +# include <asm/spinlock_types.h>
  14124. +#else
  14125. +# include <linux/spinlock_types_up.h>
  14126. +#endif
  14127. +
  14128. +#include <linux/lockdep.h>
  14129. +
  14130. +typedef struct raw_spinlock {
  14131. + arch_spinlock_t raw_lock;
  14132. +#ifdef CONFIG_GENERIC_LOCKBREAK
  14133. + unsigned int break_lock;
  14134. +#endif
  14135. +#ifdef CONFIG_DEBUG_SPINLOCK
  14136. + unsigned int magic, owner_cpu;
  14137. + void *owner;
  14138. +#endif
  14139. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  14140. + struct lockdep_map dep_map;
  14141. +#endif
  14142. +} raw_spinlock_t;
  14143. +
  14144. +#define SPINLOCK_MAGIC 0xdead4ead
  14145. +
  14146. +#define SPINLOCK_OWNER_INIT ((void *)-1L)
  14147. +
  14148. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  14149. +# define SPIN_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }
  14150. +#else
  14151. +# define SPIN_DEP_MAP_INIT(lockname)
  14152. +#endif
  14153. +
  14154. +#ifdef CONFIG_DEBUG_SPINLOCK
  14155. +# define SPIN_DEBUG_INIT(lockname) \
  14156. + .magic = SPINLOCK_MAGIC, \
  14157. + .owner_cpu = -1, \
  14158. + .owner = SPINLOCK_OWNER_INIT,
  14159. +#else
  14160. +# define SPIN_DEBUG_INIT(lockname)
  14161. +#endif
  14162. +
  14163. +#define __RAW_SPIN_LOCK_INITIALIZER(lockname) \
  14164. + { \
  14165. + .raw_lock = __ARCH_SPIN_LOCK_UNLOCKED, \
  14166. + SPIN_DEBUG_INIT(lockname) \
  14167. + SPIN_DEP_MAP_INIT(lockname) }
  14168. +
  14169. +#define __RAW_SPIN_LOCK_UNLOCKED(lockname) \
  14170. + (raw_spinlock_t) __RAW_SPIN_LOCK_INITIALIZER(lockname)
  14171. +
  14172. +#define DEFINE_RAW_SPINLOCK(x) raw_spinlock_t x = __RAW_SPIN_LOCK_UNLOCKED(x)
  14173. +
  14174. +#endif
  14175. diff --git a/include/linux/spinlock_types_rt.h b/include/linux/spinlock_types_rt.h
  14176. new file mode 100644
  14177. index 000000000000..9fd431967abc
  14178. --- /dev/null
  14179. +++ b/include/linux/spinlock_types_rt.h
  14180. @@ -0,0 +1,51 @@
  14181. +#ifndef __LINUX_SPINLOCK_TYPES_RT_H
  14182. +#define __LINUX_SPINLOCK_TYPES_RT_H
  14183. +
  14184. +#ifndef __LINUX_SPINLOCK_TYPES_H
  14185. +#error "Do not include directly. Include spinlock_types.h instead"
  14186. +#endif
  14187. +
  14188. +#include <linux/cache.h>
  14189. +
  14190. +/*
  14191. + * PREEMPT_RT: spinlocks - an RT mutex plus lock-break field:
  14192. + */
  14193. +typedef struct spinlock {
  14194. + struct rt_mutex lock;
  14195. + unsigned int break_lock;
  14196. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  14197. + struct lockdep_map dep_map;
  14198. +#endif
  14199. +} spinlock_t;
  14200. +
  14201. +#ifdef CONFIG_DEBUG_RT_MUTEXES
  14202. +# define __RT_SPIN_INITIALIZER(name) \
  14203. + { \
  14204. + .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock), \
  14205. + .save_state = 1, \
  14206. + .file = __FILE__, \
  14207. + .line = __LINE__ , \
  14208. + }
  14209. +#else
  14210. +# define __RT_SPIN_INITIALIZER(name) \
  14211. + { \
  14212. + .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock), \
  14213. + .save_state = 1, \
  14214. + }
  14215. +#endif
  14216. +
  14217. +/*
  14218. +.wait_list = PLIST_HEAD_INIT_RAW((name).lock.wait_list, (name).lock.wait_lock)
  14219. +*/
  14220. +
  14221. +#define __SPIN_LOCK_UNLOCKED(name) \
  14222. + { .lock = __RT_SPIN_INITIALIZER(name.lock), \
  14223. + SPIN_DEP_MAP_INIT(name) }
  14224. +
  14225. +#define __DEFINE_SPINLOCK(name) \
  14226. + spinlock_t name = __SPIN_LOCK_UNLOCKED(name)
  14227. +
  14228. +#define DEFINE_SPINLOCK(name) \
  14229. + spinlock_t name __cacheline_aligned_in_smp = __SPIN_LOCK_UNLOCKED(name)
  14230. +
  14231. +#endif
  14232. diff --git a/include/linux/srcu.h b/include/linux/srcu.h
  14233. index f5f80c5643ac..ec1a8f01563c 100644
  14234. --- a/include/linux/srcu.h
  14235. +++ b/include/linux/srcu.h
  14236. @@ -84,10 +84,10 @@ int init_srcu_struct(struct srcu_struct *sp);
  14237. void process_srcu(struct work_struct *work);
  14238. -#define __SRCU_STRUCT_INIT(name) \
  14239. +#define __SRCU_STRUCT_INIT(name, pcpu_name) \
  14240. { \
  14241. .completed = -300, \
  14242. - .per_cpu_ref = &name##_srcu_array, \
  14243. + .per_cpu_ref = &pcpu_name, \
  14244. .queue_lock = __SPIN_LOCK_UNLOCKED(name.queue_lock), \
  14245. .running = false, \
  14246. .batch_queue = RCU_BATCH_INIT(name.batch_queue), \
  14247. @@ -104,7 +104,7 @@ void process_srcu(struct work_struct *work);
  14248. */
  14249. #define __DEFINE_SRCU(name, is_static) \
  14250. static DEFINE_PER_CPU(struct srcu_struct_array, name##_srcu_array);\
  14251. - is_static struct srcu_struct name = __SRCU_STRUCT_INIT(name)
  14252. + is_static struct srcu_struct name = __SRCU_STRUCT_INIT(name, name##_srcu_array)
  14253. #define DEFINE_SRCU(name) __DEFINE_SRCU(name, /* not static */)
  14254. #define DEFINE_STATIC_SRCU(name) __DEFINE_SRCU(name, static)
  14255. diff --git a/include/linux/suspend.h b/include/linux/suspend.h
  14256. index 8b6ec7ef0854..9b77d4cc929f 100644
  14257. --- a/include/linux/suspend.h
  14258. +++ b/include/linux/suspend.h
  14259. @@ -194,6 +194,12 @@ struct platform_freeze_ops {
  14260. void (*end)(void);
  14261. };
  14262. +#if defined(CONFIG_SUSPEND) || defined(CONFIG_HIBERNATION)
  14263. +extern bool pm_in_action;
  14264. +#else
  14265. +# define pm_in_action false
  14266. +#endif
  14267. +
  14268. #ifdef CONFIG_SUSPEND
  14269. /**
  14270. * suspend_set_ops - set platform dependent suspend operations
  14271. diff --git a/include/linux/swait.h b/include/linux/swait.h
  14272. new file mode 100644
  14273. index 000000000000..83f004a72320
  14274. --- /dev/null
  14275. +++ b/include/linux/swait.h
  14276. @@ -0,0 +1,173 @@
  14277. +#ifndef _LINUX_SWAIT_H
  14278. +#define _LINUX_SWAIT_H
  14279. +
  14280. +#include <linux/list.h>
  14281. +#include <linux/stddef.h>
  14282. +#include <linux/spinlock.h>
  14283. +#include <asm/current.h>
  14284. +
  14285. +/*
  14286. + * Simple wait queues
  14287. + *
  14288. + * While these are very similar to the other/complex wait queues (wait.h) the
  14289. + * most important difference is that the simple waitqueue allows for
  14290. + * deterministic behaviour -- IOW it has strictly bounded IRQ and lock hold
  14291. + * times.
  14292. + *
  14293. + * In order to make this so, we had to drop a fair number of features of the
  14294. + * other waitqueue code; notably:
  14295. + *
  14296. + * - mixing INTERRUPTIBLE and UNINTERRUPTIBLE sleeps on the same waitqueue;
  14297. + * all wakeups are TASK_NORMAL in order to avoid O(n) lookups for the right
  14298. + * sleeper state.
  14299. + *
  14300. + * - the exclusive mode; because this requires preserving the list order
  14301. + * and this is hard.
  14302. + *
  14303. + * - custom wake functions; because you cannot give any guarantees about
  14304. + * random code.
  14305. + *
  14306. + * As a side effect of this; the data structures are slimmer.
  14307. + *
  14308. + * One would recommend using this wait queue where possible.
  14309. + */
  14310. +
  14311. +struct task_struct;
  14312. +
  14313. +struct swait_queue_head {
  14314. + raw_spinlock_t lock;
  14315. + struct list_head task_list;
  14316. +};
  14317. +
  14318. +struct swait_queue {
  14319. + struct task_struct *task;
  14320. + struct list_head task_list;
  14321. +};
  14322. +
  14323. +#define __SWAITQUEUE_INITIALIZER(name) { \
  14324. + .task = current, \
  14325. + .task_list = LIST_HEAD_INIT((name).task_list), \
  14326. +}
  14327. +
  14328. +#define DECLARE_SWAITQUEUE(name) \
  14329. + struct swait_queue name = __SWAITQUEUE_INITIALIZER(name)
  14330. +
  14331. +#define __SWAIT_QUEUE_HEAD_INITIALIZER(name) { \
  14332. + .lock = __RAW_SPIN_LOCK_UNLOCKED(name.lock), \
  14333. + .task_list = LIST_HEAD_INIT((name).task_list), \
  14334. +}
  14335. +
  14336. +#define DECLARE_SWAIT_QUEUE_HEAD(name) \
  14337. + struct swait_queue_head name = __SWAIT_QUEUE_HEAD_INITIALIZER(name)
  14338. +
  14339. +extern void __init_swait_queue_head(struct swait_queue_head *q, const char *name,
  14340. + struct lock_class_key *key);
  14341. +
  14342. +#define init_swait_queue_head(q) \
  14343. + do { \
  14344. + static struct lock_class_key __key; \
  14345. + __init_swait_queue_head((q), #q, &__key); \
  14346. + } while (0)
  14347. +
  14348. +#ifdef CONFIG_LOCKDEP
  14349. +# define __SWAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
  14350. + ({ init_swait_queue_head(&name); name; })
  14351. +# define DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(name) \
  14352. + struct swait_queue_head name = __SWAIT_QUEUE_HEAD_INIT_ONSTACK(name)
  14353. +#else
  14354. +# define DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(name) \
  14355. + DECLARE_SWAIT_QUEUE_HEAD(name)
  14356. +#endif
  14357. +
  14358. +static inline int swait_active(struct swait_queue_head *q)
  14359. +{
  14360. + return !list_empty(&q->task_list);
  14361. +}
  14362. +
  14363. +extern void swake_up(struct swait_queue_head *q);
  14364. +extern void swake_up_all(struct swait_queue_head *q);
  14365. +extern void swake_up_locked(struct swait_queue_head *q);
  14366. +extern void swake_up_all_locked(struct swait_queue_head *q);
  14367. +
  14368. +extern void __prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait);
  14369. +extern void prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait, int state);
  14370. +extern long prepare_to_swait_event(struct swait_queue_head *q, struct swait_queue *wait, int state);
  14371. +
  14372. +extern void __finish_swait(struct swait_queue_head *q, struct swait_queue *wait);
  14373. +extern void finish_swait(struct swait_queue_head *q, struct swait_queue *wait);
  14374. +
  14375. +/* as per ___wait_event() but for swait, therefore "exclusive == 0" */
  14376. +#define ___swait_event(wq, condition, state, ret, cmd) \
  14377. +({ \
  14378. + struct swait_queue __wait; \
  14379. + long __ret = ret; \
  14380. + \
  14381. + INIT_LIST_HEAD(&__wait.task_list); \
  14382. + for (;;) { \
  14383. + long __int = prepare_to_swait_event(&wq, &__wait, state);\
  14384. + \
  14385. + if (condition) \
  14386. + break; \
  14387. + \
  14388. + if (___wait_is_interruptible(state) && __int) { \
  14389. + __ret = __int; \
  14390. + break; \
  14391. + } \
  14392. + \
  14393. + cmd; \
  14394. + } \
  14395. + finish_swait(&wq, &__wait); \
  14396. + __ret; \
  14397. +})
  14398. +
  14399. +#define __swait_event(wq, condition) \
  14400. + (void)___swait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, \
  14401. + schedule())
  14402. +
  14403. +#define swait_event(wq, condition) \
  14404. +do { \
  14405. + if (condition) \
  14406. + break; \
  14407. + __swait_event(wq, condition); \
  14408. +} while (0)
  14409. +
  14410. +#define __swait_event_timeout(wq, condition, timeout) \
  14411. + ___swait_event(wq, ___wait_cond_timeout(condition), \
  14412. + TASK_UNINTERRUPTIBLE, timeout, \
  14413. + __ret = schedule_timeout(__ret))
  14414. +
  14415. +#define swait_event_timeout(wq, condition, timeout) \
  14416. +({ \
  14417. + long __ret = timeout; \
  14418. + if (!___wait_cond_timeout(condition)) \
  14419. + __ret = __swait_event_timeout(wq, condition, timeout); \
  14420. + __ret; \
  14421. +})
  14422. +
  14423. +#define __swait_event_interruptible(wq, condition) \
  14424. + ___swait_event(wq, condition, TASK_INTERRUPTIBLE, 0, \
  14425. + schedule())
  14426. +
  14427. +#define swait_event_interruptible(wq, condition) \
  14428. +({ \
  14429. + int __ret = 0; \
  14430. + if (!(condition)) \
  14431. + __ret = __swait_event_interruptible(wq, condition); \
  14432. + __ret; \
  14433. +})
  14434. +
  14435. +#define __swait_event_interruptible_timeout(wq, condition, timeout) \
  14436. + ___swait_event(wq, ___wait_cond_timeout(condition), \
  14437. + TASK_INTERRUPTIBLE, timeout, \
  14438. + __ret = schedule_timeout(__ret))
  14439. +
  14440. +#define swait_event_interruptible_timeout(wq, condition, timeout) \
  14441. +({ \
  14442. + long __ret = timeout; \
  14443. + if (!___wait_cond_timeout(condition)) \
  14444. + __ret = __swait_event_interruptible_timeout(wq, \
  14445. + condition, timeout); \
  14446. + __ret; \
  14447. +})
  14448. +
  14449. +#endif /* _LINUX_SWAIT_H */
  14450. diff --git a/include/linux/swap.h b/include/linux/swap.h
  14451. index d8ca2eaa3a8b..19e038054914 100644
  14452. --- a/include/linux/swap.h
  14453. +++ b/include/linux/swap.h
  14454. @@ -11,6 +11,7 @@
  14455. #include <linux/fs.h>
  14456. #include <linux/atomic.h>
  14457. #include <linux/page-flags.h>
  14458. +#include <linux/locallock.h>
  14459. #include <asm/page.h>
  14460. struct notifier_block;
  14461. @@ -252,7 +253,8 @@ struct swap_info_struct {
  14462. void *workingset_eviction(struct address_space *mapping, struct page *page);
  14463. bool workingset_refault(void *shadow);
  14464. void workingset_activation(struct page *page);
  14465. -extern struct list_lru workingset_shadow_nodes;
  14466. +extern struct list_lru __workingset_shadow_nodes;
  14467. +DECLARE_LOCAL_IRQ_LOCK(workingset_shadow_lock);
  14468. static inline unsigned int workingset_node_pages(struct radix_tree_node *node)
  14469. {
  14470. @@ -298,6 +300,7 @@ extern unsigned long nr_free_pagecache_pages(void);
  14471. /* linux/mm/swap.c */
  14472. +DECLARE_LOCAL_IRQ_LOCK(swapvec_lock);
  14473. extern void lru_cache_add(struct page *);
  14474. extern void lru_cache_add_anon(struct page *page);
  14475. extern void lru_cache_add_file(struct page *page);
  14476. diff --git a/include/linux/swork.h b/include/linux/swork.h
  14477. new file mode 100644
  14478. index 000000000000..f175fa9a6016
  14479. --- /dev/null
  14480. +++ b/include/linux/swork.h
  14481. @@ -0,0 +1,24 @@
  14482. +#ifndef _LINUX_SWORK_H
  14483. +#define _LINUX_SWORK_H
  14484. +
  14485. +#include <linux/list.h>
  14486. +
  14487. +struct swork_event {
  14488. + struct list_head item;
  14489. + unsigned long flags;
  14490. + void (*func)(struct swork_event *);
  14491. +};
  14492. +
  14493. +static inline void INIT_SWORK(struct swork_event *event,
  14494. + void (*func)(struct swork_event *))
  14495. +{
  14496. + event->flags = 0;
  14497. + event->func = func;
  14498. +}
  14499. +
  14500. +bool swork_queue(struct swork_event *sev);
  14501. +
  14502. +int swork_get(void);
  14503. +void swork_put(void);
  14504. +
  14505. +#endif /* _LINUX_SWORK_H */
  14506. diff --git a/include/linux/thread_info.h b/include/linux/thread_info.h
  14507. index ff307b548ed3..be9f9dc6a4e1 100644
  14508. --- a/include/linux/thread_info.h
  14509. +++ b/include/linux/thread_info.h
  14510. @@ -102,7 +102,17 @@ static inline int test_ti_thread_flag(struct thread_info *ti, int flag)
  14511. #define test_thread_flag(flag) \
  14512. test_ti_thread_flag(current_thread_info(), flag)
  14513. -#define tif_need_resched() test_thread_flag(TIF_NEED_RESCHED)
  14514. +#ifdef CONFIG_PREEMPT_LAZY
  14515. +#define tif_need_resched() (test_thread_flag(TIF_NEED_RESCHED) || \
  14516. + test_thread_flag(TIF_NEED_RESCHED_LAZY))
  14517. +#define tif_need_resched_now() (test_thread_flag(TIF_NEED_RESCHED))
  14518. +#define tif_need_resched_lazy() test_thread_flag(TIF_NEED_RESCHED_LAZY))
  14519. +
  14520. +#else
  14521. +#define tif_need_resched() test_thread_flag(TIF_NEED_RESCHED)
  14522. +#define tif_need_resched_now() test_thread_flag(TIF_NEED_RESCHED)
  14523. +#define tif_need_resched_lazy() 0
  14524. +#endif
  14525. #if defined TIF_RESTORE_SIGMASK && !defined HAVE_SET_RESTORE_SIGMASK
  14526. /*
  14527. diff --git a/include/linux/timer.h b/include/linux/timer.h
  14528. index 61aa61dc410c..299d2b78591f 100644
  14529. --- a/include/linux/timer.h
  14530. +++ b/include/linux/timer.h
  14531. @@ -225,7 +225,7 @@ extern void add_timer(struct timer_list *timer);
  14532. extern int try_to_del_timer_sync(struct timer_list *timer);
  14533. -#ifdef CONFIG_SMP
  14534. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  14535. extern int del_timer_sync(struct timer_list *timer);
  14536. #else
  14537. # define del_timer_sync(t) del_timer(t)
  14538. diff --git a/include/linux/trace_events.h b/include/linux/trace_events.h
  14539. index 925730bc9fc1..a591f414da6f 100644
  14540. --- a/include/linux/trace_events.h
  14541. +++ b/include/linux/trace_events.h
  14542. @@ -66,6 +66,9 @@ struct trace_entry {
  14543. unsigned char flags;
  14544. unsigned char preempt_count;
  14545. int pid;
  14546. + unsigned short migrate_disable;
  14547. + unsigned short padding;
  14548. + unsigned char preempt_lazy_count;
  14549. };
  14550. #define TRACE_EVENT_TYPE_MAX \
  14551. diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h
  14552. index 558129af828a..cf5c472bbc79 100644
  14553. --- a/include/linux/uaccess.h
  14554. +++ b/include/linux/uaccess.h
  14555. @@ -24,6 +24,7 @@ static __always_inline void pagefault_disabled_dec(void)
  14556. */
  14557. static inline void pagefault_disable(void)
  14558. {
  14559. + migrate_disable();
  14560. pagefault_disabled_inc();
  14561. /*
  14562. * make sure to have issued the store before a pagefault
  14563. @@ -40,6 +41,7 @@ static inline void pagefault_enable(void)
  14564. */
  14565. barrier();
  14566. pagefault_disabled_dec();
  14567. + migrate_enable();
  14568. }
  14569. /*
  14570. diff --git a/include/linux/uprobes.h b/include/linux/uprobes.h
  14571. index 4a29c75b146e..0a294e950df8 100644
  14572. --- a/include/linux/uprobes.h
  14573. +++ b/include/linux/uprobes.h
  14574. @@ -27,6 +27,7 @@
  14575. #include <linux/errno.h>
  14576. #include <linux/rbtree.h>
  14577. #include <linux/types.h>
  14578. +#include <linux/wait.h>
  14579. struct vm_area_struct;
  14580. struct mm_struct;
  14581. diff --git a/include/linux/vmstat.h b/include/linux/vmstat.h
  14582. index 3e5d9075960f..7eaa847cd5a5 100644
  14583. --- a/include/linux/vmstat.h
  14584. +++ b/include/linux/vmstat.h
  14585. @@ -33,7 +33,9 @@ DECLARE_PER_CPU(struct vm_event_state, vm_event_states);
  14586. */
  14587. static inline void __count_vm_event(enum vm_event_item item)
  14588. {
  14589. + preempt_disable_rt();
  14590. raw_cpu_inc(vm_event_states.event[item]);
  14591. + preempt_enable_rt();
  14592. }
  14593. static inline void count_vm_event(enum vm_event_item item)
  14594. @@ -43,7 +45,9 @@ static inline void count_vm_event(enum vm_event_item item)
  14595. static inline void __count_vm_events(enum vm_event_item item, long delta)
  14596. {
  14597. + preempt_disable_rt();
  14598. raw_cpu_add(vm_event_states.event[item], delta);
  14599. + preempt_enable_rt();
  14600. }
  14601. static inline void count_vm_events(enum vm_event_item item, long delta)
  14602. diff --git a/include/linux/wait.h b/include/linux/wait.h
  14603. index 513b36f04dfd..981c8a840f96 100644
  14604. --- a/include/linux/wait.h
  14605. +++ b/include/linux/wait.h
  14606. @@ -8,6 +8,7 @@
  14607. #include <linux/spinlock.h>
  14608. #include <asm/current.h>
  14609. #include <uapi/linux/wait.h>
  14610. +#include <linux/atomic.h>
  14611. typedef struct __wait_queue wait_queue_t;
  14612. typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key);
  14613. diff --git a/include/net/dst.h b/include/net/dst.h
  14614. index c7329dcd90cc..35c3dba16728 100644
  14615. --- a/include/net/dst.h
  14616. +++ b/include/net/dst.h
  14617. @@ -437,7 +437,7 @@ static inline void dst_confirm(struct dst_entry *dst)
  14618. static inline int dst_neigh_output(struct dst_entry *dst, struct neighbour *n,
  14619. struct sk_buff *skb)
  14620. {
  14621. - const struct hh_cache *hh;
  14622. + struct hh_cache *hh;
  14623. if (dst->pending_confirm) {
  14624. unsigned long now = jiffies;
  14625. diff --git a/include/net/neighbour.h b/include/net/neighbour.h
  14626. index 8b683841e574..bf656008f6e7 100644
  14627. --- a/include/net/neighbour.h
  14628. +++ b/include/net/neighbour.h
  14629. @@ -446,7 +446,7 @@ static inline int neigh_hh_bridge(struct hh_cache *hh, struct sk_buff *skb)
  14630. }
  14631. #endif
  14632. -static inline int neigh_hh_output(const struct hh_cache *hh, struct sk_buff *skb)
  14633. +static inline int neigh_hh_output(struct hh_cache *hh, struct sk_buff *skb)
  14634. {
  14635. unsigned int seq;
  14636. int hh_len;
  14637. @@ -501,7 +501,7 @@ struct neighbour_cb {
  14638. #define NEIGH_CB(skb) ((struct neighbour_cb *)(skb)->cb)
  14639. -static inline void neigh_ha_snapshot(char *dst, const struct neighbour *n,
  14640. +static inline void neigh_ha_snapshot(char *dst, struct neighbour *n,
  14641. const struct net_device *dev)
  14642. {
  14643. unsigned int seq;
  14644. diff --git a/include/net/netns/ipv4.h b/include/net/netns/ipv4.h
  14645. index c68926b4899c..dd0751e76065 100644
  14646. --- a/include/net/netns/ipv4.h
  14647. +++ b/include/net/netns/ipv4.h
  14648. @@ -70,6 +70,7 @@ struct netns_ipv4 {
  14649. int sysctl_icmp_echo_ignore_all;
  14650. int sysctl_icmp_echo_ignore_broadcasts;
  14651. + int sysctl_icmp_echo_sysrq;
  14652. int sysctl_icmp_ignore_bogus_error_responses;
  14653. int sysctl_icmp_ratelimit;
  14654. int sysctl_icmp_ratemask;
  14655. diff --git a/include/trace/events/hist.h b/include/trace/events/hist.h
  14656. new file mode 100644
  14657. index 000000000000..f7710de1b1f3
  14658. --- /dev/null
  14659. +++ b/include/trace/events/hist.h
  14660. @@ -0,0 +1,73 @@
  14661. +#undef TRACE_SYSTEM
  14662. +#define TRACE_SYSTEM hist
  14663. +
  14664. +#if !defined(_TRACE_HIST_H) || defined(TRACE_HEADER_MULTI_READ)
  14665. +#define _TRACE_HIST_H
  14666. +
  14667. +#include "latency_hist.h"
  14668. +#include <linux/tracepoint.h>
  14669. +
  14670. +#if !defined(CONFIG_PREEMPT_OFF_HIST) && !defined(CONFIG_INTERRUPT_OFF_HIST)
  14671. +#define trace_preemptirqsoff_hist(a, b)
  14672. +#define trace_preemptirqsoff_hist_rcuidle(a, b)
  14673. +#else
  14674. +TRACE_EVENT(preemptirqsoff_hist,
  14675. +
  14676. + TP_PROTO(int reason, int starthist),
  14677. +
  14678. + TP_ARGS(reason, starthist),
  14679. +
  14680. + TP_STRUCT__entry(
  14681. + __field(int, reason)
  14682. + __field(int, starthist)
  14683. + ),
  14684. +
  14685. + TP_fast_assign(
  14686. + __entry->reason = reason;
  14687. + __entry->starthist = starthist;
  14688. + ),
  14689. +
  14690. + TP_printk("reason=%s starthist=%s", getaction(__entry->reason),
  14691. + __entry->starthist ? "start" : "stop")
  14692. +);
  14693. +#endif
  14694. +
  14695. +#ifndef CONFIG_MISSED_TIMER_OFFSETS_HIST
  14696. +#define trace_hrtimer_interrupt(a, b, c, d)
  14697. +#else
  14698. +TRACE_EVENT(hrtimer_interrupt,
  14699. +
  14700. + TP_PROTO(int cpu, long long offset, struct task_struct *curr,
  14701. + struct task_struct *task),
  14702. +
  14703. + TP_ARGS(cpu, offset, curr, task),
  14704. +
  14705. + TP_STRUCT__entry(
  14706. + __field(int, cpu)
  14707. + __field(long long, offset)
  14708. + __array(char, ccomm, TASK_COMM_LEN)
  14709. + __field(int, cprio)
  14710. + __array(char, tcomm, TASK_COMM_LEN)
  14711. + __field(int, tprio)
  14712. + ),
  14713. +
  14714. + TP_fast_assign(
  14715. + __entry->cpu = cpu;
  14716. + __entry->offset = offset;
  14717. + memcpy(__entry->ccomm, curr->comm, TASK_COMM_LEN);
  14718. + __entry->cprio = curr->prio;
  14719. + memcpy(__entry->tcomm, task != NULL ? task->comm : "<none>",
  14720. + task != NULL ? TASK_COMM_LEN : 7);
  14721. + __entry->tprio = task != NULL ? task->prio : -1;
  14722. + ),
  14723. +
  14724. + TP_printk("cpu=%d offset=%lld curr=%s[%d] thread=%s[%d]",
  14725. + __entry->cpu, __entry->offset, __entry->ccomm,
  14726. + __entry->cprio, __entry->tcomm, __entry->tprio)
  14727. +);
  14728. +#endif
  14729. +
  14730. +#endif /* _TRACE_HIST_H */
  14731. +
  14732. +/* This part must be outside protection */
  14733. +#include <trace/define_trace.h>
  14734. diff --git a/include/trace/events/latency_hist.h b/include/trace/events/latency_hist.h
  14735. new file mode 100644
  14736. index 000000000000..d3f2fbd560b1
  14737. --- /dev/null
  14738. +++ b/include/trace/events/latency_hist.h
  14739. @@ -0,0 +1,29 @@
  14740. +#ifndef _LATENCY_HIST_H
  14741. +#define _LATENCY_HIST_H
  14742. +
  14743. +enum hist_action {
  14744. + IRQS_ON,
  14745. + PREEMPT_ON,
  14746. + TRACE_STOP,
  14747. + IRQS_OFF,
  14748. + PREEMPT_OFF,
  14749. + TRACE_START,
  14750. +};
  14751. +
  14752. +static char *actions[] = {
  14753. + "IRQS_ON",
  14754. + "PREEMPT_ON",
  14755. + "TRACE_STOP",
  14756. + "IRQS_OFF",
  14757. + "PREEMPT_OFF",
  14758. + "TRACE_START",
  14759. +};
  14760. +
  14761. +static inline char *getaction(int action)
  14762. +{
  14763. + if (action >= 0 && action <= sizeof(actions)/sizeof(actions[0]))
  14764. + return actions[action];
  14765. + return "unknown";
  14766. +}
  14767. +
  14768. +#endif /* _LATENCY_HIST_H */
  14769. diff --git a/include/trace/events/writeback.h b/include/trace/events/writeback.h
  14770. index fff846b512e6..73614ce1d204 100644
  14771. --- a/include/trace/events/writeback.h
  14772. +++ b/include/trace/events/writeback.h
  14773. @@ -134,58 +134,28 @@ DEFINE_EVENT(writeback_dirty_inode_template, writeback_dirty_inode,
  14774. #ifdef CREATE_TRACE_POINTS
  14775. #ifdef CONFIG_CGROUP_WRITEBACK
  14776. -static inline size_t __trace_wb_cgroup_size(struct bdi_writeback *wb)
  14777. +static inline unsigned int __trace_wb_assign_cgroup(struct bdi_writeback *wb)
  14778. {
  14779. - return kernfs_path_len(wb->memcg_css->cgroup->kn) + 1;
  14780. + return wb->memcg_css->cgroup->kn->ino;
  14781. }
  14782. -static inline void __trace_wb_assign_cgroup(char *buf, struct bdi_writeback *wb)
  14783. -{
  14784. - struct cgroup *cgrp = wb->memcg_css->cgroup;
  14785. - char *path;
  14786. -
  14787. - path = cgroup_path(cgrp, buf, kernfs_path_len(cgrp->kn) + 1);
  14788. - WARN_ON_ONCE(path != buf);
  14789. -}
  14790. -
  14791. -static inline size_t __trace_wbc_cgroup_size(struct writeback_control *wbc)
  14792. -{
  14793. - if (wbc->wb)
  14794. - return __trace_wb_cgroup_size(wbc->wb);
  14795. - else
  14796. - return 2;
  14797. -}
  14798. -
  14799. -static inline void __trace_wbc_assign_cgroup(char *buf,
  14800. - struct writeback_control *wbc)
  14801. +static inline unsigned int __trace_wbc_assign_cgroup(struct writeback_control *wbc)
  14802. {
  14803. if (wbc->wb)
  14804. - __trace_wb_assign_cgroup(buf, wbc->wb);
  14805. + return __trace_wb_assign_cgroup(wbc->wb);
  14806. else
  14807. - strcpy(buf, "/");
  14808. + return -1U;
  14809. }
  14810. -
  14811. #else /* CONFIG_CGROUP_WRITEBACK */
  14812. -static inline size_t __trace_wb_cgroup_size(struct bdi_writeback *wb)
  14813. -{
  14814. - return 2;
  14815. -}
  14816. -
  14817. -static inline void __trace_wb_assign_cgroup(char *buf, struct bdi_writeback *wb)
  14818. -{
  14819. - strcpy(buf, "/");
  14820. -}
  14821. -
  14822. -static inline size_t __trace_wbc_cgroup_size(struct writeback_control *wbc)
  14823. +static inline unsigned int __trace_wb_assign_cgroup(struct bdi_writeback *wb)
  14824. {
  14825. - return 2;
  14826. + return -1U;
  14827. }
  14828. -static inline void __trace_wbc_assign_cgroup(char *buf,
  14829. - struct writeback_control *wbc)
  14830. +static inline unsigned int __trace_wbc_assign_cgroup(struct writeback_control *wbc)
  14831. {
  14832. - strcpy(buf, "/");
  14833. + return -1U;
  14834. }
  14835. #endif /* CONFIG_CGROUP_WRITEBACK */
  14836. @@ -201,7 +171,7 @@ DECLARE_EVENT_CLASS(writeback_write_inode_template,
  14837. __array(char, name, 32)
  14838. __field(unsigned long, ino)
  14839. __field(int, sync_mode)
  14840. - __dynamic_array(char, cgroup, __trace_wbc_cgroup_size(wbc))
  14841. + __field(unsigned int, cgroup_ino)
  14842. ),
  14843. TP_fast_assign(
  14844. @@ -209,14 +179,14 @@ DECLARE_EVENT_CLASS(writeback_write_inode_template,
  14845. dev_name(inode_to_bdi(inode)->dev), 32);
  14846. __entry->ino = inode->i_ino;
  14847. __entry->sync_mode = wbc->sync_mode;
  14848. - __trace_wbc_assign_cgroup(__get_str(cgroup), wbc);
  14849. + __entry->cgroup_ino = __trace_wbc_assign_cgroup(wbc);
  14850. ),
  14851. - TP_printk("bdi %s: ino=%lu sync_mode=%d cgroup=%s",
  14852. + TP_printk("bdi %s: ino=%lu sync_mode=%d cgroup_ino=%u",
  14853. __entry->name,
  14854. __entry->ino,
  14855. __entry->sync_mode,
  14856. - __get_str(cgroup)
  14857. + __entry->cgroup_ino
  14858. )
  14859. );
  14860. @@ -246,7 +216,7 @@ DECLARE_EVENT_CLASS(writeback_work_class,
  14861. __field(int, range_cyclic)
  14862. __field(int, for_background)
  14863. __field(int, reason)
  14864. - __dynamic_array(char, cgroup, __trace_wb_cgroup_size(wb))
  14865. + __field(unsigned int, cgroup_ino)
  14866. ),
  14867. TP_fast_assign(
  14868. strncpy(__entry->name,
  14869. @@ -258,10 +228,10 @@ DECLARE_EVENT_CLASS(writeback_work_class,
  14870. __entry->range_cyclic = work->range_cyclic;
  14871. __entry->for_background = work->for_background;
  14872. __entry->reason = work->reason;
  14873. - __trace_wb_assign_cgroup(__get_str(cgroup), wb);
  14874. + __entry->cgroup_ino = __trace_wb_assign_cgroup(wb);
  14875. ),
  14876. TP_printk("bdi %s: sb_dev %d:%d nr_pages=%ld sync_mode=%d "
  14877. - "kupdate=%d range_cyclic=%d background=%d reason=%s cgroup=%s",
  14878. + "kupdate=%d range_cyclic=%d background=%d reason=%s cgroup_ino=%u",
  14879. __entry->name,
  14880. MAJOR(__entry->sb_dev), MINOR(__entry->sb_dev),
  14881. __entry->nr_pages,
  14882. @@ -270,7 +240,7 @@ DECLARE_EVENT_CLASS(writeback_work_class,
  14883. __entry->range_cyclic,
  14884. __entry->for_background,
  14885. __print_symbolic(__entry->reason, WB_WORK_REASON),
  14886. - __get_str(cgroup)
  14887. + __entry->cgroup_ino
  14888. )
  14889. );
  14890. #define DEFINE_WRITEBACK_WORK_EVENT(name) \
  14891. @@ -300,15 +270,15 @@ DECLARE_EVENT_CLASS(writeback_class,
  14892. TP_ARGS(wb),
  14893. TP_STRUCT__entry(
  14894. __array(char, name, 32)
  14895. - __dynamic_array(char, cgroup, __trace_wb_cgroup_size(wb))
  14896. + __field(unsigned int, cgroup_ino)
  14897. ),
  14898. TP_fast_assign(
  14899. strncpy(__entry->name, dev_name(wb->bdi->dev), 32);
  14900. - __trace_wb_assign_cgroup(__get_str(cgroup), wb);
  14901. + __entry->cgroup_ino = __trace_wb_assign_cgroup(wb);
  14902. ),
  14903. - TP_printk("bdi %s: cgroup=%s",
  14904. + TP_printk("bdi %s: cgroup_ino=%u",
  14905. __entry->name,
  14906. - __get_str(cgroup)
  14907. + __entry->cgroup_ino
  14908. )
  14909. );
  14910. #define DEFINE_WRITEBACK_EVENT(name) \
  14911. @@ -347,7 +317,7 @@ DECLARE_EVENT_CLASS(wbc_class,
  14912. __field(int, range_cyclic)
  14913. __field(long, range_start)
  14914. __field(long, range_end)
  14915. - __dynamic_array(char, cgroup, __trace_wbc_cgroup_size(wbc))
  14916. + __field(unsigned int, cgroup_ino)
  14917. ),
  14918. TP_fast_assign(
  14919. @@ -361,12 +331,12 @@ DECLARE_EVENT_CLASS(wbc_class,
  14920. __entry->range_cyclic = wbc->range_cyclic;
  14921. __entry->range_start = (long)wbc->range_start;
  14922. __entry->range_end = (long)wbc->range_end;
  14923. - __trace_wbc_assign_cgroup(__get_str(cgroup), wbc);
  14924. + __entry->cgroup_ino = __trace_wbc_assign_cgroup(wbc);
  14925. ),
  14926. TP_printk("bdi %s: towrt=%ld skip=%ld mode=%d kupd=%d "
  14927. "bgrd=%d reclm=%d cyclic=%d "
  14928. - "start=0x%lx end=0x%lx cgroup=%s",
  14929. + "start=0x%lx end=0x%lx cgroup_ino=%u",
  14930. __entry->name,
  14931. __entry->nr_to_write,
  14932. __entry->pages_skipped,
  14933. @@ -377,7 +347,7 @@ DECLARE_EVENT_CLASS(wbc_class,
  14934. __entry->range_cyclic,
  14935. __entry->range_start,
  14936. __entry->range_end,
  14937. - __get_str(cgroup)
  14938. + __entry->cgroup_ino
  14939. )
  14940. )
  14941. @@ -398,7 +368,7 @@ TRACE_EVENT(writeback_queue_io,
  14942. __field(long, age)
  14943. __field(int, moved)
  14944. __field(int, reason)
  14945. - __dynamic_array(char, cgroup, __trace_wb_cgroup_size(wb))
  14946. + __field(unsigned int, cgroup_ino)
  14947. ),
  14948. TP_fast_assign(
  14949. unsigned long *older_than_this = work->older_than_this;
  14950. @@ -408,15 +378,15 @@ TRACE_EVENT(writeback_queue_io,
  14951. (jiffies - *older_than_this) * 1000 / HZ : -1;
  14952. __entry->moved = moved;
  14953. __entry->reason = work->reason;
  14954. - __trace_wb_assign_cgroup(__get_str(cgroup), wb);
  14955. + __entry->cgroup_ino = __trace_wb_assign_cgroup(wb);
  14956. ),
  14957. - TP_printk("bdi %s: older=%lu age=%ld enqueue=%d reason=%s cgroup=%s",
  14958. + TP_printk("bdi %s: older=%lu age=%ld enqueue=%d reason=%s cgroup_ino=%u",
  14959. __entry->name,
  14960. __entry->older, /* older_than_this in jiffies */
  14961. __entry->age, /* older_than_this in relative milliseconds */
  14962. __entry->moved,
  14963. __print_symbolic(__entry->reason, WB_WORK_REASON),
  14964. - __get_str(cgroup)
  14965. + __entry->cgroup_ino
  14966. )
  14967. );
  14968. @@ -484,7 +454,7 @@ TRACE_EVENT(bdi_dirty_ratelimit,
  14969. __field(unsigned long, dirty_ratelimit)
  14970. __field(unsigned long, task_ratelimit)
  14971. __field(unsigned long, balanced_dirty_ratelimit)
  14972. - __dynamic_array(char, cgroup, __trace_wb_cgroup_size(wb))
  14973. + __field(unsigned int, cgroup_ino)
  14974. ),
  14975. TP_fast_assign(
  14976. @@ -496,13 +466,13 @@ TRACE_EVENT(bdi_dirty_ratelimit,
  14977. __entry->task_ratelimit = KBps(task_ratelimit);
  14978. __entry->balanced_dirty_ratelimit =
  14979. KBps(wb->balanced_dirty_ratelimit);
  14980. - __trace_wb_assign_cgroup(__get_str(cgroup), wb);
  14981. + __entry->cgroup_ino = __trace_wb_assign_cgroup(wb);
  14982. ),
  14983. TP_printk("bdi %s: "
  14984. "write_bw=%lu awrite_bw=%lu dirty_rate=%lu "
  14985. "dirty_ratelimit=%lu task_ratelimit=%lu "
  14986. - "balanced_dirty_ratelimit=%lu cgroup=%s",
  14987. + "balanced_dirty_ratelimit=%lu cgroup_ino=%u",
  14988. __entry->bdi,
  14989. __entry->write_bw, /* write bandwidth */
  14990. __entry->avg_write_bw, /* avg write bandwidth */
  14991. @@ -510,7 +480,7 @@ TRACE_EVENT(bdi_dirty_ratelimit,
  14992. __entry->dirty_ratelimit, /* base ratelimit */
  14993. __entry->task_ratelimit, /* ratelimit with position control */
  14994. __entry->balanced_dirty_ratelimit, /* the balanced ratelimit */
  14995. - __get_str(cgroup)
  14996. + __entry->cgroup_ino
  14997. )
  14998. );
  14999. @@ -548,7 +518,7 @@ TRACE_EVENT(balance_dirty_pages,
  15000. __field( long, pause)
  15001. __field(unsigned long, period)
  15002. __field( long, think)
  15003. - __dynamic_array(char, cgroup, __trace_wb_cgroup_size(wb))
  15004. + __field(unsigned int, cgroup_ino)
  15005. ),
  15006. TP_fast_assign(
  15007. @@ -571,7 +541,7 @@ TRACE_EVENT(balance_dirty_pages,
  15008. __entry->period = period * 1000 / HZ;
  15009. __entry->pause = pause * 1000 / HZ;
  15010. __entry->paused = (jiffies - start_time) * 1000 / HZ;
  15011. - __trace_wb_assign_cgroup(__get_str(cgroup), wb);
  15012. + __entry->cgroup_ino = __trace_wb_assign_cgroup(wb);
  15013. ),
  15014. @@ -580,7 +550,7 @@ TRACE_EVENT(balance_dirty_pages,
  15015. "bdi_setpoint=%lu bdi_dirty=%lu "
  15016. "dirty_ratelimit=%lu task_ratelimit=%lu "
  15017. "dirtied=%u dirtied_pause=%u "
  15018. - "paused=%lu pause=%ld period=%lu think=%ld cgroup=%s",
  15019. + "paused=%lu pause=%ld period=%lu think=%ld cgroup_ino=%u",
  15020. __entry->bdi,
  15021. __entry->limit,
  15022. __entry->setpoint,
  15023. @@ -595,7 +565,7 @@ TRACE_EVENT(balance_dirty_pages,
  15024. __entry->pause, /* ms */
  15025. __entry->period, /* ms */
  15026. __entry->think, /* ms */
  15027. - __get_str(cgroup)
  15028. + __entry->cgroup_ino
  15029. )
  15030. );
  15031. @@ -609,8 +579,7 @@ TRACE_EVENT(writeback_sb_inodes_requeue,
  15032. __field(unsigned long, ino)
  15033. __field(unsigned long, state)
  15034. __field(unsigned long, dirtied_when)
  15035. - __dynamic_array(char, cgroup,
  15036. - __trace_wb_cgroup_size(inode_to_wb(inode)))
  15037. + __field(unsigned int, cgroup_ino)
  15038. ),
  15039. TP_fast_assign(
  15040. @@ -619,16 +588,16 @@ TRACE_EVENT(writeback_sb_inodes_requeue,
  15041. __entry->ino = inode->i_ino;
  15042. __entry->state = inode->i_state;
  15043. __entry->dirtied_when = inode->dirtied_when;
  15044. - __trace_wb_assign_cgroup(__get_str(cgroup), inode_to_wb(inode));
  15045. + __entry->cgroup_ino = __trace_wb_assign_cgroup(inode_to_wb(inode));
  15046. ),
  15047. - TP_printk("bdi %s: ino=%lu state=%s dirtied_when=%lu age=%lu cgroup=%s",
  15048. + TP_printk("bdi %s: ino=%lu state=%s dirtied_when=%lu age=%lu cgroup_ino=%u",
  15049. __entry->name,
  15050. __entry->ino,
  15051. show_inode_state(__entry->state),
  15052. __entry->dirtied_when,
  15053. (jiffies - __entry->dirtied_when) / HZ,
  15054. - __get_str(cgroup)
  15055. + __entry->cgroup_ino
  15056. )
  15057. );
  15058. @@ -684,7 +653,7 @@ DECLARE_EVENT_CLASS(writeback_single_inode_template,
  15059. __field(unsigned long, writeback_index)
  15060. __field(long, nr_to_write)
  15061. __field(unsigned long, wrote)
  15062. - __dynamic_array(char, cgroup, __trace_wbc_cgroup_size(wbc))
  15063. + __field(unsigned int, cgroup_ino)
  15064. ),
  15065. TP_fast_assign(
  15066. @@ -696,11 +665,11 @@ DECLARE_EVENT_CLASS(writeback_single_inode_template,
  15067. __entry->writeback_index = inode->i_mapping->writeback_index;
  15068. __entry->nr_to_write = nr_to_write;
  15069. __entry->wrote = nr_to_write - wbc->nr_to_write;
  15070. - __trace_wbc_assign_cgroup(__get_str(cgroup), wbc);
  15071. + __entry->cgroup_ino = __trace_wbc_assign_cgroup(wbc);
  15072. ),
  15073. TP_printk("bdi %s: ino=%lu state=%s dirtied_when=%lu age=%lu "
  15074. - "index=%lu to_write=%ld wrote=%lu cgroup=%s",
  15075. + "index=%lu to_write=%ld wrote=%lu cgroup_ino=%u",
  15076. __entry->name,
  15077. __entry->ino,
  15078. show_inode_state(__entry->state),
  15079. @@ -709,7 +678,7 @@ DECLARE_EVENT_CLASS(writeback_single_inode_template,
  15080. __entry->writeback_index,
  15081. __entry->nr_to_write,
  15082. __entry->wrote,
  15083. - __get_str(cgroup)
  15084. + __entry->cgroup_ino
  15085. )
  15086. );
  15087. diff --git a/init/Kconfig b/init/Kconfig
  15088. index 235c7a2c0d20..a7c81c0911da 100644
  15089. --- a/init/Kconfig
  15090. +++ b/init/Kconfig
  15091. @@ -498,7 +498,7 @@ config TINY_RCU
  15092. config RCU_EXPERT
  15093. bool "Make expert-level adjustments to RCU configuration"
  15094. - default n
  15095. + default y if PREEMPT_RT_FULL
  15096. help
  15097. This option needs to be enabled if you wish to make
  15098. expert-level adjustments to RCU configuration. By default,
  15099. @@ -614,7 +614,7 @@ config RCU_FANOUT_LEAF
  15100. config RCU_FAST_NO_HZ
  15101. bool "Accelerate last non-dyntick-idle CPU's grace periods"
  15102. - depends on NO_HZ_COMMON && SMP && RCU_EXPERT
  15103. + depends on NO_HZ_COMMON && SMP && RCU_EXPERT && !PREEMPT_RT_FULL
  15104. default n
  15105. help
  15106. This option permits CPUs to enter dynticks-idle state even if
  15107. @@ -641,7 +641,7 @@ config TREE_RCU_TRACE
  15108. config RCU_BOOST
  15109. bool "Enable RCU priority boosting"
  15110. depends on RT_MUTEXES && PREEMPT_RCU && RCU_EXPERT
  15111. - default n
  15112. + default y if PREEMPT_RT_FULL
  15113. help
  15114. This option boosts the priority of preempted RCU readers that
  15115. block the current preemptible RCU grace period for too long.
  15116. @@ -1106,6 +1106,7 @@ config CFS_BANDWIDTH
  15117. config RT_GROUP_SCHED
  15118. bool "Group scheduling for SCHED_RR/FIFO"
  15119. depends on CGROUP_SCHED
  15120. + depends on !PREEMPT_RT_FULL
  15121. default n
  15122. help
  15123. This feature lets you explicitly allocate real CPU bandwidth
  15124. @@ -1719,6 +1720,7 @@ choice
  15125. config SLAB
  15126. bool "SLAB"
  15127. + depends on !PREEMPT_RT_FULL
  15128. help
  15129. The regular slab allocator that is established and known to work
  15130. well in all environments. It organizes cache hot objects in
  15131. @@ -1737,6 +1739,7 @@ config SLUB
  15132. config SLOB
  15133. depends on EXPERT
  15134. bool "SLOB (Simple Allocator)"
  15135. + depends on !PREEMPT_RT_FULL
  15136. help
  15137. SLOB replaces the stock allocator with a drastically simpler
  15138. allocator. SLOB is generally more space efficient but
  15139. @@ -1746,7 +1749,7 @@ endchoice
  15140. config SLUB_CPU_PARTIAL
  15141. default y
  15142. - depends on SLUB && SMP
  15143. + depends on SLUB && SMP && !PREEMPT_RT_FULL
  15144. bool "SLUB per cpu partial cache"
  15145. help
  15146. Per cpu partial caches accellerate objects allocation and freeing
  15147. diff --git a/init/Makefile b/init/Makefile
  15148. index 7bc47ee31c36..88cf473554e0 100644
  15149. --- a/init/Makefile
  15150. +++ b/init/Makefile
  15151. @@ -33,4 +33,4 @@ silent_chk_compile.h = :
  15152. include/generated/compile.h: FORCE
  15153. @$($(quiet)chk_compile.h)
  15154. $(Q)$(CONFIG_SHELL) $(srctree)/scripts/mkcompile_h $@ \
  15155. - "$(UTS_MACHINE)" "$(CONFIG_SMP)" "$(CONFIG_PREEMPT)" "$(CC) $(KBUILD_CFLAGS)"
  15156. + "$(UTS_MACHINE)" "$(CONFIG_SMP)" "$(CONFIG_PREEMPT)" "$(CONFIG_PREEMPT_RT_FULL)" "$(CC) $(KBUILD_CFLAGS)"
  15157. diff --git a/init/main.c b/init/main.c
  15158. index 9e64d7097f1a..4a76e629c137 100644
  15159. --- a/init/main.c
  15160. +++ b/init/main.c
  15161. @@ -530,6 +530,7 @@ asmlinkage __visible void __init start_kernel(void)
  15162. setup_command_line(command_line);
  15163. setup_nr_cpu_ids();
  15164. setup_per_cpu_areas();
  15165. + softirq_early_init();
  15166. smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */
  15167. build_all_zonelists(NULL, NULL);
  15168. diff --git a/ipc/msg.c b/ipc/msg.c
  15169. index c6521c205cb4..996d89023552 100644
  15170. --- a/ipc/msg.c
  15171. +++ b/ipc/msg.c
  15172. @@ -183,20 +183,14 @@ static void ss_wakeup(struct list_head *h, int kill)
  15173. }
  15174. }
  15175. -static void expunge_all(struct msg_queue *msq, int res)
  15176. +static void expunge_all(struct msg_queue *msq, int res,
  15177. + struct wake_q_head *wake_q)
  15178. {
  15179. struct msg_receiver *msr, *t;
  15180. list_for_each_entry_safe(msr, t, &msq->q_receivers, r_list) {
  15181. - msr->r_msg = NULL; /* initialize expunge ordering */
  15182. - wake_up_process(msr->r_tsk);
  15183. - /*
  15184. - * Ensure that the wakeup is visible before setting r_msg as
  15185. - * the receiving end depends on it: either spinning on a nil,
  15186. - * or dealing with -EAGAIN cases. See lockless receive part 1
  15187. - * and 2 in do_msgrcv().
  15188. - */
  15189. - smp_wmb(); /* barrier (B) */
  15190. +
  15191. + wake_q_add(wake_q, msr->r_tsk);
  15192. msr->r_msg = ERR_PTR(res);
  15193. }
  15194. }
  15195. @@ -213,11 +207,13 @@ static void freeque(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
  15196. {
  15197. struct msg_msg *msg, *t;
  15198. struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
  15199. + WAKE_Q(wake_q);
  15200. - expunge_all(msq, -EIDRM);
  15201. + expunge_all(msq, -EIDRM, &wake_q);
  15202. ss_wakeup(&msq->q_senders, 1);
  15203. msg_rmid(ns, msq);
  15204. ipc_unlock_object(&msq->q_perm);
  15205. + wake_up_q(&wake_q);
  15206. rcu_read_unlock();
  15207. list_for_each_entry_safe(msg, t, &msq->q_messages, m_list) {
  15208. @@ -342,6 +338,7 @@ static int msgctl_down(struct ipc_namespace *ns, int msqid, int cmd,
  15209. struct kern_ipc_perm *ipcp;
  15210. struct msqid64_ds uninitialized_var(msqid64);
  15211. struct msg_queue *msq;
  15212. + WAKE_Q(wake_q);
  15213. int err;
  15214. if (cmd == IPC_SET) {
  15215. @@ -389,7 +386,7 @@ static int msgctl_down(struct ipc_namespace *ns, int msqid, int cmd,
  15216. /* sleeping receivers might be excluded by
  15217. * stricter permissions.
  15218. */
  15219. - expunge_all(msq, -EAGAIN);
  15220. + expunge_all(msq, -EAGAIN, &wake_q);
  15221. /* sleeping senders might be able to send
  15222. * due to a larger queue size.
  15223. */
  15224. @@ -402,6 +399,7 @@ static int msgctl_down(struct ipc_namespace *ns, int msqid, int cmd,
  15225. out_unlock0:
  15226. ipc_unlock_object(&msq->q_perm);
  15227. + wake_up_q(&wake_q);
  15228. out_unlock1:
  15229. rcu_read_unlock();
  15230. out_up:
  15231. @@ -566,7 +564,8 @@ static int testmsg(struct msg_msg *msg, long type, int mode)
  15232. return 0;
  15233. }
  15234. -static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
  15235. +static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg,
  15236. + struct wake_q_head *wake_q)
  15237. {
  15238. struct msg_receiver *msr, *t;
  15239. @@ -577,27 +576,13 @@ static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
  15240. list_del(&msr->r_list);
  15241. if (msr->r_maxsize < msg->m_ts) {
  15242. - /* initialize pipelined send ordering */
  15243. - msr->r_msg = NULL;
  15244. - wake_up_process(msr->r_tsk);
  15245. - /* barrier (B) see barrier comment below */
  15246. - smp_wmb();
  15247. + wake_q_add(wake_q, msr->r_tsk);
  15248. msr->r_msg = ERR_PTR(-E2BIG);
  15249. } else {
  15250. - msr->r_msg = NULL;
  15251. msq->q_lrpid = task_pid_vnr(msr->r_tsk);
  15252. msq->q_rtime = get_seconds();
  15253. - wake_up_process(msr->r_tsk);
  15254. - /*
  15255. - * Ensure that the wakeup is visible before
  15256. - * setting r_msg, as the receiving can otherwise
  15257. - * exit - once r_msg is set, the receiver can
  15258. - * continue. See lockless receive part 1 and 2
  15259. - * in do_msgrcv(). Barrier (B).
  15260. - */
  15261. - smp_wmb();
  15262. + wake_q_add(wake_q, msr->r_tsk);
  15263. msr->r_msg = msg;
  15264. -
  15265. return 1;
  15266. }
  15267. }
  15268. @@ -613,6 +598,7 @@ long do_msgsnd(int msqid, long mtype, void __user *mtext,
  15269. struct msg_msg *msg;
  15270. int err;
  15271. struct ipc_namespace *ns;
  15272. + WAKE_Q(wake_q);
  15273. ns = current->nsproxy->ipc_ns;
  15274. @@ -698,7 +684,7 @@ long do_msgsnd(int msqid, long mtype, void __user *mtext,
  15275. msq->q_lspid = task_tgid_vnr(current);
  15276. msq->q_stime = get_seconds();
  15277. - if (!pipelined_send(msq, msg)) {
  15278. + if (!pipelined_send(msq, msg, &wake_q)) {
  15279. /* no one is waiting for this message, enqueue it */
  15280. list_add_tail(&msg->m_list, &msq->q_messages);
  15281. msq->q_cbytes += msgsz;
  15282. @@ -712,6 +698,7 @@ long do_msgsnd(int msqid, long mtype, void __user *mtext,
  15283. out_unlock0:
  15284. ipc_unlock_object(&msq->q_perm);
  15285. + wake_up_q(&wake_q);
  15286. out_unlock1:
  15287. rcu_read_unlock();
  15288. if (msg != NULL)
  15289. @@ -932,57 +919,25 @@ long do_msgrcv(int msqid, void __user *buf, size_t bufsz, long msgtyp, int msgfl
  15290. rcu_read_lock();
  15291. /* Lockless receive, part 2:
  15292. - * Wait until pipelined_send or expunge_all are outside of
  15293. - * wake_up_process(). There is a race with exit(), see
  15294. - * ipc/mqueue.c for the details. The correct serialization
  15295. - * ensures that a receiver cannot continue without the wakeup
  15296. - * being visibible _before_ setting r_msg:
  15297. + * The work in pipelined_send() and expunge_all():
  15298. + * - Set pointer to message
  15299. + * - Queue the receiver task for later wakeup
  15300. + * - Wake up the process after the lock is dropped.
  15301. *
  15302. - * CPU 0 CPU 1
  15303. - * <loop receiver>
  15304. - * smp_rmb(); (A) <-- pair -. <waker thread>
  15305. - * <load ->r_msg> | msr->r_msg = NULL;
  15306. - * | wake_up_process();
  15307. - * <continue> `------> smp_wmb(); (B)
  15308. - * msr->r_msg = msg;
  15309. - *
  15310. - * Where (A) orders the message value read and where (B) orders
  15311. - * the write to the r_msg -- done in both pipelined_send and
  15312. - * expunge_all.
  15313. + * Should the process wake up before this wakeup (due to a
  15314. + * signal) it will either see the message and continue …
  15315. */
  15316. - for (;;) {
  15317. - /*
  15318. - * Pairs with writer barrier in pipelined_send
  15319. - * or expunge_all.
  15320. - */
  15321. - smp_rmb(); /* barrier (A) */
  15322. - msg = (struct msg_msg *)msr_d.r_msg;
  15323. - if (msg)
  15324. - break;
  15325. - /*
  15326. - * The cpu_relax() call is a compiler barrier
  15327. - * which forces everything in this loop to be
  15328. - * re-loaded.
  15329. - */
  15330. - cpu_relax();
  15331. - }
  15332. -
  15333. - /* Lockless receive, part 3:
  15334. - * If there is a message or an error then accept it without
  15335. - * locking.
  15336. - */
  15337. + msg = (struct msg_msg *)msr_d.r_msg;
  15338. if (msg != ERR_PTR(-EAGAIN))
  15339. goto out_unlock1;
  15340. - /* Lockless receive, part 3:
  15341. - * Acquire the queue spinlock.
  15342. - */
  15343. + /*
  15344. + * … or see -EAGAIN, acquire the lock to check the message
  15345. + * again.
  15346. + */
  15347. ipc_lock_object(&msq->q_perm);
  15348. - /* Lockless receive, part 4:
  15349. - * Repeat test after acquiring the spinlock.
  15350. - */
  15351. msg = (struct msg_msg *)msr_d.r_msg;
  15352. if (msg != ERR_PTR(-EAGAIN))
  15353. goto out_unlock0;
  15354. diff --git a/ipc/sem.c b/ipc/sem.c
  15355. index 9862c3d1c26d..ef34d7376697 100644
  15356. --- a/ipc/sem.c
  15357. +++ b/ipc/sem.c
  15358. @@ -708,6 +708,13 @@ undo:
  15359. static void wake_up_sem_queue_prepare(struct list_head *pt,
  15360. struct sem_queue *q, int error)
  15361. {
  15362. +#ifdef CONFIG_PREEMPT_RT_BASE
  15363. + struct task_struct *p = q->sleeper;
  15364. + get_task_struct(p);
  15365. + q->status = error;
  15366. + wake_up_process(p);
  15367. + put_task_struct(p);
  15368. +#else
  15369. if (list_empty(pt)) {
  15370. /*
  15371. * Hold preempt off so that we don't get preempted and have the
  15372. @@ -719,6 +726,7 @@ static void wake_up_sem_queue_prepare(struct list_head *pt,
  15373. q->pid = error;
  15374. list_add_tail(&q->list, pt);
  15375. +#endif
  15376. }
  15377. /**
  15378. @@ -732,6 +740,7 @@ static void wake_up_sem_queue_prepare(struct list_head *pt,
  15379. */
  15380. static void wake_up_sem_queue_do(struct list_head *pt)
  15381. {
  15382. +#ifndef CONFIG_PREEMPT_RT_BASE
  15383. struct sem_queue *q, *t;
  15384. int did_something;
  15385. @@ -744,6 +753,7 @@ static void wake_up_sem_queue_do(struct list_head *pt)
  15386. }
  15387. if (did_something)
  15388. preempt_enable();
  15389. +#endif
  15390. }
  15391. static void unlink_queue(struct sem_array *sma, struct sem_queue *q)
  15392. diff --git a/kernel/Kconfig.locks b/kernel/Kconfig.locks
  15393. index ebdb0043203a..b9e6aa7e5aa6 100644
  15394. --- a/kernel/Kconfig.locks
  15395. +++ b/kernel/Kconfig.locks
  15396. @@ -225,11 +225,11 @@ config ARCH_SUPPORTS_ATOMIC_RMW
  15397. config MUTEX_SPIN_ON_OWNER
  15398. def_bool y
  15399. - depends on SMP && !DEBUG_MUTEXES && ARCH_SUPPORTS_ATOMIC_RMW
  15400. + depends on SMP && !DEBUG_MUTEXES && ARCH_SUPPORTS_ATOMIC_RMW && !PREEMPT_RT_FULL
  15401. config RWSEM_SPIN_ON_OWNER
  15402. def_bool y
  15403. - depends on SMP && RWSEM_XCHGADD_ALGORITHM && ARCH_SUPPORTS_ATOMIC_RMW
  15404. + depends on SMP && RWSEM_XCHGADD_ALGORITHM && ARCH_SUPPORTS_ATOMIC_RMW && !PREEMPT_RT_FULL
  15405. config LOCK_SPIN_ON_OWNER
  15406. def_bool y
  15407. diff --git a/kernel/Kconfig.preempt b/kernel/Kconfig.preempt
  15408. index 3f9c97419f02..11dbe26a8279 100644
  15409. --- a/kernel/Kconfig.preempt
  15410. +++ b/kernel/Kconfig.preempt
  15411. @@ -1,3 +1,16 @@
  15412. +config PREEMPT
  15413. + bool
  15414. + select PREEMPT_COUNT
  15415. +
  15416. +config PREEMPT_RT_BASE
  15417. + bool
  15418. + select PREEMPT
  15419. +
  15420. +config HAVE_PREEMPT_LAZY
  15421. + bool
  15422. +
  15423. +config PREEMPT_LAZY
  15424. + def_bool y if HAVE_PREEMPT_LAZY && PREEMPT_RT_FULL
  15425. choice
  15426. prompt "Preemption Model"
  15427. @@ -33,9 +46,9 @@ config PREEMPT_VOLUNTARY
  15428. Select this if you are building a kernel for a desktop system.
  15429. -config PREEMPT
  15430. +config PREEMPT__LL
  15431. bool "Preemptible Kernel (Low-Latency Desktop)"
  15432. - select PREEMPT_COUNT
  15433. + select PREEMPT
  15434. select UNINLINE_SPIN_UNLOCK if !ARCH_INLINE_SPIN_UNLOCK
  15435. help
  15436. This option reduces the latency of the kernel by making
  15437. @@ -52,6 +65,22 @@ config PREEMPT
  15438. embedded system with latency requirements in the milliseconds
  15439. range.
  15440. +config PREEMPT_RTB
  15441. + bool "Preemptible Kernel (Basic RT)"
  15442. + select PREEMPT_RT_BASE
  15443. + help
  15444. + This option is basically the same as (Low-Latency Desktop) but
  15445. + enables changes which are preliminary for the full preemptible
  15446. + RT kernel.
  15447. +
  15448. +config PREEMPT_RT_FULL
  15449. + bool "Fully Preemptible Kernel (RT)"
  15450. + depends on IRQ_FORCED_THREADING
  15451. + select PREEMPT_RT_BASE
  15452. + select PREEMPT_RCU
  15453. + help
  15454. + All and everything
  15455. +
  15456. endchoice
  15457. config PREEMPT_COUNT
  15458. diff --git a/kernel/cgroup.c b/kernel/cgroup.c
  15459. index 4cb94b678e9f..8c41ee8a6fee 100644
  15460. --- a/kernel/cgroup.c
  15461. +++ b/kernel/cgroup.c
  15462. @@ -4741,10 +4741,10 @@ static void css_free_rcu_fn(struct rcu_head *rcu_head)
  15463. queue_work(cgroup_destroy_wq, &css->destroy_work);
  15464. }
  15465. -static void css_release_work_fn(struct work_struct *work)
  15466. +static void css_release_work_fn(struct swork_event *sev)
  15467. {
  15468. struct cgroup_subsys_state *css =
  15469. - container_of(work, struct cgroup_subsys_state, destroy_work);
  15470. + container_of(sev, struct cgroup_subsys_state, destroy_swork);
  15471. struct cgroup_subsys *ss = css->ss;
  15472. struct cgroup *cgrp = css->cgroup;
  15473. @@ -4783,8 +4783,8 @@ static void css_release(struct percpu_ref *ref)
  15474. struct cgroup_subsys_state *css =
  15475. container_of(ref, struct cgroup_subsys_state, refcnt);
  15476. - INIT_WORK(&css->destroy_work, css_release_work_fn);
  15477. - queue_work(cgroup_destroy_wq, &css->destroy_work);
  15478. + INIT_SWORK(&css->destroy_swork, css_release_work_fn);
  15479. + swork_queue(&css->destroy_swork);
  15480. }
  15481. static void init_and_link_css(struct cgroup_subsys_state *css,
  15482. @@ -5401,6 +5401,7 @@ static int __init cgroup_wq_init(void)
  15483. */
  15484. cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
  15485. BUG_ON(!cgroup_destroy_wq);
  15486. + BUG_ON(swork_get());
  15487. /*
  15488. * Used to destroy pidlists and separate to serve as flush domain.
  15489. diff --git a/kernel/cpu.c b/kernel/cpu.c
  15490. index 40d20bf5de28..0be18c1684d8 100644
  15491. --- a/kernel/cpu.c
  15492. +++ b/kernel/cpu.c
  15493. @@ -75,8 +75,8 @@ static struct {
  15494. #endif
  15495. } cpu_hotplug = {
  15496. .active_writer = NULL,
  15497. - .wq = __WAIT_QUEUE_HEAD_INITIALIZER(cpu_hotplug.wq),
  15498. .lock = __MUTEX_INITIALIZER(cpu_hotplug.lock),
  15499. + .wq = __WAIT_QUEUE_HEAD_INITIALIZER(cpu_hotplug.wq),
  15500. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  15501. .dep_map = {.name = "cpu_hotplug.lock" },
  15502. #endif
  15503. @@ -89,6 +89,289 @@ static struct {
  15504. #define cpuhp_lock_acquire() lock_map_acquire(&cpu_hotplug.dep_map)
  15505. #define cpuhp_lock_release() lock_map_release(&cpu_hotplug.dep_map)
  15506. +/**
  15507. + * hotplug_pcp - per cpu hotplug descriptor
  15508. + * @unplug: set when pin_current_cpu() needs to sync tasks
  15509. + * @sync_tsk: the task that waits for tasks to finish pinned sections
  15510. + * @refcount: counter of tasks in pinned sections
  15511. + * @grab_lock: set when the tasks entering pinned sections should wait
  15512. + * @synced: notifier for @sync_tsk to tell cpu_down it's finished
  15513. + * @mutex: the mutex to make tasks wait (used when @grab_lock is true)
  15514. + * @mutex_init: zero if the mutex hasn't been initialized yet.
  15515. + *
  15516. + * Although @unplug and @sync_tsk may point to the same task, the @unplug
  15517. + * is used as a flag and still exists after @sync_tsk has exited and
  15518. + * @sync_tsk set to NULL.
  15519. + */
  15520. +struct hotplug_pcp {
  15521. + struct task_struct *unplug;
  15522. + struct task_struct *sync_tsk;
  15523. + int refcount;
  15524. + int grab_lock;
  15525. + struct completion synced;
  15526. + struct completion unplug_wait;
  15527. +#ifdef CONFIG_PREEMPT_RT_FULL
  15528. + /*
  15529. + * Note, on PREEMPT_RT, the hotplug lock must save the state of
  15530. + * the task, otherwise the mutex will cause the task to fail
  15531. + * to sleep when required. (Because it's called from migrate_disable())
  15532. + *
  15533. + * The spinlock_t on PREEMPT_RT is a mutex that saves the task's
  15534. + * state.
  15535. + */
  15536. + spinlock_t lock;
  15537. +#else
  15538. + struct mutex mutex;
  15539. +#endif
  15540. + int mutex_init;
  15541. +};
  15542. +
  15543. +#ifdef CONFIG_PREEMPT_RT_FULL
  15544. +# define hotplug_lock(hp) rt_spin_lock__no_mg(&(hp)->lock)
  15545. +# define hotplug_unlock(hp) rt_spin_unlock__no_mg(&(hp)->lock)
  15546. +#else
  15547. +# define hotplug_lock(hp) mutex_lock(&(hp)->mutex)
  15548. +# define hotplug_unlock(hp) mutex_unlock(&(hp)->mutex)
  15549. +#endif
  15550. +
  15551. +static DEFINE_PER_CPU(struct hotplug_pcp, hotplug_pcp);
  15552. +
  15553. +/**
  15554. + * pin_current_cpu - Prevent the current cpu from being unplugged
  15555. + *
  15556. + * Lightweight version of get_online_cpus() to prevent cpu from being
  15557. + * unplugged when code runs in a migration disabled region.
  15558. + *
  15559. + * Must be called with preemption disabled (preempt_count = 1)!
  15560. + */
  15561. +void pin_current_cpu(void)
  15562. +{
  15563. + struct hotplug_pcp *hp;
  15564. + int force = 0;
  15565. +
  15566. +retry:
  15567. + hp = this_cpu_ptr(&hotplug_pcp);
  15568. +
  15569. + if (!hp->unplug || hp->refcount || force || preempt_count() > 1 ||
  15570. + hp->unplug == current) {
  15571. + hp->refcount++;
  15572. + return;
  15573. + }
  15574. + if (hp->grab_lock) {
  15575. + preempt_enable();
  15576. + hotplug_lock(hp);
  15577. + hotplug_unlock(hp);
  15578. + } else {
  15579. + preempt_enable();
  15580. + /*
  15581. + * Try to push this task off of this CPU.
  15582. + */
  15583. + if (!migrate_me()) {
  15584. + preempt_disable();
  15585. + hp = this_cpu_ptr(&hotplug_pcp);
  15586. + if (!hp->grab_lock) {
  15587. + /*
  15588. + * Just let it continue it's already pinned
  15589. + * or about to sleep.
  15590. + */
  15591. + force = 1;
  15592. + goto retry;
  15593. + }
  15594. + preempt_enable();
  15595. + }
  15596. + }
  15597. + preempt_disable();
  15598. + goto retry;
  15599. +}
  15600. +
  15601. +/**
  15602. + * unpin_current_cpu - Allow unplug of current cpu
  15603. + *
  15604. + * Must be called with preemption or interrupts disabled!
  15605. + */
  15606. +void unpin_current_cpu(void)
  15607. +{
  15608. + struct hotplug_pcp *hp = this_cpu_ptr(&hotplug_pcp);
  15609. +
  15610. + WARN_ON(hp->refcount <= 0);
  15611. +
  15612. + /* This is safe. sync_unplug_thread is pinned to this cpu */
  15613. + if (!--hp->refcount && hp->unplug && hp->unplug != current)
  15614. + wake_up_process(hp->unplug);
  15615. +}
  15616. +
  15617. +static void wait_for_pinned_cpus(struct hotplug_pcp *hp)
  15618. +{
  15619. + set_current_state(TASK_UNINTERRUPTIBLE);
  15620. + while (hp->refcount) {
  15621. + schedule_preempt_disabled();
  15622. + set_current_state(TASK_UNINTERRUPTIBLE);
  15623. + }
  15624. +}
  15625. +
  15626. +static int sync_unplug_thread(void *data)
  15627. +{
  15628. + struct hotplug_pcp *hp = data;
  15629. +
  15630. + wait_for_completion(&hp->unplug_wait);
  15631. + preempt_disable();
  15632. + hp->unplug = current;
  15633. + wait_for_pinned_cpus(hp);
  15634. +
  15635. + /*
  15636. + * This thread will synchronize the cpu_down() with threads
  15637. + * that have pinned the CPU. When the pinned CPU count reaches
  15638. + * zero, we inform the cpu_down code to continue to the next step.
  15639. + */
  15640. + set_current_state(TASK_UNINTERRUPTIBLE);
  15641. + preempt_enable();
  15642. + complete(&hp->synced);
  15643. +
  15644. + /*
  15645. + * If all succeeds, the next step will need tasks to wait till
  15646. + * the CPU is offline before continuing. To do this, the grab_lock
  15647. + * is set and tasks going into pin_current_cpu() will block on the
  15648. + * mutex. But we still need to wait for those that are already in
  15649. + * pinned CPU sections. If the cpu_down() failed, the kthread_should_stop()
  15650. + * will kick this thread out.
  15651. + */
  15652. + while (!hp->grab_lock && !kthread_should_stop()) {
  15653. + schedule();
  15654. + set_current_state(TASK_UNINTERRUPTIBLE);
  15655. + }
  15656. +
  15657. + /* Make sure grab_lock is seen before we see a stale completion */
  15658. + smp_mb();
  15659. +
  15660. + /*
  15661. + * Now just before cpu_down() enters stop machine, we need to make
  15662. + * sure all tasks that are in pinned CPU sections are out, and new
  15663. + * tasks will now grab the lock, keeping them from entering pinned
  15664. + * CPU sections.
  15665. + */
  15666. + if (!kthread_should_stop()) {
  15667. + preempt_disable();
  15668. + wait_for_pinned_cpus(hp);
  15669. + preempt_enable();
  15670. + complete(&hp->synced);
  15671. + }
  15672. +
  15673. + set_current_state(TASK_UNINTERRUPTIBLE);
  15674. + while (!kthread_should_stop()) {
  15675. + schedule();
  15676. + set_current_state(TASK_UNINTERRUPTIBLE);
  15677. + }
  15678. + set_current_state(TASK_RUNNING);
  15679. +
  15680. + /*
  15681. + * Force this thread off this CPU as it's going down and
  15682. + * we don't want any more work on this CPU.
  15683. + */
  15684. + current->flags &= ~PF_NO_SETAFFINITY;
  15685. + set_cpus_allowed_ptr(current, cpu_present_mask);
  15686. + migrate_me();
  15687. + return 0;
  15688. +}
  15689. +
  15690. +static void __cpu_unplug_sync(struct hotplug_pcp *hp)
  15691. +{
  15692. + wake_up_process(hp->sync_tsk);
  15693. + wait_for_completion(&hp->synced);
  15694. +}
  15695. +
  15696. +static void __cpu_unplug_wait(unsigned int cpu)
  15697. +{
  15698. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  15699. +
  15700. + complete(&hp->unplug_wait);
  15701. + wait_for_completion(&hp->synced);
  15702. +}
  15703. +
  15704. +/*
  15705. + * Start the sync_unplug_thread on the target cpu and wait for it to
  15706. + * complete.
  15707. + */
  15708. +static int cpu_unplug_begin(unsigned int cpu)
  15709. +{
  15710. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  15711. + int err;
  15712. +
  15713. + /* Protected by cpu_hotplug.lock */
  15714. + if (!hp->mutex_init) {
  15715. +#ifdef CONFIG_PREEMPT_RT_FULL
  15716. + spin_lock_init(&hp->lock);
  15717. +#else
  15718. + mutex_init(&hp->mutex);
  15719. +#endif
  15720. + hp->mutex_init = 1;
  15721. + }
  15722. +
  15723. + /* Inform the scheduler to migrate tasks off this CPU */
  15724. + tell_sched_cpu_down_begin(cpu);
  15725. +
  15726. + init_completion(&hp->synced);
  15727. + init_completion(&hp->unplug_wait);
  15728. +
  15729. + hp->sync_tsk = kthread_create(sync_unplug_thread, hp, "sync_unplug/%d", cpu);
  15730. + if (IS_ERR(hp->sync_tsk)) {
  15731. + err = PTR_ERR(hp->sync_tsk);
  15732. + hp->sync_tsk = NULL;
  15733. + return err;
  15734. + }
  15735. + kthread_bind(hp->sync_tsk, cpu);
  15736. +
  15737. + /*
  15738. + * Wait for tasks to get out of the pinned sections,
  15739. + * it's still OK if new tasks enter. Some CPU notifiers will
  15740. + * wait for tasks that are going to enter these sections and
  15741. + * we must not have them block.
  15742. + */
  15743. + wake_up_process(hp->sync_tsk);
  15744. + return 0;
  15745. +}
  15746. +
  15747. +static void cpu_unplug_sync(unsigned int cpu)
  15748. +{
  15749. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  15750. +
  15751. + init_completion(&hp->synced);
  15752. + /* The completion needs to be initialzied before setting grab_lock */
  15753. + smp_wmb();
  15754. +
  15755. + /* Grab the mutex before setting grab_lock */
  15756. + hotplug_lock(hp);
  15757. + hp->grab_lock = 1;
  15758. +
  15759. + /*
  15760. + * The CPU notifiers have been completed.
  15761. + * Wait for tasks to get out of pinned CPU sections and have new
  15762. + * tasks block until the CPU is completely down.
  15763. + */
  15764. + __cpu_unplug_sync(hp);
  15765. +
  15766. + /* All done with the sync thread */
  15767. + kthread_stop(hp->sync_tsk);
  15768. + hp->sync_tsk = NULL;
  15769. +}
  15770. +
  15771. +static void cpu_unplug_done(unsigned int cpu)
  15772. +{
  15773. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  15774. +
  15775. + hp->unplug = NULL;
  15776. + /* Let all tasks know cpu unplug is finished before cleaning up */
  15777. + smp_wmb();
  15778. +
  15779. + if (hp->sync_tsk)
  15780. + kthread_stop(hp->sync_tsk);
  15781. +
  15782. + if (hp->grab_lock) {
  15783. + hotplug_unlock(hp);
  15784. + /* protected by cpu_hotplug.lock */
  15785. + hp->grab_lock = 0;
  15786. + }
  15787. + tell_sched_cpu_down_done(cpu);
  15788. +}
  15789. void get_online_cpus(void)
  15790. {
  15791. @@ -338,13 +621,15 @@ static int take_cpu_down(void *_param)
  15792. /* Requires cpu_add_remove_lock to be held */
  15793. static int _cpu_down(unsigned int cpu, int tasks_frozen)
  15794. {
  15795. - int err, nr_calls = 0;
  15796. + int mycpu, err, nr_calls = 0;
  15797. void *hcpu = (void *)(long)cpu;
  15798. unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
  15799. struct take_cpu_down_param tcd_param = {
  15800. .mod = mod,
  15801. .hcpu = hcpu,
  15802. };
  15803. + cpumask_var_t cpumask;
  15804. + cpumask_var_t cpumask_org;
  15805. if (num_online_cpus() == 1)
  15806. return -EBUSY;
  15807. @@ -352,7 +637,34 @@ static int _cpu_down(unsigned int cpu, int tasks_frozen)
  15808. if (!cpu_online(cpu))
  15809. return -EINVAL;
  15810. + /* Move the downtaker off the unplug cpu */
  15811. + if (!alloc_cpumask_var(&cpumask, GFP_KERNEL))
  15812. + return -ENOMEM;
  15813. + if (!alloc_cpumask_var(&cpumask_org, GFP_KERNEL)) {
  15814. + free_cpumask_var(cpumask);
  15815. + return -ENOMEM;
  15816. + }
  15817. +
  15818. + cpumask_copy(cpumask_org, tsk_cpus_allowed(current));
  15819. + cpumask_andnot(cpumask, cpu_online_mask, cpumask_of(cpu));
  15820. + set_cpus_allowed_ptr(current, cpumask);
  15821. + free_cpumask_var(cpumask);
  15822. + migrate_disable();
  15823. + mycpu = smp_processor_id();
  15824. + if (mycpu == cpu) {
  15825. + printk(KERN_ERR "Yuck! Still on unplug CPU\n!");
  15826. + migrate_enable();
  15827. + err = -EBUSY;
  15828. + goto restore_cpus;
  15829. + }
  15830. + migrate_enable();
  15831. +
  15832. cpu_hotplug_begin();
  15833. + err = cpu_unplug_begin(cpu);
  15834. + if (err) {
  15835. + printk("cpu_unplug_begin(%d) failed\n", cpu);
  15836. + goto out_cancel;
  15837. + }
  15838. err = __cpu_notify(CPU_DOWN_PREPARE | mod, hcpu, -1, &nr_calls);
  15839. if (err) {
  15840. @@ -378,8 +690,12 @@ static int _cpu_down(unsigned int cpu, int tasks_frozen)
  15841. else
  15842. synchronize_rcu();
  15843. + __cpu_unplug_wait(cpu);
  15844. smpboot_park_threads(cpu);
  15845. + /* Notifiers are done. Don't let any more tasks pin this CPU. */
  15846. + cpu_unplug_sync(cpu);
  15847. +
  15848. /*
  15849. * Prevent irq alloc/free while the dying cpu reorganizes the
  15850. * interrupt affinities.
  15851. @@ -424,9 +740,14 @@ static int _cpu_down(unsigned int cpu, int tasks_frozen)
  15852. check_for_tasks(cpu);
  15853. out_release:
  15854. + cpu_unplug_done(cpu);
  15855. +out_cancel:
  15856. cpu_hotplug_done();
  15857. if (!err)
  15858. cpu_notify_nofail(CPU_POST_DEAD | mod, hcpu);
  15859. +restore_cpus:
  15860. + set_cpus_allowed_ptr(current, cpumask_org);
  15861. + free_cpumask_var(cpumask_org);
  15862. return err;
  15863. }
  15864. diff --git a/kernel/cpuset.c b/kernel/cpuset.c
  15865. index b271353d5202..dd7b87b7f618 100644
  15866. --- a/kernel/cpuset.c
  15867. +++ b/kernel/cpuset.c
  15868. @@ -283,7 +283,7 @@ static struct cpuset top_cpuset = {
  15869. */
  15870. static DEFINE_MUTEX(cpuset_mutex);
  15871. -static DEFINE_SPINLOCK(callback_lock);
  15872. +static DEFINE_RAW_SPINLOCK(callback_lock);
  15873. static struct workqueue_struct *cpuset_migrate_mm_wq;
  15874. @@ -906,9 +906,9 @@ static void update_cpumasks_hier(struct cpuset *cs, struct cpumask *new_cpus)
  15875. continue;
  15876. rcu_read_unlock();
  15877. - spin_lock_irq(&callback_lock);
  15878. + raw_spin_lock_irq(&callback_lock);
  15879. cpumask_copy(cp->effective_cpus, new_cpus);
  15880. - spin_unlock_irq(&callback_lock);
  15881. + raw_spin_unlock_irq(&callback_lock);
  15882. WARN_ON(!cgroup_subsys_on_dfl(cpuset_cgrp_subsys) &&
  15883. !cpumask_equal(cp->cpus_allowed, cp->effective_cpus));
  15884. @@ -973,9 +973,9 @@ static int update_cpumask(struct cpuset *cs, struct cpuset *trialcs,
  15885. if (retval < 0)
  15886. return retval;
  15887. - spin_lock_irq(&callback_lock);
  15888. + raw_spin_lock_irq(&callback_lock);
  15889. cpumask_copy(cs->cpus_allowed, trialcs->cpus_allowed);
  15890. - spin_unlock_irq(&callback_lock);
  15891. + raw_spin_unlock_irq(&callback_lock);
  15892. /* use trialcs->cpus_allowed as a temp variable */
  15893. update_cpumasks_hier(cs, trialcs->cpus_allowed);
  15894. @@ -1184,9 +1184,9 @@ static void update_nodemasks_hier(struct cpuset *cs, nodemask_t *new_mems)
  15895. continue;
  15896. rcu_read_unlock();
  15897. - spin_lock_irq(&callback_lock);
  15898. + raw_spin_lock_irq(&callback_lock);
  15899. cp->effective_mems = *new_mems;
  15900. - spin_unlock_irq(&callback_lock);
  15901. + raw_spin_unlock_irq(&callback_lock);
  15902. WARN_ON(!cgroup_subsys_on_dfl(cpuset_cgrp_subsys) &&
  15903. !nodes_equal(cp->mems_allowed, cp->effective_mems));
  15904. @@ -1254,9 +1254,9 @@ static int update_nodemask(struct cpuset *cs, struct cpuset *trialcs,
  15905. if (retval < 0)
  15906. goto done;
  15907. - spin_lock_irq(&callback_lock);
  15908. + raw_spin_lock_irq(&callback_lock);
  15909. cs->mems_allowed = trialcs->mems_allowed;
  15910. - spin_unlock_irq(&callback_lock);
  15911. + raw_spin_unlock_irq(&callback_lock);
  15912. /* use trialcs->mems_allowed as a temp variable */
  15913. update_nodemasks_hier(cs, &trialcs->mems_allowed);
  15914. @@ -1347,9 +1347,9 @@ static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs,
  15915. spread_flag_changed = ((is_spread_slab(cs) != is_spread_slab(trialcs))
  15916. || (is_spread_page(cs) != is_spread_page(trialcs)));
  15917. - spin_lock_irq(&callback_lock);
  15918. + raw_spin_lock_irq(&callback_lock);
  15919. cs->flags = trialcs->flags;
  15920. - spin_unlock_irq(&callback_lock);
  15921. + raw_spin_unlock_irq(&callback_lock);
  15922. if (!cpumask_empty(trialcs->cpus_allowed) && balance_flag_changed)
  15923. rebuild_sched_domains_locked();
  15924. @@ -1761,7 +1761,7 @@ static int cpuset_common_seq_show(struct seq_file *sf, void *v)
  15925. cpuset_filetype_t type = seq_cft(sf)->private;
  15926. int ret = 0;
  15927. - spin_lock_irq(&callback_lock);
  15928. + raw_spin_lock_irq(&callback_lock);
  15929. switch (type) {
  15930. case FILE_CPULIST:
  15931. @@ -1780,7 +1780,7 @@ static int cpuset_common_seq_show(struct seq_file *sf, void *v)
  15932. ret = -EINVAL;
  15933. }
  15934. - spin_unlock_irq(&callback_lock);
  15935. + raw_spin_unlock_irq(&callback_lock);
  15936. return ret;
  15937. }
  15938. @@ -1994,12 +1994,12 @@ static int cpuset_css_online(struct cgroup_subsys_state *css)
  15939. cpuset_inc();
  15940. - spin_lock_irq(&callback_lock);
  15941. + raw_spin_lock_irq(&callback_lock);
  15942. if (cgroup_subsys_on_dfl(cpuset_cgrp_subsys)) {
  15943. cpumask_copy(cs->effective_cpus, parent->effective_cpus);
  15944. cs->effective_mems = parent->effective_mems;
  15945. }
  15946. - spin_unlock_irq(&callback_lock);
  15947. + raw_spin_unlock_irq(&callback_lock);
  15948. if (!test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags))
  15949. goto out_unlock;
  15950. @@ -2026,12 +2026,12 @@ static int cpuset_css_online(struct cgroup_subsys_state *css)
  15951. }
  15952. rcu_read_unlock();
  15953. - spin_lock_irq(&callback_lock);
  15954. + raw_spin_lock_irq(&callback_lock);
  15955. cs->mems_allowed = parent->mems_allowed;
  15956. cs->effective_mems = parent->mems_allowed;
  15957. cpumask_copy(cs->cpus_allowed, parent->cpus_allowed);
  15958. cpumask_copy(cs->effective_cpus, parent->cpus_allowed);
  15959. - spin_unlock_irq(&callback_lock);
  15960. + raw_spin_unlock_irq(&callback_lock);
  15961. out_unlock:
  15962. mutex_unlock(&cpuset_mutex);
  15963. return 0;
  15964. @@ -2070,7 +2070,7 @@ static void cpuset_css_free(struct cgroup_subsys_state *css)
  15965. static void cpuset_bind(struct cgroup_subsys_state *root_css)
  15966. {
  15967. mutex_lock(&cpuset_mutex);
  15968. - spin_lock_irq(&callback_lock);
  15969. + raw_spin_lock_irq(&callback_lock);
  15970. if (cgroup_subsys_on_dfl(cpuset_cgrp_subsys)) {
  15971. cpumask_copy(top_cpuset.cpus_allowed, cpu_possible_mask);
  15972. @@ -2081,7 +2081,7 @@ static void cpuset_bind(struct cgroup_subsys_state *root_css)
  15973. top_cpuset.mems_allowed = top_cpuset.effective_mems;
  15974. }
  15975. - spin_unlock_irq(&callback_lock);
  15976. + raw_spin_unlock_irq(&callback_lock);
  15977. mutex_unlock(&cpuset_mutex);
  15978. }
  15979. @@ -2182,12 +2182,12 @@ hotplug_update_tasks_legacy(struct cpuset *cs,
  15980. {
  15981. bool is_empty;
  15982. - spin_lock_irq(&callback_lock);
  15983. + raw_spin_lock_irq(&callback_lock);
  15984. cpumask_copy(cs->cpus_allowed, new_cpus);
  15985. cpumask_copy(cs->effective_cpus, new_cpus);
  15986. cs->mems_allowed = *new_mems;
  15987. cs->effective_mems = *new_mems;
  15988. - spin_unlock_irq(&callback_lock);
  15989. + raw_spin_unlock_irq(&callback_lock);
  15990. /*
  15991. * Don't call update_tasks_cpumask() if the cpuset becomes empty,
  15992. @@ -2224,10 +2224,10 @@ hotplug_update_tasks(struct cpuset *cs,
  15993. if (nodes_empty(*new_mems))
  15994. *new_mems = parent_cs(cs)->effective_mems;
  15995. - spin_lock_irq(&callback_lock);
  15996. + raw_spin_lock_irq(&callback_lock);
  15997. cpumask_copy(cs->effective_cpus, new_cpus);
  15998. cs->effective_mems = *new_mems;
  15999. - spin_unlock_irq(&callback_lock);
  16000. + raw_spin_unlock_irq(&callback_lock);
  16001. if (cpus_updated)
  16002. update_tasks_cpumask(cs);
  16003. @@ -2313,21 +2313,21 @@ static void cpuset_hotplug_workfn(struct work_struct *work)
  16004. /* synchronize cpus_allowed to cpu_active_mask */
  16005. if (cpus_updated) {
  16006. - spin_lock_irq(&callback_lock);
  16007. + raw_spin_lock_irq(&callback_lock);
  16008. if (!on_dfl)
  16009. cpumask_copy(top_cpuset.cpus_allowed, &new_cpus);
  16010. cpumask_copy(top_cpuset.effective_cpus, &new_cpus);
  16011. - spin_unlock_irq(&callback_lock);
  16012. + raw_spin_unlock_irq(&callback_lock);
  16013. /* we don't mess with cpumasks of tasks in top_cpuset */
  16014. }
  16015. /* synchronize mems_allowed to N_MEMORY */
  16016. if (mems_updated) {
  16017. - spin_lock_irq(&callback_lock);
  16018. + raw_spin_lock_irq(&callback_lock);
  16019. if (!on_dfl)
  16020. top_cpuset.mems_allowed = new_mems;
  16021. top_cpuset.effective_mems = new_mems;
  16022. - spin_unlock_irq(&callback_lock);
  16023. + raw_spin_unlock_irq(&callback_lock);
  16024. update_tasks_nodemask(&top_cpuset);
  16025. }
  16026. @@ -2425,11 +2425,11 @@ void cpuset_cpus_allowed(struct task_struct *tsk, struct cpumask *pmask)
  16027. {
  16028. unsigned long flags;
  16029. - spin_lock_irqsave(&callback_lock, flags);
  16030. + raw_spin_lock_irqsave(&callback_lock, flags);
  16031. rcu_read_lock();
  16032. guarantee_online_cpus(task_cs(tsk), pmask);
  16033. rcu_read_unlock();
  16034. - spin_unlock_irqrestore(&callback_lock, flags);
  16035. + raw_spin_unlock_irqrestore(&callback_lock, flags);
  16036. }
  16037. void cpuset_cpus_allowed_fallback(struct task_struct *tsk)
  16038. @@ -2477,11 +2477,11 @@ nodemask_t cpuset_mems_allowed(struct task_struct *tsk)
  16039. nodemask_t mask;
  16040. unsigned long flags;
  16041. - spin_lock_irqsave(&callback_lock, flags);
  16042. + raw_spin_lock_irqsave(&callback_lock, flags);
  16043. rcu_read_lock();
  16044. guarantee_online_mems(task_cs(tsk), &mask);
  16045. rcu_read_unlock();
  16046. - spin_unlock_irqrestore(&callback_lock, flags);
  16047. + raw_spin_unlock_irqrestore(&callback_lock, flags);
  16048. return mask;
  16049. }
  16050. @@ -2573,14 +2573,14 @@ int __cpuset_node_allowed(int node, gfp_t gfp_mask)
  16051. return 1;
  16052. /* Not hardwall and node outside mems_allowed: scan up cpusets */
  16053. - spin_lock_irqsave(&callback_lock, flags);
  16054. + raw_spin_lock_irqsave(&callback_lock, flags);
  16055. rcu_read_lock();
  16056. cs = nearest_hardwall_ancestor(task_cs(current));
  16057. allowed = node_isset(node, cs->mems_allowed);
  16058. rcu_read_unlock();
  16059. - spin_unlock_irqrestore(&callback_lock, flags);
  16060. + raw_spin_unlock_irqrestore(&callback_lock, flags);
  16061. return allowed;
  16062. }
  16063. diff --git a/kernel/debug/kdb/kdb_io.c b/kernel/debug/kdb/kdb_io.c
  16064. index fc1ef736253c..83c666537a7a 100644
  16065. --- a/kernel/debug/kdb/kdb_io.c
  16066. +++ b/kernel/debug/kdb/kdb_io.c
  16067. @@ -554,7 +554,6 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)
  16068. int linecount;
  16069. int colcount;
  16070. int logging, saved_loglevel = 0;
  16071. - int saved_trap_printk;
  16072. int got_printf_lock = 0;
  16073. int retlen = 0;
  16074. int fnd, len;
  16075. @@ -565,8 +564,6 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)
  16076. unsigned long uninitialized_var(flags);
  16077. preempt_disable();
  16078. - saved_trap_printk = kdb_trap_printk;
  16079. - kdb_trap_printk = 0;
  16080. /* Serialize kdb_printf if multiple cpus try to write at once.
  16081. * But if any cpu goes recursive in kdb, just print the output,
  16082. @@ -855,7 +852,6 @@ kdb_print_out:
  16083. } else {
  16084. __release(kdb_printf_lock);
  16085. }
  16086. - kdb_trap_printk = saved_trap_printk;
  16087. preempt_enable();
  16088. return retlen;
  16089. }
  16090. @@ -865,9 +861,11 @@ int kdb_printf(const char *fmt, ...)
  16091. va_list ap;
  16092. int r;
  16093. + kdb_trap_printk++;
  16094. va_start(ap, fmt);
  16095. r = vkdb_printf(KDB_MSGSRC_INTERNAL, fmt, ap);
  16096. va_end(ap);
  16097. + kdb_trap_printk--;
  16098. return r;
  16099. }
  16100. diff --git a/kernel/events/core.c b/kernel/events/core.c
  16101. index 784ab8fe8714..03166fb2364e 100644
  16102. --- a/kernel/events/core.c
  16103. +++ b/kernel/events/core.c
  16104. @@ -802,6 +802,7 @@ static void __perf_mux_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
  16105. raw_spin_lock_init(&cpuctx->hrtimer_lock);
  16106. hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
  16107. timer->function = perf_mux_hrtimer_handler;
  16108. + timer->irqsafe = 1;
  16109. }
  16110. static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx)
  16111. @@ -7240,6 +7241,7 @@ static void perf_swevent_init_hrtimer(struct perf_event *event)
  16112. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  16113. hwc->hrtimer.function = perf_swevent_hrtimer;
  16114. + hwc->hrtimer.irqsafe = 1;
  16115. /*
  16116. * Since hrtimers have a fixed rate, we can do a static freq->period
  16117. diff --git a/kernel/exit.c b/kernel/exit.c
  16118. index ffba5df4abd5..e199407f8831 100644
  16119. --- a/kernel/exit.c
  16120. +++ b/kernel/exit.c
  16121. @@ -144,7 +144,7 @@ static void __exit_signal(struct task_struct *tsk)
  16122. * Do this under ->siglock, we can race with another thread
  16123. * doing sigqueue_free() if we have SIGQUEUE_PREALLOC signals.
  16124. */
  16125. - flush_sigqueue(&tsk->pending);
  16126. + flush_task_sigqueue(tsk);
  16127. tsk->sighand = NULL;
  16128. spin_unlock(&sighand->siglock);
  16129. diff --git a/kernel/fork.c b/kernel/fork.c
  16130. index 0ee630f3ad4b..0fe2c188e94d 100644
  16131. --- a/kernel/fork.c
  16132. +++ b/kernel/fork.c
  16133. @@ -108,7 +108,7 @@ int max_threads; /* tunable limit on nr_threads */
  16134. DEFINE_PER_CPU(unsigned long, process_counts) = 0;
  16135. -__cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */
  16136. +DEFINE_RWLOCK(tasklist_lock); /* outer */
  16137. #ifdef CONFIG_PROVE_RCU
  16138. int lockdep_tasklist_lock_is_held(void)
  16139. @@ -244,7 +244,9 @@ static inline void put_signal_struct(struct signal_struct *sig)
  16140. if (atomic_dec_and_test(&sig->sigcnt))
  16141. free_signal_struct(sig);
  16142. }
  16143. -
  16144. +#ifdef CONFIG_PREEMPT_RT_BASE
  16145. +static
  16146. +#endif
  16147. void __put_task_struct(struct task_struct *tsk)
  16148. {
  16149. WARN_ON(!tsk->exit_state);
  16150. @@ -261,7 +263,18 @@ void __put_task_struct(struct task_struct *tsk)
  16151. if (!profile_handoff_task(tsk))
  16152. free_task(tsk);
  16153. }
  16154. +#ifndef CONFIG_PREEMPT_RT_BASE
  16155. EXPORT_SYMBOL_GPL(__put_task_struct);
  16156. +#else
  16157. +void __put_task_struct_cb(struct rcu_head *rhp)
  16158. +{
  16159. + struct task_struct *tsk = container_of(rhp, struct task_struct, put_rcu);
  16160. +
  16161. + __put_task_struct(tsk);
  16162. +
  16163. +}
  16164. +EXPORT_SYMBOL_GPL(__put_task_struct_cb);
  16165. +#endif
  16166. void __init __weak arch_task_cache_init(void) { }
  16167. @@ -693,6 +706,19 @@ void __mmdrop(struct mm_struct *mm)
  16168. }
  16169. EXPORT_SYMBOL_GPL(__mmdrop);
  16170. +#ifdef CONFIG_PREEMPT_RT_BASE
  16171. +/*
  16172. + * RCU callback for delayed mm drop. Not strictly rcu, but we don't
  16173. + * want another facility to make this work.
  16174. + */
  16175. +void __mmdrop_delayed(struct rcu_head *rhp)
  16176. +{
  16177. + struct mm_struct *mm = container_of(rhp, struct mm_struct, delayed_drop);
  16178. +
  16179. + __mmdrop(mm);
  16180. +}
  16181. +#endif
  16182. +
  16183. /*
  16184. * Decrement the use count and release all resources for an mm.
  16185. */
  16186. @@ -1243,6 +1269,9 @@ static void rt_mutex_init_task(struct task_struct *p)
  16187. */
  16188. static void posix_cpu_timers_init(struct task_struct *tsk)
  16189. {
  16190. +#ifdef CONFIG_PREEMPT_RT_BASE
  16191. + tsk->posix_timer_list = NULL;
  16192. +#endif
  16193. tsk->cputime_expires.prof_exp = 0;
  16194. tsk->cputime_expires.virt_exp = 0;
  16195. tsk->cputime_expires.sched_exp = 0;
  16196. @@ -1369,15 +1398,16 @@ static struct task_struct *copy_process(unsigned long clone_flags,
  16197. spin_lock_init(&p->alloc_lock);
  16198. init_sigpending(&p->pending);
  16199. + p->sigqueue_cache = NULL;
  16200. p->utime = p->stime = p->gtime = 0;
  16201. p->utimescaled = p->stimescaled = 0;
  16202. prev_cputime_init(&p->prev_cputime);
  16203. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  16204. - seqlock_init(&p->vtime_seqlock);
  16205. + seqcount_init(&p->vtime_seqcount);
  16206. p->vtime_snap = 0;
  16207. - p->vtime_snap_whence = VTIME_SLEEPING;
  16208. + p->vtime_snap_whence = VTIME_INACTIVE;
  16209. #endif
  16210. #if defined(SPLIT_RSS_COUNTING)
  16211. diff --git a/kernel/futex.c b/kernel/futex.c
  16212. index 3057dabf726f..9604b73ada47 100644
  16213. --- a/kernel/futex.c
  16214. +++ b/kernel/futex.c
  16215. @@ -815,7 +815,9 @@ void exit_pi_state_list(struct task_struct *curr)
  16216. * task still owns the PI-state:
  16217. */
  16218. if (head->next != next) {
  16219. + raw_spin_unlock_irq(&curr->pi_lock);
  16220. spin_unlock(&hb->lock);
  16221. + raw_spin_lock_irq(&curr->pi_lock);
  16222. continue;
  16223. }
  16224. @@ -1210,6 +1212,7 @@ static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this,
  16225. struct futex_pi_state *pi_state = this->pi_state;
  16226. u32 uninitialized_var(curval), newval;
  16227. WAKE_Q(wake_q);
  16228. + WAKE_Q(wake_sleeper_q);
  16229. bool deboost;
  16230. int ret = 0;
  16231. @@ -1223,7 +1226,7 @@ static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this,
  16232. if (pi_state->owner != current)
  16233. return -EINVAL;
  16234. - raw_spin_lock(&pi_state->pi_mutex.wait_lock);
  16235. + raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
  16236. new_owner = rt_mutex_next_owner(&pi_state->pi_mutex);
  16237. /*
  16238. @@ -1259,24 +1262,25 @@ static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this,
  16239. ret = -EINVAL;
  16240. }
  16241. if (ret) {
  16242. - raw_spin_unlock(&pi_state->pi_mutex.wait_lock);
  16243. + raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
  16244. return ret;
  16245. }
  16246. - raw_spin_lock_irq(&pi_state->owner->pi_lock);
  16247. + raw_spin_lock(&pi_state->owner->pi_lock);
  16248. WARN_ON(list_empty(&pi_state->list));
  16249. list_del_init(&pi_state->list);
  16250. - raw_spin_unlock_irq(&pi_state->owner->pi_lock);
  16251. + raw_spin_unlock(&pi_state->owner->pi_lock);
  16252. - raw_spin_lock_irq(&new_owner->pi_lock);
  16253. + raw_spin_lock(&new_owner->pi_lock);
  16254. WARN_ON(!list_empty(&pi_state->list));
  16255. list_add(&pi_state->list, &new_owner->pi_state_list);
  16256. pi_state->owner = new_owner;
  16257. - raw_spin_unlock_irq(&new_owner->pi_lock);
  16258. + raw_spin_unlock(&new_owner->pi_lock);
  16259. - raw_spin_unlock(&pi_state->pi_mutex.wait_lock);
  16260. + raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
  16261. - deboost = rt_mutex_futex_unlock(&pi_state->pi_mutex, &wake_q);
  16262. + deboost = rt_mutex_futex_unlock(&pi_state->pi_mutex, &wake_q,
  16263. + &wake_sleeper_q);
  16264. /*
  16265. * First unlock HB so the waiter does not spin on it once he got woken
  16266. @@ -1284,8 +1288,9 @@ static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this,
  16267. * deboost first (and lose our higher priority), then the task might get
  16268. * scheduled away before the wake up can take place.
  16269. */
  16270. - spin_unlock(&hb->lock);
  16271. + deboost |= spin_unlock_no_deboost(&hb->lock);
  16272. wake_up_q(&wake_q);
  16273. + wake_up_q_sleeper(&wake_sleeper_q);
  16274. if (deboost)
  16275. rt_mutex_adjust_prio(current);
  16276. @@ -1822,6 +1827,16 @@ retry_private:
  16277. requeue_pi_wake_futex(this, &key2, hb2);
  16278. drop_count++;
  16279. continue;
  16280. + } else if (ret == -EAGAIN) {
  16281. + /*
  16282. + * Waiter was woken by timeout or
  16283. + * signal and has set pi_blocked_on to
  16284. + * PI_WAKEUP_INPROGRESS before we
  16285. + * tried to enqueue it on the rtmutex.
  16286. + */
  16287. + this->pi_state = NULL;
  16288. + free_pi_state(pi_state);
  16289. + continue;
  16290. } else if (ret) {
  16291. /* -EDEADLK */
  16292. this->pi_state = NULL;
  16293. @@ -2139,11 +2154,11 @@ static int fixup_owner(u32 __user *uaddr, struct futex_q *q, int locked)
  16294. * we returned due to timeout or signal without taking the
  16295. * rt_mutex. Too late.
  16296. */
  16297. - raw_spin_lock(&q->pi_state->pi_mutex.wait_lock);
  16298. + raw_spin_lock_irq(&q->pi_state->pi_mutex.wait_lock);
  16299. owner = rt_mutex_owner(&q->pi_state->pi_mutex);
  16300. if (!owner)
  16301. owner = rt_mutex_next_owner(&q->pi_state->pi_mutex);
  16302. - raw_spin_unlock(&q->pi_state->pi_mutex.wait_lock);
  16303. + raw_spin_unlock_irq(&q->pi_state->pi_mutex.wait_lock);
  16304. ret = fixup_pi_state_owner(uaddr, q, owner);
  16305. goto out;
  16306. }
  16307. @@ -2690,7 +2705,7 @@ static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
  16308. {
  16309. struct hrtimer_sleeper timeout, *to = NULL;
  16310. struct rt_mutex_waiter rt_waiter;
  16311. - struct futex_hash_bucket *hb;
  16312. + struct futex_hash_bucket *hb, *hb2;
  16313. union futex_key key2 = FUTEX_KEY_INIT;
  16314. struct futex_q q = futex_q_init;
  16315. int res, ret;
  16316. @@ -2715,10 +2730,7 @@ static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
  16317. * The waiter is allocated on our stack, manipulated by the requeue
  16318. * code while we sleep on uaddr.
  16319. */
  16320. - debug_rt_mutex_init_waiter(&rt_waiter);
  16321. - RB_CLEAR_NODE(&rt_waiter.pi_tree_entry);
  16322. - RB_CLEAR_NODE(&rt_waiter.tree_entry);
  16323. - rt_waiter.task = NULL;
  16324. + rt_mutex_init_waiter(&rt_waiter, false);
  16325. ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE);
  16326. if (unlikely(ret != 0))
  16327. @@ -2749,20 +2761,55 @@ static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
  16328. /* Queue the futex_q, drop the hb lock, wait for wakeup. */
  16329. futex_wait_queue_me(hb, &q, to);
  16330. - spin_lock(&hb->lock);
  16331. - ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
  16332. - spin_unlock(&hb->lock);
  16333. - if (ret)
  16334. - goto out_put_keys;
  16335. + /*
  16336. + * On RT we must avoid races with requeue and trying to block
  16337. + * on two mutexes (hb->lock and uaddr2's rtmutex) by
  16338. + * serializing access to pi_blocked_on with pi_lock.
  16339. + */
  16340. + raw_spin_lock_irq(&current->pi_lock);
  16341. + if (current->pi_blocked_on) {
  16342. + /*
  16343. + * We have been requeued or are in the process of
  16344. + * being requeued.
  16345. + */
  16346. + raw_spin_unlock_irq(&current->pi_lock);
  16347. + } else {
  16348. + /*
  16349. + * Setting pi_blocked_on to PI_WAKEUP_INPROGRESS
  16350. + * prevents a concurrent requeue from moving us to the
  16351. + * uaddr2 rtmutex. After that we can safely acquire
  16352. + * (and possibly block on) hb->lock.
  16353. + */
  16354. + current->pi_blocked_on = PI_WAKEUP_INPROGRESS;
  16355. + raw_spin_unlock_irq(&current->pi_lock);
  16356. +
  16357. + spin_lock(&hb->lock);
  16358. +
  16359. + /*
  16360. + * Clean up pi_blocked_on. We might leak it otherwise
  16361. + * when we succeeded with the hb->lock in the fast
  16362. + * path.
  16363. + */
  16364. + raw_spin_lock_irq(&current->pi_lock);
  16365. + current->pi_blocked_on = NULL;
  16366. + raw_spin_unlock_irq(&current->pi_lock);
  16367. +
  16368. + ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
  16369. + spin_unlock(&hb->lock);
  16370. + if (ret)
  16371. + goto out_put_keys;
  16372. + }
  16373. /*
  16374. - * In order for us to be here, we know our q.key == key2, and since
  16375. - * we took the hb->lock above, we also know that futex_requeue() has
  16376. - * completed and we no longer have to concern ourselves with a wakeup
  16377. - * race with the atomic proxy lock acquisition by the requeue code. The
  16378. - * futex_requeue dropped our key1 reference and incremented our key2
  16379. - * reference count.
  16380. + * In order to be here, we have either been requeued, are in
  16381. + * the process of being requeued, or requeue successfully
  16382. + * acquired uaddr2 on our behalf. If pi_blocked_on was
  16383. + * non-null above, we may be racing with a requeue. Do not
  16384. + * rely on q->lock_ptr to be hb2->lock until after blocking on
  16385. + * hb->lock or hb2->lock. The futex_requeue dropped our key1
  16386. + * reference and incremented our key2 reference count.
  16387. */
  16388. + hb2 = hash_futex(&key2);
  16389. /* Check if the requeue code acquired the second futex for us. */
  16390. if (!q.rt_waiter) {
  16391. @@ -2771,7 +2818,8 @@ static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
  16392. * did a lock-steal - fix up the PI-state in that case.
  16393. */
  16394. if (q.pi_state && (q.pi_state->owner != current)) {
  16395. - spin_lock(q.lock_ptr);
  16396. + spin_lock(&hb2->lock);
  16397. + BUG_ON(&hb2->lock != q.lock_ptr);
  16398. ret = fixup_pi_state_owner(uaddr2, &q, current);
  16399. if (ret && rt_mutex_owner(&q.pi_state->pi_mutex) == current)
  16400. rt_mutex_unlock(&q.pi_state->pi_mutex);
  16401. @@ -2780,7 +2828,7 @@ static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
  16402. * the requeue_pi() code acquired for us.
  16403. */
  16404. free_pi_state(q.pi_state);
  16405. - spin_unlock(q.lock_ptr);
  16406. + spin_unlock(&hb2->lock);
  16407. }
  16408. } else {
  16409. struct rt_mutex *pi_mutex;
  16410. @@ -2795,7 +2843,8 @@ static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
  16411. ret = rt_mutex_finish_proxy_lock(pi_mutex, to, &rt_waiter);
  16412. debug_rt_mutex_free_waiter(&rt_waiter);
  16413. - spin_lock(q.lock_ptr);
  16414. + spin_lock(&hb2->lock);
  16415. + BUG_ON(&hb2->lock != q.lock_ptr);
  16416. /*
  16417. * Fixup the pi_state owner and possibly acquire the lock if we
  16418. * haven't already.
  16419. diff --git a/kernel/irq/handle.c b/kernel/irq/handle.c
  16420. index 57bff7857e87..6c65c9252991 100644
  16421. --- a/kernel/irq/handle.c
  16422. +++ b/kernel/irq/handle.c
  16423. @@ -134,6 +134,8 @@ void __irq_wake_thread(struct irq_desc *desc, struct irqaction *action)
  16424. irqreturn_t handle_irq_event_percpu(struct irq_desc *desc)
  16425. {
  16426. + struct pt_regs *regs = get_irq_regs();
  16427. + u64 ip = regs ? instruction_pointer(regs) : 0;
  16428. irqreturn_t retval = IRQ_NONE;
  16429. unsigned int flags = 0, irq = desc->irq_data.irq;
  16430. struct irqaction *action = desc->action;
  16431. @@ -176,7 +178,11 @@ irqreturn_t handle_irq_event_percpu(struct irq_desc *desc)
  16432. action = action->next;
  16433. }
  16434. - add_interrupt_randomness(irq, flags);
  16435. +#ifdef CONFIG_PREEMPT_RT_FULL
  16436. + desc->random_ip = ip;
  16437. +#else
  16438. + add_interrupt_randomness(irq, flags, ip);
  16439. +#endif
  16440. if (!noirqdebug)
  16441. note_interrupt(desc, retval);
  16442. diff --git a/kernel/irq/irqdesc.c b/kernel/irq/irqdesc.c
  16443. index 239e2ae2c947..0b73349a42d5 100644
  16444. --- a/kernel/irq/irqdesc.c
  16445. +++ b/kernel/irq/irqdesc.c
  16446. @@ -24,10 +24,27 @@
  16447. static struct lock_class_key irq_desc_lock_class;
  16448. #if defined(CONFIG_SMP)
  16449. +static int __init irq_affinity_setup(char *str)
  16450. +{
  16451. + zalloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
  16452. + cpulist_parse(str, irq_default_affinity);
  16453. + /*
  16454. + * Set at least the boot cpu. We don't want to end up with
  16455. + * bugreports caused by random comandline masks
  16456. + */
  16457. + cpumask_set_cpu(smp_processor_id(), irq_default_affinity);
  16458. + return 1;
  16459. +}
  16460. +__setup("irqaffinity=", irq_affinity_setup);
  16461. +
  16462. static void __init init_irq_default_affinity(void)
  16463. {
  16464. - alloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
  16465. - cpumask_setall(irq_default_affinity);
  16466. +#ifdef CONFIG_CPUMASK_OFFSTACK
  16467. + if (!irq_default_affinity)
  16468. + zalloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
  16469. +#endif
  16470. + if (cpumask_empty(irq_default_affinity))
  16471. + cpumask_setall(irq_default_affinity);
  16472. }
  16473. #else
  16474. static void __init init_irq_default_affinity(void)
  16475. diff --git a/kernel/irq/manage.c b/kernel/irq/manage.c
  16476. index 6ead200370da..8e89554aa345 100644
  16477. --- a/kernel/irq/manage.c
  16478. +++ b/kernel/irq/manage.c
  16479. @@ -22,6 +22,7 @@
  16480. #include "internals.h"
  16481. #ifdef CONFIG_IRQ_FORCED_THREADING
  16482. +# ifndef CONFIG_PREEMPT_RT_BASE
  16483. __read_mostly bool force_irqthreads;
  16484. static int __init setup_forced_irqthreads(char *arg)
  16485. @@ -30,6 +31,7 @@ static int __init setup_forced_irqthreads(char *arg)
  16486. return 0;
  16487. }
  16488. early_param("threadirqs", setup_forced_irqthreads);
  16489. +# endif
  16490. #endif
  16491. static void __synchronize_hardirq(struct irq_desc *desc)
  16492. @@ -181,6 +183,62 @@ static inline void
  16493. irq_get_pending(struct cpumask *mask, struct irq_desc *desc) { }
  16494. #endif
  16495. +#ifdef CONFIG_PREEMPT_RT_FULL
  16496. +static void _irq_affinity_notify(struct irq_affinity_notify *notify);
  16497. +static struct task_struct *set_affinity_helper;
  16498. +static LIST_HEAD(affinity_list);
  16499. +static DEFINE_RAW_SPINLOCK(affinity_list_lock);
  16500. +
  16501. +static int set_affinity_thread(void *unused)
  16502. +{
  16503. + while (1) {
  16504. + struct irq_affinity_notify *notify;
  16505. + int empty;
  16506. +
  16507. + set_current_state(TASK_INTERRUPTIBLE);
  16508. +
  16509. + raw_spin_lock_irq(&affinity_list_lock);
  16510. + empty = list_empty(&affinity_list);
  16511. + raw_spin_unlock_irq(&affinity_list_lock);
  16512. +
  16513. + if (empty)
  16514. + schedule();
  16515. + if (kthread_should_stop())
  16516. + break;
  16517. + set_current_state(TASK_RUNNING);
  16518. +try_next:
  16519. + notify = NULL;
  16520. +
  16521. + raw_spin_lock_irq(&affinity_list_lock);
  16522. + if (!list_empty(&affinity_list)) {
  16523. + notify = list_first_entry(&affinity_list,
  16524. + struct irq_affinity_notify, list);
  16525. + list_del_init(&notify->list);
  16526. + }
  16527. + raw_spin_unlock_irq(&affinity_list_lock);
  16528. +
  16529. + if (!notify)
  16530. + continue;
  16531. + _irq_affinity_notify(notify);
  16532. + goto try_next;
  16533. + }
  16534. + return 0;
  16535. +}
  16536. +
  16537. +static void init_helper_thread(void)
  16538. +{
  16539. + if (set_affinity_helper)
  16540. + return;
  16541. + set_affinity_helper = kthread_run(set_affinity_thread, NULL,
  16542. + "affinity-cb");
  16543. + WARN_ON(IS_ERR(set_affinity_helper));
  16544. +}
  16545. +#else
  16546. +
  16547. +static inline void init_helper_thread(void) { }
  16548. +
  16549. +#endif
  16550. +
  16551. int irq_do_set_affinity(struct irq_data *data, const struct cpumask *mask,
  16552. bool force)
  16553. {
  16554. @@ -220,7 +278,17 @@ int irq_set_affinity_locked(struct irq_data *data, const struct cpumask *mask,
  16555. if (desc->affinity_notify) {
  16556. kref_get(&desc->affinity_notify->kref);
  16557. +
  16558. +#ifdef CONFIG_PREEMPT_RT_FULL
  16559. + raw_spin_lock(&affinity_list_lock);
  16560. + if (list_empty(&desc->affinity_notify->list))
  16561. + list_add_tail(&affinity_list,
  16562. + &desc->affinity_notify->list);
  16563. + raw_spin_unlock(&affinity_list_lock);
  16564. + wake_up_process(set_affinity_helper);
  16565. +#else
  16566. schedule_work(&desc->affinity_notify->work);
  16567. +#endif
  16568. }
  16569. irqd_set(data, IRQD_AFFINITY_SET);
  16570. @@ -258,10 +326,8 @@ int irq_set_affinity_hint(unsigned int irq, const struct cpumask *m)
  16571. }
  16572. EXPORT_SYMBOL_GPL(irq_set_affinity_hint);
  16573. -static void irq_affinity_notify(struct work_struct *work)
  16574. +static void _irq_affinity_notify(struct irq_affinity_notify *notify)
  16575. {
  16576. - struct irq_affinity_notify *notify =
  16577. - container_of(work, struct irq_affinity_notify, work);
  16578. struct irq_desc *desc = irq_to_desc(notify->irq);
  16579. cpumask_var_t cpumask;
  16580. unsigned long flags;
  16581. @@ -283,6 +349,13 @@ out:
  16582. kref_put(&notify->kref, notify->release);
  16583. }
  16584. +static void irq_affinity_notify(struct work_struct *work)
  16585. +{
  16586. + struct irq_affinity_notify *notify =
  16587. + container_of(work, struct irq_affinity_notify, work);
  16588. + _irq_affinity_notify(notify);
  16589. +}
  16590. +
  16591. /**
  16592. * irq_set_affinity_notifier - control notification of IRQ affinity changes
  16593. * @irq: Interrupt for which to enable/disable notification
  16594. @@ -312,6 +385,8 @@ irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify)
  16595. notify->irq = irq;
  16596. kref_init(&notify->kref);
  16597. INIT_WORK(&notify->work, irq_affinity_notify);
  16598. + INIT_LIST_HEAD(&notify->list);
  16599. + init_helper_thread();
  16600. }
  16601. raw_spin_lock_irqsave(&desc->lock, flags);
  16602. @@ -865,7 +940,15 @@ irq_forced_thread_fn(struct irq_desc *desc, struct irqaction *action)
  16603. local_bh_disable();
  16604. ret = action->thread_fn(action->irq, action->dev_id);
  16605. irq_finalize_oneshot(desc, action);
  16606. - local_bh_enable();
  16607. + /*
  16608. + * Interrupts which have real time requirements can be set up
  16609. + * to avoid softirq processing in the thread handler. This is
  16610. + * safe as these interrupts do not raise soft interrupts.
  16611. + */
  16612. + if (irq_settings_no_softirq_call(desc))
  16613. + _local_bh_enable();
  16614. + else
  16615. + local_bh_enable();
  16616. return ret;
  16617. }
  16618. @@ -962,6 +1045,12 @@ static int irq_thread(void *data)
  16619. if (action_ret == IRQ_WAKE_THREAD)
  16620. irq_wake_secondary(desc, action);
  16621. +#ifdef CONFIG_PREEMPT_RT_FULL
  16622. + migrate_disable();
  16623. + add_interrupt_randomness(action->irq, 0,
  16624. + desc->random_ip ^ (unsigned long) action);
  16625. + migrate_enable();
  16626. +#endif
  16627. wake_threads_waitq(desc);
  16628. }
  16629. @@ -1315,6 +1404,9 @@ __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
  16630. irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
  16631. }
  16632. + if (new->flags & IRQF_NO_SOFTIRQ_CALL)
  16633. + irq_settings_set_no_softirq_call(desc);
  16634. +
  16635. /* Set default affinity mask once everything is setup */
  16636. setup_affinity(desc, mask);
  16637. @@ -1968,7 +2060,7 @@ EXPORT_SYMBOL_GPL(irq_get_irqchip_state);
  16638. * This call sets the internal irqchip state of an interrupt,
  16639. * depending on the value of @which.
  16640. *
  16641. - * This function should be called with preemption disabled if the
  16642. + * This function should be called with migration disabled if the
  16643. * interrupt controller has per-cpu registers.
  16644. */
  16645. int irq_set_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
  16646. diff --git a/kernel/irq/settings.h b/kernel/irq/settings.h
  16647. index 320579d89091..2df2d4445b1e 100644
  16648. --- a/kernel/irq/settings.h
  16649. +++ b/kernel/irq/settings.h
  16650. @@ -16,6 +16,7 @@ enum {
  16651. _IRQ_PER_CPU_DEVID = IRQ_PER_CPU_DEVID,
  16652. _IRQ_IS_POLLED = IRQ_IS_POLLED,
  16653. _IRQ_DISABLE_UNLAZY = IRQ_DISABLE_UNLAZY,
  16654. + _IRQ_NO_SOFTIRQ_CALL = IRQ_NO_SOFTIRQ_CALL,
  16655. _IRQF_MODIFY_MASK = IRQF_MODIFY_MASK,
  16656. };
  16657. @@ -30,6 +31,7 @@ enum {
  16658. #define IRQ_PER_CPU_DEVID GOT_YOU_MORON
  16659. #define IRQ_IS_POLLED GOT_YOU_MORON
  16660. #define IRQ_DISABLE_UNLAZY GOT_YOU_MORON
  16661. +#define IRQ_NO_SOFTIRQ_CALL GOT_YOU_MORON
  16662. #undef IRQF_MODIFY_MASK
  16663. #define IRQF_MODIFY_MASK GOT_YOU_MORON
  16664. @@ -40,6 +42,16 @@ irq_settings_clr_and_set(struct irq_desc *desc, u32 clr, u32 set)
  16665. desc->status_use_accessors |= (set & _IRQF_MODIFY_MASK);
  16666. }
  16667. +static inline bool irq_settings_no_softirq_call(struct irq_desc *desc)
  16668. +{
  16669. + return desc->status_use_accessors & _IRQ_NO_SOFTIRQ_CALL;
  16670. +}
  16671. +
  16672. +static inline void irq_settings_set_no_softirq_call(struct irq_desc *desc)
  16673. +{
  16674. + desc->status_use_accessors |= _IRQ_NO_SOFTIRQ_CALL;
  16675. +}
  16676. +
  16677. static inline bool irq_settings_is_per_cpu(struct irq_desc *desc)
  16678. {
  16679. return desc->status_use_accessors & _IRQ_PER_CPU;
  16680. diff --git a/kernel/irq/spurious.c b/kernel/irq/spurious.c
  16681. index 32144175458d..ed26f2554972 100644
  16682. --- a/kernel/irq/spurious.c
  16683. +++ b/kernel/irq/spurious.c
  16684. @@ -444,6 +444,10 @@ MODULE_PARM_DESC(noirqdebug, "Disable irq lockup detection when true");
  16685. static int __init irqfixup_setup(char *str)
  16686. {
  16687. +#ifdef CONFIG_PREEMPT_RT_BASE
  16688. + pr_warn("irqfixup boot option not supported w/ CONFIG_PREEMPT_RT_BASE\n");
  16689. + return 1;
  16690. +#endif
  16691. irqfixup = 1;
  16692. printk(KERN_WARNING "Misrouted IRQ fixup support enabled.\n");
  16693. printk(KERN_WARNING "This may impact system performance.\n");
  16694. @@ -456,6 +460,10 @@ module_param(irqfixup, int, 0644);
  16695. static int __init irqpoll_setup(char *str)
  16696. {
  16697. +#ifdef CONFIG_PREEMPT_RT_BASE
  16698. + pr_warn("irqpoll boot option not supported w/ CONFIG_PREEMPT_RT_BASE\n");
  16699. + return 1;
  16700. +#endif
  16701. irqfixup = 2;
  16702. printk(KERN_WARNING "Misrouted IRQ fixup and polling support "
  16703. "enabled\n");
  16704. diff --git a/kernel/irq_work.c b/kernel/irq_work.c
  16705. index bcf107ce0854..2899ba0d23d1 100644
  16706. --- a/kernel/irq_work.c
  16707. +++ b/kernel/irq_work.c
  16708. @@ -17,6 +17,7 @@
  16709. #include <linux/cpu.h>
  16710. #include <linux/notifier.h>
  16711. #include <linux/smp.h>
  16712. +#include <linux/interrupt.h>
  16713. #include <asm/processor.h>
  16714. @@ -65,6 +66,8 @@ void __weak arch_irq_work_raise(void)
  16715. */
  16716. bool irq_work_queue_on(struct irq_work *work, int cpu)
  16717. {
  16718. + struct llist_head *list;
  16719. +
  16720. /* All work should have been flushed before going offline */
  16721. WARN_ON_ONCE(cpu_is_offline(cpu));
  16722. @@ -75,7 +78,12 @@ bool irq_work_queue_on(struct irq_work *work, int cpu)
  16723. if (!irq_work_claim(work))
  16724. return false;
  16725. - if (llist_add(&work->llnode, &per_cpu(raised_list, cpu)))
  16726. + if (IS_ENABLED(CONFIG_PREEMPT_RT_FULL) && !(work->flags & IRQ_WORK_HARD_IRQ))
  16727. + list = &per_cpu(lazy_list, cpu);
  16728. + else
  16729. + list = &per_cpu(raised_list, cpu);
  16730. +
  16731. + if (llist_add(&work->llnode, list))
  16732. arch_send_call_function_single_ipi(cpu);
  16733. return true;
  16734. @@ -86,6 +94,9 @@ EXPORT_SYMBOL_GPL(irq_work_queue_on);
  16735. /* Enqueue the irq work @work on the current CPU */
  16736. bool irq_work_queue(struct irq_work *work)
  16737. {
  16738. + struct llist_head *list;
  16739. + bool lazy_work, realtime = IS_ENABLED(CONFIG_PREEMPT_RT_FULL);
  16740. +
  16741. /* Only queue if not already pending */
  16742. if (!irq_work_claim(work))
  16743. return false;
  16744. @@ -93,13 +104,15 @@ bool irq_work_queue(struct irq_work *work)
  16745. /* Queue the entry and raise the IPI if needed. */
  16746. preempt_disable();
  16747. - /* If the work is "lazy", handle it from next tick if any */
  16748. - if (work->flags & IRQ_WORK_LAZY) {
  16749. - if (llist_add(&work->llnode, this_cpu_ptr(&lazy_list)) &&
  16750. - tick_nohz_tick_stopped())
  16751. - arch_irq_work_raise();
  16752. - } else {
  16753. - if (llist_add(&work->llnode, this_cpu_ptr(&raised_list)))
  16754. + lazy_work = work->flags & IRQ_WORK_LAZY;
  16755. +
  16756. + if (lazy_work || (realtime && !(work->flags & IRQ_WORK_HARD_IRQ)))
  16757. + list = this_cpu_ptr(&lazy_list);
  16758. + else
  16759. + list = this_cpu_ptr(&raised_list);
  16760. +
  16761. + if (llist_add(&work->llnode, list)) {
  16762. + if (!lazy_work || tick_nohz_tick_stopped())
  16763. arch_irq_work_raise();
  16764. }
  16765. @@ -116,9 +129,8 @@ bool irq_work_needs_cpu(void)
  16766. raised = this_cpu_ptr(&raised_list);
  16767. lazy = this_cpu_ptr(&lazy_list);
  16768. - if (llist_empty(raised) || arch_irq_work_has_interrupt())
  16769. - if (llist_empty(lazy))
  16770. - return false;
  16771. + if (llist_empty(raised) && llist_empty(lazy))
  16772. + return false;
  16773. /* All work should have been flushed before going offline */
  16774. WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
  16775. @@ -132,7 +144,7 @@ static void irq_work_run_list(struct llist_head *list)
  16776. struct irq_work *work;
  16777. struct llist_node *llnode;
  16778. - BUG_ON(!irqs_disabled());
  16779. + BUG_ON_NONRT(!irqs_disabled());
  16780. if (llist_empty(list))
  16781. return;
  16782. @@ -169,7 +181,16 @@ static void irq_work_run_list(struct llist_head *list)
  16783. void irq_work_run(void)
  16784. {
  16785. irq_work_run_list(this_cpu_ptr(&raised_list));
  16786. - irq_work_run_list(this_cpu_ptr(&lazy_list));
  16787. + if (IS_ENABLED(CONFIG_PREEMPT_RT_FULL)) {
  16788. + /*
  16789. + * NOTE: we raise softirq via IPI for safety,
  16790. + * and execute in irq_work_tick() to move the
  16791. + * overhead from hard to soft irq context.
  16792. + */
  16793. + if (!llist_empty(this_cpu_ptr(&lazy_list)))
  16794. + raise_softirq(TIMER_SOFTIRQ);
  16795. + } else
  16796. + irq_work_run_list(this_cpu_ptr(&lazy_list));
  16797. }
  16798. EXPORT_SYMBOL_GPL(irq_work_run);
  16799. @@ -179,8 +200,17 @@ void irq_work_tick(void)
  16800. if (!llist_empty(raised) && !arch_irq_work_has_interrupt())
  16801. irq_work_run_list(raised);
  16802. +
  16803. + if (!IS_ENABLED(CONFIG_PREEMPT_RT_FULL))
  16804. + irq_work_run_list(this_cpu_ptr(&lazy_list));
  16805. +}
  16806. +
  16807. +#if defined(CONFIG_IRQ_WORK) && defined(CONFIG_PREEMPT_RT_FULL)
  16808. +void irq_work_tick_soft(void)
  16809. +{
  16810. irq_work_run_list(this_cpu_ptr(&lazy_list));
  16811. }
  16812. +#endif
  16813. /*
  16814. * Synchronize against the irq_work @entry, ensures the entry is not
  16815. diff --git a/kernel/ksysfs.c b/kernel/ksysfs.c
  16816. index e83b26464061..c0e08d1cf33e 100644
  16817. --- a/kernel/ksysfs.c
  16818. +++ b/kernel/ksysfs.c
  16819. @@ -136,6 +136,15 @@ KERNEL_ATTR_RO(vmcoreinfo);
  16820. #endif /* CONFIG_KEXEC_CORE */
  16821. +#if defined(CONFIG_PREEMPT_RT_FULL)
  16822. +static ssize_t realtime_show(struct kobject *kobj,
  16823. + struct kobj_attribute *attr, char *buf)
  16824. +{
  16825. + return sprintf(buf, "%d\n", 1);
  16826. +}
  16827. +KERNEL_ATTR_RO(realtime);
  16828. +#endif
  16829. +
  16830. /* whether file capabilities are enabled */
  16831. static ssize_t fscaps_show(struct kobject *kobj,
  16832. struct kobj_attribute *attr, char *buf)
  16833. @@ -203,6 +212,9 @@ static struct attribute * kernel_attrs[] = {
  16834. &vmcoreinfo_attr.attr,
  16835. #endif
  16836. &rcu_expedited_attr.attr,
  16837. +#ifdef CONFIG_PREEMPT_RT_FULL
  16838. + &realtime_attr.attr,
  16839. +#endif
  16840. NULL
  16841. };
  16842. diff --git a/kernel/locking/Makefile b/kernel/locking/Makefile
  16843. index 8e96f6cc2a4a..447b03082d88 100644
  16844. --- a/kernel/locking/Makefile
  16845. +++ b/kernel/locking/Makefile
  16846. @@ -1,5 +1,5 @@
  16847. -obj-y += mutex.o semaphore.o rwsem.o percpu-rwsem.o
  16848. +obj-y += semaphore.o percpu-rwsem.o
  16849. ifdef CONFIG_FUNCTION_TRACER
  16850. CFLAGS_REMOVE_lockdep.o = $(CC_FLAGS_FTRACE)
  16851. @@ -8,7 +8,11 @@ CFLAGS_REMOVE_mutex-debug.o = $(CC_FLAGS_FTRACE)
  16852. CFLAGS_REMOVE_rtmutex-debug.o = $(CC_FLAGS_FTRACE)
  16853. endif
  16854. +ifneq ($(CONFIG_PREEMPT_RT_FULL),y)
  16855. +obj-y += mutex.o
  16856. obj-$(CONFIG_DEBUG_MUTEXES) += mutex-debug.o
  16857. +obj-y += rwsem.o
  16858. +endif
  16859. obj-$(CONFIG_LOCKDEP) += lockdep.o
  16860. ifeq ($(CONFIG_PROC_FS),y)
  16861. obj-$(CONFIG_LOCKDEP) += lockdep_proc.o
  16862. @@ -22,7 +26,10 @@ obj-$(CONFIG_RT_MUTEXES) += rtmutex.o
  16863. obj-$(CONFIG_DEBUG_RT_MUTEXES) += rtmutex-debug.o
  16864. obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock.o
  16865. obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock_debug.o
  16866. +ifneq ($(CONFIG_PREEMPT_RT_FULL),y)
  16867. obj-$(CONFIG_RWSEM_GENERIC_SPINLOCK) += rwsem-spinlock.o
  16868. obj-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem-xadd.o
  16869. +endif
  16870. +obj-$(CONFIG_PREEMPT_RT_FULL) += rt.o
  16871. obj-$(CONFIG_QUEUED_RWLOCKS) += qrwlock.o
  16872. obj-$(CONFIG_LOCK_TORTURE_TEST) += locktorture.o
  16873. diff --git a/kernel/locking/lglock.c b/kernel/locking/lglock.c
  16874. index 951cfcd10b4a..57e0ea72c28a 100644
  16875. --- a/kernel/locking/lglock.c
  16876. +++ b/kernel/locking/lglock.c
  16877. @@ -4,6 +4,15 @@
  16878. #include <linux/cpu.h>
  16879. #include <linux/string.h>
  16880. +#ifndef CONFIG_PREEMPT_RT_FULL
  16881. +# define lg_lock_ptr arch_spinlock_t
  16882. +# define lg_do_lock(l) arch_spin_lock(l)
  16883. +# define lg_do_unlock(l) arch_spin_unlock(l)
  16884. +#else
  16885. +# define lg_lock_ptr struct rt_mutex
  16886. +# define lg_do_lock(l) __rt_spin_lock__no_mg(l)
  16887. +# define lg_do_unlock(l) __rt_spin_unlock(l)
  16888. +#endif
  16889. /*
  16890. * Note there is no uninit, so lglocks cannot be defined in
  16891. * modules (but it's fine to use them from there)
  16892. @@ -12,51 +21,60 @@
  16893. void lg_lock_init(struct lglock *lg, char *name)
  16894. {
  16895. +#ifdef CONFIG_PREEMPT_RT_FULL
  16896. + int i;
  16897. +
  16898. + for_each_possible_cpu(i) {
  16899. + struct rt_mutex *lock = per_cpu_ptr(lg->lock, i);
  16900. +
  16901. + rt_mutex_init(lock);
  16902. + }
  16903. +#endif
  16904. LOCKDEP_INIT_MAP(&lg->lock_dep_map, name, &lg->lock_key, 0);
  16905. }
  16906. EXPORT_SYMBOL(lg_lock_init);
  16907. void lg_local_lock(struct lglock *lg)
  16908. {
  16909. - arch_spinlock_t *lock;
  16910. + lg_lock_ptr *lock;
  16911. - preempt_disable();
  16912. + migrate_disable();
  16913. lock_acquire_shared(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  16914. lock = this_cpu_ptr(lg->lock);
  16915. - arch_spin_lock(lock);
  16916. + lg_do_lock(lock);
  16917. }
  16918. EXPORT_SYMBOL(lg_local_lock);
  16919. void lg_local_unlock(struct lglock *lg)
  16920. {
  16921. - arch_spinlock_t *lock;
  16922. + lg_lock_ptr *lock;
  16923. lock_release(&lg->lock_dep_map, 1, _RET_IP_);
  16924. lock = this_cpu_ptr(lg->lock);
  16925. - arch_spin_unlock(lock);
  16926. - preempt_enable();
  16927. + lg_do_unlock(lock);
  16928. + migrate_enable();
  16929. }
  16930. EXPORT_SYMBOL(lg_local_unlock);
  16931. void lg_local_lock_cpu(struct lglock *lg, int cpu)
  16932. {
  16933. - arch_spinlock_t *lock;
  16934. + lg_lock_ptr *lock;
  16935. - preempt_disable();
  16936. + preempt_disable_nort();
  16937. lock_acquire_shared(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  16938. lock = per_cpu_ptr(lg->lock, cpu);
  16939. - arch_spin_lock(lock);
  16940. + lg_do_lock(lock);
  16941. }
  16942. EXPORT_SYMBOL(lg_local_lock_cpu);
  16943. void lg_local_unlock_cpu(struct lglock *lg, int cpu)
  16944. {
  16945. - arch_spinlock_t *lock;
  16946. + lg_lock_ptr *lock;
  16947. lock_release(&lg->lock_dep_map, 1, _RET_IP_);
  16948. lock = per_cpu_ptr(lg->lock, cpu);
  16949. - arch_spin_unlock(lock);
  16950. - preempt_enable();
  16951. + lg_do_unlock(lock);
  16952. + preempt_enable_nort();
  16953. }
  16954. EXPORT_SYMBOL(lg_local_unlock_cpu);
  16955. @@ -68,30 +86,30 @@ void lg_double_lock(struct lglock *lg, int cpu1, int cpu2)
  16956. if (cpu2 < cpu1)
  16957. swap(cpu1, cpu2);
  16958. - preempt_disable();
  16959. + preempt_disable_nort();
  16960. lock_acquire_shared(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  16961. - arch_spin_lock(per_cpu_ptr(lg->lock, cpu1));
  16962. - arch_spin_lock(per_cpu_ptr(lg->lock, cpu2));
  16963. + lg_do_lock(per_cpu_ptr(lg->lock, cpu1));
  16964. + lg_do_lock(per_cpu_ptr(lg->lock, cpu2));
  16965. }
  16966. void lg_double_unlock(struct lglock *lg, int cpu1, int cpu2)
  16967. {
  16968. lock_release(&lg->lock_dep_map, 1, _RET_IP_);
  16969. - arch_spin_unlock(per_cpu_ptr(lg->lock, cpu1));
  16970. - arch_spin_unlock(per_cpu_ptr(lg->lock, cpu2));
  16971. - preempt_enable();
  16972. + lg_do_unlock(per_cpu_ptr(lg->lock, cpu1));
  16973. + lg_do_unlock(per_cpu_ptr(lg->lock, cpu2));
  16974. + preempt_enable_nort();
  16975. }
  16976. void lg_global_lock(struct lglock *lg)
  16977. {
  16978. int i;
  16979. - preempt_disable();
  16980. + preempt_disable_nort();
  16981. lock_acquire_exclusive(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  16982. for_each_possible_cpu(i) {
  16983. - arch_spinlock_t *lock;
  16984. + lg_lock_ptr *lock;
  16985. lock = per_cpu_ptr(lg->lock, i);
  16986. - arch_spin_lock(lock);
  16987. + lg_do_lock(lock);
  16988. }
  16989. }
  16990. EXPORT_SYMBOL(lg_global_lock);
  16991. @@ -102,10 +120,35 @@ void lg_global_unlock(struct lglock *lg)
  16992. lock_release(&lg->lock_dep_map, 1, _RET_IP_);
  16993. for_each_possible_cpu(i) {
  16994. - arch_spinlock_t *lock;
  16995. + lg_lock_ptr *lock;
  16996. lock = per_cpu_ptr(lg->lock, i);
  16997. - arch_spin_unlock(lock);
  16998. + lg_do_unlock(lock);
  16999. }
  17000. - preempt_enable();
  17001. + preempt_enable_nort();
  17002. }
  17003. EXPORT_SYMBOL(lg_global_unlock);
  17004. +
  17005. +#ifdef CONFIG_PREEMPT_RT_FULL
  17006. +/*
  17007. + * HACK: If you use this, you get to keep the pieces.
  17008. + * Used in queue_stop_cpus_work() when stop machinery
  17009. + * is called from inactive CPU, so we can't schedule.
  17010. + */
  17011. +# define lg_do_trylock_relax(l) \
  17012. + do { \
  17013. + while (!__rt_spin_trylock(l)) \
  17014. + cpu_relax(); \
  17015. + } while (0)
  17016. +
  17017. +void lg_global_trylock_relax(struct lglock *lg)
  17018. +{
  17019. + int i;
  17020. +
  17021. + lock_acquire_exclusive(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  17022. + for_each_possible_cpu(i) {
  17023. + lg_lock_ptr *lock;
  17024. + lock = per_cpu_ptr(lg->lock, i);
  17025. + lg_do_trylock_relax(lock);
  17026. + }
  17027. +}
  17028. +#endif
  17029. diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
  17030. index 60ace56618f6..fd54dbf686cc 100644
  17031. --- a/kernel/locking/lockdep.c
  17032. +++ b/kernel/locking/lockdep.c
  17033. @@ -668,6 +668,7 @@ look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
  17034. struct lockdep_subclass_key *key;
  17035. struct list_head *hash_head;
  17036. struct lock_class *class;
  17037. + bool is_static = false;
  17038. #ifdef CONFIG_DEBUG_LOCKDEP
  17039. /*
  17040. @@ -695,10 +696,23 @@ look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
  17041. /*
  17042. * Static locks do not have their class-keys yet - for them the key
  17043. - * is the lock object itself:
  17044. + * is the lock object itself. If the lock is in the per cpu area,
  17045. + * the canonical address of the lock (per cpu offset removed) is
  17046. + * used.
  17047. */
  17048. - if (unlikely(!lock->key))
  17049. - lock->key = (void *)lock;
  17050. + if (unlikely(!lock->key)) {
  17051. + unsigned long can_addr, addr = (unsigned long)lock;
  17052. +
  17053. + if (__is_kernel_percpu_address(addr, &can_addr))
  17054. + lock->key = (void *)can_addr;
  17055. + else if (__is_module_percpu_address(addr, &can_addr))
  17056. + lock->key = (void *)can_addr;
  17057. + else if (static_obj(lock))
  17058. + lock->key = (void *)lock;
  17059. + else
  17060. + return ERR_PTR(-EINVAL);
  17061. + is_static = true;
  17062. + }
  17063. /*
  17064. * NOTE: the class-key must be unique. For dynamic locks, a static
  17065. @@ -730,7 +744,7 @@ look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
  17066. }
  17067. }
  17068. - return NULL;
  17069. + return is_static || static_obj(lock->key) ? NULL : ERR_PTR(-EINVAL);
  17070. }
  17071. /*
  17072. @@ -748,19 +762,18 @@ register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
  17073. DEBUG_LOCKS_WARN_ON(!irqs_disabled());
  17074. class = look_up_lock_class(lock, subclass);
  17075. - if (likely(class))
  17076. + if (likely(!IS_ERR_OR_NULL(class)))
  17077. goto out_set_class_cache;
  17078. /*
  17079. * Debug-check: all keys must be persistent!
  17080. - */
  17081. - if (!static_obj(lock->key)) {
  17082. + */
  17083. + if (IS_ERR(class)) {
  17084. debug_locks_off();
  17085. printk("INFO: trying to register non-static key.\n");
  17086. printk("the code is fine but needs lockdep annotation.\n");
  17087. printk("turning off the locking correctness validator.\n");
  17088. dump_stack();
  17089. -
  17090. return NULL;
  17091. }
  17092. @@ -3278,7 +3291,7 @@ static int match_held_lock(struct held_lock *hlock, struct lockdep_map *lock)
  17093. * Clearly if the lock hasn't been acquired _ever_, we're not
  17094. * holding it either, so report failure.
  17095. */
  17096. - if (!class)
  17097. + if (IS_ERR_OR_NULL(class))
  17098. return 0;
  17099. /*
  17100. @@ -3525,6 +3538,7 @@ static void check_flags(unsigned long flags)
  17101. }
  17102. }
  17103. +#ifndef CONFIG_PREEMPT_RT_FULL
  17104. /*
  17105. * We dont accurately track softirq state in e.g.
  17106. * hardirq contexts (such as on 4KSTACKS), so only
  17107. @@ -3539,6 +3553,7 @@ static void check_flags(unsigned long flags)
  17108. DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
  17109. }
  17110. }
  17111. +#endif
  17112. if (!debug_locks)
  17113. print_irqtrace_events(current);
  17114. @@ -3977,7 +3992,7 @@ void lockdep_reset_lock(struct lockdep_map *lock)
  17115. * If the class exists we look it up and zap it:
  17116. */
  17117. class = look_up_lock_class(lock, j);
  17118. - if (class)
  17119. + if (!IS_ERR_OR_NULL(class))
  17120. zap_class(class);
  17121. }
  17122. /*
  17123. diff --git a/kernel/locking/locktorture.c b/kernel/locking/locktorture.c
  17124. index 8ef1919d63b2..291fc19e28e0 100644
  17125. --- a/kernel/locking/locktorture.c
  17126. +++ b/kernel/locking/locktorture.c
  17127. @@ -26,7 +26,6 @@
  17128. #include <linux/kthread.h>
  17129. #include <linux/sched/rt.h>
  17130. #include <linux/spinlock.h>
  17131. -#include <linux/rwlock.h>
  17132. #include <linux/mutex.h>
  17133. #include <linux/rwsem.h>
  17134. #include <linux/smp.h>
  17135. diff --git a/kernel/locking/rt.c b/kernel/locking/rt.c
  17136. new file mode 100644
  17137. index 000000000000..d4ab61c1848b
  17138. --- /dev/null
  17139. +++ b/kernel/locking/rt.c
  17140. @@ -0,0 +1,474 @@
  17141. +/*
  17142. + * kernel/rt.c
  17143. + *
  17144. + * Real-Time Preemption Support
  17145. + *
  17146. + * started by Ingo Molnar:
  17147. + *
  17148. + * Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  17149. + * Copyright (C) 2006, Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  17150. + *
  17151. + * historic credit for proving that Linux spinlocks can be implemented via
  17152. + * RT-aware mutexes goes to many people: The Pmutex project (Dirk Grambow
  17153. + * and others) who prototyped it on 2.4 and did lots of comparative
  17154. + * research and analysis; TimeSys, for proving that you can implement a
  17155. + * fully preemptible kernel via the use of IRQ threading and mutexes;
  17156. + * Bill Huey for persuasively arguing on lkml that the mutex model is the
  17157. + * right one; and to MontaVista, who ported pmutexes to 2.6.
  17158. + *
  17159. + * This code is a from-scratch implementation and is not based on pmutexes,
  17160. + * but the idea of converting spinlocks to mutexes is used here too.
  17161. + *
  17162. + * lock debugging, locking tree, deadlock detection:
  17163. + *
  17164. + * Copyright (C) 2004, LynuxWorks, Inc., Igor Manyilov, Bill Huey
  17165. + * Released under the General Public License (GPL).
  17166. + *
  17167. + * Includes portions of the generic R/W semaphore implementation from:
  17168. + *
  17169. + * Copyright (c) 2001 David Howells (dhowells@redhat.com).
  17170. + * - Derived partially from idea by Andrea Arcangeli <andrea@suse.de>
  17171. + * - Derived also from comments by Linus
  17172. + *
  17173. + * Pending ownership of locks and ownership stealing:
  17174. + *
  17175. + * Copyright (C) 2005, Kihon Technologies Inc., Steven Rostedt
  17176. + *
  17177. + * (also by Steven Rostedt)
  17178. + * - Converted single pi_lock to individual task locks.
  17179. + *
  17180. + * By Esben Nielsen:
  17181. + * Doing priority inheritance with help of the scheduler.
  17182. + *
  17183. + * Copyright (C) 2006, Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  17184. + * - major rework based on Esben Nielsens initial patch
  17185. + * - replaced thread_info references by task_struct refs
  17186. + * - removed task->pending_owner dependency
  17187. + * - BKL drop/reacquire for semaphore style locks to avoid deadlocks
  17188. + * in the scheduler return path as discussed with Steven Rostedt
  17189. + *
  17190. + * Copyright (C) 2006, Kihon Technologies Inc.
  17191. + * Steven Rostedt <rostedt@goodmis.org>
  17192. + * - debugged and patched Thomas Gleixner's rework.
  17193. + * - added back the cmpxchg to the rework.
  17194. + * - turned atomic require back on for SMP.
  17195. + */
  17196. +
  17197. +#include <linux/spinlock.h>
  17198. +#include <linux/rtmutex.h>
  17199. +#include <linux/sched.h>
  17200. +#include <linux/delay.h>
  17201. +#include <linux/module.h>
  17202. +#include <linux/kallsyms.h>
  17203. +#include <linux/syscalls.h>
  17204. +#include <linux/interrupt.h>
  17205. +#include <linux/plist.h>
  17206. +#include <linux/fs.h>
  17207. +#include <linux/futex.h>
  17208. +#include <linux/hrtimer.h>
  17209. +
  17210. +#include "rtmutex_common.h"
  17211. +
  17212. +/*
  17213. + * struct mutex functions
  17214. + */
  17215. +void __mutex_do_init(struct mutex *mutex, const char *name,
  17216. + struct lock_class_key *key)
  17217. +{
  17218. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  17219. + /*
  17220. + * Make sure we are not reinitializing a held lock:
  17221. + */
  17222. + debug_check_no_locks_freed((void *)mutex, sizeof(*mutex));
  17223. + lockdep_init_map(&mutex->dep_map, name, key, 0);
  17224. +#endif
  17225. + mutex->lock.save_state = 0;
  17226. +}
  17227. +EXPORT_SYMBOL(__mutex_do_init);
  17228. +
  17229. +void __lockfunc _mutex_lock(struct mutex *lock)
  17230. +{
  17231. + mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  17232. + rt_mutex_lock(&lock->lock);
  17233. +}
  17234. +EXPORT_SYMBOL(_mutex_lock);
  17235. +
  17236. +int __lockfunc _mutex_lock_interruptible(struct mutex *lock)
  17237. +{
  17238. + int ret;
  17239. +
  17240. + mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  17241. + ret = rt_mutex_lock_interruptible(&lock->lock);
  17242. + if (ret)
  17243. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  17244. + return ret;
  17245. +}
  17246. +EXPORT_SYMBOL(_mutex_lock_interruptible);
  17247. +
  17248. +int __lockfunc _mutex_lock_killable(struct mutex *lock)
  17249. +{
  17250. + int ret;
  17251. +
  17252. + mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  17253. + ret = rt_mutex_lock_killable(&lock->lock);
  17254. + if (ret)
  17255. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  17256. + return ret;
  17257. +}
  17258. +EXPORT_SYMBOL(_mutex_lock_killable);
  17259. +
  17260. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  17261. +void __lockfunc _mutex_lock_nested(struct mutex *lock, int subclass)
  17262. +{
  17263. + mutex_acquire_nest(&lock->dep_map, subclass, 0, NULL, _RET_IP_);
  17264. + rt_mutex_lock(&lock->lock);
  17265. +}
  17266. +EXPORT_SYMBOL(_mutex_lock_nested);
  17267. +
  17268. +void __lockfunc _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest)
  17269. +{
  17270. + mutex_acquire_nest(&lock->dep_map, 0, 0, nest, _RET_IP_);
  17271. + rt_mutex_lock(&lock->lock);
  17272. +}
  17273. +EXPORT_SYMBOL(_mutex_lock_nest_lock);
  17274. +
  17275. +int __lockfunc _mutex_lock_interruptible_nested(struct mutex *lock, int subclass)
  17276. +{
  17277. + int ret;
  17278. +
  17279. + mutex_acquire_nest(&lock->dep_map, subclass, 0, NULL, _RET_IP_);
  17280. + ret = rt_mutex_lock_interruptible(&lock->lock);
  17281. + if (ret)
  17282. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  17283. + return ret;
  17284. +}
  17285. +EXPORT_SYMBOL(_mutex_lock_interruptible_nested);
  17286. +
  17287. +int __lockfunc _mutex_lock_killable_nested(struct mutex *lock, int subclass)
  17288. +{
  17289. + int ret;
  17290. +
  17291. + mutex_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
  17292. + ret = rt_mutex_lock_killable(&lock->lock);
  17293. + if (ret)
  17294. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  17295. + return ret;
  17296. +}
  17297. +EXPORT_SYMBOL(_mutex_lock_killable_nested);
  17298. +#endif
  17299. +
  17300. +int __lockfunc _mutex_trylock(struct mutex *lock)
  17301. +{
  17302. + int ret = rt_mutex_trylock(&lock->lock);
  17303. +
  17304. + if (ret)
  17305. + mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  17306. +
  17307. + return ret;
  17308. +}
  17309. +EXPORT_SYMBOL(_mutex_trylock);
  17310. +
  17311. +void __lockfunc _mutex_unlock(struct mutex *lock)
  17312. +{
  17313. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  17314. + rt_mutex_unlock(&lock->lock);
  17315. +}
  17316. +EXPORT_SYMBOL(_mutex_unlock);
  17317. +
  17318. +/*
  17319. + * rwlock_t functions
  17320. + */
  17321. +int __lockfunc rt_write_trylock(rwlock_t *rwlock)
  17322. +{
  17323. + int ret;
  17324. +
  17325. + migrate_disable();
  17326. + ret = rt_mutex_trylock(&rwlock->lock);
  17327. + if (ret)
  17328. + rwlock_acquire(&rwlock->dep_map, 0, 1, _RET_IP_);
  17329. + else
  17330. + migrate_enable();
  17331. +
  17332. + return ret;
  17333. +}
  17334. +EXPORT_SYMBOL(rt_write_trylock);
  17335. +
  17336. +int __lockfunc rt_write_trylock_irqsave(rwlock_t *rwlock, unsigned long *flags)
  17337. +{
  17338. + int ret;
  17339. +
  17340. + *flags = 0;
  17341. + ret = rt_write_trylock(rwlock);
  17342. + return ret;
  17343. +}
  17344. +EXPORT_SYMBOL(rt_write_trylock_irqsave);
  17345. +
  17346. +int __lockfunc rt_read_trylock(rwlock_t *rwlock)
  17347. +{
  17348. + struct rt_mutex *lock = &rwlock->lock;
  17349. + int ret = 1;
  17350. +
  17351. + /*
  17352. + * recursive read locks succeed when current owns the lock,
  17353. + * but not when read_depth == 0 which means that the lock is
  17354. + * write locked.
  17355. + */
  17356. + if (rt_mutex_owner(lock) != current) {
  17357. + migrate_disable();
  17358. + ret = rt_mutex_trylock(lock);
  17359. + if (ret)
  17360. + rwlock_acquire(&rwlock->dep_map, 0, 1, _RET_IP_);
  17361. + else
  17362. + migrate_enable();
  17363. +
  17364. + } else if (!rwlock->read_depth) {
  17365. + ret = 0;
  17366. + }
  17367. +
  17368. + if (ret)
  17369. + rwlock->read_depth++;
  17370. +
  17371. + return ret;
  17372. +}
  17373. +EXPORT_SYMBOL(rt_read_trylock);
  17374. +
  17375. +void __lockfunc rt_write_lock(rwlock_t *rwlock)
  17376. +{
  17377. + rwlock_acquire(&rwlock->dep_map, 0, 0, _RET_IP_);
  17378. + __rt_spin_lock(&rwlock->lock);
  17379. +}
  17380. +EXPORT_SYMBOL(rt_write_lock);
  17381. +
  17382. +void __lockfunc rt_read_lock(rwlock_t *rwlock)
  17383. +{
  17384. + struct rt_mutex *lock = &rwlock->lock;
  17385. +
  17386. +
  17387. + /*
  17388. + * recursive read locks succeed when current owns the lock
  17389. + */
  17390. + if (rt_mutex_owner(lock) != current) {
  17391. + rwlock_acquire(&rwlock->dep_map, 0, 0, _RET_IP_);
  17392. + __rt_spin_lock(lock);
  17393. + }
  17394. + rwlock->read_depth++;
  17395. +}
  17396. +
  17397. +EXPORT_SYMBOL(rt_read_lock);
  17398. +
  17399. +void __lockfunc rt_write_unlock(rwlock_t *rwlock)
  17400. +{
  17401. + /* NOTE: we always pass in '1' for nested, for simplicity */
  17402. + rwlock_release(&rwlock->dep_map, 1, _RET_IP_);
  17403. + __rt_spin_unlock(&rwlock->lock);
  17404. + migrate_enable();
  17405. +}
  17406. +EXPORT_SYMBOL(rt_write_unlock);
  17407. +
  17408. +void __lockfunc rt_read_unlock(rwlock_t *rwlock)
  17409. +{
  17410. + /* Release the lock only when read_depth is down to 0 */
  17411. + if (--rwlock->read_depth == 0) {
  17412. + rwlock_release(&rwlock->dep_map, 1, _RET_IP_);
  17413. + __rt_spin_unlock(&rwlock->lock);
  17414. + migrate_enable();
  17415. + }
  17416. +}
  17417. +EXPORT_SYMBOL(rt_read_unlock);
  17418. +
  17419. +unsigned long __lockfunc rt_write_lock_irqsave(rwlock_t *rwlock)
  17420. +{
  17421. + rt_write_lock(rwlock);
  17422. +
  17423. + return 0;
  17424. +}
  17425. +EXPORT_SYMBOL(rt_write_lock_irqsave);
  17426. +
  17427. +unsigned long __lockfunc rt_read_lock_irqsave(rwlock_t *rwlock)
  17428. +{
  17429. + rt_read_lock(rwlock);
  17430. +
  17431. + return 0;
  17432. +}
  17433. +EXPORT_SYMBOL(rt_read_lock_irqsave);
  17434. +
  17435. +void __rt_rwlock_init(rwlock_t *rwlock, char *name, struct lock_class_key *key)
  17436. +{
  17437. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  17438. + /*
  17439. + * Make sure we are not reinitializing a held lock:
  17440. + */
  17441. + debug_check_no_locks_freed((void *)rwlock, sizeof(*rwlock));
  17442. + lockdep_init_map(&rwlock->dep_map, name, key, 0);
  17443. +#endif
  17444. + rwlock->lock.save_state = 1;
  17445. + rwlock->read_depth = 0;
  17446. +}
  17447. +EXPORT_SYMBOL(__rt_rwlock_init);
  17448. +
  17449. +/*
  17450. + * rw_semaphores
  17451. + */
  17452. +
  17453. +void rt_up_write(struct rw_semaphore *rwsem)
  17454. +{
  17455. + rwsem_release(&rwsem->dep_map, 1, _RET_IP_);
  17456. + rt_mutex_unlock(&rwsem->lock);
  17457. +}
  17458. +EXPORT_SYMBOL(rt_up_write);
  17459. +
  17460. +void __rt_up_read(struct rw_semaphore *rwsem)
  17461. +{
  17462. + if (--rwsem->read_depth == 0)
  17463. + rt_mutex_unlock(&rwsem->lock);
  17464. +}
  17465. +
  17466. +void rt_up_read(struct rw_semaphore *rwsem)
  17467. +{
  17468. + rwsem_release(&rwsem->dep_map, 1, _RET_IP_);
  17469. + __rt_up_read(rwsem);
  17470. +}
  17471. +EXPORT_SYMBOL(rt_up_read);
  17472. +
  17473. +/*
  17474. + * downgrade a write lock into a read lock
  17475. + * - just wake up any readers at the front of the queue
  17476. + */
  17477. +void rt_downgrade_write(struct rw_semaphore *rwsem)
  17478. +{
  17479. + BUG_ON(rt_mutex_owner(&rwsem->lock) != current);
  17480. + rwsem->read_depth = 1;
  17481. +}
  17482. +EXPORT_SYMBOL(rt_downgrade_write);
  17483. +
  17484. +int rt_down_write_trylock(struct rw_semaphore *rwsem)
  17485. +{
  17486. + int ret = rt_mutex_trylock(&rwsem->lock);
  17487. +
  17488. + if (ret)
  17489. + rwsem_acquire(&rwsem->dep_map, 0, 1, _RET_IP_);
  17490. + return ret;
  17491. +}
  17492. +EXPORT_SYMBOL(rt_down_write_trylock);
  17493. +
  17494. +void rt_down_write(struct rw_semaphore *rwsem)
  17495. +{
  17496. + rwsem_acquire(&rwsem->dep_map, 0, 0, _RET_IP_);
  17497. + rt_mutex_lock(&rwsem->lock);
  17498. +}
  17499. +EXPORT_SYMBOL(rt_down_write);
  17500. +
  17501. +void rt_down_write_nested(struct rw_semaphore *rwsem, int subclass)
  17502. +{
  17503. + rwsem_acquire(&rwsem->dep_map, subclass, 0, _RET_IP_);
  17504. + rt_mutex_lock(&rwsem->lock);
  17505. +}
  17506. +EXPORT_SYMBOL(rt_down_write_nested);
  17507. +
  17508. +void rt_down_write_nested_lock(struct rw_semaphore *rwsem,
  17509. + struct lockdep_map *nest)
  17510. +{
  17511. + rwsem_acquire_nest(&rwsem->dep_map, 0, 0, nest, _RET_IP_);
  17512. + rt_mutex_lock(&rwsem->lock);
  17513. +}
  17514. +EXPORT_SYMBOL(rt_down_write_nested_lock);
  17515. +
  17516. +int rt__down_read_trylock(struct rw_semaphore *rwsem)
  17517. +{
  17518. + struct rt_mutex *lock = &rwsem->lock;
  17519. + int ret = 1;
  17520. +
  17521. + /*
  17522. + * recursive read locks succeed when current owns the rwsem,
  17523. + * but not when read_depth == 0 which means that the rwsem is
  17524. + * write locked.
  17525. + */
  17526. + if (rt_mutex_owner(lock) != current)
  17527. + ret = rt_mutex_trylock(&rwsem->lock);
  17528. + else if (!rwsem->read_depth)
  17529. + ret = 0;
  17530. +
  17531. + if (ret)
  17532. + rwsem->read_depth++;
  17533. + return ret;
  17534. +
  17535. +}
  17536. +
  17537. +int rt_down_read_trylock(struct rw_semaphore *rwsem)
  17538. +{
  17539. + int ret;
  17540. +
  17541. + ret = rt__down_read_trylock(rwsem);
  17542. + if (ret)
  17543. + rwsem_acquire(&rwsem->dep_map, 0, 1, _RET_IP_);
  17544. +
  17545. + return ret;
  17546. +}
  17547. +EXPORT_SYMBOL(rt_down_read_trylock);
  17548. +
  17549. +void rt__down_read(struct rw_semaphore *rwsem)
  17550. +{
  17551. + struct rt_mutex *lock = &rwsem->lock;
  17552. +
  17553. + if (rt_mutex_owner(lock) != current)
  17554. + rt_mutex_lock(&rwsem->lock);
  17555. + rwsem->read_depth++;
  17556. +}
  17557. +EXPORT_SYMBOL(rt__down_read);
  17558. +
  17559. +static void __rt_down_read(struct rw_semaphore *rwsem, int subclass)
  17560. +{
  17561. + rwsem_acquire_read(&rwsem->dep_map, subclass, 0, _RET_IP_);
  17562. + rt__down_read(rwsem);
  17563. +}
  17564. +
  17565. +void rt_down_read(struct rw_semaphore *rwsem)
  17566. +{
  17567. + __rt_down_read(rwsem, 0);
  17568. +}
  17569. +EXPORT_SYMBOL(rt_down_read);
  17570. +
  17571. +void rt_down_read_nested(struct rw_semaphore *rwsem, int subclass)
  17572. +{
  17573. + __rt_down_read(rwsem, subclass);
  17574. +}
  17575. +EXPORT_SYMBOL(rt_down_read_nested);
  17576. +
  17577. +void __rt_rwsem_init(struct rw_semaphore *rwsem, const char *name,
  17578. + struct lock_class_key *key)
  17579. +{
  17580. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  17581. + /*
  17582. + * Make sure we are not reinitializing a held lock:
  17583. + */
  17584. + debug_check_no_locks_freed((void *)rwsem, sizeof(*rwsem));
  17585. + lockdep_init_map(&rwsem->dep_map, name, key, 0);
  17586. +#endif
  17587. + rwsem->read_depth = 0;
  17588. + rwsem->lock.save_state = 0;
  17589. +}
  17590. +EXPORT_SYMBOL(__rt_rwsem_init);
  17591. +
  17592. +/**
  17593. + * atomic_dec_and_mutex_lock - return holding mutex if we dec to 0
  17594. + * @cnt: the atomic which we are to dec
  17595. + * @lock: the mutex to return holding if we dec to 0
  17596. + *
  17597. + * return true and hold lock if we dec to 0, return false otherwise
  17598. + */
  17599. +int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock)
  17600. +{
  17601. + /* dec if we can't possibly hit 0 */
  17602. + if (atomic_add_unless(cnt, -1, 1))
  17603. + return 0;
  17604. + /* we might hit 0, so take the lock */
  17605. + mutex_lock(lock);
  17606. + if (!atomic_dec_and_test(cnt)) {
  17607. + /* when we actually did the dec, we didn't hit 0 */
  17608. + mutex_unlock(lock);
  17609. + return 0;
  17610. + }
  17611. + /* we hit 0, and we hold the lock */
  17612. + return 1;
  17613. +}
  17614. +EXPORT_SYMBOL(atomic_dec_and_mutex_lock);
  17615. diff --git a/kernel/locking/rtmutex.c b/kernel/locking/rtmutex.c
  17616. index b066724d7a5b..0e9a6260441d 100644
  17617. --- a/kernel/locking/rtmutex.c
  17618. +++ b/kernel/locking/rtmutex.c
  17619. @@ -7,6 +7,11 @@
  17620. * Copyright (C) 2005-2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  17621. * Copyright (C) 2005 Kihon Technologies Inc., Steven Rostedt
  17622. * Copyright (C) 2006 Esben Nielsen
  17623. + * Adaptive Spinlocks:
  17624. + * Copyright (C) 2008 Novell, Inc., Gregory Haskins, Sven Dietrich,
  17625. + * and Peter Morreale,
  17626. + * Adaptive Spinlocks simplification:
  17627. + * Copyright (C) 2008 Red Hat, Inc., Steven Rostedt <srostedt@redhat.com>
  17628. *
  17629. * See Documentation/locking/rt-mutex-design.txt for details.
  17630. */
  17631. @@ -16,6 +21,7 @@
  17632. #include <linux/sched/rt.h>
  17633. #include <linux/sched/deadline.h>
  17634. #include <linux/timer.h>
  17635. +#include <linux/ww_mutex.h>
  17636. #include "rtmutex_common.h"
  17637. @@ -133,6 +139,12 @@ static void fixup_rt_mutex_waiters(struct rt_mutex *lock)
  17638. WRITE_ONCE(*p, owner & ~RT_MUTEX_HAS_WAITERS);
  17639. }
  17640. +static int rt_mutex_real_waiter(struct rt_mutex_waiter *waiter)
  17641. +{
  17642. + return waiter && waiter != PI_WAKEUP_INPROGRESS &&
  17643. + waiter != PI_REQUEUE_INPROGRESS;
  17644. +}
  17645. +
  17646. /*
  17647. * We can speed up the acquire/release, if there's no debugging state to be
  17648. * set up.
  17649. @@ -163,13 +175,14 @@ static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
  17650. * 2) Drop lock->wait_lock
  17651. * 3) Try to unlock the lock with cmpxchg
  17652. */
  17653. -static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
  17654. +static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
  17655. + unsigned long flags)
  17656. __releases(lock->wait_lock)
  17657. {
  17658. struct task_struct *owner = rt_mutex_owner(lock);
  17659. clear_rt_mutex_waiters(lock);
  17660. - raw_spin_unlock(&lock->wait_lock);
  17661. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  17662. /*
  17663. * If a new waiter comes in between the unlock and the cmpxchg
  17664. * we have two situations:
  17665. @@ -211,11 +224,12 @@ static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
  17666. /*
  17667. * Simple slow path only version: lock->owner is protected by lock->wait_lock.
  17668. */
  17669. -static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
  17670. +static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
  17671. + unsigned long flags)
  17672. __releases(lock->wait_lock)
  17673. {
  17674. lock->owner = NULL;
  17675. - raw_spin_unlock(&lock->wait_lock);
  17676. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  17677. return true;
  17678. }
  17679. #endif
  17680. @@ -412,6 +426,14 @@ static bool rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
  17681. return debug_rt_mutex_detect_deadlock(waiter, chwalk);
  17682. }
  17683. +static void rt_mutex_wake_waiter(struct rt_mutex_waiter *waiter)
  17684. +{
  17685. + if (waiter->savestate)
  17686. + wake_up_lock_sleeper(waiter->task);
  17687. + else
  17688. + wake_up_process(waiter->task);
  17689. +}
  17690. +
  17691. /*
  17692. * Max number of times we'll walk the boosting chain:
  17693. */
  17694. @@ -419,7 +441,8 @@ int max_lock_depth = 1024;
  17695. static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
  17696. {
  17697. - return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
  17698. + return rt_mutex_real_waiter(p->pi_blocked_on) ?
  17699. + p->pi_blocked_on->lock : NULL;
  17700. }
  17701. /*
  17702. @@ -497,7 +520,6 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  17703. int ret = 0, depth = 0;
  17704. struct rt_mutex *lock;
  17705. bool detect_deadlock;
  17706. - unsigned long flags;
  17707. bool requeue = true;
  17708. detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
  17709. @@ -540,7 +562,7 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  17710. /*
  17711. * [1] Task cannot go away as we did a get_task() before !
  17712. */
  17713. - raw_spin_lock_irqsave(&task->pi_lock, flags);
  17714. + raw_spin_lock_irq(&task->pi_lock);
  17715. /*
  17716. * [2] Get the waiter on which @task is blocked on.
  17717. @@ -556,7 +578,7 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  17718. * reached or the state of the chain has changed while we
  17719. * dropped the locks.
  17720. */
  17721. - if (!waiter)
  17722. + if (!rt_mutex_real_waiter(waiter))
  17723. goto out_unlock_pi;
  17724. /*
  17725. @@ -624,7 +646,7 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  17726. * operations.
  17727. */
  17728. if (!raw_spin_trylock(&lock->wait_lock)) {
  17729. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  17730. + raw_spin_unlock_irq(&task->pi_lock);
  17731. cpu_relax();
  17732. goto retry;
  17733. }
  17734. @@ -655,7 +677,7 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  17735. /*
  17736. * No requeue[7] here. Just release @task [8]
  17737. */
  17738. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  17739. + raw_spin_unlock(&task->pi_lock);
  17740. put_task_struct(task);
  17741. /*
  17742. @@ -663,14 +685,14 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  17743. * If there is no owner of the lock, end of chain.
  17744. */
  17745. if (!rt_mutex_owner(lock)) {
  17746. - raw_spin_unlock(&lock->wait_lock);
  17747. + raw_spin_unlock_irq(&lock->wait_lock);
  17748. return 0;
  17749. }
  17750. /* [10] Grab the next task, i.e. owner of @lock */
  17751. task = rt_mutex_owner(lock);
  17752. get_task_struct(task);
  17753. - raw_spin_lock_irqsave(&task->pi_lock, flags);
  17754. + raw_spin_lock(&task->pi_lock);
  17755. /*
  17756. * No requeue [11] here. We just do deadlock detection.
  17757. @@ -685,8 +707,8 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  17758. top_waiter = rt_mutex_top_waiter(lock);
  17759. /* [13] Drop locks */
  17760. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  17761. - raw_spin_unlock(&lock->wait_lock);
  17762. + raw_spin_unlock(&task->pi_lock);
  17763. + raw_spin_unlock_irq(&lock->wait_lock);
  17764. /* If owner is not blocked, end of chain. */
  17765. if (!next_lock)
  17766. @@ -707,7 +729,7 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  17767. rt_mutex_enqueue(lock, waiter);
  17768. /* [8] Release the task */
  17769. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  17770. + raw_spin_unlock(&task->pi_lock);
  17771. put_task_struct(task);
  17772. /*
  17773. @@ -718,21 +740,24 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  17774. * follow here. This is the end of the chain we are walking.
  17775. */
  17776. if (!rt_mutex_owner(lock)) {
  17777. + struct rt_mutex_waiter *lock_top_waiter;
  17778. +
  17779. /*
  17780. * If the requeue [7] above changed the top waiter,
  17781. * then we need to wake the new top waiter up to try
  17782. * to get the lock.
  17783. */
  17784. - if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
  17785. - wake_up_process(rt_mutex_top_waiter(lock)->task);
  17786. - raw_spin_unlock(&lock->wait_lock);
  17787. + lock_top_waiter = rt_mutex_top_waiter(lock);
  17788. + if (prerequeue_top_waiter != lock_top_waiter)
  17789. + rt_mutex_wake_waiter(lock_top_waiter);
  17790. + raw_spin_unlock_irq(&lock->wait_lock);
  17791. return 0;
  17792. }
  17793. /* [10] Grab the next task, i.e. the owner of @lock */
  17794. task = rt_mutex_owner(lock);
  17795. get_task_struct(task);
  17796. - raw_spin_lock_irqsave(&task->pi_lock, flags);
  17797. + raw_spin_lock(&task->pi_lock);
  17798. /* [11] requeue the pi waiters if necessary */
  17799. if (waiter == rt_mutex_top_waiter(lock)) {
  17800. @@ -786,8 +811,8 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  17801. top_waiter = rt_mutex_top_waiter(lock);
  17802. /* [13] Drop the locks */
  17803. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  17804. - raw_spin_unlock(&lock->wait_lock);
  17805. + raw_spin_unlock(&task->pi_lock);
  17806. + raw_spin_unlock_irq(&lock->wait_lock);
  17807. /*
  17808. * Make the actual exit decisions [12], based on the stored
  17809. @@ -810,28 +835,46 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  17810. goto again;
  17811. out_unlock_pi:
  17812. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  17813. + raw_spin_unlock_irq(&task->pi_lock);
  17814. out_put_task:
  17815. put_task_struct(task);
  17816. return ret;
  17817. }
  17818. +
  17819. +#define STEAL_NORMAL 0
  17820. +#define STEAL_LATERAL 1
  17821. +
  17822. +/*
  17823. + * Note that RT tasks are excluded from lateral-steals to prevent the
  17824. + * introduction of an unbounded latency
  17825. + */
  17826. +static inline int lock_is_stealable(struct task_struct *task,
  17827. + struct task_struct *pendowner, int mode)
  17828. +{
  17829. + if (mode == STEAL_NORMAL || rt_task(task)) {
  17830. + if (task->prio >= pendowner->prio)
  17831. + return 0;
  17832. + } else if (task->prio > pendowner->prio)
  17833. + return 0;
  17834. + return 1;
  17835. +}
  17836. +
  17837. /*
  17838. * Try to take an rt-mutex
  17839. *
  17840. - * Must be called with lock->wait_lock held.
  17841. + * Must be called with lock->wait_lock held and interrupts disabled
  17842. *
  17843. * @lock: The lock to be acquired.
  17844. * @task: The task which wants to acquire the lock
  17845. * @waiter: The waiter that is queued to the lock's wait tree if the
  17846. * callsite called task_blocked_on_lock(), otherwise NULL
  17847. */
  17848. -static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  17849. - struct rt_mutex_waiter *waiter)
  17850. +static int __try_to_take_rt_mutex(struct rt_mutex *lock,
  17851. + struct task_struct *task,
  17852. + struct rt_mutex_waiter *waiter, int mode)
  17853. {
  17854. - unsigned long flags;
  17855. -
  17856. /*
  17857. * Before testing whether we can acquire @lock, we set the
  17858. * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
  17859. @@ -867,8 +910,10 @@ static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  17860. * If waiter is not the highest priority waiter of
  17861. * @lock, give up.
  17862. */
  17863. - if (waiter != rt_mutex_top_waiter(lock))
  17864. + if (waiter != rt_mutex_top_waiter(lock)) {
  17865. + /* XXX lock_is_stealable() ? */
  17866. return 0;
  17867. + }
  17868. /*
  17869. * We can acquire the lock. Remove the waiter from the
  17870. @@ -886,14 +931,10 @@ static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  17871. * not need to be dequeued.
  17872. */
  17873. if (rt_mutex_has_waiters(lock)) {
  17874. - /*
  17875. - * If @task->prio is greater than or equal to
  17876. - * the top waiter priority (kernel view),
  17877. - * @task lost.
  17878. - */
  17879. - if (task->prio >= rt_mutex_top_waiter(lock)->prio)
  17880. - return 0;
  17881. + struct task_struct *pown = rt_mutex_top_waiter(lock)->task;
  17882. + if (task != pown && !lock_is_stealable(task, pown, mode))
  17883. + return 0;
  17884. /*
  17885. * The current top waiter stays enqueued. We
  17886. * don't have to change anything in the lock
  17887. @@ -916,7 +957,7 @@ static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  17888. * case, but conditionals are more expensive than a redundant
  17889. * store.
  17890. */
  17891. - raw_spin_lock_irqsave(&task->pi_lock, flags);
  17892. + raw_spin_lock(&task->pi_lock);
  17893. task->pi_blocked_on = NULL;
  17894. /*
  17895. * Finish the lock acquisition. @task is the new owner. If
  17896. @@ -925,7 +966,7 @@ static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  17897. */
  17898. if (rt_mutex_has_waiters(lock))
  17899. rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
  17900. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  17901. + raw_spin_unlock(&task->pi_lock);
  17902. takeit:
  17903. /* We got the lock. */
  17904. @@ -942,12 +983,444 @@ takeit:
  17905. return 1;
  17906. }
  17907. +#ifdef CONFIG_PREEMPT_RT_FULL
  17908. +/*
  17909. + * preemptible spin_lock functions:
  17910. + */
  17911. +static inline void rt_spin_lock_fastlock(struct rt_mutex *lock,
  17912. + void (*slowfn)(struct rt_mutex *lock,
  17913. + bool mg_off),
  17914. + bool do_mig_dis)
  17915. +{
  17916. + might_sleep_no_state_check();
  17917. +
  17918. + if (do_mig_dis)
  17919. + migrate_disable();
  17920. +
  17921. + if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
  17922. + rt_mutex_deadlock_account_lock(lock, current);
  17923. + else
  17924. + slowfn(lock, do_mig_dis);
  17925. +}
  17926. +
  17927. +static inline int rt_spin_lock_fastunlock(struct rt_mutex *lock,
  17928. + int (*slowfn)(struct rt_mutex *lock))
  17929. +{
  17930. + if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
  17931. + rt_mutex_deadlock_account_unlock(current);
  17932. + return 0;
  17933. + }
  17934. + return slowfn(lock);
  17935. +}
  17936. +#ifdef CONFIG_SMP
  17937. +/*
  17938. + * Note that owner is a speculative pointer and dereferencing relies
  17939. + * on rcu_read_lock() and the check against the lock owner.
  17940. + */
  17941. +static int adaptive_wait(struct rt_mutex *lock,
  17942. + struct task_struct *owner)
  17943. +{
  17944. + int res = 0;
  17945. +
  17946. + rcu_read_lock();
  17947. + for (;;) {
  17948. + if (owner != rt_mutex_owner(lock))
  17949. + break;
  17950. + /*
  17951. + * Ensure that owner->on_cpu is dereferenced _after_
  17952. + * checking the above to be valid.
  17953. + */
  17954. + barrier();
  17955. + if (!owner->on_cpu) {
  17956. + res = 1;
  17957. + break;
  17958. + }
  17959. + cpu_relax();
  17960. + }
  17961. + rcu_read_unlock();
  17962. + return res;
  17963. +}
  17964. +#else
  17965. +static int adaptive_wait(struct rt_mutex *lock,
  17966. + struct task_struct *orig_owner)
  17967. +{
  17968. + return 1;
  17969. +}
  17970. +#endif
  17971. +
  17972. +static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  17973. + struct rt_mutex_waiter *waiter,
  17974. + struct task_struct *task,
  17975. + enum rtmutex_chainwalk chwalk);
  17976. +/*
  17977. + * Slow path lock function spin_lock style: this variant is very
  17978. + * careful not to miss any non-lock wakeups.
  17979. + *
  17980. + * We store the current state under p->pi_lock in p->saved_state and
  17981. + * the try_to_wake_up() code handles this accordingly.
  17982. + */
  17983. +static void noinline __sched rt_spin_lock_slowlock(struct rt_mutex *lock,
  17984. + bool mg_off)
  17985. +{
  17986. + struct task_struct *lock_owner, *self = current;
  17987. + struct rt_mutex_waiter waiter, *top_waiter;
  17988. + unsigned long flags;
  17989. + int ret;
  17990. +
  17991. + rt_mutex_init_waiter(&waiter, true);
  17992. +
  17993. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  17994. +
  17995. + if (__try_to_take_rt_mutex(lock, self, NULL, STEAL_LATERAL)) {
  17996. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  17997. + return;
  17998. + }
  17999. +
  18000. + BUG_ON(rt_mutex_owner(lock) == self);
  18001. +
  18002. + /*
  18003. + * We save whatever state the task is in and we'll restore it
  18004. + * after acquiring the lock taking real wakeups into account
  18005. + * as well. We are serialized via pi_lock against wakeups. See
  18006. + * try_to_wake_up().
  18007. + */
  18008. + raw_spin_lock(&self->pi_lock);
  18009. + self->saved_state = self->state;
  18010. + __set_current_state_no_track(TASK_UNINTERRUPTIBLE);
  18011. + raw_spin_unlock(&self->pi_lock);
  18012. +
  18013. + ret = task_blocks_on_rt_mutex(lock, &waiter, self, RT_MUTEX_MIN_CHAINWALK);
  18014. + BUG_ON(ret);
  18015. +
  18016. + for (;;) {
  18017. + /* Try to acquire the lock again. */
  18018. + if (__try_to_take_rt_mutex(lock, self, &waiter, STEAL_LATERAL))
  18019. + break;
  18020. +
  18021. + top_waiter = rt_mutex_top_waiter(lock);
  18022. + lock_owner = rt_mutex_owner(lock);
  18023. +
  18024. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  18025. +
  18026. + debug_rt_mutex_print_deadlock(&waiter);
  18027. +
  18028. + if (top_waiter != &waiter || adaptive_wait(lock, lock_owner)) {
  18029. + if (mg_off)
  18030. + migrate_enable();
  18031. + schedule();
  18032. + if (mg_off)
  18033. + migrate_disable();
  18034. + }
  18035. +
  18036. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  18037. +
  18038. + raw_spin_lock(&self->pi_lock);
  18039. + __set_current_state_no_track(TASK_UNINTERRUPTIBLE);
  18040. + raw_spin_unlock(&self->pi_lock);
  18041. + }
  18042. +
  18043. + /*
  18044. + * Restore the task state to current->saved_state. We set it
  18045. + * to the original state above and the try_to_wake_up() code
  18046. + * has possibly updated it when a real (non-rtmutex) wakeup
  18047. + * happened while we were blocked. Clear saved_state so
  18048. + * try_to_wakeup() does not get confused.
  18049. + */
  18050. + raw_spin_lock(&self->pi_lock);
  18051. + __set_current_state_no_track(self->saved_state);
  18052. + self->saved_state = TASK_RUNNING;
  18053. + raw_spin_unlock(&self->pi_lock);
  18054. +
  18055. + /*
  18056. + * try_to_take_rt_mutex() sets the waiter bit
  18057. + * unconditionally. We might have to fix that up:
  18058. + */
  18059. + fixup_rt_mutex_waiters(lock);
  18060. +
  18061. + BUG_ON(rt_mutex_has_waiters(lock) && &waiter == rt_mutex_top_waiter(lock));
  18062. + BUG_ON(!RB_EMPTY_NODE(&waiter.tree_entry));
  18063. +
  18064. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  18065. +
  18066. + debug_rt_mutex_free_waiter(&waiter);
  18067. +}
  18068. +
  18069. +static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
  18070. + struct wake_q_head *wake_sleeper_q,
  18071. + struct rt_mutex *lock);
  18072. +/*
  18073. + * Slow path to release a rt_mutex spin_lock style
  18074. + */
  18075. +static int noinline __sched rt_spin_lock_slowunlock(struct rt_mutex *lock)
  18076. +{
  18077. + unsigned long flags;
  18078. + WAKE_Q(wake_q);
  18079. + WAKE_Q(wake_sleeper_q);
  18080. +
  18081. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  18082. +
  18083. + debug_rt_mutex_unlock(lock);
  18084. +
  18085. + rt_mutex_deadlock_account_unlock(current);
  18086. +
  18087. + if (!rt_mutex_has_waiters(lock)) {
  18088. + lock->owner = NULL;
  18089. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  18090. + return 0;
  18091. + }
  18092. +
  18093. + mark_wakeup_next_waiter(&wake_q, &wake_sleeper_q, lock);
  18094. +
  18095. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  18096. + wake_up_q(&wake_q);
  18097. + wake_up_q_sleeper(&wake_sleeper_q);
  18098. +
  18099. + /* Undo pi boosting.when necessary */
  18100. + rt_mutex_adjust_prio(current);
  18101. + return 0;
  18102. +}
  18103. +
  18104. +static int noinline __sched rt_spin_lock_slowunlock_no_deboost(struct rt_mutex *lock)
  18105. +{
  18106. + unsigned long flags;
  18107. + WAKE_Q(wake_q);
  18108. + WAKE_Q(wake_sleeper_q);
  18109. +
  18110. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  18111. +
  18112. + debug_rt_mutex_unlock(lock);
  18113. +
  18114. + rt_mutex_deadlock_account_unlock(current);
  18115. +
  18116. + if (!rt_mutex_has_waiters(lock)) {
  18117. + lock->owner = NULL;
  18118. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  18119. + return 0;
  18120. + }
  18121. +
  18122. + mark_wakeup_next_waiter(&wake_q, &wake_sleeper_q, lock);
  18123. +
  18124. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  18125. + wake_up_q(&wake_q);
  18126. + wake_up_q_sleeper(&wake_sleeper_q);
  18127. + return 1;
  18128. +}
  18129. +
  18130. +void __lockfunc rt_spin_lock__no_mg(spinlock_t *lock)
  18131. +{
  18132. + rt_spin_lock_fastlock(&lock->lock, rt_spin_lock_slowlock, false);
  18133. + spin_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  18134. +}
  18135. +EXPORT_SYMBOL(rt_spin_lock__no_mg);
  18136. +
  18137. +void __lockfunc rt_spin_lock(spinlock_t *lock)
  18138. +{
  18139. + rt_spin_lock_fastlock(&lock->lock, rt_spin_lock_slowlock, true);
  18140. + spin_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  18141. +}
  18142. +EXPORT_SYMBOL(rt_spin_lock);
  18143. +
  18144. +void __lockfunc __rt_spin_lock(struct rt_mutex *lock)
  18145. +{
  18146. + rt_spin_lock_fastlock(lock, rt_spin_lock_slowlock, true);
  18147. +}
  18148. +EXPORT_SYMBOL(__rt_spin_lock);
  18149. +
  18150. +void __lockfunc __rt_spin_lock__no_mg(struct rt_mutex *lock)
  18151. +{
  18152. + rt_spin_lock_fastlock(lock, rt_spin_lock_slowlock, false);
  18153. +}
  18154. +EXPORT_SYMBOL(__rt_spin_lock__no_mg);
  18155. +
  18156. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  18157. +void __lockfunc rt_spin_lock_nested(spinlock_t *lock, int subclass)
  18158. +{
  18159. + spin_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
  18160. + rt_spin_lock_fastlock(&lock->lock, rt_spin_lock_slowlock, true);
  18161. +}
  18162. +EXPORT_SYMBOL(rt_spin_lock_nested);
  18163. +#endif
  18164. +
  18165. +void __lockfunc rt_spin_unlock__no_mg(spinlock_t *lock)
  18166. +{
  18167. + /* NOTE: we always pass in '1' for nested, for simplicity */
  18168. + spin_release(&lock->dep_map, 1, _RET_IP_);
  18169. + rt_spin_lock_fastunlock(&lock->lock, rt_spin_lock_slowunlock);
  18170. +}
  18171. +EXPORT_SYMBOL(rt_spin_unlock__no_mg);
  18172. +
  18173. +void __lockfunc rt_spin_unlock(spinlock_t *lock)
  18174. +{
  18175. + /* NOTE: we always pass in '1' for nested, for simplicity */
  18176. + spin_release(&lock->dep_map, 1, _RET_IP_);
  18177. + rt_spin_lock_fastunlock(&lock->lock, rt_spin_lock_slowunlock);
  18178. + migrate_enable();
  18179. +}
  18180. +EXPORT_SYMBOL(rt_spin_unlock);
  18181. +
  18182. +int __lockfunc rt_spin_unlock_no_deboost(spinlock_t *lock)
  18183. +{
  18184. + int ret;
  18185. +
  18186. + /* NOTE: we always pass in '1' for nested, for simplicity */
  18187. + spin_release(&lock->dep_map, 1, _RET_IP_);
  18188. + ret = rt_spin_lock_fastunlock(&lock->lock, rt_spin_lock_slowunlock_no_deboost);
  18189. + migrate_enable();
  18190. + return ret;
  18191. +}
  18192. +
  18193. +void __lockfunc __rt_spin_unlock(struct rt_mutex *lock)
  18194. +{
  18195. + rt_spin_lock_fastunlock(lock, rt_spin_lock_slowunlock);
  18196. +}
  18197. +EXPORT_SYMBOL(__rt_spin_unlock);
  18198. +
  18199. +/*
  18200. + * Wait for the lock to get unlocked: instead of polling for an unlock
  18201. + * (like raw spinlocks do), we lock and unlock, to force the kernel to
  18202. + * schedule if there's contention:
  18203. + */
  18204. +void __lockfunc rt_spin_unlock_wait(spinlock_t *lock)
  18205. +{
  18206. + spin_lock(lock);
  18207. + spin_unlock(lock);
  18208. +}
  18209. +EXPORT_SYMBOL(rt_spin_unlock_wait);
  18210. +
  18211. +int __lockfunc __rt_spin_trylock(struct rt_mutex *lock)
  18212. +{
  18213. + return rt_mutex_trylock(lock);
  18214. +}
  18215. +
  18216. +int __lockfunc rt_spin_trylock__no_mg(spinlock_t *lock)
  18217. +{
  18218. + int ret;
  18219. +
  18220. + ret = rt_mutex_trylock(&lock->lock);
  18221. + if (ret)
  18222. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  18223. + return ret;
  18224. +}
  18225. +EXPORT_SYMBOL(rt_spin_trylock__no_mg);
  18226. +
  18227. +int __lockfunc rt_spin_trylock(spinlock_t *lock)
  18228. +{
  18229. + int ret;
  18230. +
  18231. + migrate_disable();
  18232. + ret = rt_mutex_trylock(&lock->lock);
  18233. + if (ret)
  18234. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  18235. + else
  18236. + migrate_enable();
  18237. + return ret;
  18238. +}
  18239. +EXPORT_SYMBOL(rt_spin_trylock);
  18240. +
  18241. +int __lockfunc rt_spin_trylock_bh(spinlock_t *lock)
  18242. +{
  18243. + int ret;
  18244. +
  18245. + local_bh_disable();
  18246. + ret = rt_mutex_trylock(&lock->lock);
  18247. + if (ret) {
  18248. + migrate_disable();
  18249. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  18250. + } else
  18251. + local_bh_enable();
  18252. + return ret;
  18253. +}
  18254. +EXPORT_SYMBOL(rt_spin_trylock_bh);
  18255. +
  18256. +int __lockfunc rt_spin_trylock_irqsave(spinlock_t *lock, unsigned long *flags)
  18257. +{
  18258. + int ret;
  18259. +
  18260. + *flags = 0;
  18261. + ret = rt_mutex_trylock(&lock->lock);
  18262. + if (ret) {
  18263. + migrate_disable();
  18264. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  18265. + }
  18266. + return ret;
  18267. +}
  18268. +EXPORT_SYMBOL(rt_spin_trylock_irqsave);
  18269. +
  18270. +int atomic_dec_and_spin_lock(atomic_t *atomic, spinlock_t *lock)
  18271. +{
  18272. + /* Subtract 1 from counter unless that drops it to 0 (ie. it was 1) */
  18273. + if (atomic_add_unless(atomic, -1, 1))
  18274. + return 0;
  18275. + rt_spin_lock(lock);
  18276. + if (atomic_dec_and_test(atomic))
  18277. + return 1;
  18278. + rt_spin_unlock(lock);
  18279. + return 0;
  18280. +}
  18281. +EXPORT_SYMBOL(atomic_dec_and_spin_lock);
  18282. +
  18283. + void
  18284. +__rt_spin_lock_init(spinlock_t *lock, char *name, struct lock_class_key *key)
  18285. +{
  18286. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  18287. + /*
  18288. + * Make sure we are not reinitializing a held lock:
  18289. + */
  18290. + debug_check_no_locks_freed((void *)lock, sizeof(*lock));
  18291. + lockdep_init_map(&lock->dep_map, name, key, 0);
  18292. +#endif
  18293. +}
  18294. +EXPORT_SYMBOL(__rt_spin_lock_init);
  18295. +
  18296. +#endif /* PREEMPT_RT_FULL */
  18297. +
  18298. +#ifdef CONFIG_PREEMPT_RT_FULL
  18299. + static inline int __sched
  18300. +__mutex_lock_check_stamp(struct rt_mutex *lock, struct ww_acquire_ctx *ctx)
  18301. +{
  18302. + struct ww_mutex *ww = container_of(lock, struct ww_mutex, base.lock);
  18303. + struct ww_acquire_ctx *hold_ctx = ACCESS_ONCE(ww->ctx);
  18304. +
  18305. + if (!hold_ctx)
  18306. + return 0;
  18307. +
  18308. + if (unlikely(ctx == hold_ctx))
  18309. + return -EALREADY;
  18310. +
  18311. + if (ctx->stamp - hold_ctx->stamp <= LONG_MAX &&
  18312. + (ctx->stamp != hold_ctx->stamp || ctx > hold_ctx)) {
  18313. +#ifdef CONFIG_DEBUG_MUTEXES
  18314. + DEBUG_LOCKS_WARN_ON(ctx->contending_lock);
  18315. + ctx->contending_lock = ww;
  18316. +#endif
  18317. + return -EDEADLK;
  18318. + }
  18319. +
  18320. + return 0;
  18321. +}
  18322. +#else
  18323. + static inline int __sched
  18324. +__mutex_lock_check_stamp(struct rt_mutex *lock, struct ww_acquire_ctx *ctx)
  18325. +{
  18326. + BUG();
  18327. + return 0;
  18328. +}
  18329. +
  18330. +#endif
  18331. +
  18332. +static inline int
  18333. +try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  18334. + struct rt_mutex_waiter *waiter)
  18335. +{
  18336. + return __try_to_take_rt_mutex(lock, task, waiter, STEAL_NORMAL);
  18337. +}
  18338. +
  18339. /*
  18340. * Task blocks on lock.
  18341. *
  18342. * Prepare waiter and propagate pi chain
  18343. *
  18344. - * This must be called with lock->wait_lock held.
  18345. + * This must be called with lock->wait_lock held and interrupts disabled
  18346. */
  18347. static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  18348. struct rt_mutex_waiter *waiter,
  18349. @@ -958,7 +1431,6 @@ static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  18350. struct rt_mutex_waiter *top_waiter = waiter;
  18351. struct rt_mutex *next_lock;
  18352. int chain_walk = 0, res;
  18353. - unsigned long flags;
  18354. /*
  18355. * Early deadlock detection. We really don't want the task to
  18356. @@ -972,7 +1444,24 @@ static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  18357. if (owner == task)
  18358. return -EDEADLK;
  18359. - raw_spin_lock_irqsave(&task->pi_lock, flags);
  18360. + raw_spin_lock(&task->pi_lock);
  18361. +
  18362. + /*
  18363. + * In the case of futex requeue PI, this will be a proxy
  18364. + * lock. The task will wake unaware that it is enqueueed on
  18365. + * this lock. Avoid blocking on two locks and corrupting
  18366. + * pi_blocked_on via the PI_WAKEUP_INPROGRESS
  18367. + * flag. futex_wait_requeue_pi() sets this when it wakes up
  18368. + * before requeue (due to a signal or timeout). Do not enqueue
  18369. + * the task if PI_WAKEUP_INPROGRESS is set.
  18370. + */
  18371. + if (task != current && task->pi_blocked_on == PI_WAKEUP_INPROGRESS) {
  18372. + raw_spin_unlock(&task->pi_lock);
  18373. + return -EAGAIN;
  18374. + }
  18375. +
  18376. + BUG_ON(rt_mutex_real_waiter(task->pi_blocked_on));
  18377. +
  18378. __rt_mutex_adjust_prio(task);
  18379. waiter->task = task;
  18380. waiter->lock = lock;
  18381. @@ -985,18 +1474,18 @@ static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  18382. task->pi_blocked_on = waiter;
  18383. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  18384. + raw_spin_unlock(&task->pi_lock);
  18385. if (!owner)
  18386. return 0;
  18387. - raw_spin_lock_irqsave(&owner->pi_lock, flags);
  18388. + raw_spin_lock(&owner->pi_lock);
  18389. if (waiter == rt_mutex_top_waiter(lock)) {
  18390. rt_mutex_dequeue_pi(owner, top_waiter);
  18391. rt_mutex_enqueue_pi(owner, waiter);
  18392. __rt_mutex_adjust_prio(owner);
  18393. - if (owner->pi_blocked_on)
  18394. + if (rt_mutex_real_waiter(owner->pi_blocked_on))
  18395. chain_walk = 1;
  18396. } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
  18397. chain_walk = 1;
  18398. @@ -1005,7 +1494,7 @@ static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  18399. /* Store the lock on which owner is blocked or NULL */
  18400. next_lock = task_blocked_on_lock(owner);
  18401. - raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
  18402. + raw_spin_unlock(&owner->pi_lock);
  18403. /*
  18404. * Even if full deadlock detection is on, if the owner is not
  18405. * blocked itself, we can avoid finding this out in the chain
  18406. @@ -1021,12 +1510,12 @@ static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  18407. */
  18408. get_task_struct(owner);
  18409. - raw_spin_unlock(&lock->wait_lock);
  18410. + raw_spin_unlock_irq(&lock->wait_lock);
  18411. res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
  18412. next_lock, waiter, task);
  18413. - raw_spin_lock(&lock->wait_lock);
  18414. + raw_spin_lock_irq(&lock->wait_lock);
  18415. return res;
  18416. }
  18417. @@ -1035,15 +1524,15 @@ static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  18418. * Remove the top waiter from the current tasks pi waiter tree and
  18419. * queue it up.
  18420. *
  18421. - * Called with lock->wait_lock held.
  18422. + * Called with lock->wait_lock held and interrupts disabled.
  18423. */
  18424. static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
  18425. + struct wake_q_head *wake_sleeper_q,
  18426. struct rt_mutex *lock)
  18427. {
  18428. struct rt_mutex_waiter *waiter;
  18429. - unsigned long flags;
  18430. - raw_spin_lock_irqsave(&current->pi_lock, flags);
  18431. + raw_spin_lock(&current->pi_lock);
  18432. waiter = rt_mutex_top_waiter(lock);
  18433. @@ -1065,15 +1554,18 @@ static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
  18434. */
  18435. lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
  18436. - raw_spin_unlock_irqrestore(&current->pi_lock, flags);
  18437. + raw_spin_unlock(&current->pi_lock);
  18438. - wake_q_add(wake_q, waiter->task);
  18439. + if (waiter->savestate)
  18440. + wake_q_add(wake_sleeper_q, waiter->task);
  18441. + else
  18442. + wake_q_add(wake_q, waiter->task);
  18443. }
  18444. /*
  18445. * Remove a waiter from a lock and give up
  18446. *
  18447. - * Must be called with lock->wait_lock held and
  18448. + * Must be called with lock->wait_lock held and interrupts disabled. I must
  18449. * have just failed to try_to_take_rt_mutex().
  18450. */
  18451. static void remove_waiter(struct rt_mutex *lock,
  18452. @@ -1081,13 +1573,12 @@ static void remove_waiter(struct rt_mutex *lock,
  18453. {
  18454. bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
  18455. struct task_struct *owner = rt_mutex_owner(lock);
  18456. - struct rt_mutex *next_lock;
  18457. - unsigned long flags;
  18458. + struct rt_mutex *next_lock = NULL;
  18459. - raw_spin_lock_irqsave(&current->pi_lock, flags);
  18460. + raw_spin_lock(&current->pi_lock);
  18461. rt_mutex_dequeue(lock, waiter);
  18462. current->pi_blocked_on = NULL;
  18463. - raw_spin_unlock_irqrestore(&current->pi_lock, flags);
  18464. + raw_spin_unlock(&current->pi_lock);
  18465. /*
  18466. * Only update priority if the waiter was the highest priority
  18467. @@ -1096,7 +1587,7 @@ static void remove_waiter(struct rt_mutex *lock,
  18468. if (!owner || !is_top_waiter)
  18469. return;
  18470. - raw_spin_lock_irqsave(&owner->pi_lock, flags);
  18471. + raw_spin_lock(&owner->pi_lock);
  18472. rt_mutex_dequeue_pi(owner, waiter);
  18473. @@ -1106,9 +1597,10 @@ static void remove_waiter(struct rt_mutex *lock,
  18474. __rt_mutex_adjust_prio(owner);
  18475. /* Store the lock on which owner is blocked or NULL */
  18476. - next_lock = task_blocked_on_lock(owner);
  18477. + if (rt_mutex_real_waiter(owner->pi_blocked_on))
  18478. + next_lock = task_blocked_on_lock(owner);
  18479. - raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
  18480. + raw_spin_unlock(&owner->pi_lock);
  18481. /*
  18482. * Don't walk the chain, if the owner task is not blocked
  18483. @@ -1120,12 +1612,12 @@ static void remove_waiter(struct rt_mutex *lock,
  18484. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  18485. get_task_struct(owner);
  18486. - raw_spin_unlock(&lock->wait_lock);
  18487. + raw_spin_unlock_irq(&lock->wait_lock);
  18488. rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
  18489. next_lock, NULL, current);
  18490. - raw_spin_lock(&lock->wait_lock);
  18491. + raw_spin_lock_irq(&lock->wait_lock);
  18492. }
  18493. /*
  18494. @@ -1142,17 +1634,17 @@ void rt_mutex_adjust_pi(struct task_struct *task)
  18495. raw_spin_lock_irqsave(&task->pi_lock, flags);
  18496. waiter = task->pi_blocked_on;
  18497. - if (!waiter || (waiter->prio == task->prio &&
  18498. + if (!rt_mutex_real_waiter(waiter) || (waiter->prio == task->prio &&
  18499. !dl_prio(task->prio))) {
  18500. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  18501. return;
  18502. }
  18503. next_lock = waiter->lock;
  18504. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  18505. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  18506. get_task_struct(task);
  18507. + raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  18508. rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
  18509. next_lock, NULL, task);
  18510. }
  18511. @@ -1161,16 +1653,17 @@ void rt_mutex_adjust_pi(struct task_struct *task)
  18512. * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
  18513. * @lock: the rt_mutex to take
  18514. * @state: the state the task should block in (TASK_INTERRUPTIBLE
  18515. - * or TASK_UNINTERRUPTIBLE)
  18516. + * or TASK_UNINTERRUPTIBLE)
  18517. * @timeout: the pre-initialized and started timer, or NULL for none
  18518. * @waiter: the pre-initialized rt_mutex_waiter
  18519. *
  18520. - * lock->wait_lock must be held by the caller.
  18521. + * Must be called with lock->wait_lock held and interrupts disabled
  18522. */
  18523. static int __sched
  18524. __rt_mutex_slowlock(struct rt_mutex *lock, int state,
  18525. struct hrtimer_sleeper *timeout,
  18526. - struct rt_mutex_waiter *waiter)
  18527. + struct rt_mutex_waiter *waiter,
  18528. + struct ww_acquire_ctx *ww_ctx)
  18529. {
  18530. int ret = 0;
  18531. @@ -1193,13 +1686,19 @@ __rt_mutex_slowlock(struct rt_mutex *lock, int state,
  18532. break;
  18533. }
  18534. - raw_spin_unlock(&lock->wait_lock);
  18535. + if (ww_ctx && ww_ctx->acquired > 0) {
  18536. + ret = __mutex_lock_check_stamp(lock, ww_ctx);
  18537. + if (ret)
  18538. + break;
  18539. + }
  18540. +
  18541. + raw_spin_unlock_irq(&lock->wait_lock);
  18542. debug_rt_mutex_print_deadlock(waiter);
  18543. schedule();
  18544. - raw_spin_lock(&lock->wait_lock);
  18545. + raw_spin_lock_irq(&lock->wait_lock);
  18546. set_current_state(state);
  18547. }
  18548. @@ -1227,26 +1726,112 @@ static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
  18549. }
  18550. }
  18551. +static __always_inline void ww_mutex_lock_acquired(struct ww_mutex *ww,
  18552. + struct ww_acquire_ctx *ww_ctx)
  18553. +{
  18554. +#ifdef CONFIG_DEBUG_MUTEXES
  18555. + /*
  18556. + * If this WARN_ON triggers, you used ww_mutex_lock to acquire,
  18557. + * but released with a normal mutex_unlock in this call.
  18558. + *
  18559. + * This should never happen, always use ww_mutex_unlock.
  18560. + */
  18561. + DEBUG_LOCKS_WARN_ON(ww->ctx);
  18562. +
  18563. + /*
  18564. + * Not quite done after calling ww_acquire_done() ?
  18565. + */
  18566. + DEBUG_LOCKS_WARN_ON(ww_ctx->done_acquire);
  18567. +
  18568. + if (ww_ctx->contending_lock) {
  18569. + /*
  18570. + * After -EDEADLK you tried to
  18571. + * acquire a different ww_mutex? Bad!
  18572. + */
  18573. + DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock != ww);
  18574. +
  18575. + /*
  18576. + * You called ww_mutex_lock after receiving -EDEADLK,
  18577. + * but 'forgot' to unlock everything else first?
  18578. + */
  18579. + DEBUG_LOCKS_WARN_ON(ww_ctx->acquired > 0);
  18580. + ww_ctx->contending_lock = NULL;
  18581. + }
  18582. +
  18583. + /*
  18584. + * Naughty, using a different class will lead to undefined behavior!
  18585. + */
  18586. + DEBUG_LOCKS_WARN_ON(ww_ctx->ww_class != ww->ww_class);
  18587. +#endif
  18588. + ww_ctx->acquired++;
  18589. +}
  18590. +
  18591. +#ifdef CONFIG_PREEMPT_RT_FULL
  18592. +static void ww_mutex_account_lock(struct rt_mutex *lock,
  18593. + struct ww_acquire_ctx *ww_ctx)
  18594. +{
  18595. + struct ww_mutex *ww = container_of(lock, struct ww_mutex, base.lock);
  18596. + struct rt_mutex_waiter *waiter, *n;
  18597. +
  18598. + /*
  18599. + * This branch gets optimized out for the common case,
  18600. + * and is only important for ww_mutex_lock.
  18601. + */
  18602. + ww_mutex_lock_acquired(ww, ww_ctx);
  18603. + ww->ctx = ww_ctx;
  18604. +
  18605. + /*
  18606. + * Give any possible sleeping processes the chance to wake up,
  18607. + * so they can recheck if they have to back off.
  18608. + */
  18609. + rbtree_postorder_for_each_entry_safe(waiter, n, &lock->waiters,
  18610. + tree_entry) {
  18611. + /* XXX debug rt mutex waiter wakeup */
  18612. +
  18613. + BUG_ON(waiter->lock != lock);
  18614. + rt_mutex_wake_waiter(waiter);
  18615. + }
  18616. +}
  18617. +
  18618. +#else
  18619. +
  18620. +static void ww_mutex_account_lock(struct rt_mutex *lock,
  18621. + struct ww_acquire_ctx *ww_ctx)
  18622. +{
  18623. + BUG();
  18624. +}
  18625. +#endif
  18626. +
  18627. /*
  18628. * Slow path lock function:
  18629. */
  18630. static int __sched
  18631. rt_mutex_slowlock(struct rt_mutex *lock, int state,
  18632. struct hrtimer_sleeper *timeout,
  18633. - enum rtmutex_chainwalk chwalk)
  18634. + enum rtmutex_chainwalk chwalk,
  18635. + struct ww_acquire_ctx *ww_ctx)
  18636. {
  18637. struct rt_mutex_waiter waiter;
  18638. + unsigned long flags;
  18639. int ret = 0;
  18640. - debug_rt_mutex_init_waiter(&waiter);
  18641. - RB_CLEAR_NODE(&waiter.pi_tree_entry);
  18642. - RB_CLEAR_NODE(&waiter.tree_entry);
  18643. + rt_mutex_init_waiter(&waiter, false);
  18644. - raw_spin_lock(&lock->wait_lock);
  18645. + /*
  18646. + * Technically we could use raw_spin_[un]lock_irq() here, but this can
  18647. + * be called in early boot if the cmpxchg() fast path is disabled
  18648. + * (debug, no architecture support). In this case we will acquire the
  18649. + * rtmutex with lock->wait_lock held. But we cannot unconditionally
  18650. + * enable interrupts in that early boot case. So we need to use the
  18651. + * irqsave/restore variants.
  18652. + */
  18653. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  18654. /* Try to acquire the lock again: */
  18655. if (try_to_take_rt_mutex(lock, current, NULL)) {
  18656. - raw_spin_unlock(&lock->wait_lock);
  18657. + if (ww_ctx)
  18658. + ww_mutex_account_lock(lock, ww_ctx);
  18659. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  18660. return 0;
  18661. }
  18662. @@ -1260,13 +1845,23 @@ rt_mutex_slowlock(struct rt_mutex *lock, int state,
  18663. if (likely(!ret))
  18664. /* sleep on the mutex */
  18665. - ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
  18666. + ret = __rt_mutex_slowlock(lock, state, timeout, &waiter,
  18667. + ww_ctx);
  18668. + else if (ww_ctx) {
  18669. + /* ww_mutex received EDEADLK, let it become EALREADY */
  18670. + ret = __mutex_lock_check_stamp(lock, ww_ctx);
  18671. + BUG_ON(!ret);
  18672. + }
  18673. if (unlikely(ret)) {
  18674. __set_current_state(TASK_RUNNING);
  18675. if (rt_mutex_has_waiters(lock))
  18676. remove_waiter(lock, &waiter);
  18677. - rt_mutex_handle_deadlock(ret, chwalk, &waiter);
  18678. + /* ww_mutex want to report EDEADLK/EALREADY, let them */
  18679. + if (!ww_ctx)
  18680. + rt_mutex_handle_deadlock(ret, chwalk, &waiter);
  18681. + } else if (ww_ctx) {
  18682. + ww_mutex_account_lock(lock, ww_ctx);
  18683. }
  18684. /*
  18685. @@ -1275,7 +1870,7 @@ rt_mutex_slowlock(struct rt_mutex *lock, int state,
  18686. */
  18687. fixup_rt_mutex_waiters(lock);
  18688. - raw_spin_unlock(&lock->wait_lock);
  18689. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  18690. /* Remove pending timer: */
  18691. if (unlikely(timeout))
  18692. @@ -1291,6 +1886,7 @@ rt_mutex_slowlock(struct rt_mutex *lock, int state,
  18693. */
  18694. static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
  18695. {
  18696. + unsigned long flags;
  18697. int ret;
  18698. /*
  18699. @@ -1302,10 +1898,10 @@ static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
  18700. return 0;
  18701. /*
  18702. - * The mutex has currently no owner. Lock the wait lock and
  18703. - * try to acquire the lock.
  18704. + * The mutex has currently no owner. Lock the wait lock and try to
  18705. + * acquire the lock. We use irqsave here to support early boot calls.
  18706. */
  18707. - raw_spin_lock(&lock->wait_lock);
  18708. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  18709. ret = try_to_take_rt_mutex(lock, current, NULL);
  18710. @@ -1315,7 +1911,7 @@ static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
  18711. */
  18712. fixup_rt_mutex_waiters(lock);
  18713. - raw_spin_unlock(&lock->wait_lock);
  18714. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  18715. return ret;
  18716. }
  18717. @@ -1325,9 +1921,13 @@ static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
  18718. * Return whether the current task needs to undo a potential priority boosting.
  18719. */
  18720. static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
  18721. - struct wake_q_head *wake_q)
  18722. + struct wake_q_head *wake_q,
  18723. + struct wake_q_head *wake_sleeper_q)
  18724. {
  18725. - raw_spin_lock(&lock->wait_lock);
  18726. + unsigned long flags;
  18727. +
  18728. + /* irqsave required to support early boot calls */
  18729. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  18730. debug_rt_mutex_unlock(lock);
  18731. @@ -1366,10 +1966,10 @@ static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
  18732. */
  18733. while (!rt_mutex_has_waiters(lock)) {
  18734. /* Drops lock->wait_lock ! */
  18735. - if (unlock_rt_mutex_safe(lock) == true)
  18736. + if (unlock_rt_mutex_safe(lock, flags) == true)
  18737. return false;
  18738. /* Relock the rtmutex and try again */
  18739. - raw_spin_lock(&lock->wait_lock);
  18740. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  18741. }
  18742. /*
  18743. @@ -1378,9 +1978,9 @@ static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
  18744. *
  18745. * Queue the next waiter for wakeup once we release the wait_lock.
  18746. */
  18747. - mark_wakeup_next_waiter(wake_q, lock);
  18748. + mark_wakeup_next_waiter(wake_q, wake_sleeper_q, lock);
  18749. - raw_spin_unlock(&lock->wait_lock);
  18750. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  18751. /* check PI boosting */
  18752. return true;
  18753. @@ -1394,31 +1994,36 @@ static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
  18754. */
  18755. static inline int
  18756. rt_mutex_fastlock(struct rt_mutex *lock, int state,
  18757. + struct ww_acquire_ctx *ww_ctx,
  18758. int (*slowfn)(struct rt_mutex *lock, int state,
  18759. struct hrtimer_sleeper *timeout,
  18760. - enum rtmutex_chainwalk chwalk))
  18761. + enum rtmutex_chainwalk chwalk,
  18762. + struct ww_acquire_ctx *ww_ctx))
  18763. {
  18764. if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
  18765. rt_mutex_deadlock_account_lock(lock, current);
  18766. return 0;
  18767. } else
  18768. - return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
  18769. + return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK,
  18770. + ww_ctx);
  18771. }
  18772. static inline int
  18773. rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
  18774. struct hrtimer_sleeper *timeout,
  18775. enum rtmutex_chainwalk chwalk,
  18776. + struct ww_acquire_ctx *ww_ctx,
  18777. int (*slowfn)(struct rt_mutex *lock, int state,
  18778. struct hrtimer_sleeper *timeout,
  18779. - enum rtmutex_chainwalk chwalk))
  18780. + enum rtmutex_chainwalk chwalk,
  18781. + struct ww_acquire_ctx *ww_ctx))
  18782. {
  18783. if (chwalk == RT_MUTEX_MIN_CHAINWALK &&
  18784. likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
  18785. rt_mutex_deadlock_account_lock(lock, current);
  18786. return 0;
  18787. } else
  18788. - return slowfn(lock, state, timeout, chwalk);
  18789. + return slowfn(lock, state, timeout, chwalk, ww_ctx);
  18790. }
  18791. static inline int
  18792. @@ -1435,17 +2040,20 @@ rt_mutex_fasttrylock(struct rt_mutex *lock,
  18793. static inline void
  18794. rt_mutex_fastunlock(struct rt_mutex *lock,
  18795. bool (*slowfn)(struct rt_mutex *lock,
  18796. - struct wake_q_head *wqh))
  18797. + struct wake_q_head *wqh,
  18798. + struct wake_q_head *wq_sleeper))
  18799. {
  18800. WAKE_Q(wake_q);
  18801. + WAKE_Q(wake_sleeper_q);
  18802. if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
  18803. rt_mutex_deadlock_account_unlock(current);
  18804. } else {
  18805. - bool deboost = slowfn(lock, &wake_q);
  18806. + bool deboost = slowfn(lock, &wake_q, &wake_sleeper_q);
  18807. wake_up_q(&wake_q);
  18808. + wake_up_q_sleeper(&wake_sleeper_q);
  18809. /* Undo pi boosting if necessary: */
  18810. if (deboost)
  18811. @@ -1462,7 +2070,7 @@ void __sched rt_mutex_lock(struct rt_mutex *lock)
  18812. {
  18813. might_sleep();
  18814. - rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
  18815. + rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, NULL, rt_mutex_slowlock);
  18816. }
  18817. EXPORT_SYMBOL_GPL(rt_mutex_lock);
  18818. @@ -1479,7 +2087,7 @@ int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
  18819. {
  18820. might_sleep();
  18821. - return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
  18822. + return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, NULL, rt_mutex_slowlock);
  18823. }
  18824. EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
  18825. @@ -1492,11 +2100,30 @@ int rt_mutex_timed_futex_lock(struct rt_mutex *lock,
  18826. might_sleep();
  18827. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  18828. - RT_MUTEX_FULL_CHAINWALK,
  18829. + RT_MUTEX_FULL_CHAINWALK, NULL,
  18830. rt_mutex_slowlock);
  18831. }
  18832. /**
  18833. + * rt_mutex_lock_killable - lock a rt_mutex killable
  18834. + *
  18835. + * @lock: the rt_mutex to be locked
  18836. + * @detect_deadlock: deadlock detection on/off
  18837. + *
  18838. + * Returns:
  18839. + * 0 on success
  18840. + * -EINTR when interrupted by a signal
  18841. + * -EDEADLK when the lock would deadlock (when deadlock detection is on)
  18842. + */
  18843. +int __sched rt_mutex_lock_killable(struct rt_mutex *lock)
  18844. +{
  18845. + might_sleep();
  18846. +
  18847. + return rt_mutex_fastlock(lock, TASK_KILLABLE, NULL, rt_mutex_slowlock);
  18848. +}
  18849. +EXPORT_SYMBOL_GPL(rt_mutex_lock_killable);
  18850. +
  18851. +/**
  18852. * rt_mutex_timed_lock - lock a rt_mutex interruptible
  18853. * the timeout structure is provided
  18854. * by the caller
  18855. @@ -1516,6 +2143,7 @@ rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout)
  18856. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  18857. RT_MUTEX_MIN_CHAINWALK,
  18858. + NULL,
  18859. rt_mutex_slowlock);
  18860. }
  18861. EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
  18862. @@ -1533,7 +2161,11 @@ EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
  18863. */
  18864. int __sched rt_mutex_trylock(struct rt_mutex *lock)
  18865. {
  18866. +#ifdef CONFIG_PREEMPT_RT_FULL
  18867. + if (WARN_ON_ONCE(in_irq() || in_nmi()))
  18868. +#else
  18869. if (WARN_ON(in_irq() || in_nmi() || in_serving_softirq()))
  18870. +#endif
  18871. return 0;
  18872. return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
  18873. @@ -1559,13 +2191,14 @@ EXPORT_SYMBOL_GPL(rt_mutex_unlock);
  18874. * required or not.
  18875. */
  18876. bool __sched rt_mutex_futex_unlock(struct rt_mutex *lock,
  18877. - struct wake_q_head *wqh)
  18878. + struct wake_q_head *wqh,
  18879. + struct wake_q_head *wq_sleeper)
  18880. {
  18881. if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
  18882. rt_mutex_deadlock_account_unlock(current);
  18883. return false;
  18884. }
  18885. - return rt_mutex_slowunlock(lock, wqh);
  18886. + return rt_mutex_slowunlock(lock, wqh, wq_sleeper);
  18887. }
  18888. /**
  18889. @@ -1598,13 +2231,12 @@ EXPORT_SYMBOL_GPL(rt_mutex_destroy);
  18890. void __rt_mutex_init(struct rt_mutex *lock, const char *name)
  18891. {
  18892. lock->owner = NULL;
  18893. - raw_spin_lock_init(&lock->wait_lock);
  18894. lock->waiters = RB_ROOT;
  18895. lock->waiters_leftmost = NULL;
  18896. debug_rt_mutex_init(lock, name);
  18897. }
  18898. -EXPORT_SYMBOL_GPL(__rt_mutex_init);
  18899. +EXPORT_SYMBOL(__rt_mutex_init);
  18900. /**
  18901. * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
  18902. @@ -1619,7 +2251,7 @@ EXPORT_SYMBOL_GPL(__rt_mutex_init);
  18903. void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
  18904. struct task_struct *proxy_owner)
  18905. {
  18906. - __rt_mutex_init(lock, NULL);
  18907. + rt_mutex_init(lock);
  18908. debug_rt_mutex_proxy_lock(lock, proxy_owner);
  18909. rt_mutex_set_owner(lock, proxy_owner);
  18910. rt_mutex_deadlock_account_lock(lock, proxy_owner);
  18911. @@ -1660,13 +2292,42 @@ int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
  18912. {
  18913. int ret;
  18914. - raw_spin_lock(&lock->wait_lock);
  18915. + raw_spin_lock_irq(&lock->wait_lock);
  18916. if (try_to_take_rt_mutex(lock, task, NULL)) {
  18917. - raw_spin_unlock(&lock->wait_lock);
  18918. + raw_spin_unlock_irq(&lock->wait_lock);
  18919. return 1;
  18920. }
  18921. +#ifdef CONFIG_PREEMPT_RT_FULL
  18922. + /*
  18923. + * In PREEMPT_RT there's an added race.
  18924. + * If the task, that we are about to requeue, times out,
  18925. + * it can set the PI_WAKEUP_INPROGRESS. This tells the requeue
  18926. + * to skip this task. But right after the task sets
  18927. + * its pi_blocked_on to PI_WAKEUP_INPROGRESS it can then
  18928. + * block on the spin_lock(&hb->lock), which in RT is an rtmutex.
  18929. + * This will replace the PI_WAKEUP_INPROGRESS with the actual
  18930. + * lock that it blocks on. We *must not* place this task
  18931. + * on this proxy lock in that case.
  18932. + *
  18933. + * To prevent this race, we first take the task's pi_lock
  18934. + * and check if it has updated its pi_blocked_on. If it has,
  18935. + * we assume that it woke up and we return -EAGAIN.
  18936. + * Otherwise, we set the task's pi_blocked_on to
  18937. + * PI_REQUEUE_INPROGRESS, so that if the task is waking up
  18938. + * it will know that we are in the process of requeuing it.
  18939. + */
  18940. + raw_spin_lock(&task->pi_lock);
  18941. + if (task->pi_blocked_on) {
  18942. + raw_spin_unlock(&task->pi_lock);
  18943. + raw_spin_unlock_irq(&lock->wait_lock);
  18944. + return -EAGAIN;
  18945. + }
  18946. + task->pi_blocked_on = PI_REQUEUE_INPROGRESS;
  18947. + raw_spin_unlock(&task->pi_lock);
  18948. +#endif
  18949. +
  18950. /* We enforce deadlock detection for futexes */
  18951. ret = task_blocks_on_rt_mutex(lock, waiter, task,
  18952. RT_MUTEX_FULL_CHAINWALK);
  18953. @@ -1681,10 +2342,10 @@ int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
  18954. ret = 0;
  18955. }
  18956. - if (unlikely(ret))
  18957. + if (ret && rt_mutex_has_waiters(lock))
  18958. remove_waiter(lock, waiter);
  18959. - raw_spin_unlock(&lock->wait_lock);
  18960. + raw_spin_unlock_irq(&lock->wait_lock);
  18961. debug_rt_mutex_print_deadlock(waiter);
  18962. @@ -1732,12 +2393,12 @@ int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
  18963. {
  18964. int ret;
  18965. - raw_spin_lock(&lock->wait_lock);
  18966. + raw_spin_lock_irq(&lock->wait_lock);
  18967. set_current_state(TASK_INTERRUPTIBLE);
  18968. /* sleep on the mutex */
  18969. - ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
  18970. + ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter, NULL);
  18971. if (unlikely(ret))
  18972. remove_waiter(lock, waiter);
  18973. @@ -1748,7 +2409,93 @@ int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
  18974. */
  18975. fixup_rt_mutex_waiters(lock);
  18976. - raw_spin_unlock(&lock->wait_lock);
  18977. + raw_spin_unlock_irq(&lock->wait_lock);
  18978. return ret;
  18979. }
  18980. +
  18981. +static inline int
  18982. +ww_mutex_deadlock_injection(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
  18983. +{
  18984. +#ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
  18985. + unsigned tmp;
  18986. +
  18987. + if (ctx->deadlock_inject_countdown-- == 0) {
  18988. + tmp = ctx->deadlock_inject_interval;
  18989. + if (tmp > UINT_MAX/4)
  18990. + tmp = UINT_MAX;
  18991. + else
  18992. + tmp = tmp*2 + tmp + tmp/2;
  18993. +
  18994. + ctx->deadlock_inject_interval = tmp;
  18995. + ctx->deadlock_inject_countdown = tmp;
  18996. + ctx->contending_lock = lock;
  18997. +
  18998. + ww_mutex_unlock(lock);
  18999. +
  19000. + return -EDEADLK;
  19001. + }
  19002. +#endif
  19003. +
  19004. + return 0;
  19005. +}
  19006. +
  19007. +#ifdef CONFIG_PREEMPT_RT_FULL
  19008. +int __sched
  19009. +__ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ww_ctx)
  19010. +{
  19011. + int ret;
  19012. +
  19013. + might_sleep();
  19014. +
  19015. + mutex_acquire_nest(&lock->base.dep_map, 0, 0, &ww_ctx->dep_map, _RET_IP_);
  19016. + ret = rt_mutex_slowlock(&lock->base.lock, TASK_INTERRUPTIBLE, NULL, 0, ww_ctx);
  19017. + if (ret)
  19018. + mutex_release(&lock->base.dep_map, 1, _RET_IP_);
  19019. + else if (!ret && ww_ctx->acquired > 1)
  19020. + return ww_mutex_deadlock_injection(lock, ww_ctx);
  19021. +
  19022. + return ret;
  19023. +}
  19024. +EXPORT_SYMBOL_GPL(__ww_mutex_lock_interruptible);
  19025. +
  19026. +int __sched
  19027. +__ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ww_ctx)
  19028. +{
  19029. + int ret;
  19030. +
  19031. + might_sleep();
  19032. +
  19033. + mutex_acquire_nest(&lock->base.dep_map, 0, 0, &ww_ctx->dep_map, _RET_IP_);
  19034. + ret = rt_mutex_slowlock(&lock->base.lock, TASK_UNINTERRUPTIBLE, NULL, 0, ww_ctx);
  19035. + if (ret)
  19036. + mutex_release(&lock->base.dep_map, 1, _RET_IP_);
  19037. + else if (!ret && ww_ctx->acquired > 1)
  19038. + return ww_mutex_deadlock_injection(lock, ww_ctx);
  19039. +
  19040. + return ret;
  19041. +}
  19042. +EXPORT_SYMBOL_GPL(__ww_mutex_lock);
  19043. +
  19044. +void __sched ww_mutex_unlock(struct ww_mutex *lock)
  19045. +{
  19046. + int nest = !!lock->ctx;
  19047. +
  19048. + /*
  19049. + * The unlocking fastpath is the 0->1 transition from 'locked'
  19050. + * into 'unlocked' state:
  19051. + */
  19052. + if (nest) {
  19053. +#ifdef CONFIG_DEBUG_MUTEXES
  19054. + DEBUG_LOCKS_WARN_ON(!lock->ctx->acquired);
  19055. +#endif
  19056. + if (lock->ctx->acquired > 0)
  19057. + lock->ctx->acquired--;
  19058. + lock->ctx = NULL;
  19059. + }
  19060. +
  19061. + mutex_release(&lock->base.dep_map, nest, _RET_IP_);
  19062. + rt_mutex_unlock(&lock->base.lock);
  19063. +}
  19064. +EXPORT_SYMBOL(ww_mutex_unlock);
  19065. +#endif
  19066. diff --git a/kernel/locking/rtmutex_common.h b/kernel/locking/rtmutex_common.h
  19067. index e317e1cbb3eb..f457c7574920 100644
  19068. --- a/kernel/locking/rtmutex_common.h
  19069. +++ b/kernel/locking/rtmutex_common.h
  19070. @@ -27,6 +27,7 @@ struct rt_mutex_waiter {
  19071. struct rb_node pi_tree_entry;
  19072. struct task_struct *task;
  19073. struct rt_mutex *lock;
  19074. + bool savestate;
  19075. #ifdef CONFIG_DEBUG_RT_MUTEXES
  19076. unsigned long ip;
  19077. struct pid *deadlock_task_pid;
  19078. @@ -98,6 +99,9 @@ enum rtmutex_chainwalk {
  19079. /*
  19080. * PI-futex support (proxy locking functions, etc.):
  19081. */
  19082. +#define PI_WAKEUP_INPROGRESS ((struct rt_mutex_waiter *) 1)
  19083. +#define PI_REQUEUE_INPROGRESS ((struct rt_mutex_waiter *) 2)
  19084. +
  19085. extern struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock);
  19086. extern void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
  19087. struct task_struct *proxy_owner);
  19088. @@ -111,7 +115,8 @@ extern int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
  19089. struct rt_mutex_waiter *waiter);
  19090. extern int rt_mutex_timed_futex_lock(struct rt_mutex *l, struct hrtimer_sleeper *to);
  19091. extern bool rt_mutex_futex_unlock(struct rt_mutex *lock,
  19092. - struct wake_q_head *wqh);
  19093. + struct wake_q_head *wqh,
  19094. + struct wake_q_head *wq_sleeper);
  19095. extern void rt_mutex_adjust_prio(struct task_struct *task);
  19096. #ifdef CONFIG_DEBUG_RT_MUTEXES
  19097. @@ -120,4 +125,14 @@ extern void rt_mutex_adjust_prio(struct task_struct *task);
  19098. # include "rtmutex.h"
  19099. #endif
  19100. +static inline void
  19101. +rt_mutex_init_waiter(struct rt_mutex_waiter *waiter, bool savestate)
  19102. +{
  19103. + debug_rt_mutex_init_waiter(waiter);
  19104. + waiter->task = NULL;
  19105. + waiter->savestate = savestate;
  19106. + RB_CLEAR_NODE(&waiter->pi_tree_entry);
  19107. + RB_CLEAR_NODE(&waiter->tree_entry);
  19108. +}
  19109. +
  19110. #endif
  19111. diff --git a/kernel/locking/spinlock.c b/kernel/locking/spinlock.c
  19112. index db3ccb1dd614..909779647bd1 100644
  19113. --- a/kernel/locking/spinlock.c
  19114. +++ b/kernel/locking/spinlock.c
  19115. @@ -124,8 +124,11 @@ void __lockfunc __raw_##op##_lock_bh(locktype##_t *lock) \
  19116. * __[spin|read|write]_lock_bh()
  19117. */
  19118. BUILD_LOCK_OPS(spin, raw_spinlock);
  19119. +
  19120. +#ifndef CONFIG_PREEMPT_RT_FULL
  19121. BUILD_LOCK_OPS(read, rwlock);
  19122. BUILD_LOCK_OPS(write, rwlock);
  19123. +#endif
  19124. #endif
  19125. @@ -209,6 +212,8 @@ void __lockfunc _raw_spin_unlock_bh(raw_spinlock_t *lock)
  19126. EXPORT_SYMBOL(_raw_spin_unlock_bh);
  19127. #endif
  19128. +#ifndef CONFIG_PREEMPT_RT_FULL
  19129. +
  19130. #ifndef CONFIG_INLINE_READ_TRYLOCK
  19131. int __lockfunc _raw_read_trylock(rwlock_t *lock)
  19132. {
  19133. @@ -353,6 +358,8 @@ void __lockfunc _raw_write_unlock_bh(rwlock_t *lock)
  19134. EXPORT_SYMBOL(_raw_write_unlock_bh);
  19135. #endif
  19136. +#endif /* !PREEMPT_RT_FULL */
  19137. +
  19138. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  19139. void __lockfunc _raw_spin_lock_nested(raw_spinlock_t *lock, int subclass)
  19140. diff --git a/kernel/locking/spinlock_debug.c b/kernel/locking/spinlock_debug.c
  19141. index 0374a596cffa..94970338d518 100644
  19142. --- a/kernel/locking/spinlock_debug.c
  19143. +++ b/kernel/locking/spinlock_debug.c
  19144. @@ -31,6 +31,7 @@ void __raw_spin_lock_init(raw_spinlock_t *lock, const char *name,
  19145. EXPORT_SYMBOL(__raw_spin_lock_init);
  19146. +#ifndef CONFIG_PREEMPT_RT_FULL
  19147. void __rwlock_init(rwlock_t *lock, const char *name,
  19148. struct lock_class_key *key)
  19149. {
  19150. @@ -48,6 +49,7 @@ void __rwlock_init(rwlock_t *lock, const char *name,
  19151. }
  19152. EXPORT_SYMBOL(__rwlock_init);
  19153. +#endif
  19154. static void spin_dump(raw_spinlock_t *lock, const char *msg)
  19155. {
  19156. @@ -159,6 +161,7 @@ void do_raw_spin_unlock(raw_spinlock_t *lock)
  19157. arch_spin_unlock(&lock->raw_lock);
  19158. }
  19159. +#ifndef CONFIG_PREEMPT_RT_FULL
  19160. static void rwlock_bug(rwlock_t *lock, const char *msg)
  19161. {
  19162. if (!debug_locks_off())
  19163. @@ -300,3 +303,5 @@ void do_raw_write_unlock(rwlock_t *lock)
  19164. debug_write_unlock(lock);
  19165. arch_write_unlock(&lock->raw_lock);
  19166. }
  19167. +
  19168. +#endif
  19169. diff --git a/kernel/module.c b/kernel/module.c
  19170. index b14a4f31221f..be3f497a089d 100644
  19171. --- a/kernel/module.c
  19172. +++ b/kernel/module.c
  19173. @@ -682,16 +682,7 @@ static void percpu_modcopy(struct module *mod,
  19174. memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
  19175. }
  19176. -/**
  19177. - * is_module_percpu_address - test whether address is from module static percpu
  19178. - * @addr: address to test
  19179. - *
  19180. - * Test whether @addr belongs to module static percpu area.
  19181. - *
  19182. - * RETURNS:
  19183. - * %true if @addr is from module static percpu area
  19184. - */
  19185. -bool is_module_percpu_address(unsigned long addr)
  19186. +bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
  19187. {
  19188. struct module *mod;
  19189. unsigned int cpu;
  19190. @@ -705,9 +696,11 @@ bool is_module_percpu_address(unsigned long addr)
  19191. continue;
  19192. for_each_possible_cpu(cpu) {
  19193. void *start = per_cpu_ptr(mod->percpu, cpu);
  19194. + void *va = (void *)addr;
  19195. - if ((void *)addr >= start &&
  19196. - (void *)addr < start + mod->percpu_size) {
  19197. + if (va >= start && va < start + mod->percpu_size) {
  19198. + if (can_addr)
  19199. + *can_addr = (unsigned long) (va - start);
  19200. preempt_enable();
  19201. return true;
  19202. }
  19203. @@ -718,6 +711,20 @@ bool is_module_percpu_address(unsigned long addr)
  19204. return false;
  19205. }
  19206. +/**
  19207. + * is_module_percpu_address - test whether address is from module static percpu
  19208. + * @addr: address to test
  19209. + *
  19210. + * Test whether @addr belongs to module static percpu area.
  19211. + *
  19212. + * RETURNS:
  19213. + * %true if @addr is from module static percpu area
  19214. + */
  19215. +bool is_module_percpu_address(unsigned long addr)
  19216. +{
  19217. + return __is_module_percpu_address(addr, NULL);
  19218. +}
  19219. +
  19220. #else /* ... !CONFIG_SMP */
  19221. static inline void __percpu *mod_percpu(struct module *mod)
  19222. @@ -749,6 +756,11 @@ bool is_module_percpu_address(unsigned long addr)
  19223. return false;
  19224. }
  19225. +bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
  19226. +{
  19227. + return false;
  19228. +}
  19229. +
  19230. #endif /* CONFIG_SMP */
  19231. #define MODINFO_ATTR(field) \
  19232. diff --git a/kernel/panic.c b/kernel/panic.c
  19233. index 41e2b54f36b5..3535f802953a 100644
  19234. --- a/kernel/panic.c
  19235. +++ b/kernel/panic.c
  19236. @@ -61,6 +61,37 @@ void __weak panic_smp_self_stop(void)
  19237. cpu_relax();
  19238. }
  19239. +/*
  19240. + * Stop ourselves in NMI context if another CPU has already panicked. Arch code
  19241. + * may override this to prepare for crash dumping, e.g. save regs info.
  19242. + */
  19243. +void __weak nmi_panic_self_stop(struct pt_regs *regs)
  19244. +{
  19245. + panic_smp_self_stop();
  19246. +}
  19247. +
  19248. +atomic_t panic_cpu = ATOMIC_INIT(PANIC_CPU_INVALID);
  19249. +
  19250. +/*
  19251. + * A variant of panic() called from NMI context. We return if we've already
  19252. + * panicked on this CPU. If another CPU already panicked, loop in
  19253. + * nmi_panic_self_stop() which can provide architecture dependent code such
  19254. + * as saving register state for crash dump.
  19255. + */
  19256. +void nmi_panic(struct pt_regs *regs, const char *msg)
  19257. +{
  19258. + int old_cpu, cpu;
  19259. +
  19260. + cpu = raw_smp_processor_id();
  19261. + old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, cpu);
  19262. +
  19263. + if (old_cpu == PANIC_CPU_INVALID)
  19264. + panic("%s", msg);
  19265. + else if (old_cpu != cpu)
  19266. + nmi_panic_self_stop(regs);
  19267. +}
  19268. +EXPORT_SYMBOL(nmi_panic);
  19269. +
  19270. /**
  19271. * panic - halt the system
  19272. * @fmt: The text string to print
  19273. @@ -71,17 +102,17 @@ void __weak panic_smp_self_stop(void)
  19274. */
  19275. void panic(const char *fmt, ...)
  19276. {
  19277. - static DEFINE_SPINLOCK(panic_lock);
  19278. static char buf[1024];
  19279. va_list args;
  19280. long i, i_next = 0;
  19281. int state = 0;
  19282. + int old_cpu, this_cpu;
  19283. /*
  19284. * Disable local interrupts. This will prevent panic_smp_self_stop
  19285. * from deadlocking the first cpu that invokes the panic, since
  19286. * there is nothing to prevent an interrupt handler (that runs
  19287. - * after the panic_lock is acquired) from invoking panic again.
  19288. + * after setting panic_cpu) from invoking panic() again.
  19289. */
  19290. local_irq_disable();
  19291. @@ -94,8 +125,16 @@ void panic(const char *fmt, ...)
  19292. * multiple parallel invocations of panic, all other CPUs either
  19293. * stop themself or will wait until they are stopped by the 1st CPU
  19294. * with smp_send_stop().
  19295. + *
  19296. + * `old_cpu == PANIC_CPU_INVALID' means this is the 1st CPU which
  19297. + * comes here, so go ahead.
  19298. + * `old_cpu == this_cpu' means we came from nmi_panic() which sets
  19299. + * panic_cpu to this CPU. In this case, this is also the 1st CPU.
  19300. */
  19301. - if (!spin_trylock(&panic_lock))
  19302. + this_cpu = raw_smp_processor_id();
  19303. + old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, this_cpu);
  19304. +
  19305. + if (old_cpu != PANIC_CPU_INVALID && old_cpu != this_cpu)
  19306. panic_smp_self_stop();
  19307. console_verbose();
  19308. @@ -400,9 +439,11 @@ static u64 oops_id;
  19309. static int init_oops_id(void)
  19310. {
  19311. +#ifndef CONFIG_PREEMPT_RT_FULL
  19312. if (!oops_id)
  19313. get_random_bytes(&oops_id, sizeof(oops_id));
  19314. else
  19315. +#endif
  19316. oops_id++;
  19317. return 0;
  19318. diff --git a/kernel/power/hibernate.c b/kernel/power/hibernate.c
  19319. index 3124cebaec31..c1b981521dd0 100644
  19320. --- a/kernel/power/hibernate.c
  19321. +++ b/kernel/power/hibernate.c
  19322. @@ -285,6 +285,8 @@ static int create_image(int platform_mode)
  19323. local_irq_disable();
  19324. + system_state = SYSTEM_SUSPEND;
  19325. +
  19326. error = syscore_suspend();
  19327. if (error) {
  19328. printk(KERN_ERR "PM: Some system devices failed to power down, "
  19329. @@ -314,6 +316,7 @@ static int create_image(int platform_mode)
  19330. syscore_resume();
  19331. Enable_irqs:
  19332. + system_state = SYSTEM_RUNNING;
  19333. local_irq_enable();
  19334. Enable_cpus:
  19335. @@ -438,6 +441,7 @@ static int resume_target_kernel(bool platform_mode)
  19336. goto Enable_cpus;
  19337. local_irq_disable();
  19338. + system_state = SYSTEM_SUSPEND;
  19339. error = syscore_suspend();
  19340. if (error)
  19341. @@ -471,6 +475,7 @@ static int resume_target_kernel(bool platform_mode)
  19342. syscore_resume();
  19343. Enable_irqs:
  19344. + system_state = SYSTEM_RUNNING;
  19345. local_irq_enable();
  19346. Enable_cpus:
  19347. @@ -556,6 +561,7 @@ int hibernation_platform_enter(void)
  19348. goto Enable_cpus;
  19349. local_irq_disable();
  19350. + system_state = SYSTEM_SUSPEND;
  19351. syscore_suspend();
  19352. if (pm_wakeup_pending()) {
  19353. error = -EAGAIN;
  19354. @@ -568,6 +574,7 @@ int hibernation_platform_enter(void)
  19355. Power_up:
  19356. syscore_resume();
  19357. + system_state = SYSTEM_RUNNING;
  19358. local_irq_enable();
  19359. Enable_cpus:
  19360. @@ -642,6 +649,10 @@ static void power_down(void)
  19361. cpu_relax();
  19362. }
  19363. +#ifndef CONFIG_SUSPEND
  19364. +bool pm_in_action;
  19365. +#endif
  19366. +
  19367. /**
  19368. * hibernate - Carry out system hibernation, including saving the image.
  19369. */
  19370. @@ -654,6 +665,8 @@ int hibernate(void)
  19371. return -EPERM;
  19372. }
  19373. + pm_in_action = true;
  19374. +
  19375. lock_system_sleep();
  19376. /* The snapshot device should not be opened while we're running */
  19377. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  19378. @@ -719,6 +732,7 @@ int hibernate(void)
  19379. atomic_inc(&snapshot_device_available);
  19380. Unlock:
  19381. unlock_system_sleep();
  19382. + pm_in_action = false;
  19383. return error;
  19384. }
  19385. diff --git a/kernel/power/suspend.c b/kernel/power/suspend.c
  19386. index f9fe133c13e2..393bc342c586 100644
  19387. --- a/kernel/power/suspend.c
  19388. +++ b/kernel/power/suspend.c
  19389. @@ -359,6 +359,8 @@ static int suspend_enter(suspend_state_t state, bool *wakeup)
  19390. arch_suspend_disable_irqs();
  19391. BUG_ON(!irqs_disabled());
  19392. + system_state = SYSTEM_SUSPEND;
  19393. +
  19394. error = syscore_suspend();
  19395. if (!error) {
  19396. *wakeup = pm_wakeup_pending();
  19397. @@ -375,6 +377,8 @@ static int suspend_enter(suspend_state_t state, bool *wakeup)
  19398. syscore_resume();
  19399. }
  19400. + system_state = SYSTEM_RUNNING;
  19401. +
  19402. arch_suspend_enable_irqs();
  19403. BUG_ON(irqs_disabled());
  19404. @@ -518,6 +522,8 @@ static int enter_state(suspend_state_t state)
  19405. return error;
  19406. }
  19407. +bool pm_in_action;
  19408. +
  19409. /**
  19410. * pm_suspend - Externally visible function for suspending the system.
  19411. * @state: System sleep state to enter.
  19412. @@ -532,6 +538,8 @@ int pm_suspend(suspend_state_t state)
  19413. if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX)
  19414. return -EINVAL;
  19415. + pm_in_action = true;
  19416. +
  19417. error = enter_state(state);
  19418. if (error) {
  19419. suspend_stats.fail++;
  19420. @@ -539,6 +547,7 @@ int pm_suspend(suspend_state_t state)
  19421. } else {
  19422. suspend_stats.success++;
  19423. }
  19424. + pm_in_action = false;
  19425. return error;
  19426. }
  19427. EXPORT_SYMBOL(pm_suspend);
  19428. diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
  19429. index 0b5613554769..99deb2617308 100644
  19430. --- a/kernel/printk/printk.c
  19431. +++ b/kernel/printk/printk.c
  19432. @@ -241,6 +241,65 @@ struct printk_log {
  19433. */
  19434. static DEFINE_RAW_SPINLOCK(logbuf_lock);
  19435. +#ifdef CONFIG_EARLY_PRINTK
  19436. +struct console *early_console;
  19437. +
  19438. +static void early_vprintk(const char *fmt, va_list ap)
  19439. +{
  19440. + if (early_console) {
  19441. + char buf[512];
  19442. + int n = vscnprintf(buf, sizeof(buf), fmt, ap);
  19443. +
  19444. + early_console->write(early_console, buf, n);
  19445. + }
  19446. +}
  19447. +
  19448. +asmlinkage void early_printk(const char *fmt, ...)
  19449. +{
  19450. + va_list ap;
  19451. +
  19452. + va_start(ap, fmt);
  19453. + early_vprintk(fmt, ap);
  19454. + va_end(ap);
  19455. +}
  19456. +
  19457. +/*
  19458. + * This is independent of any log levels - a global
  19459. + * kill switch that turns off all of printk.
  19460. + *
  19461. + * Used by the NMI watchdog if early-printk is enabled.
  19462. + */
  19463. +static bool __read_mostly printk_killswitch;
  19464. +
  19465. +static int __init force_early_printk_setup(char *str)
  19466. +{
  19467. + printk_killswitch = true;
  19468. + return 0;
  19469. +}
  19470. +early_param("force_early_printk", force_early_printk_setup);
  19471. +
  19472. +void printk_kill(void)
  19473. +{
  19474. + printk_killswitch = true;
  19475. +}
  19476. +
  19477. +#ifdef CONFIG_PRINTK
  19478. +static int forced_early_printk(const char *fmt, va_list ap)
  19479. +{
  19480. + if (!printk_killswitch)
  19481. + return 0;
  19482. + early_vprintk(fmt, ap);
  19483. + return 1;
  19484. +}
  19485. +#endif
  19486. +
  19487. +#else
  19488. +static inline int forced_early_printk(const char *fmt, va_list ap)
  19489. +{
  19490. + return 0;
  19491. +}
  19492. +#endif
  19493. +
  19494. #ifdef CONFIG_PRINTK
  19495. DECLARE_WAIT_QUEUE_HEAD(log_wait);
  19496. /* the next printk record to read by syslog(READ) or /proc/kmsg */
  19497. @@ -1203,6 +1262,7 @@ static int syslog_print_all(char __user *buf, int size, bool clear)
  19498. {
  19499. char *text;
  19500. int len = 0;
  19501. + int attempts = 0;
  19502. text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
  19503. if (!text)
  19504. @@ -1214,7 +1274,14 @@ static int syslog_print_all(char __user *buf, int size, bool clear)
  19505. u64 seq;
  19506. u32 idx;
  19507. enum log_flags prev;
  19508. -
  19509. + int num_msg;
  19510. +try_again:
  19511. + attempts++;
  19512. + if (attempts > 10) {
  19513. + len = -EBUSY;
  19514. + goto out;
  19515. + }
  19516. + num_msg = 0;
  19517. if (clear_seq < log_first_seq) {
  19518. /* messages are gone, move to first available one */
  19519. clear_seq = log_first_seq;
  19520. @@ -1235,6 +1302,14 @@ static int syslog_print_all(char __user *buf, int size, bool clear)
  19521. prev = msg->flags;
  19522. idx = log_next(idx);
  19523. seq++;
  19524. + num_msg++;
  19525. + if (num_msg > 5) {
  19526. + num_msg = 0;
  19527. + raw_spin_unlock_irq(&logbuf_lock);
  19528. + raw_spin_lock_irq(&logbuf_lock);
  19529. + if (clear_seq < log_first_seq)
  19530. + goto try_again;
  19531. + }
  19532. }
  19533. /* move first record forward until length fits into the buffer */
  19534. @@ -1248,6 +1323,14 @@ static int syslog_print_all(char __user *buf, int size, bool clear)
  19535. prev = msg->flags;
  19536. idx = log_next(idx);
  19537. seq++;
  19538. + num_msg++;
  19539. + if (num_msg > 5) {
  19540. + num_msg = 0;
  19541. + raw_spin_unlock_irq(&logbuf_lock);
  19542. + raw_spin_lock_irq(&logbuf_lock);
  19543. + if (clear_seq < log_first_seq)
  19544. + goto try_again;
  19545. + }
  19546. }
  19547. /* last message fitting into this dump */
  19548. @@ -1288,6 +1371,7 @@ static int syslog_print_all(char __user *buf, int size, bool clear)
  19549. clear_seq = log_next_seq;
  19550. clear_idx = log_next_idx;
  19551. }
  19552. +out:
  19553. raw_spin_unlock_irq(&logbuf_lock);
  19554. kfree(text);
  19555. @@ -1443,6 +1527,12 @@ static void call_console_drivers(int level,
  19556. if (!console_drivers)
  19557. return;
  19558. + if (IS_ENABLED(CONFIG_PREEMPT_RT_BASE)) {
  19559. + if (in_irq() || in_nmi())
  19560. + return;
  19561. + }
  19562. +
  19563. + migrate_disable();
  19564. for_each_console(con) {
  19565. if (exclusive_console && con != exclusive_console)
  19566. continue;
  19567. @@ -1458,6 +1548,7 @@ static void call_console_drivers(int level,
  19568. else
  19569. con->write(con, text, len);
  19570. }
  19571. + migrate_enable();
  19572. }
  19573. /*
  19574. @@ -1518,6 +1609,15 @@ static inline int can_use_console(unsigned int cpu)
  19575. static int console_trylock_for_printk(void)
  19576. {
  19577. unsigned int cpu = smp_processor_id();
  19578. +#ifdef CONFIG_PREEMPT_RT_FULL
  19579. + int lock = !early_boot_irqs_disabled && (preempt_count() == 0) &&
  19580. + !irqs_disabled();
  19581. +#else
  19582. + int lock = 1;
  19583. +#endif
  19584. +
  19585. + if (!lock)
  19586. + return 0;
  19587. if (!console_trylock())
  19588. return 0;
  19589. @@ -1672,6 +1772,13 @@ asmlinkage int vprintk_emit(int facility, int level,
  19590. /* cpu currently holding logbuf_lock in this function */
  19591. static unsigned int logbuf_cpu = UINT_MAX;
  19592. + /*
  19593. + * Fall back to early_printk if a debugging subsystem has
  19594. + * killed printk output
  19595. + */
  19596. + if (unlikely(forced_early_printk(fmt, args)))
  19597. + return 1;
  19598. +
  19599. if (level == LOGLEVEL_SCHED) {
  19600. level = LOGLEVEL_DEFAULT;
  19601. in_sched = true;
  19602. @@ -1813,8 +1920,7 @@ asmlinkage int vprintk_emit(int facility, int level,
  19603. * console_sem which would prevent anyone from printing to
  19604. * console
  19605. */
  19606. - preempt_disable();
  19607. -
  19608. + migrate_disable();
  19609. /*
  19610. * Try to acquire and then immediately release the console
  19611. * semaphore. The release will print out buffers and wake up
  19612. @@ -1822,7 +1928,7 @@ asmlinkage int vprintk_emit(int facility, int level,
  19613. */
  19614. if (console_trylock_for_printk())
  19615. console_unlock();
  19616. - preempt_enable();
  19617. + migrate_enable();
  19618. lockdep_on();
  19619. }
  19620. @@ -1961,26 +2067,6 @@ DEFINE_PER_CPU(printk_func_t, printk_func);
  19621. #endif /* CONFIG_PRINTK */
  19622. -#ifdef CONFIG_EARLY_PRINTK
  19623. -struct console *early_console;
  19624. -
  19625. -asmlinkage __visible void early_printk(const char *fmt, ...)
  19626. -{
  19627. - va_list ap;
  19628. - char buf[512];
  19629. - int n;
  19630. -
  19631. - if (!early_console)
  19632. - return;
  19633. -
  19634. - va_start(ap, fmt);
  19635. - n = vscnprintf(buf, sizeof(buf), fmt, ap);
  19636. - va_end(ap);
  19637. -
  19638. - early_console->write(early_console, buf, n);
  19639. -}
  19640. -#endif
  19641. -
  19642. static int __add_preferred_console(char *name, int idx, char *options,
  19643. char *brl_options)
  19644. {
  19645. @@ -2202,11 +2288,16 @@ static void console_cont_flush(char *text, size_t size)
  19646. goto out;
  19647. len = cont_print_text(text, size);
  19648. +#ifdef CONFIG_PREEMPT_RT_FULL
  19649. + raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  19650. + call_console_drivers(cont.level, NULL, 0, text, len);
  19651. +#else
  19652. raw_spin_unlock(&logbuf_lock);
  19653. stop_critical_timings();
  19654. call_console_drivers(cont.level, NULL, 0, text, len);
  19655. start_critical_timings();
  19656. local_irq_restore(flags);
  19657. +#endif
  19658. return;
  19659. out:
  19660. raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  19661. @@ -2316,13 +2407,17 @@ skip:
  19662. console_idx = log_next(console_idx);
  19663. console_seq++;
  19664. console_prev = msg->flags;
  19665. +#ifdef CONFIG_PREEMPT_RT_FULL
  19666. + raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  19667. + call_console_drivers(level, ext_text, ext_len, text, len);
  19668. +#else
  19669. raw_spin_unlock(&logbuf_lock);
  19670. stop_critical_timings(); /* don't trace print latency */
  19671. call_console_drivers(level, ext_text, ext_len, text, len);
  19672. start_critical_timings();
  19673. local_irq_restore(flags);
  19674. -
  19675. +#endif
  19676. if (do_cond_resched)
  19677. cond_resched();
  19678. }
  19679. @@ -2374,6 +2469,11 @@ void console_unblank(void)
  19680. {
  19681. struct console *c;
  19682. + if (IS_ENABLED(CONFIG_PREEMPT_RT_BASE)) {
  19683. + if (in_irq() || in_nmi())
  19684. + return;
  19685. + }
  19686. +
  19687. /*
  19688. * console_unblank can no longer be called in interrupt context unless
  19689. * oops_in_progress is set to 1..
  19690. diff --git a/kernel/ptrace.c b/kernel/ptrace.c
  19691. index c7e8ed99c953..2a19cc5001b4 100644
  19692. --- a/kernel/ptrace.c
  19693. +++ b/kernel/ptrace.c
  19694. @@ -136,7 +136,14 @@ static bool ptrace_freeze_traced(struct task_struct *task)
  19695. spin_lock_irq(&task->sighand->siglock);
  19696. if (task_is_traced(task) && !__fatal_signal_pending(task)) {
  19697. - task->state = __TASK_TRACED;
  19698. + unsigned long flags;
  19699. +
  19700. + raw_spin_lock_irqsave(&task->pi_lock, flags);
  19701. + if (task->state & __TASK_TRACED)
  19702. + task->state = __TASK_TRACED;
  19703. + else
  19704. + task->saved_state = __TASK_TRACED;
  19705. + raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  19706. ret = true;
  19707. }
  19708. spin_unlock_irq(&task->sighand->siglock);
  19709. diff --git a/kernel/rcu/rcutorture.c b/kernel/rcu/rcutorture.c
  19710. index d89328e260df..5bb3364a6284 100644
  19711. --- a/kernel/rcu/rcutorture.c
  19712. +++ b/kernel/rcu/rcutorture.c
  19713. @@ -390,6 +390,7 @@ static struct rcu_torture_ops rcu_ops = {
  19714. .name = "rcu"
  19715. };
  19716. +#ifndef CONFIG_PREEMPT_RT_FULL
  19717. /*
  19718. * Definitions for rcu_bh torture testing.
  19719. */
  19720. @@ -429,6 +430,12 @@ static struct rcu_torture_ops rcu_bh_ops = {
  19721. .name = "rcu_bh"
  19722. };
  19723. +#else
  19724. +static struct rcu_torture_ops rcu_bh_ops = {
  19725. + .ttype = INVALID_RCU_FLAVOR,
  19726. +};
  19727. +#endif
  19728. +
  19729. /*
  19730. * Don't even think about trying any of these in real life!!!
  19731. * The names includes "busted", and they really means it!
  19732. diff --git a/kernel/rcu/tree.c b/kernel/rcu/tree.c
  19733. index f07343b54fe5..d862a203fce0 100644
  19734. --- a/kernel/rcu/tree.c
  19735. +++ b/kernel/rcu/tree.c
  19736. @@ -56,6 +56,11 @@
  19737. #include <linux/random.h>
  19738. #include <linux/trace_events.h>
  19739. #include <linux/suspend.h>
  19740. +#include <linux/delay.h>
  19741. +#include <linux/gfp.h>
  19742. +#include <linux/oom.h>
  19743. +#include <linux/smpboot.h>
  19744. +#include "../time/tick-internal.h"
  19745. #include "tree.h"
  19746. #include "rcu.h"
  19747. @@ -266,6 +271,19 @@ void rcu_sched_qs(void)
  19748. }
  19749. }
  19750. +#ifdef CONFIG_PREEMPT_RT_FULL
  19751. +static void rcu_preempt_qs(void);
  19752. +
  19753. +void rcu_bh_qs(void)
  19754. +{
  19755. + unsigned long flags;
  19756. +
  19757. + /* Callers to this function, rcu_preempt_qs(), must disable irqs. */
  19758. + local_irq_save(flags);
  19759. + rcu_preempt_qs();
  19760. + local_irq_restore(flags);
  19761. +}
  19762. +#else
  19763. void rcu_bh_qs(void)
  19764. {
  19765. if (__this_cpu_read(rcu_bh_data.cpu_no_qs.s)) {
  19766. @@ -275,6 +293,7 @@ void rcu_bh_qs(void)
  19767. __this_cpu_write(rcu_bh_data.cpu_no_qs.b.norm, false);
  19768. }
  19769. }
  19770. +#endif
  19771. static DEFINE_PER_CPU(int, rcu_sched_qs_mask);
  19772. @@ -435,11 +454,13 @@ EXPORT_SYMBOL_GPL(rcu_batches_started_sched);
  19773. /*
  19774. * Return the number of RCU BH batches started thus far for debug & stats.
  19775. */
  19776. +#ifndef CONFIG_PREEMPT_RT_FULL
  19777. unsigned long rcu_batches_started_bh(void)
  19778. {
  19779. return rcu_bh_state.gpnum;
  19780. }
  19781. EXPORT_SYMBOL_GPL(rcu_batches_started_bh);
  19782. +#endif
  19783. /*
  19784. * Return the number of RCU batches completed thus far for debug & stats.
  19785. @@ -459,6 +480,7 @@ unsigned long rcu_batches_completed_sched(void)
  19786. }
  19787. EXPORT_SYMBOL_GPL(rcu_batches_completed_sched);
  19788. +#ifndef CONFIG_PREEMPT_RT_FULL
  19789. /*
  19790. * Return the number of RCU BH batches completed thus far for debug & stats.
  19791. */
  19792. @@ -486,6 +508,13 @@ void rcu_bh_force_quiescent_state(void)
  19793. }
  19794. EXPORT_SYMBOL_GPL(rcu_bh_force_quiescent_state);
  19795. +#else
  19796. +void rcu_force_quiescent_state(void)
  19797. +{
  19798. +}
  19799. +EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
  19800. +#endif
  19801. +
  19802. /*
  19803. * Force a quiescent state for RCU-sched.
  19804. */
  19805. @@ -536,9 +565,11 @@ void rcutorture_get_gp_data(enum rcutorture_type test_type, int *flags,
  19806. case RCU_FLAVOR:
  19807. rsp = rcu_state_p;
  19808. break;
  19809. +#ifndef CONFIG_PREEMPT_RT_FULL
  19810. case RCU_BH_FLAVOR:
  19811. rsp = &rcu_bh_state;
  19812. break;
  19813. +#endif
  19814. case RCU_SCHED_FLAVOR:
  19815. rsp = &rcu_sched_state;
  19816. break;
  19817. @@ -1590,7 +1621,6 @@ static int rcu_future_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp)
  19818. int needmore;
  19819. struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
  19820. - rcu_nocb_gp_cleanup(rsp, rnp);
  19821. rnp->need_future_gp[c & 0x1] = 0;
  19822. needmore = rnp->need_future_gp[(c + 1) & 0x1];
  19823. trace_rcu_future_gp(rnp, rdp, c,
  19824. @@ -1611,7 +1641,7 @@ static void rcu_gp_kthread_wake(struct rcu_state *rsp)
  19825. !READ_ONCE(rsp->gp_flags) ||
  19826. !rsp->gp_kthread)
  19827. return;
  19828. - wake_up(&rsp->gp_wq);
  19829. + swake_up(&rsp->gp_wq);
  19830. }
  19831. /*
  19832. @@ -1991,6 +2021,7 @@ static void rcu_gp_cleanup(struct rcu_state *rsp)
  19833. int nocb = 0;
  19834. struct rcu_data *rdp;
  19835. struct rcu_node *rnp = rcu_get_root(rsp);
  19836. + struct swait_queue_head *sq;
  19837. WRITE_ONCE(rsp->gp_activity, jiffies);
  19838. raw_spin_lock_irq(&rnp->lock);
  19839. @@ -2029,7 +2060,9 @@ static void rcu_gp_cleanup(struct rcu_state *rsp)
  19840. needgp = __note_gp_changes(rsp, rnp, rdp) || needgp;
  19841. /* smp_mb() provided by prior unlock-lock pair. */
  19842. nocb += rcu_future_gp_cleanup(rsp, rnp);
  19843. + sq = rcu_nocb_gp_get(rnp);
  19844. raw_spin_unlock_irq(&rnp->lock);
  19845. + rcu_nocb_gp_cleanup(sq);
  19846. cond_resched_rcu_qs();
  19847. WRITE_ONCE(rsp->gp_activity, jiffies);
  19848. rcu_gp_slow(rsp, gp_cleanup_delay);
  19849. @@ -2076,7 +2109,7 @@ static int __noreturn rcu_gp_kthread(void *arg)
  19850. READ_ONCE(rsp->gpnum),
  19851. TPS("reqwait"));
  19852. rsp->gp_state = RCU_GP_WAIT_GPS;
  19853. - wait_event_interruptible(rsp->gp_wq,
  19854. + swait_event_interruptible(rsp->gp_wq,
  19855. READ_ONCE(rsp->gp_flags) &
  19856. RCU_GP_FLAG_INIT);
  19857. rsp->gp_state = RCU_GP_DONE_GPS;
  19858. @@ -2106,7 +2139,7 @@ static int __noreturn rcu_gp_kthread(void *arg)
  19859. READ_ONCE(rsp->gpnum),
  19860. TPS("fqswait"));
  19861. rsp->gp_state = RCU_GP_WAIT_FQS;
  19862. - ret = wait_event_interruptible_timeout(rsp->gp_wq,
  19863. + ret = swait_event_interruptible_timeout(rsp->gp_wq,
  19864. rcu_gp_fqs_check_wake(rsp, &gf), j);
  19865. rsp->gp_state = RCU_GP_DOING_FQS;
  19866. /* Locking provides needed memory barriers. */
  19867. @@ -2230,7 +2263,7 @@ static void rcu_report_qs_rsp(struct rcu_state *rsp, unsigned long flags)
  19868. WARN_ON_ONCE(!rcu_gp_in_progress(rsp));
  19869. WRITE_ONCE(rsp->gp_flags, READ_ONCE(rsp->gp_flags) | RCU_GP_FLAG_FQS);
  19870. raw_spin_unlock_irqrestore(&rcu_get_root(rsp)->lock, flags);
  19871. - rcu_gp_kthread_wake(rsp);
  19872. + swake_up(&rsp->gp_wq); /* Memory barrier implied by swake_up() path. */
  19873. }
  19874. /*
  19875. @@ -2891,7 +2924,7 @@ static void force_quiescent_state(struct rcu_state *rsp)
  19876. }
  19877. WRITE_ONCE(rsp->gp_flags, READ_ONCE(rsp->gp_flags) | RCU_GP_FLAG_FQS);
  19878. raw_spin_unlock_irqrestore(&rnp_old->lock, flags);
  19879. - rcu_gp_kthread_wake(rsp);
  19880. + swake_up(&rsp->gp_wq); /* Memory barrier implied by swake_up() path. */
  19881. }
  19882. /*
  19883. @@ -2934,18 +2967,17 @@ __rcu_process_callbacks(struct rcu_state *rsp)
  19884. /*
  19885. * Do RCU core processing for the current CPU.
  19886. */
  19887. -static void rcu_process_callbacks(struct softirq_action *unused)
  19888. +static void rcu_process_callbacks(void)
  19889. {
  19890. struct rcu_state *rsp;
  19891. if (cpu_is_offline(smp_processor_id()))
  19892. return;
  19893. - trace_rcu_utilization(TPS("Start RCU core"));
  19894. for_each_rcu_flavor(rsp)
  19895. __rcu_process_callbacks(rsp);
  19896. - trace_rcu_utilization(TPS("End RCU core"));
  19897. }
  19898. +static DEFINE_PER_CPU(struct task_struct *, rcu_cpu_kthread_task);
  19899. /*
  19900. * Schedule RCU callback invocation. If the specified type of RCU
  19901. * does not support RCU priority boosting, just do a direct call,
  19902. @@ -2957,18 +2989,105 @@ static void invoke_rcu_callbacks(struct rcu_state *rsp, struct rcu_data *rdp)
  19903. {
  19904. if (unlikely(!READ_ONCE(rcu_scheduler_fully_active)))
  19905. return;
  19906. - if (likely(!rsp->boost)) {
  19907. - rcu_do_batch(rsp, rdp);
  19908. - return;
  19909. - }
  19910. - invoke_rcu_callbacks_kthread();
  19911. + rcu_do_batch(rsp, rdp);
  19912. }
  19913. +static void rcu_wake_cond(struct task_struct *t, int status)
  19914. +{
  19915. + /*
  19916. + * If the thread is yielding, only wake it when this
  19917. + * is invoked from idle
  19918. + */
  19919. + if (t && (status != RCU_KTHREAD_YIELDING || is_idle_task(current)))
  19920. + wake_up_process(t);
  19921. +}
  19922. +
  19923. +/*
  19924. + * Wake up this CPU's rcuc kthread to do RCU core processing.
  19925. + */
  19926. static void invoke_rcu_core(void)
  19927. {
  19928. - if (cpu_online(smp_processor_id()))
  19929. - raise_softirq(RCU_SOFTIRQ);
  19930. + unsigned long flags;
  19931. + struct task_struct *t;
  19932. +
  19933. + if (!cpu_online(smp_processor_id()))
  19934. + return;
  19935. + local_irq_save(flags);
  19936. + __this_cpu_write(rcu_cpu_has_work, 1);
  19937. + t = __this_cpu_read(rcu_cpu_kthread_task);
  19938. + if (t != NULL && current != t)
  19939. + rcu_wake_cond(t, __this_cpu_read(rcu_cpu_kthread_status));
  19940. + local_irq_restore(flags);
  19941. +}
  19942. +
  19943. +static void rcu_cpu_kthread_park(unsigned int cpu)
  19944. +{
  19945. + per_cpu(rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
  19946. +}
  19947. +
  19948. +static int rcu_cpu_kthread_should_run(unsigned int cpu)
  19949. +{
  19950. + return __this_cpu_read(rcu_cpu_has_work);
  19951. +}
  19952. +
  19953. +/*
  19954. + * Per-CPU kernel thread that invokes RCU callbacks. This replaces the
  19955. + * RCU softirq used in flavors and configurations of RCU that do not
  19956. + * support RCU priority boosting.
  19957. + */
  19958. +static void rcu_cpu_kthread(unsigned int cpu)
  19959. +{
  19960. + unsigned int *statusp = this_cpu_ptr(&rcu_cpu_kthread_status);
  19961. + char work, *workp = this_cpu_ptr(&rcu_cpu_has_work);
  19962. + int spincnt;
  19963. +
  19964. + for (spincnt = 0; spincnt < 10; spincnt++) {
  19965. + trace_rcu_utilization(TPS("Start CPU kthread@rcu_wait"));
  19966. + local_bh_disable();
  19967. + *statusp = RCU_KTHREAD_RUNNING;
  19968. + this_cpu_inc(rcu_cpu_kthread_loops);
  19969. + local_irq_disable();
  19970. + work = *workp;
  19971. + *workp = 0;
  19972. + local_irq_enable();
  19973. + if (work)
  19974. + rcu_process_callbacks();
  19975. + local_bh_enable();
  19976. + if (*workp == 0) {
  19977. + trace_rcu_utilization(TPS("End CPU kthread@rcu_wait"));
  19978. + *statusp = RCU_KTHREAD_WAITING;
  19979. + return;
  19980. + }
  19981. + }
  19982. + *statusp = RCU_KTHREAD_YIELDING;
  19983. + trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield"));
  19984. + schedule_timeout_interruptible(2);
  19985. + trace_rcu_utilization(TPS("End CPU kthread@rcu_yield"));
  19986. + *statusp = RCU_KTHREAD_WAITING;
  19987. +}
  19988. +
  19989. +static struct smp_hotplug_thread rcu_cpu_thread_spec = {
  19990. + .store = &rcu_cpu_kthread_task,
  19991. + .thread_should_run = rcu_cpu_kthread_should_run,
  19992. + .thread_fn = rcu_cpu_kthread,
  19993. + .thread_comm = "rcuc/%u",
  19994. + .setup = rcu_cpu_kthread_setup,
  19995. + .park = rcu_cpu_kthread_park,
  19996. +};
  19997. +
  19998. +/*
  19999. + * Spawn per-CPU RCU core processing kthreads.
  20000. + */
  20001. +static int __init rcu_spawn_core_kthreads(void)
  20002. +{
  20003. + int cpu;
  20004. +
  20005. + for_each_possible_cpu(cpu)
  20006. + per_cpu(rcu_cpu_has_work, cpu) = 0;
  20007. + BUG_ON(smpboot_register_percpu_thread(&rcu_cpu_thread_spec));
  20008. + return 0;
  20009. }
  20010. +early_initcall(rcu_spawn_core_kthreads);
  20011. /*
  20012. * Handle any core-RCU processing required by a call_rcu() invocation.
  20013. @@ -3114,6 +3233,7 @@ void call_rcu_sched(struct rcu_head *head, rcu_callback_t func)
  20014. }
  20015. EXPORT_SYMBOL_GPL(call_rcu_sched);
  20016. +#ifndef CONFIG_PREEMPT_RT_FULL
  20017. /*
  20018. * Queue an RCU callback for invocation after a quicker grace period.
  20019. */
  20020. @@ -3122,6 +3242,7 @@ void call_rcu_bh(struct rcu_head *head, rcu_callback_t func)
  20021. __call_rcu(head, func, &rcu_bh_state, -1, 0);
  20022. }
  20023. EXPORT_SYMBOL_GPL(call_rcu_bh);
  20024. +#endif
  20025. /*
  20026. * Queue an RCU callback for lazy invocation after a grace period.
  20027. @@ -3213,6 +3334,7 @@ void synchronize_sched(void)
  20028. }
  20029. EXPORT_SYMBOL_GPL(synchronize_sched);
  20030. +#ifndef CONFIG_PREEMPT_RT_FULL
  20031. /**
  20032. * synchronize_rcu_bh - wait until an rcu_bh grace period has elapsed.
  20033. *
  20034. @@ -3239,6 +3361,7 @@ void synchronize_rcu_bh(void)
  20035. wait_rcu_gp(call_rcu_bh);
  20036. }
  20037. EXPORT_SYMBOL_GPL(synchronize_rcu_bh);
  20038. +#endif
  20039. /**
  20040. * get_state_synchronize_rcu - Snapshot current RCU state
  20041. @@ -3524,7 +3647,7 @@ static void __rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp,
  20042. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  20043. if (wake) {
  20044. smp_mb(); /* EGP done before wake_up(). */
  20045. - wake_up(&rsp->expedited_wq);
  20046. + swake_up(&rsp->expedited_wq);
  20047. }
  20048. break;
  20049. }
  20050. @@ -3781,7 +3904,7 @@ static void synchronize_sched_expedited_wait(struct rcu_state *rsp)
  20051. jiffies_start = jiffies;
  20052. for (;;) {
  20053. - ret = wait_event_interruptible_timeout(
  20054. + ret = swait_event_timeout(
  20055. rsp->expedited_wq,
  20056. sync_rcu_preempt_exp_done(rnp_root),
  20057. jiffies_stall);
  20058. @@ -3789,7 +3912,7 @@ static void synchronize_sched_expedited_wait(struct rcu_state *rsp)
  20059. return;
  20060. if (ret < 0) {
  20061. /* Hit a signal, disable CPU stall warnings. */
  20062. - wait_event(rsp->expedited_wq,
  20063. + swait_event(rsp->expedited_wq,
  20064. sync_rcu_preempt_exp_done(rnp_root));
  20065. return;
  20066. }
  20067. @@ -4101,6 +4224,7 @@ static void _rcu_barrier(struct rcu_state *rsp)
  20068. mutex_unlock(&rsp->barrier_mutex);
  20069. }
  20070. +#ifndef CONFIG_PREEMPT_RT_FULL
  20071. /**
  20072. * rcu_barrier_bh - Wait until all in-flight call_rcu_bh() callbacks complete.
  20073. */
  20074. @@ -4109,6 +4233,7 @@ void rcu_barrier_bh(void)
  20075. _rcu_barrier(&rcu_bh_state);
  20076. }
  20077. EXPORT_SYMBOL_GPL(rcu_barrier_bh);
  20078. +#endif
  20079. /**
  20080. * rcu_barrier_sched - Wait for in-flight call_rcu_sched() callbacks.
  20081. @@ -4455,8 +4580,8 @@ static void __init rcu_init_one(struct rcu_state *rsp,
  20082. }
  20083. }
  20084. - init_waitqueue_head(&rsp->gp_wq);
  20085. - init_waitqueue_head(&rsp->expedited_wq);
  20086. + init_swait_queue_head(&rsp->gp_wq);
  20087. + init_swait_queue_head(&rsp->expedited_wq);
  20088. rnp = rsp->level[rcu_num_lvls - 1];
  20089. for_each_possible_cpu(i) {
  20090. while (i > rnp->grphi)
  20091. @@ -4576,12 +4701,13 @@ void __init rcu_init(void)
  20092. rcu_bootup_announce();
  20093. rcu_init_geometry();
  20094. +#ifndef CONFIG_PREEMPT_RT_FULL
  20095. rcu_init_one(&rcu_bh_state, &rcu_bh_data);
  20096. +#endif
  20097. rcu_init_one(&rcu_sched_state, &rcu_sched_data);
  20098. if (dump_tree)
  20099. rcu_dump_rcu_node_tree(&rcu_sched_state);
  20100. __rcu_init_preempt();
  20101. - open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
  20102. /*
  20103. * We don't need protection against CPU-hotplug here because
  20104. diff --git a/kernel/rcu/tree.h b/kernel/rcu/tree.h
  20105. index 9fb4e238d4dc..c75834d8de24 100644
  20106. --- a/kernel/rcu/tree.h
  20107. +++ b/kernel/rcu/tree.h
  20108. @@ -27,6 +27,7 @@
  20109. #include <linux/threads.h>
  20110. #include <linux/cpumask.h>
  20111. #include <linux/seqlock.h>
  20112. +#include <linux/swait.h>
  20113. #include <linux/stop_machine.h>
  20114. /*
  20115. @@ -241,7 +242,7 @@ struct rcu_node {
  20116. /* Refused to boost: not sure why, though. */
  20117. /* This can happen due to race conditions. */
  20118. #ifdef CONFIG_RCU_NOCB_CPU
  20119. - wait_queue_head_t nocb_gp_wq[2];
  20120. + struct swait_queue_head nocb_gp_wq[2];
  20121. /* Place for rcu_nocb_kthread() to wait GP. */
  20122. #endif /* #ifdef CONFIG_RCU_NOCB_CPU */
  20123. int need_future_gp[2];
  20124. @@ -393,7 +394,7 @@ struct rcu_data {
  20125. atomic_long_t nocb_q_count_lazy; /* invocation (all stages). */
  20126. struct rcu_head *nocb_follower_head; /* CBs ready to invoke. */
  20127. struct rcu_head **nocb_follower_tail;
  20128. - wait_queue_head_t nocb_wq; /* For nocb kthreads to sleep on. */
  20129. + struct swait_queue_head nocb_wq; /* For nocb kthreads to sleep on. */
  20130. struct task_struct *nocb_kthread;
  20131. int nocb_defer_wakeup; /* Defer wakeup of nocb_kthread. */
  20132. @@ -472,7 +473,7 @@ struct rcu_state {
  20133. unsigned long gpnum; /* Current gp number. */
  20134. unsigned long completed; /* # of last completed gp. */
  20135. struct task_struct *gp_kthread; /* Task for grace periods. */
  20136. - wait_queue_head_t gp_wq; /* Where GP task waits. */
  20137. + struct swait_queue_head gp_wq; /* Where GP task waits. */
  20138. short gp_flags; /* Commands for GP task. */
  20139. short gp_state; /* GP kthread sleep state. */
  20140. @@ -504,7 +505,7 @@ struct rcu_state {
  20141. atomic_long_t expedited_workdone3; /* # done by others #3. */
  20142. atomic_long_t expedited_normal; /* # fallbacks to normal. */
  20143. atomic_t expedited_need_qs; /* # CPUs left to check in. */
  20144. - wait_queue_head_t expedited_wq; /* Wait for check-ins. */
  20145. + struct swait_queue_head expedited_wq; /* Wait for check-ins. */
  20146. int ncpus_snap; /* # CPUs seen last time. */
  20147. unsigned long jiffies_force_qs; /* Time at which to invoke */
  20148. @@ -556,18 +557,18 @@ extern struct list_head rcu_struct_flavors;
  20149. */
  20150. extern struct rcu_state rcu_sched_state;
  20151. +#ifndef CONFIG_PREEMPT_RT_FULL
  20152. extern struct rcu_state rcu_bh_state;
  20153. +#endif
  20154. #ifdef CONFIG_PREEMPT_RCU
  20155. extern struct rcu_state rcu_preempt_state;
  20156. #endif /* #ifdef CONFIG_PREEMPT_RCU */
  20157. -#ifdef CONFIG_RCU_BOOST
  20158. DECLARE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
  20159. DECLARE_PER_CPU(int, rcu_cpu_kthread_cpu);
  20160. DECLARE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
  20161. DECLARE_PER_CPU(char, rcu_cpu_has_work);
  20162. -#endif /* #ifdef CONFIG_RCU_BOOST */
  20163. #ifndef RCU_TREE_NONCORE
  20164. @@ -587,10 +588,9 @@ void call_rcu(struct rcu_head *head, rcu_callback_t func);
  20165. static void __init __rcu_init_preempt(void);
  20166. static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags);
  20167. static void rcu_preempt_boost_start_gp(struct rcu_node *rnp);
  20168. -static void invoke_rcu_callbacks_kthread(void);
  20169. static bool rcu_is_callbacks_kthread(void);
  20170. +static void rcu_cpu_kthread_setup(unsigned int cpu);
  20171. #ifdef CONFIG_RCU_BOOST
  20172. -static void rcu_preempt_do_callbacks(void);
  20173. static int rcu_spawn_one_boost_kthread(struct rcu_state *rsp,
  20174. struct rcu_node *rnp);
  20175. #endif /* #ifdef CONFIG_RCU_BOOST */
  20176. @@ -607,7 +607,8 @@ static void zero_cpu_stall_ticks(struct rcu_data *rdp);
  20177. static void increment_cpu_stall_ticks(void);
  20178. static bool rcu_nocb_cpu_needs_barrier(struct rcu_state *rsp, int cpu);
  20179. static void rcu_nocb_gp_set(struct rcu_node *rnp, int nrq);
  20180. -static void rcu_nocb_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp);
  20181. +static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp);
  20182. +static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq);
  20183. static void rcu_init_one_nocb(struct rcu_node *rnp);
  20184. static bool __call_rcu_nocb(struct rcu_data *rdp, struct rcu_head *rhp,
  20185. bool lazy, unsigned long flags);
  20186. diff --git a/kernel/rcu/tree_plugin.h b/kernel/rcu/tree_plugin.h
  20187. index 32cbe72bf545..45e3e3e02a5c 100644
  20188. --- a/kernel/rcu/tree_plugin.h
  20189. +++ b/kernel/rcu/tree_plugin.h
  20190. @@ -24,25 +24,10 @@
  20191. * Paul E. McKenney <paulmck@linux.vnet.ibm.com>
  20192. */
  20193. -#include <linux/delay.h>
  20194. -#include <linux/gfp.h>
  20195. -#include <linux/oom.h>
  20196. -#include <linux/smpboot.h>
  20197. -#include "../time/tick-internal.h"
  20198. -
  20199. #ifdef CONFIG_RCU_BOOST
  20200. #include "../locking/rtmutex_common.h"
  20201. -/*
  20202. - * Control variables for per-CPU and per-rcu_node kthreads. These
  20203. - * handle all flavors of RCU.
  20204. - */
  20205. -static DEFINE_PER_CPU(struct task_struct *, rcu_cpu_kthread_task);
  20206. -DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
  20207. -DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
  20208. -DEFINE_PER_CPU(char, rcu_cpu_has_work);
  20209. -
  20210. #else /* #ifdef CONFIG_RCU_BOOST */
  20211. /*
  20212. @@ -55,6 +40,14 @@ DEFINE_PER_CPU(char, rcu_cpu_has_work);
  20213. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  20214. +/*
  20215. + * Control variables for per-CPU and per-rcu_node kthreads. These
  20216. + * handle all flavors of RCU.
  20217. + */
  20218. +DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
  20219. +DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
  20220. +DEFINE_PER_CPU(char, rcu_cpu_has_work);
  20221. +
  20222. #ifdef CONFIG_RCU_NOCB_CPU
  20223. static cpumask_var_t rcu_nocb_mask; /* CPUs to have callbacks offloaded. */
  20224. static bool have_rcu_nocb_mask; /* Was rcu_nocb_mask allocated? */
  20225. @@ -432,7 +425,7 @@ void rcu_read_unlock_special(struct task_struct *t)
  20226. }
  20227. /* Hardware IRQ handlers cannot block, complain if they get here. */
  20228. - if (in_irq() || in_serving_softirq()) {
  20229. + if (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_OFFSET)) {
  20230. lockdep_rcu_suspicious(__FILE__, __LINE__,
  20231. "rcu_read_unlock() from irq or softirq with blocking in critical section!!!\n");
  20232. pr_alert("->rcu_read_unlock_special: %#x (b: %d, enq: %d nq: %d)\n",
  20233. @@ -645,15 +638,6 @@ static void rcu_preempt_check_callbacks(void)
  20234. t->rcu_read_unlock_special.b.need_qs = true;
  20235. }
  20236. -#ifdef CONFIG_RCU_BOOST
  20237. -
  20238. -static void rcu_preempt_do_callbacks(void)
  20239. -{
  20240. - rcu_do_batch(rcu_state_p, this_cpu_ptr(rcu_data_p));
  20241. -}
  20242. -
  20243. -#endif /* #ifdef CONFIG_RCU_BOOST */
  20244. -
  20245. /*
  20246. * Queue a preemptible-RCU callback for invocation after a grace period.
  20247. */
  20248. @@ -930,6 +914,19 @@ void exit_rcu(void)
  20249. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  20250. +/*
  20251. + * If boosting, set rcuc kthreads to realtime priority.
  20252. + */
  20253. +static void rcu_cpu_kthread_setup(unsigned int cpu)
  20254. +{
  20255. +#ifdef CONFIG_RCU_BOOST
  20256. + struct sched_param sp;
  20257. +
  20258. + sp.sched_priority = kthread_prio;
  20259. + sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
  20260. +#endif /* #ifdef CONFIG_RCU_BOOST */
  20261. +}
  20262. +
  20263. #ifdef CONFIG_RCU_BOOST
  20264. #include "../locking/rtmutex_common.h"
  20265. @@ -961,16 +958,6 @@ static void rcu_initiate_boost_trace(struct rcu_node *rnp)
  20266. #endif /* #else #ifdef CONFIG_RCU_TRACE */
  20267. -static void rcu_wake_cond(struct task_struct *t, int status)
  20268. -{
  20269. - /*
  20270. - * If the thread is yielding, only wake it when this
  20271. - * is invoked from idle
  20272. - */
  20273. - if (status != RCU_KTHREAD_YIELDING || is_idle_task(current))
  20274. - wake_up_process(t);
  20275. -}
  20276. -
  20277. /*
  20278. * Carry out RCU priority boosting on the task indicated by ->exp_tasks
  20279. * or ->boost_tasks, advancing the pointer to the next task in the
  20280. @@ -1115,23 +1102,6 @@ static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
  20281. }
  20282. /*
  20283. - * Wake up the per-CPU kthread to invoke RCU callbacks.
  20284. - */
  20285. -static void invoke_rcu_callbacks_kthread(void)
  20286. -{
  20287. - unsigned long flags;
  20288. -
  20289. - local_irq_save(flags);
  20290. - __this_cpu_write(rcu_cpu_has_work, 1);
  20291. - if (__this_cpu_read(rcu_cpu_kthread_task) != NULL &&
  20292. - current != __this_cpu_read(rcu_cpu_kthread_task)) {
  20293. - rcu_wake_cond(__this_cpu_read(rcu_cpu_kthread_task),
  20294. - __this_cpu_read(rcu_cpu_kthread_status));
  20295. - }
  20296. - local_irq_restore(flags);
  20297. -}
  20298. -
  20299. -/*
  20300. * Is the current CPU running the RCU-callbacks kthread?
  20301. * Caller must have preemption disabled.
  20302. */
  20303. @@ -1186,67 +1156,6 @@ static int rcu_spawn_one_boost_kthread(struct rcu_state *rsp,
  20304. return 0;
  20305. }
  20306. -static void rcu_kthread_do_work(void)
  20307. -{
  20308. - rcu_do_batch(&rcu_sched_state, this_cpu_ptr(&rcu_sched_data));
  20309. - rcu_do_batch(&rcu_bh_state, this_cpu_ptr(&rcu_bh_data));
  20310. - rcu_preempt_do_callbacks();
  20311. -}
  20312. -
  20313. -static void rcu_cpu_kthread_setup(unsigned int cpu)
  20314. -{
  20315. - struct sched_param sp;
  20316. -
  20317. - sp.sched_priority = kthread_prio;
  20318. - sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
  20319. -}
  20320. -
  20321. -static void rcu_cpu_kthread_park(unsigned int cpu)
  20322. -{
  20323. - per_cpu(rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
  20324. -}
  20325. -
  20326. -static int rcu_cpu_kthread_should_run(unsigned int cpu)
  20327. -{
  20328. - return __this_cpu_read(rcu_cpu_has_work);
  20329. -}
  20330. -
  20331. -/*
  20332. - * Per-CPU kernel thread that invokes RCU callbacks. This replaces the
  20333. - * RCU softirq used in flavors and configurations of RCU that do not
  20334. - * support RCU priority boosting.
  20335. - */
  20336. -static void rcu_cpu_kthread(unsigned int cpu)
  20337. -{
  20338. - unsigned int *statusp = this_cpu_ptr(&rcu_cpu_kthread_status);
  20339. - char work, *workp = this_cpu_ptr(&rcu_cpu_has_work);
  20340. - int spincnt;
  20341. -
  20342. - for (spincnt = 0; spincnt < 10; spincnt++) {
  20343. - trace_rcu_utilization(TPS("Start CPU kthread@rcu_wait"));
  20344. - local_bh_disable();
  20345. - *statusp = RCU_KTHREAD_RUNNING;
  20346. - this_cpu_inc(rcu_cpu_kthread_loops);
  20347. - local_irq_disable();
  20348. - work = *workp;
  20349. - *workp = 0;
  20350. - local_irq_enable();
  20351. - if (work)
  20352. - rcu_kthread_do_work();
  20353. - local_bh_enable();
  20354. - if (*workp == 0) {
  20355. - trace_rcu_utilization(TPS("End CPU kthread@rcu_wait"));
  20356. - *statusp = RCU_KTHREAD_WAITING;
  20357. - return;
  20358. - }
  20359. - }
  20360. - *statusp = RCU_KTHREAD_YIELDING;
  20361. - trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield"));
  20362. - schedule_timeout_interruptible(2);
  20363. - trace_rcu_utilization(TPS("End CPU kthread@rcu_yield"));
  20364. - *statusp = RCU_KTHREAD_WAITING;
  20365. -}
  20366. -
  20367. /*
  20368. * Set the per-rcu_node kthread's affinity to cover all CPUs that are
  20369. * served by the rcu_node in question. The CPU hotplug lock is still
  20370. @@ -1276,26 +1185,12 @@ static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
  20371. free_cpumask_var(cm);
  20372. }
  20373. -static struct smp_hotplug_thread rcu_cpu_thread_spec = {
  20374. - .store = &rcu_cpu_kthread_task,
  20375. - .thread_should_run = rcu_cpu_kthread_should_run,
  20376. - .thread_fn = rcu_cpu_kthread,
  20377. - .thread_comm = "rcuc/%u",
  20378. - .setup = rcu_cpu_kthread_setup,
  20379. - .park = rcu_cpu_kthread_park,
  20380. -};
  20381. -
  20382. /*
  20383. * Spawn boost kthreads -- called as soon as the scheduler is running.
  20384. */
  20385. static void __init rcu_spawn_boost_kthreads(void)
  20386. {
  20387. struct rcu_node *rnp;
  20388. - int cpu;
  20389. -
  20390. - for_each_possible_cpu(cpu)
  20391. - per_cpu(rcu_cpu_has_work, cpu) = 0;
  20392. - BUG_ON(smpboot_register_percpu_thread(&rcu_cpu_thread_spec));
  20393. rcu_for_each_leaf_node(rcu_state_p, rnp)
  20394. (void)rcu_spawn_one_boost_kthread(rcu_state_p, rnp);
  20395. }
  20396. @@ -1318,11 +1213,6 @@ static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
  20397. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  20398. }
  20399. -static void invoke_rcu_callbacks_kthread(void)
  20400. -{
  20401. - WARN_ON_ONCE(1);
  20402. -}
  20403. -
  20404. static bool rcu_is_callbacks_kthread(void)
  20405. {
  20406. return false;
  20407. @@ -1346,7 +1236,7 @@ static void rcu_prepare_kthreads(int cpu)
  20408. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  20409. -#if !defined(CONFIG_RCU_FAST_NO_HZ)
  20410. +#if !defined(CONFIG_RCU_FAST_NO_HZ) || defined(CONFIG_PREEMPT_RT_FULL)
  20411. /*
  20412. * Check to see if any future RCU-related work will need to be done
  20413. @@ -1363,7 +1253,9 @@ int rcu_needs_cpu(u64 basemono, u64 *nextevt)
  20414. return IS_ENABLED(CONFIG_RCU_NOCB_CPU_ALL)
  20415. ? 0 : rcu_cpu_has_callbacks(NULL);
  20416. }
  20417. +#endif /* !defined(CONFIG_RCU_FAST_NO_HZ) || defined(CONFIG_PREEMPT_RT_FULL) */
  20418. +#if !defined(CONFIG_RCU_FAST_NO_HZ)
  20419. /*
  20420. * Because we do not have RCU_FAST_NO_HZ, don't bother cleaning up
  20421. * after it.
  20422. @@ -1459,6 +1351,8 @@ static bool __maybe_unused rcu_try_advance_all_cbs(void)
  20423. return cbs_ready;
  20424. }
  20425. +#ifndef CONFIG_PREEMPT_RT_FULL
  20426. +
  20427. /*
  20428. * Allow the CPU to enter dyntick-idle mode unless it has callbacks ready
  20429. * to invoke. If the CPU has callbacks, try to advance them. Tell the
  20430. @@ -1504,6 +1398,7 @@ int rcu_needs_cpu(u64 basemono, u64 *nextevt)
  20431. *nextevt = basemono + dj * TICK_NSEC;
  20432. return 0;
  20433. }
  20434. +#endif /* #ifndef CONFIG_PREEMPT_RT_FULL */
  20435. /*
  20436. * Prepare a CPU for idle from an RCU perspective. The first major task
  20437. @@ -1822,9 +1717,9 @@ early_param("rcu_nocb_poll", parse_rcu_nocb_poll);
  20438. * Wake up any no-CBs CPUs' kthreads that were waiting on the just-ended
  20439. * grace period.
  20440. */
  20441. -static void rcu_nocb_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp)
  20442. +static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq)
  20443. {
  20444. - wake_up_all(&rnp->nocb_gp_wq[rnp->completed & 0x1]);
  20445. + swake_up_all(sq);
  20446. }
  20447. /*
  20448. @@ -1840,10 +1735,15 @@ static void rcu_nocb_gp_set(struct rcu_node *rnp, int nrq)
  20449. rnp->need_future_gp[(rnp->completed + 1) & 0x1] += nrq;
  20450. }
  20451. +static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp)
  20452. +{
  20453. + return &rnp->nocb_gp_wq[rnp->completed & 0x1];
  20454. +}
  20455. +
  20456. static void rcu_init_one_nocb(struct rcu_node *rnp)
  20457. {
  20458. - init_waitqueue_head(&rnp->nocb_gp_wq[0]);
  20459. - init_waitqueue_head(&rnp->nocb_gp_wq[1]);
  20460. + init_swait_queue_head(&rnp->nocb_gp_wq[0]);
  20461. + init_swait_queue_head(&rnp->nocb_gp_wq[1]);
  20462. }
  20463. #ifndef CONFIG_RCU_NOCB_CPU_ALL
  20464. @@ -1868,7 +1768,7 @@ static void wake_nocb_leader(struct rcu_data *rdp, bool force)
  20465. if (READ_ONCE(rdp_leader->nocb_leader_sleep) || force) {
  20466. /* Prior smp_mb__after_atomic() orders against prior enqueue. */
  20467. WRITE_ONCE(rdp_leader->nocb_leader_sleep, false);
  20468. - wake_up(&rdp_leader->nocb_wq);
  20469. + swake_up(&rdp_leader->nocb_wq);
  20470. }
  20471. }
  20472. @@ -2081,7 +1981,7 @@ static void rcu_nocb_wait_gp(struct rcu_data *rdp)
  20473. */
  20474. trace_rcu_future_gp(rnp, rdp, c, TPS("StartWait"));
  20475. for (;;) {
  20476. - wait_event_interruptible(
  20477. + swait_event_interruptible(
  20478. rnp->nocb_gp_wq[c & 0x1],
  20479. (d = ULONG_CMP_GE(READ_ONCE(rnp->completed), c)));
  20480. if (likely(d))
  20481. @@ -2109,7 +2009,7 @@ wait_again:
  20482. /* Wait for callbacks to appear. */
  20483. if (!rcu_nocb_poll) {
  20484. trace_rcu_nocb_wake(my_rdp->rsp->name, my_rdp->cpu, "Sleep");
  20485. - wait_event_interruptible(my_rdp->nocb_wq,
  20486. + swait_event_interruptible(my_rdp->nocb_wq,
  20487. !READ_ONCE(my_rdp->nocb_leader_sleep));
  20488. /* Memory barrier handled by smp_mb() calls below and repoll. */
  20489. } else if (firsttime) {
  20490. @@ -2184,7 +2084,7 @@ wait_again:
  20491. * List was empty, wake up the follower.
  20492. * Memory barriers supplied by atomic_long_add().
  20493. */
  20494. - wake_up(&rdp->nocb_wq);
  20495. + swake_up(&rdp->nocb_wq);
  20496. }
  20497. }
  20498. @@ -2205,7 +2105,7 @@ static void nocb_follower_wait(struct rcu_data *rdp)
  20499. if (!rcu_nocb_poll) {
  20500. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  20501. "FollowerSleep");
  20502. - wait_event_interruptible(rdp->nocb_wq,
  20503. + swait_event_interruptible(rdp->nocb_wq,
  20504. READ_ONCE(rdp->nocb_follower_head));
  20505. } else if (firsttime) {
  20506. /* Don't drown trace log with "Poll"! */
  20507. @@ -2365,7 +2265,7 @@ void __init rcu_init_nohz(void)
  20508. static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
  20509. {
  20510. rdp->nocb_tail = &rdp->nocb_head;
  20511. - init_waitqueue_head(&rdp->nocb_wq);
  20512. + init_swait_queue_head(&rdp->nocb_wq);
  20513. rdp->nocb_follower_tail = &rdp->nocb_follower_head;
  20514. }
  20515. @@ -2515,7 +2415,7 @@ static bool rcu_nocb_cpu_needs_barrier(struct rcu_state *rsp, int cpu)
  20516. return false;
  20517. }
  20518. -static void rcu_nocb_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp)
  20519. +static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq)
  20520. {
  20521. }
  20522. @@ -2523,6 +2423,11 @@ static void rcu_nocb_gp_set(struct rcu_node *rnp, int nrq)
  20523. {
  20524. }
  20525. +static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp)
  20526. +{
  20527. + return NULL;
  20528. +}
  20529. +
  20530. static void rcu_init_one_nocb(struct rcu_node *rnp)
  20531. {
  20532. }
  20533. diff --git a/kernel/rcu/update.c b/kernel/rcu/update.c
  20534. index 5f748c5a40f0..9a3904603ff6 100644
  20535. --- a/kernel/rcu/update.c
  20536. +++ b/kernel/rcu/update.c
  20537. @@ -276,6 +276,7 @@ int rcu_read_lock_held(void)
  20538. }
  20539. EXPORT_SYMBOL_GPL(rcu_read_lock_held);
  20540. +#ifndef CONFIG_PREEMPT_RT_FULL
  20541. /**
  20542. * rcu_read_lock_bh_held() - might we be in RCU-bh read-side critical section?
  20543. *
  20544. @@ -302,6 +303,7 @@ int rcu_read_lock_bh_held(void)
  20545. return in_softirq() || irqs_disabled();
  20546. }
  20547. EXPORT_SYMBOL_GPL(rcu_read_lock_bh_held);
  20548. +#endif
  20549. #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  20550. diff --git a/kernel/relay.c b/kernel/relay.c
  20551. index 0b4570cfacae..60684be39f22 100644
  20552. --- a/kernel/relay.c
  20553. +++ b/kernel/relay.c
  20554. @@ -336,6 +336,10 @@ static void wakeup_readers(unsigned long data)
  20555. {
  20556. struct rchan_buf *buf = (struct rchan_buf *)data;
  20557. wake_up_interruptible(&buf->read_wait);
  20558. + /*
  20559. + * Stupid polling for now:
  20560. + */
  20561. + mod_timer(&buf->timer, jiffies + 1);
  20562. }
  20563. /**
  20564. @@ -353,6 +357,7 @@ static void __relay_reset(struct rchan_buf *buf, unsigned int init)
  20565. init_waitqueue_head(&buf->read_wait);
  20566. kref_init(&buf->kref);
  20567. setup_timer(&buf->timer, wakeup_readers, (unsigned long)buf);
  20568. + mod_timer(&buf->timer, jiffies + 1);
  20569. } else
  20570. del_timer_sync(&buf->timer);
  20571. @@ -736,15 +741,6 @@ size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)
  20572. else
  20573. buf->early_bytes += buf->chan->subbuf_size -
  20574. buf->padding[old_subbuf];
  20575. - smp_mb();
  20576. - if (waitqueue_active(&buf->read_wait))
  20577. - /*
  20578. - * Calling wake_up_interruptible() from here
  20579. - * will deadlock if we happen to be logging
  20580. - * from the scheduler (trying to re-grab
  20581. - * rq->lock), so defer it.
  20582. - */
  20583. - mod_timer(&buf->timer, jiffies + 1);
  20584. }
  20585. old = buf->data;
  20586. diff --git a/kernel/sched/Makefile b/kernel/sched/Makefile
  20587. index 67687973ce80..01b9994b367a 100644
  20588. --- a/kernel/sched/Makefile
  20589. +++ b/kernel/sched/Makefile
  20590. @@ -13,7 +13,7 @@ endif
  20591. obj-y += core.o loadavg.o clock.o cputime.o
  20592. obj-y += idle_task.o fair.o rt.o deadline.o stop_task.o
  20593. -obj-y += wait.o completion.o idle.o
  20594. +obj-y += wait.o swait.o swork.o completion.o idle.o
  20595. obj-$(CONFIG_SMP) += cpupri.o cpudeadline.o
  20596. obj-$(CONFIG_SCHED_AUTOGROUP) += auto_group.o
  20597. obj-$(CONFIG_SCHEDSTATS) += stats.o
  20598. diff --git a/kernel/sched/completion.c b/kernel/sched/completion.c
  20599. index 8d0f35debf35..b62cf6400fe0 100644
  20600. --- a/kernel/sched/completion.c
  20601. +++ b/kernel/sched/completion.c
  20602. @@ -30,10 +30,10 @@ void complete(struct completion *x)
  20603. {
  20604. unsigned long flags;
  20605. - spin_lock_irqsave(&x->wait.lock, flags);
  20606. + raw_spin_lock_irqsave(&x->wait.lock, flags);
  20607. x->done++;
  20608. - __wake_up_locked(&x->wait, TASK_NORMAL, 1);
  20609. - spin_unlock_irqrestore(&x->wait.lock, flags);
  20610. + swake_up_locked(&x->wait);
  20611. + raw_spin_unlock_irqrestore(&x->wait.lock, flags);
  20612. }
  20613. EXPORT_SYMBOL(complete);
  20614. @@ -50,10 +50,10 @@ void complete_all(struct completion *x)
  20615. {
  20616. unsigned long flags;
  20617. - spin_lock_irqsave(&x->wait.lock, flags);
  20618. + raw_spin_lock_irqsave(&x->wait.lock, flags);
  20619. x->done += UINT_MAX/2;
  20620. - __wake_up_locked(&x->wait, TASK_NORMAL, 0);
  20621. - spin_unlock_irqrestore(&x->wait.lock, flags);
  20622. + swake_up_all_locked(&x->wait);
  20623. + raw_spin_unlock_irqrestore(&x->wait.lock, flags);
  20624. }
  20625. EXPORT_SYMBOL(complete_all);
  20626. @@ -62,20 +62,20 @@ do_wait_for_common(struct completion *x,
  20627. long (*action)(long), long timeout, int state)
  20628. {
  20629. if (!x->done) {
  20630. - DECLARE_WAITQUEUE(wait, current);
  20631. + DECLARE_SWAITQUEUE(wait);
  20632. - __add_wait_queue_tail_exclusive(&x->wait, &wait);
  20633. + __prepare_to_swait(&x->wait, &wait);
  20634. do {
  20635. if (signal_pending_state(state, current)) {
  20636. timeout = -ERESTARTSYS;
  20637. break;
  20638. }
  20639. __set_current_state(state);
  20640. - spin_unlock_irq(&x->wait.lock);
  20641. + raw_spin_unlock_irq(&x->wait.lock);
  20642. timeout = action(timeout);
  20643. - spin_lock_irq(&x->wait.lock);
  20644. + raw_spin_lock_irq(&x->wait.lock);
  20645. } while (!x->done && timeout);
  20646. - __remove_wait_queue(&x->wait, &wait);
  20647. + __finish_swait(&x->wait, &wait);
  20648. if (!x->done)
  20649. return timeout;
  20650. }
  20651. @@ -89,9 +89,9 @@ __wait_for_common(struct completion *x,
  20652. {
  20653. might_sleep();
  20654. - spin_lock_irq(&x->wait.lock);
  20655. + raw_spin_lock_irq(&x->wait.lock);
  20656. timeout = do_wait_for_common(x, action, timeout, state);
  20657. - spin_unlock_irq(&x->wait.lock);
  20658. + raw_spin_unlock_irq(&x->wait.lock);
  20659. return timeout;
  20660. }
  20661. @@ -277,12 +277,12 @@ bool try_wait_for_completion(struct completion *x)
  20662. if (!READ_ONCE(x->done))
  20663. return 0;
  20664. - spin_lock_irqsave(&x->wait.lock, flags);
  20665. + raw_spin_lock_irqsave(&x->wait.lock, flags);
  20666. if (!x->done)
  20667. ret = 0;
  20668. else
  20669. x->done--;
  20670. - spin_unlock_irqrestore(&x->wait.lock, flags);
  20671. + raw_spin_unlock_irqrestore(&x->wait.lock, flags);
  20672. return ret;
  20673. }
  20674. EXPORT_SYMBOL(try_wait_for_completion);
  20675. @@ -311,7 +311,7 @@ bool completion_done(struct completion *x)
  20676. * after it's acquired the lock.
  20677. */
  20678. smp_rmb();
  20679. - spin_unlock_wait(&x->wait.lock);
  20680. + raw_spin_unlock_wait(&x->wait.lock);
  20681. return true;
  20682. }
  20683. EXPORT_SYMBOL(completion_done);
  20684. diff --git a/kernel/sched/core.c b/kernel/sched/core.c
  20685. index 20253dbc8610..e9b8d518202e 100644
  20686. --- a/kernel/sched/core.c
  20687. +++ b/kernel/sched/core.c
  20688. @@ -260,7 +260,11 @@ late_initcall(sched_init_debug);
  20689. * Number of tasks to iterate in a single balance run.
  20690. * Limited because this is done with IRQs disabled.
  20691. */
  20692. +#ifndef CONFIG_PREEMPT_RT_FULL
  20693. const_debug unsigned int sysctl_sched_nr_migrate = 32;
  20694. +#else
  20695. +const_debug unsigned int sysctl_sched_nr_migrate = 8;
  20696. +#endif
  20697. /*
  20698. * period over which we average the RT time consumption, measured
  20699. @@ -438,6 +442,7 @@ static void init_rq_hrtick(struct rq *rq)
  20700. hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  20701. rq->hrtick_timer.function = hrtick;
  20702. + rq->hrtick_timer.irqsafe = 1;
  20703. }
  20704. #else /* CONFIG_SCHED_HRTICK */
  20705. static inline void hrtick_clear(struct rq *rq)
  20706. @@ -542,7 +547,7 @@ void wake_q_add(struct wake_q_head *head, struct task_struct *task)
  20707. head->lastp = &node->next;
  20708. }
  20709. -void wake_up_q(struct wake_q_head *head)
  20710. +void __wake_up_q(struct wake_q_head *head, bool sleeper)
  20711. {
  20712. struct wake_q_node *node = head->first;
  20713. @@ -559,7 +564,10 @@ void wake_up_q(struct wake_q_head *head)
  20714. * wake_up_process() implies a wmb() to pair with the queueing
  20715. * in wake_q_add() so as not to miss wakeups.
  20716. */
  20717. - wake_up_process(task);
  20718. + if (sleeper)
  20719. + wake_up_lock_sleeper(task);
  20720. + else
  20721. + wake_up_process(task);
  20722. put_task_struct(task);
  20723. }
  20724. }
  20725. @@ -595,6 +603,38 @@ void resched_curr(struct rq *rq)
  20726. trace_sched_wake_idle_without_ipi(cpu);
  20727. }
  20728. +#ifdef CONFIG_PREEMPT_LAZY
  20729. +void resched_curr_lazy(struct rq *rq)
  20730. +{
  20731. + struct task_struct *curr = rq->curr;
  20732. + int cpu;
  20733. +
  20734. + if (!sched_feat(PREEMPT_LAZY)) {
  20735. + resched_curr(rq);
  20736. + return;
  20737. + }
  20738. +
  20739. + lockdep_assert_held(&rq->lock);
  20740. +
  20741. + if (test_tsk_need_resched(curr))
  20742. + return;
  20743. +
  20744. + if (test_tsk_need_resched_lazy(curr))
  20745. + return;
  20746. +
  20747. + set_tsk_need_resched_lazy(curr);
  20748. +
  20749. + cpu = cpu_of(rq);
  20750. + if (cpu == smp_processor_id())
  20751. + return;
  20752. +
  20753. + /* NEED_RESCHED_LAZY must be visible before we test polling */
  20754. + smp_mb();
  20755. + if (!tsk_is_polling(curr))
  20756. + smp_send_reschedule(cpu);
  20757. +}
  20758. +#endif
  20759. +
  20760. void resched_cpu(int cpu)
  20761. {
  20762. struct rq *rq = cpu_rq(cpu);
  20763. @@ -618,11 +658,14 @@ void resched_cpu(int cpu)
  20764. */
  20765. int get_nohz_timer_target(void)
  20766. {
  20767. - int i, cpu = smp_processor_id();
  20768. + int i, cpu;
  20769. struct sched_domain *sd;
  20770. + preempt_disable_rt();
  20771. + cpu = smp_processor_id();
  20772. +
  20773. if (!idle_cpu(cpu) && is_housekeeping_cpu(cpu))
  20774. - return cpu;
  20775. + goto preempt_en_rt;
  20776. rcu_read_lock();
  20777. for_each_domain(cpu, sd) {
  20778. @@ -641,6 +684,8 @@ int get_nohz_timer_target(void)
  20779. cpu = housekeeping_any_cpu();
  20780. unlock:
  20781. rcu_read_unlock();
  20782. +preempt_en_rt:
  20783. + preempt_enable_rt();
  20784. return cpu;
  20785. }
  20786. /*
  20787. @@ -1174,6 +1219,11 @@ void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
  20788. lockdep_assert_held(&p->pi_lock);
  20789. + if (__migrate_disabled(p)) {
  20790. + cpumask_copy(&p->cpus_allowed, new_mask);
  20791. + return;
  20792. + }
  20793. +
  20794. queued = task_on_rq_queued(p);
  20795. running = task_current(rq, p);
  20796. @@ -1196,6 +1246,84 @@ void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
  20797. enqueue_task(rq, p, ENQUEUE_RESTORE);
  20798. }
  20799. +static DEFINE_PER_CPU(struct cpumask, sched_cpumasks);
  20800. +static DEFINE_MUTEX(sched_down_mutex);
  20801. +static cpumask_t sched_down_cpumask;
  20802. +
  20803. +void tell_sched_cpu_down_begin(int cpu)
  20804. +{
  20805. + mutex_lock(&sched_down_mutex);
  20806. + cpumask_set_cpu(cpu, &sched_down_cpumask);
  20807. + mutex_unlock(&sched_down_mutex);
  20808. +}
  20809. +
  20810. +void tell_sched_cpu_down_done(int cpu)
  20811. +{
  20812. + mutex_lock(&sched_down_mutex);
  20813. + cpumask_clear_cpu(cpu, &sched_down_cpumask);
  20814. + mutex_unlock(&sched_down_mutex);
  20815. +}
  20816. +
  20817. +/**
  20818. + * migrate_me - try to move the current task off this cpu
  20819. + *
  20820. + * Used by the pin_current_cpu() code to try to get tasks
  20821. + * to move off the current CPU as it is going down.
  20822. + * It will only move the task if the task isn't pinned to
  20823. + * the CPU (with migrate_disable, affinity or NO_SETAFFINITY)
  20824. + * and the task has to be in a RUNNING state. Otherwise the
  20825. + * movement of the task will wake it up (change its state
  20826. + * to running) when the task did not expect it.
  20827. + *
  20828. + * Returns 1 if it succeeded in moving the current task
  20829. + * 0 otherwise.
  20830. + */
  20831. +int migrate_me(void)
  20832. +{
  20833. + struct task_struct *p = current;
  20834. + struct migration_arg arg;
  20835. + struct cpumask *cpumask;
  20836. + struct cpumask *mask;
  20837. + unsigned long flags;
  20838. + unsigned int dest_cpu;
  20839. + struct rq *rq;
  20840. +
  20841. + /*
  20842. + * We can not migrate tasks bounded to a CPU or tasks not
  20843. + * running. The movement of the task will wake it up.
  20844. + */
  20845. + if (p->flags & PF_NO_SETAFFINITY || p->state)
  20846. + return 0;
  20847. +
  20848. + mutex_lock(&sched_down_mutex);
  20849. + rq = task_rq_lock(p, &flags);
  20850. +
  20851. + cpumask = this_cpu_ptr(&sched_cpumasks);
  20852. + mask = &p->cpus_allowed;
  20853. +
  20854. + cpumask_andnot(cpumask, mask, &sched_down_cpumask);
  20855. +
  20856. + if (!cpumask_weight(cpumask)) {
  20857. + /* It's only on this CPU? */
  20858. + task_rq_unlock(rq, p, &flags);
  20859. + mutex_unlock(&sched_down_mutex);
  20860. + return 0;
  20861. + }
  20862. +
  20863. + dest_cpu = cpumask_any_and(cpu_active_mask, cpumask);
  20864. +
  20865. + arg.task = p;
  20866. + arg.dest_cpu = dest_cpu;
  20867. +
  20868. + task_rq_unlock(rq, p, &flags);
  20869. +
  20870. + stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
  20871. + tlb_migrate_finish(p->mm);
  20872. + mutex_unlock(&sched_down_mutex);
  20873. +
  20874. + return 1;
  20875. +}
  20876. +
  20877. /*
  20878. * Change a given task's CPU affinity. Migrate the thread to a
  20879. * proper CPU and schedule it away if the CPU it's executing on
  20880. @@ -1235,7 +1363,7 @@ static int __set_cpus_allowed_ptr(struct task_struct *p,
  20881. do_set_cpus_allowed(p, new_mask);
  20882. /* Can the task run on the task's current CPU? If so, we're done */
  20883. - if (cpumask_test_cpu(task_cpu(p), new_mask))
  20884. + if (cpumask_test_cpu(task_cpu(p), new_mask) || __migrate_disabled(p))
  20885. goto out;
  20886. dest_cpu = cpumask_any_and(cpu_active_mask, new_mask);
  20887. @@ -1411,6 +1539,18 @@ out:
  20888. return ret;
  20889. }
  20890. +static bool check_task_state(struct task_struct *p, long match_state)
  20891. +{
  20892. + bool match = false;
  20893. +
  20894. + raw_spin_lock_irq(&p->pi_lock);
  20895. + if (p->state == match_state || p->saved_state == match_state)
  20896. + match = true;
  20897. + raw_spin_unlock_irq(&p->pi_lock);
  20898. +
  20899. + return match;
  20900. +}
  20901. +
  20902. /*
  20903. * wait_task_inactive - wait for a thread to unschedule.
  20904. *
  20905. @@ -1455,7 +1595,7 @@ unsigned long wait_task_inactive(struct task_struct *p, long match_state)
  20906. * is actually now running somewhere else!
  20907. */
  20908. while (task_running(rq, p)) {
  20909. - if (match_state && unlikely(p->state != match_state))
  20910. + if (match_state && !check_task_state(p, match_state))
  20911. return 0;
  20912. cpu_relax();
  20913. }
  20914. @@ -1470,7 +1610,8 @@ unsigned long wait_task_inactive(struct task_struct *p, long match_state)
  20915. running = task_running(rq, p);
  20916. queued = task_on_rq_queued(p);
  20917. ncsw = 0;
  20918. - if (!match_state || p->state == match_state)
  20919. + if (!match_state || p->state == match_state ||
  20920. + p->saved_state == match_state)
  20921. ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
  20922. task_rq_unlock(rq, p, &flags);
  20923. @@ -1627,7 +1768,7 @@ int select_task_rq(struct task_struct *p, int cpu, int sd_flags, int wake_flags)
  20924. {
  20925. lockdep_assert_held(&p->pi_lock);
  20926. - if (p->nr_cpus_allowed > 1)
  20927. + if (tsk_nr_cpus_allowed(p) > 1)
  20928. cpu = p->sched_class->select_task_rq(p, cpu, sd_flags, wake_flags);
  20929. /*
  20930. @@ -1707,10 +1848,6 @@ static inline void ttwu_activate(struct rq *rq, struct task_struct *p, int en_fl
  20931. {
  20932. activate_task(rq, p, en_flags);
  20933. p->on_rq = TASK_ON_RQ_QUEUED;
  20934. -
  20935. - /* if a worker is waking up, notify workqueue */
  20936. - if (p->flags & PF_WQ_WORKER)
  20937. - wq_worker_waking_up(p, cpu_of(rq));
  20938. }
  20939. /*
  20940. @@ -1937,8 +2074,27 @@ try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
  20941. */
  20942. smp_mb__before_spinlock();
  20943. raw_spin_lock_irqsave(&p->pi_lock, flags);
  20944. - if (!(p->state & state))
  20945. + if (!(p->state & state)) {
  20946. + /*
  20947. + * The task might be running due to a spinlock sleeper
  20948. + * wakeup. Check the saved state and set it to running
  20949. + * if the wakeup condition is true.
  20950. + */
  20951. + if (!(wake_flags & WF_LOCK_SLEEPER)) {
  20952. + if (p->saved_state & state) {
  20953. + p->saved_state = TASK_RUNNING;
  20954. + success = 1;
  20955. + }
  20956. + }
  20957. goto out;
  20958. + }
  20959. +
  20960. + /*
  20961. + * If this is a regular wakeup, then we can unconditionally
  20962. + * clear the saved state of a "lock sleeper".
  20963. + */
  20964. + if (!(wake_flags & WF_LOCK_SLEEPER))
  20965. + p->saved_state = TASK_RUNNING;
  20966. trace_sched_waking(p);
  20967. @@ -2030,52 +2186,6 @@ out:
  20968. }
  20969. /**
  20970. - * try_to_wake_up_local - try to wake up a local task with rq lock held
  20971. - * @p: the thread to be awakened
  20972. - *
  20973. - * Put @p on the run-queue if it's not already there. The caller must
  20974. - * ensure that this_rq() is locked, @p is bound to this_rq() and not
  20975. - * the current task.
  20976. - */
  20977. -static void try_to_wake_up_local(struct task_struct *p)
  20978. -{
  20979. - struct rq *rq = task_rq(p);
  20980. -
  20981. - if (WARN_ON_ONCE(rq != this_rq()) ||
  20982. - WARN_ON_ONCE(p == current))
  20983. - return;
  20984. -
  20985. - lockdep_assert_held(&rq->lock);
  20986. -
  20987. - if (!raw_spin_trylock(&p->pi_lock)) {
  20988. - /*
  20989. - * This is OK, because current is on_cpu, which avoids it being
  20990. - * picked for load-balance and preemption/IRQs are still
  20991. - * disabled avoiding further scheduler activity on it and we've
  20992. - * not yet picked a replacement task.
  20993. - */
  20994. - lockdep_unpin_lock(&rq->lock);
  20995. - raw_spin_unlock(&rq->lock);
  20996. - raw_spin_lock(&p->pi_lock);
  20997. - raw_spin_lock(&rq->lock);
  20998. - lockdep_pin_lock(&rq->lock);
  20999. - }
  21000. -
  21001. - if (!(p->state & TASK_NORMAL))
  21002. - goto out;
  21003. -
  21004. - trace_sched_waking(p);
  21005. -
  21006. - if (!task_on_rq_queued(p))
  21007. - ttwu_activate(rq, p, ENQUEUE_WAKEUP);
  21008. -
  21009. - ttwu_do_wakeup(rq, p, 0);
  21010. - ttwu_stat(p, smp_processor_id(), 0);
  21011. -out:
  21012. - raw_spin_unlock(&p->pi_lock);
  21013. -}
  21014. -
  21015. -/**
  21016. * wake_up_process - Wake up a specific process
  21017. * @p: The process to be woken up.
  21018. *
  21019. @@ -2093,6 +2203,18 @@ int wake_up_process(struct task_struct *p)
  21020. }
  21021. EXPORT_SYMBOL(wake_up_process);
  21022. +/**
  21023. + * wake_up_lock_sleeper - Wake up a specific process blocked on a "sleeping lock"
  21024. + * @p: The process to be woken up.
  21025. + *
  21026. + * Same as wake_up_process() above, but wake_flags=WF_LOCK_SLEEPER to indicate
  21027. + * the nature of the wakeup.
  21028. + */
  21029. +int wake_up_lock_sleeper(struct task_struct *p)
  21030. +{
  21031. + return try_to_wake_up(p, TASK_ALL, WF_LOCK_SLEEPER);
  21032. +}
  21033. +
  21034. int wake_up_state(struct task_struct *p, unsigned int state)
  21035. {
  21036. return try_to_wake_up(p, state, 0);
  21037. @@ -2279,6 +2401,9 @@ int sched_fork(unsigned long clone_flags, struct task_struct *p)
  21038. p->on_cpu = 0;
  21039. #endif
  21040. init_task_preempt_count(p);
  21041. +#ifdef CONFIG_HAVE_PREEMPT_LAZY
  21042. + task_thread_info(p)->preempt_lazy_count = 0;
  21043. +#endif
  21044. #ifdef CONFIG_SMP
  21045. plist_node_init(&p->pushable_tasks, MAX_PRIO);
  21046. RB_CLEAR_NODE(&p->pushable_dl_tasks);
  21047. @@ -2603,8 +2728,12 @@ static struct rq *finish_task_switch(struct task_struct *prev)
  21048. finish_arch_post_lock_switch();
  21049. fire_sched_in_preempt_notifiers(current);
  21050. + /*
  21051. + * We use mmdrop_delayed() here so we don't have to do the
  21052. + * full __mmdrop() when we are the last user.
  21053. + */
  21054. if (mm)
  21055. - mmdrop(mm);
  21056. + mmdrop_delayed(mm);
  21057. if (unlikely(prev_state == TASK_DEAD)) {
  21058. if (prev->sched_class->task_dead)
  21059. prev->sched_class->task_dead(prev);
  21060. @@ -2935,16 +3064,6 @@ u64 scheduler_tick_max_deferment(void)
  21061. }
  21062. #endif
  21063. -notrace unsigned long get_parent_ip(unsigned long addr)
  21064. -{
  21065. - if (in_lock_functions(addr)) {
  21066. - addr = CALLER_ADDR2;
  21067. - if (in_lock_functions(addr))
  21068. - addr = CALLER_ADDR3;
  21069. - }
  21070. - return addr;
  21071. -}
  21072. -
  21073. #if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
  21074. defined(CONFIG_PREEMPT_TRACER))
  21075. @@ -2966,7 +3085,7 @@ void preempt_count_add(int val)
  21076. PREEMPT_MASK - 10);
  21077. #endif
  21078. if (preempt_count() == val) {
  21079. - unsigned long ip = get_parent_ip(CALLER_ADDR1);
  21080. + unsigned long ip = get_lock_parent_ip();
  21081. #ifdef CONFIG_DEBUG_PREEMPT
  21082. current->preempt_disable_ip = ip;
  21083. #endif
  21084. @@ -2993,7 +3112,7 @@ void preempt_count_sub(int val)
  21085. #endif
  21086. if (preempt_count() == val)
  21087. - trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
  21088. + trace_preempt_on(CALLER_ADDR0, get_lock_parent_ip());
  21089. __preempt_count_sub(val);
  21090. }
  21091. EXPORT_SYMBOL(preempt_count_sub);
  21092. @@ -3048,6 +3167,77 @@ static inline void schedule_debug(struct task_struct *prev)
  21093. schedstat_inc(this_rq(), sched_count);
  21094. }
  21095. +#if defined(CONFIG_PREEMPT_RT_FULL) && defined(CONFIG_SMP)
  21096. +
  21097. +void migrate_disable(void)
  21098. +{
  21099. + struct task_struct *p = current;
  21100. +
  21101. + if (in_atomic() || irqs_disabled()) {
  21102. +#ifdef CONFIG_SCHED_DEBUG
  21103. + p->migrate_disable_atomic++;
  21104. +#endif
  21105. + return;
  21106. + }
  21107. +
  21108. +#ifdef CONFIG_SCHED_DEBUG
  21109. + if (unlikely(p->migrate_disable_atomic)) {
  21110. + tracing_off();
  21111. + WARN_ON_ONCE(1);
  21112. + }
  21113. +#endif
  21114. +
  21115. + if (p->migrate_disable) {
  21116. + p->migrate_disable++;
  21117. + return;
  21118. + }
  21119. +
  21120. + preempt_disable();
  21121. + preempt_lazy_disable();
  21122. + pin_current_cpu();
  21123. + p->migrate_disable = 1;
  21124. + preempt_enable();
  21125. +}
  21126. +EXPORT_SYMBOL(migrate_disable);
  21127. +
  21128. +void migrate_enable(void)
  21129. +{
  21130. + struct task_struct *p = current;
  21131. +
  21132. + if (in_atomic() || irqs_disabled()) {
  21133. +#ifdef CONFIG_SCHED_DEBUG
  21134. + p->migrate_disable_atomic--;
  21135. +#endif
  21136. + return;
  21137. + }
  21138. +
  21139. +#ifdef CONFIG_SCHED_DEBUG
  21140. + if (unlikely(p->migrate_disable_atomic)) {
  21141. + tracing_off();
  21142. + WARN_ON_ONCE(1);
  21143. + }
  21144. +#endif
  21145. + WARN_ON_ONCE(p->migrate_disable <= 0);
  21146. +
  21147. + if (p->migrate_disable > 1) {
  21148. + p->migrate_disable--;
  21149. + return;
  21150. + }
  21151. +
  21152. + preempt_disable();
  21153. + /*
  21154. + * Clearing migrate_disable causes tsk_cpus_allowed to
  21155. + * show the tasks original cpu affinity.
  21156. + */
  21157. + p->migrate_disable = 0;
  21158. +
  21159. + unpin_current_cpu();
  21160. + preempt_enable();
  21161. + preempt_lazy_enable();
  21162. +}
  21163. +EXPORT_SYMBOL(migrate_enable);
  21164. +#endif
  21165. +
  21166. /*
  21167. * Pick up the highest-prio task:
  21168. */
  21169. @@ -3172,19 +3362,6 @@ static void __sched notrace __schedule(bool preempt)
  21170. } else {
  21171. deactivate_task(rq, prev, DEQUEUE_SLEEP);
  21172. prev->on_rq = 0;
  21173. -
  21174. - /*
  21175. - * If a worker went to sleep, notify and ask workqueue
  21176. - * whether it wants to wake up a task to maintain
  21177. - * concurrency.
  21178. - */
  21179. - if (prev->flags & PF_WQ_WORKER) {
  21180. - struct task_struct *to_wakeup;
  21181. -
  21182. - to_wakeup = wq_worker_sleeping(prev, cpu);
  21183. - if (to_wakeup)
  21184. - try_to_wake_up_local(to_wakeup);
  21185. - }
  21186. }
  21187. switch_count = &prev->nvcsw;
  21188. }
  21189. @@ -3194,6 +3371,7 @@ static void __sched notrace __schedule(bool preempt)
  21190. next = pick_next_task(rq, prev);
  21191. clear_tsk_need_resched(prev);
  21192. + clear_tsk_need_resched_lazy(prev);
  21193. clear_preempt_need_resched();
  21194. rq->clock_skip_update = 0;
  21195. @@ -3215,9 +3393,20 @@ static void __sched notrace __schedule(bool preempt)
  21196. static inline void sched_submit_work(struct task_struct *tsk)
  21197. {
  21198. - if (!tsk->state || tsk_is_pi_blocked(tsk))
  21199. + if (!tsk->state)
  21200. return;
  21201. /*
  21202. + * If a worker went to sleep, notify and ask workqueue whether
  21203. + * it wants to wake up a task to maintain concurrency.
  21204. + */
  21205. + if (tsk->flags & PF_WQ_WORKER)
  21206. + wq_worker_sleeping(tsk);
  21207. +
  21208. +
  21209. + if (tsk_is_pi_blocked(tsk))
  21210. + return;
  21211. +
  21212. + /*
  21213. * If we are going to sleep and we have plugged IO queued,
  21214. * make sure to submit it to avoid deadlocks.
  21215. */
  21216. @@ -3225,6 +3414,12 @@ static inline void sched_submit_work(struct task_struct *tsk)
  21217. blk_schedule_flush_plug(tsk);
  21218. }
  21219. +static void sched_update_worker(struct task_struct *tsk)
  21220. +{
  21221. + if (tsk->flags & PF_WQ_WORKER)
  21222. + wq_worker_running(tsk);
  21223. +}
  21224. +
  21225. asmlinkage __visible void __sched schedule(void)
  21226. {
  21227. struct task_struct *tsk = current;
  21228. @@ -3235,6 +3430,7 @@ asmlinkage __visible void __sched schedule(void)
  21229. __schedule(false);
  21230. sched_preempt_enable_no_resched();
  21231. } while (need_resched());
  21232. + sched_update_worker(tsk);
  21233. }
  21234. EXPORT_SYMBOL(schedule);
  21235. @@ -3283,6 +3479,30 @@ static void __sched notrace preempt_schedule_common(void)
  21236. } while (need_resched());
  21237. }
  21238. +#ifdef CONFIG_PREEMPT_LAZY
  21239. +/*
  21240. + * If TIF_NEED_RESCHED is then we allow to be scheduled away since this is
  21241. + * set by a RT task. Oterwise we try to avoid beeing scheduled out as long as
  21242. + * preempt_lazy_count counter >0.
  21243. + */
  21244. +static __always_inline int preemptible_lazy(void)
  21245. +{
  21246. + if (test_thread_flag(TIF_NEED_RESCHED))
  21247. + return 1;
  21248. + if (current_thread_info()->preempt_lazy_count)
  21249. + return 0;
  21250. + return 1;
  21251. +}
  21252. +
  21253. +#else
  21254. +
  21255. +static inline int preemptible_lazy(void)
  21256. +{
  21257. + return 1;
  21258. +}
  21259. +
  21260. +#endif
  21261. +
  21262. #ifdef CONFIG_PREEMPT
  21263. /*
  21264. * this is the entry point to schedule() from in-kernel preemption
  21265. @@ -3297,6 +3517,8 @@ asmlinkage __visible void __sched notrace preempt_schedule(void)
  21266. */
  21267. if (likely(!preemptible()))
  21268. return;
  21269. + if (!preemptible_lazy())
  21270. + return;
  21271. preempt_schedule_common();
  21272. }
  21273. @@ -3323,6 +3545,8 @@ asmlinkage __visible void __sched notrace preempt_schedule_notrace(void)
  21274. if (likely(!preemptible()))
  21275. return;
  21276. + if (!preemptible_lazy())
  21277. + return;
  21278. do {
  21279. preempt_disable_notrace();
  21280. @@ -3332,7 +3556,16 @@ asmlinkage __visible void __sched notrace preempt_schedule_notrace(void)
  21281. * an infinite recursion.
  21282. */
  21283. prev_ctx = exception_enter();
  21284. + /*
  21285. + * The add/subtract must not be traced by the function
  21286. + * tracer. But we still want to account for the
  21287. + * preempt off latency tracer. Since the _notrace versions
  21288. + * of add/subtract skip the accounting for latency tracer
  21289. + * we must force it manually.
  21290. + */
  21291. + start_critical_timings();
  21292. __schedule(true);
  21293. + stop_critical_timings();
  21294. exception_exit(prev_ctx);
  21295. preempt_enable_no_resched_notrace();
  21296. @@ -4676,6 +4909,7 @@ int __cond_resched_lock(spinlock_t *lock)
  21297. }
  21298. EXPORT_SYMBOL(__cond_resched_lock);
  21299. +#ifndef CONFIG_PREEMPT_RT_FULL
  21300. int __sched __cond_resched_softirq(void)
  21301. {
  21302. BUG_ON(!in_softirq());
  21303. @@ -4689,6 +4923,7 @@ int __sched __cond_resched_softirq(void)
  21304. return 0;
  21305. }
  21306. EXPORT_SYMBOL(__cond_resched_softirq);
  21307. +#endif
  21308. /**
  21309. * yield - yield the current processor to other threads.
  21310. @@ -5055,7 +5290,9 @@ void init_idle(struct task_struct *idle, int cpu)
  21311. /* Set the preempt count _outside_ the spinlocks! */
  21312. init_idle_preempt_count(idle, cpu);
  21313. -
  21314. +#ifdef CONFIG_HAVE_PREEMPT_LAZY
  21315. + task_thread_info(idle)->preempt_lazy_count = 0;
  21316. +#endif
  21317. /*
  21318. * The idle tasks have their own, simple scheduling class:
  21319. */
  21320. @@ -5196,6 +5433,8 @@ void sched_setnuma(struct task_struct *p, int nid)
  21321. #endif /* CONFIG_NUMA_BALANCING */
  21322. #ifdef CONFIG_HOTPLUG_CPU
  21323. +static DEFINE_PER_CPU(struct mm_struct *, idle_last_mm);
  21324. +
  21325. /*
  21326. * Ensures that the idle task is using init_mm right before its cpu goes
  21327. * offline.
  21328. @@ -5210,7 +5449,11 @@ void idle_task_exit(void)
  21329. switch_mm(mm, &init_mm, current);
  21330. finish_arch_post_lock_switch();
  21331. }
  21332. - mmdrop(mm);
  21333. + /*
  21334. + * Defer the cleanup to an alive cpu. On RT we can neither
  21335. + * call mmdrop() nor mmdrop_delayed() from here.
  21336. + */
  21337. + per_cpu(idle_last_mm, smp_processor_id()) = mm;
  21338. }
  21339. /*
  21340. @@ -5583,6 +5826,10 @@ migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
  21341. case CPU_DEAD:
  21342. calc_load_migrate(rq);
  21343. + if (per_cpu(idle_last_mm, cpu)) {
  21344. + mmdrop(per_cpu(idle_last_mm, cpu));
  21345. + per_cpu(idle_last_mm, cpu) = NULL;
  21346. + }
  21347. break;
  21348. #endif
  21349. }
  21350. @@ -7566,7 +7813,7 @@ void __init sched_init(void)
  21351. #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
  21352. static inline int preempt_count_equals(int preempt_offset)
  21353. {
  21354. - int nested = preempt_count() + rcu_preempt_depth();
  21355. + int nested = preempt_count() + sched_rcu_preempt_depth();
  21356. return (nested == preempt_offset);
  21357. }
  21358. diff --git a/kernel/sched/cpudeadline.c b/kernel/sched/cpudeadline.c
  21359. index 5a75b08cfd85..5be58820465c 100644
  21360. --- a/kernel/sched/cpudeadline.c
  21361. +++ b/kernel/sched/cpudeadline.c
  21362. @@ -103,10 +103,10 @@ int cpudl_find(struct cpudl *cp, struct task_struct *p,
  21363. const struct sched_dl_entity *dl_se = &p->dl;
  21364. if (later_mask &&
  21365. - cpumask_and(later_mask, cp->free_cpus, &p->cpus_allowed)) {
  21366. + cpumask_and(later_mask, cp->free_cpus, tsk_cpus_allowed(p))) {
  21367. best_cpu = cpumask_any(later_mask);
  21368. goto out;
  21369. - } else if (cpumask_test_cpu(cpudl_maximum(cp), &p->cpus_allowed) &&
  21370. + } else if (cpumask_test_cpu(cpudl_maximum(cp), tsk_cpus_allowed(p)) &&
  21371. dl_time_before(dl_se->deadline, cp->elements[0].dl)) {
  21372. best_cpu = cpudl_maximum(cp);
  21373. if (later_mask)
  21374. diff --git a/kernel/sched/cpupri.c b/kernel/sched/cpupri.c
  21375. index 981fcd7dc394..11e9705bf937 100644
  21376. --- a/kernel/sched/cpupri.c
  21377. +++ b/kernel/sched/cpupri.c
  21378. @@ -103,11 +103,11 @@ int cpupri_find(struct cpupri *cp, struct task_struct *p,
  21379. if (skip)
  21380. continue;
  21381. - if (cpumask_any_and(&p->cpus_allowed, vec->mask) >= nr_cpu_ids)
  21382. + if (cpumask_any_and(tsk_cpus_allowed(p), vec->mask) >= nr_cpu_ids)
  21383. continue;
  21384. if (lowest_mask) {
  21385. - cpumask_and(lowest_mask, &p->cpus_allowed, vec->mask);
  21386. + cpumask_and(lowest_mask, tsk_cpus_allowed(p), vec->mask);
  21387. /*
  21388. * We have to ensure that we have at least one bit
  21389. diff --git a/kernel/sched/cputime.c b/kernel/sched/cputime.c
  21390. index a1aecbedf5b1..558b98af241d 100644
  21391. --- a/kernel/sched/cputime.c
  21392. +++ b/kernel/sched/cputime.c
  21393. @@ -685,7 +685,7 @@ static cputime_t get_vtime_delta(struct task_struct *tsk)
  21394. {
  21395. unsigned long long delta = vtime_delta(tsk);
  21396. - WARN_ON_ONCE(tsk->vtime_snap_whence == VTIME_SLEEPING);
  21397. + WARN_ON_ONCE(tsk->vtime_snap_whence == VTIME_INACTIVE);
  21398. tsk->vtime_snap += delta;
  21399. /* CHECKME: always safe to convert nsecs to cputime? */
  21400. @@ -701,37 +701,37 @@ static void __vtime_account_system(struct task_struct *tsk)
  21401. void vtime_account_system(struct task_struct *tsk)
  21402. {
  21403. - write_seqlock(&tsk->vtime_seqlock);
  21404. + write_seqcount_begin(&tsk->vtime_seqcount);
  21405. __vtime_account_system(tsk);
  21406. - write_sequnlock(&tsk->vtime_seqlock);
  21407. + write_seqcount_end(&tsk->vtime_seqcount);
  21408. }
  21409. void vtime_gen_account_irq_exit(struct task_struct *tsk)
  21410. {
  21411. - write_seqlock(&tsk->vtime_seqlock);
  21412. + write_seqcount_begin(&tsk->vtime_seqcount);
  21413. __vtime_account_system(tsk);
  21414. if (context_tracking_in_user())
  21415. tsk->vtime_snap_whence = VTIME_USER;
  21416. - write_sequnlock(&tsk->vtime_seqlock);
  21417. + write_seqcount_end(&tsk->vtime_seqcount);
  21418. }
  21419. void vtime_account_user(struct task_struct *tsk)
  21420. {
  21421. cputime_t delta_cpu;
  21422. - write_seqlock(&tsk->vtime_seqlock);
  21423. + write_seqcount_begin(&tsk->vtime_seqcount);
  21424. delta_cpu = get_vtime_delta(tsk);
  21425. tsk->vtime_snap_whence = VTIME_SYS;
  21426. account_user_time(tsk, delta_cpu, cputime_to_scaled(delta_cpu));
  21427. - write_sequnlock(&tsk->vtime_seqlock);
  21428. + write_seqcount_end(&tsk->vtime_seqcount);
  21429. }
  21430. void vtime_user_enter(struct task_struct *tsk)
  21431. {
  21432. - write_seqlock(&tsk->vtime_seqlock);
  21433. + write_seqcount_begin(&tsk->vtime_seqcount);
  21434. __vtime_account_system(tsk);
  21435. tsk->vtime_snap_whence = VTIME_USER;
  21436. - write_sequnlock(&tsk->vtime_seqlock);
  21437. + write_seqcount_end(&tsk->vtime_seqcount);
  21438. }
  21439. void vtime_guest_enter(struct task_struct *tsk)
  21440. @@ -743,19 +743,19 @@ void vtime_guest_enter(struct task_struct *tsk)
  21441. * synchronization against the reader (task_gtime())
  21442. * that can thus safely catch up with a tickless delta.
  21443. */
  21444. - write_seqlock(&tsk->vtime_seqlock);
  21445. + write_seqcount_begin(&tsk->vtime_seqcount);
  21446. __vtime_account_system(tsk);
  21447. current->flags |= PF_VCPU;
  21448. - write_sequnlock(&tsk->vtime_seqlock);
  21449. + write_seqcount_end(&tsk->vtime_seqcount);
  21450. }
  21451. EXPORT_SYMBOL_GPL(vtime_guest_enter);
  21452. void vtime_guest_exit(struct task_struct *tsk)
  21453. {
  21454. - write_seqlock(&tsk->vtime_seqlock);
  21455. + write_seqcount_begin(&tsk->vtime_seqcount);
  21456. __vtime_account_system(tsk);
  21457. current->flags &= ~PF_VCPU;
  21458. - write_sequnlock(&tsk->vtime_seqlock);
  21459. + write_seqcount_end(&tsk->vtime_seqcount);
  21460. }
  21461. EXPORT_SYMBOL_GPL(vtime_guest_exit);
  21462. @@ -768,24 +768,26 @@ void vtime_account_idle(struct task_struct *tsk)
  21463. void arch_vtime_task_switch(struct task_struct *prev)
  21464. {
  21465. - write_seqlock(&prev->vtime_seqlock);
  21466. - prev->vtime_snap_whence = VTIME_SLEEPING;
  21467. - write_sequnlock(&prev->vtime_seqlock);
  21468. + write_seqcount_begin(&prev->vtime_seqcount);
  21469. + prev->vtime_snap_whence = VTIME_INACTIVE;
  21470. + write_seqcount_end(&prev->vtime_seqcount);
  21471. - write_seqlock(&current->vtime_seqlock);
  21472. + write_seqcount_begin(&current->vtime_seqcount);
  21473. current->vtime_snap_whence = VTIME_SYS;
  21474. current->vtime_snap = sched_clock_cpu(smp_processor_id());
  21475. - write_sequnlock(&current->vtime_seqlock);
  21476. + write_seqcount_end(&current->vtime_seqcount);
  21477. }
  21478. void vtime_init_idle(struct task_struct *t, int cpu)
  21479. {
  21480. unsigned long flags;
  21481. - write_seqlock_irqsave(&t->vtime_seqlock, flags);
  21482. + local_irq_save(flags);
  21483. + write_seqcount_begin(&t->vtime_seqcount);
  21484. t->vtime_snap_whence = VTIME_SYS;
  21485. t->vtime_snap = sched_clock_cpu(cpu);
  21486. - write_sequnlock_irqrestore(&t->vtime_seqlock, flags);
  21487. + write_seqcount_end(&t->vtime_seqcount);
  21488. + local_irq_restore(flags);
  21489. }
  21490. cputime_t task_gtime(struct task_struct *t)
  21491. @@ -797,13 +799,13 @@ cputime_t task_gtime(struct task_struct *t)
  21492. return t->gtime;
  21493. do {
  21494. - seq = read_seqbegin(&t->vtime_seqlock);
  21495. + seq = read_seqcount_begin(&t->vtime_seqcount);
  21496. gtime = t->gtime;
  21497. if (t->flags & PF_VCPU)
  21498. gtime += vtime_delta(t);
  21499. - } while (read_seqretry(&t->vtime_seqlock, seq));
  21500. + } while (read_seqcount_retry(&t->vtime_seqcount, seq));
  21501. return gtime;
  21502. }
  21503. @@ -826,7 +828,7 @@ fetch_task_cputime(struct task_struct *t,
  21504. *udelta = 0;
  21505. *sdelta = 0;
  21506. - seq = read_seqbegin(&t->vtime_seqlock);
  21507. + seq = read_seqcount_begin(&t->vtime_seqcount);
  21508. if (u_dst)
  21509. *u_dst = *u_src;
  21510. @@ -834,7 +836,7 @@ fetch_task_cputime(struct task_struct *t,
  21511. *s_dst = *s_src;
  21512. /* Task is sleeping, nothing to add */
  21513. - if (t->vtime_snap_whence == VTIME_SLEEPING ||
  21514. + if (t->vtime_snap_whence == VTIME_INACTIVE ||
  21515. is_idle_task(t))
  21516. continue;
  21517. @@ -850,7 +852,7 @@ fetch_task_cputime(struct task_struct *t,
  21518. if (t->vtime_snap_whence == VTIME_SYS)
  21519. *sdelta = delta;
  21520. }
  21521. - } while (read_seqretry(&t->vtime_seqlock, seq));
  21522. + } while (read_seqcount_retry(&t->vtime_seqcount, seq));
  21523. }
  21524. diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
  21525. index e984f059e5fc..20e0c9b9ace5 100644
  21526. --- a/kernel/sched/deadline.c
  21527. +++ b/kernel/sched/deadline.c
  21528. @@ -134,7 +134,7 @@ static void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  21529. {
  21530. struct task_struct *p = dl_task_of(dl_se);
  21531. - if (p->nr_cpus_allowed > 1)
  21532. + if (tsk_nr_cpus_allowed(p) > 1)
  21533. dl_rq->dl_nr_migratory++;
  21534. update_dl_migration(dl_rq);
  21535. @@ -144,7 +144,7 @@ static void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  21536. {
  21537. struct task_struct *p = dl_task_of(dl_se);
  21538. - if (p->nr_cpus_allowed > 1)
  21539. + if (tsk_nr_cpus_allowed(p) > 1)
  21540. dl_rq->dl_nr_migratory--;
  21541. update_dl_migration(dl_rq);
  21542. @@ -697,6 +697,7 @@ void init_dl_task_timer(struct sched_dl_entity *dl_se)
  21543. hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  21544. timer->function = dl_task_timer;
  21545. + timer->irqsafe = 1;
  21546. }
  21547. static
  21548. @@ -989,7 +990,7 @@ static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags)
  21549. enqueue_dl_entity(&p->dl, pi_se, flags);
  21550. - if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
  21551. + if (!task_current(rq, p) && tsk_nr_cpus_allowed(p) > 1)
  21552. enqueue_pushable_dl_task(rq, p);
  21553. }
  21554. @@ -1067,9 +1068,9 @@ select_task_rq_dl(struct task_struct *p, int cpu, int sd_flag, int flags)
  21555. * try to make it stay here, it might be important.
  21556. */
  21557. if (unlikely(dl_task(curr)) &&
  21558. - (curr->nr_cpus_allowed < 2 ||
  21559. + (tsk_nr_cpus_allowed(curr) < 2 ||
  21560. !dl_entity_preempt(&p->dl, &curr->dl)) &&
  21561. - (p->nr_cpus_allowed > 1)) {
  21562. + (tsk_nr_cpus_allowed(p) > 1)) {
  21563. int target = find_later_rq(p);
  21564. if (target != -1 &&
  21565. @@ -1090,7 +1091,7 @@ static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p)
  21566. * Current can't be migrated, useless to reschedule,
  21567. * let's hope p can move out.
  21568. */
  21569. - if (rq->curr->nr_cpus_allowed == 1 ||
  21570. + if (tsk_nr_cpus_allowed(rq->curr) == 1 ||
  21571. cpudl_find(&rq->rd->cpudl, rq->curr, NULL) == -1)
  21572. return;
  21573. @@ -1098,7 +1099,7 @@ static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p)
  21574. * p is migratable, so let's not schedule it and
  21575. * see if it is pushed or pulled somewhere else.
  21576. */
  21577. - if (p->nr_cpus_allowed != 1 &&
  21578. + if (tsk_nr_cpus_allowed(p) != 1 &&
  21579. cpudl_find(&rq->rd->cpudl, p, NULL) != -1)
  21580. return;
  21581. @@ -1212,7 +1213,7 @@ static void put_prev_task_dl(struct rq *rq, struct task_struct *p)
  21582. {
  21583. update_curr_dl(rq);
  21584. - if (on_dl_rq(&p->dl) && p->nr_cpus_allowed > 1)
  21585. + if (on_dl_rq(&p->dl) && tsk_nr_cpus_allowed(p) > 1)
  21586. enqueue_pushable_dl_task(rq, p);
  21587. }
  21588. @@ -1335,7 +1336,7 @@ static int find_later_rq(struct task_struct *task)
  21589. if (unlikely(!later_mask))
  21590. return -1;
  21591. - if (task->nr_cpus_allowed == 1)
  21592. + if (tsk_nr_cpus_allowed(task) == 1)
  21593. return -1;
  21594. /*
  21595. @@ -1441,7 +1442,7 @@ static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq)
  21596. if (double_lock_balance(rq, later_rq)) {
  21597. if (unlikely(task_rq(task) != rq ||
  21598. !cpumask_test_cpu(later_rq->cpu,
  21599. - &task->cpus_allowed) ||
  21600. + tsk_cpus_allowed(task)) ||
  21601. task_running(rq, task) ||
  21602. !task_on_rq_queued(task))) {
  21603. double_unlock_balance(rq, later_rq);
  21604. @@ -1480,7 +1481,7 @@ static struct task_struct *pick_next_pushable_dl_task(struct rq *rq)
  21605. BUG_ON(rq->cpu != task_cpu(p));
  21606. BUG_ON(task_current(rq, p));
  21607. - BUG_ON(p->nr_cpus_allowed <= 1);
  21608. + BUG_ON(tsk_nr_cpus_allowed(p) <= 1);
  21609. BUG_ON(!task_on_rq_queued(p));
  21610. BUG_ON(!dl_task(p));
  21611. @@ -1519,7 +1520,7 @@ retry:
  21612. */
  21613. if (dl_task(rq->curr) &&
  21614. dl_time_before(next_task->dl.deadline, rq->curr->dl.deadline) &&
  21615. - rq->curr->nr_cpus_allowed > 1) {
  21616. + tsk_nr_cpus_allowed(rq->curr) > 1) {
  21617. resched_curr(rq);
  21618. return 0;
  21619. }
  21620. @@ -1666,9 +1667,9 @@ static void task_woken_dl(struct rq *rq, struct task_struct *p)
  21621. {
  21622. if (!task_running(rq, p) &&
  21623. !test_tsk_need_resched(rq->curr) &&
  21624. - p->nr_cpus_allowed > 1 &&
  21625. + tsk_nr_cpus_allowed(p) > 1 &&
  21626. dl_task(rq->curr) &&
  21627. - (rq->curr->nr_cpus_allowed < 2 ||
  21628. + (tsk_nr_cpus_allowed(rq->curr) < 2 ||
  21629. !dl_entity_preempt(&p->dl, &rq->curr->dl))) {
  21630. push_dl_tasks(rq);
  21631. }
  21632. @@ -1769,7 +1770,7 @@ static void switched_to_dl(struct rq *rq, struct task_struct *p)
  21633. {
  21634. if (task_on_rq_queued(p) && rq->curr != p) {
  21635. #ifdef CONFIG_SMP
  21636. - if (p->nr_cpus_allowed > 1 && rq->dl.overloaded)
  21637. + if (tsk_nr_cpus_allowed(p) > 1 && rq->dl.overloaded)
  21638. queue_push_tasks(rq);
  21639. #endif
  21640. if (dl_task(rq->curr))
  21641. diff --git a/kernel/sched/debug.c b/kernel/sched/debug.c
  21642. index 641511771ae6..a2d69b883623 100644
  21643. --- a/kernel/sched/debug.c
  21644. +++ b/kernel/sched/debug.c
  21645. @@ -251,6 +251,9 @@ void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq)
  21646. P(rt_throttled);
  21647. PN(rt_time);
  21648. PN(rt_runtime);
  21649. +#ifdef CONFIG_SMP
  21650. + P(rt_nr_migratory);
  21651. +#endif
  21652. #undef PN
  21653. #undef P
  21654. @@ -635,6 +638,10 @@ void proc_sched_show_task(struct task_struct *p, struct seq_file *m)
  21655. #endif
  21656. P(policy);
  21657. P(prio);
  21658. +#ifdef CONFIG_PREEMPT_RT_FULL
  21659. + P(migrate_disable);
  21660. +#endif
  21661. + P(nr_cpus_allowed);
  21662. #undef PN
  21663. #undef __PN
  21664. #undef P
  21665. diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
  21666. index 812069b66f47..ddf1424bcc78 100644
  21667. --- a/kernel/sched/fair.c
  21668. +++ b/kernel/sched/fair.c
  21669. @@ -3166,7 +3166,7 @@ check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  21670. ideal_runtime = sched_slice(cfs_rq, curr);
  21671. delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
  21672. if (delta_exec > ideal_runtime) {
  21673. - resched_curr(rq_of(cfs_rq));
  21674. + resched_curr_lazy(rq_of(cfs_rq));
  21675. /*
  21676. * The current task ran long enough, ensure it doesn't get
  21677. * re-elected due to buddy favours.
  21678. @@ -3190,7 +3190,7 @@ check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  21679. return;
  21680. if (delta > ideal_runtime)
  21681. - resched_curr(rq_of(cfs_rq));
  21682. + resched_curr_lazy(rq_of(cfs_rq));
  21683. }
  21684. static void
  21685. @@ -3330,7 +3330,7 @@ entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
  21686. * validating it and just reschedule.
  21687. */
  21688. if (queued) {
  21689. - resched_curr(rq_of(cfs_rq));
  21690. + resched_curr_lazy(rq_of(cfs_rq));
  21691. return;
  21692. }
  21693. /*
  21694. @@ -3512,7 +3512,7 @@ static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec)
  21695. * hierarchy can be throttled
  21696. */
  21697. if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr))
  21698. - resched_curr(rq_of(cfs_rq));
  21699. + resched_curr_lazy(rq_of(cfs_rq));
  21700. }
  21701. static __always_inline
  21702. @@ -4144,7 +4144,7 @@ static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
  21703. if (delta < 0) {
  21704. if (rq->curr == p)
  21705. - resched_curr(rq);
  21706. + resched_curr_lazy(rq);
  21707. return;
  21708. }
  21709. hrtick_start(rq, delta);
  21710. @@ -5232,7 +5232,7 @@ static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_
  21711. return;
  21712. preempt:
  21713. - resched_curr(rq);
  21714. + resched_curr_lazy(rq);
  21715. /*
  21716. * Only set the backward buddy when the current task is still
  21717. * on the rq. This can happen when a wakeup gets interleaved
  21718. @@ -7983,7 +7983,7 @@ static void task_fork_fair(struct task_struct *p)
  21719. * 'current' within the tree based on its new key value.
  21720. */
  21721. swap(curr->vruntime, se->vruntime);
  21722. - resched_curr(rq);
  21723. + resched_curr_lazy(rq);
  21724. }
  21725. se->vruntime -= cfs_rq->min_vruntime;
  21726. @@ -8008,7 +8008,7 @@ prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
  21727. */
  21728. if (rq->curr == p) {
  21729. if (p->prio > oldprio)
  21730. - resched_curr(rq);
  21731. + resched_curr_lazy(rq);
  21732. } else
  21733. check_preempt_curr(rq, p, 0);
  21734. }
  21735. diff --git a/kernel/sched/features.h b/kernel/sched/features.h
  21736. index 69631fa46c2f..6d28fcd08872 100644
  21737. --- a/kernel/sched/features.h
  21738. +++ b/kernel/sched/features.h
  21739. @@ -45,11 +45,19 @@ SCHED_FEAT(LB_BIAS, true)
  21740. */
  21741. SCHED_FEAT(NONTASK_CAPACITY, true)
  21742. +#ifdef CONFIG_PREEMPT_RT_FULL
  21743. +SCHED_FEAT(TTWU_QUEUE, false)
  21744. +# ifdef CONFIG_PREEMPT_LAZY
  21745. +SCHED_FEAT(PREEMPT_LAZY, true)
  21746. +# endif
  21747. +#else
  21748. +
  21749. /*
  21750. * Queue remote wakeups on the target CPU and process them
  21751. * using the scheduler IPI. Reduces rq->lock contention/bounces.
  21752. */
  21753. SCHED_FEAT(TTWU_QUEUE, true)
  21754. +#endif
  21755. #ifdef HAVE_RT_PUSH_IPI
  21756. /*
  21757. diff --git a/kernel/sched/rt.c b/kernel/sched/rt.c
  21758. index 78ae5c1d9412..4ac6937f4a65 100644
  21759. --- a/kernel/sched/rt.c
  21760. +++ b/kernel/sched/rt.c
  21761. @@ -47,6 +47,7 @@ void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
  21762. hrtimer_init(&rt_b->rt_period_timer,
  21763. CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  21764. + rt_b->rt_period_timer.irqsafe = 1;
  21765. rt_b->rt_period_timer.function = sched_rt_period_timer;
  21766. }
  21767. @@ -93,6 +94,7 @@ void init_rt_rq(struct rt_rq *rt_rq)
  21768. rt_rq->push_cpu = nr_cpu_ids;
  21769. raw_spin_lock_init(&rt_rq->push_lock);
  21770. init_irq_work(&rt_rq->push_work, push_irq_work_func);
  21771. + rt_rq->push_work.flags |= IRQ_WORK_HARD_IRQ;
  21772. #endif
  21773. #endif /* CONFIG_SMP */
  21774. /* We start is dequeued state, because no RT tasks are queued */
  21775. @@ -326,7 +328,7 @@ static void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  21776. rt_rq = &rq_of_rt_rq(rt_rq)->rt;
  21777. rt_rq->rt_nr_total++;
  21778. - if (p->nr_cpus_allowed > 1)
  21779. + if (tsk_nr_cpus_allowed(p) > 1)
  21780. rt_rq->rt_nr_migratory++;
  21781. update_rt_migration(rt_rq);
  21782. @@ -343,7 +345,7 @@ static void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  21783. rt_rq = &rq_of_rt_rq(rt_rq)->rt;
  21784. rt_rq->rt_nr_total--;
  21785. - if (p->nr_cpus_allowed > 1)
  21786. + if (tsk_nr_cpus_allowed(p) > 1)
  21787. rt_rq->rt_nr_migratory--;
  21788. update_rt_migration(rt_rq);
  21789. @@ -1262,7 +1264,7 @@ enqueue_task_rt(struct rq *rq, struct task_struct *p, int flags)
  21790. enqueue_rt_entity(rt_se, flags & ENQUEUE_HEAD);
  21791. - if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
  21792. + if (!task_current(rq, p) && tsk_nr_cpus_allowed(p) > 1)
  21793. enqueue_pushable_task(rq, p);
  21794. }
  21795. @@ -1351,7 +1353,7 @@ select_task_rq_rt(struct task_struct *p, int cpu, int sd_flag, int flags)
  21796. * will have to sort it out.
  21797. */
  21798. if (curr && unlikely(rt_task(curr)) &&
  21799. - (curr->nr_cpus_allowed < 2 ||
  21800. + (tsk_nr_cpus_allowed(curr) < 2 ||
  21801. curr->prio <= p->prio)) {
  21802. int target = find_lowest_rq(p);
  21803. @@ -1375,7 +1377,7 @@ static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)
  21804. * Current can't be migrated, useless to reschedule,
  21805. * let's hope p can move out.
  21806. */
  21807. - if (rq->curr->nr_cpus_allowed == 1 ||
  21808. + if (tsk_nr_cpus_allowed(rq->curr) == 1 ||
  21809. !cpupri_find(&rq->rd->cpupri, rq->curr, NULL))
  21810. return;
  21811. @@ -1383,7 +1385,7 @@ static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)
  21812. * p is migratable, so let's not schedule it and
  21813. * see if it is pushed or pulled somewhere else.
  21814. */
  21815. - if (p->nr_cpus_allowed != 1
  21816. + if (tsk_nr_cpus_allowed(p) != 1
  21817. && cpupri_find(&rq->rd->cpupri, p, NULL))
  21818. return;
  21819. @@ -1517,7 +1519,7 @@ static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
  21820. * The previous task needs to be made eligible for pushing
  21821. * if it is still active
  21822. */
  21823. - if (on_rt_rq(&p->rt) && p->nr_cpus_allowed > 1)
  21824. + if (on_rt_rq(&p->rt) && tsk_nr_cpus_allowed(p) > 1)
  21825. enqueue_pushable_task(rq, p);
  21826. }
  21827. @@ -1567,7 +1569,7 @@ static int find_lowest_rq(struct task_struct *task)
  21828. if (unlikely(!lowest_mask))
  21829. return -1;
  21830. - if (task->nr_cpus_allowed == 1)
  21831. + if (tsk_nr_cpus_allowed(task) == 1)
  21832. return -1; /* No other targets possible */
  21833. if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask))
  21834. @@ -1699,7 +1701,7 @@ static struct task_struct *pick_next_pushable_task(struct rq *rq)
  21835. BUG_ON(rq->cpu != task_cpu(p));
  21836. BUG_ON(task_current(rq, p));
  21837. - BUG_ON(p->nr_cpus_allowed <= 1);
  21838. + BUG_ON(tsk_nr_cpus_allowed(p) <= 1);
  21839. BUG_ON(!task_on_rq_queued(p));
  21840. BUG_ON(!rt_task(p));
  21841. @@ -2059,9 +2061,9 @@ static void task_woken_rt(struct rq *rq, struct task_struct *p)
  21842. {
  21843. if (!task_running(rq, p) &&
  21844. !test_tsk_need_resched(rq->curr) &&
  21845. - p->nr_cpus_allowed > 1 &&
  21846. + tsk_nr_cpus_allowed(p) > 1 &&
  21847. (dl_task(rq->curr) || rt_task(rq->curr)) &&
  21848. - (rq->curr->nr_cpus_allowed < 2 ||
  21849. + (tsk_nr_cpus_allowed(rq->curr) < 2 ||
  21850. rq->curr->prio <= p->prio))
  21851. push_rt_tasks(rq);
  21852. }
  21853. @@ -2134,7 +2136,7 @@ static void switched_to_rt(struct rq *rq, struct task_struct *p)
  21854. */
  21855. if (task_on_rq_queued(p) && rq->curr != p) {
  21856. #ifdef CONFIG_SMP
  21857. - if (p->nr_cpus_allowed > 1 && rq->rt.overloaded)
  21858. + if (tsk_nr_cpus_allowed(p) > 1 && rq->rt.overloaded)
  21859. queue_push_tasks(rq);
  21860. #endif /* CONFIG_SMP */
  21861. if (p->prio < rq->curr->prio)
  21862. diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
  21863. index 4e5db65d1aab..ae26df3bd63f 100644
  21864. --- a/kernel/sched/sched.h
  21865. +++ b/kernel/sched/sched.h
  21866. @@ -1100,6 +1100,7 @@ static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
  21867. #define WF_SYNC 0x01 /* waker goes to sleep after wakeup */
  21868. #define WF_FORK 0x02 /* child wakeup after fork */
  21869. #define WF_MIGRATED 0x4 /* internal use, task got migrated */
  21870. +#define WF_LOCK_SLEEPER 0x08 /* wakeup spinlock "sleeper" */
  21871. /*
  21872. * To aid in avoiding the subversion of "niceness" due to uneven distribution
  21873. @@ -1299,6 +1300,15 @@ extern void init_sched_fair_class(void);
  21874. extern void resched_curr(struct rq *rq);
  21875. extern void resched_cpu(int cpu);
  21876. +#ifdef CONFIG_PREEMPT_LAZY
  21877. +extern void resched_curr_lazy(struct rq *rq);
  21878. +#else
  21879. +static inline void resched_curr_lazy(struct rq *rq)
  21880. +{
  21881. + resched_curr(rq);
  21882. +}
  21883. +#endif
  21884. +
  21885. extern struct rt_bandwidth def_rt_bandwidth;
  21886. extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
  21887. diff --git a/kernel/sched/swait.c b/kernel/sched/swait.c
  21888. new file mode 100644
  21889. index 000000000000..205fe36868f9
  21890. --- /dev/null
  21891. +++ b/kernel/sched/swait.c
  21892. @@ -0,0 +1,143 @@
  21893. +#include <linux/sched.h>
  21894. +#include <linux/swait.h>
  21895. +#include <linux/suspend.h>
  21896. +
  21897. +void __init_swait_queue_head(struct swait_queue_head *q, const char *name,
  21898. + struct lock_class_key *key)
  21899. +{
  21900. + raw_spin_lock_init(&q->lock);
  21901. + lockdep_set_class_and_name(&q->lock, key, name);
  21902. + INIT_LIST_HEAD(&q->task_list);
  21903. +}
  21904. +EXPORT_SYMBOL(__init_swait_queue_head);
  21905. +
  21906. +/*
  21907. + * The thing about the wake_up_state() return value; I think we can ignore it.
  21908. + *
  21909. + * If for some reason it would return 0, that means the previously waiting
  21910. + * task is already running, so it will observe condition true (or has already).
  21911. + */
  21912. +void swake_up_locked(struct swait_queue_head *q)
  21913. +{
  21914. + struct swait_queue *curr;
  21915. +
  21916. + if (list_empty(&q->task_list))
  21917. + return;
  21918. +
  21919. + curr = list_first_entry(&q->task_list, typeof(*curr), task_list);
  21920. + wake_up_process(curr->task);
  21921. + list_del_init(&curr->task_list);
  21922. +}
  21923. +EXPORT_SYMBOL(swake_up_locked);
  21924. +
  21925. +void swake_up_all_locked(struct swait_queue_head *q)
  21926. +{
  21927. + struct swait_queue *curr;
  21928. + int wakes = 0;
  21929. +
  21930. + while (!list_empty(&q->task_list)) {
  21931. +
  21932. + curr = list_first_entry(&q->task_list, typeof(*curr),
  21933. + task_list);
  21934. + wake_up_process(curr->task);
  21935. + list_del_init(&curr->task_list);
  21936. + wakes++;
  21937. + }
  21938. + if (pm_in_action)
  21939. + return;
  21940. + WARN(wakes > 2, "complate_all() with %d waiters\n", wakes);
  21941. +}
  21942. +EXPORT_SYMBOL(swake_up_all_locked);
  21943. +
  21944. +void swake_up(struct swait_queue_head *q)
  21945. +{
  21946. + unsigned long flags;
  21947. +
  21948. + if (!swait_active(q))
  21949. + return;
  21950. +
  21951. + raw_spin_lock_irqsave(&q->lock, flags);
  21952. + swake_up_locked(q);
  21953. + raw_spin_unlock_irqrestore(&q->lock, flags);
  21954. +}
  21955. +EXPORT_SYMBOL(swake_up);
  21956. +
  21957. +/*
  21958. + * Does not allow usage from IRQ disabled, since we must be able to
  21959. + * release IRQs to guarantee bounded hold time.
  21960. + */
  21961. +void swake_up_all(struct swait_queue_head *q)
  21962. +{
  21963. + struct swait_queue *curr;
  21964. + LIST_HEAD(tmp);
  21965. +
  21966. + if (!swait_active(q))
  21967. + return;
  21968. +
  21969. + raw_spin_lock_irq(&q->lock);
  21970. + list_splice_init(&q->task_list, &tmp);
  21971. + while (!list_empty(&tmp)) {
  21972. + curr = list_first_entry(&tmp, typeof(*curr), task_list);
  21973. +
  21974. + wake_up_state(curr->task, TASK_NORMAL);
  21975. + list_del_init(&curr->task_list);
  21976. +
  21977. + if (list_empty(&tmp))
  21978. + break;
  21979. +
  21980. + raw_spin_unlock_irq(&q->lock);
  21981. + raw_spin_lock_irq(&q->lock);
  21982. + }
  21983. + raw_spin_unlock_irq(&q->lock);
  21984. +}
  21985. +EXPORT_SYMBOL(swake_up_all);
  21986. +
  21987. +void __prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait)
  21988. +{
  21989. + wait->task = current;
  21990. + if (list_empty(&wait->task_list))
  21991. + list_add(&wait->task_list, &q->task_list);
  21992. +}
  21993. +
  21994. +void prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait, int state)
  21995. +{
  21996. + unsigned long flags;
  21997. +
  21998. + raw_spin_lock_irqsave(&q->lock, flags);
  21999. + __prepare_to_swait(q, wait);
  22000. + set_current_state(state);
  22001. + raw_spin_unlock_irqrestore(&q->lock, flags);
  22002. +}
  22003. +EXPORT_SYMBOL(prepare_to_swait);
  22004. +
  22005. +long prepare_to_swait_event(struct swait_queue_head *q, struct swait_queue *wait, int state)
  22006. +{
  22007. + if (signal_pending_state(state, current))
  22008. + return -ERESTARTSYS;
  22009. +
  22010. + prepare_to_swait(q, wait, state);
  22011. +
  22012. + return 0;
  22013. +}
  22014. +EXPORT_SYMBOL(prepare_to_swait_event);
  22015. +
  22016. +void __finish_swait(struct swait_queue_head *q, struct swait_queue *wait)
  22017. +{
  22018. + __set_current_state(TASK_RUNNING);
  22019. + if (!list_empty(&wait->task_list))
  22020. + list_del_init(&wait->task_list);
  22021. +}
  22022. +
  22023. +void finish_swait(struct swait_queue_head *q, struct swait_queue *wait)
  22024. +{
  22025. + unsigned long flags;
  22026. +
  22027. + __set_current_state(TASK_RUNNING);
  22028. +
  22029. + if (!list_empty_careful(&wait->task_list)) {
  22030. + raw_spin_lock_irqsave(&q->lock, flags);
  22031. + list_del_init(&wait->task_list);
  22032. + raw_spin_unlock_irqrestore(&q->lock, flags);
  22033. + }
  22034. +}
  22035. +EXPORT_SYMBOL(finish_swait);
  22036. diff --git a/kernel/sched/swork.c b/kernel/sched/swork.c
  22037. new file mode 100644
  22038. index 000000000000..1950f40ca725
  22039. --- /dev/null
  22040. +++ b/kernel/sched/swork.c
  22041. @@ -0,0 +1,173 @@
  22042. +/*
  22043. + * Copyright (C) 2014 BMW Car IT GmbH, Daniel Wagner daniel.wagner@bmw-carit.de
  22044. + *
  22045. + * Provides a framework for enqueuing callbacks from irq context
  22046. + * PREEMPT_RT_FULL safe. The callbacks are executed in kthread context.
  22047. + */
  22048. +
  22049. +#include <linux/swait.h>
  22050. +#include <linux/swork.h>
  22051. +#include <linux/kthread.h>
  22052. +#include <linux/slab.h>
  22053. +#include <linux/spinlock.h>
  22054. +#include <linux/export.h>
  22055. +
  22056. +#define SWORK_EVENT_PENDING (1 << 0)
  22057. +
  22058. +static DEFINE_MUTEX(worker_mutex);
  22059. +static struct sworker *glob_worker;
  22060. +
  22061. +struct sworker {
  22062. + struct list_head events;
  22063. + struct swait_queue_head wq;
  22064. +
  22065. + raw_spinlock_t lock;
  22066. +
  22067. + struct task_struct *task;
  22068. + int refs;
  22069. +};
  22070. +
  22071. +static bool swork_readable(struct sworker *worker)
  22072. +{
  22073. + bool r;
  22074. +
  22075. + if (kthread_should_stop())
  22076. + return true;
  22077. +
  22078. + raw_spin_lock_irq(&worker->lock);
  22079. + r = !list_empty(&worker->events);
  22080. + raw_spin_unlock_irq(&worker->lock);
  22081. +
  22082. + return r;
  22083. +}
  22084. +
  22085. +static int swork_kthread(void *arg)
  22086. +{
  22087. + struct sworker *worker = arg;
  22088. +
  22089. + for (;;) {
  22090. + swait_event_interruptible(worker->wq,
  22091. + swork_readable(worker));
  22092. + if (kthread_should_stop())
  22093. + break;
  22094. +
  22095. + raw_spin_lock_irq(&worker->lock);
  22096. + while (!list_empty(&worker->events)) {
  22097. + struct swork_event *sev;
  22098. +
  22099. + sev = list_first_entry(&worker->events,
  22100. + struct swork_event, item);
  22101. + list_del(&sev->item);
  22102. + raw_spin_unlock_irq(&worker->lock);
  22103. +
  22104. + WARN_ON_ONCE(!test_and_clear_bit(SWORK_EVENT_PENDING,
  22105. + &sev->flags));
  22106. + sev->func(sev);
  22107. + raw_spin_lock_irq(&worker->lock);
  22108. + }
  22109. + raw_spin_unlock_irq(&worker->lock);
  22110. + }
  22111. + return 0;
  22112. +}
  22113. +
  22114. +static struct sworker *swork_create(void)
  22115. +{
  22116. + struct sworker *worker;
  22117. +
  22118. + worker = kzalloc(sizeof(*worker), GFP_KERNEL);
  22119. + if (!worker)
  22120. + return ERR_PTR(-ENOMEM);
  22121. +
  22122. + INIT_LIST_HEAD(&worker->events);
  22123. + raw_spin_lock_init(&worker->lock);
  22124. + init_swait_queue_head(&worker->wq);
  22125. +
  22126. + worker->task = kthread_run(swork_kthread, worker, "kswork");
  22127. + if (IS_ERR(worker->task)) {
  22128. + kfree(worker);
  22129. + return ERR_PTR(-ENOMEM);
  22130. + }
  22131. +
  22132. + return worker;
  22133. +}
  22134. +
  22135. +static void swork_destroy(struct sworker *worker)
  22136. +{
  22137. + kthread_stop(worker->task);
  22138. +
  22139. + WARN_ON(!list_empty(&worker->events));
  22140. + kfree(worker);
  22141. +}
  22142. +
  22143. +/**
  22144. + * swork_queue - queue swork
  22145. + *
  22146. + * Returns %false if @work was already on a queue, %true otherwise.
  22147. + *
  22148. + * The work is queued and processed on a random CPU
  22149. + */
  22150. +bool swork_queue(struct swork_event *sev)
  22151. +{
  22152. + unsigned long flags;
  22153. +
  22154. + if (test_and_set_bit(SWORK_EVENT_PENDING, &sev->flags))
  22155. + return false;
  22156. +
  22157. + raw_spin_lock_irqsave(&glob_worker->lock, flags);
  22158. + list_add_tail(&sev->item, &glob_worker->events);
  22159. + raw_spin_unlock_irqrestore(&glob_worker->lock, flags);
  22160. +
  22161. + swake_up(&glob_worker->wq);
  22162. + return true;
  22163. +}
  22164. +EXPORT_SYMBOL_GPL(swork_queue);
  22165. +
  22166. +/**
  22167. + * swork_get - get an instance of the sworker
  22168. + *
  22169. + * Returns an negative error code if the initialization if the worker did not
  22170. + * work, %0 otherwise.
  22171. + *
  22172. + */
  22173. +int swork_get(void)
  22174. +{
  22175. + struct sworker *worker;
  22176. +
  22177. + mutex_lock(&worker_mutex);
  22178. + if (!glob_worker) {
  22179. + worker = swork_create();
  22180. + if (IS_ERR(worker)) {
  22181. + mutex_unlock(&worker_mutex);
  22182. + return -ENOMEM;
  22183. + }
  22184. +
  22185. + glob_worker = worker;
  22186. + }
  22187. +
  22188. + glob_worker->refs++;
  22189. + mutex_unlock(&worker_mutex);
  22190. +
  22191. + return 0;
  22192. +}
  22193. +EXPORT_SYMBOL_GPL(swork_get);
  22194. +
  22195. +/**
  22196. + * swork_put - puts an instance of the sworker
  22197. + *
  22198. + * Will destroy the sworker thread. This function must not be called until all
  22199. + * queued events have been completed.
  22200. + */
  22201. +void swork_put(void)
  22202. +{
  22203. + mutex_lock(&worker_mutex);
  22204. +
  22205. + glob_worker->refs--;
  22206. + if (glob_worker->refs > 0)
  22207. + goto out;
  22208. +
  22209. + swork_destroy(glob_worker);
  22210. + glob_worker = NULL;
  22211. +out:
  22212. + mutex_unlock(&worker_mutex);
  22213. +}
  22214. +EXPORT_SYMBOL_GPL(swork_put);
  22215. diff --git a/kernel/signal.c b/kernel/signal.c
  22216. index f3f1f7a972fd..bc2c990f3f63 100644
  22217. --- a/kernel/signal.c
  22218. +++ b/kernel/signal.c
  22219. @@ -14,6 +14,7 @@
  22220. #include <linux/export.h>
  22221. #include <linux/init.h>
  22222. #include <linux/sched.h>
  22223. +#include <linux/sched/rt.h>
  22224. #include <linux/fs.h>
  22225. #include <linux/tty.h>
  22226. #include <linux/binfmts.h>
  22227. @@ -352,13 +353,30 @@ static bool task_participate_group_stop(struct task_struct *task)
  22228. return false;
  22229. }
  22230. +static inline struct sigqueue *get_task_cache(struct task_struct *t)
  22231. +{
  22232. + struct sigqueue *q = t->sigqueue_cache;
  22233. +
  22234. + if (cmpxchg(&t->sigqueue_cache, q, NULL) != q)
  22235. + return NULL;
  22236. + return q;
  22237. +}
  22238. +
  22239. +static inline int put_task_cache(struct task_struct *t, struct sigqueue *q)
  22240. +{
  22241. + if (cmpxchg(&t->sigqueue_cache, NULL, q) == NULL)
  22242. + return 0;
  22243. + return 1;
  22244. +}
  22245. +
  22246. /*
  22247. * allocate a new signal queue record
  22248. * - this may be called without locks if and only if t == current, otherwise an
  22249. * appropriate lock must be held to stop the target task from exiting
  22250. */
  22251. static struct sigqueue *
  22252. -__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
  22253. +__sigqueue_do_alloc(int sig, struct task_struct *t, gfp_t flags,
  22254. + int override_rlimit, int fromslab)
  22255. {
  22256. struct sigqueue *q = NULL;
  22257. struct user_struct *user;
  22258. @@ -375,7 +393,10 @@ __sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimi
  22259. if (override_rlimit ||
  22260. atomic_read(&user->sigpending) <=
  22261. task_rlimit(t, RLIMIT_SIGPENDING)) {
  22262. - q = kmem_cache_alloc(sigqueue_cachep, flags);
  22263. + if (!fromslab)
  22264. + q = get_task_cache(t);
  22265. + if (!q)
  22266. + q = kmem_cache_alloc(sigqueue_cachep, flags);
  22267. } else {
  22268. print_dropped_signal(sig);
  22269. }
  22270. @@ -392,6 +413,13 @@ __sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimi
  22271. return q;
  22272. }
  22273. +static struct sigqueue *
  22274. +__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags,
  22275. + int override_rlimit)
  22276. +{
  22277. + return __sigqueue_do_alloc(sig, t, flags, override_rlimit, 0);
  22278. +}
  22279. +
  22280. static void __sigqueue_free(struct sigqueue *q)
  22281. {
  22282. if (q->flags & SIGQUEUE_PREALLOC)
  22283. @@ -401,6 +429,21 @@ static void __sigqueue_free(struct sigqueue *q)
  22284. kmem_cache_free(sigqueue_cachep, q);
  22285. }
  22286. +static void sigqueue_free_current(struct sigqueue *q)
  22287. +{
  22288. + struct user_struct *up;
  22289. +
  22290. + if (q->flags & SIGQUEUE_PREALLOC)
  22291. + return;
  22292. +
  22293. + up = q->user;
  22294. + if (rt_prio(current->normal_prio) && !put_task_cache(current, q)) {
  22295. + atomic_dec(&up->sigpending);
  22296. + free_uid(up);
  22297. + } else
  22298. + __sigqueue_free(q);
  22299. +}
  22300. +
  22301. void flush_sigqueue(struct sigpending *queue)
  22302. {
  22303. struct sigqueue *q;
  22304. @@ -414,6 +457,21 @@ void flush_sigqueue(struct sigpending *queue)
  22305. }
  22306. /*
  22307. + * Called from __exit_signal. Flush tsk->pending and
  22308. + * tsk->sigqueue_cache
  22309. + */
  22310. +void flush_task_sigqueue(struct task_struct *tsk)
  22311. +{
  22312. + struct sigqueue *q;
  22313. +
  22314. + flush_sigqueue(&tsk->pending);
  22315. +
  22316. + q = get_task_cache(tsk);
  22317. + if (q)
  22318. + kmem_cache_free(sigqueue_cachep, q);
  22319. +}
  22320. +
  22321. +/*
  22322. * Flush all pending signals for this kthread.
  22323. */
  22324. void flush_signals(struct task_struct *t)
  22325. @@ -525,7 +583,7 @@ static void collect_signal(int sig, struct sigpending *list, siginfo_t *info)
  22326. still_pending:
  22327. list_del_init(&first->list);
  22328. copy_siginfo(info, &first->info);
  22329. - __sigqueue_free(first);
  22330. + sigqueue_free_current(first);
  22331. } else {
  22332. /*
  22333. * Ok, it wasn't in the queue. This must be
  22334. @@ -560,6 +618,8 @@ int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
  22335. {
  22336. int signr;
  22337. + WARN_ON_ONCE(tsk != current);
  22338. +
  22339. /* We only dequeue private signals from ourselves, we don't let
  22340. * signalfd steal them
  22341. */
  22342. @@ -1156,8 +1216,8 @@ int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
  22343. * We don't want to have recursive SIGSEGV's etc, for example,
  22344. * that is why we also clear SIGNAL_UNKILLABLE.
  22345. */
  22346. -int
  22347. -force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  22348. +static int
  22349. +do_force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  22350. {
  22351. unsigned long int flags;
  22352. int ret, blocked, ignored;
  22353. @@ -1182,6 +1242,39 @@ force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  22354. return ret;
  22355. }
  22356. +int force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  22357. +{
  22358. +/*
  22359. + * On some archs, PREEMPT_RT has to delay sending a signal from a trap
  22360. + * since it can not enable preemption, and the signal code's spin_locks
  22361. + * turn into mutexes. Instead, it must set TIF_NOTIFY_RESUME which will
  22362. + * send the signal on exit of the trap.
  22363. + */
  22364. +#ifdef ARCH_RT_DELAYS_SIGNAL_SEND
  22365. + if (in_atomic()) {
  22366. + if (WARN_ON_ONCE(t != current))
  22367. + return 0;
  22368. + if (WARN_ON_ONCE(t->forced_info.si_signo))
  22369. + return 0;
  22370. +
  22371. + if (is_si_special(info)) {
  22372. + WARN_ON_ONCE(info != SEND_SIG_PRIV);
  22373. + t->forced_info.si_signo = sig;
  22374. + t->forced_info.si_errno = 0;
  22375. + t->forced_info.si_code = SI_KERNEL;
  22376. + t->forced_info.si_pid = 0;
  22377. + t->forced_info.si_uid = 0;
  22378. + } else {
  22379. + t->forced_info = *info;
  22380. + }
  22381. +
  22382. + set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
  22383. + return 0;
  22384. + }
  22385. +#endif
  22386. + return do_force_sig_info(sig, info, t);
  22387. +}
  22388. +
  22389. /*
  22390. * Nuke all other threads in the group.
  22391. */
  22392. @@ -1216,12 +1309,12 @@ struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
  22393. * Disable interrupts early to avoid deadlocks.
  22394. * See rcu_read_unlock() comment header for details.
  22395. */
  22396. - local_irq_save(*flags);
  22397. + local_irq_save_nort(*flags);
  22398. rcu_read_lock();
  22399. sighand = rcu_dereference(tsk->sighand);
  22400. if (unlikely(sighand == NULL)) {
  22401. rcu_read_unlock();
  22402. - local_irq_restore(*flags);
  22403. + local_irq_restore_nort(*flags);
  22404. break;
  22405. }
  22406. /*
  22407. @@ -1242,7 +1335,7 @@ struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
  22408. }
  22409. spin_unlock(&sighand->siglock);
  22410. rcu_read_unlock();
  22411. - local_irq_restore(*flags);
  22412. + local_irq_restore_nort(*flags);
  22413. }
  22414. return sighand;
  22415. @@ -1485,7 +1578,8 @@ EXPORT_SYMBOL(kill_pid);
  22416. */
  22417. struct sigqueue *sigqueue_alloc(void)
  22418. {
  22419. - struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
  22420. + /* Preallocated sigqueue objects always from the slabcache ! */
  22421. + struct sigqueue *q = __sigqueue_do_alloc(-1, current, GFP_KERNEL, 0, 1);
  22422. if (q)
  22423. q->flags |= SIGQUEUE_PREALLOC;
  22424. @@ -1846,15 +1940,7 @@ static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
  22425. if (gstop_done && ptrace_reparented(current))
  22426. do_notify_parent_cldstop(current, false, why);
  22427. - /*
  22428. - * Don't want to allow preemption here, because
  22429. - * sys_ptrace() needs this task to be inactive.
  22430. - *
  22431. - * XXX: implement read_unlock_no_resched().
  22432. - */
  22433. - preempt_disable();
  22434. read_unlock(&tasklist_lock);
  22435. - preempt_enable_no_resched();
  22436. freezable_schedule();
  22437. } else {
  22438. /*
  22439. diff --git a/kernel/softirq.c b/kernel/softirq.c
  22440. index 479e4436f787..cb9c1d5dee10 100644
  22441. --- a/kernel/softirq.c
  22442. +++ b/kernel/softirq.c
  22443. @@ -21,10 +21,12 @@
  22444. #include <linux/freezer.h>
  22445. #include <linux/kthread.h>
  22446. #include <linux/rcupdate.h>
  22447. +#include <linux/delay.h>
  22448. #include <linux/ftrace.h>
  22449. #include <linux/smp.h>
  22450. #include <linux/smpboot.h>
  22451. #include <linux/tick.h>
  22452. +#include <linux/locallock.h>
  22453. #include <linux/irq.h>
  22454. #define CREATE_TRACE_POINTS
  22455. @@ -56,12 +58,108 @@ EXPORT_SYMBOL(irq_stat);
  22456. static struct softirq_action softirq_vec[NR_SOFTIRQS] __cacheline_aligned_in_smp;
  22457. DEFINE_PER_CPU(struct task_struct *, ksoftirqd);
  22458. +#ifdef CONFIG_PREEMPT_RT_FULL
  22459. +#define TIMER_SOFTIRQS ((1 << TIMER_SOFTIRQ) | (1 << HRTIMER_SOFTIRQ))
  22460. +DEFINE_PER_CPU(struct task_struct *, ktimer_softirqd);
  22461. +#endif
  22462. const char * const softirq_to_name[NR_SOFTIRQS] = {
  22463. "HI", "TIMER", "NET_TX", "NET_RX", "BLOCK", "BLOCK_IOPOLL",
  22464. "TASKLET", "SCHED", "HRTIMER", "RCU"
  22465. };
  22466. +#ifdef CONFIG_NO_HZ_COMMON
  22467. +# ifdef CONFIG_PREEMPT_RT_FULL
  22468. +
  22469. +struct softirq_runner {
  22470. + struct task_struct *runner[NR_SOFTIRQS];
  22471. +};
  22472. +
  22473. +static DEFINE_PER_CPU(struct softirq_runner, softirq_runners);
  22474. +
  22475. +static inline void softirq_set_runner(unsigned int sirq)
  22476. +{
  22477. + struct softirq_runner *sr = this_cpu_ptr(&softirq_runners);
  22478. +
  22479. + sr->runner[sirq] = current;
  22480. +}
  22481. +
  22482. +static inline void softirq_clr_runner(unsigned int sirq)
  22483. +{
  22484. + struct softirq_runner *sr = this_cpu_ptr(&softirq_runners);
  22485. +
  22486. + sr->runner[sirq] = NULL;
  22487. +}
  22488. +
  22489. +/*
  22490. + * On preempt-rt a softirq running context might be blocked on a
  22491. + * lock. There might be no other runnable task on this CPU because the
  22492. + * lock owner runs on some other CPU. So we have to go into idle with
  22493. + * the pending bit set. Therefor we need to check this otherwise we
  22494. + * warn about false positives which confuses users and defeats the
  22495. + * whole purpose of this test.
  22496. + *
  22497. + * This code is called with interrupts disabled.
  22498. + */
  22499. +void softirq_check_pending_idle(void)
  22500. +{
  22501. + static int rate_limit;
  22502. + struct softirq_runner *sr = this_cpu_ptr(&softirq_runners);
  22503. + u32 warnpending;
  22504. + int i;
  22505. +
  22506. + if (rate_limit >= 10)
  22507. + return;
  22508. +
  22509. + warnpending = local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK;
  22510. + for (i = 0; i < NR_SOFTIRQS; i++) {
  22511. + struct task_struct *tsk = sr->runner[i];
  22512. +
  22513. + /*
  22514. + * The wakeup code in rtmutex.c wakes up the task
  22515. + * _before_ it sets pi_blocked_on to NULL under
  22516. + * tsk->pi_lock. So we need to check for both: state
  22517. + * and pi_blocked_on.
  22518. + */
  22519. + if (tsk) {
  22520. + raw_spin_lock(&tsk->pi_lock);
  22521. + if (tsk->pi_blocked_on || tsk->state == TASK_RUNNING) {
  22522. + /* Clear all bits pending in that task */
  22523. + warnpending &= ~(tsk->softirqs_raised);
  22524. + warnpending &= ~(1 << i);
  22525. + }
  22526. + raw_spin_unlock(&tsk->pi_lock);
  22527. + }
  22528. + }
  22529. +
  22530. + if (warnpending) {
  22531. + printk(KERN_ERR "NOHZ: local_softirq_pending %02x\n",
  22532. + warnpending);
  22533. + rate_limit++;
  22534. + }
  22535. +}
  22536. +# else
  22537. +/*
  22538. + * On !PREEMPT_RT we just printk rate limited:
  22539. + */
  22540. +void softirq_check_pending_idle(void)
  22541. +{
  22542. + static int rate_limit;
  22543. +
  22544. + if (rate_limit < 10 &&
  22545. + (local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK)) {
  22546. + printk(KERN_ERR "NOHZ: local_softirq_pending %02x\n",
  22547. + local_softirq_pending());
  22548. + rate_limit++;
  22549. + }
  22550. +}
  22551. +# endif
  22552. +
  22553. +#else /* !CONFIG_NO_HZ_COMMON */
  22554. +static inline void softirq_set_runner(unsigned int sirq) { }
  22555. +static inline void softirq_clr_runner(unsigned int sirq) { }
  22556. +#endif
  22557. +
  22558. /*
  22559. * we cannot loop indefinitely here to avoid userspace starvation,
  22560. * but we also don't want to introduce a worst case 1/HZ latency
  22561. @@ -77,6 +175,79 @@ static void wakeup_softirqd(void)
  22562. wake_up_process(tsk);
  22563. }
  22564. +#ifdef CONFIG_PREEMPT_RT_FULL
  22565. +static void wakeup_timer_softirqd(void)
  22566. +{
  22567. + /* Interrupts are disabled: no need to stop preemption */
  22568. + struct task_struct *tsk = __this_cpu_read(ktimer_softirqd);
  22569. +
  22570. + if (tsk && tsk->state != TASK_RUNNING)
  22571. + wake_up_process(tsk);
  22572. +}
  22573. +#endif
  22574. +
  22575. +static void handle_softirq(unsigned int vec_nr)
  22576. +{
  22577. + struct softirq_action *h = softirq_vec + vec_nr;
  22578. + int prev_count;
  22579. +
  22580. + prev_count = preempt_count();
  22581. +
  22582. + kstat_incr_softirqs_this_cpu(vec_nr);
  22583. +
  22584. + trace_softirq_entry(vec_nr);
  22585. + h->action(h);
  22586. + trace_softirq_exit(vec_nr);
  22587. + if (unlikely(prev_count != preempt_count())) {
  22588. + pr_err("huh, entered softirq %u %s %p with preempt_count %08x, exited with %08x?\n",
  22589. + vec_nr, softirq_to_name[vec_nr], h->action,
  22590. + prev_count, preempt_count());
  22591. + preempt_count_set(prev_count);
  22592. + }
  22593. +}
  22594. +
  22595. +#ifndef CONFIG_PREEMPT_RT_FULL
  22596. +static inline int ksoftirqd_softirq_pending(void)
  22597. +{
  22598. + return local_softirq_pending();
  22599. +}
  22600. +
  22601. +static void handle_pending_softirqs(u32 pending)
  22602. +{
  22603. + struct softirq_action *h = softirq_vec;
  22604. + int softirq_bit;
  22605. +
  22606. + local_irq_enable();
  22607. +
  22608. + h = softirq_vec;
  22609. +
  22610. + while ((softirq_bit = ffs(pending))) {
  22611. + unsigned int vec_nr;
  22612. +
  22613. + h += softirq_bit - 1;
  22614. + vec_nr = h - softirq_vec;
  22615. + handle_softirq(vec_nr);
  22616. +
  22617. + h++;
  22618. + pending >>= softirq_bit;
  22619. + }
  22620. +
  22621. + rcu_bh_qs();
  22622. + local_irq_disable();
  22623. +}
  22624. +
  22625. +static void run_ksoftirqd(unsigned int cpu)
  22626. +{
  22627. + local_irq_disable();
  22628. + if (ksoftirqd_softirq_pending()) {
  22629. + __do_softirq();
  22630. + local_irq_enable();
  22631. + cond_resched_rcu_qs();
  22632. + return;
  22633. + }
  22634. + local_irq_enable();
  22635. +}
  22636. +
  22637. /*
  22638. * preempt_count and SOFTIRQ_OFFSET usage:
  22639. * - preempt_count is changed by SOFTIRQ_OFFSET on entering or leaving
  22640. @@ -116,9 +287,9 @@ void __local_bh_disable_ip(unsigned long ip, unsigned int cnt)
  22641. if (preempt_count() == cnt) {
  22642. #ifdef CONFIG_DEBUG_PREEMPT
  22643. - current->preempt_disable_ip = get_parent_ip(CALLER_ADDR1);
  22644. + current->preempt_disable_ip = get_lock_parent_ip();
  22645. #endif
  22646. - trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
  22647. + trace_preempt_off(CALLER_ADDR0, get_lock_parent_ip());
  22648. }
  22649. }
  22650. EXPORT_SYMBOL(__local_bh_disable_ip);
  22651. @@ -232,10 +403,8 @@ asmlinkage __visible void __do_softirq(void)
  22652. unsigned long end = jiffies + MAX_SOFTIRQ_TIME;
  22653. unsigned long old_flags = current->flags;
  22654. int max_restart = MAX_SOFTIRQ_RESTART;
  22655. - struct softirq_action *h;
  22656. bool in_hardirq;
  22657. __u32 pending;
  22658. - int softirq_bit;
  22659. /*
  22660. * Mask out PF_MEMALLOC s current task context is borrowed for the
  22661. @@ -254,36 +423,7 @@ restart:
  22662. /* Reset the pending bitmask before enabling irqs */
  22663. set_softirq_pending(0);
  22664. - local_irq_enable();
  22665. -
  22666. - h = softirq_vec;
  22667. -
  22668. - while ((softirq_bit = ffs(pending))) {
  22669. - unsigned int vec_nr;
  22670. - int prev_count;
  22671. -
  22672. - h += softirq_bit - 1;
  22673. -
  22674. - vec_nr = h - softirq_vec;
  22675. - prev_count = preempt_count();
  22676. -
  22677. - kstat_incr_softirqs_this_cpu(vec_nr);
  22678. -
  22679. - trace_softirq_entry(vec_nr);
  22680. - h->action(h);
  22681. - trace_softirq_exit(vec_nr);
  22682. - if (unlikely(prev_count != preempt_count())) {
  22683. - pr_err("huh, entered softirq %u %s %p with preempt_count %08x, exited with %08x?\n",
  22684. - vec_nr, softirq_to_name[vec_nr], h->action,
  22685. - prev_count, preempt_count());
  22686. - preempt_count_set(prev_count);
  22687. - }
  22688. - h++;
  22689. - pending >>= softirq_bit;
  22690. - }
  22691. -
  22692. - rcu_bh_qs();
  22693. - local_irq_disable();
  22694. + handle_pending_softirqs(pending);
  22695. pending = local_softirq_pending();
  22696. if (pending) {
  22697. @@ -320,6 +460,310 @@ asmlinkage __visible void do_softirq(void)
  22698. }
  22699. /*
  22700. + * This function must run with irqs disabled!
  22701. + */
  22702. +void raise_softirq_irqoff(unsigned int nr)
  22703. +{
  22704. + __raise_softirq_irqoff(nr);
  22705. +
  22706. + /*
  22707. + * If we're in an interrupt or softirq, we're done
  22708. + * (this also catches softirq-disabled code). We will
  22709. + * actually run the softirq once we return from
  22710. + * the irq or softirq.
  22711. + *
  22712. + * Otherwise we wake up ksoftirqd to make sure we
  22713. + * schedule the softirq soon.
  22714. + */
  22715. + if (!in_interrupt())
  22716. + wakeup_softirqd();
  22717. +}
  22718. +
  22719. +void __raise_softirq_irqoff(unsigned int nr)
  22720. +{
  22721. + trace_softirq_raise(nr);
  22722. + or_softirq_pending(1UL << nr);
  22723. +}
  22724. +
  22725. +static inline void local_bh_disable_nort(void) { local_bh_disable(); }
  22726. +static inline void _local_bh_enable_nort(void) { _local_bh_enable(); }
  22727. +static void ksoftirqd_set_sched_params(unsigned int cpu) { }
  22728. +
  22729. +#else /* !PREEMPT_RT_FULL */
  22730. +
  22731. +/*
  22732. + * On RT we serialize softirq execution with a cpu local lock per softirq
  22733. + */
  22734. +static DEFINE_PER_CPU(struct local_irq_lock [NR_SOFTIRQS], local_softirq_locks);
  22735. +
  22736. +void __init softirq_early_init(void)
  22737. +{
  22738. + int i;
  22739. +
  22740. + for (i = 0; i < NR_SOFTIRQS; i++)
  22741. + local_irq_lock_init(local_softirq_locks[i]);
  22742. +}
  22743. +
  22744. +static void lock_softirq(int which)
  22745. +{
  22746. + local_lock(local_softirq_locks[which]);
  22747. +}
  22748. +
  22749. +static void unlock_softirq(int which)
  22750. +{
  22751. + local_unlock(local_softirq_locks[which]);
  22752. +}
  22753. +
  22754. +static void do_single_softirq(int which)
  22755. +{
  22756. + unsigned long old_flags = current->flags;
  22757. +
  22758. + current->flags &= ~PF_MEMALLOC;
  22759. + vtime_account_irq_enter(current);
  22760. + current->flags |= PF_IN_SOFTIRQ;
  22761. + lockdep_softirq_enter();
  22762. + local_irq_enable();
  22763. + handle_softirq(which);
  22764. + local_irq_disable();
  22765. + lockdep_softirq_exit();
  22766. + current->flags &= ~PF_IN_SOFTIRQ;
  22767. + vtime_account_irq_enter(current);
  22768. + tsk_restore_flags(current, old_flags, PF_MEMALLOC);
  22769. +}
  22770. +
  22771. +/*
  22772. + * Called with interrupts disabled. Process softirqs which were raised
  22773. + * in current context (or on behalf of ksoftirqd).
  22774. + */
  22775. +static void do_current_softirqs(void)
  22776. +{
  22777. + while (current->softirqs_raised) {
  22778. + int i = __ffs(current->softirqs_raised);
  22779. + unsigned int pending, mask = (1U << i);
  22780. +
  22781. + current->softirqs_raised &= ~mask;
  22782. + local_irq_enable();
  22783. +
  22784. + /*
  22785. + * If the lock is contended, we boost the owner to
  22786. + * process the softirq or leave the critical section
  22787. + * now.
  22788. + */
  22789. + lock_softirq(i);
  22790. + local_irq_disable();
  22791. + softirq_set_runner(i);
  22792. + /*
  22793. + * Check with the local_softirq_pending() bits,
  22794. + * whether we need to process this still or if someone
  22795. + * else took care of it.
  22796. + */
  22797. + pending = local_softirq_pending();
  22798. + if (pending & mask) {
  22799. + set_softirq_pending(pending & ~mask);
  22800. + do_single_softirq(i);
  22801. + }
  22802. + softirq_clr_runner(i);
  22803. + WARN_ON(current->softirq_nestcnt != 1);
  22804. + local_irq_enable();
  22805. + unlock_softirq(i);
  22806. + local_irq_disable();
  22807. + }
  22808. +}
  22809. +
  22810. +void __local_bh_disable(void)
  22811. +{
  22812. + if (++current->softirq_nestcnt == 1)
  22813. + migrate_disable();
  22814. +}
  22815. +EXPORT_SYMBOL(__local_bh_disable);
  22816. +
  22817. +void __local_bh_enable(void)
  22818. +{
  22819. + if (WARN_ON(current->softirq_nestcnt == 0))
  22820. + return;
  22821. +
  22822. + local_irq_disable();
  22823. + if (current->softirq_nestcnt == 1 && current->softirqs_raised)
  22824. + do_current_softirqs();
  22825. + local_irq_enable();
  22826. +
  22827. + if (--current->softirq_nestcnt == 0)
  22828. + migrate_enable();
  22829. +}
  22830. +EXPORT_SYMBOL(__local_bh_enable);
  22831. +
  22832. +void _local_bh_enable(void)
  22833. +{
  22834. + if (WARN_ON(current->softirq_nestcnt == 0))
  22835. + return;
  22836. + if (--current->softirq_nestcnt == 0)
  22837. + migrate_enable();
  22838. +}
  22839. +EXPORT_SYMBOL(_local_bh_enable);
  22840. +
  22841. +int in_serving_softirq(void)
  22842. +{
  22843. + return current->flags & PF_IN_SOFTIRQ;
  22844. +}
  22845. +EXPORT_SYMBOL(in_serving_softirq);
  22846. +
  22847. +/* Called with preemption disabled */
  22848. +static void run_ksoftirqd(unsigned int cpu)
  22849. +{
  22850. + local_irq_disable();
  22851. + current->softirq_nestcnt++;
  22852. +
  22853. + do_current_softirqs();
  22854. + current->softirq_nestcnt--;
  22855. + local_irq_enable();
  22856. + cond_resched_rcu_qs();
  22857. +}
  22858. +
  22859. +/*
  22860. + * Called from netif_rx_ni(). Preemption enabled, but migration
  22861. + * disabled. So the cpu can't go away under us.
  22862. + */
  22863. +void thread_do_softirq(void)
  22864. +{
  22865. + if (!in_serving_softirq() && current->softirqs_raised) {
  22866. + current->softirq_nestcnt++;
  22867. + do_current_softirqs();
  22868. + current->softirq_nestcnt--;
  22869. + }
  22870. +}
  22871. +
  22872. +static void do_raise_softirq_irqoff(unsigned int nr)
  22873. +{
  22874. + unsigned int mask;
  22875. +
  22876. + mask = 1UL << nr;
  22877. +
  22878. + trace_softirq_raise(nr);
  22879. + or_softirq_pending(mask);
  22880. +
  22881. + /*
  22882. + * If we are not in a hard interrupt and inside a bh disabled
  22883. + * region, we simply raise the flag on current. local_bh_enable()
  22884. + * will make sure that the softirq is executed. Otherwise we
  22885. + * delegate it to ksoftirqd.
  22886. + */
  22887. + if (!in_irq() && current->softirq_nestcnt)
  22888. + current->softirqs_raised |= mask;
  22889. + else if (!__this_cpu_read(ksoftirqd) || !__this_cpu_read(ktimer_softirqd))
  22890. + return;
  22891. +
  22892. + if (mask & TIMER_SOFTIRQS)
  22893. + __this_cpu_read(ktimer_softirqd)->softirqs_raised |= mask;
  22894. + else
  22895. + __this_cpu_read(ksoftirqd)->softirqs_raised |= mask;
  22896. +}
  22897. +
  22898. +static void wakeup_proper_softirq(unsigned int nr)
  22899. +{
  22900. + if ((1UL << nr) & TIMER_SOFTIRQS)
  22901. + wakeup_timer_softirqd();
  22902. + else
  22903. + wakeup_softirqd();
  22904. +}
  22905. +
  22906. +
  22907. +void __raise_softirq_irqoff(unsigned int nr)
  22908. +{
  22909. + do_raise_softirq_irqoff(nr);
  22910. + if (!in_irq() && !current->softirq_nestcnt)
  22911. + wakeup_proper_softirq(nr);
  22912. +}
  22913. +
  22914. +/*
  22915. + * Same as __raise_softirq_irqoff() but will process them in ksoftirqd
  22916. + */
  22917. +void __raise_softirq_irqoff_ksoft(unsigned int nr)
  22918. +{
  22919. + unsigned int mask;
  22920. +
  22921. + if (WARN_ON_ONCE(!__this_cpu_read(ksoftirqd) ||
  22922. + !__this_cpu_read(ktimer_softirqd)))
  22923. + return;
  22924. + mask = 1UL << nr;
  22925. +
  22926. + trace_softirq_raise(nr);
  22927. + or_softirq_pending(mask);
  22928. + if (mask & TIMER_SOFTIRQS)
  22929. + __this_cpu_read(ktimer_softirqd)->softirqs_raised |= mask;
  22930. + else
  22931. + __this_cpu_read(ksoftirqd)->softirqs_raised |= mask;
  22932. + wakeup_proper_softirq(nr);
  22933. +}
  22934. +
  22935. +/*
  22936. + * This function must run with irqs disabled!
  22937. + */
  22938. +void raise_softirq_irqoff(unsigned int nr)
  22939. +{
  22940. + do_raise_softirq_irqoff(nr);
  22941. +
  22942. + /*
  22943. + * If we're in an hard interrupt we let irq return code deal
  22944. + * with the wakeup of ksoftirqd.
  22945. + */
  22946. + if (in_irq())
  22947. + return;
  22948. + /*
  22949. + * If we are in thread context but outside of a bh disabled
  22950. + * region, we need to wake ksoftirqd as well.
  22951. + *
  22952. + * CHECKME: Some of the places which do that could be wrapped
  22953. + * into local_bh_disable/enable pairs. Though it's unclear
  22954. + * whether this is worth the effort. To find those places just
  22955. + * raise a WARN() if the condition is met.
  22956. + */
  22957. + if (!current->softirq_nestcnt)
  22958. + wakeup_proper_softirq(nr);
  22959. +}
  22960. +
  22961. +static inline int ksoftirqd_softirq_pending(void)
  22962. +{
  22963. + return current->softirqs_raised;
  22964. +}
  22965. +
  22966. +static inline void local_bh_disable_nort(void) { }
  22967. +static inline void _local_bh_enable_nort(void) { }
  22968. +
  22969. +static inline void ksoftirqd_set_sched_params(unsigned int cpu)
  22970. +{
  22971. + /* Take over all but timer pending softirqs when starting */
  22972. + local_irq_disable();
  22973. + current->softirqs_raised = local_softirq_pending() & ~TIMER_SOFTIRQS;
  22974. + local_irq_enable();
  22975. +}
  22976. +
  22977. +static inline void ktimer_softirqd_set_sched_params(unsigned int cpu)
  22978. +{
  22979. + struct sched_param param = { .sched_priority = 1 };
  22980. +
  22981. + sched_setscheduler(current, SCHED_FIFO, &param);
  22982. +
  22983. + /* Take over timer pending softirqs when starting */
  22984. + local_irq_disable();
  22985. + current->softirqs_raised = local_softirq_pending() & TIMER_SOFTIRQS;
  22986. + local_irq_enable();
  22987. +}
  22988. +
  22989. +static inline void ktimer_softirqd_clr_sched_params(unsigned int cpu,
  22990. + bool online)
  22991. +{
  22992. + struct sched_param param = { .sched_priority = 0 };
  22993. +
  22994. + sched_setscheduler(current, SCHED_NORMAL, &param);
  22995. +}
  22996. +
  22997. +static int ktimer_softirqd_should_run(unsigned int cpu)
  22998. +{
  22999. + return current->softirqs_raised;
  23000. +}
  23001. +
  23002. +#endif /* PREEMPT_RT_FULL */
  23003. +/*
  23004. * Enter an interrupt context.
  23005. */
  23006. void irq_enter(void)
  23007. @@ -330,9 +774,9 @@ void irq_enter(void)
  23008. * Prevent raise_softirq from needlessly waking up ksoftirqd
  23009. * here, as softirq will be serviced on return from interrupt.
  23010. */
  23011. - local_bh_disable();
  23012. + local_bh_disable_nort();
  23013. tick_irq_enter();
  23014. - _local_bh_enable();
  23015. + _local_bh_enable_nort();
  23016. }
  23017. __irq_enter();
  23018. @@ -340,6 +784,7 @@ void irq_enter(void)
  23019. static inline void invoke_softirq(void)
  23020. {
  23021. +#ifndef CONFIG_PREEMPT_RT_FULL
  23022. if (!force_irqthreads) {
  23023. #ifdef CONFIG_HAVE_IRQ_EXIT_ON_IRQ_STACK
  23024. /*
  23025. @@ -359,6 +804,18 @@ static inline void invoke_softirq(void)
  23026. } else {
  23027. wakeup_softirqd();
  23028. }
  23029. +#else /* PREEMPT_RT_FULL */
  23030. + unsigned long flags;
  23031. +
  23032. + local_irq_save(flags);
  23033. + if (__this_cpu_read(ksoftirqd) &&
  23034. + __this_cpu_read(ksoftirqd)->softirqs_raised)
  23035. + wakeup_softirqd();
  23036. + if (__this_cpu_read(ktimer_softirqd) &&
  23037. + __this_cpu_read(ktimer_softirqd)->softirqs_raised)
  23038. + wakeup_timer_softirqd();
  23039. + local_irq_restore(flags);
  23040. +#endif
  23041. }
  23042. static inline void tick_irq_exit(void)
  23043. @@ -395,26 +852,6 @@ void irq_exit(void)
  23044. trace_hardirq_exit(); /* must be last! */
  23045. }
  23046. -/*
  23047. - * This function must run with irqs disabled!
  23048. - */
  23049. -inline void raise_softirq_irqoff(unsigned int nr)
  23050. -{
  23051. - __raise_softirq_irqoff(nr);
  23052. -
  23053. - /*
  23054. - * If we're in an interrupt or softirq, we're done
  23055. - * (this also catches softirq-disabled code). We will
  23056. - * actually run the softirq once we return from
  23057. - * the irq or softirq.
  23058. - *
  23059. - * Otherwise we wake up ksoftirqd to make sure we
  23060. - * schedule the softirq soon.
  23061. - */
  23062. - if (!in_interrupt())
  23063. - wakeup_softirqd();
  23064. -}
  23065. -
  23066. void raise_softirq(unsigned int nr)
  23067. {
  23068. unsigned long flags;
  23069. @@ -424,12 +861,6 @@ void raise_softirq(unsigned int nr)
  23070. local_irq_restore(flags);
  23071. }
  23072. -void __raise_softirq_irqoff(unsigned int nr)
  23073. -{
  23074. - trace_softirq_raise(nr);
  23075. - or_softirq_pending(1UL << nr);
  23076. -}
  23077. -
  23078. void open_softirq(int nr, void (*action)(struct softirq_action *))
  23079. {
  23080. softirq_vec[nr].action = action;
  23081. @@ -446,15 +877,45 @@ struct tasklet_head {
  23082. static DEFINE_PER_CPU(struct tasklet_head, tasklet_vec);
  23083. static DEFINE_PER_CPU(struct tasklet_head, tasklet_hi_vec);
  23084. +static void inline
  23085. +__tasklet_common_schedule(struct tasklet_struct *t, struct tasklet_head *head, unsigned int nr)
  23086. +{
  23087. + if (tasklet_trylock(t)) {
  23088. +again:
  23089. + /* We may have been preempted before tasklet_trylock
  23090. + * and __tasklet_action may have already run.
  23091. + * So double check the sched bit while the takslet
  23092. + * is locked before adding it to the list.
  23093. + */
  23094. + if (test_bit(TASKLET_STATE_SCHED, &t->state)) {
  23095. + t->next = NULL;
  23096. + *head->tail = t;
  23097. + head->tail = &(t->next);
  23098. + raise_softirq_irqoff(nr);
  23099. + tasklet_unlock(t);
  23100. + } else {
  23101. + /* This is subtle. If we hit the corner case above
  23102. + * It is possible that we get preempted right here,
  23103. + * and another task has successfully called
  23104. + * tasklet_schedule(), then this function, and
  23105. + * failed on the trylock. Thus we must be sure
  23106. + * before releasing the tasklet lock, that the
  23107. + * SCHED_BIT is clear. Otherwise the tasklet
  23108. + * may get its SCHED_BIT set, but not added to the
  23109. + * list
  23110. + */
  23111. + if (!tasklet_tryunlock(t))
  23112. + goto again;
  23113. + }
  23114. + }
  23115. +}
  23116. +
  23117. void __tasklet_schedule(struct tasklet_struct *t)
  23118. {
  23119. unsigned long flags;
  23120. local_irq_save(flags);
  23121. - t->next = NULL;
  23122. - *__this_cpu_read(tasklet_vec.tail) = t;
  23123. - __this_cpu_write(tasklet_vec.tail, &(t->next));
  23124. - raise_softirq_irqoff(TASKLET_SOFTIRQ);
  23125. + __tasklet_common_schedule(t, this_cpu_ptr(&tasklet_vec), TASKLET_SOFTIRQ);
  23126. local_irq_restore(flags);
  23127. }
  23128. EXPORT_SYMBOL(__tasklet_schedule);
  23129. @@ -464,10 +925,7 @@ void __tasklet_hi_schedule(struct tasklet_struct *t)
  23130. unsigned long flags;
  23131. local_irq_save(flags);
  23132. - t->next = NULL;
  23133. - *__this_cpu_read(tasklet_hi_vec.tail) = t;
  23134. - __this_cpu_write(tasklet_hi_vec.tail, &(t->next));
  23135. - raise_softirq_irqoff(HI_SOFTIRQ);
  23136. + __tasklet_common_schedule(t, this_cpu_ptr(&tasklet_hi_vec), HI_SOFTIRQ);
  23137. local_irq_restore(flags);
  23138. }
  23139. EXPORT_SYMBOL(__tasklet_hi_schedule);
  23140. @@ -476,82 +934,122 @@ void __tasklet_hi_schedule_first(struct tasklet_struct *t)
  23141. {
  23142. BUG_ON(!irqs_disabled());
  23143. - t->next = __this_cpu_read(tasklet_hi_vec.head);
  23144. - __this_cpu_write(tasklet_hi_vec.head, t);
  23145. - __raise_softirq_irqoff(HI_SOFTIRQ);
  23146. + __tasklet_hi_schedule(t);
  23147. }
  23148. EXPORT_SYMBOL(__tasklet_hi_schedule_first);
  23149. -static void tasklet_action(struct softirq_action *a)
  23150. +void tasklet_enable(struct tasklet_struct *t)
  23151. {
  23152. - struct tasklet_struct *list;
  23153. + if (!atomic_dec_and_test(&t->count))
  23154. + return;
  23155. + if (test_and_clear_bit(TASKLET_STATE_PENDING, &t->state))
  23156. + tasklet_schedule(t);
  23157. +}
  23158. +EXPORT_SYMBOL(tasklet_enable);
  23159. - local_irq_disable();
  23160. - list = __this_cpu_read(tasklet_vec.head);
  23161. - __this_cpu_write(tasklet_vec.head, NULL);
  23162. - __this_cpu_write(tasklet_vec.tail, this_cpu_ptr(&tasklet_vec.head));
  23163. - local_irq_enable();
  23164. +static void __tasklet_action(struct softirq_action *a,
  23165. + struct tasklet_struct *list)
  23166. +{
  23167. + int loops = 1000000;
  23168. while (list) {
  23169. struct tasklet_struct *t = list;
  23170. list = list->next;
  23171. - if (tasklet_trylock(t)) {
  23172. - if (!atomic_read(&t->count)) {
  23173. - if (!test_and_clear_bit(TASKLET_STATE_SCHED,
  23174. - &t->state))
  23175. - BUG();
  23176. - t->func(t->data);
  23177. - tasklet_unlock(t);
  23178. - continue;
  23179. - }
  23180. - tasklet_unlock(t);
  23181. + /*
  23182. + * Should always succeed - after a tasklist got on the
  23183. + * list (after getting the SCHED bit set from 0 to 1),
  23184. + * nothing but the tasklet softirq it got queued to can
  23185. + * lock it:
  23186. + */
  23187. + if (!tasklet_trylock(t)) {
  23188. + WARN_ON(1);
  23189. + continue;
  23190. }
  23191. - local_irq_disable();
  23192. t->next = NULL;
  23193. - *__this_cpu_read(tasklet_vec.tail) = t;
  23194. - __this_cpu_write(tasklet_vec.tail, &(t->next));
  23195. - __raise_softirq_irqoff(TASKLET_SOFTIRQ);
  23196. - local_irq_enable();
  23197. +
  23198. + /*
  23199. + * If we cannot handle the tasklet because it's disabled,
  23200. + * mark it as pending. tasklet_enable() will later
  23201. + * re-schedule the tasklet.
  23202. + */
  23203. + if (unlikely(atomic_read(&t->count))) {
  23204. +out_disabled:
  23205. + /* implicit unlock: */
  23206. + wmb();
  23207. + t->state = TASKLET_STATEF_PENDING;
  23208. + continue;
  23209. + }
  23210. +
  23211. + /*
  23212. + * After this point on the tasklet might be rescheduled
  23213. + * on another CPU, but it can only be added to another
  23214. + * CPU's tasklet list if we unlock the tasklet (which we
  23215. + * dont do yet).
  23216. + */
  23217. + if (!test_and_clear_bit(TASKLET_STATE_SCHED, &t->state))
  23218. + WARN_ON(1);
  23219. +
  23220. +again:
  23221. + t->func(t->data);
  23222. +
  23223. + /*
  23224. + * Try to unlock the tasklet. We must use cmpxchg, because
  23225. + * another CPU might have scheduled or disabled the tasklet.
  23226. + * We only allow the STATE_RUN -> 0 transition here.
  23227. + */
  23228. + while (!tasklet_tryunlock(t)) {
  23229. + /*
  23230. + * If it got disabled meanwhile, bail out:
  23231. + */
  23232. + if (atomic_read(&t->count))
  23233. + goto out_disabled;
  23234. + /*
  23235. + * If it got scheduled meanwhile, re-execute
  23236. + * the tasklet function:
  23237. + */
  23238. + if (test_and_clear_bit(TASKLET_STATE_SCHED, &t->state))
  23239. + goto again;
  23240. + if (!--loops) {
  23241. + printk("hm, tasklet state: %08lx\n", t->state);
  23242. + WARN_ON(1);
  23243. + tasklet_unlock(t);
  23244. + break;
  23245. + }
  23246. + }
  23247. }
  23248. }
  23249. +static void tasklet_action(struct softirq_action *a)
  23250. +{
  23251. + struct tasklet_struct *list;
  23252. +
  23253. + local_irq_disable();
  23254. +
  23255. + list = __this_cpu_read(tasklet_vec.head);
  23256. + __this_cpu_write(tasklet_vec.head, NULL);
  23257. + __this_cpu_write(tasklet_vec.tail, this_cpu_ptr(&tasklet_vec.head));
  23258. +
  23259. + local_irq_enable();
  23260. +
  23261. + __tasklet_action(a, list);
  23262. +}
  23263. +
  23264. static void tasklet_hi_action(struct softirq_action *a)
  23265. {
  23266. struct tasklet_struct *list;
  23267. local_irq_disable();
  23268. +
  23269. list = __this_cpu_read(tasklet_hi_vec.head);
  23270. __this_cpu_write(tasklet_hi_vec.head, NULL);
  23271. __this_cpu_write(tasklet_hi_vec.tail, this_cpu_ptr(&tasklet_hi_vec.head));
  23272. - local_irq_enable();
  23273. - while (list) {
  23274. - struct tasklet_struct *t = list;
  23275. -
  23276. - list = list->next;
  23277. -
  23278. - if (tasklet_trylock(t)) {
  23279. - if (!atomic_read(&t->count)) {
  23280. - if (!test_and_clear_bit(TASKLET_STATE_SCHED,
  23281. - &t->state))
  23282. - BUG();
  23283. - t->func(t->data);
  23284. - tasklet_unlock(t);
  23285. - continue;
  23286. - }
  23287. - tasklet_unlock(t);
  23288. - }
  23289. + local_irq_enable();
  23290. - local_irq_disable();
  23291. - t->next = NULL;
  23292. - *__this_cpu_read(tasklet_hi_vec.tail) = t;
  23293. - __this_cpu_write(tasklet_hi_vec.tail, &(t->next));
  23294. - __raise_softirq_irqoff(HI_SOFTIRQ);
  23295. - local_irq_enable();
  23296. - }
  23297. + __tasklet_action(a, list);
  23298. }
  23299. void tasklet_init(struct tasklet_struct *t,
  23300. @@ -572,7 +1070,7 @@ void tasklet_kill(struct tasklet_struct *t)
  23301. while (test_and_set_bit(TASKLET_STATE_SCHED, &t->state)) {
  23302. do {
  23303. - yield();
  23304. + msleep(1);
  23305. } while (test_bit(TASKLET_STATE_SCHED, &t->state));
  23306. }
  23307. tasklet_unlock_wait(t);
  23308. @@ -646,25 +1144,26 @@ void __init softirq_init(void)
  23309. open_softirq(HI_SOFTIRQ, tasklet_hi_action);
  23310. }
  23311. -static int ksoftirqd_should_run(unsigned int cpu)
  23312. -{
  23313. - return local_softirq_pending();
  23314. -}
  23315. -
  23316. -static void run_ksoftirqd(unsigned int cpu)
  23317. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  23318. +void tasklet_unlock_wait(struct tasklet_struct *t)
  23319. {
  23320. - local_irq_disable();
  23321. - if (local_softirq_pending()) {
  23322. + while (test_bit(TASKLET_STATE_RUN, &(t)->state)) {
  23323. /*
  23324. - * We can safely run softirq on inline stack, as we are not deep
  23325. - * in the task stack here.
  23326. + * Hack for now to avoid this busy-loop:
  23327. */
  23328. - __do_softirq();
  23329. - local_irq_enable();
  23330. - cond_resched_rcu_qs();
  23331. - return;
  23332. +#ifdef CONFIG_PREEMPT_RT_FULL
  23333. + msleep(1);
  23334. +#else
  23335. + barrier();
  23336. +#endif
  23337. }
  23338. - local_irq_enable();
  23339. +}
  23340. +EXPORT_SYMBOL(tasklet_unlock_wait);
  23341. +#endif
  23342. +
  23343. +static int ksoftirqd_should_run(unsigned int cpu)
  23344. +{
  23345. + return ksoftirqd_softirq_pending();
  23346. }
  23347. #ifdef CONFIG_HOTPLUG_CPU
  23348. @@ -746,16 +1245,31 @@ static struct notifier_block cpu_nfb = {
  23349. static struct smp_hotplug_thread softirq_threads = {
  23350. .store = &ksoftirqd,
  23351. + .setup = ksoftirqd_set_sched_params,
  23352. .thread_should_run = ksoftirqd_should_run,
  23353. .thread_fn = run_ksoftirqd,
  23354. .thread_comm = "ksoftirqd/%u",
  23355. };
  23356. +#ifdef CONFIG_PREEMPT_RT_FULL
  23357. +static struct smp_hotplug_thread softirq_timer_threads = {
  23358. + .store = &ktimer_softirqd,
  23359. + .setup = ktimer_softirqd_set_sched_params,
  23360. + .cleanup = ktimer_softirqd_clr_sched_params,
  23361. + .thread_should_run = ktimer_softirqd_should_run,
  23362. + .thread_fn = run_ksoftirqd,
  23363. + .thread_comm = "ktimersoftd/%u",
  23364. +};
  23365. +#endif
  23366. +
  23367. static __init int spawn_ksoftirqd(void)
  23368. {
  23369. register_cpu_notifier(&cpu_nfb);
  23370. BUG_ON(smpboot_register_percpu_thread(&softirq_threads));
  23371. +#ifdef CONFIG_PREEMPT_RT_FULL
  23372. + BUG_ON(smpboot_register_percpu_thread(&softirq_timer_threads));
  23373. +#endif
  23374. return 0;
  23375. }
  23376. diff --git a/kernel/stop_machine.c b/kernel/stop_machine.c
  23377. index a3bbaee77c58..f84d3b45cda7 100644
  23378. --- a/kernel/stop_machine.c
  23379. +++ b/kernel/stop_machine.c
  23380. @@ -37,7 +37,7 @@ struct cpu_stop_done {
  23381. struct cpu_stopper {
  23382. struct task_struct *thread;
  23383. - spinlock_t lock;
  23384. + raw_spinlock_t lock;
  23385. bool enabled; /* is this stopper enabled? */
  23386. struct list_head works; /* list of pending works */
  23387. @@ -86,12 +86,12 @@ static void cpu_stop_queue_work(unsigned int cpu, struct cpu_stop_work *work)
  23388. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  23389. unsigned long flags;
  23390. - spin_lock_irqsave(&stopper->lock, flags);
  23391. + raw_spin_lock_irqsave(&stopper->lock, flags);
  23392. if (stopper->enabled)
  23393. __cpu_stop_queue_work(stopper, work);
  23394. else
  23395. cpu_stop_signal_done(work->done, false);
  23396. - spin_unlock_irqrestore(&stopper->lock, flags);
  23397. + raw_spin_unlock_irqrestore(&stopper->lock, flags);
  23398. }
  23399. /**
  23400. @@ -224,8 +224,8 @@ static int cpu_stop_queue_two_works(int cpu1, struct cpu_stop_work *work1,
  23401. int err;
  23402. lg_double_lock(&stop_cpus_lock, cpu1, cpu2);
  23403. - spin_lock_irq(&stopper1->lock);
  23404. - spin_lock_nested(&stopper2->lock, SINGLE_DEPTH_NESTING);
  23405. + raw_spin_lock_irq(&stopper1->lock);
  23406. + raw_spin_lock_nested(&stopper2->lock, SINGLE_DEPTH_NESTING);
  23407. err = -ENOENT;
  23408. if (!stopper1->enabled || !stopper2->enabled)
  23409. @@ -235,8 +235,8 @@ static int cpu_stop_queue_two_works(int cpu1, struct cpu_stop_work *work1,
  23410. __cpu_stop_queue_work(stopper1, work1);
  23411. __cpu_stop_queue_work(stopper2, work2);
  23412. unlock:
  23413. - spin_unlock(&stopper2->lock);
  23414. - spin_unlock_irq(&stopper1->lock);
  23415. + raw_spin_unlock(&stopper2->lock);
  23416. + raw_spin_unlock_irq(&stopper1->lock);
  23417. lg_double_unlock(&stop_cpus_lock, cpu1, cpu2);
  23418. return err;
  23419. @@ -258,7 +258,7 @@ int stop_two_cpus(unsigned int cpu1, unsigned int cpu2, cpu_stop_fn_t fn, void *
  23420. struct cpu_stop_work work1, work2;
  23421. struct multi_stop_data msdata;
  23422. - preempt_disable();
  23423. + preempt_disable_nort();
  23424. msdata = (struct multi_stop_data){
  23425. .fn = fn,
  23426. .data = arg,
  23427. @@ -278,11 +278,11 @@ int stop_two_cpus(unsigned int cpu1, unsigned int cpu2, cpu_stop_fn_t fn, void *
  23428. if (cpu1 > cpu2)
  23429. swap(cpu1, cpu2);
  23430. if (cpu_stop_queue_two_works(cpu1, &work1, cpu2, &work2)) {
  23431. - preempt_enable();
  23432. + preempt_enable_nort();
  23433. return -ENOENT;
  23434. }
  23435. - preempt_enable();
  23436. + preempt_enable_nort();
  23437. wait_for_completion(&done.completion);
  23438. @@ -315,17 +315,20 @@ static DEFINE_MUTEX(stop_cpus_mutex);
  23439. static void queue_stop_cpus_work(const struct cpumask *cpumask,
  23440. cpu_stop_fn_t fn, void *arg,
  23441. - struct cpu_stop_done *done)
  23442. + struct cpu_stop_done *done, bool inactive)
  23443. {
  23444. struct cpu_stop_work *work;
  23445. unsigned int cpu;
  23446. /*
  23447. - * Disable preemption while queueing to avoid getting
  23448. - * preempted by a stopper which might wait for other stoppers
  23449. - * to enter @fn which can lead to deadlock.
  23450. + * Make sure that all work is queued on all cpus before
  23451. + * any of the cpus can execute it.
  23452. */
  23453. - lg_global_lock(&stop_cpus_lock);
  23454. + if (!inactive)
  23455. + lg_global_lock(&stop_cpus_lock);
  23456. + else
  23457. + lg_global_trylock_relax(&stop_cpus_lock);
  23458. +
  23459. for_each_cpu(cpu, cpumask) {
  23460. work = &per_cpu(cpu_stopper.stop_work, cpu);
  23461. work->fn = fn;
  23462. @@ -342,7 +345,7 @@ static int __stop_cpus(const struct cpumask *cpumask,
  23463. struct cpu_stop_done done;
  23464. cpu_stop_init_done(&done, cpumask_weight(cpumask));
  23465. - queue_stop_cpus_work(cpumask, fn, arg, &done);
  23466. + queue_stop_cpus_work(cpumask, fn, arg, &done, false);
  23467. wait_for_completion(&done.completion);
  23468. return done.executed ? done.ret : -ENOENT;
  23469. }
  23470. @@ -422,9 +425,9 @@ static int cpu_stop_should_run(unsigned int cpu)
  23471. unsigned long flags;
  23472. int run;
  23473. - spin_lock_irqsave(&stopper->lock, flags);
  23474. + raw_spin_lock_irqsave(&stopper->lock, flags);
  23475. run = !list_empty(&stopper->works);
  23476. - spin_unlock_irqrestore(&stopper->lock, flags);
  23477. + raw_spin_unlock_irqrestore(&stopper->lock, flags);
  23478. return run;
  23479. }
  23480. @@ -436,13 +439,13 @@ static void cpu_stopper_thread(unsigned int cpu)
  23481. repeat:
  23482. work = NULL;
  23483. - spin_lock_irq(&stopper->lock);
  23484. + raw_spin_lock_irq(&stopper->lock);
  23485. if (!list_empty(&stopper->works)) {
  23486. work = list_first_entry(&stopper->works,
  23487. struct cpu_stop_work, list);
  23488. list_del_init(&work->list);
  23489. }
  23490. - spin_unlock_irq(&stopper->lock);
  23491. + raw_spin_unlock_irq(&stopper->lock);
  23492. if (work) {
  23493. cpu_stop_fn_t fn = work->fn;
  23494. @@ -450,6 +453,16 @@ repeat:
  23495. struct cpu_stop_done *done = work->done;
  23496. char ksym_buf[KSYM_NAME_LEN] __maybe_unused;
  23497. + /*
  23498. + * Wait until the stopper finished scheduling on all
  23499. + * cpus
  23500. + */
  23501. + lg_global_lock(&stop_cpus_lock);
  23502. + /*
  23503. + * Let other cpu threads continue as well
  23504. + */
  23505. + lg_global_unlock(&stop_cpus_lock);
  23506. +
  23507. /* cpu stop callbacks are not allowed to sleep */
  23508. preempt_disable();
  23509. @@ -520,10 +533,12 @@ static int __init cpu_stop_init(void)
  23510. for_each_possible_cpu(cpu) {
  23511. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  23512. - spin_lock_init(&stopper->lock);
  23513. + raw_spin_lock_init(&stopper->lock);
  23514. INIT_LIST_HEAD(&stopper->works);
  23515. }
  23516. + lg_lock_init(&stop_cpus_lock, "stop_cpus_lock");
  23517. +
  23518. BUG_ON(smpboot_register_percpu_thread(&cpu_stop_threads));
  23519. stop_machine_unpark(raw_smp_processor_id());
  23520. stop_machine_initialized = true;
  23521. @@ -620,7 +635,7 @@ int stop_machine_from_inactive_cpu(cpu_stop_fn_t fn, void *data,
  23522. set_state(&msdata, MULTI_STOP_PREPARE);
  23523. cpu_stop_init_done(&done, num_active_cpus());
  23524. queue_stop_cpus_work(cpu_active_mask, multi_cpu_stop, &msdata,
  23525. - &done);
  23526. + &done, true);
  23527. ret = multi_cpu_stop(&msdata);
  23528. /* Busy wait for completion. */
  23529. diff --git a/kernel/time/hrtimer.c b/kernel/time/hrtimer.c
  23530. index 17f7bcff1e02..ba3d60144838 100644
  23531. --- a/kernel/time/hrtimer.c
  23532. +++ b/kernel/time/hrtimer.c
  23533. @@ -48,11 +48,13 @@
  23534. #include <linux/sched/rt.h>
  23535. #include <linux/sched/deadline.h>
  23536. #include <linux/timer.h>
  23537. +#include <linux/kthread.h>
  23538. #include <linux/freezer.h>
  23539. #include <asm/uaccess.h>
  23540. #include <trace/events/timer.h>
  23541. +#include <trace/events/hist.h>
  23542. #include "tick-internal.h"
  23543. @@ -717,6 +719,44 @@ static void clock_was_set_work(struct work_struct *work)
  23544. static DECLARE_WORK(hrtimer_work, clock_was_set_work);
  23545. +#ifdef CONFIG_PREEMPT_RT_FULL
  23546. +/*
  23547. + * RT can not call schedule_work from real interrupt context.
  23548. + * Need to make a thread to do the real work.
  23549. + */
  23550. +static struct task_struct *clock_set_delay_thread;
  23551. +static bool do_clock_set_delay;
  23552. +
  23553. +static int run_clock_set_delay(void *ignore)
  23554. +{
  23555. + while (!kthread_should_stop()) {
  23556. + set_current_state(TASK_INTERRUPTIBLE);
  23557. + if (do_clock_set_delay) {
  23558. + do_clock_set_delay = false;
  23559. + schedule_work(&hrtimer_work);
  23560. + }
  23561. + schedule();
  23562. + }
  23563. + __set_current_state(TASK_RUNNING);
  23564. + return 0;
  23565. +}
  23566. +
  23567. +void clock_was_set_delayed(void)
  23568. +{
  23569. + do_clock_set_delay = true;
  23570. + /* Make visible before waking up process */
  23571. + smp_wmb();
  23572. + wake_up_process(clock_set_delay_thread);
  23573. +}
  23574. +
  23575. +static __init int create_clock_set_delay_thread(void)
  23576. +{
  23577. + clock_set_delay_thread = kthread_run(run_clock_set_delay, NULL, "kclksetdelayd");
  23578. + BUG_ON(!clock_set_delay_thread);
  23579. + return 0;
  23580. +}
  23581. +early_initcall(create_clock_set_delay_thread);
  23582. +#else /* PREEMPT_RT_FULL */
  23583. /*
  23584. * Called from timekeeping and resume code to reprogramm the hrtimer
  23585. * interrupt device on all cpus.
  23586. @@ -725,6 +765,7 @@ void clock_was_set_delayed(void)
  23587. {
  23588. schedule_work(&hrtimer_work);
  23589. }
  23590. +#endif
  23591. #else
  23592. @@ -734,11 +775,8 @@ static inline int hrtimer_is_hres_enabled(void) { return 0; }
  23593. static inline void hrtimer_switch_to_hres(void) { }
  23594. static inline void
  23595. hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
  23596. -static inline int hrtimer_reprogram(struct hrtimer *timer,
  23597. - struct hrtimer_clock_base *base)
  23598. -{
  23599. - return 0;
  23600. -}
  23601. +static inline void hrtimer_reprogram(struct hrtimer *timer,
  23602. + struct hrtimer_clock_base *base) { }
  23603. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
  23604. static inline void retrigger_next_event(void *arg) { }
  23605. @@ -870,6 +908,32 @@ u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
  23606. }
  23607. EXPORT_SYMBOL_GPL(hrtimer_forward);
  23608. +#ifdef CONFIG_PREEMPT_RT_BASE
  23609. +# define wake_up_timer_waiters(b) wake_up(&(b)->wait)
  23610. +
  23611. +/**
  23612. + * hrtimer_wait_for_timer - Wait for a running timer
  23613. + *
  23614. + * @timer: timer to wait for
  23615. + *
  23616. + * The function waits in case the timers callback function is
  23617. + * currently executed on the waitqueue of the timer base. The
  23618. + * waitqueue is woken up after the timer callback function has
  23619. + * finished execution.
  23620. + */
  23621. +void hrtimer_wait_for_timer(const struct hrtimer *timer)
  23622. +{
  23623. + struct hrtimer_clock_base *base = timer->base;
  23624. +
  23625. + if (base && base->cpu_base && !timer->irqsafe)
  23626. + wait_event(base->cpu_base->wait,
  23627. + !(hrtimer_callback_running(timer)));
  23628. +}
  23629. +
  23630. +#else
  23631. +# define wake_up_timer_waiters(b) do { } while (0)
  23632. +#endif
  23633. +
  23634. /*
  23635. * enqueue_hrtimer - internal function to (re)start a timer
  23636. *
  23637. @@ -911,6 +975,11 @@ static void __remove_hrtimer(struct hrtimer *timer,
  23638. if (!(state & HRTIMER_STATE_ENQUEUED))
  23639. return;
  23640. + if (unlikely(!list_empty(&timer->cb_entry))) {
  23641. + list_del_init(&timer->cb_entry);
  23642. + return;
  23643. + }
  23644. +
  23645. if (!timerqueue_del(&base->active, &timer->node))
  23646. cpu_base->active_bases &= ~(1 << base->index);
  23647. @@ -1006,7 +1075,16 @@ void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
  23648. new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
  23649. timer_stats_hrtimer_set_start_info(timer);
  23650. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  23651. + {
  23652. + ktime_t now = new_base->get_time();
  23653. + if (ktime_to_ns(tim) < ktime_to_ns(now))
  23654. + timer->praecox = now;
  23655. + else
  23656. + timer->praecox = ktime_set(0, 0);
  23657. + }
  23658. +#endif
  23659. leftmost = enqueue_hrtimer(timer, new_base);
  23660. if (!leftmost)
  23661. goto unlock;
  23662. @@ -1078,7 +1156,7 @@ int hrtimer_cancel(struct hrtimer *timer)
  23663. if (ret >= 0)
  23664. return ret;
  23665. - cpu_relax();
  23666. + hrtimer_wait_for_timer(timer);
  23667. }
  23668. }
  23669. EXPORT_SYMBOL_GPL(hrtimer_cancel);
  23670. @@ -1142,6 +1220,7 @@ static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  23671. base = hrtimer_clockid_to_base(clock_id);
  23672. timer->base = &cpu_base->clock_base[base];
  23673. + INIT_LIST_HEAD(&timer->cb_entry);
  23674. timerqueue_init(&timer->node);
  23675. #ifdef CONFIG_TIMER_STATS
  23676. @@ -1182,6 +1261,7 @@ bool hrtimer_active(const struct hrtimer *timer)
  23677. seq = raw_read_seqcount_begin(&cpu_base->seq);
  23678. if (timer->state != HRTIMER_STATE_INACTIVE ||
  23679. + cpu_base->running_soft == timer ||
  23680. cpu_base->running == timer)
  23681. return true;
  23682. @@ -1280,10 +1360,112 @@ static void __run_hrtimer(struct hrtimer_cpu_base *cpu_base,
  23683. cpu_base->running = NULL;
  23684. }
  23685. +#ifdef CONFIG_PREEMPT_RT_BASE
  23686. +static void hrtimer_rt_reprogram(int restart, struct hrtimer *timer,
  23687. + struct hrtimer_clock_base *base)
  23688. +{
  23689. + int leftmost;
  23690. +
  23691. + if (restart != HRTIMER_NORESTART &&
  23692. + !(timer->state & HRTIMER_STATE_ENQUEUED)) {
  23693. +
  23694. + leftmost = enqueue_hrtimer(timer, base);
  23695. + if (!leftmost)
  23696. + return;
  23697. +#ifdef CONFIG_HIGH_RES_TIMERS
  23698. + if (!hrtimer_is_hres_active(timer)) {
  23699. + /*
  23700. + * Kick to reschedule the next tick to handle the new timer
  23701. + * on dynticks target.
  23702. + */
  23703. + if (base->cpu_base->nohz_active)
  23704. + wake_up_nohz_cpu(base->cpu_base->cpu);
  23705. + } else {
  23706. +
  23707. + hrtimer_reprogram(timer, base);
  23708. + }
  23709. +#endif
  23710. + }
  23711. +}
  23712. +
  23713. +/*
  23714. + * The changes in mainline which removed the callback modes from
  23715. + * hrtimer are not yet working with -rt. The non wakeup_process()
  23716. + * based callbacks which involve sleeping locks need to be treated
  23717. + * seperately.
  23718. + */
  23719. +static void hrtimer_rt_run_pending(void)
  23720. +{
  23721. + enum hrtimer_restart (*fn)(struct hrtimer *);
  23722. + struct hrtimer_cpu_base *cpu_base;
  23723. + struct hrtimer_clock_base *base;
  23724. + struct hrtimer *timer;
  23725. + int index, restart;
  23726. +
  23727. + local_irq_disable();
  23728. + cpu_base = &per_cpu(hrtimer_bases, smp_processor_id());
  23729. +
  23730. + raw_spin_lock(&cpu_base->lock);
  23731. +
  23732. + for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
  23733. + base = &cpu_base->clock_base[index];
  23734. +
  23735. + while (!list_empty(&base->expired)) {
  23736. + timer = list_first_entry(&base->expired,
  23737. + struct hrtimer, cb_entry);
  23738. +
  23739. + /*
  23740. + * Same as the above __run_hrtimer function
  23741. + * just we run with interrupts enabled.
  23742. + */
  23743. + debug_deactivate(timer);
  23744. + cpu_base->running_soft = timer;
  23745. + raw_write_seqcount_barrier(&cpu_base->seq);
  23746. +
  23747. + __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, 0);
  23748. + timer_stats_account_hrtimer(timer);
  23749. + fn = timer->function;
  23750. +
  23751. + raw_spin_unlock_irq(&cpu_base->lock);
  23752. + restart = fn(timer);
  23753. + raw_spin_lock_irq(&cpu_base->lock);
  23754. +
  23755. + hrtimer_rt_reprogram(restart, timer, base);
  23756. + raw_write_seqcount_barrier(&cpu_base->seq);
  23757. +
  23758. + WARN_ON_ONCE(cpu_base->running_soft != timer);
  23759. + cpu_base->running_soft = NULL;
  23760. + }
  23761. + }
  23762. +
  23763. + raw_spin_unlock_irq(&cpu_base->lock);
  23764. +
  23765. + wake_up_timer_waiters(cpu_base);
  23766. +}
  23767. +
  23768. +static int hrtimer_rt_defer(struct hrtimer *timer)
  23769. +{
  23770. + if (timer->irqsafe)
  23771. + return 0;
  23772. +
  23773. + __remove_hrtimer(timer, timer->base, timer->state, 0);
  23774. + list_add_tail(&timer->cb_entry, &timer->base->expired);
  23775. + return 1;
  23776. +}
  23777. +
  23778. +#else
  23779. +
  23780. +static inline int hrtimer_rt_defer(struct hrtimer *timer) { return 0; }
  23781. +
  23782. +#endif
  23783. +
  23784. +static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer);
  23785. +
  23786. static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now)
  23787. {
  23788. struct hrtimer_clock_base *base = cpu_base->clock_base;
  23789. unsigned int active = cpu_base->active_bases;
  23790. + int raise = 0;
  23791. for (; active; base++, active >>= 1) {
  23792. struct timerqueue_node *node;
  23793. @@ -1299,6 +1481,15 @@ static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now)
  23794. timer = container_of(node, struct hrtimer, node);
  23795. + trace_hrtimer_interrupt(raw_smp_processor_id(),
  23796. + ktime_to_ns(ktime_sub(ktime_to_ns(timer->praecox) ?
  23797. + timer->praecox : hrtimer_get_expires(timer),
  23798. + basenow)),
  23799. + current,
  23800. + timer->function == hrtimer_wakeup ?
  23801. + container_of(timer, struct hrtimer_sleeper,
  23802. + timer)->task : NULL);
  23803. +
  23804. /*
  23805. * The immediate goal for using the softexpires is
  23806. * minimizing wakeups, not running timers at the
  23807. @@ -1314,9 +1505,14 @@ static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now)
  23808. if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer))
  23809. break;
  23810. - __run_hrtimer(cpu_base, base, timer, &basenow);
  23811. + if (!hrtimer_rt_defer(timer))
  23812. + __run_hrtimer(cpu_base, base, timer, &basenow);
  23813. + else
  23814. + raise = 1;
  23815. }
  23816. }
  23817. + if (raise)
  23818. + raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  23819. }
  23820. #ifdef CONFIG_HIGH_RES_TIMERS
  23821. @@ -1479,16 +1675,18 @@ static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
  23822. void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
  23823. {
  23824. sl->timer.function = hrtimer_wakeup;
  23825. + sl->timer.irqsafe = 1;
  23826. sl->task = task;
  23827. }
  23828. EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
  23829. -static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
  23830. +static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode,
  23831. + unsigned long state)
  23832. {
  23833. hrtimer_init_sleeper(t, current);
  23834. do {
  23835. - set_current_state(TASK_INTERRUPTIBLE);
  23836. + set_current_state(state);
  23837. hrtimer_start_expires(&t->timer, mode);
  23838. if (likely(t->task))
  23839. @@ -1530,7 +1728,8 @@ long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
  23840. HRTIMER_MODE_ABS);
  23841. hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
  23842. - if (do_nanosleep(&t, HRTIMER_MODE_ABS))
  23843. + /* cpu_chill() does not care about restart state. */
  23844. + if (do_nanosleep(&t, HRTIMER_MODE_ABS, TASK_INTERRUPTIBLE))
  23845. goto out;
  23846. rmtp = restart->nanosleep.rmtp;
  23847. @@ -1547,8 +1746,10 @@ out:
  23848. return ret;
  23849. }
  23850. -long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  23851. - const enum hrtimer_mode mode, const clockid_t clockid)
  23852. +static long
  23853. +__hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  23854. + const enum hrtimer_mode mode, const clockid_t clockid,
  23855. + unsigned long state)
  23856. {
  23857. struct restart_block *restart;
  23858. struct hrtimer_sleeper t;
  23859. @@ -1561,7 +1762,7 @@ long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  23860. hrtimer_init_on_stack(&t.timer, clockid, mode);
  23861. hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
  23862. - if (do_nanosleep(&t, mode))
  23863. + if (do_nanosleep(&t, mode, state))
  23864. goto out;
  23865. /* Absolute timers do not update the rmtp value and restart: */
  23866. @@ -1588,6 +1789,12 @@ out:
  23867. return ret;
  23868. }
  23869. +long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  23870. + const enum hrtimer_mode mode, const clockid_t clockid)
  23871. +{
  23872. + return __hrtimer_nanosleep(rqtp, rmtp, mode, clockid, TASK_INTERRUPTIBLE);
  23873. +}
  23874. +
  23875. SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
  23876. struct timespec __user *, rmtp)
  23877. {
  23878. @@ -1602,6 +1809,26 @@ SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
  23879. return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
  23880. }
  23881. +#ifdef CONFIG_PREEMPT_RT_FULL
  23882. +/*
  23883. + * Sleep for 1 ms in hope whoever holds what we want will let it go.
  23884. + */
  23885. +void cpu_chill(void)
  23886. +{
  23887. + struct timespec tu = {
  23888. + .tv_nsec = NSEC_PER_MSEC,
  23889. + };
  23890. + unsigned int freeze_flag = current->flags & PF_NOFREEZE;
  23891. +
  23892. + current->flags |= PF_NOFREEZE;
  23893. + __hrtimer_nanosleep(&tu, NULL, HRTIMER_MODE_REL, CLOCK_MONOTONIC,
  23894. + TASK_UNINTERRUPTIBLE);
  23895. + if (!freeze_flag)
  23896. + current->flags &= ~PF_NOFREEZE;
  23897. +}
  23898. +EXPORT_SYMBOL(cpu_chill);
  23899. +#endif
  23900. +
  23901. /*
  23902. * Functions related to boot-time initialization:
  23903. */
  23904. @@ -1613,10 +1840,14 @@ static void init_hrtimers_cpu(int cpu)
  23905. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  23906. cpu_base->clock_base[i].cpu_base = cpu_base;
  23907. timerqueue_init_head(&cpu_base->clock_base[i].active);
  23908. + INIT_LIST_HEAD(&cpu_base->clock_base[i].expired);
  23909. }
  23910. cpu_base->cpu = cpu;
  23911. hrtimer_init_hres(cpu_base);
  23912. +#ifdef CONFIG_PREEMPT_RT_BASE
  23913. + init_waitqueue_head(&cpu_base->wait);
  23914. +#endif
  23915. }
  23916. #ifdef CONFIG_HOTPLUG_CPU
  23917. @@ -1714,11 +1945,21 @@ static struct notifier_block hrtimers_nb = {
  23918. .notifier_call = hrtimer_cpu_notify,
  23919. };
  23920. +#ifdef CONFIG_PREEMPT_RT_BASE
  23921. +static void run_hrtimer_softirq(struct softirq_action *h)
  23922. +{
  23923. + hrtimer_rt_run_pending();
  23924. +}
  23925. +#endif
  23926. +
  23927. void __init hrtimers_init(void)
  23928. {
  23929. hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
  23930. (void *)(long)smp_processor_id());
  23931. register_cpu_notifier(&hrtimers_nb);
  23932. +#ifdef CONFIG_PREEMPT_RT_BASE
  23933. + open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
  23934. +#endif
  23935. }
  23936. /**
  23937. diff --git a/kernel/time/itimer.c b/kernel/time/itimer.c
  23938. index 1d5c7204ddc9..184de6751180 100644
  23939. --- a/kernel/time/itimer.c
  23940. +++ b/kernel/time/itimer.c
  23941. @@ -213,6 +213,7 @@ again:
  23942. /* We are sharing ->siglock with it_real_fn() */
  23943. if (hrtimer_try_to_cancel(timer) < 0) {
  23944. spin_unlock_irq(&tsk->sighand->siglock);
  23945. + hrtimer_wait_for_timer(&tsk->signal->real_timer);
  23946. goto again;
  23947. }
  23948. expires = timeval_to_ktime(value->it_value);
  23949. diff --git a/kernel/time/jiffies.c b/kernel/time/jiffies.c
  23950. index 347fecf86a3f..2ede47408a3e 100644
  23951. --- a/kernel/time/jiffies.c
  23952. +++ b/kernel/time/jiffies.c
  23953. @@ -74,7 +74,8 @@ static struct clocksource clocksource_jiffies = {
  23954. .max_cycles = 10,
  23955. };
  23956. -__cacheline_aligned_in_smp DEFINE_SEQLOCK(jiffies_lock);
  23957. +__cacheline_aligned_in_smp DEFINE_RAW_SPINLOCK(jiffies_lock);
  23958. +__cacheline_aligned_in_smp seqcount_t jiffies_seq;
  23959. #if (BITS_PER_LONG < 64)
  23960. u64 get_jiffies_64(void)
  23961. @@ -83,9 +84,9 @@ u64 get_jiffies_64(void)
  23962. u64 ret;
  23963. do {
  23964. - seq = read_seqbegin(&jiffies_lock);
  23965. + seq = read_seqcount_begin(&jiffies_seq);
  23966. ret = jiffies_64;
  23967. - } while (read_seqretry(&jiffies_lock, seq));
  23968. + } while (read_seqcount_retry(&jiffies_seq, seq));
  23969. return ret;
  23970. }
  23971. EXPORT_SYMBOL(get_jiffies_64);
  23972. diff --git a/kernel/time/ntp.c b/kernel/time/ntp.c
  23973. index ab861771e37f..0f6868fd2de6 100644
  23974. --- a/kernel/time/ntp.c
  23975. +++ b/kernel/time/ntp.c
  23976. @@ -10,6 +10,7 @@
  23977. #include <linux/workqueue.h>
  23978. #include <linux/hrtimer.h>
  23979. #include <linux/jiffies.h>
  23980. +#include <linux/kthread.h>
  23981. #include <linux/math64.h>
  23982. #include <linux/timex.h>
  23983. #include <linux/time.h>
  23984. @@ -562,10 +563,52 @@ static void sync_cmos_clock(struct work_struct *work)
  23985. &sync_cmos_work, timespec64_to_jiffies(&next));
  23986. }
  23987. +#ifdef CONFIG_PREEMPT_RT_FULL
  23988. +/*
  23989. + * RT can not call schedule_delayed_work from real interrupt context.
  23990. + * Need to make a thread to do the real work.
  23991. + */
  23992. +static struct task_struct *cmos_delay_thread;
  23993. +static bool do_cmos_delay;
  23994. +
  23995. +static int run_cmos_delay(void *ignore)
  23996. +{
  23997. + while (!kthread_should_stop()) {
  23998. + set_current_state(TASK_INTERRUPTIBLE);
  23999. + if (do_cmos_delay) {
  24000. + do_cmos_delay = false;
  24001. + queue_delayed_work(system_power_efficient_wq,
  24002. + &sync_cmos_work, 0);
  24003. + }
  24004. + schedule();
  24005. + }
  24006. + __set_current_state(TASK_RUNNING);
  24007. + return 0;
  24008. +}
  24009. +
  24010. +void ntp_notify_cmos_timer(void)
  24011. +{
  24012. + do_cmos_delay = true;
  24013. + /* Make visible before waking up process */
  24014. + smp_wmb();
  24015. + wake_up_process(cmos_delay_thread);
  24016. +}
  24017. +
  24018. +static __init int create_cmos_delay_thread(void)
  24019. +{
  24020. + cmos_delay_thread = kthread_run(run_cmos_delay, NULL, "kcmosdelayd");
  24021. + BUG_ON(!cmos_delay_thread);
  24022. + return 0;
  24023. +}
  24024. +early_initcall(create_cmos_delay_thread);
  24025. +
  24026. +#else
  24027. +
  24028. void ntp_notify_cmos_timer(void)
  24029. {
  24030. queue_delayed_work(system_power_efficient_wq, &sync_cmos_work, 0);
  24031. }
  24032. +#endif /* CONFIG_PREEMPT_RT_FULL */
  24033. #else
  24034. void ntp_notify_cmos_timer(void) { }
  24035. diff --git a/kernel/time/posix-cpu-timers.c b/kernel/time/posix-cpu-timers.c
  24036. index 80016b329d94..b7342b6e6a5a 100644
  24037. --- a/kernel/time/posix-cpu-timers.c
  24038. +++ b/kernel/time/posix-cpu-timers.c
  24039. @@ -3,6 +3,7 @@
  24040. */
  24041. #include <linux/sched.h>
  24042. +#include <linux/sched/rt.h>
  24043. #include <linux/posix-timers.h>
  24044. #include <linux/errno.h>
  24045. #include <linux/math64.h>
  24046. @@ -650,7 +651,7 @@ static int posix_cpu_timer_set(struct k_itimer *timer, int timer_flags,
  24047. /*
  24048. * Disarm any old timer after extracting its expiry time.
  24049. */
  24050. - WARN_ON_ONCE(!irqs_disabled());
  24051. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  24052. ret = 0;
  24053. old_incr = timer->it.cpu.incr;
  24054. @@ -1092,7 +1093,7 @@ void posix_cpu_timer_schedule(struct k_itimer *timer)
  24055. /*
  24056. * Now re-arm for the new expiry time.
  24057. */
  24058. - WARN_ON_ONCE(!irqs_disabled());
  24059. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  24060. arm_timer(timer);
  24061. unlock_task_sighand(p, &flags);
  24062. @@ -1183,13 +1184,13 @@ static inline int fastpath_timer_check(struct task_struct *tsk)
  24063. * already updated our counts. We need to check if any timers fire now.
  24064. * Interrupts are disabled.
  24065. */
  24066. -void run_posix_cpu_timers(struct task_struct *tsk)
  24067. +static void __run_posix_cpu_timers(struct task_struct *tsk)
  24068. {
  24069. LIST_HEAD(firing);
  24070. struct k_itimer *timer, *next;
  24071. unsigned long flags;
  24072. - WARN_ON_ONCE(!irqs_disabled());
  24073. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  24074. /*
  24075. * The fast path checks that there are no expired thread or thread
  24076. @@ -1243,6 +1244,190 @@ void run_posix_cpu_timers(struct task_struct *tsk)
  24077. }
  24078. }
  24079. +#ifdef CONFIG_PREEMPT_RT_BASE
  24080. +#include <linux/kthread.h>
  24081. +#include <linux/cpu.h>
  24082. +DEFINE_PER_CPU(struct task_struct *, posix_timer_task);
  24083. +DEFINE_PER_CPU(struct task_struct *, posix_timer_tasklist);
  24084. +
  24085. +static int posix_cpu_timers_thread(void *data)
  24086. +{
  24087. + int cpu = (long)data;
  24088. +
  24089. + BUG_ON(per_cpu(posix_timer_task,cpu) != current);
  24090. +
  24091. + while (!kthread_should_stop()) {
  24092. + struct task_struct *tsk = NULL;
  24093. + struct task_struct *next = NULL;
  24094. +
  24095. + if (cpu_is_offline(cpu))
  24096. + goto wait_to_die;
  24097. +
  24098. + /* grab task list */
  24099. + raw_local_irq_disable();
  24100. + tsk = per_cpu(posix_timer_tasklist, cpu);
  24101. + per_cpu(posix_timer_tasklist, cpu) = NULL;
  24102. + raw_local_irq_enable();
  24103. +
  24104. + /* its possible the list is empty, just return */
  24105. + if (!tsk) {
  24106. + set_current_state(TASK_INTERRUPTIBLE);
  24107. + schedule();
  24108. + __set_current_state(TASK_RUNNING);
  24109. + continue;
  24110. + }
  24111. +
  24112. + /* Process task list */
  24113. + while (1) {
  24114. + /* save next */
  24115. + next = tsk->posix_timer_list;
  24116. +
  24117. + /* run the task timers, clear its ptr and
  24118. + * unreference it
  24119. + */
  24120. + __run_posix_cpu_timers(tsk);
  24121. + tsk->posix_timer_list = NULL;
  24122. + put_task_struct(tsk);
  24123. +
  24124. + /* check if this is the last on the list */
  24125. + if (next == tsk)
  24126. + break;
  24127. + tsk = next;
  24128. + }
  24129. + }
  24130. + return 0;
  24131. +
  24132. +wait_to_die:
  24133. + /* Wait for kthread_stop */
  24134. + set_current_state(TASK_INTERRUPTIBLE);
  24135. + while (!kthread_should_stop()) {
  24136. + schedule();
  24137. + set_current_state(TASK_INTERRUPTIBLE);
  24138. + }
  24139. + __set_current_state(TASK_RUNNING);
  24140. + return 0;
  24141. +}
  24142. +
  24143. +static inline int __fastpath_timer_check(struct task_struct *tsk)
  24144. +{
  24145. + /* tsk == current, ensure it is safe to use ->signal/sighand */
  24146. + if (unlikely(tsk->exit_state))
  24147. + return 0;
  24148. +
  24149. + if (!task_cputime_zero(&tsk->cputime_expires))
  24150. + return 1;
  24151. +
  24152. + if (!task_cputime_zero(&tsk->signal->cputime_expires))
  24153. + return 1;
  24154. +
  24155. + return 0;
  24156. +}
  24157. +
  24158. +void run_posix_cpu_timers(struct task_struct *tsk)
  24159. +{
  24160. + unsigned long cpu = smp_processor_id();
  24161. + struct task_struct *tasklist;
  24162. +
  24163. + BUG_ON(!irqs_disabled());
  24164. + if(!per_cpu(posix_timer_task, cpu))
  24165. + return;
  24166. + /* get per-cpu references */
  24167. + tasklist = per_cpu(posix_timer_tasklist, cpu);
  24168. +
  24169. + /* check to see if we're already queued */
  24170. + if (!tsk->posix_timer_list && __fastpath_timer_check(tsk)) {
  24171. + get_task_struct(tsk);
  24172. + if (tasklist) {
  24173. + tsk->posix_timer_list = tasklist;
  24174. + } else {
  24175. + /*
  24176. + * The list is terminated by a self-pointing
  24177. + * task_struct
  24178. + */
  24179. + tsk->posix_timer_list = tsk;
  24180. + }
  24181. + per_cpu(posix_timer_tasklist, cpu) = tsk;
  24182. +
  24183. + wake_up_process(per_cpu(posix_timer_task, cpu));
  24184. + }
  24185. +}
  24186. +
  24187. +/*
  24188. + * posix_cpu_thread_call - callback that gets triggered when a CPU is added.
  24189. + * Here we can start up the necessary migration thread for the new CPU.
  24190. + */
  24191. +static int posix_cpu_thread_call(struct notifier_block *nfb,
  24192. + unsigned long action, void *hcpu)
  24193. +{
  24194. + int cpu = (long)hcpu;
  24195. + struct task_struct *p;
  24196. + struct sched_param param;
  24197. +
  24198. + switch (action) {
  24199. + case CPU_UP_PREPARE:
  24200. + p = kthread_create(posix_cpu_timers_thread, hcpu,
  24201. + "posixcputmr/%d",cpu);
  24202. + if (IS_ERR(p))
  24203. + return NOTIFY_BAD;
  24204. + p->flags |= PF_NOFREEZE;
  24205. + kthread_bind(p, cpu);
  24206. + /* Must be high prio to avoid getting starved */
  24207. + param.sched_priority = MAX_RT_PRIO-1;
  24208. + sched_setscheduler(p, SCHED_FIFO, &param);
  24209. + per_cpu(posix_timer_task,cpu) = p;
  24210. + break;
  24211. + case CPU_ONLINE:
  24212. + /* Strictly unneccessary, as first user will wake it. */
  24213. + wake_up_process(per_cpu(posix_timer_task,cpu));
  24214. + break;
  24215. +#ifdef CONFIG_HOTPLUG_CPU
  24216. + case CPU_UP_CANCELED:
  24217. + /* Unbind it from offline cpu so it can run. Fall thru. */
  24218. + kthread_bind(per_cpu(posix_timer_task, cpu),
  24219. + cpumask_any(cpu_online_mask));
  24220. + kthread_stop(per_cpu(posix_timer_task,cpu));
  24221. + per_cpu(posix_timer_task,cpu) = NULL;
  24222. + break;
  24223. + case CPU_DEAD:
  24224. + kthread_stop(per_cpu(posix_timer_task,cpu));
  24225. + per_cpu(posix_timer_task,cpu) = NULL;
  24226. + break;
  24227. +#endif
  24228. + }
  24229. + return NOTIFY_OK;
  24230. +}
  24231. +
  24232. +/* Register at highest priority so that task migration (migrate_all_tasks)
  24233. + * happens before everything else.
  24234. + */
  24235. +static struct notifier_block posix_cpu_thread_notifier = {
  24236. + .notifier_call = posix_cpu_thread_call,
  24237. + .priority = 10
  24238. +};
  24239. +
  24240. +static int __init posix_cpu_thread_init(void)
  24241. +{
  24242. + void *hcpu = (void *)(long)smp_processor_id();
  24243. + /* Start one for boot CPU. */
  24244. + unsigned long cpu;
  24245. +
  24246. + /* init the per-cpu posix_timer_tasklets */
  24247. + for_each_possible_cpu(cpu)
  24248. + per_cpu(posix_timer_tasklist, cpu) = NULL;
  24249. +
  24250. + posix_cpu_thread_call(&posix_cpu_thread_notifier, CPU_UP_PREPARE, hcpu);
  24251. + posix_cpu_thread_call(&posix_cpu_thread_notifier, CPU_ONLINE, hcpu);
  24252. + register_cpu_notifier(&posix_cpu_thread_notifier);
  24253. + return 0;
  24254. +}
  24255. +early_initcall(posix_cpu_thread_init);
  24256. +#else /* CONFIG_PREEMPT_RT_BASE */
  24257. +void run_posix_cpu_timers(struct task_struct *tsk)
  24258. +{
  24259. + __run_posix_cpu_timers(tsk);
  24260. +}
  24261. +#endif /* CONFIG_PREEMPT_RT_BASE */
  24262. +
  24263. /*
  24264. * Set one of the process-wide special case CPU timers or RLIMIT_CPU.
  24265. * The tsk->sighand->siglock must be held by the caller.
  24266. diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c
  24267. index f2826c35e918..464a98155a0e 100644
  24268. --- a/kernel/time/posix-timers.c
  24269. +++ b/kernel/time/posix-timers.c
  24270. @@ -506,6 +506,7 @@ static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
  24271. static struct pid *good_sigevent(sigevent_t * event)
  24272. {
  24273. struct task_struct *rtn = current->group_leader;
  24274. + int sig = event->sigev_signo;
  24275. if ((event->sigev_notify & SIGEV_THREAD_ID ) &&
  24276. (!(rtn = find_task_by_vpid(event->sigev_notify_thread_id)) ||
  24277. @@ -514,7 +515,8 @@ static struct pid *good_sigevent(sigevent_t * event)
  24278. return NULL;
  24279. if (((event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) &&
  24280. - ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX)))
  24281. + (sig <= 0 || sig > SIGRTMAX || sig_kernel_only(sig) ||
  24282. + sig_kernel_coredump(sig)))
  24283. return NULL;
  24284. return task_pid(rtn);
  24285. @@ -826,6 +828,20 @@ SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id)
  24286. return overrun;
  24287. }
  24288. +/*
  24289. + * Protected by RCU!
  24290. + */
  24291. +static void timer_wait_for_callback(struct k_clock *kc, struct k_itimer *timr)
  24292. +{
  24293. +#ifdef CONFIG_PREEMPT_RT_FULL
  24294. + if (kc->timer_set == common_timer_set)
  24295. + hrtimer_wait_for_timer(&timr->it.real.timer);
  24296. + else
  24297. + /* FIXME: Whacky hack for posix-cpu-timers */
  24298. + schedule_timeout(1);
  24299. +#endif
  24300. +}
  24301. +
  24302. /* Set a POSIX.1b interval timer. */
  24303. /* timr->it_lock is taken. */
  24304. static int
  24305. @@ -903,6 +919,7 @@ retry:
  24306. if (!timr)
  24307. return -EINVAL;
  24308. + rcu_read_lock();
  24309. kc = clockid_to_kclock(timr->it_clock);
  24310. if (WARN_ON_ONCE(!kc || !kc->timer_set))
  24311. error = -EINVAL;
  24312. @@ -911,9 +928,12 @@ retry:
  24313. unlock_timer(timr, flag);
  24314. if (error == TIMER_RETRY) {
  24315. + timer_wait_for_callback(kc, timr);
  24316. rtn = NULL; // We already got the old time...
  24317. + rcu_read_unlock();
  24318. goto retry;
  24319. }
  24320. + rcu_read_unlock();
  24321. if (old_setting && !error &&
  24322. copy_to_user(old_setting, &old_spec, sizeof (old_spec)))
  24323. @@ -951,10 +971,15 @@ retry_delete:
  24324. if (!timer)
  24325. return -EINVAL;
  24326. + rcu_read_lock();
  24327. if (timer_delete_hook(timer) == TIMER_RETRY) {
  24328. unlock_timer(timer, flags);
  24329. + timer_wait_for_callback(clockid_to_kclock(timer->it_clock),
  24330. + timer);
  24331. + rcu_read_unlock();
  24332. goto retry_delete;
  24333. }
  24334. + rcu_read_unlock();
  24335. spin_lock(&current->sighand->siglock);
  24336. list_del(&timer->list);
  24337. @@ -980,8 +1005,18 @@ static void itimer_delete(struct k_itimer *timer)
  24338. retry_delete:
  24339. spin_lock_irqsave(&timer->it_lock, flags);
  24340. + /* On RT we can race with a deletion */
  24341. + if (!timer->it_signal) {
  24342. + unlock_timer(timer, flags);
  24343. + return;
  24344. + }
  24345. +
  24346. if (timer_delete_hook(timer) == TIMER_RETRY) {
  24347. + rcu_read_lock();
  24348. unlock_timer(timer, flags);
  24349. + timer_wait_for_callback(clockid_to_kclock(timer->it_clock),
  24350. + timer);
  24351. + rcu_read_unlock();
  24352. goto retry_delete;
  24353. }
  24354. list_del(&timer->list);
  24355. diff --git a/kernel/time/tick-broadcast-hrtimer.c b/kernel/time/tick-broadcast-hrtimer.c
  24356. index 53d7184da0be..1b4ac3361c3f 100644
  24357. --- a/kernel/time/tick-broadcast-hrtimer.c
  24358. +++ b/kernel/time/tick-broadcast-hrtimer.c
  24359. @@ -106,5 +106,6 @@ void tick_setup_hrtimer_broadcast(void)
  24360. {
  24361. hrtimer_init(&bctimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  24362. bctimer.function = bc_handler;
  24363. + bctimer.irqsafe = true;
  24364. clockevents_register_device(&ce_broadcast_hrtimer);
  24365. }
  24366. diff --git a/kernel/time/tick-common.c b/kernel/time/tick-common.c
  24367. index 4fcd99e12aa0..5a47f2e98faf 100644
  24368. --- a/kernel/time/tick-common.c
  24369. +++ b/kernel/time/tick-common.c
  24370. @@ -79,13 +79,15 @@ int tick_is_oneshot_available(void)
  24371. static void tick_periodic(int cpu)
  24372. {
  24373. if (tick_do_timer_cpu == cpu) {
  24374. - write_seqlock(&jiffies_lock);
  24375. + raw_spin_lock(&jiffies_lock);
  24376. + write_seqcount_begin(&jiffies_seq);
  24377. /* Keep track of the next tick event */
  24378. tick_next_period = ktime_add(tick_next_period, tick_period);
  24379. do_timer(1);
  24380. - write_sequnlock(&jiffies_lock);
  24381. + write_seqcount_end(&jiffies_seq);
  24382. + raw_spin_unlock(&jiffies_lock);
  24383. update_wall_time();
  24384. }
  24385. @@ -157,9 +159,9 @@ void tick_setup_periodic(struct clock_event_device *dev, int broadcast)
  24386. ktime_t next;
  24387. do {
  24388. - seq = read_seqbegin(&jiffies_lock);
  24389. + seq = read_seqcount_begin(&jiffies_seq);
  24390. next = tick_next_period;
  24391. - } while (read_seqretry(&jiffies_lock, seq));
  24392. + } while (read_seqcount_retry(&jiffies_seq, seq));
  24393. clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
  24394. diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c
  24395. index 22c57e191a23..d536824cbd36 100644
  24396. --- a/kernel/time/tick-sched.c
  24397. +++ b/kernel/time/tick-sched.c
  24398. @@ -62,7 +62,8 @@ static void tick_do_update_jiffies64(ktime_t now)
  24399. return;
  24400. /* Reevalute with jiffies_lock held */
  24401. - write_seqlock(&jiffies_lock);
  24402. + raw_spin_lock(&jiffies_lock);
  24403. + write_seqcount_begin(&jiffies_seq);
  24404. delta = ktime_sub(now, last_jiffies_update);
  24405. if (delta.tv64 >= tick_period.tv64) {
  24406. @@ -85,10 +86,12 @@ static void tick_do_update_jiffies64(ktime_t now)
  24407. /* Keep the tick_next_period variable up to date */
  24408. tick_next_period = ktime_add(last_jiffies_update, tick_period);
  24409. } else {
  24410. - write_sequnlock(&jiffies_lock);
  24411. + write_seqcount_end(&jiffies_seq);
  24412. + raw_spin_unlock(&jiffies_lock);
  24413. return;
  24414. }
  24415. - write_sequnlock(&jiffies_lock);
  24416. + write_seqcount_end(&jiffies_seq);
  24417. + raw_spin_unlock(&jiffies_lock);
  24418. update_wall_time();
  24419. }
  24420. @@ -99,12 +102,14 @@ static ktime_t tick_init_jiffy_update(void)
  24421. {
  24422. ktime_t period;
  24423. - write_seqlock(&jiffies_lock);
  24424. + raw_spin_lock(&jiffies_lock);
  24425. + write_seqcount_begin(&jiffies_seq);
  24426. /* Did we start the jiffies update yet ? */
  24427. if (last_jiffies_update.tv64 == 0)
  24428. last_jiffies_update = tick_next_period;
  24429. period = last_jiffies_update;
  24430. - write_sequnlock(&jiffies_lock);
  24431. + write_seqcount_end(&jiffies_seq);
  24432. + raw_spin_unlock(&jiffies_lock);
  24433. return period;
  24434. }
  24435. @@ -176,6 +181,11 @@ static bool can_stop_full_tick(void)
  24436. return false;
  24437. }
  24438. + if (!arch_irq_work_has_interrupt()) {
  24439. + trace_tick_stop(0, "missing irq work interrupt\n");
  24440. + return false;
  24441. + }
  24442. +
  24443. /* sched_clock_tick() needs us? */
  24444. #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
  24445. /*
  24446. @@ -204,6 +214,7 @@ static void nohz_full_kick_work_func(struct irq_work *work)
  24447. static DEFINE_PER_CPU(struct irq_work, nohz_full_kick_work) = {
  24448. .func = nohz_full_kick_work_func,
  24449. + .flags = IRQ_WORK_HARD_IRQ,
  24450. };
  24451. /*
  24452. @@ -578,10 +589,10 @@ static ktime_t tick_nohz_stop_sched_tick(struct tick_sched *ts,
  24453. /* Read jiffies and the time when jiffies were updated last */
  24454. do {
  24455. - seq = read_seqbegin(&jiffies_lock);
  24456. + seq = read_seqcount_begin(&jiffies_seq);
  24457. basemono = last_jiffies_update.tv64;
  24458. basejiff = jiffies;
  24459. - } while (read_seqretry(&jiffies_lock, seq));
  24460. + } while (read_seqcount_retry(&jiffies_seq, seq));
  24461. ts->last_jiffies = basejiff;
  24462. if (rcu_needs_cpu(basemono, &next_rcu) ||
  24463. @@ -753,14 +764,7 @@ static bool can_stop_idle_tick(int cpu, struct tick_sched *ts)
  24464. return false;
  24465. if (unlikely(local_softirq_pending() && cpu_online(cpu))) {
  24466. - static int ratelimit;
  24467. -
  24468. - if (ratelimit < 10 &&
  24469. - (local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK)) {
  24470. - pr_warn("NOHZ: local_softirq_pending %02x\n",
  24471. - (unsigned int) local_softirq_pending());
  24472. - ratelimit++;
  24473. - }
  24474. + softirq_check_pending_idle();
  24475. return false;
  24476. }
  24477. @@ -1100,6 +1104,7 @@ void tick_setup_sched_timer(void)
  24478. * Emulate tick processing via per-CPU hrtimers:
  24479. */
  24480. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  24481. + ts->sched_timer.irqsafe = 1;
  24482. ts->sched_timer.function = tick_sched_timer;
  24483. /* Get the next period (per cpu) */
  24484. diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
  24485. index 738012d68117..e060b34d5603 100644
  24486. --- a/kernel/time/timekeeping.c
  24487. +++ b/kernel/time/timekeeping.c
  24488. @@ -2070,8 +2070,10 @@ EXPORT_SYMBOL(hardpps);
  24489. */
  24490. void xtime_update(unsigned long ticks)
  24491. {
  24492. - write_seqlock(&jiffies_lock);
  24493. + raw_spin_lock(&jiffies_lock);
  24494. + write_seqcount_begin(&jiffies_seq);
  24495. do_timer(ticks);
  24496. - write_sequnlock(&jiffies_lock);
  24497. + write_seqcount_end(&jiffies_seq);
  24498. + raw_spin_unlock(&jiffies_lock);
  24499. update_wall_time();
  24500. }
  24501. diff --git a/kernel/time/timekeeping.h b/kernel/time/timekeeping.h
  24502. index 704f595ce83f..763a3e5121ff 100644
  24503. --- a/kernel/time/timekeeping.h
  24504. +++ b/kernel/time/timekeeping.h
  24505. @@ -19,7 +19,8 @@ extern void timekeeping_resume(void);
  24506. extern void do_timer(unsigned long ticks);
  24507. extern void update_wall_time(void);
  24508. -extern seqlock_t jiffies_lock;
  24509. +extern raw_spinlock_t jiffies_lock;
  24510. +extern seqcount_t jiffies_seq;
  24511. #define CS_NAME_LEN 32
  24512. diff --git a/kernel/time/timer.c b/kernel/time/timer.c
  24513. index bbc5d1114583..603699ff9411 100644
  24514. --- a/kernel/time/timer.c
  24515. +++ b/kernel/time/timer.c
  24516. @@ -80,6 +80,9 @@ struct tvec_root {
  24517. struct tvec_base {
  24518. spinlock_t lock;
  24519. struct timer_list *running_timer;
  24520. +#ifdef CONFIG_PREEMPT_RT_FULL
  24521. + wait_queue_head_t wait_for_running_timer;
  24522. +#endif
  24523. unsigned long timer_jiffies;
  24524. unsigned long next_timer;
  24525. unsigned long active_timers;
  24526. @@ -777,6 +780,39 @@ static struct tvec_base *lock_timer_base(struct timer_list *timer,
  24527. cpu_relax();
  24528. }
  24529. }
  24530. +#ifdef CONFIG_PREEMPT_RT_FULL
  24531. +static inline struct tvec_base *switch_timer_base(struct timer_list *timer,
  24532. + struct tvec_base *old,
  24533. + struct tvec_base *new)
  24534. +{
  24535. + /*
  24536. + * We cannot do the below because we might be preempted and
  24537. + * then the preempter would see NULL and loop forever.
  24538. + */
  24539. + if (spin_trylock(&new->lock)) {
  24540. + WRITE_ONCE(timer->flags,
  24541. + (timer->flags & ~TIMER_BASEMASK) | new->cpu);
  24542. + spin_unlock(&old->lock);
  24543. + return new;
  24544. + }
  24545. + return old;
  24546. +}
  24547. +
  24548. +#else
  24549. +static inline struct tvec_base *switch_timer_base(struct timer_list *timer,
  24550. + struct tvec_base *old,
  24551. + struct tvec_base *new)
  24552. +{
  24553. + /* See the comment in lock_timer_base() */
  24554. + timer->flags |= TIMER_MIGRATING;
  24555. +
  24556. + spin_unlock(&old->lock);
  24557. + spin_lock(&new->lock);
  24558. + WRITE_ONCE(timer->flags,
  24559. + (timer->flags & ~TIMER_BASEMASK) | new->cpu);
  24560. + return new;
  24561. +}
  24562. +#endif
  24563. static inline int
  24564. __mod_timer(struct timer_list *timer, unsigned long expires,
  24565. @@ -807,16 +843,8 @@ __mod_timer(struct timer_list *timer, unsigned long expires,
  24566. * handler yet has not finished. This also guarantees that
  24567. * the timer is serialized wrt itself.
  24568. */
  24569. - if (likely(base->running_timer != timer)) {
  24570. - /* See the comment in lock_timer_base() */
  24571. - timer->flags |= TIMER_MIGRATING;
  24572. -
  24573. - spin_unlock(&base->lock);
  24574. - base = new_base;
  24575. - spin_lock(&base->lock);
  24576. - WRITE_ONCE(timer->flags,
  24577. - (timer->flags & ~TIMER_BASEMASK) | base->cpu);
  24578. - }
  24579. + if (likely(base->running_timer != timer))
  24580. + base = switch_timer_base(timer, base, new_base);
  24581. }
  24582. timer->expires = expires;
  24583. @@ -1006,6 +1034,33 @@ void add_timer_on(struct timer_list *timer, int cpu)
  24584. }
  24585. EXPORT_SYMBOL_GPL(add_timer_on);
  24586. +#ifdef CONFIG_PREEMPT_RT_FULL
  24587. +/*
  24588. + * Wait for a running timer
  24589. + */
  24590. +static void wait_for_running_timer(struct timer_list *timer)
  24591. +{
  24592. + struct tvec_base *base;
  24593. + u32 tf = timer->flags;
  24594. +
  24595. + if (tf & TIMER_MIGRATING)
  24596. + return;
  24597. +
  24598. + base = per_cpu_ptr(&tvec_bases, tf & TIMER_CPUMASK);
  24599. + wait_event(base->wait_for_running_timer,
  24600. + base->running_timer != timer);
  24601. +}
  24602. +
  24603. +# define wakeup_timer_waiters(b) wake_up_all(&(b)->wait_for_running_timer)
  24604. +#else
  24605. +static inline void wait_for_running_timer(struct timer_list *timer)
  24606. +{
  24607. + cpu_relax();
  24608. +}
  24609. +
  24610. +# define wakeup_timer_waiters(b) do { } while (0)
  24611. +#endif
  24612. +
  24613. /**
  24614. * del_timer - deactive a timer.
  24615. * @timer: the timer to be deactivated
  24616. @@ -1063,7 +1118,7 @@ int try_to_del_timer_sync(struct timer_list *timer)
  24617. }
  24618. EXPORT_SYMBOL(try_to_del_timer_sync);
  24619. -#ifdef CONFIG_SMP
  24620. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  24621. /**
  24622. * del_timer_sync - deactivate a timer and wait for the handler to finish.
  24623. * @timer: the timer to be deactivated
  24624. @@ -1123,7 +1178,7 @@ int del_timer_sync(struct timer_list *timer)
  24625. int ret = try_to_del_timer_sync(timer);
  24626. if (ret >= 0)
  24627. return ret;
  24628. - cpu_relax();
  24629. + wait_for_running_timer(timer);
  24630. }
  24631. }
  24632. EXPORT_SYMBOL(del_timer_sync);
  24633. @@ -1248,16 +1303,18 @@ static inline void __run_timers(struct tvec_base *base)
  24634. if (irqsafe) {
  24635. spin_unlock(&base->lock);
  24636. call_timer_fn(timer, fn, data);
  24637. + base->running_timer = NULL;
  24638. spin_lock(&base->lock);
  24639. } else {
  24640. spin_unlock_irq(&base->lock);
  24641. call_timer_fn(timer, fn, data);
  24642. + base->running_timer = NULL;
  24643. spin_lock_irq(&base->lock);
  24644. }
  24645. }
  24646. }
  24647. - base->running_timer = NULL;
  24648. spin_unlock_irq(&base->lock);
  24649. + wakeup_timer_waiters(base);
  24650. }
  24651. #ifdef CONFIG_NO_HZ_COMMON
  24652. @@ -1390,6 +1447,14 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
  24653. if (cpu_is_offline(smp_processor_id()))
  24654. return expires;
  24655. +#ifdef CONFIG_PREEMPT_RT_FULL
  24656. + /*
  24657. + * On PREEMPT_RT we cannot sleep here. As a result we can't take
  24658. + * the base lock to check when the next timer is pending and so
  24659. + * we assume the next jiffy.
  24660. + */
  24661. + return basem + TICK_NSEC;
  24662. +#endif
  24663. spin_lock(&base->lock);
  24664. if (base->active_timers) {
  24665. if (time_before_eq(base->next_timer, base->timer_jiffies))
  24666. @@ -1416,13 +1481,13 @@ void update_process_times(int user_tick)
  24667. /* Note: this timer irq context must be accounted for as well. */
  24668. account_process_tick(p, user_tick);
  24669. + scheduler_tick();
  24670. run_local_timers();
  24671. rcu_check_callbacks(user_tick);
  24672. -#ifdef CONFIG_IRQ_WORK
  24673. +#if defined(CONFIG_IRQ_WORK)
  24674. if (in_irq())
  24675. irq_work_tick();
  24676. #endif
  24677. - scheduler_tick();
  24678. run_posix_cpu_timers(p);
  24679. }
  24680. @@ -1433,6 +1498,8 @@ static void run_timer_softirq(struct softirq_action *h)
  24681. {
  24682. struct tvec_base *base = this_cpu_ptr(&tvec_bases);
  24683. + irq_work_tick_soft();
  24684. +
  24685. if (time_after_eq(jiffies, base->timer_jiffies))
  24686. __run_timers(base);
  24687. }
  24688. @@ -1589,7 +1656,7 @@ static void migrate_timers(int cpu)
  24689. BUG_ON(cpu_online(cpu));
  24690. old_base = per_cpu_ptr(&tvec_bases, cpu);
  24691. - new_base = get_cpu_ptr(&tvec_bases);
  24692. + new_base = get_local_ptr(&tvec_bases);
  24693. /*
  24694. * The caller is globally serialized and nobody else
  24695. * takes two locks at once, deadlock is not possible.
  24696. @@ -1613,7 +1680,7 @@ static void migrate_timers(int cpu)
  24697. spin_unlock(&old_base->lock);
  24698. spin_unlock_irq(&new_base->lock);
  24699. - put_cpu_ptr(&tvec_bases);
  24700. + put_local_ptr(&tvec_bases);
  24701. }
  24702. static int timer_cpu_notify(struct notifier_block *self,
  24703. @@ -1645,6 +1712,9 @@ static void __init init_timer_cpu(int cpu)
  24704. base->cpu = cpu;
  24705. spin_lock_init(&base->lock);
  24706. +#ifdef CONFIG_PREEMPT_RT_FULL
  24707. + init_waitqueue_head(&base->wait_for_running_timer);
  24708. +#endif
  24709. base->timer_jiffies = jiffies;
  24710. base->next_timer = base->timer_jiffies;
  24711. diff --git a/kernel/trace/Kconfig b/kernel/trace/Kconfig
  24712. index e45db6b0d878..364ccd0eb57b 100644
  24713. --- a/kernel/trace/Kconfig
  24714. +++ b/kernel/trace/Kconfig
  24715. @@ -187,6 +187,24 @@ config IRQSOFF_TRACER
  24716. enabled. This option and the preempt-off timing option can be
  24717. used together or separately.)
  24718. +config INTERRUPT_OFF_HIST
  24719. + bool "Interrupts-off Latency Histogram"
  24720. + depends on IRQSOFF_TRACER
  24721. + help
  24722. + This option generates continuously updated histograms (one per cpu)
  24723. + of the duration of time periods with interrupts disabled. The
  24724. + histograms are disabled by default. To enable them, write a non-zero
  24725. + number to
  24726. +
  24727. + /sys/kernel/debug/tracing/latency_hist/enable/preemptirqsoff
  24728. +
  24729. + If PREEMPT_OFF_HIST is also selected, additional histograms (one
  24730. + per cpu) are generated that accumulate the duration of time periods
  24731. + when both interrupts and preemption are disabled. The histogram data
  24732. + will be located in the debug file system at
  24733. +
  24734. + /sys/kernel/debug/tracing/latency_hist/irqsoff
  24735. +
  24736. config PREEMPT_TRACER
  24737. bool "Preemption-off Latency Tracer"
  24738. default n
  24739. @@ -211,6 +229,24 @@ config PREEMPT_TRACER
  24740. enabled. This option and the irqs-off timing option can be
  24741. used together or separately.)
  24742. +config PREEMPT_OFF_HIST
  24743. + bool "Preemption-off Latency Histogram"
  24744. + depends on PREEMPT_TRACER
  24745. + help
  24746. + This option generates continuously updated histograms (one per cpu)
  24747. + of the duration of time periods with preemption disabled. The
  24748. + histograms are disabled by default. To enable them, write a non-zero
  24749. + number to
  24750. +
  24751. + /sys/kernel/debug/tracing/latency_hist/enable/preemptirqsoff
  24752. +
  24753. + If INTERRUPT_OFF_HIST is also selected, additional histograms (one
  24754. + per cpu) are generated that accumulate the duration of time periods
  24755. + when both interrupts and preemption are disabled. The histogram data
  24756. + will be located in the debug file system at
  24757. +
  24758. + /sys/kernel/debug/tracing/latency_hist/preemptoff
  24759. +
  24760. config SCHED_TRACER
  24761. bool "Scheduling Latency Tracer"
  24762. select GENERIC_TRACER
  24763. @@ -221,6 +257,74 @@ config SCHED_TRACER
  24764. This tracer tracks the latency of the highest priority task
  24765. to be scheduled in, starting from the point it has woken up.
  24766. +config WAKEUP_LATENCY_HIST
  24767. + bool "Scheduling Latency Histogram"
  24768. + depends on SCHED_TRACER
  24769. + help
  24770. + This option generates continuously updated histograms (one per cpu)
  24771. + of the scheduling latency of the highest priority task.
  24772. + The histograms are disabled by default. To enable them, write a
  24773. + non-zero number to
  24774. +
  24775. + /sys/kernel/debug/tracing/latency_hist/enable/wakeup
  24776. +
  24777. + Two different algorithms are used, one to determine the latency of
  24778. + processes that exclusively use the highest priority of the system and
  24779. + another one to determine the latency of processes that share the
  24780. + highest system priority with other processes. The former is used to
  24781. + improve hardware and system software, the latter to optimize the
  24782. + priority design of a given system. The histogram data will be
  24783. + located in the debug file system at
  24784. +
  24785. + /sys/kernel/debug/tracing/latency_hist/wakeup
  24786. +
  24787. + and
  24788. +
  24789. + /sys/kernel/debug/tracing/latency_hist/wakeup/sharedprio
  24790. +
  24791. + If both Scheduling Latency Histogram and Missed Timer Offsets
  24792. + Histogram are selected, additional histogram data will be collected
  24793. + that contain, in addition to the wakeup latency, the timer latency, in
  24794. + case the wakeup was triggered by an expired timer. These histograms
  24795. + are available in the
  24796. +
  24797. + /sys/kernel/debug/tracing/latency_hist/timerandwakeup
  24798. +
  24799. + directory. They reflect the apparent interrupt and scheduling latency
  24800. + and are best suitable to determine the worst-case latency of a given
  24801. + system. To enable these histograms, write a non-zero number to
  24802. +
  24803. + /sys/kernel/debug/tracing/latency_hist/enable/timerandwakeup
  24804. +
  24805. +config MISSED_TIMER_OFFSETS_HIST
  24806. + depends on HIGH_RES_TIMERS
  24807. + select GENERIC_TRACER
  24808. + bool "Missed Timer Offsets Histogram"
  24809. + help
  24810. + Generate a histogram of missed timer offsets in microseconds. The
  24811. + histograms are disabled by default. To enable them, write a non-zero
  24812. + number to
  24813. +
  24814. + /sys/kernel/debug/tracing/latency_hist/enable/missed_timer_offsets
  24815. +
  24816. + The histogram data will be located in the debug file system at
  24817. +
  24818. + /sys/kernel/debug/tracing/latency_hist/missed_timer_offsets
  24819. +
  24820. + If both Scheduling Latency Histogram and Missed Timer Offsets
  24821. + Histogram are selected, additional histogram data will be collected
  24822. + that contain, in addition to the wakeup latency, the timer latency, in
  24823. + case the wakeup was triggered by an expired timer. These histograms
  24824. + are available in the
  24825. +
  24826. + /sys/kernel/debug/tracing/latency_hist/timerandwakeup
  24827. +
  24828. + directory. They reflect the apparent interrupt and scheduling latency
  24829. + and are best suitable to determine the worst-case latency of a given
  24830. + system. To enable these histograms, write a non-zero number to
  24831. +
  24832. + /sys/kernel/debug/tracing/latency_hist/enable/timerandwakeup
  24833. +
  24834. config ENABLE_DEFAULT_TRACERS
  24835. bool "Trace process context switches and events"
  24836. depends on !GENERIC_TRACER
  24837. diff --git a/kernel/trace/Makefile b/kernel/trace/Makefile
  24838. index 05ea5167e6bb..bc08c67301ae 100644
  24839. --- a/kernel/trace/Makefile
  24840. +++ b/kernel/trace/Makefile
  24841. @@ -40,6 +40,10 @@ obj-$(CONFIG_FUNCTION_TRACER) += trace_functions.o
  24842. obj-$(CONFIG_IRQSOFF_TRACER) += trace_irqsoff.o
  24843. obj-$(CONFIG_PREEMPT_TRACER) += trace_irqsoff.o
  24844. obj-$(CONFIG_SCHED_TRACER) += trace_sched_wakeup.o
  24845. +obj-$(CONFIG_INTERRUPT_OFF_HIST) += latency_hist.o
  24846. +obj-$(CONFIG_PREEMPT_OFF_HIST) += latency_hist.o
  24847. +obj-$(CONFIG_WAKEUP_LATENCY_HIST) += latency_hist.o
  24848. +obj-$(CONFIG_MISSED_TIMER_OFFSETS_HIST) += latency_hist.o
  24849. obj-$(CONFIG_NOP_TRACER) += trace_nop.o
  24850. obj-$(CONFIG_STACK_TRACER) += trace_stack.o
  24851. obj-$(CONFIG_MMIOTRACE) += trace_mmiotrace.o
  24852. diff --git a/kernel/trace/latency_hist.c b/kernel/trace/latency_hist.c
  24853. new file mode 100644
  24854. index 000000000000..7f6ee70dea41
  24855. --- /dev/null
  24856. +++ b/kernel/trace/latency_hist.c
  24857. @@ -0,0 +1,1178 @@
  24858. +/*
  24859. + * kernel/trace/latency_hist.c
  24860. + *
  24861. + * Add support for histograms of preemption-off latency and
  24862. + * interrupt-off latency and wakeup latency, it depends on
  24863. + * Real-Time Preemption Support.
  24864. + *
  24865. + * Copyright (C) 2005 MontaVista Software, Inc.
  24866. + * Yi Yang <yyang@ch.mvista.com>
  24867. + *
  24868. + * Converted to work with the new latency tracer.
  24869. + * Copyright (C) 2008 Red Hat, Inc.
  24870. + * Steven Rostedt <srostedt@redhat.com>
  24871. + *
  24872. + */
  24873. +#include <linux/module.h>
  24874. +#include <linux/debugfs.h>
  24875. +#include <linux/seq_file.h>
  24876. +#include <linux/percpu.h>
  24877. +#include <linux/kallsyms.h>
  24878. +#include <linux/uaccess.h>
  24879. +#include <linux/sched.h>
  24880. +#include <linux/sched/rt.h>
  24881. +#include <linux/slab.h>
  24882. +#include <linux/atomic.h>
  24883. +#include <asm/div64.h>
  24884. +
  24885. +#include "trace.h"
  24886. +#include <trace/events/sched.h>
  24887. +
  24888. +#define NSECS_PER_USECS 1000L
  24889. +
  24890. +#define CREATE_TRACE_POINTS
  24891. +#include <trace/events/hist.h>
  24892. +
  24893. +enum {
  24894. + IRQSOFF_LATENCY = 0,
  24895. + PREEMPTOFF_LATENCY,
  24896. + PREEMPTIRQSOFF_LATENCY,
  24897. + WAKEUP_LATENCY,
  24898. + WAKEUP_LATENCY_SHAREDPRIO,
  24899. + MISSED_TIMER_OFFSETS,
  24900. + TIMERANDWAKEUP_LATENCY,
  24901. + MAX_LATENCY_TYPE,
  24902. +};
  24903. +
  24904. +#define MAX_ENTRY_NUM 10240
  24905. +
  24906. +struct hist_data {
  24907. + atomic_t hist_mode; /* 0 log, 1 don't log */
  24908. + long offset; /* set it to MAX_ENTRY_NUM/2 for a bipolar scale */
  24909. + long min_lat;
  24910. + long max_lat;
  24911. + unsigned long long below_hist_bound_samples;
  24912. + unsigned long long above_hist_bound_samples;
  24913. + long long accumulate_lat;
  24914. + unsigned long long total_samples;
  24915. + unsigned long long hist_array[MAX_ENTRY_NUM];
  24916. +};
  24917. +
  24918. +struct enable_data {
  24919. + int latency_type;
  24920. + int enabled;
  24921. +};
  24922. +
  24923. +static char *latency_hist_dir_root = "latency_hist";
  24924. +
  24925. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  24926. +static DEFINE_PER_CPU(struct hist_data, irqsoff_hist);
  24927. +static char *irqsoff_hist_dir = "irqsoff";
  24928. +static DEFINE_PER_CPU(cycles_t, hist_irqsoff_start);
  24929. +static DEFINE_PER_CPU(int, hist_irqsoff_counting);
  24930. +#endif
  24931. +
  24932. +#ifdef CONFIG_PREEMPT_OFF_HIST
  24933. +static DEFINE_PER_CPU(struct hist_data, preemptoff_hist);
  24934. +static char *preemptoff_hist_dir = "preemptoff";
  24935. +static DEFINE_PER_CPU(cycles_t, hist_preemptoff_start);
  24936. +static DEFINE_PER_CPU(int, hist_preemptoff_counting);
  24937. +#endif
  24938. +
  24939. +#if defined(CONFIG_PREEMPT_OFF_HIST) && defined(CONFIG_INTERRUPT_OFF_HIST)
  24940. +static DEFINE_PER_CPU(struct hist_data, preemptirqsoff_hist);
  24941. +static char *preemptirqsoff_hist_dir = "preemptirqsoff";
  24942. +static DEFINE_PER_CPU(cycles_t, hist_preemptirqsoff_start);
  24943. +static DEFINE_PER_CPU(int, hist_preemptirqsoff_counting);
  24944. +#endif
  24945. +
  24946. +#if defined(CONFIG_PREEMPT_OFF_HIST) || defined(CONFIG_INTERRUPT_OFF_HIST)
  24947. +static notrace void probe_preemptirqsoff_hist(void *v, int reason, int start);
  24948. +static struct enable_data preemptirqsoff_enabled_data = {
  24949. + .latency_type = PREEMPTIRQSOFF_LATENCY,
  24950. + .enabled = 0,
  24951. +};
  24952. +#endif
  24953. +
  24954. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  24955. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24956. +struct maxlatproc_data {
  24957. + char comm[FIELD_SIZEOF(struct task_struct, comm)];
  24958. + char current_comm[FIELD_SIZEOF(struct task_struct, comm)];
  24959. + int pid;
  24960. + int current_pid;
  24961. + int prio;
  24962. + int current_prio;
  24963. + long latency;
  24964. + long timeroffset;
  24965. + cycle_t timestamp;
  24966. +};
  24967. +#endif
  24968. +
  24969. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  24970. +static DEFINE_PER_CPU(struct hist_data, wakeup_latency_hist);
  24971. +static DEFINE_PER_CPU(struct hist_data, wakeup_latency_hist_sharedprio);
  24972. +static char *wakeup_latency_hist_dir = "wakeup";
  24973. +static char *wakeup_latency_hist_dir_sharedprio = "sharedprio";
  24974. +static notrace void probe_wakeup_latency_hist_start(void *v,
  24975. + struct task_struct *p);
  24976. +static notrace void probe_wakeup_latency_hist_stop(void *v,
  24977. + bool preempt, struct task_struct *prev, struct task_struct *next);
  24978. +static notrace void probe_sched_migrate_task(void *,
  24979. + struct task_struct *task, int cpu);
  24980. +static struct enable_data wakeup_latency_enabled_data = {
  24981. + .latency_type = WAKEUP_LATENCY,
  24982. + .enabled = 0,
  24983. +};
  24984. +static DEFINE_PER_CPU(struct maxlatproc_data, wakeup_maxlatproc);
  24985. +static DEFINE_PER_CPU(struct maxlatproc_data, wakeup_maxlatproc_sharedprio);
  24986. +static DEFINE_PER_CPU(struct task_struct *, wakeup_task);
  24987. +static DEFINE_PER_CPU(int, wakeup_sharedprio);
  24988. +static unsigned long wakeup_pid;
  24989. +#endif
  24990. +
  24991. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  24992. +static DEFINE_PER_CPU(struct hist_data, missed_timer_offsets);
  24993. +static char *missed_timer_offsets_dir = "missed_timer_offsets";
  24994. +static notrace void probe_hrtimer_interrupt(void *v, int cpu,
  24995. + long long offset, struct task_struct *curr, struct task_struct *task);
  24996. +static struct enable_data missed_timer_offsets_enabled_data = {
  24997. + .latency_type = MISSED_TIMER_OFFSETS,
  24998. + .enabled = 0,
  24999. +};
  25000. +static DEFINE_PER_CPU(struct maxlatproc_data, missed_timer_offsets_maxlatproc);
  25001. +static unsigned long missed_timer_offsets_pid;
  25002. +#endif
  25003. +
  25004. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  25005. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  25006. +static DEFINE_PER_CPU(struct hist_data, timerandwakeup_latency_hist);
  25007. +static char *timerandwakeup_latency_hist_dir = "timerandwakeup";
  25008. +static struct enable_data timerandwakeup_enabled_data = {
  25009. + .latency_type = TIMERANDWAKEUP_LATENCY,
  25010. + .enabled = 0,
  25011. +};
  25012. +static DEFINE_PER_CPU(struct maxlatproc_data, timerandwakeup_maxlatproc);
  25013. +#endif
  25014. +
  25015. +void notrace latency_hist(int latency_type, int cpu, long latency,
  25016. + long timeroffset, cycle_t stop,
  25017. + struct task_struct *p)
  25018. +{
  25019. + struct hist_data *my_hist;
  25020. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  25021. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  25022. + struct maxlatproc_data *mp = NULL;
  25023. +#endif
  25024. +
  25025. + if (!cpu_possible(cpu) || latency_type < 0 ||
  25026. + latency_type >= MAX_LATENCY_TYPE)
  25027. + return;
  25028. +
  25029. + switch (latency_type) {
  25030. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  25031. + case IRQSOFF_LATENCY:
  25032. + my_hist = &per_cpu(irqsoff_hist, cpu);
  25033. + break;
  25034. +#endif
  25035. +#ifdef CONFIG_PREEMPT_OFF_HIST
  25036. + case PREEMPTOFF_LATENCY:
  25037. + my_hist = &per_cpu(preemptoff_hist, cpu);
  25038. + break;
  25039. +#endif
  25040. +#if defined(CONFIG_PREEMPT_OFF_HIST) && defined(CONFIG_INTERRUPT_OFF_HIST)
  25041. + case PREEMPTIRQSOFF_LATENCY:
  25042. + my_hist = &per_cpu(preemptirqsoff_hist, cpu);
  25043. + break;
  25044. +#endif
  25045. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  25046. + case WAKEUP_LATENCY:
  25047. + my_hist = &per_cpu(wakeup_latency_hist, cpu);
  25048. + mp = &per_cpu(wakeup_maxlatproc, cpu);
  25049. + break;
  25050. + case WAKEUP_LATENCY_SHAREDPRIO:
  25051. + my_hist = &per_cpu(wakeup_latency_hist_sharedprio, cpu);
  25052. + mp = &per_cpu(wakeup_maxlatproc_sharedprio, cpu);
  25053. + break;
  25054. +#endif
  25055. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  25056. + case MISSED_TIMER_OFFSETS:
  25057. + my_hist = &per_cpu(missed_timer_offsets, cpu);
  25058. + mp = &per_cpu(missed_timer_offsets_maxlatproc, cpu);
  25059. + break;
  25060. +#endif
  25061. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  25062. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  25063. + case TIMERANDWAKEUP_LATENCY:
  25064. + my_hist = &per_cpu(timerandwakeup_latency_hist, cpu);
  25065. + mp = &per_cpu(timerandwakeup_maxlatproc, cpu);
  25066. + break;
  25067. +#endif
  25068. +
  25069. + default:
  25070. + return;
  25071. + }
  25072. +
  25073. + latency += my_hist->offset;
  25074. +
  25075. + if (atomic_read(&my_hist->hist_mode) == 0)
  25076. + return;
  25077. +
  25078. + if (latency < 0 || latency >= MAX_ENTRY_NUM) {
  25079. + if (latency < 0)
  25080. + my_hist->below_hist_bound_samples++;
  25081. + else
  25082. + my_hist->above_hist_bound_samples++;
  25083. + } else
  25084. + my_hist->hist_array[latency]++;
  25085. +
  25086. + if (unlikely(latency > my_hist->max_lat ||
  25087. + my_hist->min_lat == LONG_MAX)) {
  25088. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  25089. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  25090. + if (latency_type == WAKEUP_LATENCY ||
  25091. + latency_type == WAKEUP_LATENCY_SHAREDPRIO ||
  25092. + latency_type == MISSED_TIMER_OFFSETS ||
  25093. + latency_type == TIMERANDWAKEUP_LATENCY) {
  25094. + strncpy(mp->comm, p->comm, sizeof(mp->comm));
  25095. + strncpy(mp->current_comm, current->comm,
  25096. + sizeof(mp->current_comm));
  25097. + mp->pid = task_pid_nr(p);
  25098. + mp->current_pid = task_pid_nr(current);
  25099. + mp->prio = p->prio;
  25100. + mp->current_prio = current->prio;
  25101. + mp->latency = latency;
  25102. + mp->timeroffset = timeroffset;
  25103. + mp->timestamp = stop;
  25104. + }
  25105. +#endif
  25106. + my_hist->max_lat = latency;
  25107. + }
  25108. + if (unlikely(latency < my_hist->min_lat))
  25109. + my_hist->min_lat = latency;
  25110. + my_hist->total_samples++;
  25111. + my_hist->accumulate_lat += latency;
  25112. +}
  25113. +
  25114. +static void *l_start(struct seq_file *m, loff_t *pos)
  25115. +{
  25116. + loff_t *index_ptr = NULL;
  25117. + loff_t index = *pos;
  25118. + struct hist_data *my_hist = m->private;
  25119. +
  25120. + if (index == 0) {
  25121. + char minstr[32], avgstr[32], maxstr[32];
  25122. +
  25123. + atomic_dec(&my_hist->hist_mode);
  25124. +
  25125. + if (likely(my_hist->total_samples)) {
  25126. + long avg = (long) div64_s64(my_hist->accumulate_lat,
  25127. + my_hist->total_samples);
  25128. + snprintf(minstr, sizeof(minstr), "%ld",
  25129. + my_hist->min_lat - my_hist->offset);
  25130. + snprintf(avgstr, sizeof(avgstr), "%ld",
  25131. + avg - my_hist->offset);
  25132. + snprintf(maxstr, sizeof(maxstr), "%ld",
  25133. + my_hist->max_lat - my_hist->offset);
  25134. + } else {
  25135. + strcpy(minstr, "<undef>");
  25136. + strcpy(avgstr, minstr);
  25137. + strcpy(maxstr, minstr);
  25138. + }
  25139. +
  25140. + seq_printf(m, "#Minimum latency: %s microseconds\n"
  25141. + "#Average latency: %s microseconds\n"
  25142. + "#Maximum latency: %s microseconds\n"
  25143. + "#Total samples: %llu\n"
  25144. + "#There are %llu samples lower than %ld"
  25145. + " microseconds.\n"
  25146. + "#There are %llu samples greater or equal"
  25147. + " than %ld microseconds.\n"
  25148. + "#usecs\t%16s\n",
  25149. + minstr, avgstr, maxstr,
  25150. + my_hist->total_samples,
  25151. + my_hist->below_hist_bound_samples,
  25152. + -my_hist->offset,
  25153. + my_hist->above_hist_bound_samples,
  25154. + MAX_ENTRY_NUM - my_hist->offset,
  25155. + "samples");
  25156. + }
  25157. + if (index < MAX_ENTRY_NUM) {
  25158. + index_ptr = kmalloc(sizeof(loff_t), GFP_KERNEL);
  25159. + if (index_ptr)
  25160. + *index_ptr = index;
  25161. + }
  25162. +
  25163. + return index_ptr;
  25164. +}
  25165. +
  25166. +static void *l_next(struct seq_file *m, void *p, loff_t *pos)
  25167. +{
  25168. + loff_t *index_ptr = p;
  25169. + struct hist_data *my_hist = m->private;
  25170. +
  25171. + if (++*pos >= MAX_ENTRY_NUM) {
  25172. + atomic_inc(&my_hist->hist_mode);
  25173. + return NULL;
  25174. + }
  25175. + *index_ptr = *pos;
  25176. + return index_ptr;
  25177. +}
  25178. +
  25179. +static void l_stop(struct seq_file *m, void *p)
  25180. +{
  25181. + kfree(p);
  25182. +}
  25183. +
  25184. +static int l_show(struct seq_file *m, void *p)
  25185. +{
  25186. + int index = *(loff_t *) p;
  25187. + struct hist_data *my_hist = m->private;
  25188. +
  25189. + seq_printf(m, "%6ld\t%16llu\n", index - my_hist->offset,
  25190. + my_hist->hist_array[index]);
  25191. + return 0;
  25192. +}
  25193. +
  25194. +static const struct seq_operations latency_hist_seq_op = {
  25195. + .start = l_start,
  25196. + .next = l_next,
  25197. + .stop = l_stop,
  25198. + .show = l_show
  25199. +};
  25200. +
  25201. +static int latency_hist_open(struct inode *inode, struct file *file)
  25202. +{
  25203. + int ret;
  25204. +
  25205. + ret = seq_open(file, &latency_hist_seq_op);
  25206. + if (!ret) {
  25207. + struct seq_file *seq = file->private_data;
  25208. + seq->private = inode->i_private;
  25209. + }
  25210. + return ret;
  25211. +}
  25212. +
  25213. +static const struct file_operations latency_hist_fops = {
  25214. + .open = latency_hist_open,
  25215. + .read = seq_read,
  25216. + .llseek = seq_lseek,
  25217. + .release = seq_release,
  25218. +};
  25219. +
  25220. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  25221. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  25222. +static void clear_maxlatprocdata(struct maxlatproc_data *mp)
  25223. +{
  25224. + mp->comm[0] = mp->current_comm[0] = '\0';
  25225. + mp->prio = mp->current_prio = mp->pid = mp->current_pid =
  25226. + mp->latency = mp->timeroffset = -1;
  25227. + mp->timestamp = 0;
  25228. +}
  25229. +#endif
  25230. +
  25231. +static void hist_reset(struct hist_data *hist)
  25232. +{
  25233. + atomic_dec(&hist->hist_mode);
  25234. +
  25235. + memset(hist->hist_array, 0, sizeof(hist->hist_array));
  25236. + hist->below_hist_bound_samples = 0ULL;
  25237. + hist->above_hist_bound_samples = 0ULL;
  25238. + hist->min_lat = LONG_MAX;
  25239. + hist->max_lat = LONG_MIN;
  25240. + hist->total_samples = 0ULL;
  25241. + hist->accumulate_lat = 0LL;
  25242. +
  25243. + atomic_inc(&hist->hist_mode);
  25244. +}
  25245. +
  25246. +static ssize_t
  25247. +latency_hist_reset(struct file *file, const char __user *a,
  25248. + size_t size, loff_t *off)
  25249. +{
  25250. + int cpu;
  25251. + struct hist_data *hist = NULL;
  25252. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  25253. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  25254. + struct maxlatproc_data *mp = NULL;
  25255. +#endif
  25256. + off_t latency_type = (off_t) file->private_data;
  25257. +
  25258. + for_each_online_cpu(cpu) {
  25259. +
  25260. + switch (latency_type) {
  25261. +#ifdef CONFIG_PREEMPT_OFF_HIST
  25262. + case PREEMPTOFF_LATENCY:
  25263. + hist = &per_cpu(preemptoff_hist, cpu);
  25264. + break;
  25265. +#endif
  25266. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  25267. + case IRQSOFF_LATENCY:
  25268. + hist = &per_cpu(irqsoff_hist, cpu);
  25269. + break;
  25270. +#endif
  25271. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  25272. + case PREEMPTIRQSOFF_LATENCY:
  25273. + hist = &per_cpu(preemptirqsoff_hist, cpu);
  25274. + break;
  25275. +#endif
  25276. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  25277. + case WAKEUP_LATENCY:
  25278. + hist = &per_cpu(wakeup_latency_hist, cpu);
  25279. + mp = &per_cpu(wakeup_maxlatproc, cpu);
  25280. + break;
  25281. + case WAKEUP_LATENCY_SHAREDPRIO:
  25282. + hist = &per_cpu(wakeup_latency_hist_sharedprio, cpu);
  25283. + mp = &per_cpu(wakeup_maxlatproc_sharedprio, cpu);
  25284. + break;
  25285. +#endif
  25286. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  25287. + case MISSED_TIMER_OFFSETS:
  25288. + hist = &per_cpu(missed_timer_offsets, cpu);
  25289. + mp = &per_cpu(missed_timer_offsets_maxlatproc, cpu);
  25290. + break;
  25291. +#endif
  25292. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  25293. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  25294. + case TIMERANDWAKEUP_LATENCY:
  25295. + hist = &per_cpu(timerandwakeup_latency_hist, cpu);
  25296. + mp = &per_cpu(timerandwakeup_maxlatproc, cpu);
  25297. + break;
  25298. +#endif
  25299. + }
  25300. +
  25301. + hist_reset(hist);
  25302. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  25303. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  25304. + if (latency_type == WAKEUP_LATENCY ||
  25305. + latency_type == WAKEUP_LATENCY_SHAREDPRIO ||
  25306. + latency_type == MISSED_TIMER_OFFSETS ||
  25307. + latency_type == TIMERANDWAKEUP_LATENCY)
  25308. + clear_maxlatprocdata(mp);
  25309. +#endif
  25310. + }
  25311. +
  25312. + return size;
  25313. +}
  25314. +
  25315. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  25316. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  25317. +static ssize_t
  25318. +show_pid(struct file *file, char __user *ubuf, size_t cnt, loff_t *ppos)
  25319. +{
  25320. + char buf[64];
  25321. + int r;
  25322. + unsigned long *this_pid = file->private_data;
  25323. +
  25324. + r = snprintf(buf, sizeof(buf), "%lu\n", *this_pid);
  25325. + return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  25326. +}
  25327. +
  25328. +static ssize_t do_pid(struct file *file, const char __user *ubuf,
  25329. + size_t cnt, loff_t *ppos)
  25330. +{
  25331. + char buf[64];
  25332. + unsigned long pid;
  25333. + unsigned long *this_pid = file->private_data;
  25334. +
  25335. + if (cnt >= sizeof(buf))
  25336. + return -EINVAL;
  25337. +
  25338. + if (copy_from_user(&buf, ubuf, cnt))
  25339. + return -EFAULT;
  25340. +
  25341. + buf[cnt] = '\0';
  25342. +
  25343. + if (kstrtoul(buf, 10, &pid))
  25344. + return -EINVAL;
  25345. +
  25346. + *this_pid = pid;
  25347. +
  25348. + return cnt;
  25349. +}
  25350. +#endif
  25351. +
  25352. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  25353. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  25354. +static ssize_t
  25355. +show_maxlatproc(struct file *file, char __user *ubuf, size_t cnt, loff_t *ppos)
  25356. +{
  25357. + int r;
  25358. + struct maxlatproc_data *mp = file->private_data;
  25359. + int strmaxlen = (TASK_COMM_LEN * 2) + (8 * 8);
  25360. + unsigned long long t;
  25361. + unsigned long usecs, secs;
  25362. + char *buf;
  25363. +
  25364. + if (mp->pid == -1 || mp->current_pid == -1) {
  25365. + buf = "(none)\n";
  25366. + return simple_read_from_buffer(ubuf, cnt, ppos, buf,
  25367. + strlen(buf));
  25368. + }
  25369. +
  25370. + buf = kmalloc(strmaxlen, GFP_KERNEL);
  25371. + if (buf == NULL)
  25372. + return -ENOMEM;
  25373. +
  25374. + t = ns2usecs(mp->timestamp);
  25375. + usecs = do_div(t, USEC_PER_SEC);
  25376. + secs = (unsigned long) t;
  25377. + r = snprintf(buf, strmaxlen,
  25378. + "%d %d %ld (%ld) %s <- %d %d %s %lu.%06lu\n", mp->pid,
  25379. + MAX_RT_PRIO-1 - mp->prio, mp->latency, mp->timeroffset, mp->comm,
  25380. + mp->current_pid, MAX_RT_PRIO-1 - mp->current_prio, mp->current_comm,
  25381. + secs, usecs);
  25382. + r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  25383. + kfree(buf);
  25384. + return r;
  25385. +}
  25386. +#endif
  25387. +
  25388. +static ssize_t
  25389. +show_enable(struct file *file, char __user *ubuf, size_t cnt, loff_t *ppos)
  25390. +{
  25391. + char buf[64];
  25392. + struct enable_data *ed = file->private_data;
  25393. + int r;
  25394. +
  25395. + r = snprintf(buf, sizeof(buf), "%d\n", ed->enabled);
  25396. + return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  25397. +}
  25398. +
  25399. +static ssize_t
  25400. +do_enable(struct file *file, const char __user *ubuf, size_t cnt, loff_t *ppos)
  25401. +{
  25402. + char buf[64];
  25403. + long enable;
  25404. + struct enable_data *ed = file->private_data;
  25405. +
  25406. + if (cnt >= sizeof(buf))
  25407. + return -EINVAL;
  25408. +
  25409. + if (copy_from_user(&buf, ubuf, cnt))
  25410. + return -EFAULT;
  25411. +
  25412. + buf[cnt] = 0;
  25413. +
  25414. + if (kstrtoul(buf, 10, &enable))
  25415. + return -EINVAL;
  25416. +
  25417. + if ((enable && ed->enabled) || (!enable && !ed->enabled))
  25418. + return cnt;
  25419. +
  25420. + if (enable) {
  25421. + int ret;
  25422. +
  25423. + switch (ed->latency_type) {
  25424. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  25425. + case PREEMPTIRQSOFF_LATENCY:
  25426. + ret = register_trace_preemptirqsoff_hist(
  25427. + probe_preemptirqsoff_hist, NULL);
  25428. + if (ret) {
  25429. + pr_info("wakeup trace: Couldn't assign "
  25430. + "probe_preemptirqsoff_hist "
  25431. + "to trace_preemptirqsoff_hist\n");
  25432. + return ret;
  25433. + }
  25434. + break;
  25435. +#endif
  25436. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  25437. + case WAKEUP_LATENCY:
  25438. + ret = register_trace_sched_wakeup(
  25439. + probe_wakeup_latency_hist_start, NULL);
  25440. + if (ret) {
  25441. + pr_info("wakeup trace: Couldn't assign "
  25442. + "probe_wakeup_latency_hist_start "
  25443. + "to trace_sched_wakeup\n");
  25444. + return ret;
  25445. + }
  25446. + ret = register_trace_sched_wakeup_new(
  25447. + probe_wakeup_latency_hist_start, NULL);
  25448. + if (ret) {
  25449. + pr_info("wakeup trace: Couldn't assign "
  25450. + "probe_wakeup_latency_hist_start "
  25451. + "to trace_sched_wakeup_new\n");
  25452. + unregister_trace_sched_wakeup(
  25453. + probe_wakeup_latency_hist_start, NULL);
  25454. + return ret;
  25455. + }
  25456. + ret = register_trace_sched_switch(
  25457. + probe_wakeup_latency_hist_stop, NULL);
  25458. + if (ret) {
  25459. + pr_info("wakeup trace: Couldn't assign "
  25460. + "probe_wakeup_latency_hist_stop "
  25461. + "to trace_sched_switch\n");
  25462. + unregister_trace_sched_wakeup(
  25463. + probe_wakeup_latency_hist_start, NULL);
  25464. + unregister_trace_sched_wakeup_new(
  25465. + probe_wakeup_latency_hist_start, NULL);
  25466. + return ret;
  25467. + }
  25468. + ret = register_trace_sched_migrate_task(
  25469. + probe_sched_migrate_task, NULL);
  25470. + if (ret) {
  25471. + pr_info("wakeup trace: Couldn't assign "
  25472. + "probe_sched_migrate_task "
  25473. + "to trace_sched_migrate_task\n");
  25474. + unregister_trace_sched_wakeup(
  25475. + probe_wakeup_latency_hist_start, NULL);
  25476. + unregister_trace_sched_wakeup_new(
  25477. + probe_wakeup_latency_hist_start, NULL);
  25478. + unregister_trace_sched_switch(
  25479. + probe_wakeup_latency_hist_stop, NULL);
  25480. + return ret;
  25481. + }
  25482. + break;
  25483. +#endif
  25484. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  25485. + case MISSED_TIMER_OFFSETS:
  25486. + ret = register_trace_hrtimer_interrupt(
  25487. + probe_hrtimer_interrupt, NULL);
  25488. + if (ret) {
  25489. + pr_info("wakeup trace: Couldn't assign "
  25490. + "probe_hrtimer_interrupt "
  25491. + "to trace_hrtimer_interrupt\n");
  25492. + return ret;
  25493. + }
  25494. + break;
  25495. +#endif
  25496. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  25497. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  25498. + case TIMERANDWAKEUP_LATENCY:
  25499. + if (!wakeup_latency_enabled_data.enabled ||
  25500. + !missed_timer_offsets_enabled_data.enabled)
  25501. + return -EINVAL;
  25502. + break;
  25503. +#endif
  25504. + default:
  25505. + break;
  25506. + }
  25507. + } else {
  25508. + switch (ed->latency_type) {
  25509. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  25510. + case PREEMPTIRQSOFF_LATENCY:
  25511. + {
  25512. + int cpu;
  25513. +
  25514. + unregister_trace_preemptirqsoff_hist(
  25515. + probe_preemptirqsoff_hist, NULL);
  25516. + for_each_online_cpu(cpu) {
  25517. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  25518. + per_cpu(hist_irqsoff_counting,
  25519. + cpu) = 0;
  25520. +#endif
  25521. +#ifdef CONFIG_PREEMPT_OFF_HIST
  25522. + per_cpu(hist_preemptoff_counting,
  25523. + cpu) = 0;
  25524. +#endif
  25525. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  25526. + per_cpu(hist_preemptirqsoff_counting,
  25527. + cpu) = 0;
  25528. +#endif
  25529. + }
  25530. + }
  25531. + break;
  25532. +#endif
  25533. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  25534. + case WAKEUP_LATENCY:
  25535. + {
  25536. + int cpu;
  25537. +
  25538. + unregister_trace_sched_wakeup(
  25539. + probe_wakeup_latency_hist_start, NULL);
  25540. + unregister_trace_sched_wakeup_new(
  25541. + probe_wakeup_latency_hist_start, NULL);
  25542. + unregister_trace_sched_switch(
  25543. + probe_wakeup_latency_hist_stop, NULL);
  25544. + unregister_trace_sched_migrate_task(
  25545. + probe_sched_migrate_task, NULL);
  25546. +
  25547. + for_each_online_cpu(cpu) {
  25548. + per_cpu(wakeup_task, cpu) = NULL;
  25549. + per_cpu(wakeup_sharedprio, cpu) = 0;
  25550. + }
  25551. + }
  25552. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  25553. + timerandwakeup_enabled_data.enabled = 0;
  25554. +#endif
  25555. + break;
  25556. +#endif
  25557. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  25558. + case MISSED_TIMER_OFFSETS:
  25559. + unregister_trace_hrtimer_interrupt(
  25560. + probe_hrtimer_interrupt, NULL);
  25561. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  25562. + timerandwakeup_enabled_data.enabled = 0;
  25563. +#endif
  25564. + break;
  25565. +#endif
  25566. + default:
  25567. + break;
  25568. + }
  25569. + }
  25570. + ed->enabled = enable;
  25571. + return cnt;
  25572. +}
  25573. +
  25574. +static const struct file_operations latency_hist_reset_fops = {
  25575. + .open = tracing_open_generic,
  25576. + .write = latency_hist_reset,
  25577. +};
  25578. +
  25579. +static const struct file_operations enable_fops = {
  25580. + .open = tracing_open_generic,
  25581. + .read = show_enable,
  25582. + .write = do_enable,
  25583. +};
  25584. +
  25585. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  25586. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  25587. +static const struct file_operations pid_fops = {
  25588. + .open = tracing_open_generic,
  25589. + .read = show_pid,
  25590. + .write = do_pid,
  25591. +};
  25592. +
  25593. +static const struct file_operations maxlatproc_fops = {
  25594. + .open = tracing_open_generic,
  25595. + .read = show_maxlatproc,
  25596. +};
  25597. +#endif
  25598. +
  25599. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  25600. +static notrace void probe_preemptirqsoff_hist(void *v, int reason,
  25601. + int starthist)
  25602. +{
  25603. + int cpu = raw_smp_processor_id();
  25604. + int time_set = 0;
  25605. +
  25606. + if (starthist) {
  25607. + cycle_t uninitialized_var(start);
  25608. +
  25609. + if (!preempt_count() && !irqs_disabled())
  25610. + return;
  25611. +
  25612. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  25613. + if ((reason == IRQS_OFF || reason == TRACE_START) &&
  25614. + !per_cpu(hist_irqsoff_counting, cpu)) {
  25615. + per_cpu(hist_irqsoff_counting, cpu) = 1;
  25616. + start = ftrace_now(cpu);
  25617. + time_set++;
  25618. + per_cpu(hist_irqsoff_start, cpu) = start;
  25619. + }
  25620. +#endif
  25621. +
  25622. +#ifdef CONFIG_PREEMPT_OFF_HIST
  25623. + if ((reason == PREEMPT_OFF || reason == TRACE_START) &&
  25624. + !per_cpu(hist_preemptoff_counting, cpu)) {
  25625. + per_cpu(hist_preemptoff_counting, cpu) = 1;
  25626. + if (!(time_set++))
  25627. + start = ftrace_now(cpu);
  25628. + per_cpu(hist_preemptoff_start, cpu) = start;
  25629. + }
  25630. +#endif
  25631. +
  25632. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  25633. + if (per_cpu(hist_irqsoff_counting, cpu) &&
  25634. + per_cpu(hist_preemptoff_counting, cpu) &&
  25635. + !per_cpu(hist_preemptirqsoff_counting, cpu)) {
  25636. + per_cpu(hist_preemptirqsoff_counting, cpu) = 1;
  25637. + if (!time_set)
  25638. + start = ftrace_now(cpu);
  25639. + per_cpu(hist_preemptirqsoff_start, cpu) = start;
  25640. + }
  25641. +#endif
  25642. + } else {
  25643. + cycle_t uninitialized_var(stop);
  25644. +
  25645. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  25646. + if ((reason == IRQS_ON || reason == TRACE_STOP) &&
  25647. + per_cpu(hist_irqsoff_counting, cpu)) {
  25648. + cycle_t start = per_cpu(hist_irqsoff_start, cpu);
  25649. +
  25650. + stop = ftrace_now(cpu);
  25651. + time_set++;
  25652. + if (start) {
  25653. + long latency = ((long) (stop - start)) /
  25654. + NSECS_PER_USECS;
  25655. +
  25656. + latency_hist(IRQSOFF_LATENCY, cpu, latency, 0,
  25657. + stop, NULL);
  25658. + }
  25659. + per_cpu(hist_irqsoff_counting, cpu) = 0;
  25660. + }
  25661. +#endif
  25662. +
  25663. +#ifdef CONFIG_PREEMPT_OFF_HIST
  25664. + if ((reason == PREEMPT_ON || reason == TRACE_STOP) &&
  25665. + per_cpu(hist_preemptoff_counting, cpu)) {
  25666. + cycle_t start = per_cpu(hist_preemptoff_start, cpu);
  25667. +
  25668. + if (!(time_set++))
  25669. + stop = ftrace_now(cpu);
  25670. + if (start) {
  25671. + long latency = ((long) (stop - start)) /
  25672. + NSECS_PER_USECS;
  25673. +
  25674. + latency_hist(PREEMPTOFF_LATENCY, cpu, latency,
  25675. + 0, stop, NULL);
  25676. + }
  25677. + per_cpu(hist_preemptoff_counting, cpu) = 0;
  25678. + }
  25679. +#endif
  25680. +
  25681. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  25682. + if ((!per_cpu(hist_irqsoff_counting, cpu) ||
  25683. + !per_cpu(hist_preemptoff_counting, cpu)) &&
  25684. + per_cpu(hist_preemptirqsoff_counting, cpu)) {
  25685. + cycle_t start = per_cpu(hist_preemptirqsoff_start, cpu);
  25686. +
  25687. + if (!time_set)
  25688. + stop = ftrace_now(cpu);
  25689. + if (start) {
  25690. + long latency = ((long) (stop - start)) /
  25691. + NSECS_PER_USECS;
  25692. +
  25693. + latency_hist(PREEMPTIRQSOFF_LATENCY, cpu,
  25694. + latency, 0, stop, NULL);
  25695. + }
  25696. + per_cpu(hist_preemptirqsoff_counting, cpu) = 0;
  25697. + }
  25698. +#endif
  25699. + }
  25700. +}
  25701. +#endif
  25702. +
  25703. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  25704. +static DEFINE_RAW_SPINLOCK(wakeup_lock);
  25705. +static notrace void probe_sched_migrate_task(void *v, struct task_struct *task,
  25706. + int cpu)
  25707. +{
  25708. + int old_cpu = task_cpu(task);
  25709. +
  25710. + if (cpu != old_cpu) {
  25711. + unsigned long flags;
  25712. + struct task_struct *cpu_wakeup_task;
  25713. +
  25714. + raw_spin_lock_irqsave(&wakeup_lock, flags);
  25715. +
  25716. + cpu_wakeup_task = per_cpu(wakeup_task, old_cpu);
  25717. + if (task == cpu_wakeup_task) {
  25718. + put_task_struct(cpu_wakeup_task);
  25719. + per_cpu(wakeup_task, old_cpu) = NULL;
  25720. + cpu_wakeup_task = per_cpu(wakeup_task, cpu) = task;
  25721. + get_task_struct(cpu_wakeup_task);
  25722. + }
  25723. +
  25724. + raw_spin_unlock_irqrestore(&wakeup_lock, flags);
  25725. + }
  25726. +}
  25727. +
  25728. +static notrace void probe_wakeup_latency_hist_start(void *v,
  25729. + struct task_struct *p)
  25730. +{
  25731. + unsigned long flags;
  25732. + struct task_struct *curr = current;
  25733. + int cpu = task_cpu(p);
  25734. + struct task_struct *cpu_wakeup_task;
  25735. +
  25736. + raw_spin_lock_irqsave(&wakeup_lock, flags);
  25737. +
  25738. + cpu_wakeup_task = per_cpu(wakeup_task, cpu);
  25739. +
  25740. + if (wakeup_pid) {
  25741. + if ((cpu_wakeup_task && p->prio == cpu_wakeup_task->prio) ||
  25742. + p->prio == curr->prio)
  25743. + per_cpu(wakeup_sharedprio, cpu) = 1;
  25744. + if (likely(wakeup_pid != task_pid_nr(p)))
  25745. + goto out;
  25746. + } else {
  25747. + if (likely(!rt_task(p)) ||
  25748. + (cpu_wakeup_task && p->prio > cpu_wakeup_task->prio) ||
  25749. + p->prio > curr->prio)
  25750. + goto out;
  25751. + if ((cpu_wakeup_task && p->prio == cpu_wakeup_task->prio) ||
  25752. + p->prio == curr->prio)
  25753. + per_cpu(wakeup_sharedprio, cpu) = 1;
  25754. + }
  25755. +
  25756. + if (cpu_wakeup_task)
  25757. + put_task_struct(cpu_wakeup_task);
  25758. + cpu_wakeup_task = per_cpu(wakeup_task, cpu) = p;
  25759. + get_task_struct(cpu_wakeup_task);
  25760. + cpu_wakeup_task->preempt_timestamp_hist =
  25761. + ftrace_now(raw_smp_processor_id());
  25762. +out:
  25763. + raw_spin_unlock_irqrestore(&wakeup_lock, flags);
  25764. +}
  25765. +
  25766. +static notrace void probe_wakeup_latency_hist_stop(void *v,
  25767. + bool preempt, struct task_struct *prev, struct task_struct *next)
  25768. +{
  25769. + unsigned long flags;
  25770. + int cpu = task_cpu(next);
  25771. + long latency;
  25772. + cycle_t stop;
  25773. + struct task_struct *cpu_wakeup_task;
  25774. +
  25775. + raw_spin_lock_irqsave(&wakeup_lock, flags);
  25776. +
  25777. + cpu_wakeup_task = per_cpu(wakeup_task, cpu);
  25778. +
  25779. + if (cpu_wakeup_task == NULL)
  25780. + goto out;
  25781. +
  25782. + /* Already running? */
  25783. + if (unlikely(current == cpu_wakeup_task))
  25784. + goto out_reset;
  25785. +
  25786. + if (next != cpu_wakeup_task) {
  25787. + if (next->prio < cpu_wakeup_task->prio)
  25788. + goto out_reset;
  25789. +
  25790. + if (next->prio == cpu_wakeup_task->prio)
  25791. + per_cpu(wakeup_sharedprio, cpu) = 1;
  25792. +
  25793. + goto out;
  25794. + }
  25795. +
  25796. + if (current->prio == cpu_wakeup_task->prio)
  25797. + per_cpu(wakeup_sharedprio, cpu) = 1;
  25798. +
  25799. + /*
  25800. + * The task we are waiting for is about to be switched to.
  25801. + * Calculate latency and store it in histogram.
  25802. + */
  25803. + stop = ftrace_now(raw_smp_processor_id());
  25804. +
  25805. + latency = ((long) (stop - next->preempt_timestamp_hist)) /
  25806. + NSECS_PER_USECS;
  25807. +
  25808. + if (per_cpu(wakeup_sharedprio, cpu)) {
  25809. + latency_hist(WAKEUP_LATENCY_SHAREDPRIO, cpu, latency, 0, stop,
  25810. + next);
  25811. + per_cpu(wakeup_sharedprio, cpu) = 0;
  25812. + } else {
  25813. + latency_hist(WAKEUP_LATENCY, cpu, latency, 0, stop, next);
  25814. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  25815. + if (timerandwakeup_enabled_data.enabled) {
  25816. + latency_hist(TIMERANDWAKEUP_LATENCY, cpu,
  25817. + next->timer_offset + latency, next->timer_offset,
  25818. + stop, next);
  25819. + }
  25820. +#endif
  25821. + }
  25822. +
  25823. +out_reset:
  25824. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  25825. + next->timer_offset = 0;
  25826. +#endif
  25827. + put_task_struct(cpu_wakeup_task);
  25828. + per_cpu(wakeup_task, cpu) = NULL;
  25829. +out:
  25830. + raw_spin_unlock_irqrestore(&wakeup_lock, flags);
  25831. +}
  25832. +#endif
  25833. +
  25834. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  25835. +static notrace void probe_hrtimer_interrupt(void *v, int cpu,
  25836. + long long latency_ns, struct task_struct *curr,
  25837. + struct task_struct *task)
  25838. +{
  25839. + if (latency_ns <= 0 && task != NULL && rt_task(task) &&
  25840. + (task->prio < curr->prio ||
  25841. + (task->prio == curr->prio &&
  25842. + !cpumask_test_cpu(cpu, &task->cpus_allowed)))) {
  25843. + long latency;
  25844. + cycle_t now;
  25845. +
  25846. + if (missed_timer_offsets_pid) {
  25847. + if (likely(missed_timer_offsets_pid !=
  25848. + task_pid_nr(task)))
  25849. + return;
  25850. + }
  25851. +
  25852. + now = ftrace_now(cpu);
  25853. + latency = (long) div_s64(-latency_ns, NSECS_PER_USECS);
  25854. + latency_hist(MISSED_TIMER_OFFSETS, cpu, latency, latency, now,
  25855. + task);
  25856. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  25857. + task->timer_offset = latency;
  25858. +#endif
  25859. + }
  25860. +}
  25861. +#endif
  25862. +
  25863. +static __init int latency_hist_init(void)
  25864. +{
  25865. + struct dentry *latency_hist_root = NULL;
  25866. + struct dentry *dentry;
  25867. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  25868. + struct dentry *dentry_sharedprio;
  25869. +#endif
  25870. + struct dentry *entry;
  25871. + struct dentry *enable_root;
  25872. + int i = 0;
  25873. + struct hist_data *my_hist;
  25874. + char name[64];
  25875. + char *cpufmt = "CPU%d";
  25876. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  25877. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  25878. + char *cpufmt_maxlatproc = "max_latency-CPU%d";
  25879. + struct maxlatproc_data *mp = NULL;
  25880. +#endif
  25881. +
  25882. + dentry = tracing_init_dentry();
  25883. + latency_hist_root = debugfs_create_dir(latency_hist_dir_root, dentry);
  25884. + enable_root = debugfs_create_dir("enable", latency_hist_root);
  25885. +
  25886. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  25887. + dentry = debugfs_create_dir(irqsoff_hist_dir, latency_hist_root);
  25888. + for_each_possible_cpu(i) {
  25889. + sprintf(name, cpufmt, i);
  25890. + entry = debugfs_create_file(name, 0444, dentry,
  25891. + &per_cpu(irqsoff_hist, i), &latency_hist_fops);
  25892. + my_hist = &per_cpu(irqsoff_hist, i);
  25893. + atomic_set(&my_hist->hist_mode, 1);
  25894. + my_hist->min_lat = LONG_MAX;
  25895. + }
  25896. + entry = debugfs_create_file("reset", 0644, dentry,
  25897. + (void *)IRQSOFF_LATENCY, &latency_hist_reset_fops);
  25898. +#endif
  25899. +
  25900. +#ifdef CONFIG_PREEMPT_OFF_HIST
  25901. + dentry = debugfs_create_dir(preemptoff_hist_dir,
  25902. + latency_hist_root);
  25903. + for_each_possible_cpu(i) {
  25904. + sprintf(name, cpufmt, i);
  25905. + entry = debugfs_create_file(name, 0444, dentry,
  25906. + &per_cpu(preemptoff_hist, i), &latency_hist_fops);
  25907. + my_hist = &per_cpu(preemptoff_hist, i);
  25908. + atomic_set(&my_hist->hist_mode, 1);
  25909. + my_hist->min_lat = LONG_MAX;
  25910. + }
  25911. + entry = debugfs_create_file("reset", 0644, dentry,
  25912. + (void *)PREEMPTOFF_LATENCY, &latency_hist_reset_fops);
  25913. +#endif
  25914. +
  25915. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  25916. + dentry = debugfs_create_dir(preemptirqsoff_hist_dir,
  25917. + latency_hist_root);
  25918. + for_each_possible_cpu(i) {
  25919. + sprintf(name, cpufmt, i);
  25920. + entry = debugfs_create_file(name, 0444, dentry,
  25921. + &per_cpu(preemptirqsoff_hist, i), &latency_hist_fops);
  25922. + my_hist = &per_cpu(preemptirqsoff_hist, i);
  25923. + atomic_set(&my_hist->hist_mode, 1);
  25924. + my_hist->min_lat = LONG_MAX;
  25925. + }
  25926. + entry = debugfs_create_file("reset", 0644, dentry,
  25927. + (void *)PREEMPTIRQSOFF_LATENCY, &latency_hist_reset_fops);
  25928. +#endif
  25929. +
  25930. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  25931. + entry = debugfs_create_file("preemptirqsoff", 0644,
  25932. + enable_root, (void *)&preemptirqsoff_enabled_data,
  25933. + &enable_fops);
  25934. +#endif
  25935. +
  25936. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  25937. + dentry = debugfs_create_dir(wakeup_latency_hist_dir,
  25938. + latency_hist_root);
  25939. + dentry_sharedprio = debugfs_create_dir(
  25940. + wakeup_latency_hist_dir_sharedprio, dentry);
  25941. + for_each_possible_cpu(i) {
  25942. + sprintf(name, cpufmt, i);
  25943. +
  25944. + entry = debugfs_create_file(name, 0444, dentry,
  25945. + &per_cpu(wakeup_latency_hist, i),
  25946. + &latency_hist_fops);
  25947. + my_hist = &per_cpu(wakeup_latency_hist, i);
  25948. + atomic_set(&my_hist->hist_mode, 1);
  25949. + my_hist->min_lat = LONG_MAX;
  25950. +
  25951. + entry = debugfs_create_file(name, 0444, dentry_sharedprio,
  25952. + &per_cpu(wakeup_latency_hist_sharedprio, i),
  25953. + &latency_hist_fops);
  25954. + my_hist = &per_cpu(wakeup_latency_hist_sharedprio, i);
  25955. + atomic_set(&my_hist->hist_mode, 1);
  25956. + my_hist->min_lat = LONG_MAX;
  25957. +
  25958. + sprintf(name, cpufmt_maxlatproc, i);
  25959. +
  25960. + mp = &per_cpu(wakeup_maxlatproc, i);
  25961. + entry = debugfs_create_file(name, 0444, dentry, mp,
  25962. + &maxlatproc_fops);
  25963. + clear_maxlatprocdata(mp);
  25964. +
  25965. + mp = &per_cpu(wakeup_maxlatproc_sharedprio, i);
  25966. + entry = debugfs_create_file(name, 0444, dentry_sharedprio, mp,
  25967. + &maxlatproc_fops);
  25968. + clear_maxlatprocdata(mp);
  25969. + }
  25970. + entry = debugfs_create_file("pid", 0644, dentry,
  25971. + (void *)&wakeup_pid, &pid_fops);
  25972. + entry = debugfs_create_file("reset", 0644, dentry,
  25973. + (void *)WAKEUP_LATENCY, &latency_hist_reset_fops);
  25974. + entry = debugfs_create_file("reset", 0644, dentry_sharedprio,
  25975. + (void *)WAKEUP_LATENCY_SHAREDPRIO, &latency_hist_reset_fops);
  25976. + entry = debugfs_create_file("wakeup", 0644,
  25977. + enable_root, (void *)&wakeup_latency_enabled_data,
  25978. + &enable_fops);
  25979. +#endif
  25980. +
  25981. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  25982. + dentry = debugfs_create_dir(missed_timer_offsets_dir,
  25983. + latency_hist_root);
  25984. + for_each_possible_cpu(i) {
  25985. + sprintf(name, cpufmt, i);
  25986. + entry = debugfs_create_file(name, 0444, dentry,
  25987. + &per_cpu(missed_timer_offsets, i), &latency_hist_fops);
  25988. + my_hist = &per_cpu(missed_timer_offsets, i);
  25989. + atomic_set(&my_hist->hist_mode, 1);
  25990. + my_hist->min_lat = LONG_MAX;
  25991. +
  25992. + sprintf(name, cpufmt_maxlatproc, i);
  25993. + mp = &per_cpu(missed_timer_offsets_maxlatproc, i);
  25994. + entry = debugfs_create_file(name, 0444, dentry, mp,
  25995. + &maxlatproc_fops);
  25996. + clear_maxlatprocdata(mp);
  25997. + }
  25998. + entry = debugfs_create_file("pid", 0644, dentry,
  25999. + (void *)&missed_timer_offsets_pid, &pid_fops);
  26000. + entry = debugfs_create_file("reset", 0644, dentry,
  26001. + (void *)MISSED_TIMER_OFFSETS, &latency_hist_reset_fops);
  26002. + entry = debugfs_create_file("missed_timer_offsets", 0644,
  26003. + enable_root, (void *)&missed_timer_offsets_enabled_data,
  26004. + &enable_fops);
  26005. +#endif
  26006. +
  26007. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  26008. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  26009. + dentry = debugfs_create_dir(timerandwakeup_latency_hist_dir,
  26010. + latency_hist_root);
  26011. + for_each_possible_cpu(i) {
  26012. + sprintf(name, cpufmt, i);
  26013. + entry = debugfs_create_file(name, 0444, dentry,
  26014. + &per_cpu(timerandwakeup_latency_hist, i),
  26015. + &latency_hist_fops);
  26016. + my_hist = &per_cpu(timerandwakeup_latency_hist, i);
  26017. + atomic_set(&my_hist->hist_mode, 1);
  26018. + my_hist->min_lat = LONG_MAX;
  26019. +
  26020. + sprintf(name, cpufmt_maxlatproc, i);
  26021. + mp = &per_cpu(timerandwakeup_maxlatproc, i);
  26022. + entry = debugfs_create_file(name, 0444, dentry, mp,
  26023. + &maxlatproc_fops);
  26024. + clear_maxlatprocdata(mp);
  26025. + }
  26026. + entry = debugfs_create_file("reset", 0644, dentry,
  26027. + (void *)TIMERANDWAKEUP_LATENCY, &latency_hist_reset_fops);
  26028. + entry = debugfs_create_file("timerandwakeup", 0644,
  26029. + enable_root, (void *)&timerandwakeup_enabled_data,
  26030. + &enable_fops);
  26031. +#endif
  26032. + return 0;
  26033. +}
  26034. +
  26035. +device_initcall(latency_hist_init);
  26036. diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
  26037. index 4c21c0b7dc91..d0e52efed27a 100644
  26038. --- a/kernel/trace/trace.c
  26039. +++ b/kernel/trace/trace.c
  26040. @@ -1652,6 +1652,7 @@ tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
  26041. struct task_struct *tsk = current;
  26042. entry->preempt_count = pc & 0xff;
  26043. + entry->preempt_lazy_count = preempt_lazy_count();
  26044. entry->pid = (tsk) ? tsk->pid : 0;
  26045. entry->flags =
  26046. #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
  26047. @@ -1661,8 +1662,11 @@ tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
  26048. #endif
  26049. ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
  26050. ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
  26051. - (tif_need_resched() ? TRACE_FLAG_NEED_RESCHED : 0) |
  26052. + (tif_need_resched_now() ? TRACE_FLAG_NEED_RESCHED : 0) |
  26053. + (need_resched_lazy() ? TRACE_FLAG_NEED_RESCHED_LAZY : 0) |
  26054. (test_preempt_need_resched() ? TRACE_FLAG_PREEMPT_RESCHED : 0);
  26055. +
  26056. + entry->migrate_disable = (tsk) ? __migrate_disabled(tsk) & 0xFF : 0;
  26057. }
  26058. EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
  26059. @@ -2555,14 +2559,17 @@ get_total_entries(struct trace_buffer *buf,
  26060. static void print_lat_help_header(struct seq_file *m)
  26061. {
  26062. - seq_puts(m, "# _------=> CPU# \n"
  26063. - "# / _-----=> irqs-off \n"
  26064. - "# | / _----=> need-resched \n"
  26065. - "# || / _---=> hardirq/softirq \n"
  26066. - "# ||| / _--=> preempt-depth \n"
  26067. - "# |||| / delay \n"
  26068. - "# cmd pid ||||| time | caller \n"
  26069. - "# \\ / ||||| \\ | / \n");
  26070. + seq_puts(m, "# _--------=> CPU# \n"
  26071. + "# / _-------=> irqs-off \n"
  26072. + "# | / _------=> need-resched \n"
  26073. + "# || / _-----=> need-resched_lazy \n"
  26074. + "# ||| / _----=> hardirq/softirq \n"
  26075. + "# |||| / _---=> preempt-depth \n"
  26076. + "# ||||| / _--=> preempt-lazy-depth\n"
  26077. + "# |||||| / _-=> migrate-disable \n"
  26078. + "# ||||||| / delay \n"
  26079. + "# cmd pid |||||||| time | caller \n"
  26080. + "# \\ / |||||||| \\ | / \n");
  26081. }
  26082. static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
  26083. @@ -2588,11 +2595,14 @@ static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file
  26084. print_event_info(buf, m);
  26085. seq_puts(m, "# _-----=> irqs-off\n"
  26086. "# / _----=> need-resched\n"
  26087. - "# | / _---=> hardirq/softirq\n"
  26088. - "# || / _--=> preempt-depth\n"
  26089. - "# ||| / delay\n"
  26090. - "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n"
  26091. - "# | | | |||| | |\n");
  26092. + "# |/ _-----=> need-resched_lazy\n"
  26093. + "# || / _---=> hardirq/softirq\n"
  26094. + "# ||| / _--=> preempt-depth\n"
  26095. + "# |||| / _-=> preempt-lazy-depth\n"
  26096. + "# ||||| / _-=> migrate-disable \n"
  26097. + "# |||||| / delay\n"
  26098. + "# TASK-PID CPU# ||||||| TIMESTAMP FUNCTION\n"
  26099. + "# | | | ||||||| | |\n");
  26100. }
  26101. void
  26102. diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h
  26103. index 919d9d07686f..3bf86ece683c 100644
  26104. --- a/kernel/trace/trace.h
  26105. +++ b/kernel/trace/trace.h
  26106. @@ -117,6 +117,7 @@ struct kretprobe_trace_entry_head {
  26107. * NEED_RESCHED - reschedule is requested
  26108. * HARDIRQ - inside an interrupt handler
  26109. * SOFTIRQ - inside a softirq handler
  26110. + * NEED_RESCHED_LAZY - lazy reschedule is requested
  26111. */
  26112. enum trace_flag_type {
  26113. TRACE_FLAG_IRQS_OFF = 0x01,
  26114. @@ -125,6 +126,7 @@ enum trace_flag_type {
  26115. TRACE_FLAG_HARDIRQ = 0x08,
  26116. TRACE_FLAG_SOFTIRQ = 0x10,
  26117. TRACE_FLAG_PREEMPT_RESCHED = 0x20,
  26118. + TRACE_FLAG_NEED_RESCHED_LAZY = 0x40,
  26119. };
  26120. #define TRACE_BUF_SIZE 1024
  26121. diff --git a/kernel/trace/trace_events.c b/kernel/trace/trace_events.c
  26122. index 996f0fd34312..5bd79b347398 100644
  26123. --- a/kernel/trace/trace_events.c
  26124. +++ b/kernel/trace/trace_events.c
  26125. @@ -188,6 +188,8 @@ static int trace_define_common_fields(void)
  26126. __common_field(unsigned char, flags);
  26127. __common_field(unsigned char, preempt_count);
  26128. __common_field(int, pid);
  26129. + __common_field(unsigned short, migrate_disable);
  26130. + __common_field(unsigned short, padding);
  26131. return ret;
  26132. }
  26133. @@ -244,6 +246,14 @@ void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
  26134. local_save_flags(fbuffer->flags);
  26135. fbuffer->pc = preempt_count();
  26136. + /*
  26137. + * If CONFIG_PREEMPT is enabled, then the tracepoint itself disables
  26138. + * preemption (adding one to the preempt_count). Since we are
  26139. + * interested in the preempt_count at the time the tracepoint was
  26140. + * hit, we need to subtract one to offset the increment.
  26141. + */
  26142. + if (IS_ENABLED(CONFIG_PREEMPT))
  26143. + fbuffer->pc--;
  26144. fbuffer->trace_file = trace_file;
  26145. fbuffer->event =
  26146. diff --git a/kernel/trace/trace_irqsoff.c b/kernel/trace/trace_irqsoff.c
  26147. index be3222b7d72e..553e71254ad6 100644
  26148. --- a/kernel/trace/trace_irqsoff.c
  26149. +++ b/kernel/trace/trace_irqsoff.c
  26150. @@ -13,6 +13,7 @@
  26151. #include <linux/uaccess.h>
  26152. #include <linux/module.h>
  26153. #include <linux/ftrace.h>
  26154. +#include <trace/events/hist.h>
  26155. #include "trace.h"
  26156. @@ -424,11 +425,13 @@ void start_critical_timings(void)
  26157. {
  26158. if (preempt_trace() || irq_trace())
  26159. start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  26160. + trace_preemptirqsoff_hist_rcuidle(TRACE_START, 1);
  26161. }
  26162. EXPORT_SYMBOL_GPL(start_critical_timings);
  26163. void stop_critical_timings(void)
  26164. {
  26165. + trace_preemptirqsoff_hist_rcuidle(TRACE_STOP, 0);
  26166. if (preempt_trace() || irq_trace())
  26167. stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  26168. }
  26169. @@ -438,6 +441,7 @@ EXPORT_SYMBOL_GPL(stop_critical_timings);
  26170. #ifdef CONFIG_PROVE_LOCKING
  26171. void time_hardirqs_on(unsigned long a0, unsigned long a1)
  26172. {
  26173. + trace_preemptirqsoff_hist_rcuidle(IRQS_ON, 0);
  26174. if (!preempt_trace() && irq_trace())
  26175. stop_critical_timing(a0, a1);
  26176. }
  26177. @@ -446,6 +450,7 @@ void time_hardirqs_off(unsigned long a0, unsigned long a1)
  26178. {
  26179. if (!preempt_trace() && irq_trace())
  26180. start_critical_timing(a0, a1);
  26181. + trace_preemptirqsoff_hist_rcuidle(IRQS_OFF, 1);
  26182. }
  26183. #else /* !CONFIG_PROVE_LOCKING */
  26184. @@ -471,6 +476,7 @@ inline void print_irqtrace_events(struct task_struct *curr)
  26185. */
  26186. void trace_hardirqs_on(void)
  26187. {
  26188. + trace_preemptirqsoff_hist(IRQS_ON, 0);
  26189. if (!preempt_trace() && irq_trace())
  26190. stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  26191. }
  26192. @@ -480,11 +486,13 @@ void trace_hardirqs_off(void)
  26193. {
  26194. if (!preempt_trace() && irq_trace())
  26195. start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  26196. + trace_preemptirqsoff_hist(IRQS_OFF, 1);
  26197. }
  26198. EXPORT_SYMBOL(trace_hardirqs_off);
  26199. __visible void trace_hardirqs_on_caller(unsigned long caller_addr)
  26200. {
  26201. + trace_preemptirqsoff_hist(IRQS_ON, 0);
  26202. if (!preempt_trace() && irq_trace())
  26203. stop_critical_timing(CALLER_ADDR0, caller_addr);
  26204. }
  26205. @@ -494,6 +502,7 @@ __visible void trace_hardirqs_off_caller(unsigned long caller_addr)
  26206. {
  26207. if (!preempt_trace() && irq_trace())
  26208. start_critical_timing(CALLER_ADDR0, caller_addr);
  26209. + trace_preemptirqsoff_hist(IRQS_OFF, 1);
  26210. }
  26211. EXPORT_SYMBOL(trace_hardirqs_off_caller);
  26212. @@ -503,12 +512,14 @@ EXPORT_SYMBOL(trace_hardirqs_off_caller);
  26213. #ifdef CONFIG_PREEMPT_TRACER
  26214. void trace_preempt_on(unsigned long a0, unsigned long a1)
  26215. {
  26216. + trace_preemptirqsoff_hist(PREEMPT_ON, 0);
  26217. if (preempt_trace() && !irq_trace())
  26218. stop_critical_timing(a0, a1);
  26219. }
  26220. void trace_preempt_off(unsigned long a0, unsigned long a1)
  26221. {
  26222. + trace_preemptirqsoff_hist(PREEMPT_ON, 1);
  26223. if (preempt_trace() && !irq_trace())
  26224. start_critical_timing(a0, a1);
  26225. }
  26226. diff --git a/kernel/trace/trace_output.c b/kernel/trace/trace_output.c
  26227. index 282982195e09..9f19d839a756 100644
  26228. --- a/kernel/trace/trace_output.c
  26229. +++ b/kernel/trace/trace_output.c
  26230. @@ -386,6 +386,7 @@ int trace_print_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
  26231. {
  26232. char hardsoft_irq;
  26233. char need_resched;
  26234. + char need_resched_lazy;
  26235. char irqs_off;
  26236. int hardirq;
  26237. int softirq;
  26238. @@ -413,6 +414,8 @@ int trace_print_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
  26239. need_resched = '.';
  26240. break;
  26241. }
  26242. + need_resched_lazy =
  26243. + (entry->flags & TRACE_FLAG_NEED_RESCHED_LAZY) ? 'L' : '.';
  26244. hardsoft_irq =
  26245. (hardirq && softirq) ? 'H' :
  26246. @@ -420,14 +423,25 @@ int trace_print_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
  26247. softirq ? 's' :
  26248. '.';
  26249. - trace_seq_printf(s, "%c%c%c",
  26250. - irqs_off, need_resched, hardsoft_irq);
  26251. + trace_seq_printf(s, "%c%c%c%c",
  26252. + irqs_off, need_resched, need_resched_lazy,
  26253. + hardsoft_irq);
  26254. if (entry->preempt_count)
  26255. trace_seq_printf(s, "%x", entry->preempt_count);
  26256. else
  26257. trace_seq_putc(s, '.');
  26258. + if (entry->preempt_lazy_count)
  26259. + trace_seq_printf(s, "%x", entry->preempt_lazy_count);
  26260. + else
  26261. + trace_seq_putc(s, '.');
  26262. +
  26263. + if (entry->migrate_disable)
  26264. + trace_seq_printf(s, "%x", entry->migrate_disable);
  26265. + else
  26266. + trace_seq_putc(s, '.');
  26267. +
  26268. return !trace_seq_has_overflowed(s);
  26269. }
  26270. diff --git a/kernel/user.c b/kernel/user.c
  26271. index b069ccbfb0b0..1a2e88e98b5e 100644
  26272. --- a/kernel/user.c
  26273. +++ b/kernel/user.c
  26274. @@ -161,11 +161,11 @@ void free_uid(struct user_struct *up)
  26275. if (!up)
  26276. return;
  26277. - local_irq_save(flags);
  26278. + local_irq_save_nort(flags);
  26279. if (atomic_dec_and_lock(&up->__count, &uidhash_lock))
  26280. free_user(up, flags);
  26281. else
  26282. - local_irq_restore(flags);
  26283. + local_irq_restore_nort(flags);
  26284. }
  26285. struct user_struct *alloc_uid(kuid_t uid)
  26286. diff --git a/kernel/watchdog.c b/kernel/watchdog.c
  26287. index c1e0b5f429b6..fa2e079cc314 100644
  26288. --- a/kernel/watchdog.c
  26289. +++ b/kernel/watchdog.c
  26290. @@ -299,6 +299,8 @@ static int is_softlockup(unsigned long touch_ts)
  26291. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  26292. +static DEFINE_RAW_SPINLOCK(watchdog_output_lock);
  26293. +
  26294. static struct perf_event_attr wd_hw_attr = {
  26295. .type = PERF_TYPE_HARDWARE,
  26296. .config = PERF_COUNT_HW_CPU_CYCLES,
  26297. @@ -332,6 +334,13 @@ static void watchdog_overflow_callback(struct perf_event *event,
  26298. /* only print hardlockups once */
  26299. if (__this_cpu_read(hard_watchdog_warn) == true)
  26300. return;
  26301. + /*
  26302. + * If early-printk is enabled then make sure we do not
  26303. + * lock up in printk() and kill console logging:
  26304. + */
  26305. + printk_kill();
  26306. +
  26307. + raw_spin_lock(&watchdog_output_lock);
  26308. pr_emerg("Watchdog detected hard LOCKUP on cpu %d", this_cpu);
  26309. print_modules();
  26310. @@ -349,8 +358,9 @@ static void watchdog_overflow_callback(struct perf_event *event,
  26311. !test_and_set_bit(0, &hardlockup_allcpu_dumped))
  26312. trigger_allbutself_cpu_backtrace();
  26313. + raw_spin_unlock(&watchdog_output_lock);
  26314. if (hardlockup_panic)
  26315. - panic("Hard LOCKUP");
  26316. + nmi_panic(regs, "Hard LOCKUP");
  26317. __this_cpu_write(hard_watchdog_warn, true);
  26318. return;
  26319. @@ -496,6 +506,7 @@ static void watchdog_enable(unsigned int cpu)
  26320. /* kick off the timer for the hardlockup detector */
  26321. hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  26322. hrtimer->function = watchdog_timer_fn;
  26323. + hrtimer->irqsafe = 1;
  26324. /* Enable the perf event */
  26325. watchdog_nmi_enable(cpu);
  26326. diff --git a/kernel/workqueue.c b/kernel/workqueue.c
  26327. index 2c2f971f3e75..d5b0f4fc0eb0 100644
  26328. --- a/kernel/workqueue.c
  26329. +++ b/kernel/workqueue.c
  26330. @@ -48,6 +48,8 @@
  26331. #include <linux/nodemask.h>
  26332. #include <linux/moduleparam.h>
  26333. #include <linux/uaccess.h>
  26334. +#include <linux/locallock.h>
  26335. +#include <linux/delay.h>
  26336. #include "workqueue_internal.h"
  26337. @@ -121,11 +123,16 @@ enum {
  26338. * cpu or grabbing pool->lock is enough for read access. If
  26339. * POOL_DISASSOCIATED is set, it's identical to L.
  26340. *
  26341. + * On RT we need the extra protection via rt_lock_idle_list() for
  26342. + * the list manipulations against read access from
  26343. + * wq_worker_sleeping(). All other places are nicely serialized via
  26344. + * pool->lock.
  26345. + *
  26346. * A: pool->attach_mutex protected.
  26347. *
  26348. * PL: wq_pool_mutex protected.
  26349. *
  26350. - * PR: wq_pool_mutex protected for writes. Sched-RCU protected for reads.
  26351. + * PR: wq_pool_mutex protected for writes. RCU protected for reads.
  26352. *
  26353. * PW: wq_pool_mutex and wq->mutex protected for writes. Either for reads.
  26354. *
  26355. @@ -134,7 +141,7 @@ enum {
  26356. *
  26357. * WQ: wq->mutex protected.
  26358. *
  26359. - * WR: wq->mutex protected for writes. Sched-RCU protected for reads.
  26360. + * WR: wq->mutex protected for writes. RCU protected for reads.
  26361. *
  26362. * MD: wq_mayday_lock protected.
  26363. */
  26364. @@ -183,7 +190,7 @@ struct worker_pool {
  26365. atomic_t nr_running ____cacheline_aligned_in_smp;
  26366. /*
  26367. - * Destruction of pool is sched-RCU protected to allow dereferences
  26368. + * Destruction of pool is RCU protected to allow dereferences
  26369. * from get_work_pool().
  26370. */
  26371. struct rcu_head rcu;
  26372. @@ -212,7 +219,7 @@ struct pool_workqueue {
  26373. /*
  26374. * Release of unbound pwq is punted to system_wq. See put_pwq()
  26375. * and pwq_unbound_release_workfn() for details. pool_workqueue
  26376. - * itself is also sched-RCU protected so that the first pwq can be
  26377. + * itself is also RCU protected so that the first pwq can be
  26378. * determined without grabbing wq->mutex.
  26379. */
  26380. struct work_struct unbound_release_work;
  26381. @@ -331,6 +338,8 @@ EXPORT_SYMBOL_GPL(system_power_efficient_wq);
  26382. struct workqueue_struct *system_freezable_power_efficient_wq __read_mostly;
  26383. EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq);
  26384. +static DEFINE_LOCAL_IRQ_LOCK(pendingb_lock);
  26385. +
  26386. static int worker_thread(void *__worker);
  26387. static void workqueue_sysfs_unregister(struct workqueue_struct *wq);
  26388. @@ -338,20 +347,20 @@ static void workqueue_sysfs_unregister(struct workqueue_struct *wq);
  26389. #include <trace/events/workqueue.h>
  26390. #define assert_rcu_or_pool_mutex() \
  26391. - RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held() && \
  26392. + RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
  26393. !lockdep_is_held(&wq_pool_mutex), \
  26394. - "sched RCU or wq_pool_mutex should be held")
  26395. + "RCU or wq_pool_mutex should be held")
  26396. #define assert_rcu_or_wq_mutex(wq) \
  26397. - RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held() && \
  26398. + RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
  26399. !lockdep_is_held(&wq->mutex), \
  26400. - "sched RCU or wq->mutex should be held")
  26401. + "RCU or wq->mutex should be held")
  26402. #define assert_rcu_or_wq_mutex_or_pool_mutex(wq) \
  26403. - RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held() && \
  26404. + RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
  26405. !lockdep_is_held(&wq->mutex) && \
  26406. !lockdep_is_held(&wq_pool_mutex), \
  26407. - "sched RCU, wq->mutex or wq_pool_mutex should be held")
  26408. + "RCU, wq->mutex or wq_pool_mutex should be held")
  26409. #define for_each_cpu_worker_pool(pool, cpu) \
  26410. for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \
  26411. @@ -363,7 +372,7 @@ static void workqueue_sysfs_unregister(struct workqueue_struct *wq);
  26412. * @pool: iteration cursor
  26413. * @pi: integer used for iteration
  26414. *
  26415. - * This must be called either with wq_pool_mutex held or sched RCU read
  26416. + * This must be called either with wq_pool_mutex held or RCU read
  26417. * locked. If the pool needs to be used beyond the locking in effect, the
  26418. * caller is responsible for guaranteeing that the pool stays online.
  26419. *
  26420. @@ -395,7 +404,7 @@ static void workqueue_sysfs_unregister(struct workqueue_struct *wq);
  26421. * @pwq: iteration cursor
  26422. * @wq: the target workqueue
  26423. *
  26424. - * This must be called either with wq->mutex held or sched RCU read locked.
  26425. + * This must be called either with wq->mutex held or RCU read locked.
  26426. * If the pwq needs to be used beyond the locking in effect, the caller is
  26427. * responsible for guaranteeing that the pwq stays online.
  26428. *
  26429. @@ -407,6 +416,31 @@ static void workqueue_sysfs_unregister(struct workqueue_struct *wq);
  26430. if (({ assert_rcu_or_wq_mutex(wq); false; })) { } \
  26431. else
  26432. +#ifdef CONFIG_PREEMPT_RT_BASE
  26433. +static inline void rt_lock_idle_list(struct worker_pool *pool)
  26434. +{
  26435. + preempt_disable();
  26436. +}
  26437. +static inline void rt_unlock_idle_list(struct worker_pool *pool)
  26438. +{
  26439. + preempt_enable();
  26440. +}
  26441. +static inline void sched_lock_idle_list(struct worker_pool *pool) { }
  26442. +static inline void sched_unlock_idle_list(struct worker_pool *pool) { }
  26443. +#else
  26444. +static inline void rt_lock_idle_list(struct worker_pool *pool) { }
  26445. +static inline void rt_unlock_idle_list(struct worker_pool *pool) { }
  26446. +static inline void sched_lock_idle_list(struct worker_pool *pool)
  26447. +{
  26448. + spin_lock_irq(&pool->lock);
  26449. +}
  26450. +static inline void sched_unlock_idle_list(struct worker_pool *pool)
  26451. +{
  26452. + spin_unlock_irq(&pool->lock);
  26453. +}
  26454. +#endif
  26455. +
  26456. +
  26457. #ifdef CONFIG_DEBUG_OBJECTS_WORK
  26458. static struct debug_obj_descr work_debug_descr;
  26459. @@ -557,7 +591,7 @@ static int worker_pool_assign_id(struct worker_pool *pool)
  26460. * @wq: the target workqueue
  26461. * @node: the node ID
  26462. *
  26463. - * This must be called with any of wq_pool_mutex, wq->mutex or sched RCU
  26464. + * This must be called with any of wq_pool_mutex, wq->mutex or RCU
  26465. * read locked.
  26466. * If the pwq needs to be used beyond the locking in effect, the caller is
  26467. * responsible for guaranteeing that the pwq stays online.
  26468. @@ -701,8 +735,8 @@ static struct pool_workqueue *get_work_pwq(struct work_struct *work)
  26469. * @work: the work item of interest
  26470. *
  26471. * Pools are created and destroyed under wq_pool_mutex, and allows read
  26472. - * access under sched-RCU read lock. As such, this function should be
  26473. - * called under wq_pool_mutex or with preemption disabled.
  26474. + * access under RCU read lock. As such, this function should be
  26475. + * called under wq_pool_mutex or inside of a rcu_read_lock() region.
  26476. *
  26477. * All fields of the returned pool are accessible as long as the above
  26478. * mentioned locking is in effect. If the returned pool needs to be used
  26479. @@ -839,51 +873,44 @@ static struct worker *first_idle_worker(struct worker_pool *pool)
  26480. */
  26481. static void wake_up_worker(struct worker_pool *pool)
  26482. {
  26483. - struct worker *worker = first_idle_worker(pool);
  26484. + struct worker *worker;
  26485. +
  26486. + rt_lock_idle_list(pool);
  26487. +
  26488. + worker = first_idle_worker(pool);
  26489. if (likely(worker))
  26490. wake_up_process(worker->task);
  26491. +
  26492. + rt_unlock_idle_list(pool);
  26493. }
  26494. /**
  26495. - * wq_worker_waking_up - a worker is waking up
  26496. - * @task: task waking up
  26497. - * @cpu: CPU @task is waking up to
  26498. + * wq_worker_running - a worker is running again
  26499. + * @task: task returning from sleep
  26500. *
  26501. - * This function is called during try_to_wake_up() when a worker is
  26502. - * being awoken.
  26503. - *
  26504. - * CONTEXT:
  26505. - * spin_lock_irq(rq->lock)
  26506. + * This function is called when a worker returns from schedule()
  26507. */
  26508. -void wq_worker_waking_up(struct task_struct *task, int cpu)
  26509. +void wq_worker_running(struct task_struct *task)
  26510. {
  26511. struct worker *worker = kthread_data(task);
  26512. - if (!(worker->flags & WORKER_NOT_RUNNING)) {
  26513. - WARN_ON_ONCE(worker->pool->cpu != cpu);
  26514. + if (!worker->sleeping)
  26515. + return;
  26516. + if (!(worker->flags & WORKER_NOT_RUNNING))
  26517. atomic_inc(&worker->pool->nr_running);
  26518. - }
  26519. + worker->sleeping = 0;
  26520. }
  26521. /**
  26522. * wq_worker_sleeping - a worker is going to sleep
  26523. * @task: task going to sleep
  26524. - * @cpu: CPU in question, must be the current CPU number
  26525. - *
  26526. - * This function is called during schedule() when a busy worker is
  26527. - * going to sleep. Worker on the same cpu can be woken up by
  26528. - * returning pointer to its task.
  26529. - *
  26530. - * CONTEXT:
  26531. - * spin_lock_irq(rq->lock)
  26532. - *
  26533. - * Return:
  26534. - * Worker task on @cpu to wake up, %NULL if none.
  26535. + * This function is called from schedule() when a busy worker is
  26536. + * going to sleep.
  26537. */
  26538. -struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
  26539. +void wq_worker_sleeping(struct task_struct *task)
  26540. {
  26541. - struct worker *worker = kthread_data(task), *to_wakeup = NULL;
  26542. + struct worker *worker = kthread_data(task);
  26543. struct worker_pool *pool;
  26544. /*
  26545. @@ -892,29 +919,26 @@ struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
  26546. * checking NOT_RUNNING.
  26547. */
  26548. if (worker->flags & WORKER_NOT_RUNNING)
  26549. - return NULL;
  26550. + return;
  26551. pool = worker->pool;
  26552. - /* this can only happen on the local cpu */
  26553. - if (WARN_ON_ONCE(cpu != raw_smp_processor_id() || pool->cpu != cpu))
  26554. - return NULL;
  26555. + if (WARN_ON_ONCE(worker->sleeping))
  26556. + return;
  26557. +
  26558. + worker->sleeping = 1;
  26559. /*
  26560. * The counterpart of the following dec_and_test, implied mb,
  26561. * worklist not empty test sequence is in insert_work().
  26562. * Please read comment there.
  26563. - *
  26564. - * NOT_RUNNING is clear. This means that we're bound to and
  26565. - * running on the local cpu w/ rq lock held and preemption
  26566. - * disabled, which in turn means that none else could be
  26567. - * manipulating idle_list, so dereferencing idle_list without pool
  26568. - * lock is safe.
  26569. */
  26570. if (atomic_dec_and_test(&pool->nr_running) &&
  26571. - !list_empty(&pool->worklist))
  26572. - to_wakeup = first_idle_worker(pool);
  26573. - return to_wakeup ? to_wakeup->task : NULL;
  26574. + !list_empty(&pool->worklist)) {
  26575. + sched_lock_idle_list(pool);
  26576. + wake_up_worker(pool);
  26577. + sched_unlock_idle_list(pool);
  26578. + }
  26579. }
  26580. /**
  26581. @@ -1108,12 +1132,14 @@ static void put_pwq_unlocked(struct pool_workqueue *pwq)
  26582. {
  26583. if (pwq) {
  26584. /*
  26585. - * As both pwqs and pools are sched-RCU protected, the
  26586. + * As both pwqs and pools are RCU protected, the
  26587. * following lock operations are safe.
  26588. */
  26589. - spin_lock_irq(&pwq->pool->lock);
  26590. + rcu_read_lock();
  26591. + local_spin_lock_irq(pendingb_lock, &pwq->pool->lock);
  26592. put_pwq(pwq);
  26593. - spin_unlock_irq(&pwq->pool->lock);
  26594. + local_spin_unlock_irq(pendingb_lock, &pwq->pool->lock);
  26595. + rcu_read_unlock();
  26596. }
  26597. }
  26598. @@ -1215,7 +1241,7 @@ static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
  26599. struct worker_pool *pool;
  26600. struct pool_workqueue *pwq;
  26601. - local_irq_save(*flags);
  26602. + local_lock_irqsave(pendingb_lock, *flags);
  26603. /* try to steal the timer if it exists */
  26604. if (is_dwork) {
  26605. @@ -1234,6 +1260,7 @@ static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
  26606. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
  26607. return 0;
  26608. + rcu_read_lock();
  26609. /*
  26610. * The queueing is in progress, or it is already queued. Try to
  26611. * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
  26612. @@ -1272,14 +1299,16 @@ static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
  26613. set_work_pool_and_keep_pending(work, pool->id);
  26614. spin_unlock(&pool->lock);
  26615. + rcu_read_unlock();
  26616. return 1;
  26617. }
  26618. spin_unlock(&pool->lock);
  26619. fail:
  26620. - local_irq_restore(*flags);
  26621. + rcu_read_unlock();
  26622. + local_unlock_irqrestore(pendingb_lock, *flags);
  26623. if (work_is_canceling(work))
  26624. return -ENOENT;
  26625. - cpu_relax();
  26626. + cpu_chill();
  26627. return -EAGAIN;
  26628. }
  26629. @@ -1348,7 +1377,7 @@ static void __queue_work(int cpu, struct workqueue_struct *wq,
  26630. * queued or lose PENDING. Grabbing PENDING and queueing should
  26631. * happen with IRQ disabled.
  26632. */
  26633. - WARN_ON_ONCE(!irqs_disabled());
  26634. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  26635. debug_work_activate(work);
  26636. @@ -1356,6 +1385,8 @@ static void __queue_work(int cpu, struct workqueue_struct *wq,
  26637. if (unlikely(wq->flags & __WQ_DRAINING) &&
  26638. WARN_ON_ONCE(!is_chained_work(wq)))
  26639. return;
  26640. +
  26641. + rcu_read_lock();
  26642. retry:
  26643. if (req_cpu == WORK_CPU_UNBOUND)
  26644. cpu = raw_smp_processor_id();
  26645. @@ -1412,10 +1443,8 @@ retry:
  26646. /* pwq determined, queue */
  26647. trace_workqueue_queue_work(req_cpu, pwq, work);
  26648. - if (WARN_ON(!list_empty(&work->entry))) {
  26649. - spin_unlock(&pwq->pool->lock);
  26650. - return;
  26651. - }
  26652. + if (WARN_ON(!list_empty(&work->entry)))
  26653. + goto out;
  26654. pwq->nr_in_flight[pwq->work_color]++;
  26655. work_flags = work_color_to_flags(pwq->work_color);
  26656. @@ -1431,7 +1460,9 @@ retry:
  26657. insert_work(pwq, work, worklist, work_flags);
  26658. +out:
  26659. spin_unlock(&pwq->pool->lock);
  26660. + rcu_read_unlock();
  26661. }
  26662. /**
  26663. @@ -1451,14 +1482,14 @@ bool queue_work_on(int cpu, struct workqueue_struct *wq,
  26664. bool ret = false;
  26665. unsigned long flags;
  26666. - local_irq_save(flags);
  26667. + local_lock_irqsave(pendingb_lock,flags);
  26668. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
  26669. __queue_work(cpu, wq, work);
  26670. ret = true;
  26671. }
  26672. - local_irq_restore(flags);
  26673. + local_unlock_irqrestore(pendingb_lock, flags);
  26674. return ret;
  26675. }
  26676. EXPORT_SYMBOL(queue_work_on);
  26677. @@ -1525,14 +1556,14 @@ bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
  26678. unsigned long flags;
  26679. /* read the comment in __queue_work() */
  26680. - local_irq_save(flags);
  26681. + local_lock_irqsave(pendingb_lock, flags);
  26682. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
  26683. __queue_delayed_work(cpu, wq, dwork, delay);
  26684. ret = true;
  26685. }
  26686. - local_irq_restore(flags);
  26687. + local_unlock_irqrestore(pendingb_lock, flags);
  26688. return ret;
  26689. }
  26690. EXPORT_SYMBOL(queue_delayed_work_on);
  26691. @@ -1567,7 +1598,7 @@ bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
  26692. if (likely(ret >= 0)) {
  26693. __queue_delayed_work(cpu, wq, dwork, delay);
  26694. - local_irq_restore(flags);
  26695. + local_unlock_irqrestore(pendingb_lock, flags);
  26696. }
  26697. /* -ENOENT from try_to_grab_pending() becomes %true */
  26698. @@ -1600,7 +1631,9 @@ static void worker_enter_idle(struct worker *worker)
  26699. worker->last_active = jiffies;
  26700. /* idle_list is LIFO */
  26701. + rt_lock_idle_list(pool);
  26702. list_add(&worker->entry, &pool->idle_list);
  26703. + rt_unlock_idle_list(pool);
  26704. if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
  26705. mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
  26706. @@ -1633,7 +1666,9 @@ static void worker_leave_idle(struct worker *worker)
  26707. return;
  26708. worker_clr_flags(worker, WORKER_IDLE);
  26709. pool->nr_idle--;
  26710. + rt_lock_idle_list(pool);
  26711. list_del_init(&worker->entry);
  26712. + rt_unlock_idle_list(pool);
  26713. }
  26714. static struct worker *alloc_worker(int node)
  26715. @@ -1799,7 +1834,9 @@ static void destroy_worker(struct worker *worker)
  26716. pool->nr_workers--;
  26717. pool->nr_idle--;
  26718. + rt_lock_idle_list(pool);
  26719. list_del_init(&worker->entry);
  26720. + rt_unlock_idle_list(pool);
  26721. worker->flags |= WORKER_DIE;
  26722. wake_up_process(worker->task);
  26723. }
  26724. @@ -2716,14 +2753,14 @@ static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
  26725. might_sleep();
  26726. - local_irq_disable();
  26727. + rcu_read_lock();
  26728. pool = get_work_pool(work);
  26729. if (!pool) {
  26730. - local_irq_enable();
  26731. + rcu_read_unlock();
  26732. return false;
  26733. }
  26734. - spin_lock(&pool->lock);
  26735. + spin_lock_irq(&pool->lock);
  26736. /* see the comment in try_to_grab_pending() with the same code */
  26737. pwq = get_work_pwq(work);
  26738. if (pwq) {
  26739. @@ -2750,10 +2787,11 @@ static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
  26740. else
  26741. lock_map_acquire_read(&pwq->wq->lockdep_map);
  26742. lock_map_release(&pwq->wq->lockdep_map);
  26743. -
  26744. + rcu_read_unlock();
  26745. return true;
  26746. already_gone:
  26747. spin_unlock_irq(&pool->lock);
  26748. + rcu_read_unlock();
  26749. return false;
  26750. }
  26751. @@ -2840,7 +2878,7 @@ static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
  26752. /* tell other tasks trying to grab @work to back off */
  26753. mark_work_canceling(work);
  26754. - local_irq_restore(flags);
  26755. + local_unlock_irqrestore(pendingb_lock, flags);
  26756. flush_work(work);
  26757. clear_work_data(work);
  26758. @@ -2895,10 +2933,10 @@ EXPORT_SYMBOL_GPL(cancel_work_sync);
  26759. */
  26760. bool flush_delayed_work(struct delayed_work *dwork)
  26761. {
  26762. - local_irq_disable();
  26763. + local_lock_irq(pendingb_lock);
  26764. if (del_timer_sync(&dwork->timer))
  26765. __queue_work(dwork->cpu, dwork->wq, &dwork->work);
  26766. - local_irq_enable();
  26767. + local_unlock_irq(pendingb_lock);
  26768. return flush_work(&dwork->work);
  26769. }
  26770. EXPORT_SYMBOL(flush_delayed_work);
  26771. @@ -2933,7 +2971,7 @@ bool cancel_delayed_work(struct delayed_work *dwork)
  26772. set_work_pool_and_clear_pending(&dwork->work,
  26773. get_work_pool_id(&dwork->work));
  26774. - local_irq_restore(flags);
  26775. + local_unlock_irqrestore(pendingb_lock, flags);
  26776. return ret;
  26777. }
  26778. EXPORT_SYMBOL(cancel_delayed_work);
  26779. @@ -3161,7 +3199,7 @@ static void rcu_free_pool(struct rcu_head *rcu)
  26780. * put_unbound_pool - put a worker_pool
  26781. * @pool: worker_pool to put
  26782. *
  26783. - * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU
  26784. + * Put @pool. If its refcnt reaches zero, it gets destroyed in RCU
  26785. * safe manner. get_unbound_pool() calls this function on its failure path
  26786. * and this function should be able to release pools which went through,
  26787. * successfully or not, init_worker_pool().
  26788. @@ -3215,8 +3253,8 @@ static void put_unbound_pool(struct worker_pool *pool)
  26789. del_timer_sync(&pool->idle_timer);
  26790. del_timer_sync(&pool->mayday_timer);
  26791. - /* sched-RCU protected to allow dereferences from get_work_pool() */
  26792. - call_rcu_sched(&pool->rcu, rcu_free_pool);
  26793. + /* RCU protected to allow dereferences from get_work_pool() */
  26794. + call_rcu(&pool->rcu, rcu_free_pool);
  26795. }
  26796. /**
  26797. @@ -3323,14 +3361,14 @@ static void pwq_unbound_release_workfn(struct work_struct *work)
  26798. put_unbound_pool(pool);
  26799. mutex_unlock(&wq_pool_mutex);
  26800. - call_rcu_sched(&pwq->rcu, rcu_free_pwq);
  26801. + call_rcu(&pwq->rcu, rcu_free_pwq);
  26802. /*
  26803. * If we're the last pwq going away, @wq is already dead and no one
  26804. * is gonna access it anymore. Schedule RCU free.
  26805. */
  26806. if (is_last)
  26807. - call_rcu_sched(&wq->rcu, rcu_free_wq);
  26808. + call_rcu(&wq->rcu, rcu_free_wq);
  26809. }
  26810. /**
  26811. @@ -3983,7 +4021,7 @@ void destroy_workqueue(struct workqueue_struct *wq)
  26812. * The base ref is never dropped on per-cpu pwqs. Directly
  26813. * schedule RCU free.
  26814. */
  26815. - call_rcu_sched(&wq->rcu, rcu_free_wq);
  26816. + call_rcu(&wq->rcu, rcu_free_wq);
  26817. } else {
  26818. /*
  26819. * We're the sole accessor of @wq at this point. Directly
  26820. @@ -4076,7 +4114,8 @@ bool workqueue_congested(int cpu, struct workqueue_struct *wq)
  26821. struct pool_workqueue *pwq;
  26822. bool ret;
  26823. - rcu_read_lock_sched();
  26824. + rcu_read_lock();
  26825. + preempt_disable();
  26826. if (cpu == WORK_CPU_UNBOUND)
  26827. cpu = smp_processor_id();
  26828. @@ -4087,7 +4126,8 @@ bool workqueue_congested(int cpu, struct workqueue_struct *wq)
  26829. pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
  26830. ret = !list_empty(&pwq->delayed_works);
  26831. - rcu_read_unlock_sched();
  26832. + preempt_enable();
  26833. + rcu_read_unlock();
  26834. return ret;
  26835. }
  26836. @@ -4113,15 +4153,15 @@ unsigned int work_busy(struct work_struct *work)
  26837. if (work_pending(work))
  26838. ret |= WORK_BUSY_PENDING;
  26839. - local_irq_save(flags);
  26840. + rcu_read_lock();
  26841. pool = get_work_pool(work);
  26842. if (pool) {
  26843. - spin_lock(&pool->lock);
  26844. + spin_lock_irqsave(&pool->lock, flags);
  26845. if (find_worker_executing_work(pool, work))
  26846. ret |= WORK_BUSY_RUNNING;
  26847. - spin_unlock(&pool->lock);
  26848. + spin_unlock_irqrestore(&pool->lock, flags);
  26849. }
  26850. - local_irq_restore(flags);
  26851. + rcu_read_unlock();
  26852. return ret;
  26853. }
  26854. @@ -4310,7 +4350,7 @@ void show_workqueue_state(void)
  26855. unsigned long flags;
  26856. int pi;
  26857. - rcu_read_lock_sched();
  26858. + rcu_read_lock();
  26859. pr_info("Showing busy workqueues and worker pools:\n");
  26860. @@ -4361,7 +4401,7 @@ void show_workqueue_state(void)
  26861. spin_unlock_irqrestore(&pool->lock, flags);
  26862. }
  26863. - rcu_read_unlock_sched();
  26864. + rcu_read_unlock();
  26865. }
  26866. /*
  26867. @@ -4722,16 +4762,16 @@ bool freeze_workqueues_busy(void)
  26868. * nr_active is monotonically decreasing. It's safe
  26869. * to peek without lock.
  26870. */
  26871. - rcu_read_lock_sched();
  26872. + rcu_read_lock();
  26873. for_each_pwq(pwq, wq) {
  26874. WARN_ON_ONCE(pwq->nr_active < 0);
  26875. if (pwq->nr_active) {
  26876. busy = true;
  26877. - rcu_read_unlock_sched();
  26878. + rcu_read_unlock();
  26879. goto out_unlock;
  26880. }
  26881. }
  26882. - rcu_read_unlock_sched();
  26883. + rcu_read_unlock();
  26884. }
  26885. out_unlock:
  26886. mutex_unlock(&wq_pool_mutex);
  26887. @@ -4921,7 +4961,8 @@ static ssize_t wq_pool_ids_show(struct device *dev,
  26888. const char *delim = "";
  26889. int node, written = 0;
  26890. - rcu_read_lock_sched();
  26891. + get_online_cpus();
  26892. + rcu_read_lock();
  26893. for_each_node(node) {
  26894. written += scnprintf(buf + written, PAGE_SIZE - written,
  26895. "%s%d:%d", delim, node,
  26896. @@ -4929,7 +4970,8 @@ static ssize_t wq_pool_ids_show(struct device *dev,
  26897. delim = " ";
  26898. }
  26899. written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
  26900. - rcu_read_unlock_sched();
  26901. + rcu_read_unlock();
  26902. + put_online_cpus();
  26903. return written;
  26904. }
  26905. diff --git a/kernel/workqueue_internal.h b/kernel/workqueue_internal.h
  26906. index 45215870ac6c..f000c4d6917e 100644
  26907. --- a/kernel/workqueue_internal.h
  26908. +++ b/kernel/workqueue_internal.h
  26909. @@ -43,6 +43,7 @@ struct worker {
  26910. unsigned long last_active; /* L: last active timestamp */
  26911. unsigned int flags; /* X: flags */
  26912. int id; /* I: worker id */
  26913. + int sleeping; /* None */
  26914. /*
  26915. * Opaque string set with work_set_desc(). Printed out with task
  26916. @@ -68,7 +69,7 @@ static inline struct worker *current_wq_worker(void)
  26917. * Scheduler hooks for concurrency managed workqueue. Only to be used from
  26918. * sched/core.c and workqueue.c.
  26919. */
  26920. -void wq_worker_waking_up(struct task_struct *task, int cpu);
  26921. -struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu);
  26922. +void wq_worker_running(struct task_struct *task);
  26923. +void wq_worker_sleeping(struct task_struct *task);
  26924. #endif /* _KERNEL_WORKQUEUE_INTERNAL_H */
  26925. diff --git a/lib/Kconfig b/lib/Kconfig
  26926. index 1a48744253d7..f75de578cca8 100644
  26927. --- a/lib/Kconfig
  26928. +++ b/lib/Kconfig
  26929. @@ -397,6 +397,7 @@ config CHECK_SIGNATURE
  26930. config CPUMASK_OFFSTACK
  26931. bool "Force CPU masks off stack" if DEBUG_PER_CPU_MAPS
  26932. + depends on !PREEMPT_RT_FULL
  26933. help
  26934. Use dynamic allocation for cpumask_var_t, instead of putting
  26935. them on the stack. This is a bit more expensive, but avoids
  26936. diff --git a/lib/debugobjects.c b/lib/debugobjects.c
  26937. index 547f7f923dbc..8fcdbc2fc6d0 100644
  26938. --- a/lib/debugobjects.c
  26939. +++ b/lib/debugobjects.c
  26940. @@ -309,7 +309,10 @@ __debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack)
  26941. struct debug_obj *obj;
  26942. unsigned long flags;
  26943. - fill_pool();
  26944. +#ifdef CONFIG_PREEMPT_RT_FULL
  26945. + if (preempt_count() == 0 && !irqs_disabled())
  26946. +#endif
  26947. + fill_pool();
  26948. db = get_bucket((unsigned long) addr);
  26949. diff --git a/lib/idr.c b/lib/idr.c
  26950. index 6098336df267..9decbe914595 100644
  26951. --- a/lib/idr.c
  26952. +++ b/lib/idr.c
  26953. @@ -30,6 +30,7 @@
  26954. #include <linux/idr.h>
  26955. #include <linux/spinlock.h>
  26956. #include <linux/percpu.h>
  26957. +#include <linux/locallock.h>
  26958. #define MAX_IDR_SHIFT (sizeof(int) * 8 - 1)
  26959. #define MAX_IDR_BIT (1U << MAX_IDR_SHIFT)
  26960. @@ -45,6 +46,37 @@ static DEFINE_PER_CPU(struct idr_layer *, idr_preload_head);
  26961. static DEFINE_PER_CPU(int, idr_preload_cnt);
  26962. static DEFINE_SPINLOCK(simple_ida_lock);
  26963. +#ifdef CONFIG_PREEMPT_RT_FULL
  26964. +static DEFINE_LOCAL_IRQ_LOCK(idr_lock);
  26965. +
  26966. +static inline void idr_preload_lock(void)
  26967. +{
  26968. + local_lock(idr_lock);
  26969. +}
  26970. +
  26971. +static inline void idr_preload_unlock(void)
  26972. +{
  26973. + local_unlock(idr_lock);
  26974. +}
  26975. +
  26976. +void idr_preload_end(void)
  26977. +{
  26978. + idr_preload_unlock();
  26979. +}
  26980. +EXPORT_SYMBOL(idr_preload_end);
  26981. +#else
  26982. +static inline void idr_preload_lock(void)
  26983. +{
  26984. + preempt_disable();
  26985. +}
  26986. +
  26987. +static inline void idr_preload_unlock(void)
  26988. +{
  26989. + preempt_enable();
  26990. +}
  26991. +#endif
  26992. +
  26993. +
  26994. /* the maximum ID which can be allocated given idr->layers */
  26995. static int idr_max(int layers)
  26996. {
  26997. @@ -115,14 +147,14 @@ static struct idr_layer *idr_layer_alloc(gfp_t gfp_mask, struct idr *layer_idr)
  26998. * context. See idr_preload() for details.
  26999. */
  27000. if (!in_interrupt()) {
  27001. - preempt_disable();
  27002. + idr_preload_lock();
  27003. new = __this_cpu_read(idr_preload_head);
  27004. if (new) {
  27005. __this_cpu_write(idr_preload_head, new->ary[0]);
  27006. __this_cpu_dec(idr_preload_cnt);
  27007. new->ary[0] = NULL;
  27008. }
  27009. - preempt_enable();
  27010. + idr_preload_unlock();
  27011. if (new)
  27012. return new;
  27013. }
  27014. @@ -366,7 +398,6 @@ static void idr_fill_slot(struct idr *idr, void *ptr, int id,
  27015. idr_mark_full(pa, id);
  27016. }
  27017. -
  27018. /**
  27019. * idr_preload - preload for idr_alloc()
  27020. * @gfp_mask: allocation mask to use for preloading
  27021. @@ -401,7 +432,7 @@ void idr_preload(gfp_t gfp_mask)
  27022. WARN_ON_ONCE(in_interrupt());
  27023. might_sleep_if(gfpflags_allow_blocking(gfp_mask));
  27024. - preempt_disable();
  27025. + idr_preload_lock();
  27026. /*
  27027. * idr_alloc() is likely to succeed w/o full idr_layer buffer and
  27028. @@ -413,9 +444,9 @@ void idr_preload(gfp_t gfp_mask)
  27029. while (__this_cpu_read(idr_preload_cnt) < MAX_IDR_FREE) {
  27030. struct idr_layer *new;
  27031. - preempt_enable();
  27032. + idr_preload_unlock();
  27033. new = kmem_cache_zalloc(idr_layer_cache, gfp_mask);
  27034. - preempt_disable();
  27035. + idr_preload_lock();
  27036. if (!new)
  27037. break;
  27038. diff --git a/lib/locking-selftest.c b/lib/locking-selftest.c
  27039. index 872a15a2a637..b93a6103fa4d 100644
  27040. --- a/lib/locking-selftest.c
  27041. +++ b/lib/locking-selftest.c
  27042. @@ -590,6 +590,8 @@ GENERATE_TESTCASE(init_held_rsem)
  27043. #include "locking-selftest-spin-hardirq.h"
  27044. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_spin)
  27045. +#ifndef CONFIG_PREEMPT_RT_FULL
  27046. +
  27047. #include "locking-selftest-rlock-hardirq.h"
  27048. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_rlock)
  27049. @@ -605,9 +607,12 @@ GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_rlock)
  27050. #include "locking-selftest-wlock-softirq.h"
  27051. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_wlock)
  27052. +#endif
  27053. +
  27054. #undef E1
  27055. #undef E2
  27056. +#ifndef CONFIG_PREEMPT_RT_FULL
  27057. /*
  27058. * Enabling hardirqs with a softirq-safe lock held:
  27059. */
  27060. @@ -640,6 +645,8 @@ GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_rlock)
  27061. #undef E1
  27062. #undef E2
  27063. +#endif
  27064. +
  27065. /*
  27066. * Enabling irqs with an irq-safe lock held:
  27067. */
  27068. @@ -663,6 +670,8 @@ GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_rlock)
  27069. #include "locking-selftest-spin-hardirq.h"
  27070. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_spin)
  27071. +#ifndef CONFIG_PREEMPT_RT_FULL
  27072. +
  27073. #include "locking-selftest-rlock-hardirq.h"
  27074. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_rlock)
  27075. @@ -678,6 +687,8 @@ GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_rlock)
  27076. #include "locking-selftest-wlock-softirq.h"
  27077. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock)
  27078. +#endif
  27079. +
  27080. #undef E1
  27081. #undef E2
  27082. @@ -709,6 +720,8 @@ GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock)
  27083. #include "locking-selftest-spin-hardirq.h"
  27084. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_spin)
  27085. +#ifndef CONFIG_PREEMPT_RT_FULL
  27086. +
  27087. #include "locking-selftest-rlock-hardirq.h"
  27088. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_rlock)
  27089. @@ -724,6 +737,8 @@ GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_rlock)
  27090. #include "locking-selftest-wlock-softirq.h"
  27091. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock)
  27092. +#endif
  27093. +
  27094. #undef E1
  27095. #undef E2
  27096. #undef E3
  27097. @@ -757,6 +772,8 @@ GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock)
  27098. #include "locking-selftest-spin-hardirq.h"
  27099. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_spin)
  27100. +#ifndef CONFIG_PREEMPT_RT_FULL
  27101. +
  27102. #include "locking-selftest-rlock-hardirq.h"
  27103. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_rlock)
  27104. @@ -772,10 +789,14 @@ GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_rlock)
  27105. #include "locking-selftest-wlock-softirq.h"
  27106. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_wlock)
  27107. +#endif
  27108. +
  27109. #undef E1
  27110. #undef E2
  27111. #undef E3
  27112. +#ifndef CONFIG_PREEMPT_RT_FULL
  27113. +
  27114. /*
  27115. * read-lock / write-lock irq inversion.
  27116. *
  27117. @@ -838,6 +859,10 @@ GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_wlock)
  27118. #undef E2
  27119. #undef E3
  27120. +#endif
  27121. +
  27122. +#ifndef CONFIG_PREEMPT_RT_FULL
  27123. +
  27124. /*
  27125. * read-lock / write-lock recursion that is actually safe.
  27126. */
  27127. @@ -876,6 +901,8 @@ GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft)
  27128. #undef E2
  27129. #undef E3
  27130. +#endif
  27131. +
  27132. /*
  27133. * read-lock / write-lock recursion that is unsafe.
  27134. */
  27135. @@ -1858,6 +1885,7 @@ void locking_selftest(void)
  27136. printk(" --------------------------------------------------------------------------\n");
  27137. +#ifndef CONFIG_PREEMPT_RT_FULL
  27138. /*
  27139. * irq-context testcases:
  27140. */
  27141. @@ -1870,6 +1898,28 @@ void locking_selftest(void)
  27142. DO_TESTCASE_6x2("irq read-recursion", irq_read_recursion);
  27143. // DO_TESTCASE_6x2B("irq read-recursion #2", irq_read_recursion2);
  27144. +#else
  27145. + /* On -rt, we only do hardirq context test for raw spinlock */
  27146. + DO_TESTCASE_1B("hard-irqs-on + irq-safe-A", irqsafe1_hard_spin, 12);
  27147. + DO_TESTCASE_1B("hard-irqs-on + irq-safe-A", irqsafe1_hard_spin, 21);
  27148. +
  27149. + DO_TESTCASE_1B("hard-safe-A + irqs-on", irqsafe2B_hard_spin, 12);
  27150. + DO_TESTCASE_1B("hard-safe-A + irqs-on", irqsafe2B_hard_spin, 21);
  27151. +
  27152. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 123);
  27153. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 132);
  27154. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 213);
  27155. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 231);
  27156. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 312);
  27157. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 321);
  27158. +
  27159. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 123);
  27160. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 132);
  27161. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 213);
  27162. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 231);
  27163. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 312);
  27164. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 321);
  27165. +#endif
  27166. ww_tests();
  27167. diff --git a/lib/percpu_ida.c b/lib/percpu_ida.c
  27168. index 6d40944960de..822a2c027e72 100644
  27169. --- a/lib/percpu_ida.c
  27170. +++ b/lib/percpu_ida.c
  27171. @@ -26,6 +26,9 @@
  27172. #include <linux/string.h>
  27173. #include <linux/spinlock.h>
  27174. #include <linux/percpu_ida.h>
  27175. +#include <linux/locallock.h>
  27176. +
  27177. +static DEFINE_LOCAL_IRQ_LOCK(irq_off_lock);
  27178. struct percpu_ida_cpu {
  27179. /*
  27180. @@ -148,13 +151,13 @@ int percpu_ida_alloc(struct percpu_ida *pool, int state)
  27181. unsigned long flags;
  27182. int tag;
  27183. - local_irq_save(flags);
  27184. + local_lock_irqsave(irq_off_lock, flags);
  27185. tags = this_cpu_ptr(pool->tag_cpu);
  27186. /* Fastpath */
  27187. tag = alloc_local_tag(tags);
  27188. if (likely(tag >= 0)) {
  27189. - local_irq_restore(flags);
  27190. + local_unlock_irqrestore(irq_off_lock, flags);
  27191. return tag;
  27192. }
  27193. @@ -173,6 +176,7 @@ int percpu_ida_alloc(struct percpu_ida *pool, int state)
  27194. if (!tags->nr_free)
  27195. alloc_global_tags(pool, tags);
  27196. +
  27197. if (!tags->nr_free)
  27198. steal_tags(pool, tags);
  27199. @@ -184,7 +188,7 @@ int percpu_ida_alloc(struct percpu_ida *pool, int state)
  27200. }
  27201. spin_unlock(&pool->lock);
  27202. - local_irq_restore(flags);
  27203. + local_unlock_irqrestore(irq_off_lock, flags);
  27204. if (tag >= 0 || state == TASK_RUNNING)
  27205. break;
  27206. @@ -196,7 +200,7 @@ int percpu_ida_alloc(struct percpu_ida *pool, int state)
  27207. schedule();
  27208. - local_irq_save(flags);
  27209. + local_lock_irqsave(irq_off_lock, flags);
  27210. tags = this_cpu_ptr(pool->tag_cpu);
  27211. }
  27212. if (state != TASK_RUNNING)
  27213. @@ -221,7 +225,7 @@ void percpu_ida_free(struct percpu_ida *pool, unsigned tag)
  27214. BUG_ON(tag >= pool->nr_tags);
  27215. - local_irq_save(flags);
  27216. + local_lock_irqsave(irq_off_lock, flags);
  27217. tags = this_cpu_ptr(pool->tag_cpu);
  27218. spin_lock(&tags->lock);
  27219. @@ -253,7 +257,7 @@ void percpu_ida_free(struct percpu_ida *pool, unsigned tag)
  27220. spin_unlock(&pool->lock);
  27221. }
  27222. - local_irq_restore(flags);
  27223. + local_unlock_irqrestore(irq_off_lock, flags);
  27224. }
  27225. EXPORT_SYMBOL_GPL(percpu_ida_free);
  27226. @@ -345,7 +349,7 @@ int percpu_ida_for_each_free(struct percpu_ida *pool, percpu_ida_cb fn,
  27227. struct percpu_ida_cpu *remote;
  27228. unsigned cpu, i, err = 0;
  27229. - local_irq_save(flags);
  27230. + local_lock_irqsave(irq_off_lock, flags);
  27231. for_each_possible_cpu(cpu) {
  27232. remote = per_cpu_ptr(pool->tag_cpu, cpu);
  27233. spin_lock(&remote->lock);
  27234. @@ -367,7 +371,7 @@ int percpu_ida_for_each_free(struct percpu_ida *pool, percpu_ida_cb fn,
  27235. }
  27236. spin_unlock(&pool->lock);
  27237. out:
  27238. - local_irq_restore(flags);
  27239. + local_unlock_irqrestore(irq_off_lock, flags);
  27240. return err;
  27241. }
  27242. EXPORT_SYMBOL_GPL(percpu_ida_for_each_free);
  27243. diff --git a/lib/radix-tree.c b/lib/radix-tree.c
  27244. index 6b79e9026e24..44bf36a396a9 100644
  27245. --- a/lib/radix-tree.c
  27246. +++ b/lib/radix-tree.c
  27247. @@ -34,7 +34,7 @@
  27248. #include <linux/bitops.h>
  27249. #include <linux/rcupdate.h>
  27250. #include <linux/preempt.h> /* in_interrupt() */
  27251. -
  27252. +#include <linux/locallock.h>
  27253. /*
  27254. * The height_to_maxindex array needs to be one deeper than the maximum
  27255. @@ -69,6 +69,7 @@ struct radix_tree_preload {
  27256. struct radix_tree_node *nodes;
  27257. };
  27258. static DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, };
  27259. +static DEFINE_LOCAL_IRQ_LOCK(radix_tree_preloads_lock);
  27260. static inline void *ptr_to_indirect(void *ptr)
  27261. {
  27262. @@ -196,13 +197,14 @@ radix_tree_node_alloc(struct radix_tree_root *root)
  27263. * succeed in getting a node here (and never reach
  27264. * kmem_cache_alloc)
  27265. */
  27266. - rtp = this_cpu_ptr(&radix_tree_preloads);
  27267. + rtp = &get_locked_var(radix_tree_preloads_lock, radix_tree_preloads);
  27268. if (rtp->nr) {
  27269. ret = rtp->nodes;
  27270. rtp->nodes = ret->private_data;
  27271. ret->private_data = NULL;
  27272. rtp->nr--;
  27273. }
  27274. + put_locked_var(radix_tree_preloads_lock, radix_tree_preloads);
  27275. /*
  27276. * Update the allocation stack trace as this is more useful
  27277. * for debugging.
  27278. @@ -257,14 +259,14 @@ static int __radix_tree_preload(gfp_t gfp_mask)
  27279. struct radix_tree_node *node;
  27280. int ret = -ENOMEM;
  27281. - preempt_disable();
  27282. + local_lock(radix_tree_preloads_lock);
  27283. rtp = this_cpu_ptr(&radix_tree_preloads);
  27284. while (rtp->nr < RADIX_TREE_PRELOAD_SIZE) {
  27285. - preempt_enable();
  27286. + local_unlock(radix_tree_preloads_lock);
  27287. node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
  27288. if (node == NULL)
  27289. goto out;
  27290. - preempt_disable();
  27291. + local_lock(radix_tree_preloads_lock);
  27292. rtp = this_cpu_ptr(&radix_tree_preloads);
  27293. if (rtp->nr < RADIX_TREE_PRELOAD_SIZE) {
  27294. node->private_data = rtp->nodes;
  27295. @@ -306,11 +308,17 @@ int radix_tree_maybe_preload(gfp_t gfp_mask)
  27296. if (gfpflags_allow_blocking(gfp_mask))
  27297. return __radix_tree_preload(gfp_mask);
  27298. /* Preloading doesn't help anything with this gfp mask, skip it */
  27299. - preempt_disable();
  27300. + local_lock(radix_tree_preloads_lock);
  27301. return 0;
  27302. }
  27303. EXPORT_SYMBOL(radix_tree_maybe_preload);
  27304. +void radix_tree_preload_end(void)
  27305. +{
  27306. + local_unlock(radix_tree_preloads_lock);
  27307. +}
  27308. +EXPORT_SYMBOL(radix_tree_preload_end);
  27309. +
  27310. /*
  27311. * Return the maximum key which can be store into a
  27312. * radix tree with height HEIGHT.
  27313. diff --git a/lib/rbtree.c b/lib/rbtree.c
  27314. index 1356454e36de..d15d6c4327f1 100644
  27315. --- a/lib/rbtree.c
  27316. +++ b/lib/rbtree.c
  27317. @@ -23,6 +23,7 @@
  27318. #include <linux/rbtree_augmented.h>
  27319. #include <linux/export.h>
  27320. +#include <linux/rcupdate.h>
  27321. /*
  27322. * red-black trees properties: http://en.wikipedia.org/wiki/Rbtree
  27323. @@ -590,3 +591,13 @@ struct rb_node *rb_first_postorder(const struct rb_root *root)
  27324. return rb_left_deepest_node(root->rb_node);
  27325. }
  27326. EXPORT_SYMBOL(rb_first_postorder);
  27327. +
  27328. +void rb_link_node_rcu(struct rb_node *node, struct rb_node *parent,
  27329. + struct rb_node **rb_link)
  27330. +{
  27331. + node->__rb_parent_color = (unsigned long)parent;
  27332. + node->rb_left = node->rb_right = NULL;
  27333. +
  27334. + rcu_assign_pointer(*rb_link, node);
  27335. +}
  27336. +EXPORT_SYMBOL(rb_link_node_rcu);
  27337. diff --git a/lib/scatterlist.c b/lib/scatterlist.c
  27338. index bafa9933fa76..ebe3b7edd086 100644
  27339. --- a/lib/scatterlist.c
  27340. +++ b/lib/scatterlist.c
  27341. @@ -620,7 +620,7 @@ void sg_miter_stop(struct sg_mapping_iter *miter)
  27342. flush_kernel_dcache_page(miter->page);
  27343. if (miter->__flags & SG_MITER_ATOMIC) {
  27344. - WARN_ON_ONCE(preemptible());
  27345. + WARN_ON_ONCE(!pagefault_disabled());
  27346. kunmap_atomic(miter->addr);
  27347. } else
  27348. kunmap(miter->page);
  27349. @@ -664,7 +664,7 @@ size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents, void *buf,
  27350. if (!sg_miter_skip(&miter, skip))
  27351. return false;
  27352. - local_irq_save(flags);
  27353. + local_irq_save_nort(flags);
  27354. while (sg_miter_next(&miter) && offset < buflen) {
  27355. unsigned int len;
  27356. @@ -681,7 +681,7 @@ size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents, void *buf,
  27357. sg_miter_stop(&miter);
  27358. - local_irq_restore(flags);
  27359. + local_irq_restore_nort(flags);
  27360. return offset;
  27361. }
  27362. EXPORT_SYMBOL(sg_copy_buffer);
  27363. diff --git a/lib/smp_processor_id.c b/lib/smp_processor_id.c
  27364. index 1afec32de6f2..11fa431046a8 100644
  27365. --- a/lib/smp_processor_id.c
  27366. +++ b/lib/smp_processor_id.c
  27367. @@ -39,8 +39,9 @@ notrace static unsigned int check_preemption_disabled(const char *what1,
  27368. if (!printk_ratelimit())
  27369. goto out_enable;
  27370. - printk(KERN_ERR "BUG: using %s%s() in preemptible [%08x] code: %s/%d\n",
  27371. - what1, what2, preempt_count() - 1, current->comm, current->pid);
  27372. + printk(KERN_ERR "BUG: using %s%s() in preemptible [%08x %08x] code: %s/%d\n",
  27373. + what1, what2, preempt_count() - 1, __migrate_disabled(current),
  27374. + current->comm, current->pid);
  27375. print_symbol("caller is %s\n", (long)__builtin_return_address(0));
  27376. dump_stack();
  27377. diff --git a/mm/Kconfig b/mm/Kconfig
  27378. index 97a4e06b15c0..9614351e68b8 100644
  27379. --- a/mm/Kconfig
  27380. +++ b/mm/Kconfig
  27381. @@ -392,7 +392,7 @@ config NOMMU_INITIAL_TRIM_EXCESS
  27382. config TRANSPARENT_HUGEPAGE
  27383. bool "Transparent Hugepage Support"
  27384. - depends on HAVE_ARCH_TRANSPARENT_HUGEPAGE
  27385. + depends on HAVE_ARCH_TRANSPARENT_HUGEPAGE && !PREEMPT_RT_FULL
  27386. select COMPACTION
  27387. help
  27388. Transparent Hugepages allows the kernel to use huge pages and
  27389. diff --git a/mm/backing-dev.c b/mm/backing-dev.c
  27390. index a988d4ef39da..f2c2ee1d5191 100644
  27391. --- a/mm/backing-dev.c
  27392. +++ b/mm/backing-dev.c
  27393. @@ -457,9 +457,9 @@ void wb_congested_put(struct bdi_writeback_congested *congested)
  27394. {
  27395. unsigned long flags;
  27396. - local_irq_save(flags);
  27397. + local_irq_save_nort(flags);
  27398. if (!atomic_dec_and_lock(&congested->refcnt, &cgwb_lock)) {
  27399. - local_irq_restore(flags);
  27400. + local_irq_restore_nort(flags);
  27401. return;
  27402. }
  27403. diff --git a/mm/compaction.c b/mm/compaction.c
  27404. index dba02dec7195..51963f58a29b 100644
  27405. --- a/mm/compaction.c
  27406. +++ b/mm/compaction.c
  27407. @@ -1430,10 +1430,12 @@ check_drain:
  27408. cc->migrate_pfn & ~((1UL << cc->order) - 1);
  27409. if (cc->last_migrated_pfn < current_block_start) {
  27410. - cpu = get_cpu();
  27411. + cpu = get_cpu_light();
  27412. + local_lock_irq(swapvec_lock);
  27413. lru_add_drain_cpu(cpu);
  27414. + local_unlock_irq(swapvec_lock);
  27415. drain_local_pages(zone);
  27416. - put_cpu();
  27417. + put_cpu_light();
  27418. /* No more flushing until we migrate again */
  27419. cc->last_migrated_pfn = 0;
  27420. }
  27421. diff --git a/mm/filemap.c b/mm/filemap.c
  27422. index 69f75c77c098..b203169ca0b4 100644
  27423. --- a/mm/filemap.c
  27424. +++ b/mm/filemap.c
  27425. @@ -144,9 +144,12 @@ static int page_cache_tree_insert(struct address_space *mapping,
  27426. * node->private_list is protected by
  27427. * mapping->tree_lock.
  27428. */
  27429. - if (!list_empty(&node->private_list))
  27430. - list_lru_del(&workingset_shadow_nodes,
  27431. + if (!list_empty(&node->private_list)) {
  27432. + local_lock(workingset_shadow_lock);
  27433. + list_lru_del(&__workingset_shadow_nodes,
  27434. &node->private_list);
  27435. + local_unlock(workingset_shadow_lock);
  27436. + }
  27437. }
  27438. return 0;
  27439. }
  27440. @@ -218,7 +221,9 @@ static void page_cache_tree_delete(struct address_space *mapping,
  27441. if (!workingset_node_pages(node) &&
  27442. list_empty(&node->private_list)) {
  27443. node->private_data = mapping;
  27444. - list_lru_add(&workingset_shadow_nodes, &node->private_list);
  27445. + local_lock(workingset_shadow_lock);
  27446. + list_lru_add(&__workingset_shadow_nodes, &node->private_list);
  27447. + local_unlock(workingset_shadow_lock);
  27448. }
  27449. }
  27450. diff --git a/mm/highmem.c b/mm/highmem.c
  27451. index 123bcd3ed4f2..16e8cf26d38a 100644
  27452. --- a/mm/highmem.c
  27453. +++ b/mm/highmem.c
  27454. @@ -29,10 +29,11 @@
  27455. #include <linux/kgdb.h>
  27456. #include <asm/tlbflush.h>
  27457. -
  27458. +#ifndef CONFIG_PREEMPT_RT_FULL
  27459. #if defined(CONFIG_HIGHMEM) || defined(CONFIG_X86_32)
  27460. DEFINE_PER_CPU(int, __kmap_atomic_idx);
  27461. #endif
  27462. +#endif
  27463. /*
  27464. * Virtual_count is not a pure "count".
  27465. @@ -107,8 +108,9 @@ static inline wait_queue_head_t *get_pkmap_wait_queue_head(unsigned int color)
  27466. unsigned long totalhigh_pages __read_mostly;
  27467. EXPORT_SYMBOL(totalhigh_pages);
  27468. -
  27469. +#ifndef CONFIG_PREEMPT_RT_FULL
  27470. EXPORT_PER_CPU_SYMBOL(__kmap_atomic_idx);
  27471. +#endif
  27472. unsigned int nr_free_highpages (void)
  27473. {
  27474. diff --git a/mm/memcontrol.c b/mm/memcontrol.c
  27475. index e25b93a4267d..1c619267d9da 100644
  27476. --- a/mm/memcontrol.c
  27477. +++ b/mm/memcontrol.c
  27478. @@ -67,6 +67,8 @@
  27479. #include <net/sock.h>
  27480. #include <net/ip.h>
  27481. #include <net/tcp_memcontrol.h>
  27482. +#include <linux/locallock.h>
  27483. +
  27484. #include "slab.h"
  27485. #include <asm/uaccess.h>
  27486. @@ -87,6 +89,7 @@ int do_swap_account __read_mostly;
  27487. #define do_swap_account 0
  27488. #endif
  27489. +static DEFINE_LOCAL_IRQ_LOCK(event_lock);
  27490. static const char * const mem_cgroup_stat_names[] = {
  27491. "cache",
  27492. "rss",
  27493. @@ -1922,14 +1925,17 @@ static void drain_local_stock(struct work_struct *dummy)
  27494. */
  27495. static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
  27496. {
  27497. - struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
  27498. + struct memcg_stock_pcp *stock;
  27499. + int cpu = get_cpu_light();
  27500. +
  27501. + stock = &per_cpu(memcg_stock, cpu);
  27502. if (stock->cached != memcg) { /* reset if necessary */
  27503. drain_stock(stock);
  27504. stock->cached = memcg;
  27505. }
  27506. stock->nr_pages += nr_pages;
  27507. - put_cpu_var(memcg_stock);
  27508. + put_cpu_light();
  27509. }
  27510. /*
  27511. @@ -1945,7 +1951,7 @@ static void drain_all_stock(struct mem_cgroup *root_memcg)
  27512. return;
  27513. /* Notify other cpus that system-wide "drain" is running */
  27514. get_online_cpus();
  27515. - curcpu = get_cpu();
  27516. + curcpu = get_cpu_light();
  27517. for_each_online_cpu(cpu) {
  27518. struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
  27519. struct mem_cgroup *memcg;
  27520. @@ -1962,7 +1968,7 @@ static void drain_all_stock(struct mem_cgroup *root_memcg)
  27521. schedule_work_on(cpu, &stock->work);
  27522. }
  27523. }
  27524. - put_cpu();
  27525. + put_cpu_light();
  27526. put_online_cpus();
  27527. mutex_unlock(&percpu_charge_mutex);
  27528. }
  27529. @@ -4691,12 +4697,12 @@ static int mem_cgroup_move_account(struct page *page,
  27530. ret = 0;
  27531. - local_irq_disable();
  27532. + local_lock_irq(event_lock);
  27533. mem_cgroup_charge_statistics(to, page, nr_pages);
  27534. memcg_check_events(to, page);
  27535. mem_cgroup_charge_statistics(from, page, -nr_pages);
  27536. memcg_check_events(from, page);
  27537. - local_irq_enable();
  27538. + local_unlock_irq(event_lock);
  27539. out_unlock:
  27540. unlock_page(page);
  27541. out:
  27542. @@ -5486,10 +5492,10 @@ void mem_cgroup_commit_charge(struct page *page, struct mem_cgroup *memcg,
  27543. VM_BUG_ON_PAGE(!PageTransHuge(page), page);
  27544. }
  27545. - local_irq_disable();
  27546. + local_lock_irq(event_lock);
  27547. mem_cgroup_charge_statistics(memcg, page, nr_pages);
  27548. memcg_check_events(memcg, page);
  27549. - local_irq_enable();
  27550. + local_unlock_irq(event_lock);
  27551. if (do_swap_account && PageSwapCache(page)) {
  27552. swp_entry_t entry = { .val = page_private(page) };
  27553. @@ -5545,14 +5551,14 @@ static void uncharge_batch(struct mem_cgroup *memcg, unsigned long pgpgout,
  27554. memcg_oom_recover(memcg);
  27555. }
  27556. - local_irq_save(flags);
  27557. + local_lock_irqsave(event_lock, flags);
  27558. __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS], nr_anon);
  27559. __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_CACHE], nr_file);
  27560. __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE], nr_huge);
  27561. __this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT], pgpgout);
  27562. __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
  27563. memcg_check_events(memcg, dummy_page);
  27564. - local_irq_restore(flags);
  27565. + local_unlock_irqrestore(event_lock, flags);
  27566. if (!mem_cgroup_is_root(memcg))
  27567. css_put_many(&memcg->css, nr_pages);
  27568. @@ -5762,6 +5768,7 @@ void mem_cgroup_swapout(struct page *page, swp_entry_t entry)
  27569. {
  27570. struct mem_cgroup *memcg, *swap_memcg;
  27571. unsigned short oldid;
  27572. + unsigned long flags;
  27573. VM_BUG_ON_PAGE(PageLRU(page), page);
  27574. VM_BUG_ON_PAGE(page_count(page), page);
  27575. @@ -5802,12 +5809,16 @@ void mem_cgroup_swapout(struct page *page, swp_entry_t entry)
  27576. * important here to have the interrupts disabled because it is the
  27577. * only synchronisation we have for udpating the per-CPU variables.
  27578. */
  27579. + local_lock_irqsave(event_lock, flags);
  27580. +#ifndef CONFIG_PREEMPT_RT_BASE
  27581. VM_BUG_ON(!irqs_disabled());
  27582. +#endif
  27583. mem_cgroup_charge_statistics(memcg, page, -1);
  27584. memcg_check_events(memcg, page);
  27585. if (!mem_cgroup_is_root(memcg))
  27586. css_put(&memcg->css);
  27587. + local_unlock_irqrestore(event_lock, flags);
  27588. }
  27589. /**
  27590. diff --git a/mm/mmu_context.c b/mm/mmu_context.c
  27591. index f802c2d216a7..b1b6f238e42d 100644
  27592. --- a/mm/mmu_context.c
  27593. +++ b/mm/mmu_context.c
  27594. @@ -23,6 +23,7 @@ void use_mm(struct mm_struct *mm)
  27595. struct task_struct *tsk = current;
  27596. task_lock(tsk);
  27597. + preempt_disable_rt();
  27598. active_mm = tsk->active_mm;
  27599. if (active_mm != mm) {
  27600. atomic_inc(&mm->mm_count);
  27601. @@ -30,6 +31,7 @@ void use_mm(struct mm_struct *mm)
  27602. }
  27603. tsk->mm = mm;
  27604. switch_mm(active_mm, mm, tsk);
  27605. + preempt_enable_rt();
  27606. task_unlock(tsk);
  27607. #ifdef finish_arch_post_lock_switch
  27608. finish_arch_post_lock_switch();
  27609. diff --git a/mm/page_alloc.c b/mm/page_alloc.c
  27610. index 6f9005dcca2e..e8ec72251705 100644
  27611. --- a/mm/page_alloc.c
  27612. +++ b/mm/page_alloc.c
  27613. @@ -60,6 +60,7 @@
  27614. #include <linux/page_ext.h>
  27615. #include <linux/hugetlb.h>
  27616. #include <linux/sched/rt.h>
  27617. +#include <linux/locallock.h>
  27618. #include <linux/page_owner.h>
  27619. #include <linux/kthread.h>
  27620. @@ -264,6 +265,18 @@ EXPORT_SYMBOL(nr_node_ids);
  27621. EXPORT_SYMBOL(nr_online_nodes);
  27622. #endif
  27623. +static DEFINE_LOCAL_IRQ_LOCK(pa_lock);
  27624. +
  27625. +#ifdef CONFIG_PREEMPT_RT_BASE
  27626. +# define cpu_lock_irqsave(cpu, flags) \
  27627. + local_lock_irqsave_on(pa_lock, flags, cpu)
  27628. +# define cpu_unlock_irqrestore(cpu, flags) \
  27629. + local_unlock_irqrestore_on(pa_lock, flags, cpu)
  27630. +#else
  27631. +# define cpu_lock_irqsave(cpu, flags) local_irq_save(flags)
  27632. +# define cpu_unlock_irqrestore(cpu, flags) local_irq_restore(flags)
  27633. +#endif
  27634. +
  27635. int page_group_by_mobility_disabled __read_mostly;
  27636. #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
  27637. @@ -786,7 +799,7 @@ static inline int free_pages_check(struct page *page)
  27638. }
  27639. /*
  27640. - * Frees a number of pages from the PCP lists
  27641. + * Frees a number of pages which have been collected from the pcp lists.
  27642. * Assumes all pages on list are in same zone, and of same order.
  27643. * count is the number of pages to free.
  27644. *
  27645. @@ -797,18 +810,53 @@ static inline int free_pages_check(struct page *page)
  27646. * pinned" detection logic.
  27647. */
  27648. static void free_pcppages_bulk(struct zone *zone, int count,
  27649. - struct per_cpu_pages *pcp)
  27650. + struct list_head *list)
  27651. {
  27652. - int migratetype = 0;
  27653. - int batch_free = 0;
  27654. int to_free = count;
  27655. unsigned long nr_scanned;
  27656. + unsigned long flags;
  27657. +
  27658. + spin_lock_irqsave(&zone->lock, flags);
  27659. - spin_lock(&zone->lock);
  27660. nr_scanned = zone_page_state(zone, NR_PAGES_SCANNED);
  27661. if (nr_scanned)
  27662. __mod_zone_page_state(zone, NR_PAGES_SCANNED, -nr_scanned);
  27663. + while (!list_empty(list)) {
  27664. + struct page *page = list_first_entry(list, struct page, lru);
  27665. + int mt; /* migratetype of the to-be-freed page */
  27666. +
  27667. + /* must delete as __free_one_page list manipulates */
  27668. + list_del(&page->lru);
  27669. +
  27670. + mt = get_pcppage_migratetype(page);
  27671. + /* MIGRATE_ISOLATE page should not go to pcplists */
  27672. + VM_BUG_ON_PAGE(is_migrate_isolate(mt), page);
  27673. + /* Pageblock could have been isolated meanwhile */
  27674. + if (unlikely(has_isolate_pageblock(zone)))
  27675. + mt = get_pageblock_migratetype(page);
  27676. +
  27677. + __free_one_page(page, page_to_pfn(page), zone, 0, mt);
  27678. + trace_mm_page_pcpu_drain(page, 0, mt);
  27679. + to_free--;
  27680. + }
  27681. + WARN_ON(to_free != 0);
  27682. + spin_unlock_irqrestore(&zone->lock, flags);
  27683. +}
  27684. +
  27685. +/*
  27686. + * Moves a number of pages from the PCP lists to free list which
  27687. + * is freed outside of the locked region.
  27688. + *
  27689. + * Assumes all pages on list are in same zone, and of same order.
  27690. + * count is the number of pages to free.
  27691. + */
  27692. +static void isolate_pcp_pages(int to_free, struct per_cpu_pages *src,
  27693. + struct list_head *dst)
  27694. +{
  27695. + int migratetype = 0;
  27696. + int batch_free = 0;
  27697. +
  27698. while (to_free) {
  27699. struct page *page;
  27700. struct list_head *list;
  27701. @@ -824,7 +872,7 @@ static void free_pcppages_bulk(struct zone *zone, int count,
  27702. batch_free++;
  27703. if (++migratetype == MIGRATE_PCPTYPES)
  27704. migratetype = 0;
  27705. - list = &pcp->lists[migratetype];
  27706. + list = &src->lists[migratetype];
  27707. } while (list_empty(list));
  27708. /* This is the only non-empty list. Free them all. */
  27709. @@ -832,24 +880,12 @@ static void free_pcppages_bulk(struct zone *zone, int count,
  27710. batch_free = to_free;
  27711. do {
  27712. - int mt; /* migratetype of the to-be-freed page */
  27713. -
  27714. - page = list_entry(list->prev, struct page, lru);
  27715. - /* must delete as __free_one_page list manipulates */
  27716. + page = list_last_entry(list, struct page, lru);
  27717. list_del(&page->lru);
  27718. - mt = get_pcppage_migratetype(page);
  27719. - /* MIGRATE_ISOLATE page should not go to pcplists */
  27720. - VM_BUG_ON_PAGE(is_migrate_isolate(mt), page);
  27721. - /* Pageblock could have been isolated meanwhile */
  27722. - if (unlikely(has_isolate_pageblock(zone)))
  27723. - mt = get_pageblock_migratetype(page);
  27724. -
  27725. - __free_one_page(page, page_to_pfn(page), zone, 0, mt);
  27726. - trace_mm_page_pcpu_drain(page, 0, mt);
  27727. + list_add(&page->lru, dst);
  27728. } while (--to_free && --batch_free && !list_empty(list));
  27729. }
  27730. - spin_unlock(&zone->lock);
  27731. }
  27732. static void free_one_page(struct zone *zone,
  27733. @@ -858,7 +894,9 @@ static void free_one_page(struct zone *zone,
  27734. int migratetype)
  27735. {
  27736. unsigned long nr_scanned;
  27737. - spin_lock(&zone->lock);
  27738. + unsigned long flags;
  27739. +
  27740. + spin_lock_irqsave(&zone->lock, flags);
  27741. nr_scanned = zone_page_state(zone, NR_PAGES_SCANNED);
  27742. if (nr_scanned)
  27743. __mod_zone_page_state(zone, NR_PAGES_SCANNED, -nr_scanned);
  27744. @@ -868,7 +906,7 @@ static void free_one_page(struct zone *zone,
  27745. migratetype = get_pfnblock_migratetype(page, pfn);
  27746. }
  27747. __free_one_page(page, pfn, zone, order, migratetype);
  27748. - spin_unlock(&zone->lock);
  27749. + spin_unlock_irqrestore(&zone->lock, flags);
  27750. }
  27751. static int free_tail_pages_check(struct page *head_page, struct page *page)
  27752. @@ -1019,10 +1057,10 @@ static void __free_pages_ok(struct page *page, unsigned int order)
  27753. return;
  27754. migratetype = get_pfnblock_migratetype(page, pfn);
  27755. - local_irq_save(flags);
  27756. + local_lock_irqsave(pa_lock, flags);
  27757. __count_vm_events(PGFREE, 1 << order);
  27758. free_one_page(page_zone(page), page, pfn, order, migratetype);
  27759. - local_irq_restore(flags);
  27760. + local_unlock_irqrestore(pa_lock, flags);
  27761. }
  27762. static void __init __free_pages_boot_core(struct page *page,
  27763. @@ -1879,16 +1917,18 @@ static int rmqueue_bulk(struct zone *zone, unsigned int order,
  27764. void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp)
  27765. {
  27766. unsigned long flags;
  27767. + LIST_HEAD(dst);
  27768. int to_drain, batch;
  27769. - local_irq_save(flags);
  27770. + local_lock_irqsave(pa_lock, flags);
  27771. batch = READ_ONCE(pcp->batch);
  27772. to_drain = min(pcp->count, batch);
  27773. if (to_drain > 0) {
  27774. - free_pcppages_bulk(zone, to_drain, pcp);
  27775. + isolate_pcp_pages(to_drain, pcp, &dst);
  27776. pcp->count -= to_drain;
  27777. }
  27778. - local_irq_restore(flags);
  27779. + local_unlock_irqrestore(pa_lock, flags);
  27780. + free_pcppages_bulk(zone, to_drain, &dst);
  27781. }
  27782. #endif
  27783. @@ -1904,16 +1944,21 @@ static void drain_pages_zone(unsigned int cpu, struct zone *zone)
  27784. unsigned long flags;
  27785. struct per_cpu_pageset *pset;
  27786. struct per_cpu_pages *pcp;
  27787. + LIST_HEAD(dst);
  27788. + int count;
  27789. - local_irq_save(flags);
  27790. + cpu_lock_irqsave(cpu, flags);
  27791. pset = per_cpu_ptr(zone->pageset, cpu);
  27792. pcp = &pset->pcp;
  27793. - if (pcp->count) {
  27794. - free_pcppages_bulk(zone, pcp->count, pcp);
  27795. + count = pcp->count;
  27796. + if (count) {
  27797. + isolate_pcp_pages(count, pcp, &dst);
  27798. pcp->count = 0;
  27799. }
  27800. - local_irq_restore(flags);
  27801. + cpu_unlock_irqrestore(cpu, flags);
  27802. + if (count)
  27803. + free_pcppages_bulk(zone, count, &dst);
  27804. }
  27805. /*
  27806. @@ -1999,8 +2044,17 @@ void drain_all_pages(struct zone *zone)
  27807. else
  27808. cpumask_clear_cpu(cpu, &cpus_with_pcps);
  27809. }
  27810. +#ifndef CONFIG_PREEMPT_RT_BASE
  27811. on_each_cpu_mask(&cpus_with_pcps, (smp_call_func_t) drain_local_pages,
  27812. zone, 1);
  27813. +#else
  27814. + for_each_cpu(cpu, &cpus_with_pcps) {
  27815. + if (zone)
  27816. + drain_pages_zone(cpu, zone);
  27817. + else
  27818. + drain_pages(cpu);
  27819. + }
  27820. +#endif
  27821. }
  27822. #ifdef CONFIG_HIBERNATION
  27823. @@ -2056,7 +2110,7 @@ void free_hot_cold_page(struct page *page, bool cold)
  27824. migratetype = get_pfnblock_migratetype(page, pfn);
  27825. set_pcppage_migratetype(page, migratetype);
  27826. - local_irq_save(flags);
  27827. + local_lock_irqsave(pa_lock, flags);
  27828. __count_vm_event(PGFREE);
  27829. /*
  27830. @@ -2082,12 +2136,17 @@ void free_hot_cold_page(struct page *page, bool cold)
  27831. pcp->count++;
  27832. if (pcp->count >= pcp->high) {
  27833. unsigned long batch = READ_ONCE(pcp->batch);
  27834. - free_pcppages_bulk(zone, batch, pcp);
  27835. + LIST_HEAD(dst);
  27836. +
  27837. + isolate_pcp_pages(batch, pcp, &dst);
  27838. pcp->count -= batch;
  27839. + local_unlock_irqrestore(pa_lock, flags);
  27840. + free_pcppages_bulk(zone, batch, &dst);
  27841. + return;
  27842. }
  27843. out:
  27844. - local_irq_restore(flags);
  27845. + local_unlock_irqrestore(pa_lock, flags);
  27846. }
  27847. /*
  27848. @@ -2222,7 +2281,7 @@ struct page *buffered_rmqueue(struct zone *preferred_zone,
  27849. struct per_cpu_pages *pcp;
  27850. struct list_head *list;
  27851. - local_irq_save(flags);
  27852. + local_lock_irqsave(pa_lock, flags);
  27853. pcp = &this_cpu_ptr(zone->pageset)->pcp;
  27854. list = &pcp->lists[migratetype];
  27855. if (list_empty(list)) {
  27856. @@ -2254,7 +2313,7 @@ struct page *buffered_rmqueue(struct zone *preferred_zone,
  27857. */
  27858. WARN_ON_ONCE(order > 1);
  27859. }
  27860. - spin_lock_irqsave(&zone->lock, flags);
  27861. + local_spin_lock_irqsave(pa_lock, &zone->lock, flags);
  27862. page = NULL;
  27863. if (alloc_flags & ALLOC_HARDER) {
  27864. @@ -2264,11 +2323,13 @@ struct page *buffered_rmqueue(struct zone *preferred_zone,
  27865. }
  27866. if (!page)
  27867. page = __rmqueue(zone, order, migratetype, gfp_flags);
  27868. - spin_unlock(&zone->lock);
  27869. - if (!page)
  27870. + if (!page) {
  27871. + spin_unlock(&zone->lock);
  27872. goto failed;
  27873. + }
  27874. __mod_zone_freepage_state(zone, -(1 << order),
  27875. get_pcppage_migratetype(page));
  27876. + spin_unlock(&zone->lock);
  27877. }
  27878. __mod_zone_page_state(zone, NR_ALLOC_BATCH, -(1 << order));
  27879. @@ -2278,13 +2339,13 @@ struct page *buffered_rmqueue(struct zone *preferred_zone,
  27880. __count_zone_vm_events(PGALLOC, zone, 1 << order);
  27881. zone_statistics(preferred_zone, zone, gfp_flags);
  27882. - local_irq_restore(flags);
  27883. + local_unlock_irqrestore(pa_lock, flags);
  27884. VM_BUG_ON_PAGE(bad_range(zone, page), page);
  27885. return page;
  27886. failed:
  27887. - local_irq_restore(flags);
  27888. + local_unlock_irqrestore(pa_lock, flags);
  27889. return NULL;
  27890. }
  27891. @@ -5953,6 +6014,7 @@ static int page_alloc_cpu_notify(struct notifier_block *self,
  27892. void __init page_alloc_init(void)
  27893. {
  27894. hotcpu_notifier(page_alloc_cpu_notify, 0);
  27895. + local_irq_lock_init(pa_lock);
  27896. }
  27897. /*
  27898. @@ -6847,7 +6909,7 @@ void zone_pcp_reset(struct zone *zone)
  27899. struct per_cpu_pageset *pset;
  27900. /* avoid races with drain_pages() */
  27901. - local_irq_save(flags);
  27902. + local_lock_irqsave(pa_lock, flags);
  27903. if (zone->pageset != &boot_pageset) {
  27904. for_each_online_cpu(cpu) {
  27905. pset = per_cpu_ptr(zone->pageset, cpu);
  27906. @@ -6856,7 +6918,7 @@ void zone_pcp_reset(struct zone *zone)
  27907. free_percpu(zone->pageset);
  27908. zone->pageset = &boot_pageset;
  27909. }
  27910. - local_irq_restore(flags);
  27911. + local_unlock_irqrestore(pa_lock, flags);
  27912. }
  27913. #ifdef CONFIG_MEMORY_HOTREMOVE
  27914. diff --git a/mm/percpu.c b/mm/percpu.c
  27915. index ef6353f0adbd..33ccbac7cdb8 100644
  27916. --- a/mm/percpu.c
  27917. +++ b/mm/percpu.c
  27918. @@ -1285,18 +1285,7 @@ void free_percpu(void __percpu *ptr)
  27919. }
  27920. EXPORT_SYMBOL_GPL(free_percpu);
  27921. -/**
  27922. - * is_kernel_percpu_address - test whether address is from static percpu area
  27923. - * @addr: address to test
  27924. - *
  27925. - * Test whether @addr belongs to in-kernel static percpu area. Module
  27926. - * static percpu areas are not considered. For those, use
  27927. - * is_module_percpu_address().
  27928. - *
  27929. - * RETURNS:
  27930. - * %true if @addr is from in-kernel static percpu area, %false otherwise.
  27931. - */
  27932. -bool is_kernel_percpu_address(unsigned long addr)
  27933. +bool __is_kernel_percpu_address(unsigned long addr, unsigned long *can_addr)
  27934. {
  27935. #ifdef CONFIG_SMP
  27936. const size_t static_size = __per_cpu_end - __per_cpu_start;
  27937. @@ -1305,16 +1294,36 @@ bool is_kernel_percpu_address(unsigned long addr)
  27938. for_each_possible_cpu(cpu) {
  27939. void *start = per_cpu_ptr(base, cpu);
  27940. + void *va = (void *)addr;
  27941. - if ((void *)addr >= start && (void *)addr < start + static_size)
  27942. + if (va >= start && va < start + static_size) {
  27943. + if (can_addr)
  27944. + *can_addr = (unsigned long) (va - start);
  27945. return true;
  27946. - }
  27947. + }
  27948. + }
  27949. #endif
  27950. /* on UP, can't distinguish from other static vars, always false */
  27951. return false;
  27952. }
  27953. /**
  27954. + * is_kernel_percpu_address - test whether address is from static percpu area
  27955. + * @addr: address to test
  27956. + *
  27957. + * Test whether @addr belongs to in-kernel static percpu area. Module
  27958. + * static percpu areas are not considered. For those, use
  27959. + * is_module_percpu_address().
  27960. + *
  27961. + * RETURNS:
  27962. + * %true if @addr is from in-kernel static percpu area, %false otherwise.
  27963. + */
  27964. +bool is_kernel_percpu_address(unsigned long addr)
  27965. +{
  27966. + return __is_kernel_percpu_address(addr, NULL);
  27967. +}
  27968. +
  27969. +/**
  27970. * per_cpu_ptr_to_phys - convert translated percpu address to physical address
  27971. * @addr: the address to be converted to physical address
  27972. *
  27973. diff --git a/mm/slab.h b/mm/slab.h
  27974. index 7b6087197997..afdc57941179 100644
  27975. --- a/mm/slab.h
  27976. +++ b/mm/slab.h
  27977. @@ -324,7 +324,11 @@ static inline struct kmem_cache *cache_from_obj(struct kmem_cache *s, void *x)
  27978. * The slab lists for all objects.
  27979. */
  27980. struct kmem_cache_node {
  27981. +#ifdef CONFIG_SLUB
  27982. + raw_spinlock_t list_lock;
  27983. +#else
  27984. spinlock_t list_lock;
  27985. +#endif
  27986. #ifdef CONFIG_SLAB
  27987. struct list_head slabs_partial; /* partial list first, better asm code */
  27988. diff --git a/mm/slub.c b/mm/slub.c
  27989. index 65d5f92d51d2..feb4a445a546 100644
  27990. --- a/mm/slub.c
  27991. +++ b/mm/slub.c
  27992. @@ -1075,7 +1075,7 @@ static noinline struct kmem_cache_node *free_debug_processing(
  27993. void *object = head;
  27994. int cnt = 0;
  27995. - spin_lock_irqsave(&n->list_lock, *flags);
  27996. + raw_spin_lock_irqsave(&n->list_lock, *flags);
  27997. slab_lock(page);
  27998. if (!check_slab(s, page))
  27999. @@ -1136,7 +1136,7 @@ out:
  28000. fail:
  28001. slab_unlock(page);
  28002. - spin_unlock_irqrestore(&n->list_lock, *flags);
  28003. + raw_spin_unlock_irqrestore(&n->list_lock, *flags);
  28004. slab_fix(s, "Object at 0x%p not freed", object);
  28005. return NULL;
  28006. }
  28007. @@ -1263,6 +1263,12 @@ static inline void dec_slabs_node(struct kmem_cache *s, int node,
  28008. #endif /* CONFIG_SLUB_DEBUG */
  28009. +struct slub_free_list {
  28010. + raw_spinlock_t lock;
  28011. + struct list_head list;
  28012. +};
  28013. +static DEFINE_PER_CPU(struct slub_free_list, slub_free_list);
  28014. +
  28015. /*
  28016. * Hooks for other subsystems that check memory allocations. In a typical
  28017. * production configuration these hooks all should produce no code at all.
  28018. @@ -1399,10 +1405,17 @@ static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
  28019. gfp_t alloc_gfp;
  28020. void *start, *p;
  28021. int idx, order;
  28022. + bool enableirqs = false;
  28023. flags &= gfp_allowed_mask;
  28024. if (gfpflags_allow_blocking(flags))
  28025. + enableirqs = true;
  28026. +#ifdef CONFIG_PREEMPT_RT_FULL
  28027. + if (system_state == SYSTEM_RUNNING)
  28028. + enableirqs = true;
  28029. +#endif
  28030. + if (enableirqs)
  28031. local_irq_enable();
  28032. flags |= s->allocflags;
  28033. @@ -1473,7 +1486,7 @@ static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
  28034. page->frozen = 1;
  28035. out:
  28036. - if (gfpflags_allow_blocking(flags))
  28037. + if (enableirqs)
  28038. local_irq_disable();
  28039. if (!page)
  28040. return NULL;
  28041. @@ -1529,6 +1542,16 @@ static void __free_slab(struct kmem_cache *s, struct page *page)
  28042. __free_kmem_pages(page, order);
  28043. }
  28044. +static void free_delayed(struct list_head *h)
  28045. +{
  28046. + while(!list_empty(h)) {
  28047. + struct page *page = list_first_entry(h, struct page, lru);
  28048. +
  28049. + list_del(&page->lru);
  28050. + __free_slab(page->slab_cache, page);
  28051. + }
  28052. +}
  28053. +
  28054. #define need_reserve_slab_rcu \
  28055. (sizeof(((struct page *)NULL)->lru) < sizeof(struct rcu_head))
  28056. @@ -1560,6 +1583,12 @@ static void free_slab(struct kmem_cache *s, struct page *page)
  28057. }
  28058. call_rcu(head, rcu_free_slab);
  28059. + } else if (irqs_disabled()) {
  28060. + struct slub_free_list *f = this_cpu_ptr(&slub_free_list);
  28061. +
  28062. + raw_spin_lock(&f->lock);
  28063. + list_add(&page->lru, &f->list);
  28064. + raw_spin_unlock(&f->lock);
  28065. } else
  28066. __free_slab(s, page);
  28067. }
  28068. @@ -1673,7 +1702,7 @@ static void *get_partial_node(struct kmem_cache *s, struct kmem_cache_node *n,
  28069. if (!n || !n->nr_partial)
  28070. return NULL;
  28071. - spin_lock(&n->list_lock);
  28072. + raw_spin_lock(&n->list_lock);
  28073. list_for_each_entry_safe(page, page2, &n->partial, lru) {
  28074. void *t;
  28075. @@ -1698,7 +1727,7 @@ static void *get_partial_node(struct kmem_cache *s, struct kmem_cache_node *n,
  28076. break;
  28077. }
  28078. - spin_unlock(&n->list_lock);
  28079. + raw_spin_unlock(&n->list_lock);
  28080. return object;
  28081. }
  28082. @@ -1944,7 +1973,7 @@ redo:
  28083. * that acquire_slab() will see a slab page that
  28084. * is frozen
  28085. */
  28086. - spin_lock(&n->list_lock);
  28087. + raw_spin_lock(&n->list_lock);
  28088. }
  28089. } else {
  28090. m = M_FULL;
  28091. @@ -1955,7 +1984,7 @@ redo:
  28092. * slabs from diagnostic functions will not see
  28093. * any frozen slabs.
  28094. */
  28095. - spin_lock(&n->list_lock);
  28096. + raw_spin_lock(&n->list_lock);
  28097. }
  28098. }
  28099. @@ -1990,7 +2019,7 @@ redo:
  28100. goto redo;
  28101. if (lock)
  28102. - spin_unlock(&n->list_lock);
  28103. + raw_spin_unlock(&n->list_lock);
  28104. if (m == M_FREE) {
  28105. stat(s, DEACTIVATE_EMPTY);
  28106. @@ -2022,10 +2051,10 @@ static void unfreeze_partials(struct kmem_cache *s,
  28107. n2 = get_node(s, page_to_nid(page));
  28108. if (n != n2) {
  28109. if (n)
  28110. - spin_unlock(&n->list_lock);
  28111. + raw_spin_unlock(&n->list_lock);
  28112. n = n2;
  28113. - spin_lock(&n->list_lock);
  28114. + raw_spin_lock(&n->list_lock);
  28115. }
  28116. do {
  28117. @@ -2054,7 +2083,7 @@ static void unfreeze_partials(struct kmem_cache *s,
  28118. }
  28119. if (n)
  28120. - spin_unlock(&n->list_lock);
  28121. + raw_spin_unlock(&n->list_lock);
  28122. while (discard_page) {
  28123. page = discard_page;
  28124. @@ -2093,14 +2122,21 @@ static void put_cpu_partial(struct kmem_cache *s, struct page *page, int drain)
  28125. pobjects = oldpage->pobjects;
  28126. pages = oldpage->pages;
  28127. if (drain && pobjects > s->cpu_partial) {
  28128. + struct slub_free_list *f;
  28129. unsigned long flags;
  28130. + LIST_HEAD(tofree);
  28131. /*
  28132. * partial array is full. Move the existing
  28133. * set to the per node partial list.
  28134. */
  28135. local_irq_save(flags);
  28136. unfreeze_partials(s, this_cpu_ptr(s->cpu_slab));
  28137. + f = this_cpu_ptr(&slub_free_list);
  28138. + raw_spin_lock(&f->lock);
  28139. + list_splice_init(&f->list, &tofree);
  28140. + raw_spin_unlock(&f->lock);
  28141. local_irq_restore(flags);
  28142. + free_delayed(&tofree);
  28143. oldpage = NULL;
  28144. pobjects = 0;
  28145. pages = 0;
  28146. @@ -2172,7 +2208,22 @@ static bool has_cpu_slab(int cpu, void *info)
  28147. static void flush_all(struct kmem_cache *s)
  28148. {
  28149. + LIST_HEAD(tofree);
  28150. + int cpu;
  28151. +
  28152. on_each_cpu_cond(has_cpu_slab, flush_cpu_slab, s, 1, GFP_ATOMIC);
  28153. + for_each_online_cpu(cpu) {
  28154. + struct slub_free_list *f;
  28155. +
  28156. + if (!has_cpu_slab(cpu, s))
  28157. + continue;
  28158. +
  28159. + f = &per_cpu(slub_free_list, cpu);
  28160. + raw_spin_lock_irq(&f->lock);
  28161. + list_splice_init(&f->list, &tofree);
  28162. + raw_spin_unlock_irq(&f->lock);
  28163. + free_delayed(&tofree);
  28164. + }
  28165. }
  28166. /*
  28167. @@ -2208,10 +2259,10 @@ static unsigned long count_partial(struct kmem_cache_node *n,
  28168. unsigned long x = 0;
  28169. struct page *page;
  28170. - spin_lock_irqsave(&n->list_lock, flags);
  28171. + raw_spin_lock_irqsave(&n->list_lock, flags);
  28172. list_for_each_entry(page, &n->partial, lru)
  28173. x += get_count(page);
  28174. - spin_unlock_irqrestore(&n->list_lock, flags);
  28175. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  28176. return x;
  28177. }
  28178. #endif /* CONFIG_SLUB_DEBUG || CONFIG_SYSFS */
  28179. @@ -2349,8 +2400,10 @@ static inline void *get_freelist(struct kmem_cache *s, struct page *page)
  28180. * already disabled (which is the case for bulk allocation).
  28181. */
  28182. static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
  28183. - unsigned long addr, struct kmem_cache_cpu *c)
  28184. + unsigned long addr, struct kmem_cache_cpu *c,
  28185. + struct list_head *to_free)
  28186. {
  28187. + struct slub_free_list *f;
  28188. void *freelist;
  28189. struct page *page;
  28190. @@ -2410,6 +2463,13 @@ load_freelist:
  28191. VM_BUG_ON(!c->page->frozen);
  28192. c->freelist = get_freepointer(s, freelist);
  28193. c->tid = next_tid(c->tid);
  28194. +
  28195. +out:
  28196. + f = this_cpu_ptr(&slub_free_list);
  28197. + raw_spin_lock(&f->lock);
  28198. + list_splice_init(&f->list, to_free);
  28199. + raw_spin_unlock(&f->lock);
  28200. +
  28201. return freelist;
  28202. new_slab:
  28203. @@ -2441,7 +2501,7 @@ new_slab:
  28204. deactivate_slab(s, page, get_freepointer(s, freelist));
  28205. c->page = NULL;
  28206. c->freelist = NULL;
  28207. - return freelist;
  28208. + goto out;
  28209. }
  28210. /*
  28211. @@ -2453,6 +2513,7 @@ static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
  28212. {
  28213. void *p;
  28214. unsigned long flags;
  28215. + LIST_HEAD(tofree);
  28216. local_irq_save(flags);
  28217. #ifdef CONFIG_PREEMPT
  28218. @@ -2464,8 +2525,9 @@ static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
  28219. c = this_cpu_ptr(s->cpu_slab);
  28220. #endif
  28221. - p = ___slab_alloc(s, gfpflags, node, addr, c);
  28222. + p = ___slab_alloc(s, gfpflags, node, addr, c, &tofree);
  28223. local_irq_restore(flags);
  28224. + free_delayed(&tofree);
  28225. return p;
  28226. }
  28227. @@ -2652,7 +2714,7 @@ static void __slab_free(struct kmem_cache *s, struct page *page,
  28228. do {
  28229. if (unlikely(n)) {
  28230. - spin_unlock_irqrestore(&n->list_lock, flags);
  28231. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  28232. n = NULL;
  28233. }
  28234. prior = page->freelist;
  28235. @@ -2684,7 +2746,7 @@ static void __slab_free(struct kmem_cache *s, struct page *page,
  28236. * Otherwise the list_lock will synchronize with
  28237. * other processors updating the list of slabs.
  28238. */
  28239. - spin_lock_irqsave(&n->list_lock, flags);
  28240. + raw_spin_lock_irqsave(&n->list_lock, flags);
  28241. }
  28242. }
  28243. @@ -2726,7 +2788,7 @@ static void __slab_free(struct kmem_cache *s, struct page *page,
  28244. add_partial(n, page, DEACTIVATE_TO_TAIL);
  28245. stat(s, FREE_ADD_PARTIAL);
  28246. }
  28247. - spin_unlock_irqrestore(&n->list_lock, flags);
  28248. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  28249. return;
  28250. slab_empty:
  28251. @@ -2741,7 +2803,7 @@ slab_empty:
  28252. remove_full(s, n, page);
  28253. }
  28254. - spin_unlock_irqrestore(&n->list_lock, flags);
  28255. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  28256. stat(s, FREE_SLAB);
  28257. discard_slab(s, page);
  28258. }
  28259. @@ -2913,6 +2975,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
  28260. void **p)
  28261. {
  28262. struct kmem_cache_cpu *c;
  28263. + LIST_HEAD(to_free);
  28264. int i;
  28265. /* memcg and kmem_cache debug support */
  28266. @@ -2936,7 +2999,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
  28267. * of re-populating per CPU c->freelist
  28268. */
  28269. p[i] = ___slab_alloc(s, flags, NUMA_NO_NODE,
  28270. - _RET_IP_, c);
  28271. + _RET_IP_, c, &to_free);
  28272. if (unlikely(!p[i]))
  28273. goto error;
  28274. @@ -2948,6 +3011,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
  28275. }
  28276. c->tid = next_tid(c->tid);
  28277. local_irq_enable();
  28278. + free_delayed(&to_free);
  28279. /* Clear memory outside IRQ disabled fastpath loop */
  28280. if (unlikely(flags & __GFP_ZERO)) {
  28281. @@ -3095,7 +3159,7 @@ static void
  28282. init_kmem_cache_node(struct kmem_cache_node *n)
  28283. {
  28284. n->nr_partial = 0;
  28285. - spin_lock_init(&n->list_lock);
  28286. + raw_spin_lock_init(&n->list_lock);
  28287. INIT_LIST_HEAD(&n->partial);
  28288. #ifdef CONFIG_SLUB_DEBUG
  28289. atomic_long_set(&n->nr_slabs, 0);
  28290. @@ -3677,7 +3741,7 @@ int __kmem_cache_shrink(struct kmem_cache *s, bool deactivate)
  28291. for (i = 0; i < SHRINK_PROMOTE_MAX; i++)
  28292. INIT_LIST_HEAD(promote + i);
  28293. - spin_lock_irqsave(&n->list_lock, flags);
  28294. + raw_spin_lock_irqsave(&n->list_lock, flags);
  28295. /*
  28296. * Build lists of slabs to discard or promote.
  28297. @@ -3708,7 +3772,7 @@ int __kmem_cache_shrink(struct kmem_cache *s, bool deactivate)
  28298. for (i = SHRINK_PROMOTE_MAX - 1; i >= 0; i--)
  28299. list_splice(promote + i, &n->partial);
  28300. - spin_unlock_irqrestore(&n->list_lock, flags);
  28301. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  28302. /* Release empty slabs */
  28303. list_for_each_entry_safe(page, t, &discard, lru)
  28304. @@ -3884,6 +3948,12 @@ void __init kmem_cache_init(void)
  28305. {
  28306. static __initdata struct kmem_cache boot_kmem_cache,
  28307. boot_kmem_cache_node;
  28308. + int cpu;
  28309. +
  28310. + for_each_possible_cpu(cpu) {
  28311. + raw_spin_lock_init(&per_cpu(slub_free_list, cpu).lock);
  28312. + INIT_LIST_HEAD(&per_cpu(slub_free_list, cpu).list);
  28313. + }
  28314. if (debug_guardpage_minorder())
  28315. slub_max_order = 0;
  28316. @@ -4127,7 +4197,7 @@ static int validate_slab_node(struct kmem_cache *s,
  28317. struct page *page;
  28318. unsigned long flags;
  28319. - spin_lock_irqsave(&n->list_lock, flags);
  28320. + raw_spin_lock_irqsave(&n->list_lock, flags);
  28321. list_for_each_entry(page, &n->partial, lru) {
  28322. validate_slab_slab(s, page, map);
  28323. @@ -4149,7 +4219,7 @@ static int validate_slab_node(struct kmem_cache *s,
  28324. s->name, count, atomic_long_read(&n->nr_slabs));
  28325. out:
  28326. - spin_unlock_irqrestore(&n->list_lock, flags);
  28327. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  28328. return count;
  28329. }
  28330. @@ -4337,12 +4407,12 @@ static int list_locations(struct kmem_cache *s, char *buf,
  28331. if (!atomic_long_read(&n->nr_slabs))
  28332. continue;
  28333. - spin_lock_irqsave(&n->list_lock, flags);
  28334. + raw_spin_lock_irqsave(&n->list_lock, flags);
  28335. list_for_each_entry(page, &n->partial, lru)
  28336. process_slab(&t, s, page, alloc, map);
  28337. list_for_each_entry(page, &n->full, lru)
  28338. process_slab(&t, s, page, alloc, map);
  28339. - spin_unlock_irqrestore(&n->list_lock, flags);
  28340. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  28341. }
  28342. for (i = 0; i < t.count; i++) {
  28343. diff --git a/mm/swap.c b/mm/swap.c
  28344. index 39395fb549c0..ad16649221d7 100644
  28345. --- a/mm/swap.c
  28346. +++ b/mm/swap.c
  28347. @@ -31,6 +31,7 @@
  28348. #include <linux/memcontrol.h>
  28349. #include <linux/gfp.h>
  28350. #include <linux/uio.h>
  28351. +#include <linux/locallock.h>
  28352. #include <linux/hugetlb.h>
  28353. #include <linux/page_idle.h>
  28354. @@ -46,6 +47,9 @@ static DEFINE_PER_CPU(struct pagevec, lru_add_pvec);
  28355. static DEFINE_PER_CPU(struct pagevec, lru_rotate_pvecs);
  28356. static DEFINE_PER_CPU(struct pagevec, lru_deactivate_file_pvecs);
  28357. +static DEFINE_LOCAL_IRQ_LOCK(rotate_lock);
  28358. +DEFINE_LOCAL_IRQ_LOCK(swapvec_lock);
  28359. +
  28360. /*
  28361. * This path almost never happens for VM activity - pages are normally
  28362. * freed via pagevecs. But it gets used by networking.
  28363. @@ -481,11 +485,11 @@ void rotate_reclaimable_page(struct page *page)
  28364. unsigned long flags;
  28365. page_cache_get(page);
  28366. - local_irq_save(flags);
  28367. + local_lock_irqsave(rotate_lock, flags);
  28368. pvec = this_cpu_ptr(&lru_rotate_pvecs);
  28369. if (!pagevec_add(pvec, page))
  28370. pagevec_move_tail(pvec);
  28371. - local_irq_restore(flags);
  28372. + local_unlock_irqrestore(rotate_lock, flags);
  28373. }
  28374. }
  28375. @@ -536,12 +540,13 @@ static bool need_activate_page_drain(int cpu)
  28376. void activate_page(struct page *page)
  28377. {
  28378. if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
  28379. - struct pagevec *pvec = &get_cpu_var(activate_page_pvecs);
  28380. + struct pagevec *pvec = &get_locked_var(swapvec_lock,
  28381. + activate_page_pvecs);
  28382. page_cache_get(page);
  28383. if (!pagevec_add(pvec, page))
  28384. pagevec_lru_move_fn(pvec, __activate_page, NULL);
  28385. - put_cpu_var(activate_page_pvecs);
  28386. + put_locked_var(swapvec_lock, activate_page_pvecs);
  28387. }
  28388. }
  28389. @@ -567,7 +572,7 @@ void activate_page(struct page *page)
  28390. static void __lru_cache_activate_page(struct page *page)
  28391. {
  28392. - struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
  28393. + struct pagevec *pvec = &get_locked_var(swapvec_lock, lru_add_pvec);
  28394. int i;
  28395. /*
  28396. @@ -589,7 +594,7 @@ static void __lru_cache_activate_page(struct page *page)
  28397. }
  28398. }
  28399. - put_cpu_var(lru_add_pvec);
  28400. + put_locked_var(swapvec_lock, lru_add_pvec);
  28401. }
  28402. /*
  28403. @@ -630,13 +635,13 @@ EXPORT_SYMBOL(mark_page_accessed);
  28404. static void __lru_cache_add(struct page *page)
  28405. {
  28406. - struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
  28407. + struct pagevec *pvec = &get_locked_var(swapvec_lock, lru_add_pvec);
  28408. page_cache_get(page);
  28409. if (!pagevec_space(pvec))
  28410. __pagevec_lru_add(pvec);
  28411. pagevec_add(pvec, page);
  28412. - put_cpu_var(lru_add_pvec);
  28413. + put_locked_var(swapvec_lock, lru_add_pvec);
  28414. }
  28415. /**
  28416. @@ -816,9 +821,15 @@ void lru_add_drain_cpu(int cpu)
  28417. unsigned long flags;
  28418. /* No harm done if a racing interrupt already did this */
  28419. - local_irq_save(flags);
  28420. +#ifdef CONFIG_PREEMPT_RT_BASE
  28421. + local_lock_irqsave_on(rotate_lock, flags, cpu);
  28422. + pagevec_move_tail(pvec);
  28423. + local_unlock_irqrestore_on(rotate_lock, flags, cpu);
  28424. +#else
  28425. + local_lock_irqsave(rotate_lock, flags);
  28426. pagevec_move_tail(pvec);
  28427. - local_irq_restore(flags);
  28428. + local_unlock_irqrestore(rotate_lock, flags);
  28429. +#endif
  28430. }
  28431. pvec = &per_cpu(lru_deactivate_file_pvecs, cpu);
  28432. @@ -846,26 +857,47 @@ void deactivate_file_page(struct page *page)
  28433. return;
  28434. if (likely(get_page_unless_zero(page))) {
  28435. - struct pagevec *pvec = &get_cpu_var(lru_deactivate_file_pvecs);
  28436. + struct pagevec *pvec = &get_locked_var(swapvec_lock,
  28437. + lru_deactivate_file_pvecs);
  28438. if (!pagevec_add(pvec, page))
  28439. pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
  28440. - put_cpu_var(lru_deactivate_file_pvecs);
  28441. + put_locked_var(swapvec_lock, lru_deactivate_file_pvecs);
  28442. }
  28443. }
  28444. void lru_add_drain(void)
  28445. {
  28446. - lru_add_drain_cpu(get_cpu());
  28447. - put_cpu();
  28448. + lru_add_drain_cpu(local_lock_cpu(swapvec_lock));
  28449. + local_unlock_cpu(swapvec_lock);
  28450. }
  28451. +
  28452. +#ifdef CONFIG_PREEMPT_RT_BASE
  28453. +static inline void remote_lru_add_drain(int cpu, struct cpumask *has_work)
  28454. +{
  28455. + local_lock_on(swapvec_lock, cpu);
  28456. + lru_add_drain_cpu(cpu);
  28457. + local_unlock_on(swapvec_lock, cpu);
  28458. +}
  28459. +
  28460. +#else
  28461. +
  28462. static void lru_add_drain_per_cpu(struct work_struct *dummy)
  28463. {
  28464. lru_add_drain();
  28465. }
  28466. static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);
  28467. +static inline void remote_lru_add_drain(int cpu, struct cpumask *has_work)
  28468. +{
  28469. + struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
  28470. +
  28471. + INIT_WORK(work, lru_add_drain_per_cpu);
  28472. + schedule_work_on(cpu, work);
  28473. + cpumask_set_cpu(cpu, has_work);
  28474. +}
  28475. +#endif
  28476. void lru_add_drain_all(void)
  28477. {
  28478. @@ -878,20 +910,17 @@ void lru_add_drain_all(void)
  28479. cpumask_clear(&has_work);
  28480. for_each_online_cpu(cpu) {
  28481. - struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
  28482. -
  28483. if (pagevec_count(&per_cpu(lru_add_pvec, cpu)) ||
  28484. pagevec_count(&per_cpu(lru_rotate_pvecs, cpu)) ||
  28485. pagevec_count(&per_cpu(lru_deactivate_file_pvecs, cpu)) ||
  28486. - need_activate_page_drain(cpu)) {
  28487. - INIT_WORK(work, lru_add_drain_per_cpu);
  28488. - schedule_work_on(cpu, work);
  28489. - cpumask_set_cpu(cpu, &has_work);
  28490. - }
  28491. + need_activate_page_drain(cpu))
  28492. + remote_lru_add_drain(cpu, &has_work);
  28493. }
  28494. +#ifndef CONFIG_PREEMPT_RT_BASE
  28495. for_each_cpu(cpu, &has_work)
  28496. flush_work(&per_cpu(lru_add_drain_work, cpu));
  28497. +#endif
  28498. put_online_cpus();
  28499. mutex_unlock(&lock);
  28500. diff --git a/mm/truncate.c b/mm/truncate.c
  28501. index 76e35ad97102..5f196420020c 100644
  28502. --- a/mm/truncate.c
  28503. +++ b/mm/truncate.c
  28504. @@ -56,8 +56,11 @@ static void clear_exceptional_entry(struct address_space *mapping,
  28505. * protected by mapping->tree_lock.
  28506. */
  28507. if (!workingset_node_shadows(node) &&
  28508. - !list_empty(&node->private_list))
  28509. - list_lru_del(&workingset_shadow_nodes, &node->private_list);
  28510. + !list_empty(&node->private_list)) {
  28511. + local_lock(workingset_shadow_lock);
  28512. + list_lru_del(&__workingset_shadow_nodes, &node->private_list);
  28513. + local_unlock(workingset_shadow_lock);
  28514. + }
  28515. __radix_tree_delete_node(&mapping->page_tree, node);
  28516. unlock:
  28517. spin_unlock_irq(&mapping->tree_lock);
  28518. diff --git a/mm/vmalloc.c b/mm/vmalloc.c
  28519. index 8e3c9c5a3042..68740314ad54 100644
  28520. --- a/mm/vmalloc.c
  28521. +++ b/mm/vmalloc.c
  28522. @@ -821,7 +821,7 @@ static void *new_vmap_block(unsigned int order, gfp_t gfp_mask)
  28523. struct vmap_block *vb;
  28524. struct vmap_area *va;
  28525. unsigned long vb_idx;
  28526. - int node, err;
  28527. + int node, err, cpu;
  28528. void *vaddr;
  28529. node = numa_node_id();
  28530. @@ -864,11 +864,12 @@ static void *new_vmap_block(unsigned int order, gfp_t gfp_mask)
  28531. BUG_ON(err);
  28532. radix_tree_preload_end();
  28533. - vbq = &get_cpu_var(vmap_block_queue);
  28534. + cpu = get_cpu_light();
  28535. + vbq = this_cpu_ptr(&vmap_block_queue);
  28536. spin_lock(&vbq->lock);
  28537. list_add_tail_rcu(&vb->free_list, &vbq->free);
  28538. spin_unlock(&vbq->lock);
  28539. - put_cpu_var(vmap_block_queue);
  28540. + put_cpu_light();
  28541. return vaddr;
  28542. }
  28543. @@ -937,6 +938,7 @@ static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
  28544. struct vmap_block *vb;
  28545. void *vaddr = NULL;
  28546. unsigned int order;
  28547. + int cpu;
  28548. BUG_ON(offset_in_page(size));
  28549. BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
  28550. @@ -951,7 +953,8 @@ static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
  28551. order = get_order(size);
  28552. rcu_read_lock();
  28553. - vbq = &get_cpu_var(vmap_block_queue);
  28554. + cpu = get_cpu_light();
  28555. + vbq = this_cpu_ptr(&vmap_block_queue);
  28556. list_for_each_entry_rcu(vb, &vbq->free, free_list) {
  28557. unsigned long pages_off;
  28558. @@ -974,7 +977,7 @@ static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
  28559. break;
  28560. }
  28561. - put_cpu_var(vmap_block_queue);
  28562. + put_cpu_light();
  28563. rcu_read_unlock();
  28564. /* Allocate new block if nothing was found */
  28565. diff --git a/mm/vmstat.c b/mm/vmstat.c
  28566. index c54fd2924f25..64416fd7c209 100644
  28567. --- a/mm/vmstat.c
  28568. +++ b/mm/vmstat.c
  28569. @@ -226,6 +226,7 @@ void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
  28570. long x;
  28571. long t;
  28572. + preempt_disable_rt();
  28573. x = delta + __this_cpu_read(*p);
  28574. t = __this_cpu_read(pcp->stat_threshold);
  28575. @@ -235,6 +236,7 @@ void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
  28576. x = 0;
  28577. }
  28578. __this_cpu_write(*p, x);
  28579. + preempt_enable_rt();
  28580. }
  28581. EXPORT_SYMBOL(__mod_zone_page_state);
  28582. @@ -267,6 +269,7 @@ void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
  28583. s8 __percpu *p = pcp->vm_stat_diff + item;
  28584. s8 v, t;
  28585. + preempt_disable_rt();
  28586. v = __this_cpu_inc_return(*p);
  28587. t = __this_cpu_read(pcp->stat_threshold);
  28588. if (unlikely(v > t)) {
  28589. @@ -275,6 +278,7 @@ void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
  28590. zone_page_state_add(v + overstep, zone, item);
  28591. __this_cpu_write(*p, -overstep);
  28592. }
  28593. + preempt_enable_rt();
  28594. }
  28595. void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
  28596. @@ -289,6 +293,7 @@ void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
  28597. s8 __percpu *p = pcp->vm_stat_diff + item;
  28598. s8 v, t;
  28599. + preempt_disable_rt();
  28600. v = __this_cpu_dec_return(*p);
  28601. t = __this_cpu_read(pcp->stat_threshold);
  28602. if (unlikely(v < - t)) {
  28603. @@ -297,6 +302,7 @@ void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
  28604. zone_page_state_add(v - overstep, zone, item);
  28605. __this_cpu_write(*p, overstep);
  28606. }
  28607. + preempt_enable_rt();
  28608. }
  28609. void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
  28610. diff --git a/mm/workingset.c b/mm/workingset.c
  28611. index df66f426fdcf..6db7b243fa0d 100644
  28612. --- a/mm/workingset.c
  28613. +++ b/mm/workingset.c
  28614. @@ -264,7 +264,8 @@ void workingset_activation(struct page *page)
  28615. * point where they would still be useful.
  28616. */
  28617. -struct list_lru workingset_shadow_nodes;
  28618. +struct list_lru __workingset_shadow_nodes;
  28619. +DEFINE_LOCAL_IRQ_LOCK(workingset_shadow_lock);
  28620. static unsigned long count_shadow_nodes(struct shrinker *shrinker,
  28621. struct shrink_control *sc)
  28622. @@ -274,9 +275,9 @@ static unsigned long count_shadow_nodes(struct shrinker *shrinker,
  28623. unsigned long pages;
  28624. /* list_lru lock nests inside IRQ-safe mapping->tree_lock */
  28625. - local_irq_disable();
  28626. - shadow_nodes = list_lru_shrink_count(&workingset_shadow_nodes, sc);
  28627. - local_irq_enable();
  28628. + local_lock_irq(workingset_shadow_lock);
  28629. + shadow_nodes = list_lru_shrink_count(&__workingset_shadow_nodes, sc);
  28630. + local_unlock_irq(workingset_shadow_lock);
  28631. pages = node_present_pages(sc->nid);
  28632. /*
  28633. @@ -361,9 +362,9 @@ static enum lru_status shadow_lru_isolate(struct list_head *item,
  28634. spin_unlock(&mapping->tree_lock);
  28635. ret = LRU_REMOVED_RETRY;
  28636. out:
  28637. - local_irq_enable();
  28638. + local_unlock_irq(workingset_shadow_lock);
  28639. cond_resched();
  28640. - local_irq_disable();
  28641. + local_lock_irq(workingset_shadow_lock);
  28642. spin_lock(lru_lock);
  28643. return ret;
  28644. }
  28645. @@ -374,10 +375,10 @@ static unsigned long scan_shadow_nodes(struct shrinker *shrinker,
  28646. unsigned long ret;
  28647. /* list_lru lock nests inside IRQ-safe mapping->tree_lock */
  28648. - local_irq_disable();
  28649. - ret = list_lru_shrink_walk(&workingset_shadow_nodes, sc,
  28650. + local_lock_irq(workingset_shadow_lock);
  28651. + ret = list_lru_shrink_walk(&__workingset_shadow_nodes, sc,
  28652. shadow_lru_isolate, NULL);
  28653. - local_irq_enable();
  28654. + local_unlock_irq(workingset_shadow_lock);
  28655. return ret;
  28656. }
  28657. @@ -398,7 +399,7 @@ static int __init workingset_init(void)
  28658. {
  28659. int ret;
  28660. - ret = list_lru_init_key(&workingset_shadow_nodes, &shadow_nodes_key);
  28661. + ret = list_lru_init_key(&__workingset_shadow_nodes, &shadow_nodes_key);
  28662. if (ret)
  28663. goto err;
  28664. ret = register_shrinker(&workingset_shadow_shrinker);
  28665. @@ -406,7 +407,7 @@ static int __init workingset_init(void)
  28666. goto err_list_lru;
  28667. return 0;
  28668. err_list_lru:
  28669. - list_lru_destroy(&workingset_shadow_nodes);
  28670. + list_lru_destroy(&__workingset_shadow_nodes);
  28671. err:
  28672. return ret;
  28673. }
  28674. diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c
  28675. index c1ea19478119..529552c3716d 100644
  28676. --- a/mm/zsmalloc.c
  28677. +++ b/mm/zsmalloc.c
  28678. @@ -64,6 +64,7 @@
  28679. #include <linux/debugfs.h>
  28680. #include <linux/zsmalloc.h>
  28681. #include <linux/zpool.h>
  28682. +#include <linux/locallock.h>
  28683. /*
  28684. * This must be power of 2 and greater than of equal to sizeof(link_free).
  28685. @@ -403,6 +404,7 @@ static unsigned int get_maxobj_per_zspage(int size, int pages_per_zspage)
  28686. /* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
  28687. static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
  28688. +static DEFINE_LOCAL_IRQ_LOCK(zs_map_area_lock);
  28689. static int is_first_page(struct page *page)
  28690. {
  28691. @@ -1289,7 +1291,7 @@ void *zs_map_object(struct zs_pool *pool, unsigned long handle,
  28692. class = pool->size_class[class_idx];
  28693. off = obj_idx_to_offset(page, obj_idx, class->size);
  28694. - area = &get_cpu_var(zs_map_area);
  28695. + area = &get_locked_var(zs_map_area_lock, zs_map_area);
  28696. area->vm_mm = mm;
  28697. if (off + class->size <= PAGE_SIZE) {
  28698. /* this object is contained entirely within a page */
  28699. @@ -1342,7 +1344,7 @@ void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
  28700. __zs_unmap_object(area, pages, off, class->size);
  28701. }
  28702. - put_cpu_var(zs_map_area);
  28703. + put_locked_var(zs_map_area_lock, zs_map_area);
  28704. unpin_tag(handle);
  28705. }
  28706. EXPORT_SYMBOL_GPL(zs_unmap_object);
  28707. diff --git a/net/core/dev.c b/net/core/dev.c
  28708. index 48399d8ce614..2ccc02633e97 100644
  28709. --- a/net/core/dev.c
  28710. +++ b/net/core/dev.c
  28711. @@ -186,6 +186,7 @@ static unsigned int napi_gen_id;
  28712. static DEFINE_HASHTABLE(napi_hash, 8);
  28713. static seqcount_t devnet_rename_seq;
  28714. +static DEFINE_MUTEX(devnet_rename_mutex);
  28715. static inline void dev_base_seq_inc(struct net *net)
  28716. {
  28717. @@ -207,14 +208,14 @@ static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
  28718. static inline void rps_lock(struct softnet_data *sd)
  28719. {
  28720. #ifdef CONFIG_RPS
  28721. - spin_lock(&sd->input_pkt_queue.lock);
  28722. + raw_spin_lock(&sd->input_pkt_queue.raw_lock);
  28723. #endif
  28724. }
  28725. static inline void rps_unlock(struct softnet_data *sd)
  28726. {
  28727. #ifdef CONFIG_RPS
  28728. - spin_unlock(&sd->input_pkt_queue.lock);
  28729. + raw_spin_unlock(&sd->input_pkt_queue.raw_lock);
  28730. #endif
  28731. }
  28732. @@ -884,7 +885,8 @@ retry:
  28733. strcpy(name, dev->name);
  28734. rcu_read_unlock();
  28735. if (read_seqcount_retry(&devnet_rename_seq, seq)) {
  28736. - cond_resched();
  28737. + mutex_lock(&devnet_rename_mutex);
  28738. + mutex_unlock(&devnet_rename_mutex);
  28739. goto retry;
  28740. }
  28741. @@ -1153,20 +1155,17 @@ int dev_change_name(struct net_device *dev, const char *newname)
  28742. if (dev->flags & IFF_UP)
  28743. return -EBUSY;
  28744. - write_seqcount_begin(&devnet_rename_seq);
  28745. + mutex_lock(&devnet_rename_mutex);
  28746. + __raw_write_seqcount_begin(&devnet_rename_seq);
  28747. - if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
  28748. - write_seqcount_end(&devnet_rename_seq);
  28749. - return 0;
  28750. - }
  28751. + if (strncmp(newname, dev->name, IFNAMSIZ) == 0)
  28752. + goto outunlock;
  28753. memcpy(oldname, dev->name, IFNAMSIZ);
  28754. err = dev_get_valid_name(net, dev, newname);
  28755. - if (err < 0) {
  28756. - write_seqcount_end(&devnet_rename_seq);
  28757. - return err;
  28758. - }
  28759. + if (err < 0)
  28760. + goto outunlock;
  28761. if (oldname[0] && !strchr(oldname, '%'))
  28762. netdev_info(dev, "renamed from %s\n", oldname);
  28763. @@ -1179,11 +1178,12 @@ rollback:
  28764. if (ret) {
  28765. memcpy(dev->name, oldname, IFNAMSIZ);
  28766. dev->name_assign_type = old_assign_type;
  28767. - write_seqcount_end(&devnet_rename_seq);
  28768. - return ret;
  28769. + err = ret;
  28770. + goto outunlock;
  28771. }
  28772. - write_seqcount_end(&devnet_rename_seq);
  28773. + __raw_write_seqcount_end(&devnet_rename_seq);
  28774. + mutex_unlock(&devnet_rename_mutex);
  28775. netdev_adjacent_rename_links(dev, oldname);
  28776. @@ -1204,7 +1204,8 @@ rollback:
  28777. /* err >= 0 after dev_alloc_name() or stores the first errno */
  28778. if (err >= 0) {
  28779. err = ret;
  28780. - write_seqcount_begin(&devnet_rename_seq);
  28781. + mutex_lock(&devnet_rename_mutex);
  28782. + __raw_write_seqcount_begin(&devnet_rename_seq);
  28783. memcpy(dev->name, oldname, IFNAMSIZ);
  28784. memcpy(oldname, newname, IFNAMSIZ);
  28785. dev->name_assign_type = old_assign_type;
  28786. @@ -1217,6 +1218,11 @@ rollback:
  28787. }
  28788. return err;
  28789. +
  28790. +outunlock:
  28791. + __raw_write_seqcount_end(&devnet_rename_seq);
  28792. + mutex_unlock(&devnet_rename_mutex);
  28793. + return err;
  28794. }
  28795. /**
  28796. @@ -2268,6 +2274,7 @@ static inline void __netif_reschedule(struct Qdisc *q)
  28797. sd->output_queue_tailp = &q->next_sched;
  28798. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  28799. local_irq_restore(flags);
  28800. + preempt_check_resched_rt();
  28801. }
  28802. void __netif_schedule(struct Qdisc *q)
  28803. @@ -2349,6 +2356,7 @@ void __dev_kfree_skb_irq(struct sk_buff *skb, enum skb_free_reason reason)
  28804. __this_cpu_write(softnet_data.completion_queue, skb);
  28805. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  28806. local_irq_restore(flags);
  28807. + preempt_check_resched_rt();
  28808. }
  28809. EXPORT_SYMBOL(__dev_kfree_skb_irq);
  28810. @@ -2906,7 +2914,11 @@ static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
  28811. * This permits __QDISC___STATE_RUNNING owner to get the lock more
  28812. * often and dequeue packets faster.
  28813. */
  28814. +#ifdef CONFIG_PREEMPT_RT_FULL
  28815. + contended = true;
  28816. +#else
  28817. contended = qdisc_is_running(q);
  28818. +#endif
  28819. if (unlikely(contended))
  28820. spin_lock(&q->busylock);
  28821. @@ -2966,9 +2978,44 @@ static void skb_update_prio(struct sk_buff *skb)
  28822. #define skb_update_prio(skb)
  28823. #endif
  28824. +#ifdef CONFIG_PREEMPT_RT_FULL
  28825. +
  28826. +static inline int xmit_rec_read(void)
  28827. +{
  28828. + return current->xmit_recursion;
  28829. +}
  28830. +
  28831. +static inline void xmit_rec_inc(void)
  28832. +{
  28833. + current->xmit_recursion++;
  28834. +}
  28835. +
  28836. +static inline void xmit_rec_dec(void)
  28837. +{
  28838. + current->xmit_recursion--;
  28839. +}
  28840. +
  28841. +#else
  28842. +
  28843. DEFINE_PER_CPU(int, xmit_recursion);
  28844. EXPORT_SYMBOL(xmit_recursion);
  28845. +static inline int xmit_rec_read(void)
  28846. +{
  28847. + return __this_cpu_read(xmit_recursion);
  28848. +}
  28849. +
  28850. +static inline void xmit_rec_inc(void)
  28851. +{
  28852. + __this_cpu_inc(xmit_recursion);
  28853. +}
  28854. +
  28855. +static inline void xmit_rec_dec(void)
  28856. +{
  28857. + __this_cpu_dec(xmit_recursion);
  28858. +}
  28859. +#endif
  28860. +
  28861. #define RECURSION_LIMIT 10
  28862. /**
  28863. @@ -3161,7 +3208,7 @@ static int __dev_queue_xmit(struct sk_buff *skb, void *accel_priv)
  28864. if (txq->xmit_lock_owner != cpu) {
  28865. - if (__this_cpu_read(xmit_recursion) > RECURSION_LIMIT)
  28866. + if (xmit_rec_read() > RECURSION_LIMIT)
  28867. goto recursion_alert;
  28868. skb = validate_xmit_skb(skb, dev);
  28869. @@ -3171,9 +3218,9 @@ static int __dev_queue_xmit(struct sk_buff *skb, void *accel_priv)
  28870. HARD_TX_LOCK(dev, txq, cpu);
  28871. if (!netif_xmit_stopped(txq)) {
  28872. - __this_cpu_inc(xmit_recursion);
  28873. + xmit_rec_inc();
  28874. skb = dev_hard_start_xmit(skb, dev, txq, &rc);
  28875. - __this_cpu_dec(xmit_recursion);
  28876. + xmit_rec_dec();
  28877. if (dev_xmit_complete(rc)) {
  28878. HARD_TX_UNLOCK(dev, txq);
  28879. goto out;
  28880. @@ -3547,6 +3594,7 @@ drop:
  28881. rps_unlock(sd);
  28882. local_irq_restore(flags);
  28883. + preempt_check_resched_rt();
  28884. atomic_long_inc(&skb->dev->rx_dropped);
  28885. kfree_skb(skb);
  28886. @@ -3565,7 +3613,7 @@ static int netif_rx_internal(struct sk_buff *skb)
  28887. struct rps_dev_flow voidflow, *rflow = &voidflow;
  28888. int cpu;
  28889. - preempt_disable();
  28890. + migrate_disable();
  28891. rcu_read_lock();
  28892. cpu = get_rps_cpu(skb->dev, skb, &rflow);
  28893. @@ -3575,13 +3623,13 @@ static int netif_rx_internal(struct sk_buff *skb)
  28894. ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
  28895. rcu_read_unlock();
  28896. - preempt_enable();
  28897. + migrate_enable();
  28898. } else
  28899. #endif
  28900. {
  28901. unsigned int qtail;
  28902. - ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
  28903. - put_cpu();
  28904. + ret = enqueue_to_backlog(skb, get_cpu_light(), &qtail);
  28905. + put_cpu_light();
  28906. }
  28907. return ret;
  28908. }
  28909. @@ -3615,16 +3663,44 @@ int netif_rx_ni(struct sk_buff *skb)
  28910. trace_netif_rx_ni_entry(skb);
  28911. - preempt_disable();
  28912. + local_bh_disable();
  28913. err = netif_rx_internal(skb);
  28914. - if (local_softirq_pending())
  28915. - do_softirq();
  28916. - preempt_enable();
  28917. + local_bh_enable();
  28918. return err;
  28919. }
  28920. EXPORT_SYMBOL(netif_rx_ni);
  28921. +#ifdef CONFIG_PREEMPT_RT_FULL
  28922. +/*
  28923. + * RT runs ksoftirqd as a real time thread and the root_lock is a
  28924. + * "sleeping spinlock". If the trylock fails then we can go into an
  28925. + * infinite loop when ksoftirqd preempted the task which actually
  28926. + * holds the lock, because we requeue q and raise NET_TX softirq
  28927. + * causing ksoftirqd to loop forever.
  28928. + *
  28929. + * It's safe to use spin_lock on RT here as softirqs run in thread
  28930. + * context and cannot deadlock against the thread which is holding
  28931. + * root_lock.
  28932. + *
  28933. + * On !RT the trylock might fail, but there we bail out from the
  28934. + * softirq loop after 10 attempts which we can't do on RT. And the
  28935. + * task holding root_lock cannot be preempted, so the only downside of
  28936. + * that trylock is that we need 10 loops to decide that we should have
  28937. + * given up in the first one :)
  28938. + */
  28939. +static inline int take_root_lock(spinlock_t *lock)
  28940. +{
  28941. + spin_lock(lock);
  28942. + return 1;
  28943. +}
  28944. +#else
  28945. +static inline int take_root_lock(spinlock_t *lock)
  28946. +{
  28947. + return spin_trylock(lock);
  28948. +}
  28949. +#endif
  28950. +
  28951. static void net_tx_action(struct softirq_action *h)
  28952. {
  28953. struct softnet_data *sd = this_cpu_ptr(&softnet_data);
  28954. @@ -3666,7 +3742,7 @@ static void net_tx_action(struct softirq_action *h)
  28955. head = head->next_sched;
  28956. root_lock = qdisc_lock(q);
  28957. - if (spin_trylock(root_lock)) {
  28958. + if (take_root_lock(root_lock)) {
  28959. smp_mb__before_atomic();
  28960. clear_bit(__QDISC_STATE_SCHED,
  28961. &q->state);
  28962. @@ -4088,7 +4164,7 @@ static void flush_backlog(void *arg)
  28963. skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
  28964. if (skb->dev == dev) {
  28965. __skb_unlink(skb, &sd->input_pkt_queue);
  28966. - kfree_skb(skb);
  28967. + __skb_queue_tail(&sd->tofree_queue, skb);
  28968. input_queue_head_incr(sd);
  28969. }
  28970. }
  28971. @@ -4097,10 +4173,13 @@ static void flush_backlog(void *arg)
  28972. skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
  28973. if (skb->dev == dev) {
  28974. __skb_unlink(skb, &sd->process_queue);
  28975. - kfree_skb(skb);
  28976. + __skb_queue_tail(&sd->tofree_queue, skb);
  28977. input_queue_head_incr(sd);
  28978. }
  28979. }
  28980. +
  28981. + if (!skb_queue_empty(&sd->tofree_queue))
  28982. + raise_softirq_irqoff(NET_RX_SOFTIRQ);
  28983. }
  28984. static int napi_gro_complete(struct sk_buff *skb)
  28985. @@ -4557,6 +4636,7 @@ static void net_rps_action_and_irq_enable(struct softnet_data *sd)
  28986. sd->rps_ipi_list = NULL;
  28987. local_irq_enable();
  28988. + preempt_check_resched_rt();
  28989. /* Send pending IPI's to kick RPS processing on remote cpus. */
  28990. while (remsd) {
  28991. @@ -4570,6 +4650,7 @@ static void net_rps_action_and_irq_enable(struct softnet_data *sd)
  28992. } else
  28993. #endif
  28994. local_irq_enable();
  28995. + preempt_check_resched_rt();
  28996. }
  28997. static bool sd_has_rps_ipi_waiting(struct softnet_data *sd)
  28998. @@ -4651,9 +4732,11 @@ void __napi_schedule(struct napi_struct *n)
  28999. local_irq_save(flags);
  29000. ____napi_schedule(this_cpu_ptr(&softnet_data), n);
  29001. local_irq_restore(flags);
  29002. + preempt_check_resched_rt();
  29003. }
  29004. EXPORT_SYMBOL(__napi_schedule);
  29005. +#ifndef CONFIG_PREEMPT_RT_FULL
  29006. /**
  29007. * __napi_schedule_irqoff - schedule for receive
  29008. * @n: entry to schedule
  29009. @@ -4665,6 +4748,7 @@ void __napi_schedule_irqoff(struct napi_struct *n)
  29010. ____napi_schedule(this_cpu_ptr(&softnet_data), n);
  29011. }
  29012. EXPORT_SYMBOL(__napi_schedule_irqoff);
  29013. +#endif
  29014. void __napi_complete(struct napi_struct *n)
  29015. {
  29016. @@ -4891,13 +4975,21 @@ static void net_rx_action(struct softirq_action *h)
  29017. struct softnet_data *sd = this_cpu_ptr(&softnet_data);
  29018. unsigned long time_limit = jiffies + 2;
  29019. int budget = netdev_budget;
  29020. + struct sk_buff_head tofree_q;
  29021. + struct sk_buff *skb;
  29022. LIST_HEAD(list);
  29023. LIST_HEAD(repoll);
  29024. + __skb_queue_head_init(&tofree_q);
  29025. +
  29026. local_irq_disable();
  29027. + skb_queue_splice_init(&sd->tofree_queue, &tofree_q);
  29028. list_splice_init(&sd->poll_list, &list);
  29029. local_irq_enable();
  29030. + while ((skb = __skb_dequeue(&tofree_q)))
  29031. + kfree_skb(skb);
  29032. +
  29033. for (;;) {
  29034. struct napi_struct *n;
  29035. @@ -4927,7 +5019,7 @@ static void net_rx_action(struct softirq_action *h)
  29036. list_splice_tail(&repoll, &list);
  29037. list_splice(&list, &sd->poll_list);
  29038. if (!list_empty(&sd->poll_list))
  29039. - __raise_softirq_irqoff(NET_RX_SOFTIRQ);
  29040. + __raise_softirq_irqoff_ksoft(NET_RX_SOFTIRQ);
  29041. net_rps_action_and_irq_enable(sd);
  29042. }
  29043. @@ -7266,7 +7358,7 @@ EXPORT_SYMBOL(free_netdev);
  29044. void synchronize_net(void)
  29045. {
  29046. might_sleep();
  29047. - if (rtnl_is_locked())
  29048. + if (rtnl_is_locked() && !IS_ENABLED(CONFIG_PREEMPT_RT_FULL))
  29049. synchronize_rcu_expedited();
  29050. else
  29051. synchronize_rcu();
  29052. @@ -7507,16 +7599,20 @@ static int dev_cpu_callback(struct notifier_block *nfb,
  29053. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  29054. local_irq_enable();
  29055. + preempt_check_resched_rt();
  29056. /* Process offline CPU's input_pkt_queue */
  29057. while ((skb = __skb_dequeue(&oldsd->process_queue))) {
  29058. netif_rx_ni(skb);
  29059. input_queue_head_incr(oldsd);
  29060. }
  29061. - while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) {
  29062. + while ((skb = __skb_dequeue(&oldsd->input_pkt_queue))) {
  29063. netif_rx_ni(skb);
  29064. input_queue_head_incr(oldsd);
  29065. }
  29066. + while ((skb = __skb_dequeue(&oldsd->tofree_queue))) {
  29067. + kfree_skb(skb);
  29068. + }
  29069. return NOTIFY_OK;
  29070. }
  29071. @@ -7818,8 +7914,9 @@ static int __init net_dev_init(void)
  29072. for_each_possible_cpu(i) {
  29073. struct softnet_data *sd = &per_cpu(softnet_data, i);
  29074. - skb_queue_head_init(&sd->input_pkt_queue);
  29075. - skb_queue_head_init(&sd->process_queue);
  29076. + skb_queue_head_init_raw(&sd->input_pkt_queue);
  29077. + skb_queue_head_init_raw(&sd->process_queue);
  29078. + skb_queue_head_init_raw(&sd->tofree_queue);
  29079. INIT_LIST_HEAD(&sd->poll_list);
  29080. sd->output_queue_tailp = &sd->output_queue;
  29081. #ifdef CONFIG_RPS
  29082. diff --git a/net/core/skbuff.c b/net/core/skbuff.c
  29083. index 73dfd7729bc9..62f62810cddd 100644
  29084. --- a/net/core/skbuff.c
  29085. +++ b/net/core/skbuff.c
  29086. @@ -63,6 +63,7 @@
  29087. #include <linux/errqueue.h>
  29088. #include <linux/prefetch.h>
  29089. #include <linux/if_vlan.h>
  29090. +#include <linux/locallock.h>
  29091. #include <net/protocol.h>
  29092. #include <net/dst.h>
  29093. @@ -351,6 +352,8 @@ EXPORT_SYMBOL(build_skb);
  29094. static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
  29095. static DEFINE_PER_CPU(struct page_frag_cache, napi_alloc_cache);
  29096. +static DEFINE_LOCAL_IRQ_LOCK(netdev_alloc_lock);
  29097. +static DEFINE_LOCAL_IRQ_LOCK(napi_alloc_cache_lock);
  29098. static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
  29099. {
  29100. @@ -358,10 +361,10 @@ static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
  29101. unsigned long flags;
  29102. void *data;
  29103. - local_irq_save(flags);
  29104. + local_lock_irqsave(netdev_alloc_lock, flags);
  29105. nc = this_cpu_ptr(&netdev_alloc_cache);
  29106. data = __alloc_page_frag(nc, fragsz, gfp_mask);
  29107. - local_irq_restore(flags);
  29108. + local_unlock_irqrestore(netdev_alloc_lock, flags);
  29109. return data;
  29110. }
  29111. @@ -380,9 +383,13 @@ EXPORT_SYMBOL(netdev_alloc_frag);
  29112. static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
  29113. {
  29114. - struct page_frag_cache *nc = this_cpu_ptr(&napi_alloc_cache);
  29115. + struct page_frag_cache *nc;
  29116. + void *data;
  29117. - return __alloc_page_frag(nc, fragsz, gfp_mask);
  29118. + nc = &get_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  29119. + data = __alloc_page_frag(nc, fragsz, gfp_mask);
  29120. + put_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  29121. + return data;
  29122. }
  29123. void *napi_alloc_frag(unsigned int fragsz)
  29124. @@ -429,13 +436,13 @@ struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
  29125. if (sk_memalloc_socks())
  29126. gfp_mask |= __GFP_MEMALLOC;
  29127. - local_irq_save(flags);
  29128. + local_lock_irqsave(netdev_alloc_lock, flags);
  29129. nc = this_cpu_ptr(&netdev_alloc_cache);
  29130. data = __alloc_page_frag(nc, len, gfp_mask);
  29131. pfmemalloc = nc->pfmemalloc;
  29132. - local_irq_restore(flags);
  29133. + local_unlock_irqrestore(netdev_alloc_lock, flags);
  29134. if (unlikely(!data))
  29135. return NULL;
  29136. @@ -476,9 +483,10 @@ EXPORT_SYMBOL(__netdev_alloc_skb);
  29137. struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
  29138. gfp_t gfp_mask)
  29139. {
  29140. - struct page_frag_cache *nc = this_cpu_ptr(&napi_alloc_cache);
  29141. + struct page_frag_cache *nc;
  29142. struct sk_buff *skb;
  29143. void *data;
  29144. + bool pfmemalloc;
  29145. len += NET_SKB_PAD + NET_IP_ALIGN;
  29146. @@ -496,7 +504,11 @@ struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
  29147. if (sk_memalloc_socks())
  29148. gfp_mask |= __GFP_MEMALLOC;
  29149. + nc = &get_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  29150. data = __alloc_page_frag(nc, len, gfp_mask);
  29151. + pfmemalloc = nc->pfmemalloc;
  29152. + put_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  29153. +
  29154. if (unlikely(!data))
  29155. return NULL;
  29156. @@ -507,7 +519,7 @@ struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
  29157. }
  29158. /* use OR instead of assignment to avoid clearing of bits in mask */
  29159. - if (nc->pfmemalloc)
  29160. + if (pfmemalloc)
  29161. skb->pfmemalloc = 1;
  29162. skb->head_frag = 1;
  29163. diff --git a/net/core/sock.c b/net/core/sock.c
  29164. index 9c708a5fb751..823377a7e63a 100644
  29165. --- a/net/core/sock.c
  29166. +++ b/net/core/sock.c
  29167. @@ -2447,12 +2447,11 @@ void lock_sock_nested(struct sock *sk, int subclass)
  29168. if (sk->sk_lock.owned)
  29169. __lock_sock(sk);
  29170. sk->sk_lock.owned = 1;
  29171. - spin_unlock(&sk->sk_lock.slock);
  29172. + spin_unlock_bh(&sk->sk_lock.slock);
  29173. /*
  29174. * The sk_lock has mutex_lock() semantics here:
  29175. */
  29176. mutex_acquire(&sk->sk_lock.dep_map, subclass, 0, _RET_IP_);
  29177. - local_bh_enable();
  29178. }
  29179. EXPORT_SYMBOL(lock_sock_nested);
  29180. diff --git a/net/ipv4/icmp.c b/net/ipv4/icmp.c
  29181. index 36e26977c908..ff2593269089 100644
  29182. --- a/net/ipv4/icmp.c
  29183. +++ b/net/ipv4/icmp.c
  29184. @@ -69,6 +69,7 @@
  29185. #include <linux/jiffies.h>
  29186. #include <linux/kernel.h>
  29187. #include <linux/fcntl.h>
  29188. +#include <linux/sysrq.h>
  29189. #include <linux/socket.h>
  29190. #include <linux/in.h>
  29191. #include <linux/inet.h>
  29192. @@ -77,6 +78,7 @@
  29193. #include <linux/string.h>
  29194. #include <linux/netfilter_ipv4.h>
  29195. #include <linux/slab.h>
  29196. +#include <linux/locallock.h>
  29197. #include <net/snmp.h>
  29198. #include <net/ip.h>
  29199. #include <net/route.h>
  29200. @@ -204,6 +206,8 @@ static const struct icmp_control icmp_pointers[NR_ICMP_TYPES+1];
  29201. *
  29202. * On SMP we have one ICMP socket per-cpu.
  29203. */
  29204. +static DEFINE_LOCAL_IRQ_LOCK(icmp_sk_lock);
  29205. +
  29206. static struct sock *icmp_sk(struct net *net)
  29207. {
  29208. return *this_cpu_ptr(net->ipv4.icmp_sk);
  29209. @@ -215,12 +219,14 @@ static inline struct sock *icmp_xmit_lock(struct net *net)
  29210. local_bh_disable();
  29211. + local_lock(icmp_sk_lock);
  29212. sk = icmp_sk(net);
  29213. if (unlikely(!spin_trylock(&sk->sk_lock.slock))) {
  29214. /* This can happen if the output path signals a
  29215. * dst_link_failure() for an outgoing ICMP packet.
  29216. */
  29217. + local_unlock(icmp_sk_lock);
  29218. local_bh_enable();
  29219. return NULL;
  29220. }
  29221. @@ -230,6 +236,7 @@ static inline struct sock *icmp_xmit_lock(struct net *net)
  29222. static inline void icmp_xmit_unlock(struct sock *sk)
  29223. {
  29224. spin_unlock_bh(&sk->sk_lock.slock);
  29225. + local_unlock(icmp_sk_lock);
  29226. }
  29227. int sysctl_icmp_msgs_per_sec __read_mostly = 1000;
  29228. @@ -358,6 +365,7 @@ static void icmp_push_reply(struct icmp_bxm *icmp_param,
  29229. struct sock *sk;
  29230. struct sk_buff *skb;
  29231. + local_lock(icmp_sk_lock);
  29232. sk = icmp_sk(dev_net((*rt)->dst.dev));
  29233. if (ip_append_data(sk, fl4, icmp_glue_bits, icmp_param,
  29234. icmp_param->data_len+icmp_param->head_len,
  29235. @@ -380,6 +388,7 @@ static void icmp_push_reply(struct icmp_bxm *icmp_param,
  29236. skb->ip_summed = CHECKSUM_NONE;
  29237. ip_push_pending_frames(sk, fl4);
  29238. }
  29239. + local_unlock(icmp_sk_lock);
  29240. }
  29241. /*
  29242. @@ -891,6 +900,30 @@ static bool icmp_redirect(struct sk_buff *skb)
  29243. }
  29244. /*
  29245. + * 32bit and 64bit have different timestamp length, so we check for
  29246. + * the cookie at offset 20 and verify it is repeated at offset 50
  29247. + */
  29248. +#define CO_POS0 20
  29249. +#define CO_POS1 50
  29250. +#define CO_SIZE sizeof(int)
  29251. +#define ICMP_SYSRQ_SIZE 57
  29252. +
  29253. +/*
  29254. + * We got a ICMP_SYSRQ_SIZE sized ping request. Check for the cookie
  29255. + * pattern and if it matches send the next byte as a trigger to sysrq.
  29256. + */
  29257. +static void icmp_check_sysrq(struct net *net, struct sk_buff *skb)
  29258. +{
  29259. + int cookie = htonl(net->ipv4.sysctl_icmp_echo_sysrq);
  29260. + char *p = skb->data;
  29261. +
  29262. + if (!memcmp(&cookie, p + CO_POS0, CO_SIZE) &&
  29263. + !memcmp(&cookie, p + CO_POS1, CO_SIZE) &&
  29264. + p[CO_POS0 + CO_SIZE] == p[CO_POS1 + CO_SIZE])
  29265. + handle_sysrq(p[CO_POS0 + CO_SIZE]);
  29266. +}
  29267. +
  29268. +/*
  29269. * Handle ICMP_ECHO ("ping") requests.
  29270. *
  29271. * RFC 1122: 3.2.2.6 MUST have an echo server that answers ICMP echo
  29272. @@ -917,6 +950,11 @@ static bool icmp_echo(struct sk_buff *skb)
  29273. icmp_param.data_len = skb->len;
  29274. icmp_param.head_len = sizeof(struct icmphdr);
  29275. icmp_reply(&icmp_param, skb);
  29276. +
  29277. + if (skb->len == ICMP_SYSRQ_SIZE &&
  29278. + net->ipv4.sysctl_icmp_echo_sysrq) {
  29279. + icmp_check_sysrq(net, skb);
  29280. + }
  29281. }
  29282. /* should there be an ICMP stat for ignored echos? */
  29283. return true;
  29284. diff --git a/net/ipv4/sysctl_net_ipv4.c b/net/ipv4/sysctl_net_ipv4.c
  29285. index 70fb352e317f..1bcd436709a4 100644
  29286. --- a/net/ipv4/sysctl_net_ipv4.c
  29287. +++ b/net/ipv4/sysctl_net_ipv4.c
  29288. @@ -818,6 +818,13 @@ static struct ctl_table ipv4_net_table[] = {
  29289. .proc_handler = proc_dointvec
  29290. },
  29291. {
  29292. + .procname = "icmp_echo_sysrq",
  29293. + .data = &init_net.ipv4.sysctl_icmp_echo_sysrq,
  29294. + .maxlen = sizeof(int),
  29295. + .mode = 0644,
  29296. + .proc_handler = proc_dointvec
  29297. + },
  29298. + {
  29299. .procname = "icmp_ignore_bogus_error_responses",
  29300. .data = &init_net.ipv4.sysctl_icmp_ignore_bogus_error_responses,
  29301. .maxlen = sizeof(int),
  29302. diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c
  29303. index 198fc2314c82..6d84e7c8c8a9 100644
  29304. --- a/net/ipv4/tcp_ipv4.c
  29305. +++ b/net/ipv4/tcp_ipv4.c
  29306. @@ -62,6 +62,7 @@
  29307. #include <linux/init.h>
  29308. #include <linux/times.h>
  29309. #include <linux/slab.h>
  29310. +#include <linux/locallock.h>
  29311. #include <net/net_namespace.h>
  29312. #include <net/icmp.h>
  29313. @@ -570,6 +571,7 @@ void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb)
  29314. }
  29315. EXPORT_SYMBOL(tcp_v4_send_check);
  29316. +static DEFINE_LOCAL_IRQ_LOCK(tcp_sk_lock);
  29317. /*
  29318. * This routine will send an RST to the other tcp.
  29319. *
  29320. @@ -691,10 +693,13 @@ static void tcp_v4_send_reset(const struct sock *sk, struct sk_buff *skb)
  29321. arg.bound_dev_if = sk->sk_bound_dev_if;
  29322. arg.tos = ip_hdr(skb)->tos;
  29323. +
  29324. + local_lock(tcp_sk_lock);
  29325. ip_send_unicast_reply(*this_cpu_ptr(net->ipv4.tcp_sk),
  29326. skb, &TCP_SKB_CB(skb)->header.h4.opt,
  29327. ip_hdr(skb)->saddr, ip_hdr(skb)->daddr,
  29328. &arg, arg.iov[0].iov_len);
  29329. + local_unlock(tcp_sk_lock);
  29330. TCP_INC_STATS_BH(net, TCP_MIB_OUTSEGS);
  29331. TCP_INC_STATS_BH(net, TCP_MIB_OUTRSTS);
  29332. @@ -776,10 +781,12 @@ static void tcp_v4_send_ack(struct net *net,
  29333. if (oif)
  29334. arg.bound_dev_if = oif;
  29335. arg.tos = tos;
  29336. + local_lock(tcp_sk_lock);
  29337. ip_send_unicast_reply(*this_cpu_ptr(net->ipv4.tcp_sk),
  29338. skb, &TCP_SKB_CB(skb)->header.h4.opt,
  29339. ip_hdr(skb)->saddr, ip_hdr(skb)->daddr,
  29340. &arg, arg.iov[0].iov_len);
  29341. + local_unlock(tcp_sk_lock);
  29342. TCP_INC_STATS_BH(net, TCP_MIB_OUTSEGS);
  29343. }
  29344. diff --git a/net/mac80211/rx.c b/net/mac80211/rx.c
  29345. index 9f0915f72702..28d234c03cb5 100644
  29346. --- a/net/mac80211/rx.c
  29347. +++ b/net/mac80211/rx.c
  29348. @@ -3601,7 +3601,7 @@ void ieee80211_rx_napi(struct ieee80211_hw *hw, struct sk_buff *skb,
  29349. struct ieee80211_supported_band *sband;
  29350. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  29351. - WARN_ON_ONCE(softirq_count() == 0);
  29352. + WARN_ON_ONCE_NONRT(softirq_count() == 0);
  29353. if (WARN_ON(status->band >= IEEE80211_NUM_BANDS))
  29354. goto drop;
  29355. diff --git a/net/netfilter/core.c b/net/netfilter/core.c
  29356. index f39276d1c2d7..10880c89d62f 100644
  29357. --- a/net/netfilter/core.c
  29358. +++ b/net/netfilter/core.c
  29359. @@ -22,11 +22,17 @@
  29360. #include <linux/proc_fs.h>
  29361. #include <linux/mutex.h>
  29362. #include <linux/slab.h>
  29363. +#include <linux/locallock.h>
  29364. #include <net/net_namespace.h>
  29365. #include <net/sock.h>
  29366. #include "nf_internals.h"
  29367. +#ifdef CONFIG_PREEMPT_RT_BASE
  29368. +DEFINE_LOCAL_IRQ_LOCK(xt_write_lock);
  29369. +EXPORT_PER_CPU_SYMBOL(xt_write_lock);
  29370. +#endif
  29371. +
  29372. static DEFINE_MUTEX(afinfo_mutex);
  29373. const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO] __read_mostly;
  29374. diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c
  29375. index f8d6a0ca9c03..f6be45510cf6 100644
  29376. --- a/net/packet/af_packet.c
  29377. +++ b/net/packet/af_packet.c
  29378. @@ -63,6 +63,7 @@
  29379. #include <linux/if_packet.h>
  29380. #include <linux/wireless.h>
  29381. #include <linux/kernel.h>
  29382. +#include <linux/delay.h>
  29383. #include <linux/kmod.h>
  29384. #include <linux/slab.h>
  29385. #include <linux/vmalloc.h>
  29386. @@ -694,7 +695,7 @@ static void prb_retire_rx_blk_timer_expired(unsigned long data)
  29387. if (BLOCK_NUM_PKTS(pbd)) {
  29388. while (atomic_read(&pkc->blk_fill_in_prog)) {
  29389. /* Waiting for skb_copy_bits to finish... */
  29390. - cpu_relax();
  29391. + cpu_chill();
  29392. }
  29393. }
  29394. @@ -956,7 +957,7 @@ static void prb_retire_current_block(struct tpacket_kbdq_core *pkc,
  29395. if (!(status & TP_STATUS_BLK_TMO)) {
  29396. while (atomic_read(&pkc->blk_fill_in_prog)) {
  29397. /* Waiting for skb_copy_bits to finish... */
  29398. - cpu_relax();
  29399. + cpu_chill();
  29400. }
  29401. }
  29402. prb_close_block(pkc, pbd, po, status);
  29403. diff --git a/net/rds/ib_rdma.c b/net/rds/ib_rdma.c
  29404. index a2340748ec86..19123a97b354 100644
  29405. --- a/net/rds/ib_rdma.c
  29406. +++ b/net/rds/ib_rdma.c
  29407. @@ -34,6 +34,7 @@
  29408. #include <linux/slab.h>
  29409. #include <linux/rculist.h>
  29410. #include <linux/llist.h>
  29411. +#include <linux/delay.h>
  29412. #include "rds.h"
  29413. #include "ib.h"
  29414. @@ -313,7 +314,7 @@ static inline void wait_clean_list_grace(void)
  29415. for_each_online_cpu(cpu) {
  29416. flag = &per_cpu(clean_list_grace, cpu);
  29417. while (test_bit(CLEAN_LIST_BUSY_BIT, flag))
  29418. - cpu_relax();
  29419. + cpu_chill();
  29420. }
  29421. }
  29422. diff --git a/net/sched/sch_generic.c b/net/sched/sch_generic.c
  29423. index aa4725038f94..00b81cab28f3 100644
  29424. --- a/net/sched/sch_generic.c
  29425. +++ b/net/sched/sch_generic.c
  29426. @@ -893,7 +893,7 @@ void dev_deactivate_many(struct list_head *head)
  29427. /* Wait for outstanding qdisc_run calls. */
  29428. list_for_each_entry(dev, head, close_list)
  29429. while (some_qdisc_is_busy(dev))
  29430. - yield();
  29431. + msleep(1);
  29432. }
  29433. void dev_deactivate(struct net_device *dev)
  29434. diff --git a/net/sunrpc/svc_xprt.c b/net/sunrpc/svc_xprt.c
  29435. index a6cbb2104667..5b69bb580617 100644
  29436. --- a/net/sunrpc/svc_xprt.c
  29437. +++ b/net/sunrpc/svc_xprt.c
  29438. @@ -340,7 +340,7 @@ void svc_xprt_do_enqueue(struct svc_xprt *xprt)
  29439. goto out;
  29440. }
  29441. - cpu = get_cpu();
  29442. + cpu = get_cpu_light();
  29443. pool = svc_pool_for_cpu(xprt->xpt_server, cpu);
  29444. atomic_long_inc(&pool->sp_stats.packets);
  29445. @@ -376,7 +376,7 @@ redo_search:
  29446. atomic_long_inc(&pool->sp_stats.threads_woken);
  29447. wake_up_process(rqstp->rq_task);
  29448. - put_cpu();
  29449. + put_cpu_light();
  29450. goto out;
  29451. }
  29452. rcu_read_unlock();
  29453. @@ -397,7 +397,7 @@ redo_search:
  29454. goto redo_search;
  29455. }
  29456. rqstp = NULL;
  29457. - put_cpu();
  29458. + put_cpu_light();
  29459. out:
  29460. trace_svc_xprt_do_enqueue(xprt, rqstp);
  29461. }
  29462. diff --git a/scripts/mkcompile_h b/scripts/mkcompile_h
  29463. index 6fdc97ef6023..523e0420d7f0 100755
  29464. --- a/scripts/mkcompile_h
  29465. +++ b/scripts/mkcompile_h
  29466. @@ -4,7 +4,8 @@ TARGET=$1
  29467. ARCH=$2
  29468. SMP=$3
  29469. PREEMPT=$4
  29470. -CC=$5
  29471. +RT=$5
  29472. +CC=$6
  29473. vecho() { [ "${quiet}" = "silent_" ] || echo "$@" ; }
  29474. @@ -57,6 +58,7 @@ UTS_VERSION="#$VERSION"
  29475. CONFIG_FLAGS=""
  29476. if [ -n "$SMP" ] ; then CONFIG_FLAGS="SMP"; fi
  29477. if [ -n "$PREEMPT" ] ; then CONFIG_FLAGS="$CONFIG_FLAGS PREEMPT"; fi
  29478. +if [ -n "$RT" ] ; then CONFIG_FLAGS="$CONFIG_FLAGS RT"; fi
  29479. UTS_VERSION="$UTS_VERSION $CONFIG_FLAGS $TIMESTAMP"
  29480. # Truncate to maximum length
  29481. diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
  29482. index 4ba64fd49759..34e50186885d 100644
  29483. --- a/sound/core/pcm_native.c
  29484. +++ b/sound/core/pcm_native.c
  29485. @@ -135,7 +135,7 @@ EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock);
  29486. void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
  29487. {
  29488. if (!substream->pcm->nonatomic)
  29489. - local_irq_disable();
  29490. + local_irq_disable_nort();
  29491. snd_pcm_stream_lock(substream);
  29492. }
  29493. EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
  29494. @@ -150,7 +150,7 @@ void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream)
  29495. {
  29496. snd_pcm_stream_unlock(substream);
  29497. if (!substream->pcm->nonatomic)
  29498. - local_irq_enable();
  29499. + local_irq_enable_nort();
  29500. }
  29501. EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
  29502. @@ -158,7 +158,7 @@ unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream)
  29503. {
  29504. unsigned long flags = 0;
  29505. if (!substream->pcm->nonatomic)
  29506. - local_irq_save(flags);
  29507. + local_irq_save_nort(flags);
  29508. snd_pcm_stream_lock(substream);
  29509. return flags;
  29510. }
  29511. @@ -176,7 +176,7 @@ void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
  29512. {
  29513. snd_pcm_stream_unlock(substream);
  29514. if (!substream->pcm->nonatomic)
  29515. - local_irq_restore(flags);
  29516. + local_irq_restore_nort(flags);
  29517. }
  29518. EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
  29519. diff --git a/virt/kvm/async_pf.c b/virt/kvm/async_pf.c
  29520. index 4f70d12e392d..9378d0919ed8 100644
  29521. --- a/virt/kvm/async_pf.c
  29522. +++ b/virt/kvm/async_pf.c
  29523. @@ -98,8 +98,8 @@ static void async_pf_execute(struct work_struct *work)
  29524. * This memory barrier pairs with prepare_to_wait's set_current_state()
  29525. */
  29526. smp_mb();
  29527. - if (waitqueue_active(&vcpu->wq))
  29528. - wake_up_interruptible(&vcpu->wq);
  29529. + if (swait_active(&vcpu->wq))
  29530. + swake_up(&vcpu->wq);
  29531. mmput(mm);
  29532. kvm_put_kvm(vcpu->kvm);
  29533. diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
  29534. index cb092bd9965b..133e7fa2ec8b 100644
  29535. --- a/virt/kvm/kvm_main.c
  29536. +++ b/virt/kvm/kvm_main.c
  29537. @@ -228,8 +228,7 @@ int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
  29538. vcpu->kvm = kvm;
  29539. vcpu->vcpu_id = id;
  29540. vcpu->pid = NULL;
  29541. - vcpu->halt_poll_ns = 0;
  29542. - init_waitqueue_head(&vcpu->wq);
  29543. + init_swait_queue_head(&vcpu->wq);
  29544. kvm_async_pf_vcpu_init(vcpu);
  29545. vcpu->pre_pcpu = -1;
  29546. @@ -2008,7 +2007,7 @@ static int kvm_vcpu_check_block(struct kvm_vcpu *vcpu)
  29547. void kvm_vcpu_block(struct kvm_vcpu *vcpu)
  29548. {
  29549. ktime_t start, cur;
  29550. - DEFINE_WAIT(wait);
  29551. + DECLARE_SWAITQUEUE(wait);
  29552. bool waited = false;
  29553. u64 block_ns;
  29554. @@ -2033,7 +2032,7 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu)
  29555. kvm_arch_vcpu_blocking(vcpu);
  29556. for (;;) {
  29557. - prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
  29558. + prepare_to_swait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
  29559. if (kvm_vcpu_check_block(vcpu) < 0)
  29560. break;
  29561. @@ -2042,7 +2041,7 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu)
  29562. schedule();
  29563. }
  29564. - finish_wait(&vcpu->wq, &wait);
  29565. + finish_swait(&vcpu->wq, &wait);
  29566. cur = ktime_get();
  29567. kvm_arch_vcpu_unblocking(vcpu);
  29568. @@ -2074,11 +2073,11 @@ void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
  29569. {
  29570. int me;
  29571. int cpu = vcpu->cpu;
  29572. - wait_queue_head_t *wqp;
  29573. + struct swait_queue_head *wqp;
  29574. wqp = kvm_arch_vcpu_wq(vcpu);
  29575. - if (waitqueue_active(wqp)) {
  29576. - wake_up_interruptible(wqp);
  29577. + if (swait_active(wqp)) {
  29578. + swake_up(wqp);
  29579. ++vcpu->stat.halt_wakeup;
  29580. }
  29581. @@ -2179,7 +2178,7 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *me)
  29582. continue;
  29583. if (vcpu == me)
  29584. continue;
  29585. - if (waitqueue_active(&vcpu->wq) && !kvm_arch_vcpu_runnable(vcpu))
  29586. + if (swait_active(&vcpu->wq) && !kvm_arch_vcpu_runnable(vcpu))
  29587. continue;
  29588. if (!kvm_vcpu_eligible_for_directed_yield(vcpu))
  29589. continue;